Skip to content

Commit 0da527b

Browse files
committed
[GR-62435] Expand fixed guards during GuardLowering
PullRequest: graal/20118
2 parents 676ed13 + 38aa1a3 commit 0da527b

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/GuardLoweringPhase.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 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
@@ -33,6 +33,7 @@
3333
import jdk.graal.compiler.nodes.AbstractBeginNode;
3434
import jdk.graal.compiler.nodes.BeginNode;
3535
import jdk.graal.compiler.nodes.DeoptimizeNode;
36+
import jdk.graal.compiler.nodes.FixedGuardNode;
3637
import jdk.graal.compiler.nodes.FixedWithNextNode;
3738
import jdk.graal.compiler.nodes.GraphState;
3839
import jdk.graal.compiler.nodes.GraphState.GuardsStage;
@@ -50,12 +51,14 @@
5051
import jdk.graal.compiler.phases.schedule.SchedulePhase;
5152

5253
/**
53-
* This phase lowers {@link GuardNode GuardNodes} into corresponding control-flow structure and
54-
* {@link DeoptimizeNode DeoptimizeNodes}.
54+
* This phase lowers {@link GuardNode floating GuardNodes} and {@link FixedGuardNode}s into
55+
* corresponding control-flow structure and {@link DeoptimizeNode DeoptimizeNodes}.
56+
* <p/>
57+
*
58+
* This allows compilation to enter the {@link GuardsStage#FIXED_DEOPTS FIXED_DEOPTS} stage of the
59+
* graph where all nodes that may cause deoptimization are fixed.
60+
* <p/>
5561
*
56-
* This allow to enter the {@link GuardsStage#FIXED_DEOPTS FIXED_DEOPTS} stage of the graph where
57-
* all node that may cause deoptimization are fixed.
58-
* <p>
5962
* It first makes a schedule in order to know where the control flow should be placed. Then, for
6063
* each block, it applies two passes. The first one tries to replace null-check guards with implicit
6164
* null checks performed by access to the objects that need to be null checked. The second phase
@@ -113,23 +116,44 @@ private void lowerToIf(GuardNode guard) {
113116
}
114117
}
115118

119+
@Override
120+
public boolean shouldApply(StructuredGraph graph) {
121+
return graph.hasNode(GuardNode.TYPE) || graph.hasNode(FixedGuardNode.TYPE);
122+
}
123+
116124
@Override
117125
public Optional<NotApplicable> notApplicableTo(GraphState graphState) {
118126
return NotApplicable.ifAny(
119127
NotApplicable.ifApplied(this, StageFlag.GUARD_LOWERING, graphState),
120128
NotApplicable.when(!graphState.getGuardsStage().allowsFloatingGuards(), "Floating guards must be allowed"));
121129
}
122130

123-
@Override
124-
protected void run(StructuredGraph graph, CoreProviders context) {
131+
private static void lowerFloatingGuards(StructuredGraph graph) {
125132
SchedulePhase.runWithoutContextOptimizations(graph, SchedulePhase.SchedulingStrategy.EARLIEST_WITH_GUARD_ORDER);
126133
ScheduleResult schedule = graph.getLastSchedule();
127134

128135
for (HIRBlock block : schedule.getCFG().getBlocks()) {
129136
processBlock(block, schedule);
130137
}
138+
}
131139

132-
assert assertNoGuardsLeft(graph);
140+
private static void lowerFixedGuards(StructuredGraph graph) {
141+
for (FixedGuardNode fixedGuard : graph.getNodes(FixedGuardNode.TYPE)) {
142+
fixedGuard.lowerToIf();
143+
}
144+
}
145+
146+
@Override
147+
protected void run(StructuredGraph graph, CoreProviders context) {
148+
if (graph.hasNode(GuardNode.TYPE)) {
149+
lowerFloatingGuards(graph);
150+
}
151+
if (graph.hasNode(FixedGuardNode.TYPE)) {
152+
lowerFixedGuards(graph);
153+
}
154+
155+
assert graph.getNodes(GuardNode.TYPE).isEmpty() : "no floating guards must be left after guard lowering";
156+
assert graph.getNodes(FixedGuardNode.TYPE).isEmpty() : "no fixed guards must be left after guard lowering";
133157
}
134158

135159
@Override
@@ -139,11 +163,6 @@ public void updateGraphState(GraphState graphState) {
139163
graphState.setGuardsStage(GuardsStage.FIXED_DEOPTS);
140164
}
141165

142-
private static boolean assertNoGuardsLeft(StructuredGraph graph) {
143-
assert graph.getNodes(GuardNode.TYPE).isEmpty();
144-
return true;
145-
}
146-
147166
private static void processBlock(HIRBlock block, ScheduleResult schedule) {
148167
DebugContext debug = block.getBeginNode().getDebug();
149168
new LowerGuards(debug.isDumpEnabledForMethod() || debug.isLogEnabledForMethod(), schedule).processNodes(block);

0 commit comments

Comments
 (0)