refactor FoldBitCast to reduce nesting and to always return a constantexpr
[oota-llvm.git] / lib / Analysis / MemoryDependenceAnalysis.cpp
index d5db2ed651ee738b733d75338bef10b8c57ec7ee..ce7674003fe013bb2f4b02fe7bd212eb2be53db0 100644 (file)
@@ -118,6 +118,10 @@ getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
       
       // FreeInsts erase the entire structure
       PointerSize = ~0ULL;
+    } else if (isFreeCall(Inst)) {
+      Pointer = Inst->getOperand(0);
+      // calls to free() erase the entire structure
+      PointerSize = ~0ULL;
     } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
       // Debug intrinsics don't cause dependences.
       if (isa<DbgInfoIntrinsic>(Inst)) continue;
@@ -225,16 +229,11 @@ getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad,
     // the allocation, return Def.  This means that there is no dependence and
     // the access can be optimized based on that.  For example, a load could
     // turn into undef.
-    if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
-      Value *AccessPtr = MemPtr->getUnderlyingObject();
-      
-      if (AccessPtr == AI ||
-          AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
-        return MemDepResult::getDef(AI);
-      continue;
-    }
-    
-    if (isMalloc(Inst)) {
+    // Note: Only determine this to be a malloc if Inst is the malloc call, not
+    // a subsequent bitcast of the malloc call result.  There can be stores to
+    // the malloced memory between the malloc call and its bitcast uses, and we
+    // need to continue scanning until the malloc call.
+    if (isa<AllocaInst>(Inst) || extractMallocCall(Inst)) {
       Value *AccessPtr = MemPtr->getUnderlyingObject();
       
       if (AccessPtr == Inst ||
@@ -319,6 +318,10 @@ MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
       MemPtr = LI->getPointerOperand();
       MemSize = AA->getTypeStoreSize(LI->getType());
     }
+  } else if (isFreeCall(QueryInst)) {
+    MemPtr = QueryInst->getOperand(0);
+    // calls to free() erase the entire structure, not just a field.
+    MemSize = ~0UL;
   } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
     CallSite QueryCS = CallSite::get(QueryInst);
     bool isReadOnly = AA->onlyReadsMemory(QueryCS);
@@ -521,13 +524,6 @@ getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB,
   const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType();
   uint64_t PointeeSize = AA->getTypeStoreSize(EltTy);
   
-  // If Pointer is a bitcast instruction, chomp through to the pointee since
-  // they are must alias.  This increases the effectiveness of caching by
-  // finding more equivalences, avoids having to phi translate the bitcast, and
-  // avoids conflicts where we are looking for two "different" values in the
-  // same block when they are really just must aliases.
-  Pointer = Pointer->stripPointerCasts();
-  
   // This is the set of blocks we've inspected, and the pointer we consider in
   // each block.  Because of critical edges, we currently bail out if querying
   // a block with multiple different pointers.  This can happen during PHI
@@ -667,6 +663,7 @@ getNonLocalPointerDepFromBB(Value *Pointer, uint64_t PointeeSize,
                             SmallVectorImpl<NonLocalDepEntry> &Result,
                             DenseMap<BasicBlock*, Value*> &Visited,
                             bool SkipFirstBlock) {
+  
   // Look up the cached info for Pointer.
   ValueIsLoadPair CacheKey(Pointer, isLoad);
   
@@ -799,13 +796,6 @@ getNonLocalPointerDepFromBB(Value *Pointer, uint64_t PointeeSize,
         BasicBlock *Pred = *PI;
         Value *PredPtr = PtrPHI->getIncomingValueForBlock(Pred);
         
-        // If Pointer is a bitcast instruction, chomp through to the pointee since
-        // they are must alias.  This increases the effectiveness of caching by
-        // finding more equivalences, avoids having to phi translate the bitcast, and
-        // avoids conflicts where we are looking for two "different" values in the
-        // same block when they are really just must aliases.
-        PredPtr = PredPtr->stripPointerCasts();
-        
         // Check to see if we have already visited this pred block with another
         // pointer.  If so, we can't do this lookup.  This failure can occur
         // with PHI translation when a critical edge exists and the PHI node in