19b081d9691f4509af58919db5760235ca319c9daChandler Carruth//===- CodeMetrics.cpp - Code cost measurements ---------------------------===//
29b081d9691f4509af58919db5760235ca319c9daChandler Carruth//
39b081d9691f4509af58919db5760235ca319c9daChandler Carruth//                     The LLVM Compiler Infrastructure
49b081d9691f4509af58919db5760235ca319c9daChandler Carruth//
59b081d9691f4509af58919db5760235ca319c9daChandler Carruth// This file is distributed under the University of Illinois Open Source
69b081d9691f4509af58919db5760235ca319c9daChandler Carruth// License. See LICENSE.TXT for details.
79b081d9691f4509af58919db5760235ca319c9daChandler Carruth//
89b081d9691f4509af58919db5760235ca319c9daChandler Carruth//===----------------------------------------------------------------------===//
99b081d9691f4509af58919db5760235ca319c9daChandler Carruth//
109b081d9691f4509af58919db5760235ca319c9daChandler Carruth// This file implements code cost measurement utilities.
119b081d9691f4509af58919db5760235ca319c9daChandler Carruth//
129b081d9691f4509af58919db5760235ca319c9daChandler Carruth//===----------------------------------------------------------------------===//
139b081d9691f4509af58919db5760235ca319c9daChandler Carruth
149b081d9691f4509af58919db5760235ca319c9daChandler Carruth#include "llvm/Analysis/CodeMetrics.h"
15a5157e68d183e1bdf010e94a15dc0c44b65f889bChandler Carruth#include "llvm/Analysis/TargetTransformInfo.h"
160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h"
170b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h"
180b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h"
19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/CallSite.h"
209b081d9691f4509af58919db5760235ca319c9daChandler Carruth
219b081d9691f4509af58919db5760235ca319c9daChandler Carruthusing namespace llvm;
229b081d9691f4509af58919db5760235ca319c9daChandler Carruth
239b081d9691f4509af58919db5760235ca319c9daChandler Carruth/// analyzeBasicBlock - Fill in the current structure with information gleaned
249b081d9691f4509af58919db5760235ca319c9daChandler Carruth/// from the specified block.
259b081d9691f4509af58919db5760235ca319c9daChandler Carruthvoid CodeMetrics::analyzeBasicBlock(const BasicBlock *BB,
26a5157e68d183e1bdf010e94a15dc0c44b65f889bChandler Carruth                                    const TargetTransformInfo &TTI) {
279b081d9691f4509af58919db5760235ca319c9daChandler Carruth  ++NumBlocks;
289b081d9691f4509af58919db5760235ca319c9daChandler Carruth  unsigned NumInstsBeforeThisBB = NumInsts;
299b081d9691f4509af58919db5760235ca319c9daChandler Carruth  for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
309b081d9691f4509af58919db5760235ca319c9daChandler Carruth       II != E; ++II) {
319b081d9691f4509af58919db5760235ca319c9daChandler Carruth    // Special handling for calls.
329b081d9691f4509af58919db5760235ca319c9daChandler Carruth    if (isa<CallInst>(II) || isa<InvokeInst>(II)) {
339b081d9691f4509af58919db5760235ca319c9daChandler Carruth      ImmutableCallSite CS(cast<Instruction>(II));
349b081d9691f4509af58919db5760235ca319c9daChandler Carruth
359b081d9691f4509af58919db5760235ca319c9daChandler Carruth      if (const Function *F = CS.getCalledFunction()) {
369b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // If a function is both internal and has a single use, then it is
379b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // extremely likely to get inlined in the future (it was probably
389b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // exposed by an interleaved devirtualization pass).
399b081d9691f4509af58919db5760235ca319c9daChandler Carruth        if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse())
409b081d9691f4509af58919db5760235ca319c9daChandler Carruth          ++NumInlineCandidates;
419b081d9691f4509af58919db5760235ca319c9daChandler Carruth
429b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // If this call is to function itself, then the function is recursive.
439b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // Inlining it into other functions is a bad idea, because this is
449b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // basically just a form of loop peeling, and our metrics aren't useful
459b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // for that case.
469b081d9691f4509af58919db5760235ca319c9daChandler Carruth        if (F == BB->getParent())
479b081d9691f4509af58919db5760235ca319c9daChandler Carruth          isRecursive = true;
489b081d9691f4509af58919db5760235ca319c9daChandler Carruth
4913086a658ae06046ded902229f9918b8bad505bdChandler Carruth        if (TTI.isLoweredToCall(F))
5013086a658ae06046ded902229f9918b8bad505bdChandler Carruth          ++NumCalls;
5113086a658ae06046ded902229f9918b8bad505bdChandler Carruth      } else {
529b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // We don't want inline asm to count as a call - that would prevent loop
539b081d9691f4509af58919db5760235ca319c9daChandler Carruth        // unrolling. The argument setup cost is still real, though.
549b081d9691f4509af58919db5760235ca319c9daChandler Carruth        if (!isa<InlineAsm>(CS.getCalledValue()))
559b081d9691f4509af58919db5760235ca319c9daChandler Carruth          ++NumCalls;
569b081d9691f4509af58919db5760235ca319c9daChandler Carruth      }
579b081d9691f4509af58919db5760235ca319c9daChandler Carruth    }
589b081d9691f4509af58919db5760235ca319c9daChandler Carruth
599b081d9691f4509af58919db5760235ca319c9daChandler Carruth    if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
609b081d9691f4509af58919db5760235ca319c9daChandler Carruth      if (!AI->isStaticAlloca())
619b081d9691f4509af58919db5760235ca319c9daChandler Carruth        this->usesDynamicAlloca = true;
629b081d9691f4509af58919db5760235ca319c9daChandler Carruth    }
639b081d9691f4509af58919db5760235ca319c9daChandler Carruth
649b081d9691f4509af58919db5760235ca319c9daChandler Carruth    if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy())
659b081d9691f4509af58919db5760235ca319c9daChandler Carruth      ++NumVectorInsts;
669b081d9691f4509af58919db5760235ca319c9daChandler Carruth
6767ae13575900e8efd056672987249fd0adbf5e73James Molloy    if (const CallInst *CI = dyn_cast<CallInst>(II))
6867ae13575900e8efd056672987249fd0adbf5e73James Molloy      if (CI->hasFnAttr(Attribute::NoDuplicate))
6967ae13575900e8efd056672987249fd0adbf5e73James Molloy        notDuplicatable = true;
7067ae13575900e8efd056672987249fd0adbf5e73James Molloy
7167ae13575900e8efd056672987249fd0adbf5e73James Molloy    if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II))
7267ae13575900e8efd056672987249fd0adbf5e73James Molloy      if (InvI->hasFnAttr(Attribute::NoDuplicate))
7367ae13575900e8efd056672987249fd0adbf5e73James Molloy        notDuplicatable = true;
7467ae13575900e8efd056672987249fd0adbf5e73James Molloy
7513086a658ae06046ded902229f9918b8bad505bdChandler Carruth    NumInsts += TTI.getUserCost(&*II);
769b081d9691f4509af58919db5760235ca319c9daChandler Carruth  }
779b081d9691f4509af58919db5760235ca319c9daChandler Carruth
789b081d9691f4509af58919db5760235ca319c9daChandler Carruth  if (isa<ReturnInst>(BB->getTerminator()))
799b081d9691f4509af58919db5760235ca319c9daChandler Carruth    ++NumRets;
809b081d9691f4509af58919db5760235ca319c9daChandler Carruth
819b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // We never want to inline functions that contain an indirectbr.  This is
829b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // incorrect because all the blockaddress's (in static global initializers
839b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // for example) would be referring to the original function, and this indirect
849b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // jump would jump from the inlined copy of the function into the original
859b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // function which is extremely undefined behavior.
869b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // FIXME: This logic isn't really right; we can safely inline functions
879b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // with indirectbr's as long as no other function or global references the
889b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // blockaddress of a block within the current function.  And as a QOI issue,
899b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // if someone is using a blockaddress without an indirectbr, and that
909b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // reference somehow ends up in another function or global, we probably
919b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // don't want to inline this function.
9267ae13575900e8efd056672987249fd0adbf5e73James Molloy  notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
939b081d9691f4509af58919db5760235ca319c9daChandler Carruth
949b081d9691f4509af58919db5760235ca319c9daChandler Carruth  // Remember NumInsts for this BB.
959b081d9691f4509af58919db5760235ca319c9daChandler Carruth  NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
969b081d9691f4509af58919db5760235ca319c9daChandler Carruth}
97