45 #define DEBUG_TYPE "loop-utils" 58 "Must start with an empty predecessors list!");
63 bool IsDedicatedExit =
true;
66 if (isa<IndirectBrInst>(PredBB->getTerminator()))
72 IsDedicatedExit =
false;
75 assert(!InLoopPredecessors.
empty() &&
"Must have *some* loop predecessor!");
82 BB, InLoopPredecessors,
".loopexit", DT, LI,
nullptr, PreserveLCSSA);
86 dbgs() <<
"WARNING: Can't create a dedicated exit block for loop: " 89 LLVM_DEBUG(
dbgs() <<
"LoopSimplify: Creating dedicated exit block " 90 << NewExitBB->getName() <<
"\n");
97 for (
auto *BB : L->
blocks())
104 if (!Visited.
insert(SuccBB).second)
107 Changed |= RewriteExit(SuccBB);
120 for (
auto &Inst : *Block) {
121 auto Users = Inst.users();
123 auto *
Use = cast<Instruction>(U);
221 mdconst::extract_or_null<ConstantInt>(MD->
getOperand(1).
get()))
222 return IntMD->getZExtValue();
239 ConstantInt *IntMD = mdconst::extract_or_null<ConstantInt>(AttrMD->get());
248 const char *InheritOptionsExceptPrefix,
bool AlwaysNew) {
257 bool InheritAllAttrs = !InheritOptionsExceptPrefix;
258 bool InheritSomeAttrs =
259 InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] !=
'\0';
263 bool Changed =
false;
264 if (InheritAllAttrs || InheritSomeAttrs) {
266 MDNode *
Op = cast<MDNode>(Existing.get());
268 auto InheritThisAttribute = [InheritSomeAttrs,
269 InheritOptionsExceptPrefix](
MDNode *
Op) {
270 if (!InheritSomeAttrs)
277 if (!isa<MDString>(NameMD))
279 StringRef AttrName = cast<MDString>(NameMD)->getString();
282 return !AttrName.
startswith(InheritOptionsExceptPrefix);
285 if (InheritThisAttribute(Op))
295 bool HasAnyFollowup =
false;
296 for (
StringRef OptionName : FollowupOptions) {
301 HasAnyFollowup =
true;
310 if (!AlwaysNew && !HasAnyFollowup)
314 if (!AlwaysNew && !Changed)
324 return FollowupLoopID;
384 if (Enable ==
true && VectorizeWidth == 1 && InterleaveCount == 1)
393 if (VectorizeWidth == 1 && InterleaveCount == 1)
396 if (VectorizeWidth > 1 || InterleaveCount > 1)
437 AddRegionToWorklist(N);
439 for (
size_t I = 0;
I < Worklist.
size();
I++)
441 AddRegionToWorklist(Child);
451 assert(Preheader &&
"Preheader should exist!");
466 assert(ExitBlock &&
"Should have a unique exit block!");
470 assert(OldBr &&
"Preheader must end with a branch");
471 assert(OldBr->isUnconditional() &&
"Preheader must have a single successor");
498 Builder.CreateCondBr(Builder.getFalse(), L->
getHeader(), ExitBlock);
500 OldBr->eraseFromParent();
504 for (
PHINode &
P : ExitBlock->phis()) {
509 P.setIncomingBlock(PredIndex, Preheader);
517 for (
unsigned i = 0, e =
P.getNumIncomingValues() - 1; i != e; ++i)
518 P.removeIncomingValue(e - i,
false);
520 assert((
P.getNumIncomingValues() == 1 &&
521 P.getIncomingBlock(PredIndex) == Preheader) &&
522 "Should have exactly one value and that's from the preheader!");
526 Builder.SetInsertPoint(Preheader->getTerminator());
527 Builder.CreateBr(ExitBlock);
529 Preheader->getTerminator()->eraseFromParent();
552 for (
auto *Block : L->
blocks())
558 if (
auto *Usr = dyn_cast<Instruction>(U.
getUser()))
564 assert(!DT->isReachableFromEntry(U) &&
565 "Unexpected user in reachable block");
571 auto Key = DeadDebugSet.
find({DVI->getVariable(), DVI->getExpression()});
572 if (
Key != DeadDebugSet.
end())
574 DeadDebugSet.
insert({DVI->getVariable(), DVI->getExpression()});
584 for (
auto *DVI : DeadDebugInst)
585 DIB.insertDbgValueIntrinsic(
587 DVI->getExpression(), DVI->getDebugLoc(), ExitBlock->getFirstNonPHI());
591 for (
auto *Block : L->
blocks())
601 (*LpI)->eraseFromParent();
629 "At least one edge out of the latch must go to the header");
634 uint64_t TrueVal, FalseVal;
638 if (!TrueVal || !FalseVal)
644 return (TrueVal + (FalseVal / 2)) / FalseVal;
646 return (FalseVal + (TrueVal / 2)) / TrueVal;
657 const SCEV *InnerLoopBECountSC = SE.
getExitCount(InnerLoop, InnerLoopLatch);
658 if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
673 if (isa<FPMathOperator>(V)) {
676 cast<Instruction>(V)->setFastMathFlags(Flags);
718 Cmp = Builder.
CreateFCmp(P, Left, Right,
"rdx.minmax.cmp");
720 Cmp = Builder.
CreateICmp(P, Left, Right,
"rdx.minmax.cmp");
737 for (
unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) {
741 if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
767 "Reduction emission only supported for pow2 vectors!");
770 for (
unsigned i = VF; i != 1; i >>= 1) {
772 for (
unsigned j = 0; j != i / 2; ++j)
773 ShuffleMask[j] = Builder.
getInt32(i / 2 + j);
776 std::fill(&ShuffleMask[i / 2], ShuffleMask.
end(),
783 if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
786 TmpVec, Shuf,
"bin.rdx"));
805 assert(isa<VectorType>(Src->
getType()) &&
"Type must be a vector");
808 std::function<Value *()> BuildFunc;
810 RD::MinMaxRecurrenceKind MinMaxKind = RD::MRK_Invalid;
819 case Instruction::Mul:
822 case Instruction::And:
825 case Instruction::Or:
828 case Instruction::Xor:
831 case Instruction::FAdd:
834 cast<CallInst>(Rdx)->setFastMathFlags(FMFFast);
838 case Instruction::FMul:
841 cast<CallInst>(Rdx)->setFastMathFlags(FMFFast);
845 case Instruction::ICmp:
847 MinMaxKind = Flags.
IsSigned ? RD::MRK_SIntMax : RD::MRK_UIntMax;
852 MinMaxKind = Flags.
IsSigned ? RD::MRK_SIntMin : RD::MRK_UIntMin;
858 case Instruction::FCmp:
860 MinMaxKind = RD::MRK_FloatMax;
863 MinMaxKind = RD::MRK_FloatMin;
887 case RD::RK_FloatAdd:
889 case RD::RK_FloatMult:
891 case RD::RK_IntegerAdd:
893 case RD::RK_IntegerMult:
895 case RD::RK_IntegerAnd:
897 case RD::RK_IntegerOr:
899 case RD::RK_IntegerXor:
901 case RD::RK_IntegerMinMax: {
903 Flags.
IsMaxOp = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_UIntMax);
904 Flags.
IsSigned = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_SIntMin);
907 case RD::RK_FloatMinMax: {
920 auto *Intersection = (OpValue ==
nullptr) ? dyn_cast<Instruction>(VL[0])
924 const unsigned Opcode = Intersection->
getOpcode();
925 VecOp->copyIRFlags(Intersection);
930 if (OpValue ==
nullptr || Opcode == Instr->getOpcode())
Legacy wrapper pass to provide the GlobalsAAResult object.
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Type * getVectorElementType() const
Tracking metadata reference owned by Metadata.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
void setFast(bool B=true)
void dropAllReferences()
Drop all references to operands.
const SCEV * getConstant(ConstantInt *V)
This class represents lattice values for constants.
This is the interface for a simple mod/ref and alias analysis over globals.
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
bool hasDedicatedExits() const
Return true if no exit block for the loop has a predecessor that is outside the loop.
bool isLCSSAForm(DominatorTree &DT) const
Return true if the Loop is in LCSSA form.
void push_back(const T &Elt)
bool cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, bool Signed)
Returns true if S is defined and never is equal to signed/unsigned min.
The main scalar evolution driver.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION
bool isKnownNonNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Returns true if we can prove that S is defined and always non-negative in loop L. ...
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L)
Determine if the SCEV can be evaluated at loop's entry.
0 1 0 0 True if ordered and less than
Value * createSimpleTargetReduction(IRBuilder<> &B, const TargetTransformInfo *TTI, unsigned Opcode, Value *Src, TargetTransformInfo::ReductionFlags Flags=TargetTransformInfo::ReductionFlags(), ArrayRef< Value *> RedOps=None)
Create a target reduction of the given vector.
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
BasicBlock * getSuccessor(unsigned i) const
bool hasIterationCountInvariantInParent(Loop *L, ScalarEvolution &SE)
Check inner loop (L) backedge count is known to be invariant on all iterations of its outer loop...
MinMaxRecurrenceKind getMinMaxRecurrenceKind()
const MDOperand & getOperand(unsigned I) const
Value * getShuffleReduction(IRBuilder<> &Builder, Value *Src, unsigned Op, RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind=RecurrenceDescriptor::MRK_Invalid, ArrayRef< Value *> RedOps=None)
Generates a vector reduction using shufflevectors to reduce the value.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
iv Induction Variable Users
bool isKnownNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Returns true if we can prove that S is defined and always negative in loop L.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
The SCEV is loop-invariant.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
This is the interface for a SCEV-based alias analysis.
void initializeLoopPassPass(PassRegistry &)
Manually defined generic "LoopPass" dependency initialization.
unsigned getNumSuccessors() const
CallInst * CreateFAddReduce(Value *Acc, Value *Src)
Create a vector fadd reduction intrinsic of the source vector.
A Use represents the edge between a Value definition and its users.
TransformationMode hasUnrollAndJamTransformation(Loop *L)
bool isIntegerTy() const
True if this is an instance of IntegerType.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Contains a collection of routines for determining if a given instruction is guaranteed to execute if ...
SmallVector< Instruction *, 8 > findDefsUsedOutsideOfLoop(Loop *L)
Returns the instructions that use values defined in the loop.
BlockT * getHeader() const
The transformation should be applied without considering a cost model.
CallInst * CreateFPMinReduce(Value *Src, bool NoNaN=false)
Create a vector float min reduction intrinsic of the source vector.
void deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE, LoopInfo *LI)
This function deletes dead loops.
Value * getOrderedReduction(IRBuilder<> &Builder, Value *Acc, Value *Src, unsigned Op, RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind=RecurrenceDescriptor::MRK_Invalid, ArrayRef< Value *> RedOps=None)
Generates an ordered vector reduction using extracts to reduce the value.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Type * getType() const
All values are typed, get the type of this value.
op_range operands() const
const T & getValue() const LLVM_LVALUE_FUNCTION
LLVMContext & getContext() const
This is the common base class for debug info intrinsics for variables.
MDNode * findOptionMDForLoop(const Loop *TheLoop, StringRef Name)
Find string metadata for a loop.
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
AnalysisUsage & addPreservedID(const void *ID)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
The transformation must not be applied.
use_iterator_impl< Use > use_iterator
CallInst * CreateXorReduce(Value *Src)
Create a vector int XOR reduction intrinsic of the source vector.
bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS. ...
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock *> Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
static Optional< bool > getOptionalBoolLoopAttribute(const Loop *TheLoop, StringRef Name)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::Optional< int > getOptionalIntLoopAttribute(Loop *TheLoop, StringRef Name)
Find named metadata for a loop with an integer value.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
LLVM Basic Block Representation.
LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
Return the "disposition" of the given SCEV with respect to the given loop.
Conditional or Unconditional Branch instruction.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * createMinMaxOp(IRBuilder<> &Builder, RecurrenceDescriptor::MinMaxRecurrenceKind RK, Value *Left, Value *Right)
Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
std::pair< iterator, bool > insert(const ValueT &V)
static const char * LLVMLoopDisableNonforced
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
TransformationMode hasDistributeTransformation(Loop *L)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
TransformationMode hasVectorizeTransformation(Loop *L)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Optional< unsigned > getLoopEstimatedTripCount(Loop *L)
Get a loop's estimated trip count based on branch weight metadata.
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
BlockT * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
iterator_range< decltype(adl_begin(std::declval< T >)))> drop_begin(T &&t, int n)
RecurrenceKind getRecurrenceKind()
void deleteEdge(BasicBlock *From, BasicBlock *To)
Notify all available trees on an edge deletion.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void propagateIRFlags(Value *I, ArrayRef< Value *> VL, Value *OpValue=nullptr)
Get the intersection (logical and) of all of the potential IR flags of each scalar operation (VL) tha...
The transformation should not be applied.
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
0 0 1 0 True if ordered and greater than
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
void insertEdge(BasicBlock *From, BasicBlock *To)
Notify all available trees on an edge insertion.
The pass can use heuristics to determine whether a transformation should be applied.
CallInst * CreateAddReduce(Value *Src)
Create a vector int add reduction intrinsic of the source vector.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
Legacy wrapper pass to provide the SCEVAAResult object.
The transformation was directed by the user, e.g.
Type * getType() const
Return the LLVM type of this SCEV expression.
CallInst * CreateIntMaxReduce(Value *Src, bool IsSigned=false)
Create a vector integer max reduction intrinsic of the source vector.
AnalysisUsage & addRequiredID(const void *ID)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name)
Module.h This file contains the declarations for the Module class.
MDNode * findOptionMDForLoopID(MDNode *LoopID, StringRef Name)
Find and return the loop attribute node for the attribute Name in LoopID.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
pred_range predecessors(BasicBlock *BB)
CallInst * CreateFMulReduce(Value *Acc, Value *Src)
Create a vector fmul reduction intrinsic of the source vector.
Value * createTargetReduction(IRBuilder<> &B, const TargetTransformInfo *TTI, RecurrenceDescriptor &Desc, Value *Src, bool NoNaN=false)
Create a generic target reduction using a recurrence descriptor Desc The target is queried to determi...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Implements a dense probed hash-table based set with some number of buckets stored inline...
Optional< MDNode * > makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef< StringRef > FollowupAttrs, const char *InheritOptionsAttrsPrefix="", bool AlwaysNew=false)
Create a new loop identifier for a loop created from a loop transformation.
unsigned getVectorNumElements() const
LoopDisposition
An enum describing the relationship between a SCEV and a loop.
Class for arbitrary precision integers.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
LoopT * getParentLoop() const
CallInst * CreateAndReduce(Value *Src)
Create a vector int AND reduction intrinsic of the source vector.
CallInst * CreateOrReduce(Value *Src)
Create a vector int OR reduction intrinsic of the source vector.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
CallInst * CreateIntMinReduce(Value *Src, bool IsSigned=false)
Create a vector integer min reduction intrinsic of the source vector.
This class represents an analyzed expression in the program.
LLVM_NODISCARD bool empty() const
Represents a single loop in the control flow graph.
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
iterator find(const_arg_type_t< ValueT > V)
Optional< const MDOperand * > findStringMetadataForLoop(const Loop *TheLoop, StringRef Name)
Find string metadata for loop.
TransformationMode hasUnrollTransformation(Loop *L)
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
CallInst * CreateMulReduce(Value *Src)
Create a vector int mul reduction intrinsic of the source vector.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
TransformationMode
The mode sets how eager a transformation should be applied.
block_iterator block_end() const
SmallVector< DomTreeNode *, 16 > collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop)
Does a BFS from a given node to all of its children inside a given loop.
TransformationMode hasLICMVersioningTransformation(Loop *L)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
LLVM Value Representation.
succ_range successors(Instruction *I)
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
The legacy pass manager's analysis pass to compute loop information.
Convenience struct for specifying and reasoning about fast-math flags.
StringRef - Represent a constant reference to a string, i.e.
This is the interface for LLVM's primary stateless and local alias analysis.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
const SCEV * getExitCount(const Loop *L, BasicBlock *ExitingBlock)
Get the expression for the number of loop iterations for which this loop is guaranteed not to exit vi...
Legacy analysis pass which computes a DominatorTree.
bool cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, bool Signed)
Returns true if S is defined and never is equal to signed/unsigned max.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
bool hasDisableAllTransformsHint(const Loop *L)
Look for the loop attribute that disables all transformation heuristic.
CallInst * CreateFPMaxReduce(Value *Src, bool NoNaN=false)
Create a vector float max reduction intrinsic of the source vector.
unsigned getNumOperands() const
Return number of MDNode operands.
bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const
Retrieve the raw weight values of a conditional branch or select.
iterator_range< block_iterator > blocks() const
static Value * addFastMathFlag(Value *V)
Adds a 'fast' flag to floating point operations.
block_iterator block_begin() const
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
static Constant * get(ArrayRef< Constant *> V)
bool empty() const
empty - Check if the array is empty.
Legacy wrapper pass to provide the BasicAAResult object.