Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

wrong code bug #12653

Closed Quuxplusone closed 12 years ago

Quuxplusone commented 12 years ago
Bugzilla Link PR12541
Status RESOLVED FIXED
Importance P enhancement
Reported by John Regehr (regehr@cs.utah.edu)
Reported on 2012-04-12 19:00:17 -0700
Last modified on 2012-04-30 06:57:33 -0700
Version trunk
Hardware PC Linux
CC baldrick@free.fr, chenyang@cs.utah.edu, clattner@nondot.org, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
I have a few wrong-code bug reports in the system already but this one has such
a tiny test case it seems to need reporting as well.

[regehr@dyson r26]$ gcc small.c ; ./a.out
1
[regehr@dyson r26]$ clang small.c ; ./a.out
1
[regehr@dyson r26]$ clang -O small.c ; ./a.out
0
[regehr@dyson r26]$ cat small.c
int printf (const char *, ...);
int a, b;
int
main ()
{
    a = (char) (1656690544 % (b ^ 3));
    printf ("%d\n", a);
    return 0;
}
[regehr@dyson r26]$ clang -v
clang version 3.1 (trunk 154589)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Quuxplusone commented 12 years ago

What -S output are you getting? I'm getting "1" on mac x86-64 at -O. Please include the output of "clang -O small.c -S -o -" thanks John!

Quuxplusone commented 12 years ago
Hi Chris, see bc and x64 below.

I wonder if there's anything different about my configuration?  For example,
I'm not using compiler-rt, would that matter here?

I'm surprised that the math is not constant-folded.  Does clang not have an
equivalent to -fwhole-program?

These are from clang/LLVM r154641.

[regehr@dyson r26]$ clang -O -emit-llvm small.c -S -o -
; ModuleID = 'small.c'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-
f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@b = common global i32 0, align 4
@a = common global i32 0, align 4
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1

define i32 @main() nounwind uwtable {
entry:
  %0 = load i32* @b, align 4, !tbaa !0
  %xor = xor i32 %0, 3
  %rem = srem i32 1656690544, %xor
  %sext = and i32 %rem, 112
  store i32 %sext, i32* @a, align 4, !tbaa !0
  %call = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), i32 %sext) nounwind
  ret i32 0
}

declare i32 @printf(i8* nocapture, ...) nounwind

!0 = metadata !{metadata !"int", metadata !1}
!1 = metadata !{metadata !"omnipotent char", metadata !2}
!2 = metadata !{metadata !"Simple C/C++ TBAA", null}

[regehr@dyson r26]$ clang -O small.c -S -o -
    .file   "small.c"
    .text
    .globl  main
    .align  16, 0x90
    .type   main,@function
main:                                   # @main
    .cfi_startproc
# BB#0:                                 # %entry
    pushq   %rax
.Ltmp1:
    .cfi_def_cfa_offset 16
    movl    b(%rip), %ecx
    xorl    $3, %ecx
    movl    $1656690544, %eax       # imm = 0x62BF1770
    xorl    %edx, %edx
    idivl   %ecx
    andl    $112, %edx
    movl    %edx, a(%rip)
    movl    $.L.str, %edi
    movl    %edx, %esi
    xorb    %al, %al
    callq   printf
    xorl    %eax, %eax
    popq    %rdx
    ret
.Ltmp2:
    .size   main, .Ltmp2-main
    .cfi_endproc

    .type   b,@object               # @b
    .comm   b,4,4
    .type   a,@object               # @a
    .comm   a,4,4
    .type   .L.str,@object          # @.str
    .section    .rodata.str1.1,"aMS",@progbits,1
.L.str:
    .asciz   "%d\n"
    .size   .L.str, 4

    .section    ".note.GNU-stack","",@progbits
Quuxplusone commented 12 years ago
Fixed here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120430/141902.html