X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FAnalysis%2FCostModel.cpp;h=1b74f8c19c51636ab651be39ecf6021a59bda80c;hb=0cccab7515080b5a9b3efd23ee19343e05fa4453;hp=b49211d486f1ae011f2dfb1652069b0913dfe2b1;hpb=c37e6c073436be77e90ef640c0e6627200ba19f7;p=oota-llvm.git diff --git a/lib/Analysis/CostModel.cpp b/lib/Analysis/CostModel.cpp index b49211d486f..1b74f8c19c5 100644 --- a/lib/Analysis/CostModel.cpp +++ b/lib/Analysis/CostModel.cpp @@ -17,8 +17,6 @@ // //===----------------------------------------------------------------------===// -#define CM_NAME "cost-model" -#define DEBUG_TYPE CM_NAME #include "llvm/ADT/STLExtras.h" #include "llvm/Analysis/Passes.h" #include "llvm/Analysis/TargetTransformInfo.h" @@ -32,6 +30,9 @@ #include "llvm/Support/raw_ostream.h" using namespace llvm; +#define CM_NAME "cost-model" +#define DEBUG_TYPE CM_NAME + static cl::opt EnableReduxCost("costmodel-reduxcost", cl::init(false), cl::Hidden, cl::desc("Recognize reduction patterns.")); @@ -41,7 +42,7 @@ namespace { public: static char ID; // Class identification, replacement for typeinfo - CostModelAnalysis() : FunctionPass(ID), F(0), TTI(0) { + CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) { initializeCostModelAnalysisPass( *PassRegistry::getPassRegistry()); } @@ -94,6 +95,31 @@ static bool isReverseVectorMask(SmallVectorImpl &Mask) { return true; } +static bool isAlternateVectorMask(SmallVectorImpl &Mask) { + bool isAlternate = true; + unsigned MaskSize = Mask.size(); + + // Example: shufflevector A, B, <0,5,2,7> + for (unsigned i = 0; i < MaskSize && isAlternate; ++i) { + if (Mask[i] < 0) + continue; + isAlternate = Mask[i] == (int)((i & 1) ? MaskSize + i : i); + } + + if (isAlternate) + return true; + + isAlternate = true; + // Example: shufflevector A, B, <4,1,6,3> + for (unsigned i = 0; i < MaskSize && isAlternate; ++i) { + if (Mask[i] < 0) + continue; + isAlternate = Mask[i] == (int)((i & 1) ? i : MaskSize + i); + } + + return isAlternate; +} + static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) { TargetTransformInfo::OperandValueKind OpInfo = TargetTransformInfo::OK_AnyValue; @@ -101,24 +127,13 @@ static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) { // Check for a splat of a constant or for a non uniform vector of constants. if (isa(V) || isa(V)) { OpInfo = TargetTransformInfo::OK_NonUniformConstantValue; - if (cast(V)->getSplatValue() != NULL) + if (cast(V)->getSplatValue() != nullptr) OpInfo = TargetTransformInfo::OK_UniformConstantValue; } return OpInfo; } -static bool matchMask(SmallVectorImpl &M1, SmallVectorImpl &M2) { - if (M1.size() != M2.size()) - return false; - - for (unsigned i = 0, e = M1.size(); i != e; ++i) - if (M1[i] != M2[i]) - return false; - - return true; -} - static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft, unsigned Level) { // We don't need a shuffle if we just want to have element 0 in position 0 of @@ -136,7 +151,7 @@ static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft, Mask[i] = val; SmallVector ActualMask = SI->getShuffleMask(); - if (!matchMask(Mask, ActualMask)) + if (Mask != ActualMask) return false; return true; @@ -150,7 +165,7 @@ static bool matchPairwiseReductionAtLevel(const BinaryOperator *BinOp, // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef, // <4 x i32> // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1 - if (BinOp == 0) + if (BinOp == nullptr) return false; assert(BinOp->getType()->isVectorTy() && "Expecting a vector type"); @@ -171,9 +186,9 @@ static bool matchPairwiseReductionAtLevel(const BinaryOperator *BinOp, return false; // Shuffle inputs must match. - Value *NextLevelOpL = LS ? LS->getOperand(0) : 0; - Value *NextLevelOpR = RS ? RS->getOperand(0) : 0; - Value *NextLevelOp = 0; + Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr; + Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr; + Value *NextLevelOp = nullptr; if (NextLevelOpR && NextLevelOpL) { // If we have two shuffles their operands must match. if (NextLevelOpL != NextLevelOpR) @@ -198,7 +213,7 @@ static bool matchPairwiseReductionAtLevel(const BinaryOperator *BinOp, // Check that the next levels binary operation exists and matches with the // current one. - BinaryOperator *NextLevelBinOp = 0; + BinaryOperator *NextLevelBinOp = nullptr; if (Level + 1 != NumLevels) { if (!(NextLevelBinOp = dyn_cast(NextLevelOp))) return false; @@ -277,7 +292,7 @@ getShuffleAndOtherOprd(BinaryOperator *B) { Value *L = B->getOperand(0); Value *R = B->getOperand(1); - ShuffleVectorInst *S = 0; + ShuffleVectorInst *S = nullptr; if ((S = dyn_cast(L))) return std::make_pair(R, S); @@ -337,7 +352,7 @@ static bool matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot, std::tie(NextRdxOp, Shuffle) = getShuffleAndOtherOprd(BinOp); // Check the current reduction operation and the shuffle use the same value. - if (Shuffle == 0) + if (Shuffle == nullptr) return false; if (Shuffle->getOperand(0) != NextRdxOp) return false; @@ -349,7 +364,7 @@ static bool matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot, std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1); SmallVector Mask = Shuffle->getShuffleMask(); - if (!matchMask(ShuffleMask, Mask)) + if (ShuffleMask != Mask) return false; RdxOp = NextRdxOp; @@ -476,9 +491,15 @@ unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const { unsigned NumVecElems = VecTypOp0->getVectorNumElements(); SmallVector Mask = Shuffle->getShuffleMask(); - if (NumVecElems == Mask.size() && isReverseVectorMask(Mask)) - return TTI->getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0, 0, - 0); + if (NumVecElems == Mask.size()) { + if (isReverseVectorMask(Mask)) + return TTI->getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0, + 0, nullptr); + if (isAlternateVectorMask(Mask)) + return TTI->getShuffleCost(TargetTransformInfo::SK_Alternate, + VecTypOp0, 0, nullptr); + } + return -1; } case Instruction::Call: