Implement RaiseAllocations/FreeCastConstantExpr.ll
authorChris Lattner <sabre@nondot.org>
Sun, 7 Dec 2003 01:42:08 +0000 (01:42 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 7 Dec 2003 01:42:08 +0000 (01:42 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10305 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/IPO/RaiseAllocations.cpp

index dea919773e5b108de533e980924d8a2921d441a4..5302d8f7a1abc18e0162fe0cd23ac93461c4c408 100644 (file)
@@ -13,8 +13,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/IPO.h"
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/IPO.h"
-#include "llvm/Module.h"
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/DerivedTypes.h"
+#include "llvm/Module.h"
 #include "llvm/iMemory.h"
 #include "llvm/iTerminators.h"
 #include "llvm/iOther.h"
 #include "llvm/iMemory.h"
 #include "llvm/iTerminators.h"
 #include "llvm/iOther.h"
@@ -124,11 +125,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());
   // 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()) {
     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);
         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
           Value *Source = *CS.arg_begin();
           
           // If no prototype was provided for malloc, we may need to cast the
@@ -150,21 +158,33 @@ bool RaiseAllocations::run(Module &M) {
           Changed = true;
           ++NumRaised;
         }
           Changed = true;
           ++NumRaised;
         }
+      } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(U)) {
+        Users.insert(Users.end(), CPR->use_begin(), CPR->use_end());
+        EqPointers.push_back(CPR);
+      } 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());
     }
   }
 
   // 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()) {
 
     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);
         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
           
           // If no prototype was provided for free, we may need to cast the
           // source pointer.  This should be really uncommon, but it's necessary
@@ -187,12 +207,17 @@ bool RaiseAllocations::run(Module &M) {
           Changed = true;
           ++NumRaised;
         }
           Changed = true;
           ++NumRaised;
         }
+      } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(U)) {
+        Users.insert(Users.end(), CPR->use_begin(), CPR->use_end());
+        EqPointers.push_back(CPR);
+      } 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;
 }
     }
   }
 
   return Changed;
 }
-