8238178: CTW: C1 compilation fails with assert(sux->loop_depth() != block->loop_depth() || sux->loop_index() == block->loop_index() || loop_through_xhandler) failed: Loop index has to be same

Fix verification of C1 IR involving xhandlers in one-time executed loops on the non-exceptional path.

Reviewed-by: thartmann, neliasso
This commit is contained in:
Christian Hagedorn 2020-02-13 15:06:57 +01:00
parent b0b8190ea2
commit 8bdb972192
3 changed files with 495 additions and 14 deletions

@ -30,6 +30,9 @@
#include "c1/c1_ValueMap.hpp"
#include "ci/ciMethodData.hpp"
#include "runtime/deoptimization.hpp"
#ifdef ASSERT
#include "utilities/bitMap.inline.hpp"
#endif
// Macros for the Trace and the Assertion flag
#ifdef ASSERT
@ -1050,6 +1053,7 @@ void RangeCheckEliminator::dump_condition_stack(BlockBegin *block) {
}
#endif
#ifdef ASSERT
// Verification or the IR
RangeCheckEliminator::Verification::Verification(IR *ir) : _used(BlockBegin::number_of_blocks(), BlockBegin::number_of_blocks(), false) {
this->_ir = ir;
@ -1099,21 +1103,16 @@ void RangeCheckEliminator::Verification::block_do(BlockBegin *block) {
BlockList *all_blocks = _ir->linear_scan_order();
assert(block->number_of_preds() >= 1, "Block must have at least one predecessor");
assert(!block->is_set(BlockBegin::exception_entry_flag), "Loop header must not be exception handler!");
// Sometimes, the backbranch comes from an exception handler. In
// this case, loop indexes/loop depths may not appear correct.
bool loop_through_xhandler = false;
for (int i = 0; i < block->number_of_exception_handlers(); i++) {
BlockBegin *xhandler = block->exception_handler_at(i);
for (int j = 0; j < block->number_of_preds(); j++) {
if (dominates(xhandler, block->pred_at(j)) || xhandler == block->pred_at(j)) {
loop_through_xhandler = true;
}
}
}
bool loop_through_xhandler = false;
for (int i=0; i<block->number_of_sux(); i++) {
BlockBegin *sux = block->sux_at(i);
assert(sux->loop_depth() != block->loop_depth() || sux->loop_index() == block->loop_index() || loop_through_xhandler, "Loop index has to be same");
if (!loop_through_xhandler) {
if (sux->loop_depth() == block->loop_depth() && sux->loop_index() != block->loop_index()) {
loop_through_xhandler = is_backbranch_from_xhandler(block);
assert(loop_through_xhandler, "Loop indices have to be the same if same depths but no backbranch from xhandler");
}
}
assert(sux->loop_depth() == block->loop_depth() || sux->loop_index() != block->loop_index(), "Loop index has to be different");
}
@ -1132,6 +1131,54 @@ void RangeCheckEliminator::Verification::block_do(BlockBegin *block) {
}
}
// Called when a successor of a block has the same loop depth but a different loop index. This can happen if a backbranch comes from
// an exception handler of a loop head block, for example, when a loop is only executed once on the non-exceptional path but is
// repeated in case of an exception. In this case, the edge block->sux is not critical and was not split before.
// Check if there is such a backbranch from an xhandler of 'block'.
bool RangeCheckEliminator::Verification::is_backbranch_from_xhandler(BlockBegin* block) {
for (int i = 0; i < block->number_of_exception_handlers(); i++) {
BlockBegin *xhandler = block->exception_handler_at(i);
for (int j = 0; j < block->number_of_preds(); j++) {
if (dominates(xhandler, block->pred_at(j)) || xhandler == block->pred_at(j)) {
return true;
}
}
}
// In case of nested xhandlers, we need to walk through the loop (and all blocks belonging to exception handlers)
// to find an xhandler of 'block'.
if (block->number_of_exception_handlers() > 0) {
for (int i = 0; i < block->number_of_preds(); i++) {
BlockBegin* pred = block->pred_at(i);
if (pred->loop_index() == block->loop_index()) {
// Only check blocks that belong to the loop
// Do a BFS to find an xhandler block of 'block' starting from 'pred'
ResourceMark rm;
ResourceBitMap visited(BlockBegin::number_of_blocks());
BlockBeginList list;
list.push(pred);
while (!list.is_empty()) {
BlockBegin* next = list.pop();
if (!visited.at(next->block_id())) {
visited.set_bit(next->block_id());
for (int j = 0; j < block->number_of_exception_handlers(); j++) {
if (next == block->exception_handler_at(j)) {
return true;
}
}
for (int j = 0; j < next->number_of_preds(); j++) {
if (next->pred_at(j) != block) {
list.push(next->pred_at(j));
}
}
}
}
}
}
}
return false;
}
// Loop header must dominate all loop blocks
bool RangeCheckEliminator::Verification::dominates(BlockBegin *dominator, BlockBegin *block) {
BlockBegin *cur = block->dominator();
@ -1195,6 +1242,7 @@ bool RangeCheckEliminator::Verification::can_reach(BlockBegin *start, BlockBegin
return false;
}
#endif // ASSERT
// Bound
RangeCheckEliminator::Bound::~Bound() {

@ -43,6 +43,7 @@ private:
typedef GrowableArray<BlockBegin*> BlockBeginList;
typedef GrowableArray<int> IntegerStack;
#ifdef ASSERT
class Verification : public BlockClosure {
// RangeCheckEliminator::Verification should never get instatiated on the heap.
private:
@ -51,6 +52,10 @@ private:
void operator delete(void* p) { ShouldNotReachHere(); }
void operator delete[](void* p) { ShouldNotReachHere(); }
bool can_reach(BlockBegin *start, BlockBegin *end, BlockBegin *dont_use = NULL);
bool dominates(BlockBegin *dominator, BlockBegin *block);
bool is_backbranch_from_xhandler(BlockBegin* block);
IR *_ir;
boolArray _used;
BlockBeginList _current;
@ -59,9 +64,8 @@ private:
public:
Verification(IR *ir);
virtual void block_do(BlockBegin *block);
bool can_reach(BlockBegin *start, BlockBegin *end, BlockBegin *dont_use = NULL);
bool dominates(BlockBegin *dominator, BlockBegin *block);
};
#endif
public:
// Bounds for an instruction in the form x + c which c integer

@ -0,0 +1,429 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8238178
* @summary Checks the C1 RangeCheckEliminator::Verification code for nested exceptions in loops that are always executed once on the non-exceptional path.
*
* @run main/othervm -Xbatch -XX:TieredStopAtLevel=1 -XX:CompileCommand=dontinline,compiler.c1.RangeCheckVerificationOfIR::throwException*
* -XX:CompileCommand=dontinline,compiler.c1.RangeCheckVerificationOfIR::test* compiler.c1.RangeCheckVerificationOfIR
*/
package compiler.c1;
public class RangeCheckVerificationOfIR {
int a;
int i1;
int i2;
int i3;
public static void main(String[] args) {
RangeCheckVerificationOfIR instance = new RangeCheckVerificationOfIR();
instance.resetValues();
for (int i = 0; i < 1000; i++) {
instance.testSimple();
instance.resetValues();
instance.testDominatedByXhandler();
instance.resetValues();
instance.testThrowOneException();
instance.resetValues();
instance.testNestedExceptions();
instance.resetValues();
instance.testTriplyNestedExceptions();
instance.resetValues();
instance.testTriplyNestedExceptions2();
instance.resetValues();
instance.testTriplyNestedMultipleHandlers();
instance.resetValues();
instance.testTriplyNestedNoExceptionThrown();
instance.resetValues();
}
}
private void resetValues() {
i1 = 0;
i2 = 0;
i3 = 0;
}
// Is handled by current code (xhandler equals a pred of loop header block)
public void testSimple() {
int[] iArr = new int[8];
for (int i = 0; i < 8; i++) {
iArr[0] = 4;
}
while (true) {
try {
throwException();
break;
} catch(Exception ex1) {
i1++;
}
}
for (int i = 0; i < 10; i++) {
a = 5;
}
}
// Is handled by current code (xhandler dominates a pred of loop header block)
public void testDominatedByXhandler() {
int[] iArr = new int[8];
for (int i = 0; i < 8; i++) {
iArr[0] = 4;
}
while (true) {
try {
throwException();
break;
} catch (Exception ex1) {
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
i1++;
}
}
for (int i = 0; i < 10; i++) {
a = 5;
}
}
// Not a problem, since no backbranch and therefore no loop
public void testThrowOneException() {
int[] iArr = new int[8];
for (int i = 0; i < 8; i++) {
iArr[0] = 4;
}
try {
for (int i = 0; i < iArr[4]; i++) {
throwException();
}
} catch (Exception ex) {
a = 345;
}
try {
while (true) {
throwException();
break;
}
} catch (Exception e) {
a = 45;
}
for (int i = 0; i < 10; i++) {
a = 5;
}
}
// All following cases are not handled yet. Need to walk backbranch of loop header block
// to find one of the exception handlers of loop header block somewhere. Must exist.
public void testNestedExceptions() {
int[] iArr = new int[8];
for (int i = 0; i < 8; i++) {
iArr[0] = 4;
}
// The block from the backbranch, lets say B, is no xhandler block and not dominated by either of the two xhandler blocks, lets say
// E1 for the outer and E2 for the inner try/catch block: If no exception occurs in E1, then E1 is completely executed without
// executing E2. But if an exception occurs, then only parts of E1 is executed and E2 is executed completely.
// Therefore, neither of them dominates B.
while (true) {
try {
throwException();
break;
} catch (Exception ex1) {
i1++;
try {
throwException2();
} catch (Exception ex2) {
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
i2++;
}
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
i1++;
}
}
for (int i = 0; i < 10; i++) {
a = 5;
}
}
public void testTriplyNestedExceptions() {
int[] iArr = new int[8];
for (int i = 0; i < 8; i++) {
iArr[0] = 4;
}
while (true) {
try {
throwException();
break;
} catch (Exception ex1) {
i1++;
try {
throwException2();
} catch (Exception ex2) {
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
try {
throwException3();
} catch (Exception ex3) {
i3++;
}
try {
throwException3();
} catch (Exception ex3) {
i3++;
}
i2++;
}
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
i1++;
}
}
for (int i = 0; i < 10; i++) {
a = 5;
}
}
public void testTriplyNestedExceptions2() {
int[] iArr = new int[8];
for (int i = 0; i < 8; i++) {
iArr[0] = 4;
}
try {
for (int i = 0; i < iArr[4]; i++) {
throwException();
}
} catch (Exception ex) {
a = 345;
}
while (true) {
try {
throwException();
break;
} catch (Exception ex1) {
i1++;
try {
throwException2();
} catch (Exception ex2) {
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
try {
throwException3();
} catch (Exception ex3) {
i3++;
}
try {
throwException3();
} catch (Exception ex3) {
i3++;
}
i2++;
}
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
i1++;
}
}
for (int i = 0; i < 10; i++) {
a = 5;
}
}
public void testTriplyNestedMultipleHandlers() {
int[] iArr = new int[8];
for (int i = 0; i < 8; i++) {
iArr[0] = 4;
}
try {
for (int i = 0; i < iArr[4]; i++) {
throwException();
}
} catch (Exception ex) {
a = 345;
}
try {
while (true) {
try {
throwException();
break;
} catch (MyInnerException ie) {
i1++;
try {
throwException2();
} catch (Exception ex2) {
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
try {
throwException3();
} catch (Exception ex3) {
i3++;
}
try {
throwException3();
} catch (Exception ex3) {
i3++;
}
i2++;
}
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
i1++;
}
}
} catch (MyOuterException oe) {
a = 45;
}
for (int i = 0; i < 10; i++) {
a = 5;
}
}
public void testTriplyNestedNoExceptionThrown() {
int[] iArr = new int[8];
for (int i = 0; i < 8; i++) {
iArr[0] = 4;
}
try {
for (int i = 0; i < iArr[4]; i++) {
throwException();
}
} catch (Exception ex) {
a = 345;
}
try {
while (true) {
try {
a = 4;
break;
} catch (RuntimeException ie) {
i1++;
try {
throwException2();
} catch (Exception ex2) {
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
try {
throwException3();
} catch (Exception ex3) {
i3++;
}
try {
throwException3();
} catch (Exception ex3) {
i3++;
}
i2++;
}
if (i1 < i2) {
a = 3;
} else {
a = 4;
}
i1++;
}
}
} catch (Exception e) {
a = 45;
}
for (int i = 0; i < 10; i++) {
a = 5;
}
}
void throwException() throws MyInnerException, MyOuterException {
if (i1 < 3) {
throw new MyInnerException();
}
if (i1 < 5) {
throw new MyOuterException();
}
}
public void throwException2() throws Exception {
if (i2 < 3) {
throw new RuntimeException();
}
}
public void throwException3() throws Exception {
if (i3 < 2) {
throw new RuntimeException();
}
}
class MyInnerException extends Exception { }
class MyOuterException extends Exception { }
}