Skip to content

Commit

Permalink
8251513: Code around Parse::do_lookupswitch/do_tableswitch should be …
Browse files Browse the repository at this point in the history
…cleaned up

Reviewed-by: roland, thartmann
  • Loading branch information
tobiasholenstein authored and TobiHartmann committed Oct 18, 2021
1 parent bb7dacd commit ebb1363
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
8 changes: 5 additions & 3 deletions src/hotspot/share/ci/ciStreams.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ class ciBytecodeStream : StackObj {
}

// For a lookup or switch table, return target destination
int get_int_table( int index ) const {
return Bytes::get_Java_u4((address)&_table_base[index]); }
jint get_int_table( int index ) const {
return (jint)Bytes::get_Java_u4((address)&_table_base[index]);
}

int get_dest_table( int index ) const {
return cur_bci() + get_int_table(index); }
return cur_bci() + get_int_table(index);
}

// --- Constant pool access ---
int get_constant_raw_index() const;
Expand Down
53 changes: 21 additions & 32 deletions src/hotspot/share/opto/parse2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ static void merge_ranges(SwitchRange* ranges, int& rp) {
void Parse::do_tableswitch() {
// Get information about tableswitch
int default_dest = iter().get_dest_table(0);
int lo_index = iter().get_int_table(1);
int hi_index = iter().get_int_table(2);
jint lo_index = iter().get_int_table(1);
jint hi_index = iter().get_int_table(2);
int len = hi_index - lo_index + 1;

if (len < 1) {
Expand All @@ -438,19 +438,19 @@ void Parse::do_tableswitch() {
SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
int rp = -1;
if (lo_index != min_jint) {
uint cnt = 1;
float cnt = 1.0F;
if (profile != NULL) {
cnt = profile->default_count() / (hi_index != max_jint ? 2 : 1);
cnt = (float)profile->default_count() / (hi_index != max_jint ? 2.0F : 1.0F);
}
ranges[++rp].setRange(min_jint, lo_index-1, default_dest, cnt);
}
for (int j = 0; j < len; j++) {
jint match_int = lo_index+j;
int dest = iter().get_dest_table(j+3);
makes_backward_branch |= (dest <= bci());
uint cnt = 1;
float cnt = 1.0F;
if (profile != NULL) {
cnt = profile->count_at(j);
cnt = (float)profile->count_at(j);
}
if (rp < 0 || !ranges[rp].adjoin(match_int, dest, cnt, trim_ranges)) {
ranges[++rp].set(match_int, dest, cnt);
Expand All @@ -459,9 +459,9 @@ void Parse::do_tableswitch() {
jint highest = lo_index+(len-1);
assert(ranges[rp].hi() == highest, "");
if (highest != max_jint) {
uint cnt = 1;
float cnt = 1.0F;
if (profile != NULL) {
cnt = profile->default_count() / (lo_index != min_jint ? 2 : 1);
cnt = (float)profile->default_count() / (lo_index != min_jint ? 2.0F : 1.0F);
}
if (!ranges[rp].adjoinRange(highest+1, max_jint, default_dest, cnt, trim_ranges)) {
ranges[++rp].setRange(highest+1, max_jint, default_dest, cnt);
Expand All @@ -487,7 +487,7 @@ void Parse::do_tableswitch() {
void Parse::do_lookupswitch() {
// Get information about lookupswitch
int default_dest = iter().get_dest_table(0);
int len = iter().get_int_table(1);
jint len = iter().get_int_table(1);

if (len < 1) { // If this is a backward branch, add safepoint
maybe_add_safepoint(default_dest);
Expand All @@ -513,26 +513,15 @@ void Parse::do_lookupswitch() {
table[3*j+0] = iter().get_int_table(2+2*j);
table[3*j+1] = iter().get_dest_table(2+2*j+1);
// Handle overflow when converting from uint to jint
table[3*j+2] = (profile == NULL) ? 1 : MIN2<uint>(max_jint, profile->count_at(j));
table[3*j+2] = (profile == NULL) ? 1 : (jint)MIN2<uint>((uint)max_jint, profile->count_at(j));
}
qsort(table, len, 3*sizeof(table[0]), jint_cmp);
}

float defaults = 0;
jint prev = min_jint;
for (int j = 0; j < len; j++) {
jint match_int = table[3*j+0];
if (match_int != prev) {
defaults += (float)match_int - prev;
}
prev = match_int+1;
}
if (prev != min_jint) {
defaults += (float)max_jint - prev + 1;
}
float default_cnt = 1;
float default_cnt = 1.0F;
if (profile != NULL) {
default_cnt = profile->default_count()/defaults;
juint defaults = max_juint - len;
default_cnt = (float)profile->default_count()/(float)defaults;
}

int rnum = len*2+1;
Expand All @@ -541,25 +530,25 @@ void Parse::do_lookupswitch() {
int rp = -1;
for (int j = 0; j < len; j++) {
jint match_int = table[3*j+0];
int dest = table[3*j+1];
int cnt = table[3*j+2];
int next_lo = rp < 0 ? min_jint : ranges[rp].hi()+1;
jint dest = table[3*j+1];
jint cnt = table[3*j+2];
jint next_lo = rp < 0 ? min_jint : ranges[rp].hi()+1;
makes_backward_branch |= (dest <= bci());
float c = default_cnt * ((float)match_int - next_lo);
float c = default_cnt * ((float)match_int - (float)next_lo);
if (match_int != next_lo && (rp < 0 || !ranges[rp].adjoinRange(next_lo, match_int-1, default_dest, c, trim_ranges))) {
assert(default_dest != never_reached, "sentinel value for dead destinations");
ranges[++rp].setRange(next_lo, match_int-1, default_dest, c);
}
if (rp < 0 || !ranges[rp].adjoin(match_int, dest, cnt, trim_ranges)) {
if (rp < 0 || !ranges[rp].adjoin(match_int, dest, (float)cnt, trim_ranges)) {
assert(dest != never_reached, "sentinel value for dead destinations");
ranges[++rp].set(match_int, dest, cnt);
ranges[++rp].set(match_int, dest, (float)cnt);
}
}
jint highest = table[3*(len-1)];
assert(ranges[rp].hi() == highest, "");
if (highest != max_jint &&
!ranges[rp].adjoinRange(highest+1, max_jint, default_dest, default_cnt * ((float)max_jint - highest), trim_ranges)) {
ranges[++rp].setRange(highest+1, max_jint, default_dest, default_cnt * ((float)max_jint - highest));
!ranges[rp].adjoinRange(highest+1, max_jint, default_dest, default_cnt * ((float)max_jint - (float)highest), trim_ranges)) {
ranges[++rp].setRange(highest+1, max_jint, default_dest, default_cnt * ((float)max_jint - (float)highest));
}
assert(rp < rnum, "not too many ranges");

Expand Down

1 comment on commit ebb1363

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.