44 #define DEBUG_TYPE "loop-unroll"
47 "Number of loops unrolled with run-time trip counts");
68 assert(Latch &&
"Loop must have a latch");
118 assert(Count != 0 &&
"nonsensical Count!");
127 assert(Exit &&
"Loop must have a single exit block only");
144 std::vector<BasicBlock *> &NewBlocks,
156 NewLoop =
new Loop();
167 NewBlocks.push_back(NewBB);
184 VMap.
erase((*BB)->getTerminator());
185 BasicBlock *FirstLoopBB = cast<BasicBlock>(VMap[Header]);
189 Builder.CreateBr(InsertBot);
197 Builder.CreateIsNotNull(IdxSub, NewIdx->
getName() +
".cmp");
198 Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot);
202 LatchBR->eraseFromParent();
209 PHINode *NewPHI = cast<PHINode>(VMap[
I]);
212 cast<BasicBlock>(VMap[Header])->getInstList().erase(NewPHI);
216 BasicBlock *NewLatch = cast<BasicBlock>(VMap[Latch]);
232 for (
unsigned i = 1, ie = LoopID->
getNumOperands(); i < ie; ++i) {
233 bool IsUnrollMetadata =
false;
239 if (!IsUnrollMetadata)
281 bool AllowExpensiveTripCount,
LoopInfo *LI,
303 if (isa<SCEVCouldNotCompute>(BECountSC) ||
310 const SCEV *TripCountSC =
312 if (isa<SCEVCouldNotCompute>(TripCountSC))
338 auto *DT = DTWP ? &DTWP->
getDomTree() :
nullptr;
373 assert(PreHeaderBR->isUnconditional() &&
374 PreHeaderBR->getSuccessor(0) == PEnd &&
375 "CFG edges in Preheader are not correct");
388 std::vector<BasicBlock *> NewBlocks;
391 bool UnrollPrologue = Count == 2;
396 CloneLoopBlocks(L, ModVal, UnrollPrologue, PH, PEnd, NewBlocks, LoopBlocks,
405 for (
unsigned i = 0, e = NewBlocks.size(); i != e; ++i) {
407 E = NewBlocks[i]->
end();
416 BasicBlock *LastLoopBB = cast<BasicBlock>(VMap[Latch]);
417 ConnectProlog(L, BECount, Count, LastLoopBB, PEnd, PH, NewPH, VMap,
419 NumRuntimeUnrolled++;
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'.
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
BasicBlock * getUniqueExitBlock() const
getUniqueExitBlock - If getUniqueExitBlocks would return exactly one block, return that block...
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.
static MDString * get(LLVMContext &Context, StringRef Str)
unsigned getNumOperands() const
Return number of MDNode operands.
ScalarEvolution - This class is the main scalar evolution driver.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
const_iterator begin(StringRef path)
Get begin iterator over path.
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
BlockT * getHeader() const
StringRef getName() const
Return a constant reference to the value's name.
BlockT * getLoopLatch() const
getLoopLatch - If there is a single latch block for this loop, return it.
iterator begin()
Instruction iterator methods.
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...
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable, but works if you just have an AnalysisID.
DominatorTree & getDomTree()
bool isLoopSimplifyForm() const
isLoopSimplifyForm - Return true if the Loop is in the form that the LoopSimplify form transforms loo...
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...
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
addBasicBlockToLoop - This method is used by other analyses to update loop information.
void addChildLoop(LoopT *NewChild)
addChildLoop - Add the specified loop to be a child of this loop.
void setSuccessor(unsigned idx, BasicBlock *B)
Update the specified successor to point at the provided block.
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
RF_NoModuleLevelChanges - If this flag is set, the remapper knows that only local values within a fun...
RF_IgnoreMissingEntries - If this flag is set, the remapper ignores entries that are not in the value...
Interval::succ_iterator succ_end(Interval *I)
friend const_iterator end(StringRef path)
Get end iterator over path.
BlockT * getLoopPreheader() const
getLoopPreheader - If there is a preheader for this loop, return it.
LLVM Basic Block Representation.
This is an important class for using LLVM in a threaded context.
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.
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...
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
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
BlockT * getExitingBlock() const
getExitingBlock - If getExitingBlocks would return exactly one block, return that block...
Interval::pred_iterator pred_end(Interval *I)
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
StringRef getString() const
const MDOperand & getOperand(unsigned I) const
const BasicBlockListType & getBasicBlockList() const
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
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...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
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.
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.
void splice(iterator where, iplist &L2)
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...
Store the result of a depth first search within basic blocks contained by a single loop...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
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)
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)
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return an i1 value testing if Arg is not null.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
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.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
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.
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. ...
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...
Pass * getAsPass() override
Legacy analysis pass which computes a DominatorTree.
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
bool erase(const KeyT &Val)