[llvm-bugs] [Bug 49689] New: [InstCombiner] WRONG code

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Mar 22 17:42:01 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=49689

            Bug ID: 49689
           Summary: [InstCombiner] WRONG code
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: paulsson at linux.vnet.ibm.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 24683
  --> https://bugs.llvm.org/attachment.cgi?id=24683&action=edit
reduced testcase

This program:

struct a {
  int b;
} d;
int c = 3, e;
char f;
void g(int j);
struct a h() {
  return d;
}
void i() { g(0); }
void g(int j) {
  e = 27;
  for (; e != 3; e = e - 4) {
    int *k = &c;
    {
      long l;
      if (*k)     // == 3                                                       
        *k |= j;  // j == 0 => k = 3                                            
      else
        break;
      f = 0;
      for (; f;)
        h();
    }
  }  // should exit when e == 3                                                 
}
int main() {
  i();
  printf("%d\n", e);
}

should print '3', which it does with

clang -O3 -march=arch13 wrong0.i  -o a.out -w; ./a.out

However, it prints '23' with

clang -O3 -march=arch13 wrong0.i  -o a.out -w -fno-inline  -mllvm
-bonus-inst-threshold=2

It looks like InstCombiner is incorrectly causing the loop to exit after first
iteration:

*** IR Dump After SimplifyCFGPass ***
; Function Attrs: nofree noinline nounwind
define dso_local void @g(i32 signext %j) local_unnamed_addr #3 {
entry:
  %tmp = alloca %struct.a, align 4
  store i32 27, i32* @e, align 4, !tbaa !2
  %.old = load i32, i32* @c, align 4, !tbaa !2
  %tobool.not.old = icmp eq i32 %.old, 0
  br i1 %tobool.not.old, label %for.end7, label %if.then

if.then:                                          ; preds = %if.then, %entry
  %or = or i32 %1, %j
  store i32 %or, i32* @c, align 4, !tbaa !2
  store i8 0, i8* @f, align 2, !tbaa !6
  %0 = load i32, i32* @e, align 4, !tbaa !2
  %sub = add nsw i32 %0, -4
  store i32 %sub, i32* @e, align 4, !tbaa !2
  %cmp.not = icmp eq i32 %sub, 3
  %1 = load i32, i32* @c, align 4
  %tobool.not = icmp eq i32 %1, 0
  %or.cond = or i1 %cmp.not, %tobool.not
  br i1 %or.cond, label %for.end7, label %if.then, !llvm.loop !7

for.end7:                                         ; preds = %entry, %if.then
  ret void
}
*** IR Dump After InstCombinePass ***
; Function Attrs: nofree noinline nounwind
define dso_local void @g(i32 signext %j) local_unnamed_addr #3 {
entry:
  store i32 27, i32* @e, align 4, !tbaa !2
  %.old = load i32, i32* @c, align 4, !tbaa !2
  %tobool.not.old = icmp eq i32 %.old, 0
  br i1 %tobool.not.old, label %for.end7, label %if.then

if.then:                                          ; preds = %if.then, %entry
  store i8 0, i8* @f, align 2, !tbaa !6
  %0 = load i32, i32* @e, align 4, !tbaa !2
  %sub = add nsw i32 %0, -4
  store i32 %sub, i32* @e, align 4, !tbaa !2
  br i1 true, label %for.end7, label %if.then, !llvm.loop !7

for.end7:                                         ; preds = %entry, %if.then
  ret void
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210323/bec8d06c/attachment.html>


More information about the llvm-bugs mailing list