New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 48390 - [InstCombine] Miscompile, improper substitution of uadd.sat
Summary: [InstCombine] Miscompile, improper substitution of uadd.sat
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: PC All
: P enhancement
Assignee: Tom Stellard
URL:
Keywords:
Depends on:
Blocks: release-11.0.1
  Show dependency tree
 
Reported: 2020-12-04 10:37 PST by Mark Heffernan
Modified: 2020-12-14 14:14 PST (History)
7 users (show)

See Also:
Fixed By Commit(s): f16320b90b8381f2e3aac1ec17f39eff06f09ea0 e6f2a79d7aa01f8dd7f0194f97a50b480e8ede71 934376da5851 8511a8df838f


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Heffernan 2020-12-04 10:37:39 PST
$ cat >bad.ll <<EOL
define i32 @src(i32 %x1, i32 %x2) {
  %x14 = add i32 %x1, %x2
  %x6 = icmp ugt i32 %x14, %x2
  %x9 = select i1 %x6, i32 %x14, i32 -1
  ret i32 %x9
}
EOL

$ opt bad.ll --instcombine -S
; ModuleID = 'bad.ll'
source_filename = "bad.ll"

define i32 @src(i32 %x1, i32 %x2) {
  %1 = call i32 @llvm.uadd.sat.i32(i32 %x2, i32 %x1)
  ret i32 %1
}

; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
declare i32 @llvm.uadd.sat.i32(i32, i32) #0

attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }


Alive output:

----------------------------------------
define i32 @src(i32 %x1, i32 %x2) {
%0:
  %x14 = add i32 %x1, %x2
  %x6 = icmp ugt i32 %x14, %x2
  %x9 = select i1 %x6, i32 %x14, i32 4294967295
  ret i32 %x9
}
=>
define i32 @tgt(i32 %x1, i32 %x2) {
%0:
  %1 = uadd_sat i32 %x2, %x1
  ret i32 %1
}
Transformation doesn't verify!
ERROR: Value mismatch

Example:
i32 %x1 = #x00000000 (0)
i32 %x2 = #x04000000 (67108864)

Source:
i32 %x14 = #x04000000 (67108864)
i1 %x6 = #x0 (0)
i32 %x9 = #xffffffff (4294967295, -1)

Target:
i32 %1 = #x04000000 (67108864)
Source value: #xffffffff (4294967295, -1)
Target value: #x04000000 (67108864)
Comment 1 Dave Green 2020-12-05 03:46:30 PST
Thanks for the report. I put a potential fix up as https://reviews.llvm.org/D92717.
Comment 2 Roman Lebedev 2020-12-07 00:50:00 PST
I guess this bisects down to https://reviews.llvm.org/rGbf21f0d489fb461a8eeb4d6342d28ed2c6e4048d / https://reviews.llvm.org/D69244
Comment 3 Roman Lebedev 2020-12-09 07:23:55 PST
Fixed by f16320b90b8381f2e3aac1ec17f39eff06f09ea0, e6f2a79d7aa01f8dd7f0194f97a50b480e8ede71.
This should probably go into stable branch.
Comment 4 Tom Stellard 2020-12-14 14:14:35 PST
Merged: 8511a8df838f