1 //===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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:
14 // for (...) for (...)
19 // X3 = phi(X1, X2) X3 = phi(X1, X2)
20 // ... = X3 + 4 X4 = phi(X3)
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.
28 //===----------------------------------------------------------------------===//
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Constants.h"
32 #include "llvm/Pass.h"
33 #include "llvm/Function.h"
34 #include "llvm/Instructions.h"
35 #include "llvm/ADT/SetVector.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/Analysis/Dominators.h"
38 #include "llvm/Analysis/LoopInfo.h"
39 #include "llvm/Support/CFG.h"
46 static Statistic<> NumLCSSA("lcssa",
47 "Number of live out of a loop variables");
49 class LCSSA : public FunctionPass {
53 LoopInfo *LI; // Loop information
54 DominatorTree *DT; // Dominator Tree for the current Function...
55 DominanceFrontier *DF; // Current Dominance Frontier
56 std::vector<BasicBlock*> LoopBlocks;
58 virtual bool runOnFunction(Function &F);
59 bool visitSubloop(Loop* L);
60 void processInstruction(Instruction* Instr,
61 const std::vector<BasicBlock*>& exitBlocks);
63 /// This transformation requires natural loop information & requires that
64 /// loop preheaders be inserted into the CFG. It maintains both of these,
65 /// as well as the CFG. It also requires dominator information.
67 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.addRequiredID(LoopSimplifyID);
70 AU.addPreservedID(LoopSimplifyID);
71 AU.addRequired<LoopInfo>();
72 AU.addRequired<DominatorTree>();
73 AU.addRequired<DominanceFrontier>();
76 SetVector<Instruction*> getLoopValuesUsedOutsideLoop(Loop *L);
77 Value *getValueDominatingBlock(BasicBlock *BB,
78 std::map<BasicBlock*, Value*>& PotDoms) {
79 return getValueDominatingDTNode(DT->getNode(BB), PotDoms);
81 Value *getValueDominatingDTNode(DominatorTree::Node *Node,
82 std::map<BasicBlock*, Value*>& PotDoms);
84 /// inLoop - returns true if the given block is within the current loop
85 const bool inLoop(BasicBlock* B) {
86 return std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), B);
90 RegisterOpt<LCSSA> X("lcssa", "Loop-Closed SSA Form Pass");
93 FunctionPass *llvm::createLCSSAPass() { return new LCSSA(); }
94 const PassInfo *llvm::LCSSAID = X.getPassInfo();
96 /// runOnFunction - Process all loops in the function, inner-most out.
97 bool LCSSA::runOnFunction(Function &F) {
100 LI = &getAnalysis<LoopInfo>();
101 DF = &getAnalysis<DominanceFrontier>();
102 DT = &getAnalysis<DominatorTree>();
104 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) {
105 changed |= visitSubloop(*I);
111 /// visitSubloop - Recursively process all subloops, and then process the given
112 /// loop if it has live-out values.
113 bool LCSSA::visitSubloop(Loop* L) {
114 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
117 // Speed up queries by creating a sorted list of blocks
119 LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
120 std::sort(LoopBlocks.begin(), LoopBlocks.end());
122 SetVector<Instruction*> AffectedValues = getLoopValuesUsedOutsideLoop(L);
124 // If no values are affected, we can save a lot of work, since we know that
125 // nothing will be changed.
126 if (AffectedValues.empty())
129 std::vector<BasicBlock*> exitBlocks;
130 L->getExitBlocks(exitBlocks);
133 // Iterate over all affected values for this loop and insert Phi nodes
134 // for them in the appropriate exit blocks
136 for (SetVector<Instruction*>::iterator I = AffectedValues.begin(),
137 E = AffectedValues.end(); I != E; ++I) {
138 processInstruction(*I, exitBlocks);
141 assert(L->isLCSSAForm());
146 /// processInstruction - Given a live-out instruction, insert LCSSA Phi nodes,
147 /// eliminate all out-of-loop uses.
148 void LCSSA::processInstruction(Instruction* Instr,
149 const std::vector<BasicBlock*>& exitBlocks)
151 ++NumLCSSA; // We are applying the transformation
153 std::map<BasicBlock*, Value*> Phis;
155 // Add the base instruction to the Phis list. This makes tracking down
156 // the dominating values easier when we're filling in Phi nodes. This will
157 // be removed later, before we perform use replacement.
158 Phis[Instr->getParent()] = Instr;
160 // Phi nodes that need to be IDF-processed
161 std::vector<PHINode*> workList;
163 for (std::vector<BasicBlock*>::const_iterator BBI = exitBlocks.begin(),
164 BBE = exitBlocks.end(); BBI != BBE; ++BBI) {
165 Value*& phi = Phis[*BBI];
167 DT->getNode(Instr->getParent())->dominates(DT->getNode(*BBI))) {
168 phi = new PHINode(Instr->getType(), Instr->getName()+".lcssa",
170 workList.push_back(cast<PHINode>(phi));
174 // Phi nodes that need to have their incoming values filled.
175 std::vector<PHINode*> needIncomingValues;
177 // Calculate the IDF of these LCSSA Phi nodes, inserting new Phi's where
178 // necessary. Keep track of these new Phi's in the "Phis" map.
179 while (!workList.empty()) {
180 PHINode *CurPHI = workList.back();
183 // Even though we've removed this Phi from the work list, we still need
184 // to fill in its incoming values.
185 needIncomingValues.push_back(CurPHI);
187 // Get the current Phi's DF, and insert Phi nodes. Add these new
188 // nodes to our worklist.
189 DominanceFrontier::const_iterator it = DF->find(CurPHI->getParent());
190 if (it != DF->end()) {
191 const DominanceFrontier::DomSetType &S = it->second;
192 for (DominanceFrontier::DomSetType::const_iterator P = S.begin(),
193 PE = S.end(); P != PE; ++P) {
194 if (DT->getNode(Instr->getParent())->dominates(DT->getNode(*P))) {
195 Value *&Phi = Phis[*P];
197 // Still doesn't have operands...
198 Phi = new PHINode(Instr->getType(), Instr->getName()+".lcssa",
201 workList.push_back(cast<PHINode>(Phi));
208 // Fill in all Phis we've inserted that need their incoming values filled in.
209 for (std::vector<PHINode*>::iterator IVI = needIncomingValues.begin(),
210 IVE = needIncomingValues.end(); IVI != IVE; ++IVI)
211 for (pred_iterator PI = pred_begin((*IVI)->getParent()),
212 E = pred_end((*IVI)->getParent()); PI != E; ++PI)
213 (*IVI)->addIncoming(getValueDominatingBlock(*PI, Phis),
216 // Find all uses of the affected value, and replace them with the
218 std::vector<Instruction*> Uses;
219 for (Instruction::use_iterator UI = Instr->use_begin(), UE = Instr->use_end();
221 Instruction* use = cast<Instruction>(*UI);
222 BasicBlock* UserBB = use->getParent();
223 if (PHINode* p = dyn_cast<PHINode>(use)) {
224 unsigned OperandNo = UI.getOperandNo();
225 UserBB = p->getIncomingBlock(OperandNo/2);
228 // Don't need to update uses within the loop body.
229 if (!inLoop(use->getParent()))
233 for (std::vector<Instruction*>::iterator II = Uses.begin(), IE = Uses.end();
235 if (PHINode* phi = dyn_cast<PHINode>(*II)) {
236 for (unsigned int i = 0; i < phi->getNumIncomingValues(); ++i) {
237 if (phi->getIncomingValue(i) == Instr) {
239 getValueDominatingBlock(phi->getIncomingBlock(i), Phis);
240 phi->setIncomingValue(i, dominator);
244 Value *NewVal = getValueDominatingBlock((*II)->getParent(), Phis);
245 (*II)->replaceUsesOfWith(Instr, NewVal);
250 /// getLoopValuesUsedOutsideLoop - Return any values defined in the loop that
251 /// are used by instructions outside of it.
252 SetVector<Instruction*> LCSSA::getLoopValuesUsedOutsideLoop(Loop *L) {
254 // FIXME: For large loops, we may be able to avoid a lot of use-scanning
255 // by using dominance information. In particular, if a block does not
256 // dominate any of the loop exits, then none of the values defined in the
257 // block could be used outside the loop.
259 SetVector<Instruction*> AffectedValues;
260 for (Loop::block_iterator BB = L->block_begin(), E = L->block_end();
262 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I)
263 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
265 BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
266 if (PHINode* p = dyn_cast<PHINode>(*UI)) {
267 unsigned OperandNo = UI.getOperandNo();
268 UserBB = p->getIncomingBlock(OperandNo/2);
271 if (!inLoop(UserBB)) {
272 AffectedValues.insert(I);
277 return AffectedValues;
280 /// getValueDominatingBlock - Return the value within the potential dominators
281 /// map that dominates the given block.
282 Value *LCSSA::getValueDominatingDTNode(DominatorTree::Node *Node,
283 std::map<BasicBlock*, Value*>& PotDoms) {
284 // FIXME: The following insertion should be in place rather than the if
285 // statement. Currently, this is due to the fact that LCSSA isn't smart
286 // enough to avoid inserting IDF Phis that don't dominate any uses. In some
287 // of those cases, it could ask us to provide a dominating value for a block
288 // that has none, so we need to return undef.
289 //assert(Node != 0 && "Didn't find dom value?");
290 if (Node == 0) return UndefValue::get(PotDoms.begin()->second->getType());
292 Value *&CacheSlot = PotDoms[Node->getBlock()];
293 if (CacheSlot) return CacheSlot;
295 // Otherwise, return the value of the idom and remember this for next time.
296 return CacheSlot = getValueDominatingDTNode(Node->getIDom(), PotDoms);