blob: 870c446b24a892e596e3198d31311c7a5285e3dd [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
Arnold Schwaighofercae87352013-09-17 18:06:50 +000020#include "llvm/ADT/STLExtras.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000021#include "llvm/Analysis/Passes.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000022#include "llvm/Analysis/TargetTransformInfo.h"
Michael Kuperstein3ceac2b2016-08-04 22:48:03 +000023#include "llvm/Analysis/VectorUtils.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
25#include "llvm/IR/Instructions.h"
Benjamin Kramerf7cfac72013-02-28 19:09:33 +000026#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Value.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000028#include "llvm/Pass.h"
Arnold Schwaighofercae87352013-09-17 18:06:50 +000029#include "llvm/Support/CommandLine.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33
Chandler Carruthf1221bd2014-04-22 02:48:03 +000034#define CM_NAME "cost-model"
35#define DEBUG_TYPE CM_NAME
36
Arnold Schwaighofercae87352013-09-17 18:06:50 +000037static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false),
38 cl::Hidden,
39 cl::desc("Recognize reduction patterns."));
40
Nadav Rotema6b91ac2012-11-02 21:48:17 +000041namespace {
42 class CostModelAnalysis : public FunctionPass {
43
44 public:
45 static char ID; // Class identification, replacement for typeinfo
Craig Topper9f008862014-04-15 04:59:12 +000046 CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) {
Nadav Rotema6b91ac2012-11-02 21:48:17 +000047 initializeCostModelAnalysisPass(
48 *PassRegistry::getPassRegistry());
49 }
50
51 /// Returns the expected cost of the instruction.
52 /// Returns -1 if the cost is unknown.
53 /// Note, this method does not cache the cost calculation and it
54 /// can be expensive in some cases.
Nadav Rotemce5db0f2012-12-03 22:47:12 +000055 unsigned getInstructionCost(const Instruction *I) const;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000056
57 private:
Craig Toppere9ba7592014-03-05 07:30:04 +000058 void getAnalysisUsage(AnalysisUsage &AU) const override;
59 bool runOnFunction(Function &F) override;
60 void print(raw_ostream &OS, const Module*) const override;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000061
62 /// The function that we analyze.
63 Function *F;
Chandler Carruthcf569a82013-01-05 10:09:33 +000064 /// Target information.
65 const TargetTransformInfo *TTI;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000066 };
67} // End of anonymous namespace
68
69// Register this pass.
70char CostModelAnalysis::ID = 0;
71static const char cm_name[] = "Cost Model Analysis";
72INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
73INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
74
75FunctionPass *llvm::createCostModelAnalysisPass() {
76 return new CostModelAnalysis();
77}
78
79void
80CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
81 AU.setPreservesAll();
82}
83
84bool
85CostModelAnalysis::runOnFunction(Function &F) {
86 this->F = &F;
Chandler Carruth705b1852015-01-31 03:43:40 +000087 auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
Chandler Carruthfdb9c572015-02-01 12:01:35 +000088 TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000089
90 return false;
91}
92
Simon Pilgrimc93cd302016-12-21 15:49:01 +000093static bool isReverseVectorMask(ArrayRef<int> Mask) {
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +000094 for (unsigned i = 0, MaskSize = Mask.size(); i < MaskSize; ++i)
Simon Pilgrim9876ed02016-12-15 12:12:45 +000095 if (Mask[i] >= 0 && Mask[i] != (int)(MaskSize - 1 - i))
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +000096 return false;
97 return true;
98}
99
Simon Pilgrimc93cd302016-12-21 15:49:01 +0000100static bool isAlternateVectorMask(ArrayRef<int> Mask) {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000101 bool isAlternate = true;
102 unsigned MaskSize = Mask.size();
103
104 // Example: shufflevector A, B, <0,5,2,7>
105 for (unsigned i = 0; i < MaskSize && isAlternate; ++i) {
106 if (Mask[i] < 0)
107 continue;
108 isAlternate = Mask[i] == (int)((i & 1) ? MaskSize + i : i);
109 }
110
111 if (isAlternate)
112 return true;
113
114 isAlternate = true;
115 // Example: shufflevector A, B, <4,1,6,3>
116 for (unsigned i = 0; i < MaskSize && isAlternate; ++i) {
117 if (Mask[i] < 0)
118 continue;
119 isAlternate = Mask[i] == (int)((i & 1) ? i : MaskSize + i);
120 }
121
122 return isAlternate;
123}
124
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000125static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) {
126 TargetTransformInfo::OperandValueKind OpInfo =
Michael Kuperstein3ceac2b2016-08-04 22:48:03 +0000127 TargetTransformInfo::OK_AnyValue;
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000128
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000129 // Check for a splat of a constant or for a non uniform vector of constants.
Benjamin Kramer989b9292014-02-13 16:48:38 +0000130 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000131 OpInfo = TargetTransformInfo::OK_NonUniformConstantValue;
Craig Topper9f008862014-04-15 04:59:12 +0000132 if (cast<Constant>(V)->getSplatValue() != nullptr)
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000133 OpInfo = TargetTransformInfo::OK_UniformConstantValue;
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000134 }
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000135
Michael Kuperstein3ceac2b2016-08-04 22:48:03 +0000136 // Check for a splat of a uniform value. This is not loop aware, so return
137 // true only for the obviously uniform cases (argument, globalvalue)
138 const Value *Splat = getSplatValue(V);
139 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat)))
140 OpInfo = TargetTransformInfo::OK_UniformValue;
141
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000142 return OpInfo;
143}
144
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000145static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
146 unsigned Level) {
147 // We don't need a shuffle if we just want to have element 0 in position 0 of
148 // the vector.
149 if (!SI && Level == 0 && IsLeft)
150 return true;
151 else if (!SI)
152 return false;
153
154 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
155
156 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
157 // we look at the left or right side.
158 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
159 Mask[i] = val;
160
161 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
Alexander Kornienko484e48e32015-11-05 21:07:12 +0000162 return Mask == ActualMask;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000163}
164
165static bool matchPairwiseReductionAtLevel(const BinaryOperator *BinOp,
166 unsigned Level, unsigned NumLevels) {
167 // Match one level of pairwise operations.
168 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
169 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
170 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
171 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
172 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
Craig Topper9f008862014-04-15 04:59:12 +0000173 if (BinOp == nullptr)
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000174 return false;
175
Eric Christophere7af7bd2013-09-17 21:13:57 +0000176 assert(BinOp->getType()->isVectorTy() && "Expecting a vector type");
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000177
178 unsigned Opcode = BinOp->getOpcode();
179 Value *L = BinOp->getOperand(0);
180 Value *R = BinOp->getOperand(1);
181
182 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(L);
183 if (!LS && Level)
184 return false;
185 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(R);
186 if (!RS && Level)
187 return false;
188
189 // On level 0 we can omit one shufflevector instruction.
190 if (!Level && !RS && !LS)
191 return false;
192
193 // Shuffle inputs must match.
Craig Topper9f008862014-04-15 04:59:12 +0000194 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
195 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
196 Value *NextLevelOp = nullptr;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000197 if (NextLevelOpR && NextLevelOpL) {
198 // If we have two shuffles their operands must match.
199 if (NextLevelOpL != NextLevelOpR)
200 return false;
201
202 NextLevelOp = NextLevelOpL;
203 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
204 // On the first level we can omit the shufflevector <0, undef,...>. So the
205 // input to the other shufflevector <1, undef> must match with one of the
206 // inputs to the current binary operation.
207 // Example:
208 // %NextLevelOpL = shufflevector %R, <1, undef ...>
209 // %BinOp = fadd %NextLevelOpL, %R
210 if (NextLevelOpL && NextLevelOpL != R)
211 return false;
212 else if (NextLevelOpR && NextLevelOpR != L)
213 return false;
214
215 NextLevelOp = NextLevelOpL ? R : L;
216 } else
217 return false;
218
219 // Check that the next levels binary operation exists and matches with the
220 // current one.
Craig Topper9f008862014-04-15 04:59:12 +0000221 BinaryOperator *NextLevelBinOp = nullptr;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000222 if (Level + 1 != NumLevels) {
223 if (!(NextLevelBinOp = dyn_cast<BinaryOperator>(NextLevelOp)))
224 return false;
225 else if (NextLevelBinOp->getOpcode() != Opcode)
226 return false;
227 }
228
229 // Shuffle mask for pairwise operation must match.
230 if (matchPairwiseShuffleMask(LS, true, Level)) {
231 if (!matchPairwiseShuffleMask(RS, false, Level))
232 return false;
233 } else if (matchPairwiseShuffleMask(RS, true, Level)) {
234 if (!matchPairwiseShuffleMask(LS, false, Level))
235 return false;
236 } else
237 return false;
238
239 if (++Level == NumLevels)
240 return true;
241
242 // Match next level.
243 return matchPairwiseReductionAtLevel(NextLevelBinOp, Level, NumLevels);
244}
245
246static bool matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
247 unsigned &Opcode, Type *&Ty) {
248 if (!EnableReduxCost)
249 return false;
250
251 // Need to extract the first element.
252 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
253 unsigned Idx = ~0u;
254 if (CI)
255 Idx = CI->getZExtValue();
256 if (Idx != 0)
257 return false;
258
259 BinaryOperator *RdxStart = dyn_cast<BinaryOperator>(ReduxRoot->getOperand(0));
260 if (!RdxStart)
261 return false;
262
263 Type *VecTy = ReduxRoot->getOperand(0)->getType();
264 unsigned NumVecElems = VecTy->getVectorNumElements();
265 if (!isPowerOf2_32(NumVecElems))
266 return false;
267
268 // We look for a sequence of shuffle,shuffle,add triples like the following
269 // that builds a pairwise reduction tree.
270 //
271 // (X0, X1, X2, X3)
272 // (X0 + X1, X2 + X3, undef, undef)
273 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
274 //
275 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
276 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
277 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
278 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
279 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
280 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
281 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
282 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
283 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
284 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
285 // %r = extractelement <4 x float> %bin.rdx8, i32 0
286 if (!matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)))
287 return false;
288
289 Opcode = RdxStart->getOpcode();
290 Ty = VecTy;
291
292 return true;
293}
294
295static std::pair<Value *, ShuffleVectorInst *>
296getShuffleAndOtherOprd(BinaryOperator *B) {
297
298 Value *L = B->getOperand(0);
299 Value *R = B->getOperand(1);
Craig Topper9f008862014-04-15 04:59:12 +0000300 ShuffleVectorInst *S = nullptr;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000301
302 if ((S = dyn_cast<ShuffleVectorInst>(L)))
303 return std::make_pair(R, S);
304
305 S = dyn_cast<ShuffleVectorInst>(R);
306 return std::make_pair(L, S);
307}
308
309static bool matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
310 unsigned &Opcode, Type *&Ty) {
311 if (!EnableReduxCost)
312 return false;
313
314 // Need to extract the first element.
315 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
316 unsigned Idx = ~0u;
317 if (CI)
318 Idx = CI->getZExtValue();
319 if (Idx != 0)
320 return false;
321
322 BinaryOperator *RdxStart = dyn_cast<BinaryOperator>(ReduxRoot->getOperand(0));
323 if (!RdxStart)
324 return false;
325 unsigned RdxOpcode = RdxStart->getOpcode();
326
327 Type *VecTy = ReduxRoot->getOperand(0)->getType();
328 unsigned NumVecElems = VecTy->getVectorNumElements();
329 if (!isPowerOf2_32(NumVecElems))
330 return false;
331
332 // We look for a sequence of shuffles and adds like the following matching one
333 // fadd, shuffle vector pair at a time.
334 //
335 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
336 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
337 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
338 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
339 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
340 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
341 // %r = extractelement <4 x float> %bin.rdx8, i32 0
342
343 unsigned MaskStart = 1;
344 Value *RdxOp = RdxStart;
345 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
346 unsigned NumVecElemsRemain = NumVecElems;
347 while (NumVecElemsRemain - 1) {
348 // Check for the right reduction operation.
349 BinaryOperator *BinOp;
350 if (!(BinOp = dyn_cast<BinaryOperator>(RdxOp)))
351 return false;
352 if (BinOp->getOpcode() != RdxOpcode)
353 return false;
354
355 Value *NextRdxOp;
356 ShuffleVectorInst *Shuffle;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000357 std::tie(NextRdxOp, Shuffle) = getShuffleAndOtherOprd(BinOp);
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000358
359 // Check the current reduction operation and the shuffle use the same value.
Craig Topper9f008862014-04-15 04:59:12 +0000360 if (Shuffle == nullptr)
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000361 return false;
362 if (Shuffle->getOperand(0) != NextRdxOp)
363 return false;
364
365 // Check that shuffle masks matches.
366 for (unsigned j = 0; j != MaskStart; ++j)
367 ShuffleMask[j] = MaskStart + j;
368 // Fill the rest of the mask with -1 for undef.
369 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
370
371 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
Benjamin Kramer147644d2014-04-18 19:48:03 +0000372 if (ShuffleMask != Mask)
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000373 return false;
374
375 RdxOp = NextRdxOp;
376 NumVecElemsRemain /= 2;
377 MaskStart *= 2;
378 }
379
380 Opcode = RdxOpcode;
381 Ty = VecTy;
382 return true;
383}
384
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000385unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const {
Chandler Carruthcf569a82013-01-05 10:09:33 +0000386 if (!TTI)
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000387 return -1;
388
389 switch (I->getOpcode()) {
Jingyue Wubfefff52015-07-26 19:10:03 +0000390 case Instruction::GetElementPtr:
391 return TTI->getUserCost(I);
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000392
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000393 case Instruction::Ret:
394 case Instruction::PHI:
395 case Instruction::Br: {
Chandler Carruthcf569a82013-01-05 10:09:33 +0000396 return TTI->getCFInstrCost(I->getOpcode());
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000397 }
398 case Instruction::Add:
399 case Instruction::FAdd:
400 case Instruction::Sub:
401 case Instruction::FSub:
402 case Instruction::Mul:
403 case Instruction::FMul:
404 case Instruction::UDiv:
405 case Instruction::SDiv:
406 case Instruction::FDiv:
407 case Instruction::URem:
408 case Instruction::SRem:
409 case Instruction::FRem:
410 case Instruction::Shl:
411 case Instruction::LShr:
412 case Instruction::AShr:
413 case Instruction::And:
414 case Instruction::Or:
415 case Instruction::Xor: {
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000416 TargetTransformInfo::OperandValueKind Op1VK =
417 getOperandInfo(I->getOperand(0));
418 TargetTransformInfo::OperandValueKind Op2VK =
419 getOperandInfo(I->getOperand(1));
420 return TTI->getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK,
421 Op2VK);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000422 }
423 case Instruction::Select: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000424 const SelectInst *SI = cast<SelectInst>(I);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000425 Type *CondTy = SI->getCondition()->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000426 return TTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000427 }
428 case Instruction::ICmp:
429 case Instruction::FCmp: {
430 Type *ValTy = I->getOperand(0)->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000431 return TTI->getCmpSelInstrCost(I->getOpcode(), ValTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000432 }
433 case Instruction::Store: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000434 const StoreInst *SI = cast<StoreInst>(I);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000435 Type *ValTy = SI->getValueOperand()->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000436 return TTI->getMemoryOpCost(I->getOpcode(), ValTy,
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000437 SI->getAlignment(),
438 SI->getPointerAddressSpace());
439 }
440 case Instruction::Load: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000441 const LoadInst *LI = cast<LoadInst>(I);
Chandler Carruthcf569a82013-01-05 10:09:33 +0000442 return TTI->getMemoryOpCost(I->getOpcode(), I->getType(),
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000443 LI->getAlignment(),
444 LI->getPointerAddressSpace());
445 }
446 case Instruction::ZExt:
447 case Instruction::SExt:
448 case Instruction::FPToUI:
449 case Instruction::FPToSI:
450 case Instruction::FPExt:
451 case Instruction::PtrToInt:
452 case Instruction::IntToPtr:
453 case Instruction::SIToFP:
454 case Instruction::UIToFP:
455 case Instruction::Trunc:
456 case Instruction::FPTrunc:
Matt Arsenault339506d2014-01-22 20:30:16 +0000457 case Instruction::BitCast:
458 case Instruction::AddrSpaceCast: {
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000459 Type *SrcTy = I->getOperand(0)->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000460 return TTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000461 }
Nadav Rotem13da9472012-11-02 22:31:56 +0000462 case Instruction::ExtractElement: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000463 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
Nadav Rotem13da9472012-11-02 22:31:56 +0000464 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
465 unsigned Idx = -1;
466 if (CI)
467 Idx = CI->getZExtValue();
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000468
469 // Try to match a reduction sequence (series of shufflevector and vector
470 // adds followed by a extractelement).
471 unsigned ReduxOpCode;
472 Type *ReduxType;
473
474 if (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType))
475 return TTI->getReductionCost(ReduxOpCode, ReduxType, false);
476 else if (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType))
477 return TTI->getReductionCost(ReduxOpCode, ReduxType, true);
478
Chandler Carruthcf569a82013-01-05 10:09:33 +0000479 return TTI->getVectorInstrCost(I->getOpcode(),
480 EEI->getOperand(0)->getType(), Idx);
Nadav Rotem13da9472012-11-02 22:31:56 +0000481 }
482 case Instruction::InsertElement: {
Craig Topper37039642013-07-11 05:39:44 +0000483 const InsertElementInst * IE = cast<InsertElementInst>(I);
484 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
485 unsigned Idx = -1;
486 if (CI)
487 Idx = CI->getZExtValue();
488 return TTI->getVectorInstrCost(I->getOpcode(),
489 IE->getType(), Idx);
490 }
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +0000491 case Instruction::ShuffleVector: {
492 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
493 Type *VecTypOp0 = Shuffle->getOperand(0)->getType();
494 unsigned NumVecElems = VecTypOp0->getVectorNumElements();
495 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
496
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000497 if (NumVecElems == Mask.size()) {
498 if (isReverseVectorMask(Mask))
499 return TTI->getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0,
500 0, nullptr);
501 if (isAlternateVectorMask(Mask))
502 return TTI->getShuffleCost(TargetTransformInfo::SK_Alternate,
503 VecTypOp0, 0, nullptr);
504 }
505
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +0000506 return -1;
507 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000508 case Instruction::Call:
509 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
Elena Demikhovsky54946982015-12-28 20:10:59 +0000510 SmallVector<Value *, 4> Args;
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000511 for (unsigned J = 0, JE = II->getNumArgOperands(); J != JE; ++J)
Elena Demikhovsky54946982015-12-28 20:10:59 +0000512 Args.push_back(II->getArgOperand(J));
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000513
David Majnemer0f26b0a2016-04-14 07:13:24 +0000514 FastMathFlags FMF;
515 if (auto *FPMO = dyn_cast<FPMathOperator>(II))
516 FMF = FPMO->getFastMathFlags();
517
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000518 return TTI->getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
David Majnemer0f26b0a2016-04-14 07:13:24 +0000519 Args, FMF);
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000520 }
521 return -1;
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000522 default:
523 // We don't have any information on this instruction.
524 return -1;
525 }
526}
527
528void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
529 if (!F)
530 return;
531
Benjamin Krameraa209152016-06-26 17:27:42 +0000532 for (BasicBlock &B : *F) {
533 for (Instruction &Inst : B) {
534 unsigned Cost = getInstructionCost(&Inst);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000535 if (Cost != (unsigned)-1)
536 OS << "Cost Model: Found an estimated cost of " << Cost;
537 else
538 OS << "Cost Model: Unknown cost";
539
Benjamin Krameraa209152016-06-26 17:27:42 +0000540 OS << " for instruction: " << Inst << "\n";
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000541 }
542 }
543}