LLVM  mainline
Loads.cpp
Go to the documentation of this file.
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/Module.h"
00023 #include "llvm/IR/Operator.h"
00024 using namespace llvm;
00025 
00026 /// \brief Test if A and B will obviously have the same value.
00027 ///
00028 /// This includes recognizing that %t0 and %t1 will have the same
00029 /// value in code like this:
00030 /// \code
00031 ///   %t0 = getelementptr \@a, 0, 3
00032 ///   store i32 0, i32* %t0
00033 ///   %t1 = getelementptr \@a, 0, 3
00034 ///   %t2 = load i32* %t1
00035 /// \endcode
00036 ///
00037 static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
00038   // Test if the values are trivially equivalent.
00039   if (A == B)
00040     return true;
00041 
00042   // Test if the values come from identical arithmetic instructions.
00043   // Use isIdenticalToWhenDefined instead of isIdenticalTo because
00044   // this function is only used when one address use dominates the
00045   // other, which means that they'll always either have the same
00046   // value or one of them will have an undefined value.
00047   if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
00048       isa<GetElementPtrInst>(A))
00049     if (const Instruction *BI = dyn_cast<Instruction>(B))
00050       if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
00051         return true;
00052 
00053   // Otherwise they may not be equivalent.
00054   return false;
00055 }
00056 
00057 /// \brief Check if executing a load of this pointer value cannot trap.
00058 ///
00059 /// If it is not obviously safe to load from the specified pointer, we do
00060 /// a quick local scan of the basic block containing \c ScanFrom, to determine
00061 /// if the address is already accessed.
00062 ///
00063 /// This uses the pointee type to determine how many bytes need to be safe to
00064 /// load from the pointer.
00065 bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align,
00066                                        Instruction *ScanFrom) {
00067   const DataLayout &DL = ScanFrom->getModule()->getDataLayout();
00068 
00069   // Zero alignment means that the load has the ABI alignment for the target
00070   if (Align == 0)
00071     Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
00072   assert(isPowerOf2_32(Align));
00073 
00074   if (isDereferenceableAndAlignedPointer(V, Align, DL))
00075     return true;
00076 
00077   int64_t ByteOffset = 0;
00078   Value *Base = V;
00079   Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
00080 
00081   if (ByteOffset < 0) // out of bounds
00082     return false;
00083 
00084   Type *BaseType = nullptr;
00085   unsigned BaseAlign = 0;
00086   if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
00087     // An alloca is safe to load from as load as it is suitably aligned.
00088     BaseType = AI->getAllocatedType();
00089     BaseAlign = AI->getAlignment();
00090   } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
00091     // Global variables are not necessarily safe to load from if they are
00092     // overridden. Their size may change or they may be weak and require a test
00093     // to determine if they were in fact provided.
00094     if (!GV->mayBeOverridden()) {
00095       BaseType = GV->getType()->getElementType();
00096       BaseAlign = GV->getAlignment();
00097     }
00098   }
00099 
00100   PointerType *AddrTy = cast<PointerType>(V->getType());
00101   uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType());
00102 
00103   // If we found a base allocated type from either an alloca or global variable,
00104   // try to see if we are definitively within the allocated region. We need to
00105   // know the size of the base type and the loaded type to do anything in this
00106   // case.
00107   if (BaseType && BaseType->isSized()) {
00108     if (BaseAlign == 0)
00109       BaseAlign = DL.getPrefTypeAlignment(BaseType);
00110 
00111     if (Align <= BaseAlign) {
00112       // Check if the load is within the bounds of the underlying object.
00113       if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) &&
00114           ((ByteOffset % Align) == 0))
00115         return true;
00116     }
00117   }
00118 
00119   // Otherwise, be a little bit aggressive by scanning the local block where we
00120   // want to check to see if the pointer is already being loaded or stored
00121   // from/to.  If so, the previous load or store would have already trapped,
00122   // so there is no harm doing an extra load (also, CSE will later eliminate
00123   // the load entirely).
00124   BasicBlock::iterator BBI = ScanFrom->getIterator(),
00125                        E = ScanFrom->getParent()->begin();
00126 
00127   // We can at least always strip pointer casts even though we can't use the
00128   // base here.
00129   V = V->stripPointerCasts();
00130 
00131   while (BBI != E) {
00132     --BBI;
00133 
00134     // If we see a free or a call which may write to memory (i.e. which might do
00135     // a free) the pointer could be marked invalid.
00136     if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
00137         !isa<DbgInfoIntrinsic>(BBI))
00138       return false;
00139 
00140     Value *AccessedPtr;
00141     unsigned AccessedAlign;
00142     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
00143       AccessedPtr = LI->getPointerOperand();
00144       AccessedAlign = LI->getAlignment();
00145     } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
00146       AccessedPtr = SI->getPointerOperand();
00147       AccessedAlign = SI->getAlignment();
00148     } else
00149       continue;
00150 
00151     Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
00152     if (AccessedAlign == 0)
00153       AccessedAlign = DL.getABITypeAlignment(AccessedTy);
00154     if (AccessedAlign < Align)
00155       continue;
00156 
00157     // Handle trivial cases.
00158     if (AccessedPtr == V)
00159       return true;
00160 
00161     if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
00162         LoadSize <= DL.getTypeStoreSize(AccessedTy))
00163       return true;
00164   }
00165   return false;
00166 }
00167 
00168 /// DefMaxInstsToScan - the default number of maximum instructions
00169 /// to scan in the block, used by FindAvailableLoadedValue().
00170 /// FindAvailableLoadedValue() was introduced in r60148, to improve jump
00171 /// threading in part by eliminating partially redundant loads.
00172 /// At that point, the value of MaxInstsToScan was already set to '6'
00173 /// without documented explanation.
00174 cl::opt<unsigned>
00175 llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
00176   cl::desc("Use this to specify the default maximum number of instructions "
00177            "to scan backward from a given instruction, when searching for "
00178            "available loaded value"));
00179 
00180 /// \brief Scan the ScanBB block backwards to see if we have the value at the
00181 /// memory address *Ptr locally available within a small number of instructions.
00182 ///
00183 /// The scan starts from \c ScanFrom. \c MaxInstsToScan specifies the maximum
00184 /// instructions to scan in the block. If it is set to \c 0, it will scan the whole
00185 /// block.
00186 ///
00187 /// If the value is available, this function returns it. If not, it returns the
00188 /// iterator for the last validated instruction that the value would be live
00189 /// through. If we scanned the entire block and didn't find something that
00190 /// invalidates \c *Ptr or provides it, \c ScanFrom is left at the last
00191 /// instruction processed and this returns null.
00192 ///
00193 /// You can also optionally specify an alias analysis implementation, which
00194 /// makes this more precise.
00195 ///
00196 /// If \c AATags is non-null and a load or store is found, the AA tags from the
00197 /// load or store are recorded there. If there are no AA tags or if no access is
00198 /// found, it is left unmodified.
00199 Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB,
00200                                       BasicBlock::iterator &ScanFrom,
00201                                       unsigned MaxInstsToScan,
00202                                       AliasAnalysis *AA, AAMDNodes *AATags) {
00203   if (MaxInstsToScan == 0)
00204     MaxInstsToScan = ~0U;
00205 
00206   Value *Ptr = Load->getPointerOperand();
00207   Type *AccessTy = Load->getType();
00208 
00209   const DataLayout &DL = ScanBB->getModule()->getDataLayout();
00210 
00211   // Try to get the store size for the type.
00212   uint64_t AccessSize = DL.getTypeStoreSize(AccessTy);
00213 
00214   Value *StrippedPtr = Ptr->stripPointerCasts();
00215 
00216   while (ScanFrom != ScanBB->begin()) {
00217     // We must ignore debug info directives when counting (otherwise they
00218     // would affect codegen).
00219     Instruction *Inst = &*--ScanFrom;
00220     if (isa<DbgInfoIntrinsic>(Inst))
00221       continue;
00222 
00223     // Restore ScanFrom to expected value in case next test succeeds
00224     ScanFrom++;
00225 
00226     // Don't scan huge blocks.
00227     if (MaxInstsToScan-- == 0)
00228       return nullptr;
00229 
00230     --ScanFrom;
00231     // If this is a load of Ptr, the loaded value is available.
00232     // (This is true even if the load is volatile or atomic, although
00233     // those cases are unlikely.)
00234     if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
00235       if (AreEquivalentAddressValues(
00236               LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
00237           CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
00238         if (AATags)
00239           LI->getAAMetadata(*AATags);
00240         return LI;
00241       }
00242 
00243     if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
00244       Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
00245       // If this is a store through Ptr, the value is available!
00246       // (This is true even if the store is volatile or atomic, although
00247       // those cases are unlikely.)
00248       if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
00249           CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
00250                                                AccessTy, DL)) {
00251         if (AATags)
00252           SI->getAAMetadata(*AATags);
00253         return SI->getOperand(0);
00254       }
00255 
00256       // If both StrippedPtr and StorePtr reach all the way to an alloca or
00257       // global and they are different, ignore the store. This is a trivial form
00258       // of alias analysis that is important for reg2mem'd code.
00259       if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
00260           (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
00261           StrippedPtr != StorePtr)
00262         continue;
00263 
00264       // If we have alias analysis and it says the store won't modify the loaded
00265       // value, ignore the store.
00266       if (AA && (AA->getModRefInfo(SI, StrippedPtr, AccessSize) & MRI_Mod) == 0)
00267         continue;
00268 
00269       // Otherwise the store that may or may not alias the pointer, bail out.
00270       ++ScanFrom;
00271       return nullptr;
00272     }
00273 
00274     // If this is some other instruction that may clobber Ptr, bail out.
00275     if (Inst->mayWriteToMemory()) {
00276       // If alias analysis claims that it really won't modify the load,
00277       // ignore it.
00278       if (AA &&
00279           (AA->getModRefInfo(Inst, StrippedPtr, AccessSize) & MRI_Mod) == 0)
00280         continue;
00281 
00282       // May modify the pointer, bail out.
00283       ++ScanFrom;
00284       return nullptr;
00285     }
00286   }
00287 
00288   // Got to the start of the block, we didn't find it, but are done for this
00289   // block.
00290   return nullptr;
00291 }