1 //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs lightweight instruction simplification on loop bodies.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "loop-instsimplify"
15 #include "llvm/Analysis/LoopPass.h"
16 #include "llvm/Analysis/Dominators.h"
17 #include "llvm/Analysis/InstructionSimplify.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/Transforms/Utils/Local.h"
21 #include "llvm/ADT/Statistic.h"
24 STATISTIC(NumSimplified, "Number of redundant instructions simplified");
27 class LoopInstSimplify : public LoopPass {
29 static char ID; // Pass ID, replacement for typeid
30 LoopInstSimplify() : LoopPass(ID) {
31 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
34 bool runOnLoop(Loop*, LPPassManager&);
36 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
38 AU.addRequired<DominatorTree>();
39 AU.addPreserved<DominatorTree>();
40 AU.addRequired<LoopInfo>();
41 AU.addPreserved<LoopInfo>();
42 AU.addPreservedID(LCSSAID);
47 char LoopInstSimplify::ID = 0;
48 INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
49 "Simplify instructions in loops", false, false)
50 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
51 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
52 INITIALIZE_PASS_DEPENDENCY(LCSSA)
53 INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
54 "Simplify instructions in loops", false, false)
56 Pass* llvm::createLoopInstSimplifyPass() {
57 return new LoopInstSimplify();
60 bool LoopInstSimplify::runOnLoop(Loop* L, LPPassManager& LPM) {
61 DominatorTree* DT = &getAnalysis<DominatorTree>();
62 const LoopInfo* LI = &getAnalysis<LoopInfo>();
63 const TargetData* TD = getAnalysisIfAvailable<TargetData>();
70 SmallPtrSet<BasicBlock*, 32> Visited;
71 SmallVector<BasicBlock*, 32> VisitStack;
73 VisitStack.push_back(L->getHeader());
75 while (!VisitStack.empty()) {
76 BasicBlock* BB = VisitStack.back();
77 VisitStack.pop_back();
79 if (Visited.count(BB))
83 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
84 Instruction* I = BI++;
85 // Don't bother simplifying unused instructions.
86 if (!I->use_empty()) {
87 if (Value* V = SimplifyInstruction(I, TD, DT)) {
88 I->replaceAllUsesWith(V);
93 LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
95 Changed |= LocalChanged;
97 DomTreeNode* Node = DT->getNode(BB);
98 const std::vector<DomTreeNode*>& Children = Node->getChildren();
99 for (unsigned i = 0; i < Children.size(); ++i) {
100 // Only visit children that are in the same loop.
101 BasicBlock* ChildBB = Children[i]->getBlock();
102 if (!Visited.count(ChildBB) && LI->getLoopFor(ChildBB) == L)
103 VisitStack.push_back(ChildBB);
106 } while (LocalChanged);
108 // Nothing that SimplifyInstruction() does should invalidate LCSSA form.
109 assert(L->isLCSSAForm(*DT));