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 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};
118 assert((Instruction::Ret == 1) && (Instruction::LandingPad == 58) &&
119 "Instruction order had changed");
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];
126 std::pair<unsigned, EVT>
127 VectorTargetTransformImpl::getTypeLegalizationCost(LLVMContext &C,
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.
134 TargetLowering::LegalizeKind LK = TLI->getTypeConversion(C, Ty);
136 if (LK.first == TargetLowering::TypeLegal)
137 return std::make_pair(Cost, LK.second);
139 if (LK.first == TargetLowering::TypeSplitVector)
142 // Keep legalizing the type.
148 VectorTargetTransformImpl::getInstrCost(unsigned Opcode, Type *Ty1,
150 // Check if any of the operands are vector operands.
151 int ISD = InstructionOpcodeToISD(Opcode);
153 // Selects on vectors are actually vector selects.
154 if (ISD == ISD::SELECT) {
155 assert(Ty2 && "Ty2 must hold the select type");
156 if (Ty2->isVectorTy())
160 // If we don't have any information about this instruction assume it costs 1.
164 assert(Ty1 && "We need to have at least one type");
166 // From this stage we look at the legalized type.
167 std::pair<unsigned, EVT> LT =
168 getTypeLegalizationCost(Ty1->getContext(), TLI->getValueType(Ty1));
170 if (TLI->isOperationLegalOrCustom(ISD, LT.second)) {
171 // The operation is legal. Assume it costs 1. Multiply
172 // by the type-legalization overhead.
177 (LT.second.isVector() ? LT.second.getVectorNumElements() : 1);
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;
185 VectorTargetTransformImpl::getBroadcastCost(Type *Tp) const {
190 VectorTargetTransformImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
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.