blob: 898da8d0e8d1adfff7551427cdfdebc72d1587d6 [file] [log] [blame]
Nadav Rotema6b91ac2012-11-02 21:48:17 +00001//===- 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 Rotem99868e42012-12-24 05:51:12 +000011// 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 Rotema6b91ac2012-11-02 21:48:17 +000017//
18//===----------------------------------------------------------------------===//
19
20#define CM_NAME "cost-model"
21#define DEBUG_TYPE CM_NAME
Arnold Schwaighofercae87352013-09-17 18:06:50 +000022#include "llvm/ADT/STLExtras.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000023#include "llvm/Analysis/Passes.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000024#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Function.h"
26#include "llvm/IR/Instructions.h"
Benjamin Kramerf7cfac72013-02-28 19:09:33 +000027#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Value.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000029#include "llvm/Pass.h"
Arnold Schwaighofercae87352013-09-17 18:06:50 +000030#include "llvm/Support/CommandLine.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
33using namespace llvm;
34
Arnold Schwaighofercae87352013-09-17 18:06:50 +000035static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false),
36 cl::Hidden,
37 cl::desc("Recognize reduction patterns."));
38
Nadav Rotema6b91ac2012-11-02 21:48:17 +000039namespace {
40 class CostModelAnalysis : public FunctionPass {
41
42 public:
43 static char ID; // Class identification, replacement for typeinfo
Chandler Carruthcf569a82013-01-05 10:09:33 +000044 CostModelAnalysis() : FunctionPass(ID), F(0), TTI(0) {
Nadav Rotema6b91ac2012-11-02 21:48:17 +000045 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 Rotemce5db0f2012-12-03 22:47:12 +000053 unsigned getInstructionCost(const Instruction *I) const;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000054
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 Carruthcf569a82013-01-05 10:09:33 +000062 /// Target information.
63 const TargetTransformInfo *TTI;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000064 };
65} // End of anonymous namespace
66
67// Register this pass.
68char CostModelAnalysis::ID = 0;
69static const char cm_name[] = "Cost Model Analysis";
70INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
71INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
72
73FunctionPass *llvm::createCostModelAnalysisPass() {
74 return new CostModelAnalysis();
75}
76
77void
78CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.setPreservesAll();
80}
81
82bool
83CostModelAnalysis::runOnFunction(Function &F) {
84 this->F = &F;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000085 TTI = getAnalysisIfAvailable<TargetTransformInfo>();
Nadav Rotema6b91ac2012-11-02 21:48:17 +000086
87 return false;
88}
89
Craig Topper2cd5ff82013-07-11 16:22:38 +000090static bool isReverseVectorMask(SmallVectorImpl<int> &Mask) {
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +000091 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 Schwaighoferb9773872013-04-04 23:26:21 +000097static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) {
98 TargetTransformInfo::OperandValueKind OpInfo =
99 TargetTransformInfo::OK_AnyValue;
100
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000101 // Check for a splat of a constant or for a non uniform vector of constants.
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000102 ConstantDataVector *CDV = 0;
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000103 if ((CDV = dyn_cast<ConstantDataVector>(V))) {
104 OpInfo = TargetTransformInfo::OK_NonUniformConstantValue;
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000105 if (CDV->getSplatValue() != NULL)
106 OpInfo = TargetTransformInfo::OK_UniformConstantValue;
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000107 }
108
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000109 ConstantVector *CV = 0;
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000110 if ((CV = dyn_cast<ConstantVector>(V))) {
111 OpInfo = TargetTransformInfo::OK_NonUniformConstantValue;
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000112 if (CV->getSplatValue() != NULL)
113 OpInfo = TargetTransformInfo::OK_UniformConstantValue;
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000114 }
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000115
116 return OpInfo;
117}
118
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000119static bool matchMask(SmallVectorImpl<int> &M1, SmallVectorImpl<int> &M2) {
120 if (M1.size() != M2.size())
121 return false;
122
123 for (unsigned i = 0, e = M1.size(); i != e; ++i)
124 if (M1[i] != M2[i])
125 return false;
126
127 return true;
128}
129
130static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
131 unsigned Level) {
132 // We don't need a shuffle if we just want to have element 0 in position 0 of
133 // the vector.
134 if (!SI && Level == 0 && IsLeft)
135 return true;
136 else if (!SI)
137 return false;
138
139 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
140
141 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
142 // we look at the left or right side.
143 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
144 Mask[i] = val;
145
146 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
147 if (!matchMask(Mask, ActualMask))
148 return false;
149
150 return true;
151}
152
153static bool matchPairwiseReductionAtLevel(const BinaryOperator *BinOp,
154 unsigned Level, unsigned NumLevels) {
155 // Match one level of pairwise operations.
156 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
157 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
158 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
159 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
160 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
161 if (BinOp == 0)
162 return false;
163
Eric Christophere7af7bd2013-09-17 21:13:57 +0000164 assert(BinOp->getType()->isVectorTy() && "Expecting a vector type");
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000165
166 unsigned Opcode = BinOp->getOpcode();
167 Value *L = BinOp->getOperand(0);
168 Value *R = BinOp->getOperand(1);
169
170 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(L);
171 if (!LS && Level)
172 return false;
173 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(R);
174 if (!RS && Level)
175 return false;
176
177 // On level 0 we can omit one shufflevector instruction.
178 if (!Level && !RS && !LS)
179 return false;
180
181 // Shuffle inputs must match.
182 Value *NextLevelOpL = LS ? LS->getOperand(0) : 0;
183 Value *NextLevelOpR = RS ? RS->getOperand(0) : 0;
184 Value *NextLevelOp = 0;
185 if (NextLevelOpR && NextLevelOpL) {
186 // If we have two shuffles their operands must match.
187 if (NextLevelOpL != NextLevelOpR)
188 return false;
189
190 NextLevelOp = NextLevelOpL;
191 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
192 // On the first level we can omit the shufflevector <0, undef,...>. So the
193 // input to the other shufflevector <1, undef> must match with one of the
194 // inputs to the current binary operation.
195 // Example:
196 // %NextLevelOpL = shufflevector %R, <1, undef ...>
197 // %BinOp = fadd %NextLevelOpL, %R
198 if (NextLevelOpL && NextLevelOpL != R)
199 return false;
200 else if (NextLevelOpR && NextLevelOpR != L)
201 return false;
202
203 NextLevelOp = NextLevelOpL ? R : L;
204 } else
205 return false;
206
207 // Check that the next levels binary operation exists and matches with the
208 // current one.
209 BinaryOperator *NextLevelBinOp = 0;
210 if (Level + 1 != NumLevels) {
211 if (!(NextLevelBinOp = dyn_cast<BinaryOperator>(NextLevelOp)))
212 return false;
213 else if (NextLevelBinOp->getOpcode() != Opcode)
214 return false;
215 }
216
217 // Shuffle mask for pairwise operation must match.
218 if (matchPairwiseShuffleMask(LS, true, Level)) {
219 if (!matchPairwiseShuffleMask(RS, false, Level))
220 return false;
221 } else if (matchPairwiseShuffleMask(RS, true, Level)) {
222 if (!matchPairwiseShuffleMask(LS, false, Level))
223 return false;
224 } else
225 return false;
226
227 if (++Level == NumLevels)
228 return true;
229
230 // Match next level.
231 return matchPairwiseReductionAtLevel(NextLevelBinOp, Level, NumLevels);
232}
233
234static bool matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
235 unsigned &Opcode, Type *&Ty) {
236 if (!EnableReduxCost)
237 return false;
238
239 // Need to extract the first element.
240 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
241 unsigned Idx = ~0u;
242 if (CI)
243 Idx = CI->getZExtValue();
244 if (Idx != 0)
245 return false;
246
247 BinaryOperator *RdxStart = dyn_cast<BinaryOperator>(ReduxRoot->getOperand(0));
248 if (!RdxStart)
249 return false;
250
251 Type *VecTy = ReduxRoot->getOperand(0)->getType();
252 unsigned NumVecElems = VecTy->getVectorNumElements();
253 if (!isPowerOf2_32(NumVecElems))
254 return false;
255
256 // We look for a sequence of shuffle,shuffle,add triples like the following
257 // that builds a pairwise reduction tree.
258 //
259 // (X0, X1, X2, X3)
260 // (X0 + X1, X2 + X3, undef, undef)
261 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
262 //
263 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
264 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
265 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
266 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
267 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
268 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
269 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
270 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
271 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
272 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
273 // %r = extractelement <4 x float> %bin.rdx8, i32 0
274 if (!matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)))
275 return false;
276
277 Opcode = RdxStart->getOpcode();
278 Ty = VecTy;
279
280 return true;
281}
282
283static std::pair<Value *, ShuffleVectorInst *>
284getShuffleAndOtherOprd(BinaryOperator *B) {
285
286 Value *L = B->getOperand(0);
287 Value *R = B->getOperand(1);
288 ShuffleVectorInst *S = 0;
289
290 if ((S = dyn_cast<ShuffleVectorInst>(L)))
291 return std::make_pair(R, S);
292
293 S = dyn_cast<ShuffleVectorInst>(R);
294 return std::make_pair(L, S);
295}
296
297static bool matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
298 unsigned &Opcode, Type *&Ty) {
299 if (!EnableReduxCost)
300 return false;
301
302 // Need to extract the first element.
303 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
304 unsigned Idx = ~0u;
305 if (CI)
306 Idx = CI->getZExtValue();
307 if (Idx != 0)
308 return false;
309
310 BinaryOperator *RdxStart = dyn_cast<BinaryOperator>(ReduxRoot->getOperand(0));
311 if (!RdxStart)
312 return false;
313 unsigned RdxOpcode = RdxStart->getOpcode();
314
315 Type *VecTy = ReduxRoot->getOperand(0)->getType();
316 unsigned NumVecElems = VecTy->getVectorNumElements();
317 if (!isPowerOf2_32(NumVecElems))
318 return false;
319
320 // We look for a sequence of shuffles and adds like the following matching one
321 // fadd, shuffle vector pair at a time.
322 //
323 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
324 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
325 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
326 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
327 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
328 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
329 // %r = extractelement <4 x float> %bin.rdx8, i32 0
330
331 unsigned MaskStart = 1;
332 Value *RdxOp = RdxStart;
333 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
334 unsigned NumVecElemsRemain = NumVecElems;
335 while (NumVecElemsRemain - 1) {
336 // Check for the right reduction operation.
337 BinaryOperator *BinOp;
338 if (!(BinOp = dyn_cast<BinaryOperator>(RdxOp)))
339 return false;
340 if (BinOp->getOpcode() != RdxOpcode)
341 return false;
342
343 Value *NextRdxOp;
344 ShuffleVectorInst *Shuffle;
345 tie(NextRdxOp, Shuffle) = getShuffleAndOtherOprd(BinOp);
346
347 // Check the current reduction operation and the shuffle use the same value.
348 if (Shuffle == 0)
349 return false;
350 if (Shuffle->getOperand(0) != NextRdxOp)
351 return false;
352
353 // Check that shuffle masks matches.
354 for (unsigned j = 0; j != MaskStart; ++j)
355 ShuffleMask[j] = MaskStart + j;
356 // Fill the rest of the mask with -1 for undef.
357 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
358
359 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
360 if (!matchMask(ShuffleMask, Mask))
361 return false;
362
363 RdxOp = NextRdxOp;
364 NumVecElemsRemain /= 2;
365 MaskStart *= 2;
366 }
367
368 Opcode = RdxOpcode;
369 Ty = VecTy;
370 return true;
371}
372
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000373unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const {
Chandler Carruthcf569a82013-01-05 10:09:33 +0000374 if (!TTI)
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000375 return -1;
376
377 switch (I->getOpcode()) {
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000378 case Instruction::GetElementPtr:{
379 Type *ValTy = I->getOperand(0)->getType()->getPointerElementType();
380 return TTI->getAddressComputationCost(ValTy);
381 }
382
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000383 case Instruction::Ret:
384 case Instruction::PHI:
385 case Instruction::Br: {
Chandler Carruthcf569a82013-01-05 10:09:33 +0000386 return TTI->getCFInstrCost(I->getOpcode());
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000387 }
388 case Instruction::Add:
389 case Instruction::FAdd:
390 case Instruction::Sub:
391 case Instruction::FSub:
392 case Instruction::Mul:
393 case Instruction::FMul:
394 case Instruction::UDiv:
395 case Instruction::SDiv:
396 case Instruction::FDiv:
397 case Instruction::URem:
398 case Instruction::SRem:
399 case Instruction::FRem:
400 case Instruction::Shl:
401 case Instruction::LShr:
402 case Instruction::AShr:
403 case Instruction::And:
404 case Instruction::Or:
405 case Instruction::Xor: {
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000406 TargetTransformInfo::OperandValueKind Op1VK =
407 getOperandInfo(I->getOperand(0));
408 TargetTransformInfo::OperandValueKind Op2VK =
409 getOperandInfo(I->getOperand(1));
410 return TTI->getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK,
411 Op2VK);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000412 }
413 case Instruction::Select: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000414 const SelectInst *SI = cast<SelectInst>(I);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000415 Type *CondTy = SI->getCondition()->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000416 return TTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000417 }
418 case Instruction::ICmp:
419 case Instruction::FCmp: {
420 Type *ValTy = I->getOperand(0)->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000421 return TTI->getCmpSelInstrCost(I->getOpcode(), ValTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000422 }
423 case Instruction::Store: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000424 const StoreInst *SI = cast<StoreInst>(I);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000425 Type *ValTy = SI->getValueOperand()->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000426 return TTI->getMemoryOpCost(I->getOpcode(), ValTy,
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000427 SI->getAlignment(),
428 SI->getPointerAddressSpace());
429 }
430 case Instruction::Load: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000431 const LoadInst *LI = cast<LoadInst>(I);
Chandler Carruthcf569a82013-01-05 10:09:33 +0000432 return TTI->getMemoryOpCost(I->getOpcode(), I->getType(),
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000433 LI->getAlignment(),
434 LI->getPointerAddressSpace());
435 }
436 case Instruction::ZExt:
437 case Instruction::SExt:
438 case Instruction::FPToUI:
439 case Instruction::FPToSI:
440 case Instruction::FPExt:
441 case Instruction::PtrToInt:
442 case Instruction::IntToPtr:
443 case Instruction::SIToFP:
444 case Instruction::UIToFP:
445 case Instruction::Trunc:
446 case Instruction::FPTrunc:
Matt Arsenault339506d2014-01-22 20:30:16 +0000447 case Instruction::BitCast:
448 case Instruction::AddrSpaceCast: {
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000449 Type *SrcTy = I->getOperand(0)->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000450 return TTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000451 }
Nadav Rotem13da9472012-11-02 22:31:56 +0000452 case Instruction::ExtractElement: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000453 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
Nadav Rotem13da9472012-11-02 22:31:56 +0000454 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
455 unsigned Idx = -1;
456 if (CI)
457 Idx = CI->getZExtValue();
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000458
459 // Try to match a reduction sequence (series of shufflevector and vector
460 // adds followed by a extractelement).
461 unsigned ReduxOpCode;
462 Type *ReduxType;
463
464 if (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType))
465 return TTI->getReductionCost(ReduxOpCode, ReduxType, false);
466 else if (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType))
467 return TTI->getReductionCost(ReduxOpCode, ReduxType, true);
468
Chandler Carruthcf569a82013-01-05 10:09:33 +0000469 return TTI->getVectorInstrCost(I->getOpcode(),
470 EEI->getOperand(0)->getType(), Idx);
Nadav Rotem13da9472012-11-02 22:31:56 +0000471 }
472 case Instruction::InsertElement: {
Craig Topper37039642013-07-11 05:39:44 +0000473 const InsertElementInst * IE = cast<InsertElementInst>(I);
474 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
475 unsigned Idx = -1;
476 if (CI)
477 Idx = CI->getZExtValue();
478 return TTI->getVectorInstrCost(I->getOpcode(),
479 IE->getType(), Idx);
480 }
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +0000481 case Instruction::ShuffleVector: {
482 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
483 Type *VecTypOp0 = Shuffle->getOperand(0)->getType();
484 unsigned NumVecElems = VecTypOp0->getVectorNumElements();
485 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
486
487 if (NumVecElems == Mask.size() && isReverseVectorMask(Mask))
488 return TTI->getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0, 0,
489 0);
490 return -1;
491 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000492 case Instruction::Call:
493 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
494 SmallVector<Type*, 4> Tys;
495 for (unsigned J = 0, JE = II->getNumArgOperands(); J != JE; ++J)
496 Tys.push_back(II->getArgOperand(J)->getType());
497
498 return TTI->getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
499 Tys);
500 }
501 return -1;
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000502 default:
503 // We don't have any information on this instruction.
504 return -1;
505 }
506}
507
508void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
509 if (!F)
510 return;
511
512 for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) {
513 for (BasicBlock::iterator it = B->begin(), e = B->end(); it != e; ++it) {
514 Instruction *Inst = it;
515 unsigned Cost = getInstructionCost(Inst);
516 if (Cost != (unsigned)-1)
517 OS << "Cost Model: Found an estimated cost of " << Cost;
518 else
519 OS << "Cost Model: Unknown cost";
520
521 OS << " for instruction: "<< *Inst << "\n";
522 }
523 }
524}