llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[DFAJumpThreading] Wrong code at -O2

Open XChy opened this issue 7 months ago • 5 comments

Reproducer: https://godbolt.org/z/dTsnrG6ds

#include <stdint.h>
#include <stdio.h>
static _Bool g5, g6, g15;
int16_t g10, __chk, g12;
void f4(int64_t a1)
{
    int16_t v3;
    int64_t v6;
    int16_t v13;
lbl_entry:
    v6 = 0;
lbl_b1:
    if (g15) switch (v6)
        {
            case 10894909752998623: goto lbl_sw6;
            default: goto lbl_sw_def7;
        }
lbl_bf3:
    if (g5) goto lbl_bf15;
    v3 = ~g10;
    v6 = a1;
    switch (v3)
    {
        case 5009: goto lbl_sw16;
        case 3: goto lbl_b1;
        case 6: goto lbl_br10;
    }
lbl_sw6:
    goto lbl_sw22;
lbl_sw_def7:
    g10 = 0;
    g6 = v3 = -24393 ^ 19673;
lbl_br10:
    if (g15) goto lbl_br12;
    goto lbl_entry;
lbl_br12:
    g10 = v3;
    if (g6) goto lbl_bf3;
lbl_bf15:
lbl_sw16:
    v13 = g10;
lbl_sw22:
    g12 = v13 | 9;
}

int main()
{
    g15 = 1;
    f4(0);
    __chk = g12;
    printf("checksum=0x%016llx\n", (unsigned long long)__chk);
}

Output at -O2:

checksum=0x0000000000000009

Output at -O0:

checksum=0xffffffffffffec6f

XChy avatar Apr 30 '26 14:04 XChy

Further reduced with llubi and llvm-reduce:

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@g12 = global i16 0
@.str = constant [20 x i8] c"checksum=0x%016llx\0A\00"

define void @f4() {
entry:
  br label %lbl_entry

lbl_entry:                                        ; preds = %lbl_br10.peel, %entry
  %g10.promoted26 = phi i16 [ 0, %entry ], [ %g10.promoted23.peel, %lbl_br10.peel ]
  br i1 true, label %lbl_sw_def7.peel, label %if.end4.peel

lbl_sw_def7.peel:                                 ; preds = %lbl_entry
  br label %lbl_br10.peel

lbl_br10.peel:                                    ; preds = %if.end4.peel, %lbl_sw_def7.peel
  %g10.promoted23.peel = phi i16 [ 0, %lbl_sw_def7.peel ], [ 0, %if.end4.peel ]
  %v3.0.peel = phi i16 [ -5010, %lbl_sw_def7.peel ], [ 0, %if.end4.peel ]
  br i1 true, label %if.end4.peel, label %lbl_entry

if.end4.peel:                                     ; preds = %lbl_br10.peel, %lbl_entry
  %g10.promoted24.peel = phi i16 [ %g10.promoted26, %lbl_entry ], [ %v3.0.peel, %lbl_br10.peel ]
  switch i16 %g10.promoted24.peel, label %lbl_sw22.loopexit.loopexit33split [
    i16 -5010, label %lbl_sw16
    i16 -7, label %lbl_br10.peel
  ]

lbl_sw16:                                         ; preds = %if.end4.peel
  store i16 1, ptr @g12, align 2
  ret void

lbl_sw22.loopexit.loopexit33split:                ; preds = %if.end4.peel
  ret void
}

define i32 @main() {
entry:
  call void @f4()
  %0 = load i16, ptr @g12, align 2
  %conv = sext i16 %0 to i64
  %call = call i32 (ptr, ...) @printf(ptr @.str, i64 %conv)
  ret i32 0
}

declare i32 @printf(ptr, ...)

Reproducible with --passes=dfa-jump-threading.

XChy avatar May 02 '26 11:05 XChy

Clean up further:

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@.str = constant [20 x i8] c"checksum=0x%016llx\0A\00"

define i64 @f4() {
entry:
  br label %lbl_entry

lbl_entry:                                        ; preds = %case2, %entry
  %phi3 = phi i16 [ 0, %entry ], [ %phi1, %case2 ]
  br i1 true, label %then, label %switch_bb

then:                                 ; preds = %lbl_entry
  br label %case2

case2:                                    ; preds = %switch_bb, %then
  %phi1 = phi i16 [ 0, %then ], [ 0, %switch_bb ]
  %phi2 = phi i16 [ 1, %then ], [ 0, %switch_bb ]
  br i1 true, label %switch_bb, label %lbl_entry

switch_bb:                                     ; preds = %case2, %lbl_entry
  %phi_of_switch = phi i16 [ %phi3, %lbl_entry ], [ %phi2, %case2 ]
  switch i16 %phi_of_switch, label %default_bb [
    i16 1, label %case1
    i16 2, label %case2
  ]

case1:                                         ; preds = %switch_bb
  ret i64 1

default_bb:                ; preds = %switch_bb
  ret i64 0
}

define i32 @main() {
entry:
  %val = call i64 @f4()
  %call = call i32 (ptr, ...) @printf(ptr @.str, i64 %val)
  ret i32 0
}

declare i32 @printf(ptr, ...)

XChy avatar May 02 '26 11:05 XChy

@XChy Hi, can I be assigned with this issue?

VatsalKhosla avatar May 03 '26 07:05 VatsalKhosla

@VatsalKhosla, sorry, I have been working on this. I will notify you if any new DFAJumpThreading bug is found.

XChy avatar May 03 '26 08:05 XChy

@XChy ahh, no problem!

VatsalKhosla avatar May 03 '26 12:05 VatsalKhosla