blob: 7d663f57f98eb1a9c41b583b407e95dd5ffc0757 [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
30bool ScalarTargetTransformImpl::isLegalAddressingMode(const AddrMode &AM,
Hans Wennborgb9051db2012-10-29 16:26:52 +000031 Type *Ty) const {
Nadav Rotemcbd9a192012-10-18 23:22:48 +000032 return TLI->isLegalAddressingMode(AM, Ty);
33}
34
35bool ScalarTargetTransformImpl::isTruncateFree(Type *Ty1, Type *Ty2) const {
36 return TLI->isTruncateFree(Ty1, Ty2);
37}
38
39bool ScalarTargetTransformImpl::isTypeLegal(Type *Ty) const {
40 EVT T = TLI->getValueType(Ty);
41 return TLI->isTypeLegal(T);
42}
43
44unsigned ScalarTargetTransformImpl::getJumpBufAlignment() const {
45 return TLI->getJumpBufAlignment();
46}
47
48unsigned ScalarTargetTransformImpl::getJumpBufSize() const {
49 return TLI->getJumpBufSize();
50}
Nadav Rotem27048342012-10-24 17:22:41 +000051
Hans Wennborg04d7d132012-10-30 11:23:25 +000052bool ScalarTargetTransformImpl::shouldBuildLookupTables() const {
53 return TLI->supportJumpTables() &&
54 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
55 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
56}
57
Nadav Rotem27048342012-10-24 17:22:41 +000058//===----------------------------------------------------------------------===//
59//
60// Calls used by the vectorizers.
61//
62//===----------------------------------------------------------------------===//
Nadav Roteme6237022012-11-05 19:32:46 +000063int VectorTargetTransformImpl::InstructionOpcodeToISD(unsigned Opcode) const {
Renato Goline5372d62012-10-26 12:24:52 +000064 enum InstructionOpcodes {
65#define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
66#define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000067#include "llvm/IR/Instruction.def"
Renato Goline5372d62012-10-26 12:24:52 +000068 };
69 switch (static_cast<InstructionOpcodes>(Opcode)) {
70 case Ret: return 0;
71 case Br: return 0;
72 case Switch: return 0;
73 case IndirectBr: return 0;
74 case Invoke: return 0;
75 case Resume: return 0;
76 case Unreachable: return 0;
77 case Add: return ISD::ADD;
78 case FAdd: return ISD::FADD;
79 case Sub: return ISD::SUB;
80 case FSub: return ISD::FSUB;
81 case Mul: return ISD::MUL;
82 case FMul: return ISD::FMUL;
83 case UDiv: return ISD::UDIV;
84 case SDiv: return ISD::UDIV;
85 case FDiv: return ISD::FDIV;
86 case URem: return ISD::UREM;
87 case SRem: return ISD::SREM;
88 case FRem: return ISD::FREM;
89 case Shl: return ISD::SHL;
90 case LShr: return ISD::SRL;
91 case AShr: return ISD::SRA;
92 case And: return ISD::AND;
93 case Or: return ISD::OR;
94 case Xor: return ISD::XOR;
95 case Alloca: return 0;
96 case Load: return ISD::LOAD;
97 case Store: return ISD::STORE;
98 case GetElementPtr: return 0;
99 case Fence: return 0;
100 case AtomicCmpXchg: return 0;
101 case AtomicRMW: return 0;
102 case Trunc: return ISD::TRUNCATE;
103 case ZExt: return ISD::ZERO_EXTEND;
Nadav Rotem2d1528b2012-11-05 22:20:53 +0000104 case SExt: return ISD::SIGN_EXTEND;
Renato Goline5372d62012-10-26 12:24:52 +0000105 case FPToUI: return ISD::FP_TO_UINT;
106 case FPToSI: return ISD::FP_TO_SINT;
107 case UIToFP: return ISD::UINT_TO_FP;
108 case SIToFP: return ISD::SINT_TO_FP;
109 case FPTrunc: return ISD::FP_ROUND;
110 case FPExt: return ISD::FP_EXTEND;
111 case PtrToInt: return ISD::BITCAST;
112 case IntToPtr: return ISD::BITCAST;
113 case BitCast: return ISD::BITCAST;
114 case ICmp: return ISD::SETCC;
115 case FCmp: return ISD::SETCC;
116 case PHI: return 0;
117 case Call: return 0;
118 case Select: return ISD::SELECT;
119 case UserOp1: return 0;
120 case UserOp2: return 0;
121 case VAArg: return 0;
122 case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
123 case InsertElement: return ISD::INSERT_VECTOR_ELT;
124 case ShuffleVector: return ISD::VECTOR_SHUFFLE;
125 case ExtractValue: return ISD::MERGE_VALUES;
126 case InsertValue: return ISD::MERGE_VALUES;
127 case LandingPad: return 0;
128 }
Nadav Rotem2652c502012-10-24 23:47:38 +0000129
Renato Goline5372d62012-10-26 12:24:52 +0000130 llvm_unreachable("Unknown instruction type encountered!");
Nadav Rotem2652c502012-10-24 23:47:38 +0000131}
132
Nadav Roteme6237022012-11-05 19:32:46 +0000133std::pair<unsigned, MVT>
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000134VectorTargetTransformImpl::getTypeLegalizationCost(Type *Ty) const {
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000135 LLVMContext &C = Ty->getContext();
136 EVT MTy = TLI->getValueType(Ty);
137
Nadav Rotem2652c502012-10-24 23:47:38 +0000138 unsigned Cost = 1;
139 // We keep legalizing the type until we find a legal kind. We assume that
140 // the only operation that costs anything is the split. After splitting
141 // we need to handle two types.
142 while (true) {
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000143 TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, MTy);
Nadav Rotem2652c502012-10-24 23:47:38 +0000144
145 if (LK.first == TargetLowering::TypeLegal)
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000146 return std::make_pair(Cost, MTy.getSimpleVT());
Nadav Rotem2652c502012-10-24 23:47:38 +0000147
Nadav Rotem75138f52012-11-05 21:11:10 +0000148 if (LK.first == TargetLowering::TypeSplitVector ||
149 LK.first == TargetLowering::TypeExpandInteger)
Nadav Rotem2652c502012-10-24 23:47:38 +0000150 Cost *= 2;
151
152 // Keep legalizing the type.
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000153 MTy = LK.second;
Nadav Rotem2652c502012-10-24 23:47:38 +0000154 }
155}
Nadav Rotem27048342012-10-24 17:22:41 +0000156
157unsigned
Nadav Rotema5a3a612012-10-26 23:49:28 +0000158VectorTargetTransformImpl::getScalarizationOverhead(Type *Ty,
159 bool Insert,
160 bool Extract) const {
161 assert (Ty->isVectorTy() && "Can only scalarize vectors");
Hans Wennborgb9051db2012-10-29 16:26:52 +0000162 unsigned Cost = 0;
Nadav Rotem2652c502012-10-24 23:47:38 +0000163
Nadav Rotema5a3a612012-10-26 23:49:28 +0000164 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
165 if (Insert)
166 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
167 if (Extract)
168 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
Nadav Rotem1e19e462012-10-25 18:17:48 +0000169 }
170
Nadav Rotema5a3a612012-10-26 23:49:28 +0000171 return Cost;
172}
Nadav Rotem2652c502012-10-24 23:47:38 +0000173
Nadav Roteme5033192013-01-04 17:48:25 +0000174unsigned VectorTargetTransformImpl::getNumberOfRegisters(bool Vector) const {
175 return 8;
176}
177
Nadav Rotema5a3a612012-10-26 23:49:28 +0000178unsigned VectorTargetTransformImpl::getArithmeticInstrCost(unsigned Opcode,
179 Type *Ty) const {
180 // Check if any of the operands are vector operands.
181 int ISD = InstructionOpcodeToISD(Opcode);
182 assert(ISD && "Invalid opcode");
Nadav Rotem2652c502012-10-24 23:47:38 +0000183
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000184 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
Nadav Rotema5a3a612012-10-26 23:49:28 +0000185
Nadav Rotem6f3d81a2012-12-23 17:31:23 +0000186 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
187 // The operation is legal. Assume it costs 1.
188 // If the type is split to multiple registers, assume that thre is some
189 // overhead to this.
190 // TODO: Once we have extract/insert subvector cost we need to use them.
191 if (LT.first > 1)
192 return LT.first * 2;
Nadav Rotem2652c502012-10-24 23:47:38 +0000193 return LT.first * 1;
194 }
195
Nadav Rotem6f3d81a2012-12-23 17:31:23 +0000196 if (!TLI->isOperationExpand(ISD, LT.second)) {
197 // If the operation is custom lowered then assume
198 // thare the code is twice as expensive.
199 return LT.first * 2;
200 }
201
Nadav Rotema5a3a612012-10-26 23:49:28 +0000202 // Else, assume that we need to scalarize this op.
203 if (Ty->isVectorTy()) {
204 unsigned Num = Ty->getVectorNumElements();
205 unsigned Cost = getArithmeticInstrCost(Opcode, Ty->getScalarType());
206 // return the cost of multiple scalar invocation plus the cost of inserting
207 // and extracting the values.
208 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
209 }
Nadav Rotem2652c502012-10-24 23:47:38 +0000210
Nadav Rotema5a3a612012-10-26 23:49:28 +0000211 // We don't know anything about this scalar instruction.
212 return 1;
213}
214
Nadav Rotemdaf7b5c2012-12-24 08:57:47 +0000215unsigned VectorTargetTransformImpl::getShuffleCost(ShuffleKind Kind,
Hal Finkel82860f62013-01-03 02:34:09 +0000216 Type *Tp, int Index, Type *SubTp) const {
Nadav Rotema5a3a612012-10-26 23:49:28 +0000217 return 1;
218}
219
220unsigned VectorTargetTransformImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
221 Type *Src) const {
Nadav Rotema5a3a612012-10-26 23:49:28 +0000222 int ISD = InstructionOpcodeToISD(Opcode);
223 assert(ISD && "Invalid opcode");
224
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000225 std::pair<unsigned, MVT> SrcLT = getTypeLegalizationCost(Src);
226 std::pair<unsigned, MVT> DstLT = getTypeLegalizationCost(Dst);
Nadav Rotema5a3a612012-10-26 23:49:28 +0000227
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000228 // Handle scalar conversions.
229 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
Nadav Rotem2b0c96f2012-11-02 21:47:47 +0000230
Nadav Rotem955cf532012-11-11 05:34:45 +0000231 // Scalar bitcasts are usually free.
232 if (Opcode == Instruction::BitCast)
233 return 0;
234
235 if (Opcode == Instruction::Trunc &&
236 TLI->isTruncateFree(SrcLT.second, DstLT.second))
237 return 0;
238
239 if (Opcode == Instruction::ZExt &&
240 TLI->isZExtFree(SrcLT.second, DstLT.second))
Nadav Rotem2b0c96f2012-11-02 21:47:47 +0000241 return 0;
242
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000243 // Just check the op cost. If the operation is legal then assume it costs 1.
244 if (!TLI->isOperationExpand(ISD, DstLT.second))
245 return 1;
246
247 // Assume that illegal scalar instruction are expensive.
248 return 4;
249 }
250
251 // Check vector-to-vector casts.
252 if (Dst->isVectorTy() && Src->isVectorTy()) {
253
254 // If the cast is between same-sized registers, then the check is simple.
255 if (SrcLT.first == DstLT.first &&
256 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
257
258 // Bitcast between types that are legalized to the same type are free.
Nadav Rotem2d1528b2012-11-05 22:20:53 +0000259 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000260 return 0;
261
Nadav Rotem2d1528b2012-11-05 22:20:53 +0000262 // Assume that Zext is done using AND.
263 if (Opcode == Instruction::ZExt)
264 return 1;
265
266 // Assume that sext is done using SHL and SRA.
267 if (Opcode == Instruction::SExt)
268 return 2;
269
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000270 // Just check the op cost. If the operation is legal then assume it costs
271 // 1 and multiply by the type-legalization overhead.
272 if (!TLI->isOperationExpand(ISD, DstLT.second))
273 return SrcLT.first * 1;
Nadav Rotema5a3a612012-10-26 23:49:28 +0000274 }
Nadav Rotema5a3a612012-10-26 23:49:28 +0000275
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000276 // If we are converting vectors and the operation is illegal, or
277 // if the vectors are legalized to different types, estimate the
278 // scalarization costs.
Nadav Rotema5a3a612012-10-26 23:49:28 +0000279 unsigned Num = Dst->getVectorNumElements();
Hal Finkelc588e0e2012-10-30 02:41:57 +0000280 unsigned Cost = getCastInstrCost(Opcode, Dst->getScalarType(),
281 Src->getScalarType());
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000282
283 // Return the cost of multiple scalar invocation plus the cost of
284 // inserting and extracting the values.
285 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
Hal Finkelc588e0e2012-10-30 02:41:57 +0000286 }
287
Nadav Rotemd54fed22012-12-23 07:23:55 +0000288 // We already handled vector-to-vector and scalar-to-scalar conversions. This
Nadav Rotem0dba9a92012-10-31 20:52:26 +0000289 // is where we handle bitcast between vectors and scalars. We need to assume
290 // that the conversion is scalarized in one way or another.
291 if (Opcode == Instruction::BitCast)
292 // Illegal bitcasts are done by storing and loading from a stack slot.
293 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
294 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
295
296 llvm_unreachable("Unhandled cast");
297 }
Nadav Rotema5a3a612012-10-26 23:49:28 +0000298
299unsigned VectorTargetTransformImpl::getCFInstrCost(unsigned Opcode) const {
Nadav Rotemd54fed22012-12-23 07:23:55 +0000300 // Branches are assumed to be predicted.
Nadav Rotem0602bb42012-12-05 21:21:26 +0000301 return 0;
Nadav Rotema5a3a612012-10-26 23:49:28 +0000302}
303
304unsigned VectorTargetTransformImpl::getCmpSelInstrCost(unsigned Opcode,
305 Type *ValTy,
306 Type *CondTy) const {
307 int ISD = InstructionOpcodeToISD(Opcode);
308 assert(ISD && "Invalid opcode");
Hans Wennborgb9051db2012-10-29 16:26:52 +0000309
Nadav Rotema5a3a612012-10-26 23:49:28 +0000310 // Selects on vectors are actually vector selects.
311 if (ISD == ISD::SELECT) {
312 assert(CondTy && "CondTy must exist");
313 if (CondTy->isVectorTy())
314 ISD = ISD::VSELECT;
315 }
316
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000317 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
Nadav Rotema5a3a612012-10-26 23:49:28 +0000318
319 if (!TLI->isOperationExpand(ISD, LT.second)) {
320 // The operation is legal. Assume it costs 1. Multiply
321 // by the type-legalization overhead.
322 return LT.first * 1;
323 }
324
325 // Otherwise, assume that the cast is scalarized.
326 if (ValTy->isVectorTy()) {
327 unsigned Num = ValTy->getVectorNumElements();
328 if (CondTy)
329 CondTy = CondTy->getScalarType();
330 unsigned Cost = getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
331 CondTy);
332
Nadav Rotem75138f52012-11-05 21:11:10 +0000333 // Return the cost of multiple scalar invocation plus the cost of inserting
Nadav Rotema5a3a612012-10-26 23:49:28 +0000334 // and extracting the values.
335 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
336 }
337
Nadav Roteme4981602012-10-29 05:28:35 +0000338 // Unknown scalar opcode.
Nadav Rotema5a3a612012-10-26 23:49:28 +0000339 return 1;
340}
341
Nadav Rotema5a3a612012-10-26 23:49:28 +0000342unsigned VectorTargetTransformImpl::getVectorInstrCost(unsigned Opcode,
343 Type *Val,
344 unsigned Index) const {
345 return 1;
Nadav Rotem27048342012-10-24 17:22:41 +0000346}
347
348unsigned
Nadav Rotem27048342012-10-24 17:22:41 +0000349VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
350 unsigned Alignment,
351 unsigned AddressSpace) const {
Nadav Rotemc2a537b2012-12-21 01:24:36 +0000352 assert(!Src->isVoidTy() && "Invalid type");
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000353 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Src);
Nadav Rotema5a3a612012-10-26 23:49:28 +0000354
Nadav Rotem2652c502012-10-24 23:47:38 +0000355 // Assume that all loads of legal types cost 1.
356 return LT.first;
Nadav Rotem27048342012-10-24 17:22:41 +0000357}
Hal Finkel102a7c02012-10-26 04:28:02 +0000358
359unsigned
Paul Redmond88016662012-12-09 20:42:17 +0000360VectorTargetTransformImpl::getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
361 ArrayRef<Type*> Tys) const {
362 // assume that we need to scalarize this intrinsic.
363 unsigned ScalarizationCost = 0;
364 unsigned ScalarCalls = 1;
365 if (RetTy->isVectorTy()) {
366 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
367 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
368 }
369 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
370 if (Tys[i]->isVectorTy()) {
371 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
372 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
373 }
374 }
375 return ScalarCalls + ScalarizationCost;
376}
377
378unsigned
Hal Finkel102a7c02012-10-26 04:28:02 +0000379VectorTargetTransformImpl::getNumberOfParts(Type *Tp) const {
Nadav Rotem887c1fe2012-11-05 23:57:45 +0000380 std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Tp);
Nadav Roteme4981602012-10-29 05:28:35 +0000381 return LT.first;
Hal Finkel102a7c02012-10-26 04:28:02 +0000382}