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"
2297468e92SAlina Sbirlea #include "llvm/Analysis/MemorySSAUpdater.h"
2323aed5efSPhilip Reames #include "llvm/Analysis/MustExecute.h"
2445d4cb9aSWeiming Zhao #include "llvm/Analysis/ScalarEvolution.h"
252f2bd8caSAdam Nemet #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
26c434d091SElena Demikhovsky #include "llvm/Analysis/ScalarEvolutionExpander.h"
2745d4cb9aSWeiming Zhao #include "llvm/Analysis/ScalarEvolutionExpressions.h"
286bda14b3SChandler Carruth #include "llvm/Analysis/TargetTransformInfo.h"
29a097bc69SChad Rosier #include "llvm/Analysis/ValueTracking.h"
30744c3c32SDavide Italiano #include "llvm/IR/DIBuilder.h"
3131088a9dSChandler Carruth #include "llvm/IR/Dominators.h"
3276aa662cSKarthik Bhat #include "llvm/IR/Instructions.h"
33744c3c32SDavide Italiano #include "llvm/IR/IntrinsicInst.h"
3445d4cb9aSWeiming Zhao #include "llvm/IR/Module.h"
3576aa662cSKarthik Bhat #include "llvm/IR/PatternMatch.h"
3676aa662cSKarthik Bhat #include "llvm/IR/ValueHandle.h"
3731088a9dSChandler Carruth #include "llvm/Pass.h"
3876aa662cSKarthik Bhat #include "llvm/Support/Debug.h"
39a097bc69SChad Rosier #include "llvm/Support/KnownBits.h"
404a000883SChandler Carruth #include "llvm/Transforms/Utils/BasicBlockUtils.h"
4176aa662cSKarthik Bhat 
4276aa662cSKarthik Bhat using namespace llvm;
4376aa662cSKarthik Bhat using namespace llvm::PatternMatch;
4476aa662cSKarthik Bhat 
4576aa662cSKarthik Bhat #define DEBUG_TYPE "loop-utils"
4676aa662cSKarthik Bhat 
4772448525SMichael Kruse static const char *LLVMLoopDisableNonforced = "llvm.loop.disable_nonforced";
4872448525SMichael Kruse 
494a000883SChandler Carruth bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
5097468e92SAlina Sbirlea                                    MemorySSAUpdater *MSSAU,
514a000883SChandler Carruth                                    bool PreserveLCSSA) {
524a000883SChandler Carruth   bool Changed = false;
534a000883SChandler Carruth 
544a000883SChandler Carruth   // We re-use a vector for the in-loop predecesosrs.
554a000883SChandler Carruth   SmallVector<BasicBlock *, 4> InLoopPredecessors;
564a000883SChandler Carruth 
574a000883SChandler Carruth   auto RewriteExit = [&](BasicBlock *BB) {
584a000883SChandler Carruth     assert(InLoopPredecessors.empty() &&
594a000883SChandler Carruth            "Must start with an empty predecessors list!");
604a000883SChandler Carruth     auto Cleanup = make_scope_exit([&] { InLoopPredecessors.clear(); });
614a000883SChandler Carruth 
624a000883SChandler Carruth     // See if there are any non-loop predecessors of this exit block and
634a000883SChandler Carruth     // keep track of the in-loop predecessors.
644a000883SChandler Carruth     bool IsDedicatedExit = true;
654a000883SChandler Carruth     for (auto *PredBB : predecessors(BB))
664a000883SChandler Carruth       if (L->contains(PredBB)) {
674a000883SChandler Carruth         if (isa<IndirectBrInst>(PredBB->getTerminator()))
684a000883SChandler Carruth           // We cannot rewrite exiting edges from an indirectbr.
694a000883SChandler Carruth           return false;
70784929d0SCraig Topper         if (isa<CallBrInst>(PredBB->getTerminator()))
71784929d0SCraig Topper           // We cannot rewrite exiting edges from a callbr.
72784929d0SCraig Topper           return false;
734a000883SChandler Carruth 
744a000883SChandler Carruth         InLoopPredecessors.push_back(PredBB);
754a000883SChandler Carruth       } else {
764a000883SChandler Carruth         IsDedicatedExit = false;
774a000883SChandler Carruth       }
784a000883SChandler Carruth 
794a000883SChandler Carruth     assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
804a000883SChandler Carruth 
814a000883SChandler Carruth     // Nothing to do if this is already a dedicated exit.
824a000883SChandler Carruth     if (IsDedicatedExit)
834a000883SChandler Carruth       return false;
844a000883SChandler Carruth 
854a000883SChandler Carruth     auto *NewExitBB = SplitBlockPredecessors(
8697468e92SAlina Sbirlea         BB, InLoopPredecessors, ".loopexit", DT, LI, MSSAU, PreserveLCSSA);
874a000883SChandler Carruth 
884a000883SChandler Carruth     if (!NewExitBB)
89d34e60caSNicola Zaghen       LLVM_DEBUG(
90d34e60caSNicola Zaghen           dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
914a000883SChandler Carruth                  << *L << "\n");
924a000883SChandler Carruth     else
93d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
944a000883SChandler Carruth                         << NewExitBB->getName() << "\n");
954a000883SChandler Carruth     return true;
964a000883SChandler Carruth   };
974a000883SChandler Carruth 
984a000883SChandler Carruth   // Walk the exit blocks directly rather than building up a data structure for
994a000883SChandler Carruth   // them, but only visit each one once.
1004a000883SChandler Carruth   SmallPtrSet<BasicBlock *, 4> Visited;
1014a000883SChandler Carruth   for (auto *BB : L->blocks())
1024a000883SChandler Carruth     for (auto *SuccBB : successors(BB)) {
1034a000883SChandler Carruth       // We're looking for exit blocks so skip in-loop successors.
1044a000883SChandler Carruth       if (L->contains(SuccBB))
1054a000883SChandler Carruth         continue;
1064a000883SChandler Carruth 
1074a000883SChandler Carruth       // Visit each exit block exactly once.
1084a000883SChandler Carruth       if (!Visited.insert(SuccBB).second)
1094a000883SChandler Carruth         continue;
1104a000883SChandler Carruth 
1114a000883SChandler Carruth       Changed |= RewriteExit(SuccBB);
1124a000883SChandler Carruth     }
1134a000883SChandler Carruth 
1144a000883SChandler Carruth   return Changed;
1154a000883SChandler Carruth }
1164a000883SChandler Carruth 
1175f8f34e4SAdrian Prantl /// Returns the instructions that use values defined in the loop.
118c5b7b555SAshutosh Nema SmallVector<Instruction *, 8> llvm::findDefsUsedOutsideOfLoop(Loop *L) {
119c5b7b555SAshutosh Nema   SmallVector<Instruction *, 8> UsedOutside;
120c5b7b555SAshutosh Nema 
121c5b7b555SAshutosh Nema   for (auto *Block : L->getBlocks())
122c5b7b555SAshutosh Nema     // FIXME: I believe that this could use copy_if if the Inst reference could
123c5b7b555SAshutosh Nema     // be adapted into a pointer.
124c5b7b555SAshutosh Nema     for (auto &Inst : *Block) {
125c5b7b555SAshutosh Nema       auto Users = Inst.users();
1260a16c228SDavid Majnemer       if (any_of(Users, [&](User *U) {
127c5b7b555SAshutosh Nema             auto *Use = cast<Instruction>(U);
128c5b7b555SAshutosh Nema             return !L->contains(Use->getParent());
129c5b7b555SAshutosh Nema           }))
130c5b7b555SAshutosh Nema         UsedOutside.push_back(&Inst);
131c5b7b555SAshutosh Nema     }
132c5b7b555SAshutosh Nema 
133c5b7b555SAshutosh Nema   return UsedOutside;
134c5b7b555SAshutosh Nema }
13531088a9dSChandler Carruth 
13631088a9dSChandler Carruth void llvm::getLoopAnalysisUsage(AnalysisUsage &AU) {
13731088a9dSChandler Carruth   // By definition, all loop passes need the LoopInfo analysis and the
13831088a9dSChandler Carruth   // Dominator tree it depends on. Because they all participate in the loop
13931088a9dSChandler Carruth   // pass manager, they must also preserve these.
14031088a9dSChandler Carruth   AU.addRequired<DominatorTreeWrapperPass>();
14131088a9dSChandler Carruth   AU.addPreserved<DominatorTreeWrapperPass>();
14231088a9dSChandler Carruth   AU.addRequired<LoopInfoWrapperPass>();
14331088a9dSChandler Carruth   AU.addPreserved<LoopInfoWrapperPass>();
14431088a9dSChandler Carruth 
14531088a9dSChandler Carruth   // We must also preserve LoopSimplify and LCSSA. We locally access their IDs
14631088a9dSChandler Carruth   // here because users shouldn't directly get them from this header.
14731088a9dSChandler Carruth   extern char &LoopSimplifyID;
14831088a9dSChandler Carruth   extern char &LCSSAID;
14931088a9dSChandler Carruth   AU.addRequiredID(LoopSimplifyID);
15031088a9dSChandler Carruth   AU.addPreservedID(LoopSimplifyID);
15131088a9dSChandler Carruth   AU.addRequiredID(LCSSAID);
15231088a9dSChandler Carruth   AU.addPreservedID(LCSSAID);
153c3ccf5d7SIgor Laevsky   // This is used in the LPPassManager to perform LCSSA verification on passes
154c3ccf5d7SIgor Laevsky   // which preserve lcssa form
155c3ccf5d7SIgor Laevsky   AU.addRequired<LCSSAVerificationPass>();
156c3ccf5d7SIgor Laevsky   AU.addPreserved<LCSSAVerificationPass>();
15731088a9dSChandler Carruth 
15831088a9dSChandler Carruth   // Loop passes are designed to run inside of a loop pass manager which means
15931088a9dSChandler Carruth   // that any function analyses they require must be required by the first loop
16031088a9dSChandler Carruth   // pass in the manager (so that it is computed before the loop pass manager
16131088a9dSChandler Carruth   // runs) and preserved by all loop pasess in the manager. To make this
16231088a9dSChandler Carruth   // reasonably robust, the set needed for most loop passes is maintained here.
16331088a9dSChandler Carruth   // If your loop pass requires an analysis not listed here, you will need to
16431088a9dSChandler Carruth   // carefully audit the loop pass manager nesting structure that results.
16531088a9dSChandler Carruth   AU.addRequired<AAResultsWrapperPass>();
16631088a9dSChandler Carruth   AU.addPreserved<AAResultsWrapperPass>();
16731088a9dSChandler Carruth   AU.addPreserved<BasicAAWrapperPass>();
16831088a9dSChandler Carruth   AU.addPreserved<GlobalsAAWrapperPass>();
16931088a9dSChandler Carruth   AU.addPreserved<SCEVAAWrapperPass>();
17031088a9dSChandler Carruth   AU.addRequired<ScalarEvolutionWrapperPass>();
17131088a9dSChandler Carruth   AU.addPreserved<ScalarEvolutionWrapperPass>();
17231088a9dSChandler Carruth }
17331088a9dSChandler Carruth 
17431088a9dSChandler Carruth /// Manually defined generic "LoopPass" dependency initialization. This is used
17531088a9dSChandler Carruth /// to initialize the exact set of passes from above in \c
17631088a9dSChandler Carruth /// getLoopAnalysisUsage. It can be used within a loop pass's initialization
17731088a9dSChandler Carruth /// with:
17831088a9dSChandler Carruth ///
17931088a9dSChandler Carruth ///   INITIALIZE_PASS_DEPENDENCY(LoopPass)
18031088a9dSChandler Carruth ///
18131088a9dSChandler Carruth /// As-if "LoopPass" were a pass.
18231088a9dSChandler Carruth void llvm::initializeLoopPassPass(PassRegistry &Registry) {
18331088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
18431088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
18531088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
186e12c487bSEaswaran Raman   INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
18731088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
18831088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
18931088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
19031088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
19131088a9dSChandler Carruth   INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
19231088a9dSChandler Carruth }
193963341c8SAdam Nemet 
19472448525SMichael Kruse /// Find string metadata for loop
19572448525SMichael Kruse ///
19672448525SMichael Kruse /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an
19772448525SMichael Kruse /// operand or null otherwise.  If the string metadata is not found return
19872448525SMichael Kruse /// Optional's not-a-value.
199978ba615SMichael Kruse Optional<const MDOperand *> llvm::findStringMetadataForLoop(const Loop *TheLoop,
20072448525SMichael Kruse                                                             StringRef Name) {
201978ba615SMichael Kruse   MDNode *MD = findOptionMDForLoop(TheLoop, Name);
20272448525SMichael Kruse   if (!MD)
20372448525SMichael Kruse     return None;
204fe3def7cSAdam Nemet   switch (MD->getNumOperands()) {
205fe3def7cSAdam Nemet   case 1:
206fe3def7cSAdam Nemet     return nullptr;
207fe3def7cSAdam Nemet   case 2:
208fe3def7cSAdam Nemet     return &MD->getOperand(1);
209fe3def7cSAdam Nemet   default:
210fe3def7cSAdam Nemet     llvm_unreachable("loop metadata has 0 or 1 operand");
211963341c8SAdam Nemet   }
212fe3def7cSAdam Nemet }
21372448525SMichael Kruse 
21472448525SMichael Kruse static Optional<bool> getOptionalBoolLoopAttribute(const Loop *TheLoop,
21572448525SMichael Kruse                                                    StringRef Name) {
216978ba615SMichael Kruse   MDNode *MD = findOptionMDForLoop(TheLoop, Name);
217978ba615SMichael Kruse   if (!MD)
218fe3def7cSAdam Nemet     return None;
219978ba615SMichael Kruse   switch (MD->getNumOperands()) {
22072448525SMichael Kruse   case 1:
22172448525SMichael Kruse     // When the value is absent it is interpreted as 'attribute set'.
22272448525SMichael Kruse     return true;
22372448525SMichael Kruse   case 2:
224f9027e55SAlina Sbirlea     if (ConstantInt *IntMD =
225f9027e55SAlina Sbirlea             mdconst::extract_or_null<ConstantInt>(MD->getOperand(1).get()))
226f9027e55SAlina Sbirlea       return IntMD->getZExtValue();
227f9027e55SAlina Sbirlea     return true;
22872448525SMichael Kruse   }
22972448525SMichael Kruse   llvm_unreachable("unexpected number of options");
23072448525SMichael Kruse }
23172448525SMichael Kruse 
23272448525SMichael Kruse static bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name) {
23372448525SMichael Kruse   return getOptionalBoolLoopAttribute(TheLoop, Name).getValueOr(false);
23472448525SMichael Kruse }
23572448525SMichael Kruse 
23672448525SMichael Kruse llvm::Optional<int> llvm::getOptionalIntLoopAttribute(Loop *TheLoop,
23772448525SMichael Kruse                                                       StringRef Name) {
23872448525SMichael Kruse   const MDOperand *AttrMD =
23972448525SMichael Kruse       findStringMetadataForLoop(TheLoop, Name).getValueOr(nullptr);
24072448525SMichael Kruse   if (!AttrMD)
24172448525SMichael Kruse     return None;
24272448525SMichael Kruse 
24372448525SMichael Kruse   ConstantInt *IntMD = mdconst::extract_or_null<ConstantInt>(AttrMD->get());
24472448525SMichael Kruse   if (!IntMD)
24572448525SMichael Kruse     return None;
24672448525SMichael Kruse 
24772448525SMichael Kruse   return IntMD->getSExtValue();
24872448525SMichael Kruse }
24972448525SMichael Kruse 
25072448525SMichael Kruse Optional<MDNode *> llvm::makeFollowupLoopID(
25172448525SMichael Kruse     MDNode *OrigLoopID, ArrayRef<StringRef> FollowupOptions,
25272448525SMichael Kruse     const char *InheritOptionsExceptPrefix, bool AlwaysNew) {
25372448525SMichael Kruse   if (!OrigLoopID) {
25472448525SMichael Kruse     if (AlwaysNew)
25572448525SMichael Kruse       return nullptr;
25672448525SMichael Kruse     return None;
25772448525SMichael Kruse   }
25872448525SMichael Kruse 
25972448525SMichael Kruse   assert(OrigLoopID->getOperand(0) == OrigLoopID);
26072448525SMichael Kruse 
26172448525SMichael Kruse   bool InheritAllAttrs = !InheritOptionsExceptPrefix;
26272448525SMichael Kruse   bool InheritSomeAttrs =
26372448525SMichael Kruse       InheritOptionsExceptPrefix && InheritOptionsExceptPrefix[0] != '\0';
26472448525SMichael Kruse   SmallVector<Metadata *, 8> MDs;
26572448525SMichael Kruse   MDs.push_back(nullptr);
26672448525SMichael Kruse 
26772448525SMichael Kruse   bool Changed = false;
26872448525SMichael Kruse   if (InheritAllAttrs || InheritSomeAttrs) {
26972448525SMichael Kruse     for (const MDOperand &Existing : drop_begin(OrigLoopID->operands(), 1)) {
27072448525SMichael Kruse       MDNode *Op = cast<MDNode>(Existing.get());
27172448525SMichael Kruse 
27272448525SMichael Kruse       auto InheritThisAttribute = [InheritSomeAttrs,
27372448525SMichael Kruse                                    InheritOptionsExceptPrefix](MDNode *Op) {
27472448525SMichael Kruse         if (!InheritSomeAttrs)
27572448525SMichael Kruse           return false;
27672448525SMichael Kruse 
27772448525SMichael Kruse         // Skip malformatted attribute metadata nodes.
27872448525SMichael Kruse         if (Op->getNumOperands() == 0)
27972448525SMichael Kruse           return true;
28072448525SMichael Kruse         Metadata *NameMD = Op->getOperand(0).get();
28172448525SMichael Kruse         if (!isa<MDString>(NameMD))
28272448525SMichael Kruse           return true;
28372448525SMichael Kruse         StringRef AttrName = cast<MDString>(NameMD)->getString();
28472448525SMichael Kruse 
28572448525SMichael Kruse         // Do not inherit excluded attributes.
28672448525SMichael Kruse         return !AttrName.startswith(InheritOptionsExceptPrefix);
28772448525SMichael Kruse       };
28872448525SMichael Kruse 
28972448525SMichael Kruse       if (InheritThisAttribute(Op))
29072448525SMichael Kruse         MDs.push_back(Op);
29172448525SMichael Kruse       else
29272448525SMichael Kruse         Changed = true;
29372448525SMichael Kruse     }
29472448525SMichael Kruse   } else {
29572448525SMichael Kruse     // Modified if we dropped at least one attribute.
29672448525SMichael Kruse     Changed = OrigLoopID->getNumOperands() > 1;
29772448525SMichael Kruse   }
29872448525SMichael Kruse 
29972448525SMichael Kruse   bool HasAnyFollowup = false;
30072448525SMichael Kruse   for (StringRef OptionName : FollowupOptions) {
301978ba615SMichael Kruse     MDNode *FollowupNode = findOptionMDForLoopID(OrigLoopID, OptionName);
30272448525SMichael Kruse     if (!FollowupNode)
30372448525SMichael Kruse       continue;
30472448525SMichael Kruse 
30572448525SMichael Kruse     HasAnyFollowup = true;
30672448525SMichael Kruse     for (const MDOperand &Option : drop_begin(FollowupNode->operands(), 1)) {
30772448525SMichael Kruse       MDs.push_back(Option.get());
30872448525SMichael Kruse       Changed = true;
30972448525SMichael Kruse     }
31072448525SMichael Kruse   }
31172448525SMichael Kruse 
31272448525SMichael Kruse   // Attributes of the followup loop not specified explicity, so signal to the
31372448525SMichael Kruse   // transformation pass to add suitable attributes.
31472448525SMichael Kruse   if (!AlwaysNew && !HasAnyFollowup)
31572448525SMichael Kruse     return None;
31672448525SMichael Kruse 
31772448525SMichael Kruse   // If no attributes were added or remove, the previous loop Id can be reused.
31872448525SMichael Kruse   if (!AlwaysNew && !Changed)
31972448525SMichael Kruse     return OrigLoopID;
32072448525SMichael Kruse 
32172448525SMichael Kruse   // No attributes is equivalent to having no !llvm.loop metadata at all.
32272448525SMichael Kruse   if (MDs.size() == 1)
32372448525SMichael Kruse     return nullptr;
32472448525SMichael Kruse 
32572448525SMichael Kruse   // Build the new loop ID.
32672448525SMichael Kruse   MDTuple *FollowupLoopID = MDNode::get(OrigLoopID->getContext(), MDs);
32772448525SMichael Kruse   FollowupLoopID->replaceOperandWith(0, FollowupLoopID);
32872448525SMichael Kruse   return FollowupLoopID;
32972448525SMichael Kruse }
33072448525SMichael Kruse 
33172448525SMichael Kruse bool llvm::hasDisableAllTransformsHint(const Loop *L) {
33272448525SMichael Kruse   return getBooleanLoopAttribute(L, LLVMLoopDisableNonforced);
33372448525SMichael Kruse }
33472448525SMichael Kruse 
33572448525SMichael Kruse TransformationMode llvm::hasUnrollTransformation(Loop *L) {
33672448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable"))
33772448525SMichael Kruse     return TM_SuppressedByUser;
33872448525SMichael Kruse 
33972448525SMichael Kruse   Optional<int> Count =
34072448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
34172448525SMichael Kruse   if (Count.hasValue())
34272448525SMichael Kruse     return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
34372448525SMichael Kruse 
34472448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable"))
34572448525SMichael Kruse     return TM_ForcedByUser;
34672448525SMichael Kruse 
34772448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll.full"))
34872448525SMichael Kruse     return TM_ForcedByUser;
34972448525SMichael Kruse 
35072448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
35172448525SMichael Kruse     return TM_Disable;
35272448525SMichael Kruse 
35372448525SMichael Kruse   return TM_Unspecified;
35472448525SMichael Kruse }
35572448525SMichael Kruse 
35672448525SMichael Kruse TransformationMode llvm::hasUnrollAndJamTransformation(Loop *L) {
35772448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable"))
35872448525SMichael Kruse     return TM_SuppressedByUser;
35972448525SMichael Kruse 
36072448525SMichael Kruse   Optional<int> Count =
36172448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count");
36272448525SMichael Kruse   if (Count.hasValue())
36372448525SMichael Kruse     return Count.getValue() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
36472448525SMichael Kruse 
36572448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.enable"))
36672448525SMichael Kruse     return TM_ForcedByUser;
36772448525SMichael Kruse 
36872448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
36972448525SMichael Kruse     return TM_Disable;
37072448525SMichael Kruse 
37172448525SMichael Kruse   return TM_Unspecified;
37272448525SMichael Kruse }
37372448525SMichael Kruse 
37472448525SMichael Kruse TransformationMode llvm::hasVectorizeTransformation(Loop *L) {
37572448525SMichael Kruse   Optional<bool> Enable =
37672448525SMichael Kruse       getOptionalBoolLoopAttribute(L, "llvm.loop.vectorize.enable");
37772448525SMichael Kruse 
37872448525SMichael Kruse   if (Enable == false)
37972448525SMichael Kruse     return TM_SuppressedByUser;
38072448525SMichael Kruse 
38172448525SMichael Kruse   Optional<int> VectorizeWidth =
38272448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.vectorize.width");
38372448525SMichael Kruse   Optional<int> InterleaveCount =
38472448525SMichael Kruse       getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count");
38572448525SMichael Kruse 
38672448525SMichael Kruse   // 'Forcing' vector width and interleave count to one effectively disables
38772448525SMichael Kruse   // this tranformation.
38870560a0aSMichael Kruse   if (Enable == true && VectorizeWidth == 1 && InterleaveCount == 1)
38972448525SMichael Kruse     return TM_SuppressedByUser;
39072448525SMichael Kruse 
39172448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.isvectorized"))
39272448525SMichael Kruse     return TM_Disable;
39372448525SMichael Kruse 
39470560a0aSMichael Kruse   if (Enable == true)
39570560a0aSMichael Kruse     return TM_ForcedByUser;
39670560a0aSMichael Kruse 
39772448525SMichael Kruse   if (VectorizeWidth == 1 && InterleaveCount == 1)
39872448525SMichael Kruse     return TM_Disable;
39972448525SMichael Kruse 
40072448525SMichael Kruse   if (VectorizeWidth > 1 || InterleaveCount > 1)
40172448525SMichael Kruse     return TM_Enable;
40272448525SMichael Kruse 
40372448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
40472448525SMichael Kruse     return TM_Disable;
40572448525SMichael Kruse 
40672448525SMichael Kruse   return TM_Unspecified;
40772448525SMichael Kruse }
40872448525SMichael Kruse 
40972448525SMichael Kruse TransformationMode llvm::hasDistributeTransformation(Loop *L) {
41072448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.distribute.enable"))
41172448525SMichael Kruse     return TM_ForcedByUser;
41272448525SMichael Kruse 
41372448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
41472448525SMichael Kruse     return TM_Disable;
41572448525SMichael Kruse 
41672448525SMichael Kruse   return TM_Unspecified;
41772448525SMichael Kruse }
41872448525SMichael Kruse 
41972448525SMichael Kruse TransformationMode llvm::hasLICMVersioningTransformation(Loop *L) {
42072448525SMichael Kruse   if (getBooleanLoopAttribute(L, "llvm.loop.licm_versioning.disable"))
42172448525SMichael Kruse     return TM_SuppressedByUser;
42272448525SMichael Kruse 
42372448525SMichael Kruse   if (hasDisableAllTransformsHint(L))
42472448525SMichael Kruse     return TM_Disable;
42572448525SMichael Kruse 
42672448525SMichael Kruse   return TM_Unspecified;
427963341c8SAdam Nemet }
428122f984aSEvgeniy Stepanov 
4297ed5856aSAlina Sbirlea /// Does a BFS from a given node to all of its children inside a given loop.
4307ed5856aSAlina Sbirlea /// The returned vector of nodes includes the starting point.
4317ed5856aSAlina Sbirlea SmallVector<DomTreeNode *, 16>
4327ed5856aSAlina Sbirlea llvm::collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop) {
4337ed5856aSAlina Sbirlea   SmallVector<DomTreeNode *, 16> Worklist;
4347ed5856aSAlina Sbirlea   auto AddRegionToWorklist = [&](DomTreeNode *DTN) {
4357ed5856aSAlina Sbirlea     // Only include subregions in the top level loop.
4367ed5856aSAlina Sbirlea     BasicBlock *BB = DTN->getBlock();
4377ed5856aSAlina Sbirlea     if (CurLoop->contains(BB))
4387ed5856aSAlina Sbirlea       Worklist.push_back(DTN);
4397ed5856aSAlina Sbirlea   };
4407ed5856aSAlina Sbirlea 
4417ed5856aSAlina Sbirlea   AddRegionToWorklist(N);
4427ed5856aSAlina Sbirlea 
4437ed5856aSAlina Sbirlea   for (size_t I = 0; I < Worklist.size(); I++)
4447ed5856aSAlina Sbirlea     for (DomTreeNode *Child : Worklist[I]->getChildren())
4457ed5856aSAlina Sbirlea       AddRegionToWorklist(Child);
4467ed5856aSAlina Sbirlea 
4477ed5856aSAlina Sbirlea   return Worklist;
4487ed5856aSAlina Sbirlea }
4497ed5856aSAlina Sbirlea 
450df3e71e0SMarcello Maggioni void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT = nullptr,
451df3e71e0SMarcello Maggioni                           ScalarEvolution *SE = nullptr,
452df3e71e0SMarcello Maggioni                           LoopInfo *LI = nullptr) {
453899809d5SHans Wennborg   assert((!DT || L->isLCSSAForm(*DT)) && "Expected LCSSA!");
454df3e71e0SMarcello Maggioni   auto *Preheader = L->getLoopPreheader();
455df3e71e0SMarcello Maggioni   assert(Preheader && "Preheader should exist!");
456df3e71e0SMarcello Maggioni 
457df3e71e0SMarcello Maggioni   // Now that we know the removal is safe, remove the loop by changing the
458df3e71e0SMarcello Maggioni   // branch from the preheader to go to the single exit block.
459df3e71e0SMarcello Maggioni   //
460df3e71e0SMarcello Maggioni   // Because we're deleting a large chunk of code at once, the sequence in which
461df3e71e0SMarcello Maggioni   // we remove things is very important to avoid invalidation issues.
462df3e71e0SMarcello Maggioni 
463df3e71e0SMarcello Maggioni   // Tell ScalarEvolution that the loop is deleted. Do this before
464df3e71e0SMarcello Maggioni   // deleting the loop so that ScalarEvolution can look at the loop
465df3e71e0SMarcello Maggioni   // to determine what it needs to clean up.
466df3e71e0SMarcello Maggioni   if (SE)
467df3e71e0SMarcello Maggioni     SE->forgetLoop(L);
468df3e71e0SMarcello Maggioni 
469df3e71e0SMarcello Maggioni   auto *ExitBlock = L->getUniqueExitBlock();
470df3e71e0SMarcello Maggioni   assert(ExitBlock && "Should have a unique exit block!");
471df3e71e0SMarcello Maggioni   assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
472df3e71e0SMarcello Maggioni 
473df3e71e0SMarcello Maggioni   auto *OldBr = dyn_cast<BranchInst>(Preheader->getTerminator());
474df3e71e0SMarcello Maggioni   assert(OldBr && "Preheader must end with a branch");
475df3e71e0SMarcello Maggioni   assert(OldBr->isUnconditional() && "Preheader must have a single successor");
476df3e71e0SMarcello Maggioni   // Connect the preheader to the exit block. Keep the old edge to the header
477df3e71e0SMarcello Maggioni   // around to perform the dominator tree update in two separate steps
478df3e71e0SMarcello Maggioni   // -- #1 insertion of the edge preheader -> exit and #2 deletion of the edge
479df3e71e0SMarcello Maggioni   // preheader -> header.
480df3e71e0SMarcello Maggioni   //
481df3e71e0SMarcello Maggioni   //
482df3e71e0SMarcello Maggioni   // 0.  Preheader          1.  Preheader           2.  Preheader
483df3e71e0SMarcello Maggioni   //        |                    |   |                   |
484df3e71e0SMarcello Maggioni   //        V                    |   V                   |
485df3e71e0SMarcello Maggioni   //      Header <--\            | Header <--\           | Header <--\
486df3e71e0SMarcello Maggioni   //       |  |     |            |  |  |     |           |  |  |     |
487df3e71e0SMarcello Maggioni   //       |  V     |            |  |  V     |           |  |  V     |
488df3e71e0SMarcello Maggioni   //       | Body --/            |  | Body --/           |  | Body --/
489df3e71e0SMarcello Maggioni   //       V                     V  V                    V  V
490df3e71e0SMarcello Maggioni   //      Exit                   Exit                    Exit
491df3e71e0SMarcello Maggioni   //
492df3e71e0SMarcello Maggioni   // By doing this is two separate steps we can perform the dominator tree
493df3e71e0SMarcello Maggioni   // update without using the batch update API.
494df3e71e0SMarcello Maggioni   //
495df3e71e0SMarcello Maggioni   // Even when the loop is never executed, we cannot remove the edge from the
496df3e71e0SMarcello Maggioni   // source block to the exit block. Consider the case where the unexecuted loop
497df3e71e0SMarcello Maggioni   // branches back to an outer loop. If we deleted the loop and removed the edge
498df3e71e0SMarcello Maggioni   // coming to this inner loop, this will break the outer loop structure (by
499df3e71e0SMarcello Maggioni   // deleting the backedge of the outer loop). If the outer loop is indeed a
500df3e71e0SMarcello Maggioni   // non-loop, it will be deleted in a future iteration of loop deletion pass.
501df3e71e0SMarcello Maggioni   IRBuilder<> Builder(OldBr);
502df3e71e0SMarcello Maggioni   Builder.CreateCondBr(Builder.getFalse(), L->getHeader(), ExitBlock);
503df3e71e0SMarcello Maggioni   // Remove the old branch. The conditional branch becomes a new terminator.
504df3e71e0SMarcello Maggioni   OldBr->eraseFromParent();
505df3e71e0SMarcello Maggioni 
506df3e71e0SMarcello Maggioni   // Rewrite phis in the exit block to get their inputs from the Preheader
507df3e71e0SMarcello Maggioni   // instead of the exiting block.
508c7fc81e6SBenjamin Kramer   for (PHINode &P : ExitBlock->phis()) {
509df3e71e0SMarcello Maggioni     // Set the zero'th element of Phi to be from the preheader and remove all
510df3e71e0SMarcello Maggioni     // other incoming values. Given the loop has dedicated exits, all other
511df3e71e0SMarcello Maggioni     // incoming values must be from the exiting blocks.
512df3e71e0SMarcello Maggioni     int PredIndex = 0;
513c7fc81e6SBenjamin Kramer     P.setIncomingBlock(PredIndex, Preheader);
514df3e71e0SMarcello Maggioni     // Removes all incoming values from all other exiting blocks (including
515df3e71e0SMarcello Maggioni     // duplicate values from an exiting block).
516df3e71e0SMarcello Maggioni     // Nuke all entries except the zero'th entry which is the preheader entry.
517df3e71e0SMarcello Maggioni     // NOTE! We need to remove Incoming Values in the reverse order as done
518df3e71e0SMarcello Maggioni     // below, to keep the indices valid for deletion (removeIncomingValues
519df3e71e0SMarcello Maggioni     // updates getNumIncomingValues and shifts all values down into the operand
520df3e71e0SMarcello Maggioni     // being deleted).
521c7fc81e6SBenjamin Kramer     for (unsigned i = 0, e = P.getNumIncomingValues() - 1; i != e; ++i)
522c7fc81e6SBenjamin Kramer       P.removeIncomingValue(e - i, false);
523df3e71e0SMarcello Maggioni 
524c7fc81e6SBenjamin Kramer     assert((P.getNumIncomingValues() == 1 &&
525c7fc81e6SBenjamin Kramer             P.getIncomingBlock(PredIndex) == Preheader) &&
526df3e71e0SMarcello Maggioni            "Should have exactly one value and that's from the preheader!");
527df3e71e0SMarcello Maggioni   }
528df3e71e0SMarcello Maggioni 
529df3e71e0SMarcello Maggioni   // Disconnect the loop body by branching directly to its exit.
530df3e71e0SMarcello Maggioni   Builder.SetInsertPoint(Preheader->getTerminator());
531df3e71e0SMarcello Maggioni   Builder.CreateBr(ExitBlock);
532df3e71e0SMarcello Maggioni   // Remove the old branch.
533df3e71e0SMarcello Maggioni   Preheader->getTerminator()->eraseFromParent();
534df3e71e0SMarcello Maggioni 
53521a8b605SChijun Sima   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
536df3e71e0SMarcello Maggioni   if (DT) {
537df3e71e0SMarcello Maggioni     // Update the dominator tree by informing it about the new edge from the
538f131d611SChijun Sima     // preheader to the exit and the removed edge.
539f131d611SChijun Sima     DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock},
540f131d611SChijun Sima                       {DominatorTree::Delete, Preheader, L->getHeader()}});
541df3e71e0SMarcello Maggioni   }
542df3e71e0SMarcello Maggioni 
543744c3c32SDavide Italiano   // Use a map to unique and a vector to guarantee deterministic ordering.
5448ee59ca6SDavide Italiano   llvm::SmallDenseSet<std::pair<DIVariable *, DIExpression *>, 4> DeadDebugSet;
545744c3c32SDavide Italiano   llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst;
546744c3c32SDavide Italiano 
547a757d65cSSerguei Katkov   // Given LCSSA form is satisfied, we should not have users of instructions
548a757d65cSSerguei Katkov   // within the dead loop outside of the loop. However, LCSSA doesn't take
549a757d65cSSerguei Katkov   // unreachable uses into account. We handle them here.
550a757d65cSSerguei Katkov   // We could do it after drop all references (in this case all users in the
551a757d65cSSerguei Katkov   // loop will be already eliminated and we have less work to do but according
552a757d65cSSerguei Katkov   // to API doc of User::dropAllReferences only valid operation after dropping
553a757d65cSSerguei Katkov   // references, is deletion. So let's substitute all usages of
554a757d65cSSerguei Katkov   // instruction from the loop with undef value of corresponding type first.
555a757d65cSSerguei Katkov   for (auto *Block : L->blocks())
556a757d65cSSerguei Katkov     for (Instruction &I : *Block) {
557a757d65cSSerguei Katkov       auto *Undef = UndefValue::get(I.getType());
558a757d65cSSerguei Katkov       for (Value::use_iterator UI = I.use_begin(), E = I.use_end(); UI != E;) {
559a757d65cSSerguei Katkov         Use &U = *UI;
560a757d65cSSerguei Katkov         ++UI;
561a757d65cSSerguei Katkov         if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
562a757d65cSSerguei Katkov           if (L->contains(Usr->getParent()))
563a757d65cSSerguei Katkov             continue;
564a757d65cSSerguei Katkov         // If we have a DT then we can check that uses outside a loop only in
565a757d65cSSerguei Katkov         // unreachable block.
566a757d65cSSerguei Katkov         if (DT)
567a757d65cSSerguei Katkov           assert(!DT->isReachableFromEntry(U) &&
568a757d65cSSerguei Katkov                  "Unexpected user in reachable block");
569a757d65cSSerguei Katkov         U.set(Undef);
570a757d65cSSerguei Katkov       }
571744c3c32SDavide Italiano       auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
572744c3c32SDavide Italiano       if (!DVI)
573744c3c32SDavide Italiano         continue;
5748ee59ca6SDavide Italiano       auto Key = DeadDebugSet.find({DVI->getVariable(), DVI->getExpression()});
5758ee59ca6SDavide Italiano       if (Key != DeadDebugSet.end())
576744c3c32SDavide Italiano         continue;
5778ee59ca6SDavide Italiano       DeadDebugSet.insert({DVI->getVariable(), DVI->getExpression()});
578744c3c32SDavide Italiano       DeadDebugInst.push_back(DVI);
579a757d65cSSerguei Katkov     }
580a757d65cSSerguei Katkov 
581744c3c32SDavide Italiano   // After the loop has been deleted all the values defined and modified
582744c3c32SDavide Italiano   // inside the loop are going to be unavailable.
583744c3c32SDavide Italiano   // Since debug values in the loop have been deleted, inserting an undef
584744c3c32SDavide Italiano   // dbg.value truncates the range of any dbg.value before the loop where the
585744c3c32SDavide Italiano   // loop used to be. This is particularly important for constant values.
586744c3c32SDavide Italiano   DIBuilder DIB(*ExitBlock->getModule());
587e5be660eSRoman Lebedev   Instruction *InsertDbgValueBefore = ExitBlock->getFirstNonPHI();
588e5be660eSRoman Lebedev   assert(InsertDbgValueBefore &&
589e5be660eSRoman Lebedev          "There should be a non-PHI instruction in exit block, else these "
590e5be660eSRoman Lebedev          "instructions will have no parent.");
591744c3c32SDavide Italiano   for (auto *DVI : DeadDebugInst)
592e5be660eSRoman Lebedev     DIB.insertDbgValueIntrinsic(UndefValue::get(Builder.getInt32Ty()),
593e5be660eSRoman Lebedev                                 DVI->getVariable(), DVI->getExpression(),
594e5be660eSRoman Lebedev                                 DVI->getDebugLoc(), InsertDbgValueBefore);
595744c3c32SDavide Italiano 
596df3e71e0SMarcello Maggioni   // Remove the block from the reference counting scheme, so that we can
597df3e71e0SMarcello Maggioni   // delete it freely later.
598df3e71e0SMarcello Maggioni   for (auto *Block : L->blocks())
599df3e71e0SMarcello Maggioni     Block->dropAllReferences();
600df3e71e0SMarcello Maggioni 
601df3e71e0SMarcello Maggioni   if (LI) {
602df3e71e0SMarcello Maggioni     // Erase the instructions and the blocks without having to worry
603df3e71e0SMarcello Maggioni     // about ordering because we already dropped the references.
604df3e71e0SMarcello Maggioni     // NOTE: This iteration is safe because erasing the block does not remove
605df3e71e0SMarcello Maggioni     // its entry from the loop's block list.  We do that in the next section.
606df3e71e0SMarcello Maggioni     for (Loop::block_iterator LpI = L->block_begin(), LpE = L->block_end();
607df3e71e0SMarcello Maggioni          LpI != LpE; ++LpI)
608df3e71e0SMarcello Maggioni       (*LpI)->eraseFromParent();
609df3e71e0SMarcello Maggioni 
610df3e71e0SMarcello Maggioni     // Finally, the blocks from loopinfo.  This has to happen late because
611df3e71e0SMarcello Maggioni     // otherwise our loop iterators won't work.
612df3e71e0SMarcello Maggioni 
613df3e71e0SMarcello Maggioni     SmallPtrSet<BasicBlock *, 8> blocks;
614df3e71e0SMarcello Maggioni     blocks.insert(L->block_begin(), L->block_end());
615df3e71e0SMarcello Maggioni     for (BasicBlock *BB : blocks)
616df3e71e0SMarcello Maggioni       LI->removeBlock(BB);
617df3e71e0SMarcello Maggioni 
618df3e71e0SMarcello Maggioni     // The last step is to update LoopInfo now that we've eliminated this loop.
619df3e71e0SMarcello Maggioni     LI->erase(L);
620df3e71e0SMarcello Maggioni   }
621df3e71e0SMarcello Maggioni }
622df3e71e0SMarcello Maggioni 
62341d72a86SDehao Chen Optional<unsigned> llvm::getLoopEstimatedTripCount(Loop *L) {
62441d72a86SDehao Chen   // Only support loops with a unique exiting block, and a latch.
62541d72a86SDehao Chen   if (!L->getExitingBlock())
62641d72a86SDehao Chen     return None;
62741d72a86SDehao Chen 
628d24ddcd6SHiroshi Inoue   // Get the branch weights for the loop's backedge.
62941d72a86SDehao Chen   BranchInst *LatchBR =
63041d72a86SDehao Chen       dyn_cast<BranchInst>(L->getLoopLatch()->getTerminator());
63141d72a86SDehao Chen   if (!LatchBR || LatchBR->getNumSuccessors() != 2)
63241d72a86SDehao Chen     return None;
63341d72a86SDehao Chen 
63441d72a86SDehao Chen   assert((LatchBR->getSuccessor(0) == L->getHeader() ||
63541d72a86SDehao Chen           LatchBR->getSuccessor(1) == L->getHeader()) &&
63641d72a86SDehao Chen          "At least one edge out of the latch must go to the header");
63741d72a86SDehao Chen 
63841d72a86SDehao Chen   // To estimate the number of times the loop body was executed, we want to
63941d72a86SDehao Chen   // know the number of times the backedge was taken, vs. the number of times
64041d72a86SDehao Chen   // we exited the loop.
64141d72a86SDehao Chen   uint64_t TrueVal, FalseVal;
642b151a641SMichael Kuperstein   if (!LatchBR->extractProfMetadata(TrueVal, FalseVal))
64341d72a86SDehao Chen     return None;
64441d72a86SDehao Chen 
645b151a641SMichael Kuperstein   if (!TrueVal || !FalseVal)
646b151a641SMichael Kuperstein     return 0;
64741d72a86SDehao Chen 
648b151a641SMichael Kuperstein   // Divide the count of the backedge by the count of the edge exiting the loop,
649b151a641SMichael Kuperstein   // rounding to nearest.
65041d72a86SDehao Chen   if (LatchBR->getSuccessor(0) == L->getHeader())
651b151a641SMichael Kuperstein     return (TrueVal + (FalseVal / 2)) / FalseVal;
65241d72a86SDehao Chen   else
653b151a641SMichael Kuperstein     return (FalseVal + (TrueVal / 2)) / TrueVal;
65441d72a86SDehao Chen }
655cf9daa33SAmara Emerson 
6566cb64787SDavid Green bool llvm::hasIterationCountInvariantInParent(Loop *InnerLoop,
657395b80cdSDavid Green                                               ScalarEvolution &SE) {
658395b80cdSDavid Green   Loop *OuterL = InnerLoop->getParentLoop();
659395b80cdSDavid Green   if (!OuterL)
660395b80cdSDavid Green     return true;
661395b80cdSDavid Green 
662395b80cdSDavid Green   // Get the backedge taken count for the inner loop
663395b80cdSDavid Green   BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
664395b80cdSDavid Green   const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch);
665395b80cdSDavid Green   if (isa<SCEVCouldNotCompute>(InnerLoopBECountSC) ||
666395b80cdSDavid Green       !InnerLoopBECountSC->getType()->isIntegerTy())
667395b80cdSDavid Green     return false;
668395b80cdSDavid Green 
669395b80cdSDavid Green   // Get whether count is invariant to the outer loop
670395b80cdSDavid Green   ScalarEvolution::LoopDisposition LD =
671395b80cdSDavid Green       SE.getLoopDisposition(InnerLoopBECountSC, OuterL);
672395b80cdSDavid Green   if (LD != ScalarEvolution::LoopInvariant)
673395b80cdSDavid Green     return false;
674395b80cdSDavid Green 
675395b80cdSDavid Green   return true;
676395b80cdSDavid Green }
677395b80cdSDavid Green 
6786594dc37SVikram TV Value *llvm::createMinMaxOp(IRBuilder<> &Builder,
6796594dc37SVikram TV                             RecurrenceDescriptor::MinMaxRecurrenceKind RK,
6806594dc37SVikram TV                             Value *Left, Value *Right) {
6816594dc37SVikram TV   CmpInst::Predicate P = CmpInst::ICMP_NE;
6826594dc37SVikram TV   switch (RK) {
6836594dc37SVikram TV   default:
6846594dc37SVikram TV     llvm_unreachable("Unknown min/max recurrence kind");
6856594dc37SVikram TV   case RecurrenceDescriptor::MRK_UIntMin:
6866594dc37SVikram TV     P = CmpInst::ICMP_ULT;
6876594dc37SVikram TV     break;
6886594dc37SVikram TV   case RecurrenceDescriptor::MRK_UIntMax:
6896594dc37SVikram TV     P = CmpInst::ICMP_UGT;
6906594dc37SVikram TV     break;
6916594dc37SVikram TV   case RecurrenceDescriptor::MRK_SIntMin:
6926594dc37SVikram TV     P = CmpInst::ICMP_SLT;
6936594dc37SVikram TV     break;
6946594dc37SVikram TV   case RecurrenceDescriptor::MRK_SIntMax:
6956594dc37SVikram TV     P = CmpInst::ICMP_SGT;
6966594dc37SVikram TV     break;
6976594dc37SVikram TV   case RecurrenceDescriptor::MRK_FloatMin:
6986594dc37SVikram TV     P = CmpInst::FCMP_OLT;
6996594dc37SVikram TV     break;
7006594dc37SVikram TV   case RecurrenceDescriptor::MRK_FloatMax:
7016594dc37SVikram TV     P = CmpInst::FCMP_OGT;
7026594dc37SVikram TV     break;
7036594dc37SVikram TV   }
7046594dc37SVikram TV 
7056594dc37SVikram TV   // We only match FP sequences that are 'fast', so we can unconditionally
7066594dc37SVikram TV   // set it on any generated instructions.
7076594dc37SVikram TV   IRBuilder<>::FastMathFlagGuard FMFG(Builder);
7086594dc37SVikram TV   FastMathFlags FMF;
7096594dc37SVikram TV   FMF.setFast();
7106594dc37SVikram TV   Builder.setFastMathFlags(FMF);
7116594dc37SVikram TV 
7126594dc37SVikram TV   Value *Cmp;
7136594dc37SVikram TV   if (RK == RecurrenceDescriptor::MRK_FloatMin ||
7146594dc37SVikram TV       RK == RecurrenceDescriptor::MRK_FloatMax)
7156594dc37SVikram TV     Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
7166594dc37SVikram TV   else
7176594dc37SVikram TV     Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
7186594dc37SVikram TV 
7196594dc37SVikram TV   Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
7206594dc37SVikram TV   return Select;
7216594dc37SVikram TV }
7226594dc37SVikram TV 
72323c2182cSSimon Pilgrim // Helper to generate an ordered reduction.
72423c2182cSSimon Pilgrim Value *
72523c2182cSSimon Pilgrim llvm::getOrderedReduction(IRBuilder<> &Builder, Value *Acc, Value *Src,
72623c2182cSSimon Pilgrim                           unsigned Op,
72723c2182cSSimon Pilgrim                           RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind,
72823c2182cSSimon Pilgrim                           ArrayRef<Value *> RedOps) {
72923c2182cSSimon Pilgrim   unsigned VF = Src->getType()->getVectorNumElements();
73023c2182cSSimon Pilgrim 
73123c2182cSSimon Pilgrim   // Extract and apply reduction ops in ascending order:
73223c2182cSSimon Pilgrim   // e.g. ((((Acc + Scl[0]) + Scl[1]) + Scl[2]) + ) ... + Scl[VF-1]
73323c2182cSSimon Pilgrim   Value *Result = Acc;
73423c2182cSSimon Pilgrim   for (unsigned ExtractIdx = 0; ExtractIdx != VF; ++ExtractIdx) {
73523c2182cSSimon Pilgrim     Value *Ext =
73623c2182cSSimon Pilgrim         Builder.CreateExtractElement(Src, Builder.getInt32(ExtractIdx));
73723c2182cSSimon Pilgrim 
73823c2182cSSimon Pilgrim     if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
73923c2182cSSimon Pilgrim       Result = Builder.CreateBinOp((Instruction::BinaryOps)Op, Result, Ext,
74023c2182cSSimon Pilgrim                                    "bin.rdx");
74123c2182cSSimon Pilgrim     } else {
74223c2182cSSimon Pilgrim       assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid &&
74323c2182cSSimon Pilgrim              "Invalid min/max");
7446594dc37SVikram TV       Result = createMinMaxOp(Builder, MinMaxKind, Result, Ext);
74523c2182cSSimon Pilgrim     }
74623c2182cSSimon Pilgrim 
74723c2182cSSimon Pilgrim     if (!RedOps.empty())
74823c2182cSSimon Pilgrim       propagateIRFlags(Result, RedOps);
74923c2182cSSimon Pilgrim   }
75023c2182cSSimon Pilgrim 
75123c2182cSSimon Pilgrim   return Result;
75223c2182cSSimon Pilgrim }
75323c2182cSSimon Pilgrim 
754cf9daa33SAmara Emerson // Helper to generate a log2 shuffle reduction.
755836b0f48SAmara Emerson Value *
756836b0f48SAmara Emerson llvm::getShuffleReduction(IRBuilder<> &Builder, Value *Src, unsigned Op,
757836b0f48SAmara Emerson                           RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind,
758ad62a3a2SSanjay Patel                           ArrayRef<Value *> RedOps) {
759cf9daa33SAmara Emerson   unsigned VF = Src->getType()->getVectorNumElements();
760cf9daa33SAmara Emerson   // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
761cf9daa33SAmara Emerson   // and vector ops, reducing the set of values being computed by half each
762cf9daa33SAmara Emerson   // round.
763cf9daa33SAmara Emerson   assert(isPowerOf2_32(VF) &&
764cf9daa33SAmara Emerson          "Reduction emission only supported for pow2 vectors!");
765cf9daa33SAmara Emerson   Value *TmpVec = Src;
766cf9daa33SAmara Emerson   SmallVector<Constant *, 32> ShuffleMask(VF, nullptr);
767cf9daa33SAmara Emerson   for (unsigned i = VF; i != 1; i >>= 1) {
768cf9daa33SAmara Emerson     // Move the upper half of the vector to the lower half.
769cf9daa33SAmara Emerson     for (unsigned j = 0; j != i / 2; ++j)
770cf9daa33SAmara Emerson       ShuffleMask[j] = Builder.getInt32(i / 2 + j);
771cf9daa33SAmara Emerson 
772cf9daa33SAmara Emerson     // Fill the rest of the mask with undef.
773cf9daa33SAmara Emerson     std::fill(&ShuffleMask[i / 2], ShuffleMask.end(),
774cf9daa33SAmara Emerson               UndefValue::get(Builder.getInt32Ty()));
775cf9daa33SAmara Emerson 
776cf9daa33SAmara Emerson     Value *Shuf = Builder.CreateShuffleVector(
777cf9daa33SAmara Emerson         TmpVec, UndefValue::get(TmpVec->getType()),
778cf9daa33SAmara Emerson         ConstantVector::get(ShuffleMask), "rdx.shuf");
779cf9daa33SAmara Emerson 
780cf9daa33SAmara Emerson     if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
781ad62a3a2SSanjay Patel       // The builder propagates its fast-math-flags setting.
782ad62a3a2SSanjay Patel       TmpVec = Builder.CreateBinOp((Instruction::BinaryOps)Op, TmpVec, Shuf,
783ad62a3a2SSanjay Patel                                    "bin.rdx");
784cf9daa33SAmara Emerson     } else {
785cf9daa33SAmara Emerson       assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid &&
786cf9daa33SAmara Emerson              "Invalid min/max");
7876594dc37SVikram TV       TmpVec = createMinMaxOp(Builder, MinMaxKind, TmpVec, Shuf);
788cf9daa33SAmara Emerson     }
789cf9daa33SAmara Emerson     if (!RedOps.empty())
790cf9daa33SAmara Emerson       propagateIRFlags(TmpVec, RedOps);
791cf9daa33SAmara Emerson   }
792cf9daa33SAmara Emerson   // The result is in the first element of the vector.
793cf9daa33SAmara Emerson   return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
794cf9daa33SAmara Emerson }
795cf9daa33SAmara Emerson 
796cf9daa33SAmara Emerson /// Create a simple vector reduction specified by an opcode and some
797cf9daa33SAmara Emerson /// flags (if generating min/max reductions).
798cf9daa33SAmara Emerson Value *llvm::createSimpleTargetReduction(
799cf9daa33SAmara Emerson     IRBuilder<> &Builder, const TargetTransformInfo *TTI, unsigned Opcode,
800ad62a3a2SSanjay Patel     Value *Src, TargetTransformInfo::ReductionFlags Flags,
801cf9daa33SAmara Emerson     ArrayRef<Value *> RedOps) {
802cf9daa33SAmara Emerson   assert(isa<VectorType>(Src->getType()) && "Type must be a vector");
803cf9daa33SAmara Emerson 
804cf9daa33SAmara Emerson   std::function<Value *()> BuildFunc;
805cf9daa33SAmara Emerson   using RD = RecurrenceDescriptor;
806cf9daa33SAmara Emerson   RD::MinMaxRecurrenceKind MinMaxKind = RD::MRK_Invalid;
807cf9daa33SAmara Emerson 
808cf9daa33SAmara Emerson   switch (Opcode) {
809cf9daa33SAmara Emerson   case Instruction::Add:
810cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateAddReduce(Src); };
811cf9daa33SAmara Emerson     break;
812cf9daa33SAmara Emerson   case Instruction::Mul:
813cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateMulReduce(Src); };
814cf9daa33SAmara Emerson     break;
815cf9daa33SAmara Emerson   case Instruction::And:
816cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateAndReduce(Src); };
817cf9daa33SAmara Emerson     break;
818cf9daa33SAmara Emerson   case Instruction::Or:
819cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateOrReduce(Src); };
820cf9daa33SAmara Emerson     break;
821cf9daa33SAmara Emerson   case Instruction::Xor:
822cf9daa33SAmara Emerson     BuildFunc = [&]() { return Builder.CreateXorReduce(Src); };
823cf9daa33SAmara Emerson     break;
824cf9daa33SAmara Emerson   case Instruction::FAdd:
825cf9daa33SAmara Emerson     BuildFunc = [&]() {
826*cbeb563cSSander de Smalen       auto Rdx = Builder.CreateFAddReduce(
827*cbeb563cSSander de Smalen           Constant::getNullValue(Src->getType()->getVectorElementType()), Src);
828cf9daa33SAmara Emerson       return Rdx;
829cf9daa33SAmara Emerson     };
830cf9daa33SAmara Emerson     break;
831cf9daa33SAmara Emerson   case Instruction::FMul:
832cf9daa33SAmara Emerson     BuildFunc = [&]() {
833*cbeb563cSSander de Smalen       Type *Ty = Src->getType()->getVectorElementType();
834*cbeb563cSSander de Smalen       auto Rdx = Builder.CreateFMulReduce(ConstantFP::get(Ty, 1.0), Src);
835cf9daa33SAmara Emerson       return Rdx;
836cf9daa33SAmara Emerson     };
837cf9daa33SAmara Emerson     break;
838cf9daa33SAmara Emerson   case Instruction::ICmp:
839cf9daa33SAmara Emerson     if (Flags.IsMaxOp) {
840cf9daa33SAmara Emerson       MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMax : RD::MRK_UIntMax;
841cf9daa33SAmara Emerson       BuildFunc = [&]() {
842cf9daa33SAmara Emerson         return Builder.CreateIntMaxReduce(Src, Flags.IsSigned);
843cf9daa33SAmara Emerson       };
844cf9daa33SAmara Emerson     } else {
845cf9daa33SAmara Emerson       MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMin : RD::MRK_UIntMin;
846cf9daa33SAmara Emerson       BuildFunc = [&]() {
847cf9daa33SAmara Emerson         return Builder.CreateIntMinReduce(Src, Flags.IsSigned);
848cf9daa33SAmara Emerson       };
849cf9daa33SAmara Emerson     }
850cf9daa33SAmara Emerson     break;
851cf9daa33SAmara Emerson   case Instruction::FCmp:
852cf9daa33SAmara Emerson     if (Flags.IsMaxOp) {
853cf9daa33SAmara Emerson       MinMaxKind = RD::MRK_FloatMax;
854cf9daa33SAmara Emerson       BuildFunc = [&]() { return Builder.CreateFPMaxReduce(Src, Flags.NoNaN); };
855cf9daa33SAmara Emerson     } else {
856cf9daa33SAmara Emerson       MinMaxKind = RD::MRK_FloatMin;
857cf9daa33SAmara Emerson       BuildFunc = [&]() { return Builder.CreateFPMinReduce(Src, Flags.NoNaN); };
858cf9daa33SAmara Emerson     }
859cf9daa33SAmara Emerson     break;
860cf9daa33SAmara Emerson   default:
861cf9daa33SAmara Emerson     llvm_unreachable("Unhandled opcode");
862cf9daa33SAmara Emerson     break;
863cf9daa33SAmara Emerson   }
864cf9daa33SAmara Emerson   if (TTI->useReductionIntrinsic(Opcode, Src->getType(), Flags))
865cf9daa33SAmara Emerson     return BuildFunc();
866ad62a3a2SSanjay Patel   return getShuffleReduction(Builder, Src, Opcode, MinMaxKind, RedOps);
867cf9daa33SAmara Emerson }
868cf9daa33SAmara Emerson 
869cf9daa33SAmara Emerson /// Create a vector reduction using a given recurrence descriptor.
8703e069f57SSanjay Patel Value *llvm::createTargetReduction(IRBuilder<> &B,
871cf9daa33SAmara Emerson                                    const TargetTransformInfo *TTI,
872cf9daa33SAmara Emerson                                    RecurrenceDescriptor &Desc, Value *Src,
873cf9daa33SAmara Emerson                                    bool NoNaN) {
874cf9daa33SAmara Emerson   // TODO: Support in-order reductions based on the recurrence descriptor.
8753e069f57SSanjay Patel   using RD = RecurrenceDescriptor;
8763e069f57SSanjay Patel   RD::RecurrenceKind RecKind = Desc.getRecurrenceKind();
877cf9daa33SAmara Emerson   TargetTransformInfo::ReductionFlags Flags;
878cf9daa33SAmara Emerson   Flags.NoNaN = NoNaN;
879ad62a3a2SSanjay Patel 
880ad62a3a2SSanjay Patel   // All ops in the reduction inherit fast-math-flags from the recurrence
881ad62a3a2SSanjay Patel   // descriptor.
882ad62a3a2SSanjay Patel   IRBuilder<>::FastMathFlagGuard FMFGuard(B);
883ad62a3a2SSanjay Patel   B.setFastMathFlags(Desc.getFastMathFlags());
884ad62a3a2SSanjay Patel 
885cf9daa33SAmara Emerson   switch (RecKind) {
8863e069f57SSanjay Patel   case RD::RK_FloatAdd:
887ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FAdd, Src, Flags);
8883e069f57SSanjay Patel   case RD::RK_FloatMult:
889ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FMul, Src, Flags);
8903e069f57SSanjay Patel   case RD::RK_IntegerAdd:
891ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Add, Src, Flags);
8923e069f57SSanjay Patel   case RD::RK_IntegerMult:
893ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Mul, Src, Flags);
8943e069f57SSanjay Patel   case RD::RK_IntegerAnd:
895ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::And, Src, Flags);
8963e069f57SSanjay Patel   case RD::RK_IntegerOr:
897ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Or, Src, Flags);
8983e069f57SSanjay Patel   case RD::RK_IntegerXor:
899ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::Xor, Src, Flags);
9003e069f57SSanjay Patel   case RD::RK_IntegerMinMax: {
9013e069f57SSanjay Patel     RD::MinMaxRecurrenceKind MMKind = Desc.getMinMaxRecurrenceKind();
9023e069f57SSanjay Patel     Flags.IsMaxOp = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_UIntMax);
9033e069f57SSanjay Patel     Flags.IsSigned = (MMKind == RD::MRK_SIntMax || MMKind == RD::MRK_SIntMin);
904ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::ICmp, Src, Flags);
905cf9daa33SAmara Emerson   }
9063e069f57SSanjay Patel   case RD::RK_FloatMinMax: {
9073e069f57SSanjay Patel     Flags.IsMaxOp = Desc.getMinMaxRecurrenceKind() == RD::MRK_FloatMax;
908ad62a3a2SSanjay Patel     return createSimpleTargetReduction(B, TTI, Instruction::FCmp, Src, Flags);
909cf9daa33SAmara Emerson   }
910cf9daa33SAmara Emerson   default:
911cf9daa33SAmara Emerson     llvm_unreachable("Unhandled RecKind");
912cf9daa33SAmara Emerson   }
913cf9daa33SAmara Emerson }
914cf9daa33SAmara Emerson 
915a61f4b89SDinar Temirbulatov void llvm::propagateIRFlags(Value *I, ArrayRef<Value *> VL, Value *OpValue) {
916a61f4b89SDinar Temirbulatov   auto *VecOp = dyn_cast<Instruction>(I);
917a61f4b89SDinar Temirbulatov   if (!VecOp)
918a61f4b89SDinar Temirbulatov     return;
919a61f4b89SDinar Temirbulatov   auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0])
920a61f4b89SDinar Temirbulatov                                             : dyn_cast<Instruction>(OpValue);
921a61f4b89SDinar Temirbulatov   if (!Intersection)
922a61f4b89SDinar Temirbulatov     return;
923a61f4b89SDinar Temirbulatov   const unsigned Opcode = Intersection->getOpcode();
924a61f4b89SDinar Temirbulatov   VecOp->copyIRFlags(Intersection);
925a61f4b89SDinar Temirbulatov   for (auto *V : VL) {
926a61f4b89SDinar Temirbulatov     auto *Instr = dyn_cast<Instruction>(V);
927a61f4b89SDinar Temirbulatov     if (!Instr)
928a61f4b89SDinar Temirbulatov       continue;
929a61f4b89SDinar Temirbulatov     if (OpValue == nullptr || Opcode == Instr->getOpcode())
930a61f4b89SDinar Temirbulatov       VecOp->andIRFlags(V);
931cf9daa33SAmara Emerson   }
932cf9daa33SAmara Emerson }
933a78dc4d6SMax Kazantsev 
934a78dc4d6SMax Kazantsev bool llvm::isKnownNegativeInLoop(const SCEV *S, const Loop *L,
935a78dc4d6SMax Kazantsev                                  ScalarEvolution &SE) {
936a78dc4d6SMax Kazantsev   const SCEV *Zero = SE.getZero(S->getType());
937a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
938a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, S, Zero);
939a78dc4d6SMax Kazantsev }
940a78dc4d6SMax Kazantsev 
941a78dc4d6SMax Kazantsev bool llvm::isKnownNonNegativeInLoop(const SCEV *S, const Loop *L,
942a78dc4d6SMax Kazantsev                                     ScalarEvolution &SE) {
943a78dc4d6SMax Kazantsev   const SCEV *Zero = SE.getZero(S->getType());
944a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
945a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, S, Zero);
946a78dc4d6SMax Kazantsev }
947a78dc4d6SMax Kazantsev 
948a78dc4d6SMax Kazantsev bool llvm::cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
949a78dc4d6SMax Kazantsev                              bool Signed) {
950a78dc4d6SMax Kazantsev   unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
951a78dc4d6SMax Kazantsev   APInt Min = Signed ? APInt::getSignedMinValue(BitWidth) :
952a78dc4d6SMax Kazantsev     APInt::getMinValue(BitWidth);
953a78dc4d6SMax Kazantsev   auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
954a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
955a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, Predicate, S,
956a78dc4d6SMax Kazantsev                                      SE.getConstant(Min));
957a78dc4d6SMax Kazantsev }
958a78dc4d6SMax Kazantsev 
959a78dc4d6SMax Kazantsev bool llvm::cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
960a78dc4d6SMax Kazantsev                              bool Signed) {
961a78dc4d6SMax Kazantsev   unsigned BitWidth = cast<IntegerType>(S->getType())->getBitWidth();
962a78dc4d6SMax Kazantsev   APInt Max = Signed ? APInt::getSignedMaxValue(BitWidth) :
963a78dc4d6SMax Kazantsev     APInt::getMaxValue(BitWidth);
964a78dc4d6SMax Kazantsev   auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
965a78dc4d6SMax Kazantsev   return SE.isAvailableAtLoopEntry(S, L) &&
966a78dc4d6SMax Kazantsev          SE.isLoopEntryGuardedByCond(L, Predicate, S,
967a78dc4d6SMax Kazantsev                                      SE.getConstant(Max));
968a78dc4d6SMax Kazantsev }
969