LLVM  3.7.0
LoopUnrollRuntime.cpp
Go to the documentation of this file.
1 //===-- UnrollLoopRuntime.cpp - Runtime Loop unrolling utilities ----------===//
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 file implements some loop unrolling utilities for loops with run-time
11 // trip counts. See LoopUnroll.cpp for unrolling loops with compile-time
12 // trip counts.
13 //
14 // The functions in this file are used to generate extra code when the
15 // run-time trip count modulo the unroll factor is not 0. When this is the
16 // case, we need to generate code to execute these 'left over' iterations.
17 //
18 // The current strategy generates an if-then-else sequence prior to the
19 // unrolled loop to execute the 'left over' iterations. Other strategies
20 // include generate a loop before or after the unrolled loop.
21 //
22 //===----------------------------------------------------------------------===//
23 
25 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Analysis/LoopPass.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/Dominators.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/Support/Debug.h"
37 #include "llvm/Transforms/Scalar.h"
40 #include <algorithm>
41 
42 using namespace llvm;
43 
44 #define DEBUG_TYPE "loop-unroll"
45 
46 STATISTIC(NumRuntimeUnrolled,
47  "Number of loops unrolled with run-time trip counts");
48 
49 /// Connect the unrolling prolog code to the original loop.
50 /// The unrolling prolog code contains code to execute the
51 /// 'extra' iterations if the run-time trip count modulo the
52 /// unroll count is non-zero.
53 ///
54 /// This function performs the following:
55 /// - Create PHI nodes at prolog end block to combine values
56 /// that exit the prolog code and jump around the prolog.
57 /// - Add a PHI operand to a PHI node at the loop exit block
58 /// for values that exit the prolog and go around the loop.
59 /// - Branch around the original loop if the trip count is less
60 /// than the unroll factor.
61 ///
62 static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
63  BasicBlock *LastPrologBB, BasicBlock *PrologEnd,
64  BasicBlock *OrigPH, BasicBlock *NewPH,
66  DominatorTree *DT, LoopInfo *LI, Pass *P) {
67  BasicBlock *Latch = L->getLoopLatch();
68  assert(Latch && "Loop must have a latch");
69 
70  // Create a PHI node for each outgoing value from the original loop
71  // (which means it is an outgoing value from the prolog code too).
72  // The new PHI node is inserted in the prolog end basic block.
73  // The new PHI name is added as an operand of a PHI node in either
74  // the loop header or the loop exit block.
75  for (succ_iterator SBI = succ_begin(Latch), SBE = succ_end(Latch);
76  SBI != SBE; ++SBI) {
77  for (BasicBlock::iterator BBI = (*SBI)->begin();
78  PHINode *PN = dyn_cast<PHINode>(BBI); ++BBI) {
79 
80  // Add a new PHI node to the prolog end block and add the
81  // appropriate incoming values.
82  PHINode *NewPN = PHINode::Create(PN->getType(), 2, PN->getName()+".unr",
83  PrologEnd->getTerminator());
84  // Adding a value to the new PHI node from the original loop preheader.
85  // This is the value that skips all the prolog code.
86  if (L->contains(PN)) {
87  NewPN->addIncoming(PN->getIncomingValueForBlock(NewPH), OrigPH);
88  } else {
89  NewPN->addIncoming(UndefValue::get(PN->getType()), OrigPH);
90  }
91 
92  Value *V = PN->getIncomingValueForBlock(Latch);
93  if (Instruction *I = dyn_cast<Instruction>(V)) {
94  if (L->contains(I)) {
95  V = VMap[I];
96  }
97  }
98  // Adding a value to the new PHI node from the last prolog block
99  // that was created.
100  NewPN->addIncoming(V, LastPrologBB);
101 
102  // Update the existing PHI node operand with the value from the
103  // new PHI node. How this is done depends on if the existing
104  // PHI node is in the original loop block, or the exit block.
105  if (L->contains(PN)) {
106  PN->setIncomingValue(PN->getBasicBlockIndex(NewPH), NewPN);
107  } else {
108  PN->addIncoming(NewPN, PrologEnd);
109  }
110  }
111  }
112 
113  // Create a branch around the orignal loop, which is taken if there are no
114  // iterations remaining to be executed after running the prologue.
115  Instruction *InsertPt = PrologEnd->getTerminator();
116  IRBuilder<> B(InsertPt);
117 
118  assert(Count != 0 && "nonsensical Count!");
119 
120  // If BECount <u (Count - 1) then (BECount + 1) & (Count - 1) == (BECount + 1)
121  // (since Count is a power of 2). This means %xtraiter is (BECount + 1) and
122  // and all of the iterations of this loop were executed by the prologue. Note
123  // that if BECount <u (Count - 1) then (BECount + 1) cannot unsigned-overflow.
124  Value *BrLoopExit =
125  B.CreateICmpULT(BECount, ConstantInt::get(BECount->getType(), Count - 1));
126  BasicBlock *Exit = L->getUniqueExitBlock();
127  assert(Exit && "Loop must have a single exit block only");
128  // Split the exit to maintain loop canonicalization guarantees
129  SmallVector<BasicBlock*, 4> Preds(pred_begin(Exit), pred_end(Exit));
130  SplitBlockPredecessors(Exit, Preds, ".unr-lcssa", AA, DT, LI,
132  // Add the branch to the exit block (around the unrolled loop)
133  B.CreateCondBr(BrLoopExit, Exit, NewPH);
134  InsertPt->eraseFromParent();
135 }
136 
137 /// Create a clone of the blocks in a loop and connect them together.
138 /// If UnrollProlog is true, loop structure will not be cloned, otherwise a new
139 /// loop will be created including all cloned blocks, and the iterator of it
140 /// switches to count NewIter down to 0.
141 ///
142 static void CloneLoopBlocks(Loop *L, Value *NewIter, const bool UnrollProlog,
143  BasicBlock *InsertTop, BasicBlock *InsertBot,
144  std::vector<BasicBlock *> &NewBlocks,
145  LoopBlocksDFS &LoopBlocks, ValueToValueMapTy &VMap,
146  LoopInfo *LI) {
147  BasicBlock *Preheader = L->getLoopPreheader();
148  BasicBlock *Header = L->getHeader();
149  BasicBlock *Latch = L->getLoopLatch();
150  Function *F = Header->getParent();
151  LoopBlocksDFS::RPOIterator BlockBegin = LoopBlocks.beginRPO();
152  LoopBlocksDFS::RPOIterator BlockEnd = LoopBlocks.endRPO();
153  Loop *NewLoop = 0;
154  Loop *ParentLoop = L->getParentLoop();
155  if (!UnrollProlog) {
156  NewLoop = new Loop();
157  if (ParentLoop)
158  ParentLoop->addChildLoop(NewLoop);
159  else
160  LI->addTopLevelLoop(NewLoop);
161  }
162 
163  // For each block in the original loop, create a new copy,
164  // and update the value map with the newly created values.
165  for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
166  BasicBlock *NewBB = CloneBasicBlock(*BB, VMap, ".prol", F);
167  NewBlocks.push_back(NewBB);
168 
169  if (NewLoop)
170  NewLoop->addBasicBlockToLoop(NewBB, *LI);
171  else if (ParentLoop)
172  ParentLoop->addBasicBlockToLoop(NewBB, *LI);
173 
174  VMap[*BB] = NewBB;
175  if (Header == *BB) {
176  // For the first block, add a CFG connection to this newly
177  // created block.
178  InsertTop->getTerminator()->setSuccessor(0, NewBB);
179 
180  }
181  if (Latch == *BB) {
182  // For the last block, if UnrollProlog is true, create a direct jump to
183  // InsertBot. If not, create a loop back to cloned head.
184  VMap.erase((*BB)->getTerminator());
185  BasicBlock *FirstLoopBB = cast<BasicBlock>(VMap[Header]);
186  BranchInst *LatchBR = cast<BranchInst>(NewBB->getTerminator());
187  IRBuilder<> Builder(LatchBR);
188  if (UnrollProlog) {
189  Builder.CreateBr(InsertBot);
190  } else {
191  PHINode *NewIdx = PHINode::Create(NewIter->getType(), 2, "prol.iter",
192  FirstLoopBB->getFirstNonPHI());
193  Value *IdxSub =
194  Builder.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1),
195  NewIdx->getName() + ".sub");
196  Value *IdxCmp =
197  Builder.CreateIsNotNull(IdxSub, NewIdx->getName() + ".cmp");
198  Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot);
199  NewIdx->addIncoming(NewIter, InsertTop);
200  NewIdx->addIncoming(IdxSub, NewBB);
201  }
202  LatchBR->eraseFromParent();
203  }
204  }
205 
206  // Change the incoming values to the ones defined in the preheader or
207  // cloned loop.
208  for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
209  PHINode *NewPHI = cast<PHINode>(VMap[I]);
210  if (UnrollProlog) {
211  VMap[I] = NewPHI->getIncomingValueForBlock(Preheader);
212  cast<BasicBlock>(VMap[Header])->getInstList().erase(NewPHI);
213  } else {
214  unsigned idx = NewPHI->getBasicBlockIndex(Preheader);
215  NewPHI->setIncomingBlock(idx, InsertTop);
216  BasicBlock *NewLatch = cast<BasicBlock>(VMap[Latch]);
217  idx = NewPHI->getBasicBlockIndex(Latch);
218  Value *InVal = NewPHI->getIncomingValue(idx);
219  NewPHI->setIncomingBlock(idx, NewLatch);
220  if (VMap[InVal])
221  NewPHI->setIncomingValue(idx, VMap[InVal]);
222  }
223  }
224  if (NewLoop) {
225  // Add unroll disable metadata to disable future unrolling for this loop.
227  // Reserve first location for self reference to the LoopID metadata node.
228  MDs.push_back(nullptr);
229  MDNode *LoopID = NewLoop->getLoopID();
230  if (LoopID) {
231  // First remove any existing loop unrolling metadata.
232  for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
233  bool IsUnrollMetadata = false;
234  MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
235  if (MD) {
236  const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
237  IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
238  }
239  if (!IsUnrollMetadata)
240  MDs.push_back(LoopID->getOperand(i));
241  }
242  }
243 
244  LLVMContext &Context = NewLoop->getHeader()->getContext();
245  SmallVector<Metadata *, 1> DisableOperands;
246  DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
247  MDNode *DisableNode = MDNode::get(Context, DisableOperands);
248  MDs.push_back(DisableNode);
249 
250  MDNode *NewLoopID = MDNode::get(Context, MDs);
251  // Set operand 0 to refer to the loop id itself.
252  NewLoopID->replaceOperandWith(0, NewLoopID);
253  NewLoop->setLoopID(NewLoopID);
254  }
255 }
256 
257 /// Insert code in the prolog code when unrolling a loop with a
258 /// run-time trip-count.
259 ///
260 /// This method assumes that the loop unroll factor is total number
261 /// of loop bodes in the loop after unrolling. (Some folks refer
262 /// to the unroll factor as the number of *extra* copies added).
263 /// We assume also that the loop unroll factor is a power-of-two. So, after
264 /// unrolling the loop, the number of loop bodies executed is 2,
265 /// 4, 8, etc. Note - LLVM converts the if-then-sequence to a switch
266 /// instruction in SimplifyCFG.cpp. Then, the backend decides how code for
267 /// the switch instruction is generated.
268 ///
269 /// extraiters = tripcount % loopfactor
270 /// if (extraiters == 0) jump Loop:
271 /// else jump Prol
272 /// Prol: LoopBody;
273 /// extraiters -= 1 // Omitted if unroll factor is 2.
274 /// if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
275 /// if (tripcount < loopfactor) jump End
276 /// Loop:
277 /// ...
278 /// End:
279 ///
280 bool llvm::UnrollRuntimeLoopProlog(Loop *L, unsigned Count,
281  bool AllowExpensiveTripCount, LoopInfo *LI,
282  LPPassManager *LPM) {
283  // for now, only unroll loops that contain a single exit
284  if (!L->getExitingBlock())
285  return false;
286 
287  // Make sure the loop is in canonical form, and there is a single
288  // exit block only.
289  if (!L->isLoopSimplifyForm() || !L->getUniqueExitBlock())
290  return false;
291 
292  // Use Scalar Evolution to compute the trip count. This allows more
293  // loops to be unrolled than relying on induction var simplification
294  if (!LPM)
295  return false;
297  if (!SE)
298  return false;
299 
300  // Only unroll loops with a computable trip count and the trip count needs
301  // to be an int value (allowing a pointer type is a TODO item)
302  const SCEV *BECountSC = SE->getBackedgeTakenCount(L);
303  if (isa<SCEVCouldNotCompute>(BECountSC) ||
304  !BECountSC->getType()->isIntegerTy())
305  return false;
306 
307  unsigned BEWidth = cast<IntegerType>(BECountSC->getType())->getBitWidth();
308 
309  // Add 1 since the backedge count doesn't include the first loop iteration
310  const SCEV *TripCountSC =
311  SE->getAddExpr(BECountSC, SE->getConstant(BECountSC->getType(), 1));
312  if (isa<SCEVCouldNotCompute>(TripCountSC))
313  return false;
314 
315  BasicBlock *Header = L->getHeader();
316  const DataLayout &DL = Header->getModule()->getDataLayout();
317  SCEVExpander Expander(*SE, DL, "loop-unroll");
318  if (!AllowExpensiveTripCount && Expander.isHighCostExpansion(TripCountSC, L))
319  return false;
320 
321  // We only handle cases when the unroll factor is a power of 2.
322  // Count is the loop unroll factor, the number of extra copies added + 1.
323  if (!isPowerOf2_32(Count))
324  return false;
325 
326  // This constraint lets us deal with an overflowing trip count easily; see the
327  // comment on ModVal below.
328  if (Log2_32(Count) > BEWidth)
329  return false;
330 
331  // If this loop is nested, then the loop unroller changes the code in
332  // parent loop, so the Scalar Evolution pass needs to be run again
333  if (Loop *ParentLoop = L->getParentLoop())
334  SE->forgetLoop(ParentLoop);
335 
336  // Grab analyses that we preserve.
338  auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
339 
340  BasicBlock *PH = L->getLoopPreheader();
341  BasicBlock *Latch = L->getLoopLatch();
342  // It helps to splits the original preheader twice, one for the end of the
343  // prolog code and one for a new loop preheader
344  BasicBlock *PEnd = SplitEdge(PH, Header, DT, LI);
345  BasicBlock *NewPH = SplitBlock(PEnd, PEnd->getTerminator(), DT, LI);
346  BranchInst *PreHeaderBR = cast<BranchInst>(PH->getTerminator());
347 
348  // Compute the number of extra iterations required, which is:
349  // extra iterations = run-time trip count % (loop unroll factor + 1)
350  Value *TripCount = Expander.expandCodeFor(TripCountSC, TripCountSC->getType(),
351  PreHeaderBR);
352  Value *BECount = Expander.expandCodeFor(BECountSC, BECountSC->getType(),
353  PreHeaderBR);
354 
355  IRBuilder<> B(PreHeaderBR);
356  Value *ModVal = B.CreateAnd(TripCount, Count - 1, "xtraiter");
357 
358  // If ModVal is zero, we know that either
359  // 1. there are no iteration to be run in the prologue loop
360  // OR
361  // 2. the addition computing TripCount overflowed
362  //
363  // If (2) is true, we know that TripCount really is (1 << BEWidth) and so the
364  // number of iterations that remain to be run in the original loop is a
365  // multiple Count == (1 << Log2(Count)) because Log2(Count) <= BEWidth (we
366  // explicitly check this above).
367 
368  Value *BranchVal = B.CreateIsNotNull(ModVal, "lcmp.mod");
369 
370  // Branch to either the extra iterations or the cloned/unrolled loop
371  // We will fix up the true branch label when adding loop body copies
372  B.CreateCondBr(BranchVal, PEnd, PEnd);
373  assert(PreHeaderBR->isUnconditional() &&
374  PreHeaderBR->getSuccessor(0) == PEnd &&
375  "CFG edges in Preheader are not correct");
376  PreHeaderBR->eraseFromParent();
377  Function *F = Header->getParent();
378  // Get an ordered list of blocks in the loop to help with the ordering of the
379  // cloned blocks in the prolog code
380  LoopBlocksDFS LoopBlocks(L);
381  LoopBlocks.perform(LI);
382 
383  //
384  // For each extra loop iteration, create a copy of the loop's basic blocks
385  // and generate a condition that branches to the copy depending on the
386  // number of 'left over' iterations.
387  //
388  std::vector<BasicBlock *> NewBlocks;
389  ValueToValueMapTy VMap;
390 
391  bool UnrollPrologue = Count == 2;
392 
393  // Clone all the basic blocks in the loop. If Count is 2, we don't clone
394  // the loop, otherwise we create a cloned loop to execute the extra
395  // iterations. This function adds the appropriate CFG connections.
396  CloneLoopBlocks(L, ModVal, UnrollPrologue, PH, PEnd, NewBlocks, LoopBlocks,
397  VMap, LI);
398 
399  // Insert the cloned blocks into function just before the original loop
400  F->getBasicBlockList().splice(PEnd, F->getBasicBlockList(), NewBlocks[0],
401  F->end());
402 
403  // Rewrite the cloned instruction operands to use the values
404  // created when the clone is created.
405  for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i) {
406  for (BasicBlock::iterator I = NewBlocks[i]->begin(),
407  E = NewBlocks[i]->end();
408  I != E; ++I) {
409  RemapInstruction(I, VMap,
411  }
412  }
413 
414  // Connect the prolog code to the original loop and update the
415  // PHI functions.
416  BasicBlock *LastLoopBB = cast<BasicBlock>(VMap[Latch]);
417  ConnectProlog(L, BECount, Count, LastLoopBB, PEnd, PH, NewPH, VMap,
418  /*AliasAnalysis*/ nullptr, DT, LI, LPM->getAsPass());
419  NumRuntimeUnrolled++;
420  return true;
421 }
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
BasicBlock * getUniqueExitBlock() const
getUniqueExitBlock - If getUniqueExitBlocks would return exactly one block, return that block...
Definition: LoopInfo.cpp:395
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
const SCEV * getConstant(ConstantInt *V)
STATISTIC(NumFunctions,"Total number of functions")
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitBlock - Split the specified block at the specified instruction - every thing before SplitPt stay...
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:743
iterator end()
Definition: Function.h:459
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:360
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:942
ScalarEvolution - This class is the main scalar evolution driver.
This file contains the declarations for metadata subclasses.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1366
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:232
LoopT * getParentLoop() const
Definition: LoopInfo.h:97
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
Metadata node.
Definition: Metadata.h:740
F(f)
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition: IRBuilder.h:622
BlockT * getHeader() const
Definition: LoopInfo.h:96
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
BlockT * getLoopLatch() const
getLoopLatch - If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:156
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
static void CloneLoopBlocks(Loop *L, Value *NewIter, const bool UnrollProlog, BasicBlock *InsertTop, BasicBlock *InsertBot, std::vector< BasicBlock * > &NewBlocks, LoopBlocksDFS &LoopBlocks, ValueToValueMapTy &VMap, LoopInfo *LI)
Create a clone of the blocks in a loop and connect them together.
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
Definition: BasicBlock.cpp:116
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:165
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:517
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable, but works if you just have an AnalysisID.
Definition: Pass.cpp:48
DominatorTree & getDomTree()
Definition: Dominators.h:213
bool isLoopSimplifyForm() const
isLoopSimplifyForm - Return true if the Loop is in the form that the LoopSimplify form transforms loo...
Definition: LoopInfo.cpp:199
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:104
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:878
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
addBasicBlockToLoop - This method is used by other analyses to update loop information.
Definition: LoopInfoImpl.h:187
void addChildLoop(LoopT *NewChild)
addChildLoop - Add the specified loop to be a child of this loop.
Definition: LoopInfo.h:265
void setSuccessor(unsigned idx, BasicBlock *B)
Update the specified successor to point at the provided block.
Definition: InstrTypes.h:67
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Definition: LoopInfo.cpp:731
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
RF_NoModuleLevelChanges - If this flag is set, the remapper knows that only local values within a fun...
Definition: ValueMapper.h:57
RF_IgnoreMissingEntries - If this flag is set, the remapper ignores entries that are not in the value...
Definition: ValueMapper.h:62
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:107
#define P(N)
friend const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:240
BlockT * getLoopPreheader() const
getLoopPreheader - If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:108
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
Type * getType() const
getType - Return the LLVM type of this SCEV expression.
BranchInst - Conditional or Unconditional Branch instruction.
bool isHighCostExpansion(const SCEV *Expr, Loop *L)
Return true for expressions that may incur non-trivial cost to evaluate at runtime.
char & LCSSAID
Definition: LCSSA.cpp:312
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:114
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition: LoopInfo.cpp:228
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:105
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
std::vector< BasicBlock * >::const_reverse_iterator RPOIterator
Definition: LoopIterator.h:42
BlockT * getExitingBlock() const
getExitingBlock - If getExitingBlocks would return exactly one block, return that block...
Definition: LoopInfoImpl.h:51
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:117
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
Definition: LoopInfo.cpp:262
StringRef getString() const
Definition: Metadata.cpp:375
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:936
const BasicBlockListType & getBasicBlockList() const
Definition: Function.h:436
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
static void ConnectProlog(Loop *L, Value *BECount, unsigned Count, BasicBlock *LastPrologBB, BasicBlock *PrologEnd, BasicBlock *OrigPH, BasicBlock *NewPH, ValueToValueMapTy &VMap, AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI, Pass *P)
Connect the unrolling prolog code to the original loop.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
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 addTopLevelLoop(LoopT *New)
addTopLevelLoop - This adds the specified loop to the collection of top-level loops.
Definition: LoopInfo.h:597
void splice(iterator where, iplist &L2)
Definition: ilist.h:570
unsigned Log2_32(uint32_t Value)
Log2_32 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...
Definition: MathExtras.h:468
Store the result of a depth first search within basic blocks contained by a single loop...
Definition: LoopIterator.h:38
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Definition: Type.h:193
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
RemapInstruction - Convert the instruction operands from referencing the current values into those sp...
This class uses information about analyze scalars to rewrite expressions in canonical form...
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getAddExpr - Get a canonical add expression, or something simpler if possible.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1030
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return an i1 value testing if Arg is not null.
Definition: IRBuilder.h:1574
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
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 ...
SCEV - This class represents an analyzed expression in the program.
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
Definition: LoopIterator.h:77
const SCEV * getBackedgeTakenCount(const Loop *L)
getBackedgeTakenCount - If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCouldNotCompute object.
LLVM Value Representation.
Definition: Value.h:69
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitEdge - Split the edge connecting specified block.
bool isPowerOf2_32(uint32_t Value)
isPowerOf2_32 - This function returns true if the argument is a power of two > 0. ...
Definition: MathExtras.h:354
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitBlockPredecessors - This method introduces at least one new basic block into the function and mo...
A single uniqued string.
Definition: Metadata.h:508
Pass * getAsPass() override
Definition: LoopPass.h:114
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
bool UnrollRuntimeLoopProlog(Loop *L, unsigned Count, bool AllowExpensiveTripCount, LoopInfo *LI, LPPassManager *LPM)
Insert code in the prolog code when unrolling a loop with a run-time trip-count.
void setIncomingValue(unsigned i, Value *V)
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr)
CloneBasicBlock - Return a copy of the specified basic block, but without embedding the block into a ...
int getBasicBlockIndex(const BasicBlock *BB) const
getBasicBlockIndex - Return the first index of the specified basic block in the value list for this P...
RPOIterator endRPO() const
Definition: LoopIterator.h:81
bool erase(const KeyT &Val)
Definition: ValueMap.h:168