1 //===-- ARMTargetTransformInfo.cpp - ARM specific TTI pass ----------------===// 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 implements a TargetTransformInfo analysis pass specific to the 11 /// ARM target machine. It uses the target's detailed information to provide 12 /// more precise answers to certain TTI queries, while letting the target 13 /// independent and default TTI implementations handle the rest. 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #define DEBUG_TYPE "armtti" 18 #include "ARM.h" 19 #include "ARMTargetMachine.h" 20 #include "llvm/Analysis/TargetTransformInfo.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Target/TargetLowering.h" 23 using namespace llvm; 24 25 // Declare the pass initialization routine locally as target-specific passes 26 // don't havve a target-wide initialization entry point, and so we rely on the 27 // pass constructor initialization. 28 namespace llvm { 29 void initializeARMTTIPass(PassRegistry &); 30 } 31 32 namespace { 33 34 class ARMTTI : public ImmutablePass, public TargetTransformInfo { 35 const ARMBaseTargetMachine *TM; 36 const ARMSubtarget *ST; 37 38 /// Estimate the overhead of scalarizing an instruction. Insert and Extract 39 /// are set if the result needs to be inserted and/or extracted from vectors. 40 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const; 41 42 public: 43 ARMTTI() : ImmutablePass(ID), TM(0), ST(0) { 44 llvm_unreachable("This pass cannot be directly constructed"); 45 } 46 47 ARMTTI(const ARMBaseTargetMachine *TM) 48 : ImmutablePass(ID), TM(TM), ST(TM->getSubtargetImpl()) { 49 initializeARMTTIPass(*PassRegistry::getPassRegistry()); 50 } 51 52 virtual void initializePass() { 53 pushTTIStack(this); 54 } 55 56 virtual void finalizePass() { 57 popTTIStack(); 58 } 59 60 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 61 TargetTransformInfo::getAnalysisUsage(AU); 62 } 63 64 /// Pass identification. 65 static char ID; 66 67 /// Provide necessary pointer adjustments for the two base classes. 68 virtual void *getAdjustedAnalysisPointer(const void *ID) { 69 if (ID == &TargetTransformInfo::ID) 70 return (TargetTransformInfo*)this; 71 return this; 72 } 73 74 /// \name Scalar TTI Implementations 75 /// @{ 76 77 virtual unsigned getIntImmCost(const APInt &Imm, Type *Ty) const; 78 79 /// @} 80 }; 81 82 } // end anonymous namespace 83 84 INITIALIZE_AG_PASS(ARMTTI, TargetTransformInfo, "armtti", 85 "ARM Target Transform Info", true, true, false) 86 char ARMTTI::ID = 0; 87 88 ImmutablePass * 89 llvm::createARMTargetTransformInfoPass(const ARMBaseTargetMachine *TM) { 90 return new ARMTTI(TM); 91 } 92 93 94 unsigned ARMTTI::getIntImmCost(const APInt &Imm, Type *Ty) const { 95 assert(Ty->isIntegerTy()); 96 97 unsigned Bits = Ty->getPrimitiveSizeInBits(); 98 if (Bits == 0 || Bits > 32) 99 return 4; 100 101 int32_t SImmVal = Imm.getSExtValue(); 102 uint32_t ZImmVal = Imm.getZExtValue(); 103 if (!ST->isThumb()) { 104 if ((SImmVal >= 0 && SImmVal < 65536) || 105 (ARM_AM::getSOImmVal(ZImmVal) != -1) || 106 (ARM_AM::getSOImmVal(~ZImmVal) != -1)) 107 return 1; 108 return ST->hasV6T2Ops() ? 2 : 3; 109 } else if (ST->isThumb2()) { 110 if ((SImmVal >= 0 && SImmVal < 65536) || 111 (ARM_AM::getT2SOImmVal(ZImmVal) != -1) || 112 (ARM_AM::getT2SOImmVal(~ZImmVal) != -1)) 113 return 1; 114 return ST->hasV6T2Ops() ? 2 : 3; 115 } else /*Thumb1*/ { 116 if (SImmVal >= 0 && SImmVal < 256) 117 return 1; 118 if ((~ZImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal)) 119 return 2; 120 // Load from constantpool. 121 return 3; 122 } 123 return 2; 124 } 125