[PATCH] [Reassociate] Better preserve NSW/NUW flags. (PR12985)

David Majnemer david.majnemer at gmail.com
Fri Nov 7 11:44:58 PST 2014


This transform is not safe for nsw shl, it will transform `%shl = shl nsw i32 %a, 31` into `%shl = mul nsw i32 %a, -2147483648`

If `%a` is -1, `%shl` should hold `INT_MIN`.  Instead, it will transform it into poison.

However, I think it's fine to turn a nuw shl into an nuw mul in all cases.
I think you can also turn an nuw nsw shl into an nuw nsw mul.

================
Comment at: test/Transforms/Reassociate/wrap-flags.ll:16-27
@@ +15,13 @@
+
+; Verify the nsw flag is preserved when converting (X-Y)->(X+(0-Y)).
+; CHECK-LABEL: @breakup_sub(
+; CHECK: %y.neg = sub nsw i32 0, %y
+; CHECK: %sub = add nsw i32 %y.neg, %x
+; CHECK: %add = add nsw i32 %sub, %z
+
+define i32 @breakup_sub(i32 %x, i32 %y, i32 %z) {
+entry:
+  %sub = sub nsw i32 %x, %y
+  %add = add nsw i32 %sub, %z
+  ret i32 %add
+}
----------------
This transform isn't sound if %y is INT_MIN, %add would be transformed from something valid into poison.

http://reviews.llvm.org/D6172






More information about the llvm-commits mailing list