176aa662cSKarthik Bhat //===-- LoopUtils.cpp - Loop Utility functions -------------------------===//
276aa662cSKarthik Bhat //
376aa662cSKarthik Bhat //                     The LLVM Compiler Infrastructure
476aa662cSKarthik Bhat //
576aa662cSKarthik Bhat // This file is distributed under the University of Illinois Open Source
676aa662cSKarthik Bhat // License. See LICENSE.TXT for details.
776aa662cSKarthik Bhat //
876aa662cSKarthik Bhat //===----------------------------------------------------------------------===//
976aa662cSKarthik Bhat //
1076aa662cSKarthik Bhat // This file defines common loop utility functions.
1176aa662cSKarthik Bhat //
1276aa662cSKarthik Bhat //===----------------------------------------------------------------------===//
1376aa662cSKarthik Bhat 
1476aa662cSKarthik Bhat #include "llvm/Analysis/LoopInfo.h"
1576aa662cSKarthik Bhat #include "llvm/IR/Instructions.h"
1676aa662cSKarthik Bhat #include "llvm/IR/PatternMatch.h"
1776aa662cSKarthik Bhat #include "llvm/IR/ValueHandle.h"
1876aa662cSKarthik Bhat #include "llvm/Support/Debug.h"
1924e6cc2dSKarthik Bhat #include "llvm/Analysis/ScalarEvolution.h"
2024e6cc2dSKarthik Bhat #include "llvm/Analysis/ScalarEvolutionExpressions.h"
2124e6cc2dSKarthik Bhat #include "llvm/IR/Module.h"
2276aa662cSKarthik Bhat #include "llvm/Transforms/Utils/LoopUtils.h"
2376aa662cSKarthik Bhat 
2476aa662cSKarthik Bhat using namespace llvm;
2576aa662cSKarthik Bhat using namespace llvm::PatternMatch;
2676aa662cSKarthik Bhat 
2776aa662cSKarthik Bhat #define DEBUG_TYPE "loop-utils"
2876aa662cSKarthik Bhat 
290a91310cSTyler Nowicki bool RecurrenceDescriptor::areAllUsesIn(Instruction *I,
3076aa662cSKarthik Bhat                                         SmallPtrSetImpl<Instruction *> &Set) {
3176aa662cSKarthik Bhat   for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use)
3276aa662cSKarthik Bhat     if (!Set.count(dyn_cast<Instruction>(*Use)))
3376aa662cSKarthik Bhat       return false;
3476aa662cSKarthik Bhat   return true;
3576aa662cSKarthik Bhat }
3676aa662cSKarthik Bhat 
370a91310cSTyler Nowicki bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurrenceKind Kind,
3876aa662cSKarthik Bhat                                            Loop *TheLoop, bool HasFunNoNaNAttr,
390a91310cSTyler Nowicki                                            RecurrenceDescriptor &RedDes) {
4076aa662cSKarthik Bhat   if (Phi->getNumIncomingValues() != 2)
4176aa662cSKarthik Bhat     return false;
4276aa662cSKarthik Bhat 
4376aa662cSKarthik Bhat   // Reduction variables are only found in the loop header block.
4476aa662cSKarthik Bhat   if (Phi->getParent() != TheLoop->getHeader())
4576aa662cSKarthik Bhat     return false;
4676aa662cSKarthik Bhat 
4776aa662cSKarthik Bhat   // Obtain the reduction start value from the value that comes from the loop
4876aa662cSKarthik Bhat   // preheader.
4976aa662cSKarthik Bhat   Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
5076aa662cSKarthik Bhat 
5176aa662cSKarthik Bhat   // ExitInstruction is the single value which is used outside the loop.
5276aa662cSKarthik Bhat   // We only allow for a single reduction value to be used outside the loop.
5376aa662cSKarthik Bhat   // This includes users of the reduction, variables (which form a cycle
5476aa662cSKarthik Bhat   // which ends in the phi node).
5576aa662cSKarthik Bhat   Instruction *ExitInstruction = nullptr;
5676aa662cSKarthik Bhat   // Indicates that we found a reduction operation in our scan.
5776aa662cSKarthik Bhat   bool FoundReduxOp = false;
5876aa662cSKarthik Bhat 
5976aa662cSKarthik Bhat   // We start with the PHI node and scan for all of the users of this
6076aa662cSKarthik Bhat   // instruction. All users must be instructions that can be used as reduction
6176aa662cSKarthik Bhat   // variables (such as ADD). We must have a single out-of-block user. The cycle
6276aa662cSKarthik Bhat   // must include the original PHI.
6376aa662cSKarthik Bhat   bool FoundStartPHI = false;
6476aa662cSKarthik Bhat 
6576aa662cSKarthik Bhat   // To recognize min/max patterns formed by a icmp select sequence, we store
6676aa662cSKarthik Bhat   // the number of instruction we saw from the recognized min/max pattern,
6776aa662cSKarthik Bhat   //  to make sure we only see exactly the two instructions.
6876aa662cSKarthik Bhat   unsigned NumCmpSelectPatternInst = 0;
69*27b2c39eSTyler Nowicki   InstDesc ReduxDesc(false, nullptr);
7076aa662cSKarthik Bhat 
7176aa662cSKarthik Bhat   SmallPtrSet<Instruction *, 8> VisitedInsts;
7276aa662cSKarthik Bhat   SmallVector<Instruction *, 8> Worklist;
7376aa662cSKarthik Bhat   Worklist.push_back(Phi);
7476aa662cSKarthik Bhat   VisitedInsts.insert(Phi);
7576aa662cSKarthik Bhat 
7676aa662cSKarthik Bhat   // A value in the reduction can be used:
7776aa662cSKarthik Bhat   //  - By the reduction:
7876aa662cSKarthik Bhat   //      - Reduction operation:
7976aa662cSKarthik Bhat   //        - One use of reduction value (safe).
8076aa662cSKarthik Bhat   //        - Multiple use of reduction value (not safe).
8176aa662cSKarthik Bhat   //      - PHI:
8276aa662cSKarthik Bhat   //        - All uses of the PHI must be the reduction (safe).
8376aa662cSKarthik Bhat   //        - Otherwise, not safe.
8476aa662cSKarthik Bhat   //  - By one instruction outside of the loop (safe).
8576aa662cSKarthik Bhat   //  - By further instructions outside of the loop (not safe).
8676aa662cSKarthik Bhat   //  - By an instruction that is not part of the reduction (not safe).
8776aa662cSKarthik Bhat   //    This is either:
8876aa662cSKarthik Bhat   //      * An instruction type other than PHI or the reduction operation.
8976aa662cSKarthik Bhat   //      * A PHI in the header other than the initial PHI.
9076aa662cSKarthik Bhat   while (!Worklist.empty()) {
9176aa662cSKarthik Bhat     Instruction *Cur = Worklist.back();
9276aa662cSKarthik Bhat     Worklist.pop_back();
9376aa662cSKarthik Bhat 
9476aa662cSKarthik Bhat     // No Users.
9576aa662cSKarthik Bhat     // If the instruction has no users then this is a broken chain and can't be
9676aa662cSKarthik Bhat     // a reduction variable.
9776aa662cSKarthik Bhat     if (Cur->use_empty())
9876aa662cSKarthik Bhat       return false;
9976aa662cSKarthik Bhat 
10076aa662cSKarthik Bhat     bool IsAPhi = isa<PHINode>(Cur);
10176aa662cSKarthik Bhat 
10276aa662cSKarthik Bhat     // A header PHI use other than the original PHI.
10376aa662cSKarthik Bhat     if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent())
10476aa662cSKarthik Bhat       return false;
10576aa662cSKarthik Bhat 
10676aa662cSKarthik Bhat     // Reductions of instructions such as Div, and Sub is only possible if the
10776aa662cSKarthik Bhat     // LHS is the reduction variable.
10876aa662cSKarthik Bhat     if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) &&
10976aa662cSKarthik Bhat         !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
11076aa662cSKarthik Bhat         !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0))))
11176aa662cSKarthik Bhat       return false;
11276aa662cSKarthik Bhat 
11376aa662cSKarthik Bhat     // Any reduction instruction must be of one of the allowed kinds.
1140a91310cSTyler Nowicki     ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr);
1150a91310cSTyler Nowicki     if (!ReduxDesc.isRecurrence())
11676aa662cSKarthik Bhat       return false;
11776aa662cSKarthik Bhat 
11876aa662cSKarthik Bhat     // A reduction operation must only have one use of the reduction value.
11976aa662cSKarthik Bhat     if (!IsAPhi && Kind != RK_IntegerMinMax && Kind != RK_FloatMinMax &&
12076aa662cSKarthik Bhat         hasMultipleUsesOf(Cur, VisitedInsts))
12176aa662cSKarthik Bhat       return false;
12276aa662cSKarthik Bhat 
12376aa662cSKarthik Bhat     // All inputs to a PHI node must be a reduction value.
12476aa662cSKarthik Bhat     if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts))
12576aa662cSKarthik Bhat       return false;
12676aa662cSKarthik Bhat 
12776aa662cSKarthik Bhat     if (Kind == RK_IntegerMinMax &&
12876aa662cSKarthik Bhat         (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur)))
12976aa662cSKarthik Bhat       ++NumCmpSelectPatternInst;
13076aa662cSKarthik Bhat     if (Kind == RK_FloatMinMax && (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur)))
13176aa662cSKarthik Bhat       ++NumCmpSelectPatternInst;
13276aa662cSKarthik Bhat 
13376aa662cSKarthik Bhat     // Check  whether we found a reduction operator.
13476aa662cSKarthik Bhat     FoundReduxOp |= !IsAPhi;
13576aa662cSKarthik Bhat 
13676aa662cSKarthik Bhat     // Process users of current instruction. Push non-PHI nodes after PHI nodes
13776aa662cSKarthik Bhat     // onto the stack. This way we are going to have seen all inputs to PHI
13876aa662cSKarthik Bhat     // nodes once we get to them.
13976aa662cSKarthik Bhat     SmallVector<Instruction *, 8> NonPHIs;
14076aa662cSKarthik Bhat     SmallVector<Instruction *, 8> PHIs;
14176aa662cSKarthik Bhat     for (User *U : Cur->users()) {
14276aa662cSKarthik Bhat       Instruction *UI = cast<Instruction>(U);
14376aa662cSKarthik Bhat 
14476aa662cSKarthik Bhat       // Check if we found the exit user.
14576aa662cSKarthik Bhat       BasicBlock *Parent = UI->getParent();
14676aa662cSKarthik Bhat       if (!TheLoop->contains(Parent)) {
14776aa662cSKarthik Bhat         // Exit if you find multiple outside users or if the header phi node is
14876aa662cSKarthik Bhat         // being used. In this case the user uses the value of the previous
14976aa662cSKarthik Bhat         // iteration, in which case we would loose "VF-1" iterations of the
15076aa662cSKarthik Bhat         // reduction operation if we vectorize.
15176aa662cSKarthik Bhat         if (ExitInstruction != nullptr || Cur == Phi)
15276aa662cSKarthik Bhat           return false;
15376aa662cSKarthik Bhat 
15476aa662cSKarthik Bhat         // The instruction used by an outside user must be the last instruction
15576aa662cSKarthik Bhat         // before we feed back to the reduction phi. Otherwise, we loose VF-1
15676aa662cSKarthik Bhat         // operations on the value.
15776aa662cSKarthik Bhat         if (std::find(Phi->op_begin(), Phi->op_end(), Cur) == Phi->op_end())
15876aa662cSKarthik Bhat           return false;
15976aa662cSKarthik Bhat 
16076aa662cSKarthik Bhat         ExitInstruction = Cur;
16176aa662cSKarthik Bhat         continue;
16276aa662cSKarthik Bhat       }
16376aa662cSKarthik Bhat 
16476aa662cSKarthik Bhat       // Process instructions only once (termination). Each reduction cycle
16576aa662cSKarthik Bhat       // value must only be used once, except by phi nodes and min/max
16676aa662cSKarthik Bhat       // reductions which are represented as a cmp followed by a select.
167*27b2c39eSTyler Nowicki       InstDesc IgnoredVal(false, nullptr);
16876aa662cSKarthik Bhat       if (VisitedInsts.insert(UI).second) {
16976aa662cSKarthik Bhat         if (isa<PHINode>(UI))
17076aa662cSKarthik Bhat           PHIs.push_back(UI);
17176aa662cSKarthik Bhat         else
17276aa662cSKarthik Bhat           NonPHIs.push_back(UI);
17376aa662cSKarthik Bhat       } else if (!isa<PHINode>(UI) &&
17476aa662cSKarthik Bhat                  ((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) &&
17576aa662cSKarthik Bhat                    !isa<SelectInst>(UI)) ||
1760a91310cSTyler Nowicki                   !isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence()))
17776aa662cSKarthik Bhat         return false;
17876aa662cSKarthik Bhat 
17976aa662cSKarthik Bhat       // Remember that we completed the cycle.
18076aa662cSKarthik Bhat       if (UI == Phi)
18176aa662cSKarthik Bhat         FoundStartPHI = true;
18276aa662cSKarthik Bhat     }
18376aa662cSKarthik Bhat     Worklist.append(PHIs.begin(), PHIs.end());
18476aa662cSKarthik Bhat     Worklist.append(NonPHIs.begin(), NonPHIs.end());
18576aa662cSKarthik Bhat   }
18676aa662cSKarthik Bhat 
18776aa662cSKarthik Bhat   // This means we have seen one but not the other instruction of the
18876aa662cSKarthik Bhat   // pattern or more than just a select and cmp.
18976aa662cSKarthik Bhat   if ((Kind == RK_IntegerMinMax || Kind == RK_FloatMinMax) &&
19076aa662cSKarthik Bhat       NumCmpSelectPatternInst != 2)
19176aa662cSKarthik Bhat     return false;
19276aa662cSKarthik Bhat 
19376aa662cSKarthik Bhat   if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction)
19476aa662cSKarthik Bhat     return false;
19576aa662cSKarthik Bhat 
19676aa662cSKarthik Bhat   // We found a reduction var if we have reached the original phi node and we
19776aa662cSKarthik Bhat   // only have a single instruction with out-of-loop users.
19876aa662cSKarthik Bhat 
19976aa662cSKarthik Bhat   // The ExitInstruction(Instruction which is allowed to have out-of-loop users)
2000a91310cSTyler Nowicki   // is saved as part of the RecurrenceDescriptor.
20176aa662cSKarthik Bhat 
20276aa662cSKarthik Bhat   // Save the description of this reduction variable.
2030a91310cSTyler Nowicki   RecurrenceDescriptor RD(RdxStart, ExitInstruction, Kind,
20476aa662cSKarthik Bhat                           ReduxDesc.getMinMaxKind());
20576aa662cSKarthik Bhat 
20676aa662cSKarthik Bhat   RedDes = RD;
20776aa662cSKarthik Bhat 
20876aa662cSKarthik Bhat   return true;
20976aa662cSKarthik Bhat }
21076aa662cSKarthik Bhat 
21176aa662cSKarthik Bhat /// Returns true if the instruction is a Select(ICmp(X, Y), X, Y) instruction
21276aa662cSKarthik Bhat /// pattern corresponding to a min(X, Y) or max(X, Y).
213*27b2c39eSTyler Nowicki RecurrenceDescriptor::InstDesc
214*27b2c39eSTyler Nowicki RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev) {
21576aa662cSKarthik Bhat 
21676aa662cSKarthik Bhat   assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) &&
21776aa662cSKarthik Bhat          "Expect a select instruction");
21876aa662cSKarthik Bhat   Instruction *Cmp = nullptr;
21976aa662cSKarthik Bhat   SelectInst *Select = nullptr;
22076aa662cSKarthik Bhat 
22176aa662cSKarthik Bhat   // We must handle the select(cmp()) as a single instruction. Advance to the
22276aa662cSKarthik Bhat   // select.
22376aa662cSKarthik Bhat   if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) {
22476aa662cSKarthik Bhat     if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin())))
225*27b2c39eSTyler Nowicki       return InstDesc(false, I);
226*27b2c39eSTyler Nowicki     return InstDesc(Select, Prev.getMinMaxKind());
22776aa662cSKarthik Bhat   }
22876aa662cSKarthik Bhat 
22976aa662cSKarthik Bhat   // Only handle single use cases for now.
23076aa662cSKarthik Bhat   if (!(Select = dyn_cast<SelectInst>(I)))
231*27b2c39eSTyler Nowicki     return InstDesc(false, I);
23276aa662cSKarthik Bhat   if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) &&
23376aa662cSKarthik Bhat       !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0))))
234*27b2c39eSTyler Nowicki     return InstDesc(false, I);
23576aa662cSKarthik Bhat   if (!Cmp->hasOneUse())
236*27b2c39eSTyler Nowicki     return InstDesc(false, I);
23776aa662cSKarthik Bhat 
23876aa662cSKarthik Bhat   Value *CmpLeft;
23976aa662cSKarthik Bhat   Value *CmpRight;
24076aa662cSKarthik Bhat 
24176aa662cSKarthik Bhat   // Look for a min/max pattern.
24276aa662cSKarthik Bhat   if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
243*27b2c39eSTyler Nowicki     return InstDesc(Select, MRK_UIntMin);
24476aa662cSKarthik Bhat   else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
245*27b2c39eSTyler Nowicki     return InstDesc(Select, MRK_UIntMax);
24676aa662cSKarthik Bhat   else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
247*27b2c39eSTyler Nowicki     return InstDesc(Select, MRK_SIntMax);
24876aa662cSKarthik Bhat   else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
249*27b2c39eSTyler Nowicki     return InstDesc(Select, MRK_SIntMin);
25076aa662cSKarthik Bhat   else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
251*27b2c39eSTyler Nowicki     return InstDesc(Select, MRK_FloatMin);
25276aa662cSKarthik Bhat   else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
253*27b2c39eSTyler Nowicki     return InstDesc(Select, MRK_FloatMax);
25476aa662cSKarthik Bhat   else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
255*27b2c39eSTyler Nowicki     return InstDesc(Select, MRK_FloatMin);
25676aa662cSKarthik Bhat   else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
257*27b2c39eSTyler Nowicki     return InstDesc(Select, MRK_FloatMax);
25876aa662cSKarthik Bhat 
259*27b2c39eSTyler Nowicki   return InstDesc(false, I);
26076aa662cSKarthik Bhat }
26176aa662cSKarthik Bhat 
262*27b2c39eSTyler Nowicki RecurrenceDescriptor::InstDesc
2630a91310cSTyler Nowicki RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
264*27b2c39eSTyler Nowicki                                         InstDesc &Prev, bool HasFunNoNaNAttr) {
26576aa662cSKarthik Bhat   bool FP = I->getType()->isFloatingPointTy();
26676aa662cSKarthik Bhat   bool FastMath = FP && I->hasUnsafeAlgebra();
26776aa662cSKarthik Bhat   switch (I->getOpcode()) {
26876aa662cSKarthik Bhat   default:
269*27b2c39eSTyler Nowicki     return InstDesc(false, I);
27076aa662cSKarthik Bhat   case Instruction::PHI:
27176aa662cSKarthik Bhat     if (FP &&
27276aa662cSKarthik Bhat         (Kind != RK_FloatMult && Kind != RK_FloatAdd && Kind != RK_FloatMinMax))
273*27b2c39eSTyler Nowicki       return InstDesc(false, I);
274*27b2c39eSTyler Nowicki     return InstDesc(I, Prev.getMinMaxKind());
27576aa662cSKarthik Bhat   case Instruction::Sub:
27676aa662cSKarthik Bhat   case Instruction::Add:
277*27b2c39eSTyler Nowicki     return InstDesc(Kind == RK_IntegerAdd, I);
27876aa662cSKarthik Bhat   case Instruction::Mul:
279*27b2c39eSTyler Nowicki     return InstDesc(Kind == RK_IntegerMult, I);
28076aa662cSKarthik Bhat   case Instruction::And:
281*27b2c39eSTyler Nowicki     return InstDesc(Kind == RK_IntegerAnd, I);
28276aa662cSKarthik Bhat   case Instruction::Or:
283*27b2c39eSTyler Nowicki     return InstDesc(Kind == RK_IntegerOr, I);
28476aa662cSKarthik Bhat   case Instruction::Xor:
285*27b2c39eSTyler Nowicki     return InstDesc(Kind == RK_IntegerXor, I);
28676aa662cSKarthik Bhat   case Instruction::FMul:
287*27b2c39eSTyler Nowicki     return InstDesc(Kind == RK_FloatMult && FastMath, I);
28876aa662cSKarthik Bhat   case Instruction::FSub:
28976aa662cSKarthik Bhat   case Instruction::FAdd:
290*27b2c39eSTyler Nowicki     return InstDesc(Kind == RK_FloatAdd && FastMath, I);
29176aa662cSKarthik Bhat   case Instruction::FCmp:
29276aa662cSKarthik Bhat   case Instruction::ICmp:
29376aa662cSKarthik Bhat   case Instruction::Select:
29476aa662cSKarthik Bhat     if (Kind != RK_IntegerMinMax &&
29576aa662cSKarthik Bhat         (!HasFunNoNaNAttr || Kind != RK_FloatMinMax))
296*27b2c39eSTyler Nowicki       return InstDesc(false, I);
29776aa662cSKarthik Bhat     return isMinMaxSelectCmpPattern(I, Prev);
29876aa662cSKarthik Bhat   }
29976aa662cSKarthik Bhat }
30076aa662cSKarthik Bhat 
3010a91310cSTyler Nowicki bool RecurrenceDescriptor::hasMultipleUsesOf(
30276aa662cSKarthik Bhat     Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) {
30376aa662cSKarthik Bhat   unsigned NumUses = 0;
30476aa662cSKarthik Bhat   for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E;
30576aa662cSKarthik Bhat        ++Use) {
30676aa662cSKarthik Bhat     if (Insts.count(dyn_cast<Instruction>(*Use)))
30776aa662cSKarthik Bhat       ++NumUses;
30876aa662cSKarthik Bhat     if (NumUses > 1)
30976aa662cSKarthik Bhat       return true;
31076aa662cSKarthik Bhat   }
31176aa662cSKarthik Bhat 
31276aa662cSKarthik Bhat   return false;
31376aa662cSKarthik Bhat }
3140a91310cSTyler Nowicki bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
3150a91310cSTyler Nowicki                                           RecurrenceDescriptor &RedDes) {
31676aa662cSKarthik Bhat 
31776aa662cSKarthik Bhat   bool HasFunNoNaNAttr = false;
31876aa662cSKarthik Bhat   BasicBlock *Header = TheLoop->getHeader();
31976aa662cSKarthik Bhat   Function &F = *Header->getParent();
32076aa662cSKarthik Bhat   if (F.hasFnAttribute("no-nans-fp-math"))
32176aa662cSKarthik Bhat     HasFunNoNaNAttr =
32276aa662cSKarthik Bhat         F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
32376aa662cSKarthik Bhat 
32476aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
32576aa662cSKarthik Bhat     DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
32676aa662cSKarthik Bhat     return true;
32776aa662cSKarthik Bhat   }
32876aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
32976aa662cSKarthik Bhat     DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
33076aa662cSKarthik Bhat     return true;
33176aa662cSKarthik Bhat   }
33276aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes)) {
33376aa662cSKarthik Bhat     DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
33476aa662cSKarthik Bhat     return true;
33576aa662cSKarthik Bhat   }
33676aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes)) {
33776aa662cSKarthik Bhat     DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
33876aa662cSKarthik Bhat     return true;
33976aa662cSKarthik Bhat   }
34076aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes)) {
34176aa662cSKarthik Bhat     DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
34276aa662cSKarthik Bhat     return true;
34376aa662cSKarthik Bhat   }
34476aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr,
34576aa662cSKarthik Bhat                       RedDes)) {
34676aa662cSKarthik Bhat     DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n");
34776aa662cSKarthik Bhat     return true;
34876aa662cSKarthik Bhat   }
34976aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
35076aa662cSKarthik Bhat     DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
35176aa662cSKarthik Bhat     return true;
35276aa662cSKarthik Bhat   }
35376aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
35476aa662cSKarthik Bhat     DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
35576aa662cSKarthik Bhat     return true;
35676aa662cSKarthik Bhat   }
35776aa662cSKarthik Bhat   if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes)) {
35876aa662cSKarthik Bhat     DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi << "\n");
35976aa662cSKarthik Bhat     return true;
36076aa662cSKarthik Bhat   }
36176aa662cSKarthik Bhat   // Not a reduction of known type.
36276aa662cSKarthik Bhat   return false;
36376aa662cSKarthik Bhat }
36476aa662cSKarthik Bhat 
36576aa662cSKarthik Bhat /// This function returns the identity element (or neutral element) for
36676aa662cSKarthik Bhat /// the operation K.
3670a91310cSTyler Nowicki Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K,
3680a91310cSTyler Nowicki                                                       Type *Tp) {
36976aa662cSKarthik Bhat   switch (K) {
37076aa662cSKarthik Bhat   case RK_IntegerXor:
37176aa662cSKarthik Bhat   case RK_IntegerAdd:
37276aa662cSKarthik Bhat   case RK_IntegerOr:
37376aa662cSKarthik Bhat     // Adding, Xoring, Oring zero to a number does not change it.
37476aa662cSKarthik Bhat     return ConstantInt::get(Tp, 0);
37576aa662cSKarthik Bhat   case RK_IntegerMult:
37676aa662cSKarthik Bhat     // Multiplying a number by 1 does not change it.
37776aa662cSKarthik Bhat     return ConstantInt::get(Tp, 1);
37876aa662cSKarthik Bhat   case RK_IntegerAnd:
37976aa662cSKarthik Bhat     // AND-ing a number with an all-1 value does not change it.
38076aa662cSKarthik Bhat     return ConstantInt::get(Tp, -1, true);
38176aa662cSKarthik Bhat   case RK_FloatMult:
38276aa662cSKarthik Bhat     // Multiplying a number by 1 does not change it.
38376aa662cSKarthik Bhat     return ConstantFP::get(Tp, 1.0L);
38476aa662cSKarthik Bhat   case RK_FloatAdd:
38576aa662cSKarthik Bhat     // Adding zero to a number does not change it.
38676aa662cSKarthik Bhat     return ConstantFP::get(Tp, 0.0L);
38776aa662cSKarthik Bhat   default:
3880a91310cSTyler Nowicki     llvm_unreachable("Unknown recurrence kind");
38976aa662cSKarthik Bhat   }
39076aa662cSKarthik Bhat }
39176aa662cSKarthik Bhat 
3920a91310cSTyler Nowicki /// This function translates the recurrence kind to an LLVM binary operator.
3930a91310cSTyler Nowicki unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) {
39476aa662cSKarthik Bhat   switch (Kind) {
39576aa662cSKarthik Bhat   case RK_IntegerAdd:
39676aa662cSKarthik Bhat     return Instruction::Add;
39776aa662cSKarthik Bhat   case RK_IntegerMult:
39876aa662cSKarthik Bhat     return Instruction::Mul;
39976aa662cSKarthik Bhat   case RK_IntegerOr:
40076aa662cSKarthik Bhat     return Instruction::Or;
40176aa662cSKarthik Bhat   case RK_IntegerAnd:
40276aa662cSKarthik Bhat     return Instruction::And;
40376aa662cSKarthik Bhat   case RK_IntegerXor:
40476aa662cSKarthik Bhat     return Instruction::Xor;
40576aa662cSKarthik Bhat   case RK_FloatMult:
40676aa662cSKarthik Bhat     return Instruction::FMul;
40776aa662cSKarthik Bhat   case RK_FloatAdd:
40876aa662cSKarthik Bhat     return Instruction::FAdd;
40976aa662cSKarthik Bhat   case RK_IntegerMinMax:
41076aa662cSKarthik Bhat     return Instruction::ICmp;
41176aa662cSKarthik Bhat   case RK_FloatMinMax:
41276aa662cSKarthik Bhat     return Instruction::FCmp;
41376aa662cSKarthik Bhat   default:
4140a91310cSTyler Nowicki     llvm_unreachable("Unknown recurrence operation");
41576aa662cSKarthik Bhat   }
41676aa662cSKarthik Bhat }
41776aa662cSKarthik Bhat 
418*27b2c39eSTyler Nowicki Value *RecurrenceDescriptor::createMinMaxOp(IRBuilder<> &Builder,
419*27b2c39eSTyler Nowicki                                             MinMaxRecurrenceKind RK,
42076aa662cSKarthik Bhat                                             Value *Left, Value *Right) {
42176aa662cSKarthik Bhat   CmpInst::Predicate P = CmpInst::ICMP_NE;
42276aa662cSKarthik Bhat   switch (RK) {
42376aa662cSKarthik Bhat   default:
4240a91310cSTyler Nowicki     llvm_unreachable("Unknown min/max recurrence kind");
425*27b2c39eSTyler Nowicki   case MRK_UIntMin:
42676aa662cSKarthik Bhat     P = CmpInst::ICMP_ULT;
42776aa662cSKarthik Bhat     break;
428*27b2c39eSTyler Nowicki   case MRK_UIntMax:
42976aa662cSKarthik Bhat     P = CmpInst::ICMP_UGT;
43076aa662cSKarthik Bhat     break;
431*27b2c39eSTyler Nowicki   case MRK_SIntMin:
43276aa662cSKarthik Bhat     P = CmpInst::ICMP_SLT;
43376aa662cSKarthik Bhat     break;
434*27b2c39eSTyler Nowicki   case MRK_SIntMax:
43576aa662cSKarthik Bhat     P = CmpInst::ICMP_SGT;
43676aa662cSKarthik Bhat     break;
437*27b2c39eSTyler Nowicki   case MRK_FloatMin:
43876aa662cSKarthik Bhat     P = CmpInst::FCMP_OLT;
43976aa662cSKarthik Bhat     break;
440*27b2c39eSTyler Nowicki   case MRK_FloatMax:
44176aa662cSKarthik Bhat     P = CmpInst::FCMP_OGT;
44276aa662cSKarthik Bhat     break;
44376aa662cSKarthik Bhat   }
44476aa662cSKarthik Bhat 
44576aa662cSKarthik Bhat   Value *Cmp;
446*27b2c39eSTyler Nowicki   if (RK == MRK_FloatMin || RK == MRK_FloatMax)
44776aa662cSKarthik Bhat     Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
44876aa662cSKarthik Bhat   else
44976aa662cSKarthik Bhat     Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
45076aa662cSKarthik Bhat 
45176aa662cSKarthik Bhat   Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
45276aa662cSKarthik Bhat   return Select;
45376aa662cSKarthik Bhat }
45424e6cc2dSKarthik Bhat 
45524e6cc2dSKarthik Bhat bool llvm::isInductionPHI(PHINode *Phi, ScalarEvolution *SE,
45624e6cc2dSKarthik Bhat                           ConstantInt *&StepValue) {
45724e6cc2dSKarthik Bhat   Type *PhiTy = Phi->getType();
45824e6cc2dSKarthik Bhat   // We only handle integer and pointer inductions variables.
45924e6cc2dSKarthik Bhat   if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy())
46024e6cc2dSKarthik Bhat     return false;
46124e6cc2dSKarthik Bhat 
46224e6cc2dSKarthik Bhat   // Check that the PHI is consecutive.
46324e6cc2dSKarthik Bhat   const SCEV *PhiScev = SE->getSCEV(Phi);
46424e6cc2dSKarthik Bhat   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
46524e6cc2dSKarthik Bhat   if (!AR) {
46624e6cc2dSKarthik Bhat     DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
46724e6cc2dSKarthik Bhat     return false;
46824e6cc2dSKarthik Bhat   }
46924e6cc2dSKarthik Bhat 
47024e6cc2dSKarthik Bhat   const SCEV *Step = AR->getStepRecurrence(*SE);
47124e6cc2dSKarthik Bhat   // Calculate the pointer stride and check if it is consecutive.
47224e6cc2dSKarthik Bhat   const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
47324e6cc2dSKarthik Bhat   if (!C)
47424e6cc2dSKarthik Bhat     return false;
47524e6cc2dSKarthik Bhat 
47624e6cc2dSKarthik Bhat   ConstantInt *CV = C->getValue();
47724e6cc2dSKarthik Bhat   if (PhiTy->isIntegerTy()) {
47824e6cc2dSKarthik Bhat     StepValue = CV;
47924e6cc2dSKarthik Bhat     return true;
48024e6cc2dSKarthik Bhat   }
48124e6cc2dSKarthik Bhat 
48224e6cc2dSKarthik Bhat   assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
48324e6cc2dSKarthik Bhat   Type *PointerElementType = PhiTy->getPointerElementType();
48424e6cc2dSKarthik Bhat   // The pointer stride cannot be determined if the pointer element type is not
48524e6cc2dSKarthik Bhat   // sized.
48624e6cc2dSKarthik Bhat   if (!PointerElementType->isSized())
48724e6cc2dSKarthik Bhat     return false;
48824e6cc2dSKarthik Bhat 
48924e6cc2dSKarthik Bhat   const DataLayout &DL = Phi->getModule()->getDataLayout();
49024e6cc2dSKarthik Bhat   int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType));
491b58f32f7SDavid Majnemer   if (!Size)
492b58f32f7SDavid Majnemer     return false;
493b58f32f7SDavid Majnemer 
49424e6cc2dSKarthik Bhat   int64_t CVSize = CV->getSExtValue();
49524e6cc2dSKarthik Bhat   if (CVSize % Size)
49624e6cc2dSKarthik Bhat     return false;
49724e6cc2dSKarthik Bhat   StepValue = ConstantInt::getSigned(CV->getType(), CVSize / Size);
49824e6cc2dSKarthik Bhat   return true;
49924e6cc2dSKarthik Bhat }
500