LCSSA.cpp revision 9fc5cdf77c812aaa80419036de27576d45894d0d
1//===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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#define DEBUG_TYPE "lcssa"
31#include "llvm/Transforms/Scalar.h"
32#include "llvm/Constants.h"
33#include "llvm/Pass.h"
34#include "llvm/Function.h"
35#include "llvm/Instructions.h"
36#include "llvm/Analysis/Dominators.h"
37#include "llvm/Analysis/LoopPass.h"
38#include "llvm/Analysis/ScalarEvolution.h"
39#include "llvm/Transforms/Utils/SSAUpdater.h"
40#include "llvm/ADT/Statistic.h"
41#include "llvm/ADT/STLExtras.h"
42#include "llvm/Support/PredIteratorCache.h"
43using namespace llvm;
44
45STATISTIC(NumLCSSA, "Number of live out of a loop variables");
46
47namespace {
48  struct LCSSA : public LoopPass {
49    static char ID; // Pass identification, replacement for typeid
50    LCSSA() : LoopPass(ID) {
51      initializeLCSSAPass(*PassRegistry::getPassRegistry());
52    }
53
54    // Cached analysis information for the current function.
55    DominatorTree *DT;
56    std::vector<BasicBlock*> LoopBlocks;
57    PredIteratorCache PredCache;
58    Loop *L;
59
60    virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
61
62    /// This transformation requires natural loop information & requires that
63    /// loop preheaders be inserted into the CFG.  It maintains both of these,
64    /// as well as the CFG.  It also requires dominator information.
65    ///
66    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67      AU.setPreservesCFG();
68
69      AU.addRequired<DominatorTree>();
70      AU.addRequired<LoopInfo>();
71      AU.addPreservedID(LoopSimplifyID);
72      AU.addPreserved<ScalarEvolution>();
73    }
74  private:
75    bool ProcessInstruction(Instruction *Inst,
76                            const SmallVectorImpl<BasicBlock*> &ExitBlocks);
77
78    /// verifyAnalysis() - Verify loop nest.
79    virtual void verifyAnalysis() const {
80      // Check the special guarantees that LCSSA makes.
81      assert(L->isLCSSAForm(*DT) && "LCSSA form not preserved!");
82    }
83
84    /// inLoop - returns true if the given block is within the current loop
85    bool inLoop(BasicBlock *B) const {
86      return std::binary_search(LoopBlocks.begin(), LoopBlocks.end(), B);
87    }
88  };
89}
90
91char LCSSA::ID = 0;
92INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
93INITIALIZE_PASS_DEPENDENCY(DominatorTree)
94INITIALIZE_PASS_DEPENDENCY(LoopInfo)
95INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
96
97Pass *llvm::createLCSSAPass() { return new LCSSA(); }
98char &llvm::LCSSAID = LCSSA::ID;
99
100
101/// BlockDominatesAnExit - Return true if the specified block dominates at least
102/// one of the blocks in the specified list.
103static bool BlockDominatesAnExit(BasicBlock *BB,
104                                 const SmallVectorImpl<BasicBlock*> &ExitBlocks,
105                                 DominatorTree *DT) {
106  DomTreeNode *DomNode = DT->getNode(BB);
107  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
108    if (DT->dominates(DomNode, DT->getNode(ExitBlocks[i])))
109      return true;
110
111  return false;
112}
113
114
115/// runOnFunction - Process all loops in the function, inner-most out.
116bool LCSSA::runOnLoop(Loop *TheLoop, LPPassManager &LPM) {
117  L = TheLoop;
118
119  DT = &getAnalysis<DominatorTree>();
120
121  // Get the set of exiting blocks.
122  SmallVector<BasicBlock*, 8> ExitBlocks;
123  L->getExitBlocks(ExitBlocks);
124
125  if (ExitBlocks.empty())
126    return false;
127
128  // Speed up queries by creating a sorted vector of blocks.
129  LoopBlocks.clear();
130  LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
131  array_pod_sort(LoopBlocks.begin(), LoopBlocks.end());
132
133  // Look at all the instructions in the loop, checking to see if they have uses
134  // outside the loop.  If so, rewrite those uses.
135  bool MadeChange = false;
136
137  for (Loop::block_iterator BBI = L->block_begin(), E = L->block_end();
138       BBI != E; ++BBI) {
139    BasicBlock *BB = *BBI;
140
141    // For large loops, avoid use-scanning by using dominance information:  In
142    // particular, if a block does not dominate any of the loop exits, then none
143    // of the values defined in the block could be used outside the loop.
144    if (!BlockDominatesAnExit(BB, ExitBlocks, DT))
145      continue;
146
147    for (BasicBlock::iterator I = BB->begin(), E = BB->end();
148         I != E; ++I) {
149      // Reject two common cases fast: instructions with no uses (like stores)
150      // and instructions with one use that is in the same block as this.
151      if (I->use_empty() ||
152          (I->hasOneUse() && I->use_back()->getParent() == BB &&
153           !isa<PHINode>(I->use_back())))
154        continue;
155
156      MadeChange |= ProcessInstruction(I, ExitBlocks);
157    }
158  }
159
160  assert(L->isLCSSAForm(*DT));
161  PredCache.clear();
162
163  return MadeChange;
164}
165
166/// isExitBlock - Return true if the specified block is in the list.
167static bool isExitBlock(BasicBlock *BB,
168                        const SmallVectorImpl<BasicBlock*> &ExitBlocks) {
169  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
170    if (ExitBlocks[i] == BB)
171      return true;
172  return false;
173}
174
175/// ProcessInstruction - Given an instruction in the loop, check to see if it
176/// has any uses that are outside the current loop.  If so, insert LCSSA PHI
177/// nodes and rewrite the uses.
178bool LCSSA::ProcessInstruction(Instruction *Inst,
179                               const SmallVectorImpl<BasicBlock*> &ExitBlocks) {
180  SmallVector<Use*, 16> UsesToRewrite;
181
182  BasicBlock *InstBB = Inst->getParent();
183
184  for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
185       UI != E; ++UI) {
186    User *U = *UI;
187    BasicBlock *UserBB = cast<Instruction>(U)->getParent();
188    if (PHINode *PN = dyn_cast<PHINode>(U))
189      UserBB = PN->getIncomingBlock(UI);
190
191    if (InstBB != UserBB && !inLoop(UserBB))
192      UsesToRewrite.push_back(&UI.getUse());
193  }
194
195  // If there are no uses outside the loop, exit with no change.
196  if (UsesToRewrite.empty()) return false;
197
198  ++NumLCSSA; // We are applying the transformation
199
200  // Invoke instructions are special in that their result value is not available
201  // along their unwind edge. The code below tests to see whether DomBB dominates
202  // the value, so adjust DomBB to the normal destination block, which is
203  // effectively where the value is first usable.
204  BasicBlock *DomBB = Inst->getParent();
205  if (InvokeInst *Inv = dyn_cast<InvokeInst>(Inst))
206    DomBB = Inv->getNormalDest();
207
208  DomTreeNode *DomNode = DT->getNode(DomBB);
209
210  SSAUpdater SSAUpdate;
211  SSAUpdate.Initialize(Inst->getType(), Inst->getName());
212
213  // Insert the LCSSA phi's into all of the exit blocks dominated by the
214  // value, and add them to the Phi's map.
215  for (SmallVectorImpl<BasicBlock*>::const_iterator BBI = ExitBlocks.begin(),
216      BBE = ExitBlocks.end(); BBI != BBE; ++BBI) {
217    BasicBlock *ExitBB = *BBI;
218    if (!DT->dominates(DomNode, DT->getNode(ExitBB))) continue;
219
220    // If we already inserted something for this BB, don't reprocess it.
221    if (SSAUpdate.HasValueForBlock(ExitBB)) continue;
222
223    PHINode *PN = PHINode::Create(Inst->getType(), Inst->getName()+".lcssa",
224                                  ExitBB->begin());
225    PN->reserveOperandSpace(PredCache.GetNumPreds(ExitBB));
226
227    // Add inputs from inside the loop for this PHI.
228    for (BasicBlock **PI = PredCache.GetPreds(ExitBB); *PI; ++PI) {
229      PN->addIncoming(Inst, *PI);
230
231      // If the exit block has a predecessor not within the loop, arrange for
232      // the incoming value use corresponding to that predecessor to be
233      // rewritten in terms of a different LCSSA PHI.
234      if (!inLoop(*PI))
235        UsesToRewrite.push_back(
236          &PN->getOperandUse(
237            PN->getOperandNumForIncomingValue(PN->getNumIncomingValues()-1)));
238    }
239
240    // Remember that this phi makes the value alive in this block.
241    SSAUpdate.AddAvailableValue(ExitBB, PN);
242  }
243
244  // Rewrite all uses outside the loop in terms of the new PHIs we just
245  // inserted.
246  for (unsigned i = 0, e = UsesToRewrite.size(); i != e; ++i) {
247    // If this use is in an exit block, rewrite to use the newly inserted PHI.
248    // This is required for correctness because SSAUpdate doesn't handle uses in
249    // the same block.  It assumes the PHI we inserted is at the end of the
250    // block.
251    Instruction *User = cast<Instruction>(UsesToRewrite[i]->getUser());
252    BasicBlock *UserBB = User->getParent();
253    if (PHINode *PN = dyn_cast<PHINode>(User))
254      UserBB = PN->getIncomingBlock(*UsesToRewrite[i]);
255
256    if (isa<PHINode>(UserBB->begin()) &&
257        isExitBlock(UserBB, ExitBlocks)) {
258      UsesToRewrite[i]->set(UserBB->begin());
259      continue;
260    }
261
262    // Otherwise, do full PHI insertion.
263    SSAUpdate.RewriteUse(*UsesToRewrite[i]);
264  }
265
266  return true;
267}
268
269