Skip to content

Commit dae4c49

Browse files
committed
8286197: C2: Optimize MemorySegment shape in int loop
Reviewed-by: kvn, thartmann
1 parent 94b473e commit dae4c49

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

src/hotspot/share/opto/castnode.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,34 @@ void CastIINode::dump_spec(outputStream* st) const {
371371
}
372372
#endif
373373

374+
Node* CastLLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
375+
Node* progress = ConstraintCastNode::Ideal(phase, can_reshape);
376+
if (progress != NULL) {
377+
return progress;
378+
}
379+
// transform (CastLL (ConvI2L ..)) into (ConvI2L (CastII ..)) if the type of the CastLL is narrower than the type of
380+
// the ConvI2L.
381+
Node* in1 = in(1);
382+
if (in1 != NULL && in1->Opcode() == Op_ConvI2L) {
383+
const Type* t = Value(phase);
384+
const Type* t_in = phase->type(in1);
385+
if (t != Type::TOP && t_in != Type::TOP) {
386+
const TypeLong* tl = t->is_long();
387+
const TypeLong* t_in_l = t_in->is_long();
388+
assert(tl->_lo >= t_in_l->_lo && tl->_hi <= t_in_l->_hi, "CastLL type should be narrower than or equal to the type of its input");
389+
assert((tl != t_in_l) == (tl->_lo > t_in_l->_lo || tl->_hi < t_in_l->_hi), "if type differs then this nodes's type must be narrower");
390+
if (tl != t_in_l) {
391+
const TypeInt* ti = TypeInt::make(checked_cast<jint>(tl->_lo), checked_cast<jint>(tl->_hi), tl->_widen);
392+
Node* castii = phase->transform(new CastIINode(in(0), in1->in(1), ti));
393+
Node* convi2l = in1->clone();
394+
convi2l->set_req(1, castii);
395+
return convi2l;
396+
}
397+
}
398+
}
399+
return NULL;
400+
}
401+
374402
//=============================================================================
375403
//------------------------------Identity---------------------------------------
376404
// If input is already higher or equal to cast type, then this is an identity.

src/hotspot/share/opto/castnode.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class CastLLNode: public ConstraintCastNode {
118118
init_class_id(Class_CastLL);
119119
}
120120

121+
virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
121122
virtual int Opcode() const;
122123
virtual uint ideal_reg() const { return Op_RegL; }
123124
};

src/hotspot/share/opto/loopopts.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,11 @@ Node *PhaseIdealLoop::split_if_with_blocks_pre( Node *n ) {
10771077
(n_blk->is_LongCountedLoop() && n->Opcode() == Op_AddL)) {
10781078
return n;
10791079
}
1080+
// Pushing a shift through the iv Phi can get in the way of addressing optimizations or range check elimination
1081+
if (n_blk->is_BaseCountedLoop() && n->Opcode() == Op_LShift(n_blk->as_BaseCountedLoop()->bt()) &&
1082+
n->in(1) == n_blk->as_BaseCountedLoop()->phi()) {
1083+
return n;
1084+
}
10801085

10811086
// Check for having no control input; not pinned. Allow
10821087
// dominating control.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (c) 2022, Red Hat, Inc. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package compiler.c2.irTests;
25+
26+
import compiler.lib.ir_framework.*;
27+
import jdk.test.lib.Utils;
28+
import jdk.internal.misc.Unsafe;
29+
import java.util.Objects;
30+
import java.util.Random;
31+
32+
/*
33+
* @test
34+
* @bug 8286197
35+
* @key randomness
36+
* @summary C2: Optimize MemorySegment shape in int loop
37+
* @modules java.base/jdk.internal.misc
38+
* @library /test/lib /
39+
* @run driver compiler.c2.irTests.TestConvI2LCastLongLoop
40+
*/
41+
42+
public class TestConvI2LCastLongLoop {
43+
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
44+
private static final Random RANDOM = Utils.getRandomInstance();
45+
46+
public static void main(String[] args) {
47+
TestFramework.runWithFlags("--add-modules", "java.base", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-XX:LoopMaxUnroll=0", "-XX:-UseCountedLoopSafepoints");
48+
}
49+
50+
static int size = 1024;
51+
static long base = UNSAFE.allocateMemory(size * 4);
52+
53+
@Test
54+
@IR(failOn = { IRNode.CAST_LL })
55+
public static int test1() {
56+
// Make sure enough round of loop opts are executed
57+
for (int i = 0; i < 10; i++) {
58+
for (int j = 0; j < 10; j++) {
59+
for (int k = 0; k < 10; k++) {
60+
}
61+
}
62+
}
63+
int v = 0;
64+
// In order to optimize the range check the loop is
65+
// transformed to:
66+
//
67+
// for (int i1;;) {
68+
// for (int i2;;) {
69+
// long j = (i1 + i2) * UNSAFE.ARRAY_INT_INDEX_SCALE; // (i1 + i2) << 2
70+
// v += UNSAFE.getInt(base + j);
71+
// }
72+
// }
73+
//
74+
// (i1 + i2) << 2 is transformed to (i1 << 2) + (i2 << 2)
75+
// because i1 is loop invariant in the inner loop.
76+
//
77+
// long j = ... really is (CastLL (Convi2L ...))
78+
//
79+
// With that transformed into (ConvI2L (CastII ...)), The AddL
80+
// (i1 << 2) + (i2 << 2) can be pushed through the CastII and
81+
// ConvI2L.
82+
// The address of the getInt is then:
83+
// (AddP base (AddL I V)) with I, loop invariant and V loop invariant
84+
// which can be transformed into:
85+
// (AddP (AddP base I) V)
86+
// The inner AddP is computed out of loop
87+
for (int i = 0; i < size; i++) {
88+
long j = i * UNSAFE.ARRAY_INT_INDEX_SCALE;
89+
90+
j = Objects.checkIndex(j, size * 4);
91+
92+
if (((base + j) & 3) != 0) {
93+
throw new RuntimeException();
94+
}
95+
96+
v += UNSAFE.getInt(base + j);
97+
}
98+
return v;
99+
}
100+
101+
@Test
102+
@IR(counts = { IRNode.CAST_II, ">=1", IRNode.CONV_I2L, ">=1" })
103+
@IR(failOn = { IRNode.CAST_LL })
104+
public static long test2(int i) {
105+
// Convert (CastLL (ConvI2L ...)) into (ConvI2L (CastII ...))
106+
long j = i * UNSAFE.ARRAY_INT_INDEX_SCALE;
107+
j = Objects.checkIndex(j, size * 4);
108+
return j;
109+
}
110+
111+
@Run(test = "test2")
112+
public static void test2_runner() {
113+
int i = RANDOM.nextInt(size);
114+
long res = test2(i);
115+
if (res != i * UNSAFE.ARRAY_INT_INDEX_SCALE) {
116+
throw new RuntimeException("incorrect result: " + res);
117+
}
118+
}
119+
120+
@Test
121+
@IR(counts = { IRNode.PHI, "2" })
122+
public static int test3() {
123+
int v = 0;
124+
// splif if should not push LshiftI through the iv Phi
125+
for (int i = 0; i < 1024; i++) {
126+
v += i * UNSAFE.ARRAY_INT_INDEX_SCALE;
127+
}
128+
return v;
129+
}
130+
}

test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ public class IRNode {
181181
public static final String CON_L = START + "ConL" + MID + END;
182182
public static final String CONV_I2L = START + "ConvI2L" + MID + END;
183183
public static final String CONV_L2I = START + "ConvL2I" + MID + END;
184+
public static final String CAST_II = START + "CastII" + MID + END;
185+
public static final String CAST_LL = START + "CastLL" + MID + END;
184186
public static final String POPCOUNT_L = START + "PopCountL" + MID + END;
187+
public static final String PHI = START + "Phi" + MID + END;
185188

186189
public static final String VECTOR_CAST_B2X = START + "VectorCastB2X" + MID + END;
187190
public static final String VECTOR_CAST_S2X = START + "VectorCastS2X" + MID + END;

0 commit comments

Comments
 (0)