aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/Transforms/InstCombine
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2022-04-28 18:32:24 +0000
committerDimitry Andric <dim@FreeBSD.org>2022-05-14 11:46:42 +0000
commit3a9a9c0ca44ec535dcf73fe8462bee458e54814b (patch)
tree13eff4cf89a999893d2f6ead8c5b4684236df8ed /contrib/llvm-project/llvm/lib/Transforms/InstCombine
parent53683b95ef66a12337999587cd98302b1b425920 (diff)
parent139d5007613696147437159a7f0d0cdcac702529 (diff)
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Transforms/InstCombine')
-rw-r--r--contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp6
-rw-r--r--contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp17
2 files changed, 23 insertions, 0 deletions
diff --git a/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 6bbb0251f2bc..2aab79e89078 100644
--- a/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1961,6 +1961,12 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
}
}
+ // If this 'and' clears the sign-bits added by ashr, replace with lshr:
+ // and (ashr X, ShiftC), C --> lshr X, ShiftC
+ if (match(Op0, m_AShr(m_Value(X), m_APInt(ShiftC))) && ShiftC->ult(Width) &&
+ C->isMask(Width - ShiftC->getZExtValue()))
+ return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, *ShiftC));
+
const APInt *AddC;
if (match(Op0, m_Add(m_Value(X), m_APInt(AddC)))) {
// If we add zeros to every bit below a mask, the add has no effect:
diff --git a/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 65e60498ff95..881b00f2a55a 100644
--- a/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -1572,6 +1572,23 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
}
}
+ // Canonicalize a signbit condition to use zero constant by swapping:
+ // (CmpLHS > -1) ? TV : FV --> (CmpLHS < 0) ? FV : TV
+ // To avoid conflicts (infinite loops) with other canonicalizations, this is
+ // not applied with any constant select arm.
+ if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes()) &&
+ !match(TrueVal, m_Constant()) && !match(FalseVal, m_Constant()) &&
+ ICI->hasOneUse()) {
+ InstCombiner::BuilderTy::InsertPointGuard Guard(Builder);
+ Builder.SetInsertPoint(&SI);
+ Value *IsNeg = Builder.CreateICmpSLT(
+ CmpLHS, ConstantInt::getNullValue(CmpLHS->getType()), ICI->getName());
+ replaceOperand(SI, 0, IsNeg);
+ SI.swapValues();
+ SI.swapProfMetadata();
+ return &SI;
+ }
+
// FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring
// decomposeBitTestICmp() might help.
{