10b57cec5SDimitry Andric //===-- LoopUtils.cpp - Loop Utility functions -------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines common loop utility functions.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h"
145ffd83dbSDimitry Andric #include "llvm/ADT/DenseSet.h"
155ffd83dbSDimitry Andric #include "llvm/ADT/Optional.h"
165ffd83dbSDimitry Andric #include "llvm/ADT/PriorityWorklist.h"
170b57cec5SDimitry Andric #include "llvm/ADT/ScopeExit.h"
185ffd83dbSDimitry Andric #include "llvm/ADT/SetVector.h"
195ffd83dbSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
205ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h"
210b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
220b57cec5SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
230b57cec5SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h"
240b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
250b57cec5SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
265ffd83dbSDimitry Andric #include "llvm/Analysis/LoopAccessAnalysis.h"
270b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
280b57cec5SDimitry Andric #include "llvm/Analysis/LoopPass.h"
298bcb0991SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
300b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSAUpdater.h"
310b57cec5SDimitry Andric #include "llvm/Analysis/MustExecute.h"
320b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
330b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
340b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionExpressions.h"
350b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
360b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
370b57cec5SDimitry Andric #include "llvm/IR/DIBuilder.h"
380b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
390b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
400b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
415ffd83dbSDimitry Andric #include "llvm/IR/MDBuilder.h"
420b57cec5SDimitry Andric #include "llvm/IR/Module.h"
435ffd83dbSDimitry Andric #include "llvm/IR/Operator.h"
440b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
450b57cec5SDimitry Andric #include "llvm/IR/ValueHandle.h"
46480093f4SDimitry Andric #include "llvm/InitializePasses.h"
470b57cec5SDimitry Andric #include "llvm/Pass.h"
480b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
490b57cec5SDimitry Andric #include "llvm/Support/KnownBits.h"
500b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
515ffd83dbSDimitry Andric #include "llvm/Transforms/Utils/Local.h"
525ffd83dbSDimitry Andric #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric using namespace llvm;
550b57cec5SDimitry Andric using namespace llvm::PatternMatch;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric #define DEBUG_TYPE "loop-utils"
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric static const char *LLVMLoopDisableNonforced = "llvm.loop.disable_nonforced";
608bcb0991SDimitry Andric static const char *LLVMLoopDisableLICM = "llvm.licm.disable";
610b57cec5SDimitry Andric 
formDedicatedExitBlocks(Loop * L,DominatorTree * DT,LoopInfo * LI,MemorySSAUpdater * MSSAU,bool PreserveLCSSA)620b57cec5SDimitry Andric bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
630b57cec5SDimitry Andric                                    MemorySSAUpdater *MSSAU,
640b57cec5SDimitry Andric                                    bool PreserveLCSSA) {
650b57cec5SDimitry Andric   bool Changed = false;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   // We re-use a vector for the in-loop predecesosrs.
680b57cec5SDimitry Andric   SmallVector<BasicBlock *, 4> InLoopPredecessors;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   auto RewriteExit = [&](BasicBlock *BB) {
710b57cec5SDimitry Andric     assert(InLoopPredecessors.empty() &&
720b57cec5SDimitry Andric            "Must start with an empty predecessors list!");
730b57cec5SDimitry Andric     auto Cleanup = make_scope_exit([&] { InLoopPredecessors.clear(); });
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric     // See if there are any non-loop predecessors of this exit block and
760b57cec5SDimitry Andric     // keep track of the in-loop predecessors.
770b57cec5SDimitry Andric     bool IsDedicatedExit = true;
780b57cec5SDimitry Andric     for (auto *PredBB : predecessors(BB))
790b57cec5SDimitry Andric       if (L->contains(PredBB)) {
800b57cec5SDimitry Andric         if (isa<IndirectBrInst>(PredBB->getTerminator()))
810b57cec5SDimitry Andric           // We cannot rewrite exiting edges from an indirectbr.
820b57cec5SDimitry Andric           return false;
830b57cec5SDimitry Andric         if (isa<CallBrInst>(PredBB->getTerminator()))
840b57cec5SDimitry Andric           // We cannot rewrite exiting edges from a callbr.
850b57cec5SDimitry Andric           return false;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric         InLoopPredecessors.push_back(PredBB);
880b57cec5SDimitry Andric       } else {
890b57cec5SDimitry Andric         IsDedicatedExit = false;
900b57cec5SDimitry Andric       }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric     assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric     // Nothing to do if this is already a dedicated exit.
950b57cec5SDimitry Andric     if (IsDedicatedExit)
960b57cec5SDimitry Andric       return false;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric     auto *NewExitBB = SplitBlockPredecessors(
990b57cec5SDimitry Andric         BB, InLoopPredecessors, ".loopexit", DT, LI, MSSAU, PreserveLCSSA);
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric     if (!NewExitBB)
1020b57cec5SDimitry Andric       LLVM_DEBUG(
1030b57cec5SDimitry Andric           dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
1040b57cec5SDimitry Andric                  << *L << "\n");
1050b57cec5SDimitry Andric     else
1060b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
1070b57cec5SDimitry Andric                         << NewExitBB->getName() << "\n");
1080b57cec5SDimitry Andric     return true;
1090b57cec5SDimitry Andric   };
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   // Walk the exit blocks directly rather than building up a data structure for
1120b57cec5SDimitry Andric   // them, but only visit each one once.
1130b57cec5SDimitry Andric   SmallPtrSet<BasicBlock *, 4> Visited;
1140b57cec5SDimitry Andric   for (auto *BB : L->blocks())
1150b57cec5SDimitry Andric     for (auto *SuccBB : successors(BB)) {
1160b57cec5SDimitry Andric       // We're looking for exit blocks so skip in-loop successors.
1170b57cec5SDimitry Andric       if (L->contains(SuccBB))
1180b57cec5SDimitry Andric         continue;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric       // Visit each exit block exactly once.
1210b57cec5SDimitry Andric       if (!Visited.insert(SuccBB).second)
1220b57cec5SDimitry Andric         continue;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric       Changed |= RewriteExit(SuccBB);
1250b57cec5SDimitry Andric     }
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   return Changed;
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric /// Returns the instructions that use values defined in the loop.
findDefsUsedOutsideOfLoop(Loop * L)1310b57cec5SDimitry Andric SmallVector<Instruction *, 8> llvm::findDefsUsedOutsideOfLoop(Loop *L) {
1320b57cec5SDimitry Andric   SmallVector<Instruction *, 8> UsedOutside;
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   for (auto *Block : L->getBlocks())
1350b57cec5SDimitry Andric     // FIXME: I believe that this could use copy_if if the Inst reference could
1360b57cec5SDimitry Andric     // be adapted into a pointer.
1370b57cec5SDimitry Andric     for (auto &Inst : *Block) {
1380b57cec5SDimitry Andric       auto Users = Inst.users();
1390b57cec5SDimitry Andric       if (any_of(Users, [&](User *U) {
1400b57cec5SDimitry Andric             auto *Use = cast<Instruction>(U);
1410b57cec5SDimitry Andric             return !L->contains(Use->getParent());
1420b57cec5SDimitry Andric           }))
1430b57cec5SDimitry Andric         UsedOutside.push_back(&Inst);
1440b57cec5SDimitry Andric     }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   return UsedOutside;
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
getLoopAnalysisUsage(AnalysisUsage & AU)1490b57cec5SDimitry Andric void llvm::getLoopAnalysisUsage(AnalysisUsage &AU) {
1500b57cec5SDimitry Andric   // By definition, all loop passes need the LoopInfo analysis and the
1510b57cec5SDimitry Andric   // Dominator tree it depends on. Because they all participate in the loop
1520b57cec5SDimitry Andric   // pass manager, they must also preserve these.
1530b57cec5SDimitry Andric   AU.addRequired<DominatorTreeWrapperPass>();
1540b57cec5SDimitry Andric   AU.addPreserved<DominatorTreeWrapperPass>();
1550b57cec5SDimitry Andric   AU.addRequired<LoopInfoWrapperPass>();
1560b57cec5SDimitry Andric   AU.addPreserved<LoopInfoWrapperPass>();
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   // We must also preserve LoopSimplify and LCSSA. We locally access their IDs
1590b57cec5SDimitry Andric   // here because users shouldn't directly get them from this header.
1600b57cec5SDimitry Andric   extern char &LoopSimplifyID;
1610b57cec5SDimitry Andric   extern char &LCSSAID;
1620b57cec5SDimitry Andric   AU.addRequiredID(LoopSimplifyID);
1630b57cec5SDimitry Andric   AU.addPreservedID(LoopSimplifyID);
1640b57cec5SDimitry Andric   AU.addRequiredID(LCSSAID);
1650b57cec5SDimitry Andric   AU.addPreservedID(LCSSAID);
1660b57cec5SDimitry Andric   // This is used in the LPPassManager to perform LCSSA verification on passes
1670b57cec5SDimitry Andric   // which preserve lcssa form
1680b57cec5SDimitry Andric   AU.addRequired<LCSSAVerificationPass>();
1690b57cec5SDimitry Andric   AU.addPreserved<LCSSAVerificationPass>();
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   // Loop passes are designed to run inside of a loop pass manager which means
1720b57cec5SDimitry Andric   // that any function analyses they require must be required by the first loop
1730b57cec5SDimitry Andric   // pass in the manager (so that it is computed before the loop pass manager
1740b57cec5SDimitry Andric   // runs) and preserved by all loop pasess in the manager. To make this
1750b57cec5SDimitry Andric   // reasonably robust, the set needed for most loop passes is maintained here.
1760b57cec5SDimitry Andric   // If your loop pass requires an analysis not listed here, you will need to
1770b57cec5SDimitry Andric   // carefully audit the loop pass manager nesting structure that results.
1780b57cec5SDimitry Andric   AU.addRequired<AAResultsWrapperPass>();
1790b57cec5SDimitry Andric   AU.addPreserved<AAResultsWrapperPass>();
1800b57cec5SDimitry Andric   AU.addPreserved<BasicAAWrapperPass>();
1810b57cec5SDimitry Andric   AU.addPreserved<GlobalsAAWrapperPass>();
1820b57cec5SDimitry Andric   AU.addPreserved<SCEVAAWrapperPass>();
1830b57cec5SDimitry Andric   AU.addRequired<ScalarEvolutionWrapperPass>();
1840b57cec5SDimitry Andric   AU.addPreserved<ScalarEvolutionWrapperPass>();
1858bcb0991SDimitry Andric   // FIXME: When all loop passes preserve MemorySSA, it can be required and
1868bcb0991SDimitry Andric   // preserved here instead of the individual handling in each pass.
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric /// Manually defined generic "LoopPass" dependency initialization. This is used
1900b57cec5SDimitry Andric /// to initialize the exact set of passes from above in \c
1910b57cec5SDimitry Andric /// getLoopAnalysisUsage. It can be used within a loop pass's initialization
1920b57cec5SDimitry Andric /// with:
1930b57cec5SDimitry Andric ///
1940b57cec5SDimitry Andric ///   INITIALIZE_PASS_DEPENDENCY(LoopPass)
1950b57cec5SDimitry Andric ///
1960b57cec5SDimitry Andric /// As-if "LoopPass" were a pass.
initializeLoopPassPass(PassRegistry & Registry)1970b57cec5SDimitry Andric void llvm::initializeLoopPassPass(PassRegistry &Registry) {
1980b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1990b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
2000b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
2010b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
2020b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2030b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
2040b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
2050b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
2060b57cec5SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
2078bcb0991SDimitry Andric   INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
2088bcb0991SDimitry Andric }
2098bcb0991SDimitry Andric 
2108bcb0991SDimitry Andric /// Create MDNode for input string.
createStringMetadata(Loop * TheLoop,StringRef Name,unsigned V)2118bcb0991SDimitry Andric static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) {
2128bcb0991SDimitry Andric   LLVMContext &Context = TheLoop->getHeader()->getContext();
2138bcb0991SDimitry Andric   Metadata *MDs[] = {
2148bcb0991SDimitry Andric       MDString::get(Context, Name),
2158bcb0991SDimitry Andric       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))};
2168bcb0991SDimitry Andric   return MDNode::get(Context, MDs);
2178bcb0991SDimitry Andric }
2188bcb0991SDimitry Andric 
2198bcb0991SDimitry Andric /// Set input string into loop metadata by keeping other values intact.
2208bcb0991SDimitry Andric /// If the string is already in loop metadata update value if it is
2218bcb0991SDimitry Andric /// different.
addStringMetadataToLoop(Loop * TheLoop,const char * StringMD,unsigned V)2228bcb0991SDimitry Andric void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD,
2238bcb0991SDimitry Andric                                    unsigned V) {
2248bcb0991SDimitry Andric   SmallVector<Metadata *, 4> MDs(1);
2258bcb0991SDimitry Andric   // If the loop already has metadata, retain it.
2268bcb0991SDimitry Andric   MDNode *LoopID = TheLoop->getLoopID();
2278bcb0991SDimitry Andric   if (LoopID) {
2288bcb0991SDimitry Andric     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
2298bcb0991SDimitry Andric       MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
2308bcb0991SDimitry Andric       // If it is of form key = value, try to parse it.
2318bcb0991SDimitry Andric       if (Node->getNumOperands() == 2) {
2328bcb0991SDimitry Andric         MDString *S = dyn_cast<MDString>(Node->getOperand(0));
2338bcb0991SDimitry Andric         if (S && S->getString().equals(StringMD)) {
2348bcb0991SDimitry Andric           ConstantInt *IntMD =
2358bcb0991SDimitry Andric               mdconst::extract_or_null<ConstantInt>(Node->getOperand(1));
2368bcb0991SDimitry Andric           if (IntMD && IntMD->getSExtValue() == V)
2378bcb0991SDimitry Andric             // It is already in place. Do nothing.
2388bcb0991SDimitry Andric             return;
2398bcb0991SDimitry Andric           // We need to update the value, so just skip it here and it will
2408bcb0991SDimitry Andric           // be added after copying other existed nodes.
2418bcb0991SDimitry Andric           continue;
2428bcb0991SDimitry Andric         }
2438bcb0991SDimitry Andric       }
2448bcb0991SDimitry Andric       MDs.push_back(Node);
2458bcb0991SDimitry Andric     }
2468bcb0991SDimitry Andric   }
2478bcb0991SDimitry Andric   // Add new metadata.
2488bcb0991SDimitry Andric   MDs.push_back(createStringMetadata(TheLoop, StringMD, V));
2498bcb0991SDimitry Andric   // Replace current metadata node with new one.
2508bcb0991SDimitry Andric   LLVMContext &Context = TheLoop->getHeader()->getContext();
2518bcb0991SDimitry Andric   MDNode *NewLoopID = MDNode::get(Context, MDs);
2528bcb0991SDimitry Andric   // Set operand 0 to refer to the loop id itself.
2538bcb0991SDimitry Andric   NewLoopID->replaceOperandWith(0, NewLoopID);
2548bcb0991SDimitry Andric   TheLoop->setLoopID(NewLoopID);
2550b57cec5SDimitry Andric }
2560b57cec5SDimitry Andric 
257af732203SDimitry Andric Optional<ElementCount>
getOptionalElementCountLoopAttribute(const Loop * TheLoop)258*5f7ddb14SDimitry Andric llvm::getOptionalElementCountLoopAttribute(const Loop *TheLoop) {
259af732203SDimitry Andric   Optional<int> Width =
260af732203SDimitry Andric       getOptionalIntLoopAttribute(TheLoop, "llvm.loop.vectorize.width");
261af732203SDimitry Andric 
262af732203SDimitry Andric   if (Width.hasValue()) {
263af732203SDimitry Andric     Optional<int> IsScalable = getOptionalIntLoopAttribute(
264af732203SDimitry Andric         TheLoop, "llvm.loop.vectorize.scalable.enable");
265af732203SDimitry Andric     return ElementCount::get(*Width, IsScalable.getValueOr(false));
266af732203SDimitry Andric   }
267af732203SDimitry Andric 
268af732203SDimitry Andric   return None;
269af732203SDimitry Andric }
270af732203SDimitry Andric 
makeFollowupLoopID(MDNode * OrigLoopID,ArrayRef<StringRef> FollowupOptions,const char * InheritOptionsExceptPrefix,bool AlwaysNew)2710b57cec5SDimitry Andric Optional<MDNode *> llvm::makeFollowupLoopID(
2720b57cec5SDimitry Andric     MDNode *OrigLoopID, ArrayRef<StringRef> FollowupOptions,
2730b57cec5SDimitry Andric     const char *InheritOptionsExceptPrefix, bool AlwaysNew) {
2740b57cec5SDimitry Andric   if (!OrigLoopID) {
2750b57cec5SDimitry Andric     if (AlwaysNew)
2760b57cec5SDimitry Andric       return nullptr;
2770b57cec5SDimitry Andric     return None;
2780b57cec5SDimitry Andric   }
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   assert(OrigLoopID->getOperand(0) == OrigLoopID);
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   bool InheritAllAttrs = !InheritOptionsExceptPrefix;
2830b57cec5SDimitry Andric   bool InheritSomeAttrs =
2840b57cec5SDimitry Andric       InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] != '\0';
2850b57cec5SDimitry Andric   SmallVector<Metadata *, 8> MDs;
2860b57cec5SDimitry Andric   MDs.push_back(nullptr);
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   bool Changed = false;
2890b57cec5SDimitry Andric   if (InheritAllAttrs || InheritSomeAttrs) {
290af732203SDimitry Andric     for (const MDOperand &Existing : drop_begin(OrigLoopID->operands())) {
2910b57cec5SDimitry Andric       MDNode *Op = cast<MDNode>(Existing.get());
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric       auto InheritThisAttribute = [InheritSomeAttrs,
2940b57cec5SDimitry Andric                                    InheritOptionsExceptPrefix](MDNode *Op) {
2950b57cec5SDimitry Andric         if (!InheritSomeAttrs)
2960b57cec5SDimitry Andric           return false;
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric         // Skip malformatted attribute metadata nodes.
2990b57cec5SDimitry Andric         if (Op->getNumOperands() == 0)
3000b57cec5SDimitry Andric           return true;
3010b57cec5SDimitry Andric         Metadata *NameMD = Op->getOperand(0).get();
3020b57cec5SDimitry Andric         if (!isa<MDString>(NameMD))
3030b57cec5SDimitry Andric           return true;
3040b57cec5SDimitry Andric         StringRef AttrName = cast<MDString>(NameMD)->getString();
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric         // Do not inherit excluded attributes.
3070b57cec5SDimitry Andric         return !AttrName.startswith(InheritOptionsExceptPrefix);
3080b57cec5SDimitry Andric       };
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric       if (InheritThisAttribute(Op))
3110b57cec5SDimitry Andric         MDs.push_back(Op);
3120b57cec5SDimitry Andric       else
3130b57cec5SDimitry Andric         Changed = true;
3140b57cec5SDimitry Andric     }
3150b57cec5SDimitry Andric   } else {
3160b57cec5SDimitry Andric     // Modified if we dropped at least one attribute.
3170b57cec5SDimitry Andric     Changed = OrigLoopID->getNumOperands() > 1;
3180b57cec5SDimitry Andric   }
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   bool HasAnyFollowup = false;
3210b57cec5SDimitry Andric   for (StringRef OptionName : FollowupOptions) {
3220b57cec5SDimitry Andric     MDNode *FollowupNode = findOptionMDForLoopID(OrigLoopID, OptionName);
3230b57cec5SDimitry Andric     if (!FollowupNode)
3240b57cec5SDimitry Andric       continue;
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric     HasAnyFollowup = true;
327af732203SDimitry Andric     for (const MDOperand &Option : drop_begin(FollowupNode->operands())) {
3280b57cec5SDimitry Andric       MDs.push_back(Option.get());
3290b57cec5SDimitry Andric       Changed = true;
3300b57cec5SDimitry Andric     }
3310b57cec5SDimitry Andric   }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   // Attributes of the followup loop not specified explicity, so signal to the
3340b57cec5SDimitry Andric   // transformation pass to add suitable attributes.
3350b57cec5SDimitry Andric   if (!AlwaysNew && !HasAnyFollowup)
3360b57cec5SDimitry Andric     return None;
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   // If no attributes were added or remove, the previous loop Id can be reused.
3390b57cec5SDimitry Andric   if (!AlwaysNew && !Changed)
3400b57cec5SDimitry Andric     return OrigLoopID;
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   // No attributes is equivalent to having no !llvm.loop metadata at all.
3430b57cec5SDimitry Andric   if (MDs.size() == 1)
3440b57cec5SDimitry Andric     return nullptr;
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   // Build the new loop ID.
3470b57cec5SDimitry Andric   MDTuple *FollowupLoopID = MDNode::get(OrigLoopID->getContext(), MDs);
3480b57cec5SDimitry Andric   FollowupLoopID->replaceOperandWith(0, FollowupLoopID);
3490b57cec5SDimitry Andric   return FollowupLoopID;
3500b57cec5SDimitry Andric }
3510b57cec5SDimitry Andric 
hasDisableAllTransformsHint(const Loop * L)3520b57cec5SDimitry Andric bool llvm::hasDisableAllTransformsHint(const Loop *L) {
3530b57cec5SDimitry Andric   return getBooleanLoopAttribute(L, LLVMLoopDisableNonforced);
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric 
hasDisableLICMTransformsHint(const Loop * L)3568bcb0991SDimitry Andric bool llvm::hasDisableLICMTransformsHint(const Loop *L) {
3578bcb0991SDimitry Andric   return getBooleanLoopAttribute(L, LLVMLoopDisableLICM);
3588bcb0991SDimitry Andric }
3598bcb0991SDimitry Andric 
hasUnrollTransformation(const Loop * L)360*5f7ddb14SDimitry Andric TransformationMode llvm::hasUnrollTransformation(const Loop *L) {
3610b57cec5SDimitry Andric   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable"))
3620b57cec5SDimitry Andric     return TM_SuppressedByUser;
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   Optional<int> Count =
3650b57cec5SDimitry Andric       getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
3660b57cec5SDimitry Andric   if (Count.hasValue())
3670b57cec5SDimitry Andric     return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable"))
3700b57cec5SDimitry Andric     return TM_ForcedByUser;
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.full"))
3730b57cec5SDimitry Andric     return TM_ForcedByUser;
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   if (hasDisableAllTransformsHint(L))
3760b57cec5SDimitry Andric     return TM_Disable;
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric   return TM_Unspecified;
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric 
hasUnrollAndJamTransformation(const Loop * L)381*5f7ddb14SDimitry Andric TransformationMode llvm::hasUnrollAndJamTransformation(const Loop *L) {
3820b57cec5SDimitry Andric   if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable"))
3830b57cec5SDimitry Andric     return TM_SuppressedByUser;
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric   Optional<int> Count =
3860b57cec5SDimitry Andric       getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count");
3870b57cec5SDimitry Andric   if (Count.hasValue())
3880b57cec5SDimitry Andric     return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.enable"))
3910b57cec5SDimitry Andric     return TM_ForcedByUser;
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   if (hasDisableAllTransformsHint(L))
3940b57cec5SDimitry Andric     return TM_Disable;
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   return TM_Unspecified;
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric 
hasVectorizeTransformation(const Loop * L)399*5f7ddb14SDimitry Andric TransformationMode llvm::hasVectorizeTransformation(const Loop *L) {
4000b57cec5SDimitry Andric   Optional<bool> Enable =
4010b57cec5SDimitry Andric       getOptionalBoolLoopAttribute(L, "llvm.loop.vectorize.enable");
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric   if (Enable == false)
4040b57cec5SDimitry Andric     return TM_SuppressedByUser;
4050b57cec5SDimitry Andric 
406af732203SDimitry Andric   Optional<ElementCount> VectorizeWidth =
407af732203SDimitry Andric       getOptionalElementCountLoopAttribute(L);
4080b57cec5SDimitry Andric   Optional<int> InterleaveCount =
4090b57cec5SDimitry Andric       getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count");
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   // 'Forcing' vector width and interleave count to one effectively disables
4120b57cec5SDimitry Andric   // this tranformation.
413af732203SDimitry Andric   if (Enable == true && VectorizeWidth && VectorizeWidth->isScalar() &&
414af732203SDimitry Andric       InterleaveCount == 1)
4150b57cec5SDimitry Andric     return TM_SuppressedByUser;
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric   if (getBooleanLoopAttribute(L, "llvm.loop.isvectorized"))
4180b57cec5SDimitry Andric     return TM_Disable;
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric   if (Enable == true)
4210b57cec5SDimitry Andric     return TM_ForcedByUser;
4220b57cec5SDimitry Andric 
423af732203SDimitry Andric   if ((VectorizeWidth && VectorizeWidth->isScalar()) && InterleaveCount == 1)
4240b57cec5SDimitry Andric     return TM_Disable;
4250b57cec5SDimitry Andric 
426af732203SDimitry Andric   if ((VectorizeWidth && VectorizeWidth->isVector()) || InterleaveCount > 1)
4270b57cec5SDimitry Andric     return TM_Enable;
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric   if (hasDisableAllTransformsHint(L))
4300b57cec5SDimitry Andric     return TM_Disable;
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric   return TM_Unspecified;
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric 
hasDistributeTransformation(const Loop * L)435*5f7ddb14SDimitry Andric TransformationMode llvm::hasDistributeTransformation(const Loop *L) {
4360b57cec5SDimitry Andric   if (getBooleanLoopAttribute(L, "llvm.loop.distribute.enable"))
4370b57cec5SDimitry Andric     return TM_ForcedByUser;
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric   if (hasDisableAllTransformsHint(L))
4400b57cec5SDimitry Andric     return TM_Disable;
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   return TM_Unspecified;
4430b57cec5SDimitry Andric }
4440b57cec5SDimitry Andric 
hasLICMVersioningTransformation(const Loop * L)445*5f7ddb14SDimitry Andric TransformationMode llvm::hasLICMVersioningTransformation(const Loop *L) {
4460b57cec5SDimitry Andric   if (getBooleanLoopAttribute(L, "llvm.loop.licm_versioning.disable"))
4470b57cec5SDimitry Andric     return TM_SuppressedByUser;
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   if (hasDisableAllTransformsHint(L))
4500b57cec5SDimitry Andric     return TM_Disable;
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric   return TM_Unspecified;
4530b57cec5SDimitry Andric }
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric /// Does a BFS from a given node to all of its children inside a given loop.
4560b57cec5SDimitry Andric /// The returned vector of nodes includes the starting point.
4570b57cec5SDimitry Andric SmallVector<DomTreeNode *, 16>
collectChildrenInLoop(DomTreeNode * N,const Loop * CurLoop)4580b57cec5SDimitry Andric llvm::collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop) {
4590b57cec5SDimitry Andric   SmallVector<DomTreeNode *, 16> Worklist;
4600b57cec5SDimitry Andric   auto AddRegionToWorklist = [&](DomTreeNode *DTN) {
4610b57cec5SDimitry Andric     // Only include subregions in the top level loop.
4620b57cec5SDimitry Andric     BasicBlock *BB = DTN->getBlock();
4630b57cec5SDimitry Andric     if (CurLoop->contains(BB))
4640b57cec5SDimitry Andric       Worklist.push_back(DTN);
4650b57cec5SDimitry Andric   };
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   AddRegionToWorklist(N);
4680b57cec5SDimitry Andric 
4695ffd83dbSDimitry Andric   for (size_t I = 0; I < Worklist.size(); I++) {
4705ffd83dbSDimitry Andric     for (DomTreeNode *Child : Worklist[I]->children())
4710b57cec5SDimitry Andric       AddRegionToWorklist(Child);
4725ffd83dbSDimitry Andric   }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric   return Worklist;
4750b57cec5SDimitry Andric }
4760b57cec5SDimitry Andric 
deleteDeadLoop(Loop * L,DominatorTree * DT,ScalarEvolution * SE,LoopInfo * LI,MemorySSA * MSSA)4775ffd83dbSDimitry Andric void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
4785ffd83dbSDimitry Andric                           LoopInfo *LI, MemorySSA *MSSA) {
4790b57cec5SDimitry Andric   assert((!DT || L->isLCSSAForm(*DT)) && "Expected LCSSA!");
4800b57cec5SDimitry Andric   auto *Preheader = L->getLoopPreheader();
4810b57cec5SDimitry Andric   assert(Preheader && "Preheader should exist!");
4820b57cec5SDimitry Andric 
4835ffd83dbSDimitry Andric   std::unique_ptr<MemorySSAUpdater> MSSAU;
4845ffd83dbSDimitry Andric   if (MSSA)
4855ffd83dbSDimitry Andric     MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
4865ffd83dbSDimitry Andric 
4870b57cec5SDimitry Andric   // Now that we know the removal is safe, remove the loop by changing the
4880b57cec5SDimitry Andric   // branch from the preheader to go to the single exit block.
4890b57cec5SDimitry Andric   //
4900b57cec5SDimitry Andric   // Because we're deleting a large chunk of code at once, the sequence in which
4910b57cec5SDimitry Andric   // we remove things is very important to avoid invalidation issues.
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   // Tell ScalarEvolution that the loop is deleted. Do this before
4940b57cec5SDimitry Andric   // deleting the loop so that ScalarEvolution can look at the loop
4950b57cec5SDimitry Andric   // to determine what it needs to clean up.
4960b57cec5SDimitry Andric   if (SE)
4970b57cec5SDimitry Andric     SE->forgetLoop(L);
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator());
5000b57cec5SDimitry Andric   assert(OldBr && "Preheader must end with a branch");
5010b57cec5SDimitry Andric   assert(OldBr->isUnconditional() && "Preheader must have a single successor");
5020b57cec5SDimitry Andric   // Connect the preheader to the exit block. Keep the old edge to the header
5030b57cec5SDimitry Andric   // around to perform the dominator tree update in two separate steps
5040b57cec5SDimitry Andric   // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
5050b57cec5SDimitry Andric   // preheader -> header.
5060b57cec5SDimitry Andric   //
5070b57cec5SDimitry Andric   //
5080b57cec5SDimitry Andric   // 0.  Preheader          1.  Preheader           2.  Preheader
5090b57cec5SDimitry Andric   //        |                    |   |                   |
5100b57cec5SDimitry Andric   //        V                    |   V                   |
5110b57cec5SDimitry Andric   //      Header <--\            | Header <--\           | Header <--\
5120b57cec5SDimitry Andric   //       |  |     |            |  |  |     |           |  |  |     |
5130b57cec5SDimitry Andric   //       |  V     |            |  |  V     |           |  |  V     |
5140b57cec5SDimitry Andric   //       | Body --/            |  | Body --/           |  | Body --/
5150b57cec5SDimitry Andric   //       V                     V  V                    V  V
5160b57cec5SDimitry Andric   //      Exit                   Exit                    Exit
5170b57cec5SDimitry Andric   //
5180b57cec5SDimitry Andric   // By doing this is two separate steps we can perform the dominator tree
5190b57cec5SDimitry Andric   // update without using the batch update API.
5200b57cec5SDimitry Andric   //
5210b57cec5SDimitry Andric   // Even when the loop is never executed, we cannot remove the edge from the
5220b57cec5SDimitry Andric   // source block to the exit block. Consider the case where the unexecuted loop
5230b57cec5SDimitry Andric   // branches back to an outer loop. If we deleted the loop and removed the edge
5240b57cec5SDimitry Andric   // coming to this inner loop, this will break the outer loop structure (by
5250b57cec5SDimitry Andric   // deleting the backedge of the outer loop). If the outer loop is indeed a
5260b57cec5SDimitry Andric   // non-loop, it will be deleted in a future iteration of loop deletion pass.
5270b57cec5SDimitry Andric   IRBuilder<> Builder(OldBr);
528af732203SDimitry Andric 
529af732203SDimitry Andric   auto *ExitBlock = L->getUniqueExitBlock();
530af732203SDimitry Andric   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
531af732203SDimitry Andric   if (ExitBlock) {
532af732203SDimitry Andric     assert(ExitBlock && "Should have a unique exit block!");
533af732203SDimitry Andric     assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
534af732203SDimitry Andric 
5350b57cec5SDimitry Andric     Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
5360b57cec5SDimitry Andric     // Remove the old branch. The conditional branch becomes a new terminator.
5370b57cec5SDimitry Andric     OldBr->eraseFromParent();
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric     // Rewrite phis in the exit block to get their inputs from the Preheader
5400b57cec5SDimitry Andric     // instead of the exiting block.
5410b57cec5SDimitry Andric     for (PHINode &P : ExitBlock->phis()) {
5420b57cec5SDimitry Andric       // Set the zero'th element of Phi to be from the preheader and remove all
5430b57cec5SDimitry Andric       // other incoming values. Given the loop has dedicated exits, all other
5440b57cec5SDimitry Andric       // incoming values must be from the exiting blocks.
5450b57cec5SDimitry Andric       int PredIndex = 0;
5460b57cec5SDimitry Andric       P.setIncomingBlock(PredIndex, Preheader);
5470b57cec5SDimitry Andric       // Removes all incoming values from all other exiting blocks (including
5480b57cec5SDimitry Andric       // duplicate values from an exiting block).
5490b57cec5SDimitry Andric       // Nuke all entries except the zero'th entry which is the preheader entry.
5500b57cec5SDimitry Andric       // NOTE! We need to remove Incoming Values in the reverse order as done
5510b57cec5SDimitry Andric       // below, to keep the indices valid for deletion (removeIncomingValues
552af732203SDimitry Andric       // updates getNumIncomingValues and shifts all values down into the
553af732203SDimitry Andric       // operand being deleted).
5540b57cec5SDimitry Andric       for (unsigned i = 0, e = P.getNumIncomingValues() - 1; i != e; ++i)
5550b57cec5SDimitry Andric         P.removeIncomingValue(e - i, false);
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric       assert((P.getNumIncomingValues() == 1 &&
5580b57cec5SDimitry Andric               P.getIncomingBlock(PredIndex) == Preheader) &&
5590b57cec5SDimitry Andric              "Should have exactly one value and that's from the preheader!");
5600b57cec5SDimitry Andric     }
5610b57cec5SDimitry Andric 
5625ffd83dbSDimitry Andric     if (DT) {
5635ffd83dbSDimitry Andric       DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}});
5645ffd83dbSDimitry Andric       if (MSSA) {
565af732203SDimitry Andric         MSSAU->applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}},
566af732203SDimitry Andric                             *DT);
5675ffd83dbSDimitry Andric         if (VerifyMemorySSA)
5685ffd83dbSDimitry Andric           MSSA->verifyMemorySSA();
5695ffd83dbSDimitry Andric       }
5705ffd83dbSDimitry Andric     }
5715ffd83dbSDimitry Andric 
5720b57cec5SDimitry Andric     // Disconnect the loop body by branching directly to its exit.
5730b57cec5SDimitry Andric     Builder.SetInsertPoint(Preheader->getTerminator());
5740b57cec5SDimitry Andric     Builder.CreateBr(ExitBlock);
5750b57cec5SDimitry Andric     // Remove the old branch.
5760b57cec5SDimitry Andric     Preheader->getTerminator()->eraseFromParent();
577af732203SDimitry Andric   } else {
578af732203SDimitry Andric     assert(L->hasNoExitBlocks() &&
579af732203SDimitry Andric            "Loop should have either zero or one exit blocks.");
580af732203SDimitry Andric 
581af732203SDimitry Andric     Builder.SetInsertPoint(OldBr);
582af732203SDimitry Andric     Builder.CreateUnreachable();
583af732203SDimitry Andric     Preheader->getTerminator()->eraseFromParent();
584af732203SDimitry Andric   }
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   if (DT) {
5875ffd83dbSDimitry Andric     DTU.applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}});
5885ffd83dbSDimitry Andric     if (MSSA) {
5895ffd83dbSDimitry Andric       MSSAU->applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}},
5905ffd83dbSDimitry Andric                           *DT);
5915ffd83dbSDimitry Andric       SmallSetVector<BasicBlock *, 8> DeadBlockSet(L->block_begin(),
5925ffd83dbSDimitry Andric                                                    L->block_end());
5935ffd83dbSDimitry Andric       MSSAU->removeBlocks(DeadBlockSet);
5945ffd83dbSDimitry Andric       if (VerifyMemorySSA)
5955ffd83dbSDimitry Andric         MSSA->verifyMemorySSA();
5965ffd83dbSDimitry Andric     }
5970b57cec5SDimitry Andric   }
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric   // Use a map to unique and a vector to guarantee deterministic ordering.
6000b57cec5SDimitry Andric   llvm::SmallDenseSet<std::pair<DIVariable *, DIExpression *>, 4> DeadDebugSet;
6010b57cec5SDimitry Andric   llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst;
6020b57cec5SDimitry Andric 
603af732203SDimitry Andric   if (ExitBlock) {
6040b57cec5SDimitry Andric     // Given LCSSA form is satisfied, we should not have users of instructions
6050b57cec5SDimitry Andric     // within the dead loop outside of the loop. However, LCSSA doesn't take
6060b57cec5SDimitry Andric     // unreachable uses into account. We handle them here.
6070b57cec5SDimitry Andric     // We could do it after drop all references (in this case all users in the
6080b57cec5SDimitry Andric     // loop will be already eliminated and we have less work to do but according
6090b57cec5SDimitry Andric     // to API doc of User::dropAllReferences only valid operation after dropping
6100b57cec5SDimitry Andric     // references, is deletion. So let's substitute all usages of
6110b57cec5SDimitry Andric     // instruction from the loop with undef value of corresponding type first.
6120b57cec5SDimitry Andric     for (auto *Block : L->blocks())
6130b57cec5SDimitry Andric       for (Instruction &I : *Block) {
6140b57cec5SDimitry Andric         auto *Undef = UndefValue::get(I.getType());
615af732203SDimitry Andric         for (Value::use_iterator UI = I.use_begin(), E = I.use_end();
616af732203SDimitry Andric              UI != E;) {
6170b57cec5SDimitry Andric           Use &U = *UI;
6180b57cec5SDimitry Andric           ++UI;
6190b57cec5SDimitry Andric           if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
6200b57cec5SDimitry Andric             if (L->contains(Usr->getParent()))
6210b57cec5SDimitry Andric               continue;
6220b57cec5SDimitry Andric           // If we have a DT then we can check that uses outside a loop only in
6230b57cec5SDimitry Andric           // unreachable block.
6240b57cec5SDimitry Andric           if (DT)
6250b57cec5SDimitry Andric             assert(!DT->isReachableFromEntry(U) &&
6260b57cec5SDimitry Andric                    "Unexpected user in reachable block");
6270b57cec5SDimitry Andric           U.set(Undef);
6280b57cec5SDimitry Andric         }
6290b57cec5SDimitry Andric         auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
6300b57cec5SDimitry Andric         if (!DVI)
6310b57cec5SDimitry Andric           continue;
632af732203SDimitry Andric         auto Key =
633af732203SDimitry Andric             DeadDebugSet.find({DVI->getVariable(), DVI->getExpression()});
6340b57cec5SDimitry Andric         if (Key != DeadDebugSet.end())
6350b57cec5SDimitry Andric           continue;
6360b57cec5SDimitry Andric         DeadDebugSet.insert({DVI->getVariable(), DVI->getExpression()});
6370b57cec5SDimitry Andric         DeadDebugInst.push_back(DVI);
6380b57cec5SDimitry Andric       }
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric     // After the loop has been deleted all the values defined and modified
6410b57cec5SDimitry Andric     // inside the loop are going to be unavailable.
6420b57cec5SDimitry Andric     // Since debug values in the loop have been deleted, inserting an undef
6430b57cec5SDimitry Andric     // dbg.value truncates the range of any dbg.value before the loop where the
6440b57cec5SDimitry Andric     // loop used to be. This is particularly important for constant values.
6450b57cec5SDimitry Andric     DIBuilder DIB(*ExitBlock->getModule());
6460b57cec5SDimitry Andric     Instruction *InsertDbgValueBefore = ExitBlock->getFirstNonPHI();
6470b57cec5SDimitry Andric     assert(InsertDbgValueBefore &&
6480b57cec5SDimitry Andric            "There should be a non-PHI instruction in exit block, else these "
6490b57cec5SDimitry Andric            "instructions will have no parent.");
6500b57cec5SDimitry Andric     for (auto *DVI : DeadDebugInst)
6510b57cec5SDimitry Andric       DIB.insertDbgValueIntrinsic(UndefValue::get(Builder.getInt32Ty()),
6520b57cec5SDimitry Andric                                   DVI->getVariable(), DVI->getExpression(),
6530b57cec5SDimitry Andric                                   DVI->getDebugLoc(), InsertDbgValueBefore);
654af732203SDimitry Andric   }
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric   // Remove the block from the reference counting scheme, so that we can
6570b57cec5SDimitry Andric   // delete it freely later.
6580b57cec5SDimitry Andric   for (auto *Block : L->blocks())
6590b57cec5SDimitry Andric     Block->dropAllReferences();
6600b57cec5SDimitry Andric 
6615ffd83dbSDimitry Andric   if (MSSA && VerifyMemorySSA)
6625ffd83dbSDimitry Andric     MSSA->verifyMemorySSA();
6635ffd83dbSDimitry Andric 
6640b57cec5SDimitry Andric   if (LI) {
6650b57cec5SDimitry Andric     // Erase the instructions and the blocks without having to worry
6660b57cec5SDimitry Andric     // about ordering because we already dropped the references.
6670b57cec5SDimitry Andric     // NOTE: This iteration is safe because erasing the block does not remove
6680b57cec5SDimitry Andric     // its entry from the loop's block list.  We do that in the next section.
6690b57cec5SDimitry Andric     for (Loop::block_iterator LpI = L->block_begin(), LpE = L->block_end();
6700b57cec5SDimitry Andric          LpI != LpE; ++LpI)
6710b57cec5SDimitry Andric       (*LpI)->eraseFromParent();
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric     // Finally, the blocks from loopinfo.  This has to happen late because
6740b57cec5SDimitry Andric     // otherwise our loop iterators won't work.
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric     SmallPtrSet<BasicBlock *, 8> blocks;
6770b57cec5SDimitry Andric     blocks.insert(L->block_begin(), L->block_end());
6780b57cec5SDimitry Andric     for (BasicBlock *BB : blocks)
6790b57cec5SDimitry Andric       LI->removeBlock(BB);
6800b57cec5SDimitry Andric 
6810b57cec5SDimitry Andric     // The last step is to update LoopInfo now that we've eliminated this loop.
682480093f4SDimitry Andric     // Note: LoopInfo::erase remove the given loop and relink its subloops with
683480093f4SDimitry Andric     // its parent. While removeLoop/removeChildLoop remove the given loop but
684480093f4SDimitry Andric     // not relink its subloops, which is what we want.
685480093f4SDimitry Andric     if (Loop *ParentLoop = L->getParentLoop()) {
6865ffd83dbSDimitry Andric       Loop::iterator I = find(*ParentLoop, L);
687480093f4SDimitry Andric       assert(I != ParentLoop->end() && "Couldn't find loop");
688480093f4SDimitry Andric       ParentLoop->removeChildLoop(I);
689480093f4SDimitry Andric     } else {
6905ffd83dbSDimitry Andric       Loop::iterator I = find(*LI, L);
691480093f4SDimitry Andric       assert(I != LI->end() && "Couldn't find loop");
692480093f4SDimitry Andric       LI->removeLoop(I);
693480093f4SDimitry Andric     }
694480093f4SDimitry Andric     LI->destroy(L);
6950b57cec5SDimitry Andric   }
6960b57cec5SDimitry Andric }
6970b57cec5SDimitry Andric 
getOutermostLoop(Loop * L)698af732203SDimitry Andric static Loop *getOutermostLoop(Loop *L) {
699af732203SDimitry Andric   while (Loop *Parent = L->getParentLoop())
700af732203SDimitry Andric     L = Parent;
701af732203SDimitry Andric   return L;
702af732203SDimitry Andric }
703af732203SDimitry Andric 
breakLoopBackedge(Loop * L,DominatorTree & DT,ScalarEvolution & SE,LoopInfo & LI,MemorySSA * MSSA)704af732203SDimitry Andric void llvm::breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
705af732203SDimitry Andric                              LoopInfo &LI, MemorySSA *MSSA) {
706af732203SDimitry Andric   auto *Latch = L->getLoopLatch();
707af732203SDimitry Andric   assert(Latch && "multiple latches not yet supported");
708af732203SDimitry Andric   auto *Header = L->getHeader();
709af732203SDimitry Andric   Loop *OutermostLoop = getOutermostLoop(L);
710af732203SDimitry Andric 
711af732203SDimitry Andric   SE.forgetLoop(L);
712af732203SDimitry Andric 
713af732203SDimitry Andric   // Note: By splitting the backedge, and then explicitly making it unreachable
714af732203SDimitry Andric   // we gracefully handle corner cases such as non-bottom tested loops and the
715af732203SDimitry Andric   // like.  We also have the benefit of being able to reuse existing well tested
716af732203SDimitry Andric   // code.  It might be worth special casing the common bottom tested case at
717af732203SDimitry Andric   // some point to avoid code churn.
718af732203SDimitry Andric 
719af732203SDimitry Andric   std::unique_ptr<MemorySSAUpdater> MSSAU;
720af732203SDimitry Andric   if (MSSA)
721af732203SDimitry Andric     MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
722af732203SDimitry Andric 
723af732203SDimitry Andric   auto *BackedgeBB = SplitEdge(Latch, Header, &DT, &LI, MSSAU.get());
724af732203SDimitry Andric 
725af732203SDimitry Andric   DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Eager);
726*5f7ddb14SDimitry Andric   (void)changeToUnreachable(BackedgeBB->getTerminator(),
727af732203SDimitry Andric                             /*PreserveLCSSA*/ true, &DTU, MSSAU.get());
728af732203SDimitry Andric 
729af732203SDimitry Andric   // Erase (and destroy) this loop instance.  Handles relinking sub-loops
730af732203SDimitry Andric   // and blocks within the loop as needed.
731af732203SDimitry Andric   LI.erase(L);
732af732203SDimitry Andric 
733af732203SDimitry Andric   // If the loop we broke had a parent, then changeToUnreachable might have
734af732203SDimitry Andric   // caused a block to be removed from the parent loop (see loop_nest_lcssa
735af732203SDimitry Andric   // test case in zero-btc.ll for an example), thus changing the parent's
736af732203SDimitry Andric   // exit blocks.  If that happened, we need to rebuild LCSSA on the outermost
737af732203SDimitry Andric   // loop which might have a had a block removed.
738af732203SDimitry Andric   if (OutermostLoop != L)
739af732203SDimitry Andric     formLCSSARecursively(*OutermostLoop, DT, &LI, &SE);
740af732203SDimitry Andric }
741af732203SDimitry Andric 
742af732203SDimitry Andric 
7435ffd83dbSDimitry Andric /// Checks if \p L has single exit through latch block except possibly
7445ffd83dbSDimitry Andric /// "deoptimizing" exits. Returns branch instruction terminating the loop
7455ffd83dbSDimitry Andric /// latch if above check is successful, nullptr otherwise.
getExpectedExitLoopLatchBranch(Loop * L)7465ffd83dbSDimitry Andric static BranchInst *getExpectedExitLoopLatchBranch(Loop *L) {
7470b57cec5SDimitry Andric   BasicBlock *Latch = L->getLoopLatch();
7480b57cec5SDimitry Andric   if (!Latch)
7495ffd83dbSDimitry Andric     return nullptr;
7505ffd83dbSDimitry Andric 
7510b57cec5SDimitry Andric   BranchInst *LatchBR = dyn_cast<BranchInst>(Latch->getTerminator());
7520b57cec5SDimitry Andric   if (!LatchBR || LatchBR->getNumSuccessors() != 2 || !L->isLoopExiting(Latch))
7535ffd83dbSDimitry Andric     return nullptr;
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric   assert((LatchBR->getSuccessor(0) == L->getHeader() ||
7560b57cec5SDimitry Andric           LatchBR->getSuccessor(1) == L->getHeader()) &&
7570b57cec5SDimitry Andric          "At least one edge out of the latch must go to the header");
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   SmallVector<BasicBlock *, 4> ExitBlocks;
7600b57cec5SDimitry Andric   L->getUniqueNonLatchExitBlocks(ExitBlocks);
7610b57cec5SDimitry Andric   if (any_of(ExitBlocks, [](const BasicBlock *EB) {
7620b57cec5SDimitry Andric         return !EB->getTerminatingDeoptimizeCall();
7630b57cec5SDimitry Andric       }))
7645ffd83dbSDimitry Andric     return nullptr;
7655ffd83dbSDimitry Andric 
7665ffd83dbSDimitry Andric   return LatchBR;
7675ffd83dbSDimitry Andric }
7685ffd83dbSDimitry Andric 
7695ffd83dbSDimitry Andric Optional<unsigned>
getLoopEstimatedTripCount(Loop * L,unsigned * EstimatedLoopInvocationWeight)7705ffd83dbSDimitry Andric llvm::getLoopEstimatedTripCount(Loop *L,
7715ffd83dbSDimitry Andric                                 unsigned *EstimatedLoopInvocationWeight) {
7725ffd83dbSDimitry Andric   // Support loops with an exiting latch and other existing exists only
7735ffd83dbSDimitry Andric   // deoptimize.
7745ffd83dbSDimitry Andric   BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L);
7755ffd83dbSDimitry Andric   if (!LatchBranch)
7760b57cec5SDimitry Andric     return None;
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric   // To estimate the number of times the loop body was executed, we want to
7790b57cec5SDimitry Andric   // know the number of times the backedge was taken, vs. the number of times
7800b57cec5SDimitry Andric   // we exited the loop.
781480093f4SDimitry Andric   uint64_t BackedgeTakenWeight, LatchExitWeight;
7825ffd83dbSDimitry Andric   if (!LatchBranch->extractProfMetadata(BackedgeTakenWeight, LatchExitWeight))
7830b57cec5SDimitry Andric     return None;
7840b57cec5SDimitry Andric 
7855ffd83dbSDimitry Andric   if (LatchBranch->getSuccessor(0) != L->getHeader())
786480093f4SDimitry Andric     std::swap(BackedgeTakenWeight, LatchExitWeight);
787480093f4SDimitry Andric 
7885ffd83dbSDimitry Andric   if (!LatchExitWeight)
7895ffd83dbSDimitry Andric     return None;
7900b57cec5SDimitry Andric 
7915ffd83dbSDimitry Andric   if (EstimatedLoopInvocationWeight)
7925ffd83dbSDimitry Andric     *EstimatedLoopInvocationWeight = LatchExitWeight;
7935ffd83dbSDimitry Andric 
7945ffd83dbSDimitry Andric   // Estimated backedge taken count is a ratio of the backedge taken weight by
7955ffd83dbSDimitry Andric   // the weight of the edge exiting the loop, rounded to nearest.
7965ffd83dbSDimitry Andric   uint64_t BackedgeTakenCount =
7975ffd83dbSDimitry Andric       llvm::divideNearest(BackedgeTakenWeight, LatchExitWeight);
7985ffd83dbSDimitry Andric   // Estimated trip count is one plus estimated backedge taken count.
7995ffd83dbSDimitry Andric   return BackedgeTakenCount + 1;
8005ffd83dbSDimitry Andric }
8015ffd83dbSDimitry Andric 
setLoopEstimatedTripCount(Loop * L,unsigned EstimatedTripCount,unsigned EstimatedloopInvocationWeight)8025ffd83dbSDimitry Andric bool llvm::setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount,
8035ffd83dbSDimitry Andric                                      unsigned EstimatedloopInvocationWeight) {
8045ffd83dbSDimitry Andric   // Support loops with an exiting latch and other existing exists only
8055ffd83dbSDimitry Andric   // deoptimize.
8065ffd83dbSDimitry Andric   BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L);
8075ffd83dbSDimitry Andric   if (!LatchBranch)
8085ffd83dbSDimitry Andric     return false;
8095ffd83dbSDimitry Andric 
8105ffd83dbSDimitry Andric   // Calculate taken and exit weights.
8115ffd83dbSDimitry Andric   unsigned LatchExitWeight = 0;
8125ffd83dbSDimitry Andric   unsigned BackedgeTakenWeight = 0;
8135ffd83dbSDimitry Andric 
8145ffd83dbSDimitry Andric   if (EstimatedTripCount > 0) {
8155ffd83dbSDimitry Andric     LatchExitWeight = EstimatedloopInvocationWeight;
8165ffd83dbSDimitry Andric     BackedgeTakenWeight = (EstimatedTripCount - 1) * LatchExitWeight;
8175ffd83dbSDimitry Andric   }
8185ffd83dbSDimitry Andric 
8195ffd83dbSDimitry Andric   // Make a swap if back edge is taken when condition is "false".
8205ffd83dbSDimitry Andric   if (LatchBranch->getSuccessor(0) != L->getHeader())
8215ffd83dbSDimitry Andric     std::swap(BackedgeTakenWeight, LatchExitWeight);
8225ffd83dbSDimitry Andric 
8235ffd83dbSDimitry Andric   MDBuilder MDB(LatchBranch->getContext());
8245ffd83dbSDimitry Andric 
8255ffd83dbSDimitry Andric   // Set/Update profile metadata.
8265ffd83dbSDimitry Andric   LatchBranch->setMetadata(
8275ffd83dbSDimitry Andric       LLVMContext::MD_prof,
8285ffd83dbSDimitry Andric       MDB.createBranchWeights(BackedgeTakenWeight, LatchExitWeight));
8295ffd83dbSDimitry Andric 
8305ffd83dbSDimitry Andric   return true;
8310b57cec5SDimitry Andric }
8320b57cec5SDimitry Andric 
hasIterationCountInvariantInParent(Loop * InnerLoop,ScalarEvolution & SE)8330b57cec5SDimitry Andric bool llvm::hasIterationCountInvariantInParent(Loop *InnerLoop,
8340b57cec5SDimitry Andric                                               ScalarEvolution &SE) {
8350b57cec5SDimitry Andric   Loop *OuterL = InnerLoop->getParentLoop();
8360b57cec5SDimitry Andric   if (!OuterL)
8370b57cec5SDimitry Andric     return true;
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric   // Get the backedge taken count for the inner loop
8400b57cec5SDimitry Andric   BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
8410b57cec5SDimitry Andric   const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch);
8420b57cec5SDimitry Andric   if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
8430b57cec5SDimitry Andric       !InnerLoopBECountSC->getType()->isIntegerTy())
8440b57cec5SDimitry Andric     return false;
8450b57cec5SDimitry Andric 
8460b57cec5SDimitry Andric   // Get whether count is invariant to the outer loop
8470b57cec5SDimitry Andric   ScalarEvolution::LoopDisposition LD =
8480b57cec5SDimitry Andric       SE.getLoopDisposition(InnerLoopBECountSC, OuterL);
8490b57cec5SDimitry Andric   if (LD != ScalarEvolution::LoopInvariant)
8500b57cec5SDimitry Andric     return false;
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric   return true;
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric 
createMinMaxOp(IRBuilderBase & Builder,RecurKind RK,Value * Left,Value * Right)855af732203SDimitry Andric Value *llvm::createMinMaxOp(IRBuilderBase &Builder, RecurKind RK, Value *Left,
856af732203SDimitry Andric                             Value *Right) {
857af732203SDimitry Andric   CmpInst::Predicate Pred;
8580b57cec5SDimitry Andric   switch (RK) {
8590b57cec5SDimitry Andric   default:
8600b57cec5SDimitry Andric     llvm_unreachable("Unknown min/max recurrence kind");
861af732203SDimitry Andric   case RecurKind::UMin:
862af732203SDimitry Andric     Pred = CmpInst::ICMP_ULT;
8630b57cec5SDimitry Andric     break;
864af732203SDimitry Andric   case RecurKind::UMax:
865af732203SDimitry Andric     Pred = CmpInst::ICMP_UGT;
8660b57cec5SDimitry Andric     break;
867af732203SDimitry Andric   case RecurKind::SMin:
868af732203SDimitry Andric     Pred = CmpInst::ICMP_SLT;
8690b57cec5SDimitry Andric     break;
870af732203SDimitry Andric   case RecurKind::SMax:
871af732203SDimitry Andric     Pred = CmpInst::ICMP_SGT;
8720b57cec5SDimitry Andric     break;
873af732203SDimitry Andric   case RecurKind::FMin:
874af732203SDimitry Andric     Pred = CmpInst::FCMP_OLT;
8750b57cec5SDimitry Andric     break;
876af732203SDimitry Andric   case RecurKind::FMax:
877af732203SDimitry Andric     Pred = CmpInst::FCMP_OGT;
8780b57cec5SDimitry Andric     break;
8790b57cec5SDimitry Andric   }
8800b57cec5SDimitry Andric 
881af732203SDimitry Andric   Value *Cmp = Builder.CreateCmp(Pred, Left, Right, "rdx.minmax.cmp");
8820b57cec5SDimitry Andric   Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
8830b57cec5SDimitry Andric   return Select;
8840b57cec5SDimitry Andric }
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric // Helper to generate an ordered reduction.
getOrderedReduction(IRBuilderBase & Builder,Value * Acc,Value * Src,unsigned Op,RecurKind RdxKind,ArrayRef<Value * > RedOps)887af732203SDimitry Andric Value *llvm::getOrderedReduction(IRBuilderBase &Builder, Value *Acc, Value *Src,
888af732203SDimitry Andric                                  unsigned Op, RecurKind RdxKind,
8890b57cec5SDimitry Andric                                  ArrayRef<Value *> RedOps) {
8905ffd83dbSDimitry Andric   unsigned VF = cast<FixedVectorType>(Src->getType())->getNumElements();
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric   // Extract and apply reduction ops in ascending order:
8930b57cec5SDimitry Andric   // e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1]
8940b57cec5SDimitry Andric   Value *Result = Acc;
8950b57cec5SDimitry Andric   for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) {
8960b57cec5SDimitry Andric     Value *Ext =
8970b57cec5SDimitry Andric         Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx));
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric     if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
9000b57cec5SDimitry Andric       Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext,
9010b57cec5SDimitry Andric                                    "bin.rdx");
9020b57cec5SDimitry Andric     } else {
903af732203SDimitry Andric       assert(RecurrenceDescriptor::isMinMaxRecurrenceKind(RdxKind) &&
9040b57cec5SDimitry Andric              "Invalid min/max");
905af732203SDimitry Andric       Result = createMinMaxOp(Builder, RdxKind, Result, Ext);
9060b57cec5SDimitry Andric     }
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric     if (!RedOps.empty())
9090b57cec5SDimitry Andric       propagateIRFlags(Result, RedOps);
9100b57cec5SDimitry Andric   }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric   return Result;
9130b57cec5SDimitry Andric }
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric // Helper to generate a log2 shuffle reduction.
getShuffleReduction(IRBuilderBase & Builder,Value * Src,unsigned Op,RecurKind RdxKind,ArrayRef<Value * > RedOps)916af732203SDimitry Andric Value *llvm::getShuffleReduction(IRBuilderBase &Builder, Value *Src,
917af732203SDimitry Andric                                  unsigned Op, RecurKind RdxKind,
9180b57cec5SDimitry Andric                                  ArrayRef<Value *> RedOps) {
9195ffd83dbSDimitry Andric   unsigned VF = cast<FixedVectorType>(Src->getType())->getNumElements();
9200b57cec5SDimitry Andric   // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
9210b57cec5SDimitry Andric   // and vector ops, reducing the set of values being computed by half each
9220b57cec5SDimitry Andric   // round.
9230b57cec5SDimitry Andric   assert(isPowerOf2_32(VF) &&
9240b57cec5SDimitry Andric          "Reduction emission only supported for pow2 vectors!");
9250b57cec5SDimitry Andric   Value *TmpVec = Src;
9265ffd83dbSDimitry Andric   SmallVector<int, 32> ShuffleMask(VF);
9270b57cec5SDimitry Andric   for (unsigned i = VF; i != 1; i >>= 1) {
9280b57cec5SDimitry Andric     // Move the upper half of the vector to the lower half.
9290b57cec5SDimitry Andric     for (unsigned j = 0; j != i / 2; ++j)
9305ffd83dbSDimitry Andric       ShuffleMask[j] = i / 2 + j;
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric     // Fill the rest of the mask with undef.
9335ffd83dbSDimitry Andric     std::fill(&ShuffleMask[i / 2], ShuffleMask.end(), -1);
9340b57cec5SDimitry Andric 
935af732203SDimitry Andric     Value *Shuf = Builder.CreateShuffleVector(TmpVec, ShuffleMask, "rdx.shuf");
9360b57cec5SDimitry Andric 
9370b57cec5SDimitry Andric     if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
9380b57cec5SDimitry Andric       // The builder propagates its fast-math-flags setting.
9390b57cec5SDimitry Andric       TmpVec = Builder.CreateBinOp((Instruction::BinaryOps)Op, TmpVec, Shuf,
9400b57cec5SDimitry Andric                                    "bin.rdx");
9410b57cec5SDimitry Andric     } else {
942af732203SDimitry Andric       assert(RecurrenceDescriptor::isMinMaxRecurrenceKind(RdxKind) &&
9430b57cec5SDimitry Andric              "Invalid min/max");
944af732203SDimitry Andric       TmpVec = createMinMaxOp(Builder, RdxKind, TmpVec, Shuf);
9450b57cec5SDimitry Andric     }
9460b57cec5SDimitry Andric     if (!RedOps.empty())
9470b57cec5SDimitry Andric       propagateIRFlags(TmpVec, RedOps);
9485ffd83dbSDimitry Andric 
9495ffd83dbSDimitry Andric     // We may compute the reassociated scalar ops in a way that does not
9505ffd83dbSDimitry Andric     // preserve nsw/nuw etc. Conservatively, drop those flags.
9515ffd83dbSDimitry Andric     if (auto *ReductionInst = dyn_cast<Instruction>(TmpVec))
9525ffd83dbSDimitry Andric       ReductionInst->dropPoisonGeneratingFlags();
9530b57cec5SDimitry Andric   }
9540b57cec5SDimitry Andric   // The result is in the first element of the vector.
9550b57cec5SDimitry Andric   return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
9560b57cec5SDimitry Andric }
9570b57cec5SDimitry Andric 
createSimpleTargetReduction(IRBuilderBase & Builder,const TargetTransformInfo * TTI,Value * Src,RecurKind RdxKind,ArrayRef<Value * > RedOps)958af732203SDimitry Andric Value *llvm::createSimpleTargetReduction(IRBuilderBase &Builder,
959af732203SDimitry Andric                                          const TargetTransformInfo *TTI,
960af732203SDimitry Andric                                          Value *Src, RecurKind RdxKind,
9610b57cec5SDimitry Andric                                          ArrayRef<Value *> RedOps) {
962af732203SDimitry Andric   TargetTransformInfo::ReductionFlags RdxFlags;
963af732203SDimitry Andric   RdxFlags.IsMaxOp = RdxKind == RecurKind::SMax || RdxKind == RecurKind::UMax ||
964af732203SDimitry Andric                      RdxKind == RecurKind::FMax;
965af732203SDimitry Andric   RdxFlags.IsSigned = RdxKind == RecurKind::SMax || RdxKind == RecurKind::SMin;
9660b57cec5SDimitry Andric 
967af732203SDimitry Andric   auto *SrcVecEltTy = cast<VectorType>(Src->getType())->getElementType();
968af732203SDimitry Andric   switch (RdxKind) {
969af732203SDimitry Andric   case RecurKind::Add:
970af732203SDimitry Andric     return Builder.CreateAddReduce(Src);
971af732203SDimitry Andric   case RecurKind::Mul:
972af732203SDimitry Andric     return Builder.CreateMulReduce(Src);
973af732203SDimitry Andric   case RecurKind::And:
974af732203SDimitry Andric     return Builder.CreateAndReduce(Src);
975af732203SDimitry Andric   case RecurKind::Or:
976af732203SDimitry Andric     return Builder.CreateOrReduce(Src);
977af732203SDimitry Andric   case RecurKind::Xor:
978af732203SDimitry Andric     return Builder.CreateXorReduce(Src);
979af732203SDimitry Andric   case RecurKind::FAdd:
980af732203SDimitry Andric     return Builder.CreateFAddReduce(ConstantFP::getNegativeZero(SrcVecEltTy),
981af732203SDimitry Andric                                     Src);
982af732203SDimitry Andric   case RecurKind::FMul:
983af732203SDimitry Andric     return Builder.CreateFMulReduce(ConstantFP::get(SrcVecEltTy, 1.0), Src);
984af732203SDimitry Andric   case RecurKind::SMax:
985af732203SDimitry Andric     return Builder.CreateIntMaxReduce(Src, true);
986af732203SDimitry Andric   case RecurKind::SMin:
987af732203SDimitry Andric     return Builder.CreateIntMinReduce(Src, true);
988af732203SDimitry Andric   case RecurKind::UMax:
989af732203SDimitry Andric     return Builder.CreateIntMaxReduce(Src, false);
990af732203SDimitry Andric   case RecurKind::UMin:
991af732203SDimitry Andric     return Builder.CreateIntMinReduce(Src, false);
992af732203SDimitry Andric   case RecurKind::FMax:
993af732203SDimitry Andric     return Builder.CreateFPMaxReduce(Src);
994af732203SDimitry Andric   case RecurKind::FMin:
995af732203SDimitry Andric     return Builder.CreateFPMinReduce(Src);
9960b57cec5SDimitry Andric   default:
9970b57cec5SDimitry Andric     llvm_unreachable("Unhandled opcode");
9980b57cec5SDimitry Andric   }
9990b57cec5SDimitry Andric }
10000b57cec5SDimitry Andric 
createTargetReduction(IRBuilderBase & B,const TargetTransformInfo * TTI,const RecurrenceDescriptor & Desc,Value * Src)10015ffd83dbSDimitry Andric Value *llvm::createTargetReduction(IRBuilderBase &B,
10020b57cec5SDimitry Andric                                    const TargetTransformInfo *TTI,
1003*5f7ddb14SDimitry Andric                                    const RecurrenceDescriptor &Desc,
1004*5f7ddb14SDimitry Andric                                    Value *Src) {
10050b57cec5SDimitry Andric   // TODO: Support in-order reductions based on the recurrence descriptor.
10060b57cec5SDimitry Andric   // All ops in the reduction inherit fast-math-flags from the recurrence
10070b57cec5SDimitry Andric   // descriptor.
10085ffd83dbSDimitry Andric   IRBuilderBase::FastMathFlagGuard FMFGuard(B);
10090b57cec5SDimitry Andric   B.setFastMathFlags(Desc.getFastMathFlags());
1010af732203SDimitry Andric   return createSimpleTargetReduction(B, TTI, Src, Desc.getRecurrenceKind());
10110b57cec5SDimitry Andric }
10120b57cec5SDimitry Andric 
createOrderedReduction(IRBuilderBase & B,const RecurrenceDescriptor & Desc,Value * Src,Value * Start)1013*5f7ddb14SDimitry Andric Value *llvm::createOrderedReduction(IRBuilderBase &B,
1014*5f7ddb14SDimitry Andric                                     const RecurrenceDescriptor &Desc,
1015*5f7ddb14SDimitry Andric                                     Value *Src, Value *Start) {
1016*5f7ddb14SDimitry Andric   assert(Desc.getRecurrenceKind() == RecurKind::FAdd &&
1017*5f7ddb14SDimitry Andric          "Unexpected reduction kind");
1018*5f7ddb14SDimitry Andric   assert(Src->getType()->isVectorTy() && "Expected a vector type");
1019*5f7ddb14SDimitry Andric   assert(!Start->getType()->isVectorTy() && "Expected a scalar type");
1020*5f7ddb14SDimitry Andric 
1021*5f7ddb14SDimitry Andric   return B.CreateFAddReduce(Start, Src);
1022*5f7ddb14SDimitry Andric }
1023*5f7ddb14SDimitry Andric 
propagateIRFlags(Value * I,ArrayRef<Value * > VL,Value * OpValue)10240b57cec5SDimitry Andric void llvm::propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue) {
10250b57cec5SDimitry Andric   auto *VecOp = dyn_cast<Instruction>(I);
10260b57cec5SDimitry Andric   if (!VecOp)
10270b57cec5SDimitry Andric     return;
10280b57cec5SDimitry Andric   auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0])
10290b57cec5SDimitry Andric                                             : dyn_cast<Instruction>(OpValue);
10300b57cec5SDimitry Andric   if (!Intersection)
10310b57cec5SDimitry Andric     return;
10320b57cec5SDimitry Andric   const unsigned Opcode = Intersection->getOpcode();
10330b57cec5SDimitry Andric   VecOp->copyIRFlags(Intersection);
10340b57cec5SDimitry Andric   for (auto *V : VL) {
10350b57cec5SDimitry Andric     auto *Instr = dyn_cast<Instruction>(V);
10360b57cec5SDimitry Andric     if (!Instr)
10370b57cec5SDimitry Andric       continue;
10380b57cec5SDimitry Andric     if (OpValue == nullptr || Opcode == Instr->getOpcode())
10390b57cec5SDimitry Andric       VecOp->andIRFlags(V);
10400b57cec5SDimitry Andric   }
10410b57cec5SDimitry Andric }
10420b57cec5SDimitry Andric 
isKnownNegativeInLoop(const SCEV * S,const Loop * L,ScalarEvolution & SE)10430b57cec5SDimitry Andric bool llvm::isKnownNegativeInLoop(const SCEV *S, const Loop *L,
10440b57cec5SDimitry Andric                                  ScalarEvolution &SE) {
10450b57cec5SDimitry Andric   const SCEV *Zero = SE.getZero(S->getType());
10460b57cec5SDimitry Andric   return SE.isAvailableAtLoopEntry(S, L) &&
10470b57cec5SDimitry Andric          SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, S, Zero);
10480b57cec5SDimitry Andric }
10490b57cec5SDimitry Andric 
isKnownNonNegativeInLoop(const SCEV * S,const Loop * L,ScalarEvolution & SE)10500b57cec5SDimitry Andric bool llvm::isKnownNonNegativeInLoop(const SCEV *S, const Loop *L,
10510b57cec5SDimitry Andric                                     ScalarEvolution &SE) {
10520b57cec5SDimitry Andric   const SCEV *Zero = SE.getZero(S->getType());
10530b57cec5SDimitry Andric   return SE.isAvailableAtLoopEntry(S, L) &&
10540b57cec5SDimitry Andric          SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, S, Zero);
10550b57cec5SDimitry Andric }
10560b57cec5SDimitry Andric 
cannotBeMinInLoop(const SCEV * S,const Loop * L,ScalarEvolution & SE,bool Signed)10570b57cec5SDimitry Andric bool llvm::cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
10580b57cec5SDimitry Andric                              bool Signed) {
10590b57cec5SDimitry Andric   unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
10600b57cec5SDimitry Andric   APInt Min = Signed ? APInt::getSignedMinValue(BitWidth) :
10610b57cec5SDimitry Andric     APInt::getMinValue(BitWidth);
10620b57cec5SDimitry Andric   auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
10630b57cec5SDimitry Andric   return SE.isAvailableAtLoopEntry(S, L) &&
10640b57cec5SDimitry Andric          SE.isLoopEntryGuardedByCond(L, Predicate, S,
10650b57cec5SDimitry Andric                                      SE.getConstant(Min));
10660b57cec5SDimitry Andric }
10670b57cec5SDimitry Andric 
cannotBeMaxInLoop(const SCEV * S,const Loop * L,ScalarEvolution & SE,bool Signed)10680b57cec5SDimitry Andric bool llvm::cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
10690b57cec5SDimitry Andric                              bool Signed) {
10700b57cec5SDimitry Andric   unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
10710b57cec5SDimitry Andric   APInt Max = Signed ? APInt::getSignedMaxValue(BitWidth) :
10720b57cec5SDimitry Andric     APInt::getMaxValue(BitWidth);
10730b57cec5SDimitry Andric   auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
10740b57cec5SDimitry Andric   return SE.isAvailableAtLoopEntry(S, L) &&
10750b57cec5SDimitry Andric          SE.isLoopEntryGuardedByCond(L, Predicate, S,
10760b57cec5SDimitry Andric                                      SE.getConstant(Max));
10770b57cec5SDimitry Andric }
10785ffd83dbSDimitry Andric 
10795ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
10805ffd83dbSDimitry Andric // rewriteLoopExitValues - Optimize IV users outside the loop.
10815ffd83dbSDimitry Andric // As a side effect, reduces the amount of IV processing within the loop.
10825ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
10835ffd83dbSDimitry Andric 
10845ffd83dbSDimitry Andric // Return true if the SCEV expansion generated by the rewriter can replace the
10855ffd83dbSDimitry Andric // original value. SCEV guarantees that it produces the same value, but the way
10865ffd83dbSDimitry Andric // it is produced may be illegal IR.  Ideally, this function will only be
10875ffd83dbSDimitry Andric // called for verification.
isValidRewrite(ScalarEvolution * SE,Value * FromVal,Value * ToVal)10885ffd83dbSDimitry Andric static bool isValidRewrite(ScalarEvolution *SE, Value *FromVal, Value *ToVal) {
10895ffd83dbSDimitry Andric   // If an SCEV expression subsumed multiple pointers, its expansion could
10905ffd83dbSDimitry Andric   // reassociate the GEP changing the base pointer. This is illegal because the
10915ffd83dbSDimitry Andric   // final address produced by a GEP chain must be inbounds relative to its
10925ffd83dbSDimitry Andric   // underlying object. Otherwise basic alias analysis, among other things,
10935ffd83dbSDimitry Andric   // could fail in a dangerous way. Ultimately, SCEV will be improved to avoid
10945ffd83dbSDimitry Andric   // producing an expression involving multiple pointers. Until then, we must
10955ffd83dbSDimitry Andric   // bail out here.
10965ffd83dbSDimitry Andric   //
1097af732203SDimitry Andric   // Retrieve the pointer operand of the GEP. Don't use getUnderlyingObject
10985ffd83dbSDimitry Andric   // because it understands lcssa phis while SCEV does not.
10995ffd83dbSDimitry Andric   Value *FromPtr = FromVal;
11005ffd83dbSDimitry Andric   Value *ToPtr = ToVal;
11015ffd83dbSDimitry Andric   if (auto *GEP = dyn_cast<GEPOperator>(FromVal))
11025ffd83dbSDimitry Andric     FromPtr = GEP->getPointerOperand();
11035ffd83dbSDimitry Andric 
11045ffd83dbSDimitry Andric   if (auto *GEP = dyn_cast<GEPOperator>(ToVal))
11055ffd83dbSDimitry Andric     ToPtr = GEP->getPointerOperand();
11065ffd83dbSDimitry Andric 
11075ffd83dbSDimitry Andric   if (FromPtr != FromVal || ToPtr != ToVal) {
11085ffd83dbSDimitry Andric     // Quickly check the common case
11095ffd83dbSDimitry Andric     if (FromPtr == ToPtr)
11105ffd83dbSDimitry Andric       return true;
11115ffd83dbSDimitry Andric 
11125ffd83dbSDimitry Andric     // SCEV may have rewritten an expression that produces the GEP's pointer
11135ffd83dbSDimitry Andric     // operand. That's ok as long as the pointer operand has the same base
1114af732203SDimitry Andric     // pointer. Unlike getUnderlyingObject(), getPointerBase() will find the
11155ffd83dbSDimitry Andric     // base of a recurrence. This handles the case in which SCEV expansion
11165ffd83dbSDimitry Andric     // converts a pointer type recurrence into a nonrecurrent pointer base
11175ffd83dbSDimitry Andric     // indexed by an integer recurrence.
11185ffd83dbSDimitry Andric 
11195ffd83dbSDimitry Andric     // If the GEP base pointer is a vector of pointers, abort.
11205ffd83dbSDimitry Andric     if (!FromPtr->getType()->isPointerTy() || !ToPtr->getType()->isPointerTy())
11215ffd83dbSDimitry Andric       return false;
11225ffd83dbSDimitry Andric 
11235ffd83dbSDimitry Andric     const SCEV *FromBase = SE->getPointerBase(SE->getSCEV(FromPtr));
11245ffd83dbSDimitry Andric     const SCEV *ToBase = SE->getPointerBase(SE->getSCEV(ToPtr));
11255ffd83dbSDimitry Andric     if (FromBase == ToBase)
11265ffd83dbSDimitry Andric       return true;
11275ffd83dbSDimitry Andric 
11285ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: GEP rewrite bail out "
11295ffd83dbSDimitry Andric                       << *FromBase << " != " << *ToBase << "\n");
11305ffd83dbSDimitry Andric 
11315ffd83dbSDimitry Andric     return false;
11325ffd83dbSDimitry Andric   }
11335ffd83dbSDimitry Andric   return true;
11345ffd83dbSDimitry Andric }
11355ffd83dbSDimitry Andric 
hasHardUserWithinLoop(const Loop * L,const Instruction * I)11365ffd83dbSDimitry Andric static bool hasHardUserWithinLoop(const Loop *L, const Instruction *I) {
11375ffd83dbSDimitry Andric   SmallPtrSet<const Instruction *, 8> Visited;
11385ffd83dbSDimitry Andric   SmallVector<const Instruction *, 8> WorkList;
11395ffd83dbSDimitry Andric   Visited.insert(I);
11405ffd83dbSDimitry Andric   WorkList.push_back(I);
11415ffd83dbSDimitry Andric   while (!WorkList.empty()) {
11425ffd83dbSDimitry Andric     const Instruction *Curr = WorkList.pop_back_val();
11435ffd83dbSDimitry Andric     // This use is outside the loop, nothing to do.
11445ffd83dbSDimitry Andric     if (!L->contains(Curr))
11455ffd83dbSDimitry Andric       continue;
11465ffd83dbSDimitry Andric     // Do we assume it is a "hard" use which will not be eliminated easily?
11475ffd83dbSDimitry Andric     if (Curr->mayHaveSideEffects())
11485ffd83dbSDimitry Andric       return true;
11495ffd83dbSDimitry Andric     // Otherwise, add all its users to worklist.
11505ffd83dbSDimitry Andric     for (auto U : Curr->users()) {
11515ffd83dbSDimitry Andric       auto *UI = cast<Instruction>(U);
11525ffd83dbSDimitry Andric       if (Visited.insert(UI).second)
11535ffd83dbSDimitry Andric         WorkList.push_back(UI);
11545ffd83dbSDimitry Andric     }
11555ffd83dbSDimitry Andric   }
11565ffd83dbSDimitry Andric   return false;
11575ffd83dbSDimitry Andric }
11585ffd83dbSDimitry Andric 
11595ffd83dbSDimitry Andric // Collect information about PHI nodes which can be transformed in
11605ffd83dbSDimitry Andric // rewriteLoopExitValues.
11615ffd83dbSDimitry Andric struct RewritePhi {
11625ffd83dbSDimitry Andric   PHINode *PN;               // For which PHI node is this replacement?
11635ffd83dbSDimitry Andric   unsigned Ith;              // For which incoming value?
11645ffd83dbSDimitry Andric   const SCEV *ExpansionSCEV; // The SCEV of the incoming value we are rewriting.
11655ffd83dbSDimitry Andric   Instruction *ExpansionPoint; // Where we'd like to expand that SCEV?
11665ffd83dbSDimitry Andric   bool HighCost;               // Is this expansion a high-cost?
11675ffd83dbSDimitry Andric 
11685ffd83dbSDimitry Andric   Value *Expansion = nullptr;
11695ffd83dbSDimitry Andric   bool ValidRewrite = false;
11705ffd83dbSDimitry Andric 
RewritePhiRewritePhi11715ffd83dbSDimitry Andric   RewritePhi(PHINode *P, unsigned I, const SCEV *Val, Instruction *ExpansionPt,
11725ffd83dbSDimitry Andric              bool H)
11735ffd83dbSDimitry Andric       : PN(P), Ith(I), ExpansionSCEV(Val), ExpansionPoint(ExpansionPt),
11745ffd83dbSDimitry Andric         HighCost(H) {}
11755ffd83dbSDimitry Andric };
11765ffd83dbSDimitry Andric 
11775ffd83dbSDimitry Andric // Check whether it is possible to delete the loop after rewriting exit
11785ffd83dbSDimitry Andric // value. If it is possible, ignore ReplaceExitValue and do rewriting
11795ffd83dbSDimitry Andric // aggressively.
canLoopBeDeleted(Loop * L,SmallVector<RewritePhi,8> & RewritePhiSet)11805ffd83dbSDimitry Andric static bool canLoopBeDeleted(Loop *L, SmallVector<RewritePhi, 8> &RewritePhiSet) {
11815ffd83dbSDimitry Andric   BasicBlock *Preheader = L->getLoopPreheader();
11825ffd83dbSDimitry Andric   // If there is no preheader, the loop will not be deleted.
11835ffd83dbSDimitry Andric   if (!Preheader)
11845ffd83dbSDimitry Andric     return false;
11855ffd83dbSDimitry Andric 
11865ffd83dbSDimitry Andric   // In LoopDeletion pass Loop can be deleted when ExitingBlocks.size() > 1.
11875ffd83dbSDimitry Andric   // We obviate multiple ExitingBlocks case for simplicity.
11885ffd83dbSDimitry Andric   // TODO: If we see testcase with multiple ExitingBlocks can be deleted
11895ffd83dbSDimitry Andric   // after exit value rewriting, we can enhance the logic here.
11905ffd83dbSDimitry Andric   SmallVector<BasicBlock *, 4> ExitingBlocks;
11915ffd83dbSDimitry Andric   L->getExitingBlocks(ExitingBlocks);
11925ffd83dbSDimitry Andric   SmallVector<BasicBlock *, 8> ExitBlocks;
11935ffd83dbSDimitry Andric   L->getUniqueExitBlocks(ExitBlocks);
11945ffd83dbSDimitry Andric   if (ExitBlocks.size() != 1 || ExitingBlocks.size() != 1)
11955ffd83dbSDimitry Andric     return false;
11965ffd83dbSDimitry Andric 
11975ffd83dbSDimitry Andric   BasicBlock *ExitBlock = ExitBlocks[0];
11985ffd83dbSDimitry Andric   BasicBlock::iterator BI = ExitBlock->begin();
11995ffd83dbSDimitry Andric   while (PHINode *P = dyn_cast<PHINode>(BI)) {
12005ffd83dbSDimitry Andric     Value *Incoming = P->getIncomingValueForBlock(ExitingBlocks[0]);
12015ffd83dbSDimitry Andric 
12025ffd83dbSDimitry Andric     // If the Incoming value of P is found in RewritePhiSet, we know it
12035ffd83dbSDimitry Andric     // could be rewritten to use a loop invariant value in transformation
12045ffd83dbSDimitry Andric     // phase later. Skip it in the loop invariant check below.
12055ffd83dbSDimitry Andric     bool found = false;
12065ffd83dbSDimitry Andric     for (const RewritePhi &Phi : RewritePhiSet) {
12075ffd83dbSDimitry Andric       if (!Phi.ValidRewrite)
12085ffd83dbSDimitry Andric         continue;
12095ffd83dbSDimitry Andric       unsigned i = Phi.Ith;
12105ffd83dbSDimitry Andric       if (Phi.PN == P && (Phi.PN)->getIncomingValue(i) == Incoming) {
12115ffd83dbSDimitry Andric         found = true;
12125ffd83dbSDimitry Andric         break;
12135ffd83dbSDimitry Andric       }
12145ffd83dbSDimitry Andric     }
12155ffd83dbSDimitry Andric 
12165ffd83dbSDimitry Andric     Instruction *I;
12175ffd83dbSDimitry Andric     if (!found && (I = dyn_cast<Instruction>(Incoming)))
12185ffd83dbSDimitry Andric       if (!L->hasLoopInvariantOperands(I))
12195ffd83dbSDimitry Andric         return false;
12205ffd83dbSDimitry Andric 
12215ffd83dbSDimitry Andric     ++BI;
12225ffd83dbSDimitry Andric   }
12235ffd83dbSDimitry Andric 
12245ffd83dbSDimitry Andric   for (auto *BB : L->blocks())
12255ffd83dbSDimitry Andric     if (llvm::any_of(*BB, [](Instruction &I) {
12265ffd83dbSDimitry Andric           return I.mayHaveSideEffects();
12275ffd83dbSDimitry Andric         }))
12285ffd83dbSDimitry Andric       return false;
12295ffd83dbSDimitry Andric 
12305ffd83dbSDimitry Andric   return true;
12315ffd83dbSDimitry Andric }
12325ffd83dbSDimitry Andric 
rewriteLoopExitValues(Loop * L,LoopInfo * LI,TargetLibraryInfo * TLI,ScalarEvolution * SE,const TargetTransformInfo * TTI,SCEVExpander & Rewriter,DominatorTree * DT,ReplaceExitVal ReplaceExitValue,SmallVector<WeakTrackingVH,16> & DeadInsts)12335ffd83dbSDimitry Andric int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
12345ffd83dbSDimitry Andric                                 ScalarEvolution *SE,
12355ffd83dbSDimitry Andric                                 const TargetTransformInfo *TTI,
12365ffd83dbSDimitry Andric                                 SCEVExpander &Rewriter, DominatorTree *DT,
12375ffd83dbSDimitry Andric                                 ReplaceExitVal ReplaceExitValue,
12385ffd83dbSDimitry Andric                                 SmallVector<WeakTrackingVH, 16> &DeadInsts) {
12395ffd83dbSDimitry Andric   // Check a pre-condition.
12405ffd83dbSDimitry Andric   assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
12415ffd83dbSDimitry Andric          "Indvars did not preserve LCSSA!");
12425ffd83dbSDimitry Andric 
12435ffd83dbSDimitry Andric   SmallVector<BasicBlock*, 8> ExitBlocks;
12445ffd83dbSDimitry Andric   L->getUniqueExitBlocks(ExitBlocks);
12455ffd83dbSDimitry Andric 
12465ffd83dbSDimitry Andric   SmallVector<RewritePhi, 8> RewritePhiSet;
12475ffd83dbSDimitry Andric   // Find all values that are computed inside the loop, but used outside of it.
12485ffd83dbSDimitry Andric   // Because of LCSSA, these values will only occur in LCSSA PHI Nodes.  Scan
12495ffd83dbSDimitry Andric   // the exit blocks of the loop to find them.
12505ffd83dbSDimitry Andric   for (BasicBlock *ExitBB : ExitBlocks) {
12515ffd83dbSDimitry Andric     // If there are no PHI nodes in this exit block, then no values defined
12525ffd83dbSDimitry Andric     // inside the loop are used on this path, skip it.
12535ffd83dbSDimitry Andric     PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
12545ffd83dbSDimitry Andric     if (!PN) continue;
12555ffd83dbSDimitry Andric 
12565ffd83dbSDimitry Andric     unsigned NumPreds = PN->getNumIncomingValues();
12575ffd83dbSDimitry Andric 
12585ffd83dbSDimitry Andric     // Iterate over all of the PHI nodes.
12595ffd83dbSDimitry Andric     BasicBlock::iterator BBI = ExitBB->begin();
12605ffd83dbSDimitry Andric     while ((PN = dyn_cast<PHINode>(BBI++))) {
12615ffd83dbSDimitry Andric       if (PN->use_empty())
12625ffd83dbSDimitry Andric         continue; // dead use, don't replace it
12635ffd83dbSDimitry Andric 
12645ffd83dbSDimitry Andric       if (!SE->isSCEVable(PN->getType()))
12655ffd83dbSDimitry Andric         continue;
12665ffd83dbSDimitry Andric 
12675ffd83dbSDimitry Andric       // It's necessary to tell ScalarEvolution about this explicitly so that
12685ffd83dbSDimitry Andric       // it can walk the def-use list and forget all SCEVs, as it may not be
12695ffd83dbSDimitry Andric       // watching the PHI itself. Once the new exit value is in place, there
12705ffd83dbSDimitry Andric       // may not be a def-use connection between the loop and every instruction
12715ffd83dbSDimitry Andric       // which got a SCEVAddRecExpr for that loop.
12725ffd83dbSDimitry Andric       SE->forgetValue(PN);
12735ffd83dbSDimitry Andric 
12745ffd83dbSDimitry Andric       // Iterate over all of the values in all the PHI nodes.
12755ffd83dbSDimitry Andric       for (unsigned i = 0; i != NumPreds; ++i) {
12765ffd83dbSDimitry Andric         // If the value being merged in is not integer or is not defined
12775ffd83dbSDimitry Andric         // in the loop, skip it.
12785ffd83dbSDimitry Andric         Value *InVal = PN->getIncomingValue(i);
12795ffd83dbSDimitry Andric         if (!isa<Instruction>(InVal))
12805ffd83dbSDimitry Andric           continue;
12815ffd83dbSDimitry Andric 
12825ffd83dbSDimitry Andric         // If this pred is for a subloop, not L itself, skip it.
12835ffd83dbSDimitry Andric         if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
12845ffd83dbSDimitry Andric           continue; // The Block is in a subloop, skip it.
12855ffd83dbSDimitry Andric 
12865ffd83dbSDimitry Andric         // Check that InVal is defined in the loop.
12875ffd83dbSDimitry Andric         Instruction *Inst = cast<Instruction>(InVal);
12885ffd83dbSDimitry Andric         if (!L->contains(Inst))
12895ffd83dbSDimitry Andric           continue;
12905ffd83dbSDimitry Andric 
12915ffd83dbSDimitry Andric         // Okay, this instruction has a user outside of the current loop
12925ffd83dbSDimitry Andric         // and varies predictably *inside* the loop.  Evaluate the value it
12935ffd83dbSDimitry Andric         // contains when the loop exits, if possible.  We prefer to start with
12945ffd83dbSDimitry Andric         // expressions which are true for all exits (so as to maximize
12955ffd83dbSDimitry Andric         // expression reuse by the SCEVExpander), but resort to per-exit
12965ffd83dbSDimitry Andric         // evaluation if that fails.
12975ffd83dbSDimitry Andric         const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
12985ffd83dbSDimitry Andric         if (isa<SCEVCouldNotCompute>(ExitValue) ||
12995ffd83dbSDimitry Andric             !SE->isLoopInvariant(ExitValue, L) ||
13005ffd83dbSDimitry Andric             !isSafeToExpand(ExitValue, *SE)) {
13015ffd83dbSDimitry Andric           // TODO: This should probably be sunk into SCEV in some way; maybe a
13025ffd83dbSDimitry Andric           // getSCEVForExit(SCEV*, L, ExitingBB)?  It can be generalized for
13035ffd83dbSDimitry Andric           // most SCEV expressions and other recurrence types (e.g. shift
13045ffd83dbSDimitry Andric           // recurrences).  Is there existing code we can reuse?
13055ffd83dbSDimitry Andric           const SCEV *ExitCount = SE->getExitCount(L, PN->getIncomingBlock(i));
13065ffd83dbSDimitry Andric           if (isa<SCEVCouldNotCompute>(ExitCount))
13075ffd83dbSDimitry Andric             continue;
13085ffd83dbSDimitry Andric           if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Inst)))
13095ffd83dbSDimitry Andric             if (AddRec->getLoop() == L)
13105ffd83dbSDimitry Andric               ExitValue = AddRec->evaluateAtIteration(ExitCount, *SE);
13115ffd83dbSDimitry Andric           if (isa<SCEVCouldNotCompute>(ExitValue) ||
13125ffd83dbSDimitry Andric               !SE->isLoopInvariant(ExitValue, L) ||
13135ffd83dbSDimitry Andric               !isSafeToExpand(ExitValue, *SE))
13145ffd83dbSDimitry Andric             continue;
13155ffd83dbSDimitry Andric         }
13165ffd83dbSDimitry Andric 
13175ffd83dbSDimitry Andric         // Computing the value outside of the loop brings no benefit if it is
13185ffd83dbSDimitry Andric         // definitely used inside the loop in a way which can not be optimized
13195ffd83dbSDimitry Andric         // away. Avoid doing so unless we know we have a value which computes
13205ffd83dbSDimitry Andric         // the ExitValue already. TODO: This should be merged into SCEV
13215ffd83dbSDimitry Andric         // expander to leverage its knowledge of existing expressions.
13225ffd83dbSDimitry Andric         if (ReplaceExitValue != AlwaysRepl && !isa<SCEVConstant>(ExitValue) &&
13235ffd83dbSDimitry Andric             !isa<SCEVUnknown>(ExitValue) && hasHardUserWithinLoop(L, Inst))
13245ffd83dbSDimitry Andric           continue;
13255ffd83dbSDimitry Andric 
13265ffd83dbSDimitry Andric         // Check if expansions of this SCEV would count as being high cost.
13275ffd83dbSDimitry Andric         bool HighCost = Rewriter.isHighCostExpansion(
13285ffd83dbSDimitry Andric             ExitValue, L, SCEVCheapExpansionBudget, TTI, Inst);
13295ffd83dbSDimitry Andric 
13305ffd83dbSDimitry Andric         // Note that we must not perform expansions until after
13315ffd83dbSDimitry Andric         // we query *all* the costs, because if we perform temporary expansion
13325ffd83dbSDimitry Andric         // inbetween, one that we might not intend to keep, said expansion
13335ffd83dbSDimitry Andric         // *may* affect cost calculation of the the next SCEV's we'll query,
13345ffd83dbSDimitry Andric         // and next SCEV may errneously get smaller cost.
13355ffd83dbSDimitry Andric 
13365ffd83dbSDimitry Andric         // Collect all the candidate PHINodes to be rewritten.
13375ffd83dbSDimitry Andric         RewritePhiSet.emplace_back(PN, i, ExitValue, Inst, HighCost);
13385ffd83dbSDimitry Andric       }
13395ffd83dbSDimitry Andric     }
13405ffd83dbSDimitry Andric   }
13415ffd83dbSDimitry Andric 
13425ffd83dbSDimitry Andric   // Now that we've done preliminary filtering and billed all the SCEV's,
13435ffd83dbSDimitry Andric   // we can perform the last sanity check - the expansion must be valid.
13445ffd83dbSDimitry Andric   for (RewritePhi &Phi : RewritePhiSet) {
13455ffd83dbSDimitry Andric     Phi.Expansion = Rewriter.expandCodeFor(Phi.ExpansionSCEV, Phi.PN->getType(),
13465ffd83dbSDimitry Andric                                            Phi.ExpansionPoint);
13475ffd83dbSDimitry Andric 
13485ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: AfterLoopVal = "
13495ffd83dbSDimitry Andric                       << *(Phi.Expansion) << '\n'
13505ffd83dbSDimitry Andric                       << "  LoopVal = " << *(Phi.ExpansionPoint) << "\n");
13515ffd83dbSDimitry Andric 
13525ffd83dbSDimitry Andric     // FIXME: isValidRewrite() is a hack. it should be an assert, eventually.
13535ffd83dbSDimitry Andric     Phi.ValidRewrite = isValidRewrite(SE, Phi.ExpansionPoint, Phi.Expansion);
13545ffd83dbSDimitry Andric     if (!Phi.ValidRewrite) {
13555ffd83dbSDimitry Andric       DeadInsts.push_back(Phi.Expansion);
13565ffd83dbSDimitry Andric       continue;
13575ffd83dbSDimitry Andric     }
13585ffd83dbSDimitry Andric 
13595ffd83dbSDimitry Andric #ifndef NDEBUG
13605ffd83dbSDimitry Andric     // If we reuse an instruction from a loop which is neither L nor one of
13615ffd83dbSDimitry Andric     // its containing loops, we end up breaking LCSSA form for this loop by
13625ffd83dbSDimitry Andric     // creating a new use of its instruction.
13635ffd83dbSDimitry Andric     if (auto *ExitInsn = dyn_cast<Instruction>(Phi.Expansion))
13645ffd83dbSDimitry Andric       if (auto *EVL = LI->getLoopFor(ExitInsn->getParent()))
13655ffd83dbSDimitry Andric         if (EVL != L)
13665ffd83dbSDimitry Andric           assert(EVL->contains(L) && "LCSSA breach detected!");
13675ffd83dbSDimitry Andric #endif
13685ffd83dbSDimitry Andric   }
13695ffd83dbSDimitry Andric 
13705ffd83dbSDimitry Andric   // TODO: after isValidRewrite() is an assertion, evaluate whether
13715ffd83dbSDimitry Andric   // it is beneficial to change how we calculate high-cost:
13725ffd83dbSDimitry Andric   // if we have SCEV 'A' which we know we will expand, should we calculate
13735ffd83dbSDimitry Andric   // the cost of other SCEV's after expanding SCEV 'A',
13745ffd83dbSDimitry Andric   // thus potentially giving cost bonus to those other SCEV's?
13755ffd83dbSDimitry Andric 
13765ffd83dbSDimitry Andric   bool LoopCanBeDel = canLoopBeDeleted(L, RewritePhiSet);
13775ffd83dbSDimitry Andric   int NumReplaced = 0;
13785ffd83dbSDimitry Andric 
13795ffd83dbSDimitry Andric   // Transformation.
13805ffd83dbSDimitry Andric   for (const RewritePhi &Phi : RewritePhiSet) {
13815ffd83dbSDimitry Andric     if (!Phi.ValidRewrite)
13825ffd83dbSDimitry Andric       continue;
13835ffd83dbSDimitry Andric 
13845ffd83dbSDimitry Andric     PHINode *PN = Phi.PN;
13855ffd83dbSDimitry Andric     Value *ExitVal = Phi.Expansion;
13865ffd83dbSDimitry Andric 
13875ffd83dbSDimitry Andric     // Only do the rewrite when the ExitValue can be expanded cheaply.
13885ffd83dbSDimitry Andric     // If LoopCanBeDel is true, rewrite exit value aggressively.
13895ffd83dbSDimitry Andric     if (ReplaceExitValue == OnlyCheapRepl && !LoopCanBeDel && Phi.HighCost) {
13905ffd83dbSDimitry Andric       DeadInsts.push_back(ExitVal);
13915ffd83dbSDimitry Andric       continue;
13925ffd83dbSDimitry Andric     }
13935ffd83dbSDimitry Andric 
13945ffd83dbSDimitry Andric     NumReplaced++;
13955ffd83dbSDimitry Andric     Instruction *Inst = cast<Instruction>(PN->getIncomingValue(Phi.Ith));
13965ffd83dbSDimitry Andric     PN->setIncomingValue(Phi.Ith, ExitVal);
13975ffd83dbSDimitry Andric 
13985ffd83dbSDimitry Andric     // If this instruction is dead now, delete it. Don't do it now to avoid
13995ffd83dbSDimitry Andric     // invalidating iterators.
14005ffd83dbSDimitry Andric     if (isInstructionTriviallyDead(Inst, TLI))
14015ffd83dbSDimitry Andric       DeadInsts.push_back(Inst);
14025ffd83dbSDimitry Andric 
14035ffd83dbSDimitry Andric     // Replace PN with ExitVal if that is legal and does not break LCSSA.
14045ffd83dbSDimitry Andric     if (PN->getNumIncomingValues() == 1 &&
14055ffd83dbSDimitry Andric         LI->replacementPreservesLCSSAForm(PN, ExitVal)) {
14065ffd83dbSDimitry Andric       PN->replaceAllUsesWith(ExitVal);
14075ffd83dbSDimitry Andric       PN->eraseFromParent();
14085ffd83dbSDimitry Andric     }
14095ffd83dbSDimitry Andric   }
14105ffd83dbSDimitry Andric 
14115ffd83dbSDimitry Andric   // The insertion point instruction may have been deleted; clear it out
14125ffd83dbSDimitry Andric   // so that the rewriter doesn't trip over it later.
14135ffd83dbSDimitry Andric   Rewriter.clearInsertPoint();
14145ffd83dbSDimitry Andric   return NumReplaced;
14155ffd83dbSDimitry Andric }
14165ffd83dbSDimitry Andric 
14175ffd83dbSDimitry Andric /// Set weights for \p UnrolledLoop and \p RemainderLoop based on weights for
14185ffd83dbSDimitry Andric /// \p OrigLoop.
setProfileInfoAfterUnrolling(Loop * OrigLoop,Loop * UnrolledLoop,Loop * RemainderLoop,uint64_t UF)14195ffd83dbSDimitry Andric void llvm::setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop,
14205ffd83dbSDimitry Andric                                         Loop *RemainderLoop, uint64_t UF) {
14215ffd83dbSDimitry Andric   assert(UF > 0 && "Zero unrolled factor is not supported");
14225ffd83dbSDimitry Andric   assert(UnrolledLoop != RemainderLoop &&
14235ffd83dbSDimitry Andric          "Unrolled and Remainder loops are expected to distinct");
14245ffd83dbSDimitry Andric 
14255ffd83dbSDimitry Andric   // Get number of iterations in the original scalar loop.
14265ffd83dbSDimitry Andric   unsigned OrigLoopInvocationWeight = 0;
14275ffd83dbSDimitry Andric   Optional<unsigned> OrigAverageTripCount =
14285ffd83dbSDimitry Andric       getLoopEstimatedTripCount(OrigLoop, &OrigLoopInvocationWeight);
14295ffd83dbSDimitry Andric   if (!OrigAverageTripCount)
14305ffd83dbSDimitry Andric     return;
14315ffd83dbSDimitry Andric 
14325ffd83dbSDimitry Andric   // Calculate number of iterations in unrolled loop.
14335ffd83dbSDimitry Andric   unsigned UnrolledAverageTripCount = *OrigAverageTripCount / UF;
14345ffd83dbSDimitry Andric   // Calculate number of iterations for remainder loop.
14355ffd83dbSDimitry Andric   unsigned RemainderAverageTripCount = *OrigAverageTripCount % UF;
14365ffd83dbSDimitry Andric 
14375ffd83dbSDimitry Andric   setLoopEstimatedTripCount(UnrolledLoop, UnrolledAverageTripCount,
14385ffd83dbSDimitry Andric                             OrigLoopInvocationWeight);
14395ffd83dbSDimitry Andric   setLoopEstimatedTripCount(RemainderLoop, RemainderAverageTripCount,
14405ffd83dbSDimitry Andric                             OrigLoopInvocationWeight);
14415ffd83dbSDimitry Andric }
14425ffd83dbSDimitry Andric 
14435ffd83dbSDimitry Andric /// Utility that implements appending of loops onto a worklist.
14445ffd83dbSDimitry Andric /// Loops are added in preorder (analogous for reverse postorder for trees),
14455ffd83dbSDimitry Andric /// and the worklist is processed LIFO.
14465ffd83dbSDimitry Andric template <typename RangeT>
appendReversedLoopsToWorklist(RangeT && Loops,SmallPriorityWorklist<Loop *,4> & Worklist)14475ffd83dbSDimitry Andric void llvm::appendReversedLoopsToWorklist(
14485ffd83dbSDimitry Andric     RangeT &&Loops, SmallPriorityWorklist<Loop *, 4> &Worklist) {
14495ffd83dbSDimitry Andric   // We use an internal worklist to build up the preorder traversal without
14505ffd83dbSDimitry Andric   // recursion.
14515ffd83dbSDimitry Andric   SmallVector<Loop *, 4> PreOrderLoops, PreOrderWorklist;
14525ffd83dbSDimitry Andric 
14535ffd83dbSDimitry Andric   // We walk the initial sequence of loops in reverse because we generally want
14545ffd83dbSDimitry Andric   // to visit defs before uses and the worklist is LIFO.
14555ffd83dbSDimitry Andric   for (Loop *RootL : Loops) {
14565ffd83dbSDimitry Andric     assert(PreOrderLoops.empty() && "Must start with an empty preorder walk.");
14575ffd83dbSDimitry Andric     assert(PreOrderWorklist.empty() &&
14585ffd83dbSDimitry Andric            "Must start with an empty preorder walk worklist.");
14595ffd83dbSDimitry Andric     PreOrderWorklist.push_back(RootL);
14605ffd83dbSDimitry Andric     do {
14615ffd83dbSDimitry Andric       Loop *L = PreOrderWorklist.pop_back_val();
14625ffd83dbSDimitry Andric       PreOrderWorklist.append(L->begin(), L->end());
14635ffd83dbSDimitry Andric       PreOrderLoops.push_back(L);
14645ffd83dbSDimitry Andric     } while (!PreOrderWorklist.empty());
14655ffd83dbSDimitry Andric 
14665ffd83dbSDimitry Andric     Worklist.insert(std::move(PreOrderLoops));
14675ffd83dbSDimitry Andric     PreOrderLoops.clear();
14685ffd83dbSDimitry Andric   }
14695ffd83dbSDimitry Andric }
14705ffd83dbSDimitry Andric 
14715ffd83dbSDimitry Andric template <typename RangeT>
appendLoopsToWorklist(RangeT && Loops,SmallPriorityWorklist<Loop *,4> & Worklist)14725ffd83dbSDimitry Andric void llvm::appendLoopsToWorklist(RangeT &&Loops,
14735ffd83dbSDimitry Andric                                  SmallPriorityWorklist<Loop *, 4> &Worklist) {
14745ffd83dbSDimitry Andric   appendReversedLoopsToWorklist(reverse(Loops), Worklist);
14755ffd83dbSDimitry Andric }
14765ffd83dbSDimitry Andric 
14775ffd83dbSDimitry Andric template void llvm::appendLoopsToWorklist<ArrayRef<Loop *> &>(
14785ffd83dbSDimitry Andric     ArrayRef<Loop *> &Loops, SmallPriorityWorklist<Loop *, 4> &Worklist);
14795ffd83dbSDimitry Andric 
14805ffd83dbSDimitry Andric template void
14815ffd83dbSDimitry Andric llvm::appendLoopsToWorklist<Loop &>(Loop &L,
14825ffd83dbSDimitry Andric                                     SmallPriorityWorklist<Loop *, 4> &Worklist);
14835ffd83dbSDimitry Andric 
appendLoopsToWorklist(LoopInfo & LI,SmallPriorityWorklist<Loop *,4> & Worklist)14845ffd83dbSDimitry Andric void llvm::appendLoopsToWorklist(LoopInfo &LI,
14855ffd83dbSDimitry Andric                                  SmallPriorityWorklist<Loop *, 4> &Worklist) {
14865ffd83dbSDimitry Andric   appendReversedLoopsToWorklist(LI, Worklist);
14875ffd83dbSDimitry Andric }
14885ffd83dbSDimitry Andric 
cloneLoop(Loop * L,Loop * PL,ValueToValueMapTy & VM,LoopInfo * LI,LPPassManager * LPM)14895ffd83dbSDimitry Andric Loop *llvm::cloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
14905ffd83dbSDimitry Andric                       LoopInfo *LI, LPPassManager *LPM) {
14915ffd83dbSDimitry Andric   Loop &New = *LI->AllocateLoop();
14925ffd83dbSDimitry Andric   if (PL)
14935ffd83dbSDimitry Andric     PL->addChildLoop(&New);
14945ffd83dbSDimitry Andric   else
14955ffd83dbSDimitry Andric     LI->addTopLevelLoop(&New);
14965ffd83dbSDimitry Andric 
14975ffd83dbSDimitry Andric   if (LPM)
14985ffd83dbSDimitry Andric     LPM->addLoop(New);
14995ffd83dbSDimitry Andric 
15005ffd83dbSDimitry Andric   // Add all of the blocks in L to the new loop.
15015ffd83dbSDimitry Andric   for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
15025ffd83dbSDimitry Andric        I != E; ++I)
15035ffd83dbSDimitry Andric     if (LI->getLoopFor(*I) == L)
15045ffd83dbSDimitry Andric       New.addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI);
15055ffd83dbSDimitry Andric 
15065ffd83dbSDimitry Andric   // Add all of the subloops to the new loop.
15075ffd83dbSDimitry Andric   for (Loop *I : *L)
15085ffd83dbSDimitry Andric     cloneLoop(I, &New, VM, LI, LPM);
15095ffd83dbSDimitry Andric 
15105ffd83dbSDimitry Andric   return &New;
15115ffd83dbSDimitry Andric }
15125ffd83dbSDimitry Andric 
15135ffd83dbSDimitry Andric /// IR Values for the lower and upper bounds of a pointer evolution.  We
15145ffd83dbSDimitry Andric /// need to use value-handles because SCEV expansion can invalidate previously
15155ffd83dbSDimitry Andric /// expanded values.  Thus expansion of a pointer can invalidate the bounds for
15165ffd83dbSDimitry Andric /// a previous one.
15175ffd83dbSDimitry Andric struct PointerBounds {
15185ffd83dbSDimitry Andric   TrackingVH<Value> Start;
15195ffd83dbSDimitry Andric   TrackingVH<Value> End;
15205ffd83dbSDimitry Andric };
15215ffd83dbSDimitry Andric 
15225ffd83dbSDimitry Andric /// Expand code for the lower and upper bound of the pointer group \p CG
15235ffd83dbSDimitry Andric /// in \p TheLoop.  \return the values for the bounds.
expandBounds(const RuntimeCheckingPtrGroup * CG,Loop * TheLoop,Instruction * Loc,SCEVExpander & Exp)15245ffd83dbSDimitry Andric static PointerBounds expandBounds(const RuntimeCheckingPtrGroup *CG,
15255ffd83dbSDimitry Andric                                   Loop *TheLoop, Instruction *Loc,
1526*5f7ddb14SDimitry Andric                                   SCEVExpander &Exp) {
15275ffd83dbSDimitry Andric   LLVMContext &Ctx = Loc->getContext();
1528*5f7ddb14SDimitry Andric   Type *PtrArithTy = Type::getInt8PtrTy(Ctx, CG->AddressSpace);
15295ffd83dbSDimitry Andric 
15305ffd83dbSDimitry Andric   Value *Start = nullptr, *End = nullptr;
15315ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "LAA: Adding RT check for range:\n");
15325ffd83dbSDimitry Andric   Start = Exp.expandCodeFor(CG->Low, PtrArithTy, Loc);
15335ffd83dbSDimitry Andric   End = Exp.expandCodeFor(CG->High, PtrArithTy, Loc);
1534*5f7ddb14SDimitry Andric   LLVM_DEBUG(dbgs() << "Start: " << *CG->Low << " End: " << *CG->High << "\n");
15355ffd83dbSDimitry Andric   return {Start, End};
15365ffd83dbSDimitry Andric }
15375ffd83dbSDimitry Andric 
15385ffd83dbSDimitry Andric /// Turns a collection of checks into a collection of expanded upper and
15395ffd83dbSDimitry Andric /// lower bounds for both pointers in the check.
15405ffd83dbSDimitry Andric static SmallVector<std::pair<PointerBounds, PointerBounds>, 4>
expandBounds(const SmallVectorImpl<RuntimePointerCheck> & PointerChecks,Loop * L,Instruction * Loc,SCEVExpander & Exp)15415ffd83dbSDimitry Andric expandBounds(const SmallVectorImpl<RuntimePointerCheck> &PointerChecks, Loop *L,
1542*5f7ddb14SDimitry Andric              Instruction *Loc, SCEVExpander &Exp) {
15435ffd83dbSDimitry Andric   SmallVector<std::pair<PointerBounds, PointerBounds>, 4> ChecksWithBounds;
15445ffd83dbSDimitry Andric 
15455ffd83dbSDimitry Andric   // Here we're relying on the SCEV Expander's cache to only emit code for the
15465ffd83dbSDimitry Andric   // same bounds once.
15475ffd83dbSDimitry Andric   transform(PointerChecks, std::back_inserter(ChecksWithBounds),
15485ffd83dbSDimitry Andric             [&](const RuntimePointerCheck &Check) {
1549*5f7ddb14SDimitry Andric               PointerBounds First = expandBounds(Check.first, L, Loc, Exp),
1550*5f7ddb14SDimitry Andric                             Second = expandBounds(Check.second, L, Loc, Exp);
15515ffd83dbSDimitry Andric               return std::make_pair(First, Second);
15525ffd83dbSDimitry Andric             });
15535ffd83dbSDimitry Andric 
15545ffd83dbSDimitry Andric   return ChecksWithBounds;
15555ffd83dbSDimitry Andric }
15565ffd83dbSDimitry Andric 
addRuntimeChecks(Instruction * Loc,Loop * TheLoop,const SmallVectorImpl<RuntimePointerCheck> & PointerChecks,SCEVExpander & Exp)15575ffd83dbSDimitry Andric std::pair<Instruction *, Instruction *> llvm::addRuntimeChecks(
15585ffd83dbSDimitry Andric     Instruction *Loc, Loop *TheLoop,
15595ffd83dbSDimitry Andric     const SmallVectorImpl<RuntimePointerCheck> &PointerChecks,
1560*5f7ddb14SDimitry Andric     SCEVExpander &Exp) {
15615ffd83dbSDimitry Andric   // TODO: Move noalias annotation code from LoopVersioning here and share with LV if possible.
15625ffd83dbSDimitry Andric   // TODO: Pass  RtPtrChecking instead of PointerChecks and SE separately, if possible
1563*5f7ddb14SDimitry Andric   auto ExpandedChecks = expandBounds(PointerChecks, TheLoop, Loc, Exp);
15645ffd83dbSDimitry Andric 
15655ffd83dbSDimitry Andric   LLVMContext &Ctx = Loc->getContext();
15665ffd83dbSDimitry Andric   Instruction *FirstInst = nullptr;
15675ffd83dbSDimitry Andric   IRBuilder<> ChkBuilder(Loc);
15685ffd83dbSDimitry Andric   // Our instructions might fold to a constant.
15695ffd83dbSDimitry Andric   Value *MemoryRuntimeCheck = nullptr;
15705ffd83dbSDimitry Andric 
15715ffd83dbSDimitry Andric   // FIXME: this helper is currently a duplicate of the one in
15725ffd83dbSDimitry Andric   // LoopVectorize.cpp.
15735ffd83dbSDimitry Andric   auto GetFirstInst = [](Instruction *FirstInst, Value *V,
15745ffd83dbSDimitry Andric                          Instruction *Loc) -> Instruction * {
15755ffd83dbSDimitry Andric     if (FirstInst)
15765ffd83dbSDimitry Andric       return FirstInst;
15775ffd83dbSDimitry Andric     if (Instruction *I = dyn_cast<Instruction>(V))
15785ffd83dbSDimitry Andric       return I->getParent() == Loc->getParent() ? I : nullptr;
15795ffd83dbSDimitry Andric     return nullptr;
15805ffd83dbSDimitry Andric   };
15815ffd83dbSDimitry Andric 
15825ffd83dbSDimitry Andric   for (const auto &Check : ExpandedChecks) {
15835ffd83dbSDimitry Andric     const PointerBounds &A = Check.first, &B = Check.second;
15845ffd83dbSDimitry Andric     // Check if two pointers (A and B) conflict where conflict is computed as:
15855ffd83dbSDimitry Andric     // start(A) <= end(B) && start(B) <= end(A)
15865ffd83dbSDimitry Andric     unsigned AS0 = A.Start->getType()->getPointerAddressSpace();
15875ffd83dbSDimitry Andric     unsigned AS1 = B.Start->getType()->getPointerAddressSpace();
15885ffd83dbSDimitry Andric 
15895ffd83dbSDimitry Andric     assert((AS0 == B.End->getType()->getPointerAddressSpace()) &&
15905ffd83dbSDimitry Andric            (AS1 == A.End->getType()->getPointerAddressSpace()) &&
15915ffd83dbSDimitry Andric            "Trying to bounds check pointers with different address spaces");
15925ffd83dbSDimitry Andric 
15935ffd83dbSDimitry Andric     Type *PtrArithTy0 = Type::getInt8PtrTy(Ctx, AS0);
15945ffd83dbSDimitry Andric     Type *PtrArithTy1 = Type::getInt8PtrTy(Ctx, AS1);
15955ffd83dbSDimitry Andric 
15965ffd83dbSDimitry Andric     Value *Start0 = ChkBuilder.CreateBitCast(A.Start, PtrArithTy0, "bc");
15975ffd83dbSDimitry Andric     Value *Start1 = ChkBuilder.CreateBitCast(B.Start, PtrArithTy1, "bc");
15985ffd83dbSDimitry Andric     Value *End0 = ChkBuilder.CreateBitCast(A.End, PtrArithTy1, "bc");
15995ffd83dbSDimitry Andric     Value *End1 = ChkBuilder.CreateBitCast(B.End, PtrArithTy0, "bc");
16005ffd83dbSDimitry Andric 
16015ffd83dbSDimitry Andric     // [A|B].Start points to the first accessed byte under base [A|B].
16025ffd83dbSDimitry Andric     // [A|B].End points to the last accessed byte, plus one.
16035ffd83dbSDimitry Andric     // There is no conflict when the intervals are disjoint:
16045ffd83dbSDimitry Andric     // NoConflict = (B.Start >= A.End) || (A.Start >= B.End)
16055ffd83dbSDimitry Andric     //
16065ffd83dbSDimitry Andric     // bound0 = (B.Start < A.End)
16075ffd83dbSDimitry Andric     // bound1 = (A.Start < B.End)
16085ffd83dbSDimitry Andric     //  IsConflict = bound0 & bound1
16095ffd83dbSDimitry Andric     Value *Cmp0 = ChkBuilder.CreateICmpULT(Start0, End1, "bound0");
16105ffd83dbSDimitry Andric     FirstInst = GetFirstInst(FirstInst, Cmp0, Loc);
16115ffd83dbSDimitry Andric     Value *Cmp1 = ChkBuilder.CreateICmpULT(Start1, End0, "bound1");
16125ffd83dbSDimitry Andric     FirstInst = GetFirstInst(FirstInst, Cmp1, Loc);
16135ffd83dbSDimitry Andric     Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1, "found.conflict");
16145ffd83dbSDimitry Andric     FirstInst = GetFirstInst(FirstInst, IsConflict, Loc);
16155ffd83dbSDimitry Andric     if (MemoryRuntimeCheck) {
16165ffd83dbSDimitry Andric       IsConflict =
16175ffd83dbSDimitry Andric           ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict, "conflict.rdx");
16185ffd83dbSDimitry Andric       FirstInst = GetFirstInst(FirstInst, IsConflict, Loc);
16195ffd83dbSDimitry Andric     }
16205ffd83dbSDimitry Andric     MemoryRuntimeCheck = IsConflict;
16215ffd83dbSDimitry Andric   }
16225ffd83dbSDimitry Andric 
16235ffd83dbSDimitry Andric   if (!MemoryRuntimeCheck)
16245ffd83dbSDimitry Andric     return std::make_pair(nullptr, nullptr);
16255ffd83dbSDimitry Andric 
16265ffd83dbSDimitry Andric   // We have to do this trickery because the IRBuilder might fold the check to a
16275ffd83dbSDimitry Andric   // constant expression in which case there is no Instruction anchored in a
16285ffd83dbSDimitry Andric   // the block.
16295ffd83dbSDimitry Andric   Instruction *Check =
16305ffd83dbSDimitry Andric       BinaryOperator::CreateAnd(MemoryRuntimeCheck, ConstantInt::getTrue(Ctx));
16315ffd83dbSDimitry Andric   ChkBuilder.Insert(Check, "memcheck.conflict");
16325ffd83dbSDimitry Andric   FirstInst = GetFirstInst(FirstInst, Check, Loc);
16335ffd83dbSDimitry Andric   return std::make_pair(FirstInst, Check);
16345ffd83dbSDimitry Andric }
1635*5f7ddb14SDimitry Andric 
hasPartialIVCondition(Loop & L,unsigned MSSAThreshold,MemorySSA & MSSA,AAResults & AA)1636*5f7ddb14SDimitry Andric Optional<IVConditionInfo> llvm::hasPartialIVCondition(Loop &L,
1637*5f7ddb14SDimitry Andric                                                       unsigned MSSAThreshold,
1638*5f7ddb14SDimitry Andric                                                       MemorySSA &MSSA,
1639*5f7ddb14SDimitry Andric                                                       AAResults &AA) {
1640*5f7ddb14SDimitry Andric   auto *TI = dyn_cast<BranchInst>(L.getHeader()->getTerminator());
1641*5f7ddb14SDimitry Andric   if (!TI || !TI->isConditional())
1642*5f7ddb14SDimitry Andric     return {};
1643*5f7ddb14SDimitry Andric 
1644*5f7ddb14SDimitry Andric   auto *CondI = dyn_cast<CmpInst>(TI->getCondition());
1645*5f7ddb14SDimitry Andric   // The case with the condition outside the loop should already be handled
1646*5f7ddb14SDimitry Andric   // earlier.
1647*5f7ddb14SDimitry Andric   if (!CondI || !L.contains(CondI))
1648*5f7ddb14SDimitry Andric     return {};
1649*5f7ddb14SDimitry Andric 
1650*5f7ddb14SDimitry Andric   SmallVector<Instruction *> InstToDuplicate;
1651*5f7ddb14SDimitry Andric   InstToDuplicate.push_back(CondI);
1652*5f7ddb14SDimitry Andric 
1653*5f7ddb14SDimitry Andric   SmallVector<Value *, 4> WorkList;
1654*5f7ddb14SDimitry Andric   WorkList.append(CondI->op_begin(), CondI->op_end());
1655*5f7ddb14SDimitry Andric 
1656*5f7ddb14SDimitry Andric   SmallVector<MemoryAccess *, 4> AccessesToCheck;
1657*5f7ddb14SDimitry Andric   SmallVector<MemoryLocation, 4> AccessedLocs;
1658*5f7ddb14SDimitry Andric   while (!WorkList.empty()) {
1659*5f7ddb14SDimitry Andric     Instruction *I = dyn_cast<Instruction>(WorkList.pop_back_val());
1660*5f7ddb14SDimitry Andric     if (!I || !L.contains(I))
1661*5f7ddb14SDimitry Andric       continue;
1662*5f7ddb14SDimitry Andric 
1663*5f7ddb14SDimitry Andric     // TODO: support additional instructions.
1664*5f7ddb14SDimitry Andric     if (!isa<LoadInst>(I) && !isa<GetElementPtrInst>(I))
1665*5f7ddb14SDimitry Andric       return {};
1666*5f7ddb14SDimitry Andric 
1667*5f7ddb14SDimitry Andric     // Do not duplicate volatile and atomic loads.
1668*5f7ddb14SDimitry Andric     if (auto *LI = dyn_cast<LoadInst>(I))
1669*5f7ddb14SDimitry Andric       if (LI->isVolatile() || LI->isAtomic())
1670*5f7ddb14SDimitry Andric         return {};
1671*5f7ddb14SDimitry Andric 
1672*5f7ddb14SDimitry Andric     InstToDuplicate.push_back(I);
1673*5f7ddb14SDimitry Andric     if (MemoryAccess *MA = MSSA.getMemoryAccess(I)) {
1674*5f7ddb14SDimitry Andric       if (auto *MemUse = dyn_cast_or_null<MemoryUse>(MA)) {
1675*5f7ddb14SDimitry Andric         // Queue the defining access to check for alias checks.
1676*5f7ddb14SDimitry Andric         AccessesToCheck.push_back(MemUse->getDefiningAccess());
1677*5f7ddb14SDimitry Andric         AccessedLocs.push_back(MemoryLocation::get(I));
1678*5f7ddb14SDimitry Andric       } else {
1679*5f7ddb14SDimitry Andric         // MemoryDefs may clobber the location or may be atomic memory
1680*5f7ddb14SDimitry Andric         // operations. Bail out.
1681*5f7ddb14SDimitry Andric         return {};
1682*5f7ddb14SDimitry Andric       }
1683*5f7ddb14SDimitry Andric     }
1684*5f7ddb14SDimitry Andric     WorkList.append(I->op_begin(), I->op_end());
1685*5f7ddb14SDimitry Andric   }
1686*5f7ddb14SDimitry Andric 
1687*5f7ddb14SDimitry Andric   if (InstToDuplicate.empty())
1688*5f7ddb14SDimitry Andric     return {};
1689*5f7ddb14SDimitry Andric 
1690*5f7ddb14SDimitry Andric   SmallVector<BasicBlock *, 4> ExitingBlocks;
1691*5f7ddb14SDimitry Andric   L.getExitingBlocks(ExitingBlocks);
1692*5f7ddb14SDimitry Andric   auto HasNoClobbersOnPath =
1693*5f7ddb14SDimitry Andric       [&L, &AA, &AccessedLocs, &ExitingBlocks, &InstToDuplicate,
1694*5f7ddb14SDimitry Andric        MSSAThreshold](BasicBlock *Succ, BasicBlock *Header,
1695*5f7ddb14SDimitry Andric                       SmallVector<MemoryAccess *, 4> AccessesToCheck)
1696*5f7ddb14SDimitry Andric       -> Optional<IVConditionInfo> {
1697*5f7ddb14SDimitry Andric     IVConditionInfo Info;
1698*5f7ddb14SDimitry Andric     // First, collect all blocks in the loop that are on a patch from Succ
1699*5f7ddb14SDimitry Andric     // to the header.
1700*5f7ddb14SDimitry Andric     SmallVector<BasicBlock *, 4> WorkList;
1701*5f7ddb14SDimitry Andric     WorkList.push_back(Succ);
1702*5f7ddb14SDimitry Andric     WorkList.push_back(Header);
1703*5f7ddb14SDimitry Andric     SmallPtrSet<BasicBlock *, 4> Seen;
1704*5f7ddb14SDimitry Andric     Seen.insert(Header);
1705*5f7ddb14SDimitry Andric     Info.PathIsNoop &=
1706*5f7ddb14SDimitry Andric         all_of(*Header, [](Instruction &I) { return !I.mayHaveSideEffects(); });
1707*5f7ddb14SDimitry Andric 
1708*5f7ddb14SDimitry Andric     while (!WorkList.empty()) {
1709*5f7ddb14SDimitry Andric       BasicBlock *Current = WorkList.pop_back_val();
1710*5f7ddb14SDimitry Andric       if (!L.contains(Current))
1711*5f7ddb14SDimitry Andric         continue;
1712*5f7ddb14SDimitry Andric       const auto &SeenIns = Seen.insert(Current);
1713*5f7ddb14SDimitry Andric       if (!SeenIns.second)
1714*5f7ddb14SDimitry Andric         continue;
1715*5f7ddb14SDimitry Andric 
1716*5f7ddb14SDimitry Andric       Info.PathIsNoop &= all_of(
1717*5f7ddb14SDimitry Andric           *Current, [](Instruction &I) { return !I.mayHaveSideEffects(); });
1718*5f7ddb14SDimitry Andric       WorkList.append(succ_begin(Current), succ_end(Current));
1719*5f7ddb14SDimitry Andric     }
1720*5f7ddb14SDimitry Andric 
1721*5f7ddb14SDimitry Andric     // Require at least 2 blocks on a path through the loop. This skips
1722*5f7ddb14SDimitry Andric     // paths that directly exit the loop.
1723*5f7ddb14SDimitry Andric     if (Seen.size() < 2)
1724*5f7ddb14SDimitry Andric       return {};
1725*5f7ddb14SDimitry Andric 
1726*5f7ddb14SDimitry Andric     // Next, check if there are any MemoryDefs that are on the path through
1727*5f7ddb14SDimitry Andric     // the loop (in the Seen set) and they may-alias any of the locations in
1728*5f7ddb14SDimitry Andric     // AccessedLocs. If that is the case, they may modify the condition and
1729*5f7ddb14SDimitry Andric     // partial unswitching is not possible.
1730*5f7ddb14SDimitry Andric     SmallPtrSet<MemoryAccess *, 4> SeenAccesses;
1731*5f7ddb14SDimitry Andric     while (!AccessesToCheck.empty()) {
1732*5f7ddb14SDimitry Andric       MemoryAccess *Current = AccessesToCheck.pop_back_val();
1733*5f7ddb14SDimitry Andric       auto SeenI = SeenAccesses.insert(Current);
1734*5f7ddb14SDimitry Andric       if (!SeenI.second || !Seen.contains(Current->getBlock()))
1735*5f7ddb14SDimitry Andric         continue;
1736*5f7ddb14SDimitry Andric 
1737*5f7ddb14SDimitry Andric       // Bail out if exceeded the threshold.
1738*5f7ddb14SDimitry Andric       if (SeenAccesses.size() >= MSSAThreshold)
1739*5f7ddb14SDimitry Andric         return {};
1740*5f7ddb14SDimitry Andric 
1741*5f7ddb14SDimitry Andric       // MemoryUse are read-only accesses.
1742*5f7ddb14SDimitry Andric       if (isa<MemoryUse>(Current))
1743*5f7ddb14SDimitry Andric         continue;
1744*5f7ddb14SDimitry Andric 
1745*5f7ddb14SDimitry Andric       // For a MemoryDef, check if is aliases any of the location feeding
1746*5f7ddb14SDimitry Andric       // the original condition.
1747*5f7ddb14SDimitry Andric       if (auto *CurrentDef = dyn_cast<MemoryDef>(Current)) {
1748*5f7ddb14SDimitry Andric         if (any_of(AccessedLocs, [&AA, CurrentDef](MemoryLocation &Loc) {
1749*5f7ddb14SDimitry Andric               return isModSet(
1750*5f7ddb14SDimitry Andric                   AA.getModRefInfo(CurrentDef->getMemoryInst(), Loc));
1751*5f7ddb14SDimitry Andric             }))
1752*5f7ddb14SDimitry Andric           return {};
1753*5f7ddb14SDimitry Andric       }
1754*5f7ddb14SDimitry Andric 
1755*5f7ddb14SDimitry Andric       for (Use &U : Current->uses())
1756*5f7ddb14SDimitry Andric         AccessesToCheck.push_back(cast<MemoryAccess>(U.getUser()));
1757*5f7ddb14SDimitry Andric     }
1758*5f7ddb14SDimitry Andric 
1759*5f7ddb14SDimitry Andric     // We could also allow loops with known trip counts without mustprogress,
1760*5f7ddb14SDimitry Andric     // but ScalarEvolution may not be available.
1761*5f7ddb14SDimitry Andric     Info.PathIsNoop &= isMustProgress(&L);
1762*5f7ddb14SDimitry Andric 
1763*5f7ddb14SDimitry Andric     // If the path is considered a no-op so far, check if it reaches a
1764*5f7ddb14SDimitry Andric     // single exit block without any phis. This ensures no values from the
1765*5f7ddb14SDimitry Andric     // loop are used outside of the loop.
1766*5f7ddb14SDimitry Andric     if (Info.PathIsNoop) {
1767*5f7ddb14SDimitry Andric       for (auto *Exiting : ExitingBlocks) {
1768*5f7ddb14SDimitry Andric         if (!Seen.contains(Exiting))
1769*5f7ddb14SDimitry Andric           continue;
1770*5f7ddb14SDimitry Andric         for (auto *Succ : successors(Exiting)) {
1771*5f7ddb14SDimitry Andric           if (L.contains(Succ))
1772*5f7ddb14SDimitry Andric             continue;
1773*5f7ddb14SDimitry Andric 
1774*5f7ddb14SDimitry Andric           Info.PathIsNoop &= llvm::empty(Succ->phis()) &&
1775*5f7ddb14SDimitry Andric                              (!Info.ExitForPath || Info.ExitForPath == Succ);
1776*5f7ddb14SDimitry Andric           if (!Info.PathIsNoop)
1777*5f7ddb14SDimitry Andric             break;
1778*5f7ddb14SDimitry Andric           assert((!Info.ExitForPath || Info.ExitForPath == Succ) &&
1779*5f7ddb14SDimitry Andric                  "cannot have multiple exit blocks");
1780*5f7ddb14SDimitry Andric           Info.ExitForPath = Succ;
1781*5f7ddb14SDimitry Andric         }
1782*5f7ddb14SDimitry Andric       }
1783*5f7ddb14SDimitry Andric     }
1784*5f7ddb14SDimitry Andric     if (!Info.ExitForPath)
1785*5f7ddb14SDimitry Andric       Info.PathIsNoop = false;
1786*5f7ddb14SDimitry Andric 
1787*5f7ddb14SDimitry Andric     Info.InstToDuplicate = InstToDuplicate;
1788*5f7ddb14SDimitry Andric     return Info;
1789*5f7ddb14SDimitry Andric   };
1790*5f7ddb14SDimitry Andric 
1791*5f7ddb14SDimitry Andric   // If we branch to the same successor, partial unswitching will not be
1792*5f7ddb14SDimitry Andric   // beneficial.
1793*5f7ddb14SDimitry Andric   if (TI->getSuccessor(0) == TI->getSuccessor(1))
1794*5f7ddb14SDimitry Andric     return {};
1795*5f7ddb14SDimitry Andric 
1796*5f7ddb14SDimitry Andric   if (auto Info = HasNoClobbersOnPath(TI->getSuccessor(0), L.getHeader(),
1797*5f7ddb14SDimitry Andric                                       AccessesToCheck)) {
1798*5f7ddb14SDimitry Andric     Info->KnownValue = ConstantInt::getTrue(TI->getContext());
1799*5f7ddb14SDimitry Andric     return Info;
1800*5f7ddb14SDimitry Andric   }
1801*5f7ddb14SDimitry Andric   if (auto Info = HasNoClobbersOnPath(TI->getSuccessor(1), L.getHeader(),
1802*5f7ddb14SDimitry Andric                                       AccessesToCheck)) {
1803*5f7ddb14SDimitry Andric     Info->KnownValue = ConstantInt::getFalse(TI->getContext());
1804*5f7ddb14SDimitry Andric     return Info;
1805*5f7ddb14SDimitry Andric   }
1806*5f7ddb14SDimitry Andric 
1807*5f7ddb14SDimitry Andric   return {};
1808*5f7ddb14SDimitry Andric }
1809