2015-04-20 12:38:33 +08:00
|
|
|
//===-- LoopUtils.cpp - Loop Utility functions -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines common loop utility functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/PatternMatch.h"
|
|
|
|
#include "llvm/IR/ValueHandle.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-04-23 16:29:20 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2015-04-20 12:38:33 +08:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::PatternMatch;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "loop-utils"
|
|
|
|
|
2015-06-17 02:07:34 +08:00
|
|
|
bool RecurrenceDescriptor::areAllUsesIn(Instruction *I,
|
|
|
|
SmallPtrSetImpl<Instruction *> &Set) {
|
2015-04-20 12:38:33 +08:00
|
|
|
for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use)
|
|
|
|
if (!Set.count(dyn_cast<Instruction>(*Use)))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-17 02:07:34 +08:00
|
|
|
bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurrenceKind Kind,
|
|
|
|
Loop *TheLoop, bool HasFunNoNaNAttr,
|
|
|
|
RecurrenceDescriptor &RedDes) {
|
2015-04-20 12:38:33 +08:00
|
|
|
if (Phi->getNumIncomingValues() != 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Reduction variables are only found in the loop header block.
|
|
|
|
if (Phi->getParent() != TheLoop->getHeader())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Obtain the reduction start value from the value that comes from the loop
|
|
|
|
// preheader.
|
|
|
|
Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
|
|
|
|
|
|
|
|
// ExitInstruction is the single value which is used outside the loop.
|
|
|
|
// We only allow for a single reduction value to be used outside the loop.
|
|
|
|
// This includes users of the reduction, variables (which form a cycle
|
|
|
|
// which ends in the phi node).
|
|
|
|
Instruction *ExitInstruction = nullptr;
|
|
|
|
// Indicates that we found a reduction operation in our scan.
|
|
|
|
bool FoundReduxOp = false;
|
|
|
|
|
|
|
|
// We start with the PHI node and scan for all of the users of this
|
|
|
|
// instruction. All users must be instructions that can be used as reduction
|
|
|
|
// variables (such as ADD). We must have a single out-of-block user. The cycle
|
|
|
|
// must include the original PHI.
|
|
|
|
bool FoundStartPHI = false;
|
|
|
|
|
|
|
|
// To recognize min/max patterns formed by a icmp select sequence, we store
|
|
|
|
// the number of instruction we saw from the recognized min/max pattern,
|
|
|
|
// to make sure we only see exactly the two instructions.
|
|
|
|
unsigned NumCmpSelectPatternInst = 0;
|
2015-06-17 06:59:45 +08:00
|
|
|
InstDesc ReduxDesc(false, nullptr);
|
2015-04-20 12:38:33 +08:00
|
|
|
|
|
|
|
SmallPtrSet<Instruction *, 8> VisitedInsts;
|
|
|
|
SmallVector<Instruction *, 8> Worklist;
|
|
|
|
Worklist.push_back(Phi);
|
|
|
|
VisitedInsts.insert(Phi);
|
|
|
|
|
|
|
|
// A value in the reduction can be used:
|
|
|
|
// - By the reduction:
|
|
|
|
// - Reduction operation:
|
|
|
|
// - One use of reduction value (safe).
|
|
|
|
// - Multiple use of reduction value (not safe).
|
|
|
|
// - PHI:
|
|
|
|
// - All uses of the PHI must be the reduction (safe).
|
|
|
|
// - Otherwise, not safe.
|
|
|
|
// - By one instruction outside of the loop (safe).
|
|
|
|
// - By further instructions outside of the loop (not safe).
|
|
|
|
// - By an instruction that is not part of the reduction (not safe).
|
|
|
|
// This is either:
|
|
|
|
// * An instruction type other than PHI or the reduction operation.
|
|
|
|
// * A PHI in the header other than the initial PHI.
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
Instruction *Cur = Worklist.back();
|
|
|
|
Worklist.pop_back();
|
|
|
|
|
|
|
|
// No Users.
|
|
|
|
// If the instruction has no users then this is a broken chain and can't be
|
|
|
|
// a reduction variable.
|
|
|
|
if (Cur->use_empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool IsAPhi = isa<PHINode>(Cur);
|
|
|
|
|
|
|
|
// A header PHI use other than the original PHI.
|
|
|
|
if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Reductions of instructions such as Div, and Sub is only possible if the
|
|
|
|
// LHS is the reduction variable.
|
|
|
|
if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) &&
|
|
|
|
!isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
|
|
|
|
!VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0))))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Any reduction instruction must be of one of the allowed kinds.
|
2015-06-17 02:07:34 +08:00
|
|
|
ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr);
|
|
|
|
if (!ReduxDesc.isRecurrence())
|
2015-04-20 12:38:33 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// A reduction operation must only have one use of the reduction value.
|
|
|
|
if (!IsAPhi && Kind != RK_IntegerMinMax && Kind != RK_FloatMinMax &&
|
|
|
|
hasMultipleUsesOf(Cur, VisitedInsts))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// All inputs to a PHI node must be a reduction value.
|
|
|
|
if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Kind == RK_IntegerMinMax &&
|
|
|
|
(isa<ICmpInst>(Cur) || isa<SelectInst>(Cur)))
|
|
|
|
++NumCmpSelectPatternInst;
|
|
|
|
if (Kind == RK_FloatMinMax && (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur)))
|
|
|
|
++NumCmpSelectPatternInst;
|
|
|
|
|
|
|
|
// Check whether we found a reduction operator.
|
|
|
|
FoundReduxOp |= !IsAPhi;
|
|
|
|
|
|
|
|
// Process users of current instruction. Push non-PHI nodes after PHI nodes
|
|
|
|
// onto the stack. This way we are going to have seen all inputs to PHI
|
|
|
|
// nodes once we get to them.
|
|
|
|
SmallVector<Instruction *, 8> NonPHIs;
|
|
|
|
SmallVector<Instruction *, 8> PHIs;
|
|
|
|
for (User *U : Cur->users()) {
|
|
|
|
Instruction *UI = cast<Instruction>(U);
|
|
|
|
|
|
|
|
// Check if we found the exit user.
|
|
|
|
BasicBlock *Parent = UI->getParent();
|
|
|
|
if (!TheLoop->contains(Parent)) {
|
|
|
|
// Exit if you find multiple outside users or if the header phi node is
|
|
|
|
// being used. In this case the user uses the value of the previous
|
|
|
|
// iteration, in which case we would loose "VF-1" iterations of the
|
|
|
|
// reduction operation if we vectorize.
|
|
|
|
if (ExitInstruction != nullptr || Cur == Phi)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The instruction used by an outside user must be the last instruction
|
|
|
|
// before we feed back to the reduction phi. Otherwise, we loose VF-1
|
|
|
|
// operations on the value.
|
|
|
|
if (std::find(Phi->op_begin(), Phi->op_end(), Cur) == Phi->op_end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ExitInstruction = Cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process instructions only once (termination). Each reduction cycle
|
|
|
|
// value must only be used once, except by phi nodes and min/max
|
|
|
|
// reductions which are represented as a cmp followed by a select.
|
2015-06-17 06:59:45 +08:00
|
|
|
InstDesc IgnoredVal(false, nullptr);
|
2015-04-20 12:38:33 +08:00
|
|
|
if (VisitedInsts.insert(UI).second) {
|
|
|
|
if (isa<PHINode>(UI))
|
|
|
|
PHIs.push_back(UI);
|
|
|
|
else
|
|
|
|
NonPHIs.push_back(UI);
|
|
|
|
} else if (!isa<PHINode>(UI) &&
|
|
|
|
((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) &&
|
|
|
|
!isa<SelectInst>(UI)) ||
|
2015-06-17 02:07:34 +08:00
|
|
|
!isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence()))
|
2015-04-20 12:38:33 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Remember that we completed the cycle.
|
|
|
|
if (UI == Phi)
|
|
|
|
FoundStartPHI = true;
|
|
|
|
}
|
|
|
|
Worklist.append(PHIs.begin(), PHIs.end());
|
|
|
|
Worklist.append(NonPHIs.begin(), NonPHIs.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This means we have seen one but not the other instruction of the
|
|
|
|
// pattern or more than just a select and cmp.
|
|
|
|
if ((Kind == RK_IntegerMinMax || Kind == RK_FloatMinMax) &&
|
|
|
|
NumCmpSelectPatternInst != 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We found a reduction var if we have reached the original phi node and we
|
|
|
|
// only have a single instruction with out-of-loop users.
|
|
|
|
|
|
|
|
// The ExitInstruction(Instruction which is allowed to have out-of-loop users)
|
2015-06-17 02:07:34 +08:00
|
|
|
// is saved as part of the RecurrenceDescriptor.
|
2015-04-20 12:38:33 +08:00
|
|
|
|
|
|
|
// Save the description of this reduction variable.
|
2015-06-17 02:07:34 +08:00
|
|
|
RecurrenceDescriptor RD(RdxStart, ExitInstruction, Kind,
|
2015-08-11 03:51:46 +08:00
|
|
|
ReduxDesc.getMinMaxKind(),
|
|
|
|
ReduxDesc.getUnsafeAlgebraInst());
|
2015-04-20 12:38:33 +08:00
|
|
|
|
|
|
|
RedDes = RD;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the instruction is a Select(ICmp(X, Y), X, Y) instruction
|
|
|
|
/// pattern corresponding to a min(X, Y) or max(X, Y).
|
2015-06-17 06:59:45 +08:00
|
|
|
RecurrenceDescriptor::InstDesc
|
|
|
|
RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev) {
|
2015-04-20 12:38:33 +08:00
|
|
|
|
|
|
|
assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) &&
|
|
|
|
"Expect a select instruction");
|
|
|
|
Instruction *Cmp = nullptr;
|
|
|
|
SelectInst *Select = nullptr;
|
|
|
|
|
|
|
|
// We must handle the select(cmp()) as a single instruction. Advance to the
|
|
|
|
// select.
|
|
|
|
if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) {
|
|
|
|
if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin())))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(false, I);
|
|
|
|
return InstDesc(Select, Prev.getMinMaxKind());
|
2015-04-20 12:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only handle single use cases for now.
|
|
|
|
if (!(Select = dyn_cast<SelectInst>(I)))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(false, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) &&
|
|
|
|
!(Cmp = dyn_cast<FCmpInst>(I->getOperand(0))))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(false, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
if (!Cmp->hasOneUse())
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(false, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
|
|
|
|
Value *CmpLeft;
|
|
|
|
Value *CmpRight;
|
|
|
|
|
|
|
|
// Look for a min/max pattern.
|
|
|
|
if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Select, MRK_UIntMin);
|
2015-04-20 12:38:33 +08:00
|
|
|
else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Select, MRK_UIntMax);
|
2015-04-20 12:38:33 +08:00
|
|
|
else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Select, MRK_SIntMax);
|
2015-04-20 12:38:33 +08:00
|
|
|
else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Select, MRK_SIntMin);
|
2015-04-20 12:38:33 +08:00
|
|
|
else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Select, MRK_FloatMin);
|
2015-04-20 12:38:33 +08:00
|
|
|
else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Select, MRK_FloatMax);
|
2015-04-20 12:38:33 +08:00
|
|
|
else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Select, MRK_FloatMin);
|
2015-04-20 12:38:33 +08:00
|
|
|
else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Select, MRK_FloatMax);
|
2015-04-20 12:38:33 +08:00
|
|
|
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(false, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 06:59:45 +08:00
|
|
|
RecurrenceDescriptor::InstDesc
|
2015-06-17 02:07:34 +08:00
|
|
|
RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
|
2015-06-17 06:59:45 +08:00
|
|
|
InstDesc &Prev, bool HasFunNoNaNAttr) {
|
2015-04-20 12:38:33 +08:00
|
|
|
bool FP = I->getType()->isFloatingPointTy();
|
2015-08-11 03:51:46 +08:00
|
|
|
Instruction *UAI = Prev.getUnsafeAlgebraInst();
|
|
|
|
if (!UAI && FP && !I->hasUnsafeAlgebra())
|
|
|
|
UAI = I; // Found an unsafe (unvectorizable) algebra instruction.
|
|
|
|
|
2015-04-20 12:38:33 +08:00
|
|
|
switch (I->getOpcode()) {
|
|
|
|
default:
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(false, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::PHI:
|
|
|
|
if (FP &&
|
|
|
|
(Kind != RK_FloatMult && Kind != RK_FloatAdd && Kind != RK_FloatMinMax))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(false, I);
|
|
|
|
return InstDesc(I, Prev.getMinMaxKind());
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::Add:
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Kind == RK_IntegerAdd, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::Mul:
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Kind == RK_IntegerMult, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::And:
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Kind == RK_IntegerAnd, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::Or:
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Kind == RK_IntegerOr, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::Xor:
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(Kind == RK_IntegerXor, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::FMul:
|
2015-08-11 03:51:46 +08:00
|
|
|
return InstDesc(Kind == RK_FloatMult, I, UAI);
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::FSub:
|
|
|
|
case Instruction::FAdd:
|
2015-08-11 03:51:46 +08:00
|
|
|
return InstDesc(Kind == RK_FloatAdd, I, UAI);
|
2015-04-20 12:38:33 +08:00
|
|
|
case Instruction::FCmp:
|
|
|
|
case Instruction::ICmp:
|
|
|
|
case Instruction::Select:
|
|
|
|
if (Kind != RK_IntegerMinMax &&
|
|
|
|
(!HasFunNoNaNAttr || Kind != RK_FloatMinMax))
|
2015-06-17 06:59:45 +08:00
|
|
|
return InstDesc(false, I);
|
2015-04-20 12:38:33 +08:00
|
|
|
return isMinMaxSelectCmpPattern(I, Prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 02:07:34 +08:00
|
|
|
bool RecurrenceDescriptor::hasMultipleUsesOf(
|
2015-04-20 12:38:33 +08:00
|
|
|
Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) {
|
|
|
|
unsigned NumUses = 0;
|
|
|
|
for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E;
|
|
|
|
++Use) {
|
|
|
|
if (Insts.count(dyn_cast<Instruction>(*Use)))
|
|
|
|
++NumUses;
|
|
|
|
if (NumUses > 1)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-17 02:07:34 +08:00
|
|
|
bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
|
|
|
|
RecurrenceDescriptor &RedDes) {
|
2015-04-20 12:38:33 +08:00
|
|
|
|
|
|
|
bool HasFunNoNaNAttr = false;
|
|
|
|
BasicBlock *Header = TheLoop->getHeader();
|
|
|
|
Function &F = *Header->getParent();
|
|
|
|
if (F.hasFnAttribute("no-nans-fp-math"))
|
|
|
|
HasFunNoNaNAttr =
|
|
|
|
F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
|
|
|
|
|
|
|
|
if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr,
|
|
|
|
RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes)) {
|
|
|
|
DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi << "\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Not a reduction of known type.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function returns the identity element (or neutral element) for
|
|
|
|
/// the operation K.
|
2015-06-17 02:07:34 +08:00
|
|
|
Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K,
|
|
|
|
Type *Tp) {
|
2015-04-20 12:38:33 +08:00
|
|
|
switch (K) {
|
|
|
|
case RK_IntegerXor:
|
|
|
|
case RK_IntegerAdd:
|
|
|
|
case RK_IntegerOr:
|
|
|
|
// Adding, Xoring, Oring zero to a number does not change it.
|
|
|
|
return ConstantInt::get(Tp, 0);
|
|
|
|
case RK_IntegerMult:
|
|
|
|
// Multiplying a number by 1 does not change it.
|
|
|
|
return ConstantInt::get(Tp, 1);
|
|
|
|
case RK_IntegerAnd:
|
|
|
|
// AND-ing a number with an all-1 value does not change it.
|
|
|
|
return ConstantInt::get(Tp, -1, true);
|
|
|
|
case RK_FloatMult:
|
|
|
|
// Multiplying a number by 1 does not change it.
|
|
|
|
return ConstantFP::get(Tp, 1.0L);
|
|
|
|
case RK_FloatAdd:
|
|
|
|
// Adding zero to a number does not change it.
|
|
|
|
return ConstantFP::get(Tp, 0.0L);
|
|
|
|
default:
|
2015-06-17 02:07:34 +08:00
|
|
|
llvm_unreachable("Unknown recurrence kind");
|
2015-04-20 12:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 02:07:34 +08:00
|
|
|
/// This function translates the recurrence kind to an LLVM binary operator.
|
|
|
|
unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) {
|
2015-04-20 12:38:33 +08:00
|
|
|
switch (Kind) {
|
|
|
|
case RK_IntegerAdd:
|
|
|
|
return Instruction::Add;
|
|
|
|
case RK_IntegerMult:
|
|
|
|
return Instruction::Mul;
|
|
|
|
case RK_IntegerOr:
|
|
|
|
return Instruction::Or;
|
|
|
|
case RK_IntegerAnd:
|
|
|
|
return Instruction::And;
|
|
|
|
case RK_IntegerXor:
|
|
|
|
return Instruction::Xor;
|
|
|
|
case RK_FloatMult:
|
|
|
|
return Instruction::FMul;
|
|
|
|
case RK_FloatAdd:
|
|
|
|
return Instruction::FAdd;
|
|
|
|
case RK_IntegerMinMax:
|
|
|
|
return Instruction::ICmp;
|
|
|
|
case RK_FloatMinMax:
|
|
|
|
return Instruction::FCmp;
|
|
|
|
default:
|
2015-06-17 02:07:34 +08:00
|
|
|
llvm_unreachable("Unknown recurrence operation");
|
2015-04-20 12:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 06:59:45 +08:00
|
|
|
Value *RecurrenceDescriptor::createMinMaxOp(IRBuilder<> &Builder,
|
|
|
|
MinMaxRecurrenceKind RK,
|
|
|
|
Value *Left, Value *Right) {
|
2015-04-20 12:38:33 +08:00
|
|
|
CmpInst::Predicate P = CmpInst::ICMP_NE;
|
|
|
|
switch (RK) {
|
|
|
|
default:
|
2015-06-17 02:07:34 +08:00
|
|
|
llvm_unreachable("Unknown min/max recurrence kind");
|
2015-06-17 06:59:45 +08:00
|
|
|
case MRK_UIntMin:
|
2015-04-20 12:38:33 +08:00
|
|
|
P = CmpInst::ICMP_ULT;
|
|
|
|
break;
|
2015-06-17 06:59:45 +08:00
|
|
|
case MRK_UIntMax:
|
2015-04-20 12:38:33 +08:00
|
|
|
P = CmpInst::ICMP_UGT;
|
|
|
|
break;
|
2015-06-17 06:59:45 +08:00
|
|
|
case MRK_SIntMin:
|
2015-04-20 12:38:33 +08:00
|
|
|
P = CmpInst::ICMP_SLT;
|
|
|
|
break;
|
2015-06-17 06:59:45 +08:00
|
|
|
case MRK_SIntMax:
|
2015-04-20 12:38:33 +08:00
|
|
|
P = CmpInst::ICMP_SGT;
|
|
|
|
break;
|
2015-06-17 06:59:45 +08:00
|
|
|
case MRK_FloatMin:
|
2015-04-20 12:38:33 +08:00
|
|
|
P = CmpInst::FCMP_OLT;
|
|
|
|
break;
|
2015-06-17 06:59:45 +08:00
|
|
|
case MRK_FloatMax:
|
2015-04-20 12:38:33 +08:00
|
|
|
P = CmpInst::FCMP_OGT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *Cmp;
|
2015-06-17 06:59:45 +08:00
|
|
|
if (RK == MRK_FloatMin || RK == MRK_FloatMax)
|
2015-04-20 12:38:33 +08:00
|
|
|
Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
|
|
|
|
else
|
|
|
|
Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
|
|
|
|
|
|
|
|
Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
|
|
|
|
return Select;
|
|
|
|
}
|
2015-04-23 16:29:20 +08:00
|
|
|
|
|
|
|
bool llvm::isInductionPHI(PHINode *Phi, ScalarEvolution *SE,
|
|
|
|
ConstantInt *&StepValue) {
|
|
|
|
Type *PhiTy = Phi->getType();
|
|
|
|
// We only handle integer and pointer inductions variables.
|
|
|
|
if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the PHI is consecutive.
|
|
|
|
const SCEV *PhiScev = SE->getSCEV(Phi);
|
|
|
|
const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
|
|
|
|
if (!AR) {
|
|
|
|
DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *Step = AR->getStepRecurrence(*SE);
|
|
|
|
// Calculate the pointer stride and check if it is consecutive.
|
|
|
|
const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
|
|
|
|
if (!C)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ConstantInt *CV = C->getValue();
|
|
|
|
if (PhiTy->isIntegerTy()) {
|
|
|
|
StepValue = CV;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
|
|
|
|
Type *PointerElementType = PhiTy->getPointerElementType();
|
|
|
|
// The pointer stride cannot be determined if the pointer element type is not
|
|
|
|
// sized.
|
|
|
|
if (!PointerElementType->isSized())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const DataLayout &DL = Phi->getModule()->getDataLayout();
|
|
|
|
int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType));
|
2015-06-05 18:52:40 +08:00
|
|
|
if (!Size)
|
|
|
|
return false;
|
|
|
|
|
2015-04-23 16:29:20 +08:00
|
|
|
int64_t CVSize = CV->getSExtValue();
|
|
|
|
if (CVSize % Size)
|
|
|
|
return false;
|
|
|
|
StepValue = ConstantInt::getSigned(CV->getType(), CVSize / Size);
|
|
|
|
return true;
|
|
|
|
}
|