[llvm] cd865e3 - [InstCombine] Use disjoint flag instead of haveNoCommonBitsSet()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 5 05:49:56 PST 2023


Author: Nikita Popov
Date: 2023-12-05T14:44:48+01:00
New Revision: cd865e36dbc0b1778739348ac9a25eb18b5c5c16

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

LOG: [InstCombine] Use disjoint flag instead of haveNoCommonBitsSet()

Slightly stronger, if disjoint was inferred earlier with information
that is no longer available.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineInternal.h
    llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
    llvm/test/Transforms/InstCombine/sub-of-negatible.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 0bbb22be71569..1a87c4a5ba07e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -739,13 +739,11 @@ class Negator final {
   using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
   BuilderTy Builder;
 
-  const SimplifyQuery &SQ;
-
   const bool IsTrulyNegation;
 
   SmallDenseMap<Value *, Value *> NegationsCache;
 
-  Negator(LLVMContext &C, const SimplifyQuery &SQ, bool IsTrulyNegation);
+  Negator(LLVMContext &C, const DataLayout &DL, bool IsTrulyNegation);
 
 #if LLVM_ENABLE_STATS
   unsigned NumValuesVisitedInThisNegator = 0;

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
index 6e0b44c11f22a..513b185c83a49 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -97,13 +97,13 @@ static cl::opt<unsigned>
                     cl::desc("What is the maximal lookup depth when trying to "
                              "check for viability of negation sinking."));
 
-Negator::Negator(LLVMContext &C, const SimplifyQuery &SQ, bool IsTrulyNegation_)
-    : Builder(C, TargetFolder(SQ.DL),
+Negator::Negator(LLVMContext &C, const DataLayout &DL, bool IsTrulyNegation_)
+    : Builder(C, TargetFolder(DL),
               IRBuilderCallbackInserter([&](Instruction *I) {
                 ++NegatorNumInstructionsCreatedTotal;
                 NewInstructions.push_back(I);
               })),
-      SQ(SQ), IsTrulyNegation(IsTrulyNegation_) {}
+      IsTrulyNegation(IsTrulyNegation_) {}
 
 #if LLVM_ENABLE_STATS
 Negator::~Negator() {
@@ -402,8 +402,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
         I->getName() + ".neg", /* HasNUW */ false, IsNSW);
   }
   case Instruction::Or: {
-    if (!haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1),
-                             SQ.getWithInstruction(I)))
+    if (!cast<PossiblyDisjointInst>(I)->isDisjoint())
       return nullptr; // Don't know how to handle `or` in general.
     std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);
     // `or`/`add` are interchangeable when operands have no common bits set.
@@ -539,7 +538,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
   if (!NegatorEnabled || !DebugCounter::shouldExecute(NegatorCounter))
     return nullptr;
 
-  Negator N(Root->getContext(), IC.getSimplifyQuery(), LHSIsZero);
+  Negator N(Root->getContext(), IC.getDataLayout(), LHSIsZero);
   std::optional<Result> Res = N.run(Root, IsNSW);
   if (!Res) { // Negation failed.
     LLVM_DEBUG(dbgs() << "Negator: failed to sink negation into " << *Root

diff  --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
index 64cb647ae3f74..d360533f823c6 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -1068,6 +1068,17 @@ define i8 @negation_of_increment_via_or_common_bits_set(i8 %x, i8 %y) {
   ret i8 %t2
 }
 
+define i8 @negation_of_increment_via_or_disjoint(i8 %x, i8 %y) {
+; CHECK-LABEL: @negation_of_increment_via_or_disjoint(
+; CHECK-NEXT:    [[T1_NEG:%.*]] = xor i8 [[Y:%.*]], -1
+; CHECK-NEXT:    [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
+; CHECK-NEXT:    ret i8 [[T2]]
+;
+  %t1 = or disjoint i8 %y, 1
+  %t2 = sub i8 %x, %t1
+  ret i8 %t2
+}
+
 ; 'or' of operands with no common bits set is 'add'
 define i8 @add_via_or_with_no_common_bits_set(i8 %x, i8 %y) {
 ; CHECK-LABEL: @add_via_or_with_no_common_bits_set(


        


More information about the llvm-commits mailing list