LLVM API Documentation
00001 //===- Loads.cpp - Local load analysis ------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines simple local analyses for load instructions. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Analysis/Loads.h" 00015 #include "llvm/Analysis/AliasAnalysis.h" 00016 #include "llvm/Analysis/ValueTracking.h" 00017 #include "llvm/IR/DataLayout.h" 00018 #include "llvm/IR/GlobalAlias.h" 00019 #include "llvm/IR/GlobalVariable.h" 00020 #include "llvm/IR/IntrinsicInst.h" 00021 #include "llvm/IR/LLVMContext.h" 00022 #include "llvm/IR/Operator.h" 00023 using namespace llvm; 00024 00025 /// AreEquivalentAddressValues - Test if A and B will obviously have the same 00026 /// value. This includes recognizing that %t0 and %t1 will have the same 00027 /// value in code like this: 00028 /// %t0 = getelementptr \@a, 0, 3 00029 /// store i32 0, i32* %t0 00030 /// %t1 = getelementptr \@a, 0, 3 00031 /// %t2 = load i32* %t1 00032 /// 00033 static bool AreEquivalentAddressValues(const Value *A, const Value *B) { 00034 // Test if the values are trivially equivalent. 00035 if (A == B) return true; 00036 00037 // Test if the values come from identical arithmetic instructions. 00038 // Use isIdenticalToWhenDefined instead of isIdenticalTo because 00039 // this function is only used when one address use dominates the 00040 // other, which means that they'll always either have the same 00041 // value or one of them will have an undefined value. 00042 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || 00043 isa<PHINode>(A) || isa<GetElementPtrInst>(A)) 00044 if (const Instruction *BI = dyn_cast<Instruction>(B)) 00045 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) 00046 return true; 00047 00048 // Otherwise they may not be equivalent. 00049 return false; 00050 } 00051 00052 /// isSafeToLoadUnconditionally - Return true if we know that executing a load 00053 /// from this value cannot trap. If it is not obviously safe to load from the 00054 /// specified pointer, we do a quick local scan of the basic block containing 00055 /// ScanFrom, to determine if the address is already accessed. 00056 bool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom, 00057 unsigned Align, const DataLayout *TD) { 00058 int64_t ByteOffset = 0; 00059 Value *Base = V; 00060 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, TD); 00061 00062 if (ByteOffset < 0) // out of bounds 00063 return false; 00064 00065 Type *BaseType = nullptr; 00066 unsigned BaseAlign = 0; 00067 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) { 00068 // An alloca is safe to load from as load as it is suitably aligned. 00069 BaseType = AI->getAllocatedType(); 00070 BaseAlign = AI->getAlignment(); 00071 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { 00072 // Global variables are safe to load from but their size cannot be 00073 // guaranteed if they are overridden. 00074 if (!GV->mayBeOverridden()) { 00075 BaseType = GV->getType()->getElementType(); 00076 BaseAlign = GV->getAlignment(); 00077 } 00078 } 00079 00080 if (BaseType && BaseType->isSized()) { 00081 if (TD && BaseAlign == 0) 00082 BaseAlign = TD->getPrefTypeAlignment(BaseType); 00083 00084 if (Align <= BaseAlign) { 00085 if (!TD) 00086 return true; // Loading directly from an alloca or global is OK. 00087 00088 // Check if the load is within the bounds of the underlying object. 00089 PointerType *AddrTy = cast<PointerType>(V->getType()); 00090 uint64_t LoadSize = TD->getTypeStoreSize(AddrTy->getElementType()); 00091 if (ByteOffset + LoadSize <= TD->getTypeAllocSize(BaseType) && 00092 (Align == 0 || (ByteOffset % Align) == 0)) 00093 return true; 00094 } 00095 } 00096 00097 // Otherwise, be a little bit aggressive by scanning the local block where we 00098 // want to check to see if the pointer is already being loaded or stored 00099 // from/to. If so, the previous load or store would have already trapped, 00100 // so there is no harm doing an extra load (also, CSE will later eliminate 00101 // the load entirely). 00102 BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin(); 00103 00104 while (BBI != E) { 00105 --BBI; 00106 00107 // If we see a free or a call which may write to memory (i.e. which might do 00108 // a free) the pointer could be marked invalid. 00109 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() && 00110 !isa<DbgInfoIntrinsic>(BBI)) 00111 return false; 00112 00113 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { 00114 if (AreEquivalentAddressValues(LI->getOperand(0), V)) return true; 00115 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { 00116 if (AreEquivalentAddressValues(SI->getOperand(1), V)) return true; 00117 } 00118 } 00119 return false; 00120 } 00121 00122 /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the 00123 /// instruction before ScanFrom) checking to see if we have the value at the 00124 /// memory address *Ptr locally available within a small number of instructions. 00125 /// If the value is available, return it. 00126 /// 00127 /// If not, return the iterator for the last validated instruction that the 00128 /// value would be live through. If we scanned the entire block and didn't find 00129 /// something that invalidates *Ptr or provides it, ScanFrom would be left at 00130 /// begin() and this returns null. ScanFrom could also be left 00131 /// 00132 /// MaxInstsToScan specifies the maximum instructions to scan in the block. If 00133 /// it is set to 0, it will scan the whole block. You can also optionally 00134 /// specify an alias analysis implementation, which makes this more precise. 00135 /// 00136 /// If AATags is non-null and a load or store is found, the AA tags from the 00137 /// load or store are recorded there. If there are no AA tags or if no access 00138 /// is found, it is left unmodified. 00139 Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, 00140 BasicBlock::iterator &ScanFrom, 00141 unsigned MaxInstsToScan, 00142 AliasAnalysis *AA, 00143 AAMDNodes *AATags) { 00144 if (MaxInstsToScan == 0) MaxInstsToScan = ~0U; 00145 00146 // If we're using alias analysis to disambiguate get the size of *Ptr. 00147 uint64_t AccessSize = 0; 00148 if (AA) { 00149 Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType(); 00150 AccessSize = AA->getTypeStoreSize(AccessTy); 00151 } 00152 00153 while (ScanFrom != ScanBB->begin()) { 00154 // We must ignore debug info directives when counting (otherwise they 00155 // would affect codegen). 00156 Instruction *Inst = --ScanFrom; 00157 if (isa<DbgInfoIntrinsic>(Inst)) 00158 continue; 00159 00160 // Restore ScanFrom to expected value in case next test succeeds 00161 ScanFrom++; 00162 00163 // Don't scan huge blocks. 00164 if (MaxInstsToScan-- == 0) return nullptr; 00165 00166 --ScanFrom; 00167 // If this is a load of Ptr, the loaded value is available. 00168 // (This is true even if the load is volatile or atomic, although 00169 // those cases are unlikely.) 00170 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) 00171 if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) { 00172 if (AATags) LI->getAAMetadata(*AATags); 00173 return LI; 00174 } 00175 00176 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 00177 // If this is a store through Ptr, the value is available! 00178 // (This is true even if the store is volatile or atomic, although 00179 // those cases are unlikely.) 00180 if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) { 00181 if (AATags) SI->getAAMetadata(*AATags); 00182 return SI->getOperand(0); 00183 } 00184 00185 // If Ptr is an alloca and this is a store to a different alloca, ignore 00186 // the store. This is a trivial form of alias analysis that is important 00187 // for reg2mem'd code. 00188 if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) && 00189 (isa<AllocaInst>(SI->getOperand(1)) || 00190 isa<GlobalVariable>(SI->getOperand(1)))) 00191 continue; 00192 00193 // If we have alias analysis and it says the store won't modify the loaded 00194 // value, ignore the store. 00195 if (AA && 00196 (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) 00197 continue; 00198 00199 // Otherwise the store that may or may not alias the pointer, bail out. 00200 ++ScanFrom; 00201 return nullptr; 00202 } 00203 00204 // If this is some other instruction that may clobber Ptr, bail out. 00205 if (Inst->mayWriteToMemory()) { 00206 // If alias analysis claims that it really won't modify the load, 00207 // ignore it. 00208 if (AA && 00209 (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) 00210 continue; 00211 00212 // May modify the pointer, bail out. 00213 ++ScanFrom; 00214 return nullptr; 00215 } 00216 } 00217 00218 // Got to the start of the block, we didn't find it, but are done for this 00219 // block. 00220 return nullptr; 00221 }