39 #define DEBUG_TYPE "bounds-checking" 42 cl::desc(
"Use one trap block per function"));
44 STATISTIC(ChecksAdded,
"Bounds checks added");
45 STATISTIC(ChecksSkipped,
"Bounds checks skipped");
46 STATISTIC(ChecksUnable,
"Bounds checks unable to add");
91 Value *Cmp2 = SizeRange.getUnsignedMin().uge(OffsetRange.getUnsignedMax())
94 Value *Cmp3 = SizeRange.sub(OffsetRange)
96 .uge(NeededSizeRange.getUnsignedMax())
101 !SizeRange.getSignedMin().isNonNegative()) {
114 template <
typename GetTrapBBT>
155 if (
LoadInst *LI = dyn_cast<LoadInst>(&
I)) {
157 ObjSizeEval, IRB, SE);
160 DL, TLI, ObjSizeEval, IRB, SE);
163 DL, TLI, ObjSizeEval, IRB, SE);
166 TLI, ObjSizeEval, IRB, SE);
169 TrapInfo.push_back(std::make_pair(&
I, Or));
176 auto GetTrapBB = [&TrapBB](
BuilderTy &IRB) {
183 auto DebugLoc = IRB.getCurrentDebugLocation();
186 IRB.SetInsertPoint(TrapBB);
189 CallInst *TrapCall = IRB.CreateCall(F, {});
191 TrapCall->setDoesNotThrow();
193 IRB.CreateUnreachable();
199 for (
const auto &Entry : TrapInfo) {
205 return !TrapInfo.empty();
227 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
228 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
241 "Run-time bounds checking",
false,
false)
244 "Run-time bounds checking",
false, false)
247 return new BoundsCheckingLegacyPass();
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A parsed version of the target data layout string in and methods for querying it. ...
static ConstantInt * getFalse(LLVMContext &Context)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
INITIALIZE_PASS_BEGIN(BoundsCheckingLegacyPass, "bounds-checking", "Run-time bounds checking", false, false) INITIALIZE_PASS_END(BoundsCheckingLegacyPass
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class represents lattice values for constants.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
std::pair< Value *, Value * > SizeOffsetEvalType
The main scalar evolution driver.
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
bool slt(const APInt &RHS) const
Signed less than comparison.
This class represents a function call, abstracting a target machine's calling convention.
LLVMContext & getContext() const
All values hold a context through their type.
STATISTIC(NumFunctions, "Total number of functions")
An instruction for reading from memory.
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
FunctionPass * createBoundsCheckingLegacyPass()
Legacy pass creation function for the above pass.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static cl::opt< bool > SingleTrapBB("bounds-checking-single-trap", cl::desc("Use one trap block per function"))
static void insertBoundsCheck(Value *Or, BuilderTy IRB, GetTrapBBT GetTrapBB)
Adds run-time bounds checks to memory accessing instructions.
Type * getType() const
All values are typed, get the type of this value.
Evaluate the size and offset of an object pointed to by a Value*.
const APInt & getValue() const
Return the constant as an APInt value reference.
TargetFolder - Create constants with target dependent folding.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
An instruction for storing to memory.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
static bool runOnFunction(Function &F, bool PostInlining)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
A set of analyses that are preserved following a run of a transformation pass.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool addBoundsChecking(Function &F, TargetLibraryInfo &TLI, ScalarEvolution &SE)
Represent the analysis usage information of a pass.
static Value * getBoundsCheckCond(Value *Ptr, Value *InstVal, const DataLayout &DL, TargetLibraryInfo &TLI, ObjectSizeOffsetEvaluator &ObjSizeEval, BuilderTy &IRB, ScalarEvolution &SE)
Gets the conditions under which memory accessing instructions will overflow.
FunctionPass class - This class is used to implement most global optimizations.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Provides information about what library functions are available for the current target.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
InstListType::iterator iterator
Instruction iterators...
Analysis pass that exposes the ScalarEvolution for a function.
SizeOffsetEvalType compute(Value *V)
void initializeBoundsCheckingLegacyPassPass(PassRegistry &)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Analysis pass providing the TargetLibraryInfo.
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
BasicBlock::iterator GetInsertPoint() const
inst_range instructions(Function *F)
A container for analyses that lazily runs them and caches their results.
bool bothKnown(SizeOffsetEvalType SizeOffset)
const BasicBlock * getParent() const