synchronizeICache removeed from TargetJITInfo.
[oota-llvm.git] / lib / Transforms / IPO / RaiseAllocations.cpp
index fd5b7fb1f13c3f25f6a8dcd743f0030bfab31f74..2b487d009d7c2d1dd82e36ad66f0faa18dbfb3ba 100644 (file)
@@ -1,10 +1,10 @@
 //===- RaiseAllocations.cpp - Convert %malloc & %free calls to insts ------===//
-// 
+//
 //                     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 defines the RaiseAllocations pass which convert malloc and free
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/IPO.h"
-#include "llvm/Module.h"
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/iMemory.h"
-#include "llvm/iTerminators.h"
-#include "llvm/iOther.h"
+#include "llvm/Module.h"
+#include "llvm/Instructions.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/CallSite.h"
-#include "Support/Statistic.h"
-
-namespace llvm {
+#include "llvm/ADT/Statistic.h"
+using namespace llvm;
 
 namespace {
   Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
@@ -30,29 +28,29 @@ namespace {
   // RaiseAllocations - Turn %malloc and %free calls into the appropriate
   // instruction.
   //
-  class RaiseAllocations : public Pass {
+  class RaiseAllocations : public ModulePass {
     Function *MallocFunc;   // Functions in the module we are processing
     Function *FreeFunc;     // Initialized by doPassInitializationVirt
   public:
     RaiseAllocations() : MallocFunc(0), FreeFunc(0) {}
-    
+
     // doPassInitialization - For the raise allocations pass, this finds a
     // declaration for malloc and free if they exist.
     //
     void doInitialization(Module &M);
-    
+
     // run - This method does the actual work of converting instructions over.
     //
-    bool run(Module &M);
+    bool runOnModule(Module &M);
   };
-  
+
   RegisterOpt<RaiseAllocations>
   X("raiseallocs", "Raise allocations from calls to instructions");
 }  // end anonymous namespace
 
 
 // createRaiseAllocationsPass - The interface to this file...
-Pass *createRaiseAllocationsPass() {
+ModulePass *llvm::createRaiseAllocationsPass() {
   return new RaiseAllocations();
 }
 
@@ -116,7 +114,7 @@ void RaiseAllocations::doInitialization(Module &M) {
 
 // run - Transform calls into instructions...
 //
-bool RaiseAllocations::run(Module &M) {
+bool RaiseAllocations::runOnModule(Module &M) {
   // Find the malloc/free prototypes...
   doInitialization(M);
 
@@ -125,18 +123,25 @@ bool RaiseAllocations::run(Module &M) {
   // First, process all of the malloc calls...
   if (MallocFunc) {
     std::vector<User*> Users(MallocFunc->use_begin(), MallocFunc->use_end());
+    std::vector<Value*> EqPointers;   // Values equal to MallocFunc
     while (!Users.empty()) {
-      if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
+      User *U = Users.back();
+      Users.pop_back();
+
+      if (Instruction *I = dyn_cast<Instruction>(U)) {
         CallSite CS = CallSite::get(I);
-        if (CS.getInstruction() && CS.getCalledFunction() == MallocFunc &&
-            CS.arg_begin() != CS.arg_end()) {
+        if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
+            (CS.getCalledFunction() == MallocFunc ||
+             std::find(EqPointers.begin(), EqPointers.end(),
+                       CS.getCalledValue()) != EqPointers.end())) {
+
           Value *Source = *CS.arg_begin();
-          
+
           // If no prototype was provided for malloc, we may need to cast the
           // source size.
           if (Source->getType() != Type::UIntTy)
             Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I);
-          
+
           std::string Name(I->getName()); I->setName("");
           MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I);
           I->replaceAllUsesWith(MI);
@@ -151,25 +156,37 @@ bool RaiseAllocations::run(Module &M) {
           Changed = true;
           ++NumRaised;
         }
+      } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
+        Users.insert(Users.end(), GV->use_begin(), GV->use_end());
+        EqPointers.push_back(GV);
+      } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
+        if (CE->getOpcode() == Instruction::Cast) {
+          Users.insert(Users.end(), CE->use_begin(), CE->use_end());
+          EqPointers.push_back(CE);
+        }
       }
-
-      Users.pop_back();
     }
   }
 
   // Next, process all free calls...
   if (FreeFunc) {
     std::vector<User*> Users(FreeFunc->use_begin(), FreeFunc->use_end());
+    std::vector<Value*> EqPointers;   // Values equal to FreeFunc
 
     while (!Users.empty()) {
-      if (Instruction *I = dyn_cast<Instruction>(Users.back())) {
+      User *U = Users.back();
+      Users.pop_back();
+
+      if (Instruction *I = dyn_cast<Instruction>(U)) {
         CallSite CS = CallSite::get(I);
-        if (CS.getInstruction() && CS.getCalledFunction() == FreeFunc &&
-            CS.arg_begin() != CS.arg_end()) {
-          
+        if (CS.getInstruction() && CS.arg_begin() != CS.arg_end() &&
+            (CS.getCalledFunction() == FreeFunc ||
+             std::find(EqPointers.begin(), EqPointers.end(),
+                       CS.getCalledValue()) != EqPointers.end())) {
+
           // If no prototype was provided for free, we may need to cast the
           // source pointer.  This should be really uncommon, but it's necessary
-          // just in case we are dealing with wierd code like this:
+          // just in case we are dealing with weird code like this:
           //   free((long)ptr);
           //
           Value *Source = *CS.arg_begin();
@@ -184,17 +201,23 @@ bool RaiseAllocations::run(Module &M) {
             new BranchInst(II->getNormalDest(), I);
 
           // Delete the old call site
-          I->getParent()->getInstList().erase(I);
+          if (I->getType() != Type::VoidTy)
+            I->replaceAllUsesWith(UndefValue::get(I->getType()));
+          I->eraseFromParent();
           Changed = true;
           ++NumRaised;
         }
+      } else if (GlobalValue *GV = dyn_cast<GlobalValue>(U)) {
+        Users.insert(Users.end(), GV->use_begin(), GV->use_end());
+        EqPointers.push_back(GV);
+      } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
+        if (CE->getOpcode() == Instruction::Cast) {
+          Users.insert(Users.end(), CE->use_begin(), CE->use_end());
+          EqPointers.push_back(CE);
+        }
       }
-      
-      Users.pop_back();
     }
   }
 
   return Changed;
 }
-
-} // End llvm namespace