blob: 27877a9320abeda9b68c99e50fb7748be005ef16 [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
52//===----------------------------------------------------------------------===//
53//
54// Calls used by the vectorizers.
55//
56//===----------------------------------------------------------------------===//
Renato Goline5372d62012-10-26 12:24:52 +000057static int InstructionOpcodeToISD(unsigned Opcode) {
58 enum InstructionOpcodes {
59#define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
60#define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
61#include "llvm/Instruction.def"
62 };
63 switch (static_cast<InstructionOpcodes>(Opcode)) {
64 case Ret: return 0;
65 case Br: return 0;
66 case Switch: return 0;
67 case IndirectBr: return 0;
68 case Invoke: return 0;
69 case Resume: return 0;
70 case Unreachable: return 0;
71 case Add: return ISD::ADD;
72 case FAdd: return ISD::FADD;
73 case Sub: return ISD::SUB;
74 case FSub: return ISD::FSUB;
75 case Mul: return ISD::MUL;
76 case FMul: return ISD::FMUL;
77 case UDiv: return ISD::UDIV;
78 case SDiv: return ISD::UDIV;
79 case FDiv: return ISD::FDIV;
80 case URem: return ISD::UREM;
81 case SRem: return ISD::SREM;
82 case FRem: return ISD::FREM;
83 case Shl: return ISD::SHL;
84 case LShr: return ISD::SRL;
85 case AShr: return ISD::SRA;
86 case And: return ISD::AND;
87 case Or: return ISD::OR;
88 case Xor: return ISD::XOR;
89 case Alloca: return 0;
90 case Load: return ISD::LOAD;
91 case Store: return ISD::STORE;
92 case GetElementPtr: return 0;
93 case Fence: return 0;
94 case AtomicCmpXchg: return 0;
95 case AtomicRMW: return 0;
96 case Trunc: return ISD::TRUNCATE;
97 case ZExt: return ISD::ZERO_EXTEND;
98 case SExt: return ISD::SEXTLOAD;
99 case FPToUI: return ISD::FP_TO_UINT;
100 case FPToSI: return ISD::FP_TO_SINT;
101 case UIToFP: return ISD::UINT_TO_FP;
102 case SIToFP: return ISD::SINT_TO_FP;
103 case FPTrunc: return ISD::FP_ROUND;
104 case FPExt: return ISD::FP_EXTEND;
105 case PtrToInt: return ISD::BITCAST;
106 case IntToPtr: return ISD::BITCAST;
107 case BitCast: return ISD::BITCAST;
108 case ICmp: return ISD::SETCC;
109 case FCmp: return ISD::SETCC;
110 case PHI: return 0;
111 case Call: return 0;
112 case Select: return ISD::SELECT;
113 case UserOp1: return 0;
114 case UserOp2: return 0;
115 case VAArg: return 0;
116 case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
117 case InsertElement: return ISD::INSERT_VECTOR_ELT;
118 case ShuffleVector: return ISD::VECTOR_SHUFFLE;
119 case ExtractValue: return ISD::MERGE_VALUES;
120 case InsertValue: return ISD::MERGE_VALUES;
121 case LandingPad: return 0;
122 }
Nadav Rotem2652c502012-10-24 23:47:38 +0000123
Renato Goline5372d62012-10-26 12:24:52 +0000124 llvm_unreachable("Unknown instruction type encountered!");
Nadav Rotem2652c502012-10-24 23:47:38 +0000125}
126
Nadav Rotem1e19e462012-10-25 18:17:48 +0000127std::pair<unsigned, EVT>
Nadav Rotem2652c502012-10-24 23:47:38 +0000128VectorTargetTransformImpl::getTypeLegalizationCost(LLVMContext &C,
Nadav Rotema5a3a612012-10-26 23:49:28 +0000129 EVT Ty) const {
Nadav Rotem2652c502012-10-24 23:47:38 +0000130 unsigned Cost = 1;
131 // We keep legalizing the type until we find a legal kind. We assume that
132 // the only operation that costs anything is the split. After splitting
133 // we need to handle two types.
134 while (true) {
135 TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, Ty);
136
137 if (LK.first == TargetLowering::TypeLegal)
Nadav Rotema5a3a612012-10-26 23:49:28 +0000138 return std::make_pair(Cost, Ty);
Nadav Rotem2652c502012-10-24 23:47:38 +0000139
140 if (LK.first == TargetLowering::TypeSplitVector)
141 Cost *= 2;
142
143 // Keep legalizing the type.
144 Ty = LK.second;
145 }
146}
Nadav Rotem27048342012-10-24 17:22:41 +0000147
148unsigned
Nadav Rotema5a3a612012-10-26 23:49:28 +0000149VectorTargetTransformImpl::getScalarizationOverhead(Type *Ty,
150 bool Insert,
151 bool Extract) const {
152 assert (Ty->isVectorTy() && "Can only scalarize vectors");
Hans Wennborgb9051db2012-10-29 16:26:52 +0000153 unsigned Cost = 0;
Nadav Rotem2652c502012-10-24 23:47:38 +0000154
Nadav Rotema5a3a612012-10-26 23:49:28 +0000155 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
156 if (Insert)
157 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
158 if (Extract)
159 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
Nadav Rotem1e19e462012-10-25 18:17:48 +0000160 }
161
Nadav Rotema5a3a612012-10-26 23:49:28 +0000162 return Cost;
163}
Nadav Rotem2652c502012-10-24 23:47:38 +0000164
Nadav Rotema5a3a612012-10-26 23:49:28 +0000165unsigned VectorTargetTransformImpl::getArithmeticInstrCost(unsigned Opcode,
166 Type *Ty) const {
167 // Check if any of the operands are vector operands.
168 int ISD = InstructionOpcodeToISD(Opcode);
169 assert(ISD && "Invalid opcode");
Nadav Rotem2652c502012-10-24 23:47:38 +0000170
Nadav Rotema5a3a612012-10-26 23:49:28 +0000171 std::pair<unsigned, EVT> LT =
172 getTypeLegalizationCost(Ty->getContext(), TLI->getValueType(Ty));
173
174 if (!TLI->isOperationExpand(ISD, LT.second)) {
Nadav Rotem2652c502012-10-24 23:47:38 +0000175 // The operation is legal. Assume it costs 1. Multiply
176 // by the type-legalization overhead.
177 return LT.first * 1;
178 }
179
Nadav Rotema5a3a612012-10-26 23:49:28 +0000180 // Else, assume that we need to scalarize this op.
181 if (Ty->isVectorTy()) {
182 unsigned Num = Ty->getVectorNumElements();
183 unsigned Cost = getArithmeticInstrCost(Opcode, Ty->getScalarType());
184 // return the cost of multiple scalar invocation plus the cost of inserting
185 // and extracting the values.
186 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
187 }
Nadav Rotem2652c502012-10-24 23:47:38 +0000188
Nadav Rotema5a3a612012-10-26 23:49:28 +0000189 // We don't know anything about this scalar instruction.
190 return 1;
191}
192
193unsigned VectorTargetTransformImpl::getBroadcastCost(Type *Tp) const {
194 return 1;
195}
196
197unsigned VectorTargetTransformImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
198 Type *Src) const {
Nadav Rotema5a3a612012-10-26 23:49:28 +0000199 int ISD = InstructionOpcodeToISD(Opcode);
200 assert(ISD && "Invalid opcode");
201
202 std::pair<unsigned, EVT> SrcLT =
203 getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
204
205 std::pair<unsigned, EVT> DstLT =
206 getTypeLegalizationCost(Dst->getContext(), TLI->getValueType(Dst));
207
208 // If the cast is between same-sized registers, then the check is simple.
209 if (SrcLT.first == DstLT.first &&
210 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
211 // Just check the op cost:
212 if (!TLI->isOperationExpand(ISD, DstLT.second)) {
213 // The operation is legal. Assume it costs 1. Multiply
214 // by the type-legalization overhead.
215 return SrcLT.first * 1;
216 }
217 }
218
Hal Finkelc588e0e2012-10-30 02:41:57 +0000219 unsigned ScalarizationCost = 1;
220
Nadav Rotema5a3a612012-10-26 23:49:28 +0000221 // Otherwise, assume that the cast is scalarized.
222 if (Dst->isVectorTy()) {
223 unsigned Num = Dst->getVectorNumElements();
224 unsigned Cost = getCastInstrCost(Opcode, Src->getScalarType(),
225 Dst->getScalarType());
226 // return the cost of multiple scalar invocation plus the cost of inserting
227 // and extracting the values.
Hal Finkelc588e0e2012-10-30 02:41:57 +0000228 ScalarizationCost *= getScalarizationOverhead(Dst, true, true) + Num * Cost;
Nadav Rotema5a3a612012-10-26 23:49:28 +0000229 }
230
Hal Finkelc588e0e2012-10-30 02:41:57 +0000231 if (Src->isVectorTy()) {
232 unsigned Num = Src->getVectorNumElements();
233 unsigned Cost = getCastInstrCost(Opcode, Dst->getScalarType(),
234 Src->getScalarType());
235 // return the cost of multiple scalar invocation plus the cost of inserting
236 // and extracting the values.
237 ScalarizationCost *= getScalarizationOverhead(Src, true, true) + Num * Cost;
238 }
239
240 return ScalarizationCost;
Nadav Rotema5a3a612012-10-26 23:49:28 +0000241}
242
243unsigned VectorTargetTransformImpl::getCFInstrCost(unsigned Opcode) const {
244 return 1;
245}
246
247unsigned VectorTargetTransformImpl::getCmpSelInstrCost(unsigned Opcode,
248 Type *ValTy,
249 Type *CondTy) const {
250 int ISD = InstructionOpcodeToISD(Opcode);
251 assert(ISD && "Invalid opcode");
Hans Wennborgb9051db2012-10-29 16:26:52 +0000252
Nadav Rotema5a3a612012-10-26 23:49:28 +0000253 // Selects on vectors are actually vector selects.
254 if (ISD == ISD::SELECT) {
255 assert(CondTy && "CondTy must exist");
256 if (CondTy->isVectorTy())
257 ISD = ISD::VSELECT;
258 }
259
260 std::pair<unsigned, EVT> LT =
261 getTypeLegalizationCost(ValTy->getContext(), TLI->getValueType(ValTy));
262
263 if (!TLI->isOperationExpand(ISD, LT.second)) {
264 // The operation is legal. Assume it costs 1. Multiply
265 // by the type-legalization overhead.
266 return LT.first * 1;
267 }
268
269 // Otherwise, assume that the cast is scalarized.
270 if (ValTy->isVectorTy()) {
271 unsigned Num = ValTy->getVectorNumElements();
272 if (CondTy)
273 CondTy = CondTy->getScalarType();
274 unsigned Cost = getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
275 CondTy);
276
277 // return the cost of multiple scalar invocation plus the cost of inserting
278 // and extracting the values.
279 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
280 }
281
Nadav Roteme4981602012-10-29 05:28:35 +0000282 // Unknown scalar opcode.
Nadav Rotema5a3a612012-10-26 23:49:28 +0000283 return 1;
284}
285
286/// Returns the expected cost of Vector Insert and Extract.
287unsigned VectorTargetTransformImpl::getVectorInstrCost(unsigned Opcode,
288 Type *Val,
289 unsigned Index) const {
290 return 1;
Nadav Rotem27048342012-10-24 17:22:41 +0000291}
292
293unsigned
Nadav Rotema5a3a612012-10-26 23:49:28 +0000294VectorTargetTransformImpl::getInstrCost(unsigned Opcode, Type *Ty1,
295 Type *Ty2) const {
Nadav Rotem27048342012-10-24 17:22:41 +0000296 return 1;
297}
298
299unsigned
300VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
301 unsigned Alignment,
302 unsigned AddressSpace) const {
Nadav Rotema5a3a612012-10-26 23:49:28 +0000303 std::pair<unsigned, EVT> LT =
Nadav Rotem2652c502012-10-24 23:47:38 +0000304 getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
Nadav Rotema5a3a612012-10-26 23:49:28 +0000305
Nadav Rotem2652c502012-10-24 23:47:38 +0000306 // Assume that all loads of legal types cost 1.
307 return LT.first;
Nadav Rotem27048342012-10-24 17:22:41 +0000308}
Hal Finkel102a7c02012-10-26 04:28:02 +0000309
310unsigned
311VectorTargetTransformImpl::getNumberOfParts(Type *Tp) const {
Nadav Roteme4981602012-10-29 05:28:35 +0000312 std::pair<unsigned, EVT> LT =
313 getTypeLegalizationCost(Tp->getContext(), TLI->getValueType(Tp));
314 return LT.first;
Hal Finkel102a7c02012-10-26 04:28:02 +0000315}
316