2012-11-02 21:48:17 +00:00
|
|
|
//===- CostModel.cpp ------ Cost Model Analysis ---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the cost model analysis. It provides a very basic cost
|
2012-12-24 05:51:12 +00:00
|
|
|
// estimation for LLVM-IR. This analysis uses the services of the codegen
|
|
|
|
// to approximate the cost of any IR instruction when lowered to machine
|
|
|
|
// instructions. The cost results are unit-less and the cost number represents
|
|
|
|
// the throughput of the machine assuming that all loads hit the cache, all
|
|
|
|
// branches are predicted, etc. The cost numbers can be added in order to
|
|
|
|
// compare two or more transformation alternatives.
|
2012-11-02 21:48:17 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define CM_NAME "cost-model"
|
|
|
|
#define DEBUG_TYPE CM_NAME
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
2013-01-07 03:08:10 +00:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2013-02-28 19:09:33 +00:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Value.h"
|
2012-11-02 21:48:17 +00:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CostModelAnalysis : public FunctionPass {
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2013-01-05 10:09:33 +00:00
|
|
|
CostModelAnalysis() : FunctionPass(ID), F(0), TTI(0) {
|
2012-11-02 21:48:17 +00:00
|
|
|
initializeCostModelAnalysisPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the expected cost of the instruction.
|
|
|
|
/// Returns -1 if the cost is unknown.
|
|
|
|
/// Note, this method does not cache the cost calculation and it
|
|
|
|
/// can be expensive in some cases.
|
2012-12-03 22:47:12 +00:00
|
|
|
unsigned getInstructionCost(const Instruction *I) const;
|
2012-11-02 21:48:17 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
virtual void print(raw_ostream &OS, const Module*) const;
|
|
|
|
|
|
|
|
/// The function that we analyze.
|
|
|
|
Function *F;
|
2013-01-05 10:09:33 +00:00
|
|
|
/// Target information.
|
|
|
|
const TargetTransformInfo *TTI;
|
2012-11-02 21:48:17 +00:00
|
|
|
};
|
|
|
|
} // End of anonymous namespace
|
|
|
|
|
|
|
|
// Register this pass.
|
|
|
|
char CostModelAnalysis::ID = 0;
|
|
|
|
static const char cm_name[] = "Cost Model Analysis";
|
|
|
|
INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
|
|
|
|
INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createCostModelAnalysisPass() {
|
|
|
|
return new CostModelAnalysis();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CostModelAnalysis::runOnFunction(Function &F) {
|
|
|
|
this->F = &F;
|
|
|
|
TTI = getAnalysisIfAvailable<TargetTransformInfo>();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-12 02:40:37 +00:00
|
|
|
static bool isReverseVectorMask(SmallVector<int, 16> &Mask) {
|
|
|
|
for (unsigned i = 0, MaskSize = Mask.size(); i < MaskSize; ++i)
|
|
|
|
if (Mask[i] > 0 && Mask[i] != (int)(MaskSize - 1 - i))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-04 23:26:21 +00:00
|
|
|
static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) {
|
|
|
|
TargetTransformInfo::OperandValueKind OpInfo =
|
|
|
|
TargetTransformInfo::OK_AnyValue;
|
|
|
|
|
|
|
|
// Check for a splat of a constant.
|
|
|
|
ConstantDataVector *CDV = 0;
|
|
|
|
if ((CDV = dyn_cast<ConstantDataVector>(V)))
|
|
|
|
if (CDV->getSplatValue() != NULL)
|
|
|
|
OpInfo = TargetTransformInfo::OK_UniformConstantValue;
|
|
|
|
ConstantVector *CV = 0;
|
|
|
|
if ((CV = dyn_cast<ConstantVector>(V)))
|
|
|
|
if (CV->getSplatValue() != NULL)
|
|
|
|
OpInfo = TargetTransformInfo::OK_UniformConstantValue;
|
|
|
|
|
|
|
|
return OpInfo;
|
|
|
|
}
|
|
|
|
|
2012-12-03 22:47:12 +00:00
|
|
|
unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const {
|
2013-01-05 10:09:33 +00:00
|
|
|
if (!TTI)
|
2012-11-02 21:48:17 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (I->getOpcode()) {
|
2013-02-08 14:50:48 +00:00
|
|
|
case Instruction::GetElementPtr:{
|
|
|
|
Type *ValTy = I->getOperand(0)->getType()->getPointerElementType();
|
|
|
|
return TTI->getAddressComputationCost(ValTy);
|
|
|
|
}
|
|
|
|
|
2012-11-02 21:48:17 +00:00
|
|
|
case Instruction::Ret:
|
|
|
|
case Instruction::PHI:
|
|
|
|
case Instruction::Br: {
|
2013-01-05 10:09:33 +00:00
|
|
|
return TTI->getCFInstrCost(I->getOpcode());
|
2012-11-02 21:48:17 +00:00
|
|
|
}
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::FAdd:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::FSub:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::FMul:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::FDiv:
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::FRem:
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor: {
|
2013-04-04 23:26:21 +00:00
|
|
|
TargetTransformInfo::OperandValueKind Op1VK =
|
|
|
|
getOperandInfo(I->getOperand(0));
|
|
|
|
TargetTransformInfo::OperandValueKind Op2VK =
|
|
|
|
getOperandInfo(I->getOperand(1));
|
|
|
|
return TTI->getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK,
|
|
|
|
Op2VK);
|
2012-11-02 21:48:17 +00:00
|
|
|
}
|
|
|
|
case Instruction::Select: {
|
2012-12-03 22:47:12 +00:00
|
|
|
const SelectInst *SI = cast<SelectInst>(I);
|
2012-11-02 21:48:17 +00:00
|
|
|
Type *CondTy = SI->getCondition()->getType();
|
2013-01-05 10:09:33 +00:00
|
|
|
return TTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy);
|
2012-11-02 21:48:17 +00:00
|
|
|
}
|
|
|
|
case Instruction::ICmp:
|
|
|
|
case Instruction::FCmp: {
|
|
|
|
Type *ValTy = I->getOperand(0)->getType();
|
2013-01-05 10:09:33 +00:00
|
|
|
return TTI->getCmpSelInstrCost(I->getOpcode(), ValTy);
|
2012-11-02 21:48:17 +00:00
|
|
|
}
|
|
|
|
case Instruction::Store: {
|
2012-12-03 22:47:12 +00:00
|
|
|
const StoreInst *SI = cast<StoreInst>(I);
|
2012-11-02 21:48:17 +00:00
|
|
|
Type *ValTy = SI->getValueOperand()->getType();
|
2013-01-05 10:09:33 +00:00
|
|
|
return TTI->getMemoryOpCost(I->getOpcode(), ValTy,
|
2012-11-02 21:48:17 +00:00
|
|
|
SI->getAlignment(),
|
|
|
|
SI->getPointerAddressSpace());
|
|
|
|
}
|
|
|
|
case Instruction::Load: {
|
2012-12-03 22:47:12 +00:00
|
|
|
const LoadInst *LI = cast<LoadInst>(I);
|
2013-01-05 10:09:33 +00:00
|
|
|
return TTI->getMemoryOpCost(I->getOpcode(), I->getType(),
|
2012-11-02 21:48:17 +00:00
|
|
|
LI->getAlignment(),
|
|
|
|
LI->getPointerAddressSpace());
|
|
|
|
}
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::BitCast: {
|
|
|
|
Type *SrcTy = I->getOperand(0)->getType();
|
2013-01-05 10:09:33 +00:00
|
|
|
return TTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy);
|
2012-11-02 21:48:17 +00:00
|
|
|
}
|
2012-11-02 22:31:56 +00:00
|
|
|
case Instruction::ExtractElement: {
|
2012-12-03 22:47:12 +00:00
|
|
|
const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
|
2012-11-02 22:31:56 +00:00
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
|
|
|
|
unsigned Idx = -1;
|
|
|
|
if (CI)
|
|
|
|
Idx = CI->getZExtValue();
|
2013-01-05 10:09:33 +00:00
|
|
|
return TTI->getVectorInstrCost(I->getOpcode(),
|
|
|
|
EEI->getOperand(0)->getType(), Idx);
|
2012-11-02 22:31:56 +00:00
|
|
|
}
|
|
|
|
case Instruction::InsertElement: {
|
2012-12-03 22:47:12 +00:00
|
|
|
const InsertElementInst * IE = cast<InsertElementInst>(I);
|
2012-11-02 22:31:56 +00:00
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
|
|
|
|
unsigned Idx = -1;
|
|
|
|
if (CI)
|
|
|
|
Idx = CI->getZExtValue();
|
2013-01-05 10:09:33 +00:00
|
|
|
return TTI->getVectorInstrCost(I->getOpcode(),
|
|
|
|
IE->getType(), Idx);
|
2012-11-02 22:31:56 +00:00
|
|
|
}
|
2013-02-12 02:40:37 +00:00
|
|
|
case Instruction::ShuffleVector: {
|
|
|
|
const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
|
|
|
|
Type *VecTypOp0 = Shuffle->getOperand(0)->getType();
|
|
|
|
unsigned NumVecElems = VecTypOp0->getVectorNumElements();
|
|
|
|
SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
|
|
|
|
|
|
|
|
if (NumVecElems == Mask.size() && isReverseVectorMask(Mask))
|
|
|
|
return TTI->getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0, 0,
|
|
|
|
0);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-02-28 19:09:33 +00:00
|
|
|
case Instruction::Call:
|
|
|
|
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
|
|
|
|
SmallVector<Type*, 4> Tys;
|
|
|
|
for (unsigned J = 0, JE = II->getNumArgOperands(); J != JE; ++J)
|
|
|
|
Tys.push_back(II->getArgOperand(J)->getType());
|
|
|
|
|
|
|
|
return TTI->getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
|
|
|
|
Tys);
|
|
|
|
}
|
|
|
|
return -1;
|
2012-11-02 21:48:17 +00:00
|
|
|
default:
|
|
|
|
// We don't have any information on this instruction.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
|
|
|
|
if (!F)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) {
|
|
|
|
for (BasicBlock::iterator it = B->begin(), e = B->end(); it != e; ++it) {
|
|
|
|
Instruction *Inst = it;
|
|
|
|
unsigned Cost = getInstructionCost(Inst);
|
|
|
|
if (Cost != (unsigned)-1)
|
|
|
|
OS << "Cost Model: Found an estimated cost of " << Cost;
|
|
|
|
else
|
|
|
|
OS << "Cost Model: Unknown cost";
|
|
|
|
|
|
|
|
OS << " for instruction: "<< *Inst << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|