@@ -353,29 +353,37 @@ foldShiftByConstOfShiftByConst(BinaryOperator &I, const APInt *COp1,
353
353
// Combinations of right and left shifts will still be optimized in
354
354
// DAGCombine where scalar evolution no longer applies.
355
355
356
+ Value *X = ShiftOp->getOperand (0 );
357
+ unsigned ShiftAmt1 = ShAmt1->getLimitedValue ();
358
+ unsigned ShiftAmt2 = COp1->getLimitedValue ();
359
+ assert (ShiftAmt2 != 0 && " Should have been simplified earlier" );
360
+ if (ShiftAmt1 == 0 )
361
+ return nullptr ; // Will be simplified in the future.
362
+
363
+ if (ShiftAmt1 == ShiftAmt2) {
364
+ // FIXME: This repeats a fold that exists in foldShiftedShift(), but we're
365
+ // not handling the related fold here:
366
+ // (X >>u C) << C --> X & (-1 << C).
367
+ // foldShiftedShift() is always called before this, but it is restricted to
368
+ // only handle cases where the ShiftOp has one use. We don't have that
369
+ // restriction here.
370
+ if (I.getOpcode () != Instruction::LShr ||
371
+ ShiftOp->getOpcode () != Instruction::Shl)
372
+ return nullptr ;
373
+
374
+ // (X << C) >>u C --> X & (-1 >>u C).
375
+ APInt Mask (APInt::getLowBitsSet (TypeBits, TypeBits - ShiftAmt1));
376
+ return BinaryOperator::CreateAnd (X, ConstantInt::get (I.getType (), Mask));
377
+ }
378
+
356
379
// FIXME: Everything under here should be extended to work with vector types.
357
380
358
381
auto *ShiftAmt1C = dyn_cast<ConstantInt>(ShiftOp->getOperand (1 ));
359
382
if (!ShiftAmt1C)
360
383
return nullptr ;
361
384
362
- uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue (TypeBits);
363
- uint32_t ShiftAmt2 = COp1->getLimitedValue (TypeBits);
364
- assert (ShiftAmt2 != 0 && " Should have been simplified earlier" );
365
- if (ShiftAmt1 == 0 )
366
- return nullptr ; // Will be simplified in the future.
367
-
368
- Value *X = ShiftOp->getOperand (0 );
369
385
IntegerType *Ty = cast<IntegerType>(I.getType ());
370
- if (ShiftAmt1 == ShiftAmt2) {
371
- // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
372
- if (I.getOpcode () == Instruction::LShr &&
373
- ShiftOp->getOpcode () == Instruction::Shl) {
374
- APInt Mask (APInt::getLowBitsSet (TypeBits, TypeBits - ShiftAmt1));
375
- return BinaryOperator::CreateAnd (X,
376
- ConstantInt::get (I.getContext (), Mask));
377
- }
378
- } else if (ShiftAmt1 < ShiftAmt2) {
386
+ if (ShiftAmt1 < ShiftAmt2) {
379
387
uint32_t ShiftDiff = ShiftAmt2 - ShiftAmt1;
380
388
381
389
// (X >>?,exact C1) << C2 --> X << (C2-C1)
0 commit comments