170745c661de12e3e5b7892776b190f2db703509bChris Lattner//===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
26899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel//
36899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel//                     The LLVM Compiler Infrastructure
46899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
76899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel//
86899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel//===----------------------------------------------------------------------===//
96899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel//
1058ca8d55d4ad9d277f251c4c85fb6db30d96bfc7Dan Gohman// This file implements heuristics for inlining decisions.
116899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel//
126899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel//===----------------------------------------------------------------------===//
136899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel
14e4aeec003f82a5263ffb168e175e6fca8b6f681dDan Gohman#ifndef LLVM_ANALYSIS_INLINECOST_H
15e4aeec003f82a5263ffb168e175e6fca8b6f681dDan Gohman#define LLVM_ANALYSIS_INLINECOST_H
166899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel
1786953b5795007eaa98838297360a6987e33e92e7Chandler Carruth#include "llvm/Analysis/CallGraphSCCPass.h"
18066b5d8403483bf3a8bb033b690da318fbc68e79Benjamin Kramer#include <cassert>
19066b5d8403483bf3a8bb033b690da318fbc68e79Benjamin Kramer#include <climits>
206899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel
216899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patelnamespace llvm {
22ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesclass AssumptionCacheTracker;
234ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruthclass CallSite;
244ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruthclass DataLayout;
254ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruthclass Function;
26ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesclass TargetTransformInfoWrapperPass;
274ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
284ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruthnamespace InlineConstants {
294ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  // Various magic constants used to adjust heuristics.
304ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const int InstrCost = 5;
314ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const int IndirectCallThreshold = 100;
324ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const int CallPenalty = 25;
334ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const int LastCallToStaticBonus = -15000;
344ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const int ColdccPenalty = 2000;
354ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const int NoreturnPenalty = 10000;
364ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// Do not inline functions which allocate this many bytes on the stack
374ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// when the caller is recursive.
384ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const unsigned TotalAllocaSizeRecursiveCaller = 1024;
394ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth}
404ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
414ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth/// \brief Represents the cost of inlining a function.
424ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth///
434ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth/// This supports special values for functions which should "always" or
444ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth/// "never" be inlined. Otherwise, the cost represents a unitless amount;
454ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth/// smaller values increase the likelihood of the function being inlined.
464ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth///
474ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth/// Objects of this type also provide the adjusted threshold for inlining
484ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth/// based on the information available for a particular callsite. They can be
494ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth/// directly tested to determine if inlining should occur given the cost and
504ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth/// threshold for this cost metric.
514ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruthclass InlineCost {
524ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  enum SentinelValues {
534ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    AlwaysInlineCost = INT_MIN,
544ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    NeverInlineCost = INT_MAX
554ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  };
564ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
574ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// \brief The estimated cost of inlining this callsite.
584ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const int Cost;
594ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
604ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// \brief The adjusted threshold against which this cost was computed.
614ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  const int Threshold;
626899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel
634ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  // Trivial constructor, interesting logic in the factory functions below.
644ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  InlineCost(int Cost, int Threshold) : Cost(Cost), Threshold(Threshold) {}
654ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
664ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruthpublic:
674ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  static InlineCost get(int Cost, int Threshold) {
684ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
694ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
704ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    return InlineCost(Cost, Threshold);
714ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  }
724ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  static InlineCost getAlways() {
734ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    return InlineCost(AlwaysInlineCost, 0);
744ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  }
754ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  static InlineCost getNever() {
764ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    return InlineCost(NeverInlineCost, 0);
77bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen  }
78bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen
794ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// \brief Test whether the inline cost is low enough for inlining.
80ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  explicit operator bool() const {
814ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    return Cost < Threshold;
824ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  }
834ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
844ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  bool isAlways() const { return Cost == AlwaysInlineCost; }
854ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  bool isNever() const { return Cost == NeverInlineCost; }
864ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  bool isVariable() const { return !isAlways() && !isNever(); }
874ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
884ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// \brief Get the inline cost estimate.
894ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// It is an error to call this on an "always" or "never" InlineCost.
904ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  int getCost() const {
914ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    assert(isVariable() && "Invalid access of InlineCost");
924ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth    return Cost;
934ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  }
944ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
954ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// \brief Get the cost delta from the threshold for inlining.
964ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// Only valid if the cost is of the variable kind. Returns a negative
974ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// value if the cost is too high to inline.
984ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  int getCostDelta() const { return Threshold - getCost(); }
994ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth};
1004ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
10178e1cdaba3cad6e85ff631542052bbb718c7e588Chandler Carruth/// \brief Cost analyzer used by inliner.
10286953b5795007eaa98838297360a6987e33e92e7Chandler Carruthclass InlineCostAnalysis : public CallGraphSCCPass {
103ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  TargetTransformInfoWrapperPass *TTIWP;
104ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  AssumptionCacheTracker *ACT;
1054ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
1064ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruthpublic:
10786953b5795007eaa98838297360a6987e33e92e7Chandler Carruth  static char ID;
1084ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
10986953b5795007eaa98838297360a6987e33e92e7Chandler Carruth  InlineCostAnalysis();
1102c3e0051c31c3f5b2328b447eadf1cf9c4427442Pirama Arumuga Nainar  ~InlineCostAnalysis() override;
11186953b5795007eaa98838297360a6987e33e92e7Chandler Carruth
11286953b5795007eaa98838297360a6987e33e92e7Chandler Carruth  // Pass interface implementation.
11336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void getAnalysisUsage(AnalysisUsage &AU) const override;
11436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool runOnSCC(CallGraphSCC &SCC) override;
1154ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
1164ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// \brief Get an InlineCost object representing the cost of inlining this
1174ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// callsite.
118f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth  ///
1194ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// Note that threshold is passed into this function. Only costs below the
1204ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// threshold are computed with any accuracy. The threshold can be used to
1214ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// bound the computation necessary to determine whether the cost is
1224ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// sufficiently low to warrant inlining.
12386953b5795007eaa98838297360a6987e33e92e7Chandler Carruth  ///
12486953b5795007eaa98838297360a6987e33e92e7Chandler Carruth  /// Also note that calling this function *dynamically* computes the cost of
12586953b5795007eaa98838297360a6987e33e92e7Chandler Carruth  /// inlining the callsite. It is an expensive, heavyweight call.
1264ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  InlineCost getInlineCost(CallSite CS, int Threshold);
1274ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
1284ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// \brief Get an InlineCost with the callee explicitly specified.
1294ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// This allows you to calculate the cost of inlining a function via a
1304ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// pointer. This behaves exactly as the version with no explicit callee
1314ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// parameter in all other respects.
1324ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  //
1334ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  //  Note: This is used by out-of-tree passes, please do not remove without
1344ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  //  adding a replacement API.
1354ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  InlineCost getInlineCost(CallSite CS, Function *Callee, int Threshold);
1364ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth
1374ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  /// \brief Minimal filter to detect invalid constructs for inlining.
1384ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth  bool isInlineViable(Function &Callee);
1394ef13242ab7473de62c2c4d77de3c69d362bd9efChandler Carruth};
140aa034fa2299e41b73f60d3993f5460260e239fabJakob Stoklund Olesen
1416899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel}
1426899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel
1436899b314225dd5fa5ccc2a5692daaa89c1d623d8Devang Patel#endif
144