Remove FreeInst.
Remove LowerAllocations pass.
Update some more passes to treate free calls just like they were treating FreeInst.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85176 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/AliasSetTracker.cpp b/lib/Analysis/AliasSetTracker.cpp
index 081f478..5b1b7d1 100644
--- a/lib/Analysis/AliasSetTracker.cpp
+++ b/lib/Analysis/AliasSetTracker.cpp
@@ -13,6 +13,7 @@
 
 #include "llvm/Analysis/AliasSetTracker.h"
 #include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Analysis/MallocHelper.h"
 #include "llvm/Instructions.h"
 #include "llvm/IntrinsicInst.h"
 #include "llvm/Pass.h"
@@ -296,12 +297,6 @@
   return NewPtr;
 }
 
-bool AliasSetTracker::add(FreeInst *FI) {
-  bool NewPtr;
-  addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
-  return NewPtr;
-}
-
 bool AliasSetTracker::add(VAArgInst *VAAI) {
   bool NewPtr;
   addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
@@ -310,6 +305,13 @@
 
 
 bool AliasSetTracker::add(CallSite CS) {
+  Instruction* Inst = CS.getInstruction();
+  if (isFreeCall(Inst)) {
+    bool NewPtr;
+    addPointer(Inst->getOperand(1), ~0, AliasSet::Mods, NewPtr);
+    return NewPtr;
+  }
+  
   if (isa<DbgInfoIntrinsic>(CS.getInstruction())) 
     return true; // Ignore DbgInfo Intrinsics.
   if (AA.doesNotAccessMemory(CS))
@@ -337,8 +339,6 @@
     return add(CI);
   else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
     return add(II);
-  else if (FreeInst *FI = dyn_cast<FreeInst>(I))
-    return add(FI);
   else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
     return add(VAAI);
   return true;
@@ -427,13 +427,6 @@
   return true;
 }
 
-bool AliasSetTracker::remove(FreeInst *FI) {
-  AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
-  if (!AS) return false;
-  remove(*AS);
-  return true;
-}
-
 bool AliasSetTracker::remove(VAArgInst *VAAI) {
   AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
   if (!AS) return false;
@@ -442,6 +435,14 @@
 }
 
 bool AliasSetTracker::remove(CallSite CS) {
+  Instruction* Inst = CS.getInstruction();
+  if (isFreeCall(Inst)) {
+    AliasSet *AS = findAliasSetForPointer(Inst->getOperand(1), ~0);
+    if (!AS) return false;
+    remove(*AS);
+    return true;
+  }
+
   if (AA.doesNotAccessMemory(CS))
     return false; // doesn't alias anything
 
@@ -459,8 +460,6 @@
     return remove(SI);
   else if (CallInst *CI = dyn_cast<CallInst>(I))
     return remove(CI);
-  else if (FreeInst *FI = dyn_cast<FreeInst>(I))
-    return remove(FI);
   else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
     return remove(VAAI);
   return true;
diff --git a/lib/Analysis/CaptureTracking.cpp b/lib/Analysis/CaptureTracking.cpp
index b30ac71..649b760 100644
--- a/lib/Analysis/CaptureTracking.cpp
+++ b/lib/Analysis/CaptureTracking.cpp
@@ -17,6 +17,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/CaptureTracking.h"
+#include "llvm/Analysis/MallocHelper.h"
 #include "llvm/Instructions.h"
 #include "llvm/Value.h"
 #include "llvm/ADT/SmallSet.h"
@@ -48,6 +49,9 @@
 
     switch (I->getOpcode()) {
     case Instruction::Call:
+      if (isFreeCall(I))
+        // Freeing a pointer does not cause it to be captured.
+        break;
     case Instruction::Invoke: {
       CallSite CS = CallSite::get(I);
       // Not captured if the callee is readonly, doesn't return a copy through
@@ -73,9 +77,6 @@
       // captured.
       break;
     }
-    case Instruction::Free:
-      // Freeing a pointer does not cause it to be captured.
-      break;
     case Instruction::Load:
       // Loading from a pointer does not cause it to be captured.
       break;
diff --git a/lib/Analysis/IPA/Andersens.cpp b/lib/Analysis/IPA/Andersens.cpp
index 6372342..78ec06f 100644
--- a/lib/Analysis/IPA/Andersens.cpp
+++ b/lib/Analysis/IPA/Andersens.cpp
@@ -1016,7 +1016,7 @@
       }
     } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
       if (AnalyzeUsesOfFunction(GEP)) return true;
-    } else if (isa<FreeInst>(*UI) || isFreeCall(*UI)) {
+    } else if (isFreeCall(*UI)) {
       return false;
     } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
       // Make sure that this is just the function being called, not that it is
@@ -1156,7 +1156,6 @@
   case Instruction::Switch:
   case Instruction::Unwind:
   case Instruction::Unreachable:
-  case Instruction::Free:
   case Instruction::ICmp:
   case Instruction::FCmp:
     return;
diff --git a/lib/Analysis/IPA/GlobalsModRef.cpp b/lib/Analysis/IPA/GlobalsModRef.cpp
index 01090f1..011b0ef 100644
--- a/lib/Analysis/IPA/GlobalsModRef.cpp
+++ b/lib/Analysis/IPA/GlobalsModRef.cpp
@@ -238,7 +238,7 @@
     } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) {
       if (AnalyzeUsesOfPointer(BCI, Readers, Writers, OkayStoreDest))
         return true;
-    } else if (isa<FreeInst>(*UI) || isFreeCall(*UI)) {
+    } else if (isFreeCall(*UI)) {
       Writers.push_back(cast<Instruction>(*UI)->getParent()->getParent());
     } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
       // Make sure that this is just the function being called, not that it is
@@ -437,7 +437,7 @@
           if (cast<StoreInst>(*II).isVolatile())
             // Treat volatile stores as reading memory somewhere.
             FunctionEffect |= Ref;
-        } else if (isMalloc(&cast<Instruction>(*II)) || isa<FreeInst>(*II) ||
+        } else if (isMalloc(&cast<Instruction>(*II)) ||
                    isFreeCall(&cast<Instruction>(*II))) {
           FunctionEffect |= ModRef;
         }
diff --git a/lib/Analysis/InlineCost.cpp b/lib/Analysis/InlineCost.cpp
index 4f010b6..febdd91 100644
--- a/lib/Analysis/InlineCost.cpp
+++ b/lib/Analysis/InlineCost.cpp
@@ -130,10 +130,6 @@
         NumInsts += InlineConstants::CallPenalty;
     }
     
-    // These, too, are calls.
-    if (isa<FreeInst>(II))
-      NumInsts += InlineConstants::CallPenalty;
-
     if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
       if (!AI->isStaticAlloca())
         this->usesDynamicAlloca = true;
diff --git a/lib/Analysis/InstCount.cpp b/lib/Analysis/InstCount.cpp
index c4f3b68..a4b041f 100644
--- a/lib/Analysis/InstCount.cpp
+++ b/lib/Analysis/InstCount.cpp
@@ -74,11 +74,11 @@
 bool InstCount::runOnFunction(Function &F) {
   unsigned StartMemInsts =
     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
-    NumInvokeInst + NumAllocaInst + NumFreeInst;
+    NumInvokeInst + NumAllocaInst;
   visit(F);
   unsigned EndMemInsts =
     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
-    NumInvokeInst + NumAllocaInst + NumFreeInst;
+    NumInvokeInst + NumAllocaInst;
   TotalMemInst += EndMemInsts-StartMemInsts;
   return false;
 }
diff --git a/lib/Analysis/MallocHelper.cpp b/lib/Analysis/MallocHelper.cpp
index 511de9d..70afa88 100644
--- a/lib/Analysis/MallocHelper.cpp
+++ b/lib/Analysis/MallocHelper.cpp
@@ -1,4 +1,4 @@
-//===-- MallocHelper.cpp - Functions to identify malloc calls -------------===//
+//===-- MallocFreeHelper.cpp - Identify calls to malloc and free builtins -===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -8,7 +8,8 @@
 //===----------------------------------------------------------------------===//
 //
 // This family of functions identifies calls to malloc, bitcasts of malloc
-// calls, and the types and array sizes associated with them.
+// calls, and the types and array sizes associated with them.  It also
+// identifies calls to the free builtin.
 //
 //===----------------------------------------------------------------------===//
 
@@ -264,6 +265,10 @@
   return BO->getOperand(0);
 }
 
+//===----------------------------------------------------------------------===//
+//  free Call Utility Functions.
+//
+
 /// isFreeCall - Returns true if the the value is a call to the builtin free()
 bool llvm::isFreeCall(const Value* I) {
   const CallInst *CI = dyn_cast<CallInst>(I);
diff --git a/lib/Analysis/MemoryDependenceAnalysis.cpp b/lib/Analysis/MemoryDependenceAnalysis.cpp
index ce76740..7b3fe64 100644
--- a/lib/Analysis/MemoryDependenceAnalysis.cpp
+++ b/lib/Analysis/MemoryDependenceAnalysis.cpp
@@ -113,10 +113,9 @@
     } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
       Pointer = V->getOperand(0);
       PointerSize = AA->getTypeStoreSize(V->getType());
-    } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
-      Pointer = F->getPointerOperand();
-      
-      // FreeInsts erase the entire structure
+    } else if (isFreeCall(Inst)) {
+      Pointer = Inst->getOperand(1);
+      // calls to free() erase the entire structure
       PointerSize = ~0ULL;
     } else if (isFreeCall(Inst)) {
       Pointer = Inst->getOperand(0);
@@ -319,7 +318,7 @@
       MemSize = AA->getTypeStoreSize(LI->getType());
     }
   } else if (isFreeCall(QueryInst)) {
-    MemPtr = QueryInst->getOperand(0);
+    MemPtr = QueryInst->getOperand(1);
     // calls to free() erase the entire structure, not just a field.
     MemSize = ~0UL;
   } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
@@ -327,10 +326,6 @@
     bool isReadOnly = AA->onlyReadsMemory(QueryCS);
     LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
                                            QueryParent);
-  } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) {
-    MemPtr = FI->getPointerOperand();
-    // FreeInsts erase the entire structure, not just a field.
-    MemSize = ~0UL;
   } else {
     // Non-memory instruction.
     LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));