Loads.cpp revision c869b4397aa7c45bfcd71951a1f15e6e7228be79
1dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman//===- Loads.cpp - Local load analysis ------------------------------------===// 2dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// 3dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// The LLVM Compiler Infrastructure 4dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// 5dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// This file is distributed under the University of Illinois Open Source 6dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// License. See LICENSE.TXT for details. 7dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// 8dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman//===----------------------------------------------------------------------===// 9dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// 10dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// This file defines simple local analyses for load instructions. 11dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman// 12dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman//===----------------------------------------------------------------------===// 13dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 14dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman#include "llvm/Analysis/Loads.h" 15dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman#include "llvm/Analysis/AliasAnalysis.h" 16dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman#include "llvm/Target/TargetData.h" 17dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman#include "llvm/GlobalAlias.h" 18dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman#include "llvm/GlobalVariable.h" 19dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman#include "llvm/IntrinsicInst.h" 20562b84b3aea359d1f918184e355da82bf05eb290Jay Foad#include "llvm/Operator.h" 21dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohmanusing namespace llvm; 22dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 23dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// AreEquivalentAddressValues - Test if A and B will obviously have the same 24dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// value. This includes recognizing that %t0 and %t1 will have the same 25dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// value in code like this: 26dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// %t0 = getelementptr \@a, 0, 3 27dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// store i32 0, i32* %t0 28dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// %t1 = getelementptr \@a, 0, 3 29dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// %t2 = load i32* %t1 30dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// 31dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohmanstatic bool AreEquivalentAddressValues(const Value *A, const Value *B) { 32dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Test if the values are trivially equivalent. 33dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (A == B) return true; 3468df608fd1eeb7f7fb6f7de49ff8cd6382654f79Hans Wennborg 35dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Test if the values come from identical arithmetic instructions. 36dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Use isIdenticalToWhenDefined instead of isIdenticalTo because 37dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // this function is only used when one address use dominates the 38dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // other, which means that they'll always either have the same 39dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // value or one of them will have an undefined value. 40dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (isa<BinaryOperator>(A) || isa<CastInst>(A) || 41dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman isa<PHINode>(A) || isa<GetElementPtrInst>(A)) 42dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (const Instruction *BI = dyn_cast<Instruction>(B)) 43dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) 44dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return true; 4568df608fd1eeb7f7fb6f7de49ff8cd6382654f79Hans Wennborg 46dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Otherwise they may not be equivalent. 47dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return false; 48dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman} 49dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 50dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// getUnderlyingObjectWithOffset - Strip off up to MaxLookup GEPs and 51dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// bitcasts to get back to the underlying object being addressed, keeping 52dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// track of the offset in bytes from the GEPs relative to the result. 535034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman/// This is closely related to GetUnderlyingObject but is located 54dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// here to avoid making VMCore depend on TargetData. 55dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohmanstatic Value *getUnderlyingObjectWithOffset(Value *V, const TargetData *TD, 56dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman uint64_t &ByteOffset, 57dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman unsigned MaxLookup = 6) { 58dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (!V->getType()->isPointerTy()) 59dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return V; 60dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { 61dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 62dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (!GEP->hasAllConstantIndices()) 63dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return V; 64dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman SmallVector<Value*, 8> Indices(GEP->op_begin() + 1, GEP->op_end()); 65dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman ByteOffset += TD->getIndexedOffset(GEP->getPointerOperandType(), 668fbbb3980755d74539a0aed02bc18842ed2bd18dJay Foad Indices); 67dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman V = GEP->getPointerOperand(); 68dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } else if (Operator::getOpcode(V) == Instruction::BitCast) { 69dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman V = cast<Operator>(V)->getOperand(0); 70dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 71dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (GA->mayBeOverridden()) 72dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return V; 73dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman V = GA->getAliasee(); 74dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } else { 75dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return V; 76dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 77dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 78dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 79dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return V; 80dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman} 81dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 82dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// isSafeToLoadUnconditionally - Return true if we know that executing a load 83dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// from this value cannot trap. If it is not obviously safe to load from the 84dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// specified pointer, we do a quick local scan of the basic block containing 85dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// ScanFrom, to determine if the address is already accessed. 86dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohmanbool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom, 87dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman unsigned Align, const TargetData *TD) { 88dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman uint64_t ByteOffset = 0; 89dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman Value *Base = V; 90dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (TD) 91dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman Base = getUnderlyingObjectWithOffset(V, TD, ByteOffset); 92dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 93db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *BaseType = 0; 94dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman unsigned BaseAlign = 0; 95dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { 96dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // An alloca is safe to load from as load as it is suitably aligned. 97dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman BaseType = AI->getAllocatedType(); 98dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman BaseAlign = AI->getAlignment(); 99dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(Base)) { 100dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Global variables are safe to load from but their size cannot be 101dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // guaranteed if they are overridden. 102dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (!isa<GlobalAlias>(GV) && !GV->mayBeOverridden()) { 103dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman BaseType = GV->getType()->getElementType(); 104dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman BaseAlign = GV->getAlignment(); 105dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 106dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 107dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 108dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (BaseType && BaseType->isSized()) { 109dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (TD && BaseAlign == 0) 110dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman BaseAlign = TD->getPrefTypeAlignment(BaseType); 111dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 112dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (Align <= BaseAlign) { 113dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (!TD) 114dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return true; // Loading directly from an alloca or global is OK. 115dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 116dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Check if the load is within the bounds of the underlying object. 117db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner PointerType *AddrTy = cast<PointerType>(V->getType()); 118dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman uint64_t LoadSize = TD->getTypeStoreSize(AddrTy->getElementType()); 119dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (ByteOffset + LoadSize <= TD->getTypeAllocSize(BaseType) && 120dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman (Align == 0 || (ByteOffset % Align) == 0)) 121dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return true; 122dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 123dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 124dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 125dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Otherwise, be a little bit aggressive by scanning the local block where we 126dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // want to check to see if the pointer is already being loaded or stored 127dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // from/to. If so, the previous load or store would have already trapped, 128dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // so there is no harm doing an extra load (also, CSE will later eliminate 129dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // the load entirely). 130dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); 131dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 132dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman while (BBI != E) { 133dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman --BBI; 134dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 135dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // If we see a free or a call which may write to memory (i.e. which might do 136dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // a free) the pointer could be marked invalid. 137dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() && 138dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman !isa<DbgInfoIntrinsic>(BBI)) 139dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return false; 140dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 141dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { 142dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (AreEquivalentAddressValues(LI->getOperand(0), V)) return true; 143dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { 144dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (AreEquivalentAddressValues(SI->getOperand(1), V)) return true; 145dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 146dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 147dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return false; 148dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman} 149dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 150dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the 151dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// instruction before ScanFrom) checking to see if we have the value at the 152dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// memory address *Ptr locally available within a small number of instructions. 153dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// If the value is available, return it. 154dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// 155dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// If not, return the iterator for the last validated instruction that the 156dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// value would be live through. If we scanned the entire block and didn't find 157dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// something that invalidates *Ptr or provides it, ScanFrom would be left at 158dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// begin() and this returns null. ScanFrom could also be left 159dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// 160dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// MaxInstsToScan specifies the maximum instructions to scan in the block. If 161dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// it is set to 0, it will scan the whole block. You can also optionally 162dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman/// specify an alias analysis implementation, which makes this more precise. 163dd9344f3face8f1978a7f9f393c31b628144d1f6Dan GohmanValue *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, 164dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman BasicBlock::iterator &ScanFrom, 165dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman unsigned MaxInstsToScan, 166dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman AliasAnalysis *AA) { 167dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (MaxInstsToScan == 0) MaxInstsToScan = ~0U; 168dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 169dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // If we're using alias analysis to disambiguate get the size of *Ptr. 1703da848bbda62b25c12335998aaa44ab361f0bf15Dan Gohman uint64_t AccessSize = 0; 171dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (AA) { 172db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType(); 173dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman AccessSize = AA->getTypeStoreSize(AccessTy); 174dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 175dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 176dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman while (ScanFrom != ScanBB->begin()) { 177dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // We must ignore debug info directives when counting (otherwise they 178dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // would affect codegen). 179dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman Instruction *Inst = --ScanFrom; 180dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (isa<DbgInfoIntrinsic>(Inst)) 181dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman continue; 182dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 183dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Restore ScanFrom to expected value in case next test succeeds 184dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman ScanFrom++; 185dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 186dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Don't scan huge blocks. 187dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (MaxInstsToScan-- == 0) return 0; 188dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 189dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman --ScanFrom; 190dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // If this is a load of Ptr, the loaded value is available. 191c869b4397aa7c45bfcd71951a1f15e6e7228be79Eli Friedman // (This is true even if the load is volatile or atomic, although 192c869b4397aa7c45bfcd71951a1f15e6e7228be79Eli Friedman // those cases are unlikely.) 193dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) 194dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) 195dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return LI; 196dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 197dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 198dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // If this is a store through Ptr, the value is available! 199c869b4397aa7c45bfcd71951a1f15e6e7228be79Eli Friedman // (This is true even if the store is volatile or atomic, although 200c869b4397aa7c45bfcd71951a1f15e6e7228be79Eli Friedman // those cases are unlikely.) 201dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) 202dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return SI->getOperand(0); 203dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 204dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // If Ptr is an alloca and this is a store to a different alloca, ignore 205dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // the store. This is a trivial form of alias analysis that is important 206dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // for reg2mem'd code. 207dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) && 208dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman (isa<AllocaInst>(SI->getOperand(1)) || 209dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman isa<GlobalVariable>(SI->getOperand(1)))) 210dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman continue; 211dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 212dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // If we have alias analysis and it says the store won't modify the loaded 213dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // value, ignore the store. 214dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (AA && 215dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) 216dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman continue; 217dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 218dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Otherwise the store that may or may not alias the pointer, bail out. 219dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman ++ScanFrom; 220dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return 0; 221dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 222dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 223dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // If this is some other instruction that may clobber Ptr, bail out. 224dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (Inst->mayWriteToMemory()) { 225dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // If alias analysis claims that it really won't modify the load, 226dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // ignore it. 227dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman if (AA && 228dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) 229dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman continue; 230dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 231dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // May modify the pointer, bail out. 232dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman ++ScanFrom; 233dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return 0; 234dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 235dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman } 236dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman 237dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // Got to the start of the block, we didn't find it, but are done for this 238dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman // block. 239dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman return 0; 240dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman} 241