LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 20189 - Preserving nsw/nuw for x-(-A)=>x+A is unsafe
Summary: Preserving nsw/nuw for x-(-A)=>x+A is unsafe
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: All All
: P normal
Assignee: David Majnemer
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-01 23:56 PDT by Jingyue Wu
Modified: 2014-07-30 23:50 PDT (History)
7 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jingyue Wu 2014-07-01 23:56:05 PDT
When optimizing x-(-A) to x+A, the current logic of preserving nsw/nuw has a hole.

For example, the current logic transforms "x -nsw (0-A)" to "x +nsw A". However, when x = a = INT_MIN = -2^31, "x -nsw (0 - A)" is defined but "x +nsw A"
returns a poison value. The following test case exposes the bug. 

define i32 @test37(i32 %A, i32 %x) {
  %B = sub i32 0, %A
  %C = sub nsw i32 %x, %B
  ret i32 %C
; CHECK-LABEL: @test37(
; CHECK: %C = add i32 %x, %A
; CHECK: ret i32 %C
}

To fix this issue, we need to consider the nsw/nuw of both minuses in x-(-A). If both of them are marked nsw/nuw, we can preserve the nsw/nuw in the result x+A.
Comment 1 David Majnemer 2014-07-30 23:50:53 PDT
Fixed with r214384 and r214385.