Skip to content
6 changes: 3 additions & 3 deletions src/hotspot/share/jvmci/jvmciCompilerToVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,9 +1049,9 @@ C2V_END
C2V_VMENTRY_0(jlong, getMaxCallTargetOffset, (JNIEnv* env, jobject, jlong addr))
address target_addr = (address) addr;
if (target_addr != 0x0) {
int64_t off_low = (int64_t)target_addr - ((int64_t)CodeCache::low_bound() + sizeof(int));
int64_t off_high = (int64_t)target_addr - ((int64_t)CodeCache::high_bound() + sizeof(int));
return MAX2(ABS(off_low), ABS(off_high));
jlong off_low = (jlong)(target_addr - (CodeCache::low_bound() + sizeof(int)));
jlong off_high = (jlong)(target_addr - (CodeCache::high_bound() + sizeof(int)));
return checked_cast<jlong>(MAX2(uabs(off_low), uabs(off_high)));
}
return -1;
C2V_END
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/opto/divnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static bool magic_int_divide_constants(jint d, jint &M, jint &s) {
uint32_t ad, anc, delta, q1, r1, q2, r2, t;
const uint32_t two31 = 0x80000000L; // 2**31.

ad = ABS(d);
ad = uabs(d);
if (d == 0 || d == 1) return false;
t = two31 + ((uint32_t)d >> 31);
anc = t - 1 - t%ad; // Absolute value of nc.
Expand Down Expand Up @@ -222,7 +222,7 @@ static bool magic_long_divide_constants(jlong d, jlong &M, jint &s) {
uint64_t ad, anc, delta, q1, r1, q2, r2, t;
const uint64_t two63 = UCONST64(0x8000000000000000); // 2**63.

ad = ABS(d);
ad = uabs(d);
if (d == 0 || d == 1) return false;
t = two63 + ((uint64_t)d >> 63);
anc = t - 1 - t%ad; // Absolute value of nc.
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/opto/ifnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {
// the argument above still holds.
//
// Note that the same optimization with the same maximal accepted interval size can also be found in C1.
const jlong maximum_number_of_min_max_interval_indices = (jlong)max_jint;
const julong maximum_number_of_min_max_interval_indices = (julong)max_jint;

// The top 3 range checks seen
const int NRC = 3;
Expand Down Expand Up @@ -1975,7 +1975,7 @@ Node* RangeCheckNode::Ideal(PhaseGVN *phase, bool can_reshape) {

// "x - y" -> must add one to the difference for number of elements in [x,y]
const jlong diff = (jlong)MIN2(offset2, off_lo) - (jlong)MAX2(offset2, off_hi);
if (ABS(diff) < maximum_number_of_min_max_interval_indices) {
if (uabs(diff) < maximum_number_of_min_max_interval_indices) {
// Gather expanded bounds
off_lo = MIN2(off_lo, offset2);
off_hi = MAX2(off_hi, offset2);
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/loopPredicate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ bool PhaseIdealLoop::loop_predication_should_follow_branches(IdealLoopTree* loop
CountedLoopNode* cl = head->as_CountedLoop();
if (cl->phi() != nullptr) {
const TypeInt* t = _igvn.type(cl->phi())->is_int();
float worst_case_trip_cnt = ((float)t->_hi - t->_lo) / ABS(cl->stride_con());
float worst_case_trip_cnt = ((float)t->_hi - t->_lo) / uabs(cl->stride_con());
if (worst_case_trip_cnt < loop_trip_cnt) {
loop_trip_cnt = worst_case_trip_cnt;
}
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/loopTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2486,7 +2486,7 @@ void PhaseIdealLoop::add_constraint(jlong stride_con, jlong scale_con, Node* off
// If the absolute scale value is greater one, division in 'adjust_limit' may require
// rounding. Make sure the ABS method correctly handles min_jint.
// Only do this for the pre-loop, one less iteration of the main loop doesn't hurt.
bool round = ABS(scale_con) > 1;
bool round = uabs(scale_con) > 1;

Node* scale = _igvn.longcon(scale_con);
set_ctrl(scale, C->root());
Expand Down
18 changes: 8 additions & 10 deletions src/hotspot/share/opto/loopnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,14 +802,14 @@ bool PhaseIdealLoop::create_loop_nest(IdealLoopTree* loop, Node_List &old_new) {
// The number of iterations for the integer count loop: guarantee no
// overflow: max_jint - stride_con max. -1 so there's no need for a
// loop limit check if the exit test is <= or >=.
int iters_limit = max_jint - ABS(stride_con) - 1;
int iters_limit = max_jint - uabs(stride_con) - 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears iters_limit can become -1 or -2 here, depending on the value of stride_con. See below for the problem.

#ifdef ASSERT
if (bt == T_LONG && StressLongCountedLoop > 0) {
iters_limit = iters_limit / StressLongCountedLoop;
}
#endif
// At least 2 iterations so counted loop construction doesn't fail
if (iters_limit/ABS(stride_con) < 2) {
if (iters_limit/uabs(stride_con) < 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously negative values of iters_limit would cause us to return false here. Now the comparison is unsigned, so we can get a different result:
stride_con is max_jint
iters_limit is -1
(unsigned) -1 / max_jint is 2
so we no longer return false here.
Using uabs with signed values is error-prone.

return false;
}

Expand Down Expand Up @@ -1106,8 +1106,8 @@ int PhaseIdealLoop::extract_long_range_checks(const IdealLoopTree* loop, jlong s
jlong scale = 0;
if (loop->is_range_check_if(if_proj, this, T_LONG, phi, range, offset, scale) &&
loop->is_invariant(range) && loop->is_invariant(offset) &&
original_iters_limit / ABS(scale * stride_con) >= min_iters) {
reduced_iters_limit = MIN2(reduced_iters_limit, original_iters_limit/ABS(scale));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixing signed and unsigned can cause compiler warnings and a change in semantics. And again, checked_cast is not better than a raw cast here. I think it would be better to make min_iters, reduced_iters_limit, original_iters_limit all unsigned.

original_iters_limit / uabs(scale * stride_con) >= min_iters) {
reduced_iters_limit = MIN2(reduced_iters_limit, checked_cast<jlong>(original_iters_limit / uabs(scale)));
range_checks.push(c);
}
}
Expand Down Expand Up @@ -2341,7 +2341,7 @@ Node* PhaseIdealLoop::exact_limit( IdealLoopTree *loop ) {
CountedLoopNode *cl = loop->_head->as_CountedLoop();
assert(cl->is_valid_counted_loop(T_INT), "");

if (ABS(cl->stride_con()) == 1 ||
if (uabs(cl->stride_con()) == 1 ||
cl->limit()->Opcode() == Op_LoopLimit) {
// Old code has exact limit (it could be incorrect in case of int overflow).
// Loop limit is exact with stride == 1. And loop may already have exact limit.
Expand Down Expand Up @@ -2559,20 +2559,18 @@ Node *LoopLimitNode::Ideal(PhaseGVN *phase, bool can_reshape) {

const TypeInt* init_t = phase->type(in(Init) )->is_int();
const TypeInt* limit_t = phase->type(in(Limit))->is_int();
int stride_p;
jlong lim, ini;
julong max;
if (stride_con > 0) {
stride_p = stride_con;
lim = limit_t->_hi;
ini = init_t->_lo;
max = (julong)max_jint;
} else {
stride_p = -stride_con;
lim = init_t->_hi;
ini = limit_t->_lo;
max = (julong)min_jint;
}
uint stride_p = uabs(stride_con);
julong range = lim - ini + stride_p;
if (range <= max) {
// Convert to integer expression if it is not overflow.
Expand Down Expand Up @@ -2951,9 +2949,9 @@ void OuterStripMinedLoopNode::adjust_strip_mined_loop(PhaseIterGVN* igvn) {
CountedLoopEndNode* inner_cle = inner_cl->loopexit();

int stride = inner_cl->stride_con();
jlong scaled_iters_long = ((jlong)LoopStripMiningIter) * ABS(stride);
jlong scaled_iters_long = ((jlong)LoopStripMiningIter) * uabs(stride);
int scaled_iters = (int)scaled_iters_long;
int short_scaled_iters = LoopStripMiningIterShortLoop* ABS(stride);
int short_scaled_iters = LoopStripMiningIterShortLoop * uabs(stride);
const TypeInt* inner_iv_t = igvn->type(inner_iv_phi)->is_int();
jlong iter_estimate = (jlong)inner_iv_t->_hi - (jlong)inner_iv_t->_lo;
assert(iter_estimate > 0, "broken");
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/superword.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class SuperWord : public ResourceObj {

int vector_width(const Node* n) const {
BasicType bt = velt_basic_type(n);
return MIN2(ABS(iv_stride()), Matcher::max_vector_size(bt));
return MIN2(checked_cast<int>(uabs(iv_stride())), Matcher::max_vector_size(bt));
}
int vector_width_in_bytes(const Node* n) const {
BasicType bt = velt_basic_type(n);
Expand Down
2 changes: 0 additions & 2 deletions src/hotspot/share/utilities/globalDefinitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,8 +1109,6 @@ template<class T> constexpr T MIN3(T a, T b, T c) { return MIN2(MIN2(a, b),
template<class T> constexpr T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); }
template<class T> constexpr T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); }

template<class T> inline T ABS(T x) { return (x > 0) ? x : -x; }

// Return the given value clamped to the range [min ... max]
template<typename T>
inline T clamp(T value, T min, T max) {
Expand Down