Do not remove NullChecks & BoundsChecks in HDeadCodeElimination.
Removing a NullCheck or a BoundsCheck instruction may change
the behavior of a program.
Change-Id: Ib2c9beff0cc98c382210e7cc88b1fa9af3c61887
diff --git a/compiler/optimizing/dead_code_elimination.cc b/compiler/optimizing/dead_code_elimination.cc
index fe2adc7..5655544 100644
--- a/compiler/optimizing/dead_code_elimination.cc
+++ b/compiler/optimizing/dead_code_elimination.cc
@@ -35,7 +35,10 @@
for (i.Advance(); !i.Done(); i.Advance()) {
HInstruction* inst = i.Current();
DCHECK(!inst->IsControlFlow());
- if (!inst->HasSideEffects() && !inst->HasUses() && !inst->IsSuspendCheck()) {
+ if (!inst->HasSideEffects()
+ && !inst->CanThrow()
+ && !inst->IsSuspendCheck()
+ && !inst->HasUses()) {
block->RemoveInstruction(inst);
}
}
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index 677a4f8..c6eb806 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -650,6 +650,7 @@
virtual bool NeedsEnvironment() const { return false; }
virtual bool IsControlFlow() const { return false; }
+ virtual bool CanThrow() const { return false; }
bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
void AddUseAt(HInstruction* user, size_t index) {
@@ -1642,6 +1643,8 @@
virtual bool NeedsEnvironment() const { return true; }
+ virtual bool CanThrow() const { return true; }
+
uint32_t GetDexPc() const { return dex_pc_; }
DECLARE_INSTRUCTION(NullCheck);
@@ -1802,6 +1805,8 @@
virtual bool NeedsEnvironment() const { return true; }
+ virtual bool CanThrow() const { return true; }
+
uint32_t GetDexPc() const { return dex_pc_; }
DECLARE_INSTRUCTION(BoundsCheck);