[llvm-commits] CVS: llvm/lib/Transforms/Utils/InlineFunction.cpp

Chris Lattner lattner at cs.uiuc.edu
Sat Jan 14 12:08:17 PST 2006



Changes in directory llvm/lib/Transforms/Utils:

InlineFunction.cpp updated: 1.40 -> 1.41
---
Log message:

Teach the inliner to update the CallGraph itself, and have it add edges to 
llvm.stacksave/restore when it inserts calls to them.


---
Diffs of the changes:  (+53 -4)

 InlineFunction.cpp |   57 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 53 insertions(+), 4 deletions(-)


Index: llvm/lib/Transforms/Utils/InlineFunction.cpp
diff -u llvm/lib/Transforms/Utils/InlineFunction.cpp:1.40 llvm/lib/Transforms/Utils/InlineFunction.cpp:1.41
--- llvm/lib/Transforms/Utils/InlineFunction.cpp:1.40	Fri Jan 13 13:34:14 2006
+++ llvm/lib/Transforms/Utils/InlineFunction.cpp	Sat Jan 14 14:07:50 2006
@@ -18,11 +18,16 @@
 #include "llvm/Module.h"
 #include "llvm/Instructions.h"
 #include "llvm/Intrinsics.h"
+#include "llvm/Analysis/CallGraph.h"
 #include "llvm/Support/CallSite.h"
 using namespace llvm;
 
-bool llvm::InlineFunction(CallInst *CI) { return InlineFunction(CallSite(CI)); }
-bool llvm::InlineFunction(InvokeInst *II) {return InlineFunction(CallSite(II));}
+bool llvm::InlineFunction(CallInst *CI, CallGraph *CG) {
+  return InlineFunction(CallSite(CI), CG);
+}
+bool llvm::InlineFunction(InvokeInst *II, CallGraph *CG) {
+  return InlineFunction(CallSite(II), CG);
+}
 
 /// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
 /// in the body of the inlined function into invokes and turn unwind
@@ -131,6 +136,24 @@
   InvokeDest->removePredecessor(II->getParent());
 }
 
+/// UpdateCallGraphAfterInlining - Once we have finished inlining a call from
+/// caller to callee, update the specified callgraph to reflect the changes we
+/// made.
+static void UpdateCallGraphAfterInlining(const Function *Caller, 
+                                         const Function *Callee,
+                                         CallGraph &CG) {
+  // Update the call graph by deleting the edge from Callee to Caller
+  CallGraphNode *CalleeNode = CG[Callee];
+  CallGraphNode *CallerNode = CG[Caller];
+  CallerNode->removeCallEdgeTo(CalleeNode);
+  
+  // Since we inlined all uninlined call sites in the callee into the caller,
+  // add edges from the caller to all of the callees of the callee.
+  for (CallGraphNode::iterator I = CalleeNode->begin(),
+       E = CalleeNode->end(); I != E; ++I)
+    CallerNode->addCalledFunction(*I);
+}
+
 
 // InlineFunction - This function inlines the called function into the basic
 // block of the caller.  This returns false if it is not possible to inline this
@@ -141,7 +164,7 @@
 // exists in the instruction stream.  Similiarly this will inline a recursive
 // function by one level.
 //
-bool llvm::InlineFunction(CallSite CS) {
+bool llvm::InlineFunction(CallSite CS, CallGraph *CG) {
   Instruction *TheCall = CS.getInstruction();
   assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
          "Instruction not in function!");
@@ -234,14 +257,33 @@
     // inlined function.
     for (unsigned i = 0, e = Returns.size(); i != e; ++i)
       new CallInst(StackRestore, SavedPtr, "", Returns[i]);
+
+    // Count the number of StackRestore calls we insert.
+    unsigned NumStackRestores = Returns.size();
     
     // If we are inlining an invoke instruction, insert restores before each
     // unwind.  These unwinds will be rewritten into branches later.
     if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
       for (Function::iterator BB = FirstNewBlock, E = Caller->end();
            BB != E; ++BB)
-        if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator()))
+        if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
           new CallInst(StackRestore, SavedPtr, "", UI);
+          ++NumStackRestores;
+        }
+    }
+      
+    // If we are supposed to update the callgraph, do so now.
+    if (CG) {
+      CallGraphNode *StackSaveCGN    = CG->getOrInsertFunction(StackSave);
+      CallGraphNode *StackRestoreCGN = CG->getOrInsertFunction(StackRestore);
+      CallGraphNode *CallerNode = (*CG)[Caller];
+
+      // 'Caller' now calls llvm.stacksave one more time.
+      CallerNode->addCalledFunction(StackSaveCGN);
+      
+      // 'Caller' now calls llvm.stackrestore the appropriate number of times.
+      for (unsigned i = 0; i != NumStackRestores; ++i)
+        CallerNode->addCalledFunction(StackRestoreCGN);
     }
   }
 
@@ -288,6 +330,9 @@
     // Since we are now done with the return instruction, delete it also.
     Returns[0]->getParent()->getInstList().erase(Returns[0]);
 
+    // Update the callgraph if requested.
+    if (CG) UpdateCallGraphAfterInlining(Caller, CalledFunc, *CG);
+    
     // We are now done with the inlining.
     return true;
   }
@@ -413,5 +458,9 @@
 
   // Now we can remove the CalleeEntry block, which is now empty.
   Caller->getBasicBlockList().erase(CalleeEntry);
+  
+  // Update the callgraph if requested.
+  if (CG) UpdateCallGraphAfterInlining(Caller, CalledFunc, *CG);
+
   return true;
 }






More information about the llvm-commits mailing list