Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 1 | //===-- X86TargetTransformInfo.cpp - X86 specific TTI pass ----------------===// |
| 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 | /// \file |
| 10 | /// This file implements a TargetTransformInfo analysis pass specific to the |
| 11 | /// X86 target machine. It uses the target's detailed information to provide |
| 12 | /// more precise answers to certain TTI queries, while letting the target |
| 13 | /// independent and default TTI implementations handle the rest. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 17 | #include "X86TargetTransformInfo.h" |
Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/BasicTTIImpl.h" |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 20 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Renato Golin | d4c392e | 2013-01-24 23:01:00 +0000 | [diff] [blame] | 22 | #include "llvm/Target/CostTable.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetLowering.h" |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 24 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "x86tti" |
| 28 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // |
| 31 | // X86 cost model. |
| 32 | // |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 35 | TargetTransformInfo::PopcntSupportKind |
| 36 | X86TTIImpl::getPopcntSupport(unsigned TyWidth) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 37 | assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2"); |
| 38 | // TODO: Currently the __builtin_popcount() implementation using SSE3 |
| 39 | // instructions is inefficient. Once the problem is fixed, we should |
Craig Topper | 0a63e1d | 2013-09-08 00:47:31 +0000 | [diff] [blame] | 40 | // call ST->hasSSE3() instead of ST->hasPOPCNT(). |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 41 | return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 44 | unsigned X86TTIImpl::getNumberOfRegisters(bool Vector) { |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 45 | if (Vector && !ST->hasSSE1()) |
| 46 | return 0; |
| 47 | |
Adam Nemet | 2820a5b | 2014-07-09 18:22:33 +0000 | [diff] [blame] | 48 | if (ST->is64Bit()) { |
| 49 | if (Vector && ST->hasAVX512()) |
| 50 | return 32; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 51 | return 16; |
Adam Nemet | 2820a5b | 2014-07-09 18:22:33 +0000 | [diff] [blame] | 52 | } |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 53 | return 8; |
| 54 | } |
| 55 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 56 | unsigned X86TTIImpl::getRegisterBitWidth(bool Vector) { |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 57 | if (Vector) { |
Adam Nemet | 2820a5b | 2014-07-09 18:22:33 +0000 | [diff] [blame] | 58 | if (ST->hasAVX512()) return 512; |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 59 | if (ST->hasAVX()) return 256; |
| 60 | if (ST->hasSSE1()) return 128; |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | if (ST->is64Bit()) |
| 65 | return 64; |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 66 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 67 | return 32; |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Wei Mi | 062c744 | 2015-05-06 17:12:25 +0000 | [diff] [blame] | 70 | unsigned X86TTIImpl::getMaxInterleaveFactor(unsigned VF) { |
| 71 | // If the loop will not be vectorized, don't interleave the loop. |
| 72 | // Let regular unroll to unroll the loop, which saves the overflow |
| 73 | // check and memory check cost. |
| 74 | if (VF == 1) |
| 75 | return 1; |
| 76 | |
Nadav Rotem | b696c36 | 2013-01-09 01:15:42 +0000 | [diff] [blame] | 77 | if (ST->isAtom()) |
| 78 | return 1; |
| 79 | |
| 80 | // Sandybridge and Haswell have multiple execution ports and pipelined |
| 81 | // vector units. |
| 82 | if (ST->hasAVX()) |
| 83 | return 4; |
| 84 | |
| 85 | return 2; |
| 86 | } |
| 87 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 88 | int X86TTIImpl::getArithmeticInstrCost( |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 89 | unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info, |
| 90 | TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo, |
| 91 | TTI::OperandValueProperties Opd2PropInfo) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 92 | // Legalize the type. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 93 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 94 | |
| 95 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 96 | assert(ISD && "Invalid opcode"); |
| 97 | |
Karthik Bhat | 7f33ff7 | 2014-08-25 04:56:54 +0000 | [diff] [blame] | 98 | if (ISD == ISD::SDIV && |
| 99 | Op2Info == TargetTransformInfo::OK_UniformConstantValue && |
| 100 | Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) { |
| 101 | // On X86, vector signed division by constants power-of-two are |
| 102 | // normally expanded to the sequence SRA + SRL + ADD + SRA. |
| 103 | // The OperandValue properties many not be same as that of previous |
| 104 | // operation;conservatively assume OP_None. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 105 | int Cost = 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info, |
| 106 | Op2Info, TargetTransformInfo::OP_None, |
| 107 | TargetTransformInfo::OP_None); |
Karthik Bhat | 7f33ff7 | 2014-08-25 04:56:54 +0000 | [diff] [blame] | 108 | Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info, |
| 109 | TargetTransformInfo::OP_None, |
| 110 | TargetTransformInfo::OP_None); |
| 111 | Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info, |
| 112 | TargetTransformInfo::OP_None, |
| 113 | TargetTransformInfo::OP_None); |
| 114 | |
| 115 | return Cost; |
| 116 | } |
| 117 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 118 | static const CostTblEntry AVX2UniformConstCostTable[] = { |
Simon Pilgrim | 8fbf1c1 | 2015-07-06 22:35:19 +0000 | [diff] [blame] | 119 | { ISD::SRA, MVT::v4i64, 4 }, // 2 x psrad + shuffle. |
| 120 | |
Benjamin Kramer | 7c37227 | 2014-04-26 14:53:05 +0000 | [diff] [blame] | 121 | { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence |
| 122 | { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence |
| 123 | { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence |
| 124 | { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence |
| 125 | }; |
| 126 | |
| 127 | if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && |
| 128 | ST->hasAVX2()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 129 | if (const auto *Entry = CostTableLookup(AVX2UniformConstCostTable, ISD, |
| 130 | LT.second)) |
| 131 | return LT.first * Entry->Cost; |
Benjamin Kramer | 7c37227 | 2014-04-26 14:53:05 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 134 | static const CostTblEntry AVX512CostTable[] = { |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 135 | { ISD::SHL, MVT::v16i32, 1 }, |
| 136 | { ISD::SRL, MVT::v16i32, 1 }, |
| 137 | { ISD::SRA, MVT::v16i32, 1 }, |
| 138 | { ISD::SHL, MVT::v8i64, 1 }, |
| 139 | { ISD::SRL, MVT::v8i64, 1 }, |
| 140 | { ISD::SRA, MVT::v8i64, 1 }, |
| 141 | }; |
| 142 | |
Simon Pilgrim | 3d11c99 | 2015-09-30 08:17:50 +0000 | [diff] [blame] | 143 | if (ST->hasAVX512()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 144 | if (const auto *Entry = CostTableLookup(AVX512CostTable, ISD, LT.second)) |
| 145 | return LT.first * Entry->Cost; |
Simon Pilgrim | 3d11c99 | 2015-09-30 08:17:50 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 148 | static const CostTblEntry AVX2CostTable[] = { |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 149 | // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to |
| 150 | // customize them to detect the cases where shift amount is a scalar one. |
| 151 | { ISD::SHL, MVT::v4i32, 1 }, |
| 152 | { ISD::SRL, MVT::v4i32, 1 }, |
| 153 | { ISD::SRA, MVT::v4i32, 1 }, |
| 154 | { ISD::SHL, MVT::v8i32, 1 }, |
| 155 | { ISD::SRL, MVT::v8i32, 1 }, |
| 156 | { ISD::SRA, MVT::v8i32, 1 }, |
| 157 | { ISD::SHL, MVT::v2i64, 1 }, |
| 158 | { ISD::SRL, MVT::v2i64, 1 }, |
| 159 | { ISD::SHL, MVT::v4i64, 1 }, |
| 160 | { ISD::SRL, MVT::v4i64, 1 }, |
Simon Pilgrim | 3d11c99 | 2015-09-30 08:17:50 +0000 | [diff] [blame] | 161 | }; |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 162 | |
Simon Pilgrim | 3d11c99 | 2015-09-30 08:17:50 +0000 | [diff] [blame] | 163 | // Look for AVX2 lowering tricks. |
| 164 | if (ST->hasAVX2()) { |
| 165 | if (ISD == ISD::SHL && LT.second == MVT::v16i16 && |
| 166 | (Op2Info == TargetTransformInfo::OK_UniformConstantValue || |
| 167 | Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)) |
| 168 | // On AVX2, a packed v16i16 shift left by a constant build_vector |
| 169 | // is lowered into a vector multiply (vpmullw). |
| 170 | return LT.first; |
| 171 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 172 | if (const auto *Entry = CostTableLookup(AVX2CostTable, ISD, LT.second)) |
| 173 | return LT.first * Entry->Cost; |
Simon Pilgrim | 3d11c99 | 2015-09-30 08:17:50 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 176 | static const CostTblEntry XOPCostTable[] = { |
Simon Pilgrim | 3d11c99 | 2015-09-30 08:17:50 +0000 | [diff] [blame] | 177 | // 128bit shifts take 1cy, but right shifts require negation beforehand. |
| 178 | { ISD::SHL, MVT::v16i8, 1 }, |
| 179 | { ISD::SRL, MVT::v16i8, 2 }, |
| 180 | { ISD::SRA, MVT::v16i8, 2 }, |
| 181 | { ISD::SHL, MVT::v8i16, 1 }, |
| 182 | { ISD::SRL, MVT::v8i16, 2 }, |
| 183 | { ISD::SRA, MVT::v8i16, 2 }, |
| 184 | { ISD::SHL, MVT::v4i32, 1 }, |
| 185 | { ISD::SRL, MVT::v4i32, 2 }, |
| 186 | { ISD::SRA, MVT::v4i32, 2 }, |
| 187 | { ISD::SHL, MVT::v2i64, 1 }, |
| 188 | { ISD::SRL, MVT::v2i64, 2 }, |
| 189 | { ISD::SRA, MVT::v2i64, 2 }, |
| 190 | // 256bit shifts require splitting if AVX2 didn't catch them above. |
| 191 | { ISD::SHL, MVT::v32i8, 2 }, |
| 192 | { ISD::SRL, MVT::v32i8, 4 }, |
| 193 | { ISD::SRA, MVT::v32i8, 4 }, |
| 194 | { ISD::SHL, MVT::v16i16, 2 }, |
| 195 | { ISD::SRL, MVT::v16i16, 4 }, |
| 196 | { ISD::SRA, MVT::v16i16, 4 }, |
| 197 | { ISD::SHL, MVT::v8i32, 2 }, |
| 198 | { ISD::SRL, MVT::v8i32, 4 }, |
| 199 | { ISD::SRA, MVT::v8i32, 4 }, |
| 200 | { ISD::SHL, MVT::v4i64, 2 }, |
| 201 | { ISD::SRL, MVT::v4i64, 4 }, |
| 202 | { ISD::SRA, MVT::v4i64, 4 }, |
| 203 | }; |
| 204 | |
| 205 | // Look for XOP lowering tricks. |
| 206 | if (ST->hasXOP()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 207 | if (const auto *Entry = CostTableLookup(XOPCostTable, ISD, LT.second)) |
| 208 | return LT.first * Entry->Cost; |
Simon Pilgrim | 3d11c99 | 2015-09-30 08:17:50 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 211 | static const CostTblEntry AVX2CustomCostTable[] = { |
Simon Pilgrim | 5965680 | 2015-06-11 07:46:37 +0000 | [diff] [blame] | 212 | { ISD::SHL, MVT::v32i8, 11 }, // vpblendvb sequence. |
Simon Pilgrim | 0be4fa7 | 2015-05-25 17:49:13 +0000 | [diff] [blame] | 213 | { ISD::SHL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence. |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 214 | |
Simon Pilgrim | 5965680 | 2015-06-11 07:46:37 +0000 | [diff] [blame] | 215 | { ISD::SRL, MVT::v32i8, 11 }, // vpblendvb sequence. |
Simon Pilgrim | 0be4fa7 | 2015-05-25 17:49:13 +0000 | [diff] [blame] | 216 | { ISD::SRL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence. |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 217 | |
Simon Pilgrim | 5965680 | 2015-06-11 07:46:37 +0000 | [diff] [blame] | 218 | { ISD::SRA, MVT::v32i8, 24 }, // vpblendvb sequence. |
Simon Pilgrim | 0be4fa7 | 2015-05-25 17:49:13 +0000 | [diff] [blame] | 219 | { ISD::SRA, MVT::v16i16, 10 }, // extend/vpsravd/pack sequence. |
Simon Pilgrim | 86478c6 | 2015-07-29 20:31:45 +0000 | [diff] [blame] | 220 | { ISD::SRA, MVT::v2i64, 4 }, // srl/xor/sub sequence. |
| 221 | { ISD::SRA, MVT::v4i64, 4 }, // srl/xor/sub sequence. |
Arnold Schwaighofer | a04b9ef | 2013-06-25 19:14:09 +0000 | [diff] [blame] | 222 | |
| 223 | // Vectorizing division is a bad idea. See the SSE2 table for more comments. |
| 224 | { ISD::SDIV, MVT::v32i8, 32*20 }, |
| 225 | { ISD::SDIV, MVT::v16i16, 16*20 }, |
| 226 | { ISD::SDIV, MVT::v8i32, 8*20 }, |
| 227 | { ISD::SDIV, MVT::v4i64, 4*20 }, |
| 228 | { ISD::UDIV, MVT::v32i8, 32*20 }, |
| 229 | { ISD::UDIV, MVT::v16i16, 16*20 }, |
| 230 | { ISD::UDIV, MVT::v8i32, 8*20 }, |
| 231 | { ISD::UDIV, MVT::v4i64, 4*20 }, |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
Simon Pilgrim | 3d11c99 | 2015-09-30 08:17:50 +0000 | [diff] [blame] | 234 | // Look for AVX2 lowering tricks for custom cases. |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 235 | if (ST->hasAVX2()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 236 | if (const auto *Entry = CostTableLookup(AVX2CustomCostTable, ISD, |
| 237 | LT.second)) |
| 238 | return LT.first * Entry->Cost; |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 241 | static const CostTblEntry |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 242 | SSE2UniformConstCostTable[] = { |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 243 | // We don't correctly identify costs of casts because they are marked as |
| 244 | // custom. |
| 245 | // Constant splats are cheaper for the following instructions. |
| 246 | { ISD::SHL, MVT::v16i8, 1 }, // psllw. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 247 | { ISD::SHL, MVT::v32i8, 2 }, // psllw. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 248 | { ISD::SHL, MVT::v8i16, 1 }, // psllw. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 249 | { ISD::SHL, MVT::v16i16, 2 }, // psllw. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 250 | { ISD::SHL, MVT::v4i32, 1 }, // pslld |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 251 | { ISD::SHL, MVT::v8i32, 2 }, // pslld |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 252 | { ISD::SHL, MVT::v2i64, 1 }, // psllq. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 253 | { ISD::SHL, MVT::v4i64, 2 }, // psllq. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 254 | |
| 255 | { ISD::SRL, MVT::v16i8, 1 }, // psrlw. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 256 | { ISD::SRL, MVT::v32i8, 2 }, // psrlw. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 257 | { ISD::SRL, MVT::v8i16, 1 }, // psrlw. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 258 | { ISD::SRL, MVT::v16i16, 2 }, // psrlw. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 259 | { ISD::SRL, MVT::v4i32, 1 }, // psrld. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 260 | { ISD::SRL, MVT::v8i32, 2 }, // psrld. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 261 | { ISD::SRL, MVT::v2i64, 1 }, // psrlq. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 262 | { ISD::SRL, MVT::v4i64, 2 }, // psrlq. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 263 | |
| 264 | { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 265 | { ISD::SRA, MVT::v32i8, 8 }, // psrlw, pand, pxor, psubb. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 266 | { ISD::SRA, MVT::v8i16, 1 }, // psraw. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 267 | { ISD::SRA, MVT::v16i16, 2 }, // psraw. |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 268 | { ISD::SRA, MVT::v4i32, 1 }, // psrad. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 269 | { ISD::SRA, MVT::v8i32, 2 }, // psrad. |
Simon Pilgrim | 8fbf1c1 | 2015-07-06 22:35:19 +0000 | [diff] [blame] | 270 | { ISD::SRA, MVT::v2i64, 4 }, // 2 x psrad + shuffle. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 271 | { ISD::SRA, MVT::v4i64, 8 }, // 2 x psrad + shuffle. |
Benjamin Kramer | 7c37227 | 2014-04-26 14:53:05 +0000 | [diff] [blame] | 272 | |
| 273 | { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence |
| 274 | { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence |
Benjamin Kramer | ce4b3fe | 2014-04-27 18:47:54 +0000 | [diff] [blame] | 275 | { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence |
Benjamin Kramer | 7c37227 | 2014-04-26 14:53:05 +0000 | [diff] [blame] | 276 | { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && |
| 280 | ST->hasSSE2()) { |
Benjamin Kramer | ce4b3fe | 2014-04-27 18:47:54 +0000 | [diff] [blame] | 281 | // pmuldq sequence. |
| 282 | if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41()) |
| 283 | return LT.first * 15; |
| 284 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 285 | if (const auto *Entry = CostTableLookup(SSE2UniformConstCostTable, ISD, |
| 286 | LT.second)) |
| 287 | return LT.first * Entry->Cost; |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 290 | if (ISD == ISD::SHL && |
| 291 | Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) { |
Craig Topper | eda02a9 | 2015-10-25 03:15:29 +0000 | [diff] [blame] | 292 | MVT VT = LT.second; |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 293 | // Vector shift left by non uniform constant can be lowered |
| 294 | // into vector multiply (pmullw/pmulld). |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 295 | if ((VT == MVT::v8i16 && ST->hasSSE2()) || |
| 296 | (VT == MVT::v4i32 && ST->hasSSE41())) |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 297 | return LT.first; |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 298 | |
| 299 | // v16i16 and v8i32 shifts by non-uniform constants are lowered into a |
| 300 | // sequence of extract + two vector multiply + insert. |
| 301 | if ((VT == MVT::v8i32 || VT == MVT::v16i16) && |
| 302 | (ST->hasAVX() && !ST->hasAVX2())) |
| 303 | ISD = ISD::MUL; |
| 304 | |
| 305 | // A vector shift left by non uniform constant is converted |
| 306 | // into a vector multiply; the new multiply is eventually |
| 307 | // lowered into a sequence of shuffles and 2 x pmuludq. |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 308 | if (VT == MVT::v4i32 && ST->hasSSE2()) |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 309 | ISD = ISD::MUL; |
| 310 | } |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 311 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 312 | static const CostTblEntry SSE2CostTable[] = { |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 313 | // We don't correctly identify costs of casts because they are marked as |
| 314 | // custom. |
| 315 | // For some cases, where the shift amount is a scalar we would be able |
| 316 | // to generate better code. Unfortunately, when this is the case the value |
| 317 | // (the splat) will get hoisted out of the loop, thereby making it invisible |
| 318 | // to ISel. The cost model must return worst case assumptions because it is |
| 319 | // used for vectorization and we don't want to make vectorized code worse |
| 320 | // than scalar code. |
Simon Pilgrim | 5965680 | 2015-06-11 07:46:37 +0000 | [diff] [blame] | 321 | { ISD::SHL, MVT::v16i8, 26 }, // cmpgtb sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 322 | { ISD::SHL, MVT::v32i8, 2*26 }, // cmpgtb sequence. |
Simon Pilgrim | 5965680 | 2015-06-11 07:46:37 +0000 | [diff] [blame] | 323 | { ISD::SHL, MVT::v8i16, 32 }, // cmpgtb sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 324 | { ISD::SHL, MVT::v16i16, 2*32 }, // cmpgtb sequence. |
Simon Pilgrim | 5965680 | 2015-06-11 07:46:37 +0000 | [diff] [blame] | 325 | { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 326 | { ISD::SHL, MVT::v8i32, 2*2*5 }, // We optimized this using mul. |
Simon Pilgrim | 59764dc | 2015-07-18 20:06:30 +0000 | [diff] [blame] | 327 | { ISD::SHL, MVT::v2i64, 4 }, // splat+shuffle sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 328 | { ISD::SHL, MVT::v4i64, 2*4 }, // splat+shuffle sequence. |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 329 | |
| 330 | { ISD::SRL, MVT::v16i8, 26 }, // cmpgtb sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 331 | { ISD::SRL, MVT::v32i8, 2*26 }, // cmpgtb sequence. |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 332 | { ISD::SRL, MVT::v8i16, 32 }, // cmpgtb sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 333 | { ISD::SRL, MVT::v16i16, 2*32 }, // cmpgtb sequence. |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 334 | { ISD::SRL, MVT::v4i32, 16 }, // Shift each lane + blend. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 335 | { ISD::SRL, MVT::v8i32, 2*16 }, // Shift each lane + blend. |
Simon Pilgrim | 59764dc | 2015-07-18 20:06:30 +0000 | [diff] [blame] | 336 | { ISD::SRL, MVT::v2i64, 4 }, // splat+shuffle sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 337 | { ISD::SRL, MVT::v4i64, 2*4 }, // splat+shuffle sequence. |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 338 | |
| 339 | { ISD::SRA, MVT::v16i8, 54 }, // unpacked cmpgtb sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 340 | { ISD::SRA, MVT::v32i8, 2*54 }, // unpacked cmpgtb sequence. |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 341 | { ISD::SRA, MVT::v8i16, 32 }, // cmpgtb sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 342 | { ISD::SRA, MVT::v16i16, 2*32 }, // cmpgtb sequence. |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 343 | { ISD::SRA, MVT::v4i32, 16 }, // Shift each lane + blend. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 344 | { ISD::SRA, MVT::v8i32, 2*16 }, // Shift each lane + blend. |
Simon Pilgrim | 86478c6 | 2015-07-29 20:31:45 +0000 | [diff] [blame] | 345 | { ISD::SRA, MVT::v2i64, 12 }, // srl/xor/sub sequence. |
Simon Pilgrim | a18ae9b | 2015-10-17 13:23:38 +0000 | [diff] [blame] | 346 | { ISD::SRA, MVT::v4i64, 2*12 }, // srl/xor/sub sequence. |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 347 | |
| 348 | // It is not a good idea to vectorize division. We have to scalarize it and |
Arnold Schwaighofer | a04b9ef | 2013-06-25 19:14:09 +0000 | [diff] [blame] | 349 | // in the process we will often end up having to spilling regular |
| 350 | // registers. The overhead of division is going to dominate most kernels |
| 351 | // anyways so try hard to prevent vectorization of division - it is |
| 352 | // generally a bad idea. Assume somewhat arbitrarily that we have to be able |
| 353 | // to hide "20 cycles" for each lane. |
| 354 | { ISD::SDIV, MVT::v16i8, 16*20 }, |
| 355 | { ISD::SDIV, MVT::v8i16, 8*20 }, |
| 356 | { ISD::SDIV, MVT::v4i32, 4*20 }, |
| 357 | { ISD::SDIV, MVT::v2i64, 2*20 }, |
| 358 | { ISD::UDIV, MVT::v16i8, 16*20 }, |
| 359 | { ISD::UDIV, MVT::v8i16, 8*20 }, |
| 360 | { ISD::UDIV, MVT::v4i32, 4*20 }, |
| 361 | { ISD::UDIV, MVT::v2i64, 2*20 }, |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | if (ST->hasSSE2()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 365 | if (const auto *Entry = CostTableLookup(SSE2CostTable, ISD, LT.second)) |
| 366 | return LT.first * Entry->Cost; |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 369 | static const CostTblEntry AVX1CostTable[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 370 | // We don't have to scalarize unsupported ops. We can issue two half-sized |
| 371 | // operations and we only need to extract the upper YMM half. |
| 372 | // Two ops + 1 extract + 1 insert = 4. |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 373 | { ISD::MUL, MVT::v16i16, 4 }, |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 374 | { ISD::MUL, MVT::v8i32, 4 }, |
| 375 | { ISD::SUB, MVT::v8i32, 4 }, |
| 376 | { ISD::ADD, MVT::v8i32, 4 }, |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 377 | { ISD::SUB, MVT::v4i64, 4 }, |
| 378 | { ISD::ADD, MVT::v4i64, 4 }, |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 379 | // A v4i64 multiply is custom lowered as two split v2i64 vectors that then |
| 380 | // are lowered as a series of long multiplies(3), shifts(4) and adds(2) |
| 381 | // Because we believe v4i64 to be a legal type, we must also include the |
| 382 | // split factor of two in the cost table. Therefore, the cost here is 18 |
| 383 | // instead of 9. |
| 384 | { ISD::MUL, MVT::v4i64, 18 }, |
| 385 | }; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 386 | |
| 387 | // Look for AVX1 lowering tricks. |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 388 | if (ST->hasAVX() && !ST->hasAVX2()) { |
Craig Topper | eda02a9 | 2015-10-25 03:15:29 +0000 | [diff] [blame] | 389 | MVT VT = LT.second; |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 390 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 391 | if (const auto *Entry = CostTableLookup(AVX1CostTable, ISD, VT)) |
| 392 | return LT.first * Entry->Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 393 | } |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 394 | |
| 395 | // Custom lowering of vectors. |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 396 | static const CostTblEntry CustomLowered[] = { |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 397 | // A v2i64/v4i64 and multiply is custom lowered as a series of long |
| 398 | // multiplies(3), shifts(4) and adds(2). |
| 399 | { ISD::MUL, MVT::v2i64, 9 }, |
| 400 | { ISD::MUL, MVT::v4i64, 9 }, |
| 401 | }; |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 402 | if (const auto *Entry = CostTableLookup(CustomLowered, ISD, LT.second)) |
| 403 | return LT.first * Entry->Cost; |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 404 | |
| 405 | // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle, |
| 406 | // 2x pmuludq, 2x shuffle. |
| 407 | if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() && |
| 408 | !ST->hasSSE41()) |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 409 | return LT.first * 6; |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 410 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 411 | // Fallback to the default implementation. |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 412 | return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 415 | int X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, |
| 416 | Type *SubTp) { |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 417 | // We only estimate the cost of reverse and alternate shuffles. |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 418 | if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate) |
| 419 | return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 420 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 421 | if (Kind == TTI::SK_Reverse) { |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 422 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); |
| 423 | int Cost = 1; |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 424 | if (LT.second.getSizeInBits() > 128) |
| 425 | Cost = 3; // Extract + insert + copy. |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 426 | |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 427 | // Multiple by the number of parts. |
| 428 | return Cost * LT.first; |
| 429 | } |
| 430 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 431 | if (Kind == TTI::SK_Alternate) { |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 432 | // 64-bit packed float vectors (v2f32) are widened to type v4f32. |
| 433 | // 64-bit packed integer vectors (v2i32) are promoted to type v2i64. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 434 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 435 | |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 436 | // The backend knows how to generate a single VEX.256 version of |
| 437 | // instruction VPBLENDW if the target supports AVX2. |
| 438 | if (ST->hasAVX2() && LT.second == MVT::v16i16) |
| 439 | return LT.first; |
| 440 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 441 | static const CostTblEntry AVXAltShuffleTbl[] = { |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 442 | {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd |
| 443 | {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd |
| 444 | |
| 445 | {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps |
| 446 | {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps |
| 447 | |
| 448 | // This shuffle is custom lowered into a sequence of: |
| 449 | // 2x vextractf128 , 2x vpblendw , 1x vinsertf128 |
| 450 | {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5}, |
| 451 | |
| 452 | // This shuffle is custom lowered into a long sequence of: |
| 453 | // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128 |
| 454 | {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9} |
| 455 | }; |
| 456 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 457 | if (ST->hasAVX()) |
| 458 | if (const auto *Entry = CostTableLookup(AVXAltShuffleTbl, |
| 459 | ISD::VECTOR_SHUFFLE, LT.second)) |
| 460 | return LT.first * Entry->Cost; |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 461 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 462 | static const CostTblEntry SSE41AltShuffleTbl[] = { |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 463 | // These are lowered into movsd. |
| 464 | {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, |
| 465 | {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, |
| 466 | |
| 467 | // packed float vectors with four elements are lowered into BLENDI dag |
| 468 | // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'. |
| 469 | {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1}, |
| 470 | {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1}, |
| 471 | |
| 472 | // This shuffle generates a single pshufw. |
| 473 | {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1}, |
| 474 | |
| 475 | // There is no instruction that matches a v16i8 alternate shuffle. |
| 476 | // The backend will expand it into the sequence 'pshufb + pshufb + or'. |
| 477 | {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} |
| 478 | }; |
| 479 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 480 | if (ST->hasSSE41()) |
| 481 | if (const auto *Entry = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE, |
| 482 | LT.second)) |
| 483 | return LT.first * Entry->Cost; |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 484 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 485 | static const CostTblEntry SSSE3AltShuffleTbl[] = { |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 486 | {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd |
| 487 | {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd |
| 488 | |
| 489 | // SSE3 doesn't have 'blendps'. The following shuffles are expanded into |
| 490 | // the sequence 'shufps + pshufd' |
| 491 | {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, |
| 492 | {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, |
| 493 | |
| 494 | {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or |
| 495 | {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or |
| 496 | }; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 497 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 498 | if (ST->hasSSSE3()) |
| 499 | if (const auto *Entry = CostTableLookup(SSSE3AltShuffleTbl, |
| 500 | ISD::VECTOR_SHUFFLE, LT.second)) |
| 501 | return LT.first * Entry->Cost; |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 502 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 503 | static const CostTblEntry SSEAltShuffleTbl[] = { |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 504 | {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd |
| 505 | {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd |
| 506 | |
| 507 | {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd |
| 508 | {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 509 | |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 510 | // This is expanded into a long sequence of four extract + four insert. |
| 511 | {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw. |
| 512 | |
| 513 | // 8 x (pinsrw + pextrw + and + movb + movzb + or) |
| 514 | {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48} |
| 515 | }; |
| 516 | |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 517 | // Fall-back (SSE3 and SSE2). |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 518 | if (const auto *Entry = CostTableLookup(SSEAltShuffleTbl, |
| 519 | ISD::VECTOR_SHUFFLE, LT.second)) |
| 520 | return LT.first * Entry->Cost; |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 521 | return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 524 | return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 527 | int X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 528 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 529 | assert(ISD && "Invalid opcode"); |
| 530 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 531 | static const TypeConversionCostTblEntry AVX512ConversionTbl[] = { |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 532 | { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 }, |
| 533 | { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 }, |
| 534 | { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 }, |
| 535 | { ISD::FP_ROUND, MVT::v16f32, MVT::v8f64, 3 }, |
| 536 | |
| 537 | { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 }, |
| 538 | { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 }, |
| 539 | { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 }, |
| 540 | { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 }, |
| 541 | { ISD::TRUNCATE, MVT::v16i32, MVT::v8i64, 4 }, |
| 542 | |
| 543 | // v16i1 -> v16i32 - load + broadcast |
| 544 | { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, |
| 545 | { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, |
| 546 | |
| 547 | { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, |
| 548 | { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, |
| 549 | { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, |
| 550 | { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, |
| 551 | { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v16i32, 3 }, |
| 552 | { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v16i32, 3 }, |
| 553 | |
Elena Demikhovsky | d5e95b5 | 2014-11-13 11:46:16 +0000 | [diff] [blame] | 554 | { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 }, |
| 555 | { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 }, |
| 556 | { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 }, |
| 557 | { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 }, |
| 558 | { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 }, |
| 559 | { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 }, |
| 560 | { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 }, |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 561 | }; |
| 562 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 563 | static const TypeConversionCostTblEntry AVX2ConversionTbl[] = { |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 564 | { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, |
| 565 | { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, |
| 566 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, |
| 567 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, |
| 568 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, |
| 569 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, |
| 570 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, |
| 571 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, |
| 572 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, |
| 573 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, |
| 574 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, |
| 575 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, |
| 576 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 577 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 578 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, |
| 579 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, |
| 580 | |
| 581 | { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 }, |
| 582 | { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 }, |
| 583 | { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 }, |
| 584 | { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 }, |
| 585 | { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 }, |
| 586 | { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 }, |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 587 | |
| 588 | { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 }, |
| 589 | { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 }, |
Quentin Colombet | 360460b | 2014-11-11 02:23:47 +0000 | [diff] [blame] | 590 | |
| 591 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 }, |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 594 | static const TypeConversionCostTblEntry AVXConversionTbl[] = { |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 595 | { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, |
| 596 | { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, |
| 597 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 }, |
| 598 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 }, |
| 599 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 }, |
| 600 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 }, |
| 601 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, |
| 602 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, |
| 603 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 }, |
| 604 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 }, |
| 605 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 }, |
| 606 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 }, |
| 607 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 }, |
| 608 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 609 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, |
| 610 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, |
| 611 | |
| 612 | { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 }, |
| 613 | { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 }, |
| 614 | { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 }, |
| 615 | { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 }, |
| 616 | { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 }, |
| 617 | { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 }, |
| 618 | { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 }, |
Benjamin Kramer | 52ceb44 | 2013-04-01 10:23:49 +0000 | [diff] [blame] | 619 | |
| 620 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 }, |
| 621 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 }, |
| 622 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, |
| 623 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, |
| 624 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 }, |
| 625 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, |
| 626 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 }, |
| 627 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, |
| 628 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 }, |
| 629 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 }, |
| 630 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 }, |
| 631 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, |
| 632 | |
| 633 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 }, |
| 634 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 }, |
| 635 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, |
| 636 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 }, |
| 637 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 }, |
| 638 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 }, |
| 639 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, |
| 640 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 }, |
| 641 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 }, |
| 642 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, |
| 643 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, |
| 644 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 }, |
Quentin Colombet | 85b904d | 2014-03-27 22:27:41 +0000 | [diff] [blame] | 645 | // The generic code to compute the scalar overhead is currently broken. |
| 646 | // Workaround this limitation by estimating the scalarization overhead |
| 647 | // here. We have roughly 10 instructions per scalar element. |
| 648 | // Multiply that by the vector width. |
| 649 | // FIXME: remove that when PR19268 is fixed. |
Quentin Colombet | 3914bf5 | 2014-03-27 00:52:16 +0000 | [diff] [blame] | 650 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, |
| 651 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 4*10 }, |
Benjamin Kramer | 52ceb44 | 2013-04-01 10:23:49 +0000 | [diff] [blame] | 652 | |
Jim Grosbach | 72fbde8 | 2014-03-27 00:04:11 +0000 | [diff] [blame] | 653 | { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 }, |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 654 | { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 }, |
Adam Nemet | 6dafe97 | 2014-03-30 18:07:13 +0000 | [diff] [blame] | 655 | // This node is expanded into scalarized operations but BasicTTI is overly |
| 656 | // optimistic estimating its cost. It computes 3 per element (one |
| 657 | // vector-extract, one scalar conversion and one vector-insert). The |
| 658 | // problem is that the inserts form a read-modify-write chain so latency |
| 659 | // should be factored in too. Inflating the cost per element by 1. |
| 660 | { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 }, |
Adam Nemet | 10c4ce2 | 2014-03-31 21:54:48 +0000 | [diff] [blame] | 661 | { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 662 | }; |
| 663 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 664 | static const TypeConversionCostTblEntry SSE2ConvTbl[] = { |
Simon Pilgrim | e2c244f | 2015-07-19 15:36:12 +0000 | [diff] [blame] | 665 | // These are somewhat magic numbers justified by looking at the output of |
| 666 | // Intel's IACA, running some kernels and making sure when we take |
| 667 | // legalization into account the throughput will be overestimated. |
| 668 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, |
| 669 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, |
| 670 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, |
| 671 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, |
| 672 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, |
| 673 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, |
| 674 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, |
| 675 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, |
| 676 | // There are faster sequences for float conversions. |
| 677 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, |
| 678 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 }, |
| 679 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, |
| 680 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, |
| 681 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, |
| 682 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 }, |
| 683 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, |
| 684 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, |
| 685 | }; |
| 686 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 687 | std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src); |
| 688 | std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst); |
Simon Pilgrim | e2c244f | 2015-07-19 15:36:12 +0000 | [diff] [blame] | 689 | |
| 690 | if (ST->hasSSE2() && !ST->hasAVX()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 691 | if (const auto *Entry = ConvertCostTableLookup(SSE2ConvTbl, ISD, |
| 692 | LTDest.second, LTSrc.second)) |
| 693 | return LTSrc.first * Entry->Cost; |
Simon Pilgrim | e2c244f | 2015-07-19 15:36:12 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | if (ST->hasAVX512()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 697 | if (const auto *Entry = ConvertCostTableLookup(AVX512ConversionTbl, ISD, |
| 698 | LTDest.second, LTSrc.second)) |
| 699 | return Entry->Cost; |
Simon Pilgrim | e2c244f | 2015-07-19 15:36:12 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | EVT SrcTy = TLI->getValueType(DL, Src); |
| 703 | EVT DstTy = TLI->getValueType(DL, Dst); |
| 704 | |
| 705 | // The function getSimpleVT only handles simple value types. |
| 706 | if (!SrcTy.isSimple() || !DstTy.isSimple()) |
| 707 | return BaseT::getCastInstrCost(Opcode, Dst, Src); |
| 708 | |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 709 | if (ST->hasAVX2()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 710 | if (const auto *Entry = ConvertCostTableLookup(AVX2ConversionTbl, ISD, |
| 711 | DstTy.getSimpleVT(), |
| 712 | SrcTy.getSimpleVT())) |
| 713 | return Entry->Cost; |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 716 | if (ST->hasAVX()) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 717 | if (const auto *Entry = ConvertCostTableLookup(AVXConversionTbl, ISD, |
| 718 | DstTy.getSimpleVT(), |
| 719 | SrcTy.getSimpleVT())) |
| 720 | return Entry->Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 723 | return BaseT::getCastInstrCost(Opcode, Dst, Src); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 726 | int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 727 | // Legalize the type. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 728 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 729 | |
| 730 | MVT MTy = LT.second; |
| 731 | |
| 732 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 733 | assert(ISD && "Invalid opcode"); |
| 734 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 735 | static const CostTblEntry SSE42CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 736 | { ISD::SETCC, MVT::v2f64, 1 }, |
| 737 | { ISD::SETCC, MVT::v4f32, 1 }, |
| 738 | { ISD::SETCC, MVT::v2i64, 1 }, |
| 739 | { ISD::SETCC, MVT::v4i32, 1 }, |
| 740 | { ISD::SETCC, MVT::v8i16, 1 }, |
| 741 | { ISD::SETCC, MVT::v16i8, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 742 | }; |
| 743 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 744 | static const CostTblEntry AVX1CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 745 | { ISD::SETCC, MVT::v4f64, 1 }, |
| 746 | { ISD::SETCC, MVT::v8f32, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 747 | // AVX1 does not support 8-wide integer compare. |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 748 | { ISD::SETCC, MVT::v4i64, 4 }, |
| 749 | { ISD::SETCC, MVT::v8i32, 4 }, |
| 750 | { ISD::SETCC, MVT::v16i16, 4 }, |
| 751 | { ISD::SETCC, MVT::v32i8, 4 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 752 | }; |
| 753 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 754 | static const CostTblEntry AVX2CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 755 | { ISD::SETCC, MVT::v4i64, 1 }, |
| 756 | { ISD::SETCC, MVT::v8i32, 1 }, |
| 757 | { ISD::SETCC, MVT::v16i16, 1 }, |
| 758 | { ISD::SETCC, MVT::v32i8, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 759 | }; |
| 760 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 761 | static const CostTblEntry AVX512CostTbl[] = { |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 762 | { ISD::SETCC, MVT::v8i64, 1 }, |
| 763 | { ISD::SETCC, MVT::v16i32, 1 }, |
| 764 | { ISD::SETCC, MVT::v8f64, 1 }, |
| 765 | { ISD::SETCC, MVT::v16f32, 1 }, |
| 766 | }; |
| 767 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 768 | if (ST->hasAVX512()) |
| 769 | if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy)) |
| 770 | return LT.first * Entry->Cost; |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 771 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 772 | if (ST->hasAVX2()) |
| 773 | if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy)) |
| 774 | return LT.first * Entry->Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 775 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 776 | if (ST->hasAVX()) |
| 777 | if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy)) |
| 778 | return LT.first * Entry->Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 779 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 780 | if (ST->hasSSE42()) |
| 781 | if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy)) |
| 782 | return LT.first * Entry->Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 783 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 784 | return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 787 | int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 788 | assert(Val->isVectorTy() && "This must be a vector type"); |
| 789 | |
| 790 | if (Index != -1U) { |
| 791 | // Legalize the type. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 792 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 793 | |
| 794 | // This type is legalized to a scalar type. |
| 795 | if (!LT.second.isVector()) |
| 796 | return 0; |
| 797 | |
| 798 | // The type may be split. Normalize the index to the new type. |
| 799 | unsigned Width = LT.second.getVectorNumElements(); |
| 800 | Index = Index % Width; |
| 801 | |
| 802 | // Floating point scalars are already located in index #0. |
| 803 | if (Val->getScalarType()->isFloatingPointTy() && Index == 0) |
| 804 | return 0; |
| 805 | } |
| 806 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 807 | return BaseT::getVectorInstrCost(Opcode, Val, Index); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 810 | int X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) { |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 811 | assert (Ty->isVectorTy() && "Can only scalarize vectors"); |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 812 | int Cost = 0; |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 813 | |
| 814 | for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { |
| 815 | if (Insert) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 816 | Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i); |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 817 | if (Extract) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 818 | Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i); |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | return Cost; |
| 822 | } |
| 823 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 824 | int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, |
| 825 | unsigned AddressSpace) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 826 | // Handle non-power-of-two vectors such as <3 x float> |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 827 | if (VectorType *VTy = dyn_cast<VectorType>(Src)) { |
| 828 | unsigned NumElem = VTy->getVectorNumElements(); |
| 829 | |
| 830 | // Handle a few common cases: |
| 831 | // <3 x float> |
| 832 | if (NumElem == 3 && VTy->getScalarSizeInBits() == 32) |
| 833 | // Cost = 64 bit store + extract + 32 bit store. |
| 834 | return 3; |
| 835 | |
| 836 | // <3 x double> |
| 837 | if (NumElem == 3 && VTy->getScalarSizeInBits() == 64) |
| 838 | // Cost = 128 bit store + unpack + 64 bit store. |
| 839 | return 3; |
| 840 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 841 | // Assume that all other non-power-of-two numbers are scalarized. |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 842 | if (!isPowerOf2_32(NumElem)) { |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 843 | int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment, |
| 844 | AddressSpace); |
| 845 | int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load, |
| 846 | Opcode == Instruction::Store); |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 847 | return NumElem * Cost + SplitCost; |
| 848 | } |
| 849 | } |
| 850 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 851 | // Legalize the type. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 852 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 853 | assert((Opcode == Instruction::Load || Opcode == Instruction::Store) && |
| 854 | "Invalid Opcode"); |
| 855 | |
| 856 | // Each load/store unit costs 1. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 857 | int Cost = LT.first * 1; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 858 | |
| 859 | // On Sandybridge 256bit load/stores are double pumped |
| 860 | // (but not on Haswell). |
| 861 | if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2()) |
| 862 | Cost*=2; |
| 863 | |
| 864 | return Cost; |
| 865 | } |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 866 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 867 | int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy, |
| 868 | unsigned Alignment, |
| 869 | unsigned AddressSpace) { |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 870 | VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy); |
| 871 | if (!SrcVTy) |
| 872 | // To calculate scalar take the regular cost, without mask |
| 873 | return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace); |
| 874 | |
| 875 | unsigned NumElem = SrcVTy->getVectorNumElements(); |
| 876 | VectorType *MaskTy = |
| 877 | VectorType::get(Type::getInt8Ty(getGlobalContext()), NumElem); |
Elena Demikhovsky | 20662e3 | 2015-10-19 07:43:38 +0000 | [diff] [blame] | 878 | if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy)) || |
| 879 | (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy)) || |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 880 | !isPowerOf2_32(NumElem)) { |
| 881 | // Scalarization |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 882 | int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true); |
| 883 | int ScalarCompareCost = getCmpSelInstrCost( |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 884 | Instruction::ICmp, Type::getInt8Ty(getGlobalContext()), nullptr); |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 885 | int BranchCost = getCFInstrCost(Instruction::Br); |
| 886 | int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost); |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 887 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 888 | int ValueSplitCost = getScalarizationOverhead( |
| 889 | SrcVTy, Opcode == Instruction::Load, Opcode == Instruction::Store); |
| 890 | int MemopCost = |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 891 | NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(), |
| 892 | Alignment, AddressSpace); |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 893 | return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost; |
| 894 | } |
| 895 | |
| 896 | // Legalize the type. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 897 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy); |
Cong Hou | da4e8ae | 2015-10-28 18:15:46 +0000 | [diff] [blame] | 898 | auto VT = TLI->getValueType(DL, SrcVTy); |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 899 | int Cost = 0; |
Cong Hou | da4e8ae | 2015-10-28 18:15:46 +0000 | [diff] [blame] | 900 | if (VT.isSimple() && LT.second != VT.getSimpleVT() && |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 901 | LT.second.getVectorNumElements() == NumElem) |
| 902 | // Promotion requires expand/truncate for data and a shuffle for mask. |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 903 | Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, nullptr) + |
| 904 | getShuffleCost(TTI::SK_Alternate, MaskTy, 0, nullptr); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 905 | |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 906 | else if (LT.second.getVectorNumElements() > NumElem) { |
| 907 | VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(), |
| 908 | LT.second.getVectorNumElements()); |
| 909 | // Expanding requires fill mask with zeroes |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 910 | Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy); |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 911 | } |
| 912 | if (!ST->hasAVX512()) |
| 913 | return Cost + LT.first*4; // Each maskmov costs 4 |
| 914 | |
| 915 | // AVX-512 masked load/store is cheapper |
| 916 | return Cost+LT.first; |
| 917 | } |
| 918 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 919 | int X86TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) { |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 920 | // Address computations in vectorized code with non-consecutive addresses will |
| 921 | // likely result in more instructions compared to scalar code where the |
| 922 | // computation can more often be merged into the index mode. The resulting |
| 923 | // extra micro-ops can significantly decrease throughput. |
| 924 | unsigned NumVectorInstToHideOverhead = 10; |
| 925 | |
| 926 | if (Ty->isVectorTy() && IsComplex) |
| 927 | return NumVectorInstToHideOverhead; |
| 928 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 929 | return BaseT::getAddressComputationCost(Ty, IsComplex); |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 930 | } |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 931 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 932 | int X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy, |
| 933 | bool IsPairwise) { |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 934 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 935 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 936 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 937 | MVT MTy = LT.second; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 938 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 939 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 940 | assert(ISD && "Invalid opcode"); |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 941 | |
| 942 | // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput |
| 943 | // and make it as the cost. |
| 944 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 945 | static const CostTblEntry SSE42CostTblPairWise[] = { |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 946 | { ISD::FADD, MVT::v2f64, 2 }, |
| 947 | { ISD::FADD, MVT::v4f32, 4 }, |
| 948 | { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". |
| 949 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". |
| 950 | { ISD::ADD, MVT::v8i16, 5 }, |
| 951 | }; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 952 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 953 | static const CostTblEntry AVX1CostTblPairWise[] = { |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 954 | { ISD::FADD, MVT::v4f32, 4 }, |
| 955 | { ISD::FADD, MVT::v4f64, 5 }, |
| 956 | { ISD::FADD, MVT::v8f32, 7 }, |
| 957 | { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". |
| 958 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". |
| 959 | { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8". |
| 960 | { ISD::ADD, MVT::v8i16, 5 }, |
| 961 | { ISD::ADD, MVT::v8i32, 5 }, |
| 962 | }; |
| 963 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 964 | static const CostTblEntry SSE42CostTblNoPairWise[] = { |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 965 | { ISD::FADD, MVT::v2f64, 2 }, |
| 966 | { ISD::FADD, MVT::v4f32, 4 }, |
| 967 | { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". |
| 968 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3". |
| 969 | { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3". |
| 970 | }; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 971 | |
Craig Topper | 4b27576 | 2015-10-28 04:02:12 +0000 | [diff] [blame] | 972 | static const CostTblEntry AVX1CostTblNoPairWise[] = { |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 973 | { ISD::FADD, MVT::v4f32, 3 }, |
| 974 | { ISD::FADD, MVT::v4f64, 3 }, |
| 975 | { ISD::FADD, MVT::v8f32, 4 }, |
| 976 | { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". |
| 977 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8". |
| 978 | { ISD::ADD, MVT::v4i64, 3 }, |
| 979 | { ISD::ADD, MVT::v8i16, 4 }, |
| 980 | { ISD::ADD, MVT::v8i32, 5 }, |
| 981 | }; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 982 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 983 | if (IsPairwise) { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 984 | if (ST->hasAVX()) |
| 985 | if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy)) |
| 986 | return LT.first * Entry->Cost; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 987 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 988 | if (ST->hasSSE42()) |
| 989 | if (const auto *Entry = CostTableLookup(SSE42CostTblPairWise, ISD, MTy)) |
| 990 | return LT.first * Entry->Cost; |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 991 | } else { |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 992 | if (ST->hasAVX()) |
| 993 | if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy)) |
| 994 | return LT.first * Entry->Cost; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 995 | |
Craig Topper | ee0c859 | 2015-10-27 04:14:24 +0000 | [diff] [blame] | 996 | if (ST->hasSSE42()) |
| 997 | if (const auto *Entry = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy)) |
| 998 | return LT.first * Entry->Cost; |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1001 | return BaseT::getReductionCost(Opcode, ValTy, IsPairwise); |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1004 | /// \brief Calculate the cost of materializing a 64-bit value. This helper |
| 1005 | /// method might only calculate a fraction of a larger immediate. Therefore it |
| 1006 | /// is valid to return a cost of ZERO. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 1007 | int X86TTIImpl::getIntImmCost(int64_t Val) { |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1008 | if (Val == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1009 | return TTI::TCC_Free; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1010 | |
| 1011 | if (isInt<32>(Val)) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1012 | return TTI::TCC_Basic; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1013 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1014 | return 2 * TTI::TCC_Basic; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 1017 | int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1018 | assert(Ty->isIntegerTy()); |
| 1019 | |
| 1020 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 1021 | if (BitSize == 0) |
| 1022 | return ~0U; |
| 1023 | |
Juergen Ributzka | 4317617 | 2014-05-19 21:00:53 +0000 | [diff] [blame] | 1024 | // Never hoist constants larger than 128bit, because this might lead to |
| 1025 | // incorrect code generation or assertions in codegen. |
| 1026 | // Fixme: Create a cost model for types larger than i128 once the codegen |
| 1027 | // issues have been fixed. |
| 1028 | if (BitSize > 128) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1029 | return TTI::TCC_Free; |
Juergen Ributzka | 4317617 | 2014-05-19 21:00:53 +0000 | [diff] [blame] | 1030 | |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1031 | if (Imm == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1032 | return TTI::TCC_Free; |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1033 | |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1034 | // Sign-extend all constants to a multiple of 64-bit. |
| 1035 | APInt ImmVal = Imm; |
| 1036 | if (BitSize & 0x3f) |
| 1037 | ImmVal = Imm.sext((BitSize + 63) & ~0x3fU); |
| 1038 | |
| 1039 | // Split the constant into 64-bit chunks and calculate the cost for each |
| 1040 | // chunk. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 1041 | int Cost = 0; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1042 | for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) { |
| 1043 | APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64); |
| 1044 | int64_t Val = Tmp.getSExtValue(); |
| 1045 | Cost += getIntImmCost(Val); |
| 1046 | } |
| 1047 | // We need at least one instruction to materialze the constant. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 1048 | return std::max(1, Cost); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 1051 | int X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, |
| 1052 | Type *Ty) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1053 | assert(Ty->isIntegerTy()); |
| 1054 | |
| 1055 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
Juergen Ributzka | 4317617 | 2014-05-19 21:00:53 +0000 | [diff] [blame] | 1056 | // There is no cost model for constants with a bit size of 0. Return TCC_Free |
| 1057 | // here, so that constant hoisting will ignore this constant. |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1058 | if (BitSize == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1059 | return TTI::TCC_Free; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1060 | |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1061 | unsigned ImmIdx = ~0U; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1062 | switch (Opcode) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1063 | default: |
| 1064 | return TTI::TCC_Free; |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1065 | case Instruction::GetElementPtr: |
Juergen Ributzka | 27435b3 | 2014-04-02 21:45:36 +0000 | [diff] [blame] | 1066 | // Always hoist the base address of a GetElementPtr. This prevents the |
| 1067 | // creation of new constants for every base constant that gets constant |
| 1068 | // folded with the offset. |
Juergen Ributzka | 631c491 | 2014-03-25 18:01:25 +0000 | [diff] [blame] | 1069 | if (Idx == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1070 | return 2 * TTI::TCC_Basic; |
| 1071 | return TTI::TCC_Free; |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1072 | case Instruction::Store: |
| 1073 | ImmIdx = 0; |
| 1074 | break; |
Craig Topper | 79dd1bf | 2015-10-06 02:50:24 +0000 | [diff] [blame] | 1075 | case Instruction::And: |
| 1076 | // We support 64-bit ANDs with immediates with 32-bits of leading zeroes |
| 1077 | // by using a 32-bit operation with implicit zero extension. Detect such |
| 1078 | // immediates here as the normal path expects bit 31 to be sign extended. |
| 1079 | if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue())) |
| 1080 | return TTI::TCC_Free; |
| 1081 | // Fallthrough |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1082 | case Instruction::Add: |
| 1083 | case Instruction::Sub: |
| 1084 | case Instruction::Mul: |
| 1085 | case Instruction::UDiv: |
| 1086 | case Instruction::SDiv: |
| 1087 | case Instruction::URem: |
| 1088 | case Instruction::SRem: |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1089 | case Instruction::Or: |
| 1090 | case Instruction::Xor: |
| 1091 | case Instruction::ICmp: |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1092 | ImmIdx = 1; |
| 1093 | break; |
Michael Zolotukhin | 1f4a960 | 2014-04-30 19:17:32 +0000 | [diff] [blame] | 1094 | // Always return TCC_Free for the shift value of a shift instruction. |
| 1095 | case Instruction::Shl: |
| 1096 | case Instruction::LShr: |
| 1097 | case Instruction::AShr: |
| 1098 | if (Idx == 1) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1099 | return TTI::TCC_Free; |
Michael Zolotukhin | 1f4a960 | 2014-04-30 19:17:32 +0000 | [diff] [blame] | 1100 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1101 | case Instruction::Trunc: |
| 1102 | case Instruction::ZExt: |
| 1103 | case Instruction::SExt: |
| 1104 | case Instruction::IntToPtr: |
| 1105 | case Instruction::PtrToInt: |
| 1106 | case Instruction::BitCast: |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1107 | case Instruction::PHI: |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1108 | case Instruction::Call: |
| 1109 | case Instruction::Select: |
| 1110 | case Instruction::Ret: |
| 1111 | case Instruction::Load: |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1112 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1113 | } |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1114 | |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1115 | if (Idx == ImmIdx) { |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 1116 | int NumConstants = (BitSize + 63) / 64; |
| 1117 | int Cost = X86TTIImpl::getIntImmCost(Imm, Ty); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1118 | return (Cost <= NumConstants * TTI::TCC_Basic) |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 1119 | ? static_cast<int>(TTI::TCC_Free) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1120 | : Cost; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1121 | } |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1122 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1123 | return X86TTIImpl::getIntImmCost(Imm, Ty); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 1126 | int X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, |
| 1127 | Type *Ty) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1128 | assert(Ty->isIntegerTy()); |
| 1129 | |
| 1130 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
Juergen Ributzka | 4317617 | 2014-05-19 21:00:53 +0000 | [diff] [blame] | 1131 | // There is no cost model for constants with a bit size of 0. Return TCC_Free |
| 1132 | // here, so that constant hoisting will ignore this constant. |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1133 | if (BitSize == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1134 | return TTI::TCC_Free; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1135 | |
| 1136 | switch (IID) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1137 | default: |
| 1138 | return TTI::TCC_Free; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1139 | case Intrinsic::sadd_with_overflow: |
| 1140 | case Intrinsic::uadd_with_overflow: |
| 1141 | case Intrinsic::ssub_with_overflow: |
| 1142 | case Intrinsic::usub_with_overflow: |
| 1143 | case Intrinsic::smul_with_overflow: |
| 1144 | case Intrinsic::umul_with_overflow: |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1145 | if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1146 | return TTI::TCC_Free; |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1147 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1148 | case Intrinsic::experimental_stackmap: |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1149 | if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1150 | return TTI::TCC_Free; |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1151 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1152 | case Intrinsic::experimental_patchpoint_void: |
| 1153 | case Intrinsic::experimental_patchpoint_i64: |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1154 | if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1155 | return TTI::TCC_Free; |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1156 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1157 | } |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 1158 | return X86TTIImpl::getIntImmCost(Imm, Ty); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1159 | } |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 1160 | |
Elena Demikhovsky | 20662e3 | 2015-10-19 07:43:38 +0000 | [diff] [blame] | 1161 | bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy) { |
| 1162 | Type *ScalarTy = DataTy->getScalarType(); |
Elena Demikhovsky | 7ad0d56 | 2015-10-22 06:20:29 +0000 | [diff] [blame] | 1163 | // TODO: Pointers should also be legal, |
| 1164 | // but it requires additional support in composing intrinsics name. |
| 1165 | // getPrimitiveSizeInBits() returns 0 for PointerType |
| 1166 | int DataWidth = ScalarTy->getPrimitiveSizeInBits(); |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 1167 | |
Elena Demikhovsky | 20662e3 | 2015-10-19 07:43:38 +0000 | [diff] [blame] | 1168 | return (DataWidth >= 32 && ST->hasAVX2()); |
NAKAMURA Takumi | 0b305db | 2015-07-14 04:03:49 +0000 | [diff] [blame] | 1169 | } |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 1170 | |
Elena Demikhovsky | 20662e3 | 2015-10-19 07:43:38 +0000 | [diff] [blame] | 1171 | bool X86TTIImpl::isLegalMaskedStore(Type *DataType) { |
| 1172 | return isLegalMaskedLoad(DataType); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Elena Demikhovsky | 0928585 | 2015-10-25 15:37:55 +0000 | [diff] [blame] | 1175 | bool X86TTIImpl::isLegalMaskedGather(Type *DataTy) { |
| 1176 | // This function is called now in two cases: from the Loop Vectorizer |
| 1177 | // and from the Scalarizer. |
| 1178 | // When the Loop Vectorizer asks about legality of the feature, |
| 1179 | // the vectorization factor is not calculated yet. The Loop Vectorizer |
| 1180 | // sends a scalar type and the decision is based on the width of the |
| 1181 | // scalar element. |
| 1182 | // Later on, the cost model will estimate usage this intrinsic based on |
| 1183 | // the vector type. |
| 1184 | // The Scalarizer asks again about legality. It sends a vector type. |
| 1185 | // In this case we can reject non-power-of-2 vectors. |
| 1186 | if (isa<VectorType>(DataTy) && !isPowerOf2_32(DataTy->getVectorNumElements())) |
| 1187 | return false; |
| 1188 | Type *ScalarTy = DataTy->getScalarType(); |
| 1189 | // TODO: Pointers should also be legal, |
| 1190 | // but it requires additional support in composing intrinsics name. |
| 1191 | // getPrimitiveSizeInBits() returns 0 for PointerType |
| 1192 | int DataWidth = ScalarTy->getPrimitiveSizeInBits(); |
| 1193 | |
| 1194 | // AVX-512 allows gather and scatter |
| 1195 | return DataWidth >= 32 && ST->hasAVX512(); |
| 1196 | } |
| 1197 | |
| 1198 | bool X86TTIImpl::isLegalMaskedScatter(Type *DataType) { |
| 1199 | return isLegalMaskedGather(DataType); |
| 1200 | } |
| 1201 | |
Eric Christopher | d566fb1 | 2015-07-29 22:09:48 +0000 | [diff] [blame] | 1202 | bool X86TTIImpl::areInlineCompatible(const Function *Caller, |
| 1203 | const Function *Callee) const { |
Eric Christopher | e100226 | 2015-07-02 01:11:50 +0000 | [diff] [blame] | 1204 | const TargetMachine &TM = getTLI()->getTargetMachine(); |
| 1205 | |
| 1206 | // Work this as a subsetting of subtarget features. |
| 1207 | const FeatureBitset &CallerBits = |
| 1208 | TM.getSubtargetImpl(*Caller)->getFeatureBits(); |
| 1209 | const FeatureBitset &CalleeBits = |
| 1210 | TM.getSubtargetImpl(*Callee)->getFeatureBits(); |
| 1211 | |
| 1212 | // FIXME: This is likely too limiting as it will include subtarget features |
| 1213 | // that we might not care about for inlining, but it is conservatively |
| 1214 | // correct. |
| 1215 | return (CallerBits & CalleeBits) == CalleeBits; |
| 1216 | } |