13c256fbfSChandler Carruth //===- CodeMetrics.cpp - Code cost measurements ---------------------------===// 23c256fbfSChandler Carruth // 33c256fbfSChandler Carruth // The LLVM Compiler Infrastructure 43c256fbfSChandler Carruth // 53c256fbfSChandler Carruth // This file is distributed under the University of Illinois Open Source 63c256fbfSChandler Carruth // License. See LICENSE.TXT for details. 73c256fbfSChandler Carruth // 83c256fbfSChandler Carruth //===----------------------------------------------------------------------===// 93c256fbfSChandler Carruth // 103c256fbfSChandler Carruth // This file implements code cost measurement utilities. 113c256fbfSChandler Carruth // 123c256fbfSChandler Carruth //===----------------------------------------------------------------------===// 133c256fbfSChandler Carruth 143c256fbfSChandler Carruth #include "llvm/Analysis/CodeMetrics.h" 15bb9caa92SChandler Carruth #include "llvm/Analysis/TargetTransformInfo.h" 16*219b89b9SChandler Carruth #include "llvm/IR/CallSite.h" 179fb823bbSChandler Carruth #include "llvm/IR/DataLayout.h" 189fb823bbSChandler Carruth #include "llvm/IR/Function.h" 199fb823bbSChandler Carruth #include "llvm/IR/IntrinsicInst.h" 203c256fbfSChandler Carruth 213c256fbfSChandler Carruth using namespace llvm; 223c256fbfSChandler Carruth 230539c071SChandler Carruth /// analyzeBasicBlock - Fill in the current structure with information gleaned 240539c071SChandler Carruth /// from the specified block. 250539c071SChandler Carruth void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB, 26bb9caa92SChandler Carruth const TargetTransformInfo &TTI) { 270539c071SChandler Carruth ++NumBlocks; 280539c071SChandler Carruth unsigned NumInstsBeforeThisBB = NumInsts; 290539c071SChandler Carruth for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); 300539c071SChandler Carruth II != E; ++II) { 310539c071SChandler Carruth // Special handling for calls. 320539c071SChandler Carruth if (isa<CallInst>(II) || isa<InvokeInst>(II)) { 333c256fbfSChandler Carruth ImmutableCallSite CS(cast<Instruction>(II)); 343c256fbfSChandler Carruth 353c256fbfSChandler Carruth if (const Function *F = CS.getCalledFunction()) { 363c256fbfSChandler Carruth // If a function is both internal and has a single use, then it is 373c256fbfSChandler Carruth // extremely likely to get inlined in the future (it was probably 383c256fbfSChandler Carruth // exposed by an interleaved devirtualization pass). 393c256fbfSChandler Carruth if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse()) 403c256fbfSChandler Carruth ++NumInlineCandidates; 413c256fbfSChandler Carruth 423c256fbfSChandler Carruth // If this call is to function itself, then the function is recursive. 433c256fbfSChandler Carruth // Inlining it into other functions is a bad idea, because this is 443c256fbfSChandler Carruth // basically just a form of loop peeling, and our metrics aren't useful 453c256fbfSChandler Carruth // for that case. 463c256fbfSChandler Carruth if (F == BB->getParent()) 473c256fbfSChandler Carruth isRecursive = true; 483c256fbfSChandler Carruth 490ba8db45SChandler Carruth if (TTI.isLoweredToCall(F)) 500ba8db45SChandler Carruth ++NumCalls; 510ba8db45SChandler Carruth } else { 523c256fbfSChandler Carruth // We don't want inline asm to count as a call - that would prevent loop 533c256fbfSChandler Carruth // unrolling. The argument setup cost is still real, though. 543c256fbfSChandler Carruth if (!isa<InlineAsm>(CS.getCalledValue())) 553c256fbfSChandler Carruth ++NumCalls; 563c256fbfSChandler Carruth } 573c256fbfSChandler Carruth } 583c256fbfSChandler Carruth 593c256fbfSChandler Carruth if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 603c256fbfSChandler Carruth if (!AI->isStaticAlloca()) 613c256fbfSChandler Carruth this->usesDynamicAlloca = true; 623c256fbfSChandler Carruth } 633c256fbfSChandler Carruth 643c256fbfSChandler Carruth if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy()) 653c256fbfSChandler Carruth ++NumVectorInsts; 663c256fbfSChandler Carruth 674f6fb953SJames Molloy if (const CallInst *CI = dyn_cast<CallInst>(II)) 684f6fb953SJames Molloy if (CI->hasFnAttr(Attribute::NoDuplicate)) 694f6fb953SJames Molloy notDuplicatable = true; 704f6fb953SJames Molloy 714f6fb953SJames Molloy if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II)) 724f6fb953SJames Molloy if (InvI->hasFnAttr(Attribute::NoDuplicate)) 734f6fb953SJames Molloy notDuplicatable = true; 744f6fb953SJames Molloy 750ba8db45SChandler Carruth NumInsts += TTI.getUserCost(&*II); 763c256fbfSChandler Carruth } 773c256fbfSChandler Carruth 783c256fbfSChandler Carruth if (isa<ReturnInst>(BB->getTerminator())) 793c256fbfSChandler Carruth ++NumRets; 803c256fbfSChandler Carruth 813c256fbfSChandler Carruth // We never want to inline functions that contain an indirectbr. This is 823c256fbfSChandler Carruth // incorrect because all the blockaddress's (in static global initializers 833c256fbfSChandler Carruth // for example) would be referring to the original function, and this indirect 843c256fbfSChandler Carruth // jump would jump from the inlined copy of the function into the original 853c256fbfSChandler Carruth // function which is extremely undefined behavior. 863c256fbfSChandler Carruth // FIXME: This logic isn't really right; we can safely inline functions 873c256fbfSChandler Carruth // with indirectbr's as long as no other function or global references the 883c256fbfSChandler Carruth // blockaddress of a block within the current function. And as a QOI issue, 893c256fbfSChandler Carruth // if someone is using a blockaddress without an indirectbr, and that 903c256fbfSChandler Carruth // reference somehow ends up in another function or global, we probably 913c256fbfSChandler Carruth // don't want to inline this function. 924f6fb953SJames Molloy notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator()); 933c256fbfSChandler Carruth 943c256fbfSChandler Carruth // Remember NumInsts for this BB. 953c256fbfSChandler Carruth NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; 963c256fbfSChandler Carruth } 97