33 #define CM_NAME "cost-model"
34 #define DEBUG_TYPE CM_NAME
38 cl::desc(
"Recognize reduction patterns."));
70 static const char cm_name[] =
"Cost Model Analysis";
75 return new CostModelAnalysis();
79 CostModelAnalysis::getAnalysisUsage(
AnalysisUsage &AU)
const {
84 CostModelAnalysis::runOnFunction(
Function &
F) {
86 auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
87 TTI = TTIWP ? &TTIWP->getTTI(F) :
nullptr;
93 for (
unsigned i = 0, MaskSize = Mask.
size(); i < MaskSize; ++i)
94 if (Mask[i] > 0 && Mask[i] != (
int)(MaskSize - 1 - i))
100 bool isAlternate =
true;
101 unsigned MaskSize = Mask.
size();
104 for (
unsigned i = 0; i < MaskSize && isAlternate; ++i) {
107 isAlternate = Mask[i] == (
int)((i & 1) ? MaskSize + i : i);
115 for (
unsigned i = 0; i < MaskSize && isAlternate; ++i) {
118 isAlternate = Mask[i] == (
int)((i & 1) ? i : MaskSize + i);
129 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
131 if (cast<Constant>(V)->getSplatValue() !=
nullptr)
142 if (!SI && Level == 0 && IsLeft)
151 for (
unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
155 if (Mask != ActualMask)
162 unsigned Level,
unsigned NumLevels) {
169 if (BinOp ==
nullptr)
186 if (!Level && !RS && !LS)
192 Value *NextLevelOp =
nullptr;
193 if (NextLevelOpR && NextLevelOpL) {
195 if (NextLevelOpL != NextLevelOpR)
198 NextLevelOp = NextLevelOpL;
199 }
else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
206 if (NextLevelOpL && NextLevelOpL != R)
208 else if (NextLevelOpR && NextLevelOpR != L)
211 NextLevelOp = NextLevelOpL ? R : L;
218 if (Level + 1 != NumLevels) {
219 if (!(NextLevelBinOp = dyn_cast<BinaryOperator>(NextLevelOp)))
221 else if (NextLevelBinOp->
getOpcode() != Opcode)
235 if (++Level == NumLevels)
243 unsigned &Opcode,
Type *&Ty) {
291 static std::pair<Value *, ShuffleVectorInst *>
298 if ((S = dyn_cast<ShuffleVectorInst>(L)))
299 return std::make_pair(R, S);
302 return std::make_pair(L, S);
306 unsigned &Opcode,
Type *&Ty) {
321 unsigned RdxOpcode = RdxStart->
getOpcode();
339 unsigned MaskStart = 1;
340 Value *RdxOp = RdxStart;
342 unsigned NumVecElemsRemain = NumVecElems;
343 while (NumVecElemsRemain - 1) {
346 if (!(BinOp = dyn_cast<BinaryOperator>(RdxOp)))
356 if (Shuffle ==
nullptr)
362 for (
unsigned j = 0; j != MaskStart; ++j)
363 ShuffleMask[j] = MaskStart + j;
365 std::fill(&ShuffleMask[MaskStart], ShuffleMask.
end(), -1);
368 if (ShuffleMask != Mask)
372 NumVecElemsRemain /= 2;
381 unsigned CostModelAnalysis::getInstructionCost(
const Instruction *
I)
const {
386 case Instruction::GetElementPtr:{
388 return TTI->getAddressComputationCost(ValTy);
393 case Instruction::Br: {
394 return TTI->getCFInstrCost(I->
getOpcode());
396 case Instruction::Add:
397 case Instruction::FAdd:
398 case Instruction::Sub:
399 case Instruction::FSub:
400 case Instruction::Mul:
401 case Instruction::FMul:
402 case Instruction::UDiv:
403 case Instruction::SDiv:
404 case Instruction::FDiv:
405 case Instruction::URem:
406 case Instruction::SRem:
407 case Instruction::FRem:
408 case Instruction::Shl:
409 case Instruction::LShr:
410 case Instruction::AShr:
426 case Instruction::ICmp:
427 case Instruction::FCmp: {
429 return TTI->getCmpSelInstrCost(I->
getOpcode(), ValTy);
434 return TTI->getMemoryOpCost(I->
getOpcode(), ValTy,
444 case Instruction::ZExt:
445 case Instruction::SExt:
446 case Instruction::FPToUI:
447 case Instruction::FPToSI:
448 case Instruction::FPExt:
449 case Instruction::PtrToInt:
450 case Instruction::IntToPtr:
451 case Instruction::SIToFP:
452 case Instruction::UIToFP:
453 case Instruction::Trunc:
454 case Instruction::FPTrunc:
455 case Instruction::BitCast:
456 case Instruction::AddrSpaceCast: {
469 unsigned ReduxOpCode;
473 return TTI->getReductionCost(ReduxOpCode, ReduxType,
false);
475 return TTI->getReductionCost(ReduxOpCode, ReduxType,
true);
477 return TTI->getVectorInstrCost(I->
getOpcode(),
480 case Instruction::InsertElement: {
486 return TTI->getVectorInstrCost(I->
getOpcode(),
489 case Instruction::ShuffleVector: {
495 if (NumVecElems == Mask.
size()) {
501 VecTypOp0, 0,
nullptr);
509 for (
unsigned J = 0, JE = II->getNumArgOperands(); J != JE; ++J)
510 Tys.
push_back(II->getArgOperand(J)->getType());
512 return TTI->getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
529 unsigned Cost = getInstructionCost(Inst);
530 if (Cost != (
unsigned)-1)
531 OS <<
"Cost Model: Found an estimated cost of " << Cost;
533 OS <<
"Cost Model: Unknown cost";
535 OS <<
" for instruction: "<< *Inst <<
"\n";
Value * getValueOperand()
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
void initializeCostModelAnalysisPass(PassRegistry &)
ShuffleVectorInst - This instruction constructs a fixed permutation of two input vectors.
LoadInst - an instruction for reading from memory.
static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V)
Type * getPointerElementType() const
static const char cm_name[]
SelectInst - This class represents the LLVM 'select' instruction.
#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)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
VectorType * getType() const
getType - Overload to return most specific vector type.
StoreInst - an instruction for storing to memory.
initializer< Ty > init(const Ty &Val)
InsertElementInst - This instruction inserts a single (scalar) element into a VectorType value...
unsigned getAlignment() const
getAlignment - Return the alignment of the access that is being performed
The instances of the Type class are immutable: once they are created, they are never changed...
bool isVectorTy() const
isVectorTy - True if this is an instance of VectorType.
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.
static bool isAlternateVectorMask(SmallVectorImpl< int > &Mask)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
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)
This is the shared class of boolean and integer constants.
unsigned getVectorNumElements() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
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)
Log2_32 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
unsigned getAlignment() const
getAlignment - Return the alignment of the access that is being performed
static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft, unsigned Level)
VectorType * getType() const
getType - Overload to return most specific vector type.
FunctionPass * createCostModelAnalysisPass()
LLVM Value Representation.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
This class implements an extremely fast bulk output stream that can only output to a stream...
static bool matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot, unsigned &Opcode, Type *&Ty)
bool isPowerOf2_32(uint32_t Value)
isPowerOf2_32 - This function returns true if the argument is a power of two > 0. ...
static void getShuffleMask(Constant *Mask, SmallVectorImpl< int > &Result)
getShuffleMask - Return the full mask for this instruction, where each element is the element number ...
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.