Skip to content

Commit cd865e3

Browse files
committed
[InstCombine] Use disjoint flag instead of haveNoCommonBitsSet()
Slightly stronger, if disjoint was inferred earlier with information that is no longer available.
1 parent 17168f7 commit cd865e3

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,13 +739,11 @@ class Negator final {
739739
using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
740740
BuilderTy Builder;
741741

742-
const SimplifyQuery &SQ;
743-
744742
const bool IsTrulyNegation;
745743

746744
SmallDenseMap<Value *, Value *> NegationsCache;
747745

748-
Negator(LLVMContext &C, const SimplifyQuery &SQ, bool IsTrulyNegation);
746+
Negator(LLVMContext &C, const DataLayout &DL, bool IsTrulyNegation);
749747

750748
#if LLVM_ENABLE_STATS
751749
unsigned NumValuesVisitedInThisNegator = 0;

llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ static cl::opt<unsigned>
9797
cl::desc("What is the maximal lookup depth when trying to "
9898
"check for viability of negation sinking."));
9999

100-
Negator::Negator(LLVMContext &C, const SimplifyQuery &SQ, bool IsTrulyNegation_)
101-
: Builder(C, TargetFolder(SQ.DL),
100+
Negator::Negator(LLVMContext &C, const DataLayout &DL, bool IsTrulyNegation_)
101+
: Builder(C, TargetFolder(DL),
102102
IRBuilderCallbackInserter([&](Instruction *I) {
103103
++NegatorNumInstructionsCreatedTotal;
104104
NewInstructions.push_back(I);
105105
})),
106-
SQ(SQ), IsTrulyNegation(IsTrulyNegation_) {}
106+
IsTrulyNegation(IsTrulyNegation_) {}
107107

108108
#if LLVM_ENABLE_STATS
109109
Negator::~Negator() {
@@ -402,8 +402,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
402402
I->getName() + ".neg", /* HasNUW */ false, IsNSW);
403403
}
404404
case Instruction::Or: {
405-
if (!haveNoCommonBitsSet(I->getOperand(0), I->getOperand(1),
406-
SQ.getWithInstruction(I)))
405+
if (!cast<PossiblyDisjointInst>(I)->isDisjoint())
407406
return nullptr; // Don't know how to handle `or` in general.
408407
std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);
409408
// `or`/`add` are interchangeable when operands have no common bits set.
@@ -539,7 +538,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
539538
if (!NegatorEnabled || !DebugCounter::shouldExecute(NegatorCounter))
540539
return nullptr;
541540

542-
Negator N(Root->getContext(), IC.getSimplifyQuery(), LHSIsZero);
541+
Negator N(Root->getContext(), IC.getDataLayout(), LHSIsZero);
543542
std::optional<Result> Res = N.run(Root, IsNSW);
544543
if (!Res) { // Negation failed.
545544
LLVM_DEBUG(dbgs() << "Negator: failed to sink negation into " << *Root

llvm/test/Transforms/InstCombine/sub-of-negatible.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,17 @@ define i8 @negation_of_increment_via_or_common_bits_set(i8 %x, i8 %y) {
10681068
ret i8 %t2
10691069
}
10701070

1071+
define i8 @negation_of_increment_via_or_disjoint(i8 %x, i8 %y) {
1072+
; CHECK-LABEL: @negation_of_increment_via_or_disjoint(
1073+
; CHECK-NEXT: [[T1_NEG:%.*]] = xor i8 [[Y:%.*]], -1
1074+
; CHECK-NEXT: [[T2:%.*]] = add i8 [[T1_NEG]], [[X:%.*]]
1075+
; CHECK-NEXT: ret i8 [[T2]]
1076+
;
1077+
%t1 = or disjoint i8 %y, 1
1078+
%t2 = sub i8 %x, %t1
1079+
ret i8 %t2
1080+
}
1081+
10711082
; 'or' of operands with no common bits set is 'add'
10721083
define i8 @add_via_or_with_no_common_bits_set(i8 %x, i8 %y) {
10731084
; CHECK-LABEL: @add_via_or_with_no_common_bits_set(

0 commit comments

Comments
 (0)