[llvm] r251049 - [SCEV] Commute sign extends through nsw additions

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 22 12:57:25 PDT 2015


Author: sanjoy
Date: Thu Oct 22 14:57:25 2015
New Revision: 251049

URL: http://llvm.org/viewvc/llvm-project?rev=251049&view=rev
Log:
[SCEV] Commute sign extends through nsw additions

Summary: Depends on D13613.

Reviewers: atrick, hfinkel, reames, nlewycky

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D13685

Modified:
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp
    llvm/trunk/test/Analysis/ScalarEvolution/no-wrap-add-exprs.ll

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=251049&r1=251048&r2=251049&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Thu Oct 22 14:57:25 2015
@@ -1631,6 +1631,16 @@ const SCEV *ScalarEvolution::getSignExte
         }
       }
     }
+
+    // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
+    if (SA->getNoWrapFlags(SCEV::FlagNSW)) {
+      // If the addition does not sign overflow then we can, by definition,
+      // commute the sign extension with the addition operation.
+      SmallVector<const SCEV *, 4> Ops;
+      for (const auto *Op : SA->operands())
+        Ops.push_back(getSignExtendExpr(Op, Ty));
+      return getAddExpr(Ops, SCEV::FlagNSW);
+    }
   }
   // If the input value is a chrec scev, and we can prove that the value
   // did not overflow the old, smaller, value, we can sign extend all of the

Modified: llvm/trunk/test/Analysis/ScalarEvolution/no-wrap-add-exprs.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/no-wrap-add-exprs.ll?rev=251049&r1=251048&r2=251049&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/no-wrap-add-exprs.ll (original)
+++ llvm/trunk/test/Analysis/ScalarEvolution/no-wrap-add-exprs.ll Thu Oct 22 14:57:25 2015
@@ -42,3 +42,44 @@ define void @f0(i8* %len_addr) {
 
   ret void
 }
+
+define void @f1(i8* %len_addr) {
+; CHECK-LABEL: Classifying expressions for: @f1
+ entry:
+  %len = load i8, i8* %len_addr, !range !0
+  %len_norange = load i8, i8* %len_addr
+; CHECK:  %len = load i8, i8* %len_addr, !range !0
+; CHECK-NEXT:  -->  %len U: [0,127) S: [0,127)
+; CHECK:  %len_norange = load i8, i8* %len_addr
+; CHECK-NEXT:  -->  %len_norange U: full-set S: full-set
+
+  %t0 = add i8 %len, -1
+  %t1 = add i8 %len, -2
+; CHECK:  %t0 = add i8 %len, -1
+; CHECK-NEXT:  -->  (-1 + %len)<nsw> U: [-1,126) S: [-1,126)
+; CHECK:  %t1 = add i8 %len, -2
+; CHECK-NEXT:  -->  (-2 + %len)<nsw> U: [-2,125) S: [-2,125)
+
+  %t0.sext = sext i8 %t0 to i16
+  %t1.sext = sext i8 %t1 to i16
+; CHECK:  %t0.sext = sext i8 %t0 to i16
+; CHECK-NEXT:  -->  (-1 + (zext i8 %len to i16))<nsw> U: [-1,126) S: [-1,126)
+; CHECK:  %t1.sext = sext i8 %t1 to i16
+; CHECK-NEXT:  -->  (-2 + (zext i8 %len to i16))<nsw> U: [-2,125) S: [-2,125)
+
+  %q0 = add i8 %len_norange, 1
+  %q1 = add i8 %len_norange, 2
+; CHECK:  %q0 = add i8 %len_norange, 1
+; CHECK-NEXT:  -->  (1 + %len_norange) U: full-set S: full-set
+; CHECK:  %q1 = add i8 %len_norange, 2
+; CHECK-NEXT:  -->  (2 + %len_norange) U: full-set S: full-set
+
+  %q0.sext = sext i8 %q0 to i16
+  %q1.sext = sext i8 %q1 to i16
+; CHECK:  %q0.sext = sext i8 %q0 to i16
+; CHECK-NEXT:  -->  (sext i8 (1 + %len_norange) to i16) U: [-128,128) S: [-128,128)
+; CHECK:  %q1.sext = sext i8 %q1 to i16
+; CHECK-NEXT:  -->  (sext i8 (2 + %len_norange) to i16) U: [-128,128) S: [-128,128)
+
+  ret void
+}




More information about the llvm-commits mailing list