From 6899b314225dd5fa5ccc2a5692daaa89c1d623d8 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Wed, 25 Jul 2007 18:00:25 +0000 Subject: Add BasicInliner interface. This interface allows clients to inline bunch of functions with module level call graph information.:wq git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@40486 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/Utils/BasicInliner.h | 55 +++++++++++++++++++ include/llvm/Transforms/Utils/InlineCost.h | 80 ++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 include/llvm/Transforms/Utils/BasicInliner.h create mode 100644 include/llvm/Transforms/Utils/InlineCost.h (limited to 'include/llvm/Transforms') diff --git a/include/llvm/Transforms/Utils/BasicInliner.h b/include/llvm/Transforms/Utils/BasicInliner.h new file mode 100644 index 0000000000..311047ccdd --- /dev/null +++ b/include/llvm/Transforms/Utils/BasicInliner.h @@ -0,0 +1,55 @@ +//===- BasicInliner.h - Basic function level inliner ------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Devang Patel and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines a simple function based inliner that does not use +// call graph information. +// +//===----------------------------------------------------------------------===// + +#ifndef BASICINLINER_H +#define BASICINLINER_H + +#include "llvm/Transforms/Utils/InlineCost.h" + +namespace llvm { + + class Function; + class TargetData; + class BasicInlinerImpl; + + /// BasicInliner - BasicInliner provides function level inlining interface. + /// Clients provide list of functions which are inline without using + /// module level call graph information. Note that the BasicInliner is + /// free to delete a function if it is inlined into all call sites. + class BasicInliner { + public: + + BasicInliner(TargetData *T = NULL); + ~BasicInliner(); + + /// addFunction - Add function into the list of functions to process. + /// All functions must be inserted using this interface before invoking + /// inlineFunctions(). + void addFunction(Function *F); + + /// neverInlineFunction - Sometimes a function is never to be inlined + /// because of one or other reason. + void neverInlineFunction(Function *F); + + /// inlineFuctions - Walk all call sites in all functions supplied by + /// client. Inline as many call sites as possible. Delete completely + /// inlined functions. + void inlineFunctions(); + + private: + BasicInlinerImpl *Impl; + }; +} + +#endif diff --git a/include/llvm/Transforms/Utils/InlineCost.h b/include/llvm/Transforms/Utils/InlineCost.h new file mode 100644 index 0000000000..c54b98af52 --- /dev/null +++ b/include/llvm/Transforms/Utils/InlineCost.h @@ -0,0 +1,80 @@ +//===- InlineCost.cpp - Cost analysis for inliner ---------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements bottom-up inlining of functions into callees. +// +//===----------------------------------------------------------------------===// + +#ifndef INLINECOST_H +#define INLINECOST_H + +#include +#include +#include + +namespace llvm { + + class Value; + class Function; + class CallSite; + + /// InlineCostAnalyzer - Cost analyzer used by inliner. + class InlineCostAnalyzer { + struct ArgInfo { + public: + unsigned ConstantWeight; + unsigned AllocaWeight; + + ArgInfo(unsigned CWeight, unsigned AWeight) + : ConstantWeight(CWeight), AllocaWeight(AWeight) {} + }; + + // FunctionInfo - For each function, calculate the size of it in blocks and + // instructions. + struct FunctionInfo { + // NumInsts, NumBlocks - Keep track of how large each function is, which is + // used to estimate the code size cost of inlining it. + unsigned NumInsts, NumBlocks; + + // ArgumentWeights - Each formal argument of the function is inspected to + // see if it is used in any contexts where making it a constant or alloca + // would reduce the code size. If so, we add some value to the argument + // entry here. + std::vector ArgumentWeights; + + FunctionInfo() : NumInsts(0), NumBlocks(0) {} + + /// analyzeFunction - Fill in the current structure with information gleaned + /// from the specified function. + void analyzeFunction(Function *F); + + // CountCodeReductionForConstant - Figure out an approximation for how many + // instructions will be constant folded if the specified value is constant. + // + unsigned CountCodeReductionForConstant(Value *V); + + // CountCodeReductionForAlloca - Figure out an approximation of how much smaller + // the function will be if it is inlined into a context where an argument + // becomes an alloca. + // + unsigned CountCodeReductionForAlloca(Value *V); + }; + + std::mapCachedFunctionInfo; + + public: + + // getInlineCost - The heuristic used to determine if we should inline the + // function call or not. + // + int getInlineCost(CallSite CS, std::set &NeverInline); + }; +} + +#endif -- cgit v1.2.3-70-g09d2