Skip to content

Commit

Permalink
[InstCombine] Explicitly set disjoint flag when converting xor to or. (
Browse files Browse the repository at this point in the history
  • Loading branch information
topperc committed Dec 6, 2023
1 parent 5b0db27 commit 56248ca
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
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 @@ -4462,7 +4462,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

0 comments on commit 56248ca

Please sign in to comment.