11/*
2- * Copyright (c) 2011, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2011, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -250,8 +250,30 @@ public static void propagateFixed(FixedNode from, StaticDeoptimizingNode deopt,
250250 FixedGuardNode guard = graph .add (
251251 new FixedGuardNode (conditionNode , deopt .getReason (), deopt .getAction (), deopt .getSpeculation (), negateGuardCondition , survivingSuccessorPosition ));
252252 FixedWithNextNode pred = (FixedWithNextNode ) ifNode .predecessor ();
253- AbstractBeginNode survivingSuccessor ;
253+ /**
254+ * We may have the following special case if pred is a LoopBegin:
255+ *
256+ * <pre>
257+ * LoopBegin (= pred) <--------+
258+ * | |
259+ * | <- guard insert pos |
260+ * | |
261+ * ifNode |
262+ * | \ |
263+ * surviving LoopEnd -----------+
264+ * |
265+ * ...
266+ * </pre>
267+ *
268+ * If the only loop end is on the non-surviving successor path, then
269+ * killing that CFG path will also kill the LoopBegin, i.e., the pred
270+ * node. We would lose our place in the graph for inserting the guard.
271+ * Therefore, insert the guard before killing anything.
272+ */
273+ pred .setNext (guard );
274+ guard .setNext (ifNode );
254275
276+ AbstractBeginNode survivingSuccessor ;
255277 if (negateGuardCondition ) {
256278 survivingSuccessor = ifNode .falseSuccessor ();
257279 } else {
@@ -272,9 +294,6 @@ public static void propagateFixed(FixedNode from, StaticDeoptimizingNode deopt,
272294 survivingSuccessor .replaceAtUsages (newGuard , InputType .Guard );
273295 }
274296 graph .getOptimizationLog ().report (ConvertDeoptimizeToGuardPhase .class , "DeoptimizeToGuardConversion" , deopt .asNode ());
275- FixedNode next = pred .next ();
276- pred .setNext (guard );
277- guard .setNext (next );
278297 assert providers != null ;
279298 SimplifierTool simplifierTool = GraphUtil .getDefaultSimplifier (providers , false , graph .getAssumptions (), graph .getOptions ());
280299 if (survivingSuccessor .isAlive ()) {
0 commit comments