Minor cleanups.
[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 //===----------------------------------------------------------------------===//
53 //
54 // Calls used by the vectorizers.
55 //
56 //===----------------------------------------------------------------------===//
57 int InstructionOpcodeToISD(unsigned Opcode) {
58   static const int OpToISDTbl[] = {
59     /*Instruction::Ret           */ 0, // Opcode numbering start at #1.
60     /*Instruction::Br            */ 0,
61     /*Instruction::Switch        */ 0,
62     /*Instruction::IndirectBr    */ 0,
63     /*Instruction::Invoke        */ 0,
64     /*Instruction::Resume        */ 0,
65     /*Instruction::Unreachable   */ 0,
66     /*Instruction::Add           */ ISD::ADD,
67     /*Instruction::FAdd          */ ISD::FADD,
68     /*Instruction::Sub           */ ISD::SUB,
69     /*Instruction::FSub          */ ISD::FSUB,
70     /*Instruction::Mul           */ ISD::MUL,
71     /*Instruction::FMul          */ ISD::FMUL,
72     /*Instruction::UDiv          */ ISD::UDIV,
73     /*Instruction::SDiv          */ ISD::UDIV,
74     /*Instruction::FDiv          */ ISD::FDIV,
75     /*Instruction::URem          */ ISD::UREM,
76     /*Instruction::SRem          */ ISD::SREM,
77     /*Instruction::FRem          */ ISD::FREM,
78     /*Instruction::Shl           */ ISD::SHL,
79     /*Instruction::LShr          */ ISD::SRL,
80     /*Instruction::AShr          */ ISD::SRA,
81     /*Instruction::And           */ ISD::AND,
82     /*Instruction::Or            */ ISD::OR,
83     /*Instruction::Xor           */ ISD::XOR,
84     /*Instruction::Alloca        */ 0,
85     /*Instruction::Load          */ ISD::LOAD,
86     /*Instruction::Store         */ ISD::STORE,
87     /*Instruction::GetElementPtr */ 0,
88     /*Instruction::Fence         */ 0,
89     /*Instruction::AtomicCmpXchg */ 0,
90     /*Instruction::AtomicRMW     */ 0,
91     /*Instruction::Trunc         */ ISD::TRUNCATE,
92     /*Instruction::ZExt          */ ISD::ZERO_EXTEND,
93     /*Instruction::SExt          */ ISD::SEXTLOAD,
94     /*Instruction::FPToUI        */ ISD::FP_TO_UINT,
95     /*Instruction::FPToSI        */ ISD::FP_TO_SINT,
96     /*Instruction::UIToFP        */ ISD::UINT_TO_FP,
97     /*Instruction::SIToFP        */ ISD::SINT_TO_FP,
98     /*Instruction::FPTrunc       */ ISD::FP_ROUND,
99     /*Instruction::FPExt         */ ISD::FP_EXTEND,
100     /*Instruction::PtrToInt      */ ISD::BITCAST,
101     /*Instruction::IntToPtr      */ ISD::BITCAST,
102     /*Instruction::BitCast       */ ISD::BITCAST,
103     /*Instruction::ICmp          */ ISD::SETCC,
104     /*Instruction::FCmp          */ ISD::SETCC,
105     /*Instruction::PHI           */ 0,
106     /*Instruction::Call          */ 0,
107     /*Instruction::Select        */ ISD::SELECT,
108     /*Instruction::UserOp1       */ 0,
109     /*Instruction::UserOp2       */ 0,
110     /*Instruction::VAArg         */ 0,
111     /*Instruction::ExtractElement*/ ISD::EXTRACT_VECTOR_ELT,
112     /*Instruction::InsertElement */ ISD::INSERT_VECTOR_ELT,
113     /*Instruction::ShuffleVector */ ISD::VECTOR_SHUFFLE,
114     /*Instruction::ExtractValue  */ ISD::MERGE_VALUES,
115     /*Instruction::InsertValue   */ ISD::MERGE_VALUES,
116     /*Instruction::LandingPad    */ 0};
117
118   assert((Instruction::Ret == 1) && (Instruction::LandingPad == 58) &&
119          "Instruction order had changed");
120
121   // Opcode numbering starts at #1 but the table starts at #0, so we subtract
122   // one from the opcode number.
123   return OpToISDTbl[Opcode - 1];
124 }
125
126 std::pair<unsigned, EVT>
127 VectorTargetTransformImpl::getTypeLegalizationCost(LLVMContext &C,
128                                                          EVT Ty) const {
129   unsigned Cost = 1;
130   // We keep legalizing the type until we find a legal kind. We assume that
131   // the only operation that costs anything is the split. After splitting
132   // we need to handle two types.
133   while (true) {
134     TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, Ty);
135
136     if (LK.first == TargetLowering::TypeLegal)
137       return std::make_pair(Cost, LK.second);
138
139     if (LK.first == TargetLowering::TypeSplitVector)
140       Cost *= 2;
141
142     // Keep legalizing the type.
143     Ty = LK.second;
144   }
145 }
146
147 unsigned
148 VectorTargetTransformImpl::getInstrCost(unsigned Opcode, Type *Ty1,
149                                         Type *Ty2) const {
150   // Check if any of the operands are vector operands.
151   int ISD = InstructionOpcodeToISD(Opcode);
152
153   // If we don't have any information about this instruction assume it costs 1.
154   if (ISD == 0)
155     return 1;
156
157   // Selects on vectors are actually vector selects.
158   if (ISD == ISD::SELECT) {
159     assert(Ty2 && "Ty2 must hold the condition type");
160     if (Ty2->isVectorTy())
161     ISD = ISD::VSELECT;
162   }
163
164   assert(Ty1 && "We need to have at least one type");
165
166   // From this stage we look at the legalized type.
167   std::pair<unsigned, EVT>  LT =
168   getTypeLegalizationCost(Ty1->getContext(), TLI->getValueType(Ty1));
169
170   if (TLI->isOperationLegalOrCustom(ISD, LT.second)) {
171     // The operation is legal. Assume it costs 1. Multiply
172     // by the type-legalization overhead.
173     return LT.first * 1;
174   }
175
176   unsigned NumElem =
177     (LT.second.isVector() ? LT.second.getVectorNumElements() : 1);
178
179   // We will probably scalarize this instruction. Assume that the cost is the
180   // number of the vector elements.
181   return LT.first * NumElem * 1;
182 }
183
184 unsigned
185 VectorTargetTransformImpl::getBroadcastCost(Type *Tp) const {
186   return 1;
187 }
188
189 unsigned
190 VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
191                                            unsigned Alignment,
192                                            unsigned AddressSpace) const {
193   // From this stage we look at the legalized type.
194   std::pair<unsigned, EVT>  LT =
195   getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
196   // Assume that all loads of legal types cost 1.
197   return LT.first;
198 }