[PATCH] D141875: [InstCombine] Reorder (shl (add (shl x, C0), y), C1) -> (add (shl x, C0 + C1), (shl y, C1))
Noah Goldstein via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 16 15:21:29 PST 2023
goldstein.w.n created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
This is just expanding the existing pattern that exists for AND/XOR/OR
and gets a bit more parallelism in from the instruction sequence.
Alive2: https://alive2.llvm.org/ce/z/dSmPkV
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D141875
Files:
llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
llvm/test/Transforms/InstCombine/shift-logic.ll
Index: llvm/test/Transforms/InstCombine/shift-logic.ll
===================================================================
--- llvm/test/Transforms/InstCombine/shift-logic.ll
+++ llvm/test/Transforms/InstCombine/shift-logic.ll
@@ -335,9 +335,9 @@
define i8 @shl_add(i8 %x, i8 %y) {
; CHECK-LABEL: @shl_add(
-; CHECK-NEXT: [[SH0:%.*]] = shl i8 [[X:%.*]], 3
-; CHECK-NEXT: [[R:%.*]] = add i8 [[SH0]], [[Y:%.*]]
-; CHECK-NEXT: [[SH1:%.*]] = shl i8 [[R]], 2
+; CHECK-NEXT: [[TMP1:%.*]] = shl i8 [[X:%.*]], 5
+; CHECK-NEXT: [[TMP2:%.*]] = shl i8 [[Y:%.*]], 2
+; CHECK-NEXT: [[SH1:%.*]] = add i8 [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret i8 [[SH1]]
;
%sh0 = shl i8 %x, 3
@@ -348,9 +348,9 @@
define <2 x i8> @shl_add_nonuniform(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @shl_add_nonuniform(
-; CHECK-NEXT: [[SH0:%.*]] = shl <2 x i8> [[X:%.*]], <i8 3, i8 4>
-; CHECK-NEXT: [[R:%.*]] = add <2 x i8> [[SH0]], [[Y:%.*]]
-; CHECK-NEXT: [[SH1:%.*]] = shl <2 x i8> [[R]], <i8 2, i8 0>
+; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i8> [[X:%.*]], <i8 5, i8 4>
+; CHECK-NEXT: [[TMP2:%.*]] = shl <2 x i8> [[Y:%.*]], <i8 2, i8 0>
+; CHECK-NEXT: [[SH1:%.*]] = add <2 x i8> [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret <2 x i8> [[SH1]]
;
%sh0 = shl <2 x i8> %x, <i8 3, i8 4>
@@ -363,9 +363,9 @@
define <2 x i64> @shl_add_undef(<2 x i64> %x, <2 x i64> %py) {
; CHECK-LABEL: @shl_add_undef(
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i64> [[PY:%.*]], <i64 42, i64 42>
-; CHECK-NEXT: [[SH0:%.*]] = shl <2 x i64> [[X:%.*]], <i64 5, i64 undef>
-; CHECK-NEXT: [[R:%.*]] = add <2 x i64> [[Y]], [[SH0]]
-; CHECK-NEXT: [[SH1:%.*]] = shl <2 x i64> [[R]], <i64 7, i64 undef>
+; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i64> [[X:%.*]], <i64 12, i64 undef>
+; CHECK-NEXT: [[TMP2:%.*]] = shl <2 x i64> [[Y]], <i64 7, i64 undef>
+; CHECK-NEXT: [[SH1:%.*]] = add <2 x i64> [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret <2 x i64> [[SH1]]
;
%y = srem <2 x i64> %py, <i64 42, i64 42> ; thwart complexity-based canonicalization
Index: llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -322,15 +322,18 @@
return BinaryOperator::Create(Instruction::And, NewShift, NewMask);
}
-/// If we have a shift-by-constant of a bitwise logic op that itself has a
-/// shift-by-constant operand with identical opcode, we may be able to convert
-/// that into 2 independent shifts followed by the logic op. This eliminates a
-/// a use of an intermediate value (reduces dependency chain).
+/// If we have a shift-by-constant of a bin op (bitwise logic op or add w/ shl)
+/// that itself has a shift-by-constant operand with identical opcode, we may be
+/// able to convert that into 2 independent shifts followed by the logic op.
+/// This eliminates a a use of an intermediate value (reduces dependency chain).
static Instruction *foldShiftOfShiftedLogic(BinaryOperator &I,
InstCombiner::BuilderTy &Builder) {
assert(I.isShift() && "Expected a shift as input");
- auto *LogicInst = dyn_cast<BinaryOperator>(I.getOperand(0));
- if (!LogicInst || !LogicInst->isBitwiseLogicOp() || !LogicInst->hasOneUse())
+ auto *BinInst = dyn_cast<BinaryOperator>(I.getOperand(0));
+ if (!BinInst ||
+ (!BinInst->isBitwiseLogicOp() &&
+ BinInst->getOpcode() != Instruction::Add) ||
+ !BinInst->hasOneUse())
return nullptr;
Constant *C0, *C1;
@@ -353,18 +356,21 @@
};
// Logic ops are commutative, so check each operand for a match.
- if (matchFirstShift(LogicInst->getOperand(0)))
- Y = LogicInst->getOperand(1);
- else if (matchFirstShift(LogicInst->getOperand(1)))
- Y = LogicInst->getOperand(0);
+ if (matchFirstShift(BinInst->getOperand(0)))
+ Y = BinInst->getOperand(1);
+ else if (matchFirstShift(BinInst->getOperand(1)))
+ Y = BinInst->getOperand(0);
else
return nullptr;
+ if(BinInst->getOpcode() == Instruction::Add && ShiftOpcode != Instruction::Shl)
+ return nullptr;
+
// shift (logic (shift X, C0), Y), C1 -> logic (shift X, C0+C1), (shift Y, C1)
Constant *ShiftSumC = ConstantExpr::getAdd(C0, C1);
Value *NewShift1 = Builder.CreateBinOp(ShiftOpcode, X, ShiftSumC);
Value *NewShift2 = Builder.CreateBinOp(ShiftOpcode, Y, I.getOperand(1));
- return BinaryOperator::Create(LogicInst->getOpcode(), NewShift1, NewShift2);
+ return BinaryOperator::Create(BinInst->getOpcode(), NewShift1, NewShift2);
}
Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141875.489642.patch
Type: text/x-patch
Size: 4697 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230116/08b0b3d6/attachment.bin>
More information about the llvm-commits
mailing list