Home | History | Annotate | Download | only in Analysis
      1 //===- Loads.h - Local load analysis --------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file declares simple local analyses for load instructions.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_ANALYSIS_LOADS_H
     15 #define LLVM_ANALYSIS_LOADS_H
     16 
     17 #include "llvm/Analysis/AliasAnalysis.h"
     18 #include "llvm/IR/BasicBlock.h"
     19 #include "llvm/Support/CommandLine.h"
     20 
     21 namespace llvm {
     22 
     23 class DataLayout;
     24 class MDNode;
     25 
     26 /// Return true if this is always a dereferenceable pointer. If the context
     27 /// instruction is specified perform context-sensitive analysis and return true
     28 /// if the pointer is dereferenceable at the specified instruction.
     29 bool isDereferenceablePointer(const Value *V, const DataLayout &DL,
     30                               const Instruction *CtxI = nullptr,
     31                               const DominatorTree *DT = nullptr);
     32 
     33 /// Returns true if V is always a dereferenceable pointer with alignment
     34 /// greater or equal than requested. If the context instruction is specified
     35 /// performs context-sensitive analysis and returns true if the pointer is
     36 /// dereferenceable at the specified instruction.
     37 bool isDereferenceableAndAlignedPointer(const Value *V, unsigned Align,
     38                                         const DataLayout &DL,
     39                                         const Instruction *CtxI = nullptr,
     40                                         const DominatorTree *DT = nullptr);
     41 
     42 /// Return true if we know that executing a load from this value cannot trap.
     43 ///
     44 /// If DT and ScanFrom are specified this method performs context-sensitive
     45 /// analysis and returns true if it is safe to load immediately before ScanFrom.
     46 ///
     47 /// If it is not obviously safe to load from the specified pointer, we do a
     48 /// quick local scan of the basic block containing ScanFrom, to determine if
     49 /// the address is already accessed.
     50 bool isSafeToLoadUnconditionally(Value *V, unsigned Align,
     51                                  const DataLayout &DL,
     52                                  Instruction *ScanFrom = nullptr,
     53                                  const DominatorTree *DT = nullptr);
     54 
     55 /// The default number of maximum instructions to scan in the block, used by
     56 /// FindAvailableLoadedValue().
     57 extern cl::opt<unsigned> DefMaxInstsToScan;
     58 
     59 /// Scan backwards to see if we have the value of the given load available
     60 /// locally within a small number of instructions.
     61 ///
     62 /// You can use this function to scan across multiple blocks: after you call
     63 /// this function, if ScanFrom points at the beginning of the block, it's safe
     64 /// to continue scanning the predecessors.
     65 ///
     66 /// Note that performing load CSE requires special care to make sure the
     67 /// metadata is set appropriately.  In particular, aliasing metadata needs
     68 /// to be merged.  (This doesn't matter for store-to-load forwarding because
     69 /// the only relevant load gets deleted.)
     70 ///
     71 /// \param Load The load we want to replace.
     72 /// \param ScanBB The basic block to scan.
     73 /// \param [in,out] ScanFrom The location to start scanning from. When this
     74 /// function returns, it points at the last instruction scanned.
     75 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
     76 /// is zero, the whole block will be scanned.
     77 /// \param AA Optional pointer to alias analysis, to make the scan more
     78 /// precise.
     79 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
     80 /// location in memory, as opposed to the value operand of a store.
     81 ///
     82 /// \returns The found value, or nullptr if no value is found.
     83 Value *FindAvailableLoadedValue(LoadInst *Load,
     84                                 BasicBlock *ScanBB,
     85                                 BasicBlock::iterator &ScanFrom,
     86                                 unsigned MaxInstsToScan = DefMaxInstsToScan,
     87                                 AliasAnalysis *AA = nullptr,
     88                                 bool *IsLoadCSE = nullptr,
     89                                 unsigned *NumScanedInst = nullptr);
     90 
     91 /// Scan backwards to see if we have the value of the given pointer available
     92 /// locally within a small number of instructions.
     93 ///
     94 /// You can use this function to scan across multiple blocks: after you call
     95 /// this function, if ScanFrom points at the beginning of the block, it's safe
     96 /// to continue scanning the predecessors.
     97 ///
     98 /// \param Ptr The pointer we want the load and store to originate from.
     99 /// \param AccessTy The access type of the pointer.
    100 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
    101 /// case it is false, we can return an atomic or non-atomic load or store. In
    102 /// case it is true, we need to return an atomic load or store.
    103 /// \param ScanBB The basic block to scan.
    104 /// \param [in,out] ScanFrom The location to start scanning from. When this
    105 /// function returns, it points at the last instruction scanned.
    106 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
    107 /// is zero, the whole block will be scanned.
    108 /// \param AA Optional pointer to alias analysis, to make the scan more
    109 /// precise.
    110 /// \param [out] IsLoad Whether the returned value is a load from the same
    111 /// location in memory, as opposed to the value operand of a store.
    112 ///
    113 /// \returns The found value, or nullptr if no value is found.
    114 Value *FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, bool AtLeastAtomic,
    115                                  BasicBlock *ScanBB,
    116                                  BasicBlock::iterator &ScanFrom,
    117                                  unsigned MaxInstsToScan, AliasAnalysis *AA,
    118                                  bool *IsLoad, unsigned *NumScanedInst);
    119 }
    120 
    121 #endif
    122