[PATCH] [InstCombine] Don't propagate nsw for A*B+A*C => A*(B+C).

Sanjoy Das sanjoy at playingwithpointers.com
Mon May 11 11:17:10 PDT 2015


================
Comment at: test/Transforms/InstCombine/add2.ll:209-213
@@ -208,7 +208,7 @@
 define i16 @add_nsw_mul_nsw(i16 %x) {
  %add1 = add nsw i16 %x, %x
  %add2 = add nsw i16 %add1, %x
  ret i16 %add2
 ; CHECK-LABEL: @add_nsw_mul_nsw(
-; CHECK-NEXT: %add2 = mul nsw i16 %x, 3
 ; CHECK-NEXT: ret i16 %add2
----------------
majnemer wrote:
> This transform is correct, would it be hard to keep it from breaking?
No, it won't be hard.

I have to admit I was lazier than usual when making this change.  I'll send in a more aggressive version soon.

================
Comment at: test/Transforms/InstCombine/add2.ll:246-261
@@ -245,18 +245,18 @@
 define i16 @mul_add_to_mul_4(i16 %a) {
  %mul1 = mul nsw i16 %a, 2
  %mul2 = mul nsw i16 %a, 7
  %add = add nsw i16 %mul1, %mul2
  ret i16 %add
 ; CHECK-LABEL: @mul_add_to_mul_4(
-; CHECK-NEXT: %add = mul nsw i16 %a, 9
+; CHECK-NEXT: %add = mul i16 %a, 9
 ; CHECK-NEXT: ret i16 %add
 }
 
 define i16 @mul_add_to_mul_5(i16 %a) {
  %mul1 = mul nsw i16 %a, 3
  %mul2 = mul nsw i16 %a, 7
  %add = add nsw i16 %mul1, %mul2
  ret i16 %add
 ; CHECK-LABEL: @mul_add_to_mul_5(
-; CHECK-NEXT: %add = mul nsw i16 %a, 10
 ; CHECK-NEXT: ret i16 %add
----------------
majnemer wrote:
> A *nsw B+A *nsw C => A *nsw (B+C) is safe when `((B + C) != -(1 << (width(%a) - 1)))`  How hard would this be to implement?
> How hard would this be to implement?

I guess we can just ask ValueTracking on what the constraints on the values feeding in to the expression are.  I'm not sure how effective ValueTracking will be at discovering these ranges though, in practice.

http://reviews.llvm.org/D9629

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the llvm-commits mailing list