20 #define CM_NAME "cost-model"
21 #define DEBUG_TYPE CM_NAME
37 cl::desc(
"Recognize reduction patterns."));
69 static const char cm_name[] =
"Cost Model Analysis";
74 return new CostModelAnalysis();
78 CostModelAnalysis::getAnalysisUsage(
AnalysisUsage &AU)
const {
83 CostModelAnalysis::runOnFunction(
Function &
F) {
85 TTI = getAnalysisIfAvailable<TargetTransformInfo>();
91 for (
unsigned i = 0, MaskSize = Mask.
size(); i < MaskSize; ++i)
92 if (Mask[i] > 0 && Mask[i] != (
int)(MaskSize - 1 - i))
103 if ((CDV = dyn_cast<ConstantDataVector>(V)))
107 if ((CV = dyn_cast<ConstantVector>(V)))
118 for (
unsigned i = 0, e = M1.
size(); i != e; ++i)
129 if (!SI && Level == 0 && IsLeft)
138 for (
unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
149 unsigned Level,
unsigned NumLevels) {
173 if (!Level && !RS && !LS)
179 Value *NextLevelOp = 0;
180 if (NextLevelOpR && NextLevelOpL) {
182 if (NextLevelOpL != NextLevelOpR)
185 NextLevelOp = NextLevelOpL;
186 }
else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
193 if (NextLevelOpL && NextLevelOpL != R)
195 else if (NextLevelOpR && NextLevelOpR != L)
198 NextLevelOp = NextLevelOpL ? R : L;
205 if (Level + 1 != NumLevels) {
206 if (!(NextLevelBinOp = dyn_cast<BinaryOperator>(NextLevelOp)))
208 else if (NextLevelBinOp->
getOpcode() != Opcode)
222 if (++Level == NumLevels)
230 unsigned &Opcode,
Type *&Ty) {
278 static std::pair<Value *, ShuffleVectorInst *>
285 if ((S = dyn_cast<ShuffleVectorInst>(L)))
286 return std::make_pair(R, S);
289 return std::make_pair(L, S);
293 unsigned &Opcode,
Type *&Ty) {
308 unsigned RdxOpcode = RdxStart->
getOpcode();
326 unsigned MaskStart = 1;
327 Value *RdxOp = RdxStart;
329 unsigned NumVecElemsRemain = NumVecElems;
330 while (NumVecElemsRemain - 1) {
333 if (!(BinOp = dyn_cast<BinaryOperator>(RdxOp)))
349 for (
unsigned j = 0; j != MaskStart; ++j)
350 ShuffleMask[j] = MaskStart + j;
352 std::fill(&ShuffleMask[MaskStart], ShuffleMask.
end(), -1);
359 NumVecElemsRemain /= 2;
368 unsigned CostModelAnalysis::getInstructionCost(
const Instruction *
I)
const {
373 case Instruction::GetElementPtr:{
375 return TTI->getAddressComputationCost(ValTy);
380 case Instruction::Br: {
381 return TTI->getCFInstrCost(I->
getOpcode());
383 case Instruction::Add:
384 case Instruction::FAdd:
385 case Instruction::Sub:
386 case Instruction::FSub:
387 case Instruction::Mul:
388 case Instruction::FMul:
389 case Instruction::UDiv:
390 case Instruction::SDiv:
391 case Instruction::FDiv:
392 case Instruction::URem:
393 case Instruction::SRem:
394 case Instruction::FRem:
395 case Instruction::Shl:
396 case Instruction::LShr:
397 case Instruction::AShr:
413 case Instruction::ICmp:
414 case Instruction::FCmp: {
416 return TTI->getCmpSelInstrCost(I->
getOpcode(), ValTy);
421 return TTI->getMemoryOpCost(I->
getOpcode(), ValTy,
431 case Instruction::ZExt:
432 case Instruction::SExt:
433 case Instruction::FPToUI:
434 case Instruction::FPToSI:
435 case Instruction::FPExt:
436 case Instruction::PtrToInt:
437 case Instruction::IntToPtr:
438 case Instruction::SIToFP:
439 case Instruction::UIToFP:
440 case Instruction::Trunc:
441 case Instruction::FPTrunc:
442 case Instruction::BitCast: {
455 unsigned ReduxOpCode;
459 return TTI->getReductionCost(ReduxOpCode, ReduxType,
false);
461 return TTI->getReductionCost(ReduxOpCode, ReduxType,
true);
463 return TTI->getVectorInstrCost(I->
getOpcode(),
466 case Instruction::InsertElement: {
472 return TTI->getVectorInstrCost(I->
getOpcode(),
475 case Instruction::ShuffleVector: {
489 for (
unsigned J = 0, JE = II->getNumArgOperands(); J != JE; ++J)
490 Tys.
push_back(II->getArgOperand(J)->getType());
492 return TTI->getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
509 unsigned Cost = getInstructionCost(Inst);
510 if (Cost != (
unsigned)-1)
511 OS <<
"Cost Model: Found an estimated cost of " << Cost;
513 OS <<
"Cost Model: Unknown cost";
515 OS <<
" for instruction: "<< *Inst <<
"\n";
Value * getValueOperand()
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
The main container class for the LLVM Intermediate Representation.
void initializeCostModelAnalysisPass(PassRegistry &)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Constant * getSplatValue() const
static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V)
LoopInfoBase< BlockT, LoopT > * LI
Type * getPointerElementType() const
static const char cm_name[]
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
static bool matchPairwiseReduction(const ExtractElementInst *ReduxRoot, unsigned &Opcode, Type *&Ty)
static ConstantInt * ExtractElement(Constant *V, Constant *Idx)
ID
LLVM Calling Convention Representation.
uint64_t getZExtValue() const
Return the zero extended value.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
VectorType * getType() const
initializer< Ty > init(const Ty &Val)
unsigned getAlignment() const
static bool isReverseVectorMask(SmallVectorImpl< int > &Mask)
const Value * getCondition() const
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Value * getOperand(unsigned i) const
static bool matchMask(SmallVectorImpl< int > &M1, SmallVectorImpl< int > &M2)
static cl::opt< bool > EnableReduxCost("costmodel-reduxcost", cl::init(false), cl::Hidden, cl::desc("Recognize reduction patterns."))
BinaryOps getOpcode() const
static std::pair< Value *, ShuffleVectorInst * > getShuffleAndOtherOprd(BinaryOperator *B)
Class for constant integers.
unsigned getVectorNumElements() const
static bool matchPairwiseReductionAtLevel(const BinaryOperator *BinOp, unsigned Level, unsigned NumLevels)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
unsigned Log2_32(uint32_t Value)
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
unsigned getAlignment() const
static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft, unsigned Level)
VectorType * getType() const
FunctionPass * createCostModelAnalysisPass()
LLVM Value Representation.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
static bool matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot, unsigned &Opcode, Type *&Ty)
bool isPowerOf2_32(uint32_t Value)
static void getShuffleMask(Constant *Mask, SmallVectorImpl< int > &Result)
tier< T1, T2 > tie(T1 &f, T2 &s)
Constant * getSplatValue() const