Nadav Rotem | a6b91ac | 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 | 99868e4 | 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 | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/TargetTransformInfo.h" |
Michael Kuperstein | 3ceac2b | 2016-08-04 22:48:03 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/VectorUtils.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
| 25 | #include "llvm/IR/Instructions.h" |
Benjamin Kramer | f7cfac7 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 26 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Value.h" |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 28 | #include "llvm/Pass.h" |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | using namespace llvm; |
| 33 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 34 | #define CM_NAME "cost-model" |
| 35 | #define DEBUG_TYPE CM_NAME |
| 36 | |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 37 | static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false), |
| 38 | cl::Hidden, |
| 39 | cl::desc("Recognize reduction patterns.")); |
| 40 | |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | class CostModelAnalysis : public FunctionPass { |
| 43 | |
| 44 | public: |
| 45 | static char ID; // Class identification, replacement for typeinfo |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 46 | CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) { |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 47 | 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 Rotem | ce5db0f | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 55 | unsigned getInstructionCost(const Instruction *I) const; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 56 | |
| 57 | private: |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 58 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 59 | bool runOnFunction(Function &F) override; |
| 60 | void print(raw_ostream &OS, const Module*) const override; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 61 | |
| 62 | /// The function that we analyze. |
| 63 | Function *F; |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 64 | /// Target information. |
| 65 | const TargetTransformInfo *TTI; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 66 | }; |
| 67 | } // End of anonymous namespace |
| 68 | |
| 69 | // Register this pass. |
| 70 | char CostModelAnalysis::ID = 0; |
| 71 | static const char cm_name[] = "Cost Model Analysis"; |
| 72 | INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true) |
| 73 | INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true) |
| 74 | |
| 75 | FunctionPass *llvm::createCostModelAnalysisPass() { |
| 76 | return new CostModelAnalysis(); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 81 | AU.setPreservesAll(); |
| 82 | } |
| 83 | |
| 84 | bool |
| 85 | CostModelAnalysis::runOnFunction(Function &F) { |
| 86 | this->F = &F; |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 87 | auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>(); |
Chandler Carruth | fdb9c57 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 88 | TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 89 | |
| 90 | return false; |
| 91 | } |
| 92 | |
Simon Pilgrim | c93cd30 | 2016-12-21 15:49:01 +0000 | [diff] [blame] | 93 | static bool isReverseVectorMask(ArrayRef<int> Mask) { |
Arnold Schwaighofer | 7e2ca6e | 2013-02-12 02:40:37 +0000 | [diff] [blame] | 94 | for (unsigned i = 0, MaskSize = Mask.size(); i < MaskSize; ++i) |
Simon Pilgrim | 9876ed0 | 2016-12-15 12:12:45 +0000 | [diff] [blame] | 95 | if (Mask[i] >= 0 && Mask[i] != (int)(MaskSize - 1 - i)) |
Arnold Schwaighofer | 7e2ca6e | 2013-02-12 02:40:37 +0000 | [diff] [blame] | 96 | return false; |
| 97 | return true; |
| 98 | } |
| 99 | |
Simon Pilgrim | c93cd30 | 2016-12-21 15:49:01 +0000 | [diff] [blame] | 100 | static bool isAlternateVectorMask(ArrayRef<int> Mask) { |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 101 | 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 Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 125 | static TargetTransformInfo::OperandValueKind getOperandInfo(Value *V) { |
| 126 | TargetTransformInfo::OperandValueKind OpInfo = |
Michael Kuperstein | 3ceac2b | 2016-08-04 22:48:03 +0000 | [diff] [blame] | 127 | TargetTransformInfo::OK_AnyValue; |
Arnold Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 128 | |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 129 | // Check for a splat of a constant or for a non uniform vector of constants. |
Benjamin Kramer | 989b929 | 2014-02-13 16:48:38 +0000 | [diff] [blame] | 130 | if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) { |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 131 | OpInfo = TargetTransformInfo::OK_NonUniformConstantValue; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 132 | if (cast<Constant>(V)->getSplatValue() != nullptr) |
Arnold Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 133 | OpInfo = TargetTransformInfo::OK_UniformConstantValue; |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 134 | } |
Arnold Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 135 | |
Michael Kuperstein | 3ceac2b | 2016-08-04 22:48:03 +0000 | [diff] [blame] | 136 | // 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 Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 142 | return OpInfo; |
| 143 | } |
| 144 | |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 145 | static 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 Kornienko | 484e48e3 | 2015-11-05 21:07:12 +0000 | [diff] [blame] | 162 | return Mask == ActualMask; |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static 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 Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 173 | if (BinOp == nullptr) |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 174 | return false; |
| 175 | |
Eric Christopher | e7af7bd | 2013-09-17 21:13:57 +0000 | [diff] [blame] | 176 | assert(BinOp->getType()->isVectorTy() && "Expecting a vector type"); |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 177 | |
| 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 Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 194 | Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr; |
| 195 | Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr; |
| 196 | Value *NextLevelOp = nullptr; |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 197 | 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 Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 221 | BinaryOperator *NextLevelBinOp = nullptr; |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 222 | 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 | |
| 246 | static 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 | |
| 295 | static std::pair<Value *, ShuffleVectorInst *> |
| 296 | getShuffleAndOtherOprd(BinaryOperator *B) { |
| 297 | |
| 298 | Value *L = B->getOperand(0); |
| 299 | Value *R = B->getOperand(1); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 300 | ShuffleVectorInst *S = nullptr; |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 301 | |
| 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 | |
| 309 | static 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 Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 357 | std::tie(NextRdxOp, Shuffle) = getShuffleAndOtherOprd(BinOp); |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 358 | |
| 359 | // Check the current reduction operation and the shuffle use the same value. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 360 | if (Shuffle == nullptr) |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 361 | 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 Kramer | 147644d | 2014-04-18 19:48:03 +0000 | [diff] [blame] | 372 | if (ShuffleMask != Mask) |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 373 | 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 Rotem | ce5db0f | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 385 | unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const { |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 386 | if (!TTI) |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 387 | return -1; |
| 388 | |
| 389 | switch (I->getOpcode()) { |
Jingyue Wu | bfefff5 | 2015-07-26 19:10:03 +0000 | [diff] [blame] | 390 | case Instruction::GetElementPtr: |
| 391 | return TTI->getUserCost(I); |
Arnold Schwaighofer | 594fa2d | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 392 | |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 393 | case Instruction::Ret: |
| 394 | case Instruction::PHI: |
| 395 | case Instruction::Br: { |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 396 | return TTI->getCFInstrCost(I->getOpcode()); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 397 | } |
| 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 Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 416 | 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 Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 422 | } |
| 423 | case Instruction::Select: { |
Nadav Rotem | ce5db0f | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 424 | const SelectInst *SI = cast<SelectInst>(I); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 425 | Type *CondTy = SI->getCondition()->getType(); |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 426 | return TTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 427 | } |
| 428 | case Instruction::ICmp: |
| 429 | case Instruction::FCmp: { |
| 430 | Type *ValTy = I->getOperand(0)->getType(); |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 431 | return TTI->getCmpSelInstrCost(I->getOpcode(), ValTy); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 432 | } |
| 433 | case Instruction::Store: { |
Nadav Rotem | ce5db0f | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 434 | const StoreInst *SI = cast<StoreInst>(I); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 435 | Type *ValTy = SI->getValueOperand()->getType(); |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 436 | return TTI->getMemoryOpCost(I->getOpcode(), ValTy, |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 437 | SI->getAlignment(), |
| 438 | SI->getPointerAddressSpace()); |
| 439 | } |
| 440 | case Instruction::Load: { |
Nadav Rotem | ce5db0f | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 441 | const LoadInst *LI = cast<LoadInst>(I); |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 442 | return TTI->getMemoryOpCost(I->getOpcode(), I->getType(), |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 443 | 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 Arsenault | 339506d | 2014-01-22 20:30:16 +0000 | [diff] [blame] | 457 | case Instruction::BitCast: |
| 458 | case Instruction::AddrSpaceCast: { |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 459 | Type *SrcTy = I->getOperand(0)->getType(); |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 460 | return TTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 461 | } |
Nadav Rotem | 13da947 | 2012-11-02 22:31:56 +0000 | [diff] [blame] | 462 | case Instruction::ExtractElement: { |
Nadav Rotem | ce5db0f | 2012-12-03 22:47:12 +0000 | [diff] [blame] | 463 | const ExtractElementInst * EEI = cast<ExtractElementInst>(I); |
Nadav Rotem | 13da947 | 2012-11-02 22:31:56 +0000 | [diff] [blame] | 464 | ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1)); |
| 465 | unsigned Idx = -1; |
| 466 | if (CI) |
| 467 | Idx = CI->getZExtValue(); |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 468 | |
| 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 Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 479 | return TTI->getVectorInstrCost(I->getOpcode(), |
| 480 | EEI->getOperand(0)->getType(), Idx); |
Nadav Rotem | 13da947 | 2012-11-02 22:31:56 +0000 | [diff] [blame] | 481 | } |
| 482 | case Instruction::InsertElement: { |
Craig Topper | 3703964 | 2013-07-11 05:39:44 +0000 | [diff] [blame] | 483 | 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 Schwaighofer | 7e2ca6e | 2013-02-12 02:40:37 +0000 | [diff] [blame] | 491 | 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 Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 497 | 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 Schwaighofer | 7e2ca6e | 2013-02-12 02:40:37 +0000 | [diff] [blame] | 506 | return -1; |
| 507 | } |
Benjamin Kramer | f7cfac7 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 508 | case Instruction::Call: |
| 509 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
Elena Demikhovsky | 5494698 | 2015-12-28 20:10:59 +0000 | [diff] [blame] | 510 | SmallVector<Value *, 4> Args; |
Benjamin Kramer | f7cfac7 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 511 | for (unsigned J = 0, JE = II->getNumArgOperands(); J != JE; ++J) |
Elena Demikhovsky | 5494698 | 2015-12-28 20:10:59 +0000 | [diff] [blame] | 512 | Args.push_back(II->getArgOperand(J)); |
Benjamin Kramer | f7cfac7 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 513 | |
David Majnemer | 0f26b0a | 2016-04-14 07:13:24 +0000 | [diff] [blame] | 514 | FastMathFlags FMF; |
| 515 | if (auto *FPMO = dyn_cast<FPMathOperator>(II)) |
| 516 | FMF = FPMO->getFastMathFlags(); |
| 517 | |
Benjamin Kramer | f7cfac7 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 518 | return TTI->getIntrinsicInstrCost(II->getIntrinsicID(), II->getType(), |
David Majnemer | 0f26b0a | 2016-04-14 07:13:24 +0000 | [diff] [blame] | 519 | Args, FMF); |
Benjamin Kramer | f7cfac7 | 2013-02-28 19:09:33 +0000 | [diff] [blame] | 520 | } |
| 521 | return -1; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 522 | default: |
| 523 | // We don't have any information on this instruction. |
| 524 | return -1; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void CostModelAnalysis::print(raw_ostream &OS, const Module*) const { |
| 529 | if (!F) |
| 530 | return; |
| 531 | |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 532 | for (BasicBlock &B : *F) { |
| 533 | for (Instruction &Inst : B) { |
| 534 | unsigned Cost = getInstructionCost(&Inst); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 535 | if (Cost != (unsigned)-1) |
| 536 | OS << "Cost Model: Found an estimated cost of " << Cost; |
| 537 | else |
| 538 | OS << "Cost Model: Unknown cost"; |
| 539 | |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 540 | OS << " for instruction: " << Inst << "\n"; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |