Uninitialized register on Shift operation
| Bugzilla Link | 15607 |
| Resolution | DUPLICATE |
| Resolved on | Apr 10, 2013 03:26 |
| Version | 3.2 |
| OS | MacOS X |
| Reporter | LLVM Bugzilla Contributor |
| CC | @darkbuck |
Extended Description
Apple clang 4.1 (based on LLVM 3.1svn) seems to miscompile the following C program with optimize option -O1.
$ cat test.c
int x = 0; int main (void) { return ( 1 >> ( x << 8 ) ); /* should return 1 */ }
$ clang -O1 test.c $ ./a.out $ echo $? 0
Note that a.out may yield correct output (1) depending on environments. This seems to happen because register "%cl" on the "shrl" instruction is not initialized in the generated code.
pushq %rbp
Ltmp2: .cfi_def_cfa_offset 16 Ltmp3: .cfi_offset %rbp, -16 movq %rsp, %rbp Ltmp4: .cfi_def_cfa_register %rbp movl $1, %eax shrl %cl, %eax popq %rbp ret
The code may be valid for x != 0 (due to undefined behavior), but not for x == 0.
Confirmed that this is fixed in the development (SVN) version.
Intuition isn't relevant here. All that matters is how the LangRef defines the semantics of shift right operations. Here is what it says about lshr:
"This instruction always performs a logical shift right operation. The most significant bits of the result will be filled with zero bits after the shift. If op2 is (statically or dynamically) equal to or larger than the number of bits in op1, the result is undefined."
Intuitively, (1 >> x) should be 0 if x >= 32. The only case when (1 >> x) is equal to 1 when x >= 32 is where the processor only checks the lower N bits for the shift amount. So, defining (1 >> (x << 8) to be 1 when x != 0 has the assumption that the processor will only uses the lower 8 bits in the shift amount operand. That assumption may be true for most of processors but it breaks the target-independence of LLVM IR. In addition, there're always exceptional processors breaks that assumption.
This can be achieved at the IR level using the following reasoning:
a) x << 8 is either zero or >= 256 b) 1 >> (x << 8) is therefore shifting right either by 0 or by a number >= 256 c) the LangRef says that a shift by the bitsize of the integer or greater, in this case by 32 or more, results in an undefined value d) 1 >> (x << 8) is therefore either 1 >> 0 or undefined e) the undefined value can be chosen to be anything we like, for example 1 >> 0 f) thus we can always return 1 >> 0, i.e. 1
This reasoning would work the same for 1 >> (x << 5) too for example.
Only X86 (or other targets with i8 as shift amount) could do that as DAG combining knows the shift amount will be truncated into i8. Lower bits must be all 0s after left-shifting by 8.
As a target independent layer, IR at most knows that (x << 8) is a value of 0 or value no less than 256 (even this cannot hold for arbitrary x).
That test definitely should return 1. But, whether 1 is got at compile time or calculated at runtime is totally target dependant.
One outstanding question here is why the IR optimizers don't turn this function into "ret i32 1".
*** This bug has been marked as a duplicate of bug llvm/llvm-project#14934 ***
One thing I find surprising here is that the IR optimizers don't turn this into "ret 1".
The problem is also present in 3.2. For the development version, just check out and build the latest sources from the subversion repository.
We are on a 64bit platform.
The error was detected also on Ubuntu as well as MacOSX. There seems to be no problem with the bitcode that clang emits for this source code.
If it is fixed on the development version, when and on which version should we confirm it?
Thanks.
This seems to be fixed in the development version (tested on x86-64). Are you on a 32 bit or a 64 bit platform? Also, bug reports for Apple clang should be made to Apple.