MEGAPATCH checkin.

For details, See: docs/2002-06-25-MegaPatchInfo.txt

llvm-svn: 2779
diff --git a/llvm/lib/Transforms/IPO/InlineSimple.cpp b/llvm/lib/Transforms/IPO/InlineSimple.cpp
index 12430e1..7d36e37 100644
--- a/llvm/lib/Transforms/IPO/InlineSimple.cpp
+++ b/llvm/lib/Transforms/IPO/InlineSimple.cpp
@@ -20,18 +20,16 @@
 
 #include "llvm/Transforms/FunctionInlining.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
 #include "llvm/Pass.h"
 #include "llvm/iTerminators.h"
 #include "llvm/iPHINode.h"
 #include "llvm/iOther.h"
 #include "llvm/Type.h"
-#include "llvm/Argument.h"
 #include "Support/StatisticReporter.h"
-
-static Statistic<> NumInlined("inline\t\t- Number of functions inlined");
 #include <algorithm>
 #include <iostream>
+
+static Statistic<> NumInlined("inline\t\t- Number of functions inlined");
 using std::cerr;
 
 // RemapInstruction - Convert the instruction operands from referencing the 
@@ -65,17 +63,16 @@
 // exists in the instruction stream.  Similiarly this will inline a recursive
 // function by one level.
 //
-bool InlineFunction(BasicBlock::iterator CIIt) {
-  assert(isa<CallInst>(*CIIt) && "InlineFunction only works on CallInst nodes");
-  assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
-  assert((*CIIt)->getParent()->getParent() && "Instruction not in function!");
+bool InlineFunction(CallInst *CI) {
+  assert(isa<CallInst>(CI) && "InlineFunction only works on CallInst nodes");
+  assert(CI->getParent() && "Instruction not embedded in basic block!");
+  assert(CI->getParent()->getParent() && "Instruction not in function!");
 
-  CallInst *CI = cast<CallInst>(*CIIt);
-  const Function *CalledMeth = CI->getCalledFunction();
-  if (CalledMeth == 0 ||   // Can't inline external function or indirect call!
-      CalledMeth->isExternal()) return false;
+  const Function *CalledFunc = CI->getCalledFunction();
+  if (CalledFunc == 0 ||   // Can't inline external function or indirect call!
+      CalledFunc->isExternal()) return false;
 
-  //cerr << "Inlining " << CalledMeth->getName() << " into " 
+  //cerr << "Inlining " << CalledFunc->getName() << " into " 
   //     << CurrentMeth->getName() << "\n";
 
   BasicBlock *OrigBB = CI->getParent();
@@ -84,7 +81,7 @@
   // immediately before the call.  The original basic block now ends with an
   // unconditional branch to NewBB, and NewBB starts with the call instruction.
   //
-  BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
+  BasicBlock *NewBB = OrigBB->splitBasicBlock(CI);
   NewBB->setName("InlinedFunctionReturnNode");
 
   // Remove (unlink) the CallInst from the start of the new basic block.  
@@ -95,8 +92,8 @@
   // function.
   //
   PHINode *PHI = 0;
-  if (CalledMeth->getReturnType() != Type::VoidTy) {
-    PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
+  if (CalledFunc->getReturnType() != Type::VoidTy) {
+    PHI = new PHINode(CalledFunc->getReturnType(), CI->getName());
 
     // The PHI node should go at the front of the new basic block to merge all 
     // possible incoming values.
@@ -118,19 +115,17 @@
   // Add the function arguments to the mapping: (start counting at 1 to skip the
   // function reference itself)
   //
-  Function::ArgumentListType::const_iterator PTI = 
-    CalledMeth->getArgumentList().begin();
+  Function::const_aiterator PTI = CalledFunc->abegin();
   for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
-    ValueMap[*PTI] = CI->getOperand(a);
+    ValueMap[PTI] = CI->getOperand(a);
   
   ValueMap[NewBB] = NewBB;  // Returns get converted to reference NewBB
 
   // Loop over all of the basic blocks in the function, inlining them as 
   // appropriate.  Keep track of the first basic block of the function...
   //
-  for (Function::const_iterator BI = CalledMeth->begin(); 
-       BI != CalledMeth->end(); ++BI) {
-    const BasicBlock *BB = *BI;
+  for (Function::const_iterator BB = CalledFunc->begin(); 
+       BB != CalledFunc->end(); ++BB) {
     assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
     
     // Create a new basic block to copy instructions into!
@@ -148,23 +143,24 @@
     // Loop over all instructions copying them over...
     Instruction *NewInst;
     for (BasicBlock::const_iterator II = BB->begin();
-	 II != (BB->end()-1); ++II) {
-      IBB->getInstList().push_back((NewInst = (*II)->clone()));
-      ValueMap[*II] = NewInst;                  // Add instruction map to value.
-      if ((*II)->hasName())
-        NewInst->setName((*II)->getName()+".i");  // .i = inlined once
+	 II != --BB->end(); ++II) {
+      IBB->getInstList().push_back((NewInst = II->clone()));
+      ValueMap[II] = NewInst;                  // Add instruction map to value.
+      if (II->hasName())
+        NewInst->setName(II->getName()+".i");  // .i = inlined once
     }
 
     // Copy over the terminator now...
     switch (TI->getOpcode()) {
     case Instruction::Ret: {
-      const ReturnInst *RI = cast<const ReturnInst>(TI);
+      const ReturnInst *RI = cast<ReturnInst>(TI);
 
       if (PHI) {   // The PHI node should include this value!
 	assert(RI->getReturnValue() && "Ret should have value!");
 	assert(RI->getReturnValue()->getType() == PHI->getType() && 
 	       "Ret value not consistent in function!");
-	PHI->addIncoming((Value*)RI->getReturnValue(), cast<BasicBlock>(BB));
+	PHI->addIncoming((Value*)RI->getReturnValue(),
+                         (BasicBlock*)cast<BasicBlock>(&*BB));
       }
 
       // Add a branch to the code that was after the original Call.
@@ -185,15 +181,14 @@
   // Loop over all of the instructions in the function, fixing up operand 
   // references as we go.  This uses ValueMap to do all the hard work.
   //
-  for (Function::const_iterator BI = CalledMeth->begin(); 
-       BI != CalledMeth->end(); ++BI) {
-    const BasicBlock *BB = *BI;
+  for (Function::const_iterator BB = CalledFunc->begin(); 
+       BB != CalledFunc->end(); ++BB) {
     BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
 
     // Loop over all instructions, fixing each one as we find it...
     //
-    for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
-      RemapInstruction(*II, ValueMap);
+    for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
+      RemapInstruction(II, ValueMap);
   }
 
   if (PHI) RemapInstruction(PHI, ValueMap);  // Fix the PHI node also...
@@ -204,24 +199,13 @@
   TerminatorInst *Br = OrigBB->getTerminator();
   assert(Br && Br->getOpcode() == Instruction::Br && 
 	 "splitBasicBlock broken!");
-  Br->setOperand(0, ValueMap[CalledMeth->front()]);
+  Br->setOperand(0, ValueMap[&CalledFunc->front()]);
 
   // Since we are now done with the CallInst, we can finally delete it.
   delete CI;
   return true;
 }
 
-bool InlineFunction(CallInst *CI) {
-  assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
-  BasicBlock *PBB = CI->getParent();
-
-  BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
-
-  assert(CallIt != PBB->end() && 
-	 "CallInst has parent that doesn't contain CallInst?!?");
-  return InlineFunction(CallIt);
-}
-
 static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
   assert(CI->getParent() && CI->getParent()->getParent() && 
 	 "Call not embedded into a function!");
@@ -242,11 +226,12 @@
 
 static inline bool DoFunctionInlining(BasicBlock *BB) {
   for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
-    if (CallInst *CI = dyn_cast<CallInst>(*I)) {
+    if (CallInst *CI = dyn_cast<CallInst>(&*I)) {
       // Check to see if we should inline this function
       Function *F = CI->getCalledFunction();
-      if (F && ShouldInlineFunction(CI, F))
-	return InlineFunction(I);
+      if (F && ShouldInlineFunction(CI, F)) {
+	return InlineFunction(CI);
+      }
     }
   }
   return false;
@@ -255,16 +240,14 @@
 // doFunctionInlining - Use a heuristic based approach to inline functions that
 // seem to look good.
 //
-static bool doFunctionInlining(Function *F) {
+static bool doFunctionInlining(Function &F) {
   bool Changed = false;
 
   // Loop through now and inline instructions a basic block at a time...
-  for (Function::iterator I = F->begin(); I != F->end(); )
-    if (DoFunctionInlining(*I)) {
+  for (Function::iterator I = F.begin(); I != F.end(); )
+    if (DoFunctionInlining(I)) {
       ++NumInlined;
       Changed = true;
-      // Iterator is now invalidated by new basic blocks inserted
-      I = F->begin();
     } else {
       ++I;
     }
@@ -275,7 +258,7 @@
 namespace {
   struct FunctionInlining : public FunctionPass {
     const char *getPassName() const { return "Function Inlining"; }
-    virtual bool runOnFunction(Function *F) {
+    virtual bool runOnFunction(Function &F) {
       return doFunctionInlining(F);
     }
   };