1 #include "llvm/IR/DataLayout.h"
2 #include "llvm/IR/Instruction.h"
3 #include "llvm/IR/Instructions.h"
4 
5 namespace llvm {
6 
7 class CheriNeedBoundsChecker {
8 public:
CheriNeedBoundsChecker(AllocaInst * AI,const DataLayout & DL)9   CheriNeedBoundsChecker(AllocaInst *AI, const DataLayout &DL) : I(AI), DL(DL) {
10     auto AllocaSize = AI->getAllocationSizeInBits(DL);
11     if (AllocaSize)
12       MinSizeInBytes = *AllocaSize / 8;
13     PointerAS = AI->getType()->getAddressSpace();
14   }
CheriNeedBoundsChecker(Instruction * I,Optional<uint64_t> MinSize,const DataLayout & DL)15   CheriNeedBoundsChecker(Instruction *I, Optional<uint64_t> MinSize,
16                          const DataLayout &DL)
17       : I(I), DL(DL), MinSizeInBytes(MinSize) {
18     assert(I->getType()->isPointerTy());
19     PointerAS = I->getType()->getPointerAddressSpace();
20   }
21   bool check(const Use &U) const;
22   void findUsesThatNeedBounds(SmallVectorImpl<const Use *> *UsesThatNeedBounds,
23                               bool BoundAllUses,
24                               bool *MustUseSingleIntrinsic) const;
25   bool anyUseNeedsBounds() const;
26 
27 private:
28   bool useNeedsBounds(const Use &U, const APInt &CurrentGEPOffset,
29                       unsigned Depth) const;
30   bool anyUserNeedsBounds(const Instruction *I, const APInt &CurrentGEPOffset,
31                           unsigned Depth) const;
32   bool canLoadStoreBeOutOfBounds(const Instruction *I, const Use &U,
33                                  const APInt &CurrentGEPOffset,
34                                  unsigned Depth) const;
35 
36   const Instruction *I;
37   const DataLayout &DL;
38   Optional<uint64_t> MinSizeInBytes;
39   unsigned PointerAS = 0;
40 };
41 
42 } // namespace llvm
43