By min, I mean max.
[oota-llvm.git] / lib / Analysis / EscapeAnalysis.cpp
index 69dde4d7fdfcf125666c027cebe5a74599a80d99..67cc6009e39767d416ea66c345c6d0fb5f38d918 100644 (file)
 
 #define DEBUG_TYPE "escape-analysis"
 #include "llvm/Analysis/EscapeAnalysis.h"
+#include "llvm/Constants.h"
 #include "llvm/Module.h"
 #include "llvm/Support/InstIterator.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include <vector>
 using namespace llvm;
 
 char EscapeAnalysis::ID = 0;
@@ -49,7 +51,7 @@ bool EscapeAnalysis::runOnFunction(Function& F) {
       for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
            AI != AE; ++AI) {
         if (!isa<PointerType>(AI->getType())) continue;
-        AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, AI, ~0UL);
+        AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, AI, ~0U);
         if (R != AliasAnalysis::NoAlias) {
           EscapePoints.insert(S);
           inserted = true;
@@ -62,7 +64,7 @@ bool EscapeAnalysis::runOnFunction(Function& F) {
       
       for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
            GI != GE; ++GI) {
-        AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, GI, ~0UL);
+        AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, GI, ~0U);
         if (R != AliasAnalysis::NoAlias) {
           EscapePoints.insert(S);
           break;
@@ -98,18 +100,35 @@ bool EscapeAnalysis::runOnFunction(Function& F) {
 /// escape point.
 /// FIXME: Once we've discovered a path, it would be a good idea to memoize it,
 /// and all of its subpaths, to amortize the cost of future queries.
-bool EscapeAnalysis::escapes(AllocationInst* A) {
-  std::vector<Instruction*> worklist;
+bool EscapeAnalysis::escapes(Value* A) {
+  assert(isa<PointerType>(A->getType()) && 
+         "Can't do escape analysis on non-pointer types!");
+  
+  std::vector<Value*> worklist;
   worklist.push_back(A);
   
-  SmallPtrSet<Instruction*, 8> visited;
+  SmallPtrSet<Value*, 8> visited;
   visited.insert(A);
   while (!worklist.empty()) {
-    Instruction* curr = worklist.back();
+    Value* curr = worklist.back();
     worklist.pop_back();
     
-    if (EscapePoints.count(curr))
-      return true;
+    if (Instruction* I = dyn_cast<Instruction>(curr))
+      if (EscapePoints.count(I)) {
+        BranchInst* B = dyn_cast<BranchInst>(I);
+        if (!B) return true;
+        Value* condition = B->getCondition();
+        ICmpInst* C = dyn_cast<ICmpInst>(condition);
+        if (!C) return true;
+        Value* O1 = C->getOperand(0);
+        Value* O2 = C->getOperand(1);
+        if (isa<MallocInst>(O1->stripPointerCasts())) {
+          if (!isa<ConstantPointerNull>(O2)) return true;
+        } else if(isa<MallocInst>(O2->stripPointerCasts())) {
+          if (!isa<ConstantPointerNull>(O1)) return true;
+        } else
+          return true;
+      }
     
     if (StoreInst* S = dyn_cast<StoreInst>(curr)) {
       // We know this must be an instruction, because constant gep's would
@@ -127,4 +146,4 @@ bool EscapeAnalysis::escapes(AllocationInst* A) {
   }
   
   return false;
-}
\ No newline at end of file
+}