@@ -368,10 +368,14 @@ static Instruction *foldShiftOfShiftedBinOp(BinaryOperator &I,
368
368
369
369
// Find a matching one-use shift by constant. The fold is not valid if the sum
370
370
// of the shift values equals or exceeds bitwidth.
371
- // TODO: Remove the one-use check if the other logic operand (Y) is constant.
372
371
Value *X, *Y;
373
- auto matchFirstShift = [&](Value *V) {
372
+ auto matchFirstShift = [&](Value *V, Value *W ) {
374
373
APInt Threshold (Ty->getScalarSizeInBits (), Ty->getScalarSizeInBits ());
374
+ if (isa<Constant>(W)) {
375
+ return match (V, (m_BinOp (ShiftOpcode, m_Value (X), m_Constant (C0)))) &&
376
+ match (ConstantExpr::getAdd (C0, C1),
377
+ m_SpecificInt_ICMP (ICmpInst::ICMP_ULT, Threshold));
378
+ }
375
379
return match (V,
376
380
m_OneUse (m_BinOp (ShiftOpcode, m_Value (X), m_Constant (C0)))) &&
377
381
match (ConstantExpr::getAdd (C0, C1),
@@ -382,9 +386,9 @@ static Instruction *foldShiftOfShiftedBinOp(BinaryOperator &I,
382
386
// is not so we cannot reoder if we match operand(1) and need to keep the
383
387
// operands in their original positions.
384
388
bool FirstShiftIsOp1 = false ;
385
- if (matchFirstShift (BinInst->getOperand (0 )))
389
+ if (matchFirstShift (BinInst->getOperand (0 ), BinInst-> getOperand ( 1 ) ))
386
390
Y = BinInst->getOperand (1 );
387
- else if (matchFirstShift (BinInst->getOperand (1 ))) {
391
+ else if (matchFirstShift (BinInst->getOperand (1 ), BinInst-> getOperand ( 0 ) )) {
388
392
Y = BinInst->getOperand (0 );
389
393
FirstShiftIsOp1 = BinInst->getOpcode () == Instruction::Sub;
390
394
} else
0 commit comments