Skip to content

Commit 56248ca

Browse files
authored
[InstCombine] Explicitly set disjoint flag when converting xor to or. (#74229)
1 parent 5b0db27 commit 56248ca

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,15 @@ class BinaryOperator : public Instruction {
336336
return BO;
337337
}
338338

339+
static inline BinaryOperator *
340+
CreateDisjoint(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name = "");
341+
static inline BinaryOperator *CreateDisjoint(BinaryOps Opc, Value *V1,
342+
Value *V2, const Twine &Name,
343+
BasicBlock *BB);
344+
static inline BinaryOperator *CreateDisjoint(BinaryOps Opc, Value *V1,
345+
Value *V2, const Twine &Name,
346+
Instruction *I);
347+
339348
#define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
340349
static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \
341350
const Twine &Name = "") { \
@@ -364,6 +373,8 @@ class BinaryOperator : public Instruction {
364373
DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
365374
DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
366375

376+
DEFINE_HELPERS(Or, Disjoint) // CreateDisjointOr
377+
367378
#undef DEFINE_HELPERS
368379

369380
/// Helper functions to construct and inspect unary operations (NEG and NOT)
@@ -438,6 +449,27 @@ class PossiblyDisjointInst : public BinaryOperator {
438449
}
439450
};
440451

452+
BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
453+
Value *V2, const Twine &Name) {
454+
BinaryOperator *BO = Create(Opc, V1, V2, Name);
455+
cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
456+
return BO;
457+
}
458+
BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
459+
Value *V2, const Twine &Name,
460+
BasicBlock *BB) {
461+
BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
462+
cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
463+
return BO;
464+
}
465+
BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
466+
Value *V2, const Twine &Name,
467+
Instruction *I) {
468+
BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
469+
cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
470+
return BO;
471+
}
472+
441473
//===----------------------------------------------------------------------===//
442474
// CastInst Class
443475
//===----------------------------------------------------------------------===//

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,11 +1582,8 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
15821582

15831583
// A+B --> A|B iff A and B have no bits set in common.
15841584
WithCache<const Value *> LHSCache(LHS), RHSCache(RHS);
1585-
if (haveNoCommonBitsSet(LHSCache, RHSCache, SQ.getWithInstruction(&I))) {
1586-
auto *Or = BinaryOperator::CreateOr(LHS, RHS);
1587-
cast<PossiblyDisjointInst>(Or)->setIsDisjoint(true);
1588-
return Or;
1589-
}
1585+
if (haveNoCommonBitsSet(LHSCache, RHSCache, SQ.getWithInstruction(&I)))
1586+
return BinaryOperator::CreateDisjointOr(LHS, RHS);
15901587

15911588
if (Instruction *Ext = narrowMathIfNoOverflow(I))
15921589
return Ext;

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4462,7 +4462,7 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
44624462
Value *M;
44634463
if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(M)), m_Value()),
44644464
m_c_And(m_Deferred(M), m_Value()))))
4465-
return BinaryOperator::CreateOr(Op0, Op1);
4465+
return BinaryOperator::CreateDisjointOr(Op0, Op1);
44664466

44674467
if (Instruction *Xor = visitMaskedMerge(I, Builder))
44684468
return Xor;

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
315315
if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) {
316316
Instruction *Or =
317317
BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1));
318+
if (DemandedMask.isAllOnes())
319+
cast<PossiblyDisjointInst>(Or)->setIsDisjoint(true);
318320
Or->takeName(I);
319321
return InsertNewInstWith(Or, I->getIterator());
320322
}

0 commit comments

Comments
 (0)