1 // llvm/Target/TargetTransformImpl.cpp - Target Loop Trans Info ---*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Target/TargetTransformImpl.h"
11 #include "llvm/Target/TargetLowering.h"
16 //===----------------------------------------------------------------------===//
18 // Calls used by scalar transformations.
20 //===----------------------------------------------------------------------===//
22 bool ScalarTargetTransformImpl::isLegalAddImmediate(int64_t imm) const {
23 return TLI->isLegalAddImmediate(imm);
26 bool ScalarTargetTransformImpl::isLegalICmpImmediate(int64_t imm) const {
27 return TLI->isLegalICmpImmediate(imm);
30 bool ScalarTargetTransformImpl::isLegalAddressingMode(const AddrMode &AM,
32 return TLI->isLegalAddressingMode(AM, Ty);
35 bool ScalarTargetTransformImpl::isTruncateFree(Type *Ty1, Type *Ty2) const {
36 return TLI->isTruncateFree(Ty1, Ty2);
39 bool ScalarTargetTransformImpl::isTypeLegal(Type *Ty) const {
40 EVT T = TLI->getValueType(Ty);
41 return TLI->isTypeLegal(T);
44 unsigned ScalarTargetTransformImpl::getJumpBufAlignment() const {
45 return TLI->getJumpBufAlignment();
48 unsigned ScalarTargetTransformImpl::getJumpBufSize() const {
49 return TLI->getJumpBufSize();
52 bool ScalarTargetTransformImpl::shouldBuildLookupTables() const {
53 return TLI->supportJumpTables() &&
54 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
55 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
58 //===----------------------------------------------------------------------===//
60 // Calls used by the vectorizers.
62 //===----------------------------------------------------------------------===//
63 static 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"
69 switch (static_cast<InstructionOpcodes>(Opcode)) {
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;
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;
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;
130 llvm_unreachable("Unknown instruction type encountered!");
133 std::pair<unsigned, EVT>
134 VectorTargetTransformImpl::getTypeLegalizationCost(LLVMContext &C,
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.
141 TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, Ty);
143 if (LK.first == TargetLowering::TypeLegal)
144 return std::make_pair(Cost, Ty);
146 if (LK.first == TargetLowering::TypeSplitVector)
149 // Keep legalizing the type.
155 VectorTargetTransformImpl::getScalarizationOverhead(Type *Ty,
157 bool Extract) const {
158 assert (Ty->isVectorTy() && "Can only scalarize vectors");
161 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
163 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
165 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
171 unsigned VectorTargetTransformImpl::getArithmeticInstrCost(unsigned Opcode,
173 // Check if any of the operands are vector operands.
174 int ISD = InstructionOpcodeToISD(Opcode);
175 assert(ISD && "Invalid opcode");
177 std::pair<unsigned, EVT> LT =
178 getTypeLegalizationCost(Ty->getContext(), TLI->getValueType(Ty));
180 if (!TLI->isOperationExpand(ISD, LT.second)) {
181 // The operation is legal. Assume it costs 1. Multiply
182 // by the type-legalization overhead.
186 // 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;
195 // We don't know anything about this scalar instruction.
199 unsigned VectorTargetTransformImpl::getBroadcastCost(Type *Tp) const {
203 unsigned VectorTargetTransformImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
205 int ISD = InstructionOpcodeToISD(Opcode);
206 assert(ISD && "Invalid opcode");
208 std::pair<unsigned, EVT> SrcLT =
209 getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
211 std::pair<unsigned, EVT> DstLT =
212 getTypeLegalizationCost(Dst->getContext(), TLI->getValueType(Dst));
214 // Handle scalar conversions.
215 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
216 // Just check the op cost. If the operation is legal then assume it costs 1.
217 if (!TLI->isOperationExpand(ISD, DstLT.second))
220 // Assume that illegal scalar instruction are expensive.
224 // Check vector-to-vector casts.
225 if (Dst->isVectorTy() && Src->isVectorTy()) {
227 // If the cast is between same-sized registers, then the check is simple.
228 if (SrcLT.first == DstLT.first &&
229 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
231 // Bitcast between types that are legalized to the same type are free.
232 if (Opcode == Instruction::BitCast)
235 // Just check the op cost. If the operation is legal then assume it costs
236 // 1 and multiply by the type-legalization overhead.
237 if (!TLI->isOperationExpand(ISD, DstLT.second))
238 return SrcLT.first * 1;
241 // If we are converting vectors and the operation is illegal, or
242 // if the vectors are legalized to different types, estimate the
243 // scalarization costs.
244 unsigned Num = Dst->getVectorNumElements();
245 unsigned Cost = getCastInstrCost(Opcode, Dst->getScalarType(),
246 Src->getScalarType());
248 // Return the cost of multiple scalar invocation plus the cost of
249 // inserting and extracting the values.
250 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
253 // We already handled vector-to-vector and scalar-to-scalar conversions. This
254 // is where we handle bitcast between vectors and scalars. We need to assume
255 // that the conversion is scalarized in one way or another.
256 if (Opcode == Instruction::BitCast)
257 // Illegal bitcasts are done by storing and loading from a stack slot.
258 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
259 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
261 llvm_unreachable("Unhandled cast");
264 unsigned VectorTargetTransformImpl::getCFInstrCost(unsigned Opcode) const {
268 unsigned VectorTargetTransformImpl::getCmpSelInstrCost(unsigned Opcode,
270 Type *CondTy) const {
271 int ISD = InstructionOpcodeToISD(Opcode);
272 assert(ISD && "Invalid opcode");
274 // Selects on vectors are actually vector selects.
275 if (ISD == ISD::SELECT) {
276 assert(CondTy && "CondTy must exist");
277 if (CondTy->isVectorTy())
281 std::pair<unsigned, EVT> LT =
282 getTypeLegalizationCost(ValTy->getContext(), TLI->getValueType(ValTy));
284 if (!TLI->isOperationExpand(ISD, LT.second)) {
285 // The operation is legal. Assume it costs 1. Multiply
286 // by the type-legalization overhead.
290 // Otherwise, assume that the cast is scalarized.
291 if (ValTy->isVectorTy()) {
292 unsigned Num = ValTy->getVectorNumElements();
294 CondTy = CondTy->getScalarType();
295 unsigned Cost = getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
298 // return the cost of multiple scalar invocation plus the cost of inserting
299 // and extracting the values.
300 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
303 // Unknown scalar opcode.
307 /// Returns the expected cost of Vector Insert and Extract.
308 unsigned VectorTargetTransformImpl::getVectorInstrCost(unsigned Opcode,
310 unsigned Index) const {
315 VectorTargetTransformImpl::getInstrCost(unsigned Opcode, Type *Ty1,
321 VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
323 unsigned AddressSpace) const {
324 std::pair<unsigned, EVT> LT =
325 getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
327 // Assume that all loads of legal types cost 1.
332 VectorTargetTransformImpl::getNumberOfParts(Type *Tp) const {
333 std::pair<unsigned, EVT> LT =
334 getTypeLegalizationCost(Tp->getContext(), TLI->getValueType(Tp));