Skip to content

Commit 96adf69

Browse files
authored
[InstCombine] Remove one-use check if other logic operand is constant (#77973)
By using `match(W, m_ImmConstant())`, we do not need to worry about one-time use anymore.
1 parent 74cb09f commit 96adf69

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,36 @@ define i8 @shl_add(i8 %x, i8 %y) {
346346
ret i8 %sh1
347347
}
348348

349+
define i8 @shl_add_multiuse(i8 %x) {
350+
; CHECK-LABEL: @shl_add_multiuse(
351+
; CHECK-NEXT: [[SH0:%.*]] = shl i8 [[X:%.*]], 3
352+
; CHECK-NEXT: call void @use(i8 [[SH0]])
353+
; CHECK-NEXT: [[R:%.*]] = shl i8 [[X]], 5
354+
; CHECK-NEXT: [[SH1:%.*]] = add i8 [[R]], 88
355+
; CHECK-NEXT: ret i8 [[SH1]]
356+
;
357+
%sh0 = shl i8 %x, 3
358+
%r = add i8 %sh0, -42
359+
call void @use(i8 %sh0)
360+
%sh1 = shl i8 %r, 2
361+
ret i8 %sh1
362+
}
363+
364+
define i8 @shl_add_multiuse_nonconstant(i8 %x, i8 %y) {
365+
; CHECK-LABEL: @shl_add_multiuse_nonconstant(
366+
; CHECK-NEXT: [[SH0:%.*]] = shl i8 [[X:%.*]], 3
367+
; CHECK-NEXT: [[R:%.*]] = add i8 [[SH0]], [[Y:%.*]]
368+
; CHECK-NEXT: call void @use(i8 [[SH0]])
369+
; CHECK-NEXT: [[SH1:%.*]] = shl i8 [[R]], 2
370+
; CHECK-NEXT: ret i8 [[SH1]]
371+
;
372+
%sh0 = shl i8 %x, 3
373+
%r = add i8 %sh0, %y
374+
call void @use(i8 %sh0)
375+
%sh1 = shl i8 %r, 2
376+
ret i8 %sh1
377+
}
378+
349379
define <2 x i8> @shl_add_nonuniform(<2 x i8> %x, <2 x i8> %y) {
350380
; CHECK-LABEL: @shl_add_nonuniform(
351381
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i8> [[X:%.*]], <i8 5, i8 4>

0 commit comments

Comments
 (0)