Skip to content

Commit 9fdbd5a

Browse files
committed
[Transforms] Remove one-time check if other logic operand (Y) is constant
By using match(W, m_ImmConstant()), we do not need to worry about one-time use anymore.
1 parent c2d21f3 commit 9fdbd5a

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,14 @@ static Instruction *foldShiftOfShiftedBinOp(BinaryOperator &I,
366366

367367
Type *Ty = I.getType();
368368

369-
// Find a matching one-use shift by constant. The fold is not valid if the sum
369+
// Find a matching shift by constant. The fold is not valid if the sum
370370
// of the shift values equals or exceeds bitwidth.
371-
// TODO: Remove the one-use check if the other logic operand (Y) is constant.
372371
Value *X, *Y;
373-
auto matchFirstShift = [&](Value *V) {
374-
APInt Threshold(Ty->getScalarSizeInBits(), Ty->getScalarSizeInBits());
375-
return match(V,
376-
m_OneUse(m_BinOp(ShiftOpcode, m_Value(X), m_Constant(C0)))) &&
372+
auto matchFirstShift = [&](Value *V, Value *W) {
373+
unsigned Size = Ty->getScalarSizeInBits();
374+
APInt Threshold(Size, Size);
375+
return match(V, m_BinOp(ShiftOpcode, m_Value(X), m_Constant(C0))) &&
376+
(V->hasOneUse() || match(W, m_ImmConstant())) &&
377377
match(ConstantExpr::getAdd(C0, C1),
378378
m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold));
379379
};
@@ -382,9 +382,9 @@ static Instruction *foldShiftOfShiftedBinOp(BinaryOperator &I,
382382
// is not so we cannot reoder if we match operand(1) and need to keep the
383383
// operands in their original positions.
384384
bool FirstShiftIsOp1 = false;
385-
if (matchFirstShift(BinInst->getOperand(0)))
385+
if (matchFirstShift(BinInst->getOperand(0), BinInst->getOperand(1)))
386386
Y = BinInst->getOperand(1);
387-
else if (matchFirstShift(BinInst->getOperand(1))) {
387+
else if (matchFirstShift(BinInst->getOperand(1), BinInst->getOperand(0))) {
388388
Y = BinInst->getOperand(0);
389389
FirstShiftIsOp1 = BinInst->getOpcode() == Instruction::Sub;
390390
} else

llvm/test/Transforms/InstCombine/shift-logic.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ define i8 @shl_add(i8 %x, i8 %y) {
349349
define i8 @shl_add_multiuse(i8 %x) {
350350
; CHECK-LABEL: @shl_add_multiuse(
351351
; CHECK-NEXT: [[SH0:%.*]] = shl i8 [[X:%.*]], 3
352-
; CHECK-NEXT: [[R:%.*]] = add i8 [[SH0]], -42
353352
; CHECK-NEXT: call void @use(i8 [[SH0]])
354-
; CHECK-NEXT: [[SH1:%.*]] = shl i8 [[R]], 2
353+
; CHECK-NEXT: [[R:%.*]] = shl i8 [[X]], 5
354+
; CHECK-NEXT: [[SH1:%.*]] = add i8 [[R]], 88
355355
; CHECK-NEXT: ret i8 [[SH1]]
356356
;
357357
%sh0 = shl i8 %x, 3

0 commit comments

Comments
 (0)