[PATCH] D63527: InstCombine: Preserve nuw when reassociating nuw ops [2/3]

Matt Arsenault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 18 16:25:39 PDT 2019


arsenm created this revision.
arsenm added a reviewer: lebedev.ri.
Herald added a subscriber: wdng.
arsenm added a parent revision: D39417: InstCombine: Preserve nuw when reassociating nuw ops [1/3].

Alive says this is OK.


https://reviews.llvm.org/D63527

Files:
  lib/Transforms/InstCombine/InstructionCombining.cpp
  test/Transforms/InstCombine/reassociate-nuw.ll


Index: test/Transforms/InstCombine/reassociate-nuw.ll
===================================================================
--- test/Transforms/InstCombine/reassociate-nuw.ll
+++ test/Transforms/InstCombine/reassociate-nuw.ll
@@ -55,8 +55,8 @@
 
 define i32 @reassoc_x2_add_nuw(i32 %x, i32 %y) {
 ; CHECK-LABEL: @reassoc_x2_add_nuw(
-; CHECK-NEXT:    [[ADD1:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[ADD2:%.*]] = add i32 [[ADD1]], 12
+; CHECK-NEXT:    [[ADD1:%.*]] = add nuw i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[ADD2:%.*]] = add nuw i32 [[ADD1]], 12
 ; CHECK-NEXT:    ret i32 [[ADD2]]
 ;
   %add0 = add nuw i32 %x, 4
@@ -68,7 +68,7 @@
 define i32 @reassoc_x2_mul_nuw(i32 %x, i32 %y) {
 ; CHECK-LABEL: @reassoc_x2_mul_nuw(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[MUL2:%.*]] = mul i32 [[MUL1]], 45
+; CHECK-NEXT:    [[MUL2:%.*]] = mul nuw i32 [[MUL1]], 45
 ; CHECK-NEXT:    ret i32 [[MUL2]]
 ;
   %mul0 = mul nuw i32 %x, 5
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -432,8 +432,14 @@
           Op0->getOpcode() == Opcode && Op1->getOpcode() == Opcode &&
           match(Op0, m_OneUse(m_BinOp(m_Value(A), m_Constant(C1)))) &&
           match(Op1, m_OneUse(m_BinOp(m_Value(B), m_Constant(C2))))) {
-        BinaryOperator *NewBO = BinaryOperator::Create(Opcode, A, B);
-        if (isa<FPMathOperator>(NewBO)) {
+        bool IsNUW = hasNoUnsignedWrap(I) &&
+           hasNoUnsignedWrap(*Op0) &&
+           hasNoUnsignedWrap(*Op1);
+         BinaryOperator *NewBO = (IsNUW && Opcode == Instruction::Add) ?
+           BinaryOperator::CreateNUW(Opcode, A, B) :
+           BinaryOperator::Create(Opcode, A, B);
+
+         if (isa<FPMathOperator>(NewBO)) {
           FastMathFlags Flags = I.getFastMathFlags();
           Flags &= Op0->getFastMathFlags();
           Flags &= Op1->getFastMathFlags();
@@ -446,6 +452,8 @@
         // Conservatively clear the optional flags, since they may not be
         // preserved by the reassociation.
         ClearSubclassDataAfterReassociation(I);
+        if (IsNUW)
+          I.setHasNoUnsignedWrap(true);
 
         Changed = true;
         continue;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63527.205465.patch
Type: text/x-patch
Size: 2359 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190618/c419f8d6/attachment.bin>


More information about the llvm-commits mailing list