[llvm] 533a085 - Recommit "[Reassociate] Use disjoint flag to convert Or to Add. (#72772)"

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 6 14:17:07 PST 2023


Author: Craig Topper
Date: 2023-12-06T14:16:56-08:00
New Revision: 533a0856bff75c395deacadf98df8b83c72d55e4

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

LOG: Recommit "[Reassociate] Use disjoint flag to convert Or to Add. (#72772)"

Original message:
We still have to keep the noCommonBitsSet call to handle multiple reassociations in one pass. We'll lose the flag on the first reassociation.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/Reassociate.cpp
    llvm/test/Transforms/Reassociate/add-like-or.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index d3f6d24d909611..42e979db24d29b 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -2256,9 +2256,10 @@ void ReassociatePass::OptimizeInst(Instruction *I) {
   // with no common bits set, convert it to X+Y.
   if (I->getOpcode() == Instruction::Or &&
       shouldConvertOrWithNoCommonBitsToAdd(I) && !isLoadCombineCandidate(I) &&
-      haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1),
-                          SimplifyQuery(I->getModule()->getDataLayout(),
-                                        /*DT=*/nullptr, /*AC=*/nullptr, I))) {
+      (cast<PossiblyDisjointInst>(I)->isDisjoint() ||
+       haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1),
+                           SimplifyQuery(I->getModule()->getDataLayout(),
+                                         /*DT=*/nullptr, /*AC=*/nullptr, I)))) {
     Instruction *NI = convertOrWithNoCommonBitsToAdd(I);
     RedoInsts.insert(I);
     MadeChange = true;

diff  --git a/llvm/test/Transforms/Reassociate/add-like-or.ll b/llvm/test/Transforms/Reassociate/add-like-or.ll
index 514b10b5a76b3b..d88458f630bc05 100644
--- a/llvm/test/Transforms/Reassociate/add-like-or.ll
+++ b/llvm/test/Transforms/Reassociate/add-like-or.ll
@@ -59,6 +59,18 @@ define i32 @test3(i32 %x, i32 %bit) {
   ret i32 %res
 }
 
+; Test that disjoint allow reassociation.
+define i32 @test4(i32 %a, i32 %b) {
+; CHECK-LABEL: @test4(
+; CHECK-NEXT:    [[C:%.*]] = add i32 [[A:%.*]], 1
+; CHECK-NEXT:    [[C_PLUS_ONE:%.*]] = add i32 [[C]], [[B:%.*]]
+; CHECK-NEXT:    ret i32 [[C_PLUS_ONE]]
+;
+  %c = or disjoint i32 %a, %b
+  %c.plus.one = add i32 %c, 1
+  ret i32 %c.plus.one
+}
+
 declare i32 @llvm.ctlz.i32(i32, i1 immarg) #2
 
 !0 = !{i32 0, i32 33}


        


More information about the llvm-commits mailing list