Add a fix for an issue where LCSSA would fail to insert undef's in some corner
[oota-llvm.git] / lib / Transforms / Utils / LCSSA.cpp
1 //===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
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
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"
40 #include <algorithm>
41 #include <map>
42
43 using namespace llvm;
44
45 namespace {
46   static Statistic<> NumLCSSA("lcssa",
47                               "Number of live out of a loop variables");
48   
49   class LCSSA : public FunctionPass {
50   public:
51     
52   
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;
57     
58     virtual bool runOnFunction(Function &F);
59     bool visitSubloop(Loop* L);
60     void processInstruction(Instruction* Instr,
61                             const std::vector<BasicBlock*>& exitBlocks);
62     
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.
66     ///
67     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
68       AU.setPreservesCFG();
69       AU.addRequiredID(LoopSimplifyID);
70       AU.addPreservedID(LoopSimplifyID);
71       AU.addRequired<LoopInfo>();
72       AU.addRequired<DominatorTree>();
73       AU.addRequired<DominanceFrontier>();
74     }
75   private:
76     SetVector<Instruction*> getLoopValuesUsedOutsideLoop(Loop *L);
77     Value *getValueDominatingBlock(BasicBlock *BB,
78                                  std::map<BasicBlock*, Value*>& PotDoms) {
79       return getValueDominatingDTNode(DT->getNode(BB), PotDoms);
80     }
81     Value *getValueDominatingDTNode(DominatorTree::Node *Node,
82                                   std::map<BasicBlock*, Value*>& PotDoms);
83                                       
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);
87     }
88   };
89   
90   RegisterOpt<LCSSA> X("lcssa", "Loop-Closed SSA Form Pass");
91 }
92
93 FunctionPass *llvm::createLCSSAPass() { return new LCSSA(); }
94 const PassInfo *llvm::LCSSAID = X.getPassInfo();
95
96 /// runOnFunction - Process all loops in the function, inner-most out.
97 bool LCSSA::runOnFunction(Function &F) {
98   bool changed = false;
99   
100   LI = &getAnalysis<LoopInfo>();
101   DF = &getAnalysis<DominanceFrontier>();
102   DT = &getAnalysis<DominatorTree>();
103     
104   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) {
105     changed |= visitSubloop(*I);
106   }
107       
108   return changed;
109 }
110
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)
115     visitSubloop(*I);
116     
117   // Speed up queries by creating a sorted list of blocks
118   LoopBlocks.clear();
119   LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
120   std::sort(LoopBlocks.begin(), LoopBlocks.end());
121   
122   SetVector<Instruction*> AffectedValues = getLoopValuesUsedOutsideLoop(L);
123   
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())
127     return false;
128   
129   std::vector<BasicBlock*> exitBlocks;
130   L->getExitBlocks(exitBlocks);
131   
132   
133   // Iterate over all affected values for this loop and insert Phi nodes
134   // for them in the appropriate exit blocks
135   
136   for (SetVector<Instruction*>::iterator I = AffectedValues.begin(),
137        E = AffectedValues.end(); I != E; ++I) {
138     processInstruction(*I, exitBlocks);
139   }
140   
141   assert(L->isLCSSAForm());
142   
143   return true;
144 }
145
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)
150 {
151   ++NumLCSSA; // We are applying the transformation
152   
153   std::map<BasicBlock*, Value*> Phis;
154   
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;
159   
160   // Phi nodes that need to be IDF-processed
161   std::vector<PHINode*> workList;
162   
163   for (std::vector<BasicBlock*>::const_iterator BBI = exitBlocks.begin(),
164       BBE = exitBlocks.end(); BBI != BBE; ++BBI) {
165     Value*& phi = Phis[*BBI];
166     if (phi == 0 &&
167         DT->getNode(Instr->getParent())->dominates(DT->getNode(*BBI))) {
168       phi = new PHINode(Instr->getType(), Instr->getName()+".lcssa",
169                                  (*BBI)->begin());
170       workList.push_back(cast<PHINode>(phi));
171     }
172   }
173   
174   // Phi nodes that need to have their incoming values filled.
175   std::vector<PHINode*> needIncomingValues;
176   
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();
181     workList.pop_back();
182     
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);
186     
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];
196           if (Phi == 0) {
197             // Still doesn't have operands...
198             Phi = new PHINode(Instr->getType(), Instr->getName()+".lcssa",
199                               (*P)->begin());
200           
201             workList.push_back(cast<PHINode>(Phi));
202           }
203         }
204       }
205     }
206   }
207   
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),
214                           *PI);
215   
216   // Find all uses of the affected value, and replace them with the
217   // appropriate Phi.
218   std::vector<Instruction*> Uses;
219   for (Instruction::use_iterator UI = Instr->use_begin(), UE = Instr->use_end();
220        UI != UE; ++UI) {
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);
226     }
227     
228     // Don't need to update uses within the loop body.
229     if (!inLoop(use->getParent()))
230       Uses.push_back(use);
231   }
232   
233   for (std::vector<Instruction*>::iterator II = Uses.begin(), IE = Uses.end();
234        II != IE; ++II) {
235     if (PHINode* phi = dyn_cast<PHINode>(*II)) {
236       for (unsigned int i = 0; i < phi->getNumIncomingValues(); ++i) {
237         if (phi->getIncomingValue(i) == Instr) {
238           Value* dominator = 
239                         getValueDominatingBlock(phi->getIncomingBlock(i), Phis);
240           phi->setIncomingValue(i, dominator);
241         }
242       }
243     } else {
244       Value *NewVal = getValueDominatingBlock((*II)->getParent(), Phis);
245       (*II)->replaceUsesOfWith(Instr, NewVal);
246     }
247   }
248 }
249
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) {
253   
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.
258   
259   SetVector<Instruction*> AffectedValues;  
260   for (Loop::block_iterator BB = L->block_begin(), E = L->block_end();
261        BB != E; ++BB) {
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;
264            ++UI) {
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);
269         }
270         
271         if (!inLoop(UserBB)) {
272           AffectedValues.insert(I);
273           break;
274         }
275       }
276   }
277   return AffectedValues;
278 }
279
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());
291   
292   Value *&CacheSlot = PotDoms[Node->getBlock()];
293   if (CacheSlot) return CacheSlot;
294   
295   // Otherwise, return the value of the idom and remember this for next time.
296   return CacheSlot = getValueDominatingDTNode(Node->getIDom(), PotDoms);
297 }