[llvm] b210cbb - [BDCE] Fix clearing of poison-generating flags

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 31 02:24:24 PST 2024


Author: Nikita Popov
Date: 2024-01-31T11:24:13+01:00
New Revision: b210cbbd0eb8ef7cd2735e99570474e6e53ee00b

URL: https://github.com/llvm/llvm-project/commit/b210cbbd0eb8ef7cd2735e99570474e6e53ee00b
DIFF: https://github.com/llvm/llvm-project/commit/b210cbbd0eb8ef7cd2735e99570474e6e53ee00b.diff

LOG: [BDCE] Fix clearing of poison-generating flags

If the demanded bits of an instruction are full, we don't have to
recurse to its users, but we may still have to clear flags on the
instruction itself.

Fixes https://github.com/llvm/llvm-project/issues/80113.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/BDCE.cpp
    llvm/test/Transforms/BDCE/invalidate-assumptions.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/BDCE.cpp b/llvm/lib/Transforms/Scalar/BDCE.cpp
index e99210ce2f228..6e6fa6c8ffc4d 100644
--- a/llvm/lib/Transforms/Scalar/BDCE.cpp
+++ b/llvm/lib/Transforms/Scalar/BDCE.cpp
@@ -45,15 +45,17 @@ static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
   assert(I->getType()->isIntOrIntVectorTy() &&
          "Trivializing a non-integer value?");
 
+  // If all bits of a user are demanded, then we know that nothing below that
+  // in the def-use chain needs to be changed.
+  if (DB.getDemandedBits(I).isAllOnes())
+    return;
+
   // Initialize the worklist with eligible direct users.
   SmallPtrSet<Instruction *, 16> Visited;
   SmallVector<Instruction *, 16> WorkList;
   for (User *JU : I->users()) {
-    // If all bits of a user are demanded, then we know that nothing below that
-    // in the def-use chain needs to be changed.
-    auto *J = dyn_cast<Instruction>(JU);
-    if (J && J->getType()->isIntOrIntVectorTy() &&
-        !DB.getDemandedBits(J).isAllOnes()) {
+    auto *J = cast<Instruction>(JU);
+    if (J->getType()->isIntOrIntVectorTy()) {
       Visited.insert(J);
       WorkList.push_back(J);
     }
@@ -79,12 +81,14 @@ static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
     // 1. llvm.assume demands its operand, so trivializing can't change it.
     // 2. range metadata only applies to memory accesses which demand all bits.
 
+    // If all bits of a user are demanded, then we know that nothing below
+    // that in the def-use chain needs to be changed.
+    if (DB.getDemandedBits(J).isAllOnes())
+      continue;
+
     for (User *KU : J->users()) {
-      // If all bits of a user are demanded, then we know that nothing below
-      // that in the def-use chain needs to be changed.
-      auto *K = dyn_cast<Instruction>(KU);
-      if (K && Visited.insert(K).second && K->getType()->isIntOrIntVectorTy() &&
-          !DB.getDemandedBits(K).isAllOnes())
+      auto *K = cast<Instruction>(KU);
+      if (Visited.insert(K).second && K->getType()->isIntOrIntVectorTy())
         WorkList.push_back(K);
     }
   }

diff  --git a/llvm/test/Transforms/BDCE/invalidate-assumptions.ll b/llvm/test/Transforms/BDCE/invalidate-assumptions.ll
index 95a004b5e9d33..85d21c33d3d91 100644
--- a/llvm/test/Transforms/BDCE/invalidate-assumptions.ll
+++ b/llvm/test/Transforms/BDCE/invalidate-assumptions.ll
@@ -100,7 +100,7 @@ define void @PR34179(ptr %a) {
 define i64 @disjoint(i64 %x) {
 ; CHECK-LABEL: define i64 @disjoint(
 ; CHECK-SAME: i64 [[X:%.*]]) {
-; CHECK-NEXT:    [[OR:%.*]] = or disjoint i64 [[X]], -2
+; CHECK-NEXT:    [[OR:%.*]] = or i64 [[X]], -2
 ; CHECK-NEXT:    ret i64 [[OR]]
 ;
   %and = and i64 %x, 1
@@ -112,7 +112,7 @@ define i32 @disjoint_indirect(i64 %x) {
 ; CHECK-LABEL: define i32 @disjoint_indirect(
 ; CHECK-SAME: i64 [[X:%.*]]) {
 ; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i64 [[X]] to i32
-; CHECK-NEXT:    [[OR:%.*]] = or disjoint i32 [[TRUNC]], -2
+; CHECK-NEXT:    [[OR:%.*]] = or i32 [[TRUNC]], -2
 ; CHECK-NEXT:    ret i32 [[OR]]
 ;
   %and = and i64 %x, 1


        


More information about the llvm-commits mailing list