176aa662cSKarthik Bhat //===-- LoopUtils.cpp - Loop Utility functions -------------------------===// 276aa662cSKarthik Bhat // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 676aa662cSKarthik Bhat // 776aa662cSKarthik Bhat //===----------------------------------------------------------------------===// 876aa662cSKarthik Bhat // 976aa662cSKarthik Bhat // This file defines common loop utility functions. 1076aa662cSKarthik Bhat // 1176aa662cSKarthik Bhat //===----------------------------------------------------------------------===// 1276aa662cSKarthik Bhat 132f2bd8caSAdam Nemet #include "llvm/Transforms/Utils/LoopUtils.h" 144a000883SChandler Carruth #include "llvm/ADT/ScopeExit.h" 1531088a9dSChandler Carruth #include "llvm/Analysis/AliasAnalysis.h" 1631088a9dSChandler Carruth #include "llvm/Analysis/BasicAliasAnalysis.h" 175f436fc5SRichard Trieu #include "llvm/Analysis/DomTreeUpdater.h" 1831088a9dSChandler Carruth #include "llvm/Analysis/GlobalsModRef.h" 19a21d5f1eSPhilip Reames #include "llvm/Analysis/InstructionSimplify.h" 202f2bd8caSAdam Nemet #include "llvm/Analysis/LoopInfo.h" 21c3ccf5d7SIgor Laevsky #include "llvm/Analysis/LoopPass.h" 226da79ce1SAlina Sbirlea #include "llvm/Analysis/MemorySSA.h" 2397468e92SAlina Sbirlea #include "llvm/Analysis/MemorySSAUpdater.h" 2423aed5efSPhilip Reames #include "llvm/Analysis/MustExecute.h" 2545d4cb9aSWeiming Zhao #include "llvm/Analysis/ScalarEvolution.h" 262f2bd8caSAdam Nemet #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 2745d4cb9aSWeiming Zhao #include "llvm/Analysis/ScalarEvolutionExpressions.h" 286bda14b3SChandler Carruth #include "llvm/Analysis/TargetTransformInfo.h" 29a097bc69SChad Rosier #include "llvm/Analysis/ValueTracking.h" 30744c3c32SDavide Italiano #include "llvm/IR/DIBuilder.h" 3131088a9dSChandler Carruth #include "llvm/IR/Dominators.h" 3276aa662cSKarthik Bhat #include "llvm/IR/Instructions.h" 33744c3c32SDavide Italiano #include "llvm/IR/IntrinsicInst.h" 3445d4cb9aSWeiming Zhao #include "llvm/IR/Module.h" 3576aa662cSKarthik Bhat #include "llvm/IR/PatternMatch.h" 3676aa662cSKarthik Bhat #include "llvm/IR/ValueHandle.h" 3705da2fe5SReid Kleckner #include "llvm/InitializePasses.h" 3831088a9dSChandler Carruth #include "llvm/Pass.h" 3976aa662cSKarthik Bhat #include "llvm/Support/Debug.h" 40a097bc69SChad Rosier #include "llvm/Support/KnownBits.h" 414a000883SChandler Carruth #include "llvm/Transforms/Utils/BasicBlockUtils.h" 4276aa662cSKarthik Bhat 4376aa662cSKarthik Bhat using namespace llvm; 4476aa662cSKarthik Bhat using namespace llvm::PatternMatch; 4576aa662cSKarthik Bhat 4676aa662cSKarthik Bhat #define DEBUG_TYPE "loop-utils" 4776aa662cSKarthik Bhat 4872448525SMichael Kruse static const char *LLVMLoopDisableNonforced = "llvm.loop.disable_nonforced"; 494f64f1baSTim Corringham static const char *LLVMLoopDisableLICM = "llvm.licm.disable"; 5072448525SMichael Kruse 514a000883SChandler Carruth bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, 5297468e92SAlina Sbirlea MemorySSAUpdater *MSSAU, 534a000883SChandler Carruth bool PreserveLCSSA) { 544a000883SChandler Carruth bool Changed = false; 554a000883SChandler Carruth 564a000883SChandler Carruth // We re-use a vector for the in-loop predecesosrs. 574a000883SChandler Carruth SmallVector<BasicBlock *, 4> InLoopPredecessors; 584a000883SChandler Carruth 594a000883SChandler Carruth auto RewriteExit = [&](BasicBlock *BB) { 604a000883SChandler Carruth assert(InLoopPredecessors.empty() && 614a000883SChandler Carruth "Must start with an empty predecessors list!"); 624a000883SChandler Carruth auto Cleanup = make_scope_exit([&] { InLoopPredecessors.clear(); }); 634a000883SChandler Carruth 644a000883SChandler Carruth // See if there are any non-loop predecessors of this exit block and 654a000883SChandler Carruth // keep track of the in-loop predecessors. 664a000883SChandler Carruth bool IsDedicatedExit = true; 674a000883SChandler Carruth for (auto *PredBB : predecessors(BB)) 684a000883SChandler Carruth if (L->contains(PredBB)) { 694a000883SChandler Carruth if (isa<IndirectBrInst>(PredBB->getTerminator())) 704a000883SChandler Carruth // We cannot rewrite exiting edges from an indirectbr. 714a000883SChandler Carruth return false; 72784929d0SCraig Topper if (isa<CallBrInst>(PredBB->getTerminator())) 73784929d0SCraig Topper // We cannot rewrite exiting edges from a callbr. 74784929d0SCraig Topper return false; 754a000883SChandler Carruth 764a000883SChandler Carruth InLoopPredecessors.push_back(PredBB); 774a000883SChandler Carruth } else { 784a000883SChandler Carruth IsDedicatedExit = false; 794a000883SChandler Carruth } 804a000883SChandler Carruth 814a000883SChandler Carruth assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!"); 824a000883SChandler Carruth 834a000883SChandler Carruth // Nothing to do if this is already a dedicated exit. 844a000883SChandler Carruth if (IsDedicatedExit) 854a000883SChandler Carruth return false; 864a000883SChandler Carruth 874a000883SChandler Carruth auto *NewExitBB = SplitBlockPredecessors( 8897468e92SAlina Sbirlea BB, InLoopPredecessors, ".loopexit", DT, LI, MSSAU, PreserveLCSSA); 894a000883SChandler Carruth 904a000883SChandler Carruth if (!NewExitBB) 91d34e60caSNicola Zaghen LLVM_DEBUG( 92d34e60caSNicola Zaghen dbgs() << "WARNING: Can't create a dedicated exit block for loop: " 934a000883SChandler Carruth << *L << "\n"); 944a000883SChandler Carruth else 95d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block " 964a000883SChandler Carruth << NewExitBB->getName() << "\n"); 974a000883SChandler Carruth return true; 984a000883SChandler Carruth }; 994a000883SChandler Carruth 1004a000883SChandler Carruth // Walk the exit blocks directly rather than building up a data structure for 1014a000883SChandler Carruth // them, but only visit each one once. 1024a000883SChandler Carruth SmallPtrSet<BasicBlock *, 4> Visited; 1034a000883SChandler Carruth for (auto *BB : L->blocks()) 1044a000883SChandler Carruth for (auto *SuccBB : successors(BB)) { 1054a000883SChandler Carruth // We're looking for exit blocks so skip in-loop successors. 1064a000883SChandler Carruth if (L->contains(SuccBB)) 1074a000883SChandler Carruth continue; 1084a000883SChandler Carruth 1094a000883SChandler Carruth // Visit each exit block exactly once. 1104a000883SChandler Carruth if (!Visited.insert(SuccBB).second) 1114a000883SChandler Carruth continue; 1124a000883SChandler Carruth 1134a000883SChandler Carruth Changed |= RewriteExit(SuccBB); 1144a000883SChandler Carruth } 1154a000883SChandler Carruth 1164a000883SChandler Carruth return Changed; 1174a000883SChandler Carruth } 1184a000883SChandler Carruth 1195f8f34e4SAdrian Prantl /// Returns the instructions that use values defined in the loop. 120c5b7b555SAshutosh Nema SmallVector<Instruction *, 8> llvm::findDefsUsedOutsideOfLoop(Loop *L) { 121c5b7b555SAshutosh Nema SmallVector<Instruction *, 8> UsedOutside; 122c5b7b555SAshutosh Nema 123c5b7b555SAshutosh Nema for (auto *Block : L->getBlocks()) 124c5b7b555SAshutosh Nema // FIXME: I believe that this could use copy_if if the Inst reference could 125c5b7b555SAshutosh Nema // be adapted into a pointer. 126c5b7b555SAshutosh Nema for (auto &Inst : *Block) { 127c5b7b555SAshutosh Nema auto Users = Inst.users(); 1280a16c228SDavid Majnemer if (any_of(Users, [&](User *U) { 129c5b7b555SAshutosh Nema auto *Use = cast<Instruction>(U); 130c5b7b555SAshutosh Nema return !L->contains(Use->getParent()); 131c5b7b555SAshutosh Nema })) 132c5b7b555SAshutosh Nema UsedOutside.push_back(&Inst); 133c5b7b555SAshutosh Nema } 134c5b7b555SAshutosh Nema 135c5b7b555SAshutosh Nema return UsedOutside; 136c5b7b555SAshutosh Nema } 13731088a9dSChandler Carruth 13831088a9dSChandler Carruth void llvm::getLoopAnalysisUsage(AnalysisUsage &AU) { 13931088a9dSChandler Carruth // By definition, all loop passes need the LoopInfo analysis and the 14031088a9dSChandler Carruth // Dominator tree it depends on. Because they all participate in the loop 14131088a9dSChandler Carruth // pass manager, they must also preserve these. 14231088a9dSChandler Carruth AU.addRequired<DominatorTreeWrapperPass>(); 14331088a9dSChandler Carruth AU.addPreserved<DominatorTreeWrapperPass>(); 14431088a9dSChandler Carruth AU.addRequired<LoopInfoWrapperPass>(); 14531088a9dSChandler Carruth AU.addPreserved<LoopInfoWrapperPass>(); 14631088a9dSChandler Carruth 14731088a9dSChandler Carruth // We must also preserve LoopSimplify and LCSSA. We locally access their IDs 14831088a9dSChandler Carruth // here because users shouldn't directly get them from this header. 14931088a9dSChandler Carruth extern char &LoopSimplifyID; 15031088a9dSChandler Carruth extern char &LCSSAID; 15131088a9dSChandler Carruth AU.addRequiredID(LoopSimplifyID); 15231088a9dSChandler Carruth AU.addPreservedID(LoopSimplifyID); 15331088a9dSChandler Carruth AU.addRequiredID(LCSSAID); 15431088a9dSChandler Carruth AU.addPreservedID(LCSSAID); 155c3ccf5d7SIgor Laevsky // This is used in the LPPassManager to perform LCSSA verification on passes 156c3ccf5d7SIgor Laevsky // which preserve lcssa form 157c3ccf5d7SIgor Laevsky AU.addRequired<LCSSAVerificationPass>(); 158c3ccf5d7SIgor Laevsky AU.addPreserved<LCSSAVerificationPass>(); 15931088a9dSChandler Carruth 16031088a9dSChandler Carruth // Loop passes are designed to run inside of a loop pass manager which means 16131088a9dSChandler Carruth // that any function analyses they require must be required by the first loop 16231088a9dSChandler Carruth // pass in the manager (so that it is computed before the loop pass manager 16331088a9dSChandler Carruth // runs) and preserved by all loop pasess in the manager. To make this 16431088a9dSChandler Carruth // reasonably robust, the set needed for most loop passes is maintained here. 16531088a9dSChandler Carruth // If your loop pass requires an analysis not listed here, you will need to 16631088a9dSChandler Carruth // carefully audit the loop pass manager nesting structure that results. 16731088a9dSChandler Carruth AU.addRequired<AAResultsWrapperPass>(); 16831088a9dSChandler Carruth AU.addPreserved<AAResultsWrapperPass>(); 16931088a9dSChandler Carruth AU.addPreserved<BasicAAWrapperPass>(); 17031088a9dSChandler Carruth AU.addPreserved<GlobalsAAWrapperPass>(); 17131088a9dSChandler Carruth AU.addPreserved<SCEVAAWrapperPass>(); 17231088a9dSChandler Carruth AU.addRequired<ScalarEvolutionWrapperPass>(); 17331088a9dSChandler Carruth AU.addPreserved<ScalarEvolutionWrapperPass>(); 1746da79ce1SAlina Sbirlea // FIXME: When all loop passes preserve MemorySSA, it can be required and 1756da79ce1SAlina Sbirlea // preserved here instead of the individual handling in each pass. 17631088a9dSChandler Carruth } 17731088a9dSChandler Carruth 17831088a9dSChandler Carruth /// Manually defined generic "LoopPass" dependency initialization. This is used 17931088a9dSChandler Carruth /// to initialize the exact set of passes from above in \c 18031088a9dSChandler Carruth /// getLoopAnalysisUsage. It can be used within a loop pass's initialization 18131088a9dSChandler Carruth /// with: 18231088a9dSChandler Carruth /// 18331088a9dSChandler Carruth /// INITIALIZE_PASS_DEPENDENCY(LoopPass) 18431088a9dSChandler Carruth /// 18531088a9dSChandler Carruth /// As-if "LoopPass" were a pass. 18631088a9dSChandler Carruth void llvm::initializeLoopPassPass(PassRegistry &Registry) { 18731088a9dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 18831088a9dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 18931088a9dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(LoopSimplify) 190e12c487bSEaswaran Raman INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) 19131088a9dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 19231088a9dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass) 19331088a9dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) 19431088a9dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass) 19531088a9dSChandler Carruth INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 1966da79ce1SAlina Sbirlea INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) 19731088a9dSChandler Carruth } 198963341c8SAdam Nemet 1993c3a7652SSerguei Katkov /// Create MDNode for input string. 2003c3a7652SSerguei Katkov static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) { 2013c3a7652SSerguei Katkov LLVMContext &Context = TheLoop->getHeader()->getContext(); 2023c3a7652SSerguei Katkov Metadata *MDs[] = { 2033c3a7652SSerguei Katkov MDString::get(Context, Name), 2043c3a7652SSerguei Katkov ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))}; 2053c3a7652SSerguei Katkov return MDNode::get(Context, MDs); 2063c3a7652SSerguei Katkov } 2073c3a7652SSerguei Katkov 2083c3a7652SSerguei Katkov /// Set input string into loop metadata by keeping other values intact. 2097f8c8095SSerguei Katkov /// If the string is already in loop metadata update value if it is 2107f8c8095SSerguei Katkov /// different. 2117f8c8095SSerguei Katkov void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD, 2123c3a7652SSerguei Katkov unsigned V) { 2133c3a7652SSerguei Katkov SmallVector<Metadata *, 4> MDs(1); 2143c3a7652SSerguei Katkov // If the loop already has metadata, retain it. 2153c3a7652SSerguei Katkov MDNode *LoopID = TheLoop->getLoopID(); 2163c3a7652SSerguei Katkov if (LoopID) { 2173c3a7652SSerguei Katkov for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { 2183c3a7652SSerguei Katkov MDNode *Node = cast<MDNode>(LoopID->getOperand(i)); 2197f8c8095SSerguei Katkov // If it is of form key = value, try to parse it. 2207f8c8095SSerguei Katkov if (Node->getNumOperands() == 2) { 2217f8c8095SSerguei Katkov MDString *S = dyn_cast<MDString>(Node->getOperand(0)); 2227f8c8095SSerguei Katkov if (S && S->getString().equals(StringMD)) { 2237f8c8095SSerguei Katkov ConstantInt *IntMD = 2247f8c8095SSerguei Katkov mdconst::extract_or_null<ConstantInt>(Node->getOperand(1)); 2257f8c8095SSerguei Katkov if (IntMD && IntMD->getSExtValue() == V) 2267f8c8095SSerguei Katkov // It is already in place. Do nothing. 2277f8c8095SSerguei Katkov return; 2287f8c8095SSerguei Katkov // We need to update the value, so just skip it here and it will 2297f8c8095SSerguei Katkov // be added after copying other existed nodes. 2307f8c8095SSerguei Katkov continue; 2317f8c8095SSerguei Katkov } 2327f8c8095SSerguei Katkov } 2333c3a7652SSerguei Katkov MDs.push_back(Node); 2343c3a7652SSerguei Katkov } 2353c3a7652SSerguei Katkov } 2363c3a7652SSerguei Katkov // Add new metadata. 2377f8c8095SSerguei Katkov MDs.push_back(createStringMetadata(TheLoop, StringMD, V)); 2383c3a7652SSerguei Katkov // Replace current metadata node with new one. 2393c3a7652SSerguei Katkov LLVMContext &Context = TheLoop->getHeader()->getContext(); 2403c3a7652SSerguei Katkov MDNode *NewLoopID = MDNode::get(Context, MDs); 2413c3a7652SSerguei Katkov // Set operand 0 to refer to the loop id itself. 2423c3a7652SSerguei Katkov NewLoopID->replaceOperandWith(0, NewLoopID); 2433c3a7652SSerguei Katkov TheLoop->setLoopID(NewLoopID); 2443c3a7652SSerguei Katkov } 2453c3a7652SSerguei Katkov 24672448525SMichael Kruse /// Find string metadata for loop 24772448525SMichael Kruse /// 24872448525SMichael Kruse /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an 24972448525SMichael Kruse /// operand or null otherwise. If the string metadata is not found return 25072448525SMichael Kruse /// Optional's not-a-value. 251978ba615SMichael Kruse Optional<const MDOperand *> llvm::findStringMetadataForLoop(const Loop *TheLoop, 25272448525SMichael Kruse StringRef Name) { 253978ba615SMichael Kruse MDNode *MD = findOptionMDForLoop(TheLoop, Name); 25472448525SMichael Kruse if (!MD) 25572448525SMichael Kruse return None; 256fe3def7cSAdam Nemet switch (MD->getNumOperands()) { 257fe3def7cSAdam Nemet case 1: 258fe3def7cSAdam Nemet return nullptr; 259fe3def7cSAdam Nemet case 2: 260fe3def7cSAdam Nemet return &MD->getOperand(1); 261fe3def7cSAdam Nemet default: 262fe3def7cSAdam Nemet llvm_unreachable("loop metadata has 0 or 1 operand"); 263963341c8SAdam Nemet } 264fe3def7cSAdam Nemet } 26572448525SMichael Kruse 26672448525SMichael Kruse static Optional<bool> getOptionalBoolLoopAttribute(const Loop *TheLoop, 26772448525SMichael Kruse StringRef Name) { 268978ba615SMichael Kruse MDNode *MD = findOptionMDForLoop(TheLoop, Name); 269978ba615SMichael Kruse if (!MD) 270fe3def7cSAdam Nemet return None; 271978ba615SMichael Kruse switch (MD->getNumOperands()) { 27272448525SMichael Kruse case 1: 27372448525SMichael Kruse // When the value is absent it is interpreted as 'attribute set'. 27472448525SMichael Kruse return true; 27572448525SMichael Kruse case 2: 276f9027e55SAlina Sbirlea if (ConstantInt *IntMD = 277f9027e55SAlina Sbirlea mdconst::extract_or_null<ConstantInt>(MD->getOperand(1).get())) 278f9027e55SAlina Sbirlea return IntMD->getZExtValue(); 279f9027e55SAlina Sbirlea return true; 28072448525SMichael Kruse } 28172448525SMichael Kruse llvm_unreachable("unexpected number of options"); 28272448525SMichael Kruse } 28372448525SMichael Kruse 28472448525SMichael Kruse static bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name) { 28572448525SMichael Kruse return getOptionalBoolLoopAttribute(TheLoop, Name).getValueOr(false); 28672448525SMichael Kruse } 28772448525SMichael Kruse 28872448525SMichael Kruse llvm::Optional<int> llvm::getOptionalIntLoopAttribute(Loop *TheLoop, 28972448525SMichael Kruse StringRef Name) { 29072448525SMichael Kruse const MDOperand *AttrMD = 29172448525SMichael Kruse findStringMetadataForLoop(TheLoop, Name).getValueOr(nullptr); 29272448525SMichael Kruse if (!AttrMD) 29372448525SMichael Kruse return None; 29472448525SMichael Kruse 29572448525SMichael Kruse ConstantInt *IntMD = mdconst::extract_or_null<ConstantInt>(AttrMD->get()); 29672448525SMichael Kruse if (!IntMD) 29772448525SMichael Kruse return None; 29872448525SMichael Kruse 29972448525SMichael Kruse return IntMD->getSExtValue(); 30072448525SMichael Kruse } 30172448525SMichael Kruse 30272448525SMichael Kruse Optional<MDNode *> llvm::makeFollowupLoopID( 30372448525SMichael Kruse MDNode *OrigLoopID, ArrayRef<StringRef> FollowupOptions, 30472448525SMichael Kruse const char *InheritOptionsExceptPrefix, bool AlwaysNew) { 30572448525SMichael Kruse if (!OrigLoopID) { 30672448525SMichael Kruse if (AlwaysNew) 30772448525SMichael Kruse return nullptr; 30872448525SMichael Kruse return None; 30972448525SMichael Kruse } 31072448525SMichael Kruse 31172448525SMichael Kruse assert(OrigLoopID->getOperand(0) == OrigLoopID); 31272448525SMichael Kruse 31372448525SMichael Kruse bool InheritAllAttrs = !InheritOptionsExceptPrefix; 31472448525SMichael Kruse bool InheritSomeAttrs = 31572448525SMichael Kruse InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] != '\0'; 31672448525SMichael Kruse SmallVector<Metadata *, 8> MDs; 31772448525SMichael Kruse MDs.push_back(nullptr); 31872448525SMichael Kruse 31972448525SMichael Kruse bool Changed = false; 32072448525SMichael Kruse if (InheritAllAttrs || InheritSomeAttrs) { 32172448525SMichael Kruse for (const MDOperand &Existing : drop_begin(OrigLoopID->operands(), 1)) { 32272448525SMichael Kruse MDNode *Op = cast<MDNode>(Existing.get()); 32372448525SMichael Kruse 32472448525SMichael Kruse auto InheritThisAttribute = [InheritSomeAttrs, 32572448525SMichael Kruse InheritOptionsExceptPrefix](MDNode *Op) { 32672448525SMichael Kruse if (!InheritSomeAttrs) 32772448525SMichael Kruse return false; 32872448525SMichael Kruse 32972448525SMichael Kruse // Skip malformatted attribute metadata nodes. 33072448525SMichael Kruse if (Op->getNumOperands() == 0) 33172448525SMichael Kruse return true; 33272448525SMichael Kruse Metadata *NameMD = Op->getOperand(0).get(); 33372448525SMichael Kruse if (!isa<MDString>(NameMD)) 33472448525SMichael Kruse return true; 33572448525SMichael Kruse StringRef AttrName = cast<MDString>(NameMD)->getString(); 33672448525SMichael Kruse 33772448525SMichael Kruse // Do not inherit excluded attributes. 33872448525SMichael Kruse return !AttrName.startswith(InheritOptionsExceptPrefix); 33972448525SMichael Kruse }; 34072448525SMichael Kruse 34172448525SMichael Kruse if (InheritThisAttribute(Op)) 34272448525SMichael Kruse MDs.push_back(Op); 34372448525SMichael Kruse else 34472448525SMichael Kruse Changed = true; 34572448525SMichael Kruse } 34672448525SMichael Kruse } else { 34772448525SMichael Kruse // Modified if we dropped at least one attribute. 34872448525SMichael Kruse Changed = OrigLoopID->getNumOperands() > 1; 34972448525SMichael Kruse } 35072448525SMichael Kruse 35172448525SMichael Kruse bool HasAnyFollowup = false; 35272448525SMichael Kruse for (StringRef OptionName : FollowupOptions) { 353978ba615SMichael Kruse MDNode *FollowupNode = findOptionMDForLoopID(OrigLoopID, OptionName); 35472448525SMichael Kruse if (!FollowupNode) 35572448525SMichael Kruse continue; 35672448525SMichael Kruse 35772448525SMichael Kruse HasAnyFollowup = true; 35872448525SMichael Kruse for (const MDOperand &Option : drop_begin(FollowupNode->operands(), 1)) { 35972448525SMichael Kruse MDs.push_back(Option.get()); 36072448525SMichael Kruse Changed = true; 36172448525SMichael Kruse } 36272448525SMichael Kruse } 36372448525SMichael Kruse 36472448525SMichael Kruse // Attributes of the followup loop not specified explicity, so signal to the 36572448525SMichael Kruse // transformation pass to add suitable attributes. 36672448525SMichael Kruse if (!AlwaysNew && !HasAnyFollowup) 36772448525SMichael Kruse return None; 36872448525SMichael Kruse 36972448525SMichael Kruse // If no attributes were added or remove, the previous loop Id can be reused. 37072448525SMichael Kruse if (!AlwaysNew && !Changed) 37172448525SMichael Kruse return OrigLoopID; 37272448525SMichael Kruse 37372448525SMichael Kruse // No attributes is equivalent to having no !llvm.loop metadata at all. 37472448525SMichael Kruse if (MDs.size() == 1) 37572448525SMichael Kruse return nullptr; 37672448525SMichael Kruse 37772448525SMichael Kruse // Build the new loop ID. 37872448525SMichael Kruse MDTuple *FollowupLoopID = MDNode::get(OrigLoopID->getContext(), MDs); 37972448525SMichael Kruse FollowupLoopID->replaceOperandWith(0, FollowupLoopID); 38072448525SMichael Kruse return FollowupLoopID; 38172448525SMichael Kruse } 38272448525SMichael Kruse 38372448525SMichael Kruse bool llvm::hasDisableAllTransformsHint(const Loop *L) { 38472448525SMichael Kruse return getBooleanLoopAttribute(L, LLVMLoopDisableNonforced); 38572448525SMichael Kruse } 38672448525SMichael Kruse 3874f64f1baSTim Corringham bool llvm::hasDisableLICMTransformsHint(const Loop *L) { 3884f64f1baSTim Corringham return getBooleanLoopAttribute(L, LLVMLoopDisableLICM); 3894f64f1baSTim Corringham } 3904f64f1baSTim Corringham 39172448525SMichael Kruse TransformationMode llvm::hasUnrollTransformation(Loop *L) { 39272448525SMichael Kruse if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable")) 39372448525SMichael Kruse return TM_SuppressedByUser; 39472448525SMichael Kruse 39572448525SMichael Kruse Optional<int> Count = 39672448525SMichael Kruse getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count"); 39772448525SMichael Kruse if (Count.hasValue()) 39872448525SMichael Kruse return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser; 39972448525SMichael Kruse 40072448525SMichael Kruse if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable")) 40172448525SMichael Kruse return TM_ForcedByUser; 40272448525SMichael Kruse 40372448525SMichael Kruse if (getBooleanLoopAttribute(L, "llvm.loop.unroll.full")) 40472448525SMichael Kruse return TM_ForcedByUser; 40572448525SMichael Kruse 40672448525SMichael Kruse if (hasDisableAllTransformsHint(L)) 40772448525SMichael Kruse return TM_Disable; 40872448525SMichael Kruse 40972448525SMichael Kruse return TM_Unspecified; 41072448525SMichael Kruse } 41172448525SMichael Kruse 41272448525SMichael Kruse TransformationMode llvm::hasUnrollAndJamTransformation(Loop *L) { 41372448525SMichael Kruse if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable")) 41472448525SMichael Kruse return TM_SuppressedByUser; 41572448525SMichael Kruse 41672448525SMichael Kruse Optional<int> Count = 41772448525SMichael Kruse getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count"); 41872448525SMichael Kruse if (Count.hasValue()) 41972448525SMichael Kruse return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser; 42072448525SMichael Kruse 42172448525SMichael Kruse if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.enable")) 42272448525SMichael Kruse return TM_ForcedByUser; 42372448525SMichael Kruse 42472448525SMichael Kruse if (hasDisableAllTransformsHint(L)) 42572448525SMichael Kruse return TM_Disable; 42672448525SMichael Kruse 42772448525SMichael Kruse return TM_Unspecified; 42872448525SMichael Kruse } 42972448525SMichael Kruse 43072448525SMichael Kruse TransformationMode llvm::hasVectorizeTransformation(Loop *L) { 43172448525SMichael Kruse Optional<bool> Enable = 43272448525SMichael Kruse getOptionalBoolLoopAttribute(L, "llvm.loop.vectorize.enable"); 43372448525SMichael Kruse 43472448525SMichael Kruse if (Enable == false) 43572448525SMichael Kruse return TM_SuppressedByUser; 43672448525SMichael Kruse 43772448525SMichael Kruse Optional<int> VectorizeWidth = 43872448525SMichael Kruse getOptionalIntLoopAttribute(L, "llvm.loop.vectorize.width"); 43972448525SMichael Kruse Optional<int> InterleaveCount = 44072448525SMichael Kruse getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count"); 44172448525SMichael Kruse 44272448525SMichael Kruse // 'Forcing' vector width and interleave count to one effectively disables 44372448525SMichael Kruse // this tranformation. 44470560a0aSMichael Kruse if (Enable == true && VectorizeWidth == 1 && InterleaveCount == 1) 44572448525SMichael Kruse return TM_SuppressedByUser; 44672448525SMichael Kruse 44772448525SMichael Kruse if (getBooleanLoopAttribute(L, "llvm.loop.isvectorized")) 44872448525SMichael Kruse return TM_Disable; 44972448525SMichael Kruse 45070560a0aSMichael Kruse if (Enable == true) 45170560a0aSMichael Kruse return TM_ForcedByUser; 45270560a0aSMichael Kruse 45372448525SMichael Kruse if (VectorizeWidth == 1 && InterleaveCount == 1) 45472448525SMichael Kruse return TM_Disable; 45572448525SMichael Kruse 45672448525SMichael Kruse if (VectorizeWidth > 1 || InterleaveCount > 1) 45772448525SMichael Kruse return TM_Enable; 45872448525SMichael Kruse 45972448525SMichael Kruse if (hasDisableAllTransformsHint(L)) 46072448525SMichael Kruse return TM_Disable; 46172448525SMichael Kruse 46272448525SMichael Kruse return TM_Unspecified; 46372448525SMichael Kruse } 46472448525SMichael Kruse 46572448525SMichael Kruse TransformationMode llvm::hasDistributeTransformation(Loop *L) { 46672448525SMichael Kruse if (getBooleanLoopAttribute(L, "llvm.loop.distribute.enable")) 46772448525SMichael Kruse return TM_ForcedByUser; 46872448525SMichael Kruse 46972448525SMichael Kruse if (hasDisableAllTransformsHint(L)) 47072448525SMichael Kruse return TM_Disable; 47172448525SMichael Kruse 47272448525SMichael Kruse return TM_Unspecified; 47372448525SMichael Kruse } 47472448525SMichael Kruse 47572448525SMichael Kruse TransformationMode llvm::hasLICMVersioningTransformation(Loop *L) { 47672448525SMichael Kruse if (getBooleanLoopAttribute(L, "llvm.loop.licm_versioning.disable")) 47772448525SMichael Kruse return TM_SuppressedByUser; 47872448525SMichael Kruse 47972448525SMichael Kruse if (hasDisableAllTransformsHint(L)) 48072448525SMichael Kruse return TM_Disable; 48172448525SMichael Kruse 48272448525SMichael Kruse return TM_Unspecified; 483963341c8SAdam Nemet } 484122f984aSEvgeniy Stepanov 4857ed5856aSAlina Sbirlea /// Does a BFS from a given node to all of its children inside a given loop. 4867ed5856aSAlina Sbirlea /// The returned vector of nodes includes the starting point. 4877ed5856aSAlina Sbirlea SmallVector<DomTreeNode *, 16> 4887ed5856aSAlina Sbirlea llvm::collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop) { 4897ed5856aSAlina Sbirlea SmallVector<DomTreeNode *, 16> Worklist; 4907ed5856aSAlina Sbirlea auto AddRegionToWorklist = [&](DomTreeNode *DTN) { 4917ed5856aSAlina Sbirlea // Only include subregions in the top level loop. 4927ed5856aSAlina Sbirlea BasicBlock *BB = DTN->getBlock(); 4937ed5856aSAlina Sbirlea if (CurLoop->contains(BB)) 4947ed5856aSAlina Sbirlea Worklist.push_back(DTN); 4957ed5856aSAlina Sbirlea }; 4967ed5856aSAlina Sbirlea 4977ed5856aSAlina Sbirlea AddRegionToWorklist(N); 4987ed5856aSAlina Sbirlea 4997ed5856aSAlina Sbirlea for (size_t I = 0; I < Worklist.size(); I++) 5007ed5856aSAlina Sbirlea for (DomTreeNode *Child : Worklist[I]->getChildren()) 5017ed5856aSAlina Sbirlea AddRegionToWorklist(Child); 5027ed5856aSAlina Sbirlea 5037ed5856aSAlina Sbirlea return Worklist; 5047ed5856aSAlina Sbirlea } 5057ed5856aSAlina Sbirlea 506df3e71e0SMarcello Maggioni void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT = nullptr, 507df3e71e0SMarcello Maggioni ScalarEvolution *SE = nullptr, 508df3e71e0SMarcello Maggioni LoopInfo *LI = nullptr) { 509899809d5SHans Wennborg assert((!DT || L->isLCSSAForm(*DT)) && "Expected LCSSA!"); 510df3e71e0SMarcello Maggioni auto *Preheader = L->getLoopPreheader(); 511df3e71e0SMarcello Maggioni assert(Preheader && "Preheader should exist!"); 512df3e71e0SMarcello Maggioni 513df3e71e0SMarcello Maggioni // Now that we know the removal is safe, remove the loop by changing the 514df3e71e0SMarcello Maggioni // branch from the preheader to go to the single exit block. 515df3e71e0SMarcello Maggioni // 516df3e71e0SMarcello Maggioni // Because we're deleting a large chunk of code at once, the sequence in which 517df3e71e0SMarcello Maggioni // we remove things is very important to avoid invalidation issues. 518df3e71e0SMarcello Maggioni 519df3e71e0SMarcello Maggioni // Tell ScalarEvolution that the loop is deleted. Do this before 520df3e71e0SMarcello Maggioni // deleting the loop so that ScalarEvolution can look at the loop 521df3e71e0SMarcello Maggioni // to determine what it needs to clean up. 522df3e71e0SMarcello Maggioni if (SE) 523df3e71e0SMarcello Maggioni SE->forgetLoop(L); 524df3e71e0SMarcello Maggioni 525df3e71e0SMarcello Maggioni auto *ExitBlock = L->getUniqueExitBlock(); 526df3e71e0SMarcello Maggioni assert(ExitBlock && "Should have a unique exit block!"); 527df3e71e0SMarcello Maggioni assert(L->hasDedicatedExits() && "Loop should have dedicated exits!"); 528df3e71e0SMarcello Maggioni 529df3e71e0SMarcello Maggioni auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator()); 530df3e71e0SMarcello Maggioni assert(OldBr && "Preheader must end with a branch"); 531df3e71e0SMarcello Maggioni assert(OldBr->isUnconditional() && "Preheader must have a single successor"); 532df3e71e0SMarcello Maggioni // Connect the preheader to the exit block. Keep the old edge to the header 533df3e71e0SMarcello Maggioni // around to perform the dominator tree update in two separate steps 534df3e71e0SMarcello Maggioni // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge 535df3e71e0SMarcello Maggioni // preheader -> header. 536df3e71e0SMarcello Maggioni // 537df3e71e0SMarcello Maggioni // 538df3e71e0SMarcello Maggioni // 0. Preheader 1. Preheader 2. Preheader 539df3e71e0SMarcello Maggioni // | | | | 540df3e71e0SMarcello Maggioni // V | V | 541df3e71e0SMarcello Maggioni // Header <--\ | Header <--\ | Header <--\ 542df3e71e0SMarcello Maggioni // | | | | | | | | | | | 543df3e71e0SMarcello Maggioni // | V | | | V | | | V | 544df3e71e0SMarcello Maggioni // | Body --/ | | Body --/ | | Body --/ 545df3e71e0SMarcello Maggioni // V V V V V 546df3e71e0SMarcello Maggioni // Exit Exit Exit 547df3e71e0SMarcello Maggioni // 548df3e71e0SMarcello Maggioni // By doing this is two separate steps we can perform the dominator tree 549df3e71e0SMarcello Maggioni // update without using the batch update API. 550df3e71e0SMarcello Maggioni // 551df3e71e0SMarcello Maggioni // Even when the loop is never executed, we cannot remove the edge from the 552df3e71e0SMarcello Maggioni // source block to the exit block. Consider the case where the unexecuted loop 553df3e71e0SMarcello Maggioni // branches back to an outer loop. If we deleted the loop and removed the edge 554df3e71e0SMarcello Maggioni // coming to this inner loop, this will break the outer loop structure (by 555df3e71e0SMarcello Maggioni // deleting the backedge of the outer loop). If the outer loop is indeed a 556df3e71e0SMarcello Maggioni // non-loop, it will be deleted in a future iteration of loop deletion pass. 557df3e71e0SMarcello Maggioni IRBuilder<> Builder(OldBr); 558df3e71e0SMarcello Maggioni Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock); 559df3e71e0SMarcello Maggioni // Remove the old branch. The conditional branch becomes a new terminator. 560df3e71e0SMarcello Maggioni OldBr->eraseFromParent(); 561df3e71e0SMarcello Maggioni 562df3e71e0SMarcello Maggioni // Rewrite phis in the exit block to get their inputs from the Preheader 563df3e71e0SMarcello Maggioni // instead of the exiting block. 564c7fc81e6SBenjamin Kramer for (PHINode &P : ExitBlock->phis()) { 565df3e71e0SMarcello Maggioni // Set the zero'th element of Phi to be from the preheader and remove all 566df3e71e0SMarcello Maggioni // other incoming values. Given the loop has dedicated exits, all other 567df3e71e0SMarcello Maggioni // incoming values must be from the exiting blocks. 568df3e71e0SMarcello Maggioni int PredIndex = 0; 569c7fc81e6SBenjamin Kramer P.setIncomingBlock(PredIndex, Preheader); 570df3e71e0SMarcello Maggioni // Removes all incoming values from all other exiting blocks (including 571df3e71e0SMarcello Maggioni // duplicate values from an exiting block). 572df3e71e0SMarcello Maggioni // Nuke all entries except the zero'th entry which is the preheader entry. 573df3e71e0SMarcello Maggioni // NOTE! We need to remove Incoming Values in the reverse order as done 574df3e71e0SMarcello Maggioni // below, to keep the indices valid for deletion (removeIncomingValues 575df3e71e0SMarcello Maggioni // updates getNumIncomingValues and shifts all values down into the operand 576df3e71e0SMarcello Maggioni // being deleted). 577c7fc81e6SBenjamin Kramer for (unsigned i = 0, e = P.getNumIncomingValues() - 1; i != e; ++i) 578c7fc81e6SBenjamin Kramer P.removeIncomingValue(e - i, false); 579df3e71e0SMarcello Maggioni 580c7fc81e6SBenjamin Kramer assert((P.getNumIncomingValues() == 1 && 581c7fc81e6SBenjamin Kramer P.getIncomingBlock(PredIndex) == Preheader) && 582df3e71e0SMarcello Maggioni "Should have exactly one value and that's from the preheader!"); 583df3e71e0SMarcello Maggioni } 584df3e71e0SMarcello Maggioni 585df3e71e0SMarcello Maggioni // Disconnect the loop body by branching directly to its exit. 586df3e71e0SMarcello Maggioni Builder.SetInsertPoint(Preheader->getTerminator()); 587df3e71e0SMarcello Maggioni Builder.CreateBr(ExitBlock); 588df3e71e0SMarcello Maggioni // Remove the old branch. 589df3e71e0SMarcello Maggioni Preheader->getTerminator()->eraseFromParent(); 590df3e71e0SMarcello Maggioni 59121a8b605SChijun Sima DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); 592df3e71e0SMarcello Maggioni if (DT) { 593df3e71e0SMarcello Maggioni // Update the dominator tree by informing it about the new edge from the 594f131d611SChijun Sima // preheader to the exit and the removed edge. 595f131d611SChijun Sima DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}, 596f131d611SChijun Sima {DominatorTree::Delete, Preheader, L->getHeader()}}); 597df3e71e0SMarcello Maggioni } 598df3e71e0SMarcello Maggioni 599744c3c32SDavide Italiano // Use a map to unique and a vector to guarantee deterministic ordering. 6008ee59ca6SDavide Italiano llvm::SmallDenseSet<std::pair<DIVariable *, DIExpression *>, 4> DeadDebugSet; 601744c3c32SDavide Italiano llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst; 602744c3c32SDavide Italiano 603a757d65cSSerguei Katkov // Given LCSSA form is satisfied, we should not have users of instructions 604a757d65cSSerguei Katkov // within the dead loop outside of the loop. However, LCSSA doesn't take 605a757d65cSSerguei Katkov // unreachable uses into account. We handle them here. 606a757d65cSSerguei Katkov // We could do it after drop all references (in this case all users in the 607a757d65cSSerguei Katkov // loop will be already eliminated and we have less work to do but according 608a757d65cSSerguei Katkov // to API doc of User::dropAllReferences only valid operation after dropping 609a757d65cSSerguei Katkov // references, is deletion. So let's substitute all usages of 610a757d65cSSerguei Katkov // instruction from the loop with undef value of corresponding type first. 611a757d65cSSerguei Katkov for (auto *Block : L->blocks()) 612a757d65cSSerguei Katkov for (Instruction &I : *Block) { 613a757d65cSSerguei Katkov auto *Undef = UndefValue::get(I.getType()); 614a757d65cSSerguei Katkov for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E;) { 615a757d65cSSerguei Katkov Use &U = *UI; 616a757d65cSSerguei Katkov ++UI; 617a757d65cSSerguei Katkov if (auto *Usr = dyn_cast<Instruction>(U.getUser())) 618a757d65cSSerguei Katkov if (L->contains(Usr->getParent())) 619a757d65cSSerguei Katkov continue; 620a757d65cSSerguei Katkov // If we have a DT then we can check that uses outside a loop only in 621a757d65cSSerguei Katkov // unreachable block. 622a757d65cSSerguei Katkov if (DT) 623a757d65cSSerguei Katkov assert(!DT->isReachableFromEntry(U) && 624a757d65cSSerguei Katkov "Unexpected user in reachable block"); 625a757d65cSSerguei Katkov U.set(Undef); 626a757d65cSSerguei Katkov } 627744c3c32SDavide Italiano auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I); 628744c3c32SDavide Italiano if (!DVI) 629744c3c32SDavide Italiano continue; 6308ee59ca6SDavide Italiano auto Key = DeadDebugSet.find({DVI->getVariable(), DVI->getExpression()}); 6318ee59ca6SDavide Italiano if (Key != DeadDebugSet.end()) 632744c3c32SDavide Italiano continue; 6338ee59ca6SDavide Italiano DeadDebugSet.insert({DVI->getVariable(), DVI->getExpression()}); 634744c3c32SDavide Italiano DeadDebugInst.push_back(DVI); 635a757d65cSSerguei Katkov } 636a757d65cSSerguei Katkov 637744c3c32SDavide Italiano // After the loop has been deleted all the values defined and modified 638744c3c32SDavide Italiano // inside the loop are going to be unavailable. 639744c3c32SDavide Italiano // Since debug values in the loop have been deleted, inserting an undef 640744c3c32SDavide Italiano // dbg.value truncates the range of any dbg.value before the loop where the 641744c3c32SDavide Italiano // loop used to be. This is particularly important for constant values. 642744c3c32SDavide Italiano DIBuilder DIB(*ExitBlock->getModule()); 643e5be660eSRoman Lebedev Instruction *InsertDbgValueBefore = ExitBlock->getFirstNonPHI(); 644e5be660eSRoman Lebedev assert(InsertDbgValueBefore && 645e5be660eSRoman Lebedev "There should be a non-PHI instruction in exit block, else these " 646e5be660eSRoman Lebedev "instructions will have no parent."); 647744c3c32SDavide Italiano for (auto *DVI : DeadDebugInst) 648e5be660eSRoman Lebedev DIB.insertDbgValueIntrinsic(UndefValue::get(Builder.getInt32Ty()), 649e5be660eSRoman Lebedev DVI->getVariable(), DVI->getExpression(), 650e5be660eSRoman Lebedev DVI->getDebugLoc(), InsertDbgValueBefore); 651744c3c32SDavide Italiano 652df3e71e0SMarcello Maggioni // Remove the block from the reference counting scheme, so that we can 653df3e71e0SMarcello Maggioni // delete it freely later. 654df3e71e0SMarcello Maggioni for (auto *Block : L->blocks()) 655df3e71e0SMarcello Maggioni Block->dropAllReferences(); 656df3e71e0SMarcello Maggioni 657df3e71e0SMarcello Maggioni if (LI) { 658df3e71e0SMarcello Maggioni // Erase the instructions and the blocks without having to worry 659df3e71e0SMarcello Maggioni // about ordering because we already dropped the references. 660df3e71e0SMarcello Maggioni // NOTE: This iteration is safe because erasing the block does not remove 661df3e71e0SMarcello Maggioni // its entry from the loop's block list. We do that in the next section. 662df3e71e0SMarcello Maggioni for (Loop::block_iterator LpI = L->block_begin(), LpE = L->block_end(); 663df3e71e0SMarcello Maggioni LpI != LpE; ++LpI) 664df3e71e0SMarcello Maggioni (*LpI)->eraseFromParent(); 665df3e71e0SMarcello Maggioni 666df3e71e0SMarcello Maggioni // Finally, the blocks from loopinfo. This has to happen late because 667df3e71e0SMarcello Maggioni // otherwise our loop iterators won't work. 668df3e71e0SMarcello Maggioni 669df3e71e0SMarcello Maggioni SmallPtrSet<BasicBlock *, 8> blocks; 670df3e71e0SMarcello Maggioni blocks.insert(L->block_begin(), L->block_end()); 671df3e71e0SMarcello Maggioni for (BasicBlock *BB : blocks) 672df3e71e0SMarcello Maggioni LI->removeBlock(BB); 673df3e71e0SMarcello Maggioni 674df3e71e0SMarcello Maggioni // The last step is to update LoopInfo now that we've eliminated this loop. 6759883d7edSWhitney Tsang // Note: LoopInfo::erase remove the given loop and relink its subloops with 6769883d7edSWhitney Tsang // its parent. While removeLoop/removeChildLoop remove the given loop but 6779883d7edSWhitney Tsang // not relink its subloops, which is what we want. 6789883d7edSWhitney Tsang if (Loop *ParentLoop = L->getParentLoop()) { 6799883d7edSWhitney Tsang Loop::iterator I = find(ParentLoop->begin(), ParentLoop->end(), L); 6809883d7edSWhitney Tsang assert(I != ParentLoop->end() && "Couldn't find loop"); 6819883d7edSWhitney Tsang ParentLoop->removeChildLoop(I); 6829883d7edSWhitney Tsang } else { 6839883d7edSWhitney Tsang Loop::iterator I = find(LI->begin(), LI->end(), L); 6849883d7edSWhitney Tsang assert(I != LI->end() && "Couldn't find loop"); 6859883d7edSWhitney Tsang LI->removeLoop(I); 6869883d7edSWhitney Tsang } 6879883d7edSWhitney Tsang LI->destroy(L); 688df3e71e0SMarcello Maggioni } 689df3e71e0SMarcello Maggioni } 690df3e71e0SMarcello Maggioni 69141d72a86SDehao Chen Optional<unsigned> llvm::getLoopEstimatedTripCount(Loop *L) { 69245c43e7dSSerguei Katkov // Support loops with an exiting latch and other existing exists only 69345c43e7dSSerguei Katkov // deoptimize. 69441d72a86SDehao Chen 695d24ddcd6SHiroshi Inoue // Get the branch weights for the loop's backedge. 69645c43e7dSSerguei Katkov BasicBlock *Latch = L->getLoopLatch(); 69745c43e7dSSerguei Katkov if (!Latch) 69845c43e7dSSerguei Katkov return None; 69945c43e7dSSerguei Katkov BranchInst *LatchBR = dyn_cast<BranchInst>(Latch->getTerminator()); 70045c43e7dSSerguei Katkov if (!LatchBR || LatchBR->getNumSuccessors() != 2 || !L->isLoopExiting(Latch)) 70141d72a86SDehao Chen return None; 70241d72a86SDehao Chen 70341d72a86SDehao Chen assert((LatchBR->getSuccessor(0) == L->getHeader() || 70441d72a86SDehao Chen LatchBR->getSuccessor(1) == L->getHeader()) && 70541d72a86SDehao Chen "At least one edge out of the latch must go to the header"); 70641d72a86SDehao Chen 70745c43e7dSSerguei Katkov SmallVector<BasicBlock *, 4> ExitBlocks; 70845c43e7dSSerguei Katkov L->getUniqueNonLatchExitBlocks(ExitBlocks); 70945c43e7dSSerguei Katkov if (any_of(ExitBlocks, [](const BasicBlock *EB) { 71045c43e7dSSerguei Katkov return !EB->getTerminatingDeoptimizeCall(); 71145c43e7dSSerguei Katkov })) 71245c43e7dSSerguei Katkov return None; 71345c43e7dSSerguei Katkov 71441d72a86SDehao Chen // To estimate the number of times the loop body was executed, we want to 71541d72a86SDehao Chen // know the number of times the backedge was taken, vs. the number of times 71641d72a86SDehao Chen // we exited the loop. 717*f0abe820SEvgeniy Brevnov uint64_t BackedgeTakenWeight, LatchExitWeight; 718*f0abe820SEvgeniy Brevnov if (!LatchBR->extractProfMetadata(BackedgeTakenWeight, LatchExitWeight)) 71941d72a86SDehao Chen return None; 72041d72a86SDehao Chen 721*f0abe820SEvgeniy Brevnov if (LatchBR->getSuccessor(0) != L->getHeader()) 722*f0abe820SEvgeniy Brevnov std::swap(BackedgeTakenWeight, LatchExitWeight); 723*f0abe820SEvgeniy Brevnov 724*f0abe820SEvgeniy Brevnov if (!BackedgeTakenWeight || !LatchExitWeight) 725b151a641SMichael Kuperstein return 0; 72641d72a86SDehao Chen 727b151a641SMichael Kuperstein // Divide the count of the backedge by the count of the edge exiting the loop, 728b151a641SMichael Kuperstein // rounding to nearest. 729*f0abe820SEvgeniy Brevnov return llvm::divideNearest(BackedgeTakenWeight, LatchExitWeight); 73041d72a86SDehao Chen } 731cf9daa33SAmara Emerson 7326cb64787SDavid Green bool llvm::hasIterationCountInvariantInParent(Loop *InnerLoop, 733395b80cdSDavid Green ScalarEvolution &SE) { 734395b80cdSDavid Green Loop *OuterL = InnerLoop->getParentLoop(); 735395b80cdSDavid Green if (!OuterL) 736395b80cdSDavid Green return true; 737395b80cdSDavid Green 738395b80cdSDavid Green // Get the backedge taken count for the inner loop 739395b80cdSDavid Green BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); 740395b80cdSDavid Green const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch); 741395b80cdSDavid Green if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) || 742395b80cdSDavid Green !InnerLoopBECountSC->getType()->isIntegerTy()) 743395b80cdSDavid Green return false; 744395b80cdSDavid Green 745395b80cdSDavid Green // Get whether count is invariant to the outer loop 746395b80cdSDavid Green ScalarEvolution::LoopDisposition LD = 747395b80cdSDavid Green SE.getLoopDisposition(InnerLoopBECountSC, OuterL); 748395b80cdSDavid Green if (LD != ScalarEvolution::LoopInvariant) 749395b80cdSDavid Green return false; 750395b80cdSDavid Green 751395b80cdSDavid Green return true; 752395b80cdSDavid Green } 753395b80cdSDavid Green 7546594dc37SVikram TV Value *llvm::createMinMaxOp(IRBuilder<> &Builder, 7556594dc37SVikram TV RecurrenceDescriptor::MinMaxRecurrenceKind RK, 7566594dc37SVikram TV Value *Left, Value *Right) { 7576594dc37SVikram TV CmpInst::Predicate P = CmpInst::ICMP_NE; 7586594dc37SVikram TV switch (RK) { 7596594dc37SVikram TV default: 7606594dc37SVikram TV llvm_unreachable("Unknown min/max recurrence kind"); 7616594dc37SVikram TV case RecurrenceDescriptor::MRK_UIntMin: 7626594dc37SVikram TV P = CmpInst::ICMP_ULT; 7636594dc37SVikram TV break; 7646594dc37SVikram TV case RecurrenceDescriptor::MRK_UIntMax: 7656594dc37SVikram TV P = CmpInst::ICMP_UGT; 7666594dc37SVikram TV break; 7676594dc37SVikram TV case RecurrenceDescriptor::MRK_SIntMin: 7686594dc37SVikram TV P = CmpInst::ICMP_SLT; 7696594dc37SVikram TV break; 7706594dc37SVikram TV case RecurrenceDescriptor::MRK_SIntMax: 7716594dc37SVikram TV P = CmpInst::ICMP_SGT; 7726594dc37SVikram TV break; 7736594dc37SVikram TV case RecurrenceDescriptor::MRK_FloatMin: 7746594dc37SVikram TV P = CmpInst::FCMP_OLT; 7756594dc37SVikram TV break; 7766594dc37SVikram TV case RecurrenceDescriptor::MRK_FloatMax: 7776594dc37SVikram TV P = CmpInst::FCMP_OGT; 7786594dc37SVikram TV break; 7796594dc37SVikram TV } 7806594dc37SVikram TV 7816594dc37SVikram TV // We only match FP sequences that are 'fast', so we can unconditionally 7826594dc37SVikram TV // set it on any generated instructions. 7836594dc37SVikram TV IRBuilder<>::FastMathFlagGuard FMFG(Builder); 7846594dc37SVikram TV FastMathFlags FMF; 7856594dc37SVikram TV FMF.setFast(); 7866594dc37SVikram TV Builder.setFastMathFlags(FMF); 7876594dc37SVikram TV 7886594dc37SVikram TV Value *Cmp; 7896594dc37SVikram TV if (RK == RecurrenceDescriptor::MRK_FloatMin || 7906594dc37SVikram TV RK == RecurrenceDescriptor::MRK_FloatMax) 7916594dc37SVikram TV Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp"); 7926594dc37SVikram TV else 7936594dc37SVikram TV Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp"); 7946594dc37SVikram TV 7956594dc37SVikram TV Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select"); 7966594dc37SVikram TV return Select; 7976594dc37SVikram TV } 7986594dc37SVikram TV 79923c2182cSSimon Pilgrim // Helper to generate an ordered reduction. 80023c2182cSSimon Pilgrim Value * 80123c2182cSSimon Pilgrim llvm::getOrderedReduction(IRBuilder<> &Builder, Value *Acc, Value *Src, 80223c2182cSSimon Pilgrim unsigned Op, 80323c2182cSSimon Pilgrim RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind, 80423c2182cSSimon Pilgrim ArrayRef<Value *> RedOps) { 80523c2182cSSimon Pilgrim unsigned VF = Src->getType()->getVectorNumElements(); 80623c2182cSSimon Pilgrim 80723c2182cSSimon Pilgrim // Extract and apply reduction ops in ascending order: 80823c2182cSSimon Pilgrim // e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1] 80923c2182cSSimon Pilgrim Value *Result = Acc; 81023c2182cSSimon Pilgrim for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) { 81123c2182cSSimon Pilgrim Value *Ext = 81223c2182cSSimon Pilgrim Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx)); 81323c2182cSSimon Pilgrim 81423c2182cSSimon Pilgrim if (Op != Instruction::ICmp && Op != Instruction::FCmp) { 81523c2182cSSimon Pilgrim Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext, 81623c2182cSSimon Pilgrim "bin.rdx"); 81723c2182cSSimon Pilgrim } else { 81823c2182cSSimon Pilgrim assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid && 81923c2182cSSimon Pilgrim "Invalid min/max"); 8206594dc37SVikram TV Result = createMinMaxOp(Builder, MinMaxKind, Result, Ext); 82123c2182cSSimon Pilgrim } 82223c2182cSSimon Pilgrim 82323c2182cSSimon Pilgrim if (!RedOps.empty()) 82423c2182cSSimon Pilgrim propagateIRFlags(Result, RedOps); 82523c2182cSSimon Pilgrim } 82623c2182cSSimon Pilgrim 82723c2182cSSimon Pilgrim return Result; 82823c2182cSSimon Pilgrim } 82923c2182cSSimon Pilgrim 830cf9daa33SAmara Emerson // Helper to generate a log2 shuffle reduction. 831836b0f48SAmara Emerson Value * 832836b0f48SAmara Emerson llvm::getShuffleReduction(IRBuilder<> &Builder, Value *Src, unsigned Op, 833836b0f48SAmara Emerson RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind, 834ad62a3a2SSanjay Patel ArrayRef<Value *> RedOps) { 835cf9daa33SAmara Emerson unsigned VF = Src->getType()->getVectorNumElements(); 836cf9daa33SAmara Emerson // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles 837cf9daa33SAmara Emerson // and vector ops, reducing the set of values being computed by half each 838cf9daa33SAmara Emerson // round. 839cf9daa33SAmara Emerson assert(isPowerOf2_32(VF) && 840cf9daa33SAmara Emerson "Reduction emission only supported for pow2 vectors!"); 841cf9daa33SAmara Emerson Value *TmpVec = Src; 842cf9daa33SAmara Emerson SmallVector<Constant *, 32> ShuffleMask(VF, nullptr); 843cf9daa33SAmara Emerson for (unsigned i = VF; i != 1; i >>= 1) { 844cf9daa33SAmara Emerson // Move the upper half of the vector to the lower half. 845cf9daa33SAmara Emerson for (unsigned j = 0; j != i / 2; ++j) 846cf9daa33SAmara Emerson ShuffleMask[j] = Builder.getInt32(i / 2 + j); 847cf9daa33SAmara Emerson 848cf9daa33SAmara Emerson // Fill the rest of the mask with undef. 849cf9daa33SAmara Emerson std::fill(&ShuffleMask[i / 2], ShuffleMask.end(), 850cf9daa33SAmara Emerson UndefValue::get(Builder.getInt32Ty())); 851cf9daa33SAmara Emerson 852cf9daa33SAmara Emerson Value *Shuf = Builder.CreateShuffleVector( 853cf9daa33SAmara Emerson TmpVec, UndefValue::get(TmpVec->getType()), 854cf9daa33SAmara Emerson ConstantVector::get(ShuffleMask), "rdx.shuf"); 855cf9daa33SAmara Emerson 856cf9daa33SAmara Emerson if (Op != Instruction::ICmp && Op != Instruction::FCmp) { 857ad62a3a2SSanjay Patel // The builder propagates its fast-math-flags setting. 858ad62a3a2SSanjay Patel TmpVec = Builder.CreateBinOp((Instruction::BinaryOps)Op, TmpVec, Shuf, 859ad62a3a2SSanjay Patel "bin.rdx"); 860cf9daa33SAmara Emerson } else { 861cf9daa33SAmara Emerson assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid && 862cf9daa33SAmara Emerson "Invalid min/max"); 8636594dc37SVikram TV TmpVec = createMinMaxOp(Builder, MinMaxKind, TmpVec, Shuf); 864cf9daa33SAmara Emerson } 865cf9daa33SAmara Emerson if (!RedOps.empty()) 866cf9daa33SAmara Emerson propagateIRFlags(TmpVec, RedOps); 867cf9daa33SAmara Emerson } 868cf9daa33SAmara Emerson // The result is in the first element of the vector. 869cf9daa33SAmara Emerson return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0)); 870cf9daa33SAmara Emerson } 871cf9daa33SAmara Emerson 872cf9daa33SAmara Emerson /// Create a simple vector reduction specified by an opcode and some 873cf9daa33SAmara Emerson /// flags (if generating min/max reductions). 874cf9daa33SAmara Emerson Value *llvm::createSimpleTargetReduction( 875cf9daa33SAmara Emerson IRBuilder<> &Builder, const TargetTransformInfo *TTI, unsigned Opcode, 876ad62a3a2SSanjay Patel Value *Src, TargetTransformInfo::ReductionFlags Flags, 877cf9daa33SAmara Emerson ArrayRef<Value *> RedOps) { 878cf9daa33SAmara Emerson assert(isa<VectorType>(Src->getType()) && "Type must be a vector"); 879cf9daa33SAmara Emerson 880cf9daa33SAmara Emerson std::function<Value *()> BuildFunc; 881cf9daa33SAmara Emerson using RD = RecurrenceDescriptor; 882cf9daa33SAmara Emerson RD::MinMaxRecurrenceKind MinMaxKind = RD::MRK_Invalid; 883cf9daa33SAmara Emerson 884cf9daa33SAmara Emerson switch (Opcode) { 885cf9daa33SAmara Emerson case Instruction::Add: 886cf9daa33SAmara Emerson BuildFunc = [&]() { return Builder.CreateAddReduce(Src); }; 887cf9daa33SAmara Emerson break; 888cf9daa33SAmara Emerson case Instruction::Mul: 889cf9daa33SAmara Emerson BuildFunc = [&]() { return Builder.CreateMulReduce(Src); }; 890cf9daa33SAmara Emerson break; 891cf9daa33SAmara Emerson case Instruction::And: 892cf9daa33SAmara Emerson BuildFunc = [&]() { return Builder.CreateAndReduce(Src); }; 893cf9daa33SAmara Emerson break; 894cf9daa33SAmara Emerson case Instruction::Or: 895cf9daa33SAmara Emerson BuildFunc = [&]() { return Builder.CreateOrReduce(Src); }; 896cf9daa33SAmara Emerson break; 897cf9daa33SAmara Emerson case Instruction::Xor: 898cf9daa33SAmara Emerson BuildFunc = [&]() { return Builder.CreateXorReduce(Src); }; 899cf9daa33SAmara Emerson break; 900cf9daa33SAmara Emerson case Instruction::FAdd: 901cf9daa33SAmara Emerson BuildFunc = [&]() { 902cbeb563cSSander de Smalen auto Rdx = Builder.CreateFAddReduce( 903cbeb563cSSander de Smalen Constant::getNullValue(Src->getType()->getVectorElementType()), Src); 904cf9daa33SAmara Emerson return Rdx; 905cf9daa33SAmara Emerson }; 906cf9daa33SAmara Emerson break; 907cf9daa33SAmara Emerson case Instruction::FMul: 908cf9daa33SAmara Emerson BuildFunc = [&]() { 909cbeb563cSSander de Smalen Type *Ty = Src->getType()->getVectorElementType(); 910cbeb563cSSander de Smalen auto Rdx = Builder.CreateFMulReduce(ConstantFP::get(Ty, 1.0), Src); 911cf9daa33SAmara Emerson return Rdx; 912cf9daa33SAmara Emerson }; 913cf9daa33SAmara Emerson break; 914cf9daa33SAmara Emerson case Instruction::ICmp: 915cf9daa33SAmara Emerson if (Flags.IsMaxOp) { 916cf9daa33SAmara Emerson MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMax : RD::MRK_UIntMax; 917cf9daa33SAmara Emerson BuildFunc = [&]() { 918cf9daa33SAmara Emerson return Builder.CreateIntMaxReduce(Src, Flags.IsSigned); 919cf9daa33SAmara Emerson }; 920cf9daa33SAmara Emerson } else { 921cf9daa33SAmara Emerson MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMin : RD::MRK_UIntMin; 922cf9daa33SAmara Emerson BuildFunc = [&]() { 923cf9daa33SAmara Emerson return Builder.CreateIntMinReduce(Src, Flags.IsSigned); 924cf9daa33SAmara Emerson }; 925cf9daa33SAmara Emerson } 926cf9daa33SAmara Emerson break; 927cf9daa33SAmara Emerson case Instruction::FCmp: 928cf9daa33SAmara Emerson if (Flags.IsMaxOp) { 929cf9daa33SAmara Emerson MinMaxKind = RD::MRK_FloatMax; 930cf9daa33SAmara Emerson BuildFunc = [&]() { return Builder.CreateFPMaxReduce(Src, Flags.NoNaN); }; 931cf9daa33SAmara Emerson } else { 932cf9daa33SAmara Emerson MinMaxKind = RD::MRK_FloatMin; 933cf9daa33SAmara Emerson BuildFunc = [&]() { return Builder.CreateFPMinReduce(Src, Flags.NoNaN); }; 934cf9daa33SAmara Emerson } 935cf9daa33SAmara Emerson break; 936cf9daa33SAmara Emerson default: 937cf9daa33SAmara Emerson llvm_unreachable("Unhandled opcode"); 938cf9daa33SAmara Emerson break; 939cf9daa33SAmara Emerson } 940cf9daa33SAmara Emerson if (TTI->useReductionIntrinsic(Opcode, Src->getType(), Flags)) 941cf9daa33SAmara Emerson return BuildFunc(); 942ad62a3a2SSanjay Patel return getShuffleReduction(Builder, Src, Opcode, MinMaxKind, RedOps); 943cf9daa33SAmara Emerson } 944cf9daa33SAmara Emerson 945cf9daa33SAmara Emerson /// Create a vector reduction using a given recurrence descriptor. 9463e069f57SSanjay Patel Value *llvm::createTargetReduction(IRBuilder<> &B, 947cf9daa33SAmara Emerson const TargetTransformInfo *TTI, 948cf9daa33SAmara Emerson RecurrenceDescriptor &Desc, Value *Src, 949cf9daa33SAmara Emerson bool NoNaN) { 950cf9daa33SAmara Emerson // TODO: Support in-order reductions based on the recurrence descriptor. 9513e069f57SSanjay Patel using RD = RecurrenceDescriptor; 9523e069f57SSanjay Patel RD::RecurrenceKind RecKind = Desc.getRecurrenceKind(); 953cf9daa33SAmara Emerson TargetTransformInfo::ReductionFlags Flags; 954cf9daa33SAmara Emerson Flags.NoNaN = NoNaN; 955ad62a3a2SSanjay Patel 956ad62a3a2SSanjay Patel // All ops in the reduction inherit fast-math-flags from the recurrence 957ad62a3a2SSanjay Patel // descriptor. 958ad62a3a2SSanjay Patel IRBuilder<>::FastMathFlagGuard FMFGuard(B); 959ad62a3a2SSanjay Patel B.setFastMathFlags(Desc.getFastMathFlags()); 960ad62a3a2SSanjay Patel 961cf9daa33SAmara Emerson switch (RecKind) { 9623e069f57SSanjay Patel case RD::RK_FloatAdd: 963ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::FAdd, Src, Flags); 9643e069f57SSanjay Patel case RD::RK_FloatMult: 965ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::FMul, Src, Flags); 9663e069f57SSanjay Patel case RD::RK_IntegerAdd: 967ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::Add, Src, Flags); 9683e069f57SSanjay Patel case RD::RK_IntegerMult: 969ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::Mul, Src, Flags); 9703e069f57SSanjay Patel case RD::RK_IntegerAnd: 971ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::And, Src, Flags); 9723e069f57SSanjay Patel case RD::RK_IntegerOr: 973ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::Or, Src, Flags); 9743e069f57SSanjay Patel case RD::RK_IntegerXor: 975ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::Xor, Src, Flags); 9763e069f57SSanjay Patel case RD::RK_IntegerMinMax: { 9773e069f57SSanjay Patel RD::MinMaxRecurrenceKind MMKind = Desc.getMinMaxRecurrenceKind(); 9783e069f57SSanjay Patel Flags.IsMaxOp = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_UIntMax); 9793e069f57SSanjay Patel Flags.IsSigned = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_SIntMin); 980ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::ICmp, Src, Flags); 981cf9daa33SAmara Emerson } 9823e069f57SSanjay Patel case RD::RK_FloatMinMax: { 9833e069f57SSanjay Patel Flags.IsMaxOp = Desc.getMinMaxRecurrenceKind() == RD::MRK_FloatMax; 984ad62a3a2SSanjay Patel return createSimpleTargetReduction(B, TTI, Instruction::FCmp, Src, Flags); 985cf9daa33SAmara Emerson } 986cf9daa33SAmara Emerson default: 987cf9daa33SAmara Emerson llvm_unreachable("Unhandled RecKind"); 988cf9daa33SAmara Emerson } 989cf9daa33SAmara Emerson } 990cf9daa33SAmara Emerson 991a61f4b89SDinar Temirbulatov void llvm::propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue) { 992a61f4b89SDinar Temirbulatov auto *VecOp = dyn_cast<Instruction>(I); 993a61f4b89SDinar Temirbulatov if (!VecOp) 994a61f4b89SDinar Temirbulatov return; 995a61f4b89SDinar Temirbulatov auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0]) 996a61f4b89SDinar Temirbulatov : dyn_cast<Instruction>(OpValue); 997a61f4b89SDinar Temirbulatov if (!Intersection) 998a61f4b89SDinar Temirbulatov return; 999a61f4b89SDinar Temirbulatov const unsigned Opcode = Intersection->getOpcode(); 1000a61f4b89SDinar Temirbulatov VecOp->copyIRFlags(Intersection); 1001a61f4b89SDinar Temirbulatov for (auto *V : VL) { 1002a61f4b89SDinar Temirbulatov auto *Instr = dyn_cast<Instruction>(V); 1003a61f4b89SDinar Temirbulatov if (!Instr) 1004a61f4b89SDinar Temirbulatov continue; 1005a61f4b89SDinar Temirbulatov if (OpValue == nullptr || Opcode == Instr->getOpcode()) 1006a61f4b89SDinar Temirbulatov VecOp->andIRFlags(V); 1007cf9daa33SAmara Emerson } 1008cf9daa33SAmara Emerson } 1009a78dc4d6SMax Kazantsev 1010a78dc4d6SMax Kazantsev bool llvm::isKnownNegativeInLoop(const SCEV *S, const Loop *L, 1011a78dc4d6SMax Kazantsev ScalarEvolution &SE) { 1012a78dc4d6SMax Kazantsev const SCEV *Zero = SE.getZero(S->getType()); 1013a78dc4d6SMax Kazantsev return SE.isAvailableAtLoopEntry(S, L) && 1014a78dc4d6SMax Kazantsev SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, S, Zero); 1015a78dc4d6SMax Kazantsev } 1016a78dc4d6SMax Kazantsev 1017a78dc4d6SMax Kazantsev bool llvm::isKnownNonNegativeInLoop(const SCEV *S, const Loop *L, 1018a78dc4d6SMax Kazantsev ScalarEvolution &SE) { 1019a78dc4d6SMax Kazantsev const SCEV *Zero = SE.getZero(S->getType()); 1020a78dc4d6SMax Kazantsev return SE.isAvailableAtLoopEntry(S, L) && 1021a78dc4d6SMax Kazantsev SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, S, Zero); 1022a78dc4d6SMax Kazantsev } 1023a78dc4d6SMax Kazantsev 1024a78dc4d6SMax Kazantsev bool llvm::cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, 1025a78dc4d6SMax Kazantsev bool Signed) { 1026a78dc4d6SMax Kazantsev unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth(); 1027a78dc4d6SMax Kazantsev APInt Min = Signed ? APInt::getSignedMinValue(BitWidth) : 1028a78dc4d6SMax Kazantsev APInt::getMinValue(BitWidth); 1029a78dc4d6SMax Kazantsev auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 1030a78dc4d6SMax Kazantsev return SE.isAvailableAtLoopEntry(S, L) && 1031a78dc4d6SMax Kazantsev SE.isLoopEntryGuardedByCond(L, Predicate, S, 1032a78dc4d6SMax Kazantsev SE.getConstant(Min)); 1033a78dc4d6SMax Kazantsev } 1034a78dc4d6SMax Kazantsev 1035a78dc4d6SMax Kazantsev bool llvm::cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE, 1036a78dc4d6SMax Kazantsev bool Signed) { 1037a78dc4d6SMax Kazantsev unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth(); 1038a78dc4d6SMax Kazantsev APInt Max = Signed ? APInt::getSignedMaxValue(BitWidth) : 1039a78dc4d6SMax Kazantsev APInt::getMaxValue(BitWidth); 1040a78dc4d6SMax Kazantsev auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; 1041a78dc4d6SMax Kazantsev return SE.isAvailableAtLoopEntry(S, L) && 1042a78dc4d6SMax Kazantsev SE.isLoopEntryGuardedByCond(L, Predicate, S, 1043a78dc4d6SMax Kazantsev SE.getConstant(Max)); 1044a78dc4d6SMax Kazantsev } 1045