Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 1 | //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===// |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "tti" |
Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 12 | #include "llvm/IR/DataLayout.h" |
| 13 | #include "llvm/IR/Operator.h" |
| 14 | #include "llvm/IR/Instruction.h" |
| 15 | #include "llvm/IR/IntrinsicInst.h" |
| 16 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CallSite.h" |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 22 | // Setup the analysis group to manage the TargetTransformInfo passes. |
| 23 | INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI) |
Nadav Rotem | 5dc203e | 2012-10-18 23:22:48 +0000 | [diff] [blame] | 24 | char TargetTransformInfo::ID = 0; |
| 25 | |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 26 | TargetTransformInfo::~TargetTransformInfo() { |
| 27 | } |
| 28 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 29 | void TargetTransformInfo::pushTTIStack(Pass *P) { |
| 30 | TopTTI = this; |
| 31 | PrevTTI = &P->getAnalysis<TargetTransformInfo>(); |
| 32 | |
| 33 | // Walk up the chain and update the top TTI pointer. |
| 34 | for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI) |
| 35 | PTTI->TopTTI = this; |
| 36 | } |
| 37 | |
| 38 | void TargetTransformInfo::popTTIStack() { |
| 39 | TopTTI = 0; |
| 40 | |
| 41 | // Walk up the chain and update the top TTI pointer. |
| 42 | for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI) |
| 43 | PTTI->TopTTI = PrevTTI; |
| 44 | |
| 45 | PrevTTI = 0; |
| 46 | } |
| 47 | |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 48 | void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 49 | AU.addRequired<TargetTransformInfo>(); |
| 50 | } |
| 51 | |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 52 | unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty, |
| 53 | Type *OpTy) const { |
| 54 | return PrevTTI->getOperationCost(Opcode, Ty, OpTy); |
| 55 | } |
| 56 | |
| 57 | unsigned TargetTransformInfo::getGEPCost( |
| 58 | const Value *Ptr, ArrayRef<const Value *> Operands) const { |
| 59 | return PrevTTI->getGEPCost(Ptr, Operands); |
| 60 | } |
| 61 | |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 62 | unsigned TargetTransformInfo::getCallCost(FunctionType *FTy, |
| 63 | int NumArgs) const { |
| 64 | return PrevTTI->getCallCost(FTy, NumArgs); |
| 65 | } |
| 66 | |
| 67 | unsigned TargetTransformInfo::getCallCost(const Function *F, |
| 68 | int NumArgs) const { |
| 69 | return PrevTTI->getCallCost(F, NumArgs); |
| 70 | } |
| 71 | |
| 72 | unsigned TargetTransformInfo::getCallCost( |
| 73 | const Function *F, ArrayRef<const Value *> Arguments) const { |
| 74 | return PrevTTI->getCallCost(F, Arguments); |
| 75 | } |
| 76 | |
| 77 | unsigned TargetTransformInfo::getIntrinsicCost( |
| 78 | Intrinsic::ID IID, Type *RetTy, ArrayRef<Type *> ParamTys) const { |
| 79 | return PrevTTI->getIntrinsicCost(IID, RetTy, ParamTys); |
| 80 | } |
| 81 | |
| 82 | unsigned TargetTransformInfo::getIntrinsicCost( |
| 83 | Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const { |
| 84 | return PrevTTI->getIntrinsicCost(IID, RetTy, Arguments); |
| 85 | } |
| 86 | |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 87 | unsigned TargetTransformInfo::getUserCost(const User *U) const { |
| 88 | return PrevTTI->getUserCost(U); |
| 89 | } |
| 90 | |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 91 | bool TargetTransformInfo::isLoweredToCall(const Function *F) const { |
| 92 | return PrevTTI->isLoweredToCall(F); |
| 93 | } |
| 94 | |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 95 | bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const { |
| 96 | return PrevTTI->isLegalAddImmediate(Imm); |
| 97 | } |
| 98 | |
| 99 | bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const { |
| 100 | return PrevTTI->isLegalICmpImmediate(Imm); |
| 101 | } |
| 102 | |
| 103 | bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, |
| 104 | int64_t BaseOffset, |
| 105 | bool HasBaseReg, |
| 106 | int64_t Scale) const { |
| 107 | return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, |
| 108 | Scale); |
| 109 | } |
| 110 | |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 111 | int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, |
| 112 | int64_t BaseOffset, |
| 113 | bool HasBaseReg, |
| 114 | int64_t Scale) const { |
| 115 | return PrevTTI->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg, |
| 116 | Scale); |
| 117 | } |
| 118 | |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 119 | bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const { |
| 120 | return PrevTTI->isTruncateFree(Ty1, Ty2); |
| 121 | } |
| 122 | |
| 123 | bool TargetTransformInfo::isTypeLegal(Type *Ty) const { |
| 124 | return PrevTTI->isTypeLegal(Ty); |
| 125 | } |
| 126 | |
| 127 | unsigned TargetTransformInfo::getJumpBufAlignment() const { |
| 128 | return PrevTTI->getJumpBufAlignment(); |
| 129 | } |
| 130 | |
| 131 | unsigned TargetTransformInfo::getJumpBufSize() const { |
| 132 | return PrevTTI->getJumpBufSize(); |
| 133 | } |
| 134 | |
| 135 | bool TargetTransformInfo::shouldBuildLookupTables() const { |
| 136 | return PrevTTI->shouldBuildLookupTables(); |
| 137 | } |
| 138 | |
Chandler Carruth | 50a36cd | 2013-01-07 03:16:03 +0000 | [diff] [blame] | 139 | TargetTransformInfo::PopcntSupportKind |
| 140 | TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const { |
| 141 | return PrevTTI->getPopcntSupport(IntTyWidthInBit); |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const { |
| 145 | return PrevTTI->getIntImmCost(Imm, Ty); |
| 146 | } |
| 147 | |
| 148 | unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const { |
| 149 | return PrevTTI->getNumberOfRegisters(Vector); |
| 150 | } |
| 151 | |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 152 | unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const { |
| 153 | return PrevTTI->getRegisterBitWidth(Vector); |
| 154 | } |
| 155 | |
Nadav Rotem | b696c36 | 2013-01-09 01:15:42 +0000 | [diff] [blame] | 156 | unsigned TargetTransformInfo::getMaximumUnrollFactor() const { |
| 157 | return PrevTTI->getMaximumUnrollFactor(); |
| 158 | } |
| 159 | |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 160 | unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode, |
Arnold Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 161 | Type *Ty, |
| 162 | OperandValueKind Op1Info, |
| 163 | OperandValueKind Op2Info) const { |
| 164 | return PrevTTI->getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info); |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp, |
| 168 | int Index, Type *SubTp) const { |
| 169 | return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp); |
| 170 | } |
| 171 | |
| 172 | unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst, |
| 173 | Type *Src) const { |
| 174 | return PrevTTI->getCastInstrCost(Opcode, Dst, Src); |
| 175 | } |
| 176 | |
| 177 | unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const { |
| 178 | return PrevTTI->getCFInstrCost(Opcode); |
| 179 | } |
| 180 | |
| 181 | unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 182 | Type *CondTy) const { |
| 183 | return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy); |
| 184 | } |
| 185 | |
| 186 | unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val, |
| 187 | unsigned Index) const { |
| 188 | return PrevTTI->getVectorInstrCost(Opcode, Val, Index); |
| 189 | } |
| 190 | |
| 191 | unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src, |
| 192 | unsigned Alignment, |
| 193 | unsigned AddressSpace) const { |
| 194 | return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace); |
| 195 | ; |
| 196 | } |
| 197 | |
| 198 | unsigned |
| 199 | TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, |
| 200 | Type *RetTy, |
| 201 | ArrayRef<Type *> Tys) const { |
| 202 | return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys); |
| 203 | } |
| 204 | |
| 205 | unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const { |
| 206 | return PrevTTI->getNumberOfParts(Tp); |
| 207 | } |
| 208 | |
Arnold Schwaighofer | 9da9a43 | 2013-07-12 19:16:02 +0000 | [diff] [blame] | 209 | unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp, |
| 210 | bool IsComplex) const { |
| 211 | return PrevTTI->getAddressComputationCost(Tp, IsComplex); |
Arnold Schwaighofer | 594fa2d | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 212 | } |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 213 | |
| 214 | namespace { |
| 215 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 216 | struct NoTTI : ImmutablePass, TargetTransformInfo { |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 217 | const DataLayout *DL; |
| 218 | |
| 219 | NoTTI() : ImmutablePass(ID), DL(0) { |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 220 | initializeNoTTIPass(*PassRegistry::getPassRegistry()); |
| 221 | } |
| 222 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 223 | virtual void initializePass() { |
| 224 | // Note that this subclass is special, and must *not* call initializeTTI as |
| 225 | // it does not chain. |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 226 | TopTTI = this; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 227 | PrevTTI = 0; |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 228 | DL = getAnalysisIfAvailable<DataLayout>(); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 232 | // Note that this subclass is special, and must *not* call |
| 233 | // TTI::getAnalysisUsage as it breaks the recursion. |
| 234 | } |
| 235 | |
| 236 | /// Pass identification. |
| 237 | static char ID; |
| 238 | |
| 239 | /// Provide necessary pointer adjustments for the two base classes. |
| 240 | virtual void *getAdjustedAnalysisPointer(const void *ID) { |
| 241 | if (ID == &TargetTransformInfo::ID) |
| 242 | return (TargetTransformInfo*)this; |
| 243 | return this; |
| 244 | } |
| 245 | |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 246 | unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) const { |
| 247 | switch (Opcode) { |
| 248 | default: |
| 249 | // By default, just classify everything as 'basic'. |
| 250 | return TCC_Basic; |
| 251 | |
| 252 | case Instruction::GetElementPtr: |
| 253 | llvm_unreachable("Use getGEPCost for GEP operations!"); |
| 254 | |
| 255 | case Instruction::BitCast: |
| 256 | assert(OpTy && "Cast instructions must provide the operand type"); |
| 257 | if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy())) |
| 258 | // Identity and pointer-to-pointer casts are free. |
| 259 | return TCC_Free; |
| 260 | |
| 261 | // Otherwise, the default basic cost is used. |
| 262 | return TCC_Basic; |
| 263 | |
| 264 | case Instruction::IntToPtr: |
| 265 | // An inttoptr cast is free so long as the input is a legal integer type |
| 266 | // which doesn't contain values outside the range of a pointer. |
| 267 | if (DL && DL->isLegalInteger(OpTy->getScalarSizeInBits()) && |
| 268 | OpTy->getScalarSizeInBits() <= DL->getPointerSizeInBits()) |
| 269 | return TCC_Free; |
| 270 | |
| 271 | // Otherwise it's not a no-op. |
| 272 | return TCC_Basic; |
| 273 | |
| 274 | case Instruction::PtrToInt: |
| 275 | // A ptrtoint cast is free so long as the result is large enough to store |
| 276 | // the pointer, and a legal integer type. |
Patrik Hagglund | 3eaa4b9 | 2013-03-12 13:18:30 +0000 | [diff] [blame] | 277 | if (DL && DL->isLegalInteger(Ty->getScalarSizeInBits()) && |
| 278 | Ty->getScalarSizeInBits() >= DL->getPointerSizeInBits()) |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 279 | return TCC_Free; |
| 280 | |
| 281 | // Otherwise it's not a no-op. |
| 282 | return TCC_Basic; |
| 283 | |
| 284 | case Instruction::Trunc: |
| 285 | // trunc to a native type is free (assuming the target has compare and |
| 286 | // shift-right of the same width). |
| 287 | if (DL && DL->isLegalInteger(DL->getTypeSizeInBits(Ty))) |
| 288 | return TCC_Free; |
| 289 | |
| 290 | return TCC_Basic; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | unsigned getGEPCost(const Value *Ptr, |
| 295 | ArrayRef<const Value *> Operands) const { |
| 296 | // In the basic model, we just assume that all-constant GEPs will be folded |
| 297 | // into their uses via addressing modes. |
| 298 | for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx) |
| 299 | if (!isa<Constant>(Operands[Idx])) |
| 300 | return TCC_Basic; |
| 301 | |
| 302 | return TCC_Free; |
| 303 | } |
| 304 | |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 305 | unsigned getCallCost(FunctionType *FTy, int NumArgs = -1) const { |
| 306 | assert(FTy && "FunctionType must be provided to this routine."); |
| 307 | |
| 308 | // The target-independent implementation just measures the size of the |
| 309 | // function by approximating that each argument will take on average one |
| 310 | // instruction to prepare. |
| 311 | |
| 312 | if (NumArgs < 0) |
| 313 | // Set the argument number to the number of explicit arguments in the |
| 314 | // function. |
| 315 | NumArgs = FTy->getNumParams(); |
| 316 | |
| 317 | return TCC_Basic * (NumArgs + 1); |
| 318 | } |
| 319 | |
| 320 | unsigned getCallCost(const Function *F, int NumArgs = -1) const { |
| 321 | assert(F && "A concrete function must be provided to this routine."); |
| 322 | |
| 323 | if (NumArgs < 0) |
| 324 | // Set the argument number to the number of explicit arguments in the |
| 325 | // function. |
| 326 | NumArgs = F->arg_size(); |
| 327 | |
| 328 | if (Intrinsic::ID IID = (Intrinsic::ID)F->getIntrinsicID()) { |
| 329 | FunctionType *FTy = F->getFunctionType(); |
| 330 | SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end()); |
| 331 | return TopTTI->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys); |
| 332 | } |
| 333 | |
| 334 | if (!TopTTI->isLoweredToCall(F)) |
| 335 | return TCC_Basic; // Give a basic cost if it will be lowered directly. |
| 336 | |
| 337 | return TopTTI->getCallCost(F->getFunctionType(), NumArgs); |
| 338 | } |
| 339 | |
| 340 | unsigned getCallCost(const Function *F, |
| 341 | ArrayRef<const Value *> Arguments) const { |
| 342 | // Simply delegate to generic handling of the call. |
| 343 | // FIXME: We should use instsimplify or something else to catch calls which |
| 344 | // will constant fold with these arguments. |
| 345 | return TopTTI->getCallCost(F, Arguments.size()); |
| 346 | } |
| 347 | |
| 348 | unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, |
| 349 | ArrayRef<Type *> ParamTys) const { |
| 350 | switch (IID) { |
| 351 | default: |
| 352 | // Intrinsics rarely (if ever) have normal argument setup constraints. |
| 353 | // Model them as having a basic instruction cost. |
| 354 | // FIXME: This is wrong for libc intrinsics. |
| 355 | return TCC_Basic; |
| 356 | |
| 357 | case Intrinsic::dbg_declare: |
| 358 | case Intrinsic::dbg_value: |
| 359 | case Intrinsic::invariant_start: |
| 360 | case Intrinsic::invariant_end: |
| 361 | case Intrinsic::lifetime_start: |
| 362 | case Intrinsic::lifetime_end: |
| 363 | case Intrinsic::objectsize: |
| 364 | case Intrinsic::ptr_annotation: |
| 365 | case Intrinsic::var_annotation: |
| 366 | // These intrinsics don't actually represent code after lowering. |
| 367 | return TCC_Free; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, |
| 372 | ArrayRef<const Value *> Arguments) const { |
| 373 | // Delegate to the generic intrinsic handling code. This mostly provides an |
| 374 | // opportunity for targets to (for example) special case the cost of |
| 375 | // certain intrinsics based on constants used as arguments. |
| 376 | SmallVector<Type *, 8> ParamTys; |
| 377 | ParamTys.reserve(Arguments.size()); |
| 378 | for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx) |
| 379 | ParamTys.push_back(Arguments[Idx]->getType()); |
| 380 | return TopTTI->getIntrinsicCost(IID, RetTy, ParamTys); |
| 381 | } |
| 382 | |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 383 | unsigned getUserCost(const User *U) const { |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 384 | if (isa<PHINode>(U)) |
| 385 | return TCC_Free; // Model all PHI nodes as free. |
| 386 | |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 387 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) |
| 388 | // In the basic model we just assume that all-constant GEPs will be |
| 389 | // folded into their uses via addressing modes. |
| 390 | return GEP->hasAllConstantIndices() ? TCC_Free : TCC_Basic; |
| 391 | |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 392 | if (ImmutableCallSite CS = U) { |
| 393 | const Function *F = CS.getCalledFunction(); |
| 394 | if (!F) { |
| 395 | // Just use the called value type. |
| 396 | Type *FTy = CS.getCalledValue()->getType()->getPointerElementType(); |
| 397 | return TopTTI->getCallCost(cast<FunctionType>(FTy), CS.arg_size()); |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 398 | } |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 399 | |
| 400 | SmallVector<const Value *, 8> Arguments; |
| 401 | for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), |
| 402 | AE = CS.arg_end(); |
| 403 | AI != AE; ++AI) |
| 404 | Arguments.push_back(*AI); |
| 405 | |
| 406 | return TopTTI->getCallCost(F, Arguments); |
Chandler Carruth | 511aa76 | 2013-01-21 01:27:39 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (const CastInst *CI = dyn_cast<CastInst>(U)) { |
| 410 | // Result of a cmp instruction is often extended (to be used by other |
| 411 | // cmp instructions, logical or return instructions). These are usually |
| 412 | // nop on most sane targets. |
| 413 | if (isa<CmpInst>(CI->getOperand(0))) |
| 414 | return TCC_Free; |
| 415 | } |
| 416 | |
| 417 | // Otherwise delegate to the fully generic implementations. |
| 418 | return getOperationCost(Operator::getOpcode(U), U->getType(), |
| 419 | U->getNumOperands() == 1 ? |
| 420 | U->getOperand(0)->getType() : 0); |
| 421 | } |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 422 | |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 423 | bool isLoweredToCall(const Function *F) const { |
| 424 | // FIXME: These should almost certainly not be handled here, and instead |
| 425 | // handled with the help of TLI or the target itself. This was largely |
| 426 | // ported from existing analysis heuristics here so that such refactorings |
| 427 | // can take place in the future. |
| 428 | |
| 429 | if (F->isIntrinsic()) |
| 430 | return false; |
| 431 | |
| 432 | if (F->hasLocalLinkage() || !F->hasName()) |
| 433 | return true; |
| 434 | |
| 435 | StringRef Name = F->getName(); |
| 436 | |
| 437 | // These will all likely lower to a single selection DAG node. |
| 438 | if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" || |
| 439 | Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" || |
| 440 | Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" || |
| 441 | Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") |
| 442 | return false; |
| 443 | |
| 444 | // These are all likely to be optimized into something smaller. |
| 445 | if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" || |
| 446 | Name == "exp2l" || Name == "exp2f" || Name == "floor" || Name == |
| 447 | "floorf" || Name == "ceil" || Name == "round" || Name == "ffs" || |
| 448 | Name == "ffsl" || Name == "abs" || Name == "labs" || Name == "llabs") |
| 449 | return false; |
| 450 | |
| 451 | return true; |
| 452 | } |
| 453 | |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 454 | bool isLegalAddImmediate(int64_t Imm) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 455 | return false; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | bool isLegalICmpImmediate(int64_t Imm) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 459 | return false; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, |
| 463 | bool HasBaseReg, int64_t Scale) const { |
Chandler Carruth | 26c59fa | 2013-01-07 14:41:08 +0000 | [diff] [blame] | 464 | // Guess that reg+reg addressing is allowed. This heuristic is taken from |
| 465 | // the implementation of LSR. |
| 466 | return !BaseGV && BaseOffset == 0 && Scale <= 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Quentin Colombet | bf490d4 | 2013-05-31 21:29:03 +0000 | [diff] [blame] | 469 | int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, |
| 470 | bool HasBaseReg, int64_t Scale) const { |
| 471 | // Guess that all legal addressing mode are free. |
| 472 | if(isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, Scale)) |
| 473 | return 0; |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 478 | bool isTruncateFree(Type *Ty1, Type *Ty2) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 479 | return false; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | bool isTypeLegal(Type *Ty) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 483 | return false; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | unsigned getJumpBufAlignment() const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 487 | return 0; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | unsigned getJumpBufSize() const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 491 | return 0; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | bool shouldBuildLookupTables() const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 495 | return true; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Chandler Carruth | 50a36cd | 2013-01-07 03:16:03 +0000 | [diff] [blame] | 498 | PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const { |
| 499 | return PSK_Software; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | unsigned getIntImmCost(const APInt &Imm, Type *Ty) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 503 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | unsigned getNumberOfRegisters(bool Vector) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 507 | return 8; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 510 | unsigned getRegisterBitWidth(bool Vector) const { |
| 511 | return 32; |
| 512 | } |
| 513 | |
Nadav Rotem | b696c36 | 2013-01-09 01:15:42 +0000 | [diff] [blame] | 514 | unsigned getMaximumUnrollFactor() const { |
| 515 | return 1; |
| 516 | } |
| 517 | |
Arnold Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 518 | unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind, |
| 519 | OperandValueKind) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 520 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, |
| 524 | int Index = 0, Type *SubTp = 0) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 525 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | unsigned getCastInstrCost(unsigned Opcode, Type *Dst, |
| 529 | Type *Src) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 530 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | unsigned getCFInstrCost(unsigned Opcode) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 534 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 538 | Type *CondTy = 0) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 539 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | unsigned getVectorInstrCost(unsigned Opcode, Type *Val, |
| 543 | unsigned Index = -1) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 544 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | unsigned getMemoryOpCost(unsigned Opcode, Type *Src, |
| 548 | unsigned Alignment, |
| 549 | unsigned AddressSpace) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 550 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | unsigned getIntrinsicInstrCost(Intrinsic::ID ID, |
| 554 | Type *RetTy, |
| 555 | ArrayRef<Type*> Tys) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 556 | return 1; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | unsigned getNumberOfParts(Type *Tp) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 560 | return 0; |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 561 | } |
Arnold Schwaighofer | 594fa2d | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 562 | |
Arnold Schwaighofer | 9da9a43 | 2013-07-12 19:16:02 +0000 | [diff] [blame] | 563 | unsigned getAddressComputationCost(Type *Tp, bool) const { |
Arnold Schwaighofer | 594fa2d | 2013-02-08 14:50:48 +0000 | [diff] [blame] | 564 | return 0; |
| 565 | } |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | } // end anonymous namespace |
| 569 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 570 | INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti", |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 571 | "No target information", true, true, true) |
| 572 | char NoTTI::ID = 0; |
| 573 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 574 | ImmutablePass *llvm::createNoTargetTransformInfoPass() { |
| 575 | return new NoTTI(); |
Chandler Carruth | 539edf4 | 2013-01-05 11:43:11 +0000 | [diff] [blame] | 576 | } |