blob: 3892cc4dd551fdf71258013c5ce6b17acc911050 [file] [log] [blame]
Chandler Carruthaeef83c2013-01-07 01:37:14 +00001//===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===//
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/// \file
10/// This file provides the implementation of a basic TargetTransformInfo pass
11/// predicated on the target abstractions present in the target independent
12/// code generator. It uses these (primarily TargetLowering) to model as much
13/// of the TTI query interface as possible. It is included by most targets so
14/// that they can specialize only a small subset of the query space.
15///
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "basictti"
19#include "llvm/CodeGen/Passes.h"
Chandler Carruthbe049292013-01-07 03:08:10 +000020#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthaeef83c2013-01-07 01:37:14 +000021#include "llvm/Target/TargetLowering.h"
Chandler Carruthaeef83c2013-01-07 01:37:14 +000022#include <utility>
23
24using namespace llvm;
25
26namespace {
27
28class BasicTTI : public ImmutablePass, public TargetTransformInfo {
29 const TargetLowering *TLI;
30
31 /// Estimate the overhead of scalarizing an instruction. Insert and Extract
32 /// are set if the result needs to be inserted and/or extracted from vectors.
33 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
34
35public:
36 BasicTTI() : ImmutablePass(ID), TLI(0) {
37 llvm_unreachable("This pass cannot be directly constructed");
38 }
39
40 BasicTTI(const TargetLowering *TLI) : ImmutablePass(ID), TLI(TLI) {
41 initializeBasicTTIPass(*PassRegistry::getPassRegistry());
42 }
43
44 virtual void initializePass() {
45 pushTTIStack(this);
46 }
47
48 virtual void finalizePass() {
49 popTTIStack();
50 }
51
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 TargetTransformInfo::getAnalysisUsage(AU);
54 }
55
56 /// Pass identification.
57 static char ID;
58
59 /// Provide necessary pointer adjustments for the two base classes.
60 virtual void *getAdjustedAnalysisPointer(const void *ID) {
61 if (ID == &TargetTransformInfo::ID)
62 return (TargetTransformInfo*)this;
63 return this;
64 }
65
66 /// \name Scalar TTI Implementations
67 /// @{
68
69 virtual bool isLegalAddImmediate(int64_t imm) const;
70 virtual bool isLegalICmpImmediate(int64_t imm) const;
71 virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
72 int64_t BaseOffset, bool HasBaseReg,
73 int64_t Scale) const;
74 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
75 virtual bool isTypeLegal(Type *Ty) const;
76 virtual unsigned getJumpBufAlignment() const;
77 virtual unsigned getJumpBufSize() const;
78 virtual bool shouldBuildLookupTables() const;
79
80 /// @}
81
82 /// \name Vector TTI Implementations
83 /// @{
84
85 virtual unsigned getNumberOfRegisters(bool Vector) const;
Nadav Rotem83be7b02013-01-09 01:15:42 +000086 virtual unsigned getMaximumUnrollFactor() const;
Nadav Rotem14925e62013-01-09 22:29:00 +000087 virtual unsigned getRegisterBitWidth(bool Vector) const;
Chandler Carruthaeef83c2013-01-07 01:37:14 +000088 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const;
89 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
90 int Index, Type *SubTp) const;
91 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
92 Type *Src) const;
93 virtual unsigned getCFInstrCost(unsigned Opcode) const;
94 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
95 Type *CondTy) const;
96 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
97 unsigned Index) const;
98 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
99 unsigned Alignment,
100 unsigned AddressSpace) const;
101 virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
102 ArrayRef<Type*> Tys) const;
103 virtual unsigned getNumberOfParts(Type *Tp) const;
104
105 /// @}
106};
107
108}
109
110INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
111 "Target independent code generator's TTI", true, true, false)
112char BasicTTI::ID = 0;
113
114ImmutablePass *
115llvm::createBasicTargetTransformInfoPass(const TargetLowering *TLI) {
116 return new BasicTTI(TLI);
117}
118
119
120bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
121 return TLI->isLegalAddImmediate(imm);
122}
123
124bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
125 return TLI->isLegalICmpImmediate(imm);
126}
127
128bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
129 int64_t BaseOffset, bool HasBaseReg,
130 int64_t Scale) const {
Chandler Carruth56d433d2013-01-07 15:14:13 +0000131 TargetLowering::AddrMode AM;
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000132 AM.BaseGV = BaseGV;
133 AM.BaseOffs = BaseOffset;
134 AM.HasBaseReg = HasBaseReg;
135 AM.Scale = Scale;
136 return TLI->isLegalAddressingMode(AM, Ty);
137}
138
139bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
140 return TLI->isTruncateFree(Ty1, Ty2);
141}
142
143bool BasicTTI::isTypeLegal(Type *Ty) const {
144 EVT T = TLI->getValueType(Ty);
145 return TLI->isTypeLegal(T);
146}
147
148unsigned BasicTTI::getJumpBufAlignment() const {
149 return TLI->getJumpBufAlignment();
150}
151
152unsigned BasicTTI::getJumpBufSize() const {
153 return TLI->getJumpBufSize();
154}
155
156bool BasicTTI::shouldBuildLookupTables() const {
157 return TLI->supportJumpTables() &&
158 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
159 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
160}
161
162//===----------------------------------------------------------------------===//
163//
164// Calls used by the vectorizers.
165//
166//===----------------------------------------------------------------------===//
167
168unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
169 bool Extract) const {
170 assert (Ty->isVectorTy() && "Can only scalarize vectors");
171 unsigned Cost = 0;
172
173 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
174 if (Insert)
175 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
176 if (Extract)
177 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
178 }
179
180 return Cost;
181}
182
183unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
184 return 1;
185}
186
Nadav Rotem14925e62013-01-09 22:29:00 +0000187unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
188 return 32;
189}
190
Nadav Rotem83be7b02013-01-09 01:15:42 +0000191unsigned BasicTTI::getMaximumUnrollFactor() const {
192 return 1;
193}
194
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000195unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
196 // Check if any of the operands are vector operands.
197 int ISD = TLI->InstructionOpcodeToISD(Opcode);
198 assert(ISD && "Invalid opcode");
199
200 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
201
202 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
203 // The operation is legal. Assume it costs 1.
204 // If the type is split to multiple registers, assume that thre is some
205 // overhead to this.
206 // TODO: Once we have extract/insert subvector cost we need to use them.
207 if (LT.first > 1)
208 return LT.first * 2;
209 return LT.first * 1;
210 }
211
212 if (!TLI->isOperationExpand(ISD, LT.second)) {
213 // If the operation is custom lowered then assume
214 // thare the code is twice as expensive.
215 return LT.first * 2;
216 }
217
218 // Else, assume that we need to scalarize this op.
219 if (Ty->isVectorTy()) {
220 unsigned Num = Ty->getVectorNumElements();
221 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
222 // return the cost of multiple scalar invocation plus the cost of inserting
223 // and extracting the values.
224 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
225 }
226
227 // We don't know anything about this scalar instruction.
228 return 1;
229}
230
231unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
232 Type *SubTp) const {
233 return 1;
234}
235
236unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
237 Type *Src) const {
238 int ISD = TLI->InstructionOpcodeToISD(Opcode);
239 assert(ISD && "Invalid opcode");
240
241 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
242 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
243
244 // Handle scalar conversions.
245 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
246
247 // Scalar bitcasts are usually free.
248 if (Opcode == Instruction::BitCast)
249 return 0;
250
251 if (Opcode == Instruction::Trunc &&
252 TLI->isTruncateFree(SrcLT.second, DstLT.second))
253 return 0;
254
255 if (Opcode == Instruction::ZExt &&
256 TLI->isZExtFree(SrcLT.second, DstLT.second))
257 return 0;
258
259 // Just check the op cost. If the operation is legal then assume it costs 1.
260 if (!TLI->isOperationExpand(ISD, DstLT.second))
261 return 1;
262
263 // Assume that illegal scalar instruction are expensive.
264 return 4;
265 }
266
267 // Check vector-to-vector casts.
268 if (Dst->isVectorTy() && Src->isVectorTy()) {
269
270 // If the cast is between same-sized registers, then the check is simple.
271 if (SrcLT.first == DstLT.first &&
272 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
273
274 // Bitcast between types that are legalized to the same type are free.
275 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
276 return 0;
277
278 // Assume that Zext is done using AND.
279 if (Opcode == Instruction::ZExt)
280 return 1;
281
282 // Assume that sext is done using SHL and SRA.
283 if (Opcode == Instruction::SExt)
284 return 2;
285
286 // Just check the op cost. If the operation is legal then assume it costs
287 // 1 and multiply by the type-legalization overhead.
288 if (!TLI->isOperationExpand(ISD, DstLT.second))
289 return SrcLT.first * 1;
290 }
291
292 // If we are converting vectors and the operation is illegal, or
293 // if the vectors are legalized to different types, estimate the
294 // scalarization costs.
295 unsigned Num = Dst->getVectorNumElements();
296 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
297 Src->getScalarType());
298
299 // Return the cost of multiple scalar invocation plus the cost of
300 // inserting and extracting the values.
301 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
302 }
303
304 // We already handled vector-to-vector and scalar-to-scalar conversions. This
305 // is where we handle bitcast between vectors and scalars. We need to assume
306 // that the conversion is scalarized in one way or another.
307 if (Opcode == Instruction::BitCast)
308 // Illegal bitcasts are done by storing and loading from a stack slot.
309 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
310 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
311
312 llvm_unreachable("Unhandled cast");
313 }
314
315unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
316 // Branches are assumed to be predicted.
317 return 0;
318}
319
320unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
321 Type *CondTy) const {
322 int ISD = TLI->InstructionOpcodeToISD(Opcode);
323 assert(ISD && "Invalid opcode");
324
325 // Selects on vectors are actually vector selects.
326 if (ISD == ISD::SELECT) {
327 assert(CondTy && "CondTy must exist");
328 if (CondTy->isVectorTy())
329 ISD = ISD::VSELECT;
330 }
331
332 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
333
334 if (!TLI->isOperationExpand(ISD, LT.second)) {
335 // The operation is legal. Assume it costs 1. Multiply
336 // by the type-legalization overhead.
337 return LT.first * 1;
338 }
339
340 // Otherwise, assume that the cast is scalarized.
341 if (ValTy->isVectorTy()) {
342 unsigned Num = ValTy->getVectorNumElements();
343 if (CondTy)
344 CondTy = CondTy->getScalarType();
345 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
346 CondTy);
347
348 // Return the cost of multiple scalar invocation plus the cost of inserting
349 // and extracting the values.
350 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
351 }
352
353 // Unknown scalar opcode.
354 return 1;
355}
356
357unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
358 unsigned Index) const {
359 return 1;
360}
361
362unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
363 unsigned Alignment,
364 unsigned AddressSpace) const {
365 assert(!Src->isVoidTy() && "Invalid type");
366 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
367
368 // Assume that all loads of legal types cost 1.
369 return LT.first;
370}
371
372unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
373 ArrayRef<Type *> Tys) const {
374 // assume that we need to scalarize this intrinsic.
375 unsigned ScalarizationCost = 0;
376 unsigned ScalarCalls = 1;
377 if (RetTy->isVectorTy()) {
378 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
379 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
380 }
381 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
382 if (Tys[i]->isVectorTy()) {
383 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
384 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
385 }
386 }
387 return ScalarCalls + ScalarizationCost;
388}
389
390unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
391 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
392 return LT.first;
393}