blob: 38c704f2b1a6a02c5fae4b632c7dd50ad0ba5dde [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//===----------------------------------------------------------------------===//
Renato Goline5372d62012-10-26 12:24:52 +000063static int InstructionOpcodeToISD(unsigned Opcode) {
64 enum InstructionOpcodes {
65#define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
66#define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
67#include "llvm/Instruction.def"
68 };
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;
104 case SExt: return ISD::SEXTLOAD;
105 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 Rotem1e19e462012-10-25 18:17:48 +0000133std::pair<unsigned, EVT>
Nadav Rotem2652c502012-10-24 23:47:38 +0000134VectorTargetTransformImpl::getTypeLegalizationCost(LLVMContext &C,
Nadav Rotema5a3a612012-10-26 23:49:28 +0000135 EVT Ty) const {
Nadav Rotem2652c502012-10-24 23:47:38 +0000136 unsigned Cost = 1;
137 // We keep legalizing the type until we find a legal kind. We assume that
138 // the only operation that costs anything is the split. After splitting
139 // we need to handle two types.
140 while (true) {
141 TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, Ty);
142
143 if (LK.first == TargetLowering::TypeLegal)
Nadav Rotema5a3a612012-10-26 23:49:28 +0000144 return std::make_pair(Cost, Ty);
Nadav Rotem2652c502012-10-24 23:47:38 +0000145
146 if (LK.first == TargetLowering::TypeSplitVector)
147 Cost *= 2;
148
149 // Keep legalizing the type.
150 Ty = LK.second;
151 }
152}
Nadav Rotem27048342012-10-24 17:22:41 +0000153
154unsigned
Nadav Rotema5a3a612012-10-26 23:49:28 +0000155VectorTargetTransformImpl::getScalarizationOverhead(Type *Ty,
156 bool Insert,
157 bool Extract) const {
158 assert (Ty->isVectorTy() && "Can only scalarize vectors");
Hans Wennborgb9051db2012-10-29 16:26:52 +0000159 unsigned Cost = 0;
Nadav Rotem2652c502012-10-24 23:47:38 +0000160
Nadav Rotema5a3a612012-10-26 23:49:28 +0000161 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
162 if (Insert)
163 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
164 if (Extract)
165 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
Nadav Rotem1e19e462012-10-25 18:17:48 +0000166 }
167
Nadav Rotema5a3a612012-10-26 23:49:28 +0000168 return Cost;
169}
Nadav Rotem2652c502012-10-24 23:47:38 +0000170
Nadav Rotema5a3a612012-10-26 23:49:28 +0000171unsigned VectorTargetTransformImpl::getArithmeticInstrCost(unsigned Opcode,
172 Type *Ty) const {
173 // Check if any of the operands are vector operands.
174 int ISD = InstructionOpcodeToISD(Opcode);
175 assert(ISD && "Invalid opcode");
Nadav Rotem2652c502012-10-24 23:47:38 +0000176
Nadav Rotema5a3a612012-10-26 23:49:28 +0000177 std::pair<unsigned, EVT> LT =
178 getTypeLegalizationCost(Ty->getContext(), TLI->getValueType(Ty));
179
180 if (!TLI->isOperationExpand(ISD, LT.second)) {
Nadav Rotem2652c502012-10-24 23:47:38 +0000181 // The operation is legal. Assume it costs 1. Multiply
182 // by the type-legalization overhead.
183 return LT.first * 1;
184 }
185
Nadav Rotema5a3a612012-10-26 23:49:28 +0000186 // Else, assume that we need to scalarize this op.
187 if (Ty->isVectorTy()) {
188 unsigned Num = Ty->getVectorNumElements();
189 unsigned Cost = getArithmeticInstrCost(Opcode, Ty->getScalarType());
190 // return the cost of multiple scalar invocation plus the cost of inserting
191 // and extracting the values.
192 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
193 }
Nadav Rotem2652c502012-10-24 23:47:38 +0000194
Nadav Rotema5a3a612012-10-26 23:49:28 +0000195 // We don't know anything about this scalar instruction.
196 return 1;
197}
198
199unsigned VectorTargetTransformImpl::getBroadcastCost(Type *Tp) const {
200 return 1;
201}
202
203unsigned VectorTargetTransformImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
204 Type *Src) const {
Nadav Rotema5a3a612012-10-26 23:49:28 +0000205 int ISD = InstructionOpcodeToISD(Opcode);
206 assert(ISD && "Invalid opcode");
207
208 std::pair<unsigned, EVT> SrcLT =
209 getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
210
211 std::pair<unsigned, EVT> DstLT =
212 getTypeLegalizationCost(Dst->getContext(), TLI->getValueType(Dst));
213
214 // If the cast is between same-sized registers, then the check is simple.
215 if (SrcLT.first == DstLT.first &&
216 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
217 // Just check the op cost:
218 if (!TLI->isOperationExpand(ISD, DstLT.second)) {
219 // The operation is legal. Assume it costs 1. Multiply
220 // by the type-legalization overhead.
221 return SrcLT.first * 1;
222 }
223 }
224
Hal Finkelc588e0e2012-10-30 02:41:57 +0000225 unsigned ScalarizationCost = 1;
226
Nadav Rotema5a3a612012-10-26 23:49:28 +0000227 // Otherwise, assume that the cast is scalarized.
228 if (Dst->isVectorTy()) {
229 unsigned Num = Dst->getVectorNumElements();
230 unsigned Cost = getCastInstrCost(Opcode, Src->getScalarType(),
231 Dst->getScalarType());
232 // return the cost of multiple scalar invocation plus the cost of inserting
233 // and extracting the values.
Hal Finkelc588e0e2012-10-30 02:41:57 +0000234 ScalarizationCost *= getScalarizationOverhead(Dst, true, true) + Num * Cost;
Nadav Rotema5a3a612012-10-26 23:49:28 +0000235 }
236
Hal Finkelc588e0e2012-10-30 02:41:57 +0000237 if (Src->isVectorTy()) {
238 unsigned Num = Src->getVectorNumElements();
239 unsigned Cost = getCastInstrCost(Opcode, Dst->getScalarType(),
240 Src->getScalarType());
241 // return the cost of multiple scalar invocation plus the cost of inserting
242 // and extracting the values.
243 ScalarizationCost *= getScalarizationOverhead(Src, true, true) + Num * Cost;
244 }
245
246 return ScalarizationCost;
Nadav Rotema5a3a612012-10-26 23:49:28 +0000247}
248
249unsigned VectorTargetTransformImpl::getCFInstrCost(unsigned Opcode) const {
250 return 1;
251}
252
253unsigned VectorTargetTransformImpl::getCmpSelInstrCost(unsigned Opcode,
254 Type *ValTy,
255 Type *CondTy) const {
256 int ISD = InstructionOpcodeToISD(Opcode);
257 assert(ISD && "Invalid opcode");
Hans Wennborgb9051db2012-10-29 16:26:52 +0000258
Nadav Rotema5a3a612012-10-26 23:49:28 +0000259 // Selects on vectors are actually vector selects.
260 if (ISD == ISD::SELECT) {
261 assert(CondTy && "CondTy must exist");
262 if (CondTy->isVectorTy())
263 ISD = ISD::VSELECT;
264 }
265
266 std::pair<unsigned, EVT> LT =
267 getTypeLegalizationCost(ValTy->getContext(), TLI->getValueType(ValTy));
268
269 if (!TLI->isOperationExpand(ISD, LT.second)) {
270 // The operation is legal. Assume it costs 1. Multiply
271 // by the type-legalization overhead.
272 return LT.first * 1;
273 }
274
275 // Otherwise, assume that the cast is scalarized.
276 if (ValTy->isVectorTy()) {
277 unsigned Num = ValTy->getVectorNumElements();
278 if (CondTy)
279 CondTy = CondTy->getScalarType();
280 unsigned Cost = getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
281 CondTy);
282
283 // return the cost of multiple scalar invocation plus the cost of inserting
284 // and extracting the values.
285 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
286 }
287
Nadav Roteme4981602012-10-29 05:28:35 +0000288 // Unknown scalar opcode.
Nadav Rotema5a3a612012-10-26 23:49:28 +0000289 return 1;
290}
291
292/// Returns the expected cost of Vector Insert and Extract.
293unsigned VectorTargetTransformImpl::getVectorInstrCost(unsigned Opcode,
294 Type *Val,
295 unsigned Index) const {
296 return 1;
Nadav Rotem27048342012-10-24 17:22:41 +0000297}
298
299unsigned
Nadav Rotema5a3a612012-10-26 23:49:28 +0000300VectorTargetTransformImpl::getInstrCost(unsigned Opcode, Type *Ty1,
301 Type *Ty2) const {
Nadav Rotem27048342012-10-24 17:22:41 +0000302 return 1;
303}
304
305unsigned
306VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
307 unsigned Alignment,
308 unsigned AddressSpace) const {
Nadav Rotema5a3a612012-10-26 23:49:28 +0000309 std::pair<unsigned, EVT> LT =
Nadav Rotem2652c502012-10-24 23:47:38 +0000310 getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
Nadav Rotema5a3a612012-10-26 23:49:28 +0000311
Nadav Rotem2652c502012-10-24 23:47:38 +0000312 // Assume that all loads of legal types cost 1.
313 return LT.first;
Nadav Rotem27048342012-10-24 17:22:41 +0000314}
Hal Finkel102a7c02012-10-26 04:28:02 +0000315
316unsigned
317VectorTargetTransformImpl::getNumberOfParts(Type *Tp) const {
Nadav Roteme4981602012-10-29 05:28:35 +0000318 std::pair<unsigned, EVT> LT =
319 getTypeLegalizationCost(Tp->getContext(), TLI->getValueType(Tp));
320 return LT.first;
Hal Finkel102a7c02012-10-26 04:28:02 +0000321}