101 #define DEBUG_TYPE "loop-idiom" 103 STATISTIC(NumMemSet,
"Number of memset's formed from loop stores");
104 STATISTIC(NumMemCpy,
"Number of memcpy's formed from loop load+stores");
107 "use-lir-code-size-heurs",
108 cl::desc(
"Use loop idiom recognition code size heuristics when compiling" 114 class LoopIdiomRecognize {
115 Loop *CurLoop =
nullptr;
123 bool ApplyCodeSizeHeuristics;
131 : AA(AA), DT(DT), LI(LI), SE(SE), TLI(TLI), TTI(TTI), DL(DL) {}
133 bool runOnLoop(
Loop *L);
139 StoreListMap StoreRefsForMemset;
140 StoreListMap StoreRefsForMemsetPattern;
141 StoreList StoreRefsForMemcpy;
143 bool HasMemsetPattern;
147 enum LegalStoreKind {
152 UnorderedAtomicMemcpy,
160 bool runOnCountableLoop();
166 enum class ForMemset {
No,
Yes };
171 bool processLoopStridedStore(
Value *DestPtr,
unsigned StoreSize,
172 unsigned StoreAlignment,
Value *StoredVal,
176 bool NegStride,
bool IsLoopMemset =
false);
178 bool avoidLIRForMultiBlockLoop(
bool IsMemset =
false,
179 bool IsLoopMemset =
false);
185 bool runOnNoncountableLoop();
187 bool recognizePopcount();
190 bool recognizeAndInsertFFS();
195 bool IsCntPhiUsedOutsideLoop);
200 class LoopIdiomRecognizeLegacyPass :
public LoopPass {
204 explicit LoopIdiomRecognizeLegacyPass() :
LoopPass(ID) {
213 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
214 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
215 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
216 ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
218 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
220 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
224 LoopIdiomRecognize LIR(AA, DT, LI, SE, TLI, TTI, DL);
225 return LIR.runOnLoop(L);
246 LoopIdiomRecognize LIR(&AR.
AA, &AR.
DT, &AR.
LI, &AR.
SE, &AR.
TLI, &AR.
TTI, DL);
247 if (!LIR.runOnLoop(&L))
254 "Recognize loop idioms",
false,
false)
274 bool LoopIdiomRecognize::runOnLoop(
Loop *L) {
283 if (Name ==
"memset" || Name ==
"memcpy")
287 ApplyCodeSizeHeuristics =
290 HasMemset = TLI->
has(LibFunc_memset);
291 HasMemsetPattern = TLI->
has(LibFunc_memset_pattern16);
292 HasMemcpy = TLI->
has(LibFunc_memcpy);
294 if (HasMemset || HasMemsetPattern || HasMemcpy)
296 return runOnCountableLoop();
298 return runOnNoncountableLoop();
301 bool LoopIdiomRecognize::runOnCountableLoop() {
303 assert(!isa<SCEVCouldNotCompute>(BECount) &&
304 "runOnCountableLoop() called on a loop without a predictable" 305 "backedge-taken count");
309 if (
const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
310 if (BECst->getAPInt() == 0)
314 CurLoop->getUniqueExitBlocks(ExitBlocks);
317 << CurLoop->getHeader()->getParent()->getName()
318 <<
"] Loop %" << CurLoop->getHeader()->getName() <<
"\n");
320 bool MadeChange =
false;
330 for (
auto *BB : CurLoop->getBlocks()) {
335 MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks);
364 if (Size == 0 || (Size & 7) || (Size & (Size - 1)))
384 unsigned ArraySize = 16 /
Size;
389 LoopIdiomRecognize::LegalStoreKind
411 uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->
getType());
412 if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
424 if (!isa<SCEVConstant>(StoreEv->
getOperand(1)))
441 if (!UnorderedAtomic && HasMemset && SplatValue &&
444 CurLoop->isLoopInvariant(SplatValue)) {
446 return LegalStoreKind::Memset;
447 }
else if (!UnorderedAtomic && HasMemsetPattern &&
452 return LegalStoreKind::MemsetPattern;
461 if (StoreSize != Stride && StoreSize != -Stride)
487 UnorderedAtomic = UnorderedAtomic || LI->
isAtomic();
488 return UnorderedAtomic ? LegalStoreKind::UnorderedAtomicMemcpy
489 : LegalStoreKind::Memcpy;
495 void LoopIdiomRecognize::collectStores(
BasicBlock *BB) {
496 StoreRefsForMemset.clear();
497 StoreRefsForMemsetPattern.clear();
498 StoreRefsForMemcpy.clear();
505 switch (isLegalStore(SI)) {
509 case LegalStoreKind::Memset: {
512 StoreRefsForMemset[Ptr].push_back(SI);
514 case LegalStoreKind::MemsetPattern: {
517 StoreRefsForMemsetPattern[Ptr].push_back(SI);
519 case LegalStoreKind::Memcpy:
520 case LegalStoreKind::UnorderedAtomicMemcpy:
521 StoreRefsForMemcpy.push_back(SI);
524 assert(
false &&
"unhandled return value");
533 bool LoopIdiomRecognize::runOnLoopBlock(
539 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i)
543 bool MadeChange =
false;
550 for (
auto &SL : StoreRefsForMemset)
551 MadeChange |= processLoopStores(SL.second, BECount,
ForMemset::Yes);
553 for (
auto &SL : StoreRefsForMemsetPattern)
554 MadeChange |= processLoopStores(SL.second, BECount,
ForMemset::No);
557 for (
auto &SI : StoreRefsForMemcpy)
558 MadeChange |= processLoopStoreOfLoopLoad(SI, BECount);
563 if (
MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) {
565 if (!processLoopMemSet(MSI, BECount))
582 const SCEV *BECount, ForMemset For) {
590 for (
unsigned i = 0, e = SL.
size(); i < e; ++i) {
591 assert(SL[i]->
isSimple() &&
"Expected only non-volatile stores.");
593 Value *FirstStoredVal = SL[i]->getValueOperand();
594 Value *FirstStorePtr = SL[i]->getPointerOperand();
596 cast<SCEVAddRecExpr>(SE->
getSCEV(FirstStorePtr));
598 unsigned FirstStoreSize = DL->getTypeStoreSize(SL[i]->getValueOperand()->
getType());
601 if (FirstStride == FirstStoreSize || -FirstStride == FirstStoreSize) {
606 Value *FirstSplatValue =
nullptr;
607 Constant *FirstPatternValue =
nullptr;
614 assert((FirstSplatValue || FirstPatternValue) &&
615 "Expected either splat value or pattern value.");
623 for (j = i + 1; j < e; ++j)
625 for (j = i; j > 0; --j)
628 for (
auto &k : IndexQueue) {
629 assert(SL[k]->
isSimple() &&
"Expected only non-volatile stores.");
630 Value *SecondStorePtr = SL[k]->getPointerOperand();
632 cast<SCEVAddRecExpr>(SE->
getSCEV(SecondStorePtr));
635 if (FirstStride != SecondStride)
638 Value *SecondStoredVal = SL[k]->getValueOperand();
639 Value *SecondSplatValue =
nullptr;
640 Constant *SecondPatternValue =
nullptr;
647 assert((SecondSplatValue || SecondPatternValue) &&
648 "Expected either splat value or pattern value.");
652 if (isa<UndefValue>(FirstSplatValue))
653 FirstSplatValue = SecondSplatValue;
654 if (FirstSplatValue != SecondSplatValue)
657 if (isa<UndefValue>(FirstPatternValue))
658 FirstPatternValue = SecondPatternValue;
659 if (FirstPatternValue != SecondPatternValue)
664 ConsecutiveChain[SL[i]] = SL[k];
673 bool Changed =
false;
678 if (Tails.
count(*it))
687 unsigned StoreSize = 0;
691 if (TransformedStores.
count(I))
697 I = ConsecutiveChain[
I];
707 if (StoreSize != Stride && StoreSize != -Stride)
710 bool NegStride = StoreSize == -Stride;
712 if (processLoopStridedStore(StorePtr, StoreSize, HeadStore->
getAlignment(),
713 StoredVal, HeadStore, AdjacentStores, StoreEv,
714 BECount, NegStride)) {
715 TransformedStores.
insert(AdjacentStores.begin(), AdjacentStores.end());
724 bool LoopIdiomRecognize::processLoopMemSet(
MemSetInst *MSI,
725 const SCEV *BECount) {
744 uint64_t SizeInBytes = cast<ConstantInt>(MSI->
getLength())->getZExtValue();
745 if ((SizeInBytes >> 32) != 0)
755 if (SizeInBytes != Stride && SizeInBytes != -Stride)
761 if (!SplatValue || !CurLoop->isLoopInvariant(SplatValue))
766 bool NegStride = SizeInBytes == -Stride;
767 return processLoopStridedStore(Pointer, (
unsigned)SizeInBytes,
769 Ev, BECount, NegStride,
true);
777 const SCEV *BECount,
unsigned StoreSize,
787 if (
const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
800 if (IgnoredStores.
count(&
I) == 0 &&
812 Type *IntPtr,
unsigned StoreSize,
826 unsigned StoreSize,
Loop *CurLoop,
828 const SCEV *NumBytesS;
849 if (StoreSize != 1) {
858 bool LoopIdiomRecognize::processLoopStridedStore(
859 Value *DestPtr,
unsigned StoreSize,
unsigned StoreAlignment,
862 const SCEV *BECount,
bool NegStride,
bool IsLoopMemset) {
869 assert((SplatValue || PatternValue) &&
870 "Expected either splat value or pattern value.");
876 BasicBlock *Preheader = CurLoop->getLoopPreheader();
881 Type *IntPtr = Builder.getIntPtrTy(*DL, DestAS);
899 Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->
getTerminator());
901 StoreSize, *AA, Stores)) {
908 if (avoidLIRForMultiBlockLoop(
true, IsLoopMemset))
913 const SCEV *NumBytesS =
914 getNumBytes(BECount, IntPtr, StoreSize, CurLoop, DL, SE);
922 Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->
getTerminator());
927 Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, StoreAlignment);
930 Type *Int8PtrTy = DestInt8PtrTy;
936 Int8PtrTy, Int8PtrTy, IntPtr);
943 PatternValue,
".memset_pattern");
947 NewCall = Builder.CreateCall(MSP, {BasePtr, PatternPtr, NumBytes});
951 <<
" from store to: " << *Ev <<
" at: " << *TheStore
957 for (
auto *
I : Stores)
966 bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(
StoreInst *SI,
967 const SCEV *BECount) {
974 bool NegStride = StoreSize == -Stride;
989 BasicBlock *Preheader = CurLoop->getLoopPreheader();
995 Type *IntPtrTy = Builder.getIntPtrTy(*DL, StrAS);
1007 Value *StoreBasePtr = Expander.expandCodeFor(
1008 StrStart, Builder.getInt8PtrTy(StrAS), Preheader->
getTerminator());
1013 StoreSize, *AA, Stores)) {
1020 const SCEV *LdStart = LoadEv->getStart();
1029 Value *LoadBasePtr = Expander.expandCodeFor(
1030 LdStart, Builder.getInt8PtrTy(LdAS), Preheader->
getTerminator());
1033 StoreSize, *AA, Stores)) {
1041 if (avoidLIRForMultiBlockLoop())
1046 const SCEV *NumBytesS =
1047 getNumBytes(BECount, IntPtrTy, StoreSize, CurLoop, DL, SE);
1050 Expander.expandCodeFor(NumBytesS, IntPtrTy, Preheader->
getTerminator());
1057 NewCall = Builder.CreateMemCpy(StoreBasePtr, SI->
getAlignment(),
1063 if (Align < StoreSize)
1076 NewCall = Builder.CreateElementUnorderedAtomicMemCpy(
1078 NumBytes, StoreSize);
1083 <<
" from load ptr=" << *LoadEv <<
" at: " << *LI <<
"\n" 1084 <<
" from store ptr=" << *StoreEv <<
" at: " << *SI
1097 bool LoopIdiomRecognize::avoidLIRForMultiBlockLoop(
bool IsMemset,
1098 bool IsLoopMemset) {
1099 if (ApplyCodeSizeHeuristics && CurLoop->getNumBlocks() > 1) {
1100 if (!CurLoop->getParentLoop() && (!IsMemset || !IsLoopMemset)) {
1101 LLVM_DEBUG(
dbgs() <<
" " << CurLoop->getHeader()->getParent()->getName()
1102 <<
" : LIR " << (IsMemset ?
"Memset" :
"Memcpy")
1103 <<
" avoided: multi-block top-level loop\n");
1111 bool LoopIdiomRecognize::runOnNoncountableLoop() {
1112 return recognizePopcount() || recognizeAndInsertFFS();
1122 bool JmpOnZero =
false) {
1131 if (!CmpZero || !CmpZero->
isZero())
1152 if (PhiX && PhiX->getParent() == LoopEntry &&
1153 (PhiX->getOperand(0) == DefX || PhiX->
getOperand(1) == DefX))
1189 Value *VarX1, *VarX0;
1192 DefX2 = CountInst =
nullptr;
1193 VarX1 = VarX0 =
nullptr;
1194 PhiX = CountPhi =
nullptr;
1200 dyn_cast<BranchInst>(LoopEntry->
getTerminator()), LoopEntry))
1201 DefX2 = dyn_cast<Instruction>(
T);
1208 if (!DefX2 || DefX2->
getOpcode() != Instruction::And)
1213 if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->
getOperand(0))))
1219 if (!SubOneOp || SubOneOp->
getOperand(0) != VarX1)
1238 CountInst =
nullptr;
1240 IterE = LoopEntry->
end();
1241 Iter != IterE; Iter++) {
1247 if (!Inc || !Inc->
isOne())
1255 bool LiveOutLoop =
false;
1257 if ((cast<Instruction>(U))->getParent() != LoopEntry) {
1282 CntInst = CountInst;
1322 Value *VarX =
nullptr;
1331 dyn_cast<BranchInst>(LoopEntry->
getTerminator()), LoopEntry))
1332 DefX = dyn_cast<Instruction>(
T);
1337 if (!DefX || !DefX->
isShift())
1342 if (!Shft || !Shft->
isOne())
1365 IterE = LoopEntry->
end();
1366 Iter != IterE; Iter++) {
1372 if (!Inc || !Inc->
isOne())
1392 bool LoopIdiomRecognize::recognizeAndInsertFFS() {
1394 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
1404 size_t IdiomCanonicalSize = 6;
1407 CntInst, CntPhi, DefX))
1410 bool IsCntPhiUsedOutsideLoop =
false;
1412 if (!CurLoop->contains(cast<Instruction>(U))) {
1413 IsCntPhiUsedOutsideLoop =
true;
1416 bool IsCntInstUsedOutsideLoop =
false;
1418 if (!CurLoop->contains(cast<Instruction>(U))) {
1419 IsCntInstUsedOutsideLoop =
true;
1424 if (IsCntInstUsedOutsideLoop && IsCntPhiUsedOutsideLoop)
1430 bool ZeroCheck =
false;
1433 BasicBlock *PH = CurLoop->getLoopPreheader();
1439 if (!IsCntPhiUsedOutsideLoop) {
1465 if (CurLoop->getHeader()->size() != IdiomCanonicalSize &&
1470 transformLoopToCountable(IntrinID, PH, CntInst, CntPhi, InitX, DefX,
1472 IsCntPhiUsedOutsideLoop);
1480 bool LoopIdiomRecognize::recognizePopcount() {
1490 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
1493 BasicBlock *LoopBody = *(CurLoop->block_begin());
1494 if (LoopBody->
size() >= 20) {
1500 BasicBlock *PH = CurLoop->getLoopPreheader();
1504 if (!EntryBI || EntryBI->isConditional())
1513 if (!PreCondBI || PreCondBI->isUnconditional())
1522 transformLoopToPopcount(PreCondBB, CntInst, CntPhi, Val);
1528 Value *Ops[] = {Val};
1540 const DebugLoc &DL,
bool ZeroCheck,
1584 void LoopIdiomRecognize::transformLoopToCountable(
1587 bool ZeroCheck,
bool IsCntPhiUsedOutsideLoop) {
1592 Builder.SetCurrentDebugLocation(DL);
1593 Value *FFS, *Count, *CountPrev, *NewCount, *InitXNext;
1598 if (IsCntPhiUsedOutsideLoop) {
1599 if (DefX->
getOpcode() == Instruction::AShr)
1602 else if (DefX->
getOpcode() == Instruction::LShr)
1605 else if (DefX->
getOpcode() == Instruction::Shl)
1613 Count = Builder.CreateSub(
1617 if (IsCntPhiUsedOutsideLoop) {
1619 Count = Builder.CreateAdd(
1624 NewCount = Builder.CreateZExtOrTrunc(
1625 IsCntPhiUsedOutsideLoop ? CountPrev : Count,
1626 cast<IntegerType>(CntInst->
getType()));
1631 if (!InitConst || !InitConst->
isZero())
1632 NewCount = Builder.CreateAdd(NewCount, CntInitVal);
1642 BasicBlock *Body = *(CurLoop->block_begin());
1644 ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
1645 Type *Ty = Count->getType();
1649 Builder.SetInsertPoint(LbCond);
1652 "tcdec",
false,
true));
1659 LbCond->setPredicate(Pred);
1660 LbCond->setOperand(0, TcDec);
1665 if (IsCntPhiUsedOutsideLoop)
1675 void LoopIdiomRecognize::transformLoopToPopcount(
BasicBlock *PreCondBB,
1678 BasicBlock *PreHead = CurLoop->getLoopPreheader();
1679 auto *PreCondBr = cast<BranchInst>(PreCondBB->
getTerminator());
1688 Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
1691 NewCount = PopCntZext =
1694 if (NewCount != PopCnt)
1695 (cast<Instruction>(NewCount))->setDebugLoc(DL);
1703 if (!InitConst || !InitConst->
isZero()) {
1704 NewCount = Builder.
CreateAdd(NewCount, CntInitVal);
1705 (cast<Instruction>(NewCount))->setDebugLoc(DL);
1714 ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition());
1716 Value *Opnd0 = PopCntZext;
1721 ICmpInst *NewPreCond = cast<ICmpInst>(
1723 PreCondBr->setCondition(NewPreCond);
1748 BasicBlock *Body = *(CurLoop->block_begin());
1751 ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
1759 "tcdec",
false,
true));
1766 LbCond->setPredicate(Pred);
1767 LbCond->setOperand(0, TcDec);
The access may reference and may modify the value stored in memory.
virtual void computeLoopSafetyInfo(const Loop *CurLoop)
Computes safety information for a loop checks loop body & header for the possibility of may throw exc...
Pass interface - Implemented by all 'passes'.
Value * getValueOperand()
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A parsed version of the target data layout string in and methods for querying it. ...
static ConstantInt * getFalse(LLVMContext &Context)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
void addIncoming(Value *V, BasicBlock *BB)
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...
static CallInst * createPopcntIntrinsic(IRBuilder<> &IRBuilder, Value *Val, const DebugLoc &DL)
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
const SCEV * getConstant(ConstantInt *V)
This class represents lattice values for constants.
BinaryOps getOpcode() const
Value * isBytewiseValue(Value *V)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
bool isAtomic() const
Return true if this instruction has an AtomicOrdering of unordered or higher.
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
ArrayRef< BasicBlock *>::const_iterator block_iterator
A Module instance is used to store all the information related to an LLVM module. ...
static constexpr LocationSize unknown()
void push_back(const T &Elt)
The main scalar evolution driver.
This class represents a function call, abstracting a target machine's calling convention.
static PHINode * getRecurrenceVar(Value *VarX, Instruction *DefX, BasicBlock *LoopEntry)
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Like Internal, but omit from symbol table.
LLVMContext & getContext() const
All values hold a context through their type.
static LocationSize precise(uint64_t Value)
This class wraps the llvm.memset intrinsic.
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
An instruction for reading from memory.
static Value * matchCondition(BranchInst *BI, BasicBlock *LoopEntry, bool JmpOnZero=false)
Check if the given conditional branch is based on the comparison between a variable and zero...
Value * getCondition() const
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...
void setAlignment(unsigned Align)
Value * getLength() const
iterator end()
Get an iterator to the end of the SetVector.
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions (including addrspacecast) that ...
iterator begin()
Instruction iterator methods.
Simple and conservative implementation of LoopSafetyInfo that can give false-positive answers to its ...
AnalysisUsage & addRequired()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
#define INITIALIZE_PASS_DEPENDENCY(depName)
bool isVolatile() const
Return true if this is a load from a volatile memory location.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
const Loop * getLoop() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This file contains the simple types necessary to represent the attributes associated with functions a...
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
unsigned getDestAlignment() const
This file implements a class to represent arbitrary precision integral constant values and operations...
const APInt & getAPInt() const
BlockT * getHeader() const
static bool isSimple(Instruction *I)
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Type * getType() const
All values are typed, get the type of this value.
Value handle that is nullable, but tries to track the Value.
bool insert(const value_type &X)
Insert a new element into the SetVector.
This node represents a polynomial recurrence on the trip count of the specified loop.
static APInt getStoreStride(const SCEVAddRecExpr *StoreEv)
BasicBlock * GetInsertBlock() const
Class to represent array types.
static cl::opt< bool > UseLIRCodeSizeHeurs("use-lir-code-size-heurs", cl::desc("Use loop idiom recognition code size heuristics when compiling" "with -Os/-Oz"), cl::init(true), cl::Hidden)
bool has(LibFunc F) const
Tests whether a library function is available.
iterator begin()
Get an iterator to the beginning of the SetVector.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
void initializeLoopIdiomRecognizeLegacyPassPass(PassRegistry &)
An instruction for storing to memory.
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
bool isKnownNonNegative(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Returns true if the give value is known to be non-negative.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static void deleteDeadInstruction(Instruction *I)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Value * getOperand(unsigned i) const
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
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. ...
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL, ScalarEvolution &SE, bool CheckType=true)
Returns true if the memory operations A and B are consecutive.
bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI)
Analyze the name and prototype of the given function and set any applicable attributes.
initializer< Ty > init(const Ty &Val)
const SCEV * getOperand(unsigned i) const
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
A set of analyses that are preserved following a run of a transformation pass.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
ConstantInt * getTrue()
Get the constant value for i1 true.
Conditional or Unconditional Branch instruction.
static CallInst * createFFSIntrinsic(IRBuilder<> &IRBuilder, Value *Val, const DebugLoc &DL, bool ZeroCheck, Intrinsic::ID IID)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const Instruction & front() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
const SCEV * getAddExpr(SmallVectorImpl< const SCEV *> &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
Represent the analysis usage information of a pass.
bool optForSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
static bool detectShiftUntilZeroIdiom(Loop *CurLoop, const DataLayout &DL, Intrinsic::ID &IntrinID, Value *&InitX, Instruction *&CntInst, PHINode *&CntPhi, Instruction *&DefX)
Return true if the idiom is detected in the loop.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values...
Value * getPointerOperand()
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
self_iterator getIterator()
const SCEV * getStart() const
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
static wasm::ValType getType(const TargetRegisterClass *RC)
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
const SCEV * getMulExpr(SmallVectorImpl< const SCEV *> &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
TargetTransformInfo & TTI
Representation for a specific memory location.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
typename vector_type::const_iterator iterator
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
Type * getType() const
Return the LLVM type of this SCEV expression.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
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.
bool isConditional() const
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...
static Constant * getMemSetPatternValue(Value *V, const DataLayout *DL)
getMemSetPatternValue - If a strided store of the specified value is safe to turn into a memset_patte...
static const SCEV * getNumBytes(const SCEV *BECount, Type *IntPtr, unsigned StoreSize, Loop *CurLoop, const DataLayout *DL, ScalarEvolution *SE)
Compute the number of bytes as a SCEV from the backedge taken count.
static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB, Instruction *&CntInst, PHINode *&CntPhi, Value *&Var)
Return true iff the idiom is detected in the loop.
static ConstantInt * getTrue(LLVMContext &Context)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
The access may modify the value stored in memory.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
loop Recognize loop idioms
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Class for arbitrary precision integers.
iterator_range< user_iterator > users()
This class uses information about analyze scalars to rewrite expressions in canonical form...
Pass * createLoopIdiomPass()
ConstantInt * getFalse()
Get the constant value for i1 false.
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Predicate getPredicate() const
Return the predicate for this instruction.
bool isVolatile() const
Return true if this is a store to a volatile memory location.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
INITIALIZE_PASS_BEGIN(LoopIdiomRecognizeLegacyPass, "loop-idiom", "Recognize loop idioms", false, false) INITIALIZE_PASS_END(LoopIdiomRecognizeLegacyPass
void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
void setUnnamedAddr(UnnamedAddr Val)
unsigned getAlignment() const
Return the alignment of the access that is being performed.
This class represents an analyzed expression in the program.
unsigned getIntegerBitWidth() const
Represents a single loop in the control flow graph.
This file provides utility analysis objects describing memory locations.
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
LLVM_NODISCARD ModRefInfo intersectModRef(const ModRefInfo MRI1, const ModRefInfo MRI2)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
block_iterator block_end() const
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
unsigned getAlignment() const
Return the alignment of the access that is being performed.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
static const SCEV * getStartForNegStride(const SCEV *Start, const SCEV *BECount, Type *IntPtr, unsigned StoreSize, ScalarEvolution *SE)
const SCEV * getBackedgeTakenCount(const Loop *L)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
A vector that has set insertion semantics.
static bool mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L, const SCEV *BECount, unsigned StoreSize, AliasAnalysis &AA, SmallPtrSetImpl< Instruction *> &IgnoredStores)
mayLoopAccessLocation - Return true if the specified loop might access the specified pointer location...
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB)
replaceUsesOutsideBlock - Go through the uses list for this definition and make each use point to "V"...
const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_NODISCARD bool isModOrRefSet(const ModRefInfo MRI)
This header defines various interfaces for pass management in LLVM.
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc)
getModRefInfo (for call sites) - Return information about whether a particular call site modifies or ...
virtual bool anyBlockMayThrow() const
Returns true iff any block of the loop for which this info is contains an instruction that may throw ...
bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE)
Return true if the given expression is safe to expand in the sense that all materialized values are s...
block_iterator block_begin() const
bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count...
Value * getPointerOperand()
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
This class represents a constant integer value.