Skip to content

Commit 6d62821

Browse files
committed
Make API commutatable
1 parent f058c17 commit 6d62821

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,11 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
15211521
return R;
15221522

15231523
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1524-
if (Instruction *R =
1525-
foldAddLike(LHS, RHS, I.hasNoSignedWrap(), I.hasNoUnsignedWrap()))
1524+
if (Instruction *R = foldAddLikeCommutative(LHS, RHS, I.hasNoSignedWrap(),
1525+
I.hasNoUnsignedWrap()))
1526+
return R;
1527+
if (Instruction *R = foldAddLikeCommutative(RHS, LHS, I.hasNoSignedWrap(),
1528+
I.hasNoUnsignedWrap()))
15261529
return R;
15271530
Type *Ty = I.getType();
15281531
if (Ty->isIntOrIntVectorTy(1))

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,10 +3580,16 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
35803580
if (Instruction *R = tryFoldInstWithCtpopWithNot(&I))
35813581
return R;
35823582

3583-
if (cast<PossiblyDisjointInst>(I).isDisjoint())
3584-
if (Instruction *R = foldAddLike(I.getOperand(0), I.getOperand(1),
3585-
/*NSW=*/true, /*NUW=*/true))
3583+
if (cast<PossiblyDisjointInst>(I).isDisjoint()) {
3584+
if (Instruction *R =
3585+
foldAddLikeCommutative(I.getOperand(0), I.getOperand(1),
3586+
/*NSW=*/true, /*NUW=*/true))
3587+
return R;
3588+
if (Instruction *R =
3589+
foldAddLikeCommutative(I.getOperand(1), I.getOperand(0),
3590+
/*NSW=*/true, /*NUW=*/true))
35863591
return R;
3592+
}
35873593

35883594
Value *X, *Y;
35893595
const APInt *CV;

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
586586
unsigned Depth = 0);
587587

588588
/// Common transforms for add / disjoint or
589-
Instruction *foldAddLike(Value *LHS, Value *RHS, bool NSW, bool NUW);
589+
Instruction *foldAddLikeCommutative(Value *LHS, Value *RHS, bool NSW,
590+
bool NUW);
590591

591592
/// Canonicalize the position of binops relative to shufflevector.
592593
Instruction *foldVectorBinop(BinaryOperator &Inst);

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,26 +2008,20 @@ static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src) {
20082008
return true;
20092009
}
20102010

2011-
Instruction *InstCombinerImpl::foldAddLike(Value *LHS, Value *RHS, bool NSW,
2012-
bool NUW) {
2013-
Value *A, *B, *C, *D;
2011+
Instruction *InstCombinerImpl::foldAddLikeCommutative(Value *LHS, Value *RHS,
2012+
bool NSW, bool NUW) {
2013+
Value *A, *B, *C;
20142014
if (match(LHS, m_Sub(m_Value(A), m_Value(B))) &&
2015-
match(RHS, m_Sub(m_Value(C), m_Value(D)))) {
2016-
Instruction *R = nullptr;
2017-
if (A == D)
2018-
R = BinaryOperator::CreateSub(C, B);
2019-
else if (C == B)
2020-
R = BinaryOperator::CreateSub(A, D);
2021-
if (R) {
2022-
bool NSWOut = NSW && match(LHS, m_NSWSub(m_Value(), m_Value())) &&
2023-
match(RHS, m_NSWSub(m_Value(), m_Value()));
2024-
2025-
bool NUWOut = match(LHS, m_NUWSub(m_Value(), m_Value())) &&
2026-
match(RHS, m_NUWSub(m_Value(), m_Value()));
2027-
R->setHasNoSignedWrap(NSWOut);
2028-
R->setHasNoUnsignedWrap(NUWOut);
2029-
return R;
2030-
}
2015+
match(RHS, m_Sub(m_Value(C), m_Specific(A)))) {
2016+
Instruction *R = BinaryOperator::CreateSub(C, B);
2017+
bool NSWOut = NSW && match(LHS, m_NSWSub(m_Value(), m_Value())) &&
2018+
match(RHS, m_NSWSub(m_Value(), m_Value()));
2019+
2020+
bool NUWOut = match(LHS, m_NUWSub(m_Value(), m_Value())) &&
2021+
match(RHS, m_NUWSub(m_Value(), m_Value()));
2022+
R->setHasNoSignedWrap(NSWOut);
2023+
R->setHasNoUnsignedWrap(NUWOut);
2024+
return R;
20312025
}
20322026
return nullptr;
20332027
}

0 commit comments

Comments
 (0)