* Remove all cfg simplification stuff for a new cfg simplify pass (todo)
[oota-llvm.git] / lib / Transforms / LevelRaise.cpp
index 2eafcf05ee85d5c3ddb9d48218a20d68cc521a44..cc684b5f150fa7f09d2169855a36f79dc2ac6909 100644 (file)
@@ -8,20 +8,16 @@
 
 #include "llvm/Transforms/LevelChange.h"
 #include "TransformInternals.h"
-#include "llvm/Method.h"
 #include "llvm/iOther.h"
 #include "llvm/iMemory.h"
-#include "llvm/ConstantVals.h"
 #include "llvm/Pass.h"
+#include "llvm/ConstantHandling.h"
 #include "llvm/Transforms/Scalar/DCE.h"
-#include "llvm/Transforms/Scalar/ConstantHandling.h"
 #include "llvm/Transforms/Scalar/ConstantProp.h"
 #include "llvm/Analysis/Expressions.h"
 #include "Support/STLExtras.h"
 #include <algorithm>
 
-#include "llvm/Assembly/Writer.h"
-
 //#define DEBUG_PEEPHOLE_INSTS 1
 
 #ifdef DEBUG_PEEPHOLE_INSTS
@@ -51,7 +47,6 @@ static inline bool isReinterpretingCast(const CastInst *CI) {
 }
 
 
-
 // Peephole optimize the following instructions:
 // %t1 = cast ? to x *
 // %t2 = add x * %SP, %t1              ;; Constant must be 2nd operand
@@ -62,6 +57,7 @@ static inline bool isReinterpretingCast(const CastInst *CI) {
 static bool HandleCastToPointer(BasicBlock::iterator BI,
                                 const PointerType *DestPTy) {
   CastInst *CI = cast<CastInst>(*BI);
+  if (CI->use_empty()) return false;
 
   // Scan all of the uses, looking for any uses that are not add
   // instructions.  If we have non-adds, do not make this transformation.
@@ -86,18 +82,36 @@ static bool HandleCastToPointer(BasicBlock::iterator BI,
   // If we have a getelementptr capability... transform all of the 
   // add instruction uses into getelementptr's.
   while (!CI->use_empty()) {
-    Instruction *I = cast<Instruction>(*CI->use_begin());
+    BinaryOperator *I = cast<BinaryOperator>(*CI->use_begin());
     assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
            "Use is not a valid add instruction!");
     
     // Get the value added to the cast result pointer...
     Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0);
 
-    GetElementPtrInst *GEP = new GetElementPtrInst(OtherPtr, Indices);
+    Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
     PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
-    
-    // Replace the old add instruction with the shiny new GEP inst
-    ReplaceInstWithInst(I, GEP);
+
+    if (GEP->getType() == I->getType()) {
+      // Replace the old add instruction with the shiny new GEP inst
+      ReplaceInstWithInst(I, GEP);
+    } else {
+      // If the type produced by the gep instruction differs from the original
+      // add instruction type, insert a cast now.
+      //
+
+      // Insert the GEP instruction before the old add instruction... and get an
+      // iterator to point at the add instruction...
+      BasicBlock::iterator GEPI = InsertInstBeforeInst(GEP, I)+1;
+
+      PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
+      CastInst *CI = new CastInst(GEP, I->getType());
+      GEP = CI;
+
+      // Replace the old add instruction with the shiny new GEP inst
+      ReplaceInstWithInst(I->getParent()->getInstList(), GEPI, GEP);
+    }
+
     PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
   }
   return true;
@@ -176,26 +190,6 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
       return true;
     }
 
-    // Peephole optimize the following instructions:
-    // %tmp = cast <ty> %V to <ty2>
-    // %V   = cast <ty2> %tmp to <ty3>     ; Where ty & ty2 are same size
-    //
-    // Into: cast <ty> %V to <ty3>
-    //
-    if (SrcI)
-      if (CastInst *CSrc = dyn_cast<CastInst>(SrcI))
-        if (isReinterpretingCast(CI) + isReinterpretingCast(CSrc) < 2) {
-          // We can only do c-c elimination if, at most, one cast does a
-          // reinterpretation of the input data.
-          //
-          // If legal, make this cast refer the the original casts argument!
-          //
-          PRINT_PEEPHOLE2("cast-cast:in ", CI, CSrc);
-          CI->setOperand(0, CSrc->getOperand(0));
-          PRINT_PEEPHOLE1("cast-cast:out", CI);
-          return true;
-        }
-
     // Check to see if it's a cast of an instruction that does not depend on the
     // specific type of the operands to do it's job.
     if (!isReinterpretingCast(CI)) {
@@ -398,9 +392,9 @@ static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
 
 
 
-static bool DoRaisePass(Method *M) {
+static bool DoRaisePass(Function *F) {
   bool Changed = false;
-  for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
+  for (Function::iterator MI = F->begin(), ME = F->end(); MI != ME; ++MI) {
     BasicBlock *BB = *MI;
     BasicBlock::InstListType &BIL = BB->getInstList();
 
@@ -423,12 +417,12 @@ static bool DoRaisePass(Method *M) {
 }
 
 
-// RaisePointerReferences::doit - Raise a method representation to a higher
+// RaisePointerReferences::doit - Raise a function representation to a higher
 // level.
 //
-static bool doRPR(Method *M) {
+static bool doRPR(Function *F) {
 #ifdef DEBUG_PEEPHOLE_INSTS
-  cerr << "\n\n\nStarting to work on Method '" << M->getName() << "'\n";
+  cerr << "\n\n\nStarting to work on Function '" << F->getName() << "'\n";
 #endif
 
   // Insert casts for all incoming pointer pointer values that are treated as
@@ -438,13 +432,13 @@ static bool doRPR(Method *M) {
   
   do {
 #ifdef DEBUG_PEEPHOLE_INSTS
-    cerr << "Looping: \n" << M;
+    cerr << "Looping: \n" << F;
 #endif
 
-    // Iterate over the method, refining it, until it converges on a stable
+    // Iterate over the function, refining it, until it converges on a stable
     // state
     LocalChange = false;
-    while (DoRaisePass(M)) LocalChange = true;
+    while (DoRaisePass(F)) LocalChange = true;
     Changed |= LocalChange;
 
   } while (LocalChange);
@@ -453,8 +447,14 @@ static bool doRPR(Method *M) {
 }
 
 namespace {
-  struct RaisePointerReferences : public MethodPass {
-    virtual bool runOnMethod(Method *M) { return doRPR(M); }
+  struct RaisePointerReferences : public FunctionPass {
+    const char *getPassName() const { return "Raise Pointer References"; }
+
+    virtual bool runOnFunction(Function *F) { return doRPR(F); }
+
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.preservesCFG();
+    }
   };
 }