Changes For Bug 352
[oota-llvm.git] / lib / Transforms / IPO / RaiseAllocations.cpp
index d214d2edfe691f7e8e874706beaa004b12a7245a..42edb7e05de31fdf3d4ca4bad9e448afe4e0859d 100644 (file)
@@ -1,4 +1,11 @@
 //===- 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
 // calls to malloc and free instructions.
 //===----------------------------------------------------------------------===//
 
 #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"
+#include "llvm/ADT/Statistic.h"
+using namespace llvm;
 
 namespace {
   Statistic<> NumRaised("raiseallocs", "Number of allocations raised");
@@ -43,7 +50,7 @@ namespace {
 
 
 // createRaiseAllocationsPass - The interface to this file...
-Pass *createRaiseAllocationsPass() {
+Pass *llvm::createRaiseAllocationsPass() {
   return new RaiseAllocations();
 }
 
@@ -116,11 +123,18 @@ 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
@@ -142,21 +156,33 @@ 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
@@ -179,9 +205,15 @@ 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();
     }
   }