Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 1 | //===- CostModel.cpp ------ Cost Model Analysis ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the cost model analysis. It provides a very basic cost |
Nadav Rotem | 99b7a99 | 2012-12-24 05:51:12 +0000 | [diff] [blame] | 11 | // estimation for LLVM-IR. This analysis uses the services of the codegen |
| 12 | // to approximate the cost of any IR instruction when lowered to machine |
| 13 | // instructions. The cost results are unit-less and the cost number represents |
| 14 | // the throughput of the machine assuming that all loads hit the cache, all |
| 15 | // branches are predicted, etc. The cost numbers can be added in order to |
| 16 | // compare two or more transformation alternatives. |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #define CM_NAME "cost-model" |
| 21 | #define DEBUG_TYPE CM_NAME |
Arnold Schwaighofer | 65457b6 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | be04929 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Function.h" |
| 26 | #include "llvm/IR/Instructions.h" |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 27 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Value.h" |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Arnold Schwaighofer | 65457b6 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | using namespace llvm; |
| 34 | |
Arnold Schwaighofer | 65457b6 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 35 | static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false), |
| 36 | cl::Hidden, |
| 37 | cl::desc("Recognize reduction patterns.")); |
| 38 | |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | class CostModelAnalysis : public FunctionPass { |
| 41 | |
| 42 | public: |
| 43 | static char ID; // Class identification, replacement for typeinfo |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 44 | CostModelAnalysis() : FunctionPass(ID), F(0), TTI(0) { |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 45 | initializeCostModelAnalysisPass( |
| 46 | *PassRegistry::getPassRegistry()); |
| 47 | } |
| 48 | |
| 49 | /// Returns the expected cost of the instruction. |
| 50 | /// Returns -1 if the cost is unknown. |
| 51 | /// Note, this method does not cache the cost calculation and it |
| 52 | /// can be expensive in some cases. |
Nadav Rotem | e70b268 | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 53 | unsigned getInstructionCost(const Instruction *I) const; |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 54 | |
| 55 | private: |
| 56 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 57 | virtual bool runOnFunction(Function &F); |
| 58 | virtual void print(raw_ostream &OS, const Module*) const; |
| 59 | |
| 60 | /// The function that we analyze. |
| 61 | Function *F; |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 62 | /// Target information. |
| 63 | const TargetTransformInfo *TTI; |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 64 | }; |
| 65 | } // End of anonymous namespace |
| 66 | |
| 67 | // Register this pass. |
| 68 | char CostModelAnalysis::ID = 0; |
| 69 | static const char cm_name[] = "Cost Model Analysis"; |
| 70 | INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true) |
| 71 | INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true) |
| 72 | |
| 73 | FunctionPass *llvm::createCostModelAnalysisPass() { |
| 74 | return new CostModelAnalysis(); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 79 | AU.setPreservesAll(); |
| 80 | } |
| 81 | |
| 82 | bool |
| 83 | CostModelAnalysis::runOnFunction(Function &F) { |
| 84 | this->F = &F; |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 85 | TTI = getAnalysisIfAvailable<TargetTransformInfo>(); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 90 | static bool isReverseVectorMask(SmallVectorImpl<int> &Mask) { |
Arnold Schwaighofer | 4fc6484 | 2013-02-12 02:40:37 +0000 | [diff] [blame] | 91 | for (unsigned i = 0, MaskSize = Mask.size(); i < MaskSize; ++i) |
| 92 | if (Mask[i] > 0 && Mask[i] != (int)(MaskSize - 1 - i)) |
| 93 | return false; |
| 94 | return true; |
| 95 | } |
| 96 | |
Arnold Schwaighofer | 6bf4f67 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 97 | static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) { |
| 98 | TargetTransformInfo::OperandValueKind OpInfo = |
| 99 | TargetTransformInfo::OK_AnyValue; |
| 100 | |
| 101 | // Check for a splat of a constant. |
| 102 | ConstantDataVector *CDV = 0; |
| 103 | if ((CDV = dyn_cast<ConstantDataVector>(V))) |
| 104 | if (CDV->getSplatValue() != NULL) |
| 105 | OpInfo = TargetTransformInfo::OK_UniformConstantValue; |
| 106 | ConstantVector *CV = 0; |
| 107 | if ((CV = dyn_cast<ConstantVector>(V))) |
| 108 | if (CV->getSplatValue() != NULL) |
| 109 | OpInfo = TargetTransformInfo::OK_UniformConstantValue; |
| 110 | |
| 111 | return OpInfo; |
| 112 | } |
| 113 | |
Arnold Schwaighofer | 65457b6 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 114 | static bool matchMask(SmallVectorImpl<int> &M1, SmallVectorImpl<int> &M2) { |
| 115 | if (M1.size() != M2.size()) |
| 116 | return false; |
| 117 | |
| 118 | for (unsigned i = 0, e = M1.size(); i != e; ++i) |
| 119 | if (M1[i] != M2[i]) |
| 120 | return false; |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft, |
| 126 | unsigned Level) { |
| 127 | // We don't need a shuffle if we just want to have element 0 in position 0 of |
| 128 | // the vector. |
| 129 | if (!SI && Level == 0 && IsLeft) |
| 130 | return true; |
| 131 | else if (!SI) |
| 132 | return false; |
| 133 | |
| 134 | SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1); |
| 135 | |
| 136 | // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether |
| 137 | // we look at the left or right side. |
| 138 | for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2) |
| 139 | Mask[i] = val; |
| 140 | |
| 141 | SmallVector<int, 16> ActualMask = SI->getShuffleMask(); |
| 142 | if (!matchMask(Mask, ActualMask)) |
| 143 | return false; |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | static bool matchPairwiseReductionAtLevel(const BinaryOperator *BinOp, |
| 149 | unsigned Level, unsigned NumLevels) { |
| 150 | // Match one level of pairwise operations. |
| 151 | // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef, |
| 152 | // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef> |
| 153 | // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef, |
| 154 | // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef> |
| 155 | // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1 |
| 156 | if (BinOp == 0) |
| 157 | return false; |
| 158 | |
| 159 | Type *VecTy = BinOp->getType(); |
| 160 | assert(VecTy->isVectorTy() && "Expecting a vector type"); |
| 161 | |
| 162 | unsigned Opcode = BinOp->getOpcode(); |
| 163 | Value *L = BinOp->getOperand(0); |
| 164 | Value *R = BinOp->getOperand(1); |
| 165 | |
| 166 | ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(L); |
| 167 | if (!LS && Level) |
| 168 | return false; |
| 169 | ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(R); |
| 170 | if (!RS && Level) |
| 171 | return false; |
| 172 | |
| 173 | // On level 0 we can omit one shufflevector instruction. |
| 174 | if (!Level && !RS && !LS) |
| 175 | return false; |
| 176 | |
| 177 | // Shuffle inputs must match. |
| 178 | Value *NextLevelOpL = LS ? LS->getOperand(0) : 0; |
| 179 | Value *NextLevelOpR = RS ? RS->getOperand(0) : 0; |
| 180 | Value *NextLevelOp = 0; |
| 181 | if (NextLevelOpR && NextLevelOpL) { |
| 182 | // If we have two shuffles their operands must match. |
| 183 | if (NextLevelOpL != NextLevelOpR) |
| 184 | return false; |
| 185 | |
| 186 | NextLevelOp = NextLevelOpL; |
| 187 | } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) { |
| 188 | // On the first level we can omit the shufflevector <0, undef,...>. So the |
| 189 | // input to the other shufflevector <1, undef> must match with one of the |
| 190 | // inputs to the current binary operation. |
| 191 | // Example: |
| 192 | // %NextLevelOpL = shufflevector %R, <1, undef ...> |
| 193 | // %BinOp = fadd %NextLevelOpL, %R |
| 194 | if (NextLevelOpL && NextLevelOpL != R) |
| 195 | return false; |
| 196 | else if (NextLevelOpR && NextLevelOpR != L) |
| 197 | return false; |
| 198 | |
| 199 | NextLevelOp = NextLevelOpL ? R : L; |
| 200 | } else |
| 201 | return false; |
| 202 | |
| 203 | // Check that the next levels binary operation exists and matches with the |
| 204 | // current one. |
| 205 | BinaryOperator *NextLevelBinOp = 0; |
| 206 | if (Level + 1 != NumLevels) { |
| 207 | if (!(NextLevelBinOp = dyn_cast<BinaryOperator>(NextLevelOp))) |
| 208 | return false; |
| 209 | else if (NextLevelBinOp->getOpcode() != Opcode) |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // Shuffle mask for pairwise operation must match. |
| 214 | if (matchPairwiseShuffleMask(LS, true, Level)) { |
| 215 | if (!matchPairwiseShuffleMask(RS, false, Level)) |
| 216 | return false; |
| 217 | } else if (matchPairwiseShuffleMask(RS, true, Level)) { |
| 218 | if (!matchPairwiseShuffleMask(LS, false, Level)) |
| 219 | return false; |
| 220 | } else |
| 221 | return false; |
| 222 | |
| 223 | if (++Level == NumLevels) |
| 224 | return true; |
| 225 | |
| 226 | // Match next level. |
| 227 | return matchPairwiseReductionAtLevel(NextLevelBinOp, Level, NumLevels); |
| 228 | } |
| 229 | |
| 230 | static bool matchPairwiseReduction(const ExtractElementInst *ReduxRoot, |
| 231 | unsigned &Opcode, Type *&Ty) { |
| 232 | if (!EnableReduxCost) |
| 233 | return false; |
| 234 | |
| 235 | // Need to extract the first element. |
| 236 | ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1)); |
| 237 | unsigned Idx = ~0u; |
| 238 | if (CI) |
| 239 | Idx = CI->getZExtValue(); |
| 240 | if (Idx != 0) |
| 241 | return false; |
| 242 | |
| 243 | BinaryOperator *RdxStart = dyn_cast<BinaryOperator>(ReduxRoot->getOperand(0)); |
| 244 | if (!RdxStart) |
| 245 | return false; |
| 246 | |
| 247 | Type *VecTy = ReduxRoot->getOperand(0)->getType(); |
| 248 | unsigned NumVecElems = VecTy->getVectorNumElements(); |
| 249 | if (!isPowerOf2_32(NumVecElems)) |
| 250 | return false; |
| 251 | |
| 252 | // We look for a sequence of shuffle,shuffle,add triples like the following |
| 253 | // that builds a pairwise reduction tree. |
| 254 | // |
| 255 | // (X0, X1, X2, X3) |
| 256 | // (X0 + X1, X2 + X3, undef, undef) |
| 257 | // ((X0 + X1) + (X2 + X3), undef, undef, undef) |
| 258 | // |
| 259 | // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef, |
| 260 | // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef> |
| 261 | // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef, |
| 262 | // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef> |
| 263 | // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1 |
| 264 | // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef, |
| 265 | // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef> |
| 266 | // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef, |
| 267 | // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef> |
| 268 | // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1 |
| 269 | // %r = extractelement <4 x float> %bin.rdx8, i32 0 |
| 270 | if (!matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems))) |
| 271 | return false; |
| 272 | |
| 273 | Opcode = RdxStart->getOpcode(); |
| 274 | Ty = VecTy; |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | static std::pair<Value *, ShuffleVectorInst *> |
| 280 | getShuffleAndOtherOprd(BinaryOperator *B) { |
| 281 | |
| 282 | Value *L = B->getOperand(0); |
| 283 | Value *R = B->getOperand(1); |
| 284 | ShuffleVectorInst *S = 0; |
| 285 | |
| 286 | if ((S = dyn_cast<ShuffleVectorInst>(L))) |
| 287 | return std::make_pair(R, S); |
| 288 | |
| 289 | S = dyn_cast<ShuffleVectorInst>(R); |
| 290 | return std::make_pair(L, S); |
| 291 | } |
| 292 | |
| 293 | static bool matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot, |
| 294 | unsigned &Opcode, Type *&Ty) { |
| 295 | if (!EnableReduxCost) |
| 296 | return false; |
| 297 | |
| 298 | // Need to extract the first element. |
| 299 | ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1)); |
| 300 | unsigned Idx = ~0u; |
| 301 | if (CI) |
| 302 | Idx = CI->getZExtValue(); |
| 303 | if (Idx != 0) |
| 304 | return false; |
| 305 | |
| 306 | BinaryOperator *RdxStart = dyn_cast<BinaryOperator>(ReduxRoot->getOperand(0)); |
| 307 | if (!RdxStart) |
| 308 | return false; |
| 309 | unsigned RdxOpcode = RdxStart->getOpcode(); |
| 310 | |
| 311 | Type *VecTy = ReduxRoot->getOperand(0)->getType(); |
| 312 | unsigned NumVecElems = VecTy->getVectorNumElements(); |
| 313 | if (!isPowerOf2_32(NumVecElems)) |
| 314 | return false; |
| 315 | |
| 316 | // We look for a sequence of shuffles and adds like the following matching one |
| 317 | // fadd, shuffle vector pair at a time. |
| 318 | // |
| 319 | // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef, |
| 320 | // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef> |
| 321 | // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf |
| 322 | // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef, |
| 323 | // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef> |
| 324 | // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7 |
| 325 | // %r = extractelement <4 x float> %bin.rdx8, i32 0 |
| 326 | |
| 327 | unsigned MaskStart = 1; |
| 328 | Value *RdxOp = RdxStart; |
| 329 | SmallVector<int, 32> ShuffleMask(NumVecElems, 0); |
| 330 | unsigned NumVecElemsRemain = NumVecElems; |
| 331 | while (NumVecElemsRemain - 1) { |
| 332 | // Check for the right reduction operation. |
| 333 | BinaryOperator *BinOp; |
| 334 | if (!(BinOp = dyn_cast<BinaryOperator>(RdxOp))) |
| 335 | return false; |
| 336 | if (BinOp->getOpcode() != RdxOpcode) |
| 337 | return false; |
| 338 | |
| 339 | Value *NextRdxOp; |
| 340 | ShuffleVectorInst *Shuffle; |
| 341 | tie(NextRdxOp, Shuffle) = getShuffleAndOtherOprd(BinOp); |
| 342 | |
| 343 | // Check the current reduction operation and the shuffle use the same value. |
| 344 | if (Shuffle == 0) |
| 345 | return false; |
| 346 | if (Shuffle->getOperand(0) != NextRdxOp) |
| 347 | return false; |
| 348 | |
| 349 | // Check that shuffle masks matches. |
| 350 | for (unsigned j = 0; j != MaskStart; ++j) |
| 351 | ShuffleMask[j] = MaskStart + j; |
| 352 | // Fill the rest of the mask with -1 for undef. |
| 353 | std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1); |
| 354 | |
| 355 | SmallVector<int, 16> Mask = Shuffle->getShuffleMask(); |
| 356 | if (!matchMask(ShuffleMask, Mask)) |
| 357 | return false; |
| 358 | |
| 359 | RdxOp = NextRdxOp; |
| 360 | NumVecElemsRemain /= 2; |
| 361 | MaskStart *= 2; |
| 362 | } |
| 363 | |
| 364 | Opcode = RdxOpcode; |
| 365 | Ty = VecTy; |
| 366 | return true; |
| 367 | } |
| 368 | |
Nadav Rotem | e70b268 | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 369 | unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const { |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 370 | if (!TTI) |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 371 | return -1; |
| 372 | |
| 373 | switch (I->getOpcode()) { |
Arnold Schwaighofer | fb55a8f | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 374 | case Instruction::GetElementPtr:{ |
| 375 | Type *ValTy = I->getOperand(0)->getType()->getPointerElementType(); |
| 376 | return TTI->getAddressComputationCost(ValTy); |
| 377 | } |
| 378 | |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 379 | case Instruction::Ret: |
| 380 | case Instruction::PHI: |
| 381 | case Instruction::Br: { |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 382 | return TTI->getCFInstrCost(I->getOpcode()); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 383 | } |
| 384 | case Instruction::Add: |
| 385 | case Instruction::FAdd: |
| 386 | case Instruction::Sub: |
| 387 | case Instruction::FSub: |
| 388 | case Instruction::Mul: |
| 389 | case Instruction::FMul: |
| 390 | case Instruction::UDiv: |
| 391 | case Instruction::SDiv: |
| 392 | case Instruction::FDiv: |
| 393 | case Instruction::URem: |
| 394 | case Instruction::SRem: |
| 395 | case Instruction::FRem: |
| 396 | case Instruction::Shl: |
| 397 | case Instruction::LShr: |
| 398 | case Instruction::AShr: |
| 399 | case Instruction::And: |
| 400 | case Instruction::Or: |
| 401 | case Instruction::Xor: { |
Arnold Schwaighofer | 6bf4f67 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 402 | TargetTransformInfo::OperandValueKind Op1VK = |
| 403 | getOperandInfo(I->getOperand(0)); |
| 404 | TargetTransformInfo::OperandValueKind Op2VK = |
| 405 | getOperandInfo(I->getOperand(1)); |
| 406 | return TTI->getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK, |
| 407 | Op2VK); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 408 | } |
| 409 | case Instruction::Select: { |
Nadav Rotem | e70b268 | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 410 | const SelectInst *SI = cast<SelectInst>(I); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 411 | Type *CondTy = SI->getCondition()->getType(); |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 412 | return TTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 413 | } |
| 414 | case Instruction::ICmp: |
| 415 | case Instruction::FCmp: { |
| 416 | Type *ValTy = I->getOperand(0)->getType(); |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 417 | return TTI->getCmpSelInstrCost(I->getOpcode(), ValTy); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 418 | } |
| 419 | case Instruction::Store: { |
Nadav Rotem | e70b268 | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 420 | const StoreInst *SI = cast<StoreInst>(I); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 421 | Type *ValTy = SI->getValueOperand()->getType(); |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 422 | return TTI->getMemoryOpCost(I->getOpcode(), ValTy, |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 423 | SI->getAlignment(), |
| 424 | SI->getPointerAddressSpace()); |
| 425 | } |
| 426 | case Instruction::Load: { |
Nadav Rotem | e70b268 | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 427 | const LoadInst *LI = cast<LoadInst>(I); |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 428 | return TTI->getMemoryOpCost(I->getOpcode(), I->getType(), |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 429 | LI->getAlignment(), |
| 430 | LI->getPointerAddressSpace()); |
| 431 | } |
| 432 | case Instruction::ZExt: |
| 433 | case Instruction::SExt: |
| 434 | case Instruction::FPToUI: |
| 435 | case Instruction::FPToSI: |
| 436 | case Instruction::FPExt: |
| 437 | case Instruction::PtrToInt: |
| 438 | case Instruction::IntToPtr: |
| 439 | case Instruction::SIToFP: |
| 440 | case Instruction::UIToFP: |
| 441 | case Instruction::Trunc: |
| 442 | case Instruction::FPTrunc: |
| 443 | case Instruction::BitCast: { |
| 444 | Type *SrcTy = I->getOperand(0)->getType(); |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 445 | return TTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 446 | } |
Nadav Rotem | f149567 | 2012-11-02 22:31:56 +0000 | [diff] [blame] | 447 | case Instruction::ExtractElement: { |
Nadav Rotem | e70b268 | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 448 | const ExtractElementInst * EEI = cast<ExtractElementInst>(I); |
Nadav Rotem | f149567 | 2012-11-02 22:31:56 +0000 | [diff] [blame] | 449 | ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1)); |
| 450 | unsigned Idx = -1; |
| 451 | if (CI) |
| 452 | Idx = CI->getZExtValue(); |
Arnold Schwaighofer | 65457b6 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 453 | |
| 454 | // Try to match a reduction sequence (series of shufflevector and vector |
| 455 | // adds followed by a extractelement). |
| 456 | unsigned ReduxOpCode; |
| 457 | Type *ReduxType; |
| 458 | |
| 459 | if (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType)) |
| 460 | return TTI->getReductionCost(ReduxOpCode, ReduxType, false); |
| 461 | else if (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType)) |
| 462 | return TTI->getReductionCost(ReduxOpCode, ReduxType, true); |
| 463 | |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 464 | return TTI->getVectorInstrCost(I->getOpcode(), |
| 465 | EEI->getOperand(0)->getType(), Idx); |
Nadav Rotem | f149567 | 2012-11-02 22:31:56 +0000 | [diff] [blame] | 466 | } |
| 467 | case Instruction::InsertElement: { |
Craig Topper | 2ebba64 | 2013-07-11 05:39:44 +0000 | [diff] [blame] | 468 | const InsertElementInst * IE = cast<InsertElementInst>(I); |
| 469 | ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2)); |
| 470 | unsigned Idx = -1; |
| 471 | if (CI) |
| 472 | Idx = CI->getZExtValue(); |
| 473 | return TTI->getVectorInstrCost(I->getOpcode(), |
| 474 | IE->getType(), Idx); |
| 475 | } |
Arnold Schwaighofer | 4fc6484 | 2013-02-12 02:40:37 +0000 | [diff] [blame] | 476 | case Instruction::ShuffleVector: { |
| 477 | const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I); |
| 478 | Type *VecTypOp0 = Shuffle->getOperand(0)->getType(); |
| 479 | unsigned NumVecElems = VecTypOp0->getVectorNumElements(); |
| 480 | SmallVector<int, 16> Mask = Shuffle->getShuffleMask(); |
| 481 | |
| 482 | if (NumVecElems == Mask.size() && isReverseVectorMask(Mask)) |
| 483 | return TTI->getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0, 0, |
| 484 | 0); |
| 485 | return -1; |
| 486 | } |
Benjamin Kramer | 8611d44 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 487 | case Instruction::Call: |
| 488 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 489 | SmallVector<Type*, 4> Tys; |
| 490 | for (unsigned J = 0, JE = II->getNumArgOperands(); J != JE; ++J) |
| 491 | Tys.push_back(II->getArgOperand(J)->getType()); |
| 492 | |
| 493 | return TTI->getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(), |
| 494 | Tys); |
| 495 | } |
| 496 | return -1; |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 497 | default: |
| 498 | // We don't have any information on this instruction. |
| 499 | return -1; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void CostModelAnalysis::print(raw_ostream &OS, const Module*) const { |
| 504 | if (!F) |
| 505 | return; |
| 506 | |
| 507 | for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) { |
| 508 | for (BasicBlock::iterator it = B->begin(), e = B->end(); it != e; ++it) { |
| 509 | Instruction *Inst = it; |
| 510 | unsigned Cost = getInstructionCost(Inst); |
| 511 | if (Cost != (unsigned)-1) |
| 512 | OS << "Cost Model: Found an estimated cost of " << Cost; |
| 513 | else |
| 514 | OS << "Cost Model: Unknown cost"; |
| 515 | |
| 516 | OS << " for instruction: "<< *Inst << "\n"; |
| 517 | } |
| 518 | } |
| 519 | } |