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 //===----------------------------------------------------------------------===//
54 // Calls used by the vectorizers.
56 //===----------------------------------------------------------------------===//
57 static 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"
63 switch (static_cast<InstructionOpcodes>(Opcode)) {
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;
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;
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;
124 llvm_unreachable("Unknown instruction type encountered!");
127 std::pair<unsigned, EVT>
128 VectorTargetTransformImpl::getTypeLegalizationCost(LLVMContext &C,
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.
135 TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, Ty);
137 if (LK.first == TargetLowering::TypeLegal)
138 return std::make_pair(Cost, Ty);
140 if (LK.first == TargetLowering::TypeSplitVector)
143 // Keep legalizing the type.
149 VectorTargetTransformImpl::getScalarizationOverhead(Type *Ty,
151 bool Extract) const {
152 assert (Ty->isVectorTy() && "Can only scalarize vectors");
155 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
157 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
159 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
165 unsigned VectorTargetTransformImpl::getArithmeticInstrCost(unsigned Opcode,
167 // Check if any of the operands are vector operands.
168 int ISD = InstructionOpcodeToISD(Opcode);
169 assert(ISD && "Invalid opcode");
171 std::pair<unsigned, EVT> LT =
172 getTypeLegalizationCost(Ty->getContext(), TLI->getValueType(Ty));
174 if (!TLI->isOperationExpand(ISD, LT.second)) {
175 // The operation is legal. Assume it costs 1. Multiply
176 // by the type-legalization overhead.
180 // 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;
189 // We don't know anything about this scalar instruction.
193 unsigned VectorTargetTransformImpl::getBroadcastCost(Type *Tp) const {
197 unsigned VectorTargetTransformImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
199 assert(Src->isVectorTy() == Dst->isVectorTy() && "Invalid input types");
200 int ISD = InstructionOpcodeToISD(Opcode);
201 assert(ISD && "Invalid opcode");
203 std::pair<unsigned, EVT> SrcLT =
204 getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
206 std::pair<unsigned, EVT> DstLT =
207 getTypeLegalizationCost(Dst->getContext(), TLI->getValueType(Dst));
209 // If the cast is between same-sized registers, then the check is simple.
210 if (SrcLT.first == DstLT.first &&
211 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
212 // Just check the op cost:
213 if (!TLI->isOperationExpand(ISD, DstLT.second)) {
214 // The operation is legal. Assume it costs 1. Multiply
215 // by the type-legalization overhead.
216 return SrcLT.first * 1;
220 // Otherwise, assume that the cast is scalarized.
221 if (Dst->isVectorTy()) {
222 unsigned Num = Dst->getVectorNumElements();
223 unsigned Cost = getCastInstrCost(Opcode, Src->getScalarType(),
224 Dst->getScalarType());
225 // return the cost of multiple scalar invocation plus the cost of inserting
226 // and extracting the values.
227 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
230 // Unknown scalar opcode.
234 unsigned VectorTargetTransformImpl::getCFInstrCost(unsigned Opcode) const {
238 unsigned VectorTargetTransformImpl::getCmpSelInstrCost(unsigned Opcode,
240 Type *CondTy) const {
241 int ISD = InstructionOpcodeToISD(Opcode);
242 assert(ISD && "Invalid opcode");
244 // Selects on vectors are actually vector selects.
245 if (ISD == ISD::SELECT) {
246 assert(CondTy && "CondTy must exist");
247 if (CondTy->isVectorTy())
251 std::pair<unsigned, EVT> LT =
252 getTypeLegalizationCost(ValTy->getContext(), TLI->getValueType(ValTy));
254 if (!TLI->isOperationExpand(ISD, LT.second)) {
255 // The operation is legal. Assume it costs 1. Multiply
256 // by the type-legalization overhead.
260 // Otherwise, assume that the cast is scalarized.
261 if (ValTy->isVectorTy()) {
262 unsigned Num = ValTy->getVectorNumElements();
264 CondTy = CondTy->getScalarType();
265 unsigned Cost = getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
268 // return the cost of multiple scalar invocation plus the cost of inserting
269 // and extracting the values.
270 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
273 // Unknown scalar opcode.
277 /// Returns the expected cost of Vector Insert and Extract.
278 unsigned VectorTargetTransformImpl::getVectorInstrCost(unsigned Opcode,
280 unsigned Index) const {
285 VectorTargetTransformImpl::getInstrCost(unsigned Opcode, Type *Ty1,
291 VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
293 unsigned AddressSpace) const {
294 std::pair<unsigned, EVT> LT =
295 getTypeLegalizationCost(Src->getContext(), TLI->getValueType(Src));
297 // Assume that all loads of legal types cost 1.
302 VectorTargetTransformImpl::getNumberOfParts(Type *Tp) const {
303 std::pair<unsigned, EVT> LT =
304 getTypeLegalizationCost(Tp->getContext(), TLI->getValueType(Tp));