Skip to content

Commit 5b8e1a6

Browse files
authored
[SCEVExpander] Do not reuse disjoint or (#80281)
SCEV treats "or disjoint" the same as "add nsw nuw". However, when expanding, we cannot generally replace an add SCEV node with an "or disjoint" instruction. Just dropping the poison flag is insufficient in this case, we would have to actually convert the or into an add. This is a partial fix for #79861.
1 parent 42ec993 commit 5b8e1a6

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,13 @@ canReuseInstruction(ScalarEvolution &SE, const SCEV *S, Instruction *I,
13981398
if (!I)
13991399
return false;
14001400

1401+
// Disjoint or instructions are interpreted as adds by SCEV. However, we
1402+
// can't replace an arbitrary add with disjoint or, even if we drop the
1403+
// flag. We would need to convert the or into an add.
1404+
if (auto *PDI = dyn_cast<PossiblyDisjointInst>(I))
1405+
if (PDI->isDisjoint())
1406+
return false;
1407+
14011408
// FIXME: Ignore vscale, even though it technically could be poison. Do this
14021409
// because SCEV currently assumes it can't be poison. Remove this special
14031410
// case once we proper model when vscale can be poison.

llvm/test/Transforms/IndVarSimplify/pr79861.ll

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ define void @expander_or_disjoint(i64 %n) {
7575
; CHECK-LABEL: define void @expander_or_disjoint(
7676
; CHECK-SAME: i64 [[N:%.*]]) {
7777
; CHECK-NEXT: entry:
78-
; CHECK-NEXT: [[OR:%.*]] = or i64 [[N]], 1
78+
; CHECK-NEXT: [[OR:%.*]] = or disjoint i64 [[N]], 1
79+
; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[N]], 1
7980
; CHECK-NEXT: br label [[LOOP:%.*]]
8081
; CHECK: loop:
8182
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_INC:%.*]], [[LOOP]] ]
8283
; CHECK-NEXT: [[IV_INC]] = add i64 [[IV]], 1
8384
; CHECK-NEXT: [[ADD:%.*]] = add i64 [[IV]], [[OR]]
8485
; CHECK-NEXT: call void @use(i64 [[ADD]])
85-
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[IV_INC]], [[OR]]
86+
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[IV_INC]], [[TMP0]]
8687
; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT:%.*]]
8788
; CHECK: exit:
8889
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)