[llvm-bugs] [Bug 49884] New: Unexpected LCV type promotion in HardwareLoops
via llvm-bugs
llvm-bugs at lists.llvm.org
Wed Apr 7 12:51:57 PDT 2021
https://bugs.llvm.org/show_bug.cgi?id=49884
Bug ID: 49884
Summary: Unexpected LCV type promotion in HardwareLoops
Product: libraries
Version: trunk
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P
Component: Loop Optimizer
Assignee: unassignedbugs at nondot.org
Reporter: j-nagurne at ti.com
CC: llvm-bugs at lists.llvm.org
Created attachment 24731
--> https://bugs.llvm.org/attachment.cgi?id=24731&action=edit
Test file exhibiting the unexpected promotion
Compilation:
- Attached test file with '-Wall -Wextra -c -std=c++11 -O1 --target=ppc'
- Compiler Explorer: https://godbolt.org/z/vb7Y1dMh8
Expectation:
The loop control variable 'count' is purposefully underflowed, decrementing
from 0x0 to 0xff. The loop is then executed exactly 256 times, leaving pointer
'p' 1 element past the end of array 'buffer'.
Problem:
The loop control variable 'count' is promoted to an i32 through the
HardwareLoops pass, leading to the following PowerPC assembly:
mr 30, 3
…
mtctr 30
.LBB0_1: # =>This Inner Loop Header: Depth=1
bdnz .LBB0_1
1. r3 (count) is placed into r30
2. The memset (*p++ = 0) portion of the loop is factored out into an
actual call to memset
3. r30 (count) is placed into the CTR
4. The CTR is used in bdnz
a. With a quick glance at the definition of that instruction, the
decrement happens before the compare. This means that the CTR may underflow,
and will end up as either 0xffffffff or 0xffffffffffffffff
b. The CTR will be compared to 0 and, now being a large positive value,
will not be 0
c. The branch will occur, repeating 4a-4c until 'p' passes the end of the
buffer. Since the failure case is seemingly optimized out as undefined
behavior, the loop will continue a finite but undesirable number of times.
--
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/20210407/602b9a0d/attachment.html>
More information about the llvm-bugs
mailing list