[llvm] r364234 - InstCombine: Preserve nuw when reassociating nuw ops [2/3]
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 24 14:37:02 PDT 2019
Author: arsenm
Date: Mon Jun 24 14:37:02 2019
New Revision: 364234
URL: http://llvm.org/viewvc/llvm-project?rev=364234&view=rev
Log:
InstCombine: Preserve nuw when reassociating nuw ops [2/3]
Alive says this is OK.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/trunk/test/Transforms/InstCombine/reassociate-nuw.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=364234&r1=364233&r2=364234&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Mon Jun 24 14:37:02 2019
@@ -431,8 +431,14 @@ bool InstCombiner::SimplifyAssociativeOr
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();
@@ -445,6 +451,8 @@ bool InstCombiner::SimplifyAssociativeOr
// Conservatively clear the optional flags, since they may not be
// preserved by the reassociation.
ClearSubclassDataAfterReassociation(I);
+ if (IsNUW)
+ I.setHasNoUnsignedWrap(true);
Changed = true;
continue;
Modified: llvm/trunk/test/Transforms/InstCombine/reassociate-nuw.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/reassociate-nuw.ll?rev=364234&r1=364233&r2=364234&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/reassociate-nuw.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/reassociate-nuw.ll Mon Jun 24 14:37:02 2019
@@ -55,8 +55,8 @@ define i32 @no_reassoc_add_none_nuw(i32
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_add_nuw(i32 %x, i
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
More information about the llvm-commits
mailing list