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" 15*bb9caa92SChandler Carruth #include "llvm/Analysis/TargetTransformInfo.h" 169fb823bbSChandler Carruth #include "llvm/IR/DataLayout.h" 179fb823bbSChandler Carruth #include "llvm/IR/Function.h" 189fb823bbSChandler Carruth #include "llvm/IR/IntrinsicInst.h" 19ed0881b2SChandler Carruth #include "llvm/Support/CallSite.h" 203c256fbfSChandler Carruth 213c256fbfSChandler Carruth using namespace llvm; 223c256fbfSChandler Carruth 233c256fbfSChandler Carruth /// callIsSmall - If a call is likely to lower to a single target instruction, 243c256fbfSChandler Carruth /// or is otherwise deemed small return true. 253c256fbfSChandler Carruth /// TODO: Perhaps calls like memcpy, strcpy, etc? 26da7513a8SChandler Carruth bool llvm::callIsSmall(ImmutableCallSite CS) { 27da7513a8SChandler Carruth if (isa<IntrinsicInst>(CS.getInstruction())) 28da7513a8SChandler Carruth return true; 29da7513a8SChandler Carruth 30da7513a8SChandler Carruth const Function *F = CS.getCalledFunction(); 313c256fbfSChandler Carruth if (!F) return false; 323c256fbfSChandler Carruth 333c256fbfSChandler Carruth if (F->hasLocalLinkage()) return false; 343c256fbfSChandler Carruth 353c256fbfSChandler Carruth if (!F->hasName()) return false; 363c256fbfSChandler Carruth 373c256fbfSChandler Carruth StringRef Name = F->getName(); 383c256fbfSChandler Carruth 393c256fbfSChandler Carruth // These will all likely lower to a single selection DAG node. 403c256fbfSChandler Carruth if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" || 413c256fbfSChandler Carruth Name == "fabs" || Name == "fabsf" || Name == "fabsl" || 423c256fbfSChandler Carruth Name == "sin" || Name == "sinf" || Name == "sinl" || 433c256fbfSChandler Carruth Name == "cos" || Name == "cosf" || Name == "cosl" || 443c256fbfSChandler Carruth Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl" ) 453c256fbfSChandler Carruth return true; 463c256fbfSChandler Carruth 473c256fbfSChandler Carruth // These are all likely to be optimized into something smaller. 483c256fbfSChandler Carruth if (Name == "pow" || Name == "powf" || Name == "powl" || 493c256fbfSChandler Carruth Name == "exp2" || Name == "exp2l" || Name == "exp2f" || 503c256fbfSChandler Carruth Name == "floor" || Name == "floorf" || Name == "ceil" || 513c256fbfSChandler Carruth Name == "round" || Name == "ffs" || Name == "ffsl" || 523c256fbfSChandler Carruth Name == "abs" || Name == "labs" || Name == "llabs") 533c256fbfSChandler Carruth return true; 543c256fbfSChandler Carruth 553c256fbfSChandler Carruth return false; 563c256fbfSChandler Carruth } 573c256fbfSChandler Carruth 580539c071SChandler Carruth /// analyzeBasicBlock - Fill in the current structure with information gleaned 590539c071SChandler Carruth /// from the specified block. 600539c071SChandler Carruth void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB, 61*bb9caa92SChandler Carruth const TargetTransformInfo &TTI) { 620539c071SChandler Carruth ++NumBlocks; 630539c071SChandler Carruth unsigned NumInstsBeforeThisBB = NumInsts; 640539c071SChandler Carruth for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); 650539c071SChandler Carruth II != E; ++II) { 66*bb9caa92SChandler Carruth if (TargetTransformInfo::TCC_Free == TTI.getUserCost(&*II)) 670539c071SChandler Carruth continue; 680539c071SChandler Carruth 690539c071SChandler Carruth // Special handling for calls. 700539c071SChandler Carruth if (isa<CallInst>(II) || isa<InvokeInst>(II)) { 713c256fbfSChandler Carruth ImmutableCallSite CS(cast<Instruction>(II)); 723c256fbfSChandler Carruth 733c256fbfSChandler Carruth if (const Function *F = CS.getCalledFunction()) { 743c256fbfSChandler Carruth // If a function is both internal and has a single use, then it is 753c256fbfSChandler Carruth // extremely likely to get inlined in the future (it was probably 763c256fbfSChandler Carruth // exposed by an interleaved devirtualization pass). 773c256fbfSChandler Carruth if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse()) 783c256fbfSChandler Carruth ++NumInlineCandidates; 793c256fbfSChandler Carruth 803c256fbfSChandler Carruth // If this call is to function itself, then the function is recursive. 813c256fbfSChandler Carruth // Inlining it into other functions is a bad idea, because this is 823c256fbfSChandler Carruth // basically just a form of loop peeling, and our metrics aren't useful 833c256fbfSChandler Carruth // for that case. 843c256fbfSChandler Carruth if (F == BB->getParent()) 853c256fbfSChandler Carruth isRecursive = true; 863c256fbfSChandler Carruth } 873c256fbfSChandler Carruth 88da7513a8SChandler Carruth if (!callIsSmall(CS)) { 893c256fbfSChandler Carruth // Each argument to a call takes on average one instruction to set up. 903c256fbfSChandler Carruth NumInsts += CS.arg_size(); 913c256fbfSChandler Carruth 923c256fbfSChandler Carruth // We don't want inline asm to count as a call - that would prevent loop 933c256fbfSChandler Carruth // unrolling. The argument setup cost is still real, though. 943c256fbfSChandler Carruth if (!isa<InlineAsm>(CS.getCalledValue())) 953c256fbfSChandler Carruth ++NumCalls; 963c256fbfSChandler Carruth } 973c256fbfSChandler Carruth } 983c256fbfSChandler Carruth 993c256fbfSChandler Carruth if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 1003c256fbfSChandler Carruth if (!AI->isStaticAlloca()) 1013c256fbfSChandler Carruth this->usesDynamicAlloca = true; 1023c256fbfSChandler Carruth } 1033c256fbfSChandler Carruth 1043c256fbfSChandler Carruth if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy()) 1053c256fbfSChandler Carruth ++NumVectorInsts; 1063c256fbfSChandler Carruth 1074f6fb953SJames Molloy if (const CallInst *CI = dyn_cast<CallInst>(II)) 1084f6fb953SJames Molloy if (CI->hasFnAttr(Attribute::NoDuplicate)) 1094f6fb953SJames Molloy notDuplicatable = true; 1104f6fb953SJames Molloy 1114f6fb953SJames Molloy if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II)) 1124f6fb953SJames Molloy if (InvI->hasFnAttr(Attribute::NoDuplicate)) 1134f6fb953SJames Molloy notDuplicatable = true; 1144f6fb953SJames Molloy 1153c256fbfSChandler Carruth ++NumInsts; 1163c256fbfSChandler Carruth } 1173c256fbfSChandler Carruth 1183c256fbfSChandler Carruth if (isa<ReturnInst>(BB->getTerminator())) 1193c256fbfSChandler Carruth ++NumRets; 1203c256fbfSChandler Carruth 1213c256fbfSChandler Carruth // We never want to inline functions that contain an indirectbr. This is 1223c256fbfSChandler Carruth // incorrect because all the blockaddress's (in static global initializers 1233c256fbfSChandler Carruth // for example) would be referring to the original function, and this indirect 1243c256fbfSChandler Carruth // jump would jump from the inlined copy of the function into the original 1253c256fbfSChandler Carruth // function which is extremely undefined behavior. 1263c256fbfSChandler Carruth // FIXME: This logic isn't really right; we can safely inline functions 1273c256fbfSChandler Carruth // with indirectbr's as long as no other function or global references the 1283c256fbfSChandler Carruth // blockaddress of a block within the current function. And as a QOI issue, 1293c256fbfSChandler Carruth // if someone is using a blockaddress without an indirectbr, and that 1303c256fbfSChandler Carruth // reference somehow ends up in another function or global, we probably 1313c256fbfSChandler Carruth // don't want to inline this function. 1324f6fb953SJames Molloy notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator()); 1333c256fbfSChandler Carruth 1343c256fbfSChandler Carruth // Remember NumInsts for this BB. 1353c256fbfSChandler Carruth NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; 1363c256fbfSChandler Carruth } 137