Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/hotspot/share/opto/rangeinference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class TypeIntHelper {
static const Type* int_type_xmeet(const CT* i1, const Type* t2);

template <class CTP>
static CTP int_type_union(CTP t1, CTP t2) {
static auto int_type_union(CTP t1, CTP t2) {
using CT = std::conditional_t<std::is_pointer_v<CTP>, std::remove_pointer_t<CTP>, CTP>;
using S = std::remove_const_t<decltype(CT::_lo)>;
using U = std::remove_const_t<decltype(CT::_ulo)>;
Expand Down Expand Up @@ -209,19 +209,23 @@ class TypeIntMirror {
KnownBits<U> _bits;
int _widen = 0; // dummy field to mimic the same field in TypeInt, useful in testing

static TypeIntMirror make(const TypeIntPrototype<S, U>& t, int widen) {
static TypeIntMirror make(const TypeIntPrototype<S, U>& t, int widen = 0) {
auto canonicalized_t = t.canonicalize_constraints();
assert(!canonicalized_t.empty(), "must not be empty");
return TypeIntMirror{canonicalized_t._data._srange._lo, canonicalized_t._data._srange._hi,
canonicalized_t._data._urange._lo, canonicalized_t._data._urange._hi,
canonicalized_t._data._bits};
}

TypeIntMirror meet(const TypeIntMirror& o) const {
return TypeIntHelper::int_type_union(this, &o);
}

// These allow TypeIntMirror to mimick the behaviors of TypeInt* and TypeLong*, so they can be
// passed into RangeInference methods. These are only used in testing, so they are implemented in
// the test file.
static TypeIntMirror make(const TypeIntMirror& t, int widen);
const TypeIntMirror* operator->() const;
TypeIntMirror meet(const TypeIntMirror& o) const;
bool contains(U u) const;
bool contains(const TypeIntMirror& o) const;
bool operator==(const TypeIntMirror& o) const;
Expand Down Expand Up @@ -322,18 +326,18 @@ class RangeInference {
// Infer a result given the input types of a binary operation
template <class CTP, class Inference>
static CTP infer_binary(CTP t1, CTP t2, Inference infer) {
CTP res;
TypeIntMirror<S<CTP>, U<CTP>> res;
bool is_init = false;

SimpleIntervalIterable<CTP> t1_simple_intervals(t1);
SimpleIntervalIterable<CTP> t2_simple_intervals(t2);

for (auto& st1 : t1_simple_intervals) {
for (auto& st2 : t2_simple_intervals) {
CTP current = infer(st1, st2);
TypeIntMirror<S<CTP>, U<CTP>> current = infer(st1, st2);

if (is_init) {
res = res->meet(current)->template cast<CT<CTP>>();
res = res.meet(current);
} else {
is_init = true;
res = current;
Expand All @@ -342,7 +346,7 @@ class RangeInference {
}

assert(is_init, "must be initialized");
return res;
return CT<CTP>::make(res, MAX2(t1->_widen, t2->_widen));
}

public:
Expand All @@ -357,7 +361,7 @@ class RangeInference {
U<CTP> uhi = MIN2(st1._uhi, st2._uhi);
U<CTP> zeros = st1._bits._zeros | st2._bits._zeros;
U<CTP> ones = st1._bits._ones & st2._bits._ones;
return CT<CTP>::make(TypeIntPrototype<S<CTP>, U<CTP>>{{lo, hi}, {ulo, uhi}, {zeros, ones}}, MAX2(t1->_widen, t2->_widen));
return TypeIntMirror<S<CTP>, U<CTP>>::make(TypeIntPrototype<S<CTP>, U<CTP>>{{lo, hi}, {ulo, uhi}, {zeros, ones}});
});
}

Expand All @@ -372,7 +376,7 @@ class RangeInference {
U<CTP> uhi = std::numeric_limits<U<CTP>>::max();
U<CTP> zeros = st1._bits._zeros & st2._bits._zeros;
U<CTP> ones = st1._bits._ones | st2._bits._ones;
return CT<CTP>::make(TypeIntPrototype<S<CTP>, U<CTP>>{{lo, hi}, {ulo, uhi}, {zeros, ones}}, MAX2(t1->_widen, t2->_widen));
return TypeIntMirror<S<CTP>, U<CTP>>::make(TypeIntPrototype<S<CTP>, U<CTP>>{{lo, hi}, {ulo, uhi}, {zeros, ones}});
});
}

Expand All @@ -385,7 +389,7 @@ class RangeInference {
U<CTP> uhi = std::numeric_limits<U<CTP>>::max();
U<CTP> zeros = (st1._bits._zeros & st2._bits._zeros) | (st1._bits._ones & st2._bits._ones);
U<CTP> ones = (st1._bits._zeros & st2._bits._ones) | (st1._bits._ones & st2._bits._zeros);
return CT<CTP>::make(TypeIntPrototype<S<CTP>, U<CTP>>{{lo, hi}, {ulo, uhi}, {zeros, ones}}, MAX2(t1->_widen, t2->_widen));
return TypeIntMirror<S<CTP>, U<CTP>>::make(TypeIntPrototype<S<CTP>, U<CTP>>{{lo, hi}, {ulo, uhi}, {zeros, ones}});
});
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/hotspot/share/opto/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,9 @@ class TypeInt : public TypeInteger {
static const TypeInt* make(jint lo, jint hi, int widen);
static const Type* make_or_top(const TypeIntPrototype<jint, juint>& t, int widen);
static const TypeInt* make(const TypeIntPrototype<jint, juint>& t, int widen) { return make_or_top(t, widen)->is_int(); }
static const TypeInt* make(const TypeIntMirror<jint, juint>& t, int widen) {
return (new TypeInt(TypeIntPrototype<jint, juint>{{t._lo, t._hi}, {t._ulo, t._uhi}, t._bits}, widen, false))->hashcons()->is_int();
}

// Check for single integer
bool is_con() const { return _lo == _hi; }
Expand Down Expand Up @@ -881,6 +884,9 @@ class TypeLong : public TypeInteger {
static const TypeLong* make(jlong lo, jlong hi, int widen);
static const Type* make_or_top(const TypeIntPrototype<jlong, julong>& t, int widen);
static const TypeLong* make(const TypeIntPrototype<jlong, julong>& t, int widen) { return make_or_top(t, widen)->is_long(); }
static const TypeLong* make(const TypeIntMirror<jlong, julong>& t, int widen) {
return (new TypeLong(TypeIntPrototype<jlong, julong>{{t._lo, t._hi}, {t._ulo, t._uhi}, t._bits}, widen, false))->hashcons()->is_long();
}

// Check for single integer
bool is_con() const { return _lo == _hi; }
Expand Down
8 changes: 4 additions & 4 deletions test/hotspot/gtest/opto/test_rangeinference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ TEST(opto, canonicalize_constraints) {

// Implementations of TypeIntMirror methods for testing purposes
template <class S, class U>
const TypeIntMirror<S, U>* TypeIntMirror<S, U>::operator->() const {
return this;
TypeIntMirror<S, U> TypeIntMirror<S, U>::make(const TypeIntMirror<S, U>& t, int widen) {
return t;
}

template <class S, class U>
TypeIntMirror<S, U> TypeIntMirror<S, U>::meet(const TypeIntMirror& o) const {
return TypeIntHelper::int_type_union(*this, o);
const TypeIntMirror<S, U>* TypeIntMirror<S, U>::operator->() const {
return this;
}

template <class S, class U>
Expand Down
54 changes: 54 additions & 0 deletions test/hotspot/jtreg/compiler/ccp/TestWrongXorIWiden.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2025, 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.
*/

package compiler.ccp;

/*
* @test
* @bug 8374180
* @summary Test that _widen is set correctly in XorI::add_ring() to ensure monotonicity.
* @run main/othervm -XX:CompileCommand=compileonly,${test.main.class}::* -Xcomp ${test.main.class}
*/
public class TestWrongXorIWiden {
static byte byFld;

public static void main(String[] strArr) {
test();
}

static void test() {
int k, i17 = 0;
long lArr[] = new long[400];
for (int i = 9; i < 54; ++i) {
for (int j = 7; j > 1; j--) {
for (k = 1; k < 2; k++) {
i17 >>= i;
}
byFld += j ^ i17;
for (int a = 1; a < 2; a++) {
i17 = k;
}
}
}
}
}