Skip to content

[InstCombine] Explicitly set disjoint flag when converting xor to or. #74229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions llvm/include/llvm/IR/InstrTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ class BinaryOperator : public Instruction {
return BO;
}

static inline BinaryOperator *
CreateDisjoint(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name = "");
static inline BinaryOperator *CreateDisjoint(BinaryOps Opc, Value *V1,
Value *V2, const Twine &Name,
BasicBlock *BB);
static inline BinaryOperator *CreateDisjoint(BinaryOps Opc, Value *V1,
Value *V2, const Twine &Name,
Instruction *I);

#define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
static BinaryOperator *Create##NUWNSWEXACT##OPC(Value *V1, Value *V2, \
const Twine &Name = "") { \
Expand Down Expand Up @@ -364,6 +373,8 @@ class BinaryOperator : public Instruction {
DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
DEFINE_HELPERS(LShr, Exact) // CreateExactLShr

DEFINE_HELPERS(Or, Disjoint) // CreateDisjointOr

#undef DEFINE_HELPERS

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

BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
Value *V2, const Twine &Name) {
BinaryOperator *BO = Create(Opc, V1, V2, Name);
cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
return BO;
}
BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
Value *V2, const Twine &Name,
BasicBlock *BB) {
BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
return BO;
}
BinaryOperator *BinaryOperator::CreateDisjoint(BinaryOps Opc, Value *V1,
Value *V2, const Twine &Name,
Instruction *I) {
BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
cast<PossiblyDisjointInst>(BO)->setIsDisjoint(true);
return BO;
}

//===----------------------------------------------------------------------===//
// CastInst Class
//===----------------------------------------------------------------------===//
Expand Down
7 changes: 2 additions & 5 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1582,11 +1582,8 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {

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

if (Instruction *Ext = narrowMathIfNoOverflow(I))
return Ext;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4463,7 +4463,7 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
Value *M;
if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(M)), m_Value()),
m_c_And(m_Deferred(M), m_Value()))))
return BinaryOperator::CreateOr(Op0, Op1);
return BinaryOperator::CreateDisjointOr(Op0, Op1);

if (Instruction *Xor = visitMaskedMerge(I, Builder))
return Xor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) {
Instruction *Or =
BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1));
if (DemandedMask.isAllOnes())
cast<PossiblyDisjointInst>(Or)->setIsDisjoint(true);
Or->takeName(I);
return InsertNewInstWith(Or, I->getIterator());
}
Expand Down