xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- InstCombineShifts.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the visitShl, visitLShr, and visitAShr functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "InstCombineInternal.h"
14 #include "llvm/Analysis/InstructionSimplify.h"
15 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/IR/PatternMatch.h"
17 #include "llvm/Transforms/InstCombine/InstCombiner.h"
18 using namespace llvm;
19 using namespace PatternMatch;
20 
21 #define DEBUG_TYPE "instcombine"
22 
canTryToConstantAddTwoShiftAmounts(Value * Sh0,Value * ShAmt0,Value * Sh1,Value * ShAmt1)23 bool canTryToConstantAddTwoShiftAmounts(Value *Sh0, Value *ShAmt0, Value *Sh1,
24                                         Value *ShAmt1) {
25   // We have two shift amounts from two different shifts. The types of those
26   // shift amounts may not match. If that's the case let's bailout now..
27   if (ShAmt0->getType() != ShAmt1->getType())
28     return false;
29 
30   // As input, we have the following pattern:
31   //   Sh0 (Sh1 X, Q), K
32   // We want to rewrite that as:
33   //   Sh x, (Q+K)  iff (Q+K) u< bitwidth(x)
34   // While we know that originally (Q+K) would not overflow
35   // (because  2 * (N-1) u<= iN -1), we have looked past extensions of
36   // shift amounts. so it may now overflow in smaller bitwidth.
37   // To ensure that does not happen, we need to ensure that the total maximal
38   // shift amount is still representable in that smaller bit width.
39   unsigned MaximalPossibleTotalShiftAmount =
40       (Sh0->getType()->getScalarSizeInBits() - 1) +
41       (Sh1->getType()->getScalarSizeInBits() - 1);
42   APInt MaximalRepresentableShiftAmount =
43       APInt::getAllOnes(ShAmt0->getType()->getScalarSizeInBits());
44   return MaximalRepresentableShiftAmount.uge(MaximalPossibleTotalShiftAmount);
45 }
46 
47 // Given pattern:
48 //   (x shiftopcode Q) shiftopcode K
49 // we should rewrite it as
50 //   x shiftopcode (Q+K)  iff (Q+K) u< bitwidth(x) and
51 //
52 // This is valid for any shift, but they must be identical, and we must be
53 // careful in case we have (zext(Q)+zext(K)) and look past extensions,
54 // (Q+K) must not overflow or else (Q+K) u< bitwidth(x) is bogus.
55 //
56 // AnalyzeForSignBitExtraction indicates that we will only analyze whether this
57 // pattern has any 2 right-shifts that sum to 1 less than original bit width.
reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator * Sh0,const SimplifyQuery & SQ,bool AnalyzeForSignBitExtraction)58 Value *InstCombinerImpl::reassociateShiftAmtsOfTwoSameDirectionShifts(
59     BinaryOperator *Sh0, const SimplifyQuery &SQ,
60     bool AnalyzeForSignBitExtraction) {
61   // Look for a shift of some instruction, ignore zext of shift amount if any.
62   Instruction *Sh0Op0;
63   Value *ShAmt0;
64   if (!match(Sh0,
65              m_Shift(m_Instruction(Sh0Op0), m_ZExtOrSelf(m_Value(ShAmt0)))))
66     return nullptr;
67 
68   // If there is a truncation between the two shifts, we must make note of it
69   // and look through it. The truncation imposes additional constraints on the
70   // transform.
71   Instruction *Sh1;
72   Value *Trunc = nullptr;
73   match(Sh0Op0,
74         m_CombineOr(m_CombineAnd(m_Trunc(m_Instruction(Sh1)), m_Value(Trunc)),
75                     m_Instruction(Sh1)));
76 
77   // Inner shift: (x shiftopcode ShAmt1)
78   // Like with other shift, ignore zext of shift amount if any.
79   Value *X, *ShAmt1;
80   if (!match(Sh1, m_Shift(m_Value(X), m_ZExtOrSelf(m_Value(ShAmt1)))))
81     return nullptr;
82 
83   // Verify that it would be safe to try to add those two shift amounts.
84   if (!canTryToConstantAddTwoShiftAmounts(Sh0, ShAmt0, Sh1, ShAmt1))
85     return nullptr;
86 
87   // We are only looking for signbit extraction if we have two right shifts.
88   bool HadTwoRightShifts = match(Sh0, m_Shr(m_Value(), m_Value())) &&
89                            match(Sh1, m_Shr(m_Value(), m_Value()));
90   // ... and if it's not two right-shifts, we know the answer already.
91   if (AnalyzeForSignBitExtraction && !HadTwoRightShifts)
92     return nullptr;
93 
94   // The shift opcodes must be identical, unless we are just checking whether
95   // this pattern can be interpreted as a sign-bit-extraction.
96   Instruction::BinaryOps ShiftOpcode = Sh0->getOpcode();
97   bool IdenticalShOpcodes = Sh0->getOpcode() == Sh1->getOpcode();
98   if (!IdenticalShOpcodes && !AnalyzeForSignBitExtraction)
99     return nullptr;
100 
101   // If we saw truncation, we'll need to produce extra instruction,
102   // and for that one of the operands of the shift must be one-use,
103   // unless of course we don't actually plan to produce any instructions here.
104   if (Trunc && !AnalyzeForSignBitExtraction &&
105       !match(Sh0, m_c_BinOp(m_OneUse(m_Value()), m_Value())))
106     return nullptr;
107 
108   // Can we fold (ShAmt0+ShAmt1) ?
109   auto *NewShAmt = dyn_cast_or_null<Constant>(
110       simplifyAddInst(ShAmt0, ShAmt1, /*isNSW=*/false, /*isNUW=*/false,
111                       SQ.getWithInstruction(Sh0)));
112   if (!NewShAmt)
113     return nullptr; // Did not simplify.
114   unsigned NewShAmtBitWidth = NewShAmt->getType()->getScalarSizeInBits();
115   unsigned XBitWidth = X->getType()->getScalarSizeInBits();
116   // Is the new shift amount smaller than the bit width of inner/new shift?
117   if (!match(NewShAmt, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT,
118                                           APInt(NewShAmtBitWidth, XBitWidth))))
119     return nullptr; // FIXME: could perform constant-folding.
120 
121   // If there was a truncation, and we have a right-shift, we can only fold if
122   // we are left with the original sign bit. Likewise, if we were just checking
123   // that this is a sighbit extraction, this is the place to check it.
124   // FIXME: zero shift amount is also legal here, but we can't *easily* check
125   // more than one predicate so it's not really worth it.
126   if (HadTwoRightShifts && (Trunc || AnalyzeForSignBitExtraction)) {
127     // If it's not a sign bit extraction, then we're done.
128     if (!match(NewShAmt,
129                m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
130                                   APInt(NewShAmtBitWidth, XBitWidth - 1))))
131       return nullptr;
132     // If it is, and that was the question, return the base value.
133     if (AnalyzeForSignBitExtraction)
134       return X;
135   }
136 
137   assert(IdenticalShOpcodes && "Should not get here with different shifts.");
138 
139   if (NewShAmt->getType() != X->getType()) {
140     NewShAmt = ConstantFoldCastOperand(Instruction::ZExt, NewShAmt,
141                                        X->getType(), SQ.DL);
142     if (!NewShAmt)
143       return nullptr;
144   }
145 
146   // All good, we can do this fold.
147   BinaryOperator *NewShift = BinaryOperator::Create(ShiftOpcode, X, NewShAmt);
148 
149   // The flags can only be propagated if there wasn't a trunc.
150   if (!Trunc) {
151     // If the pattern did not involve trunc, and both of the original shifts
152     // had the same flag set, preserve the flag.
153     if (ShiftOpcode == Instruction::BinaryOps::Shl) {
154       NewShift->setHasNoUnsignedWrap(Sh0->hasNoUnsignedWrap() &&
155                                      Sh1->hasNoUnsignedWrap());
156       NewShift->setHasNoSignedWrap(Sh0->hasNoSignedWrap() &&
157                                    Sh1->hasNoSignedWrap());
158     } else {
159       NewShift->setIsExact(Sh0->isExact() && Sh1->isExact());
160     }
161   }
162 
163   Instruction *Ret = NewShift;
164   if (Trunc) {
165     Builder.Insert(NewShift);
166     Ret = CastInst::Create(Instruction::Trunc, NewShift, Sh0->getType());
167   }
168 
169   return Ret;
170 }
171 
172 // If we have some pattern that leaves only some low bits set, and then performs
173 // left-shift of those bits, if none of the bits that are left after the final
174 // shift are modified by the mask, we can omit the mask.
175 //
176 // There are many variants to this pattern:
177 //   a)  (x & ((1 << MaskShAmt) - 1)) << ShiftShAmt
178 //   b)  (x & (~(-1 << MaskShAmt))) << ShiftShAmt
179 //   c)  (x & (-1 l>> MaskShAmt)) << ShiftShAmt
180 //   d)  (x & ((-1 << MaskShAmt) l>> MaskShAmt)) << ShiftShAmt
181 //   e)  ((x << MaskShAmt) l>> MaskShAmt) << ShiftShAmt
182 //   f)  ((x << MaskShAmt) a>> MaskShAmt) << ShiftShAmt
183 // All these patterns can be simplified to just:
184 //   x << ShiftShAmt
185 // iff:
186 //   a,b)     (MaskShAmt+ShiftShAmt) u>= bitwidth(x)
187 //   c,d,e,f) (ShiftShAmt-MaskShAmt) s>= 0 (i.e. ShiftShAmt u>= MaskShAmt)
188 static Instruction *
dropRedundantMaskingOfLeftShiftInput(BinaryOperator * OuterShift,const SimplifyQuery & Q,InstCombiner::BuilderTy & Builder)189 dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
190                                      const SimplifyQuery &Q,
191                                      InstCombiner::BuilderTy &Builder) {
192   assert(OuterShift->getOpcode() == Instruction::BinaryOps::Shl &&
193          "The input must be 'shl'!");
194 
195   Value *Masked, *ShiftShAmt;
196   match(OuterShift,
197         m_Shift(m_Value(Masked), m_ZExtOrSelf(m_Value(ShiftShAmt))));
198 
199   // *If* there is a truncation between an outer shift and a possibly-mask,
200   // then said truncation *must* be one-use, else we can't perform the fold.
201   Value *Trunc;
202   if (match(Masked, m_CombineAnd(m_Trunc(m_Value(Masked)), m_Value(Trunc))) &&
203       !Trunc->hasOneUse())
204     return nullptr;
205 
206   Type *NarrowestTy = OuterShift->getType();
207   Type *WidestTy = Masked->getType();
208   bool HadTrunc = WidestTy != NarrowestTy;
209 
210   // The mask must be computed in a type twice as wide to ensure
211   // that no bits are lost if the sum-of-shifts is wider than the base type.
212   Type *ExtendedTy = WidestTy->getExtendedType();
213 
214   Value *MaskShAmt;
215 
216   // ((1 << MaskShAmt) - 1)
217   auto MaskA = m_Add(m_Shl(m_One(), m_Value(MaskShAmt)), m_AllOnes());
218   // (~(-1 << maskNbits))
219   auto MaskB = m_Not(m_Shl(m_AllOnes(), m_Value(MaskShAmt)));
220   // (-1 l>> MaskShAmt)
221   auto MaskC = m_LShr(m_AllOnes(), m_Value(MaskShAmt));
222   // ((-1 << MaskShAmt) l>> MaskShAmt)
223   auto MaskD =
224       m_LShr(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_Deferred(MaskShAmt));
225 
226   Value *X;
227   Constant *NewMask;
228 
229   if (match(Masked, m_c_And(m_CombineOr(MaskA, MaskB), m_Value(X)))) {
230     // Peek through an optional zext of the shift amount.
231     match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
232 
233     // Verify that it would be safe to try to add those two shift amounts.
234     if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked,
235                                             MaskShAmt))
236       return nullptr;
237 
238     // Can we simplify (MaskShAmt+ShiftShAmt) ?
239     auto *SumOfShAmts = dyn_cast_or_null<Constant>(simplifyAddInst(
240         MaskShAmt, ShiftShAmt, /*IsNSW=*/false, /*IsNUW=*/false, Q));
241     if (!SumOfShAmts)
242       return nullptr; // Did not simplify.
243     // In this pattern SumOfShAmts correlates with the number of low bits
244     // that shall remain in the root value (OuterShift).
245 
246     // An extend of an undef value becomes zero because the high bits are never
247     // completely unknown. Replace the `undef` shift amounts with final
248     // shift bitwidth to ensure that the value remains undef when creating the
249     // subsequent shift op.
250     SumOfShAmts = Constant::replaceUndefsWith(
251         SumOfShAmts, ConstantInt::get(SumOfShAmts->getType()->getScalarType(),
252                                       ExtendedTy->getScalarSizeInBits()));
253     auto *ExtendedSumOfShAmts = ConstantFoldCastOperand(
254         Instruction::ZExt, SumOfShAmts, ExtendedTy, Q.DL);
255     if (!ExtendedSumOfShAmts)
256       return nullptr;
257 
258     // And compute the mask as usual: ~(-1 << (SumOfShAmts))
259     auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy);
260     Constant *ExtendedInvertedMask = ConstantFoldBinaryOpOperands(
261         Instruction::Shl, ExtendedAllOnes, ExtendedSumOfShAmts, Q.DL);
262     if (!ExtendedInvertedMask)
263       return nullptr;
264 
265     NewMask = ConstantExpr::getNot(ExtendedInvertedMask);
266   } else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X))) ||
267              match(Masked, m_Shr(m_Shl(m_Value(X), m_Value(MaskShAmt)),
268                                  m_Deferred(MaskShAmt)))) {
269     // Peek through an optional zext of the shift amount.
270     match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
271 
272     // Verify that it would be safe to try to add those two shift amounts.
273     if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked,
274                                             MaskShAmt))
275       return nullptr;
276 
277     // Can we simplify (ShiftShAmt-MaskShAmt) ?
278     auto *ShAmtsDiff = dyn_cast_or_null<Constant>(simplifySubInst(
279         ShiftShAmt, MaskShAmt, /*IsNSW=*/false, /*IsNUW=*/false, Q));
280     if (!ShAmtsDiff)
281       return nullptr; // Did not simplify.
282     // In this pattern ShAmtsDiff correlates with the number of high bits that
283     // shall be unset in the root value (OuterShift).
284 
285     // An extend of an undef value becomes zero because the high bits are never
286     // completely unknown. Replace the `undef` shift amounts with negated
287     // bitwidth of innermost shift to ensure that the value remains undef when
288     // creating the subsequent shift op.
289     unsigned WidestTyBitWidth = WidestTy->getScalarSizeInBits();
290     ShAmtsDiff = Constant::replaceUndefsWith(
291         ShAmtsDiff, ConstantInt::get(ShAmtsDiff->getType()->getScalarType(),
292                                      -WidestTyBitWidth));
293     auto *ExtendedNumHighBitsToClear = ConstantFoldCastOperand(
294         Instruction::ZExt,
295         ConstantExpr::getSub(ConstantInt::get(ShAmtsDiff->getType(),
296                                               WidestTyBitWidth,
297                                               /*isSigned=*/false),
298                              ShAmtsDiff),
299         ExtendedTy, Q.DL);
300     if (!ExtendedNumHighBitsToClear)
301       return nullptr;
302 
303     // And compute the mask as usual: (-1 l>> (NumHighBitsToClear))
304     auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy);
305     NewMask = ConstantFoldBinaryOpOperands(Instruction::LShr, ExtendedAllOnes,
306                                            ExtendedNumHighBitsToClear, Q.DL);
307     if (!NewMask)
308       return nullptr;
309   } else
310     return nullptr; // Don't know anything about this pattern.
311 
312   NewMask = ConstantExpr::getTrunc(NewMask, NarrowestTy);
313 
314   // Does this mask has any unset bits? If not then we can just not apply it.
315   bool NeedMask = !match(NewMask, m_AllOnes());
316 
317   // If we need to apply a mask, there are several more restrictions we have.
318   if (NeedMask) {
319     // The old masking instruction must go away.
320     if (!Masked->hasOneUse())
321       return nullptr;
322     // The original "masking" instruction must not have been`ashr`.
323     if (match(Masked, m_AShr(m_Value(), m_Value())))
324       return nullptr;
325   }
326 
327   // If we need to apply truncation, let's do it first, since we can.
328   // We have already ensured that the old truncation will go away.
329   if (HadTrunc)
330     X = Builder.CreateTrunc(X, NarrowestTy);
331 
332   // No 'NUW'/'NSW'! We no longer know that we won't shift-out non-0 bits.
333   // We didn't change the Type of this outermost shift, so we can just do it.
334   auto *NewShift = BinaryOperator::Create(OuterShift->getOpcode(), X,
335                                           OuterShift->getOperand(1));
336   if (!NeedMask)
337     return NewShift;
338 
339   Builder.Insert(NewShift);
340   return BinaryOperator::Create(Instruction::And, NewShift, NewMask);
341 }
342 
343 /// If we have a shift-by-constant of a bin op (bitwise logic op or add/sub w/
344 /// shl) that itself has a shift-by-constant operand with identical opcode, we
345 /// may be able to convert that into 2 independent shifts followed by the logic
346 /// op. This eliminates a use of an intermediate value (reduces dependency
347 /// chain).
foldShiftOfShiftedBinOp(BinaryOperator & I,InstCombiner::BuilderTy & Builder)348 static Instruction *foldShiftOfShiftedBinOp(BinaryOperator &I,
349                                             InstCombiner::BuilderTy &Builder) {
350   assert(I.isShift() && "Expected a shift as input");
351   auto *BinInst = dyn_cast<BinaryOperator>(I.getOperand(0));
352   if (!BinInst ||
353       (!BinInst->isBitwiseLogicOp() &&
354        BinInst->getOpcode() != Instruction::Add &&
355        BinInst->getOpcode() != Instruction::Sub) ||
356       !BinInst->hasOneUse())
357     return nullptr;
358 
359   Constant *C0, *C1;
360   if (!match(I.getOperand(1), m_Constant(C1)))
361     return nullptr;
362 
363   Instruction::BinaryOps ShiftOpcode = I.getOpcode();
364   // Transform for add/sub only works with shl.
365   if ((BinInst->getOpcode() == Instruction::Add ||
366        BinInst->getOpcode() == Instruction::Sub) &&
367       ShiftOpcode != Instruction::Shl)
368     return nullptr;
369 
370   Type *Ty = I.getType();
371 
372   // Find a matching shift by constant. The fold is not valid if the sum
373   // of the shift values equals or exceeds bitwidth.
374   Value *X, *Y;
375   auto matchFirstShift = [&](Value *V, Value *W) {
376     unsigned Size = Ty->getScalarSizeInBits();
377     APInt Threshold(Size, Size);
378     return match(V, m_BinOp(ShiftOpcode, m_Value(X), m_Constant(C0))) &&
379            (V->hasOneUse() || match(W, m_ImmConstant())) &&
380            match(ConstantExpr::getAdd(C0, C1),
381                  m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold));
382   };
383 
384   // Logic ops and Add are commutative, so check each operand for a match. Sub
385   // is not so we cannot reoder if we match operand(1) and need to keep the
386   // operands in their original positions.
387   bool FirstShiftIsOp1 = false;
388   if (matchFirstShift(BinInst->getOperand(0), BinInst->getOperand(1)))
389     Y = BinInst->getOperand(1);
390   else if (matchFirstShift(BinInst->getOperand(1), BinInst->getOperand(0))) {
391     Y = BinInst->getOperand(0);
392     FirstShiftIsOp1 = BinInst->getOpcode() == Instruction::Sub;
393   } else
394     return nullptr;
395 
396   // shift (binop (shift X, C0), Y), C1 -> binop (shift X, C0+C1), (shift Y, C1)
397   Constant *ShiftSumC = ConstantExpr::getAdd(C0, C1);
398   Value *NewShift1 = Builder.CreateBinOp(ShiftOpcode, X, ShiftSumC);
399   Value *NewShift2 = Builder.CreateBinOp(ShiftOpcode, Y, C1);
400   Value *Op1 = FirstShiftIsOp1 ? NewShift2 : NewShift1;
401   Value *Op2 = FirstShiftIsOp1 ? NewShift1 : NewShift2;
402   return BinaryOperator::Create(BinInst->getOpcode(), Op1, Op2);
403 }
404 
commonShiftTransforms(BinaryOperator & I)405 Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
406   if (Instruction *Phi = foldBinopWithPhiOperands(I))
407     return Phi;
408 
409   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
410   assert(Op0->getType() == Op1->getType());
411   Type *Ty = I.getType();
412 
413   // If the shift amount is a one-use `sext`, we can demote it to `zext`.
414   Value *Y;
415   if (match(Op1, m_OneUse(m_SExt(m_Value(Y))))) {
416     Value *NewExt = Builder.CreateZExt(Y, Ty, Op1->getName());
417     return BinaryOperator::Create(I.getOpcode(), Op0, NewExt);
418   }
419 
420   // See if we can fold away this shift.
421   if (SimplifyDemandedInstructionBits(I))
422     return &I;
423 
424   // Try to fold constant and into select arguments.
425   if (isa<Constant>(Op0))
426     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
427       if (Instruction *R = FoldOpIntoSelect(I, SI))
428         return R;
429 
430   if (Constant *CUI = dyn_cast<Constant>(Op1))
431     if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
432       return Res;
433 
434   if (auto *NewShift = cast_or_null<Instruction>(
435           reassociateShiftAmtsOfTwoSameDirectionShifts(&I, SQ)))
436     return NewShift;
437 
438   // Pre-shift a constant shifted by a variable amount with constant offset:
439   // C shift (A add nuw C1) --> (C shift C1) shift A
440   Value *A;
441   Constant *C, *C1;
442   if (match(Op0, m_Constant(C)) &&
443       match(Op1, m_NUWAddLike(m_Value(A), m_Constant(C1)))) {
444     Value *NewC = Builder.CreateBinOp(I.getOpcode(), C, C1);
445     BinaryOperator *NewShiftOp = BinaryOperator::Create(I.getOpcode(), NewC, A);
446     if (I.getOpcode() == Instruction::Shl) {
447       NewShiftOp->setHasNoSignedWrap(I.hasNoSignedWrap());
448       NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
449     } else {
450       NewShiftOp->setIsExact(I.isExact());
451     }
452     return NewShiftOp;
453   }
454 
455   unsigned BitWidth = Ty->getScalarSizeInBits();
456 
457   const APInt *AC, *AddC;
458   // Try to pre-shift a constant shifted by a variable amount added with a
459   // negative number:
460   // C << (X - AddC) --> (C >> AddC) << X
461   // and
462   // C >> (X - AddC) --> (C << AddC) >> X
463   if (match(Op0, m_APInt(AC)) && match(Op1, m_Add(m_Value(A), m_APInt(AddC))) &&
464       AddC->isNegative() && (-*AddC).ult(BitWidth)) {
465     assert(!AC->isZero() && "Expected simplify of shifted zero");
466     unsigned PosOffset = (-*AddC).getZExtValue();
467 
468     auto isSuitableForPreShift = [PosOffset, &I, AC]() {
469       switch (I.getOpcode()) {
470       default:
471         return false;
472       case Instruction::Shl:
473         return (I.hasNoSignedWrap() || I.hasNoUnsignedWrap()) &&
474                AC->eq(AC->lshr(PosOffset).shl(PosOffset));
475       case Instruction::LShr:
476         return I.isExact() && AC->eq(AC->shl(PosOffset).lshr(PosOffset));
477       case Instruction::AShr:
478         return I.isExact() && AC->eq(AC->shl(PosOffset).ashr(PosOffset));
479       }
480     };
481     if (isSuitableForPreShift()) {
482       Constant *NewC = ConstantInt::get(Ty, I.getOpcode() == Instruction::Shl
483                                                 ? AC->lshr(PosOffset)
484                                                 : AC->shl(PosOffset));
485       BinaryOperator *NewShiftOp =
486           BinaryOperator::Create(I.getOpcode(), NewC, A);
487       if (I.getOpcode() == Instruction::Shl) {
488         NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
489       } else {
490         NewShiftOp->setIsExact();
491       }
492       return NewShiftOp;
493     }
494   }
495 
496   // X shift (A srem C) -> X shift (A and (C - 1)) iff C is a power of 2.
497   // Because shifts by negative values (which could occur if A were negative)
498   // are undefined.
499   if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Constant(C))) &&
500       match(C, m_Power2())) {
501     // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
502     // demand the sign bit (and many others) here??
503     Constant *Mask = ConstantExpr::getSub(C, ConstantInt::get(Ty, 1));
504     Value *Rem = Builder.CreateAnd(A, Mask, Op1->getName());
505     return replaceOperand(I, 1, Rem);
506   }
507 
508   if (Instruction *Logic = foldShiftOfShiftedBinOp(I, Builder))
509     return Logic;
510 
511   if (match(Op1, m_Or(m_Value(), m_SpecificInt(BitWidth - 1))))
512     return replaceOperand(I, 1, ConstantInt::get(Ty, BitWidth - 1));
513 
514   return nullptr;
515 }
516 
517 /// Return true if we can simplify two logical (either left or right) shifts
518 /// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
canEvaluateShiftedShift(unsigned OuterShAmt,bool IsOuterShl,Instruction * InnerShift,InstCombinerImpl & IC,Instruction * CxtI)519 static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
520                                     Instruction *InnerShift,
521                                     InstCombinerImpl &IC, Instruction *CxtI) {
522   assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
523 
524   // We need constant scalar or constant splat shifts.
525   const APInt *InnerShiftConst;
526   if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
527     return false;
528 
529   // Two logical shifts in the same direction:
530   // shl (shl X, C1), C2 -->  shl X, C1 + C2
531   // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
532   bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
533   if (IsInnerShl == IsOuterShl)
534     return true;
535 
536   // Equal shift amounts in opposite directions become bitwise 'and':
537   // lshr (shl X, C), C --> and X, C'
538   // shl (lshr X, C), C --> and X, C'
539   if (*InnerShiftConst == OuterShAmt)
540     return true;
541 
542   // If the 2nd shift is bigger than the 1st, we can fold:
543   // lshr (shl X, C1), C2 -->  and (shl X, C1 - C2), C3
544   // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3
545   // but it isn't profitable unless we know the and'd out bits are already zero.
546   // Also, check that the inner shift is valid (less than the type width) or
547   // we'll crash trying to produce the bit mask for the 'and'.
548   unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits();
549   if (InnerShiftConst->ugt(OuterShAmt) && InnerShiftConst->ult(TypeWidth)) {
550     unsigned InnerShAmt = InnerShiftConst->getZExtValue();
551     unsigned MaskShift =
552         IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
553     APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
554     if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
555       return true;
556   }
557 
558   return false;
559 }
560 
561 /// See if we can compute the specified value, but shifted logically to the left
562 /// or right by some number of bits. This should return true if the expression
563 /// can be computed for the same cost as the current expression tree. This is
564 /// used to eliminate extraneous shifting from things like:
565 ///      %C = shl i128 %A, 64
566 ///      %D = shl i128 %B, 96
567 ///      %E = or i128 %C, %D
568 ///      %F = lshr i128 %E, 64
569 /// where the client will ask if E can be computed shifted right by 64-bits. If
570 /// this succeeds, getShiftedValue() will be called to produce the value.
canEvaluateShifted(Value * V,unsigned NumBits,bool IsLeftShift,InstCombinerImpl & IC,Instruction * CxtI)571 static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
572                                InstCombinerImpl &IC, Instruction *CxtI) {
573   // We can always evaluate immediate constants.
574   if (match(V, m_ImmConstant()))
575     return true;
576 
577   Instruction *I = dyn_cast<Instruction>(V);
578   if (!I) return false;
579 
580   // We can't mutate something that has multiple uses: doing so would
581   // require duplicating the instruction in general, which isn't profitable.
582   if (!I->hasOneUse()) return false;
583 
584   switch (I->getOpcode()) {
585   default: return false;
586   case Instruction::And:
587   case Instruction::Or:
588   case Instruction::Xor:
589     // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
590     return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
591            canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
592 
593   case Instruction::Shl:
594   case Instruction::LShr:
595     return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
596 
597   case Instruction::Select: {
598     SelectInst *SI = cast<SelectInst>(I);
599     Value *TrueVal = SI->getTrueValue();
600     Value *FalseVal = SI->getFalseValue();
601     return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
602            canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
603   }
604   case Instruction::PHI: {
605     // We can change a phi if we can change all operands.  Note that we never
606     // get into trouble with cyclic PHIs here because we only consider
607     // instructions with a single use.
608     PHINode *PN = cast<PHINode>(I);
609     for (Value *IncValue : PN->incoming_values())
610       if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
611         return false;
612     return true;
613   }
614   case Instruction::Mul: {
615     const APInt *MulConst;
616     // We can fold (shr (mul X, -(1 << C)), C) -> (and (neg X), C`)
617     return !IsLeftShift && match(I->getOperand(1), m_APInt(MulConst)) &&
618            MulConst->isNegatedPowerOf2() && MulConst->countr_zero() == NumBits;
619   }
620   }
621 }
622 
623 /// Fold OuterShift (InnerShift X, C1), C2.
624 /// See canEvaluateShiftedShift() for the constraints on these instructions.
foldShiftedShift(BinaryOperator * InnerShift,unsigned OuterShAmt,bool IsOuterShl,InstCombiner::BuilderTy & Builder)625 static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
626                                bool IsOuterShl,
627                                InstCombiner::BuilderTy &Builder) {
628   bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
629   Type *ShType = InnerShift->getType();
630   unsigned TypeWidth = ShType->getScalarSizeInBits();
631 
632   // We only accept shifts-by-a-constant in canEvaluateShifted().
633   const APInt *C1;
634   match(InnerShift->getOperand(1), m_APInt(C1));
635   unsigned InnerShAmt = C1->getZExtValue();
636 
637   // Change the shift amount and clear the appropriate IR flags.
638   auto NewInnerShift = [&](unsigned ShAmt) {
639     InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt));
640     if (IsInnerShl) {
641       InnerShift->setHasNoUnsignedWrap(false);
642       InnerShift->setHasNoSignedWrap(false);
643     } else {
644       InnerShift->setIsExact(false);
645     }
646     return InnerShift;
647   };
648 
649   // Two logical shifts in the same direction:
650   // shl (shl X, C1), C2 -->  shl X, C1 + C2
651   // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
652   if (IsInnerShl == IsOuterShl) {
653     // If this is an oversized composite shift, then unsigned shifts get 0.
654     if (InnerShAmt + OuterShAmt >= TypeWidth)
655       return Constant::getNullValue(ShType);
656 
657     return NewInnerShift(InnerShAmt + OuterShAmt);
658   }
659 
660   // Equal shift amounts in opposite directions become bitwise 'and':
661   // lshr (shl X, C), C --> and X, C'
662   // shl (lshr X, C), C --> and X, C'
663   if (InnerShAmt == OuterShAmt) {
664     APInt Mask = IsInnerShl
665                      ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
666                      : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
667     Value *And = Builder.CreateAnd(InnerShift->getOperand(0),
668                                    ConstantInt::get(ShType, Mask));
669     if (auto *AndI = dyn_cast<Instruction>(And)) {
670       AndI->moveBefore(InnerShift);
671       AndI->takeName(InnerShift);
672     }
673     return And;
674   }
675 
676   assert(InnerShAmt > OuterShAmt &&
677          "Unexpected opposite direction logical shift pair");
678 
679   // In general, we would need an 'and' for this transform, but
680   // canEvaluateShiftedShift() guarantees that the masked-off bits are not used.
681   // lshr (shl X, C1), C2 -->  shl X, C1 - C2
682   // shl (lshr X, C1), C2 --> lshr X, C1 - C2
683   return NewInnerShift(InnerShAmt - OuterShAmt);
684 }
685 
686 /// When canEvaluateShifted() returns true for an expression, this function
687 /// inserts the new computation that produces the shifted value.
getShiftedValue(Value * V,unsigned NumBits,bool isLeftShift,InstCombinerImpl & IC,const DataLayout & DL)688 static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
689                               InstCombinerImpl &IC, const DataLayout &DL) {
690   // We can always evaluate constants shifted.
691   if (Constant *C = dyn_cast<Constant>(V)) {
692     if (isLeftShift)
693       return IC.Builder.CreateShl(C, NumBits);
694     else
695       return IC.Builder.CreateLShr(C, NumBits);
696   }
697 
698   Instruction *I = cast<Instruction>(V);
699   IC.addToWorklist(I);
700 
701   switch (I->getOpcode()) {
702   default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
703   case Instruction::And:
704   case Instruction::Or:
705   case Instruction::Xor:
706     // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
707     I->setOperand(
708         0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
709     I->setOperand(
710         1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
711     return I;
712 
713   case Instruction::Shl:
714   case Instruction::LShr:
715     return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
716                             IC.Builder);
717 
718   case Instruction::Select:
719     I->setOperand(
720         1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
721     I->setOperand(
722         2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
723     return I;
724   case Instruction::PHI: {
725     // We can change a phi if we can change all operands.  Note that we never
726     // get into trouble with cyclic PHIs here because we only consider
727     // instructions with a single use.
728     PHINode *PN = cast<PHINode>(I);
729     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
730       PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits,
731                                               isLeftShift, IC, DL));
732     return PN;
733   }
734   case Instruction::Mul: {
735     assert(!isLeftShift && "Unexpected shift direction!");
736     auto *Neg = BinaryOperator::CreateNeg(I->getOperand(0));
737     IC.InsertNewInstWith(Neg, I->getIterator());
738     unsigned TypeWidth = I->getType()->getScalarSizeInBits();
739     APInt Mask = APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits);
740     auto *And = BinaryOperator::CreateAnd(Neg,
741                                           ConstantInt::get(I->getType(), Mask));
742     And->takeName(I);
743     return IC.InsertNewInstWith(And, I->getIterator());
744   }
745   }
746 }
747 
748 // If this is a bitwise operator or add with a constant RHS we might be able
749 // to pull it through a shift.
canShiftBinOpWithConstantRHS(BinaryOperator & Shift,BinaryOperator * BO)750 static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift,
751                                          BinaryOperator *BO) {
752   switch (BO->getOpcode()) {
753   default:
754     return false; // Do not perform transform!
755   case Instruction::Add:
756     return Shift.getOpcode() == Instruction::Shl;
757   case Instruction::Or:
758   case Instruction::And:
759     return true;
760   case Instruction::Xor:
761     // Do not change a 'not' of logical shift because that would create a normal
762     // 'xor'. The 'not' is likely better for analysis, SCEV, and codegen.
763     return !(Shift.isLogicalShift() && match(BO, m_Not(m_Value())));
764   }
765 }
766 
FoldShiftByConstant(Value * Op0,Constant * C1,BinaryOperator & I)767 Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *C1,
768                                                    BinaryOperator &I) {
769   // (C2 << X) << C1 --> (C2 << C1) << X
770   // (C2 >> X) >> C1 --> (C2 >> C1) >> X
771   Constant *C2;
772   Value *X;
773   bool IsLeftShift = I.getOpcode() == Instruction::Shl;
774   if (match(Op0, m_BinOp(I.getOpcode(), m_ImmConstant(C2), m_Value(X)))) {
775     Instruction *R = BinaryOperator::Create(
776         I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), C2, C1), X);
777     BinaryOperator *BO0 = cast<BinaryOperator>(Op0);
778     if (IsLeftShift) {
779       R->setHasNoUnsignedWrap(I.hasNoUnsignedWrap() &&
780                               BO0->hasNoUnsignedWrap());
781       R->setHasNoSignedWrap(I.hasNoSignedWrap() && BO0->hasNoSignedWrap());
782     } else
783       R->setIsExact(I.isExact() && BO0->isExact());
784     return R;
785   }
786 
787   Type *Ty = I.getType();
788   unsigned TypeBits = Ty->getScalarSizeInBits();
789 
790   // (X / +DivC) >> (Width - 1) --> ext (X <= -DivC)
791   // (X / -DivC) >> (Width - 1) --> ext (X >= +DivC)
792   const APInt *DivC;
793   if (!IsLeftShift && match(C1, m_SpecificIntAllowPoison(TypeBits - 1)) &&
794       match(Op0, m_SDiv(m_Value(X), m_APInt(DivC))) && !DivC->isZero() &&
795       !DivC->isMinSignedValue()) {
796     Constant *NegDivC = ConstantInt::get(Ty, -(*DivC));
797     ICmpInst::Predicate Pred =
798         DivC->isNegative() ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SLE;
799     Value *Cmp = Builder.CreateICmp(Pred, X, NegDivC);
800     auto ExtOpcode = (I.getOpcode() == Instruction::AShr) ? Instruction::SExt
801                                                           : Instruction::ZExt;
802     return CastInst::Create(ExtOpcode, Cmp, Ty);
803   }
804 
805   const APInt *Op1C;
806   if (!match(C1, m_APInt(Op1C)))
807     return nullptr;
808 
809   assert(!Op1C->uge(TypeBits) &&
810          "Shift over the type width should have been removed already");
811 
812   // See if we can propagate this shift into the input, this covers the trivial
813   // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
814   if (I.getOpcode() != Instruction::AShr &&
815       canEvaluateShifted(Op0, Op1C->getZExtValue(), IsLeftShift, *this, &I)) {
816     LLVM_DEBUG(
817         dbgs() << "ICE: GetShiftedValue propagating shift through expression"
818                   " to eliminate shift:\n  IN: "
819                << *Op0 << "\n  SH: " << I << "\n");
820 
821     return replaceInstUsesWith(
822         I, getShiftedValue(Op0, Op1C->getZExtValue(), IsLeftShift, *this, DL));
823   }
824 
825   if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
826     return FoldedShift;
827 
828   if (!Op0->hasOneUse())
829     return nullptr;
830 
831   if (auto *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
832     // If the operand is a bitwise operator with a constant RHS, and the
833     // shift is the only use, we can pull it out of the shift.
834     const APInt *Op0C;
835     if (match(Op0BO->getOperand(1), m_APInt(Op0C))) {
836       if (canShiftBinOpWithConstantRHS(I, Op0BO)) {
837         Value *NewRHS =
838             Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(1), C1);
839 
840         Value *NewShift =
841             Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), C1);
842         NewShift->takeName(Op0BO);
843 
844         return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, NewRHS);
845       }
846     }
847   }
848 
849   // If we have a select that conditionally executes some binary operator,
850   // see if we can pull it the select and operator through the shift.
851   //
852   // For example, turning:
853   //   shl (select C, (add X, C1), X), C2
854   // Into:
855   //   Y = shl X, C2
856   //   select C, (add Y, C1 << C2), Y
857   Value *Cond;
858   BinaryOperator *TBO;
859   Value *FalseVal;
860   if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)),
861                           m_Value(FalseVal)))) {
862     const APInt *C;
863     if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal &&
864         match(TBO->getOperand(1), m_APInt(C)) &&
865         canShiftBinOpWithConstantRHS(I, TBO)) {
866       Value *NewRHS =
867           Builder.CreateBinOp(I.getOpcode(), TBO->getOperand(1), C1);
868 
869       Value *NewShift = Builder.CreateBinOp(I.getOpcode(), FalseVal, C1);
870       Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift, NewRHS);
871       return SelectInst::Create(Cond, NewOp, NewShift);
872     }
873   }
874 
875   BinaryOperator *FBO;
876   Value *TrueVal;
877   if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal),
878                           m_OneUse(m_BinOp(FBO))))) {
879     const APInt *C;
880     if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal &&
881         match(FBO->getOperand(1), m_APInt(C)) &&
882         canShiftBinOpWithConstantRHS(I, FBO)) {
883       Value *NewRHS =
884           Builder.CreateBinOp(I.getOpcode(), FBO->getOperand(1), C1);
885 
886       Value *NewShift = Builder.CreateBinOp(I.getOpcode(), TrueVal, C1);
887       Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift, NewRHS);
888       return SelectInst::Create(Cond, NewShift, NewOp);
889     }
890   }
891 
892   return nullptr;
893 }
894 
895 // Tries to perform
896 //    (lshr (add (zext X), (zext Y)), K)
897 //      -> (icmp ult (add X, Y), X)
898 //    where
899 //      - The add's operands are zexts from a K-bits integer to a bigger type.
900 //      - The add is only used by the shr, or by iK (or narrower) truncates.
901 //      - The lshr type has more than 2 bits (other types are boolean math).
902 //      - K > 1
903 //    note that
904 //      - The resulting add cannot have nuw/nsw, else on overflow we get a
905 //        poison value and the transform isn't legal anymore.
foldLShrOverflowBit(BinaryOperator & I)906 Instruction *InstCombinerImpl::foldLShrOverflowBit(BinaryOperator &I) {
907   assert(I.getOpcode() == Instruction::LShr);
908 
909   Value *Add = I.getOperand(0);
910   Value *ShiftAmt = I.getOperand(1);
911   Type *Ty = I.getType();
912 
913   if (Ty->getScalarSizeInBits() < 3)
914     return nullptr;
915 
916   const APInt *ShAmtAPInt = nullptr;
917   Value *X = nullptr, *Y = nullptr;
918   if (!match(ShiftAmt, m_APInt(ShAmtAPInt)) ||
919       !match(Add,
920              m_Add(m_OneUse(m_ZExt(m_Value(X))), m_OneUse(m_ZExt(m_Value(Y))))))
921     return nullptr;
922 
923   const unsigned ShAmt = ShAmtAPInt->getZExtValue();
924   if (ShAmt == 1)
925     return nullptr;
926 
927   // X/Y are zexts from `ShAmt`-sized ints.
928   if (X->getType()->getScalarSizeInBits() != ShAmt ||
929       Y->getType()->getScalarSizeInBits() != ShAmt)
930     return nullptr;
931 
932   // Make sure that `Add` is only used by `I` and `ShAmt`-truncates.
933   if (!Add->hasOneUse()) {
934     for (User *U : Add->users()) {
935       if (U == &I)
936         continue;
937 
938       TruncInst *Trunc = dyn_cast<TruncInst>(U);
939       if (!Trunc || Trunc->getType()->getScalarSizeInBits() > ShAmt)
940         return nullptr;
941     }
942   }
943 
944   // Insert at Add so that the newly created `NarrowAdd` will dominate it's
945   // users (i.e. `Add`'s users).
946   Instruction *AddInst = cast<Instruction>(Add);
947   Builder.SetInsertPoint(AddInst);
948 
949   Value *NarrowAdd = Builder.CreateAdd(X, Y, "add.narrowed");
950   Value *Overflow =
951       Builder.CreateICmpULT(NarrowAdd, X, "add.narrowed.overflow");
952 
953   // Replace the uses of the original add with a zext of the
954   // NarrowAdd's result. Note that all users at this stage are known to
955   // be ShAmt-sized truncs, or the lshr itself.
956   if (!Add->hasOneUse()) {
957     replaceInstUsesWith(*AddInst, Builder.CreateZExt(NarrowAdd, Ty));
958     eraseInstFromFunction(*AddInst);
959   }
960 
961   // Replace the LShr with a zext of the overflow check.
962   return new ZExtInst(Overflow, Ty);
963 }
964 
965 // Try to set nuw/nsw flags on shl or exact flag on lshr/ashr using knownbits.
setShiftFlags(BinaryOperator & I,const SimplifyQuery & Q)966 static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) {
967   assert(I.isShift() && "Expected a shift as input");
968   // We already have all the flags.
969   if (I.getOpcode() == Instruction::Shl) {
970     if (I.hasNoUnsignedWrap() && I.hasNoSignedWrap())
971       return false;
972   } else {
973     if (I.isExact())
974       return false;
975 
976     // shr (shl X, Y), Y
977     if (match(I.getOperand(0), m_Shl(m_Value(), m_Specific(I.getOperand(1))))) {
978       I.setIsExact();
979       return true;
980     }
981   }
982 
983   // Compute what we know about shift count.
984   KnownBits KnownCnt = computeKnownBits(I.getOperand(1), /* Depth */ 0, Q);
985   unsigned BitWidth = KnownCnt.getBitWidth();
986   // Since shift produces a poison value if RHS is equal to or larger than the
987   // bit width, we can safely assume that RHS is less than the bit width.
988   uint64_t MaxCnt = KnownCnt.getMaxValue().getLimitedValue(BitWidth - 1);
989 
990   KnownBits KnownAmt = computeKnownBits(I.getOperand(0), /* Depth */ 0, Q);
991   bool Changed = false;
992 
993   if (I.getOpcode() == Instruction::Shl) {
994     // If we have as many leading zeros than maximum shift cnt we have nuw.
995     if (!I.hasNoUnsignedWrap() && MaxCnt <= KnownAmt.countMinLeadingZeros()) {
996       I.setHasNoUnsignedWrap();
997       Changed = true;
998     }
999     // If we have more sign bits than maximum shift cnt we have nsw.
1000     if (!I.hasNoSignedWrap()) {
1001       if (MaxCnt < KnownAmt.countMinSignBits() ||
1002           MaxCnt < ComputeNumSignBits(I.getOperand(0), Q.DL, /*Depth*/ 0, Q.AC,
1003                                       Q.CxtI, Q.DT)) {
1004         I.setHasNoSignedWrap();
1005         Changed = true;
1006       }
1007     }
1008     return Changed;
1009   }
1010 
1011   // If we have at least as many trailing zeros as maximum count then we have
1012   // exact.
1013   Changed = MaxCnt <= KnownAmt.countMinTrailingZeros();
1014   I.setIsExact(Changed);
1015 
1016   return Changed;
1017 }
1018 
visitShl(BinaryOperator & I)1019 Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
1020   const SimplifyQuery Q = SQ.getWithInstruction(&I);
1021 
1022   if (Value *V = simplifyShlInst(I.getOperand(0), I.getOperand(1),
1023                                  I.hasNoSignedWrap(), I.hasNoUnsignedWrap(), Q))
1024     return replaceInstUsesWith(I, V);
1025 
1026   if (Instruction *X = foldVectorBinop(I))
1027     return X;
1028 
1029   if (Instruction *V = commonShiftTransforms(I))
1030     return V;
1031 
1032   if (Instruction *V = dropRedundantMaskingOfLeftShiftInput(&I, Q, Builder))
1033     return V;
1034 
1035   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1036   Type *Ty = I.getType();
1037   unsigned BitWidth = Ty->getScalarSizeInBits();
1038 
1039   const APInt *C;
1040   if (match(Op1, m_APInt(C))) {
1041     unsigned ShAmtC = C->getZExtValue();
1042 
1043     // shl (zext X), C --> zext (shl X, C)
1044     // This is only valid if X would have zeros shifted out.
1045     Value *X;
1046     if (match(Op0, m_OneUse(m_ZExt(m_Value(X))))) {
1047       unsigned SrcWidth = X->getType()->getScalarSizeInBits();
1048       if (ShAmtC < SrcWidth &&
1049           MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmtC), 0, &I))
1050         return new ZExtInst(Builder.CreateShl(X, ShAmtC), Ty);
1051     }
1052 
1053     // (X >> C) << C --> X & (-1 << C)
1054     if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) {
1055       APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
1056       return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
1057     }
1058 
1059     const APInt *C1;
1060     if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(C1)))) &&
1061         C1->ult(BitWidth)) {
1062       unsigned ShrAmt = C1->getZExtValue();
1063       if (ShrAmt < ShAmtC) {
1064         // If C1 < C: (X >>?,exact C1) << C --> X << (C - C1)
1065         Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt);
1066         auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
1067         NewShl->setHasNoUnsignedWrap(
1068             I.hasNoUnsignedWrap() ||
1069             (ShrAmt &&
1070              cast<Instruction>(Op0)->getOpcode() == Instruction::LShr &&
1071              I.hasNoSignedWrap()));
1072         NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
1073         return NewShl;
1074       }
1075       if (ShrAmt > ShAmtC) {
1076         // If C1 > C: (X >>?exact C1) << C --> X >>?exact (C1 - C)
1077         Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC);
1078         auto *NewShr = BinaryOperator::Create(
1079             cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
1080         NewShr->setIsExact(true);
1081         return NewShr;
1082       }
1083     }
1084 
1085     if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_APInt(C1)))) &&
1086         C1->ult(BitWidth)) {
1087       unsigned ShrAmt = C1->getZExtValue();
1088       if (ShrAmt < ShAmtC) {
1089         // If C1 < C: (X >>? C1) << C --> (X << (C - C1)) & (-1 << C)
1090         Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt);
1091         auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
1092         NewShl->setHasNoUnsignedWrap(
1093             I.hasNoUnsignedWrap() ||
1094             (ShrAmt &&
1095              cast<Instruction>(Op0)->getOpcode() == Instruction::LShr &&
1096              I.hasNoSignedWrap()));
1097         NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
1098         Builder.Insert(NewShl);
1099         APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
1100         return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
1101       }
1102       if (ShrAmt > ShAmtC) {
1103         // If C1 > C: (X >>? C1) << C --> (X >>? (C1 - C)) & (-1 << C)
1104         Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC);
1105         auto *OldShr = cast<BinaryOperator>(Op0);
1106         auto *NewShr =
1107             BinaryOperator::Create(OldShr->getOpcode(), X, ShiftDiff);
1108         NewShr->setIsExact(OldShr->isExact());
1109         Builder.Insert(NewShr);
1110         APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
1111         return BinaryOperator::CreateAnd(NewShr, ConstantInt::get(Ty, Mask));
1112       }
1113     }
1114 
1115     // Similar to above, but look through an intermediate trunc instruction.
1116     BinaryOperator *Shr;
1117     if (match(Op0, m_OneUse(m_Trunc(m_OneUse(m_BinOp(Shr))))) &&
1118         match(Shr, m_Shr(m_Value(X), m_APInt(C1)))) {
1119       // The larger shift direction survives through the transform.
1120       unsigned ShrAmtC = C1->getZExtValue();
1121       unsigned ShDiff = ShrAmtC > ShAmtC ? ShrAmtC - ShAmtC : ShAmtC - ShrAmtC;
1122       Constant *ShiftDiffC = ConstantInt::get(X->getType(), ShDiff);
1123       auto ShiftOpc = ShrAmtC > ShAmtC ? Shr->getOpcode() : Instruction::Shl;
1124 
1125       // If C1 > C:
1126       // (trunc (X >> C1)) << C --> (trunc (X >> (C1 - C))) && (-1 << C)
1127       // If C > C1:
1128       // (trunc (X >> C1)) << C --> (trunc (X << (C - C1))) && (-1 << C)
1129       Value *NewShift = Builder.CreateBinOp(ShiftOpc, X, ShiftDiffC, "sh.diff");
1130       Value *Trunc = Builder.CreateTrunc(NewShift, Ty, "tr.sh.diff");
1131       APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
1132       return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, Mask));
1133     }
1134 
1135     // If we have an opposite shift by the same amount, we may be able to
1136     // reorder binops and shifts to eliminate math/logic.
1137     auto isSuitableBinOpcode = [](Instruction::BinaryOps BinOpcode) {
1138       switch (BinOpcode) {
1139       default:
1140         return false;
1141       case Instruction::Add:
1142       case Instruction::And:
1143       case Instruction::Or:
1144       case Instruction::Xor:
1145       case Instruction::Sub:
1146         // NOTE: Sub is not commutable and the tranforms below may not be valid
1147         //       when the shift-right is operand 1 (RHS) of the sub.
1148         return true;
1149       }
1150     };
1151     BinaryOperator *Op0BO;
1152     if (match(Op0, m_OneUse(m_BinOp(Op0BO))) &&
1153         isSuitableBinOpcode(Op0BO->getOpcode())) {
1154       // Commute so shift-right is on LHS of the binop.
1155       // (Y bop (X >> C)) << C         ->  ((X >> C) bop Y) << C
1156       // (Y bop ((X >> C) & CC)) << C  ->  (((X >> C) & CC) bop Y) << C
1157       Value *Shr = Op0BO->getOperand(0);
1158       Value *Y = Op0BO->getOperand(1);
1159       Value *X;
1160       const APInt *CC;
1161       if (Op0BO->isCommutative() && Y->hasOneUse() &&
1162           (match(Y, m_Shr(m_Value(), m_Specific(Op1))) ||
1163            match(Y, m_And(m_OneUse(m_Shr(m_Value(), m_Specific(Op1))),
1164                           m_APInt(CC)))))
1165         std::swap(Shr, Y);
1166 
1167       // ((X >> C) bop Y) << C  ->  (X bop (Y << C)) & (~0 << C)
1168       if (match(Shr, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
1169         // Y << C
1170         Value *YS = Builder.CreateShl(Y, Op1, Op0BO->getName());
1171         // (X bop (Y << C))
1172         Value *B =
1173             Builder.CreateBinOp(Op0BO->getOpcode(), X, YS, Shr->getName());
1174         unsigned Op1Val = C->getLimitedValue(BitWidth);
1175         APInt Bits = APInt::getHighBitsSet(BitWidth, BitWidth - Op1Val);
1176         Constant *Mask = ConstantInt::get(Ty, Bits);
1177         return BinaryOperator::CreateAnd(B, Mask);
1178       }
1179 
1180       // (((X >> C) & CC) bop Y) << C  ->  (X & (CC << C)) bop (Y << C)
1181       if (match(Shr,
1182                 m_OneUse(m_And(m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))),
1183                                m_APInt(CC))))) {
1184         // Y << C
1185         Value *YS = Builder.CreateShl(Y, Op1, Op0BO->getName());
1186         // X & (CC << C)
1187         Value *M = Builder.CreateAnd(X, ConstantInt::get(Ty, CC->shl(*C)),
1188                                      X->getName() + ".mask");
1189         auto *NewOp = BinaryOperator::Create(Op0BO->getOpcode(), M, YS);
1190         if (auto *Disjoint = dyn_cast<PossiblyDisjointInst>(Op0BO);
1191             Disjoint && Disjoint->isDisjoint())
1192           cast<PossiblyDisjointInst>(NewOp)->setIsDisjoint(true);
1193         return NewOp;
1194       }
1195     }
1196 
1197     // (C1 - X) << C --> (C1 << C) - (X << C)
1198     if (match(Op0, m_OneUse(m_Sub(m_APInt(C1), m_Value(X))))) {
1199       Constant *NewLHS = ConstantInt::get(Ty, C1->shl(*C));
1200       Value *NewShift = Builder.CreateShl(X, Op1);
1201       return BinaryOperator::CreateSub(NewLHS, NewShift);
1202     }
1203   }
1204 
1205   if (setShiftFlags(I, Q))
1206     return &I;
1207 
1208   // Transform  (x >> y) << y  to  x & (-1 << y)
1209   // Valid for any type of right-shift.
1210   Value *X;
1211   if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
1212     Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
1213     Value *Mask = Builder.CreateShl(AllOnes, Op1);
1214     return BinaryOperator::CreateAnd(Mask, X);
1215   }
1216 
1217   // Transform  (-1 >> y) << y  to -1 << y
1218   if (match(Op0, m_LShr(m_AllOnes(), m_Specific(Op1)))) {
1219     Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
1220     return BinaryOperator::CreateShl(AllOnes, Op1);
1221   }
1222 
1223   Constant *C1;
1224   if (match(Op1, m_ImmConstant(C1))) {
1225     Constant *C2;
1226     Value *X;
1227     // (X * C2) << C1 --> X * (C2 << C1)
1228     if (match(Op0, m_Mul(m_Value(X), m_ImmConstant(C2))))
1229       return BinaryOperator::CreateMul(X, Builder.CreateShl(C2, C1));
1230 
1231     // shl (zext i1 X), C1 --> select (X, 1 << C1, 0)
1232     if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
1233       auto *NewC = Builder.CreateShl(ConstantInt::get(Ty, 1), C1);
1234       return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
1235     }
1236   }
1237 
1238   if (match(Op0, m_One())) {
1239     // (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1
1240     if (match(Op1, m_Sub(m_SpecificInt(BitWidth - 1), m_Value(X))))
1241       return BinaryOperator::CreateLShr(
1242           ConstantInt::get(Ty, APInt::getSignMask(BitWidth)), X);
1243 
1244     // Canonicalize "extract lowest set bit" using cttz to and-with-negate:
1245     // 1 << (cttz X) --> -X & X
1246     if (match(Op1,
1247               m_OneUse(m_Intrinsic<Intrinsic::cttz>(m_Value(X), m_Value())))) {
1248       Value *NegX = Builder.CreateNeg(X, "neg");
1249       return BinaryOperator::CreateAnd(NegX, X);
1250     }
1251   }
1252 
1253   return nullptr;
1254 }
1255 
visitLShr(BinaryOperator & I)1256 Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
1257   if (Value *V = simplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
1258                                   SQ.getWithInstruction(&I)))
1259     return replaceInstUsesWith(I, V);
1260 
1261   if (Instruction *X = foldVectorBinop(I))
1262     return X;
1263 
1264   if (Instruction *R = commonShiftTransforms(I))
1265     return R;
1266 
1267   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1268   Type *Ty = I.getType();
1269   Value *X;
1270   const APInt *C;
1271   unsigned BitWidth = Ty->getScalarSizeInBits();
1272 
1273   // (iN (~X) u>> (N - 1)) --> zext (X > -1)
1274   if (match(Op0, m_OneUse(m_Not(m_Value(X)))) &&
1275       match(Op1, m_SpecificIntAllowPoison(BitWidth - 1)))
1276     return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty);
1277 
1278   // ((X << nuw Z) sub nuw Y) >>u exact Z --> X sub nuw (Y >>u exact Z)
1279   Value *Y;
1280   if (I.isExact() &&
1281       match(Op0, m_OneUse(m_NUWSub(m_NUWShl(m_Value(X), m_Specific(Op1)),
1282                                    m_Value(Y))))) {
1283     Value *NewLshr = Builder.CreateLShr(Y, Op1, "", /*isExact=*/true);
1284     auto *NewSub = BinaryOperator::CreateNUWSub(X, NewLshr);
1285     NewSub->setHasNoSignedWrap(
1286         cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap());
1287     return NewSub;
1288   }
1289 
1290   // Fold (X + Y) / 2 --> (X & Y) iff (X u<= 1) && (Y u<= 1)
1291   if (match(Op0, m_Add(m_Value(X), m_Value(Y))) && match(Op1, m_One()) &&
1292       computeKnownBits(X, /*Depth=*/0, &I).countMaxActiveBits() <= 1 &&
1293       computeKnownBits(Y, /*Depth=*/0, &I).countMaxActiveBits() <= 1)
1294     return BinaryOperator::CreateAnd(X, Y);
1295 
1296   // (sub nuw X, (Y << nuw Z)) >>u exact Z --> (X >>u exact Z) sub nuw Y
1297   if (I.isExact() &&
1298       match(Op0, m_OneUse(m_NUWSub(m_Value(X),
1299                                    m_NUWShl(m_Value(Y), m_Specific(Op1)))))) {
1300     Value *NewLshr = Builder.CreateLShr(X, Op1, "", /*isExact=*/true);
1301     auto *NewSub = BinaryOperator::CreateNUWSub(NewLshr, Y);
1302     NewSub->setHasNoSignedWrap(
1303         cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap());
1304     return NewSub;
1305   }
1306 
1307   auto isSuitableBinOpcode = [](Instruction::BinaryOps BinOpcode) {
1308     switch (BinOpcode) {
1309     default:
1310       return false;
1311     case Instruction::Add:
1312     case Instruction::And:
1313     case Instruction::Or:
1314     case Instruction::Xor:
1315       // Sub is handled separately.
1316       return true;
1317     }
1318   };
1319 
1320   // If both the binop and the shift are nuw, then:
1321   // ((X << nuw Z) binop nuw Y) >>u Z --> X binop nuw (Y >>u Z)
1322   if (match(Op0, m_OneUse(m_c_BinOp(m_NUWShl(m_Value(X), m_Specific(Op1)),
1323                                     m_Value(Y))))) {
1324     BinaryOperator *Op0OB = cast<BinaryOperator>(Op0);
1325     if (isSuitableBinOpcode(Op0OB->getOpcode())) {
1326       if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op0);
1327           !OBO || OBO->hasNoUnsignedWrap()) {
1328         Value *NewLshr = Builder.CreateLShr(
1329             Y, Op1, "", I.isExact() && Op0OB->getOpcode() != Instruction::And);
1330         auto *NewBinOp = BinaryOperator::Create(Op0OB->getOpcode(), NewLshr, X);
1331         if (OBO) {
1332           NewBinOp->setHasNoUnsignedWrap(true);
1333           NewBinOp->setHasNoSignedWrap(OBO->hasNoSignedWrap());
1334         } else if (auto *Disjoint = dyn_cast<PossiblyDisjointInst>(Op0)) {
1335           cast<PossiblyDisjointInst>(NewBinOp)->setIsDisjoint(
1336               Disjoint->isDisjoint());
1337         }
1338         return NewBinOp;
1339       }
1340     }
1341   }
1342 
1343   if (match(Op1, m_APInt(C))) {
1344     unsigned ShAmtC = C->getZExtValue();
1345     auto *II = dyn_cast<IntrinsicInst>(Op0);
1346     if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmtC &&
1347         (II->getIntrinsicID() == Intrinsic::ctlz ||
1348          II->getIntrinsicID() == Intrinsic::cttz ||
1349          II->getIntrinsicID() == Intrinsic::ctpop)) {
1350       // ctlz.i32(x)>>5  --> zext(x == 0)
1351       // cttz.i32(x)>>5  --> zext(x == 0)
1352       // ctpop.i32(x)>>5 --> zext(x == -1)
1353       bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
1354       Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
1355       Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
1356       return new ZExtInst(Cmp, Ty);
1357     }
1358 
1359     const APInt *C1;
1360     if (match(Op0, m_Shl(m_Value(X), m_APInt(C1))) && C1->ult(BitWidth)) {
1361       if (C1->ult(ShAmtC)) {
1362         unsigned ShlAmtC = C1->getZExtValue();
1363         Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShlAmtC);
1364         if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
1365           // (X <<nuw C1) >>u C --> X >>u (C - C1)
1366           auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
1367           NewLShr->setIsExact(I.isExact());
1368           return NewLShr;
1369         }
1370         if (Op0->hasOneUse()) {
1371           // (X << C1) >>u C  --> (X >>u (C - C1)) & (-1 >> C)
1372           Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact());
1373           APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
1374           return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
1375         }
1376       } else if (C1->ugt(ShAmtC)) {
1377         unsigned ShlAmtC = C1->getZExtValue();
1378         Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmtC - ShAmtC);
1379         if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
1380           // (X <<nuw C1) >>u C --> X <<nuw/nsw (C1 - C)
1381           auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
1382           NewShl->setHasNoUnsignedWrap(true);
1383           NewShl->setHasNoSignedWrap(ShAmtC > 0);
1384           return NewShl;
1385         }
1386         if (Op0->hasOneUse()) {
1387           // (X << C1) >>u C  --> X << (C1 - C) & (-1 >> C)
1388           Value *NewShl = Builder.CreateShl(X, ShiftDiff);
1389           APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
1390           return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
1391         }
1392       } else {
1393         assert(*C1 == ShAmtC);
1394         // (X << C) >>u C --> X & (-1 >>u C)
1395         APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
1396         return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
1397       }
1398     }
1399 
1400     // ((X << C) + Y) >>u C --> (X + (Y >>u C)) & (-1 >>u C)
1401     // TODO: Consolidate with the more general transform that starts from shl
1402     //       (the shifts are in the opposite order).
1403     if (match(Op0,
1404               m_OneUse(m_c_Add(m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))),
1405                                m_Value(Y))))) {
1406       Value *NewLshr = Builder.CreateLShr(Y, Op1);
1407       Value *NewAdd = Builder.CreateAdd(NewLshr, X);
1408       unsigned Op1Val = C->getLimitedValue(BitWidth);
1409       APInt Bits = APInt::getLowBitsSet(BitWidth, BitWidth - Op1Val);
1410       Constant *Mask = ConstantInt::get(Ty, Bits);
1411       return BinaryOperator::CreateAnd(NewAdd, Mask);
1412     }
1413 
1414     if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) &&
1415         (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
1416       assert(ShAmtC < X->getType()->getScalarSizeInBits() &&
1417              "Big shift not simplified to zero?");
1418       // lshr (zext iM X to iN), C --> zext (lshr X, C) to iN
1419       Value *NewLShr = Builder.CreateLShr(X, ShAmtC);
1420       return new ZExtInst(NewLShr, Ty);
1421     }
1422 
1423     if (match(Op0, m_SExt(m_Value(X)))) {
1424       unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits();
1425       // lshr (sext i1 X to iN), C --> select (X, -1 >> C, 0)
1426       if (SrcTyBitWidth == 1) {
1427         auto *NewC = ConstantInt::get(
1428             Ty, APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
1429         return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
1430       }
1431 
1432       if ((!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType())) &&
1433           Op0->hasOneUse()) {
1434         // Are we moving the sign bit to the low bit and widening with high
1435         // zeros? lshr (sext iM X to iN), N-1 --> zext (lshr X, M-1) to iN
1436         if (ShAmtC == BitWidth - 1) {
1437           Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
1438           return new ZExtInst(NewLShr, Ty);
1439         }
1440 
1441         // lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN
1442         if (ShAmtC == BitWidth - SrcTyBitWidth) {
1443           // The new shift amount can't be more than the narrow source type.
1444           unsigned NewShAmt = std::min(ShAmtC, SrcTyBitWidth - 1);
1445           Value *AShr = Builder.CreateAShr(X, NewShAmt);
1446           return new ZExtInst(AShr, Ty);
1447         }
1448       }
1449     }
1450 
1451     if (ShAmtC == BitWidth - 1) {
1452       // lshr i32 or(X,-X), 31 --> zext (X != 0)
1453       if (match(Op0, m_OneUse(m_c_Or(m_Neg(m_Value(X)), m_Deferred(X)))))
1454         return new ZExtInst(Builder.CreateIsNotNull(X), Ty);
1455 
1456       // lshr i32 (X -nsw Y), 31 --> zext (X < Y)
1457       if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
1458         return new ZExtInst(Builder.CreateICmpSLT(X, Y), Ty);
1459 
1460       // Check if a number is negative and odd:
1461       // lshr i32 (srem X, 2), 31 --> and (X >> 31), X
1462       if (match(Op0, m_OneUse(m_SRem(m_Value(X), m_SpecificInt(2))))) {
1463         Value *Signbit = Builder.CreateLShr(X, ShAmtC);
1464         return BinaryOperator::CreateAnd(Signbit, X);
1465       }
1466     }
1467 
1468     Instruction *TruncSrc;
1469     if (match(Op0, m_OneUse(m_Trunc(m_Instruction(TruncSrc)))) &&
1470         match(TruncSrc, m_LShr(m_Value(X), m_APInt(C1)))) {
1471       unsigned SrcWidth = X->getType()->getScalarSizeInBits();
1472       unsigned AmtSum = ShAmtC + C1->getZExtValue();
1473 
1474       // If the combined shift fits in the source width:
1475       // (trunc (X >>u C1)) >>u C --> and (trunc (X >>u (C1 + C)), MaskC
1476       //
1477       // If the first shift covers the number of bits truncated, then the
1478       // mask instruction is eliminated (and so the use check is relaxed).
1479       if (AmtSum < SrcWidth &&
1480           (TruncSrc->hasOneUse() || C1->uge(SrcWidth - BitWidth))) {
1481         Value *SumShift = Builder.CreateLShr(X, AmtSum, "sum.shift");
1482         Value *Trunc = Builder.CreateTrunc(SumShift, Ty, I.getName());
1483 
1484         // If the first shift does not cover the number of bits truncated, then
1485         // we require a mask to get rid of high bits in the result.
1486         APInt MaskC = APInt::getAllOnes(BitWidth).lshr(ShAmtC);
1487         return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, MaskC));
1488       }
1489     }
1490 
1491     const APInt *MulC;
1492     if (match(Op0, m_NUWMul(m_Value(X), m_APInt(MulC)))) {
1493       if (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
1494           MulC->logBase2() == ShAmtC) {
1495         // Look for a "splat" mul pattern - it replicates bits across each half
1496         // of a value, so a right shift simplifies back to just X:
1497         // lshr i[2N] (mul nuw X, (2^N)+1), N --> X
1498         if (ShAmtC * 2 == BitWidth)
1499           return replaceInstUsesWith(I, X);
1500 
1501         // lshr (mul nuw (X, 2^N + 1)), N -> add nuw (X, lshr(X, N))
1502         if (Op0->hasOneUse()) {
1503           auto *NewAdd = BinaryOperator::CreateNUWAdd(
1504               X, Builder.CreateLShr(X, ConstantInt::get(Ty, ShAmtC), "",
1505                                     I.isExact()));
1506           NewAdd->setHasNoSignedWrap(
1507               cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap());
1508           return NewAdd;
1509         }
1510       }
1511 
1512       // The one-use check is not strictly necessary, but codegen may not be
1513       // able to invert the transform and perf may suffer with an extra mul
1514       // instruction.
1515       if (Op0->hasOneUse()) {
1516         APInt NewMulC = MulC->lshr(ShAmtC);
1517         // if c is divisible by (1 << ShAmtC):
1518         // lshr (mul nuw x, MulC), ShAmtC -> mul nuw nsw x, (MulC >> ShAmtC)
1519         if (MulC->eq(NewMulC.shl(ShAmtC))) {
1520           auto *NewMul =
1521               BinaryOperator::CreateNUWMul(X, ConstantInt::get(Ty, NewMulC));
1522           assert(ShAmtC != 0 &&
1523                  "lshr X, 0 should be handled by simplifyLShrInst.");
1524           NewMul->setHasNoSignedWrap(true);
1525           return NewMul;
1526         }
1527       }
1528     }
1529 
1530     // lshr (mul nsw (X, 2^N + 1)), N -> add nsw (X, lshr(X, N))
1531     if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_APInt(MulC))))) {
1532       if (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
1533           MulC->logBase2() == ShAmtC) {
1534         return BinaryOperator::CreateNSWAdd(
1535             X, Builder.CreateLShr(X, ConstantInt::get(Ty, ShAmtC), "",
1536                                   I.isExact()));
1537       }
1538     }
1539 
1540     // Try to narrow bswap.
1541     // In the case where the shift amount equals the bitwidth difference, the
1542     // shift is eliminated.
1543     if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::bswap>(
1544                        m_OneUse(m_ZExt(m_Value(X))))))) {
1545       unsigned SrcWidth = X->getType()->getScalarSizeInBits();
1546       unsigned WidthDiff = BitWidth - SrcWidth;
1547       if (SrcWidth % 16 == 0) {
1548         Value *NarrowSwap = Builder.CreateUnaryIntrinsic(Intrinsic::bswap, X);
1549         if (ShAmtC >= WidthDiff) {
1550           // (bswap (zext X)) >> C --> zext (bswap X >> C')
1551           Value *NewShift = Builder.CreateLShr(NarrowSwap, ShAmtC - WidthDiff);
1552           return new ZExtInst(NewShift, Ty);
1553         } else {
1554           // (bswap (zext X)) >> C --> (zext (bswap X)) << C'
1555           Value *NewZExt = Builder.CreateZExt(NarrowSwap, Ty);
1556           Constant *ShiftDiff = ConstantInt::get(Ty, WidthDiff - ShAmtC);
1557           return BinaryOperator::CreateShl(NewZExt, ShiftDiff);
1558         }
1559       }
1560     }
1561 
1562     // Reduce add-carry of bools to logic:
1563     // ((zext BoolX) + (zext BoolY)) >> 1 --> zext (BoolX && BoolY)
1564     Value *BoolX, *BoolY;
1565     if (ShAmtC == 1 && match(Op0, m_Add(m_Value(X), m_Value(Y))) &&
1566         match(X, m_ZExt(m_Value(BoolX))) && match(Y, m_ZExt(m_Value(BoolY))) &&
1567         BoolX->getType()->isIntOrIntVectorTy(1) &&
1568         BoolY->getType()->isIntOrIntVectorTy(1) &&
1569         (X->hasOneUse() || Y->hasOneUse() || Op0->hasOneUse())) {
1570       Value *And = Builder.CreateAnd(BoolX, BoolY);
1571       return new ZExtInst(And, Ty);
1572     }
1573   }
1574 
1575   const SimplifyQuery Q = SQ.getWithInstruction(&I);
1576   if (setShiftFlags(I, Q))
1577     return &I;
1578 
1579   // Transform  (x << y) >> y  to  x & (-1 >> y)
1580   if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) {
1581     Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
1582     Value *Mask = Builder.CreateLShr(AllOnes, Op1);
1583     return BinaryOperator::CreateAnd(Mask, X);
1584   }
1585 
1586   // Transform  (-1 << y) >> y  to -1 >> y
1587   if (match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1)))) {
1588     Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
1589     return BinaryOperator::CreateLShr(AllOnes, Op1);
1590   }
1591 
1592   if (Instruction *Overflow = foldLShrOverflowBit(I))
1593     return Overflow;
1594 
1595   return nullptr;
1596 }
1597 
1598 Instruction *
foldVariableSignZeroExtensionOfVariableHighBitExtract(BinaryOperator & OldAShr)1599 InstCombinerImpl::foldVariableSignZeroExtensionOfVariableHighBitExtract(
1600     BinaryOperator &OldAShr) {
1601   assert(OldAShr.getOpcode() == Instruction::AShr &&
1602          "Must be called with arithmetic right-shift instruction only.");
1603 
1604   // Check that constant C is a splat of the element-wise bitwidth of V.
1605   auto BitWidthSplat = [](Constant *C, Value *V) {
1606     return match(
1607         C, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
1608                               APInt(C->getType()->getScalarSizeInBits(),
1609                                     V->getType()->getScalarSizeInBits())));
1610   };
1611 
1612   // It should look like variable-length sign-extension on the outside:
1613   //   (Val << (bitwidth(Val)-Nbits)) a>> (bitwidth(Val)-Nbits)
1614   Value *NBits;
1615   Instruction *MaybeTrunc;
1616   Constant *C1, *C2;
1617   if (!match(&OldAShr,
1618              m_AShr(m_Shl(m_Instruction(MaybeTrunc),
1619                           m_ZExtOrSelf(m_Sub(m_Constant(C1),
1620                                              m_ZExtOrSelf(m_Value(NBits))))),
1621                     m_ZExtOrSelf(m_Sub(m_Constant(C2),
1622                                        m_ZExtOrSelf(m_Deferred(NBits)))))) ||
1623       !BitWidthSplat(C1, &OldAShr) || !BitWidthSplat(C2, &OldAShr))
1624     return nullptr;
1625 
1626   // There may or may not be a truncation after outer two shifts.
1627   Instruction *HighBitExtract;
1628   match(MaybeTrunc, m_TruncOrSelf(m_Instruction(HighBitExtract)));
1629   bool HadTrunc = MaybeTrunc != HighBitExtract;
1630 
1631   // And finally, the innermost part of the pattern must be a right-shift.
1632   Value *X, *NumLowBitsToSkip;
1633   if (!match(HighBitExtract, m_Shr(m_Value(X), m_Value(NumLowBitsToSkip))))
1634     return nullptr;
1635 
1636   // Said right-shift must extract high NBits bits - C0 must be it's bitwidth.
1637   Constant *C0;
1638   if (!match(NumLowBitsToSkip,
1639              m_ZExtOrSelf(
1640                  m_Sub(m_Constant(C0), m_ZExtOrSelf(m_Specific(NBits))))) ||
1641       !BitWidthSplat(C0, HighBitExtract))
1642     return nullptr;
1643 
1644   // Since the NBits is identical for all shifts, if the outermost and
1645   // innermost shifts are identical, then outermost shifts are redundant.
1646   // If we had truncation, do keep it though.
1647   if (HighBitExtract->getOpcode() == OldAShr.getOpcode())
1648     return replaceInstUsesWith(OldAShr, MaybeTrunc);
1649 
1650   // Else, if there was a truncation, then we need to ensure that one
1651   // instruction will go away.
1652   if (HadTrunc && !match(&OldAShr, m_c_BinOp(m_OneUse(m_Value()), m_Value())))
1653     return nullptr;
1654 
1655   // Finally, bypass two innermost shifts, and perform the outermost shift on
1656   // the operands of the innermost shift.
1657   Instruction *NewAShr =
1658       BinaryOperator::Create(OldAShr.getOpcode(), X, NumLowBitsToSkip);
1659   NewAShr->copyIRFlags(HighBitExtract); // We can preserve 'exact'-ness.
1660   if (!HadTrunc)
1661     return NewAShr;
1662 
1663   Builder.Insert(NewAShr);
1664   return TruncInst::CreateTruncOrBitCast(NewAShr, OldAShr.getType());
1665 }
1666 
visitAShr(BinaryOperator & I)1667 Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
1668   if (Value *V = simplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
1669                                   SQ.getWithInstruction(&I)))
1670     return replaceInstUsesWith(I, V);
1671 
1672   if (Instruction *X = foldVectorBinop(I))
1673     return X;
1674 
1675   if (Instruction *R = commonShiftTransforms(I))
1676     return R;
1677 
1678   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1679   Type *Ty = I.getType();
1680   unsigned BitWidth = Ty->getScalarSizeInBits();
1681   const APInt *ShAmtAPInt;
1682   if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) {
1683     unsigned ShAmt = ShAmtAPInt->getZExtValue();
1684 
1685     // If the shift amount equals the difference in width of the destination
1686     // and source scalar types:
1687     // ashr (shl (zext X), C), C --> sext X
1688     Value *X;
1689     if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
1690         ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
1691       return new SExtInst(X, Ty);
1692 
1693     // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
1694     // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
1695     const APInt *ShOp1;
1696     if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) &&
1697         ShOp1->ult(BitWidth)) {
1698       unsigned ShlAmt = ShOp1->getZExtValue();
1699       if (ShlAmt < ShAmt) {
1700         // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
1701         Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
1702         auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
1703         NewAShr->setIsExact(I.isExact());
1704         return NewAShr;
1705       }
1706       if (ShlAmt > ShAmt) {
1707         // (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
1708         Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
1709         auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
1710         NewShl->setHasNoSignedWrap(true);
1711         return NewShl;
1712       }
1713     }
1714 
1715     if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) &&
1716         ShOp1->ult(BitWidth)) {
1717       unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
1718       // Oversized arithmetic shifts replicate the sign bit.
1719       AmtSum = std::min(AmtSum, BitWidth - 1);
1720       // (X >>s C1) >>s C2 --> X >>s (C1 + C2)
1721       return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
1722     }
1723 
1724     if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) &&
1725         (Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) {
1726       // ashr (sext X), C --> sext (ashr X, C')
1727       Type *SrcTy = X->getType();
1728       ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1);
1729       Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt));
1730       return new SExtInst(NewSh, Ty);
1731     }
1732 
1733     if (ShAmt == BitWidth - 1) {
1734       // ashr i32 or(X,-X), 31 --> sext (X != 0)
1735       if (match(Op0, m_OneUse(m_c_Or(m_Neg(m_Value(X)), m_Deferred(X)))))
1736         return new SExtInst(Builder.CreateIsNotNull(X), Ty);
1737 
1738       // ashr i32 (X -nsw Y), 31 --> sext (X < Y)
1739       Value *Y;
1740       if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
1741         return new SExtInst(Builder.CreateICmpSLT(X, Y), Ty);
1742     }
1743 
1744     const APInt *MulC;
1745     if (match(Op0, m_OneUse(m_NSWMul(m_Value(X), m_APInt(MulC)))) &&
1746         (BitWidth > 2 && (*MulC - 1).isPowerOf2() &&
1747          MulC->logBase2() == ShAmt &&
1748          (ShAmt < BitWidth - 1))) /* Minus 1 for the sign bit */ {
1749 
1750       // ashr (mul nsw (X, 2^N + 1)), N -> add nsw (X, ashr(X, N))
1751       auto *NewAdd = BinaryOperator::CreateNSWAdd(
1752           X,
1753           Builder.CreateAShr(X, ConstantInt::get(Ty, ShAmt), "", I.isExact()));
1754       NewAdd->setHasNoUnsignedWrap(
1755           cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap());
1756       return NewAdd;
1757     }
1758   }
1759 
1760   const SimplifyQuery Q = SQ.getWithInstruction(&I);
1761   if (setShiftFlags(I, Q))
1762     return &I;
1763 
1764   // Prefer `-(x & 1)` over `(x << (bitwidth(x)-1)) a>> (bitwidth(x)-1)`
1765   // as the pattern to splat the lowest bit.
1766   // FIXME: iff X is already masked, we don't need the one-use check.
1767   Value *X;
1768   if (match(Op1, m_SpecificIntAllowPoison(BitWidth - 1)) &&
1769       match(Op0, m_OneUse(m_Shl(m_Value(X),
1770                                 m_SpecificIntAllowPoison(BitWidth - 1))))) {
1771     Constant *Mask = ConstantInt::get(Ty, 1);
1772     // Retain the knowledge about the ignored lanes.
1773     Mask = Constant::mergeUndefsWith(
1774         Constant::mergeUndefsWith(Mask, cast<Constant>(Op1)),
1775         cast<Constant>(cast<Instruction>(Op0)->getOperand(1)));
1776     X = Builder.CreateAnd(X, Mask);
1777     return BinaryOperator::CreateNeg(X);
1778   }
1779 
1780   if (Instruction *R = foldVariableSignZeroExtensionOfVariableHighBitExtract(I))
1781     return R;
1782 
1783   // See if we can turn a signed shr into an unsigned shr.
1784   if (MaskedValueIsZero(Op0, APInt::getSignMask(BitWidth), 0, &I)) {
1785     Instruction *Lshr = BinaryOperator::CreateLShr(Op0, Op1);
1786     Lshr->setIsExact(I.isExact());
1787     return Lshr;
1788   }
1789 
1790   // ashr (xor %x, -1), %y  -->  xor (ashr %x, %y), -1
1791   if (match(Op0, m_OneUse(m_Not(m_Value(X))))) {
1792     // Note that we must drop 'exact'-ness of the shift!
1793     // Note that we can't keep undef's in -1 vector constant!
1794     auto *NewAShr = Builder.CreateAShr(X, Op1, Op0->getName() + ".not");
1795     return BinaryOperator::CreateNot(NewAShr);
1796   }
1797 
1798   return nullptr;
1799 }
1800