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 /// analyzeBasicBlock - Fill in the current structure with information gleaned 24 /// from the specified block. 25 void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB, 26 const TargetTransformInfo &TTI) { 27 ++NumBlocks; 28 unsigned NumInstsBeforeThisBB = NumInsts; 29 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); 30 II != E; ++II) { 31 // Special handling for calls. 32 if (isa<CallInst>(II) || isa<InvokeInst>(II)) { 33 ImmutableCallSite CS(cast<Instruction>(II)); 34 35 if (const Function *F = CS.getCalledFunction()) { 36 // If a function is both internal and has a single use, then it is 37 // extremely likely to get inlined in the future (it was probably 38 // exposed by an interleaved devirtualization pass). 39 if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse()) 40 ++NumInlineCandidates; 41 42 // If this call is to function itself, then the function is recursive. 43 // Inlining it into other functions is a bad idea, because this is 44 // basically just a form of loop peeling, and our metrics aren't useful 45 // for that case. 46 if (F == BB->getParent()) 47 isRecursive = true; 48 49 if (TTI.isLoweredToCall(F)) 50 ++NumCalls; 51 } else { 52 // We don't want inline asm to count as a call - that would prevent loop 53 // unrolling. The argument setup cost is still real, though. 54 if (!isa<InlineAsm>(CS.getCalledValue())) 55 ++NumCalls; 56 } 57 } 58 59 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 60 if (!AI->isStaticAlloca()) 61 this->usesDynamicAlloca = true; 62 } 63 64 if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy()) 65 ++NumVectorInsts; 66 67 if (const CallInst *CI = dyn_cast<CallInst>(II)) 68 if (CI->hasFnAttr(Attribute::NoDuplicate)) 69 notDuplicatable = true; 70 71 if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II)) 72 if (InvI->hasFnAttr(Attribute::NoDuplicate)) 73 notDuplicatable = true; 74 75 NumInsts += TTI.getUserCost(&*II); 76 } 77 78 if (isa<ReturnInst>(BB->getTerminator())) 79 ++NumRets; 80 81 // We never want to inline functions that contain an indirectbr. This is 82 // incorrect because all the blockaddress's (in static global initializers 83 // for example) would be referring to the original function, and this indirect 84 // jump would jump from the inlined copy of the function into the original 85 // function which is extremely undefined behavior. 86 // FIXME: This logic isn't really right; we can safely inline functions 87 // with indirectbr's as long as no other function or global references the 88 // blockaddress of a block within the current function. And as a QOI issue, 89 // if someone is using a blockaddress without an indirectbr, and that 90 // reference somehow ends up in another function or global, we probably 91 // don't want to inline this function. 92 notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator()); 93 94 // Remember NumInsts for this BB. 95 NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; 96 } 97