[llvm] [SCEVExpander] Do not reuse disjoint or (PR #80281)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 1 04:04:48 PST 2024


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/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.

>From 95a627e242040c0703e92cf6d19e7e6457e6bd6b Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Thu, 1 Feb 2024 13:02:19 +0100
Subject: [PATCH] [SCEVExpander] Do not reuse disjoint or

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.
---
 llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp | 7 +++++++
 llvm/test/Transforms/IndVarSimplify/pr79861.ll        | 5 +++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index ed55a13072aaa..eea01825a72b2 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -1370,6 +1370,13 @@ canReuseInstruction(ScalarEvolution &SE, const SCEV *S, Instruction *I,
   if (programUndefinedIfPoison(I))
     return true;
 
+  // Disjoint or instructions are interpreted as adds by SCEV. However, we
+  // can't replace an arbitrary add with disjoint or, even if we drop the flag.
+  // We would need to convert the or into an add.
+  if (auto *PDI = dyn_cast<PossiblyDisjointInst>(I))
+    if (PDI->isDisjoint())
+      return false;
+
   // Otherwise, it is possible that I is more poisonous that S. Collect the
   // poison-contributors of S, and then check whether I has any additional
   // poison-contributors. Poison that is contributed through poison-generating
diff --git a/llvm/test/Transforms/IndVarSimplify/pr79861.ll b/llvm/test/Transforms/IndVarSimplify/pr79861.ll
index a8e2aa42a365c..7e267d04c94cc 100644
--- a/llvm/test/Transforms/IndVarSimplify/pr79861.ll
+++ b/llvm/test/Transforms/IndVarSimplify/pr79861.ll
@@ -75,14 +75,15 @@ define void @expander_or_disjoint(i64 %n) {
 ; CHECK-LABEL: define void @expander_or_disjoint(
 ; CHECK-SAME: i64 [[N:%.*]]) {
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[OR:%.*]] = or i64 [[N]], 1
+; CHECK-NEXT:    [[OR:%.*]] = or disjoint i64 [[N]], 1
+; CHECK-NEXT:    [[TMP0:%.*]] = add i64 [[N]], 1
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
 ; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_INC:%.*]], [[LOOP]] ]
 ; CHECK-NEXT:    [[IV_INC]] = add i64 [[IV]], 1
 ; CHECK-NEXT:    [[ADD:%.*]] = add i64 [[IV]], [[OR]]
 ; CHECK-NEXT:    call void @use(i64 [[ADD]])
-; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i64 [[IV_INC]], [[OR]]
+; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i64 [[IV_INC]], [[TMP0]]
 ; CHECK-NEXT:    br i1 [[EXITCOND]], label [[LOOP]], label [[EXIT:%.*]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret void



More information about the llvm-commits mailing list