LLVM  3.7.0
LCSSA.cpp
Go to the documentation of this file.
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 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Analysis/LoopPass.h"
36 #include "llvm/IR/Constants.h"
37 #include "llvm/IR/Dominators.h"
38 #include "llvm/IR/Function.h"
39 #include "llvm/IR/Instructions.h"
41 #include "llvm/Pass.h"
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "lcssa"
47 
48 STATISTIC(NumLCSSA, "Number of live out of a loop variables");
49 
50 /// Return true if the specified block is in the list.
51 static bool isExitBlock(BasicBlock *BB,
52  const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
53  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
54  if (ExitBlocks[i] == BB)
55  return true;
56  return false;
57 }
58 
59 /// Given an instruction in the loop, check to see if it has any uses that are
60 /// outside the current loop. If so, insert LCSSA PHI nodes and rewrite the
61 /// uses.
62 static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT,
63  const SmallVectorImpl<BasicBlock *> &ExitBlocks,
64  PredIteratorCache &PredCache, LoopInfo *LI) {
65  SmallVector<Use *, 16> UsesToRewrite;
66 
67  BasicBlock *InstBB = Inst.getParent();
68 
69  for (Use &U : Inst.uses()) {
70  Instruction *User = cast<Instruction>(U.getUser());
71  BasicBlock *UserBB = User->getParent();
72  if (PHINode *PN = dyn_cast<PHINode>(User))
73  UserBB = PN->getIncomingBlock(U);
74 
75  if (InstBB != UserBB && !L.contains(UserBB))
76  UsesToRewrite.push_back(&U);
77  }
78 
79  // If there are no uses outside the loop, exit with no change.
80  if (UsesToRewrite.empty())
81  return false;
82 
83  ++NumLCSSA; // We are applying the transformation
84 
85  // Invoke instructions are special in that their result value is not available
86  // along their unwind edge. The code below tests to see whether DomBB
87  // dominates
88  // the value, so adjust DomBB to the normal destination block, which is
89  // effectively where the value is first usable.
90  BasicBlock *DomBB = Inst.getParent();
91  if (InvokeInst *Inv = dyn_cast<InvokeInst>(&Inst))
92  DomBB = Inv->getNormalDest();
93 
94  DomTreeNode *DomNode = DT.getNode(DomBB);
95 
97  SmallVector<PHINode *, 8> PostProcessPHIs;
98 
99  SSAUpdater SSAUpdate;
100  SSAUpdate.Initialize(Inst.getType(), Inst.getName());
101 
102  // Insert the LCSSA phi's into all of the exit blocks dominated by the
103  // value, and add them to the Phi's map.
105  BBE = ExitBlocks.end();
106  BBI != BBE; ++BBI) {
107  BasicBlock *ExitBB = *BBI;
108  if (!DT.dominates(DomNode, DT.getNode(ExitBB)))
109  continue;
110 
111  // If we already inserted something for this BB, don't reprocess it.
112  if (SSAUpdate.HasValueForBlock(ExitBB))
113  continue;
114 
115  PHINode *PN = PHINode::Create(Inst.getType(), PredCache.size(ExitBB),
116  Inst.getName() + ".lcssa", ExitBB->begin());
117 
118  // Add inputs from inside the loop for this PHI.
119  for (BasicBlock *Pred : PredCache.get(ExitBB)) {
120  PN->addIncoming(&Inst, Pred);
121 
122  // If the exit block has a predecessor not within the loop, arrange for
123  // the incoming value use corresponding to that predecessor to be
124  // rewritten in terms of a different LCSSA PHI.
125  if (!L.contains(Pred))
126  UsesToRewrite.push_back(
128  PN->getNumIncomingValues() - 1)));
129  }
130 
131  AddedPHIs.push_back(PN);
132 
133  // Remember that this phi makes the value alive in this block.
134  SSAUpdate.AddAvailableValue(ExitBB, PN);
135 
136  // LoopSimplify might fail to simplify some loops (e.g. when indirect
137  // branches are involved). In such situations, it might happen that an exit
138  // for Loop L1 is the header of a disjoint Loop L2. Thus, when we create
139  // PHIs in such an exit block, we are also inserting PHIs into L2's header.
140  // This could break LCSSA form for L2 because these inserted PHIs can also
141  // have uses outside of L2. Remember all PHIs in such situation as to
142  // revisit than later on. FIXME: Remove this if indirectbr support into
143  // LoopSimplify gets improved.
144  if (auto *OtherLoop = LI->getLoopFor(ExitBB))
145  if (!L.contains(OtherLoop))
146  PostProcessPHIs.push_back(PN);
147  }
148 
149  // Rewrite all uses outside the loop in terms of the new PHIs we just
150  // inserted.
151  for (unsigned i = 0, e = UsesToRewrite.size(); i != e; ++i) {
152  // If this use is in an exit block, rewrite to use the newly inserted PHI.
153  // This is required for correctness because SSAUpdate doesn't handle uses in
154  // the same block. It assumes the PHI we inserted is at the end of the
155  // block.
156  Instruction *User = cast<Instruction>(UsesToRewrite[i]->getUser());
157  BasicBlock *UserBB = User->getParent();
158  if (PHINode *PN = dyn_cast<PHINode>(User))
159  UserBB = PN->getIncomingBlock(*UsesToRewrite[i]);
160 
161  if (isa<PHINode>(UserBB->begin()) && isExitBlock(UserBB, ExitBlocks)) {
162  // Tell the VHs that the uses changed. This updates SCEV's caches.
163  if (UsesToRewrite[i]->get()->hasValueHandle())
164  ValueHandleBase::ValueIsRAUWd(*UsesToRewrite[i], UserBB->begin());
165  UsesToRewrite[i]->set(UserBB->begin());
166  continue;
167  }
168 
169  // Otherwise, do full PHI insertion.
170  SSAUpdate.RewriteUse(*UsesToRewrite[i]);
171  }
172 
173  // Post process PHI instructions that were inserted into another disjoint loop
174  // and update their exits properly.
175  for (auto *I : PostProcessPHIs) {
176  if (I->use_empty())
177  continue;
178 
179  BasicBlock *PHIBB = I->getParent();
180  Loop *OtherLoop = LI->getLoopFor(PHIBB);
182  OtherLoop->getExitBlocks(EBs);
183  if (EBs.empty())
184  continue;
185 
186  // Recurse and re-process each PHI instruction. FIXME: we should really
187  // convert this entire thing to a worklist approach where we process a
188  // vector of instructions...
189  processInstruction(*OtherLoop, *I, DT, EBs, PredCache, LI);
190  }
191 
192  // Remove PHI nodes that did not have any uses rewritten.
193  for (unsigned i = 0, e = AddedPHIs.size(); i != e; ++i) {
194  if (AddedPHIs[i]->use_empty())
195  AddedPHIs[i]->eraseFromParent();
196  }
197 
198  return true;
199 }
200 
201 /// Return true if the specified block dominates at least
202 /// one of the blocks in the specified list.
203 static bool
205  DominatorTree &DT,
206  const SmallVectorImpl<BasicBlock *> &ExitBlocks) {
207  DomTreeNode *DomNode = DT.getNode(BB);
208  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
209  if (DT.dominates(DomNode, DT.getNode(ExitBlocks[i])))
210  return true;
211 
212  return false;
213 }
214 
216  ScalarEvolution *SE) {
217  bool Changed = false;
218 
219  // Get the set of exiting blocks.
220  SmallVector<BasicBlock *, 8> ExitBlocks;
221  L.getExitBlocks(ExitBlocks);
222 
223  if (ExitBlocks.empty())
224  return false;
225 
226  PredIteratorCache PredCache;
227 
228  // Look at all the instructions in the loop, checking to see if they have uses
229  // outside the loop. If so, rewrite those uses.
230  for (Loop::block_iterator BBI = L.block_begin(), BBE = L.block_end();
231  BBI != BBE; ++BBI) {
232  BasicBlock *BB = *BBI;
233 
234  // For large loops, avoid use-scanning by using dominance information: In
235  // particular, if a block does not dominate any of the loop exits, then none
236  // of the values defined in the block could be used outside the loop.
237  if (!blockDominatesAnExit(BB, DT, ExitBlocks))
238  continue;
239 
240  for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
241  // Reject two common cases fast: instructions with no uses (like stores)
242  // and instructions with one use that is in the same block as this.
243  if (I->use_empty() ||
244  (I->hasOneUse() && I->user_back()->getParent() == BB &&
245  !isa<PHINode>(I->user_back())))
246  continue;
247 
248  Changed |= processInstruction(L, *I, DT, ExitBlocks, PredCache, LI);
249  }
250  }
251 
252  // If we modified the code, remove any caches about the loop from SCEV to
253  // avoid dangling entries.
254  // FIXME: This is a big hammer, can we clear the cache more selectively?
255  if (SE && Changed)
256  SE->forgetLoop(&L);
257 
258  assert(L.isLCSSAForm(DT));
259 
260  return Changed;
261 }
262 
263 /// Process a loop nest depth first.
265  ScalarEvolution *SE) {
266  bool Changed = false;
267 
268  // Recurse depth-first through inner loops.
269  for (Loop::iterator I = L.begin(), E = L.end(); I != E; ++I)
270  Changed |= formLCSSARecursively(**I, DT, LI, SE);
271 
272  Changed |= formLCSSA(L, DT, LI, SE);
273  return Changed;
274 }
275 
276 namespace {
277 struct LCSSA : public FunctionPass {
278  static char ID; // Pass identification, replacement for typeid
279  LCSSA() : FunctionPass(ID) {
281  }
282 
283  // Cached analysis information for the current function.
284  DominatorTree *DT;
285  LoopInfo *LI;
286  ScalarEvolution *SE;
287 
288  bool runOnFunction(Function &F) override;
289 
290  /// This transformation requires natural loop information & requires that
291  /// loop preheaders be inserted into the CFG. It maintains both of these,
292  /// as well as the CFG. It also requires dominator information.
293  void getAnalysisUsage(AnalysisUsage &AU) const override {
294  AU.setPreservesCFG();
295 
301  }
302 };
303 }
304 
305 char LCSSA::ID = 0;
306 INITIALIZE_PASS_BEGIN(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
309 INITIALIZE_PASS_END(LCSSA, "lcssa", "Loop-Closed SSA Form Pass", false, false)
310 
311 Pass *llvm::createLCSSAPass() { return new LCSSA(); }
313 
314 
315 /// Process all loops in the function, inner-most out.
316 bool LCSSA::runOnFunction(Function &F) {
317  bool Changed = false;
318  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
319  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
320  SE = getAnalysisIfAvailable<ScalarEvolution>();
321 
322  // Simplify each loop nest in the function.
323  for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
324  Changed |= formLCSSARecursively(**I, *DT, LI, SE);
325 
326  return Changed;
327 }
328 
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
const Use & getOperandUse(unsigned i) const
Definition: User.h:129
iterator_range< use_iterator > uses()
Definition: Value.h:283
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:38
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
Definition: SSAUpdater.cpp:45
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
Definition: SSAUpdater.cpp:58
ScalarEvolution - This class is the main scalar evolution driver.
F(f)
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
Definition: LoopInfo.h:540
bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE=nullptr)
Put loop into LCSSA form.
Definition: LCSSA.cpp:215
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
std::vector< LoopT * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
Definition: LoopInfo.h:528
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
static unsigned getOperandNumForIncomingValue(unsigned i)
AnalysisUsage & addRequired()
ArrayRef< BasicBlock * > get(BasicBlock *BB)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
#define false
Definition: ConvertUTF.c:65
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
getExitBlocks - Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:64
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
PredIteratorCache - This class is an extremely trivial cache for predecessor iterator queries...
static bool processInstruction(Loop &L, Instruction &Inst, DominatorTree &DT, const SmallVectorImpl< BasicBlock * > &ExitBlocks, PredIteratorCache &PredCache, LoopInfo *LI)
Given an instruction in the loop, check to see if it has any uses that are outside the current loop...
Definition: LCSSA.cpp:62
Base class for the actual dominator tree node.
AnalysisUsage & addPreservedID(const void *ID)
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:694
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
iterator begin() const
Definition: LoopInfo.h:131
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
This file contains the declarations for the subclasses of Constant, which represent the different fla...
char & LCSSAID
Definition: LCSSA.cpp:312
iterator end() const
Definition: LoopInfo.h:132
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:105
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
Definition: SSAUpdater.cpp:54
static bool blockDominatesAnExit(BasicBlock *BB, DominatorTree &DT, const SmallVectorImpl< BasicBlock * > &ExitBlocks)
Return true if the specified block dominates at least one of the blocks in the specified list...
Definition: LCSSA.cpp:204
char & LoopSimplifyID
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:214
Pass * createLCSSAPass()
Definition: LCSSA.cpp:311
iterator end()
Definition: BasicBlock.h:233
bool isLCSSAForm(DominatorTree &DT) const
isLCSSAForm - Return true if the Loop is in LCSSA form
Definition: LoopInfo.cpp:172
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
void initializeLCSSAPass(PassRegistry &)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:67
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
std::vector< BlockT * >::const_iterator block_iterator
Definition: LoopInfo.h:140
bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE=nullptr)
Put a loop nest into LCSSA form.
Definition: LCSSA.cpp:264
block_iterator block_end() const
Definition: LoopInfo.h:142
void forgetLoop(const Loop *L)
forgetLoop - This method should be called by the client when it has changed a loop in a way that may ...
#define I(x, y, z)
Definition: MD5.cpp:54
InvokeInst - Invoke instruction.
block_iterator block_begin() const
Definition: LoopInfo.h:141
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:737
static bool isExitBlock(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &ExitBlocks)
Return true if the specified block is in the list.
Definition: LCSSA.cpp:51
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
std::vector< LoopT * >::const_iterator iterator
Definition: LoopInfo.h:128
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
Definition: SSAUpdater.cpp:178
size_t size(BasicBlock *BB)
const BasicBlock * getParent() const
Definition: Instruction.h:72