1193323Sed//===-- LCSSA.cpp - Convert loops into loop-closed SSA form ---------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This pass transforms loops by placing phi nodes at the end of the loops for
11193323Sed// all values that are live across the loop boundary.  For example, it turns
12193323Sed// the left into the right code:
13193323Sed//
14193323Sed// for (...)                for (...)
15193323Sed//   if (c)                   if (c)
16193323Sed//     X1 = ...                 X1 = ...
17193323Sed//   else                     else
18193323Sed//     X2 = ...                 X2 = ...
19193323Sed//   X3 = phi(X1, X2)         X3 = phi(X1, X2)
20193323Sed// ... = X3 + 4             X4 = phi(X3)
21193323Sed//                          ... = X4 + 4
22193323Sed//
23193323Sed// This is still valid LLVM; the extra phi nodes are purely redundant, and will
24193323Sed// be trivially eliminated by InstCombine.  The major benefit of this
25193323Sed// transformation is that it makes many other loop optimizations, such as
26193323Sed// LoopUnswitching, simpler.
27193323Sed//
28193323Sed//===----------------------------------------------------------------------===//
29193323Sed
30193323Sed#define DEBUG_TYPE "lcssa"
31193323Sed#include "llvm/Transforms/Scalar.h"
32252723Sdim#include "llvm/ADT/STLExtras.h"
33252723Sdim#include "llvm/ADT/Statistic.h"
34193323Sed#include "llvm/Analysis/Dominators.h"
35266759Sdim#include "llvm/Analysis/AliasAnalysis.h"
36193323Sed#include "llvm/Analysis/LoopPass.h"
37193323Sed#include "llvm/Analysis/ScalarEvolution.h"
38252723Sdim#include "llvm/IR/Constants.h"
39252723Sdim#include "llvm/IR/Function.h"
40252723Sdim#include "llvm/IR/Instructions.h"
41252723Sdim#include "llvm/Pass.h"
42252723Sdim#include "llvm/Support/PredIteratorCache.h"
43198090Srdivacky#include "llvm/Transforms/Utils/SSAUpdater.h"
44193323Sedusing namespace llvm;
45193323Sed
46193323SedSTATISTIC(NumLCSSA, "Number of live out of a loop variables");
47193323Sed
48193323Sednamespace {
49198090Srdivacky  struct LCSSA : public LoopPass {
50193323Sed    static char ID; // Pass identification, replacement for typeid
51218893Sdim    LCSSA() : LoopPass(ID) {
52218893Sdim      initializeLCSSAPass(*PassRegistry::getPassRegistry());
53218893Sdim    }
54193323Sed
55193323Sed    // Cached analysis information for the current function.
56193323Sed    DominatorTree *DT;
57245431Sdim    LoopInfo *LI;
58245431Sdim    ScalarEvolution *SE;
59193323Sed    PredIteratorCache PredCache;
60198090Srdivacky    Loop *L;
61193323Sed
62193323Sed    virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
63193323Sed
64193323Sed    /// This transformation requires natural loop information & requires that
65193323Sed    /// loop preheaders be inserted into the CFG.  It maintains both of these,
66193323Sed    /// as well as the CFG.  It also requires dominator information.
67193323Sed    ///
68193323Sed    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
69193323Sed      AU.setPreservesCFG();
70199481Srdivacky
71212904Sdim      AU.addRequired<DominatorTree>();
72212904Sdim      AU.addRequired<LoopInfo>();
73193323Sed      AU.addPreservedID(LoopSimplifyID);
74266759Sdim      AU.addPreserved<AliasAnalysis>();
75193323Sed      AU.addPreserved<ScalarEvolution>();
76193323Sed    }
77193323Sed  private:
78198090Srdivacky    bool ProcessInstruction(Instruction *Inst,
79198090Srdivacky                            const SmallVectorImpl<BasicBlock*> &ExitBlocks);
80198090Srdivacky
81198090Srdivacky    /// verifyAnalysis() - Verify loop nest.
82198090Srdivacky    virtual void verifyAnalysis() const {
83198090Srdivacky      // Check the special guarantees that LCSSA makes.
84205218Srdivacky      assert(L->isLCSSAForm(*DT) && "LCSSA form not preserved!");
85198090Srdivacky    }
86193323Sed  };
87193323Sed}
88193323Sed
89193323Sedchar LCSSA::ID = 0;
90218893SdimINITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
91218893SdimINITIALIZE_PASS_DEPENDENCY(DominatorTree)
92218893SdimINITIALIZE_PASS_DEPENDENCY(LoopInfo)
93218893SdimINITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
94193323Sed
95193323SedPass *llvm::createLCSSAPass() { return new LCSSA(); }
96212904Sdimchar &llvm::LCSSAID = LCSSA::ID;
97193323Sed
98198090Srdivacky
99198090Srdivacky/// BlockDominatesAnExit - Return true if the specified block dominates at least
100198090Srdivacky/// one of the blocks in the specified list.
101198090Srdivackystatic bool BlockDominatesAnExit(BasicBlock *BB,
102198090Srdivacky                                 const SmallVectorImpl<BasicBlock*> &ExitBlocks,
103198090Srdivacky                                 DominatorTree *DT) {
104198090Srdivacky  DomTreeNode *DomNode = DT->getNode(BB);
105198090Srdivacky  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
106198090Srdivacky    if (DT->dominates(DomNode, DT->getNode(ExitBlocks[i])))
107198090Srdivacky      return true;
108198090Srdivacky
109198090Srdivacky  return false;
110198090Srdivacky}
111198090Srdivacky
112198090Srdivacky
113193323Sed/// runOnFunction - Process all loops in the function, inner-most out.
114198090Srdivackybool LCSSA::runOnLoop(Loop *TheLoop, LPPassManager &LPM) {
115198090Srdivacky  L = TheLoop;
116193323Sed
117193323Sed  DT = &getAnalysis<DominatorTree>();
118245431Sdim  LI = &getAnalysis<LoopInfo>();
119245431Sdim  SE = getAnalysisIfAvailable<ScalarEvolution>();
120193323Sed
121198090Srdivacky  // Get the set of exiting blocks.
122198090Srdivacky  SmallVector<BasicBlock*, 8> ExitBlocks;
123198090Srdivacky  L->getExitBlocks(ExitBlocks);
124198090Srdivacky
125198090Srdivacky  if (ExitBlocks.empty())
126198090Srdivacky    return false;
127198090Srdivacky
128198090Srdivacky  // Look at all the instructions in the loop, checking to see if they have uses
129198090Srdivacky  // outside the loop.  If so, rewrite those uses.
130198090Srdivacky  bool MadeChange = false;
131193323Sed
132198090Srdivacky  for (Loop::block_iterator BBI = L->block_begin(), E = L->block_end();
133198090Srdivacky       BBI != E; ++BBI) {
134198090Srdivacky    BasicBlock *BB = *BBI;
135198090Srdivacky
136198090Srdivacky    // For large loops, avoid use-scanning by using dominance information:  In
137198090Srdivacky    // particular, if a block does not dominate any of the loop exits, then none
138198090Srdivacky    // of the values defined in the block could be used outside the loop.
139198090Srdivacky    if (!BlockDominatesAnExit(BB, ExitBlocks, DT))
140198090Srdivacky      continue;
141198090Srdivacky
142198090Srdivacky    for (BasicBlock::iterator I = BB->begin(), E = BB->end();
143198090Srdivacky         I != E; ++I) {
144198090Srdivacky      // Reject two common cases fast: instructions with no uses (like stores)
145198090Srdivacky      // and instructions with one use that is in the same block as this.
146198090Srdivacky      if (I->use_empty() ||
147198090Srdivacky          (I->hasOneUse() && I->use_back()->getParent() == BB &&
148198090Srdivacky           !isa<PHINode>(I->use_back())))
149198090Srdivacky        continue;
150198090Srdivacky
151198090Srdivacky      MadeChange |= ProcessInstruction(I, ExitBlocks);
152198090Srdivacky    }
153198090Srdivacky  }
154245431Sdim
155245431Sdim  // If we modified the code, remove any caches about the loop from SCEV to
156245431Sdim  // avoid dangling entries.
157245431Sdim  // FIXME: This is a big hammer, can we clear the cache more selectively?
158245431Sdim  if (SE && MadeChange)
159245431Sdim    SE->forgetLoop(L);
160193323Sed
161205218Srdivacky  assert(L->isLCSSAForm(*DT));
162198090Srdivacky  PredCache.clear();
163198090Srdivacky
164198090Srdivacky  return MadeChange;
165198090Srdivacky}
166198090Srdivacky
167198090Srdivacky/// isExitBlock - Return true if the specified block is in the list.
168198090Srdivackystatic bool isExitBlock(BasicBlock *BB,
169198090Srdivacky                        const SmallVectorImpl<BasicBlock*> &ExitBlocks) {
170198090Srdivacky  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
171198090Srdivacky    if (ExitBlocks[i] == BB)
172198090Srdivacky      return true;
173198090Srdivacky  return false;
174198090Srdivacky}
175198090Srdivacky
176198090Srdivacky/// ProcessInstruction - Given an instruction in the loop, check to see if it
177198090Srdivacky/// has any uses that are outside the current loop.  If so, insert LCSSA PHI
178198090Srdivacky/// nodes and rewrite the uses.
179198090Srdivackybool LCSSA::ProcessInstruction(Instruction *Inst,
180198090Srdivacky                               const SmallVectorImpl<BasicBlock*> &ExitBlocks) {
181198090Srdivacky  SmallVector<Use*, 16> UsesToRewrite;
182193323Sed
183198090Srdivacky  BasicBlock *InstBB = Inst->getParent();
184193323Sed
185198090Srdivacky  for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
186198090Srdivacky       UI != E; ++UI) {
187210299Sed    User *U = *UI;
188210299Sed    BasicBlock *UserBB = cast<Instruction>(U)->getParent();
189210299Sed    if (PHINode *PN = dyn_cast<PHINode>(U))
190198090Srdivacky      UserBB = PN->getIncomingBlock(UI);
191198090Srdivacky
192263509Sdim    if (InstBB != UserBB && !L->contains(UserBB))
193198090Srdivacky      UsesToRewrite.push_back(&UI.getUse());
194198090Srdivacky  }
195210299Sed
196198090Srdivacky  // If there are no uses outside the loop, exit with no change.
197198090Srdivacky  if (UsesToRewrite.empty()) return false;
198193323Sed
199193323Sed  ++NumLCSSA; // We are applying the transformation
200193323Sed
201195098Sed  // Invoke instructions are special in that their result value is not available
202195098Sed  // along their unwind edge. The code below tests to see whether DomBB dominates
203195098Sed  // the value, so adjust DomBB to the normal destination block, which is
204195098Sed  // effectively where the value is first usable.
205198090Srdivacky  BasicBlock *DomBB = Inst->getParent();
206198090Srdivacky  if (InvokeInst *Inv = dyn_cast<InvokeInst>(Inst))
207195098Sed    DomBB = Inv->getNormalDest();
208195098Sed
209195098Sed  DomTreeNode *DomNode = DT->getNode(DomBB);
210195098Sed
211221345Sdim  SmallVector<PHINode*, 16> AddedPHIs;
212221345Sdim
213198090Srdivacky  SSAUpdater SSAUpdate;
214212904Sdim  SSAUpdate.Initialize(Inst->getType(), Inst->getName());
215198090Srdivacky
216198090Srdivacky  // Insert the LCSSA phi's into all of the exit blocks dominated by the
217199481Srdivacky  // value, and add them to the Phi's map.
218198090Srdivacky  for (SmallVectorImpl<BasicBlock*>::const_iterator BBI = ExitBlocks.begin(),
219198090Srdivacky      BBE = ExitBlocks.end(); BBI != BBE; ++BBI) {
220198090Srdivacky    BasicBlock *ExitBB = *BBI;
221198090Srdivacky    if (!DT->dominates(DomNode, DT->getNode(ExitBB))) continue;
222198090Srdivacky
223198090Srdivacky    // If we already inserted something for this BB, don't reprocess it.
224198090Srdivacky    if (SSAUpdate.HasValueForBlock(ExitBB)) continue;
225198090Srdivacky
226221345Sdim    PHINode *PN = PHINode::Create(Inst->getType(),
227221345Sdim                                  PredCache.GetNumPreds(ExitBB),
228221345Sdim                                  Inst->getName()+".lcssa",
229198090Srdivacky                                  ExitBB->begin());
230193323Sed
231198090Srdivacky    // Add inputs from inside the loop for this PHI.
232199481Srdivacky    for (BasicBlock **PI = PredCache.GetPreds(ExitBB); *PI; ++PI) {
233198090Srdivacky      PN->addIncoming(Inst, *PI);
234199481Srdivacky
235199481Srdivacky      // If the exit block has a predecessor not within the loop, arrange for
236199481Srdivacky      // the incoming value use corresponding to that predecessor to be
237199481Srdivacky      // rewritten in terms of a different LCSSA PHI.
238263509Sdim      if (!L->contains(*PI))
239199481Srdivacky        UsesToRewrite.push_back(
240199481Srdivacky          &PN->getOperandUse(
241199481Srdivacky            PN->getOperandNumForIncomingValue(PN->getNumIncomingValues()-1)));
242199481Srdivacky    }
243221345Sdim
244221345Sdim    AddedPHIs.push_back(PN);
245198090Srdivacky
246198090Srdivacky    // Remember that this phi makes the value alive in this block.
247198090Srdivacky    SSAUpdate.AddAvailableValue(ExitBB, PN);
248193323Sed  }
249245431Sdim
250198090Srdivacky  // Rewrite all uses outside the loop in terms of the new PHIs we just
251198090Srdivacky  // inserted.
252198090Srdivacky  for (unsigned i = 0, e = UsesToRewrite.size(); i != e; ++i) {
253198090Srdivacky    // If this use is in an exit block, rewrite to use the newly inserted PHI.
254198090Srdivacky    // This is required for correctness because SSAUpdate doesn't handle uses in
255198090Srdivacky    // the same block.  It assumes the PHI we inserted is at the end of the
256198090Srdivacky    // block.
257198090Srdivacky    Instruction *User = cast<Instruction>(UsesToRewrite[i]->getUser());
258198090Srdivacky    BasicBlock *UserBB = User->getParent();
259198090Srdivacky    if (PHINode *PN = dyn_cast<PHINode>(User))
260198090Srdivacky      UserBB = PN->getIncomingBlock(*UsesToRewrite[i]);
261198090Srdivacky
262198090Srdivacky    if (isa<PHINode>(UserBB->begin()) &&
263198090Srdivacky        isExitBlock(UserBB, ExitBlocks)) {
264245431Sdim      // Tell the VHs that the uses changed. This updates SCEV's caches.
265245431Sdim      if (UsesToRewrite[i]->get()->hasValueHandle())
266245431Sdim        ValueHandleBase::ValueIsRAUWd(*UsesToRewrite[i], UserBB->begin());
267198090Srdivacky      UsesToRewrite[i]->set(UserBB->begin());
268193323Sed      continue;
269193323Sed    }
270193323Sed
271198090Srdivacky    // Otherwise, do full PHI insertion.
272198090Srdivacky    SSAUpdate.RewriteUse(*UsesToRewrite[i]);
273193323Sed  }
274221345Sdim
275221345Sdim  // Remove PHI nodes that did not have any uses rewritten.
276221345Sdim  for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) {
277221345Sdim    if (AddedPHIs[i]->use_empty())
278221345Sdim      AddedPHIs[i]->eraseFromParent();
279221345Sdim  }
280193323Sed
281198090Srdivacky  return true;
282193323Sed}
283193323Sed
284