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
@@ -243,8 +243,30 @@ public static void propagateFixed(FixedNode from, StaticDeoptimizingNode deopt,
243243 FixedGuardNode guard = graph .add (
244244 new FixedGuardNode (conditionNode , deopt .getReason (), deopt .getAction (), deopt .getSpeculation (), negateGuardCondition , survivingSuccessorPosition ));
245245 FixedWithNextNode pred = (FixedWithNextNode ) ifNode .predecessor ();
246- AbstractBeginNode survivingSuccessor ;
246+ /**
247+ * We may have the following special case if pred is a LoopBegin:
248+ *
249+ * <pre>
250+ * LoopBegin (= pred) <--------+
251+ * | |
252+ * | <- guard insert pos |
253+ * | |
254+ * ifNode |
255+ * | \ |
256+ * surviving LoopEnd -----------+
257+ * |
258+ * ...
259+ * </pre>
260+ *
261+ * If the only loop end is on the non-surviving successor path, then
262+ * killing that CFG path will also kill the LoopBegin, i.e., the pred
263+ * node. We would lose our place in the graph for inserting the guard.
264+ * Therefore, insert the guard before killing anything.
265+ */
266+ pred .setNext (guard );
267+ guard .setNext (ifNode );
247268
269+ AbstractBeginNode survivingSuccessor ;
248270 if (negateGuardCondition ) {
249271 survivingSuccessor = ifNode .falseSuccessor ();
250272 } else {
@@ -265,9 +287,6 @@ public static void propagateFixed(FixedNode from, StaticDeoptimizingNode deopt,
265287 survivingSuccessor .replaceAtUsages (newGuard , InputType .Guard );
266288 }
267289 graph .getOptimizationLog ().report (ConvertDeoptimizeToGuardPhase .class , "DeoptimizeToGuardConversion" , deopt .asNode ());
268- FixedNode next = pred .next ();
269- pred .setNext (guard );
270- guard .setNext (next );
271290 assert providers != null ;
272291 SimplifierTool simplifierTool = GraphUtil .getDefaultSimplifier (providers , false , graph .getAssumptions (), graph .getOptions ());
273292 if (survivingSuccessor .isAlive ()) {
0 commit comments