176aa662cSKarthik Bhat //===-- LoopUtils.cpp - Loop Utility functions -------------------------===//
276aa662cSKarthik Bhat //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
676aa662cSKarthik Bhat //
776aa662cSKarthik Bhat //===----------------------------------------------------------------------===//
876aa662cSKarthik Bhat //
976aa662cSKarthik Bhat // This file defines common loop utility functions.
1076aa662cSKarthik Bhat //
1176aa662cSKarthik Bhat //===----------------------------------------------------------------------===//
1276aa662cSKarthik Bhat 
132f2bd8caSAdam Nemet #include "llvm/Transforms/Utils/LoopUtils.h"
144a000883SChandler Carruth #include "llvm/ADT/ScopeExit.h"
1531088a9dSChandler Carruth #include "llvm/Analysis/AliasAnalysis.h"
1631088a9dSChandler Carruth #include "llvm/Analysis/BasicAliasAnalysis.h"
175f436fc5SRichard Trieu #include "llvm/Analysis/DomTreeUpdater.h"
1831088a9dSChandler Carruth #include "llvm/Analysis/GlobalsModRef.h"
19a21d5f1eSPhilip Reames #include "llvm/Analysis/InstructionSimplify.h"
202f2bd8caSAdam Nemet #include "llvm/Analysis/LoopInfo.h"
21c3ccf5d7SIgor Laevsky #include "llvm/Analysis/LoopPass.h"
226da79ce1SAlina Sbirlea #include "llvm/Analysis/MemorySSA.h"
2397468e92SAlina Sbirlea #include "llvm/Analysis/MemorySSAUpdater.h"
2423aed5efSPhilip Reames #include "llvm/Analysis/MustExecute.h"
2545d4cb9aSWeiming Zhao #include "llvm/Analysis/ScalarEvolution.h"
262f2bd8caSAdam Nemet #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2793175a5cSSjoerd Meijer #include "llvm/Analysis/ScalarEvolutionExpander.h"
2845d4cb9aSWeiming Zhao #include "llvm/Analysis/ScalarEvolutionExpressions.h"
296bda14b3SChandler Carruth #include "llvm/Analysis/TargetTransformInfo.h"
30a097bc69SChad Rosier #include "llvm/Analysis/ValueTracking.h"
31744c3c32SDavide Italiano #include "llvm/IR/DIBuilder.h"
3231088a9dSChandler Carruth #include "llvm/IR/Dominators.h"
3376aa662cSKarthik Bhat #include "llvm/IR/Instructions.h"
34744c3c32SDavide Italiano #include "llvm/IR/IntrinsicInst.h"
35af7e1588SEvgeniy Brevnov #include "llvm/IR/MDBuilder.h"
3645d4cb9aSWeiming Zhao #include "llvm/IR/Module.h"
3776aa662cSKarthik Bhat #include "llvm/IR/PatternMatch.h"
3876aa662cSKarthik Bhat #include "llvm/IR/ValueHandle.h"
3905da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
4031088a9dSChandler Carruth #include "llvm/Pass.h"
4176aa662cSKarthik Bhat #include "llvm/Support/Debug.h"
42a097bc69SChad Rosier #include "llvm/Support/KnownBits.h"
434a000883SChandler Carruth #include "llvm/Transforms/Utils/BasicBlockUtils.h"
4493175a5cSSjoerd Meijer #include "llvm/Transforms/Utils/Local.h"
4576aa662cSKarthik Bhat 
4676aa662cSKarthik Bhat using namespace llvm;
4776aa662cSKarthik Bhat using namespace llvm::PatternMatch;
4876aa662cSKarthik Bhat 
4976aa662cSKarthik Bhat #define DEBUG_TYPE "loop-utils"
5076aa662cSKarthik Bhat 
5172448525SMichael Kruse static const char *LLVMLoopDisableNonforced = "llvm.loop.disable_nonforced";
524f64f1baSTim Corringham static const char *LLVMLoopDisableLICM = "llvm.licm.disable";
5372448525SMichael Kruse 
544a000883SChandler Carruth bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
5597468e92SAlina Sbirlea                                    MemorySSAUpdater *MSSAU,
564a000883SChandler Carruth                                    bool PreserveLCSSA) {
574a000883SChandler Carruth   bool Changed = false;
584a000883SChandler Carruth 
594a000883SChandler Carruth   // We re-use a vector for the in-loop predecesosrs.
604a000883SChandler Carruth   SmallVector<BasicBlock *, 4> InLoopPredecessors;
614a000883SChandler Carruth 
624a000883SChandler Carruth   auto RewriteExit = [&](BasicBlock *BB) {
634a000883SChandler Carruth     assert(InLoopPredecessors.empty() &&
644a000883SChandler Carruth            "Must start with an empty predecessors list!");
654a000883SChandler Carruth     auto Cleanup = make_scope_exit([&] { InLoopPredecessors.clear(); });
664a000883SChandler Carruth 
674a000883SChandler Carruth     // See if there are any non-loop predecessors of this exit block and
684a000883SChandler Carruth     // keep track of the in-loop predecessors.
694a000883SChandler Carruth     bool IsDedicatedExit = true;
704a000883SChandler Carruth     for (auto *PredBB : predecessors(BB))
714a000883SChandler Carruth       if (L->contains(PredBB)) {
724a000883SChandler Carruth         if (isa<IndirectBrInst>(PredBB->getTerminator()))
734a000883SChandler Carruth           // We cannot rewrite exiting edges from an indirectbr.
744a000883SChandler Carruth           return false;
75784929d0SCraig Topper         if (isa<CallBrInst>(PredBB->getTerminator()))
76784929d0SCraig Topper           // We cannot rewrite exiting edges from a callbr.
77784929d0SCraig Topper           return false;
784a000883SChandler Carruth 
794a000883SChandler Carruth         InLoopPredecessors.push_back(PredBB);
804a000883SChandler Carruth       } else {
814a000883SChandler Carruth         IsDedicatedExit = false;
824a000883SChandler Carruth       }
834a000883SChandler Carruth 
844a000883SChandler Carruth     assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
854a000883SChandler Carruth 
864a000883SChandler Carruth     // Nothing to do if this is already a dedicated exit.
874a000883SChandler Carruth     if (IsDedicatedExit)
884a000883SChandler Carruth       return false;
894a000883SChandler Carruth 
904a000883SChandler Carruth     auto *NewExitBB = SplitBlockPredecessors(
9197468e92SAlina Sbirlea         BB, InLoopPredecessors, ".loopexit", DT, LI, MSSAU, PreserveLCSSA);
924a000883SChandler Carruth 
934a000883SChandler Carruth     if (!NewExitBB)
94d34e60caSNicola Zaghen       LLVM_DEBUG(
95d34e60caSNicola Zaghen           dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
964a000883SChandler Carruth                  << *L << "\n");
974a000883SChandler Carruth     else
98d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
994a000883SChandler Carruth                         << NewExitBB->getName() << "\n");
1004a000883SChandler Carruth     return true;
1014a000883SChandler Carruth   };
1024a000883SChandler Carruth 
1034a000883SChandler Carruth   // Walk the exit blocks directly rather than building up a data structure for
1044a000883SChandler Carruth   // them, but only visit each one once.
1054a000883SChandler Carruth   SmallPtrSet<BasicBlock *, 4> Visited;
1064a000883SChandler Carruth   for (auto *BB : L->blocks())
1074a000883SChandler Carruth     for (auto *SuccBB : successors(BB)) {
1084a000883SChandler Carruth       // We're looking for exit blocks so skip in-loop successors.
1094a000883SChandler Carruth       if (L->contains(SuccBB))
1104a000883SChandler Carruth         continue;
1114a000883SChandler Carruth 
1124a000883SChandler Carruth       // Visit each exit block exactly once.
1134a000883SChandler Carruth       if (!Visited.insert(SuccBB).second)
1144a000883SChandler Carruth         continue;
1154a000883SChandler Carruth 
1164a000883SChandler Carruth       Changed |= RewriteExit(SuccBB);
1174a000883SChandler Carruth     }
1184a000883SChandler Carruth 
1194a000883SChandler Carruth   return Changed;
1204a000883SChandler Carruth }
1214a000883SChandler Carruth 
1225f8f34e4SAdrian Prantl /// Returns the instructions that use values defined in the loop.
123c5b7b555SAshutosh Nema SmallVector<Instruction *, 8> llvm::findDefsUsedOutsideOfLoop(Loop *L) {
124c5b7b555SAshutosh Nema   SmallVector<Instruction *, 8> UsedOutside;
125c5b7b555SAshutosh Nema 
126c5b7b555SAshutosh Nema   for (auto *Block : L->getBlocks())
127c5b7b555SAshutosh Nema     // FIXME: I believe that this could use copy_if if the Inst reference could
128c5b7b555SAshutosh Nema     // be adapted into a pointer.
129c5b7b555SAshutosh Nema     for (auto &Inst : *Block) {
130c5b7b555SAshutosh Nema       auto Users = Inst.users();
1310a16c228SDavid Majnemer       if (any_of(Users, [&](User *U) {
132c5b7b555SAshutosh Nema             auto *Use = cast<Instruction>(U);
133c5b7b555SAshutosh Nema             return !L->contains(Use->getParent());
134c5b7b555SAshutosh Nema           }))
135c5b7b555SAshutosh Nema         UsedOutside.push_back(&Inst);
136c5b7b555SAshutosh Nema     }
137c5b7b555SAshutosh Nema 
138c5b7b555SAshutosh Nema   return UsedOutside;
139c5b7b555SAshutosh Nema }
14031088a9dSChandler Carruth 
14131088a9dSChandler Carruth void llvm::getLoopAnalysisUsage(AnalysisUsage &AU) {
14231088a9dSChandler Carruth   // By definition, all loop passes need the LoopInfo analysis and the
14331088a9dSChandler Carruth   // Dominator tree it depends on. Because they all participate in the loop
14431088a9dSChandler Carruth   // pass manager, they must also preserve these.
14531088a9dSChandler Carruth   AU.addRequired<DominatorTreeWrapperPass>();
14631088a9dSChandler Carruth   AU.addPreserved<DominatorTreeWrapperPass>();
14731088a9dSChandler Carruth   AU.addRequired<LoopInfoWrapperPass>();
14831088a9dSChandler Carruth   AU.addPreserved<LoopInfoWrapperPass>();
14931088a9dSChandler Carruth 
15031088a9dSChandler Carruth   // We must also preserve LoopSimplify and LCSSA. We locally access their IDs
15131088a9dSChandler Carruth   // here because users shouldn't directly get them from this header.
15231088a9dSChandler Carruth   extern char &LoopSimplifyID;
15331088a9dSChandler Carruth   extern char &LCSSAID;
15431088a9dSChandler Carruth   AU.addRequiredID(LoopSimplifyID);
15531088a9dSChandler Carruth   AU.addPreservedID(LoopSimplifyID);
15631088a9dSChandler Carruth   AU.addRequiredID(LCSSAID);
15731088a9dSChandler Carruth   AU.addPreservedID(LCSSAID);
158c3ccf5d7SIgor Laevsky   // This is used in the LPPassManager to perform LCSSA verification on passes
159c3ccf5d7SIgor Laevsky   // which preserve lcssa form
160c3ccf5d7SIgor Laevsky   AU.addRequired<LCSSAVerificationPass>();
161c3ccf5d7SIgor Laevsky   AU.addPreserved<LCSSAVerificationPass>();
16231088a9dSChandler Carruth 
16331088a9dSChandler Carruth   // Loop passes are designed to run inside of a loop pass manager which means
16431088a9dSChandler Carruth   // that any function analyses they require must be required by the first loop
16531088a9dSChandler Carruth   // pass in the manager (so that it is computed before the loop pass manager
16631088a9dSChandler Carruth   // runs) and preserved by all loop pasess in the manager. To make this
16731088a9dSChandler Carruth   // reasonably robust, the set needed for most loop passes is maintained here.
16831088a9dSChandler Carruth   // If your loop pass requires an analysis not listed here, you will need to
16931088a9dSChandler Carruth   // carefully audit the loop pass manager nesting structure that results.
17031088a9dSChandler Carruth   AU.addRequired<AAResultsWrapperPass>();
17131088a9dSChandler Carruth   AU.addPreserved<AAResultsWrapperPass>();
17231088a9dSChandler Carruth   AU.addPreserved<BasicAAWrapperPass>();
17331088a9dSChandler Carruth   AU.addPreserved<GlobalsAAWrapperPass>();
17431088a9dSChandler Carruth   AU.addPreserved<SCEVAAWrapperPass>();
17531088a9dSChandler Carruth   AU.addRequired<ScalarEvolutionWrapperPass>();
17631088a9dSChandler Carruth   AU.addPreserved<ScalarEvolutionWrapperPass>();
1776da79ce1SAlina Sbirlea   // FIXME: When all loop passes preserve MemorySSA, it can be required and
1786da79ce1SAlina Sbirlea   // preserved here instead of the individual handling in each pass.
17931088a9dSChandler Carruth }
18031088a9dSChandler Carruth 
18131088a9dSChandler Carruth /// Manually defined generic "LoopPass" dependency initialization. This is used
18231088a9dSChandler Carruth /// to initialize the exact set of passes from above in \c
18331088a9dSChandler Carruth /// getLoopAnalysisUsage. It can be used within a loop pass's initialization
18431088a9dSChandler Carruth /// with:
18531088a9dSChandler Carruth ///
18631088a9dSChandler Carruth ///   INITIALIZE_PASS_DEPENDENCY(LoopPass)
18731088a9dSChandler Carruth ///
18831088a9dSChandler Carruth /// As-if "LoopPass" were a pass.
18931088a9dSChandler Carruth void llvm::initializeLoopPassPass(PassRegistry &Registry) {
19031088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
19131088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
19231088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
193e12c487bSEaswaran Raman   INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
19431088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
19531088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
19631088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
19731088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
19831088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
1996da79ce1SAlina Sbirlea   INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
20031088a9dSChandler Carruth }
201963341c8SAdam Nemet 
2023c3a7652SSerguei Katkov /// Create MDNode for input string.
2033c3a7652SSerguei Katkov static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) {
2043c3a7652SSerguei Katkov   LLVMContext &Context = TheLoop->getHeader()->getContext();
2053c3a7652SSerguei Katkov   Metadata *MDs[] = {
2063c3a7652SSerguei Katkov       MDString::get(Context, Name),
2073c3a7652SSerguei Katkov       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))};
2083c3a7652SSerguei Katkov   return MDNode::get(Context, MDs);
2093c3a7652SSerguei Katkov }
2103c3a7652SSerguei Katkov 
2113c3a7652SSerguei Katkov /// Set input string into loop metadata by keeping other values intact.
2127f8c8095SSerguei Katkov /// If the string is already in loop metadata update value if it is
2137f8c8095SSerguei Katkov /// different.
2147f8c8095SSerguei Katkov void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD,
2153c3a7652SSerguei Katkov                                    unsigned V) {
2163c3a7652SSerguei Katkov   SmallVector<Metadata *, 4> MDs(1);
2173c3a7652SSerguei Katkov   // If the loop already has metadata, retain it.
2183c3a7652SSerguei Katkov   MDNode *LoopID = TheLoop->getLoopID();
2193c3a7652SSerguei Katkov   if (LoopID) {
2203c3a7652SSerguei Katkov     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
2213c3a7652SSerguei Katkov       MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
2227f8c8095SSerguei Katkov       // If it is of form key = value, try to parse it.
2237f8c8095SSerguei Katkov       if (Node->getNumOperands() == 2) {
2247f8c8095SSerguei Katkov         MDString *S = dyn_cast<MDString>(Node->getOperand(0));
2257f8c8095SSerguei Katkov         if (S && S->getString().equals(StringMD)) {
2267f8c8095SSerguei Katkov           ConstantInt *IntMD =
2277f8c8095SSerguei Katkov               mdconst::extract_or_null<ConstantInt>(Node->getOperand(1));
2287f8c8095SSerguei Katkov           if (IntMD && IntMD->getSExtValue() == V)
2297f8c8095SSerguei Katkov             // It is already in place. Do nothing.
2307f8c8095SSerguei Katkov             return;
2317f8c8095SSerguei Katkov           // We need to update the value, so just skip it here and it will
2327f8c8095SSerguei Katkov           // be added after copying other existed nodes.
2337f8c8095SSerguei Katkov           continue;
2347f8c8095SSerguei Katkov         }
2357f8c8095SSerguei Katkov       }
2363c3a7652SSerguei Katkov       MDs.push_back(Node);
2373c3a7652SSerguei Katkov     }
2383c3a7652SSerguei Katkov   }
2393c3a7652SSerguei Katkov   // Add new metadata.
2407f8c8095SSerguei Katkov   MDs.push_back(createStringMetadata(TheLoop, StringMD, V));
2413c3a7652SSerguei Katkov   // Replace current metadata node with new one.
2423c3a7652SSerguei Katkov   LLVMContext &Context = TheLoop->getHeader()->getContext();
2433c3a7652SSerguei Katkov   MDNode *NewLoopID = MDNode::get(Context, MDs);
2443c3a7652SSerguei Katkov   // Set operand 0 to refer to the loop id itself.
2453c3a7652SSerguei Katkov   NewLoopID->replaceOperandWith(0, NewLoopID);
2463c3a7652SSerguei Katkov   TheLoop->setLoopID(NewLoopID);
2473c3a7652SSerguei Katkov }
2483c3a7652SSerguei Katkov 
24972448525SMichael Kruse /// Find string metadata for loop
25072448525SMichael Kruse ///
25172448525SMichael Kruse /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an
25272448525SMichael Kruse /// operand or null otherwise.  If the string metadata is not found return
25372448525SMichael Kruse /// Optional's not-a-value.
254978ba615SMichael Kruse Optional<const MDOperand *> llvm::findStringMetadataForLoop(const Loop *TheLoop,
25572448525SMichael Kruse                                                             StringRef Name) {
256978ba615SMichael Kruse   MDNode *MD = findOptionMDForLoop(TheLoop, Name);
25772448525SMichael Kruse   if (!MD)
25872448525SMichael Kruse     return None;
259fe3def7cSAdam Nemet   switch (MD->getNumOperands()) {
260fe3def7cSAdam Nemet   case 1:
261fe3def7cSAdam Nemet     return nullptr;
262fe3def7cSAdam Nemet   case 2:
263fe3def7cSAdam Nemet     return &MD->getOperand(1);
264fe3def7cSAdam Nemet   default:
265fe3def7cSAdam Nemet     llvm_unreachable("loop metadata has 0 or 1 operand");
266963341c8SAdam Nemet   }
267fe3def7cSAdam Nemet }
26872448525SMichael Kruse 
26972448525SMichael Kruse static Optional<bool> getOptionalBoolLoopAttribute(const Loop *TheLoop,
27072448525SMichael Kruse                                                    StringRef Name) {
271978ba615SMichael Kruse   MDNode *MD = findOptionMDForLoop(TheLoop, Name);
272978ba615SMichael Kruse   if (!MD)
273fe3def7cSAdam Nemet     return None;
274978ba615SMichael Kruse   switch (MD->getNumOperands()) {
27572448525SMichael Kruse   case 1:
27672448525SMichael Kruse     // When the value is absent it is interpreted as 'attribute set'.
27772448525SMichael Kruse     return true;
27872448525SMichael Kruse   case 2:
279f9027e55SAlina Sbirlea     if (ConstantInt *IntMD =
280f9027e55SAlina Sbirlea             mdconst::extract_or_null<ConstantInt>(MD->getOperand(1).get()))
281f9027e55SAlina Sbirlea       return IntMD->getZExtValue();
282f9027e55SAlina Sbirlea     return true;
28372448525SMichael Kruse   }
28472448525SMichael Kruse   llvm_unreachable("unexpected number of options");
28572448525SMichael Kruse }
28672448525SMichael Kruse 
28772448525SMichael Kruse static bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name) {
28872448525SMichael Kruse   return getOptionalBoolLoopAttribute(TheLoop, Name).getValueOr(false);
28972448525SMichael Kruse }
29072448525SMichael Kruse 
29172448525SMichael Kruse llvm::Optional<int> llvm::getOptionalIntLoopAttribute(Loop *TheLoop,
29272448525SMichael Kruse                                                       StringRef Name) {
29372448525SMichael Kruse   const MDOperand *AttrMD =
29472448525SMichael Kruse       findStringMetadataForLoop(TheLoop, Name).getValueOr(nullptr);
29572448525SMichael Kruse   if (!AttrMD)
29672448525SMichael Kruse     return None;
29772448525SMichael Kruse 
29872448525SMichael Kruse   ConstantInt *IntMD = mdconst::extract_or_null<ConstantInt>(AttrMD->get());
29972448525SMichael Kruse   if (!IntMD)
30072448525SMichael Kruse     return None;
30172448525SMichael Kruse 
30272448525SMichael Kruse   return IntMD->getSExtValue();
30372448525SMichael Kruse }
30472448525SMichael Kruse 
30572448525SMichael Kruse Optional<MDNode *> llvm::makeFollowupLoopID(
30672448525SMichael Kruse     MDNode *OrigLoopID, ArrayRef<StringRef> FollowupOptions,
30772448525SMichael Kruse     const char *InheritOptionsExceptPrefix, bool AlwaysNew) {
30872448525SMichael Kruse   if (!OrigLoopID) {
30972448525SMichael Kruse     if (AlwaysNew)
31072448525SMichael Kruse       return nullptr;
31172448525SMichael Kruse     return None;
31272448525SMichael Kruse   }
31372448525SMichael Kruse 
31472448525SMichael Kruse   assert(OrigLoopID->getOperand(0) == OrigLoopID);
31572448525SMichael Kruse 
31672448525SMichael Kruse   bool InheritAllAttrs = !InheritOptionsExceptPrefix;
31772448525SMichael Kruse   bool InheritSomeAttrs =
31872448525SMichael Kruse       InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] != '\0';
31972448525SMichael Kruse   SmallVector<Metadata *, 8> MDs;
32072448525SMichael Kruse   MDs.push_back(nullptr);
32172448525SMichael Kruse 
32272448525SMichael Kruse   bool Changed = false;
32372448525SMichael Kruse   if (InheritAllAttrs || InheritSomeAttrs) {
32472448525SMichael Kruse     for (const MDOperand &Existing : drop_begin(OrigLoopID->operands(), 1)) {
32572448525SMichael Kruse       MDNode *Op = cast<MDNode>(Existing.get());
32672448525SMichael Kruse 
32772448525SMichael Kruse       auto InheritThisAttribute = [InheritSomeAttrs,
32872448525SMichael Kruse                                    InheritOptionsExceptPrefix](MDNode *Op) {
32972448525SMichael Kruse         if (!InheritSomeAttrs)
33072448525SMichael Kruse           return false;
33172448525SMichael Kruse 
33272448525SMichael Kruse         // Skip malformatted attribute metadata nodes.
33372448525SMichael Kruse         if (Op->getNumOperands() == 0)
33472448525SMichael Kruse           return true;
33572448525SMichael Kruse         Metadata *NameMD = Op->getOperand(0).get();
33672448525SMichael Kruse         if (!isa<MDString>(NameMD))
33772448525SMichael Kruse           return true;
33872448525SMichael Kruse         StringRef AttrName = cast<MDString>(NameMD)->getString();
33972448525SMichael Kruse 
34072448525SMichael Kruse         // Do not inherit excluded attributes.
34172448525SMichael Kruse         return !AttrName.startswith(InheritOptionsExceptPrefix);
34272448525SMichael Kruse       };
34372448525SMichael Kruse 
34472448525SMichael Kruse       if (InheritThisAttribute(Op))
34572448525SMichael Kruse         MDs.push_back(Op);
34672448525SMichael Kruse       else
34772448525SMichael Kruse         Changed = true;
34872448525SMichael Kruse     }
34972448525SMichael Kruse   } else {
35072448525SMichael Kruse     // Modified if we dropped at least one attribute.
35172448525SMichael Kruse     Changed = OrigLoopID->getNumOperands() > 1;
35272448525SMichael Kruse   }
35372448525SMichael Kruse 
35472448525SMichael Kruse   bool HasAnyFollowup = false;
35572448525SMichael Kruse   for (StringRef OptionName : FollowupOptions) {
356978ba615SMichael Kruse     MDNode *FollowupNode = findOptionMDForLoopID(OrigLoopID, OptionName);
35772448525SMichael Kruse     if (!FollowupNode)
35872448525SMichael Kruse       continue;
35972448525SMichael Kruse 
36072448525SMichael Kruse     HasAnyFollowup = true;
36172448525SMichael Kruse     for (const MDOperand &Option : drop_begin(FollowupNode->operands(), 1)) {
36272448525SMichael Kruse       MDs.push_back(Option.get());
36372448525SMichael Kruse       Changed = true;
36472448525SMichael Kruse     }
36572448525SMichael Kruse   }
36672448525SMichael Kruse 
36772448525SMichael Kruse   // Attributes of the followup loop not specified explicity, so signal to the
36872448525SMichael Kruse   // transformation pass to add suitable attributes.
36972448525SMichael Kruse   if (!AlwaysNew && !HasAnyFollowup)
37072448525SMichael Kruse     return None;
37172448525SMichael Kruse 
37272448525SMichael Kruse   // If no attributes were added or remove, the previous loop Id can be reused.
37372448525SMichael Kruse   if (!AlwaysNew && !Changed)
37472448525SMichael Kruse     return OrigLoopID;
37572448525SMichael Kruse 
37672448525SMichael Kruse   // No attributes is equivalent to having no !llvm.loop metadata at all.
37772448525SMichael Kruse   if (MDs.size() == 1)
37872448525SMichael Kruse     return nullptr;
37972448525SMichael Kruse 
38072448525SMichael Kruse   // Build the new loop ID.
38172448525SMichael Kruse   MDTuple *FollowupLoopID = MDNode::get(OrigLoopID->getContext(), MDs);
38272448525SMichael Kruse   FollowupLoopID->replaceOperandWith(0, FollowupLoopID);
38372448525SMichael Kruse   return FollowupLoopID;
38472448525SMichael Kruse }
38572448525SMichael Kruse 
38672448525SMichael Kruse bool llvm::hasDisableAllTransformsHint(const Loop *L) {
38772448525SMichael Kruse   return getBooleanLoopAttribute(L, LLVMLoopDisableNonforced);
38872448525SMichael Kruse }
38972448525SMichael Kruse 
3904f64f1baSTim Corringham bool llvm::hasDisableLICMTransformsHint(const Loop *L) {
3914f64f1baSTim Corringham   return getBooleanLoopAttribute(L, LLVMLoopDisableLICM);
3924f64f1baSTim Corringham }
3934f64f1baSTim Corringham 
39472448525SMichael Kruse TransformationMode llvm::hasUnrollTransformation(Loop *L) {
39572448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable"))
39672448525SMichael Kruse     return TM_SuppressedByUser;
39772448525SMichael Kruse 
39872448525SMichael Kruse   Optional<int> Count =
39972448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
40072448525SMichael Kruse   if (Count.hasValue())
40172448525SMichael Kruse     return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
40272448525SMichael Kruse 
40372448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable"))
40472448525SMichael Kruse     return TM_ForcedByUser;
40572448525SMichael Kruse 
40672448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.full"))
40772448525SMichael Kruse     return TM_ForcedByUser;
40872448525SMichael Kruse 
40972448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
41072448525SMichael Kruse     return TM_Disable;
41172448525SMichael Kruse 
41272448525SMichael Kruse   return TM_Unspecified;
41372448525SMichael Kruse }
41472448525SMichael Kruse 
41572448525SMichael Kruse TransformationMode llvm::hasUnrollAndJamTransformation(Loop *L) {
41672448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable"))
41772448525SMichael Kruse     return TM_SuppressedByUser;
41872448525SMichael Kruse 
41972448525SMichael Kruse   Optional<int> Count =
42072448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count");
42172448525SMichael Kruse   if (Count.hasValue())
42272448525SMichael Kruse     return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
42372448525SMichael Kruse 
42472448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.enable"))
42572448525SMichael Kruse     return TM_ForcedByUser;
42672448525SMichael Kruse 
42772448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
42872448525SMichael Kruse     return TM_Disable;
42972448525SMichael Kruse 
43072448525SMichael Kruse   return TM_Unspecified;
43172448525SMichael Kruse }
43272448525SMichael Kruse 
43372448525SMichael Kruse TransformationMode llvm::hasVectorizeTransformation(Loop *L) {
43472448525SMichael Kruse   Optional<bool> Enable =
43572448525SMichael Kruse       getOptionalBoolLoopAttribute(L, "llvm.loop.vectorize.enable");
43672448525SMichael Kruse 
43772448525SMichael Kruse   if (Enable == false)
43872448525SMichael Kruse     return TM_SuppressedByUser;
43972448525SMichael Kruse 
44072448525SMichael Kruse   Optional<int> VectorizeWidth =
44172448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.vectorize.width");
44272448525SMichael Kruse   Optional<int> InterleaveCount =
44372448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count");
44472448525SMichael Kruse 
44572448525SMichael Kruse   // 'Forcing' vector width and interleave count to one effectively disables
44672448525SMichael Kruse   // this tranformation.
44770560a0aSMichael Kruse   if (Enable == true && VectorizeWidth == 1 && InterleaveCount == 1)
44872448525SMichael Kruse     return TM_SuppressedByUser;
44972448525SMichael Kruse 
45072448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.isvectorized"))
45172448525SMichael Kruse     return TM_Disable;
45272448525SMichael Kruse 
45370560a0aSMichael Kruse   if (Enable == true)
45470560a0aSMichael Kruse     return TM_ForcedByUser;
45570560a0aSMichael Kruse 
45672448525SMichael Kruse   if (VectorizeWidth == 1 && InterleaveCount == 1)
45772448525SMichael Kruse     return TM_Disable;
45872448525SMichael Kruse 
45972448525SMichael Kruse   if (VectorizeWidth > 1 || InterleaveCount > 1)
46072448525SMichael Kruse     return TM_Enable;
46172448525SMichael Kruse 
46272448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
46372448525SMichael Kruse     return TM_Disable;
46472448525SMichael Kruse 
46572448525SMichael Kruse   return TM_Unspecified;
46672448525SMichael Kruse }
46772448525SMichael Kruse 
46872448525SMichael Kruse TransformationMode llvm::hasDistributeTransformation(Loop *L) {
46972448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.distribute.enable"))
47072448525SMichael Kruse     return TM_ForcedByUser;
47172448525SMichael Kruse 
47272448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
47372448525SMichael Kruse     return TM_Disable;
47472448525SMichael Kruse 
47572448525SMichael Kruse   return TM_Unspecified;
47672448525SMichael Kruse }
47772448525SMichael Kruse 
47872448525SMichael Kruse TransformationMode llvm::hasLICMVersioningTransformation(Loop *L) {
47972448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.licm_versioning.disable"))
48072448525SMichael Kruse     return TM_SuppressedByUser;
48172448525SMichael Kruse 
48272448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
48372448525SMichael Kruse     return TM_Disable;
48472448525SMichael Kruse 
48572448525SMichael Kruse   return TM_Unspecified;
486963341c8SAdam Nemet }
487122f984aSEvgeniy Stepanov 
4887ed5856aSAlina Sbirlea /// Does a BFS from a given node to all of its children inside a given loop.
4897ed5856aSAlina Sbirlea /// The returned vector of nodes includes the starting point.
4907ed5856aSAlina Sbirlea SmallVector<DomTreeNode *, 16>
4917ed5856aSAlina Sbirlea llvm::collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop) {
4927ed5856aSAlina Sbirlea   SmallVector<DomTreeNode *, 16> Worklist;
4937ed5856aSAlina Sbirlea   auto AddRegionToWorklist = [&](DomTreeNode *DTN) {
4947ed5856aSAlina Sbirlea     // Only include subregions in the top level loop.
4957ed5856aSAlina Sbirlea     BasicBlock *BB = DTN->getBlock();
4967ed5856aSAlina Sbirlea     if (CurLoop->contains(BB))
4977ed5856aSAlina Sbirlea       Worklist.push_back(DTN);
4987ed5856aSAlina Sbirlea   };
4997ed5856aSAlina Sbirlea 
5007ed5856aSAlina Sbirlea   AddRegionToWorklist(N);
5017ed5856aSAlina Sbirlea 
5027ed5856aSAlina Sbirlea   for (size_t I = 0; I < Worklist.size(); I++)
5037ed5856aSAlina Sbirlea     for (DomTreeNode *Child : Worklist[I]->getChildren())
5047ed5856aSAlina Sbirlea       AddRegionToWorklist(Child);
5057ed5856aSAlina Sbirlea 
5067ed5856aSAlina Sbirlea   return Worklist;
5077ed5856aSAlina Sbirlea }
5087ed5856aSAlina Sbirlea 
509efb130fcSAlina Sbirlea void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
510efb130fcSAlina Sbirlea                           LoopInfo *LI, MemorySSA *MSSA) {
511899809d5SHans Wennborg   assert((!DT || L->isLCSSAForm(*DT)) && "Expected LCSSA!");
512df3e71e0SMarcello Maggioni   auto *Preheader = L->getLoopPreheader();
513df3e71e0SMarcello Maggioni   assert(Preheader && "Preheader should exist!");
514df3e71e0SMarcello Maggioni 
515efb130fcSAlina Sbirlea   std::unique_ptr<MemorySSAUpdater> MSSAU;
516efb130fcSAlina Sbirlea   if (MSSA)
517efb130fcSAlina Sbirlea     MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
518efb130fcSAlina Sbirlea 
519df3e71e0SMarcello Maggioni   // Now that we know the removal is safe, remove the loop by changing the
520df3e71e0SMarcello Maggioni   // branch from the preheader to go to the single exit block.
521df3e71e0SMarcello Maggioni   //
522df3e71e0SMarcello Maggioni   // Because we're deleting a large chunk of code at once, the sequence in which
523df3e71e0SMarcello Maggioni   // we remove things is very important to avoid invalidation issues.
524df3e71e0SMarcello Maggioni 
525df3e71e0SMarcello Maggioni   // Tell ScalarEvolution that the loop is deleted. Do this before
526df3e71e0SMarcello Maggioni   // deleting the loop so that ScalarEvolution can look at the loop
527df3e71e0SMarcello Maggioni   // to determine what it needs to clean up.
528df3e71e0SMarcello Maggioni   if (SE)
529df3e71e0SMarcello Maggioni     SE->forgetLoop(L);
530df3e71e0SMarcello Maggioni 
531df3e71e0SMarcello Maggioni   auto *ExitBlock = L->getUniqueExitBlock();
532df3e71e0SMarcello Maggioni   assert(ExitBlock && "Should have a unique exit block!");
533df3e71e0SMarcello Maggioni   assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
534df3e71e0SMarcello Maggioni 
535df3e71e0SMarcello Maggioni   auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator());
536df3e71e0SMarcello Maggioni   assert(OldBr && "Preheader must end with a branch");
537df3e71e0SMarcello Maggioni   assert(OldBr->isUnconditional() && "Preheader must have a single successor");
538df3e71e0SMarcello Maggioni   // Connect the preheader to the exit block. Keep the old edge to the header
539df3e71e0SMarcello Maggioni   // around to perform the dominator tree update in two separate steps
540df3e71e0SMarcello Maggioni   // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
541df3e71e0SMarcello Maggioni   // preheader -> header.
542df3e71e0SMarcello Maggioni   //
543df3e71e0SMarcello Maggioni   //
544df3e71e0SMarcello Maggioni   // 0.  Preheader          1.  Preheader           2.  Preheader
545df3e71e0SMarcello Maggioni   //        |                    |   |                   |
546df3e71e0SMarcello Maggioni   //        V                    |   V                   |
547df3e71e0SMarcello Maggioni   //      Header <--\            | Header <--\           | Header <--\
548df3e71e0SMarcello Maggioni   //       |  |     |            |  |  |     |           |  |  |     |
549df3e71e0SMarcello Maggioni   //       |  V     |            |  |  V     |           |  |  V     |
550df3e71e0SMarcello Maggioni   //       | Body --/            |  | Body --/           |  | Body --/
551df3e71e0SMarcello Maggioni   //       V                     V  V                    V  V
552df3e71e0SMarcello Maggioni   //      Exit                   Exit                    Exit
553df3e71e0SMarcello Maggioni   //
554df3e71e0SMarcello Maggioni   // By doing this is two separate steps we can perform the dominator tree
555df3e71e0SMarcello Maggioni   // update without using the batch update API.
556df3e71e0SMarcello Maggioni   //
557df3e71e0SMarcello Maggioni   // Even when the loop is never executed, we cannot remove the edge from the
558df3e71e0SMarcello Maggioni   // source block to the exit block. Consider the case where the unexecuted loop
559df3e71e0SMarcello Maggioni   // branches back to an outer loop. If we deleted the loop and removed the edge
560df3e71e0SMarcello Maggioni   // coming to this inner loop, this will break the outer loop structure (by
561df3e71e0SMarcello Maggioni   // deleting the backedge of the outer loop). If the outer loop is indeed a
562df3e71e0SMarcello Maggioni   // non-loop, it will be deleted in a future iteration of loop deletion pass.
563df3e71e0SMarcello Maggioni   IRBuilder<> Builder(OldBr);
564df3e71e0SMarcello Maggioni   Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
565df3e71e0SMarcello Maggioni   // Remove the old branch. The conditional branch becomes a new terminator.
566df3e71e0SMarcello Maggioni   OldBr->eraseFromParent();
567df3e71e0SMarcello Maggioni 
568df3e71e0SMarcello Maggioni   // Rewrite phis in the exit block to get their inputs from the Preheader
569df3e71e0SMarcello Maggioni   // instead of the exiting block.
570c7fc81e6SBenjamin Kramer   for (PHINode &P : ExitBlock->phis()) {
571df3e71e0SMarcello Maggioni     // Set the zero'th element of Phi to be from the preheader and remove all
572df3e71e0SMarcello Maggioni     // other incoming values. Given the loop has dedicated exits, all other
573df3e71e0SMarcello Maggioni     // incoming values must be from the exiting blocks.
574df3e71e0SMarcello Maggioni     int PredIndex = 0;
575c7fc81e6SBenjamin Kramer     P.setIncomingBlock(PredIndex, Preheader);
576df3e71e0SMarcello Maggioni     // Removes all incoming values from all other exiting blocks (including
577df3e71e0SMarcello Maggioni     // duplicate values from an exiting block).
578df3e71e0SMarcello Maggioni     // Nuke all entries except the zero'th entry which is the preheader entry.
579df3e71e0SMarcello Maggioni     // NOTE! We need to remove Incoming Values in the reverse order as done
580df3e71e0SMarcello Maggioni     // below, to keep the indices valid for deletion (removeIncomingValues
581df3e71e0SMarcello Maggioni     // updates getNumIncomingValues and shifts all values down into the operand
582df3e71e0SMarcello Maggioni     // being deleted).
583c7fc81e6SBenjamin Kramer     for (unsigned i = 0, e = P.getNumIncomingValues() - 1; i != e; ++i)
584c7fc81e6SBenjamin Kramer       P.removeIncomingValue(e - i, false);
585df3e71e0SMarcello Maggioni 
586c7fc81e6SBenjamin Kramer     assert((P.getNumIncomingValues() == 1 &&
587c7fc81e6SBenjamin Kramer             P.getIncomingBlock(PredIndex) == Preheader) &&
588df3e71e0SMarcello Maggioni            "Should have exactly one value and that's from the preheader!");
589df3e71e0SMarcello Maggioni   }
590df3e71e0SMarcello Maggioni 
591efb130fcSAlina Sbirlea   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
592efb130fcSAlina Sbirlea   if (DT) {
593efb130fcSAlina Sbirlea     DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}});
594efb130fcSAlina Sbirlea     if (MSSA) {
595efb130fcSAlina Sbirlea       MSSAU->applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}}, *DT);
596efb130fcSAlina Sbirlea       if (VerifyMemorySSA)
597efb130fcSAlina Sbirlea         MSSA->verifyMemorySSA();
598efb130fcSAlina Sbirlea     }
599efb130fcSAlina Sbirlea   }
600efb130fcSAlina Sbirlea 
601df3e71e0SMarcello Maggioni   // Disconnect the loop body by branching directly to its exit.
602df3e71e0SMarcello Maggioni   Builder.SetInsertPoint(Preheader->getTerminator());
603df3e71e0SMarcello Maggioni   Builder.CreateBr(ExitBlock);
604df3e71e0SMarcello Maggioni   // Remove the old branch.
605df3e71e0SMarcello Maggioni   Preheader->getTerminator()->eraseFromParent();
606df3e71e0SMarcello Maggioni 
607df3e71e0SMarcello Maggioni   if (DT) {
608efb130fcSAlina Sbirlea     DTU.applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}});
609efb130fcSAlina Sbirlea     if (MSSA) {
610efb130fcSAlina Sbirlea       MSSAU->applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}},
611efb130fcSAlina Sbirlea                           *DT);
612efb130fcSAlina Sbirlea       if (VerifyMemorySSA)
613efb130fcSAlina Sbirlea         MSSA->verifyMemorySSA();
614efb130fcSAlina Sbirlea       SmallSetVector<BasicBlock *, 8> DeadBlockSet(L->block_begin(),
615efb130fcSAlina Sbirlea                                                    L->block_end());
616efb130fcSAlina Sbirlea       MSSAU->removeBlocks(DeadBlockSet);
617efb130fcSAlina Sbirlea     }
618df3e71e0SMarcello Maggioni   }
619df3e71e0SMarcello Maggioni 
620744c3c32SDavide Italiano   // Use a map to unique and a vector to guarantee deterministic ordering.
6218ee59ca6SDavide Italiano   llvm::SmallDenseSet<std::pair<DIVariable *, DIExpression *>, 4> DeadDebugSet;
622744c3c32SDavide Italiano   llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst;
623744c3c32SDavide Italiano 
624a757d65cSSerguei Katkov   // Given LCSSA form is satisfied, we should not have users of instructions
625a757d65cSSerguei Katkov   // within the dead loop outside of the loop. However, LCSSA doesn't take
626a757d65cSSerguei Katkov   // unreachable uses into account. We handle them here.
627a757d65cSSerguei Katkov   // We could do it after drop all references (in this case all users in the
628a757d65cSSerguei Katkov   // loop will be already eliminated and we have less work to do but according
629a757d65cSSerguei Katkov   // to API doc of User::dropAllReferences only valid operation after dropping
630a757d65cSSerguei Katkov   // references, is deletion. So let's substitute all usages of
631a757d65cSSerguei Katkov   // instruction from the loop with undef value of corresponding type first.
632a757d65cSSerguei Katkov   for (auto *Block : L->blocks())
633a757d65cSSerguei Katkov     for (Instruction &I : *Block) {
634a757d65cSSerguei Katkov       auto *Undef = UndefValue::get(I.getType());
635a757d65cSSerguei Katkov       for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E;) {
636a757d65cSSerguei Katkov         Use &U = *UI;
637a757d65cSSerguei Katkov         ++UI;
638a757d65cSSerguei Katkov         if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
639a757d65cSSerguei Katkov           if (L->contains(Usr->getParent()))
640a757d65cSSerguei Katkov             continue;
641a757d65cSSerguei Katkov         // If we have a DT then we can check that uses outside a loop only in
642a757d65cSSerguei Katkov         // unreachable block.
643a757d65cSSerguei Katkov         if (DT)
644a757d65cSSerguei Katkov           assert(!DT->isReachableFromEntry(U) &&
645a757d65cSSerguei Katkov                  "Unexpected user in reachable block");
646a757d65cSSerguei Katkov         U.set(Undef);
647a757d65cSSerguei Katkov       }
648744c3c32SDavide Italiano       auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
649744c3c32SDavide Italiano       if (!DVI)
650744c3c32SDavide Italiano         continue;
6518ee59ca6SDavide Italiano       auto Key = DeadDebugSet.find({DVI->getVariable(), DVI->getExpression()});
6528ee59ca6SDavide Italiano       if (Key != DeadDebugSet.end())
653744c3c32SDavide Italiano         continue;
6548ee59ca6SDavide Italiano       DeadDebugSet.insert({DVI->getVariable(), DVI->getExpression()});
655744c3c32SDavide Italiano       DeadDebugInst.push_back(DVI);
656a757d65cSSerguei Katkov     }
657a757d65cSSerguei Katkov 
658744c3c32SDavide Italiano   // After the loop has been deleted all the values defined and modified
659744c3c32SDavide Italiano   // inside the loop are going to be unavailable.
660744c3c32SDavide Italiano   // Since debug values in the loop have been deleted, inserting an undef
661744c3c32SDavide Italiano   // dbg.value truncates the range of any dbg.value before the loop where the
662744c3c32SDavide Italiano   // loop used to be. This is particularly important for constant values.
663744c3c32SDavide Italiano   DIBuilder DIB(*ExitBlock->getModule());
664e5be660eSRoman Lebedev   Instruction *InsertDbgValueBefore = ExitBlock->getFirstNonPHI();
665e5be660eSRoman Lebedev   assert(InsertDbgValueBefore &&
666e5be660eSRoman Lebedev          "There should be a non-PHI instruction in exit block, else these "
667e5be660eSRoman Lebedev          "instructions will have no parent.");
668744c3c32SDavide Italiano   for (auto *DVI : DeadDebugInst)
669e5be660eSRoman Lebedev     DIB.insertDbgValueIntrinsic(UndefValue::get(Builder.getInt32Ty()),
670e5be660eSRoman Lebedev                                 DVI->getVariable(), DVI->getExpression(),
671e5be660eSRoman Lebedev                                 DVI->getDebugLoc(), InsertDbgValueBefore);
672744c3c32SDavide Italiano 
673df3e71e0SMarcello Maggioni   // Remove the block from the reference counting scheme, so that we can
674df3e71e0SMarcello Maggioni   // delete it freely later.
675df3e71e0SMarcello Maggioni   for (auto *Block : L->blocks())
676df3e71e0SMarcello Maggioni     Block->dropAllReferences();
677df3e71e0SMarcello Maggioni 
678efb130fcSAlina Sbirlea   if (MSSA && VerifyMemorySSA)
679efb130fcSAlina Sbirlea     MSSA->verifyMemorySSA();
680efb130fcSAlina Sbirlea 
681df3e71e0SMarcello Maggioni   if (LI) {
682df3e71e0SMarcello Maggioni     // Erase the instructions and the blocks without having to worry
683df3e71e0SMarcello Maggioni     // about ordering because we already dropped the references.
684df3e71e0SMarcello Maggioni     // NOTE: This iteration is safe because erasing the block does not remove
685df3e71e0SMarcello Maggioni     // its entry from the loop's block list.  We do that in the next section.
686df3e71e0SMarcello Maggioni     for (Loop::block_iterator LpI = L->block_begin(), LpE = L->block_end();
687df3e71e0SMarcello Maggioni          LpI != LpE; ++LpI)
688df3e71e0SMarcello Maggioni       (*LpI)->eraseFromParent();
689df3e71e0SMarcello Maggioni 
690df3e71e0SMarcello Maggioni     // Finally, the blocks from loopinfo.  This has to happen late because
691df3e71e0SMarcello Maggioni     // otherwise our loop iterators won't work.
692df3e71e0SMarcello Maggioni 
693df3e71e0SMarcello Maggioni     SmallPtrSet<BasicBlock *, 8> blocks;
694df3e71e0SMarcello Maggioni     blocks.insert(L->block_begin(), L->block_end());
695df3e71e0SMarcello Maggioni     for (BasicBlock *BB : blocks)
696df3e71e0SMarcello Maggioni       LI->removeBlock(BB);
697df3e71e0SMarcello Maggioni 
698df3e71e0SMarcello Maggioni     // The last step is to update LoopInfo now that we've eliminated this loop.
6999883d7edSWhitney Tsang     // Note: LoopInfo::erase remove the given loop and relink its subloops with
7009883d7edSWhitney Tsang     // its parent. While removeLoop/removeChildLoop remove the given loop but
7019883d7edSWhitney Tsang     // not relink its subloops, which is what we want.
7029883d7edSWhitney Tsang     if (Loop *ParentLoop = L->getParentLoop()) {
7039883d7edSWhitney Tsang       Loop::iterator I = find(ParentLoop->begin(), ParentLoop->end(), L);
7049883d7edSWhitney Tsang       assert(I != ParentLoop->end() && "Couldn't find loop");
7059883d7edSWhitney Tsang       ParentLoop->removeChildLoop(I);
7069883d7edSWhitney Tsang     } else {
7079883d7edSWhitney Tsang       Loop::iterator I = find(LI->begin(), LI->end(), L);
7089883d7edSWhitney Tsang       assert(I != LI->end() && "Couldn't find loop");
7099883d7edSWhitney Tsang       LI->removeLoop(I);
7109883d7edSWhitney Tsang     }
7119883d7edSWhitney Tsang     LI->destroy(L);
712df3e71e0SMarcello Maggioni   }
713df3e71e0SMarcello Maggioni }
714df3e71e0SMarcello Maggioni 
715af7e1588SEvgeniy Brevnov /// Checks if \p L has single exit through latch block except possibly
716af7e1588SEvgeniy Brevnov /// "deoptimizing" exits. Returns branch instruction terminating the loop
717af7e1588SEvgeniy Brevnov /// latch if above check is successful, nullptr otherwise.
718af7e1588SEvgeniy Brevnov static BranchInst *getExpectedExitLoopLatchBranch(Loop *L) {
71945c43e7dSSerguei Katkov   BasicBlock *Latch = L->getLoopLatch();
72045c43e7dSSerguei Katkov   if (!Latch)
721af7e1588SEvgeniy Brevnov     return nullptr;
722af7e1588SEvgeniy Brevnov 
72345c43e7dSSerguei Katkov   BranchInst *LatchBR = dyn_cast<BranchInst>(Latch->getTerminator());
72445c43e7dSSerguei Katkov   if (!LatchBR || LatchBR->getNumSuccessors() != 2 || !L->isLoopExiting(Latch))
725af7e1588SEvgeniy Brevnov     return nullptr;
72641d72a86SDehao Chen 
72741d72a86SDehao Chen   assert((LatchBR->getSuccessor(0) == L->getHeader() ||
72841d72a86SDehao Chen           LatchBR->getSuccessor(1) == L->getHeader()) &&
72941d72a86SDehao Chen          "At least one edge out of the latch must go to the header");
73041d72a86SDehao Chen 
73145c43e7dSSerguei Katkov   SmallVector<BasicBlock *, 4> ExitBlocks;
73245c43e7dSSerguei Katkov   L->getUniqueNonLatchExitBlocks(ExitBlocks);
73345c43e7dSSerguei Katkov   if (any_of(ExitBlocks, [](const BasicBlock *EB) {
73445c43e7dSSerguei Katkov         return !EB->getTerminatingDeoptimizeCall();
73545c43e7dSSerguei Katkov       }))
736af7e1588SEvgeniy Brevnov     return nullptr;
737af7e1588SEvgeniy Brevnov 
738af7e1588SEvgeniy Brevnov   return LatchBR;
739af7e1588SEvgeniy Brevnov }
740af7e1588SEvgeniy Brevnov 
741af7e1588SEvgeniy Brevnov Optional<unsigned>
742af7e1588SEvgeniy Brevnov llvm::getLoopEstimatedTripCount(Loop *L,
743af7e1588SEvgeniy Brevnov                                 unsigned *EstimatedLoopInvocationWeight) {
744af7e1588SEvgeniy Brevnov   // Support loops with an exiting latch and other existing exists only
745af7e1588SEvgeniy Brevnov   // deoptimize.
746af7e1588SEvgeniy Brevnov   BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L);
747af7e1588SEvgeniy Brevnov   if (!LatchBranch)
74845c43e7dSSerguei Katkov     return None;
74945c43e7dSSerguei Katkov 
75041d72a86SDehao Chen   // To estimate the number of times the loop body was executed, we want to
75141d72a86SDehao Chen   // know the number of times the backedge was taken, vs. the number of times
75241d72a86SDehao Chen   // we exited the loop.
753f0abe820SEvgeniy Brevnov   uint64_t BackedgeTakenWeight, LatchExitWeight;
754af7e1588SEvgeniy Brevnov   if (!LatchBranch->extractProfMetadata(BackedgeTakenWeight, LatchExitWeight))
75541d72a86SDehao Chen     return None;
75641d72a86SDehao Chen 
757af7e1588SEvgeniy Brevnov   if (LatchBranch->getSuccessor(0) != L->getHeader())
758f0abe820SEvgeniy Brevnov     std::swap(BackedgeTakenWeight, LatchExitWeight);
759f0abe820SEvgeniy Brevnov 
76010357e1cSEvgeniy Brevnov   if (!LatchExitWeight)
76110357e1cSEvgeniy Brevnov     return None;
76241d72a86SDehao Chen 
763af7e1588SEvgeniy Brevnov   if (EstimatedLoopInvocationWeight)
764af7e1588SEvgeniy Brevnov     *EstimatedLoopInvocationWeight = LatchExitWeight;
765af7e1588SEvgeniy Brevnov 
76610357e1cSEvgeniy Brevnov   // Estimated backedge taken count is a ratio of the backedge taken weight by
767cfe97681SEvgeniy Brevnov   // the weight of the edge exiting the loop, rounded to nearest.
76810357e1cSEvgeniy Brevnov   uint64_t BackedgeTakenCount =
76910357e1cSEvgeniy Brevnov       llvm::divideNearest(BackedgeTakenWeight, LatchExitWeight);
77010357e1cSEvgeniy Brevnov   // Estimated trip count is one plus estimated backedge taken count.
77110357e1cSEvgeniy Brevnov   return BackedgeTakenCount + 1;
77241d72a86SDehao Chen }
773cf9daa33SAmara Emerson 
774af7e1588SEvgeniy Brevnov bool llvm::setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount,
775af7e1588SEvgeniy Brevnov                                      unsigned EstimatedloopInvocationWeight) {
776af7e1588SEvgeniy Brevnov   // Support loops with an exiting latch and other existing exists only
777af7e1588SEvgeniy Brevnov   // deoptimize.
778af7e1588SEvgeniy Brevnov   BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L);
779af7e1588SEvgeniy Brevnov   if (!LatchBranch)
780af7e1588SEvgeniy Brevnov     return false;
781af7e1588SEvgeniy Brevnov 
782af7e1588SEvgeniy Brevnov   // Calculate taken and exit weights.
783af7e1588SEvgeniy Brevnov   unsigned LatchExitWeight = 0;
784af7e1588SEvgeniy Brevnov   unsigned BackedgeTakenWeight = 0;
785af7e1588SEvgeniy Brevnov 
786af7e1588SEvgeniy Brevnov   if (EstimatedTripCount > 0) {
787af7e1588SEvgeniy Brevnov     LatchExitWeight = EstimatedloopInvocationWeight;
788af7e1588SEvgeniy Brevnov     BackedgeTakenWeight = (EstimatedTripCount - 1) * LatchExitWeight;
789af7e1588SEvgeniy Brevnov   }
790af7e1588SEvgeniy Brevnov 
791af7e1588SEvgeniy Brevnov   // Make a swap if back edge is taken when condition is "false".
792af7e1588SEvgeniy Brevnov   if (LatchBranch->getSuccessor(0) != L->getHeader())
793af7e1588SEvgeniy Brevnov     std::swap(BackedgeTakenWeight, LatchExitWeight);
794af7e1588SEvgeniy Brevnov 
795af7e1588SEvgeniy Brevnov   MDBuilder MDB(LatchBranch->getContext());
796af7e1588SEvgeniy Brevnov 
797af7e1588SEvgeniy Brevnov   // Set/Update profile metadata.
798af7e1588SEvgeniy Brevnov   LatchBranch->setMetadata(
799af7e1588SEvgeniy Brevnov       LLVMContext::MD_prof,
800af7e1588SEvgeniy Brevnov       MDB.createBranchWeights(BackedgeTakenWeight, LatchExitWeight));
801af7e1588SEvgeniy Brevnov 
802af7e1588SEvgeniy Brevnov   return true;
803af7e1588SEvgeniy Brevnov }
804af7e1588SEvgeniy Brevnov 
8056cb64787SDavid Green bool llvm::hasIterationCountInvariantInParent(Loop *InnerLoop,
806395b80cdSDavid Green                                               ScalarEvolution &SE) {
807395b80cdSDavid Green   Loop *OuterL = InnerLoop->getParentLoop();
808395b80cdSDavid Green   if (!OuterL)
809395b80cdSDavid Green     return true;
810395b80cdSDavid Green 
811395b80cdSDavid Green   // Get the backedge taken count for the inner loop
812395b80cdSDavid Green   BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
813395b80cdSDavid Green   const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch);
814395b80cdSDavid Green   if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
815395b80cdSDavid Green       !InnerLoopBECountSC->getType()->isIntegerTy())
816395b80cdSDavid Green     return false;
817395b80cdSDavid Green 
818395b80cdSDavid Green   // Get whether count is invariant to the outer loop
819395b80cdSDavid Green   ScalarEvolution::LoopDisposition LD =
820395b80cdSDavid Green       SE.getLoopDisposition(InnerLoopBECountSC, OuterL);
821395b80cdSDavid Green   if (LD != ScalarEvolution::LoopInvariant)
822395b80cdSDavid Green     return false;
823395b80cdSDavid Green 
824395b80cdSDavid Green   return true;
825395b80cdSDavid Green }
826395b80cdSDavid Green 
8276594dc37SVikram TV Value *llvm::createMinMaxOp(IRBuilder<> &Builder,
8286594dc37SVikram TV                             RecurrenceDescriptor::MinMaxRecurrenceKind RK,
8296594dc37SVikram TV                             Value *Left, Value *Right) {
8306594dc37SVikram TV   CmpInst::Predicate P = CmpInst::ICMP_NE;
8316594dc37SVikram TV   switch (RK) {
8326594dc37SVikram TV   default:
8336594dc37SVikram TV     llvm_unreachable("Unknown min/max recurrence kind");
8346594dc37SVikram TV   case RecurrenceDescriptor::MRK_UIntMin:
8356594dc37SVikram TV     P = CmpInst::ICMP_ULT;
8366594dc37SVikram TV     break;
8376594dc37SVikram TV   case RecurrenceDescriptor::MRK_UIntMax:
8386594dc37SVikram TV     P = CmpInst::ICMP_UGT;
8396594dc37SVikram TV     break;
8406594dc37SVikram TV   case RecurrenceDescriptor::MRK_SIntMin:
8416594dc37SVikram TV     P = CmpInst::ICMP_SLT;
8426594dc37SVikram TV     break;
8436594dc37SVikram TV   case RecurrenceDescriptor::MRK_SIntMax:
8446594dc37SVikram TV     P = CmpInst::ICMP_SGT;
8456594dc37SVikram TV     break;
8466594dc37SVikram TV   case RecurrenceDescriptor::MRK_FloatMin:
8476594dc37SVikram TV     P = CmpInst::FCMP_OLT;
8486594dc37SVikram TV     break;
8496594dc37SVikram TV   case RecurrenceDescriptor::MRK_FloatMax:
8506594dc37SVikram TV     P = CmpInst::FCMP_OGT;
8516594dc37SVikram TV     break;
8526594dc37SVikram TV   }
8536594dc37SVikram TV 
8546594dc37SVikram TV   // We only match FP sequences that are 'fast', so we can unconditionally
8556594dc37SVikram TV   // set it on any generated instructions.
8566594dc37SVikram TV   IRBuilder<>::FastMathFlagGuard FMFG(Builder);
8576594dc37SVikram TV   FastMathFlags FMF;
8586594dc37SVikram TV   FMF.setFast();
8596594dc37SVikram TV   Builder.setFastMathFlags(FMF);
8606594dc37SVikram TV 
8616594dc37SVikram TV   Value *Cmp;
8626594dc37SVikram TV   if (RK == RecurrenceDescriptor::MRK_FloatMin ||
8636594dc37SVikram TV       RK == RecurrenceDescriptor::MRK_FloatMax)
8646594dc37SVikram TV     Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
8656594dc37SVikram TV   else
8666594dc37SVikram TV     Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
8676594dc37SVikram TV 
8686594dc37SVikram TV   Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
8696594dc37SVikram TV   return Select;
8706594dc37SVikram TV }
8716594dc37SVikram TV 
87223c2182cSSimon Pilgrim // Helper to generate an ordered reduction.
87323c2182cSSimon Pilgrim Value *
87423c2182cSSimon Pilgrim llvm::getOrderedReduction(IRBuilder<> &Builder, Value *Acc, Value *Src,
87523c2182cSSimon Pilgrim                           unsigned Op,
87623c2182cSSimon Pilgrim                           RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind,
87723c2182cSSimon Pilgrim                           ArrayRef<Value *> RedOps) {
87823c2182cSSimon Pilgrim   unsigned VF = Src->getType()->getVectorNumElements();
87923c2182cSSimon Pilgrim 
88023c2182cSSimon Pilgrim   // Extract and apply reduction ops in ascending order:
88123c2182cSSimon Pilgrim   // e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1]
88223c2182cSSimon Pilgrim   Value *Result = Acc;
88323c2182cSSimon Pilgrim   for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) {
88423c2182cSSimon Pilgrim     Value *Ext =
88523c2182cSSimon Pilgrim         Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx));
88623c2182cSSimon Pilgrim 
88723c2182cSSimon Pilgrim     if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
88823c2182cSSimon Pilgrim       Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext,
88923c2182cSSimon Pilgrim                                    "bin.rdx");
89023c2182cSSimon Pilgrim     } else {
89123c2182cSSimon Pilgrim       assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid &&
89223c2182cSSimon Pilgrim              "Invalid min/max");
8936594dc37SVikram TV       Result = createMinMaxOp(Builder, MinMaxKind, Result, Ext);
89423c2182cSSimon Pilgrim     }
89523c2182cSSimon Pilgrim 
89623c2182cSSimon Pilgrim     if (!RedOps.empty())
89723c2182cSSimon Pilgrim       propagateIRFlags(Result, RedOps);
89823c2182cSSimon Pilgrim   }
89923c2182cSSimon Pilgrim 
90023c2182cSSimon Pilgrim   return Result;
90123c2182cSSimon Pilgrim }
90223c2182cSSimon Pilgrim 
903cf9daa33SAmara Emerson // Helper to generate a log2 shuffle reduction.
904836b0f48SAmara Emerson Value *
905836b0f48SAmara Emerson llvm::getShuffleReduction(IRBuilder<> &Builder, Value *Src, unsigned Op,
906836b0f48SAmara Emerson                           RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind,
907ad62a3a2SSanjay Patel                           ArrayRef<Value *> RedOps) {
908cf9daa33SAmara Emerson   unsigned VF = Src->getType()->getVectorNumElements();
909cf9daa33SAmara Emerson   // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
910cf9daa33SAmara Emerson   // and vector ops, reducing the set of values being computed by half each
911cf9daa33SAmara Emerson   // round.
912cf9daa33SAmara Emerson   assert(isPowerOf2_32(VF) &&
913cf9daa33SAmara Emerson          "Reduction emission only supported for pow2 vectors!");
914cf9daa33SAmara Emerson   Value *TmpVec = Src;
915cf9daa33SAmara Emerson   SmallVector<Constant *, 32> ShuffleMask(VF, nullptr);
916cf9daa33SAmara Emerson   for (unsigned i = VF; i != 1; i >>= 1) {
917cf9daa33SAmara Emerson     // Move the upper half of the vector to the lower half.
918cf9daa33SAmara Emerson     for (unsigned j = 0; j != i / 2; ++j)
919cf9daa33SAmara Emerson       ShuffleMask[j] = Builder.getInt32(i / 2 + j);
920cf9daa33SAmara Emerson 
921cf9daa33SAmara Emerson     // Fill the rest of the mask with undef.
922cf9daa33SAmara Emerson     std::fill(&ShuffleMask[i / 2], ShuffleMask.end(),
923cf9daa33SAmara Emerson               UndefValue::get(Builder.getInt32Ty()));
924cf9daa33SAmara Emerson 
925cf9daa33SAmara Emerson     Value *Shuf = Builder.CreateShuffleVector(
926cf9daa33SAmara Emerson         TmpVec, UndefValue::get(TmpVec->getType()),
927cf9daa33SAmara Emerson         ConstantVector::get(ShuffleMask), "rdx.shuf");
928cf9daa33SAmara Emerson 
929cf9daa33SAmara Emerson     if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
930ad62a3a2SSanjay Patel       // The builder propagates its fast-math-flags setting.
931ad62a3a2SSanjay Patel       TmpVec = Builder.CreateBinOp((Instruction::BinaryOps)Op, TmpVec, Shuf,
932ad62a3a2SSanjay Patel                                    "bin.rdx");
933cf9daa33SAmara Emerson     } else {
934cf9daa33SAmara Emerson       assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid &&
935cf9daa33SAmara Emerson              "Invalid min/max");
9366594dc37SVikram TV       TmpVec = createMinMaxOp(Builder, MinMaxKind, TmpVec, Shuf);
937cf9daa33SAmara Emerson     }
938cf9daa33SAmara Emerson     if (!RedOps.empty())
939cf9daa33SAmara Emerson       propagateIRFlags(TmpVec, RedOps);
940bc1148e7SSanjay Patel 
941bc1148e7SSanjay Patel     // We may compute the reassociated scalar ops in a way that does not
942bc1148e7SSanjay Patel     // preserve nsw/nuw etc. Conservatively, drop those flags.
943bc1148e7SSanjay Patel     if (auto *ReductionInst = dyn_cast<Instruction>(TmpVec))
944bc1148e7SSanjay Patel       ReductionInst->dropPoisonGeneratingFlags();
945cf9daa33SAmara Emerson   }
946cf9daa33SAmara Emerson   // The result is in the first element of the vector.
947cf9daa33SAmara Emerson   return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
948cf9daa33SAmara Emerson }
949cf9daa33SAmara Emerson 
950cf9daa33SAmara Emerson /// Create a simple vector reduction specified by an opcode and some
951cf9daa33SAmara Emerson /// flags (if generating min/max reductions).
952cf9daa33SAmara Emerson Value *llvm::createSimpleTargetReduction(
953cf9daa33SAmara Emerson     IRBuilder<> &Builder, const TargetTransformInfo *TTI, unsigned Opcode,
954ad62a3a2SSanjay Patel     Value *Src, TargetTransformInfo::ReductionFlags Flags,
955cf9daa33SAmara Emerson     ArrayRef<Value *> RedOps) {
956cf9daa33SAmara Emerson   assert(isa<VectorType>(Src->getType()) && "Type must be a vector");
957cf9daa33SAmara Emerson 
958cf9daa33SAmara Emerson   std::function<Value *()> BuildFunc;
959cf9daa33SAmara Emerson   using RD = RecurrenceDescriptor;
960cf9daa33SAmara Emerson   RD::MinMaxRecurrenceKind MinMaxKind = RD::MRK_Invalid;
961cf9daa33SAmara Emerson 
962cf9daa33SAmara Emerson   switch (Opcode) {
963cf9daa33SAmara Emerson   case Instruction::Add:
964cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateAddReduce(Src); };
965cf9daa33SAmara Emerson     break;
966cf9daa33SAmara Emerson   case Instruction::Mul:
967cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateMulReduce(Src); };
968cf9daa33SAmara Emerson     break;
969cf9daa33SAmara Emerson   case Instruction::And:
970cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateAndReduce(Src); };
971cf9daa33SAmara Emerson     break;
972cf9daa33SAmara Emerson   case Instruction::Or:
973cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateOrReduce(Src); };
974cf9daa33SAmara Emerson     break;
975cf9daa33SAmara Emerson   case Instruction::Xor:
976cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateXorReduce(Src); };
977cf9daa33SAmara Emerson     break;
978cf9daa33SAmara Emerson   case Instruction::FAdd:
979cf9daa33SAmara Emerson     BuildFunc = [&]() {
980cbeb563cSSander de Smalen       auto Rdx = Builder.CreateFAddReduce(
981cbeb563cSSander de Smalen           Constant::getNullValue(Src->getType()->getVectorElementType()), Src);
982cf9daa33SAmara Emerson       return Rdx;
983cf9daa33SAmara Emerson     };
984cf9daa33SAmara Emerson     break;
985cf9daa33SAmara Emerson   case Instruction::FMul:
986cf9daa33SAmara Emerson     BuildFunc = [&]() {
987cbeb563cSSander de Smalen       Type *Ty = Src->getType()->getVectorElementType();
988cbeb563cSSander de Smalen       auto Rdx = Builder.CreateFMulReduce(ConstantFP::get(Ty, 1.0), Src);
989cf9daa33SAmara Emerson       return Rdx;
990cf9daa33SAmara Emerson     };
991cf9daa33SAmara Emerson     break;
992cf9daa33SAmara Emerson   case Instruction::ICmp:
993cf9daa33SAmara Emerson     if (Flags.IsMaxOp) {
994cf9daa33SAmara Emerson       MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMax : RD::MRK_UIntMax;
995cf9daa33SAmara Emerson       BuildFunc = [&]() {
996cf9daa33SAmara Emerson         return Builder.CreateIntMaxReduce(Src, Flags.IsSigned);
997cf9daa33SAmara Emerson       };
998cf9daa33SAmara Emerson     } else {
999cf9daa33SAmara Emerson       MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMin : RD::MRK_UIntMin;
1000cf9daa33SAmara Emerson       BuildFunc = [&]() {
1001cf9daa33SAmara Emerson         return Builder.CreateIntMinReduce(Src, Flags.IsSigned);
1002cf9daa33SAmara Emerson       };
1003cf9daa33SAmara Emerson     }
1004cf9daa33SAmara Emerson     break;
1005cf9daa33SAmara Emerson   case Instruction::FCmp:
1006cf9daa33SAmara Emerson     if (Flags.IsMaxOp) {
1007cf9daa33SAmara Emerson       MinMaxKind = RD::MRK_FloatMax;
1008cf9daa33SAmara Emerson       BuildFunc = [&]() { return Builder.CreateFPMaxReduce(Src, Flags.NoNaN); };
1009cf9daa33SAmara Emerson     } else {
1010cf9daa33SAmara Emerson       MinMaxKind = RD::MRK_FloatMin;
1011cf9daa33SAmara Emerson       BuildFunc = [&]() { return Builder.CreateFPMinReduce(Src, Flags.NoNaN); };
1012cf9daa33SAmara Emerson     }
1013cf9daa33SAmara Emerson     break;
1014cf9daa33SAmara Emerson   default:
1015cf9daa33SAmara Emerson     llvm_unreachable("Unhandled opcode");
1016cf9daa33SAmara Emerson     break;
1017cf9daa33SAmara Emerson   }
1018cf9daa33SAmara Emerson   if (TTI->useReductionIntrinsic(Opcode, Src->getType(), Flags))
1019cf9daa33SAmara Emerson     return BuildFunc();
1020ad62a3a2SSanjay Patel   return getShuffleReduction(Builder, Src, Opcode, MinMaxKind, RedOps);
1021cf9daa33SAmara Emerson }
1022cf9daa33SAmara Emerson 
1023cf9daa33SAmara Emerson /// Create a vector reduction using a given recurrence descriptor.
10243e069f57SSanjay Patel Value *llvm::createTargetReduction(IRBuilder<> &B,
1025cf9daa33SAmara Emerson                                    const TargetTransformInfo *TTI,
1026cf9daa33SAmara Emerson                                    RecurrenceDescriptor &Desc, Value *Src,
1027cf9daa33SAmara Emerson                                    bool NoNaN) {
1028cf9daa33SAmara Emerson   // TODO: Support in-order reductions based on the recurrence descriptor.
10293e069f57SSanjay Patel   using RD = RecurrenceDescriptor;
10303e069f57SSanjay Patel   RD::RecurrenceKind RecKind = Desc.getRecurrenceKind();
1031cf9daa33SAmara Emerson   TargetTransformInfo::ReductionFlags Flags;
1032cf9daa33SAmara Emerson   Flags.NoNaN = NoNaN;
1033ad62a3a2SSanjay Patel 
1034ad62a3a2SSanjay Patel   // All ops in the reduction inherit fast-math-flags from the recurrence
1035ad62a3a2SSanjay Patel   // descriptor.
1036ad62a3a2SSanjay Patel   IRBuilder<>::FastMathFlagGuard FMFGuard(B);
1037ad62a3a2SSanjay Patel   B.setFastMathFlags(Desc.getFastMathFlags());
1038ad62a3a2SSanjay Patel 
1039cf9daa33SAmara Emerson   switch (RecKind) {
10403e069f57SSanjay Patel   case RD::RK_FloatAdd:
1041ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FAdd, Src, Flags);
10423e069f57SSanjay Patel   case RD::RK_FloatMult:
1043ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FMul, Src, Flags);
10443e069f57SSanjay Patel   case RD::RK_IntegerAdd:
1045ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Add, Src, Flags);
10463e069f57SSanjay Patel   case RD::RK_IntegerMult:
1047ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Mul, Src, Flags);
10483e069f57SSanjay Patel   case RD::RK_IntegerAnd:
1049ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::And, Src, Flags);
10503e069f57SSanjay Patel   case RD::RK_IntegerOr:
1051ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Or, Src, Flags);
10523e069f57SSanjay Patel   case RD::RK_IntegerXor:
1053ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Xor, Src, Flags);
10543e069f57SSanjay Patel   case RD::RK_IntegerMinMax: {
10553e069f57SSanjay Patel     RD::MinMaxRecurrenceKind MMKind = Desc.getMinMaxRecurrenceKind();
10563e069f57SSanjay Patel     Flags.IsMaxOp = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_UIntMax);
10573e069f57SSanjay Patel     Flags.IsSigned = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_SIntMin);
1058ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::ICmp, Src, Flags);
1059cf9daa33SAmara Emerson   }
10603e069f57SSanjay Patel   case RD::RK_FloatMinMax: {
10613e069f57SSanjay Patel     Flags.IsMaxOp = Desc.getMinMaxRecurrenceKind() == RD::MRK_FloatMax;
1062ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FCmp, Src, Flags);
1063cf9daa33SAmara Emerson   }
1064cf9daa33SAmara Emerson   default:
1065cf9daa33SAmara Emerson     llvm_unreachable("Unhandled RecKind");
1066cf9daa33SAmara Emerson   }
1067cf9daa33SAmara Emerson }
1068cf9daa33SAmara Emerson 
1069a61f4b89SDinar Temirbulatov void llvm::propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue) {
1070a61f4b89SDinar Temirbulatov   auto *VecOp = dyn_cast<Instruction>(I);
1071a61f4b89SDinar Temirbulatov   if (!VecOp)
1072a61f4b89SDinar Temirbulatov     return;
1073a61f4b89SDinar Temirbulatov   auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0])
1074a61f4b89SDinar Temirbulatov                                             : dyn_cast<Instruction>(OpValue);
1075a61f4b89SDinar Temirbulatov   if (!Intersection)
1076a61f4b89SDinar Temirbulatov     return;
1077a61f4b89SDinar Temirbulatov   const unsigned Opcode = Intersection->getOpcode();
1078a61f4b89SDinar Temirbulatov   VecOp->copyIRFlags(Intersection);
1079a61f4b89SDinar Temirbulatov   for (auto *V : VL) {
1080a61f4b89SDinar Temirbulatov     auto *Instr = dyn_cast<Instruction>(V);
1081a61f4b89SDinar Temirbulatov     if (!Instr)
1082a61f4b89SDinar Temirbulatov       continue;
1083a61f4b89SDinar Temirbulatov     if (OpValue == nullptr || Opcode == Instr->getOpcode())
1084a61f4b89SDinar Temirbulatov       VecOp->andIRFlags(V);
1085cf9daa33SAmara Emerson   }
1086cf9daa33SAmara Emerson }
1087a78dc4d6SMax Kazantsev 
1088a78dc4d6SMax Kazantsev bool llvm::isKnownNegativeInLoop(const SCEV *S, const Loop *L,
1089a78dc4d6SMax Kazantsev                                  ScalarEvolution &SE) {
1090a78dc4d6SMax Kazantsev   const SCEV *Zero = SE.getZero(S->getType());
1091a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
1092a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, S, Zero);
1093a78dc4d6SMax Kazantsev }
1094a78dc4d6SMax Kazantsev 
1095a78dc4d6SMax Kazantsev bool llvm::isKnownNonNegativeInLoop(const SCEV *S, const Loop *L,
1096a78dc4d6SMax Kazantsev                                     ScalarEvolution &SE) {
1097a78dc4d6SMax Kazantsev   const SCEV *Zero = SE.getZero(S->getType());
1098a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
1099a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, S, Zero);
1100a78dc4d6SMax Kazantsev }
1101a78dc4d6SMax Kazantsev 
1102a78dc4d6SMax Kazantsev bool llvm::cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
1103a78dc4d6SMax Kazantsev                              bool Signed) {
1104a78dc4d6SMax Kazantsev   unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1105a78dc4d6SMax Kazantsev   APInt Min = Signed ? APInt::getSignedMinValue(BitWidth) :
1106a78dc4d6SMax Kazantsev     APInt::getMinValue(BitWidth);
1107a78dc4d6SMax Kazantsev   auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1108a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
1109a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, Predicate, S,
1110a78dc4d6SMax Kazantsev                                      SE.getConstant(Min));
1111a78dc4d6SMax Kazantsev }
1112a78dc4d6SMax Kazantsev 
1113a78dc4d6SMax Kazantsev bool llvm::cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
1114a78dc4d6SMax Kazantsev                              bool Signed) {
1115a78dc4d6SMax Kazantsev   unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1116a78dc4d6SMax Kazantsev   APInt Max = Signed ? APInt::getSignedMaxValue(BitWidth) :
1117a78dc4d6SMax Kazantsev     APInt::getMaxValue(BitWidth);
1118a78dc4d6SMax Kazantsev   auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1119a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
1120a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, Predicate, S,
1121a78dc4d6SMax Kazantsev                                      SE.getConstant(Max));
1122a78dc4d6SMax Kazantsev }
112393175a5cSSjoerd Meijer 
112493175a5cSSjoerd Meijer //===----------------------------------------------------------------------===//
112593175a5cSSjoerd Meijer // rewriteLoopExitValues - Optimize IV users outside the loop.
112693175a5cSSjoerd Meijer // As a side effect, reduces the amount of IV processing within the loop.
112793175a5cSSjoerd Meijer //===----------------------------------------------------------------------===//
112893175a5cSSjoerd Meijer 
112993175a5cSSjoerd Meijer // Return true if the SCEV expansion generated by the rewriter can replace the
113093175a5cSSjoerd Meijer // original value. SCEV guarantees that it produces the same value, but the way
113193175a5cSSjoerd Meijer // it is produced may be illegal IR.  Ideally, this function will only be
113293175a5cSSjoerd Meijer // called for verification.
113393175a5cSSjoerd Meijer static bool isValidRewrite(ScalarEvolution *SE, Value *FromVal, Value *ToVal) {
113493175a5cSSjoerd Meijer   // If an SCEV expression subsumed multiple pointers, its expansion could
113593175a5cSSjoerd Meijer   // reassociate the GEP changing the base pointer. This is illegal because the
113693175a5cSSjoerd Meijer   // final address produced by a GEP chain must be inbounds relative to its
113793175a5cSSjoerd Meijer   // underlying object. Otherwise basic alias analysis, among other things,
113893175a5cSSjoerd Meijer   // could fail in a dangerous way. Ultimately, SCEV will be improved to avoid
113993175a5cSSjoerd Meijer   // producing an expression involving multiple pointers. Until then, we must
114093175a5cSSjoerd Meijer   // bail out here.
114193175a5cSSjoerd Meijer   //
114293175a5cSSjoerd Meijer   // Retrieve the pointer operand of the GEP. Don't use GetUnderlyingObject
114393175a5cSSjoerd Meijer   // because it understands lcssa phis while SCEV does not.
114493175a5cSSjoerd Meijer   Value *FromPtr = FromVal;
114593175a5cSSjoerd Meijer   Value *ToPtr = ToVal;
114693175a5cSSjoerd Meijer   if (auto *GEP = dyn_cast<GEPOperator>(FromVal))
114793175a5cSSjoerd Meijer     FromPtr = GEP->getPointerOperand();
114893175a5cSSjoerd Meijer 
114993175a5cSSjoerd Meijer   if (auto *GEP = dyn_cast<GEPOperator>(ToVal))
115093175a5cSSjoerd Meijer     ToPtr = GEP->getPointerOperand();
115193175a5cSSjoerd Meijer 
115293175a5cSSjoerd Meijer   if (FromPtr != FromVal || ToPtr != ToVal) {
115393175a5cSSjoerd Meijer     // Quickly check the common case
115493175a5cSSjoerd Meijer     if (FromPtr == ToPtr)
115593175a5cSSjoerd Meijer       return true;
115693175a5cSSjoerd Meijer 
115793175a5cSSjoerd Meijer     // SCEV may have rewritten an expression that produces the GEP's pointer
115893175a5cSSjoerd Meijer     // operand. That's ok as long as the pointer operand has the same base
115993175a5cSSjoerd Meijer     // pointer. Unlike GetUnderlyingObject(), getPointerBase() will find the
116093175a5cSSjoerd Meijer     // base of a recurrence. This handles the case in which SCEV expansion
116193175a5cSSjoerd Meijer     // converts a pointer type recurrence into a nonrecurrent pointer base
116293175a5cSSjoerd Meijer     // indexed by an integer recurrence.
116393175a5cSSjoerd Meijer 
116493175a5cSSjoerd Meijer     // If the GEP base pointer is a vector of pointers, abort.
116593175a5cSSjoerd Meijer     if (!FromPtr->getType()->isPointerTy() || !ToPtr->getType()->isPointerTy())
116693175a5cSSjoerd Meijer       return false;
116793175a5cSSjoerd Meijer 
116893175a5cSSjoerd Meijer     const SCEV *FromBase = SE->getPointerBase(SE->getSCEV(FromPtr));
116993175a5cSSjoerd Meijer     const SCEV *ToBase = SE->getPointerBase(SE->getSCEV(ToPtr));
117093175a5cSSjoerd Meijer     if (FromBase == ToBase)
117193175a5cSSjoerd Meijer       return true;
117293175a5cSSjoerd Meijer 
117393175a5cSSjoerd Meijer     LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: GEP rewrite bail out "
117493175a5cSSjoerd Meijer                       << *FromBase << " != " << *ToBase << "\n");
117593175a5cSSjoerd Meijer 
117693175a5cSSjoerd Meijer     return false;
117793175a5cSSjoerd Meijer   }
117893175a5cSSjoerd Meijer   return true;
117993175a5cSSjoerd Meijer }
118093175a5cSSjoerd Meijer 
118193175a5cSSjoerd Meijer static bool hasHardUserWithinLoop(const Loop *L, const Instruction *I) {
118293175a5cSSjoerd Meijer   SmallPtrSet<const Instruction *, 8> Visited;
118393175a5cSSjoerd Meijer   SmallVector<const Instruction *, 8> WorkList;
118493175a5cSSjoerd Meijer   Visited.insert(I);
118593175a5cSSjoerd Meijer   WorkList.push_back(I);
118693175a5cSSjoerd Meijer   while (!WorkList.empty()) {
118793175a5cSSjoerd Meijer     const Instruction *Curr = WorkList.pop_back_val();
118893175a5cSSjoerd Meijer     // This use is outside the loop, nothing to do.
118993175a5cSSjoerd Meijer     if (!L->contains(Curr))
119093175a5cSSjoerd Meijer       continue;
119193175a5cSSjoerd Meijer     // Do we assume it is a "hard" use which will not be eliminated easily?
119293175a5cSSjoerd Meijer     if (Curr->mayHaveSideEffects())
119393175a5cSSjoerd Meijer       return true;
119493175a5cSSjoerd Meijer     // Otherwise, add all its users to worklist.
119593175a5cSSjoerd Meijer     for (auto U : Curr->users()) {
119693175a5cSSjoerd Meijer       auto *UI = cast<Instruction>(U);
119793175a5cSSjoerd Meijer       if (Visited.insert(UI).second)
119893175a5cSSjoerd Meijer         WorkList.push_back(UI);
119993175a5cSSjoerd Meijer     }
120093175a5cSSjoerd Meijer   }
120193175a5cSSjoerd Meijer   return false;
120293175a5cSSjoerd Meijer }
120393175a5cSSjoerd Meijer 
120493175a5cSSjoerd Meijer // Collect information about PHI nodes which can be transformed in
120593175a5cSSjoerd Meijer // rewriteLoopExitValues.
120693175a5cSSjoerd Meijer struct RewritePhi {
120793175a5cSSjoerd Meijer   PHINode *PN;
120893175a5cSSjoerd Meijer   unsigned Ith;   // Ith incoming value.
120993175a5cSSjoerd Meijer   Value *Val;     // Exit value after expansion.
121093175a5cSSjoerd Meijer   bool HighCost;  // High Cost when expansion.
121193175a5cSSjoerd Meijer 
121293175a5cSSjoerd Meijer   RewritePhi(PHINode *P, unsigned I, Value *V, bool H)
121393175a5cSSjoerd Meijer       : PN(P), Ith(I), Val(V), HighCost(H) {}
121493175a5cSSjoerd Meijer };
121593175a5cSSjoerd Meijer 
121693175a5cSSjoerd Meijer // Check whether it is possible to delete the loop after rewriting exit
121793175a5cSSjoerd Meijer // value. If it is possible, ignore ReplaceExitValue and do rewriting
121893175a5cSSjoerd Meijer // aggressively.
121993175a5cSSjoerd Meijer static bool canLoopBeDeleted(Loop *L, SmallVector<RewritePhi, 8> &RewritePhiSet) {
122093175a5cSSjoerd Meijer   BasicBlock *Preheader = L->getLoopPreheader();
122193175a5cSSjoerd Meijer   // If there is no preheader, the loop will not be deleted.
122293175a5cSSjoerd Meijer   if (!Preheader)
122393175a5cSSjoerd Meijer     return false;
122493175a5cSSjoerd Meijer 
122593175a5cSSjoerd Meijer   // In LoopDeletion pass Loop can be deleted when ExitingBlocks.size() > 1.
122693175a5cSSjoerd Meijer   // We obviate multiple ExitingBlocks case for simplicity.
122793175a5cSSjoerd Meijer   // TODO: If we see testcase with multiple ExitingBlocks can be deleted
122893175a5cSSjoerd Meijer   // after exit value rewriting, we can enhance the logic here.
122993175a5cSSjoerd Meijer   SmallVector<BasicBlock *, 4> ExitingBlocks;
123093175a5cSSjoerd Meijer   L->getExitingBlocks(ExitingBlocks);
123193175a5cSSjoerd Meijer   SmallVector<BasicBlock *, 8> ExitBlocks;
123293175a5cSSjoerd Meijer   L->getUniqueExitBlocks(ExitBlocks);
123393175a5cSSjoerd Meijer   if (ExitBlocks.size() != 1 || ExitingBlocks.size() != 1)
123493175a5cSSjoerd Meijer     return false;
123593175a5cSSjoerd Meijer 
123693175a5cSSjoerd Meijer   BasicBlock *ExitBlock = ExitBlocks[0];
123793175a5cSSjoerd Meijer   BasicBlock::iterator BI = ExitBlock->begin();
123893175a5cSSjoerd Meijer   while (PHINode *P = dyn_cast<PHINode>(BI)) {
123993175a5cSSjoerd Meijer     Value *Incoming = P->getIncomingValueForBlock(ExitingBlocks[0]);
124093175a5cSSjoerd Meijer 
124193175a5cSSjoerd Meijer     // If the Incoming value of P is found in RewritePhiSet, we know it
124293175a5cSSjoerd Meijer     // could be rewritten to use a loop invariant value in transformation
124393175a5cSSjoerd Meijer     // phase later. Skip it in the loop invariant check below.
124493175a5cSSjoerd Meijer     bool found = false;
124593175a5cSSjoerd Meijer     for (const RewritePhi &Phi : RewritePhiSet) {
124693175a5cSSjoerd Meijer       unsigned i = Phi.Ith;
124793175a5cSSjoerd Meijer       if (Phi.PN == P && (Phi.PN)->getIncomingValue(i) == Incoming) {
124893175a5cSSjoerd Meijer         found = true;
124993175a5cSSjoerd Meijer         break;
125093175a5cSSjoerd Meijer       }
125193175a5cSSjoerd Meijer     }
125293175a5cSSjoerd Meijer 
125393175a5cSSjoerd Meijer     Instruction *I;
125493175a5cSSjoerd Meijer     if (!found && (I = dyn_cast<Instruction>(Incoming)))
125593175a5cSSjoerd Meijer       if (!L->hasLoopInvariantOperands(I))
125693175a5cSSjoerd Meijer         return false;
125793175a5cSSjoerd Meijer 
125893175a5cSSjoerd Meijer     ++BI;
125993175a5cSSjoerd Meijer   }
126093175a5cSSjoerd Meijer 
126193175a5cSSjoerd Meijer   for (auto *BB : L->blocks())
126293175a5cSSjoerd Meijer     if (llvm::any_of(*BB, [](Instruction &I) {
126393175a5cSSjoerd Meijer           return I.mayHaveSideEffects();
126493175a5cSSjoerd Meijer         }))
126593175a5cSSjoerd Meijer       return false;
126693175a5cSSjoerd Meijer 
126793175a5cSSjoerd Meijer   return true;
126893175a5cSSjoerd Meijer }
126993175a5cSSjoerd Meijer 
127093175a5cSSjoerd Meijer int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI,
127193175a5cSSjoerd Meijer     TargetLibraryInfo *TLI, ScalarEvolution *SE, SCEVExpander &Rewriter,
127293175a5cSSjoerd Meijer     DominatorTree *DT, ReplaceExitVal ReplaceExitValue,
127393175a5cSSjoerd Meijer     SmallVector<WeakTrackingVH, 16> &DeadInsts) {
127493175a5cSSjoerd Meijer   // Check a pre-condition.
127593175a5cSSjoerd Meijer   assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
127693175a5cSSjoerd Meijer          "Indvars did not preserve LCSSA!");
127793175a5cSSjoerd Meijer 
127893175a5cSSjoerd Meijer   SmallVector<BasicBlock*, 8> ExitBlocks;
127993175a5cSSjoerd Meijer   L->getUniqueExitBlocks(ExitBlocks);
128093175a5cSSjoerd Meijer 
128193175a5cSSjoerd Meijer   SmallVector<RewritePhi, 8> RewritePhiSet;
128293175a5cSSjoerd Meijer   // Find all values that are computed inside the loop, but used outside of it.
128393175a5cSSjoerd Meijer   // Because of LCSSA, these values will only occur in LCSSA PHI Nodes.  Scan
128493175a5cSSjoerd Meijer   // the exit blocks of the loop to find them.
128593175a5cSSjoerd Meijer   for (BasicBlock *ExitBB : ExitBlocks) {
128693175a5cSSjoerd Meijer     // If there are no PHI nodes in this exit block, then no values defined
128793175a5cSSjoerd Meijer     // inside the loop are used on this path, skip it.
128893175a5cSSjoerd Meijer     PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
128993175a5cSSjoerd Meijer     if (!PN) continue;
129093175a5cSSjoerd Meijer 
129193175a5cSSjoerd Meijer     unsigned NumPreds = PN->getNumIncomingValues();
129293175a5cSSjoerd Meijer 
129393175a5cSSjoerd Meijer     // Iterate over all of the PHI nodes.
129493175a5cSSjoerd Meijer     BasicBlock::iterator BBI = ExitBB->begin();
129593175a5cSSjoerd Meijer     while ((PN = dyn_cast<PHINode>(BBI++))) {
129693175a5cSSjoerd Meijer       if (PN->use_empty())
129793175a5cSSjoerd Meijer         continue; // dead use, don't replace it
129893175a5cSSjoerd Meijer 
129993175a5cSSjoerd Meijer       if (!SE->isSCEVable(PN->getType()))
130093175a5cSSjoerd Meijer         continue;
130193175a5cSSjoerd Meijer 
130293175a5cSSjoerd Meijer       // It's necessary to tell ScalarEvolution about this explicitly so that
130393175a5cSSjoerd Meijer       // it can walk the def-use list and forget all SCEVs, as it may not be
130493175a5cSSjoerd Meijer       // watching the PHI itself. Once the new exit value is in place, there
130593175a5cSSjoerd Meijer       // may not be a def-use connection between the loop and every instruction
130693175a5cSSjoerd Meijer       // which got a SCEVAddRecExpr for that loop.
130793175a5cSSjoerd Meijer       SE->forgetValue(PN);
130893175a5cSSjoerd Meijer 
130993175a5cSSjoerd Meijer       // Iterate over all of the values in all the PHI nodes.
131093175a5cSSjoerd Meijer       for (unsigned i = 0; i != NumPreds; ++i) {
131193175a5cSSjoerd Meijer         // If the value being merged in is not integer or is not defined
131293175a5cSSjoerd Meijer         // in the loop, skip it.
131393175a5cSSjoerd Meijer         Value *InVal = PN->getIncomingValue(i);
131493175a5cSSjoerd Meijer         if (!isa<Instruction>(InVal))
131593175a5cSSjoerd Meijer           continue;
131693175a5cSSjoerd Meijer 
131793175a5cSSjoerd Meijer         // If this pred is for a subloop, not L itself, skip it.
131893175a5cSSjoerd Meijer         if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
131993175a5cSSjoerd Meijer           continue; // The Block is in a subloop, skip it.
132093175a5cSSjoerd Meijer 
132193175a5cSSjoerd Meijer         // Check that InVal is defined in the loop.
132293175a5cSSjoerd Meijer         Instruction *Inst = cast<Instruction>(InVal);
132393175a5cSSjoerd Meijer         if (!L->contains(Inst))
132493175a5cSSjoerd Meijer           continue;
132593175a5cSSjoerd Meijer 
132693175a5cSSjoerd Meijer         // Okay, this instruction has a user outside of the current loop
132793175a5cSSjoerd Meijer         // and varies predictably *inside* the loop.  Evaluate the value it
132893175a5cSSjoerd Meijer         // contains when the loop exits, if possible.  We prefer to start with
132993175a5cSSjoerd Meijer         // expressions which are true for all exits (so as to maximize
133093175a5cSSjoerd Meijer         // expression reuse by the SCEVExpander), but resort to per-exit
133193175a5cSSjoerd Meijer         // evaluation if that fails.
133293175a5cSSjoerd Meijer         const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
133393175a5cSSjoerd Meijer         if (isa<SCEVCouldNotCompute>(ExitValue) ||
133493175a5cSSjoerd Meijer             !SE->isLoopInvariant(ExitValue, L) ||
133593175a5cSSjoerd Meijer             !isSafeToExpand(ExitValue, *SE)) {
133693175a5cSSjoerd Meijer           // TODO: This should probably be sunk into SCEV in some way; maybe a
133793175a5cSSjoerd Meijer           // getSCEVForExit(SCEV*, L, ExitingBB)?  It can be generalized for
133893175a5cSSjoerd Meijer           // most SCEV expressions and other recurrence types (e.g. shift
133993175a5cSSjoerd Meijer           // recurrences).  Is there existing code we can reuse?
134093175a5cSSjoerd Meijer           const SCEV *ExitCount = SE->getExitCount(L, PN->getIncomingBlock(i));
134193175a5cSSjoerd Meijer           if (isa<SCEVCouldNotCompute>(ExitCount))
134293175a5cSSjoerd Meijer             continue;
134393175a5cSSjoerd Meijer           if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Inst)))
134493175a5cSSjoerd Meijer             if (AddRec->getLoop() == L)
134593175a5cSSjoerd Meijer               ExitValue = AddRec->evaluateAtIteration(ExitCount, *SE);
134693175a5cSSjoerd Meijer           if (isa<SCEVCouldNotCompute>(ExitValue) ||
134793175a5cSSjoerd Meijer               !SE->isLoopInvariant(ExitValue, L) ||
134893175a5cSSjoerd Meijer               !isSafeToExpand(ExitValue, *SE))
134993175a5cSSjoerd Meijer             continue;
135093175a5cSSjoerd Meijer         }
135193175a5cSSjoerd Meijer 
135293175a5cSSjoerd Meijer         // Computing the value outside of the loop brings no benefit if it is
135393175a5cSSjoerd Meijer         // definitely used inside the loop in a way which can not be optimized
135493175a5cSSjoerd Meijer         // away. Avoid doing so unless we know we have a value which computes
135593175a5cSSjoerd Meijer         // the ExitValue already. TODO: This should be merged into SCEV
135693175a5cSSjoerd Meijer         // expander to leverage its knowledge of existing expressions.
135793175a5cSSjoerd Meijer         if (ReplaceExitValue != AlwaysRepl &&
135893175a5cSSjoerd Meijer             !isa<SCEVConstant>(ExitValue) && !isa<SCEVUnknown>(ExitValue) &&
135993175a5cSSjoerd Meijer             hasHardUserWithinLoop(L, Inst))
136093175a5cSSjoerd Meijer           continue;
136193175a5cSSjoerd Meijer 
136293175a5cSSjoerd Meijer         bool HighCost = Rewriter.isHighCostExpansion(ExitValue, L, Inst);
136393175a5cSSjoerd Meijer         Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst);
136493175a5cSSjoerd Meijer 
136593175a5cSSjoerd Meijer         LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: AfterLoopVal = "
136693175a5cSSjoerd Meijer                           << *ExitVal << '\n' << "  LoopVal = " << *Inst
136793175a5cSSjoerd Meijer                           << "\n");
136893175a5cSSjoerd Meijer 
136993175a5cSSjoerd Meijer         if (!isValidRewrite(SE, Inst, ExitVal)) {
137093175a5cSSjoerd Meijer           DeadInsts.push_back(ExitVal);
137193175a5cSSjoerd Meijer           continue;
137293175a5cSSjoerd Meijer         }
137393175a5cSSjoerd Meijer 
137493175a5cSSjoerd Meijer #ifndef NDEBUG
137593175a5cSSjoerd Meijer         // If we reuse an instruction from a loop which is neither L nor one of
137693175a5cSSjoerd Meijer         // its containing loops, we end up breaking LCSSA form for this loop by
137793175a5cSSjoerd Meijer         // creating a new use of its instruction.
137893175a5cSSjoerd Meijer         if (auto *ExitInsn = dyn_cast<Instruction>(ExitVal))
137993175a5cSSjoerd Meijer           if (auto *EVL = LI->getLoopFor(ExitInsn->getParent()))
138093175a5cSSjoerd Meijer             if (EVL != L)
138193175a5cSSjoerd Meijer               assert(EVL->contains(L) && "LCSSA breach detected!");
138293175a5cSSjoerd Meijer #endif
138393175a5cSSjoerd Meijer 
138493175a5cSSjoerd Meijer         // Collect all the candidate PHINodes to be rewritten.
138593175a5cSSjoerd Meijer         RewritePhiSet.emplace_back(PN, i, ExitVal, HighCost);
138693175a5cSSjoerd Meijer       }
138793175a5cSSjoerd Meijer     }
138893175a5cSSjoerd Meijer   }
138993175a5cSSjoerd Meijer 
139093175a5cSSjoerd Meijer   bool LoopCanBeDel = canLoopBeDeleted(L, RewritePhiSet);
139193175a5cSSjoerd Meijer   int NumReplaced = 0;
139293175a5cSSjoerd Meijer 
139393175a5cSSjoerd Meijer   // Transformation.
139493175a5cSSjoerd Meijer   for (const RewritePhi &Phi : RewritePhiSet) {
139593175a5cSSjoerd Meijer     PHINode *PN = Phi.PN;
139693175a5cSSjoerd Meijer     Value *ExitVal = Phi.Val;
139793175a5cSSjoerd Meijer 
139893175a5cSSjoerd Meijer     // Only do the rewrite when the ExitValue can be expanded cheaply.
139993175a5cSSjoerd Meijer     // If LoopCanBeDel is true, rewrite exit value aggressively.
140093175a5cSSjoerd Meijer     if (ReplaceExitValue == OnlyCheapRepl && !LoopCanBeDel && Phi.HighCost) {
140193175a5cSSjoerd Meijer       DeadInsts.push_back(ExitVal);
140293175a5cSSjoerd Meijer       continue;
140393175a5cSSjoerd Meijer     }
140493175a5cSSjoerd Meijer 
140593175a5cSSjoerd Meijer     NumReplaced++;
140693175a5cSSjoerd Meijer     Instruction *Inst = cast<Instruction>(PN->getIncomingValue(Phi.Ith));
140793175a5cSSjoerd Meijer     PN->setIncomingValue(Phi.Ith, ExitVal);
140893175a5cSSjoerd Meijer 
140993175a5cSSjoerd Meijer     // If this instruction is dead now, delete it. Don't do it now to avoid
141093175a5cSSjoerd Meijer     // invalidating iterators.
141193175a5cSSjoerd Meijer     if (isInstructionTriviallyDead(Inst, TLI))
141293175a5cSSjoerd Meijer       DeadInsts.push_back(Inst);
141393175a5cSSjoerd Meijer 
141493175a5cSSjoerd Meijer     // Replace PN with ExitVal if that is legal and does not break LCSSA.
141593175a5cSSjoerd Meijer     if (PN->getNumIncomingValues() == 1 &&
141693175a5cSSjoerd Meijer         LI->replacementPreservesLCSSAForm(PN, ExitVal)) {
141793175a5cSSjoerd Meijer       PN->replaceAllUsesWith(ExitVal);
141893175a5cSSjoerd Meijer       PN->eraseFromParent();
141993175a5cSSjoerd Meijer     }
142093175a5cSSjoerd Meijer   }
142193175a5cSSjoerd Meijer 
142293175a5cSSjoerd Meijer   // The insertion point instruction may have been deleted; clear it out
142393175a5cSSjoerd Meijer   // so that the rewriter doesn't trip over it later.
142493175a5cSSjoerd Meijer   Rewriter.clearInsertPoint();
142593175a5cSSjoerd Meijer   return NumReplaced;
142693175a5cSSjoerd Meijer }
1427af7e1588SEvgeniy Brevnov 
1428af7e1588SEvgeniy Brevnov /// Set weights for \p UnrolledLoop and \p RemainderLoop based on weights for
1429af7e1588SEvgeniy Brevnov /// \p OrigLoop.
1430af7e1588SEvgeniy Brevnov void llvm::setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop,
1431af7e1588SEvgeniy Brevnov                                         Loop *RemainderLoop, uint64_t UF) {
1432af7e1588SEvgeniy Brevnov   assert(UF > 0 && "Zero unrolled factor is not supported");
1433af7e1588SEvgeniy Brevnov   assert(UnrolledLoop != RemainderLoop &&
1434af7e1588SEvgeniy Brevnov          "Unrolled and Remainder loops are expected to distinct");
1435af7e1588SEvgeniy Brevnov 
1436af7e1588SEvgeniy Brevnov   // Get number of iterations in the original scalar loop.
1437af7e1588SEvgeniy Brevnov   unsigned OrigLoopInvocationWeight = 0;
1438af7e1588SEvgeniy Brevnov   Optional<unsigned> OrigAverageTripCount =
1439af7e1588SEvgeniy Brevnov       getLoopEstimatedTripCount(OrigLoop, &OrigLoopInvocationWeight);
1440af7e1588SEvgeniy Brevnov   if (!OrigAverageTripCount)
1441af7e1588SEvgeniy Brevnov     return;
1442af7e1588SEvgeniy Brevnov 
1443af7e1588SEvgeniy Brevnov   // Calculate number of iterations in unrolled loop.
1444af7e1588SEvgeniy Brevnov   unsigned UnrolledAverageTripCount = *OrigAverageTripCount / UF;
1445af7e1588SEvgeniy Brevnov   // Calculate number of iterations for remainder loop.
1446af7e1588SEvgeniy Brevnov   unsigned RemainderAverageTripCount = *OrigAverageTripCount % UF;
1447af7e1588SEvgeniy Brevnov 
1448af7e1588SEvgeniy Brevnov   setLoopEstimatedTripCount(UnrolledLoop, UnrolledAverageTripCount,
1449af7e1588SEvgeniy Brevnov                             OrigLoopInvocationWeight);
1450af7e1588SEvgeniy Brevnov   setLoopEstimatedTripCount(RemainderLoop, RemainderAverageTripCount,
1451af7e1588SEvgeniy Brevnov                             OrigLoopInvocationWeight);
1452af7e1588SEvgeniy Brevnov }
1453388de9dfSAlina Sbirlea 
1454388de9dfSAlina Sbirlea /// Utility that implements appending of loops onto a worklist.
1455388de9dfSAlina Sbirlea /// Loops are added in preorder (analogous for reverse postorder for trees),
1456388de9dfSAlina Sbirlea /// and the worklist is processed LIFO.
1457388de9dfSAlina Sbirlea template <typename RangeT>
1458388de9dfSAlina Sbirlea void llvm::appendReversedLoopsToWorklist(
1459388de9dfSAlina Sbirlea     RangeT &&Loops, SmallPriorityWorklist<Loop *, 4> &Worklist) {
1460388de9dfSAlina Sbirlea   // We use an internal worklist to build up the preorder traversal without
1461388de9dfSAlina Sbirlea   // recursion.
1462388de9dfSAlina Sbirlea   SmallVector<Loop *, 4> PreOrderLoops, PreOrderWorklist;
1463388de9dfSAlina Sbirlea 
1464388de9dfSAlina Sbirlea   // We walk the initial sequence of loops in reverse because we generally want
1465388de9dfSAlina Sbirlea   // to visit defs before uses and the worklist is LIFO.
1466388de9dfSAlina Sbirlea   for (Loop *RootL : Loops) {
1467388de9dfSAlina Sbirlea     assert(PreOrderLoops.empty() && "Must start with an empty preorder walk.");
1468388de9dfSAlina Sbirlea     assert(PreOrderWorklist.empty() &&
1469388de9dfSAlina Sbirlea            "Must start with an empty preorder walk worklist.");
1470388de9dfSAlina Sbirlea     PreOrderWorklist.push_back(RootL);
1471388de9dfSAlina Sbirlea     do {
1472388de9dfSAlina Sbirlea       Loop *L = PreOrderWorklist.pop_back_val();
1473388de9dfSAlina Sbirlea       PreOrderWorklist.append(L->begin(), L->end());
1474388de9dfSAlina Sbirlea       PreOrderLoops.push_back(L);
1475388de9dfSAlina Sbirlea     } while (!PreOrderWorklist.empty());
1476388de9dfSAlina Sbirlea 
1477388de9dfSAlina Sbirlea     Worklist.insert(std::move(PreOrderLoops));
1478388de9dfSAlina Sbirlea     PreOrderLoops.clear();
1479388de9dfSAlina Sbirlea   }
1480388de9dfSAlina Sbirlea }
1481388de9dfSAlina Sbirlea 
1482388de9dfSAlina Sbirlea template <typename RangeT>
1483388de9dfSAlina Sbirlea void llvm::appendLoopsToWorklist(RangeT &&Loops,
1484388de9dfSAlina Sbirlea                                  SmallPriorityWorklist<Loop *, 4> &Worklist) {
1485388de9dfSAlina Sbirlea   appendReversedLoopsToWorklist(reverse(Loops), Worklist);
1486388de9dfSAlina Sbirlea }
1487388de9dfSAlina Sbirlea 
1488388de9dfSAlina Sbirlea template void llvm::appendLoopsToWorklist<ArrayRef<Loop *> &>(
1489388de9dfSAlina Sbirlea     ArrayRef<Loop *> &Loops, SmallPriorityWorklist<Loop *, 4> &Worklist);
1490388de9dfSAlina Sbirlea 
1491*67904db2SAlina Sbirlea template void
1492*67904db2SAlina Sbirlea llvm::appendLoopsToWorklist<Loop &>(Loop &L,
1493*67904db2SAlina Sbirlea                                     SmallPriorityWorklist<Loop *, 4> &Worklist);
1494*67904db2SAlina Sbirlea 
1495388de9dfSAlina Sbirlea void llvm::appendLoopsToWorklist(LoopInfo &LI,
1496388de9dfSAlina Sbirlea                                  SmallPriorityWorklist<Loop *, 4> &Worklist) {
1497388de9dfSAlina Sbirlea   appendReversedLoopsToWorklist(LI, Worklist);
1498388de9dfSAlina Sbirlea }
1499