For PR1043:
[oota-llvm.git] / lib / Transforms / IPO / InlineSimple.cpp
index 770f05c162a0875a7044868fdb87a9716aee7c40..52d75735ef749a998634c543add96773492c6af2 100644 (file)
@@ -1,10 +1,10 @@
 //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
-// 
+//
 //                     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.
@@ -12,7 +12,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "Inliner.h"
+#include "llvm/CallingConv.h"
 #include "llvm/Instructions.h"
+#include "llvm/IntrinsicInst.h"
 #include "llvm/Function.h"
 #include "llvm/Type.h"
 #include "llvm/Support/CallSite.h"
@@ -31,16 +33,6 @@ namespace {
   // FunctionInfo - For each function, calculate the size of it in blocks and
   // instructions.
   struct FunctionInfo {
-    // HasAllocas - Keep track of whether or not a function contains an alloca
-    // instruction that is not in the entry block of the function.  Inlining
-    // this call could cause us to blow out the stack, because the stack memory
-    // would never be released.
-    //
-    // FIXME: LLVM needs a way of dealloca'ing memory, which would make this
-    // irrelevant!
-    //
-    bool HasAllocas;
-
     // 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;
@@ -51,7 +43,7 @@ namespace {
     // entry here.
     std::vector<ArgInfo> ArgumentWeights;
 
-    FunctionInfo() : HasAllocas(false), NumInsts(0), NumBlocks(0) {}
+    FunctionInfo() : NumInsts(0), NumBlocks(0) {}
 
     /// analyzeFunction - Fill in the current structure with information gleaned
     /// from the specified function.
@@ -63,7 +55,7 @@ namespace {
   public:
     int getInlineCost(CallSite CS);
   };
-  RegisterOpt<SimpleInliner> X("inline", "Function Integration/Inlining");
+  RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
 }
 
 ModulePass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
@@ -100,7 +92,7 @@ static unsigned CountCodeReductionForConstant(Value *V) {
       if (AllOperandsConstant) {
         // We will get to remove this instruction...
         Reduction += 7;
-        
+
         // And any other instructions that use it which become constants
         // themselves.
         Reduction += CountCodeReductionForConstant(&Inst);
@@ -147,15 +139,27 @@ void FunctionInfo::analyzeFunction(Function *F) {
   for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
     for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
          II != E; ++II) {
-      ++NumInsts;
-
-      // If there is an alloca in the body of the function, we cannot currently
-      // inline the function without the risk of exploding the stack.
-      if (isa<AllocaInst>(II) && BB != F->begin()) {
-        HasAllocas = true;
-        this->NumBlocks = this->NumInsts = 1;
-        return;
+      if (isa<DbgInfoIntrinsic>(II)) continue;  // Debug intrinsics don't count.
+      
+      // Noop casts, including ptr <-> int,  don't count.
+      if (const CastInst *CI = dyn_cast<CastInst>(II)) {
+        if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) || 
+            isa<PtrToIntInst>(CI))
+          continue;
+      } else if (const GetElementPtrInst *GEPI =
+                         dyn_cast<GetElementPtrInst>(II)) {
+        // If a GEP has all constant indices, it will probably be folded with
+        // a load/store.
+        bool AllConstant = true;
+        for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
+          if (!isa<ConstantInt>(GEPI->getOperand(i))) {
+            AllConstant = false;
+            break;
+          }
+        if (AllConstant) continue;
       }
+      
+      ++NumInsts;
     }
 
     ++NumBlocks;
@@ -166,7 +170,7 @@ void FunctionInfo::analyzeFunction(Function *F) {
 
   // Check out all of the arguments to the function, figuring out how much
   // code can be eliminated if one of the arguments is a constant.
-  for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
+  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
     ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I),
                                       CountCodeReductionForAlloca(I)));
 }
@@ -193,7 +197,21 @@ int SimpleInliner::getInlineCost(CallSite CS) {
   // make it almost guaranteed to be inlined.
   //
   if (Callee->hasInternalLinkage() && Callee->hasOneUse())
-    InlineCost -= 5000;
+    InlineCost -= 30000;
+
+  // If this function uses the coldcc calling convention, prefer not to inline
+  // it.
+  if (Callee->getCallingConv() == CallingConv::Cold)
+    InlineCost += 2000;
+
+  // If the instruction after the call, or if the normal destination of the
+  // invoke is an unreachable instruction, the function is noreturn.  As such,
+  // there is little point in inlining this.
+  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
+    if (isa<UnreachableInst>(II->getNormalDest()->begin()))
+      InlineCost += 10000;
+  } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall)))
+    InlineCost += 10000;
 
   // Get information about the callee...
   FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
@@ -202,11 +220,6 @@ int SimpleInliner::getInlineCost(CallSite CS) {
   if (CalleeFI.NumBlocks == 0)
     CalleeFI.analyzeFunction(Callee);
 
-  // Don't inline calls to functions with allocas that are not in the entry
-  // block of the function.
-  if (CalleeFI.HasAllocas)
-    return 2000000000;
-
   // Add to the inline quality for properties that make the call valuable to
   // inline.  This includes factors that indicate that the result of inlining
   // the function will be optimizable.  Currently this just looks at arguments
@@ -229,7 +242,7 @@ int SimpleInliner::getInlineCost(CallSite CS) {
     // significant future optimization possibilities (like scalar promotion, and
     // scalarization), so encourage the inlining of the function.
     //
-    else if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
+    else if (isa<AllocaInst>(I)) {
       if (ArgNo < CalleeFI.ArgumentWeights.size())
         InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight;