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