summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2017-10-18 22:42:36 +0000
committerChandler Carruth <chandlerc@gmail.com>2017-10-18 22:42:36 +0000
commit3f0e056df4fba3343ea5a323c8cc747e41e4dd27 (patch)
treed77137afdf04dd3dbccc5b6a8658808d27c9b7a3 /llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
parentda9600e646c8776698a292e738f91e699fce256b (diff)
downloadbcm5719-llvm-3f0e056df4fba3343ea5a323c8cc747e41e4dd27.tar.gz
bcm5719-llvm-3f0e056df4fba3343ea5a323c8cc747e41e4dd27.zip
[PM] Refactor the bounds checking pass to remove a method only called in
one place. llvm-svn: 316135
Diffstat (limited to 'llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp')
-rw-r--r--llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp53
1 files changed, 25 insertions, 28 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp b/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
index a193efe902c..4a7875b6d48 100644
--- a/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
+++ b/llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
@@ -60,7 +60,6 @@ namespace {
BasicBlock *TrapBB;
BasicBlock *getTrapBB();
- void emitBranchToTrap(Value *Cmp = nullptr);
bool instrument(Value *Ptr, Value *Val, const DataLayout &DL);
};
}
@@ -92,32 +91,6 @@ 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) {
- // check if the comparison is always false
- ConstantInt *C = dyn_cast_or_null<ConstantInt>(Cmp);
- if (C) {
- ++ChecksSkipped;
- if (!C->getZExtValue())
- return;
- else
- Cmp = nullptr; // unconditional branch
- }
- ++ChecksAdded;
-
- BasicBlock::iterator Inst = Builder->GetInsertPoint();
- BasicBlock *OldBB = Inst->getParent();
- BasicBlock *Cont = OldBB->splitBasicBlock(Inst);
- OldBB->getTerminator()->eraseFromParent();
-
- if (Cmp)
- BranchInst::Create(getTrapBB(), Cont, Cmp, OldBB);
- else
- BranchInst::Create(getTrapBB(), OldBB);
-}
-
-
/// instrument - adds run-time bounds checks to memory accessing instructions.
/// Ptr is the pointer that will be read/written, and InstVal is either the
/// result from the load or the value being stored. It is used to determine the
@@ -158,8 +131,32 @@ bool BoundsChecking::instrument(Value *Ptr, Value *InstVal,
Value *Cmp1 = Builder->CreateICmpSLT(Offset, ConstantInt::get(IntTy, 0));
Or = Builder->CreateOr(Cmp1, Or);
}
- emitBranchToTrap(Or);
+ // check if the comparison is always false
+ ConstantInt *C = dyn_cast_or_null<ConstantInt>(Or);
+ if (C) {
+ ++ChecksSkipped;
+ // If non-zero, nothing to do.
+ if (!C->getZExtValue())
+ return true;
+ }
+ ++ChecksAdded;
+
+ BasicBlock::iterator SplitI = Builder->GetInsertPoint();
+ BasicBlock *OldBB = SplitI->getParent();
+ BasicBlock *Cont = OldBB->splitBasicBlock(SplitI);
+ OldBB->getTerminator()->eraseFromParent();
+
+ if (C) {
+ // If we have a constant zero, unconditionally branch.
+ // FIXME: We should really handle this differently to bypass the splitting
+ // the block.
+ BranchInst::Create(getTrapBB(), OldBB);
+ return true;
+ }
+
+ // Create the conditional branch.
+ BranchInst::Create(getTrapBB(), Cont, Or, OldBB);
return true;
}
OpenPOWER on IntegriCloud