Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 1 | //===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===// |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Owen Anderson and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass transforms loops by placing phi nodes at the end of the loops for |
| 11 | // all values that are live across the loop boundary. For example, it turns |
| 12 | // the left into the right code: |
| 13 | // |
| 14 | // for (...) for (...) |
| 15 | // if (c) if(c) |
| 16 | // X1 = ... X1 = ... |
| 17 | // else else |
| 18 | // X2 = ... X2 = ... |
| 19 | // X3 = phi(X1, X2) X3 = phi(X1, X2) |
| 20 | // ... = X3 + 4 X4 = phi(X3) |
| 21 | // ... = X4 + 4 |
| 22 | // |
| 23 | // This is still valid LLVM; the extra phi nodes are purely redundant, and will |
| 24 | // be trivially eliminated by InstCombine. The major benefit of this |
| 25 | // transformation is that it makes many other loop optimizations, such as |
| 26 | // LoopUnswitching, simpler. |
| 27 | // |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Scalar.h" |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
| 32 | #include "llvm/Function.h" |
| 33 | #include "llvm/Instructions.h" |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame^] | 34 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/Dominators.h" |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/LoopInfo.h" |
| 37 | #include "llvm/Support/CFG.h" |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 39 | #include <vector> |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | namespace { |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame^] | 44 | static Statistic<> NumLCSSA("lcssa", "Number of times LCSSA was applied"); |
| 45 | |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 46 | class LCSSA : public FunctionPass { |
| 47 | public: |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame^] | 48 | |
| 49 | |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 50 | LoopInfo *LI; // Loop information |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 51 | DominatorTree *DT; // Dominator Tree for the current Loop... |
| 52 | DominanceFrontier *DF; // Current Dominance Frontier |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 53 | |
| 54 | virtual bool runOnFunction(Function &F); |
Owen Anderson | 6e047ab | 2006-05-26 21:19:17 +0000 | [diff] [blame] | 55 | bool visitSubloop(Loop* L); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 56 | |
| 57 | /// This transformation requires natural loop information & requires that |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame^] | 58 | /// loop preheaders be inserted into the CFG. It maintains both of these, |
| 59 | /// as well as the CFG. It also requires dominator information. |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 60 | /// |
| 61 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 62 | AU.setPreservesCFG(); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 63 | AU.addRequiredID(LoopSimplifyID); |
| 64 | AU.addPreservedID(LoopSimplifyID); |
| 65 | AU.addRequired<LoopInfo>(); |
| 66 | AU.addPreserved<LoopInfo>(); |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 67 | AU.addRequired<DominatorTree>(); // Not sure if this one will actually |
| 68 | // be needed. |
| 69 | AU.addRequired<DominanceFrontier>(); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 70 | } |
| 71 | private: |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 72 | std::set<Instruction*> getLoopValuesUsedOutsideLoop(Loop *L, |
| 73 | std::vector<BasicBlock*> LoopBlocks); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | RegisterOpt<LCSSA> X("lcssa", "Loop-Closed SSA Form Pass"); |
| 77 | } |
| 78 | |
| 79 | FunctionPass *llvm::createLCSSAPass() { return new LCSSA(); } |
| 80 | |
| 81 | bool LCSSA::runOnFunction(Function &F) { |
| 82 | bool changed = false; |
| 83 | LI = &getAnalysis<LoopInfo>(); |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 84 | DF = &getAnalysis<DominanceFrontier>(); |
| 85 | DT = &getAnalysis<DominatorTree>(); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 86 | |
| 87 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) { |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 88 | changed |= visitSubloop(*I); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return changed; |
| 92 | } |
| 93 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 94 | bool LCSSA::visitSubloop(Loop* L) { |
| 95 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 96 | visitSubloop(*I); |
| 97 | |
| 98 | // Speed up queries by creating a sorted list of blocks |
| 99 | std::vector<BasicBlock*> LoopBlocks(L->block_begin(), L->block_end()); |
| 100 | std::sort(LoopBlocks.begin(), LoopBlocks.end()); |
| 101 | |
| 102 | std::set<Instruction*> AffectedValues = getLoopValuesUsedOutsideLoop(L, |
| 103 | LoopBlocks); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 104 | |
| 105 | std::vector<BasicBlock*> exitBlocks; |
| 106 | L->getExitBlocks(exitBlocks); |
| 107 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 108 | for (std::set<Instruction*>::iterator I = AffectedValues.begin(), |
| 109 | E = AffectedValues.end(); I != E; ++I) { |
Owen Anderson | b4e1699 | 2006-05-27 00:31:37 +0000 | [diff] [blame^] | 110 | ++NumLCSSA; // We are applying the transformation |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 111 | for (std::vector<BasicBlock*>::iterator BBI = exitBlocks.begin(), |
| 112 | BBE = exitBlocks.end(); BBI != BBE; ++BBI) { |
| 113 | PHINode *phi = new PHINode((*I)->getType(), "lcssa"); |
| 114 | (*BBI)->getInstList().insert((*BBI)->front(), phi); |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 115 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 116 | for (pred_iterator PI = pred_begin(*BBI), PE = pred_end(*BBI); PI != PE; |
| 117 | ++PI) |
| 118 | phi->addIncoming(*I, *PI); |
| 119 | } |
| 120 | |
| 121 | for (Value::use_iterator UI = (*I)->use_begin(), UE = (*I)->use_end(); |
| 122 | UI != UE; ++UI) { |
| 123 | BasicBlock *UserBB = cast<Instruction>(*UI)->getParent(); |
| 124 | if (!std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), UserBB)) |
| 125 | ; // FIXME: This should update the SSA form. |
| 126 | } |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 129 | return true; // FIXME: Should be more intelligent in our return value. |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 132 | /// getLoopValuesUsedOutsideLoop - Return any values defined in the loop that |
| 133 | /// are used by instructions outside of it. |
| 134 | std::set<Instruction*> LCSSA::getLoopValuesUsedOutsideLoop(Loop *L, |
| 135 | std::vector<BasicBlock*> LoopBlocks) { |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 136 | |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 137 | std::set<Instruction*> AffectedValues; |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 138 | for (Loop::block_iterator BB = L->block_begin(), E = L->block_end(); |
| 139 | BB != E; ++BB) { |
| 140 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I) |
| 141 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 142 | ++UI) { |
| 143 | BasicBlock *UserBB = cast<Instruction>(*UI)->getParent(); |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 144 | if (!std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), UserBB)) |
Owen Anderson | 8eca891 | 2006-05-26 13:58:26 +0000 | [diff] [blame] | 145 | AffectedValues.insert(I); |
| 146 | } |
| 147 | } |
| 148 | return AffectedValues; |
Owen Anderson | f3dd3e2 | 2006-05-26 21:11:53 +0000 | [diff] [blame] | 149 | } |