Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8309266: C2: assert(final_con == (jlong)final_int) failed: final value should be integer #14353

wants to merge 3 commits into from


Copy link

@quadhier quadhier commented Jun 7, 2023

This patch should fix JDK-8309266. A jtreg test is also added. I'd appreciate any comments and reviews. Thanks in advance!

Problem Analysis

For the following program,

public class Test {

    static boolean flag;

    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            flag = !flag;

    public static void test() {
        int limit = flag ? Integer.MAX_VALUE : 1000;

        int i = 0;
        while (i < limit) {
            i += 3;
            if (flag) {

A LoopLimitNode will be generated and its Limit input is a PhiNode, as depicted in the following picture.


During PhaseCCP, the LoopLimitNode::Value() tries to calculate the constant final value:

if (init_t->is_int()->is_con() && limit_t->is_int()->is_con()) {
// Use jlongs to avoid integer overflow.
jlong init_con = init_t->is_int()->get_con();
jlong limit_con = limit_t->is_int()->get_con();
int stride_m = stride_con - (stride_con > 0 ? 1 : -1);
jlong trip_count = (limit_con - init_con + stride_m)/stride_con;
jlong final_con = init_con + stride_con*trip_count;
int final_int = (int)final_con;
// The final value should be in integer range since the loop
// is counted and the limit was checked for overflow.
assert(final_con == (jlong)final_int, "final value should be integer");
return TypeInt::make(final_int);

The problem is that the assertion in line 2299 could fail during CCP though it must hold true at the end of CCP. Here is the reason: PhaseCCP initializes all nodes with the type TOP and iterates in an "arbitrary" order. The following order may happen:

28 IfTrue => 34 Region => 36 Phi => 195 LoopLimit => ... => 29 IfFalse
  1. In ProjNode::Value() (IfTrue inherits it), the type of IfTrue is set to Type::CONTROL

    const Type* ProjNode::Value(PhaseGVN* phase) const {
    if (in(0) == nullptr) return Type::TOP;
    return proj_type(phase->type(in(0)));

  2. In PhiNode::Value(), only 28 IfTrue's correspondence 33 ConI gets merged (as 29 IfFalse has not been dealt with yet), then it has a value of int:max.

    // Default case: merge all inputs
    const Type *t = Type::TOP; // Merged type starting value
    for (uint i = 1; i < req(); ++i) {// For all paths in
    // Reachable control path?
    if (r->in(i) && phase->type(r->in(i)) == Type::CONTROL) {
    const Type* ti = phase->type(in(i));
    t = t->meet_speculative(ti);

  3. In LoopLimitNode::Value(), it finds its Limit input 36 Phi is constant, which triggers the assertion, and the assertion fails since the final value calculated from that constant limit (int:max) overflows.


Move the overflow check to the end of CCP, where it must not fail.


  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue


  • JDK-8309266: C2: assert(final_con == (jlong)final_int) failed: final value should be integer (Bug - "3")


Using git

Checkout this PR locally:
$ git fetch pull/14353/head:pull/14353
$ git checkout pull/14353

Update a local copy of the PR:
$ git checkout pull/14353
$ git pull pull/14353/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 14353

View PR using the GUI difftool:
$ git pr show -t 14353

Using diff file

Download this PR as a diff file:


Link to Webrev Comment

Copy link

bridgekeeper bot commented Jun 7, 2023

👋 Welcome back quadhier! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 7, 2023
Copy link

openjdk bot commented Jun 7, 2023

@quadhier The following label will be automatically applied to this pull request:

  • hotspot-compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot-compiler label Jun 7, 2023
Copy link

mlbridge bot commented Jun 7, 2023


Copy link

eme64 commented Jun 7, 2023

@quadhier Thanks for looking into this! This but is currently not assigned to you. Please always make sure that you have it assigned to you, or at least mention in JIRA that you are working on it. Currently, @enothum had it assigned and was also working on it.

The regression test cleanly reproduces before the patch, good.

Why is the overflow acceptable? Does that not mean that the calculation did something wrong?

In the example, we have

init_con = 3
limit_con = 2147483647 = max_jint
stride_con = 3
stride_m = stride_con - 1 = 2
trip_count = (limit_con - init_con + stride_m)/stride_con = 715827882
final_con = init_con + stride_con*trip_count = 2147483649 = max_jint + 2 (overflow!)
final_int = -2147483647 (overflow!)

Does that not mean that we mis-calculated the trip_count? If it was 1 less, we would not have an overflow. Would that not fix the issue in a simpler way? Or did I get something wrong?

Let's expand the formula:

final_con = init_con + stride_con*trip_count
final_con = init_con + stride_con * ((limit_con - init_con + stride_m) / stride_con)
final_con = init_con + stride_con * ((limit_con - init_con + stride_con - 1) / stride_con)

Is the issue not that instead of coming up with a final value that is slightly below limit_con, we come up with one that is slightly above limit_con, and can thus overflow?

Would this be correct instead (for positive stride)?

final_con = init_con + stride_con * ((limit_con - init_con + 1 - stride_con) / stride_con)

Could the limit type ever overflow at runtime? Does the loop limit check not prevent that (unsure)?

Can you explain again why exactly we calculate what we calculate here, and why that is correct?

Copy link
Contributor Author

quadhier commented Jun 7, 2023

Please always make sure that you have it assigned to you, or at least mention in JIRA that you are working on it.

Hi, @eme64 , I'm so sorry for that! I don't have a JBS account yet, but from now on I will avoid to work on assigned issues and try to get an account ASAP.

Why is the overflow acceptable? Does that not mean that the calculation did something wrong?

If my understanding is correct, the final value should be i's value at loop exit. If limit is max_jint, the final value should be max_jint + 2. So the calculation is not wrong but is an intermediate result. We need to check LoopLimitNode::Value() at end of CCP to ensure this calculation doesn't overflow.

BTW, The LoopLimitNode is generated and its check happens because it believe that the code don't always overflow and overflow case will be handled by a uncommon_trap, which is done by the following code (If check_stride_overflow() returns -1, the overflow will always happen).

int sov = check_stride_overflow(stride_m, limit_t, iv_bt);
// If sov==0, limit's type always satisfies the condition, for
// example, when it is an array length.
if (sov != 0) {
if (sov < 0) {
return false; // Bailout: integer overflow is certain.
assert(!x->as_Loop()->is_loop_nest_inner_loop(), "loop was transformed");
// Generate loop's limit check.
// Loop limit check predicate should be near the loop.
if (!ParsePredicates::is_loop_limit_check_predicate_proj(init_control)) {
// The limit check predicate is not generated if this method trapped here before.
#ifdef ASSERT
if (TraceLoopLimitCheck) {
tty->print("missing loop limit check:");
return false;
ParsePredicateSuccessProj* loop_limit_check_parse_predicate_proj = init_control->as_IfTrue();
ParsePredicateNode* parse_predicate = loop_limit_check_parse_predicate_proj->in(0)->as_ParsePredicate();

Copy link
Contributor Author

quadhier commented Jun 8, 2023

This patch causes many compilation timeouts. I'd close this PR and wait for a better fix. Thanks for your review and suggestion @eme64 !

@quadhier quadhier closed this Jun 8, 2023
Copy link

eme64 commented Jun 8, 2023

@quadhier ok, good luck!

Copy link

eme64 commented Jun 8, 2023

@quadhier So you intend to keep working on this?

Copy link
Contributor Author

quadhier commented Jun 8, 2023

@eme64 Sorry, I won't work on this bug.

Copy link

eme64 commented Jun 8, 2023

@quadhier I hope I did not discourage you, my feedback yesterday was a bit scattered and maybe overwhealming, I'm sorry for that.
These things are not easy to get right. I was impressed how far you got!
Let me know if you want to take this back up, or want another task to work on - though a JBS account would help ;)

Copy link
Contributor Author

quadhier commented Jun 8, 2023

@eme64 That's alright! I appreciate your telling me about some disciplines not written in the contributor guides. :P

After some explorations, I realized that I didn't fully understand the root cause of this bug. Since I use my spare time to contribute, I think it would be better for some other experts to work on this so that we don't have to wait too long. I'm applying for an Author role and hope then I could continue contributing. Many thanks for your kindness again! :D

Copy link

eme64 commented Jun 8, 2023

@quadhier Ok, sounds good. Looking forward to your future PR's! 😊

Copy link

@quadhier Please let me know if there is anything you think is missing from the OpenJDK Developers' Guide, I'd be happy to work on any improvements.

Since you mention the guide, I assume that you have read and in particular

Copy link
Contributor Author

Thanks, @JesperIRL. The guide is good and thoughtful. But it seems that nowadays the paradigm has slightly changed
— developers create issues in JBS, and someone picks up an issue and creates a PR in GitHub. Most of the communications happen in GitHub comments.

It would be better to emphasize that before working on an issue, you should make sure that it is assigned to you, or should mention in JBS or mailing list that you are working on it.

BTW, maybe for an unassigned issue, we could create a PR directly? Since we can communicate in the GitHub comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
hotspot-compiler rfr Pull request is ready for review
3 participants