blob: 604cc2af74641780896df973db3bb8630ae7bb7b [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Function.h"
24#include "llvm/IR/Instructions.h"
Benjamin Kramerf7cfac72013-02-28 19:09:33 +000025#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Value.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000027#include "llvm/Pass.h"
Arnold Schwaighofercae87352013-09-17 18:06:50 +000028#include "llvm/Support/CommandLine.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
31using namespace llvm;
32
Chandler Carruthf1221bd2014-04-22 02:48:03 +000033#define CM_NAME "cost-model"
34#define DEBUG_TYPE CM_NAME
35
Arnold Schwaighofercae87352013-09-17 18:06:50 +000036static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false),
37 cl::Hidden,
38 cl::desc("Recognize reduction patterns."));
39
Nadav Rotema6b91ac2012-11-02 21:48:17 +000040namespace {
41 class CostModelAnalysis : public FunctionPass {
42
43 public:
44 static char ID; // Class identification, replacement for typeinfo
Craig Topper9f008862014-04-15 04:59:12 +000045 CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) {
Nadav Rotema6b91ac2012-11-02 21:48:17 +000046 initializeCostModelAnalysisPass(
47 *PassRegistry::getPassRegistry());
48 }
49
50 /// Returns the expected cost of the instruction.
51 /// Returns -1 if the cost is unknown.
52 /// Note, this method does not cache the cost calculation and it
53 /// can be expensive in some cases.
Nadav Rotemce5db0f2012-12-03 22:47:12 +000054 unsigned getInstructionCost(const Instruction *I) const;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000055
56 private:
Craig Toppere9ba7592014-03-05 07:30:04 +000057 void getAnalysisUsage(AnalysisUsage &AU) const override;
58 bool runOnFunction(Function &F) override;
59 void print(raw_ostream &OS, const Module*) const override;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000060
61 /// The function that we analyze.
62 Function *F;
Chandler Carruthcf569a82013-01-05 10:09:33 +000063 /// Target information.
64 const TargetTransformInfo *TTI;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000065 };
66} // End of anonymous namespace
67
68// Register this pass.
69char CostModelAnalysis::ID = 0;
70static const char cm_name[] = "Cost Model Analysis";
71INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
72INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
73
74FunctionPass *llvm::createCostModelAnalysisPass() {
75 return new CostModelAnalysis();
76}
77
78void
79CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
80 AU.setPreservesAll();
81}
82
83bool
84CostModelAnalysis::runOnFunction(Function &F) {
85 this->F = &F;
Chandler Carruth705b1852015-01-31 03:43:40 +000086 auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
Chandler Carruthfdb9c572015-02-01 12:01:35 +000087 TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000088
89 return false;
90}
91
Craig Topper2cd5ff82013-07-11 16:22:38 +000092static bool isReverseVectorMask(SmallVectorImpl<int> &Mask) {
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +000093 for (unsigned i = 0, MaskSize = Mask.size(); i < MaskSize; ++i)
94 if (Mask[i] > 0 && Mask[i] != (int)(MaskSize - 1 - i))
95 return false;
96 return true;
97}
98
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +000099static bool isAlternateVectorMask(SmallVectorImpl<int> &Mask) {
100 bool isAlternate = true;
101 unsigned MaskSize = Mask.size();
102
103 // Example: shufflevector A, B, <0,5,2,7>
104 for (unsigned i = 0; i < MaskSize && isAlternate; ++i) {
105 if (Mask[i] < 0)
106 continue;
107 isAlternate = Mask[i] == (int)((i & 1) ? MaskSize + i : i);
108 }
109
110 if (isAlternate)
111 return true;
112
113 isAlternate = true;
114 // Example: shufflevector A, B, <4,1,6,3>
115 for (unsigned i = 0; i < MaskSize && isAlternate; ++i) {
116 if (Mask[i] < 0)
117 continue;
118 isAlternate = Mask[i] == (int)((i & 1) ? i : MaskSize + i);
119 }
120
121 return isAlternate;
122}
123
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000124static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) {
125 TargetTransformInfo::OperandValueKind OpInfo =
126 TargetTransformInfo::OK_AnyValue;
127
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000128 // Check for a splat of a constant or for a non uniform vector of constants.
Benjamin Kramer989b9292014-02-13 16:48:38 +0000129 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) {
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000130 OpInfo = TargetTransformInfo::OK_NonUniformConstantValue;
Craig Topper9f008862014-04-15 04:59:12 +0000131 if (cast<Constant>(V)->getSplatValue() != nullptr)
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000132 OpInfo = TargetTransformInfo::OK_UniformConstantValue;
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000133 }
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000134
135 return OpInfo;
136}
137
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000138static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft,
139 unsigned Level) {
140 // We don't need a shuffle if we just want to have element 0 in position 0 of
141 // the vector.
142 if (!SI && Level == 0 && IsLeft)
143 return true;
144 else if (!SI)
145 return false;
146
147 SmallVector<int, 32> Mask(SI->getType()->getVectorNumElements(), -1);
148
149 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether
150 // we look at the left or right side.
151 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2)
152 Mask[i] = val;
153
154 SmallVector<int, 16> ActualMask = SI->getShuffleMask();
Benjamin Kramer147644d2014-04-18 19:48:03 +0000155 if (Mask != ActualMask)
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000156 return false;
157
158 return true;
159}
160
161static bool matchPairwiseReductionAtLevel(const BinaryOperator *BinOp,
162 unsigned Level, unsigned NumLevels) {
163 // Match one level of pairwise operations.
164 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
165 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
166 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
167 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
168 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
Craig Topper9f008862014-04-15 04:59:12 +0000169 if (BinOp == nullptr)
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000170 return false;
171
Eric Christophere7af7bd2013-09-17 21:13:57 +0000172 assert(BinOp->getType()->isVectorTy() && "Expecting a vector type");
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000173
174 unsigned Opcode = BinOp->getOpcode();
175 Value *L = BinOp->getOperand(0);
176 Value *R = BinOp->getOperand(1);
177
178 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(L);
179 if (!LS && Level)
180 return false;
181 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(R);
182 if (!RS && Level)
183 return false;
184
185 // On level 0 we can omit one shufflevector instruction.
186 if (!Level && !RS && !LS)
187 return false;
188
189 // Shuffle inputs must match.
Craig Topper9f008862014-04-15 04:59:12 +0000190 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr;
191 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr;
192 Value *NextLevelOp = nullptr;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000193 if (NextLevelOpR && NextLevelOpL) {
194 // If we have two shuffles their operands must match.
195 if (NextLevelOpL != NextLevelOpR)
196 return false;
197
198 NextLevelOp = NextLevelOpL;
199 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) {
200 // On the first level we can omit the shufflevector <0, undef,...>. So the
201 // input to the other shufflevector <1, undef> must match with one of the
202 // inputs to the current binary operation.
203 // Example:
204 // %NextLevelOpL = shufflevector %R, <1, undef ...>
205 // %BinOp = fadd %NextLevelOpL, %R
206 if (NextLevelOpL && NextLevelOpL != R)
207 return false;
208 else if (NextLevelOpR && NextLevelOpR != L)
209 return false;
210
211 NextLevelOp = NextLevelOpL ? R : L;
212 } else
213 return false;
214
215 // Check that the next levels binary operation exists and matches with the
216 // current one.
Craig Topper9f008862014-04-15 04:59:12 +0000217 BinaryOperator *NextLevelBinOp = nullptr;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000218 if (Level + 1 != NumLevels) {
219 if (!(NextLevelBinOp = dyn_cast<BinaryOperator>(NextLevelOp)))
220 return false;
221 else if (NextLevelBinOp->getOpcode() != Opcode)
222 return false;
223 }
224
225 // Shuffle mask for pairwise operation must match.
226 if (matchPairwiseShuffleMask(LS, true, Level)) {
227 if (!matchPairwiseShuffleMask(RS, false, Level))
228 return false;
229 } else if (matchPairwiseShuffleMask(RS, true, Level)) {
230 if (!matchPairwiseShuffleMask(LS, false, Level))
231 return false;
232 } else
233 return false;
234
235 if (++Level == NumLevels)
236 return true;
237
238 // Match next level.
239 return matchPairwiseReductionAtLevel(NextLevelBinOp, Level, NumLevels);
240}
241
242static bool matchPairwiseReduction(const ExtractElementInst *ReduxRoot,
243 unsigned &Opcode, Type *&Ty) {
244 if (!EnableReduxCost)
245 return false;
246
247 // Need to extract the first element.
248 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
249 unsigned Idx = ~0u;
250 if (CI)
251 Idx = CI->getZExtValue();
252 if (Idx != 0)
253 return false;
254
255 BinaryOperator *RdxStart = dyn_cast<BinaryOperator>(ReduxRoot->getOperand(0));
256 if (!RdxStart)
257 return false;
258
259 Type *VecTy = ReduxRoot->getOperand(0)->getType();
260 unsigned NumVecElems = VecTy->getVectorNumElements();
261 if (!isPowerOf2_32(NumVecElems))
262 return false;
263
264 // We look for a sequence of shuffle,shuffle,add triples like the following
265 // that builds a pairwise reduction tree.
266 //
267 // (X0, X1, X2, X3)
268 // (X0 + X1, X2 + X3, undef, undef)
269 // ((X0 + X1) + (X2 + X3), undef, undef, undef)
270 //
271 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef,
272 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef>
273 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef,
274 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef>
275 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1
276 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
277 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
278 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef,
279 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
280 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1
281 // %r = extractelement <4 x float> %bin.rdx8, i32 0
282 if (!matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)))
283 return false;
284
285 Opcode = RdxStart->getOpcode();
286 Ty = VecTy;
287
288 return true;
289}
290
291static std::pair<Value *, ShuffleVectorInst *>
292getShuffleAndOtherOprd(BinaryOperator *B) {
293
294 Value *L = B->getOperand(0);
295 Value *R = B->getOperand(1);
Craig Topper9f008862014-04-15 04:59:12 +0000296 ShuffleVectorInst *S = nullptr;
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000297
298 if ((S = dyn_cast<ShuffleVectorInst>(L)))
299 return std::make_pair(R, S);
300
301 S = dyn_cast<ShuffleVectorInst>(R);
302 return std::make_pair(L, S);
303}
304
305static bool matchVectorSplittingReduction(const ExtractElementInst *ReduxRoot,
306 unsigned &Opcode, Type *&Ty) {
307 if (!EnableReduxCost)
308 return false;
309
310 // Need to extract the first element.
311 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1));
312 unsigned Idx = ~0u;
313 if (CI)
314 Idx = CI->getZExtValue();
315 if (Idx != 0)
316 return false;
317
318 BinaryOperator *RdxStart = dyn_cast<BinaryOperator>(ReduxRoot->getOperand(0));
319 if (!RdxStart)
320 return false;
321 unsigned RdxOpcode = RdxStart->getOpcode();
322
323 Type *VecTy = ReduxRoot->getOperand(0)->getType();
324 unsigned NumVecElems = VecTy->getVectorNumElements();
325 if (!isPowerOf2_32(NumVecElems))
326 return false;
327
328 // We look for a sequence of shuffles and adds like the following matching one
329 // fadd, shuffle vector pair at a time.
330 //
331 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef,
332 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef>
333 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf
334 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef,
335 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
336 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7
337 // %r = extractelement <4 x float> %bin.rdx8, i32 0
338
339 unsigned MaskStart = 1;
340 Value *RdxOp = RdxStart;
341 SmallVector<int, 32> ShuffleMask(NumVecElems, 0);
342 unsigned NumVecElemsRemain = NumVecElems;
343 while (NumVecElemsRemain - 1) {
344 // Check for the right reduction operation.
345 BinaryOperator *BinOp;
346 if (!(BinOp = dyn_cast<BinaryOperator>(RdxOp)))
347 return false;
348 if (BinOp->getOpcode() != RdxOpcode)
349 return false;
350
351 Value *NextRdxOp;
352 ShuffleVectorInst *Shuffle;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000353 std::tie(NextRdxOp, Shuffle) = getShuffleAndOtherOprd(BinOp);
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000354
355 // Check the current reduction operation and the shuffle use the same value.
Craig Topper9f008862014-04-15 04:59:12 +0000356 if (Shuffle == nullptr)
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000357 return false;
358 if (Shuffle->getOperand(0) != NextRdxOp)
359 return false;
360
361 // Check that shuffle masks matches.
362 for (unsigned j = 0; j != MaskStart; ++j)
363 ShuffleMask[j] = MaskStart + j;
364 // Fill the rest of the mask with -1 for undef.
365 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1);
366
367 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
Benjamin Kramer147644d2014-04-18 19:48:03 +0000368 if (ShuffleMask != Mask)
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000369 return false;
370
371 RdxOp = NextRdxOp;
372 NumVecElemsRemain /= 2;
373 MaskStart *= 2;
374 }
375
376 Opcode = RdxOpcode;
377 Ty = VecTy;
378 return true;
379}
380
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000381unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const {
Chandler Carruthcf569a82013-01-05 10:09:33 +0000382 if (!TTI)
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000383 return -1;
384
385 switch (I->getOpcode()) {
Jingyue Wubfefff52015-07-26 19:10:03 +0000386 case Instruction::GetElementPtr:
387 return TTI->getUserCost(I);
Arnold Schwaighofer594fa2d2013-02-08 14:50:48 +0000388
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000389 case Instruction::Ret:
390 case Instruction::PHI:
391 case Instruction::Br: {
Chandler Carruthcf569a82013-01-05 10:09:33 +0000392 return TTI->getCFInstrCost(I->getOpcode());
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000393 }
394 case Instruction::Add:
395 case Instruction::FAdd:
396 case Instruction::Sub:
397 case Instruction::FSub:
398 case Instruction::Mul:
399 case Instruction::FMul:
400 case Instruction::UDiv:
401 case Instruction::SDiv:
402 case Instruction::FDiv:
403 case Instruction::URem:
404 case Instruction::SRem:
405 case Instruction::FRem:
406 case Instruction::Shl:
407 case Instruction::LShr:
408 case Instruction::AShr:
409 case Instruction::And:
410 case Instruction::Or:
411 case Instruction::Xor: {
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000412 TargetTransformInfo::OperandValueKind Op1VK =
413 getOperandInfo(I->getOperand(0));
414 TargetTransformInfo::OperandValueKind Op2VK =
415 getOperandInfo(I->getOperand(1));
416 return TTI->getArithmeticInstrCost(I->getOpcode(), I->getType(), Op1VK,
417 Op2VK);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000418 }
419 case Instruction::Select: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000420 const SelectInst *SI = cast<SelectInst>(I);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000421 Type *CondTy = SI->getCondition()->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000422 return TTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000423 }
424 case Instruction::ICmp:
425 case Instruction::FCmp: {
426 Type *ValTy = I->getOperand(0)->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000427 return TTI->getCmpSelInstrCost(I->getOpcode(), ValTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000428 }
429 case Instruction::Store: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000430 const StoreInst *SI = cast<StoreInst>(I);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000431 Type *ValTy = SI->getValueOperand()->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000432 return TTI->getMemoryOpCost(I->getOpcode(), ValTy,
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000433 SI->getAlignment(),
434 SI->getPointerAddressSpace());
435 }
436 case Instruction::Load: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000437 const LoadInst *LI = cast<LoadInst>(I);
Chandler Carruthcf569a82013-01-05 10:09:33 +0000438 return TTI->getMemoryOpCost(I->getOpcode(), I->getType(),
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000439 LI->getAlignment(),
440 LI->getPointerAddressSpace());
441 }
442 case Instruction::ZExt:
443 case Instruction::SExt:
444 case Instruction::FPToUI:
445 case Instruction::FPToSI:
446 case Instruction::FPExt:
447 case Instruction::PtrToInt:
448 case Instruction::IntToPtr:
449 case Instruction::SIToFP:
450 case Instruction::UIToFP:
451 case Instruction::Trunc:
452 case Instruction::FPTrunc:
Matt Arsenault339506d2014-01-22 20:30:16 +0000453 case Instruction::BitCast:
454 case Instruction::AddrSpaceCast: {
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000455 Type *SrcTy = I->getOperand(0)->getType();
Chandler Carruthcf569a82013-01-05 10:09:33 +0000456 return TTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000457 }
Nadav Rotem13da9472012-11-02 22:31:56 +0000458 case Instruction::ExtractElement: {
Nadav Rotemce5db0f2012-12-03 22:47:12 +0000459 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
Nadav Rotem13da9472012-11-02 22:31:56 +0000460 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
461 unsigned Idx = -1;
462 if (CI)
463 Idx = CI->getZExtValue();
Arnold Schwaighofercae87352013-09-17 18:06:50 +0000464
465 // Try to match a reduction sequence (series of shufflevector and vector
466 // adds followed by a extractelement).
467 unsigned ReduxOpCode;
468 Type *ReduxType;
469
470 if (matchVectorSplittingReduction(EEI, ReduxOpCode, ReduxType))
471 return TTI->getReductionCost(ReduxOpCode, ReduxType, false);
472 else if (matchPairwiseReduction(EEI, ReduxOpCode, ReduxType))
473 return TTI->getReductionCost(ReduxOpCode, ReduxType, true);
474
Chandler Carruthcf569a82013-01-05 10:09:33 +0000475 return TTI->getVectorInstrCost(I->getOpcode(),
476 EEI->getOperand(0)->getType(), Idx);
Nadav Rotem13da9472012-11-02 22:31:56 +0000477 }
478 case Instruction::InsertElement: {
Craig Topper37039642013-07-11 05:39:44 +0000479 const InsertElementInst * IE = cast<InsertElementInst>(I);
480 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
481 unsigned Idx = -1;
482 if (CI)
483 Idx = CI->getZExtValue();
484 return TTI->getVectorInstrCost(I->getOpcode(),
485 IE->getType(), Idx);
486 }
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +0000487 case Instruction::ShuffleVector: {
488 const ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I);
489 Type *VecTypOp0 = Shuffle->getOperand(0)->getType();
490 unsigned NumVecElems = VecTypOp0->getVectorNumElements();
491 SmallVector<int, 16> Mask = Shuffle->getShuffleMask();
492
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000493 if (NumVecElems == Mask.size()) {
494 if (isReverseVectorMask(Mask))
495 return TTI->getShuffleCost(TargetTransformInfo::SK_Reverse, VecTypOp0,
496 0, nullptr);
497 if (isAlternateVectorMask(Mask))
498 return TTI->getShuffleCost(TargetTransformInfo::SK_Alternate,
499 VecTypOp0, 0, nullptr);
500 }
501
Arnold Schwaighofer7e2ca6e2013-02-12 02:40:37 +0000502 return -1;
503 }
Benjamin Kramerf7cfac72013-02-28 19:09:33 +0000504 case Instruction::Call:
505 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
506 SmallVector<Type*, 4> Tys;
507 for (unsigned J = 0, JE = II->getNumArgOperands(); J != JE; ++J)
508 Tys.push_back(II->getArgOperand(J)->getType());
509
510 return TTI->getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(),
511 Tys);
512 }
513 return -1;
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000514 default:
515 // We don't have any information on this instruction.
516 return -1;
517 }
518}
519
520void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
521 if (!F)
522 return;
523
524 for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) {
525 for (BasicBlock::iterator it = B->begin(), e = B->end(); it != e; ++it) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000526 Instruction *Inst = &*it;
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000527 unsigned Cost = getInstructionCost(Inst);
528 if (Cost != (unsigned)-1)
529 OS << "Cost Model: Found an estimated cost of " << Cost;
530 else
531 OS << "Cost Model: Unknown cost";
532
533 OS << " for instruction: "<< *Inst << "\n";
534 }
535 }
536}