LLVM API Documentation

CodeMetrics.h
Go to the documentation of this file.
00001 //===- CodeMetrics.h - Code cost measurements -------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements various weight measurements for code, helping
00011 // the Inliner and other passes decide whether to duplicate its contents.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_ANALYSIS_CODEMETRICS_H
00016 #define LLVM_ANALYSIS_CODEMETRICS_H
00017 
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/SmallPtrSet.h"
00020 #include "llvm/IR/CallSite.h"
00021 
00022 namespace llvm {
00023 class AssumptionTracker;
00024 class BasicBlock;
00025 class Loop;
00026 class Function;
00027 class Instruction;
00028 class DataLayout;
00029 class TargetTransformInfo;
00030 class Value;
00031 
00032 /// \brief Check whether a call will lower to something small.
00033 ///
00034 /// This tests checks whether this callsite will lower to something
00035 /// significantly cheaper than a traditional call, often a single
00036 /// instruction. Note that if isInstructionFree(CS.getInstruction()) would
00037 /// return true, so will this function.
00038 bool callIsSmall(ImmutableCallSite CS);
00039 
00040 /// \brief Utility to calculate the size and a few similar metrics for a set
00041 /// of basic blocks.
00042 struct CodeMetrics {
00043   /// \brief True if this function contains a call to setjmp or other functions
00044   /// with attribute "returns twice" without having the attribute itself.
00045   bool exposesReturnsTwice;
00046 
00047   /// \brief True if this function calls itself.
00048   bool isRecursive;
00049 
00050   /// \brief True if this function cannot be duplicated.
00051   ///
00052   /// True if this function contains one or more indirect branches, or it contains
00053   /// one or more 'noduplicate' instructions.
00054   bool notDuplicatable;
00055 
00056   /// \brief True if this function calls alloca (in the C sense).
00057   bool usesDynamicAlloca;
00058 
00059   /// \brief Number of instructions in the analyzed blocks.
00060   unsigned NumInsts;
00061 
00062   /// \brief Number of analyzed blocks.
00063   unsigned NumBlocks;
00064 
00065   /// \brief Keeps track of basic block code size estimates.
00066   DenseMap<const BasicBlock *, unsigned> NumBBInsts;
00067 
00068   /// \brief Keep track of the number of calls to 'big' functions.
00069   unsigned NumCalls;
00070 
00071   /// \brief The number of calls to internal functions with a single caller.
00072   ///
00073   /// These are likely targets for future inlining, likely exposed by
00074   /// interleaved devirtualization.
00075   unsigned NumInlineCandidates;
00076 
00077   /// \brief How many instructions produce vector values.
00078   ///
00079   /// The inliner is more aggressive with inlining vector kernels.
00080   unsigned NumVectorInsts;
00081 
00082   /// \brief How many 'ret' instructions the blocks contain.
00083   unsigned NumRets;
00084 
00085   CodeMetrics()
00086       : exposesReturnsTwice(false), isRecursive(false), notDuplicatable(false),
00087         usesDynamicAlloca(false), NumInsts(0), NumBlocks(0), NumCalls(0),
00088         NumInlineCandidates(0), NumVectorInsts(0), NumRets(0) {}
00089 
00090   /// \brief Add information about a block to the current state.
00091   void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI,
00092                          SmallPtrSetImpl<const Value*> &EphValues);
00093 
00094   /// \brief Collect a loop's ephemeral values (those used only by an assume
00095   /// or similar intrinsics in the loop).
00096   static void collectEphemeralValues(const Loop *L, AssumptionTracker *AT,
00097                                      SmallPtrSetImpl<const Value*> &EphValues);
00098 
00099   /// \brief Collect a functions's ephemeral values (those used only by an
00100   /// assume or similar intrinsics in the function).
00101   static void collectEphemeralValues(const Function *L, AssumptionTracker *AT,
00102                                      SmallPtrSetImpl<const Value*> &EphValues);
00103 };
00104 
00105 }
00106 
00107 #endif