diff options
| author | Nuno Lopes <nunoplopes@sapo.pt> | 2012-05-23 16:24:52 +0000 |
|---|---|---|
| committer | Nuno Lopes <nunoplopes@sapo.pt> | 2012-05-23 16:24:52 +0000 |
| commit | 10287d839f401a879aa674c08050b5878b53e517 (patch) | |
| tree | cbcd3b3738053176fdfa83931b12f499802721e1 /llvm/lib/Transforms/Scalar/BoundsChecking.cpp | |
| parent | 52008c641c90b2f19a38e6a996597f53954e9351 (diff) | |
| download | bcm5719-llvm-10287d839f401a879aa674c08050b5878b53e517.tar.gz bcm5719-llvm-10287d839f401a879aa674c08050b5878b53e517.zip | |
BoundsChecking: add a couple of simple tests and fix a bug in branch emition
llvm-svn: 157329
Diffstat (limited to 'llvm/lib/Transforms/Scalar/BoundsChecking.cpp')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/BoundsChecking.cpp | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/Scalar/BoundsChecking.cpp b/llvm/lib/Transforms/Scalar/BoundsChecking.cpp index 004f34f3bd6..85c5e111e0f 100644 --- a/llvm/lib/Transforms/Scalar/BoundsChecking.cpp +++ b/llvm/lib/Transforms/Scalar/BoundsChecking.cpp @@ -62,6 +62,7 @@ namespace { unsigned Penalty; BasicBlock *getTrapBB(); + void emitBranchToTrap(Value *Cmp = 0); ConstTriState computeAllocSize(Value *Alloc, uint64_t &Size, Value* &SizeValue); bool instrument(Value *Ptr, Value *Val); @@ -94,6 +95,22 @@ BasicBlock *BoundsChecking::getTrapBB() { } +/// emitBranchToTrap - emit a branch instruction to a trap block. +/// If Cmp is non-null, perform a jump only if its value evaluates to true. +void BoundsChecking::emitBranchToTrap(Value *Cmp) { + Instruction *Inst = Builder->GetInsertPoint(); + BasicBlock *OldBB = Inst->getParent(); + BasicBlock *Cont = OldBB->splitBasicBlock(Inst); + OldBB->getTerminator()->eraseFromParent(); + + // FIXME: add unlikely branch taken metadata? + if (Cmp) + BranchInst::Create(getTrapBB(), Cont, Cmp, OldBB); + else + BranchInst::Create(getTrapBB(), OldBB); +} + + /// computeAllocSize - compute the object size allocated by an allocation /// site. Returns NotConst if the size is not constant (in SizeValue), Const if /// the size is constant (in Size), and Dunno if the size could not be @@ -254,7 +271,7 @@ bool BoundsChecking::instrument(Value *Ptr, Value *InstVal) { if (!OffsetValue && ConstAlloc == Const) { if (Size < Offset || (Size - Offset) < NeededSize) { // Out of bounds - Builder->CreateBr(getTrapBB()); + emitBranchToTrap(); ++ChecksAdded; return true; } @@ -278,13 +295,8 @@ bool BoundsChecking::instrument(Value *Ptr, Value *InstVal) { Value *Cmp1 = Builder->CreateICmpULT(SizeValue, OffsetValue); Value *Cmp2 = Builder->CreateICmpULT(ObjSize, NeededSizeVal); Value *Or = Builder->CreateOr(Cmp1, Cmp2); + emitBranchToTrap(Or); - // FIXME: add unlikely branch taken metadata? - Instruction *Inst = Builder->GetInsertPoint(); - BasicBlock *OldBB = Inst->getParent(); - BasicBlock *Cont = OldBB->splitBasicBlock(Inst); - OldBB->getTerminator()->eraseFromParent(); - BranchInst::Create(getTrapBB(), Cont, Or, OldBB); ++ChecksAdded; return true; } |

