1 //===- CodeMetrics.cpp - Code cost measurements ---------------------------===// 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 // 10 // This file implements code cost measurement utilities. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/CodeMetrics.h" 15 #include "llvm/Analysis/TargetTransformInfo.h" 16 #include "llvm/IR/DataLayout.h" 17 #include "llvm/IR/Function.h" 18 #include "llvm/IR/IntrinsicInst.h" 19 #include "llvm/Support/CallSite.h" 20 21 using namespace llvm; 22 23 /// callIsSmall - If a call is likely to lower to a single target instruction, 24 /// or is otherwise deemed small return true. 25 /// TODO: Perhaps calls like memcpy, strcpy, etc? 26 bool llvm::callIsSmall(ImmutableCallSite CS) { 27 if (isa<IntrinsicInst>(CS.getInstruction())) 28 return true; 29 30 const Function *F = CS.getCalledFunction(); 31 if (!F) return false; 32 33 if (F->hasLocalLinkage()) return false; 34 35 if (!F->hasName()) return false; 36 37 StringRef Name = F->getName(); 38 39 // These will all likely lower to a single selection DAG node. 40 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" || 41 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || 42 Name == "sin" || Name == "sinf" || Name == "sinl" || 43 Name == "cos" || Name == "cosf" || Name == "cosl" || 44 Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl" ) 45 return true; 46 47 // These are all likely to be optimized into something smaller. 48 if (Name == "pow" || Name == "powf" || Name == "powl" || 49 Name == "exp2" || Name == "exp2l" || Name == "exp2f" || 50 Name == "floor" || Name == "floorf" || Name == "ceil" || 51 Name == "round" || Name == "ffs" || Name == "ffsl" || 52 Name == "abs" || Name == "labs" || Name == "llabs") 53 return true; 54 55 return false; 56 } 57 58 /// analyzeBasicBlock - Fill in the current structure with information gleaned 59 /// from the specified block. 60 void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB, 61 const TargetTransformInfo &TTI) { 62 ++NumBlocks; 63 unsigned NumInstsBeforeThisBB = NumInsts; 64 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); 65 II != E; ++II) { 66 if (TargetTransformInfo::TCC_Free == TTI.getUserCost(&*II)) 67 continue; 68 69 // Special handling for calls. 70 if (isa<CallInst>(II) || isa<InvokeInst>(II)) { 71 ImmutableCallSite CS(cast<Instruction>(II)); 72 73 if (const Function *F = CS.getCalledFunction()) { 74 // If a function is both internal and has a single use, then it is 75 // extremely likely to get inlined in the future (it was probably 76 // exposed by an interleaved devirtualization pass). 77 if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse()) 78 ++NumInlineCandidates; 79 80 // If this call is to function itself, then the function is recursive. 81 // Inlining it into other functions is a bad idea, because this is 82 // basically just a form of loop peeling, and our metrics aren't useful 83 // for that case. 84 if (F == BB->getParent()) 85 isRecursive = true; 86 } 87 88 if (!callIsSmall(CS)) { 89 // Each argument to a call takes on average one instruction to set up. 90 NumInsts += CS.arg_size(); 91 92 // We don't want inline asm to count as a call - that would prevent loop 93 // unrolling. The argument setup cost is still real, though. 94 if (!isa<InlineAsm>(CS.getCalledValue())) 95 ++NumCalls; 96 } 97 } 98 99 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 100 if (!AI->isStaticAlloca()) 101 this->usesDynamicAlloca = true; 102 } 103 104 if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy()) 105 ++NumVectorInsts; 106 107 if (const CallInst *CI = dyn_cast<CallInst>(II)) 108 if (CI->hasFnAttr(Attribute::NoDuplicate)) 109 notDuplicatable = true; 110 111 if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II)) 112 if (InvI->hasFnAttr(Attribute::NoDuplicate)) 113 notDuplicatable = true; 114 115 ++NumInsts; 116 } 117 118 if (isa<ReturnInst>(BB->getTerminator())) 119 ++NumRets; 120 121 // We never want to inline functions that contain an indirectbr. This is 122 // incorrect because all the blockaddress's (in static global initializers 123 // for example) would be referring to the original function, and this indirect 124 // jump would jump from the inlined copy of the function into the original 125 // function which is extremely undefined behavior. 126 // FIXME: This logic isn't really right; we can safely inline functions 127 // with indirectbr's as long as no other function or global references the 128 // blockaddress of a block within the current function. And as a QOI issue, 129 // if someone is using a blockaddress without an indirectbr, and that 130 // reference somehow ends up in another function or global, we probably 131 // don't want to inline this function. 132 notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator()); 133 134 // Remember NumInsts for this BB. 135 NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; 136 } 137