[llvm] b92693a - [InstCombine] Support inverting lshr with non-negative operand
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 1 06:55:40 PST 2023
Author: Nikita Popov
Date: 2023-12-01T15:55:27+01:00
New Revision: b92693ac6afc522ea56bede0b9805ca7c138754c
URL: https://github.com/llvm/llvm-project/commit/b92693ac6afc522ea56bede0b9805ca7c138754c
DIFF: https://github.com/llvm/llvm-project/commit/b92693ac6afc522ea56bede0b9805ca7c138754c.diff
LOG: [InstCombine] Support inverting lshr with non-negative operand
If the lshr operand is non-negative, we can treat it the same
way as an ashr. Ideally we would represent this as "lshr nneg",
but for now just perform the necessary ValueTracking query.
Proof: https://alive2.llvm.org/ce/z/Ahg4ri
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/free-inversion.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 26fdef672506a68..a2fadbd6999c5b1 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2181,6 +2181,16 @@ Value *InstCombiner::getFreelyInvertedImpl(Value *V, bool WillInvertAllUses,
return nullptr;
}
+ // Treat lshr with non-negative operand as ashr.
+ if (match(V, m_LShr(m_Value(A), m_Value(B))) &&
+ isKnownNonNegative(V, SQ.getWithInstruction(cast<Instruction>(V)),
+ Depth)) {
+ if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
+ DoesConsume, Depth))
+ return Builder ? Builder->CreateAShr(AV, B) : NonNull;
+ return nullptr;
+ }
+
Value *Cond;
// LogicOps are special in that we canonicalize them at the cost of an
// instruction.
diff --git a/llvm/test/Transforms/InstCombine/free-inversion.ll b/llvm/test/Transforms/InstCombine/free-inversion.ll
index c16310cf09631c5..8d5b1936f95637a 100644
--- a/llvm/test/Transforms/InstCombine/free-inversion.ll
+++ b/llvm/test/Transforms/InstCombine/free-inversion.ll
@@ -499,9 +499,7 @@ define i8 @lshr_nneg(i8 %x, i8 %y) {
; CHECK-LABEL: @lshr_nneg(
; CHECK-NEXT: [[NEG:%.*]] = icmp slt i8 [[X:%.*]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[NEG]])
-; CHECK-NEXT: [[X_NOT:%.*]] = xor i8 [[X]], -1
-; CHECK-NEXT: [[SHR:%.*]] = lshr i8 [[X_NOT]], [[Y:%.*]]
-; CHECK-NEXT: [[SHR_NOT:%.*]] = xor i8 [[SHR]], -1
+; CHECK-NEXT: [[SHR_NOT:%.*]] = ashr i8 [[X]], [[Y:%.*]]
; CHECK-NEXT: ret i8 [[SHR_NOT]]
;
%neg = icmp slt i8 %x, 0
More information about the llvm-commits
mailing list