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 
49ec7e4a9aSDavid Green static cl::opt<bool> ForceReductionIntrinsic(
50ec7e4a9aSDavid Green     "force-reduction-intrinsics", cl::Hidden,
51ec7e4a9aSDavid Green     cl::desc("Force creating reduction intrinsics for testing."),
52ec7e4a9aSDavid Green     cl::init(false));
53ec7e4a9aSDavid Green 
5476aa662cSKarthik Bhat #define DEBUG_TYPE "loop-utils"
5576aa662cSKarthik Bhat 
5672448525SMichael Kruse static const char *LLVMLoopDisableNonforced = "llvm.loop.disable_nonforced";
574f64f1baSTim Corringham static const char *LLVMLoopDisableLICM = "llvm.licm.disable";
5872448525SMichael Kruse 
594a000883SChandler Carruth bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
6097468e92SAlina Sbirlea                                    MemorySSAUpdater *MSSAU,
614a000883SChandler Carruth                                    bool PreserveLCSSA) {
624a000883SChandler Carruth   bool Changed = false;
634a000883SChandler Carruth 
644a000883SChandler Carruth   // We re-use a vector for the in-loop predecesosrs.
654a000883SChandler Carruth   SmallVector<BasicBlock *, 4> InLoopPredecessors;
664a000883SChandler Carruth 
674a000883SChandler Carruth   auto RewriteExit = [&](BasicBlock *BB) {
684a000883SChandler Carruth     assert(InLoopPredecessors.empty() &&
694a000883SChandler Carruth            "Must start with an empty predecessors list!");
704a000883SChandler Carruth     auto Cleanup = make_scope_exit([&] { InLoopPredecessors.clear(); });
714a000883SChandler Carruth 
724a000883SChandler Carruth     // See if there are any non-loop predecessors of this exit block and
734a000883SChandler Carruth     // keep track of the in-loop predecessors.
744a000883SChandler Carruth     bool IsDedicatedExit = true;
754a000883SChandler Carruth     for (auto *PredBB : predecessors(BB))
764a000883SChandler Carruth       if (L->contains(PredBB)) {
774a000883SChandler Carruth         if (isa<IndirectBrInst>(PredBB->getTerminator()))
784a000883SChandler Carruth           // We cannot rewrite exiting edges from an indirectbr.
794a000883SChandler Carruth           return false;
80784929d0SCraig Topper         if (isa<CallBrInst>(PredBB->getTerminator()))
81784929d0SCraig Topper           // We cannot rewrite exiting edges from a callbr.
82784929d0SCraig Topper           return false;
834a000883SChandler Carruth 
844a000883SChandler Carruth         InLoopPredecessors.push_back(PredBB);
854a000883SChandler Carruth       } else {
864a000883SChandler Carruth         IsDedicatedExit = false;
874a000883SChandler Carruth       }
884a000883SChandler Carruth 
894a000883SChandler Carruth     assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
904a000883SChandler Carruth 
914a000883SChandler Carruth     // Nothing to do if this is already a dedicated exit.
924a000883SChandler Carruth     if (IsDedicatedExit)
934a000883SChandler Carruth       return false;
944a000883SChandler Carruth 
954a000883SChandler Carruth     auto *NewExitBB = SplitBlockPredecessors(
9697468e92SAlina Sbirlea         BB, InLoopPredecessors, ".loopexit", DT, LI, MSSAU, PreserveLCSSA);
974a000883SChandler Carruth 
984a000883SChandler Carruth     if (!NewExitBB)
99d34e60caSNicola Zaghen       LLVM_DEBUG(
100d34e60caSNicola Zaghen           dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
1014a000883SChandler Carruth                  << *L << "\n");
1024a000883SChandler Carruth     else
103d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
1044a000883SChandler Carruth                         << NewExitBB->getName() << "\n");
1054a000883SChandler Carruth     return true;
1064a000883SChandler Carruth   };
1074a000883SChandler Carruth 
1084a000883SChandler Carruth   // Walk the exit blocks directly rather than building up a data structure for
1094a000883SChandler Carruth   // them, but only visit each one once.
1104a000883SChandler Carruth   SmallPtrSet<BasicBlock *, 4> Visited;
1114a000883SChandler Carruth   for (auto *BB : L->blocks())
1124a000883SChandler Carruth     for (auto *SuccBB : successors(BB)) {
1134a000883SChandler Carruth       // We're looking for exit blocks so skip in-loop successors.
1144a000883SChandler Carruth       if (L->contains(SuccBB))
1154a000883SChandler Carruth         continue;
1164a000883SChandler Carruth 
1174a000883SChandler Carruth       // Visit each exit block exactly once.
1184a000883SChandler Carruth       if (!Visited.insert(SuccBB).second)
1194a000883SChandler Carruth         continue;
1204a000883SChandler Carruth 
1214a000883SChandler Carruth       Changed |= RewriteExit(SuccBB);
1224a000883SChandler Carruth     }
1234a000883SChandler Carruth 
1244a000883SChandler Carruth   return Changed;
1254a000883SChandler Carruth }
1264a000883SChandler Carruth 
1275f8f34e4SAdrian Prantl /// Returns the instructions that use values defined in the loop.
128c5b7b555SAshutosh Nema SmallVector<Instruction *, 8> llvm::findDefsUsedOutsideOfLoop(Loop *L) {
129c5b7b555SAshutosh Nema   SmallVector<Instruction *, 8> UsedOutside;
130c5b7b555SAshutosh Nema 
131c5b7b555SAshutosh Nema   for (auto *Block : L->getBlocks())
132c5b7b555SAshutosh Nema     // FIXME: I believe that this could use copy_if if the Inst reference could
133c5b7b555SAshutosh Nema     // be adapted into a pointer.
134c5b7b555SAshutosh Nema     for (auto &Inst : *Block) {
135c5b7b555SAshutosh Nema       auto Users = Inst.users();
1360a16c228SDavid Majnemer       if (any_of(Users, [&](User *U) {
137c5b7b555SAshutosh Nema             auto *Use = cast<Instruction>(U);
138c5b7b555SAshutosh Nema             return !L->contains(Use->getParent());
139c5b7b555SAshutosh Nema           }))
140c5b7b555SAshutosh Nema         UsedOutside.push_back(&Inst);
141c5b7b555SAshutosh Nema     }
142c5b7b555SAshutosh Nema 
143c5b7b555SAshutosh Nema   return UsedOutside;
144c5b7b555SAshutosh Nema }
14531088a9dSChandler Carruth 
14631088a9dSChandler Carruth void llvm::getLoopAnalysisUsage(AnalysisUsage &AU) {
14731088a9dSChandler Carruth   // By definition, all loop passes need the LoopInfo analysis and the
14831088a9dSChandler Carruth   // Dominator tree it depends on. Because they all participate in the loop
14931088a9dSChandler Carruth   // pass manager, they must also preserve these.
15031088a9dSChandler Carruth   AU.addRequired<DominatorTreeWrapperPass>();
15131088a9dSChandler Carruth   AU.addPreserved<DominatorTreeWrapperPass>();
15231088a9dSChandler Carruth   AU.addRequired<LoopInfoWrapperPass>();
15331088a9dSChandler Carruth   AU.addPreserved<LoopInfoWrapperPass>();
15431088a9dSChandler Carruth 
15531088a9dSChandler Carruth   // We must also preserve LoopSimplify and LCSSA. We locally access their IDs
15631088a9dSChandler Carruth   // here because users shouldn't directly get them from this header.
15731088a9dSChandler Carruth   extern char &LoopSimplifyID;
15831088a9dSChandler Carruth   extern char &LCSSAID;
15931088a9dSChandler Carruth   AU.addRequiredID(LoopSimplifyID);
16031088a9dSChandler Carruth   AU.addPreservedID(LoopSimplifyID);
16131088a9dSChandler Carruth   AU.addRequiredID(LCSSAID);
16231088a9dSChandler Carruth   AU.addPreservedID(LCSSAID);
163c3ccf5d7SIgor Laevsky   // This is used in the LPPassManager to perform LCSSA verification on passes
164c3ccf5d7SIgor Laevsky   // which preserve lcssa form
165c3ccf5d7SIgor Laevsky   AU.addRequired<LCSSAVerificationPass>();
166c3ccf5d7SIgor Laevsky   AU.addPreserved<LCSSAVerificationPass>();
16731088a9dSChandler Carruth 
16831088a9dSChandler Carruth   // Loop passes are designed to run inside of a loop pass manager which means
16931088a9dSChandler Carruth   // that any function analyses they require must be required by the first loop
17031088a9dSChandler Carruth   // pass in the manager (so that it is computed before the loop pass manager
17131088a9dSChandler Carruth   // runs) and preserved by all loop pasess in the manager. To make this
17231088a9dSChandler Carruth   // reasonably robust, the set needed for most loop passes is maintained here.
17331088a9dSChandler Carruth   // If your loop pass requires an analysis not listed here, you will need to
17431088a9dSChandler Carruth   // carefully audit the loop pass manager nesting structure that results.
17531088a9dSChandler Carruth   AU.addRequired<AAResultsWrapperPass>();
17631088a9dSChandler Carruth   AU.addPreserved<AAResultsWrapperPass>();
17731088a9dSChandler Carruth   AU.addPreserved<BasicAAWrapperPass>();
17831088a9dSChandler Carruth   AU.addPreserved<GlobalsAAWrapperPass>();
17931088a9dSChandler Carruth   AU.addPreserved<SCEVAAWrapperPass>();
18031088a9dSChandler Carruth   AU.addRequired<ScalarEvolutionWrapperPass>();
18131088a9dSChandler Carruth   AU.addPreserved<ScalarEvolutionWrapperPass>();
1826da79ce1SAlina Sbirlea   // FIXME: When all loop passes preserve MemorySSA, it can be required and
1836da79ce1SAlina Sbirlea   // preserved here instead of the individual handling in each pass.
18431088a9dSChandler Carruth }
18531088a9dSChandler Carruth 
18631088a9dSChandler Carruth /// Manually defined generic "LoopPass" dependency initialization. This is used
18731088a9dSChandler Carruth /// to initialize the exact set of passes from above in \c
18831088a9dSChandler Carruth /// getLoopAnalysisUsage. It can be used within a loop pass's initialization
18931088a9dSChandler Carruth /// with:
19031088a9dSChandler Carruth ///
19131088a9dSChandler Carruth ///   INITIALIZE_PASS_DEPENDENCY(LoopPass)
19231088a9dSChandler Carruth ///
19331088a9dSChandler Carruth /// As-if "LoopPass" were a pass.
19431088a9dSChandler Carruth void llvm::initializeLoopPassPass(PassRegistry &Registry) {
19531088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
19631088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
19731088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
198e12c487bSEaswaran Raman   INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
19931088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
20031088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
20131088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
20231088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
20331088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
2046da79ce1SAlina Sbirlea   INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
20531088a9dSChandler Carruth }
206963341c8SAdam Nemet 
2073c3a7652SSerguei Katkov /// Create MDNode for input string.
2083c3a7652SSerguei Katkov static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) {
2093c3a7652SSerguei Katkov   LLVMContext &Context = TheLoop->getHeader()->getContext();
2103c3a7652SSerguei Katkov   Metadata *MDs[] = {
2113c3a7652SSerguei Katkov       MDString::get(Context, Name),
2123c3a7652SSerguei Katkov       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))};
2133c3a7652SSerguei Katkov   return MDNode::get(Context, MDs);
2143c3a7652SSerguei Katkov }
2153c3a7652SSerguei Katkov 
2163c3a7652SSerguei Katkov /// Set input string into loop metadata by keeping other values intact.
2177f8c8095SSerguei Katkov /// If the string is already in loop metadata update value if it is
2187f8c8095SSerguei Katkov /// different.
2197f8c8095SSerguei Katkov void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD,
2203c3a7652SSerguei Katkov                                    unsigned V) {
2213c3a7652SSerguei Katkov   SmallVector<Metadata *, 4> MDs(1);
2223c3a7652SSerguei Katkov   // If the loop already has metadata, retain it.
2233c3a7652SSerguei Katkov   MDNode *LoopID = TheLoop->getLoopID();
2243c3a7652SSerguei Katkov   if (LoopID) {
2253c3a7652SSerguei Katkov     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
2263c3a7652SSerguei Katkov       MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
2277f8c8095SSerguei Katkov       // If it is of form key = value, try to parse it.
2287f8c8095SSerguei Katkov       if (Node->getNumOperands() == 2) {
2297f8c8095SSerguei Katkov         MDString *S = dyn_cast<MDString>(Node->getOperand(0));
2307f8c8095SSerguei Katkov         if (S && S->getString().equals(StringMD)) {
2317f8c8095SSerguei Katkov           ConstantInt *IntMD =
2327f8c8095SSerguei Katkov               mdconst::extract_or_null<ConstantInt>(Node->getOperand(1));
2337f8c8095SSerguei Katkov           if (IntMD && IntMD->getSExtValue() == V)
2347f8c8095SSerguei Katkov             // It is already in place. Do nothing.
2357f8c8095SSerguei Katkov             return;
2367f8c8095SSerguei Katkov           // We need to update the value, so just skip it here and it will
2377f8c8095SSerguei Katkov           // be added after copying other existed nodes.
2387f8c8095SSerguei Katkov           continue;
2397f8c8095SSerguei Katkov         }
2407f8c8095SSerguei Katkov       }
2413c3a7652SSerguei Katkov       MDs.push_back(Node);
2423c3a7652SSerguei Katkov     }
2433c3a7652SSerguei Katkov   }
2443c3a7652SSerguei Katkov   // Add new metadata.
2457f8c8095SSerguei Katkov   MDs.push_back(createStringMetadata(TheLoop, StringMD, V));
2463c3a7652SSerguei Katkov   // Replace current metadata node with new one.
2473c3a7652SSerguei Katkov   LLVMContext &Context = TheLoop->getHeader()->getContext();
2483c3a7652SSerguei Katkov   MDNode *NewLoopID = MDNode::get(Context, MDs);
2493c3a7652SSerguei Katkov   // Set operand 0 to refer to the loop id itself.
2503c3a7652SSerguei Katkov   NewLoopID->replaceOperandWith(0, NewLoopID);
2513c3a7652SSerguei Katkov   TheLoop->setLoopID(NewLoopID);
2523c3a7652SSerguei Katkov }
2533c3a7652SSerguei Katkov 
25472448525SMichael Kruse /// Find string metadata for loop
25572448525SMichael Kruse ///
25672448525SMichael Kruse /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an
25772448525SMichael Kruse /// operand or null otherwise.  If the string metadata is not found return
25872448525SMichael Kruse /// Optional's not-a-value.
259978ba615SMichael Kruse Optional<const MDOperand *> llvm::findStringMetadataForLoop(const Loop *TheLoop,
26072448525SMichael Kruse                                                             StringRef Name) {
261978ba615SMichael Kruse   MDNode *MD = findOptionMDForLoop(TheLoop, Name);
26272448525SMichael Kruse   if (!MD)
26372448525SMichael Kruse     return None;
264fe3def7cSAdam Nemet   switch (MD->getNumOperands()) {
265fe3def7cSAdam Nemet   case 1:
266fe3def7cSAdam Nemet     return nullptr;
267fe3def7cSAdam Nemet   case 2:
268fe3def7cSAdam Nemet     return &MD->getOperand(1);
269fe3def7cSAdam Nemet   default:
270fe3def7cSAdam Nemet     llvm_unreachable("loop metadata has 0 or 1 operand");
271963341c8SAdam Nemet   }
272fe3def7cSAdam Nemet }
27372448525SMichael Kruse 
27472448525SMichael Kruse static Optional<bool> getOptionalBoolLoopAttribute(const Loop *TheLoop,
27572448525SMichael Kruse                                                    StringRef Name) {
276978ba615SMichael Kruse   MDNode *MD = findOptionMDForLoop(TheLoop, Name);
277978ba615SMichael Kruse   if (!MD)
278fe3def7cSAdam Nemet     return None;
279978ba615SMichael Kruse   switch (MD->getNumOperands()) {
28072448525SMichael Kruse   case 1:
28172448525SMichael Kruse     // When the value is absent it is interpreted as 'attribute set'.
28272448525SMichael Kruse     return true;
28372448525SMichael Kruse   case 2:
284f9027e55SAlina Sbirlea     if (ConstantInt *IntMD =
285f9027e55SAlina Sbirlea             mdconst::extract_or_null<ConstantInt>(MD->getOperand(1).get()))
286f9027e55SAlina Sbirlea       return IntMD->getZExtValue();
287f9027e55SAlina Sbirlea     return true;
28872448525SMichael Kruse   }
28972448525SMichael Kruse   llvm_unreachable("unexpected number of options");
29072448525SMichael Kruse }
29172448525SMichael Kruse 
29272448525SMichael Kruse static bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name) {
29372448525SMichael Kruse   return getOptionalBoolLoopAttribute(TheLoop, Name).getValueOr(false);
29472448525SMichael Kruse }
29572448525SMichael Kruse 
29672448525SMichael Kruse llvm::Optional<int> llvm::getOptionalIntLoopAttribute(Loop *TheLoop,
29772448525SMichael Kruse                                                       StringRef Name) {
29872448525SMichael Kruse   const MDOperand *AttrMD =
29972448525SMichael Kruse       findStringMetadataForLoop(TheLoop, Name).getValueOr(nullptr);
30072448525SMichael Kruse   if (!AttrMD)
30172448525SMichael Kruse     return None;
30272448525SMichael Kruse 
30372448525SMichael Kruse   ConstantInt *IntMD = mdconst::extract_or_null<ConstantInt>(AttrMD->get());
30472448525SMichael Kruse   if (!IntMD)
30572448525SMichael Kruse     return None;
30672448525SMichael Kruse 
30772448525SMichael Kruse   return IntMD->getSExtValue();
30872448525SMichael Kruse }
30972448525SMichael Kruse 
31072448525SMichael Kruse Optional<MDNode *> llvm::makeFollowupLoopID(
31172448525SMichael Kruse     MDNode *OrigLoopID, ArrayRef<StringRef> FollowupOptions,
31272448525SMichael Kruse     const char *InheritOptionsExceptPrefix, bool AlwaysNew) {
31372448525SMichael Kruse   if (!OrigLoopID) {
31472448525SMichael Kruse     if (AlwaysNew)
31572448525SMichael Kruse       return nullptr;
31672448525SMichael Kruse     return None;
31772448525SMichael Kruse   }
31872448525SMichael Kruse 
31972448525SMichael Kruse   assert(OrigLoopID->getOperand(0) == OrigLoopID);
32072448525SMichael Kruse 
32172448525SMichael Kruse   bool InheritAllAttrs = !InheritOptionsExceptPrefix;
32272448525SMichael Kruse   bool InheritSomeAttrs =
32372448525SMichael Kruse       InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] != '\0';
32472448525SMichael Kruse   SmallVector<Metadata *, 8> MDs;
32572448525SMichael Kruse   MDs.push_back(nullptr);
32672448525SMichael Kruse 
32772448525SMichael Kruse   bool Changed = false;
32872448525SMichael Kruse   if (InheritAllAttrs || InheritSomeAttrs) {
32972448525SMichael Kruse     for (const MDOperand &Existing : drop_begin(OrigLoopID->operands(), 1)) {
33072448525SMichael Kruse       MDNode *Op = cast<MDNode>(Existing.get());
33172448525SMichael Kruse 
33272448525SMichael Kruse       auto InheritThisAttribute = [InheritSomeAttrs,
33372448525SMichael Kruse                                    InheritOptionsExceptPrefix](MDNode *Op) {
33472448525SMichael Kruse         if (!InheritSomeAttrs)
33572448525SMichael Kruse           return false;
33672448525SMichael Kruse 
33772448525SMichael Kruse         // Skip malformatted attribute metadata nodes.
33872448525SMichael Kruse         if (Op->getNumOperands() == 0)
33972448525SMichael Kruse           return true;
34072448525SMichael Kruse         Metadata *NameMD = Op->getOperand(0).get();
34172448525SMichael Kruse         if (!isa<MDString>(NameMD))
34272448525SMichael Kruse           return true;
34372448525SMichael Kruse         StringRef AttrName = cast<MDString>(NameMD)->getString();
34472448525SMichael Kruse 
34572448525SMichael Kruse         // Do not inherit excluded attributes.
34672448525SMichael Kruse         return !AttrName.startswith(InheritOptionsExceptPrefix);
34772448525SMichael Kruse       };
34872448525SMichael Kruse 
34972448525SMichael Kruse       if (InheritThisAttribute(Op))
35072448525SMichael Kruse         MDs.push_back(Op);
35172448525SMichael Kruse       else
35272448525SMichael Kruse         Changed = true;
35372448525SMichael Kruse     }
35472448525SMichael Kruse   } else {
35572448525SMichael Kruse     // Modified if we dropped at least one attribute.
35672448525SMichael Kruse     Changed = OrigLoopID->getNumOperands() > 1;
35772448525SMichael Kruse   }
35872448525SMichael Kruse 
35972448525SMichael Kruse   bool HasAnyFollowup = false;
36072448525SMichael Kruse   for (StringRef OptionName : FollowupOptions) {
361978ba615SMichael Kruse     MDNode *FollowupNode = findOptionMDForLoopID(OrigLoopID, OptionName);
36272448525SMichael Kruse     if (!FollowupNode)
36372448525SMichael Kruse       continue;
36472448525SMichael Kruse 
36572448525SMichael Kruse     HasAnyFollowup = true;
36672448525SMichael Kruse     for (const MDOperand &Option : drop_begin(FollowupNode->operands(), 1)) {
36772448525SMichael Kruse       MDs.push_back(Option.get());
36872448525SMichael Kruse       Changed = true;
36972448525SMichael Kruse     }
37072448525SMichael Kruse   }
37172448525SMichael Kruse 
37272448525SMichael Kruse   // Attributes of the followup loop not specified explicity, so signal to the
37372448525SMichael Kruse   // transformation pass to add suitable attributes.
37472448525SMichael Kruse   if (!AlwaysNew && !HasAnyFollowup)
37572448525SMichael Kruse     return None;
37672448525SMichael Kruse 
37772448525SMichael Kruse   // If no attributes were added or remove, the previous loop Id can be reused.
37872448525SMichael Kruse   if (!AlwaysNew && !Changed)
37972448525SMichael Kruse     return OrigLoopID;
38072448525SMichael Kruse 
38172448525SMichael Kruse   // No attributes is equivalent to having no !llvm.loop metadata at all.
38272448525SMichael Kruse   if (MDs.size() == 1)
38372448525SMichael Kruse     return nullptr;
38472448525SMichael Kruse 
38572448525SMichael Kruse   // Build the new loop ID.
38672448525SMichael Kruse   MDTuple *FollowupLoopID = MDNode::get(OrigLoopID->getContext(), MDs);
38772448525SMichael Kruse   FollowupLoopID->replaceOperandWith(0, FollowupLoopID);
38872448525SMichael Kruse   return FollowupLoopID;
38972448525SMichael Kruse }
39072448525SMichael Kruse 
39172448525SMichael Kruse bool llvm::hasDisableAllTransformsHint(const Loop *L) {
39272448525SMichael Kruse   return getBooleanLoopAttribute(L, LLVMLoopDisableNonforced);
39372448525SMichael Kruse }
39472448525SMichael Kruse 
3954f64f1baSTim Corringham bool llvm::hasDisableLICMTransformsHint(const Loop *L) {
3964f64f1baSTim Corringham   return getBooleanLoopAttribute(L, LLVMLoopDisableLICM);
3974f64f1baSTim Corringham }
3984f64f1baSTim Corringham 
39972448525SMichael Kruse TransformationMode llvm::hasUnrollTransformation(Loop *L) {
40072448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable"))
40172448525SMichael Kruse     return TM_SuppressedByUser;
40272448525SMichael Kruse 
40372448525SMichael Kruse   Optional<int> Count =
40472448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
40572448525SMichael Kruse   if (Count.hasValue())
40672448525SMichael Kruse     return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
40772448525SMichael Kruse 
40872448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable"))
40972448525SMichael Kruse     return TM_ForcedByUser;
41072448525SMichael Kruse 
41172448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.full"))
41272448525SMichael Kruse     return TM_ForcedByUser;
41372448525SMichael Kruse 
41472448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
41572448525SMichael Kruse     return TM_Disable;
41672448525SMichael Kruse 
41772448525SMichael Kruse   return TM_Unspecified;
41872448525SMichael Kruse }
41972448525SMichael Kruse 
42072448525SMichael Kruse TransformationMode llvm::hasUnrollAndJamTransformation(Loop *L) {
42172448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable"))
42272448525SMichael Kruse     return TM_SuppressedByUser;
42372448525SMichael Kruse 
42472448525SMichael Kruse   Optional<int> Count =
42572448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count");
42672448525SMichael Kruse   if (Count.hasValue())
42772448525SMichael Kruse     return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
42872448525SMichael Kruse 
42972448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.enable"))
43072448525SMichael Kruse     return TM_ForcedByUser;
43172448525SMichael Kruse 
43272448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
43372448525SMichael Kruse     return TM_Disable;
43472448525SMichael Kruse 
43572448525SMichael Kruse   return TM_Unspecified;
43672448525SMichael Kruse }
43772448525SMichael Kruse 
43872448525SMichael Kruse TransformationMode llvm::hasVectorizeTransformation(Loop *L) {
43972448525SMichael Kruse   Optional<bool> Enable =
44072448525SMichael Kruse       getOptionalBoolLoopAttribute(L, "llvm.loop.vectorize.enable");
44172448525SMichael Kruse 
44272448525SMichael Kruse   if (Enable == false)
44372448525SMichael Kruse     return TM_SuppressedByUser;
44472448525SMichael Kruse 
44572448525SMichael Kruse   Optional<int> VectorizeWidth =
44672448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.vectorize.width");
44772448525SMichael Kruse   Optional<int> InterleaveCount =
44872448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count");
44972448525SMichael Kruse 
45072448525SMichael Kruse   // 'Forcing' vector width and interleave count to one effectively disables
45172448525SMichael Kruse   // this tranformation.
45270560a0aSMichael Kruse   if (Enable == true && VectorizeWidth == 1 && InterleaveCount == 1)
45372448525SMichael Kruse     return TM_SuppressedByUser;
45472448525SMichael Kruse 
45572448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.isvectorized"))
45672448525SMichael Kruse     return TM_Disable;
45772448525SMichael Kruse 
45870560a0aSMichael Kruse   if (Enable == true)
45970560a0aSMichael Kruse     return TM_ForcedByUser;
46070560a0aSMichael Kruse 
46172448525SMichael Kruse   if (VectorizeWidth == 1 && InterleaveCount == 1)
46272448525SMichael Kruse     return TM_Disable;
46372448525SMichael Kruse 
46472448525SMichael Kruse   if (VectorizeWidth > 1 || InterleaveCount > 1)
46572448525SMichael Kruse     return TM_Enable;
46672448525SMichael Kruse 
46772448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
46872448525SMichael Kruse     return TM_Disable;
46972448525SMichael Kruse 
47072448525SMichael Kruse   return TM_Unspecified;
47172448525SMichael Kruse }
47272448525SMichael Kruse 
47372448525SMichael Kruse TransformationMode llvm::hasDistributeTransformation(Loop *L) {
47472448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.distribute.enable"))
47572448525SMichael Kruse     return TM_ForcedByUser;
47672448525SMichael Kruse 
47772448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
47872448525SMichael Kruse     return TM_Disable;
47972448525SMichael Kruse 
48072448525SMichael Kruse   return TM_Unspecified;
48172448525SMichael Kruse }
48272448525SMichael Kruse 
48372448525SMichael Kruse TransformationMode llvm::hasLICMVersioningTransformation(Loop *L) {
48472448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.licm_versioning.disable"))
48572448525SMichael Kruse     return TM_SuppressedByUser;
48672448525SMichael Kruse 
48772448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
48872448525SMichael Kruse     return TM_Disable;
48972448525SMichael Kruse 
49072448525SMichael Kruse   return TM_Unspecified;
491963341c8SAdam Nemet }
492122f984aSEvgeniy Stepanov 
4937ed5856aSAlina Sbirlea /// Does a BFS from a given node to all of its children inside a given loop.
4947ed5856aSAlina Sbirlea /// The returned vector of nodes includes the starting point.
4957ed5856aSAlina Sbirlea SmallVector<DomTreeNode *, 16>
4967ed5856aSAlina Sbirlea llvm::collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop) {
4977ed5856aSAlina Sbirlea   SmallVector<DomTreeNode *, 16> Worklist;
4987ed5856aSAlina Sbirlea   auto AddRegionToWorklist = [&](DomTreeNode *DTN) {
4997ed5856aSAlina Sbirlea     // Only include subregions in the top level loop.
5007ed5856aSAlina Sbirlea     BasicBlock *BB = DTN->getBlock();
5017ed5856aSAlina Sbirlea     if (CurLoop->contains(BB))
5027ed5856aSAlina Sbirlea       Worklist.push_back(DTN);
5037ed5856aSAlina Sbirlea   };
5047ed5856aSAlina Sbirlea 
5057ed5856aSAlina Sbirlea   AddRegionToWorklist(N);
5067ed5856aSAlina Sbirlea 
5077ed5856aSAlina Sbirlea   for (size_t I = 0; I < Worklist.size(); I++)
5087ed5856aSAlina Sbirlea     for (DomTreeNode *Child : Worklist[I]->getChildren())
5097ed5856aSAlina Sbirlea       AddRegionToWorklist(Child);
5107ed5856aSAlina Sbirlea 
5117ed5856aSAlina Sbirlea   return Worklist;
5127ed5856aSAlina Sbirlea }
5137ed5856aSAlina Sbirlea 
514efb130fcSAlina Sbirlea void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
515efb130fcSAlina Sbirlea                           LoopInfo *LI, MemorySSA *MSSA) {
516899809d5SHans Wennborg   assert((!DT || L->isLCSSAForm(*DT)) && "Expected LCSSA!");
517df3e71e0SMarcello Maggioni   auto *Preheader = L->getLoopPreheader();
518df3e71e0SMarcello Maggioni   assert(Preheader && "Preheader should exist!");
519df3e71e0SMarcello Maggioni 
520efb130fcSAlina Sbirlea   std::unique_ptr<MemorySSAUpdater> MSSAU;
521efb130fcSAlina Sbirlea   if (MSSA)
522efb130fcSAlina Sbirlea     MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
523efb130fcSAlina Sbirlea 
524df3e71e0SMarcello Maggioni   // Now that we know the removal is safe, remove the loop by changing the
525df3e71e0SMarcello Maggioni   // branch from the preheader to go to the single exit block.
526df3e71e0SMarcello Maggioni   //
527df3e71e0SMarcello Maggioni   // Because we're deleting a large chunk of code at once, the sequence in which
528df3e71e0SMarcello Maggioni   // we remove things is very important to avoid invalidation issues.
529df3e71e0SMarcello Maggioni 
530df3e71e0SMarcello Maggioni   // Tell ScalarEvolution that the loop is deleted. Do this before
531df3e71e0SMarcello Maggioni   // deleting the loop so that ScalarEvolution can look at the loop
532df3e71e0SMarcello Maggioni   // to determine what it needs to clean up.
533df3e71e0SMarcello Maggioni   if (SE)
534df3e71e0SMarcello Maggioni     SE->forgetLoop(L);
535df3e71e0SMarcello Maggioni 
536df3e71e0SMarcello Maggioni   auto *ExitBlock = L->getUniqueExitBlock();
537df3e71e0SMarcello Maggioni   assert(ExitBlock && "Should have a unique exit block!");
538df3e71e0SMarcello Maggioni   assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
539df3e71e0SMarcello Maggioni 
540df3e71e0SMarcello Maggioni   auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator());
541df3e71e0SMarcello Maggioni   assert(OldBr && "Preheader must end with a branch");
542df3e71e0SMarcello Maggioni   assert(OldBr->isUnconditional() && "Preheader must have a single successor");
543df3e71e0SMarcello Maggioni   // Connect the preheader to the exit block. Keep the old edge to the header
544df3e71e0SMarcello Maggioni   // around to perform the dominator tree update in two separate steps
545df3e71e0SMarcello Maggioni   // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
546df3e71e0SMarcello Maggioni   // preheader -> header.
547df3e71e0SMarcello Maggioni   //
548df3e71e0SMarcello Maggioni   //
549df3e71e0SMarcello Maggioni   // 0.  Preheader          1.  Preheader           2.  Preheader
550df3e71e0SMarcello Maggioni   //        |                    |   |                   |
551df3e71e0SMarcello Maggioni   //        V                    |   V                   |
552df3e71e0SMarcello Maggioni   //      Header <--\            | Header <--\           | Header <--\
553df3e71e0SMarcello Maggioni   //       |  |     |            |  |  |     |           |  |  |     |
554df3e71e0SMarcello Maggioni   //       |  V     |            |  |  V     |           |  |  V     |
555df3e71e0SMarcello Maggioni   //       | Body --/            |  | Body --/           |  | Body --/
556df3e71e0SMarcello Maggioni   //       V                     V  V                    V  V
557df3e71e0SMarcello Maggioni   //      Exit                   Exit                    Exit
558df3e71e0SMarcello Maggioni   //
559df3e71e0SMarcello Maggioni   // By doing this is two separate steps we can perform the dominator tree
560df3e71e0SMarcello Maggioni   // update without using the batch update API.
561df3e71e0SMarcello Maggioni   //
562df3e71e0SMarcello Maggioni   // Even when the loop is never executed, we cannot remove the edge from the
563df3e71e0SMarcello Maggioni   // source block to the exit block. Consider the case where the unexecuted loop
564df3e71e0SMarcello Maggioni   // branches back to an outer loop. If we deleted the loop and removed the edge
565df3e71e0SMarcello Maggioni   // coming to this inner loop, this will break the outer loop structure (by
566df3e71e0SMarcello Maggioni   // deleting the backedge of the outer loop). If the outer loop is indeed a
567df3e71e0SMarcello Maggioni   // non-loop, it will be deleted in a future iteration of loop deletion pass.
568df3e71e0SMarcello Maggioni   IRBuilder<> Builder(OldBr);
569df3e71e0SMarcello Maggioni   Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
570df3e71e0SMarcello Maggioni   // Remove the old branch. The conditional branch becomes a new terminator.
571df3e71e0SMarcello Maggioni   OldBr->eraseFromParent();
572df3e71e0SMarcello Maggioni 
573df3e71e0SMarcello Maggioni   // Rewrite phis in the exit block to get their inputs from the Preheader
574df3e71e0SMarcello Maggioni   // instead of the exiting block.
575c7fc81e6SBenjamin Kramer   for (PHINode &P : ExitBlock->phis()) {
576df3e71e0SMarcello Maggioni     // Set the zero'th element of Phi to be from the preheader and remove all
577df3e71e0SMarcello Maggioni     // other incoming values. Given the loop has dedicated exits, all other
578df3e71e0SMarcello Maggioni     // incoming values must be from the exiting blocks.
579df3e71e0SMarcello Maggioni     int PredIndex = 0;
580c7fc81e6SBenjamin Kramer     P.setIncomingBlock(PredIndex, Preheader);
581df3e71e0SMarcello Maggioni     // Removes all incoming values from all other exiting blocks (including
582df3e71e0SMarcello Maggioni     // duplicate values from an exiting block).
583df3e71e0SMarcello Maggioni     // Nuke all entries except the zero'th entry which is the preheader entry.
584df3e71e0SMarcello Maggioni     // NOTE! We need to remove Incoming Values in the reverse order as done
585df3e71e0SMarcello Maggioni     // below, to keep the indices valid for deletion (removeIncomingValues
586df3e71e0SMarcello Maggioni     // updates getNumIncomingValues and shifts all values down into the operand
587df3e71e0SMarcello Maggioni     // being deleted).
588c7fc81e6SBenjamin Kramer     for (unsigned i = 0, e = P.getNumIncomingValues() - 1; i != e; ++i)
589c7fc81e6SBenjamin Kramer       P.removeIncomingValue(e - i, false);
590df3e71e0SMarcello Maggioni 
591c7fc81e6SBenjamin Kramer     assert((P.getNumIncomingValues() == 1 &&
592c7fc81e6SBenjamin Kramer             P.getIncomingBlock(PredIndex) == Preheader) &&
593df3e71e0SMarcello Maggioni            "Should have exactly one value and that's from the preheader!");
594df3e71e0SMarcello Maggioni   }
595df3e71e0SMarcello Maggioni 
596efb130fcSAlina Sbirlea   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
597efb130fcSAlina Sbirlea   if (DT) {
598efb130fcSAlina Sbirlea     DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}});
599efb130fcSAlina Sbirlea     if (MSSA) {
600efb130fcSAlina Sbirlea       MSSAU->applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}}, *DT);
601efb130fcSAlina Sbirlea       if (VerifyMemorySSA)
602efb130fcSAlina Sbirlea         MSSA->verifyMemorySSA();
603efb130fcSAlina Sbirlea     }
604efb130fcSAlina Sbirlea   }
605efb130fcSAlina Sbirlea 
606df3e71e0SMarcello Maggioni   // Disconnect the loop body by branching directly to its exit.
607df3e71e0SMarcello Maggioni   Builder.SetInsertPoint(Preheader->getTerminator());
608df3e71e0SMarcello Maggioni   Builder.CreateBr(ExitBlock);
609df3e71e0SMarcello Maggioni   // Remove the old branch.
610df3e71e0SMarcello Maggioni   Preheader->getTerminator()->eraseFromParent();
611df3e71e0SMarcello Maggioni 
612df3e71e0SMarcello Maggioni   if (DT) {
613efb130fcSAlina Sbirlea     DTU.applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}});
614efb130fcSAlina Sbirlea     if (MSSA) {
615efb130fcSAlina Sbirlea       MSSAU->applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}},
616efb130fcSAlina Sbirlea                           *DT);
617efb130fcSAlina Sbirlea       if (VerifyMemorySSA)
618efb130fcSAlina Sbirlea         MSSA->verifyMemorySSA();
619efb130fcSAlina Sbirlea       SmallSetVector<BasicBlock *, 8> DeadBlockSet(L->block_begin(),
620efb130fcSAlina Sbirlea                                                    L->block_end());
621efb130fcSAlina Sbirlea       MSSAU->removeBlocks(DeadBlockSet);
622efb130fcSAlina Sbirlea     }
623df3e71e0SMarcello Maggioni   }
624df3e71e0SMarcello Maggioni 
625744c3c32SDavide Italiano   // Use a map to unique and a vector to guarantee deterministic ordering.
6268ee59ca6SDavide Italiano   llvm::SmallDenseSet<std::pair<DIVariable *, DIExpression *>, 4> DeadDebugSet;
627744c3c32SDavide Italiano   llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst;
628744c3c32SDavide Italiano 
629a757d65cSSerguei Katkov   // Given LCSSA form is satisfied, we should not have users of instructions
630a757d65cSSerguei Katkov   // within the dead loop outside of the loop. However, LCSSA doesn't take
631a757d65cSSerguei Katkov   // unreachable uses into account. We handle them here.
632a757d65cSSerguei Katkov   // We could do it after drop all references (in this case all users in the
633a757d65cSSerguei Katkov   // loop will be already eliminated and we have less work to do but according
634a757d65cSSerguei Katkov   // to API doc of User::dropAllReferences only valid operation after dropping
635a757d65cSSerguei Katkov   // references, is deletion. So let's substitute all usages of
636a757d65cSSerguei Katkov   // instruction from the loop with undef value of corresponding type first.
637a757d65cSSerguei Katkov   for (auto *Block : L->blocks())
638a757d65cSSerguei Katkov     for (Instruction &I : *Block) {
639a757d65cSSerguei Katkov       auto *Undef = UndefValue::get(I.getType());
640a757d65cSSerguei Katkov       for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E;) {
641a757d65cSSerguei Katkov         Use &U = *UI;
642a757d65cSSerguei Katkov         ++UI;
643a757d65cSSerguei Katkov         if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
644a757d65cSSerguei Katkov           if (L->contains(Usr->getParent()))
645a757d65cSSerguei Katkov             continue;
646a757d65cSSerguei Katkov         // If we have a DT then we can check that uses outside a loop only in
647a757d65cSSerguei Katkov         // unreachable block.
648a757d65cSSerguei Katkov         if (DT)
649a757d65cSSerguei Katkov           assert(!DT->isReachableFromEntry(U) &&
650a757d65cSSerguei Katkov                  "Unexpected user in reachable block");
651a757d65cSSerguei Katkov         U.set(Undef);
652a757d65cSSerguei Katkov       }
653744c3c32SDavide Italiano       auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
654744c3c32SDavide Italiano       if (!DVI)
655744c3c32SDavide Italiano         continue;
6568ee59ca6SDavide Italiano       auto Key = DeadDebugSet.find({DVI->getVariable(), DVI->getExpression()});
6578ee59ca6SDavide Italiano       if (Key != DeadDebugSet.end())
658744c3c32SDavide Italiano         continue;
6598ee59ca6SDavide Italiano       DeadDebugSet.insert({DVI->getVariable(), DVI->getExpression()});
660744c3c32SDavide Italiano       DeadDebugInst.push_back(DVI);
661a757d65cSSerguei Katkov     }
662a757d65cSSerguei Katkov 
663744c3c32SDavide Italiano   // After the loop has been deleted all the values defined and modified
664744c3c32SDavide Italiano   // inside the loop are going to be unavailable.
665744c3c32SDavide Italiano   // Since debug values in the loop have been deleted, inserting an undef
666744c3c32SDavide Italiano   // dbg.value truncates the range of any dbg.value before the loop where the
667744c3c32SDavide Italiano   // loop used to be. This is particularly important for constant values.
668744c3c32SDavide Italiano   DIBuilder DIB(*ExitBlock->getModule());
669e5be660eSRoman Lebedev   Instruction *InsertDbgValueBefore = ExitBlock->getFirstNonPHI();
670e5be660eSRoman Lebedev   assert(InsertDbgValueBefore &&
671e5be660eSRoman Lebedev          "There should be a non-PHI instruction in exit block, else these "
672e5be660eSRoman Lebedev          "instructions will have no parent.");
673744c3c32SDavide Italiano   for (auto *DVI : DeadDebugInst)
674e5be660eSRoman Lebedev     DIB.insertDbgValueIntrinsic(UndefValue::get(Builder.getInt32Ty()),
675e5be660eSRoman Lebedev                                 DVI->getVariable(), DVI->getExpression(),
676e5be660eSRoman Lebedev                                 DVI->getDebugLoc(), InsertDbgValueBefore);
677744c3c32SDavide Italiano 
678df3e71e0SMarcello Maggioni   // Remove the block from the reference counting scheme, so that we can
679df3e71e0SMarcello Maggioni   // delete it freely later.
680df3e71e0SMarcello Maggioni   for (auto *Block : L->blocks())
681df3e71e0SMarcello Maggioni     Block->dropAllReferences();
682df3e71e0SMarcello Maggioni 
683efb130fcSAlina Sbirlea   if (MSSA && VerifyMemorySSA)
684efb130fcSAlina Sbirlea     MSSA->verifyMemorySSA();
685efb130fcSAlina Sbirlea 
686df3e71e0SMarcello Maggioni   if (LI) {
687df3e71e0SMarcello Maggioni     // Erase the instructions and the blocks without having to worry
688df3e71e0SMarcello Maggioni     // about ordering because we already dropped the references.
689df3e71e0SMarcello Maggioni     // NOTE: This iteration is safe because erasing the block does not remove
690df3e71e0SMarcello Maggioni     // its entry from the loop's block list.  We do that in the next section.
691df3e71e0SMarcello Maggioni     for (Loop::block_iterator LpI = L->block_begin(), LpE = L->block_end();
692df3e71e0SMarcello Maggioni          LpI != LpE; ++LpI)
693df3e71e0SMarcello Maggioni       (*LpI)->eraseFromParent();
694df3e71e0SMarcello Maggioni 
695df3e71e0SMarcello Maggioni     // Finally, the blocks from loopinfo.  This has to happen late because
696df3e71e0SMarcello Maggioni     // otherwise our loop iterators won't work.
697df3e71e0SMarcello Maggioni 
698df3e71e0SMarcello Maggioni     SmallPtrSet<BasicBlock *, 8> blocks;
699df3e71e0SMarcello Maggioni     blocks.insert(L->block_begin(), L->block_end());
700df3e71e0SMarcello Maggioni     for (BasicBlock *BB : blocks)
701df3e71e0SMarcello Maggioni       LI->removeBlock(BB);
702df3e71e0SMarcello Maggioni 
703df3e71e0SMarcello Maggioni     // The last step is to update LoopInfo now that we've eliminated this loop.
7049883d7edSWhitney Tsang     // Note: LoopInfo::erase remove the given loop and relink its subloops with
7059883d7edSWhitney Tsang     // its parent. While removeLoop/removeChildLoop remove the given loop but
7069883d7edSWhitney Tsang     // not relink its subloops, which is what we want.
7079883d7edSWhitney Tsang     if (Loop *ParentLoop = L->getParentLoop()) {
7089883d7edSWhitney Tsang       Loop::iterator I = find(ParentLoop->begin(), ParentLoop->end(), L);
7099883d7edSWhitney Tsang       assert(I != ParentLoop->end() && "Couldn't find loop");
7109883d7edSWhitney Tsang       ParentLoop->removeChildLoop(I);
7119883d7edSWhitney Tsang     } else {
7129883d7edSWhitney Tsang       Loop::iterator I = find(LI->begin(), LI->end(), L);
7139883d7edSWhitney Tsang       assert(I != LI->end() && "Couldn't find loop");
7149883d7edSWhitney Tsang       LI->removeLoop(I);
7159883d7edSWhitney Tsang     }
7169883d7edSWhitney Tsang     LI->destroy(L);
717df3e71e0SMarcello Maggioni   }
718df3e71e0SMarcello Maggioni }
719df3e71e0SMarcello Maggioni 
720af7e1588SEvgeniy Brevnov /// Checks if \p L has single exit through latch block except possibly
721af7e1588SEvgeniy Brevnov /// "deoptimizing" exits. Returns branch instruction terminating the loop
722af7e1588SEvgeniy Brevnov /// latch if above check is successful, nullptr otherwise.
723af7e1588SEvgeniy Brevnov static BranchInst *getExpectedExitLoopLatchBranch(Loop *L) {
72445c43e7dSSerguei Katkov   BasicBlock *Latch = L->getLoopLatch();
72545c43e7dSSerguei Katkov   if (!Latch)
726af7e1588SEvgeniy Brevnov     return nullptr;
727af7e1588SEvgeniy Brevnov 
72845c43e7dSSerguei Katkov   BranchInst *LatchBR = dyn_cast<BranchInst>(Latch->getTerminator());
72945c43e7dSSerguei Katkov   if (!LatchBR || LatchBR->getNumSuccessors() != 2 || !L->isLoopExiting(Latch))
730af7e1588SEvgeniy Brevnov     return nullptr;
73141d72a86SDehao Chen 
73241d72a86SDehao Chen   assert((LatchBR->getSuccessor(0) == L->getHeader() ||
73341d72a86SDehao Chen           LatchBR->getSuccessor(1) == L->getHeader()) &&
73441d72a86SDehao Chen          "At least one edge out of the latch must go to the header");
73541d72a86SDehao Chen 
73645c43e7dSSerguei Katkov   SmallVector<BasicBlock *, 4> ExitBlocks;
73745c43e7dSSerguei Katkov   L->getUniqueNonLatchExitBlocks(ExitBlocks);
73845c43e7dSSerguei Katkov   if (any_of(ExitBlocks, [](const BasicBlock *EB) {
73945c43e7dSSerguei Katkov         return !EB->getTerminatingDeoptimizeCall();
74045c43e7dSSerguei Katkov       }))
741af7e1588SEvgeniy Brevnov     return nullptr;
742af7e1588SEvgeniy Brevnov 
743af7e1588SEvgeniy Brevnov   return LatchBR;
744af7e1588SEvgeniy Brevnov }
745af7e1588SEvgeniy Brevnov 
746af7e1588SEvgeniy Brevnov Optional<unsigned>
747af7e1588SEvgeniy Brevnov llvm::getLoopEstimatedTripCount(Loop *L,
748af7e1588SEvgeniy Brevnov                                 unsigned *EstimatedLoopInvocationWeight) {
749af7e1588SEvgeniy Brevnov   // Support loops with an exiting latch and other existing exists only
750af7e1588SEvgeniy Brevnov   // deoptimize.
751af7e1588SEvgeniy Brevnov   BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L);
752af7e1588SEvgeniy Brevnov   if (!LatchBranch)
75345c43e7dSSerguei Katkov     return None;
75445c43e7dSSerguei Katkov 
75541d72a86SDehao Chen   // To estimate the number of times the loop body was executed, we want to
75641d72a86SDehao Chen   // know the number of times the backedge was taken, vs. the number of times
75741d72a86SDehao Chen   // we exited the loop.
758f0abe820SEvgeniy Brevnov   uint64_t BackedgeTakenWeight, LatchExitWeight;
759af7e1588SEvgeniy Brevnov   if (!LatchBranch->extractProfMetadata(BackedgeTakenWeight, LatchExitWeight))
76041d72a86SDehao Chen     return None;
76141d72a86SDehao Chen 
762af7e1588SEvgeniy Brevnov   if (LatchBranch->getSuccessor(0) != L->getHeader())
763f0abe820SEvgeniy Brevnov     std::swap(BackedgeTakenWeight, LatchExitWeight);
764f0abe820SEvgeniy Brevnov 
76510357e1cSEvgeniy Brevnov   if (!LatchExitWeight)
76610357e1cSEvgeniy Brevnov     return None;
76741d72a86SDehao Chen 
768af7e1588SEvgeniy Brevnov   if (EstimatedLoopInvocationWeight)
769af7e1588SEvgeniy Brevnov     *EstimatedLoopInvocationWeight = LatchExitWeight;
770af7e1588SEvgeniy Brevnov 
77110357e1cSEvgeniy Brevnov   // Estimated backedge taken count is a ratio of the backedge taken weight by
772cfe97681SEvgeniy Brevnov   // the weight of the edge exiting the loop, rounded to nearest.
77310357e1cSEvgeniy Brevnov   uint64_t BackedgeTakenCount =
77410357e1cSEvgeniy Brevnov       llvm::divideNearest(BackedgeTakenWeight, LatchExitWeight);
77510357e1cSEvgeniy Brevnov   // Estimated trip count is one plus estimated backedge taken count.
77610357e1cSEvgeniy Brevnov   return BackedgeTakenCount + 1;
77741d72a86SDehao Chen }
778cf9daa33SAmara Emerson 
779af7e1588SEvgeniy Brevnov bool llvm::setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount,
780af7e1588SEvgeniy Brevnov                                      unsigned EstimatedloopInvocationWeight) {
781af7e1588SEvgeniy Brevnov   // Support loops with an exiting latch and other existing exists only
782af7e1588SEvgeniy Brevnov   // deoptimize.
783af7e1588SEvgeniy Brevnov   BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L);
784af7e1588SEvgeniy Brevnov   if (!LatchBranch)
785af7e1588SEvgeniy Brevnov     return false;
786af7e1588SEvgeniy Brevnov 
787af7e1588SEvgeniy Brevnov   // Calculate taken and exit weights.
788af7e1588SEvgeniy Brevnov   unsigned LatchExitWeight = 0;
789af7e1588SEvgeniy Brevnov   unsigned BackedgeTakenWeight = 0;
790af7e1588SEvgeniy Brevnov 
791af7e1588SEvgeniy Brevnov   if (EstimatedTripCount > 0) {
792af7e1588SEvgeniy Brevnov     LatchExitWeight = EstimatedloopInvocationWeight;
793af7e1588SEvgeniy Brevnov     BackedgeTakenWeight = (EstimatedTripCount - 1) * LatchExitWeight;
794af7e1588SEvgeniy Brevnov   }
795af7e1588SEvgeniy Brevnov 
796af7e1588SEvgeniy Brevnov   // Make a swap if back edge is taken when condition is "false".
797af7e1588SEvgeniy Brevnov   if (LatchBranch->getSuccessor(0) != L->getHeader())
798af7e1588SEvgeniy Brevnov     std::swap(BackedgeTakenWeight, LatchExitWeight);
799af7e1588SEvgeniy Brevnov 
800af7e1588SEvgeniy Brevnov   MDBuilder MDB(LatchBranch->getContext());
801af7e1588SEvgeniy Brevnov 
802af7e1588SEvgeniy Brevnov   // Set/Update profile metadata.
803af7e1588SEvgeniy Brevnov   LatchBranch->setMetadata(
804af7e1588SEvgeniy Brevnov       LLVMContext::MD_prof,
805af7e1588SEvgeniy Brevnov       MDB.createBranchWeights(BackedgeTakenWeight, LatchExitWeight));
806af7e1588SEvgeniy Brevnov 
807af7e1588SEvgeniy Brevnov   return true;
808af7e1588SEvgeniy Brevnov }
809af7e1588SEvgeniy Brevnov 
8106cb64787SDavid Green bool llvm::hasIterationCountInvariantInParent(Loop *InnerLoop,
811395b80cdSDavid Green                                               ScalarEvolution &SE) {
812395b80cdSDavid Green   Loop *OuterL = InnerLoop->getParentLoop();
813395b80cdSDavid Green   if (!OuterL)
814395b80cdSDavid Green     return true;
815395b80cdSDavid Green 
816395b80cdSDavid Green   // Get the backedge taken count for the inner loop
817395b80cdSDavid Green   BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
818395b80cdSDavid Green   const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch);
819395b80cdSDavid Green   if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
820395b80cdSDavid Green       !InnerLoopBECountSC->getType()->isIntegerTy())
821395b80cdSDavid Green     return false;
822395b80cdSDavid Green 
823395b80cdSDavid Green   // Get whether count is invariant to the outer loop
824395b80cdSDavid Green   ScalarEvolution::LoopDisposition LD =
825395b80cdSDavid Green       SE.getLoopDisposition(InnerLoopBECountSC, OuterL);
826395b80cdSDavid Green   if (LD != ScalarEvolution::LoopInvariant)
827395b80cdSDavid Green     return false;
828395b80cdSDavid Green 
829395b80cdSDavid Green   return true;
830395b80cdSDavid Green }
831395b80cdSDavid Green 
83228ffe38bSNikita Popov Value *llvm::createMinMaxOp(IRBuilderBase &Builder,
8336594dc37SVikram TV                             RecurrenceDescriptor::MinMaxRecurrenceKind RK,
8346594dc37SVikram TV                             Value *Left, Value *Right) {
8356594dc37SVikram TV   CmpInst::Predicate P = CmpInst::ICMP_NE;
8366594dc37SVikram TV   switch (RK) {
8376594dc37SVikram TV   default:
8386594dc37SVikram TV     llvm_unreachable("Unknown min/max recurrence kind");
8396594dc37SVikram TV   case RecurrenceDescriptor::MRK_UIntMin:
8406594dc37SVikram TV     P = CmpInst::ICMP_ULT;
8416594dc37SVikram TV     break;
8426594dc37SVikram TV   case RecurrenceDescriptor::MRK_UIntMax:
8436594dc37SVikram TV     P = CmpInst::ICMP_UGT;
8446594dc37SVikram TV     break;
8456594dc37SVikram TV   case RecurrenceDescriptor::MRK_SIntMin:
8466594dc37SVikram TV     P = CmpInst::ICMP_SLT;
8476594dc37SVikram TV     break;
8486594dc37SVikram TV   case RecurrenceDescriptor::MRK_SIntMax:
8496594dc37SVikram TV     P = CmpInst::ICMP_SGT;
8506594dc37SVikram TV     break;
8516594dc37SVikram TV   case RecurrenceDescriptor::MRK_FloatMin:
8526594dc37SVikram TV     P = CmpInst::FCMP_OLT;
8536594dc37SVikram TV     break;
8546594dc37SVikram TV   case RecurrenceDescriptor::MRK_FloatMax:
8556594dc37SVikram TV     P = CmpInst::FCMP_OGT;
8566594dc37SVikram TV     break;
8576594dc37SVikram TV   }
8586594dc37SVikram TV 
8596594dc37SVikram TV   // We only match FP sequences that are 'fast', so we can unconditionally
8606594dc37SVikram TV   // set it on any generated instructions.
86128ffe38bSNikita Popov   IRBuilderBase::FastMathFlagGuard FMFG(Builder);
8626594dc37SVikram TV   FastMathFlags FMF;
8636594dc37SVikram TV   FMF.setFast();
8646594dc37SVikram TV   Builder.setFastMathFlags(FMF);
8656594dc37SVikram TV 
8666594dc37SVikram TV   Value *Cmp;
8676594dc37SVikram TV   if (RK == RecurrenceDescriptor::MRK_FloatMin ||
8686594dc37SVikram TV       RK == RecurrenceDescriptor::MRK_FloatMax)
8696594dc37SVikram TV     Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
8706594dc37SVikram TV   else
8716594dc37SVikram TV     Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
8726594dc37SVikram TV 
8736594dc37SVikram TV   Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
8746594dc37SVikram TV   return Select;
8756594dc37SVikram TV }
8766594dc37SVikram TV 
87723c2182cSSimon Pilgrim // Helper to generate an ordered reduction.
87823c2182cSSimon Pilgrim Value *
87928ffe38bSNikita Popov llvm::getOrderedReduction(IRBuilderBase &Builder, Value *Acc, Value *Src,
88023c2182cSSimon Pilgrim                           unsigned Op,
88123c2182cSSimon Pilgrim                           RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind,
88223c2182cSSimon Pilgrim                           ArrayRef<Value *> RedOps) {
883*00a10324SChristopher Tetreault   unsigned VF = cast<VectorType>(Src->getType())->getNumElements();
88423c2182cSSimon Pilgrim 
88523c2182cSSimon Pilgrim   // Extract and apply reduction ops in ascending order:
88623c2182cSSimon Pilgrim   // e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1]
88723c2182cSSimon Pilgrim   Value *Result = Acc;
88823c2182cSSimon Pilgrim   for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) {
88923c2182cSSimon Pilgrim     Value *Ext =
89023c2182cSSimon Pilgrim         Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx));
89123c2182cSSimon Pilgrim 
89223c2182cSSimon Pilgrim     if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
89323c2182cSSimon Pilgrim       Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext,
89423c2182cSSimon Pilgrim                                    "bin.rdx");
89523c2182cSSimon Pilgrim     } else {
89623c2182cSSimon Pilgrim       assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid &&
89723c2182cSSimon Pilgrim              "Invalid min/max");
8986594dc37SVikram TV       Result = createMinMaxOp(Builder, MinMaxKind, Result, Ext);
89923c2182cSSimon Pilgrim     }
90023c2182cSSimon Pilgrim 
90123c2182cSSimon Pilgrim     if (!RedOps.empty())
90223c2182cSSimon Pilgrim       propagateIRFlags(Result, RedOps);
90323c2182cSSimon Pilgrim   }
90423c2182cSSimon Pilgrim 
90523c2182cSSimon Pilgrim   return Result;
90623c2182cSSimon Pilgrim }
90723c2182cSSimon Pilgrim 
908cf9daa33SAmara Emerson // Helper to generate a log2 shuffle reduction.
909836b0f48SAmara Emerson Value *
91028ffe38bSNikita Popov llvm::getShuffleReduction(IRBuilderBase &Builder, Value *Src, unsigned Op,
911836b0f48SAmara Emerson                           RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind,
912ad62a3a2SSanjay Patel                           ArrayRef<Value *> RedOps) {
913*00a10324SChristopher Tetreault   unsigned VF = cast<VectorType>(Src->getType())->getNumElements();
914cf9daa33SAmara Emerson   // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
915cf9daa33SAmara Emerson   // and vector ops, reducing the set of values being computed by half each
916cf9daa33SAmara Emerson   // round.
917cf9daa33SAmara Emerson   assert(isPowerOf2_32(VF) &&
918cf9daa33SAmara Emerson          "Reduction emission only supported for pow2 vectors!");
919cf9daa33SAmara Emerson   Value *TmpVec = Src;
920cf9daa33SAmara Emerson   SmallVector<Constant *, 32> ShuffleMask(VF, nullptr);
921cf9daa33SAmara Emerson   for (unsigned i = VF; i != 1; i >>= 1) {
922cf9daa33SAmara Emerson     // Move the upper half of the vector to the lower half.
923cf9daa33SAmara Emerson     for (unsigned j = 0; j != i / 2; ++j)
924cf9daa33SAmara Emerson       ShuffleMask[j] = Builder.getInt32(i / 2 + j);
925cf9daa33SAmara Emerson 
926cf9daa33SAmara Emerson     // Fill the rest of the mask with undef.
927cf9daa33SAmara Emerson     std::fill(&ShuffleMask[i / 2], ShuffleMask.end(),
928cf9daa33SAmara Emerson               UndefValue::get(Builder.getInt32Ty()));
929cf9daa33SAmara Emerson 
930cf9daa33SAmara Emerson     Value *Shuf = Builder.CreateShuffleVector(
931cf9daa33SAmara Emerson         TmpVec, UndefValue::get(TmpVec->getType()),
932cf9daa33SAmara Emerson         ConstantVector::get(ShuffleMask), "rdx.shuf");
933cf9daa33SAmara Emerson 
934cf9daa33SAmara Emerson     if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
935ad62a3a2SSanjay Patel       // The builder propagates its fast-math-flags setting.
936ad62a3a2SSanjay Patel       TmpVec = Builder.CreateBinOp((Instruction::BinaryOps)Op, TmpVec, Shuf,
937ad62a3a2SSanjay Patel                                    "bin.rdx");
938cf9daa33SAmara Emerson     } else {
939cf9daa33SAmara Emerson       assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid &&
940cf9daa33SAmara Emerson              "Invalid min/max");
9416594dc37SVikram TV       TmpVec = createMinMaxOp(Builder, MinMaxKind, TmpVec, Shuf);
942cf9daa33SAmara Emerson     }
943cf9daa33SAmara Emerson     if (!RedOps.empty())
944cf9daa33SAmara Emerson       propagateIRFlags(TmpVec, RedOps);
945bc1148e7SSanjay Patel 
946bc1148e7SSanjay Patel     // We may compute the reassociated scalar ops in a way that does not
947bc1148e7SSanjay Patel     // preserve nsw/nuw etc. Conservatively, drop those flags.
948bc1148e7SSanjay Patel     if (auto *ReductionInst = dyn_cast<Instruction>(TmpVec))
949bc1148e7SSanjay Patel       ReductionInst->dropPoisonGeneratingFlags();
950cf9daa33SAmara Emerson   }
951cf9daa33SAmara Emerson   // The result is in the first element of the vector.
952cf9daa33SAmara Emerson   return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
953cf9daa33SAmara Emerson }
954cf9daa33SAmara Emerson 
955cf9daa33SAmara Emerson /// Create a simple vector reduction specified by an opcode and some
956cf9daa33SAmara Emerson /// flags (if generating min/max reductions).
957cf9daa33SAmara Emerson Value *llvm::createSimpleTargetReduction(
95828ffe38bSNikita Popov     IRBuilderBase &Builder, const TargetTransformInfo *TTI, unsigned Opcode,
959ad62a3a2SSanjay Patel     Value *Src, TargetTransformInfo::ReductionFlags Flags,
960cf9daa33SAmara Emerson     ArrayRef<Value *> RedOps) {
961*00a10324SChristopher Tetreault   auto *SrcVTy = cast<VectorType>(Src->getType());
962cf9daa33SAmara Emerson 
963cf9daa33SAmara Emerson   std::function<Value *()> BuildFunc;
964cf9daa33SAmara Emerson   using RD = RecurrenceDescriptor;
965cf9daa33SAmara Emerson   RD::MinMaxRecurrenceKind MinMaxKind = RD::MRK_Invalid;
966cf9daa33SAmara Emerson 
967cf9daa33SAmara Emerson   switch (Opcode) {
968cf9daa33SAmara Emerson   case Instruction::Add:
969cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateAddReduce(Src); };
970cf9daa33SAmara Emerson     break;
971cf9daa33SAmara Emerson   case Instruction::Mul:
972cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateMulReduce(Src); };
973cf9daa33SAmara Emerson     break;
974cf9daa33SAmara Emerson   case Instruction::And:
975cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateAndReduce(Src); };
976cf9daa33SAmara Emerson     break;
977cf9daa33SAmara Emerson   case Instruction::Or:
978cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateOrReduce(Src); };
979cf9daa33SAmara Emerson     break;
980cf9daa33SAmara Emerson   case Instruction::Xor:
981cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateXorReduce(Src); };
982cf9daa33SAmara Emerson     break;
983cf9daa33SAmara Emerson   case Instruction::FAdd:
984cf9daa33SAmara Emerson     BuildFunc = [&]() {
985cbeb563cSSander de Smalen       auto Rdx = Builder.CreateFAddReduce(
986*00a10324SChristopher Tetreault           Constant::getNullValue(SrcVTy->getElementType()), Src);
987cf9daa33SAmara Emerson       return Rdx;
988cf9daa33SAmara Emerson     };
989cf9daa33SAmara Emerson     break;
990cf9daa33SAmara Emerson   case Instruction::FMul:
991cf9daa33SAmara Emerson     BuildFunc = [&]() {
992*00a10324SChristopher Tetreault       Type *Ty = SrcVTy->getElementType();
993cbeb563cSSander de Smalen       auto Rdx = Builder.CreateFMulReduce(ConstantFP::get(Ty, 1.0), Src);
994cf9daa33SAmara Emerson       return Rdx;
995cf9daa33SAmara Emerson     };
996cf9daa33SAmara Emerson     break;
997cf9daa33SAmara Emerson   case Instruction::ICmp:
998cf9daa33SAmara Emerson     if (Flags.IsMaxOp) {
999cf9daa33SAmara Emerson       MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMax : RD::MRK_UIntMax;
1000cf9daa33SAmara Emerson       BuildFunc = [&]() {
1001cf9daa33SAmara Emerson         return Builder.CreateIntMaxReduce(Src, Flags.IsSigned);
1002cf9daa33SAmara Emerson       };
1003cf9daa33SAmara Emerson     } else {
1004cf9daa33SAmara Emerson       MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMin : RD::MRK_UIntMin;
1005cf9daa33SAmara Emerson       BuildFunc = [&]() {
1006cf9daa33SAmara Emerson         return Builder.CreateIntMinReduce(Src, Flags.IsSigned);
1007cf9daa33SAmara Emerson       };
1008cf9daa33SAmara Emerson     }
1009cf9daa33SAmara Emerson     break;
1010cf9daa33SAmara Emerson   case Instruction::FCmp:
1011cf9daa33SAmara Emerson     if (Flags.IsMaxOp) {
1012cf9daa33SAmara Emerson       MinMaxKind = RD::MRK_FloatMax;
1013cf9daa33SAmara Emerson       BuildFunc = [&]() { return Builder.CreateFPMaxReduce(Src, Flags.NoNaN); };
1014cf9daa33SAmara Emerson     } else {
1015cf9daa33SAmara Emerson       MinMaxKind = RD::MRK_FloatMin;
1016cf9daa33SAmara Emerson       BuildFunc = [&]() { return Builder.CreateFPMinReduce(Src, Flags.NoNaN); };
1017cf9daa33SAmara Emerson     }
1018cf9daa33SAmara Emerson     break;
1019cf9daa33SAmara Emerson   default:
1020cf9daa33SAmara Emerson     llvm_unreachable("Unhandled opcode");
1021cf9daa33SAmara Emerson     break;
1022cf9daa33SAmara Emerson   }
1023ec7e4a9aSDavid Green   if (ForceReductionIntrinsic ||
1024ec7e4a9aSDavid Green       TTI->useReductionIntrinsic(Opcode, Src->getType(), Flags))
1025cf9daa33SAmara Emerson     return BuildFunc();
1026ad62a3a2SSanjay Patel   return getShuffleReduction(Builder, Src, Opcode, MinMaxKind, RedOps);
1027cf9daa33SAmara Emerson }
1028cf9daa33SAmara Emerson 
1029cf9daa33SAmara Emerson /// Create a vector reduction using a given recurrence descriptor.
103028ffe38bSNikita Popov Value *llvm::createTargetReduction(IRBuilderBase &B,
1031cf9daa33SAmara Emerson                                    const TargetTransformInfo *TTI,
1032cf9daa33SAmara Emerson                                    RecurrenceDescriptor &Desc, Value *Src,
1033cf9daa33SAmara Emerson                                    bool NoNaN) {
1034cf9daa33SAmara Emerson   // TODO: Support in-order reductions based on the recurrence descriptor.
10353e069f57SSanjay Patel   using RD = RecurrenceDescriptor;
10363e069f57SSanjay Patel   RD::RecurrenceKind RecKind = Desc.getRecurrenceKind();
1037cf9daa33SAmara Emerson   TargetTransformInfo::ReductionFlags Flags;
1038cf9daa33SAmara Emerson   Flags.NoNaN = NoNaN;
1039ad62a3a2SSanjay Patel 
1040ad62a3a2SSanjay Patel   // All ops in the reduction inherit fast-math-flags from the recurrence
1041ad62a3a2SSanjay Patel   // descriptor.
104228ffe38bSNikita Popov   IRBuilderBase::FastMathFlagGuard FMFGuard(B);
1043ad62a3a2SSanjay Patel   B.setFastMathFlags(Desc.getFastMathFlags());
1044ad62a3a2SSanjay Patel 
1045cf9daa33SAmara Emerson   switch (RecKind) {
10463e069f57SSanjay Patel   case RD::RK_FloatAdd:
1047ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FAdd, Src, Flags);
10483e069f57SSanjay Patel   case RD::RK_FloatMult:
1049ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FMul, Src, Flags);
10503e069f57SSanjay Patel   case RD::RK_IntegerAdd:
1051ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Add, Src, Flags);
10523e069f57SSanjay Patel   case RD::RK_IntegerMult:
1053ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Mul, Src, Flags);
10543e069f57SSanjay Patel   case RD::RK_IntegerAnd:
1055ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::And, Src, Flags);
10563e069f57SSanjay Patel   case RD::RK_IntegerOr:
1057ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Or, Src, Flags);
10583e069f57SSanjay Patel   case RD::RK_IntegerXor:
1059ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Xor, Src, Flags);
10603e069f57SSanjay Patel   case RD::RK_IntegerMinMax: {
10613e069f57SSanjay Patel     RD::MinMaxRecurrenceKind MMKind = Desc.getMinMaxRecurrenceKind();
10623e069f57SSanjay Patel     Flags.IsMaxOp = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_UIntMax);
10633e069f57SSanjay Patel     Flags.IsSigned = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_SIntMin);
1064ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::ICmp, Src, Flags);
1065cf9daa33SAmara Emerson   }
10663e069f57SSanjay Patel   case RD::RK_FloatMinMax: {
10673e069f57SSanjay Patel     Flags.IsMaxOp = Desc.getMinMaxRecurrenceKind() == RD::MRK_FloatMax;
1068ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FCmp, Src, Flags);
1069cf9daa33SAmara Emerson   }
1070cf9daa33SAmara Emerson   default:
1071cf9daa33SAmara Emerson     llvm_unreachable("Unhandled RecKind");
1072cf9daa33SAmara Emerson   }
1073cf9daa33SAmara Emerson }
1074cf9daa33SAmara Emerson 
1075a61f4b89SDinar Temirbulatov void llvm::propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue) {
1076a61f4b89SDinar Temirbulatov   auto *VecOp = dyn_cast<Instruction>(I);
1077a61f4b89SDinar Temirbulatov   if (!VecOp)
1078a61f4b89SDinar Temirbulatov     return;
1079a61f4b89SDinar Temirbulatov   auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0])
1080a61f4b89SDinar Temirbulatov                                             : dyn_cast<Instruction>(OpValue);
1081a61f4b89SDinar Temirbulatov   if (!Intersection)
1082a61f4b89SDinar Temirbulatov     return;
1083a61f4b89SDinar Temirbulatov   const unsigned Opcode = Intersection->getOpcode();
1084a61f4b89SDinar Temirbulatov   VecOp->copyIRFlags(Intersection);
1085a61f4b89SDinar Temirbulatov   for (auto *V : VL) {
1086a61f4b89SDinar Temirbulatov     auto *Instr = dyn_cast<Instruction>(V);
1087a61f4b89SDinar Temirbulatov     if (!Instr)
1088a61f4b89SDinar Temirbulatov       continue;
1089a61f4b89SDinar Temirbulatov     if (OpValue == nullptr || Opcode == Instr->getOpcode())
1090a61f4b89SDinar Temirbulatov       VecOp->andIRFlags(V);
1091cf9daa33SAmara Emerson   }
1092cf9daa33SAmara Emerson }
1093a78dc4d6SMax Kazantsev 
1094a78dc4d6SMax Kazantsev bool llvm::isKnownNegativeInLoop(const SCEV *S, const Loop *L,
1095a78dc4d6SMax Kazantsev                                  ScalarEvolution &SE) {
1096a78dc4d6SMax Kazantsev   const SCEV *Zero = SE.getZero(S->getType());
1097a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
1098a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, S, Zero);
1099a78dc4d6SMax Kazantsev }
1100a78dc4d6SMax Kazantsev 
1101a78dc4d6SMax Kazantsev bool llvm::isKnownNonNegativeInLoop(const SCEV *S, const Loop *L,
1102a78dc4d6SMax Kazantsev                                     ScalarEvolution &SE) {
1103a78dc4d6SMax Kazantsev   const SCEV *Zero = SE.getZero(S->getType());
1104a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
1105a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, S, Zero);
1106a78dc4d6SMax Kazantsev }
1107a78dc4d6SMax Kazantsev 
1108a78dc4d6SMax Kazantsev bool llvm::cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
1109a78dc4d6SMax Kazantsev                              bool Signed) {
1110a78dc4d6SMax Kazantsev   unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1111a78dc4d6SMax Kazantsev   APInt Min = Signed ? APInt::getSignedMinValue(BitWidth) :
1112a78dc4d6SMax Kazantsev     APInt::getMinValue(BitWidth);
1113a78dc4d6SMax Kazantsev   auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1114a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
1115a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, Predicate, S,
1116a78dc4d6SMax Kazantsev                                      SE.getConstant(Min));
1117a78dc4d6SMax Kazantsev }
1118a78dc4d6SMax Kazantsev 
1119a78dc4d6SMax Kazantsev bool llvm::cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
1120a78dc4d6SMax Kazantsev                              bool Signed) {
1121a78dc4d6SMax Kazantsev   unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
1122a78dc4d6SMax Kazantsev   APInt Max = Signed ? APInt::getSignedMaxValue(BitWidth) :
1123a78dc4d6SMax Kazantsev     APInt::getMaxValue(BitWidth);
1124a78dc4d6SMax Kazantsev   auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1125a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
1126a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, Predicate, S,
1127a78dc4d6SMax Kazantsev                                      SE.getConstant(Max));
1128a78dc4d6SMax Kazantsev }
112993175a5cSSjoerd Meijer 
113093175a5cSSjoerd Meijer //===----------------------------------------------------------------------===//
113193175a5cSSjoerd Meijer // rewriteLoopExitValues - Optimize IV users outside the loop.
113293175a5cSSjoerd Meijer // As a side effect, reduces the amount of IV processing within the loop.
113393175a5cSSjoerd Meijer //===----------------------------------------------------------------------===//
113493175a5cSSjoerd Meijer 
113593175a5cSSjoerd Meijer // Return true if the SCEV expansion generated by the rewriter can replace the
113693175a5cSSjoerd Meijer // original value. SCEV guarantees that it produces the same value, but the way
113793175a5cSSjoerd Meijer // it is produced may be illegal IR.  Ideally, this function will only be
113893175a5cSSjoerd Meijer // called for verification.
113993175a5cSSjoerd Meijer static bool isValidRewrite(ScalarEvolution *SE, Value *FromVal, Value *ToVal) {
114093175a5cSSjoerd Meijer   // If an SCEV expression subsumed multiple pointers, its expansion could
114193175a5cSSjoerd Meijer   // reassociate the GEP changing the base pointer. This is illegal because the
114293175a5cSSjoerd Meijer   // final address produced by a GEP chain must be inbounds relative to its
114393175a5cSSjoerd Meijer   // underlying object. Otherwise basic alias analysis, among other things,
114493175a5cSSjoerd Meijer   // could fail in a dangerous way. Ultimately, SCEV will be improved to avoid
114593175a5cSSjoerd Meijer   // producing an expression involving multiple pointers. Until then, we must
114693175a5cSSjoerd Meijer   // bail out here.
114793175a5cSSjoerd Meijer   //
114893175a5cSSjoerd Meijer   // Retrieve the pointer operand of the GEP. Don't use GetUnderlyingObject
114993175a5cSSjoerd Meijer   // because it understands lcssa phis while SCEV does not.
115093175a5cSSjoerd Meijer   Value *FromPtr = FromVal;
115193175a5cSSjoerd Meijer   Value *ToPtr = ToVal;
115293175a5cSSjoerd Meijer   if (auto *GEP = dyn_cast<GEPOperator>(FromVal))
115393175a5cSSjoerd Meijer     FromPtr = GEP->getPointerOperand();
115493175a5cSSjoerd Meijer 
115593175a5cSSjoerd Meijer   if (auto *GEP = dyn_cast<GEPOperator>(ToVal))
115693175a5cSSjoerd Meijer     ToPtr = GEP->getPointerOperand();
115793175a5cSSjoerd Meijer 
115893175a5cSSjoerd Meijer   if (FromPtr != FromVal || ToPtr != ToVal) {
115993175a5cSSjoerd Meijer     // Quickly check the common case
116093175a5cSSjoerd Meijer     if (FromPtr == ToPtr)
116193175a5cSSjoerd Meijer       return true;
116293175a5cSSjoerd Meijer 
116393175a5cSSjoerd Meijer     // SCEV may have rewritten an expression that produces the GEP's pointer
116493175a5cSSjoerd Meijer     // operand. That's ok as long as the pointer operand has the same base
116593175a5cSSjoerd Meijer     // pointer. Unlike GetUnderlyingObject(), getPointerBase() will find the
116693175a5cSSjoerd Meijer     // base of a recurrence. This handles the case in which SCEV expansion
116793175a5cSSjoerd Meijer     // converts a pointer type recurrence into a nonrecurrent pointer base
116893175a5cSSjoerd Meijer     // indexed by an integer recurrence.
116993175a5cSSjoerd Meijer 
117093175a5cSSjoerd Meijer     // If the GEP base pointer is a vector of pointers, abort.
117193175a5cSSjoerd Meijer     if (!FromPtr->getType()->isPointerTy() || !ToPtr->getType()->isPointerTy())
117293175a5cSSjoerd Meijer       return false;
117393175a5cSSjoerd Meijer 
117493175a5cSSjoerd Meijer     const SCEV *FromBase = SE->getPointerBase(SE->getSCEV(FromPtr));
117593175a5cSSjoerd Meijer     const SCEV *ToBase = SE->getPointerBase(SE->getSCEV(ToPtr));
117693175a5cSSjoerd Meijer     if (FromBase == ToBase)
117793175a5cSSjoerd Meijer       return true;
117893175a5cSSjoerd Meijer 
117993175a5cSSjoerd Meijer     LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: GEP rewrite bail out "
118093175a5cSSjoerd Meijer                       << *FromBase << " != " << *ToBase << "\n");
118193175a5cSSjoerd Meijer 
118293175a5cSSjoerd Meijer     return false;
118393175a5cSSjoerd Meijer   }
118493175a5cSSjoerd Meijer   return true;
118593175a5cSSjoerd Meijer }
118693175a5cSSjoerd Meijer 
118793175a5cSSjoerd Meijer static bool hasHardUserWithinLoop(const Loop *L, const Instruction *I) {
118893175a5cSSjoerd Meijer   SmallPtrSet<const Instruction *, 8> Visited;
118993175a5cSSjoerd Meijer   SmallVector<const Instruction *, 8> WorkList;
119093175a5cSSjoerd Meijer   Visited.insert(I);
119193175a5cSSjoerd Meijer   WorkList.push_back(I);
119293175a5cSSjoerd Meijer   while (!WorkList.empty()) {
119393175a5cSSjoerd Meijer     const Instruction *Curr = WorkList.pop_back_val();
119493175a5cSSjoerd Meijer     // This use is outside the loop, nothing to do.
119593175a5cSSjoerd Meijer     if (!L->contains(Curr))
119693175a5cSSjoerd Meijer       continue;
119793175a5cSSjoerd Meijer     // Do we assume it is a "hard" use which will not be eliminated easily?
119893175a5cSSjoerd Meijer     if (Curr->mayHaveSideEffects())
119993175a5cSSjoerd Meijer       return true;
120093175a5cSSjoerd Meijer     // Otherwise, add all its users to worklist.
120193175a5cSSjoerd Meijer     for (auto U : Curr->users()) {
120293175a5cSSjoerd Meijer       auto *UI = cast<Instruction>(U);
120393175a5cSSjoerd Meijer       if (Visited.insert(UI).second)
120493175a5cSSjoerd Meijer         WorkList.push_back(UI);
120593175a5cSSjoerd Meijer     }
120693175a5cSSjoerd Meijer   }
120793175a5cSSjoerd Meijer   return false;
120893175a5cSSjoerd Meijer }
120993175a5cSSjoerd Meijer 
121093175a5cSSjoerd Meijer // Collect information about PHI nodes which can be transformed in
121193175a5cSSjoerd Meijer // rewriteLoopExitValues.
121293175a5cSSjoerd Meijer struct RewritePhi {
121393175a5cSSjoerd Meijer   PHINode *PN;
121493175a5cSSjoerd Meijer   unsigned Ith;   // Ith incoming value.
121593175a5cSSjoerd Meijer   Value *Val;     // Exit value after expansion.
121693175a5cSSjoerd Meijer   bool HighCost;  // High Cost when expansion.
121793175a5cSSjoerd Meijer 
121893175a5cSSjoerd Meijer   RewritePhi(PHINode *P, unsigned I, Value *V, bool H)
121993175a5cSSjoerd Meijer       : PN(P), Ith(I), Val(V), HighCost(H) {}
122093175a5cSSjoerd Meijer };
122193175a5cSSjoerd Meijer 
122293175a5cSSjoerd Meijer // Check whether it is possible to delete the loop after rewriting exit
122393175a5cSSjoerd Meijer // value. If it is possible, ignore ReplaceExitValue and do rewriting
122493175a5cSSjoerd Meijer // aggressively.
122593175a5cSSjoerd Meijer static bool canLoopBeDeleted(Loop *L, SmallVector<RewritePhi, 8> &RewritePhiSet) {
122693175a5cSSjoerd Meijer   BasicBlock *Preheader = L->getLoopPreheader();
122793175a5cSSjoerd Meijer   // If there is no preheader, the loop will not be deleted.
122893175a5cSSjoerd Meijer   if (!Preheader)
122993175a5cSSjoerd Meijer     return false;
123093175a5cSSjoerd Meijer 
123193175a5cSSjoerd Meijer   // In LoopDeletion pass Loop can be deleted when ExitingBlocks.size() > 1.
123293175a5cSSjoerd Meijer   // We obviate multiple ExitingBlocks case for simplicity.
123393175a5cSSjoerd Meijer   // TODO: If we see testcase with multiple ExitingBlocks can be deleted
123493175a5cSSjoerd Meijer   // after exit value rewriting, we can enhance the logic here.
123593175a5cSSjoerd Meijer   SmallVector<BasicBlock *, 4> ExitingBlocks;
123693175a5cSSjoerd Meijer   L->getExitingBlocks(ExitingBlocks);
123793175a5cSSjoerd Meijer   SmallVector<BasicBlock *, 8> ExitBlocks;
123893175a5cSSjoerd Meijer   L->getUniqueExitBlocks(ExitBlocks);
123993175a5cSSjoerd Meijer   if (ExitBlocks.size() != 1 || ExitingBlocks.size() != 1)
124093175a5cSSjoerd Meijer     return false;
124193175a5cSSjoerd Meijer 
124293175a5cSSjoerd Meijer   BasicBlock *ExitBlock = ExitBlocks[0];
124393175a5cSSjoerd Meijer   BasicBlock::iterator BI = ExitBlock->begin();
124493175a5cSSjoerd Meijer   while (PHINode *P = dyn_cast<PHINode>(BI)) {
124593175a5cSSjoerd Meijer     Value *Incoming = P->getIncomingValueForBlock(ExitingBlocks[0]);
124693175a5cSSjoerd Meijer 
124793175a5cSSjoerd Meijer     // If the Incoming value of P is found in RewritePhiSet, we know it
124893175a5cSSjoerd Meijer     // could be rewritten to use a loop invariant value in transformation
124993175a5cSSjoerd Meijer     // phase later. Skip it in the loop invariant check below.
125093175a5cSSjoerd Meijer     bool found = false;
125193175a5cSSjoerd Meijer     for (const RewritePhi &Phi : RewritePhiSet) {
125293175a5cSSjoerd Meijer       unsigned i = Phi.Ith;
125393175a5cSSjoerd Meijer       if (Phi.PN == P && (Phi.PN)->getIncomingValue(i) == Incoming) {
125493175a5cSSjoerd Meijer         found = true;
125593175a5cSSjoerd Meijer         break;
125693175a5cSSjoerd Meijer       }
125793175a5cSSjoerd Meijer     }
125893175a5cSSjoerd Meijer 
125993175a5cSSjoerd Meijer     Instruction *I;
126093175a5cSSjoerd Meijer     if (!found && (I = dyn_cast<Instruction>(Incoming)))
126193175a5cSSjoerd Meijer       if (!L->hasLoopInvariantOperands(I))
126293175a5cSSjoerd Meijer         return false;
126393175a5cSSjoerd Meijer 
126493175a5cSSjoerd Meijer     ++BI;
126593175a5cSSjoerd Meijer   }
126693175a5cSSjoerd Meijer 
126793175a5cSSjoerd Meijer   for (auto *BB : L->blocks())
126893175a5cSSjoerd Meijer     if (llvm::any_of(*BB, [](Instruction &I) {
126993175a5cSSjoerd Meijer           return I.mayHaveSideEffects();
127093175a5cSSjoerd Meijer         }))
127193175a5cSSjoerd Meijer       return false;
127293175a5cSSjoerd Meijer 
127393175a5cSSjoerd Meijer   return true;
127493175a5cSSjoerd Meijer }
127593175a5cSSjoerd Meijer 
12760789f280SRoman Lebedev int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
12770789f280SRoman Lebedev                                 ScalarEvolution *SE,
12780789f280SRoman Lebedev                                 const TargetTransformInfo *TTI,
12790789f280SRoman Lebedev                                 SCEVExpander &Rewriter, DominatorTree *DT,
12800789f280SRoman Lebedev                                 ReplaceExitVal ReplaceExitValue,
128193175a5cSSjoerd Meijer                                 SmallVector<WeakTrackingVH, 16> &DeadInsts) {
128293175a5cSSjoerd Meijer   // Check a pre-condition.
128393175a5cSSjoerd Meijer   assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
128493175a5cSSjoerd Meijer          "Indvars did not preserve LCSSA!");
128593175a5cSSjoerd Meijer 
128693175a5cSSjoerd Meijer   SmallVector<BasicBlock*, 8> ExitBlocks;
128793175a5cSSjoerd Meijer   L->getUniqueExitBlocks(ExitBlocks);
128893175a5cSSjoerd Meijer 
128993175a5cSSjoerd Meijer   SmallVector<RewritePhi, 8> RewritePhiSet;
129093175a5cSSjoerd Meijer   // Find all values that are computed inside the loop, but used outside of it.
129193175a5cSSjoerd Meijer   // Because of LCSSA, these values will only occur in LCSSA PHI Nodes.  Scan
129293175a5cSSjoerd Meijer   // the exit blocks of the loop to find them.
129393175a5cSSjoerd Meijer   for (BasicBlock *ExitBB : ExitBlocks) {
129493175a5cSSjoerd Meijer     // If there are no PHI nodes in this exit block, then no values defined
129593175a5cSSjoerd Meijer     // inside the loop are used on this path, skip it.
129693175a5cSSjoerd Meijer     PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
129793175a5cSSjoerd Meijer     if (!PN) continue;
129893175a5cSSjoerd Meijer 
129993175a5cSSjoerd Meijer     unsigned NumPreds = PN->getNumIncomingValues();
130093175a5cSSjoerd Meijer 
130193175a5cSSjoerd Meijer     // Iterate over all of the PHI nodes.
130293175a5cSSjoerd Meijer     BasicBlock::iterator BBI = ExitBB->begin();
130393175a5cSSjoerd Meijer     while ((PN = dyn_cast<PHINode>(BBI++))) {
130493175a5cSSjoerd Meijer       if (PN->use_empty())
130593175a5cSSjoerd Meijer         continue; // dead use, don't replace it
130693175a5cSSjoerd Meijer 
130793175a5cSSjoerd Meijer       if (!SE->isSCEVable(PN->getType()))
130893175a5cSSjoerd Meijer         continue;
130993175a5cSSjoerd Meijer 
131093175a5cSSjoerd Meijer       // It's necessary to tell ScalarEvolution about this explicitly so that
131193175a5cSSjoerd Meijer       // it can walk the def-use list and forget all SCEVs, as it may not be
131293175a5cSSjoerd Meijer       // watching the PHI itself. Once the new exit value is in place, there
131393175a5cSSjoerd Meijer       // may not be a def-use connection between the loop and every instruction
131493175a5cSSjoerd Meijer       // which got a SCEVAddRecExpr for that loop.
131593175a5cSSjoerd Meijer       SE->forgetValue(PN);
131693175a5cSSjoerd Meijer 
131793175a5cSSjoerd Meijer       // Iterate over all of the values in all the PHI nodes.
131893175a5cSSjoerd Meijer       for (unsigned i = 0; i != NumPreds; ++i) {
131993175a5cSSjoerd Meijer         // If the value being merged in is not integer or is not defined
132093175a5cSSjoerd Meijer         // in the loop, skip it.
132193175a5cSSjoerd Meijer         Value *InVal = PN->getIncomingValue(i);
132293175a5cSSjoerd Meijer         if (!isa<Instruction>(InVal))
132393175a5cSSjoerd Meijer           continue;
132493175a5cSSjoerd Meijer 
132593175a5cSSjoerd Meijer         // If this pred is for a subloop, not L itself, skip it.
132693175a5cSSjoerd Meijer         if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
132793175a5cSSjoerd Meijer           continue; // The Block is in a subloop, skip it.
132893175a5cSSjoerd Meijer 
132993175a5cSSjoerd Meijer         // Check that InVal is defined in the loop.
133093175a5cSSjoerd Meijer         Instruction *Inst = cast<Instruction>(InVal);
133193175a5cSSjoerd Meijer         if (!L->contains(Inst))
133293175a5cSSjoerd Meijer           continue;
133393175a5cSSjoerd Meijer 
133493175a5cSSjoerd Meijer         // Okay, this instruction has a user outside of the current loop
133593175a5cSSjoerd Meijer         // and varies predictably *inside* the loop.  Evaluate the value it
133693175a5cSSjoerd Meijer         // contains when the loop exits, if possible.  We prefer to start with
133793175a5cSSjoerd Meijer         // expressions which are true for all exits (so as to maximize
133893175a5cSSjoerd Meijer         // expression reuse by the SCEVExpander), but resort to per-exit
133993175a5cSSjoerd Meijer         // evaluation if that fails.
134093175a5cSSjoerd Meijer         const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
134193175a5cSSjoerd Meijer         if (isa<SCEVCouldNotCompute>(ExitValue) ||
134293175a5cSSjoerd Meijer             !SE->isLoopInvariant(ExitValue, L) ||
134393175a5cSSjoerd Meijer             !isSafeToExpand(ExitValue, *SE)) {
134493175a5cSSjoerd Meijer           // TODO: This should probably be sunk into SCEV in some way; maybe a
134593175a5cSSjoerd Meijer           // getSCEVForExit(SCEV*, L, ExitingBB)?  It can be generalized for
134693175a5cSSjoerd Meijer           // most SCEV expressions and other recurrence types (e.g. shift
134793175a5cSSjoerd Meijer           // recurrences).  Is there existing code we can reuse?
134893175a5cSSjoerd Meijer           const SCEV *ExitCount = SE->getExitCount(L, PN->getIncomingBlock(i));
134993175a5cSSjoerd Meijer           if (isa<SCEVCouldNotCompute>(ExitCount))
135093175a5cSSjoerd Meijer             continue;
135193175a5cSSjoerd Meijer           if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Inst)))
135293175a5cSSjoerd Meijer             if (AddRec->getLoop() == L)
135393175a5cSSjoerd Meijer               ExitValue = AddRec->evaluateAtIteration(ExitCount, *SE);
135493175a5cSSjoerd Meijer           if (isa<SCEVCouldNotCompute>(ExitValue) ||
135593175a5cSSjoerd Meijer               !SE->isLoopInvariant(ExitValue, L) ||
135693175a5cSSjoerd Meijer               !isSafeToExpand(ExitValue, *SE))
135793175a5cSSjoerd Meijer             continue;
135893175a5cSSjoerd Meijer         }
135993175a5cSSjoerd Meijer 
136093175a5cSSjoerd Meijer         // Computing the value outside of the loop brings no benefit if it is
136193175a5cSSjoerd Meijer         // definitely used inside the loop in a way which can not be optimized
13627d572ef2SRoman Lebedev         // away. Avoid doing so unless we know we have a value which computes
13637d572ef2SRoman Lebedev         // the ExitValue already. TODO: This should be merged into SCEV
13647d572ef2SRoman Lebedev         // expander to leverage its knowledge of existing expressions.
13657d572ef2SRoman Lebedev         if (ReplaceExitValue != AlwaysRepl && !isa<SCEVConstant>(ExitValue) &&
13667d572ef2SRoman Lebedev             !isa<SCEVUnknown>(ExitValue) && hasHardUserWithinLoop(L, Inst))
136793175a5cSSjoerd Meijer           continue;
136893175a5cSSjoerd Meijer 
13697d572ef2SRoman Lebedev         bool HighCost = Rewriter.isHighCostExpansion(
13707d572ef2SRoman Lebedev             ExitValue, L, SCEVCheapExpansionBudget, TTI, Inst);
137193175a5cSSjoerd Meijer         Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst);
137293175a5cSSjoerd Meijer 
137393175a5cSSjoerd Meijer         LLVM_DEBUG(dbgs() << "rewriteLoopExitValues: AfterLoopVal = "
137493175a5cSSjoerd Meijer                           << *ExitVal << '\n' << "  LoopVal = " << *Inst
137593175a5cSSjoerd Meijer                           << "\n");
137693175a5cSSjoerd Meijer 
137793175a5cSSjoerd Meijer         if (!isValidRewrite(SE, Inst, ExitVal)) {
137893175a5cSSjoerd Meijer           DeadInsts.push_back(ExitVal);
137993175a5cSSjoerd Meijer           continue;
138093175a5cSSjoerd Meijer         }
138193175a5cSSjoerd Meijer 
138293175a5cSSjoerd Meijer #ifndef NDEBUG
138393175a5cSSjoerd Meijer         // If we reuse an instruction from a loop which is neither L nor one of
138493175a5cSSjoerd Meijer         // its containing loops, we end up breaking LCSSA form for this loop by
138593175a5cSSjoerd Meijer         // creating a new use of its instruction.
138693175a5cSSjoerd Meijer         if (auto *ExitInsn = dyn_cast<Instruction>(ExitVal))
138793175a5cSSjoerd Meijer           if (auto *EVL = LI->getLoopFor(ExitInsn->getParent()))
138893175a5cSSjoerd Meijer             if (EVL != L)
138993175a5cSSjoerd Meijer               assert(EVL->contains(L) && "LCSSA breach detected!");
139093175a5cSSjoerd Meijer #endif
139193175a5cSSjoerd Meijer 
139293175a5cSSjoerd Meijer         // Collect all the candidate PHINodes to be rewritten.
139393175a5cSSjoerd Meijer         RewritePhiSet.emplace_back(PN, i, ExitVal, HighCost);
139493175a5cSSjoerd Meijer       }
139593175a5cSSjoerd Meijer     }
139693175a5cSSjoerd Meijer   }
139793175a5cSSjoerd Meijer 
139893175a5cSSjoerd Meijer   bool LoopCanBeDel = canLoopBeDeleted(L, RewritePhiSet);
139993175a5cSSjoerd Meijer   int NumReplaced = 0;
140093175a5cSSjoerd Meijer 
140193175a5cSSjoerd Meijer   // Transformation.
140293175a5cSSjoerd Meijer   for (const RewritePhi &Phi : RewritePhiSet) {
140393175a5cSSjoerd Meijer     PHINode *PN = Phi.PN;
140493175a5cSSjoerd Meijer     Value *ExitVal = Phi.Val;
140593175a5cSSjoerd Meijer 
140693175a5cSSjoerd Meijer     // Only do the rewrite when the ExitValue can be expanded cheaply.
140793175a5cSSjoerd Meijer     // If LoopCanBeDel is true, rewrite exit value aggressively.
140893175a5cSSjoerd Meijer     if (ReplaceExitValue == OnlyCheapRepl && !LoopCanBeDel && Phi.HighCost) {
140993175a5cSSjoerd Meijer       DeadInsts.push_back(ExitVal);
141093175a5cSSjoerd Meijer       continue;
141193175a5cSSjoerd Meijer     }
141293175a5cSSjoerd Meijer 
141393175a5cSSjoerd Meijer     NumReplaced++;
141493175a5cSSjoerd Meijer     Instruction *Inst = cast<Instruction>(PN->getIncomingValue(Phi.Ith));
141593175a5cSSjoerd Meijer     PN->setIncomingValue(Phi.Ith, ExitVal);
141693175a5cSSjoerd Meijer 
141793175a5cSSjoerd Meijer     // If this instruction is dead now, delete it. Don't do it now to avoid
141893175a5cSSjoerd Meijer     // invalidating iterators.
141993175a5cSSjoerd Meijer     if (isInstructionTriviallyDead(Inst, TLI))
142093175a5cSSjoerd Meijer       DeadInsts.push_back(Inst);
142193175a5cSSjoerd Meijer 
142293175a5cSSjoerd Meijer     // Replace PN with ExitVal if that is legal and does not break LCSSA.
142393175a5cSSjoerd Meijer     if (PN->getNumIncomingValues() == 1 &&
142493175a5cSSjoerd Meijer         LI->replacementPreservesLCSSAForm(PN, ExitVal)) {
142593175a5cSSjoerd Meijer       PN->replaceAllUsesWith(ExitVal);
142693175a5cSSjoerd Meijer       PN->eraseFromParent();
142793175a5cSSjoerd Meijer     }
142893175a5cSSjoerd Meijer   }
142993175a5cSSjoerd Meijer 
143093175a5cSSjoerd Meijer   // The insertion point instruction may have been deleted; clear it out
143193175a5cSSjoerd Meijer   // so that the rewriter doesn't trip over it later.
143293175a5cSSjoerd Meijer   Rewriter.clearInsertPoint();
143393175a5cSSjoerd Meijer   return NumReplaced;
143493175a5cSSjoerd Meijer }
1435af7e1588SEvgeniy Brevnov 
1436af7e1588SEvgeniy Brevnov /// Set weights for \p UnrolledLoop and \p RemainderLoop based on weights for
1437af7e1588SEvgeniy Brevnov /// \p OrigLoop.
1438af7e1588SEvgeniy Brevnov void llvm::setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop,
1439af7e1588SEvgeniy Brevnov                                         Loop *RemainderLoop, uint64_t UF) {
1440af7e1588SEvgeniy Brevnov   assert(UF > 0 && "Zero unrolled factor is not supported");
1441af7e1588SEvgeniy Brevnov   assert(UnrolledLoop != RemainderLoop &&
1442af7e1588SEvgeniy Brevnov          "Unrolled and Remainder loops are expected to distinct");
1443af7e1588SEvgeniy Brevnov 
1444af7e1588SEvgeniy Brevnov   // Get number of iterations in the original scalar loop.
1445af7e1588SEvgeniy Brevnov   unsigned OrigLoopInvocationWeight = 0;
1446af7e1588SEvgeniy Brevnov   Optional<unsigned> OrigAverageTripCount =
1447af7e1588SEvgeniy Brevnov       getLoopEstimatedTripCount(OrigLoop, &OrigLoopInvocationWeight);
1448af7e1588SEvgeniy Brevnov   if (!OrigAverageTripCount)
1449af7e1588SEvgeniy Brevnov     return;
1450af7e1588SEvgeniy Brevnov 
1451af7e1588SEvgeniy Brevnov   // Calculate number of iterations in unrolled loop.
1452af7e1588SEvgeniy Brevnov   unsigned UnrolledAverageTripCount = *OrigAverageTripCount / UF;
1453af7e1588SEvgeniy Brevnov   // Calculate number of iterations for remainder loop.
1454af7e1588SEvgeniy Brevnov   unsigned RemainderAverageTripCount = *OrigAverageTripCount % UF;
1455af7e1588SEvgeniy Brevnov 
1456af7e1588SEvgeniy Brevnov   setLoopEstimatedTripCount(UnrolledLoop, UnrolledAverageTripCount,
1457af7e1588SEvgeniy Brevnov                             OrigLoopInvocationWeight);
1458af7e1588SEvgeniy Brevnov   setLoopEstimatedTripCount(RemainderLoop, RemainderAverageTripCount,
1459af7e1588SEvgeniy Brevnov                             OrigLoopInvocationWeight);
1460af7e1588SEvgeniy Brevnov }
1461388de9dfSAlina Sbirlea 
1462388de9dfSAlina Sbirlea /// Utility that implements appending of loops onto a worklist.
1463388de9dfSAlina Sbirlea /// Loops are added in preorder (analogous for reverse postorder for trees),
1464388de9dfSAlina Sbirlea /// and the worklist is processed LIFO.
1465388de9dfSAlina Sbirlea template <typename RangeT>
1466388de9dfSAlina Sbirlea void llvm::appendReversedLoopsToWorklist(
1467388de9dfSAlina Sbirlea     RangeT &&Loops, SmallPriorityWorklist<Loop *, 4> &Worklist) {
1468388de9dfSAlina Sbirlea   // We use an internal worklist to build up the preorder traversal without
1469388de9dfSAlina Sbirlea   // recursion.
1470388de9dfSAlina Sbirlea   SmallVector<Loop *, 4> PreOrderLoops, PreOrderWorklist;
1471388de9dfSAlina Sbirlea 
1472388de9dfSAlina Sbirlea   // We walk the initial sequence of loops in reverse because we generally want
1473388de9dfSAlina Sbirlea   // to visit defs before uses and the worklist is LIFO.
1474388de9dfSAlina Sbirlea   for (Loop *RootL : Loops) {
1475388de9dfSAlina Sbirlea     assert(PreOrderLoops.empty() && "Must start with an empty preorder walk.");
1476388de9dfSAlina Sbirlea     assert(PreOrderWorklist.empty() &&
1477388de9dfSAlina Sbirlea            "Must start with an empty preorder walk worklist.");
1478388de9dfSAlina Sbirlea     PreOrderWorklist.push_back(RootL);
1479388de9dfSAlina Sbirlea     do {
1480388de9dfSAlina Sbirlea       Loop *L = PreOrderWorklist.pop_back_val();
1481388de9dfSAlina Sbirlea       PreOrderWorklist.append(L->begin(), L->end());
1482388de9dfSAlina Sbirlea       PreOrderLoops.push_back(L);
1483388de9dfSAlina Sbirlea     } while (!PreOrderWorklist.empty());
1484388de9dfSAlina Sbirlea 
1485388de9dfSAlina Sbirlea     Worklist.insert(std::move(PreOrderLoops));
1486388de9dfSAlina Sbirlea     PreOrderLoops.clear();
1487388de9dfSAlina Sbirlea   }
1488388de9dfSAlina Sbirlea }
1489388de9dfSAlina Sbirlea 
1490388de9dfSAlina Sbirlea template <typename RangeT>
1491388de9dfSAlina Sbirlea void llvm::appendLoopsToWorklist(RangeT &&Loops,
1492388de9dfSAlina Sbirlea                                  SmallPriorityWorklist<Loop *, 4> &Worklist) {
1493388de9dfSAlina Sbirlea   appendReversedLoopsToWorklist(reverse(Loops), Worklist);
1494388de9dfSAlina Sbirlea }
1495388de9dfSAlina Sbirlea 
1496388de9dfSAlina Sbirlea template void llvm::appendLoopsToWorklist<ArrayRef<Loop *> &>(
1497388de9dfSAlina Sbirlea     ArrayRef<Loop *> &Loops, SmallPriorityWorklist<Loop *, 4> &Worklist);
1498388de9dfSAlina Sbirlea 
149967904db2SAlina Sbirlea template void
150067904db2SAlina Sbirlea llvm::appendLoopsToWorklist<Loop &>(Loop &L,
150167904db2SAlina Sbirlea                                     SmallPriorityWorklist<Loop *, 4> &Worklist);
150267904db2SAlina Sbirlea 
1503388de9dfSAlina Sbirlea void llvm::appendLoopsToWorklist(LoopInfo &LI,
1504388de9dfSAlina Sbirlea                                  SmallPriorityWorklist<Loop *, 4> &Worklist) {
1505388de9dfSAlina Sbirlea   appendReversedLoopsToWorklist(LI, Worklist);
1506388de9dfSAlina Sbirlea }
15073dcaf296SArkady Shlykov 
15083dcaf296SArkady Shlykov Loop *llvm::cloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
15093dcaf296SArkady Shlykov                       LoopInfo *LI, LPPassManager *LPM) {
15103dcaf296SArkady Shlykov   Loop &New = *LI->AllocateLoop();
15113dcaf296SArkady Shlykov   if (PL)
15123dcaf296SArkady Shlykov     PL->addChildLoop(&New);
15133dcaf296SArkady Shlykov   else
15143dcaf296SArkady Shlykov     LI->addTopLevelLoop(&New);
15153dcaf296SArkady Shlykov 
15163dcaf296SArkady Shlykov   if (LPM)
15173dcaf296SArkady Shlykov     LPM->addLoop(New);
15183dcaf296SArkady Shlykov 
15193dcaf296SArkady Shlykov   // Add all of the blocks in L to the new loop.
15203dcaf296SArkady Shlykov   for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
15213dcaf296SArkady Shlykov        I != E; ++I)
15223dcaf296SArkady Shlykov     if (LI->getLoopFor(*I) == L)
15233dcaf296SArkady Shlykov       New.addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI);
15243dcaf296SArkady Shlykov 
15253dcaf296SArkady Shlykov   // Add all of the subloops to the new loop.
15263dcaf296SArkady Shlykov   for (Loop *I : *L)
15273dcaf296SArkady Shlykov     cloneLoop(I, &New, VM, LI, LPM);
15283dcaf296SArkady Shlykov 
15293dcaf296SArkady Shlykov   return &New;
15303dcaf296SArkady Shlykov }
1531