Use the 'count' attribute to calculate the upper bound of an array.
[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/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
136   LLVMContext &C = Ty->getContext();
137   EVT MTy = TLI->getValueType(Ty);
138
139   unsigned Cost = 1;
140   // We keep legalizing the type until we find a legal kind. We assume that
141   // the only operation that costs anything is the split. After splitting
142   // we need to handle two types.
143   while (true) {
144     TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, MTy);
145
146     if (LK.first == TargetLowering::TypeLegal)
147       return std::make_pair(Cost, MTy.getSimpleVT());
148
149     if (LK.first == TargetLowering::TypeSplitVector ||
150         LK.first == TargetLowering::TypeExpandInteger)
151       Cost *= 2;
152
153     // Keep legalizing the type.
154     MTy = LK.second;
155   }
156 }
157
158 unsigned
159 VectorTargetTransformImpl::getScalarizationOverhead(Type *Ty,
160                                                     bool Insert,
161                                                     bool Extract) const {
162   assert (Ty->isVectorTy() && "Can only scalarize vectors");
163   unsigned Cost = 0;
164
165   for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
166     if (Insert)
167       Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
168     if (Extract)
169       Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
170   }
171
172   return Cost;
173 }
174
175 unsigned VectorTargetTransformImpl::getArithmeticInstrCost(unsigned Opcode,
176                                                            Type *Ty) const {
177   // Check if any of the operands are vector operands.
178   int ISD = InstructionOpcodeToISD(Opcode);
179   assert(ISD && "Invalid opcode");
180
181   std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Ty);
182
183   if (!TLI->isOperationExpand(ISD, LT.second)) {
184     // The operation is legal. Assume it costs 1. Multiply
185     // by the type-legalization overhead.
186     return LT.first * 1;
187   }
188
189   // Else, assume that we need to scalarize this op.
190   if (Ty->isVectorTy()) {
191     unsigned Num = Ty->getVectorNumElements();
192     unsigned Cost = getArithmeticInstrCost(Opcode, Ty->getScalarType());
193     // return the cost of multiple scalar invocation plus the cost of inserting
194     // and extracting the values.
195     return getScalarizationOverhead(Ty, true, true) + Num * Cost;
196   }
197
198   // We don't know anything about this scalar instruction.
199   return 1;
200 }
201
202 unsigned VectorTargetTransformImpl::getBroadcastCost(Type *Tp) const {
203   return 1;
204 }
205
206 unsigned VectorTargetTransformImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
207                                   Type *Src) const {
208   int ISD = InstructionOpcodeToISD(Opcode);
209   assert(ISD && "Invalid opcode");
210
211   std::pair<unsigned, MVT> SrcLT = getTypeLegalizationCost(Src);
212   std::pair<unsigned, MVT> DstLT = getTypeLegalizationCost(Dst);
213
214   // Handle scalar conversions.
215   if (!Src->isVectorTy() && !Dst->isVectorTy()) {
216
217     // Scalar bitcasts are usually free.
218     if (Opcode == Instruction::BitCast)
219       return 0;
220
221     if (Opcode == Instruction::Trunc &&
222         TLI->isTruncateFree(SrcLT.second, DstLT.second))
223       return 0;
224
225     if (Opcode == Instruction::ZExt &&
226         TLI->isZExtFree(SrcLT.second, DstLT.second))
227       return 0;
228
229     // Just check the op cost. If the operation is legal then assume it costs 1.
230     if (!TLI->isOperationExpand(ISD, DstLT.second))
231       return  1;
232
233     // Assume that illegal scalar instruction are expensive.
234     return 4;
235   }
236
237   // Check vector-to-vector casts.
238   if (Dst->isVectorTy() && Src->isVectorTy()) {
239
240     // If the cast is between same-sized registers, then the check is simple.
241     if (SrcLT.first == DstLT.first &&
242         SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
243
244       // Bitcast between types that are legalized to the same type are free.
245       if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
246         return 0;
247
248       // Assume that Zext is done using AND.
249       if (Opcode == Instruction::ZExt)
250         return 1;
251
252       // Assume that sext is done using SHL and SRA.
253       if (Opcode == Instruction::SExt)
254         return 2;
255
256       // Just check the op cost. If the operation is legal then assume it costs
257       // 1 and multiply by the type-legalization overhead.
258       if (!TLI->isOperationExpand(ISD, DstLT.second))
259         return SrcLT.first * 1;
260     }
261
262     // If we are converting vectors and the operation is illegal, or
263     // if the vectors are legalized to different types, estimate the
264     // scalarization costs.
265     unsigned Num = Dst->getVectorNumElements();
266     unsigned Cost = getCastInstrCost(Opcode, Dst->getScalarType(),
267                                      Src->getScalarType());
268
269     // Return the cost of multiple scalar invocation plus the cost of
270     // inserting and extracting the values.
271     return getScalarizationOverhead(Dst, true, true) + Num * Cost;
272   }
273
274   // We already handled vector-to-vector and scalar-to-scalar conversions. This 
275   // is where we handle bitcast between vectors and scalars. We need to assume
276   //  that the conversion is scalarized in one way or another.
277   if (Opcode == Instruction::BitCast)
278     // Illegal bitcasts are done by storing and loading from a stack slot.
279     return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
280            (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
281
282   llvm_unreachable("Unhandled cast");
283  }
284
285 unsigned VectorTargetTransformImpl::getCFInstrCost(unsigned Opcode) const {
286   return 1;
287 }
288
289 unsigned VectorTargetTransformImpl::getCmpSelInstrCost(unsigned Opcode,
290                                                        Type *ValTy,
291                                                        Type *CondTy) const {
292   int ISD = InstructionOpcodeToISD(Opcode);
293   assert(ISD && "Invalid opcode");
294
295   // Selects on vectors are actually vector selects.
296   if (ISD == ISD::SELECT) {
297     assert(CondTy && "CondTy must exist");
298     if (CondTy->isVectorTy())
299       ISD = ISD::VSELECT;
300   }
301
302   std::pair<unsigned, MVT> LT = getTypeLegalizationCost(ValTy);
303
304   if (!TLI->isOperationExpand(ISD, LT.second)) {
305     // The operation is legal. Assume it costs 1. Multiply
306     // by the type-legalization overhead.
307     return LT.first * 1;
308   }
309
310   // Otherwise, assume that the cast is scalarized.
311   if (ValTy->isVectorTy()) {
312     unsigned Num = ValTy->getVectorNumElements();
313     if (CondTy)
314       CondTy = CondTy->getScalarType();
315     unsigned Cost = getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
316                                        CondTy);
317
318     // Return the cost of multiple scalar invocation plus the cost of inserting
319     // and extracting the values.
320     return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
321   }
322
323   // Unknown scalar opcode.
324   return 1;
325 }
326
327 unsigned VectorTargetTransformImpl::getVectorInstrCost(unsigned Opcode,
328                                                        Type *Val,
329                                                        unsigned Index) const {
330   return 1;
331 }
332
333 unsigned
334 VectorTargetTransformImpl::getInstrCost(unsigned Opcode, Type *Ty1,
335                                         Type *Ty2) const {
336   return 1;
337 }
338
339 unsigned
340 VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
341                                            unsigned Alignment,
342                                            unsigned AddressSpace) const {
343   std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Src);
344
345   // Assume that all loads of legal types cost 1.
346   return LT.first;
347 }
348
349 unsigned
350 VectorTargetTransformImpl::getNumberOfParts(Type *Tp) const {
351   std::pair<unsigned, MVT> LT = getTypeLegalizationCost(Tp);
352   return LT.first;
353 }