Rename Reduction variables/structures to Recurrence.

A reduction is a special kind of recurrence. In the loop vectorizer we currently
identify basic reductions. Future patches will extend this to identifying basic
recurrences.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239835 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Tyler Nowicki
2015-06-16 18:07:34 +00:00
parent 37c67e6bb6
commit d4364d8c12
4 changed files with 150 additions and 137 deletions

View File

@@ -26,17 +26,17 @@ using namespace llvm::PatternMatch;
#define DEBUG_TYPE "loop-utils"
bool ReductionDescriptor::areAllUsesIn(Instruction *I,
SmallPtrSetImpl<Instruction *> &Set) {
bool RecurrenceDescriptor::areAllUsesIn(Instruction *I,
SmallPtrSetImpl<Instruction *> &Set) {
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;
}
bool ReductionDescriptor::AddReductionVar(PHINode *Phi, ReductionKind Kind,
Loop *TheLoop, bool HasFunNoNaNAttr,
ReductionDescriptor &RedDes) {
bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurrenceKind Kind,
Loop *TheLoop, bool HasFunNoNaNAttr,
RecurrenceDescriptor &RedDes) {
if (Phi->getNumIncomingValues() != 2)
return false;
@@ -66,7 +66,7 @@ bool ReductionDescriptor::AddReductionVar(PHINode *Phi, ReductionKind Kind,
// 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;
ReductionInstDesc ReduxDesc(false, nullptr);
RecurrenceInstDesc ReduxDesc(false, nullptr);
SmallPtrSet<Instruction *, 8> VisitedInsts;
SmallVector<Instruction *, 8> Worklist;
@@ -111,8 +111,8 @@ bool ReductionDescriptor::AddReductionVar(PHINode *Phi, ReductionKind Kind,
return false;
// Any reduction instruction must be of one of the allowed kinds.
ReduxDesc = isReductionInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr);
if (!ReduxDesc.isReduction())
ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr);
if (!ReduxDesc.isRecurrence())
return false;
// A reduction operation must only have one use of the reduction value.
@@ -164,7 +164,7 @@ bool ReductionDescriptor::AddReductionVar(PHINode *Phi, ReductionKind Kind,
// 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.
ReductionInstDesc IgnoredVal(false, nullptr);
RecurrenceInstDesc IgnoredVal(false, nullptr);
if (VisitedInsts.insert(UI).second) {
if (isa<PHINode>(UI))
PHIs.push_back(UI);
@@ -173,7 +173,7 @@ bool ReductionDescriptor::AddReductionVar(PHINode *Phi, ReductionKind Kind,
} else if (!isa<PHINode>(UI) &&
((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) &&
!isa<SelectInst>(UI)) ||
!isMinMaxSelectCmpPattern(UI, IgnoredVal).isReduction()))
!isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence()))
return false;
// Remember that we completed the cycle.
@@ -197,11 +197,11 @@ bool ReductionDescriptor::AddReductionVar(PHINode *Phi, ReductionKind Kind,
// only have a single instruction with out-of-loop users.
// The ExitInstruction(Instruction which is allowed to have out-of-loop users)
// is saved as part of the ReductionDescriptor.
// is saved as part of the RecurrenceDescriptor.
// Save the description of this reduction variable.
ReductionDescriptor RD(RdxStart, ExitInstruction, Kind,
ReduxDesc.getMinMaxKind());
RecurrenceDescriptor RD(RdxStart, ExitInstruction, Kind,
ReduxDesc.getMinMaxKind());
RedDes = RD;
@@ -210,9 +210,9 @@ bool ReductionDescriptor::AddReductionVar(PHINode *Phi, ReductionKind Kind,
/// 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).
ReductionInstDesc
ReductionDescriptor::isMinMaxSelectCmpPattern(Instruction *I,
ReductionInstDesc &Prev) {
RecurrenceInstDesc
RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I,
RecurrenceInstDesc &Prev) {
assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) &&
"Expect a select instruction");
@@ -223,84 +223,84 @@ ReductionDescriptor::isMinMaxSelectCmpPattern(Instruction *I,
// select.
if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) {
if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin())))
return ReductionInstDesc(false, I);
return ReductionInstDesc(Select, Prev.getMinMaxKind());
return RecurrenceInstDesc(false, I);
return RecurrenceInstDesc(Select, Prev.getMinMaxKind());
}
// Only handle single use cases for now.
if (!(Select = dyn_cast<SelectInst>(I)))
return ReductionInstDesc(false, I);
return RecurrenceInstDesc(false, I);
if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) &&
!(Cmp = dyn_cast<FCmpInst>(I->getOperand(0))))
return ReductionInstDesc(false, I);
return RecurrenceInstDesc(false, I);
if (!Cmp->hasOneUse())
return ReductionInstDesc(false, I);
return RecurrenceInstDesc(false, I);
Value *CmpLeft;
Value *CmpRight;
// Look for a min/max pattern.
if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
return ReductionInstDesc(Select, ReductionInstDesc::MRK_UIntMin);
return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_UIntMin);
else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
return ReductionInstDesc(Select, ReductionInstDesc::MRK_UIntMax);
return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_UIntMax);
else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
return ReductionInstDesc(Select, ReductionInstDesc::MRK_SIntMax);
return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_SIntMax);
else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
return ReductionInstDesc(Select, ReductionInstDesc::MRK_SIntMin);
return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_SIntMin);
else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
return ReductionInstDesc(Select, ReductionInstDesc::MRK_FloatMin);
return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_FloatMin);
else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
return ReductionInstDesc(Select, ReductionInstDesc::MRK_FloatMax);
return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_FloatMax);
else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
return ReductionInstDesc(Select, ReductionInstDesc::MRK_FloatMin);
return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_FloatMin);
else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
return ReductionInstDesc(Select, ReductionInstDesc::MRK_FloatMax);
return RecurrenceInstDesc(Select, RecurrenceInstDesc::MRK_FloatMax);
return ReductionInstDesc(false, I);
return RecurrenceInstDesc(false, I);
}
ReductionInstDesc ReductionDescriptor::isReductionInstr(Instruction *I,
ReductionKind Kind,
ReductionInstDesc &Prev,
bool HasFunNoNaNAttr) {
RecurrenceInstDesc
RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
RecurrenceInstDesc &Prev,
bool HasFunNoNaNAttr) {
bool FP = I->getType()->isFloatingPointTy();
bool FastMath = FP && I->hasUnsafeAlgebra();
switch (I->getOpcode()) {
default:
return ReductionInstDesc(false, I);
return RecurrenceInstDesc(false, I);
case Instruction::PHI:
if (FP &&
(Kind != RK_FloatMult && Kind != RK_FloatAdd && Kind != RK_FloatMinMax))
return ReductionInstDesc(false, I);
return ReductionInstDesc(I, Prev.getMinMaxKind());
return RecurrenceInstDesc(false, I);
return RecurrenceInstDesc(I, Prev.getMinMaxKind());
case Instruction::Sub:
case Instruction::Add:
return ReductionInstDesc(Kind == RK_IntegerAdd, I);
return RecurrenceInstDesc(Kind == RK_IntegerAdd, I);
case Instruction::Mul:
return ReductionInstDesc(Kind == RK_IntegerMult, I);
return RecurrenceInstDesc(Kind == RK_IntegerMult, I);
case Instruction::And:
return ReductionInstDesc(Kind == RK_IntegerAnd, I);
return RecurrenceInstDesc(Kind == RK_IntegerAnd, I);
case Instruction::Or:
return ReductionInstDesc(Kind == RK_IntegerOr, I);
return RecurrenceInstDesc(Kind == RK_IntegerOr, I);
case Instruction::Xor:
return ReductionInstDesc(Kind == RK_IntegerXor, I);
return RecurrenceInstDesc(Kind == RK_IntegerXor, I);
case Instruction::FMul:
return ReductionInstDesc(Kind == RK_FloatMult && FastMath, I);
return RecurrenceInstDesc(Kind == RK_FloatMult && FastMath, I);
case Instruction::FSub:
case Instruction::FAdd:
return ReductionInstDesc(Kind == RK_FloatAdd && FastMath, I);
return RecurrenceInstDesc(Kind == RK_FloatAdd && FastMath, I);
case Instruction::FCmp:
case Instruction::ICmp:
case Instruction::Select:
if (Kind != RK_IntegerMinMax &&
(!HasFunNoNaNAttr || Kind != RK_FloatMinMax))
return ReductionInstDesc(false, I);
return RecurrenceInstDesc(false, I);
return isMinMaxSelectCmpPattern(I, Prev);
}
}
bool ReductionDescriptor::hasMultipleUsesOf(
bool RecurrenceDescriptor::hasMultipleUsesOf(
Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) {
unsigned NumUses = 0;
for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E;
@@ -313,8 +313,8 @@ bool ReductionDescriptor::hasMultipleUsesOf(
return false;
}
bool ReductionDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
ReductionDescriptor &RedDes) {
bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
RecurrenceDescriptor &RedDes) {
bool HasFunNoNaNAttr = false;
BasicBlock *Header = TheLoop->getHeader();
@@ -366,7 +366,8 @@ bool ReductionDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
/// This function returns the identity element (or neutral element) for
/// the operation K.
Constant *ReductionDescriptor::getReductionIdentity(ReductionKind K, Type *Tp) {
Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K,
Type *Tp) {
switch (K) {
case RK_IntegerXor:
case RK_IntegerAdd:
@@ -386,12 +387,12 @@ Constant *ReductionDescriptor::getReductionIdentity(ReductionKind K, Type *Tp) {
// Adding zero to a number does not change it.
return ConstantFP::get(Tp, 0.0L);
default:
llvm_unreachable("Unknown reduction kind");
llvm_unreachable("Unknown recurrence kind");
}
}
/// This function translates the reduction kind to an LLVM binary operator.
unsigned ReductionDescriptor::getReductionBinOp(ReductionKind Kind) {
/// This function translates the recurrence kind to an LLVM binary operator.
unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) {
switch (Kind) {
case RK_IntegerAdd:
return Instruction::Add;
@@ -412,41 +413,40 @@ unsigned ReductionDescriptor::getReductionBinOp(ReductionKind Kind) {
case RK_FloatMinMax:
return Instruction::FCmp;
default:
llvm_unreachable("Unknown reduction operation");
llvm_unreachable("Unknown recurrence operation");
}
}
Value *
ReductionDescriptor::createMinMaxOp(IRBuilder<> &Builder,
ReductionInstDesc::MinMaxReductionKind RK,
Value *Left, Value *Right) {
Value *RecurrenceDescriptor::createMinMaxOp(
IRBuilder<> &Builder, RecurrenceInstDesc::MinMaxRecurrenceKind RK,
Value *Left, Value *Right) {
CmpInst::Predicate P = CmpInst::ICMP_NE;
switch (RK) {
default:
llvm_unreachable("Unknown min/max reduction kind");
case ReductionInstDesc::MRK_UIntMin:
llvm_unreachable("Unknown min/max recurrence kind");
case RecurrenceInstDesc::MRK_UIntMin:
P = CmpInst::ICMP_ULT;
break;
case ReductionInstDesc::MRK_UIntMax:
case RecurrenceInstDesc::MRK_UIntMax:
P = CmpInst::ICMP_UGT;
break;
case ReductionInstDesc::MRK_SIntMin:
case RecurrenceInstDesc::MRK_SIntMin:
P = CmpInst::ICMP_SLT;
break;
case ReductionInstDesc::MRK_SIntMax:
case RecurrenceInstDesc::MRK_SIntMax:
P = CmpInst::ICMP_SGT;
break;
case ReductionInstDesc::MRK_FloatMin:
case RecurrenceInstDesc::MRK_FloatMin:
P = CmpInst::FCMP_OLT;
break;
case ReductionInstDesc::MRK_FloatMax:
case RecurrenceInstDesc::MRK_FloatMax:
P = CmpInst::FCMP_OGT;
break;
}
Value *Cmp;
if (RK == ReductionInstDesc::MRK_FloatMin ||
RK == ReductionInstDesc::MRK_FloatMax)
if (RK == RecurrenceInstDesc::MRK_FloatMin ||
RK == RecurrenceInstDesc::MRK_FloatMax)
Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
else
Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");