Move all of the header files which are involved in modelling the LLVM IR
[oota-llvm.git] / lib / Target / TargetTransformImpl.cpp
1 // 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"
12 #include <utility>
13
14 using namespace llvm;
15
16 //===----------------------------------------------------------------------===//
17 //
18 // Calls used by scalar transformations.
19 //
20 //===----------------------------------------------------------------------===//
21
22 bool ScalarTargetTransformImpl::isLegalAddImmediate(int64_t imm) const {
23   return TLI->isLegalAddImmediate(imm);
24 }
25
26 bool ScalarTargetTransformImpl::isLegalICmpImmediate(int64_t imm) const {
27   return TLI->isLegalICmpImmediate(imm);
28 }
29
30 bool ScalarTargetTransformImpl::isLegalAddressingMode(const AddrMode &AM,
31                                                       Type *Ty) const {
32   return TLI->isLegalAddressingMode(AM, Ty);
33 }
34
35 bool ScalarTargetTransformImpl::isTruncateFree(Type *Ty1, Type *Ty2) const {
36   return TLI->isTruncateFree(Ty1, Ty2);
37 }
38
39 bool ScalarTargetTransformImpl::isTypeLegal(Type *Ty) const {
40   EVT T = TLI->getValueType(Ty);
41   return TLI->isTypeLegal(T);
42 }
43
44 unsigned ScalarTargetTransformImpl::getJumpBufAlignment() const {
45   return TLI->getJumpBufAlignment();
46 }
47
48 unsigned ScalarTargetTransformImpl::getJumpBufSize() const {
49   return TLI->getJumpBufSize();
50 }
51
52 bool ScalarTargetTransformImpl::shouldBuildLookupTables() const {
53   return TLI->supportJumpTables() &&
54       (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
55        TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
56 }
57
58 //===----------------------------------------------------------------------===//
59 //
60 // Calls used by the vectorizers.
61 //
62 //===----------------------------------------------------------------------===//
63 int VectorTargetTransformImpl::InstructionOpcodeToISD(unsigned Opcode) const {
64   enum InstructionOpcodes {
65 #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
66 #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
67 #include "llvm/IR/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::SIGN_EXTEND;
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   }
129
130   llvm_unreachable("Unknown instruction type encountered!");
131 }
132
133 std::pair<unsigned, MVT>
134 VectorTargetTransformImpl::getTypeLegalizationCost(Type *Ty) const {
135   LLVMContext &C = Ty->getContext();
136   EVT MTy = TLI->getValueType(Ty);
137
138   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) {
143     TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, MTy);
144
145     if (LK.first == TargetLowering::TypeLegal)
146       return std::make_pair(Cost, MTy.getSimpleVT());
147
148     if (LK.first == TargetLowering::TypeSplitVector ||
149         LK.first == TargetLowering::TypeExpandInteger)
150       Cost *= 2;
151
152     // Keep legalizing the type.
153     MTy = LK.second;
154   }
155 }
156
157 unsigned
158 VectorTargetTransformImpl::getScalarizationOverhead(Type *Ty,
159                                                     bool Insert,
160                                                     bool Extract) const {
161   assert (Ty->isVectorTy() && "Can only scalarize vectors");
162   unsigned Cost = 0;
163
164   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);
169   }
170
171   return Cost;
172 }
173
174 unsigned VectorTargetTransformImpl::getArithmeticInstrCost(unsigned Opcode,
175                                                            Type *Ty) const {
176   // Check if any of the operands are vector operands.
177   int ISD = InstructionOpcodeToISD(Opcode);
178   assert(ISD && "Invalid opcode");
179
180   std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
181
182   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
183     // The operation is legal. Assume it costs 1.
184     // If the type is split to multiple registers, assume that thre is some
185     // overhead to this.
186     // TODO: Once we have extract/insert subvector cost we need to use them.
187     if (LT.first > 1)
188       return LT.first * 2;
189     return LT.first * 1;
190   }
191
192   if (!TLI->isOperationExpand(ISD, LT.second)) {
193     // If the operation is custom lowered then assume
194     // thare the code is twice as expensive.
195     return LT.first * 2;
196   }
197
198   // Else, assume that we need to scalarize this op.
199   if (Ty->isVectorTy()) {
200     unsigned Num = Ty->getVectorNumElements();
201     unsigned Cost = getArithmeticInstrCost(Opcode, Ty->getScalarType());
202     // return the cost of multiple scalar invocation plus the cost of inserting
203     // and extracting the values.
204     return getScalarizationOverhead(Ty, true, true) + Num * Cost;
205   }
206
207   // We don't know anything about this scalar instruction.
208   return 1;
209 }
210
211 unsigned VectorTargetTransformImpl::getShuffleCost(ShuffleKind Kind,
212                                                    Type *Tp,
213                                                    int Index) const {
214   return 1;
215 }
216
217 unsigned VectorTargetTransformImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
218                                   Type *Src) const {
219   int ISD = InstructionOpcodeToISD(Opcode);
220   assert(ISD && "Invalid opcode");
221
222   std::pair<unsigned, MVT> SrcLT = getTypeLegalizationCost(Src);
223   std::pair<unsigned, MVT> DstLT = getTypeLegalizationCost(Dst);
224
225   // Handle scalar conversions.
226   if (!Src->isVectorTy() && !Dst->isVectorTy()) {
227
228     // Scalar bitcasts are usually free.
229     if (Opcode == Instruction::BitCast)
230       return 0;
231
232     if (Opcode == Instruction::Trunc &&
233         TLI->isTruncateFree(SrcLT.second, DstLT.second))
234       return 0;
235
236     if (Opcode == Instruction::ZExt &&
237         TLI->isZExtFree(SrcLT.second, DstLT.second))
238       return 0;
239
240     // Just check the op cost. If the operation is legal then assume it costs 1.
241     if (!TLI->isOperationExpand(ISD, DstLT.second))
242       return  1;
243
244     // Assume that illegal scalar instruction are expensive.
245     return 4;
246   }
247
248   // Check vector-to-vector casts.
249   if (Dst->isVectorTy() && Src->isVectorTy()) {
250
251     // If the cast is between same-sized registers, then the check is simple.
252     if (SrcLT.first == DstLT.first &&
253         SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
254
255       // Bitcast between types that are legalized to the same type are free.
256       if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
257         return 0;
258
259       // Assume that Zext is done using AND.
260       if (Opcode == Instruction::ZExt)
261         return 1;
262
263       // Assume that sext is done using SHL and SRA.
264       if (Opcode == Instruction::SExt)
265         return 2;
266
267       // Just check the op cost. If the operation is legal then assume it costs
268       // 1 and multiply by the type-legalization overhead.
269       if (!TLI->isOperationExpand(ISD, DstLT.second))
270         return SrcLT.first * 1;
271     }
272
273     // If we are converting vectors and the operation is illegal, or
274     // if the vectors are legalized to different types, estimate the
275     // scalarization costs.
276     unsigned Num = Dst->getVectorNumElements();
277     unsigned Cost = getCastInstrCost(Opcode, Dst->getScalarType(),
278                                      Src->getScalarType());
279
280     // Return the cost of multiple scalar invocation plus the cost of
281     // inserting and extracting the values.
282     return getScalarizationOverhead(Dst, true, true) + Num * Cost;
283   }
284
285   // We already handled vector-to-vector and scalar-to-scalar conversions. This
286   // is where we handle bitcast between vectors and scalars. We need to assume
287   //  that the conversion is scalarized in one way or another.
288   if (Opcode == Instruction::BitCast)
289     // Illegal bitcasts are done by storing and loading from a stack slot.
290     return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
291            (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
292
293   llvm_unreachable("Unhandled cast");
294  }
295
296 unsigned VectorTargetTransformImpl::getCFInstrCost(unsigned Opcode) const {
297   // Branches are assumed to be predicted.
298   return 0;
299 }
300
301 unsigned VectorTargetTransformImpl::getCmpSelInstrCost(unsigned Opcode,
302                                                        Type *ValTy,
303                                                        Type *CondTy) const {
304   int ISD = InstructionOpcodeToISD(Opcode);
305   assert(ISD && "Invalid opcode");
306
307   // Selects on vectors are actually vector selects.
308   if (ISD == ISD::SELECT) {
309     assert(CondTy && "CondTy must exist");
310     if (CondTy->isVectorTy())
311       ISD = ISD::VSELECT;
312   }
313
314   std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
315
316   if (!TLI->isOperationExpand(ISD, LT.second)) {
317     // The operation is legal. Assume it costs 1. Multiply
318     // by the type-legalization overhead.
319     return LT.first * 1;
320   }
321
322   // Otherwise, assume that the cast is scalarized.
323   if (ValTy->isVectorTy()) {
324     unsigned Num = ValTy->getVectorNumElements();
325     if (CondTy)
326       CondTy = CondTy->getScalarType();
327     unsigned Cost = getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
328                                        CondTy);
329
330     // Return the cost of multiple scalar invocation plus the cost of inserting
331     // and extracting the values.
332     return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
333   }
334
335   // Unknown scalar opcode.
336   return 1;
337 }
338
339 unsigned VectorTargetTransformImpl::getVectorInstrCost(unsigned Opcode,
340                                                        Type *Val,
341                                                        unsigned Index) const {
342   return 1;
343 }
344
345 unsigned
346 VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
347                                            unsigned Alignment,
348                                            unsigned AddressSpace) const {
349   assert(!Src->isVoidTy() && "Invalid type");
350   std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Src);
351
352   // Assume that all loads of legal types cost 1.
353   return LT.first;
354 }
355
356 unsigned
357 VectorTargetTransformImpl::getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
358                                                  ArrayRef<Type*> Tys) const {
359   // assume that we need to scalarize this intrinsic.
360   unsigned ScalarizationCost = 0;
361   unsigned ScalarCalls = 1;
362   if (RetTy->isVectorTy()) {
363     ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
364     ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
365   }
366   for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
367     if (Tys[i]->isVectorTy()) {
368       ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
369       ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
370     }
371   }
372   return ScalarCalls + ScalarizationCost;
373 }
374
375 unsigned
376 VectorTargetTransformImpl::getNumberOfParts(Type *Tp) const {
377   std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Tp);
378   return LT.first;
379 }