Skip to content

Commit 0c82e06

Browse files
authored
[LICM] Use OverflowTracking to preserve NUW/NSW when reassociating. (#140404)
This enables preserving NSW when both adds have NSW and NUW. For now, set AllKnownNonNegative/AllKnownNonZero to false when using in LICM. https://alive2.llvm.org/ce/z/uu79Xc Depends on #140403. PR: #140404
1 parent 5dfaf84 commit 0c82e06

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,14 +2864,7 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
28642864
auto *NewBO = BinaryOperator::Create(
28652865
Opcode, LV, Inv, BO->getName() + ".reass", BO->getIterator());
28662866

2867-
// Copy NUW for ADDs if both instructions have it.
2868-
if (Opcode == Instruction::Add && BO->hasNoUnsignedWrap() &&
2869-
BO0->hasNoUnsignedWrap()) {
2870-
// If `Inv` was not constant-folded, a new Instruction has been created.
2871-
if (auto *I = dyn_cast<Instruction>(Inv))
2872-
I->setHasNoUnsignedWrap(true);
2873-
NewBO->setHasNoUnsignedWrap(true);
2874-
} else if (Opcode == Instruction::FAdd || Opcode == Instruction::FMul) {
2867+
if (Opcode == Instruction::FAdd || Opcode == Instruction::FMul) {
28752868
// Intersect FMF flags for FADD and FMUL.
28762869
FastMathFlags Intersect = BO->getFastMathFlags() & BO0->getFastMathFlags();
28772870
if (auto *I = dyn_cast<Instruction>(Inv))
@@ -2884,6 +2877,16 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
28842877
if (auto *I = dyn_cast<PossiblyDisjointInst>(Inv))
28852878
I->setIsDisjoint(Disjoint);
28862879
cast<PossiblyDisjointInst>(NewBO)->setIsDisjoint(Disjoint);
2880+
} else {
2881+
OverflowTracking Flags;
2882+
Flags.AllKnownNonNegative = false;
2883+
Flags.AllKnownNonZero = false;
2884+
Flags.mergeFlags(*BO);
2885+
Flags.mergeFlags(*BO0);
2886+
// If `Inv` was not constant-folded, a new Instruction has been created.
2887+
if (auto *I = dyn_cast<Instruction>(Inv))
2888+
Flags.applyFlags(*I);
2889+
Flags.applyFlags(*NewBO);
28872890
}
28882891

28892892
BO->replaceAllUsesWith(NewBO);

llvm/test/Transforms/LICM/hoist-binop.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,13 @@ loop:
371371
define void @add_nuw_nsw(i64 %c1, i64 %c2) {
372372
; CHECK-LABEL: @add_nuw_nsw(
373373
; CHECK-NEXT: entry:
374-
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add nuw i64 [[C1:%.*]], [[C2:%.*]]
374+
; CHECK-NEXT: [[INVARIANT_OP:%.*]] = add nuw nsw i64 [[C1:%.*]], [[C2:%.*]]
375375
; CHECK-NEXT: br label [[LOOP:%.*]]
376376
; CHECK: loop:
377377
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDEX_NEXT_REASS:%.*]], [[LOOP]] ]
378378
; CHECK-NEXT: [[STEP_ADD:%.*]] = add nuw nsw i64 [[INDEX]], [[C1]]
379379
; CHECK-NEXT: call void @use(i64 [[STEP_ADD]])
380-
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = add nuw i64 [[INDEX]], [[INVARIANT_OP]]
380+
; CHECK-NEXT: [[INDEX_NEXT_REASS]] = add nuw nsw i64 [[INDEX]], [[INVARIANT_OP]]
381381
; CHECK-NEXT: br label [[LOOP]]
382382
;
383383
entry:

0 commit comments

Comments
 (0)