blob: 63f34a8c909c7a7474f340ec29bba93993d88f8d [file] [log] [blame]
Nadav Rotemcbd9a192012-10-18 23:22:48 +00001// llvm/Target/TargetTransformImpl.cpp - Target Loop Trans Info ---*- C++ -*-=//
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#include "llvm/Target/TargetTransformImpl.h"
11#include "llvm/Target/TargetLowering.h"
Nadav Rotem2652c502012-10-24 23:47:38 +000012#include <utility>
Nadav Rotemcbd9a192012-10-18 23:22:48 +000013
14using namespace llvm;
15
Nadav Rotem27048342012-10-24 17:22:41 +000016//===----------------------------------------------------------------------===//
17//
18// Calls used by scalar transformations.
19//
20//===----------------------------------------------------------------------===//
21
Nadav Rotemcbd9a192012-10-18 23:22:48 +000022bool ScalarTargetTransformImpl::isLegalAddImmediate(int64_t imm) const {
23 return TLI->isLegalAddImmediate(imm);
24}
25
26bool ScalarTargetTransformImpl::isLegalICmpImmediate(int64_t imm) const {
27 return TLI->isLegalICmpImmediate(imm);
28}
29
Chandler Carruth64e407b2013-01-05 03:36:17 +000030bool ScalarTargetTransformImpl::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
31 int64_t BaseOffset, bool HasBaseReg,
32 int64_t Scale) const {
33 AddrMode AM;
34 AM.BaseGV = BaseGV;
35 AM.BaseOffs = BaseOffset;
36 AM.HasBaseReg = HasBaseReg;
37 AM.Scale = Scale;
Nadav Rotemcbd9a192012-10-18 23:22:48 +000038 return TLI->isLegalAddressingMode(AM, Ty);
39}
40
41bool ScalarTargetTransformImpl::isTruncateFree(Type *Ty1, Type *Ty2) const {
42 return TLI->isTruncateFree(Ty1, Ty2);
43}
44
45bool ScalarTargetTransformImpl::isTypeLegal(Type *Ty) const {
46 EVT T = TLI->getValueType(Ty);
47 return TLI->isTypeLegal(T);
48}
49
50unsigned ScalarTargetTransformImpl::getJumpBufAlignment() const {
51 return TLI->getJumpBufAlignment();
52}
53
54unsigned ScalarTargetTransformImpl::getJumpBufSize() const {
55 return TLI->getJumpBufSize();
56}
Nadav Rotem27048342012-10-24 17:22:41 +000057
Hans Wennborg04d7d132012-10-30 11:23:25 +000058bool ScalarTargetTransformImpl::shouldBuildLookupTables() const {
59 return TLI->supportJumpTables() &&
60 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
61 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
62}
63
Nadav Rotem27048342012-10-24 17:22:41 +000064//===----------------------------------------------------------------------===//
65//
66// Calls used by the vectorizers.
67//
68//===----------------------------------------------------------------------===//
Nadav Roteme6237022012-11-05 19:32:46 +000069int VectorTargetTransformImpl::InstructionOpcodeToISD(unsigned Opcode) const {
Renato Goline5372d62012-10-26 12:24:52 +000070 enum InstructionOpcodes {
71#define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
72#define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000073#include "llvm/IR/Instruction.def"
Renato Goline5372d62012-10-26 12:24:52 +000074 };
75 switch (static_cast<InstructionOpcodes>(Opcode)) {
76 case Ret: return 0;
77 case Br: return 0;
78 case Switch: return 0;
79 case IndirectBr: return 0;
80 case Invoke: return 0;
81 case Resume: return 0;
82 case Unreachable: return 0;
83 case Add: return ISD::ADD;
84 case FAdd: return ISD::FADD;
85 case Sub: return ISD::SUB;
86 case FSub: return ISD::FSUB;
87 case Mul: return ISD::MUL;
88 case FMul: return ISD::FMUL;
89 case UDiv: return ISD::UDIV;
90 case SDiv: return ISD::UDIV;
91 case FDiv: return ISD::FDIV;
92 case URem: return ISD::UREM;
93 case SRem: return ISD::SREM;
94 case FRem: return ISD::FREM;
95 case Shl: return ISD::SHL;
96 case LShr: return ISD::SRL;
97 case AShr: return ISD::SRA;
98 case And: return ISD::AND;
99 case Or: return ISD::OR;
100 case Xor: return ISD::XOR;
101 case Alloca: return 0;
102 case Load: return ISD::LOAD;
103 case Store: return ISD::STORE;
104 case GetElementPtr: return 0;
105 case Fence: return 0;
106 case AtomicCmpXchg: return 0;
107 case AtomicRMW: return 0;
108 case Trunc: return ISD::TRUNCATE;
109 case ZExt: return ISD::ZERO_EXTEND;
Nadav Rotem2d1528b2012-11-05 22:20:53 +0000110 case SExt: return ISD::SIGN_EXTEND;
Renato Goline5372d62012-10-26 12:24:52 +0000111 case FPToUI: return ISD::FP_TO_UINT;
112 case FPToSI: return ISD::FP_TO_SINT;
113 case UIToFP: return ISD::UINT_TO_FP;
114 case SIToFP: return ISD::SINT_TO_FP;
115 case FPTrunc: return ISD::FP_ROUND;
116 case FPExt: return ISD::FP_EXTEND;
117 case PtrToInt: return ISD::BITCAST;
118 case IntToPtr: return ISD::BITCAST;
119 case BitCast: return ISD::BITCAST;
120 case ICmp: return ISD::SETCC;
121 case FCmp: return ISD::SETCC;
122 case PHI: return 0;
123 case Call: return 0;
124 case Select: return ISD::SELECT;
125 case UserOp1: return 0;
126 case UserOp2: return 0;
127 case VAArg: return 0;
128 case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
129 case InsertElement: return ISD::INSERT_VECTOR_ELT;
130 case ShuffleVector: return ISD::VECTOR_SHUFFLE;
131 case ExtractValue: return ISD::MERGE_VALUES;
132 case InsertValue: return ISD::MERGE_VALUES;
133 case LandingPad: return 0;
134 }
Nadav Rotem2652c502012-10-24 23:47:38 +0000135
Renato Goline5372d62012-10-26 12:24:52 +0000136 llvm_unreachable("Unknown instruction type encountered!");
Nadav Rotem2652c502012-10-24 23:47:38 +0000137}
138
Nadav Roteme6237022012-11-05 19:32:46 +0000139std::pair<unsigned, MVT>
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000140VectorTargetTransformImpl::getTypeLegalizationCost(Type *Ty) const {
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000141 LLVMContext &C = Ty->getContext();
142 EVT MTy = TLI->getValueType(Ty);
143
Nadav Rotem2652c502012-10-24 23:47:38 +0000144 unsigned Cost = 1;
145 // We keep legalizing the type until we find a legal kind. We assume that
146 // the only operation that costs anything is the split. After splitting
147 // we need to handle two types.
148 while (true) {
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000149 TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, MTy);
Nadav Rotem2652c502012-10-24 23:47:38 +0000150
151 if (LK.first == TargetLowering::TypeLegal)
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000152 return std::make_pair(Cost, MTy.getSimpleVT());
Nadav Rotem2652c502012-10-24 23:47:38 +0000153
Nadav Rotem75138f52012-11-05 21:11:10 +0000154 if (LK.first == TargetLowering::TypeSplitVector ||
155 LK.first == TargetLowering::TypeExpandInteger)
Nadav Rotem2652c502012-10-24 23:47:38 +0000156 Cost *= 2;
157
158 // Keep legalizing the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000159 MTy = LK.second;
Nadav Rotem2652c502012-10-24 23:47:38 +0000160 }
161}
Nadav Rotem27048342012-10-24 17:22:41 +0000162
163unsigned
Nadav Rotema5a3a612012-10-26 23:49:28 +0000164VectorTargetTransformImpl::getScalarizationOverhead(Type *Ty,
165 bool Insert,
166 bool Extract) const {
167 assert (Ty->isVectorTy() && "Can only scalarize vectors");
Hans Wennborgb9051db2012-10-29 16:26:52 +0000168 unsigned Cost = 0;
Nadav Rotem2652c502012-10-24 23:47:38 +0000169
Nadav Rotema5a3a612012-10-26 23:49:28 +0000170 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
171 if (Insert)
172 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
173 if (Extract)
174 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
Nadav Rotem1e19e462012-10-25 18:17:48 +0000175 }
176
Nadav Rotema5a3a612012-10-26 23:49:28 +0000177 return Cost;
178}
Nadav Rotem2652c502012-10-24 23:47:38 +0000179
Nadav Roteme5033192013-01-04 17:48:25 +0000180unsigned VectorTargetTransformImpl::getNumberOfRegisters(bool Vector) const {
Nadav Rotem5d592d22013-01-04 18:40:39 +0000181 return 1;
Nadav Roteme5033192013-01-04 17:48:25 +0000182}
183
Nadav Rotema5a3a612012-10-26 23:49:28 +0000184unsigned VectorTargetTransformImpl::getArithmeticInstrCost(unsigned Opcode,
185 Type *Ty) const {
186 // Check if any of the operands are vector operands.
187 int ISD = InstructionOpcodeToISD(Opcode);
188 assert(ISD && "Invalid opcode");
Nadav Rotem2652c502012-10-24 23:47:38 +0000189
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000190 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Rotema5a3a612012-10-26 23:49:28 +0000191
Nadav Rotem6f3d81a2012-12-23 17:31:23 +0000192 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
193 // The operation is legal. Assume it costs 1.
194 // If the type is split to multiple registers, assume that thre is some
195 // overhead to this.
196 // TODO: Once we have extract/insert subvector cost we need to use them.
197 if (LT.first > 1)
198 return LT.first * 2;
Nadav Rotem2652c502012-10-24 23:47:38 +0000199 return LT.first * 1;
200 }
201
Nadav Rotem6f3d81a2012-12-23 17:31:23 +0000202 if (!TLI->isOperationExpand(ISD, LT.second)) {
203 // If the operation is custom lowered then assume
204 // thare the code is twice as expensive.
205 return LT.first * 2;
206 }
207
Nadav Rotema5a3a612012-10-26 23:49:28 +0000208 // Else, assume that we need to scalarize this op.
209 if (Ty->isVectorTy()) {
210 unsigned Num = Ty->getVectorNumElements();
211 unsigned Cost = getArithmeticInstrCost(Opcode, Ty->getScalarType());
212 // return the cost of multiple scalar invocation plus the cost of inserting
213 // and extracting the values.
214 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
215 }
Nadav Rotem2652c502012-10-24 23:47:38 +0000216
Nadav Rotema5a3a612012-10-26 23:49:28 +0000217 // We don't know anything about this scalar instruction.
218 return 1;
219}
220
Nadav Rotemdaf7b5c2012-12-24 08:57:47 +0000221unsigned VectorTargetTransformImpl::getShuffleCost(ShuffleKind Kind,
Hal Finkel82860f62013-01-03 02:34:09 +0000222 Type *Tp, int Index, Type *SubTp) const {
Nadav Rotema5a3a612012-10-26 23:49:28 +0000223 return 1;
224}
225
226unsigned VectorTargetTransformImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
227 Type *Src) const {
Nadav Rotema5a3a612012-10-26 23:49:28 +0000228 int ISD = InstructionOpcodeToISD(Opcode);
229 assert(ISD && "Invalid opcode");
230
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000231 std::pair<unsigned, MVT> SrcLT = getTypeLegalizationCost(Src);
232 std::pair<unsigned, MVT> DstLT = getTypeLegalizationCost(Dst);
Nadav Rotema5a3a612012-10-26 23:49:28 +0000233
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000234 // Handle scalar conversions.
235 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
Nadav Rotem2b0c96f2012-11-02 21:47:47 +0000236
Nadav Rotem955cf532012-11-11 05:34:45 +0000237 // Scalar bitcasts are usually free.
238 if (Opcode == Instruction::BitCast)
239 return 0;
240
241 if (Opcode == Instruction::Trunc &&
242 TLI->isTruncateFree(SrcLT.second, DstLT.second))
243 return 0;
244
245 if (Opcode == Instruction::ZExt &&
246 TLI->isZExtFree(SrcLT.second, DstLT.second))
Nadav Rotem2b0c96f2012-11-02 21:47:47 +0000247 return 0;
248
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000249 // Just check the op cost. If the operation is legal then assume it costs 1.
250 if (!TLI->isOperationExpand(ISD, DstLT.second))
251 return 1;
252
253 // Assume that illegal scalar instruction are expensive.
254 return 4;
255 }
256
257 // Check vector-to-vector casts.
258 if (Dst->isVectorTy() && Src->isVectorTy()) {
259
260 // If the cast is between same-sized registers, then the check is simple.
261 if (SrcLT.first == DstLT.first &&
262 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
263
264 // Bitcast between types that are legalized to the same type are free.
Nadav Rotem2d1528b2012-11-05 22:20:53 +0000265 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000266 return 0;
267
Nadav Rotem2d1528b2012-11-05 22:20:53 +0000268 // Assume that Zext is done using AND.
269 if (Opcode == Instruction::ZExt)
270 return 1;
271
272 // Assume that sext is done using SHL and SRA.
273 if (Opcode == Instruction::SExt)
274 return 2;
275
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000276 // Just check the op cost. If the operation is legal then assume it costs
277 // 1 and multiply by the type-legalization overhead.
278 if (!TLI->isOperationExpand(ISD, DstLT.second))
279 return SrcLT.first * 1;
Nadav Rotema5a3a612012-10-26 23:49:28 +0000280 }
Nadav Rotema5a3a612012-10-26 23:49:28 +0000281
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000282 // If we are converting vectors and the operation is illegal, or
283 // if the vectors are legalized to different types, estimate the
284 // scalarization costs.
Nadav Rotema5a3a612012-10-26 23:49:28 +0000285 unsigned Num = Dst->getVectorNumElements();
Hal Finkelc588e0e2012-10-30 02:41:57 +0000286 unsigned Cost = getCastInstrCost(Opcode, Dst->getScalarType(),
287 Src->getScalarType());
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000288
289 // Return the cost of multiple scalar invocation plus the cost of
290 // inserting and extracting the values.
291 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
Hal Finkelc588e0e2012-10-30 02:41:57 +0000292 }
293
Nadav Rotemd54fed22012-12-23 07:23:55 +0000294 // We already handled vector-to-vector and scalar-to-scalar conversions. This
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000295 // is where we handle bitcast between vectors and scalars. We need to assume
296 // that the conversion is scalarized in one way or another.
297 if (Opcode == Instruction::BitCast)
298 // Illegal bitcasts are done by storing and loading from a stack slot.
299 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
300 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
301
302 llvm_unreachable("Unhandled cast");
303 }
Nadav Rotema5a3a612012-10-26 23:49:28 +0000304
305unsigned VectorTargetTransformImpl::getCFInstrCost(unsigned Opcode) const {
Nadav Rotemd54fed22012-12-23 07:23:55 +0000306 // Branches are assumed to be predicted.
Nadav Rotem0602bb42012-12-05 21:21:26 +0000307 return 0;
Nadav Rotema5a3a612012-10-26 23:49:28 +0000308}
309
310unsigned VectorTargetTransformImpl::getCmpSelInstrCost(unsigned Opcode,
311 Type *ValTy,
312 Type *CondTy) const {
313 int ISD = InstructionOpcodeToISD(Opcode);
314 assert(ISD && "Invalid opcode");
Hans Wennborgb9051db2012-10-29 16:26:52 +0000315
Nadav Rotema5a3a612012-10-26 23:49:28 +0000316 // Selects on vectors are actually vector selects.
317 if (ISD == ISD::SELECT) {
318 assert(CondTy && "CondTy must exist");
319 if (CondTy->isVectorTy())
320 ISD = ISD::VSELECT;
321 }
322
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000323 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotema5a3a612012-10-26 23:49:28 +0000324
325 if (!TLI->isOperationExpand(ISD, LT.second)) {
326 // The operation is legal. Assume it costs 1. Multiply
327 // by the type-legalization overhead.
328 return LT.first * 1;
329 }
330
331 // Otherwise, assume that the cast is scalarized.
332 if (ValTy->isVectorTy()) {
333 unsigned Num = ValTy->getVectorNumElements();
334 if (CondTy)
335 CondTy = CondTy->getScalarType();
336 unsigned Cost = getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
337 CondTy);
338
Nadav Rotem75138f52012-11-05 21:11:10 +0000339 // Return the cost of multiple scalar invocation plus the cost of inserting
Nadav Rotema5a3a612012-10-26 23:49:28 +0000340 // and extracting the values.
341 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
342 }
343
Nadav Roteme4981602012-10-29 05:28:35 +0000344 // Unknown scalar opcode.
Nadav Rotema5a3a612012-10-26 23:49:28 +0000345 return 1;
346}
347
Nadav Rotema5a3a612012-10-26 23:49:28 +0000348unsigned VectorTargetTransformImpl::getVectorInstrCost(unsigned Opcode,
349 Type *Val,
350 unsigned Index) const {
351 return 1;
Nadav Rotem27048342012-10-24 17:22:41 +0000352}
353
354unsigned
Nadav Rotem27048342012-10-24 17:22:41 +0000355VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
356 unsigned Alignment,
357 unsigned AddressSpace) const {
Nadav Rotemc2a537b2012-12-21 01:24:36 +0000358 assert(!Src->isVoidTy() && "Invalid type");
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000359 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Src);
Nadav Rotema5a3a612012-10-26 23:49:28 +0000360
Nadav Rotem2652c502012-10-24 23:47:38 +0000361 // Assume that all loads of legal types cost 1.
362 return LT.first;
Nadav Rotem27048342012-10-24 17:22:41 +0000363}
Hal Finkel102a7c02012-10-26 04:28:02 +0000364
365unsigned
Paul Redmond88016662012-12-09 20:42:17 +0000366VectorTargetTransformImpl::getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
367 ArrayRef<Type*> Tys) const {
368 // assume that we need to scalarize this intrinsic.
369 unsigned ScalarizationCost = 0;
370 unsigned ScalarCalls = 1;
371 if (RetTy->isVectorTy()) {
372 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
373 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
374 }
375 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
376 if (Tys[i]->isVectorTy()) {
377 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
378 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
379 }
380 }
381 return ScalarCalls + ScalarizationCost;
382}
383
384unsigned
Hal Finkel102a7c02012-10-26 04:28:02 +0000385VectorTargetTransformImpl::getNumberOfParts(Type *Tp) const {
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000386 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Tp);
Nadav Roteme4981602012-10-29 05:28:35 +0000387 return LT.first;
Hal Finkel102a7c02012-10-26 04:28:02 +0000388}