[llvm] [Transforms] Copy nsw if it is present on the neg (PR #81496)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 12 18:33:44 PST 2024
================
@@ -1559,14 +1559,22 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
bool IntMinIsPoison = cast<Constant>(II->getArgOperand(1))->isOneValue();
// abs(-x) -> abs(x)
- // TODO: Copy nsw if it was present on the neg?
Value *X;
- if (match(IIOperand, m_Neg(m_Value(X))))
- return replaceOperand(*II, 0, X);
- if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
- return replaceOperand(*II, 0, X);
- if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
+ if (match(IIOperand, m_Neg(m_Value(X))) ||
+ match(IIOperand,
+ m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))) ||
+ match(IIOperand,
+ m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X)))) {
+ // Check if the negation has the nsw flag
+ if (auto *NegInst = dyn_cast<Instruction>(IIOperand)) {
+ if (NegInst->hasNoSignedWrap()) {
----------------
goldsteinn wrote:
This won't work if you match the select case.
Think since you actually need the neg instruction something long the following lines would be better:
```
Value *X, *NegX = nullptr;
if (match(IIOperand, m_Neg(m_Value(X))))
NegX = IIOperand;
if (match(IIOperand, m_Select(m_Value(), m_Value(X), m_Neg(m_Deferred(X)))))
NegX = cast<SelectInst>(IIOperand)->FalseVal();
if (match(IIOperand, m_Select(m_Value(), m_Neg(m_Value(X)), m_Deferred(X))))
NegX = cast<SelectInst>(IIOperand)->TrueVal();
if(NegX != nullptr) {
...
}
```
```
https://github.com/llvm/llvm-project/pull/81496
More information about the llvm-commits
mailing list