LLVM 19.0.0git
InstCombineShifts.cpp
Go to the documentation of this file.
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"
18using namespace llvm;
19using namespace PatternMatch;
20
21#define DEBUG_TYPE "instcombine"
22
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 =
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.
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,
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?
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,
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)
188static Instruction *
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_Xor(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_AllOnes());
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 auto *ExtendedInvertedMask =
261 ConstantExpr::getShl(ExtendedAllOnes, ExtendedSumOfShAmts);
262 NewMask = ConstantExpr::getNot(ExtendedInvertedMask);
263 } else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X))) ||
264 match(Masked, m_Shr(m_Shl(m_Value(X), m_Value(MaskShAmt)),
265 m_Deferred(MaskShAmt)))) {
266 // Peek through an optional zext of the shift amount.
267 match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
268
269 // Verify that it would be safe to try to add those two shift amounts.
270 if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked,
271 MaskShAmt))
272 return nullptr;
273
274 // Can we simplify (ShiftShAmt-MaskShAmt) ?
275 auto *ShAmtsDiff = dyn_cast_or_null<Constant>(simplifySubInst(
276 ShiftShAmt, MaskShAmt, /*IsNSW=*/false, /*IsNUW=*/false, Q));
277 if (!ShAmtsDiff)
278 return nullptr; // Did not simplify.
279 // In this pattern ShAmtsDiff correlates with the number of high bits that
280 // shall be unset in the root value (OuterShift).
281
282 // An extend of an undef value becomes zero because the high bits are never
283 // completely unknown. Replace the `undef` shift amounts with negated
284 // bitwidth of innermost shift to ensure that the value remains undef when
285 // creating the subsequent shift op.
286 unsigned WidestTyBitWidth = WidestTy->getScalarSizeInBits();
287 ShAmtsDiff = Constant::replaceUndefsWith(
288 ShAmtsDiff, ConstantInt::get(ShAmtsDiff->getType()->getScalarType(),
289 -WidestTyBitWidth));
290 auto *ExtendedNumHighBitsToClear = ConstantFoldCastOperand(
291 Instruction::ZExt,
292 ConstantExpr::getSub(ConstantInt::get(ShAmtsDiff->getType(),
293 WidestTyBitWidth,
294 /*isSigned=*/false),
295 ShAmtsDiff),
296 ExtendedTy, Q.DL);
297 if (!ExtendedNumHighBitsToClear)
298 return nullptr;
299
300 // And compute the mask as usual: (-1 l>> (NumHighBitsToClear))
301 auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy);
302 NewMask = ConstantFoldBinaryOpOperands(Instruction::LShr, ExtendedAllOnes,
303 ExtendedNumHighBitsToClear, Q.DL);
304 if (!NewMask)
305 return nullptr;
306 } else
307 return nullptr; // Don't know anything about this pattern.
308
309 NewMask = ConstantExpr::getTrunc(NewMask, NarrowestTy);
310
311 // Does this mask has any unset bits? If not then we can just not apply it.
312 bool NeedMask = !match(NewMask, m_AllOnes());
313
314 // If we need to apply a mask, there are several more restrictions we have.
315 if (NeedMask) {
316 // The old masking instruction must go away.
317 if (!Masked->hasOneUse())
318 return nullptr;
319 // The original "masking" instruction must not have been`ashr`.
320 if (match(Masked, m_AShr(m_Value(), m_Value())))
321 return nullptr;
322 }
323
324 // If we need to apply truncation, let's do it first, since we can.
325 // We have already ensured that the old truncation will go away.
326 if (HadTrunc)
327 X = Builder.CreateTrunc(X, NarrowestTy);
328
329 // No 'NUW'/'NSW'! We no longer know that we won't shift-out non-0 bits.
330 // We didn't change the Type of this outermost shift, so we can just do it.
331 auto *NewShift = BinaryOperator::Create(OuterShift->getOpcode(), X,
332 OuterShift->getOperand(1));
333 if (!NeedMask)
334 return NewShift;
335
336 Builder.Insert(NewShift);
337 return BinaryOperator::Create(Instruction::And, NewShift, NewMask);
338}
339
340/// If we have a shift-by-constant of a bin op (bitwise logic op or add/sub w/
341/// shl) that itself has a shift-by-constant operand with identical opcode, we
342/// may be able to convert that into 2 independent shifts followed by the logic
343/// op. This eliminates a use of an intermediate value (reduces dependency
344/// chain).
346 InstCombiner::BuilderTy &Builder) {
347 assert(I.isShift() && "Expected a shift as input");
348 auto *BinInst = dyn_cast<BinaryOperator>(I.getOperand(0));
349 if (!BinInst ||
350 (!BinInst->isBitwiseLogicOp() &&
351 BinInst->getOpcode() != Instruction::Add &&
352 BinInst->getOpcode() != Instruction::Sub) ||
353 !BinInst->hasOneUse())
354 return nullptr;
355
356 Constant *C0, *C1;
357 if (!match(I.getOperand(1), m_Constant(C1)))
358 return nullptr;
359
360 Instruction::BinaryOps ShiftOpcode = I.getOpcode();
361 // Transform for add/sub only works with shl.
362 if ((BinInst->getOpcode() == Instruction::Add ||
363 BinInst->getOpcode() == Instruction::Sub) &&
364 ShiftOpcode != Instruction::Shl)
365 return nullptr;
366
367 Type *Ty = I.getType();
368
369 // Find a matching shift by constant. The fold is not valid if the sum
370 // of the shift values equals or exceeds bitwidth.
371 Value *X, *Y;
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())) &&
379 };
380
381 // Logic ops and Add are commutative, so check each operand for a match. Sub
382 // is not so we cannot reoder if we match operand(1) and need to keep the
383 // operands in their original positions.
384 bool FirstShiftIsOp1 = false;
385 if (matchFirstShift(BinInst->getOperand(0), BinInst->getOperand(1)))
386 Y = BinInst->getOperand(1);
387 else if (matchFirstShift(BinInst->getOperand(1), BinInst->getOperand(0))) {
388 Y = BinInst->getOperand(0);
389 FirstShiftIsOp1 = BinInst->getOpcode() == Instruction::Sub;
390 } else
391 return nullptr;
392
393 // shift (binop (shift X, C0), Y), C1 -> binop (shift X, C0+C1), (shift Y, C1)
394 Constant *ShiftSumC = ConstantExpr::getAdd(C0, C1);
395 Value *NewShift1 = Builder.CreateBinOp(ShiftOpcode, X, ShiftSumC);
396 Value *NewShift2 = Builder.CreateBinOp(ShiftOpcode, Y, C1);
397 Value *Op1 = FirstShiftIsOp1 ? NewShift2 : NewShift1;
398 Value *Op2 = FirstShiftIsOp1 ? NewShift1 : NewShift2;
399 return BinaryOperator::Create(BinInst->getOpcode(), Op1, Op2);
400}
401
404 return Phi;
405
406 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
407 assert(Op0->getType() == Op1->getType());
408 Type *Ty = I.getType();
409
410 // If the shift amount is a one-use `sext`, we can demote it to `zext`.
411 Value *Y;
412 if (match(Op1, m_OneUse(m_SExt(m_Value(Y))))) {
413 Value *NewExt = Builder.CreateZExt(Y, Ty, Op1->getName());
414 return BinaryOperator::Create(I.getOpcode(), Op0, NewExt);
415 }
416
417 // See if we can fold away this shift.
419 return &I;
420
421 // Try to fold constant and into select arguments.
422 if (isa<Constant>(Op0))
423 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
424 if (Instruction *R = FoldOpIntoSelect(I, SI))
425 return R;
426
427 if (Constant *CUI = dyn_cast<Constant>(Op1))
428 if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
429 return Res;
430
431 if (auto *NewShift = cast_or_null<Instruction>(
433 return NewShift;
434
435 // Pre-shift a constant shifted by a variable amount with constant offset:
436 // C shift (A add nuw C1) --> (C shift C1) shift A
437 Value *A;
438 Constant *C, *C1;
439 if (match(Op0, m_Constant(C)) &&
440 match(Op1, m_NUWAddLike(m_Value(A), m_Constant(C1)))) {
441 Value *NewC = Builder.CreateBinOp(I.getOpcode(), C, C1);
442 BinaryOperator *NewShiftOp = BinaryOperator::Create(I.getOpcode(), NewC, A);
443 if (I.getOpcode() == Instruction::Shl) {
444 NewShiftOp->setHasNoSignedWrap(I.hasNoSignedWrap());
445 NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
446 } else {
447 NewShiftOp->setIsExact(I.isExact());
448 }
449 return NewShiftOp;
450 }
451
452 unsigned BitWidth = Ty->getScalarSizeInBits();
453
454 const APInt *AC, *AddC;
455 // Try to pre-shift a constant shifted by a variable amount added with a
456 // negative number:
457 // C << (X - AddC) --> (C >> AddC) << X
458 // and
459 // C >> (X - AddC) --> (C << AddC) >> X
460 if (match(Op0, m_APInt(AC)) && match(Op1, m_Add(m_Value(A), m_APInt(AddC))) &&
461 AddC->isNegative() && (-*AddC).ult(BitWidth)) {
462 assert(!AC->isZero() && "Expected simplify of shifted zero");
463 unsigned PosOffset = (-*AddC).getZExtValue();
464
465 auto isSuitableForPreShift = [PosOffset, &I, AC]() {
466 switch (I.getOpcode()) {
467 default:
468 return false;
469 case Instruction::Shl:
470 return (I.hasNoSignedWrap() || I.hasNoUnsignedWrap()) &&
471 AC->eq(AC->lshr(PosOffset).shl(PosOffset));
472 case Instruction::LShr:
473 return I.isExact() && AC->eq(AC->shl(PosOffset).lshr(PosOffset));
474 case Instruction::AShr:
475 return I.isExact() && AC->eq(AC->shl(PosOffset).ashr(PosOffset));
476 }
477 };
478 if (isSuitableForPreShift()) {
479 Constant *NewC = ConstantInt::get(Ty, I.getOpcode() == Instruction::Shl
480 ? AC->lshr(PosOffset)
481 : AC->shl(PosOffset));
482 BinaryOperator *NewShiftOp =
483 BinaryOperator::Create(I.getOpcode(), NewC, A);
484 if (I.getOpcode() == Instruction::Shl) {
485 NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
486 } else {
487 NewShiftOp->setIsExact();
488 }
489 return NewShiftOp;
490 }
491 }
492
493 // X shift (A srem C) -> X shift (A and (C - 1)) iff C is a power of 2.
494 // Because shifts by negative values (which could occur if A were negative)
495 // are undefined.
496 if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Constant(C))) &&
497 match(C, m_Power2())) {
498 // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
499 // demand the sign bit (and many others) here??
500 Constant *Mask = ConstantExpr::getSub(C, ConstantInt::get(Ty, 1));
501 Value *Rem = Builder.CreateAnd(A, Mask, Op1->getName());
502 return replaceOperand(I, 1, Rem);
503 }
504
506 return Logic;
507
508 if (match(Op1, m_Or(m_Value(), m_SpecificInt(BitWidth - 1))))
509 return replaceOperand(I, 1, ConstantInt::get(Ty, BitWidth - 1));
510
511 return nullptr;
512}
513
514/// Return true if we can simplify two logical (either left or right) shifts
515/// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
516static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
517 Instruction *InnerShift,
518 InstCombinerImpl &IC, Instruction *CxtI) {
519 assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
520
521 // We need constant scalar or constant splat shifts.
522 const APInt *InnerShiftConst;
523 if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
524 return false;
525
526 // Two logical shifts in the same direction:
527 // shl (shl X, C1), C2 --> shl X, C1 + C2
528 // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
529 bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
530 if (IsInnerShl == IsOuterShl)
531 return true;
532
533 // Equal shift amounts in opposite directions become bitwise 'and':
534 // lshr (shl X, C), C --> and X, C'
535 // shl (lshr X, C), C --> and X, C'
536 if (*InnerShiftConst == OuterShAmt)
537 return true;
538
539 // If the 2nd shift is bigger than the 1st, we can fold:
540 // lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3
541 // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3
542 // but it isn't profitable unless we know the and'd out bits are already zero.
543 // Also, check that the inner shift is valid (less than the type width) or
544 // we'll crash trying to produce the bit mask for the 'and'.
545 unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits();
546 if (InnerShiftConst->ugt(OuterShAmt) && InnerShiftConst->ult(TypeWidth)) {
547 unsigned InnerShAmt = InnerShiftConst->getZExtValue();
548 unsigned MaskShift =
549 IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
550 APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
551 if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
552 return true;
553 }
554
555 return false;
556}
557
558/// See if we can compute the specified value, but shifted logically to the left
559/// or right by some number of bits. This should return true if the expression
560/// can be computed for the same cost as the current expression tree. This is
561/// used to eliminate extraneous shifting from things like:
562/// %C = shl i128 %A, 64
563/// %D = shl i128 %B, 96
564/// %E = or i128 %C, %D
565/// %F = lshr i128 %E, 64
566/// where the client will ask if E can be computed shifted right by 64-bits. If
567/// this succeeds, getShiftedValue() will be called to produce the value.
568static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
569 InstCombinerImpl &IC, Instruction *CxtI) {
570 // We can always evaluate immediate constants.
571 if (match(V, m_ImmConstant()))
572 return true;
573
574 Instruction *I = dyn_cast<Instruction>(V);
575 if (!I) return false;
576
577 // We can't mutate something that has multiple uses: doing so would
578 // require duplicating the instruction in general, which isn't profitable.
579 if (!I->hasOneUse()) return false;
580
581 switch (I->getOpcode()) {
582 default: return false;
583 case Instruction::And:
584 case Instruction::Or:
585 case Instruction::Xor:
586 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
587 return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
588 canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
589
590 case Instruction::Shl:
591 case Instruction::LShr:
592 return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
593
594 case Instruction::Select: {
595 SelectInst *SI = cast<SelectInst>(I);
596 Value *TrueVal = SI->getTrueValue();
597 Value *FalseVal = SI->getFalseValue();
598 return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
599 canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
600 }
601 case Instruction::PHI: {
602 // We can change a phi if we can change all operands. Note that we never
603 // get into trouble with cyclic PHIs here because we only consider
604 // instructions with a single use.
605 PHINode *PN = cast<PHINode>(I);
606 for (Value *IncValue : PN->incoming_values())
607 if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
608 return false;
609 return true;
610 }
611 case Instruction::Mul: {
612 const APInt *MulConst;
613 // We can fold (shr (mul X, -(1 << C)), C) -> (and (neg X), C`)
614 return !IsLeftShift && match(I->getOperand(1), m_APInt(MulConst)) &&
615 MulConst->isNegatedPowerOf2() && MulConst->countr_zero() == NumBits;
616 }
617 }
618}
619
620/// Fold OuterShift (InnerShift X, C1), C2.
621/// See canEvaluateShiftedShift() for the constraints on these instructions.
622static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
623 bool IsOuterShl,
624 InstCombiner::BuilderTy &Builder) {
625 bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
626 Type *ShType = InnerShift->getType();
627 unsigned TypeWidth = ShType->getScalarSizeInBits();
628
629 // We only accept shifts-by-a-constant in canEvaluateShifted().
630 const APInt *C1;
631 match(InnerShift->getOperand(1), m_APInt(C1));
632 unsigned InnerShAmt = C1->getZExtValue();
633
634 // Change the shift amount and clear the appropriate IR flags.
635 auto NewInnerShift = [&](unsigned ShAmt) {
636 InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt));
637 if (IsInnerShl) {
638 InnerShift->setHasNoUnsignedWrap(false);
639 InnerShift->setHasNoSignedWrap(false);
640 } else {
641 InnerShift->setIsExact(false);
642 }
643 return InnerShift;
644 };
645
646 // Two logical shifts in the same direction:
647 // shl (shl X, C1), C2 --> shl X, C1 + C2
648 // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
649 if (IsInnerShl == IsOuterShl) {
650 // If this is an oversized composite shift, then unsigned shifts get 0.
651 if (InnerShAmt + OuterShAmt >= TypeWidth)
652 return Constant::getNullValue(ShType);
653
654 return NewInnerShift(InnerShAmt + OuterShAmt);
655 }
656
657 // Equal shift amounts in opposite directions become bitwise 'and':
658 // lshr (shl X, C), C --> and X, C'
659 // shl (lshr X, C), C --> and X, C'
660 if (InnerShAmt == OuterShAmt) {
661 APInt Mask = IsInnerShl
662 ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
663 : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
664 Value *And = Builder.CreateAnd(InnerShift->getOperand(0),
665 ConstantInt::get(ShType, Mask));
666 if (auto *AndI = dyn_cast<Instruction>(And)) {
667 AndI->moveBefore(InnerShift);
668 AndI->takeName(InnerShift);
669 }
670 return And;
671 }
672
673 assert(InnerShAmt > OuterShAmt &&
674 "Unexpected opposite direction logical shift pair");
675
676 // In general, we would need an 'and' for this transform, but
677 // canEvaluateShiftedShift() guarantees that the masked-off bits are not used.
678 // lshr (shl X, C1), C2 --> shl X, C1 - C2
679 // shl (lshr X, C1), C2 --> lshr X, C1 - C2
680 return NewInnerShift(InnerShAmt - OuterShAmt);
681}
682
683/// When canEvaluateShifted() returns true for an expression, this function
684/// inserts the new computation that produces the shifted value.
685static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
686 InstCombinerImpl &IC, const DataLayout &DL) {
687 // We can always evaluate constants shifted.
688 if (Constant *C = dyn_cast<Constant>(V)) {
689 if (isLeftShift)
690 return IC.Builder.CreateShl(C, NumBits);
691 else
692 return IC.Builder.CreateLShr(C, NumBits);
693 }
694
695 Instruction *I = cast<Instruction>(V);
696 IC.addToWorklist(I);
697
698 switch (I->getOpcode()) {
699 default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
700 case Instruction::And:
701 case Instruction::Or:
702 case Instruction::Xor:
703 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
704 I->setOperand(
705 0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
706 I->setOperand(
707 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
708 return I;
709
710 case Instruction::Shl:
711 case Instruction::LShr:
712 return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
713 IC.Builder);
714
715 case Instruction::Select:
716 I->setOperand(
717 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
718 I->setOperand(
719 2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
720 return I;
721 case Instruction::PHI: {
722 // We can change a phi if we can change all operands. Note that we never
723 // get into trouble with cyclic PHIs here because we only consider
724 // instructions with a single use.
725 PHINode *PN = cast<PHINode>(I);
726 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
728 isLeftShift, IC, DL));
729 return PN;
730 }
731 case Instruction::Mul: {
732 assert(!isLeftShift && "Unexpected shift direction!");
733 auto *Neg = BinaryOperator::CreateNeg(I->getOperand(0));
734 IC.InsertNewInstWith(Neg, I->getIterator());
735 unsigned TypeWidth = I->getType()->getScalarSizeInBits();
736 APInt Mask = APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits);
737 auto *And = BinaryOperator::CreateAnd(Neg,
738 ConstantInt::get(I->getType(), Mask));
739 And->takeName(I);
740 return IC.InsertNewInstWith(And, I->getIterator());
741 }
742 }
743}
744
745// If this is a bitwise operator or add with a constant RHS we might be able
746// to pull it through a shift.
748 BinaryOperator *BO) {
749 switch (BO->getOpcode()) {
750 default:
751 return false; // Do not perform transform!
752 case Instruction::Add:
753 return Shift.getOpcode() == Instruction::Shl;
754 case Instruction::Or:
755 case Instruction::And:
756 return true;
757 case Instruction::Xor:
758 // Do not change a 'not' of logical shift because that would create a normal
759 // 'xor'. The 'not' is likely better for analysis, SCEV, and codegen.
760 return !(Shift.isLogicalShift() && match(BO, m_Not(m_Value())));
761 }
762}
763
765 BinaryOperator &I) {
766 // (C2 << X) << C1 --> (C2 << C1) << X
767 // (C2 >> X) >> C1 --> (C2 >> C1) >> X
768 Constant *C2;
769 Value *X;
770 if (match(Op0, m_BinOp(I.getOpcode(), m_ImmConstant(C2), m_Value(X))))
772 I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), C2, C1), X);
773
774 bool IsLeftShift = I.getOpcode() == Instruction::Shl;
775 Type *Ty = I.getType();
776 unsigned TypeBits = Ty->getScalarSizeInBits();
777
778 // (X / +DivC) >> (Width - 1) --> ext (X <= -DivC)
779 // (X / -DivC) >> (Width - 1) --> ext (X >= +DivC)
780 const APInt *DivC;
781 if (!IsLeftShift && match(C1, m_SpecificIntAllowPoison(TypeBits - 1)) &&
782 match(Op0, m_SDiv(m_Value(X), m_APInt(DivC))) && !DivC->isZero() &&
783 !DivC->isMinSignedValue()) {
784 Constant *NegDivC = ConstantInt::get(Ty, -(*DivC));
787 Value *Cmp = Builder.CreateICmp(Pred, X, NegDivC);
788 auto ExtOpcode = (I.getOpcode() == Instruction::AShr) ? Instruction::SExt
789 : Instruction::ZExt;
790 return CastInst::Create(ExtOpcode, Cmp, Ty);
791 }
792
793 const APInt *Op1C;
794 if (!match(C1, m_APInt(Op1C)))
795 return nullptr;
796
797 assert(!Op1C->uge(TypeBits) &&
798 "Shift over the type width should have been removed already");
799
800 // See if we can propagate this shift into the input, this covers the trivial
801 // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
802 if (I.getOpcode() != Instruction::AShr &&
803 canEvaluateShifted(Op0, Op1C->getZExtValue(), IsLeftShift, *this, &I)) {
805 dbgs() << "ICE: GetShiftedValue propagating shift through expression"
806 " to eliminate shift:\n IN: "
807 << *Op0 << "\n SH: " << I << "\n");
808
809 return replaceInstUsesWith(
810 I, getShiftedValue(Op0, Op1C->getZExtValue(), IsLeftShift, *this, DL));
811 }
812
813 if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
814 return FoldedShift;
815
816 if (!Op0->hasOneUse())
817 return nullptr;
818
819 if (auto *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
820 // If the operand is a bitwise operator with a constant RHS, and the
821 // shift is the only use, we can pull it out of the shift.
822 const APInt *Op0C;
823 if (match(Op0BO->getOperand(1), m_APInt(Op0C))) {
824 if (canShiftBinOpWithConstantRHS(I, Op0BO)) {
825 Value *NewRHS =
826 Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(1), C1);
827
828 Value *NewShift =
829 Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), C1);
830 NewShift->takeName(Op0BO);
831
832 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, NewRHS);
833 }
834 }
835 }
836
837 // If we have a select that conditionally executes some binary operator,
838 // see if we can pull it the select and operator through the shift.
839 //
840 // For example, turning:
841 // shl (select C, (add X, C1), X), C2
842 // Into:
843 // Y = shl X, C2
844 // select C, (add Y, C1 << C2), Y
845 Value *Cond;
846 BinaryOperator *TBO;
847 Value *FalseVal;
848 if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)),
849 m_Value(FalseVal)))) {
850 const APInt *C;
851 if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal &&
852 match(TBO->getOperand(1), m_APInt(C)) &&
854 Value *NewRHS =
855 Builder.CreateBinOp(I.getOpcode(), TBO->getOperand(1), C1);
856
857 Value *NewShift = Builder.CreateBinOp(I.getOpcode(), FalseVal, C1);
858 Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift, NewRHS);
859 return SelectInst::Create(Cond, NewOp, NewShift);
860 }
861 }
862
863 BinaryOperator *FBO;
864 Value *TrueVal;
865 if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal),
866 m_OneUse(m_BinOp(FBO))))) {
867 const APInt *C;
868 if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal &&
869 match(FBO->getOperand(1), m_APInt(C)) &&
871 Value *NewRHS =
872 Builder.CreateBinOp(I.getOpcode(), FBO->getOperand(1), C1);
873
874 Value *NewShift = Builder.CreateBinOp(I.getOpcode(), TrueVal, C1);
875 Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift, NewRHS);
876 return SelectInst::Create(Cond, NewShift, NewOp);
877 }
878 }
879
880 return nullptr;
881}
882
883// Tries to perform
884// (lshr (add (zext X), (zext Y)), K)
885// -> (icmp ult (add X, Y), X)
886// where
887// - The add's operands are zexts from a K-bits integer to a bigger type.
888// - The add is only used by the shr, or by iK (or narrower) truncates.
889// - The lshr type has more than 2 bits (other types are boolean math).
890// - K > 1
891// note that
892// - The resulting add cannot have nuw/nsw, else on overflow we get a
893// poison value and the transform isn't legal anymore.
894Instruction *InstCombinerImpl::foldLShrOverflowBit(BinaryOperator &I) {
895 assert(I.getOpcode() == Instruction::LShr);
896
897 Value *Add = I.getOperand(0);
898 Value *ShiftAmt = I.getOperand(1);
899 Type *Ty = I.getType();
900
901 if (Ty->getScalarSizeInBits() < 3)
902 return nullptr;
903
904 const APInt *ShAmtAPInt = nullptr;
905 Value *X = nullptr, *Y = nullptr;
906 if (!match(ShiftAmt, m_APInt(ShAmtAPInt)) ||
907 !match(Add,
909 return nullptr;
910
911 const unsigned ShAmt = ShAmtAPInt->getZExtValue();
912 if (ShAmt == 1)
913 return nullptr;
914
915 // X/Y are zexts from `ShAmt`-sized ints.
916 if (X->getType()->getScalarSizeInBits() != ShAmt ||
917 Y->getType()->getScalarSizeInBits() != ShAmt)
918 return nullptr;
919
920 // Make sure that `Add` is only used by `I` and `ShAmt`-truncates.
921 if (!Add->hasOneUse()) {
922 for (User *U : Add->users()) {
923 if (U == &I)
924 continue;
925
926 TruncInst *Trunc = dyn_cast<TruncInst>(U);
927 if (!Trunc || Trunc->getType()->getScalarSizeInBits() > ShAmt)
928 return nullptr;
929 }
930 }
931
932 // Insert at Add so that the newly created `NarrowAdd` will dominate it's
933 // users (i.e. `Add`'s users).
934 Instruction *AddInst = cast<Instruction>(Add);
935 Builder.SetInsertPoint(AddInst);
936
937 Value *NarrowAdd = Builder.CreateAdd(X, Y, "add.narrowed");
938 Value *Overflow =
939 Builder.CreateICmpULT(NarrowAdd, X, "add.narrowed.overflow");
940
941 // Replace the uses of the original add with a zext of the
942 // NarrowAdd's result. Note that all users at this stage are known to
943 // be ShAmt-sized truncs, or the lshr itself.
944 if (!Add->hasOneUse()) {
945 replaceInstUsesWith(*AddInst, Builder.CreateZExt(NarrowAdd, Ty));
946 eraseInstFromFunction(*AddInst);
947 }
948
949 // Replace the LShr with a zext of the overflow check.
950 return new ZExtInst(Overflow, Ty);
951}
952
953// Try to set nuw/nsw flags on shl or exact flag on lshr/ashr using knownbits.
955 assert(I.isShift() && "Expected a shift as input");
956 // We already have all the flags.
957 if (I.getOpcode() == Instruction::Shl) {
958 if (I.hasNoUnsignedWrap() && I.hasNoSignedWrap())
959 return false;
960 } else {
961 if (I.isExact())
962 return false;
963
964 // shr (shl X, Y), Y
965 if (match(I.getOperand(0), m_Shl(m_Value(), m_Specific(I.getOperand(1))))) {
966 I.setIsExact();
967 return true;
968 }
969 }
970
971 // Compute what we know about shift count.
972 KnownBits KnownCnt = computeKnownBits(I.getOperand(1), /* Depth */ 0, Q);
973 unsigned BitWidth = KnownCnt.getBitWidth();
974 // Since shift produces a poison value if RHS is equal to or larger than the
975 // bit width, we can safely assume that RHS is less than the bit width.
976 uint64_t MaxCnt = KnownCnt.getMaxValue().getLimitedValue(BitWidth - 1);
977
978 KnownBits KnownAmt = computeKnownBits(I.getOperand(0), /* Depth */ 0, Q);
979 bool Changed = false;
980
981 if (I.getOpcode() == Instruction::Shl) {
982 // If we have as many leading zeros than maximum shift cnt we have nuw.
983 if (!I.hasNoUnsignedWrap() && MaxCnt <= KnownAmt.countMinLeadingZeros()) {
984 I.setHasNoUnsignedWrap();
985 Changed = true;
986 }
987 // If we have more sign bits than maximum shift cnt we have nsw.
988 if (!I.hasNoSignedWrap()) {
989 if (MaxCnt < KnownAmt.countMinSignBits() ||
990 MaxCnt < ComputeNumSignBits(I.getOperand(0), Q.DL, /*Depth*/ 0, Q.AC,
991 Q.CxtI, Q.DT)) {
992 I.setHasNoSignedWrap();
993 Changed = true;
994 }
995 }
996 return Changed;
997 }
998
999 // If we have at least as many trailing zeros as maximum count then we have
1000 // exact.
1001 Changed = MaxCnt <= KnownAmt.countMinTrailingZeros();
1002 I.setIsExact(Changed);
1003
1004 return Changed;
1005}
1006
1009
1010 if (Value *V = simplifyShlInst(I.getOperand(0), I.getOperand(1),
1011 I.hasNoSignedWrap(), I.hasNoUnsignedWrap(), Q))
1012 return replaceInstUsesWith(I, V);
1013
1015 return X;
1016
1018 return V;
1019
1021 return V;
1022
1023 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1024 Type *Ty = I.getType();
1025 unsigned BitWidth = Ty->getScalarSizeInBits();
1026
1027 const APInt *C;
1028 if (match(Op1, m_APInt(C))) {
1029 unsigned ShAmtC = C->getZExtValue();
1030
1031 // shl (zext X), C --> zext (shl X, C)
1032 // This is only valid if X would have zeros shifted out.
1033 Value *X;
1034 if (match(Op0, m_OneUse(m_ZExt(m_Value(X))))) {
1035 unsigned SrcWidth = X->getType()->getScalarSizeInBits();
1036 if (ShAmtC < SrcWidth &&
1037 MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmtC), 0, &I))
1038 return new ZExtInst(Builder.CreateShl(X, ShAmtC), Ty);
1039 }
1040
1041 // (X >> C) << C --> X & (-1 << C)
1042 if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) {
1044 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
1045 }
1046
1047 const APInt *C1;
1048 if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(C1)))) &&
1049 C1->ult(BitWidth)) {
1050 unsigned ShrAmt = C1->getZExtValue();
1051 if (ShrAmt < ShAmtC) {
1052 // If C1 < C: (X >>?,exact C1) << C --> X << (C - C1)
1053 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt);
1054 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
1055 NewShl->setHasNoUnsignedWrap(
1056 I.hasNoUnsignedWrap() ||
1057 (ShrAmt &&
1058 cast<Instruction>(Op0)->getOpcode() == Instruction::LShr &&
1059 I.hasNoSignedWrap()));
1060 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
1061 return NewShl;
1062 }
1063 if (ShrAmt > ShAmtC) {
1064 // If C1 > C: (X >>?exact C1) << C --> X >>?exact (C1 - C)
1065 Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC);
1066 auto *NewShr = BinaryOperator::Create(
1067 cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
1068 NewShr->setIsExact(true);
1069 return NewShr;
1070 }
1071 }
1072
1073 if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_APInt(C1)))) &&
1074 C1->ult(BitWidth)) {
1075 unsigned ShrAmt = C1->getZExtValue();
1076 if (ShrAmt < ShAmtC) {
1077 // If C1 < C: (X >>? C1) << C --> (X << (C - C1)) & (-1 << C)
1078 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt);
1079 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
1080 NewShl->setHasNoUnsignedWrap(
1081 I.hasNoUnsignedWrap() ||
1082 (ShrAmt &&
1083 cast<Instruction>(Op0)->getOpcode() == Instruction::LShr &&
1084 I.hasNoSignedWrap()));
1085 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
1086 Builder.Insert(NewShl);
1088 return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
1089 }
1090 if (ShrAmt > ShAmtC) {
1091 // If C1 > C: (X >>? C1) << C --> (X >>? (C1 - C)) & (-1 << C)
1092 Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC);
1093 auto *OldShr = cast<BinaryOperator>(Op0);
1094 auto *NewShr =
1095 BinaryOperator::Create(OldShr->getOpcode(), X, ShiftDiff);
1096 NewShr->setIsExact(OldShr->isExact());
1097 Builder.Insert(NewShr);
1099 return BinaryOperator::CreateAnd(NewShr, ConstantInt::get(Ty, Mask));
1100 }
1101 }
1102
1103 // Similar to above, but look through an intermediate trunc instruction.
1104 BinaryOperator *Shr;
1105 if (match(Op0, m_OneUse(m_Trunc(m_OneUse(m_BinOp(Shr))))) &&
1106 match(Shr, m_Shr(m_Value(X), m_APInt(C1)))) {
1107 // The larger shift direction survives through the transform.
1108 unsigned ShrAmtC = C1->getZExtValue();
1109 unsigned ShDiff = ShrAmtC > ShAmtC ? ShrAmtC - ShAmtC : ShAmtC - ShrAmtC;
1110 Constant *ShiftDiffC = ConstantInt::get(X->getType(), ShDiff);
1111 auto ShiftOpc = ShrAmtC > ShAmtC ? Shr->getOpcode() : Instruction::Shl;
1112
1113 // If C1 > C:
1114 // (trunc (X >> C1)) << C --> (trunc (X >> (C1 - C))) && (-1 << C)
1115 // If C > C1:
1116 // (trunc (X >> C1)) << C --> (trunc (X << (C - C1))) && (-1 << C)
1117 Value *NewShift = Builder.CreateBinOp(ShiftOpc, X, ShiftDiffC, "sh.diff");
1118 Value *Trunc = Builder.CreateTrunc(NewShift, Ty, "tr.sh.diff");
1120 return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, Mask));
1121 }
1122
1123 // If we have an opposite shift by the same amount, we may be able to
1124 // reorder binops and shifts to eliminate math/logic.
1125 auto isSuitableBinOpcode = [](Instruction::BinaryOps BinOpcode) {
1126 switch (BinOpcode) {
1127 default:
1128 return false;
1129 case Instruction::Add:
1130 case Instruction::And:
1131 case Instruction::Or:
1132 case Instruction::Xor:
1133 case Instruction::Sub:
1134 // NOTE: Sub is not commutable and the tranforms below may not be valid
1135 // when the shift-right is operand 1 (RHS) of the sub.
1136 return true;
1137 }
1138 };
1139 BinaryOperator *Op0BO;
1140 if (match(Op0, m_OneUse(m_BinOp(Op0BO))) &&
1141 isSuitableBinOpcode(Op0BO->getOpcode())) {
1142 // Commute so shift-right is on LHS of the binop.
1143 // (Y bop (X >> C)) << C -> ((X >> C) bop Y) << C
1144 // (Y bop ((X >> C) & CC)) << C -> (((X >> C) & CC) bop Y) << C
1145 Value *Shr = Op0BO->getOperand(0);
1146 Value *Y = Op0BO->getOperand(1);
1147 Value *X;
1148 const APInt *CC;
1149 if (Op0BO->isCommutative() && Y->hasOneUse() &&
1150 (match(Y, m_Shr(m_Value(), m_Specific(Op1))) ||
1152 m_APInt(CC)))))
1153 std::swap(Shr, Y);
1154
1155 // ((X >> C) bop Y) << C -> (X bop (Y << C)) & (~0 << C)
1156 if (match(Shr, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
1157 // Y << C
1158 Value *YS = Builder.CreateShl(Y, Op1, Op0BO->getName());
1159 // (X bop (Y << C))
1160 Value *B =
1161 Builder.CreateBinOp(Op0BO->getOpcode(), X, YS, Shr->getName());
1162 unsigned Op1Val = C->getLimitedValue(BitWidth);
1163 APInt Bits = APInt::getHighBitsSet(BitWidth, BitWidth - Op1Val);
1164 Constant *Mask = ConstantInt::get(Ty, Bits);
1165 return BinaryOperator::CreateAnd(B, Mask);
1166 }
1167
1168 // (((X >> C) & CC) bop Y) << C -> (X & (CC << C)) bop (Y << C)
1169 if (match(Shr,
1171 m_APInt(CC))))) {
1172 // Y << C
1173 Value *YS = Builder.CreateShl(Y, Op1, Op0BO->getName());
1174 // X & (CC << C)
1175 Value *M = Builder.CreateAnd(X, ConstantInt::get(Ty, CC->shl(*C)),
1176 X->getName() + ".mask");
1177 auto *NewOp = BinaryOperator::Create(Op0BO->getOpcode(), M, YS);
1178 if (auto *Disjoint = dyn_cast<PossiblyDisjointInst>(Op0BO);
1179 Disjoint && Disjoint->isDisjoint())
1180 cast<PossiblyDisjointInst>(NewOp)->setIsDisjoint(true);
1181 return NewOp;
1182 }
1183 }
1184
1185 // (C1 - X) << C --> (C1 << C) - (X << C)
1186 if (match(Op0, m_OneUse(m_Sub(m_APInt(C1), m_Value(X))))) {
1187 Constant *NewLHS = ConstantInt::get(Ty, C1->shl(*C));
1188 Value *NewShift = Builder.CreateShl(X, Op1);
1189 return BinaryOperator::CreateSub(NewLHS, NewShift);
1190 }
1191 }
1192
1193 if (setShiftFlags(I, Q))
1194 return &I;
1195
1196 // Transform (x >> y) << y to x & (-1 << y)
1197 // Valid for any type of right-shift.
1198 Value *X;
1199 if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
1201 Value *Mask = Builder.CreateShl(AllOnes, Op1);
1202 return BinaryOperator::CreateAnd(Mask, X);
1203 }
1204
1205 // Transform (-1 >> y) << y to -1 << y
1206 if (match(Op0, m_LShr(m_AllOnes(), m_Specific(Op1)))) {
1208 return BinaryOperator::CreateShl(AllOnes, Op1);
1209 }
1210
1211 Constant *C1;
1212 if (match(Op1, m_Constant(C1))) {
1213 Constant *C2;
1214 Value *X;
1215 // (X * C2) << C1 --> X * (C2 << C1)
1216 if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
1217 return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1));
1218
1219 // shl (zext i1 X), C1 --> select (X, 1 << C1, 0)
1220 if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
1221 auto *NewC = ConstantExpr::getShl(ConstantInt::get(Ty, 1), C1);
1223 }
1224 }
1225
1226 if (match(Op0, m_One())) {
1227 // (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1
1228 if (match(Op1, m_Sub(m_SpecificInt(BitWidth - 1), m_Value(X))))
1229 return BinaryOperator::CreateLShr(
1230 ConstantInt::get(Ty, APInt::getSignMask(BitWidth)), X);
1231
1232 // Canonicalize "extract lowest set bit" using cttz to and-with-negate:
1233 // 1 << (cttz X) --> -X & X
1234 if (match(Op1,
1235 m_OneUse(m_Intrinsic<Intrinsic::cttz>(m_Value(X), m_Value())))) {
1236 Value *NegX = Builder.CreateNeg(X, "neg");
1237 return BinaryOperator::CreateAnd(NegX, X);
1238 }
1239 }
1240
1241 return nullptr;
1242}
1243
1245 if (Value *V = simplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
1247 return replaceInstUsesWith(I, V);
1248
1250 return X;
1251
1253 return R;
1254
1255 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1256 Type *Ty = I.getType();
1257 Value *X;
1258 const APInt *C;
1259 unsigned BitWidth = Ty->getScalarSizeInBits();
1260
1261 // (iN (~X) u>> (N - 1)) --> zext (X > -1)
1262 if (match(Op0, m_OneUse(m_Not(m_Value(X)))) &&
1264 return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty);
1265
1266 // ((X << nuw Z) sub nuw Y) >>u exact Z --> X sub nuw (Y >>u exact Z)
1267 Value *Y;
1268 if (I.isExact() &&
1270 m_Value(Y))))) {
1271 Value *NewLshr = Builder.CreateLShr(Y, Op1, "", /*isExact=*/true);
1272 auto *NewSub = BinaryOperator::CreateNUWSub(X, NewLshr);
1273 NewSub->setHasNoSignedWrap(
1274 cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap());
1275 return NewSub;
1276 }
1277
1278 auto isSuitableBinOpcode = [](Instruction::BinaryOps BinOpcode) {
1279 switch (BinOpcode) {
1280 default:
1281 return false;
1282 case Instruction::Add:
1283 case Instruction::And:
1284 case Instruction::Or:
1285 case Instruction::Xor:
1286 // Sub is handled separately.
1287 return true;
1288 }
1289 };
1290
1291 // If both the binop and the shift are nuw, then:
1292 // ((X << nuw Z) binop nuw Y) >>u Z --> X binop nuw (Y >>u Z)
1294 m_Value(Y))))) {
1295 BinaryOperator *Op0OB = cast<BinaryOperator>(Op0);
1296 if (isSuitableBinOpcode(Op0OB->getOpcode())) {
1297 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op0);
1298 !OBO || OBO->hasNoUnsignedWrap()) {
1299 Value *NewLshr = Builder.CreateLShr(
1300 Y, Op1, "", I.isExact() && Op0OB->getOpcode() != Instruction::And);
1301 auto *NewBinOp = BinaryOperator::Create(Op0OB->getOpcode(), NewLshr, X);
1302 if (OBO) {
1303 NewBinOp->setHasNoUnsignedWrap(true);
1304 NewBinOp->setHasNoSignedWrap(OBO->hasNoSignedWrap());
1305 } else if (auto *Disjoint = dyn_cast<PossiblyDisjointInst>(Op0)) {
1306 cast<PossiblyDisjointInst>(NewBinOp)->setIsDisjoint(
1307 Disjoint->isDisjoint());
1308 }
1309 return NewBinOp;
1310 }
1311 }
1312 }
1313
1314 if (match(Op1, m_APInt(C))) {
1315 unsigned ShAmtC = C->getZExtValue();
1316 auto *II = dyn_cast<IntrinsicInst>(Op0);
1317 if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmtC &&
1318 (II->getIntrinsicID() == Intrinsic::ctlz ||
1319 II->getIntrinsicID() == Intrinsic::cttz ||
1320 II->getIntrinsicID() == Intrinsic::ctpop)) {
1321 // ctlz.i32(x)>>5 --> zext(x == 0)
1322 // cttz.i32(x)>>5 --> zext(x == 0)
1323 // ctpop.i32(x)>>5 --> zext(x == -1)
1324 bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
1325 Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
1326 Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
1327 return new ZExtInst(Cmp, Ty);
1328 }
1329
1330 const APInt *C1;
1331 if (match(Op0, m_Shl(m_Value(X), m_APInt(C1))) && C1->ult(BitWidth)) {
1332 if (C1->ult(ShAmtC)) {
1333 unsigned ShlAmtC = C1->getZExtValue();
1334 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShlAmtC);
1335 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
1336 // (X <<nuw C1) >>u C --> X >>u (C - C1)
1337 auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
1338 NewLShr->setIsExact(I.isExact());
1339 return NewLShr;
1340 }
1341 if (Op0->hasOneUse()) {
1342 // (X << C1) >>u C --> (X >>u (C - C1)) & (-1 >> C)
1343 Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact());
1345 return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
1346 }
1347 } else if (C1->ugt(ShAmtC)) {
1348 unsigned ShlAmtC = C1->getZExtValue();
1349 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmtC - ShAmtC);
1350 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
1351 // (X <<nuw C1) >>u C --> X <<nuw/nsw (C1 - C)
1352 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
1353 NewShl->setHasNoUnsignedWrap(true);
1354 NewShl->setHasNoSignedWrap(ShAmtC > 0);
1355 return NewShl;
1356 }
1357 if (Op0->hasOneUse()) {
1358 // (X << C1) >>u C --> X << (C1 - C) & (-1 >> C)
1359 Value *NewShl = Builder.CreateShl(X, ShiftDiff);
1361 return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
1362 }
1363 } else {
1364 assert(*C1 == ShAmtC);
1365 // (X << C) >>u C --> X & (-1 >>u C)
1367 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
1368 }
1369 }
1370
1371 // ((X << C) + Y) >>u C --> (X + (Y >>u C)) & (-1 >>u C)
1372 // TODO: Consolidate with the more general transform that starts from shl
1373 // (the shifts are in the opposite order).
1374 if (match(Op0,
1376 m_Value(Y))))) {
1377 Value *NewLshr = Builder.CreateLShr(Y, Op1);
1378 Value *NewAdd = Builder.CreateAdd(NewLshr, X);
1379 unsigned Op1Val = C->getLimitedValue(BitWidth);
1380 APInt Bits = APInt::getLowBitsSet(BitWidth, BitWidth - Op1Val);
1381 Constant *Mask = ConstantInt::get(Ty, Bits);
1382 return BinaryOperator::CreateAnd(NewAdd, Mask);
1383 }
1384
1385 if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) &&
1386 (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
1387 assert(ShAmtC < X->getType()->getScalarSizeInBits() &&
1388 "Big shift not simplified to zero?");
1389 // lshr (zext iM X to iN), C --> zext (lshr X, C) to iN
1390 Value *NewLShr = Builder.CreateLShr(X, ShAmtC);
1391 return new ZExtInst(NewLShr, Ty);
1392 }
1393
1394 if (match(Op0, m_SExt(m_Value(X)))) {
1395 unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits();
1396 // lshr (sext i1 X to iN), C --> select (X, -1 >> C, 0)
1397 if (SrcTyBitWidth == 1) {
1398 auto *NewC = ConstantInt::get(
1399 Ty, APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC));
1401 }
1402
1403 if ((!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType())) &&
1404 Op0->hasOneUse()) {
1405 // Are we moving the sign bit to the low bit and widening with high
1406 // zeros? lshr (sext iM X to iN), N-1 --> zext (lshr X, M-1) to iN
1407 if (ShAmtC == BitWidth - 1) {
1408 Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
1409 return new ZExtInst(NewLShr, Ty);
1410 }
1411
1412 // lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN
1413 if (ShAmtC == BitWidth - SrcTyBitWidth) {
1414 // The new shift amount can't be more than the narrow source type.
1415 unsigned NewShAmt = std::min(ShAmtC, SrcTyBitWidth - 1);
1416 Value *AShr = Builder.CreateAShr(X, NewShAmt);
1417 return new ZExtInst(AShr, Ty);
1418 }
1419 }
1420 }
1421
1422 if (ShAmtC == BitWidth - 1) {
1423 // lshr i32 or(X,-X), 31 --> zext (X != 0)
1424 if (match(Op0, m_OneUse(m_c_Or(m_Neg(m_Value(X)), m_Deferred(X)))))
1425 return new ZExtInst(Builder.CreateIsNotNull(X), Ty);
1426
1427 // lshr i32 (X -nsw Y), 31 --> zext (X < Y)
1428 if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
1429 return new ZExtInst(Builder.CreateICmpSLT(X, Y), Ty);
1430
1431 // Check if a number is negative and odd:
1432 // lshr i32 (srem X, 2), 31 --> and (X >> 31), X
1433 if (match(Op0, m_OneUse(m_SRem(m_Value(X), m_SpecificInt(2))))) {
1434 Value *Signbit = Builder.CreateLShr(X, ShAmtC);
1435 return BinaryOperator::CreateAnd(Signbit, X);
1436 }
1437 }
1438
1439 Instruction *TruncSrc;
1440 if (match(Op0, m_OneUse(m_Trunc(m_Instruction(TruncSrc)))) &&
1441 match(TruncSrc, m_LShr(m_Value(X), m_APInt(C1)))) {
1442 unsigned SrcWidth = X->getType()->getScalarSizeInBits();
1443 unsigned AmtSum = ShAmtC + C1->getZExtValue();
1444
1445 // If the combined shift fits in the source width:
1446 // (trunc (X >>u C1)) >>u C --> and (trunc (X >>u (C1 + C)), MaskC
1447 //
1448 // If the first shift covers the number of bits truncated, then the
1449 // mask instruction is eliminated (and so the use check is relaxed).
1450 if (AmtSum < SrcWidth &&
1451 (TruncSrc->hasOneUse() || C1->uge(SrcWidth - BitWidth))) {
1452 Value *SumShift = Builder.CreateLShr(X, AmtSum, "sum.shift");
1453 Value *Trunc = Builder.CreateTrunc(SumShift, Ty, I.getName());
1454
1455 // If the first shift does not cover the number of bits truncated, then
1456 // we require a mask to get rid of high bits in the result.
1457 APInt MaskC = APInt::getAllOnes(BitWidth).lshr(ShAmtC);
1458 return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, MaskC));
1459 }
1460 }
1461
1462 const APInt *MulC;
1463 if (match(Op0, m_NUWMul(m_Value(X), m_APInt(MulC)))) {
1464 // Look for a "splat" mul pattern - it replicates bits across each half of
1465 // a value, so a right shift is just a mask of the low bits:
1466 // lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1
1467 // TODO: Generalize to allow more than just half-width shifts?
1468 if (BitWidth > 2 && ShAmtC * 2 == BitWidth && (*MulC - 1).isPowerOf2() &&
1469 MulC->logBase2() == ShAmtC)
1470 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2));
1471
1472 // The one-use check is not strictly necessary, but codegen may not be
1473 // able to invert the transform and perf may suffer with an extra mul
1474 // instruction.
1475 if (Op0->hasOneUse()) {
1476 APInt NewMulC = MulC->lshr(ShAmtC);
1477 // if c is divisible by (1 << ShAmtC):
1478 // lshr (mul nuw x, MulC), ShAmtC -> mul nuw nsw x, (MulC >> ShAmtC)
1479 if (MulC->eq(NewMulC.shl(ShAmtC))) {
1480 auto *NewMul =
1481 BinaryOperator::CreateNUWMul(X, ConstantInt::get(Ty, NewMulC));
1482 assert(ShAmtC != 0 &&
1483 "lshr X, 0 should be handled by simplifyLShrInst.");
1484 NewMul->setHasNoSignedWrap(true);
1485 return NewMul;
1486 }
1487 }
1488 }
1489
1490 // Try to narrow bswap.
1491 // In the case where the shift amount equals the bitwidth difference, the
1492 // shift is eliminated.
1493 if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::bswap>(
1494 m_OneUse(m_ZExt(m_Value(X))))))) {
1495 unsigned SrcWidth = X->getType()->getScalarSizeInBits();
1496 unsigned WidthDiff = BitWidth - SrcWidth;
1497 if (SrcWidth % 16 == 0) {
1498 Value *NarrowSwap = Builder.CreateUnaryIntrinsic(Intrinsic::bswap, X);
1499 if (ShAmtC >= WidthDiff) {
1500 // (bswap (zext X)) >> C --> zext (bswap X >> C')
1501 Value *NewShift = Builder.CreateLShr(NarrowSwap, ShAmtC - WidthDiff);
1502 return new ZExtInst(NewShift, Ty);
1503 } else {
1504 // (bswap (zext X)) >> C --> (zext (bswap X)) << C'
1505 Value *NewZExt = Builder.CreateZExt(NarrowSwap, Ty);
1506 Constant *ShiftDiff = ConstantInt::get(Ty, WidthDiff - ShAmtC);
1507 return BinaryOperator::CreateShl(NewZExt, ShiftDiff);
1508 }
1509 }
1510 }
1511
1512 // Reduce add-carry of bools to logic:
1513 // ((zext BoolX) + (zext BoolY)) >> 1 --> zext (BoolX && BoolY)
1514 Value *BoolX, *BoolY;
1515 if (ShAmtC == 1 && match(Op0, m_Add(m_Value(X), m_Value(Y))) &&
1516 match(X, m_ZExt(m_Value(BoolX))) && match(Y, m_ZExt(m_Value(BoolY))) &&
1517 BoolX->getType()->isIntOrIntVectorTy(1) &&
1518 BoolY->getType()->isIntOrIntVectorTy(1) &&
1519 (X->hasOneUse() || Y->hasOneUse() || Op0->hasOneUse())) {
1520 Value *And = Builder.CreateAnd(BoolX, BoolY);
1521 return new ZExtInst(And, Ty);
1522 }
1523 }
1524
1526 if (setShiftFlags(I, Q))
1527 return &I;
1528
1529 // Transform (x << y) >> y to x & (-1 >> y)
1530 if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) {
1532 Value *Mask = Builder.CreateLShr(AllOnes, Op1);
1533 return BinaryOperator::CreateAnd(Mask, X);
1534 }
1535
1536 // Transform (-1 << y) >> y to -1 >> y
1537 if (match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1)))) {
1539 return BinaryOperator::CreateLShr(AllOnes, Op1);
1540 }
1541
1542 if (Instruction *Overflow = foldLShrOverflowBit(I))
1543 return Overflow;
1544
1545 return nullptr;
1546}
1547
1550 BinaryOperator &OldAShr) {
1551 assert(OldAShr.getOpcode() == Instruction::AShr &&
1552 "Must be called with arithmetic right-shift instruction only.");
1553
1554 // Check that constant C is a splat of the element-wise bitwidth of V.
1555 auto BitWidthSplat = [](Constant *C, Value *V) {
1556 return match(
1558 APInt(C->getType()->getScalarSizeInBits(),
1559 V->getType()->getScalarSizeInBits())));
1560 };
1561
1562 // It should look like variable-length sign-extension on the outside:
1563 // (Val << (bitwidth(Val)-Nbits)) a>> (bitwidth(Val)-Nbits)
1564 Value *NBits;
1565 Instruction *MaybeTrunc;
1566 Constant *C1, *C2;
1567 if (!match(&OldAShr,
1568 m_AShr(m_Shl(m_Instruction(MaybeTrunc),
1570 m_ZExtOrSelf(m_Value(NBits))))),
1572 m_ZExtOrSelf(m_Deferred(NBits)))))) ||
1573 !BitWidthSplat(C1, &OldAShr) || !BitWidthSplat(C2, &OldAShr))
1574 return nullptr;
1575
1576 // There may or may not be a truncation after outer two shifts.
1577 Instruction *HighBitExtract;
1578 match(MaybeTrunc, m_TruncOrSelf(m_Instruction(HighBitExtract)));
1579 bool HadTrunc = MaybeTrunc != HighBitExtract;
1580
1581 // And finally, the innermost part of the pattern must be a right-shift.
1582 Value *X, *NumLowBitsToSkip;
1583 if (!match(HighBitExtract, m_Shr(m_Value(X), m_Value(NumLowBitsToSkip))))
1584 return nullptr;
1585
1586 // Said right-shift must extract high NBits bits - C0 must be it's bitwidth.
1587 Constant *C0;
1588 if (!match(NumLowBitsToSkip,
1590 m_Sub(m_Constant(C0), m_ZExtOrSelf(m_Specific(NBits))))) ||
1591 !BitWidthSplat(C0, HighBitExtract))
1592 return nullptr;
1593
1594 // Since the NBits is identical for all shifts, if the outermost and
1595 // innermost shifts are identical, then outermost shifts are redundant.
1596 // If we had truncation, do keep it though.
1597 if (HighBitExtract->getOpcode() == OldAShr.getOpcode())
1598 return replaceInstUsesWith(OldAShr, MaybeTrunc);
1599
1600 // Else, if there was a truncation, then we need to ensure that one
1601 // instruction will go away.
1602 if (HadTrunc && !match(&OldAShr, m_c_BinOp(m_OneUse(m_Value()), m_Value())))
1603 return nullptr;
1604
1605 // Finally, bypass two innermost shifts, and perform the outermost shift on
1606 // the operands of the innermost shift.
1607 Instruction *NewAShr =
1608 BinaryOperator::Create(OldAShr.getOpcode(), X, NumLowBitsToSkip);
1609 NewAShr->copyIRFlags(HighBitExtract); // We can preserve 'exact'-ness.
1610 if (!HadTrunc)
1611 return NewAShr;
1612
1613 Builder.Insert(NewAShr);
1614 return TruncInst::CreateTruncOrBitCast(NewAShr, OldAShr.getType());
1615}
1616
1618 if (Value *V = simplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
1620 return replaceInstUsesWith(I, V);
1621
1623 return X;
1624
1626 return R;
1627
1628 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1629 Type *Ty = I.getType();
1630 unsigned BitWidth = Ty->getScalarSizeInBits();
1631 const APInt *ShAmtAPInt;
1632 if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) {
1633 unsigned ShAmt = ShAmtAPInt->getZExtValue();
1634
1635 // If the shift amount equals the difference in width of the destination
1636 // and source scalar types:
1637 // ashr (shl (zext X), C), C --> sext X
1638 Value *X;
1639 if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
1640 ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
1641 return new SExtInst(X, Ty);
1642
1643 // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
1644 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
1645 const APInt *ShOp1;
1646 if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) &&
1647 ShOp1->ult(BitWidth)) {
1648 unsigned ShlAmt = ShOp1->getZExtValue();
1649 if (ShlAmt < ShAmt) {
1650 // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
1651 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
1652 auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
1653 NewAShr->setIsExact(I.isExact());
1654 return NewAShr;
1655 }
1656 if (ShlAmt > ShAmt) {
1657 // (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
1658 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
1659 auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
1660 NewShl->setHasNoSignedWrap(true);
1661 return NewShl;
1662 }
1663 }
1664
1665 if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) &&
1666 ShOp1->ult(BitWidth)) {
1667 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
1668 // Oversized arithmetic shifts replicate the sign bit.
1669 AmtSum = std::min(AmtSum, BitWidth - 1);
1670 // (X >>s C1) >>s C2 --> X >>s (C1 + C2)
1671 return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
1672 }
1673
1674 if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) &&
1675 (Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) {
1676 // ashr (sext X), C --> sext (ashr X, C')
1677 Type *SrcTy = X->getType();
1678 ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1);
1679 Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt));
1680 return new SExtInst(NewSh, Ty);
1681 }
1682
1683 if (ShAmt == BitWidth - 1) {
1684 // ashr i32 or(X,-X), 31 --> sext (X != 0)
1685 if (match(Op0, m_OneUse(m_c_Or(m_Neg(m_Value(X)), m_Deferred(X)))))
1686 return new SExtInst(Builder.CreateIsNotNull(X), Ty);
1687
1688 // ashr i32 (X -nsw Y), 31 --> sext (X < Y)
1689 Value *Y;
1690 if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
1691 return new SExtInst(Builder.CreateICmpSLT(X, Y), Ty);
1692 }
1693 }
1694
1696 if (setShiftFlags(I, Q))
1697 return &I;
1698
1699 // Prefer `-(x & 1)` over `(x << (bitwidth(x)-1)) a>> (bitwidth(x)-1)`
1700 // as the pattern to splat the lowest bit.
1701 // FIXME: iff X is already masked, we don't need the one-use check.
1702 Value *X;
1703 if (match(Op1, m_SpecificIntAllowPoison(BitWidth - 1)) &&
1706 Constant *Mask = ConstantInt::get(Ty, 1);
1707 // Retain the knowledge about the ignored lanes.
1709 Constant::mergeUndefsWith(Mask, cast<Constant>(Op1)),
1710 cast<Constant>(cast<Instruction>(Op0)->getOperand(1)));
1711 X = Builder.CreateAnd(X, Mask);
1713 }
1714
1716 return R;
1717
1718 // See if we can turn a signed shr into an unsigned shr.
1720 Instruction *Lshr = BinaryOperator::CreateLShr(Op0, Op1);
1721 Lshr->setIsExact(I.isExact());
1722 return Lshr;
1723 }
1724
1725 // ashr (xor %x, -1), %y --> xor (ashr %x, %y), -1
1726 if (match(Op0, m_OneUse(m_Not(m_Value(X))))) {
1727 // Note that we must drop 'exact'-ness of the shift!
1728 // Note that we can't keep undef's in -1 vector constant!
1729 auto *NewAShr = Builder.CreateAShr(X, Op1, Op0->getName() + ".not");
1730 return BinaryOperator::CreateNot(NewAShr);
1731 }
1732
1733 return nullptr;
1734}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DEBUG(X)
Definition: Debug.h:101
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file provides internal interfaces used to implement the InstCombine.
static Value * foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt, bool IsOuterShl, InstCombiner::BuilderTy &Builder)
Fold OuterShift (InnerShift X, C1), C2.
static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q)
static Instruction * dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift, const SimplifyQuery &Q, InstCombiner::BuilderTy &Builder)
static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift, InstCombinerImpl &IC, Instruction *CxtI)
See if we can compute the specified value, but shifted logically to the left or right by some number ...
bool canTryToConstantAddTwoShiftAmounts(Value *Sh0, Value *ShAmt0, Value *Sh1, Value *ShAmt1)
static Instruction * foldShiftOfShiftedBinOp(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
If we have a shift-by-constant of a bin op (bitwise logic op or add/sub w/ shl) that itself has a shi...
static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl, Instruction *InnerShift, InstCombinerImpl &IC, Instruction *CxtI)
Return true if we can simplify two logical (either left or right) shifts that have constant shift amo...
static Value * getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, InstCombinerImpl &IC, const DataLayout &DL)
When canEvaluateShifted() returns true for an expression, this function inserts the new computation t...
static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift, BinaryOperator *BO)
This file provides the interface for the instcombine pass implementation.
static bool hasNoSignedWrap(BinaryOperator &I)
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define I(x, y, z)
Definition: MD5.cpp:58
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const MCExpr * MaskShift(const MCExpr *Val, uint32_t Mask, uint32_t Shift, MCContext &Ctx)
static unsigned getScalarSizeInBits(Type *Ty)
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
Value * RHS
Class for arbitrary precision integers.
Definition: APInt.h:76
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:212
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition: APInt.h:427
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:207
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:401
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1498
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1089
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
bool eq(const APInt &RHS) const
Equality comparison.
Definition: APInt.h:1057
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1596
unsigned logBase2() const
Definition: APInt.h:1710
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition: APInt.h:453
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:274
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1199
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore)
Construct a binary instruction, given the opcode and the two operands.
BinaryOps getOpcode() const
Definition: InstrTypes.h:513
static BinaryOperator * CreateNeg(Value *Op, const Twine &Name, BasicBlock::iterator InsertBefore)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
static BinaryOperator * CreateNot(Value *Op, const Twine &Name, BasicBlock::iterator InsertBefore)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name, BasicBlock::iterator InsertBefore)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name, BasicBlock::iterator InsertBefore)
Create a Trunc or BitCast cast instruction.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ ICMP_EQ
equal
Definition: InstrTypes.h:1014
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2542
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2529
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2560
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2535
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2098
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.h:124
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:767
static Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
Definition: Constants.cpp:791
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:417
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition: IRBuilder.cpp:913
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2258
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1437
Value * CreateIsNotNeg(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg > -1.
Definition: IRBuilder.h:2560
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1721
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2242
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
Definition: IRBuilder.h:145
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1416
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2022
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1475
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1327
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition: IRBuilder.h:2550
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2008
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1666
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2274
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1456
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2352
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * visitLShr(BinaryOperator &I)
Instruction * foldBinOpIntoSelectOrPhi(BinaryOperator &I)
This is a convenience wrapper function for the above two functions.
Value * reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator *Sh0, const SimplifyQuery &SQ, bool AnalyzeForSignBitExtraction=false)
Instruction * visitAShr(BinaryOperator &I)
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * visitShl(BinaryOperator &I)
Instruction * foldBinopWithPhiOperands(BinaryOperator &BO)
For a binary operator with 2 phi operands, try to hoist the binary operation before the phi.
Instruction * foldVariableSignZeroExtensionOfVariableHighBitExtract(BinaryOperator &OldAShr)
Instruction * commonShiftTransforms(BinaryOperator &I)
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Tries to simplify operands to an integer instruction based on its demanded bits.
Instruction * foldVectorBinop(BinaryOperator &Inst)
Canonicalize the position of binops relative to shufflevector.
Instruction * FoldShiftByConstant(Value *Op0, Constant *Op1, BinaryOperator &I)
SimplifyQuery SQ
Definition: InstCombiner.h:76
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:386
Instruction * InsertNewInstWith(Instruction *New, BasicBlock::iterator Old)
Same as InsertNewInstBefore, but also sets the debug loc.
Definition: InstCombiner.h:375
const DataLayout & DL
Definition: InstCombiner.h:75
AssumptionCache & AC
Definition: InstCombiner.h:72
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:336
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:410
BuilderTy & Builder
Definition: InstCombiner.h:60
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:447
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
bool isLogicalShift() const
Return true if this is a logical shift left or a logical shift right.
Definition: Instruction.h:291
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:252
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
op_range incoming_values()
void setIncomingValue(unsigned i, Value *V)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr, BasicBlock::iterator InsertBefore, Instruction *MDFrom=nullptr)
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:234
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Type * getExtendedType() const
Given scalar/vector integer type, returns a type with elements twice as wide as in the original type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
This class represents zero extension of integer types.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:619
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:972
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:816
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:875
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
Definition: PatternMatch.h:980
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
CastOperator_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:893
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:854
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< CastOperator_match< OpTy, Instruction::Trunc >, OpTy > m_TruncOrSelf(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Exact_match< T > m_Exact(const T &SubPattern)
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
Definition: PatternMatch.h:698
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Value * simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
Value * simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Sub, fold the result or return null.
Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:324
Value * simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:275
Value * simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
@ And
Bitwise or logical AND of integers.
@ Add
Sum of integers.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:251
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:238
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:244
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:141
const DataLayout & DL
Definition: SimplifyQuery.h:61
const Instruction * CxtI
Definition: SimplifyQuery.h:65
const DominatorTree * DT
Definition: SimplifyQuery.h:63
SimplifyQuery getWithInstruction(const Instruction *I) const
Definition: SimplifyQuery.h:96
AssumptionCache * AC
Definition: SimplifyQuery.h:64