LLVM  9.0.0svn
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"
16 #include "llvm/IR/IntrinsicInst.h"
17 #include "llvm/IR/PatternMatch.h"
18 using namespace llvm;
19 using namespace PatternMatch;
20 
21 #define DEBUG_TYPE "instcombine"
22 
24  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
25  assert(Op0->getType() == Op1->getType());
26 
27  // See if we can fold away this shift.
28  if (SimplifyDemandedInstructionBits(I))
29  return &I;
30 
31  // Try to fold constant and into select arguments.
32  if (isa<Constant>(Op0))
33  if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
34  if (Instruction *R = FoldOpIntoSelect(I, SI))
35  return R;
36 
37  if (Constant *CUI = dyn_cast<Constant>(Op1))
38  if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
39  return Res;
40 
41  // (C1 shift (A add C2)) -> (C1 shift C2) shift A)
42  // iff A and C2 are both positive.
43  Value *A;
44  Constant *C;
45  if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C))))
46  if (isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
47  isKnownNonNegative(C, DL, 0, &AC, &I, &DT))
49  I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), Op0, C), A);
50 
51  // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
52  // Because shifts by negative values (which could occur if A were negative)
53  // are undefined.
54  const APInt *B;
55  if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) {
56  // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
57  // demand the sign bit (and many others) here??
58  Value *Rem = Builder.CreateAnd(A, ConstantInt::get(I.getType(), *B - 1),
59  Op1->getName());
60  I.setOperand(1, Rem);
61  return &I;
62  }
63 
64  return nullptr;
65 }
66 
67 /// Return true if we can simplify two logical (either left or right) shifts
68 /// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
69 static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
70  Instruction *InnerShift, InstCombiner &IC,
71  Instruction *CxtI) {
72  assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
73 
74  // We need constant scalar or constant splat shifts.
75  const APInt *InnerShiftConst;
76  if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
77  return false;
78 
79  // Two logical shifts in the same direction:
80  // shl (shl X, C1), C2 --> shl X, C1 + C2
81  // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
82  bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
83  if (IsInnerShl == IsOuterShl)
84  return true;
85 
86  // Equal shift amounts in opposite directions become bitwise 'and':
87  // lshr (shl X, C), C --> and X, C'
88  // shl (lshr X, C), C --> and X, C'
89  if (*InnerShiftConst == OuterShAmt)
90  return true;
91 
92  // If the 2nd shift is bigger than the 1st, we can fold:
93  // lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3
94  // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3
95  // but it isn't profitable unless we know the and'd out bits are already zero.
96  // Also, check that the inner shift is valid (less than the type width) or
97  // we'll crash trying to produce the bit mask for the 'and'.
98  unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits();
99  if (InnerShiftConst->ugt(OuterShAmt) && InnerShiftConst->ult(TypeWidth)) {
100  unsigned InnerShAmt = InnerShiftConst->getZExtValue();
101  unsigned MaskShift =
102  IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
103  APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
104  if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
105  return true;
106  }
107 
108  return false;
109 }
110 
111 /// See if we can compute the specified value, but shifted logically to the left
112 /// or right by some number of bits. This should return true if the expression
113 /// can be computed for the same cost as the current expression tree. This is
114 /// used to eliminate extraneous shifting from things like:
115 /// %C = shl i128 %A, 64
116 /// %D = shl i128 %B, 96
117 /// %E = or i128 %C, %D
118 /// %F = lshr i128 %E, 64
119 /// where the client will ask if E can be computed shifted right by 64-bits. If
120 /// this succeeds, getShiftedValue() will be called to produce the value.
121 static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
122  InstCombiner &IC, Instruction *CxtI) {
123  // We can always evaluate constants shifted.
124  if (isa<Constant>(V))
125  return true;
126 
128  if (!I) return false;
129 
130  // If this is the opposite shift, we can directly reuse the input of the shift
131  // if the needed bits are already zero in the input. This allows us to reuse
132  // the value which means that we don't care if the shift has multiple uses.
133  // TODO: Handle opposite shift by exact value.
134  ConstantInt *CI = nullptr;
135  if ((IsLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) ||
136  (!IsLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) {
137  if (CI->getValue() == NumBits) {
138  // TODO: Check that the input bits are already zero with MaskedValueIsZero
139 #if 0
140  // If this is a truncate of a logical shr, we can truncate it to a smaller
141  // lshr iff we know that the bits we would otherwise be shifting in are
142  // already zeros.
143  uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
144  uint32_t BitWidth = Ty->getScalarSizeInBits();
145  if (MaskedValueIsZero(I->getOperand(0),
146  APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
147  CI->getLimitedValue(BitWidth) < BitWidth) {
148  return CanEvaluateTruncated(I->getOperand(0), Ty);
149  }
150 #endif
151 
152  }
153  }
154 
155  // We can't mutate something that has multiple uses: doing so would
156  // require duplicating the instruction in general, which isn't profitable.
157  if (!I->hasOneUse()) return false;
158 
159  switch (I->getOpcode()) {
160  default: return false;
161  case Instruction::And:
162  case Instruction::Or:
163  case Instruction::Xor:
164  // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
165  return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
166  canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
167 
168  case Instruction::Shl:
169  case Instruction::LShr:
170  return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
171 
172  case Instruction::Select: {
173  SelectInst *SI = cast<SelectInst>(I);
174  Value *TrueVal = SI->getTrueValue();
175  Value *FalseVal = SI->getFalseValue();
176  return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
177  canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
178  }
179  case Instruction::PHI: {
180  // We can change a phi if we can change all operands. Note that we never
181  // get into trouble with cyclic PHIs here because we only consider
182  // instructions with a single use.
183  PHINode *PN = cast<PHINode>(I);
184  for (Value *IncValue : PN->incoming_values())
185  if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
186  return false;
187  return true;
188  }
189  }
190 }
191 
192 /// Fold OuterShift (InnerShift X, C1), C2.
193 /// See canEvaluateShiftedShift() for the constraints on these instructions.
194 static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
195  bool IsOuterShl,
196  InstCombiner::BuilderTy &Builder) {
197  bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
198  Type *ShType = InnerShift->getType();
199  unsigned TypeWidth = ShType->getScalarSizeInBits();
200 
201  // We only accept shifts-by-a-constant in canEvaluateShifted().
202  const APInt *C1;
203  match(InnerShift->getOperand(1), m_APInt(C1));
204  unsigned InnerShAmt = C1->getZExtValue();
205 
206  // Change the shift amount and clear the appropriate IR flags.
207  auto NewInnerShift = [&](unsigned ShAmt) {
208  InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt));
209  if (IsInnerShl) {
210  InnerShift->setHasNoUnsignedWrap(false);
211  InnerShift->setHasNoSignedWrap(false);
212  } else {
213  InnerShift->setIsExact(false);
214  }
215  return InnerShift;
216  };
217 
218  // Two logical shifts in the same direction:
219  // shl (shl X, C1), C2 --> shl X, C1 + C2
220  // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
221  if (IsInnerShl == IsOuterShl) {
222  // If this is an oversized composite shift, then unsigned shifts get 0.
223  if (InnerShAmt + OuterShAmt >= TypeWidth)
224  return Constant::getNullValue(ShType);
225 
226  return NewInnerShift(InnerShAmt + OuterShAmt);
227  }
228 
229  // Equal shift amounts in opposite directions become bitwise 'and':
230  // lshr (shl X, C), C --> and X, C'
231  // shl (lshr X, C), C --> and X, C'
232  if (InnerShAmt == OuterShAmt) {
233  APInt Mask = IsInnerShl
234  ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
235  : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
236  Value *And = Builder.CreateAnd(InnerShift->getOperand(0),
237  ConstantInt::get(ShType, Mask));
238  if (auto *AndI = dyn_cast<Instruction>(And)) {
239  AndI->moveBefore(InnerShift);
240  AndI->takeName(InnerShift);
241  }
242  return And;
243  }
244 
245  assert(InnerShAmt > OuterShAmt &&
246  "Unexpected opposite direction logical shift pair");
247 
248  // In general, we would need an 'and' for this transform, but
249  // canEvaluateShiftedShift() guarantees that the masked-off bits are not used.
250  // lshr (shl X, C1), C2 --> shl X, C1 - C2
251  // shl (lshr X, C1), C2 --> lshr X, C1 - C2
252  return NewInnerShift(InnerShAmt - OuterShAmt);
253 }
254 
255 /// When canEvaluateShifted() returns true for an expression, this function
256 /// inserts the new computation that produces the shifted value.
257 static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
258  InstCombiner &IC, const DataLayout &DL) {
259  // We can always evaluate constants shifted.
260  if (Constant *C = dyn_cast<Constant>(V)) {
261  if (isLeftShift)
262  V = IC.Builder.CreateShl(C, NumBits);
263  else
264  V = IC.Builder.CreateLShr(C, NumBits);
265  // If we got a constantexpr back, try to simplify it with TD info.
266  if (auto *C = dyn_cast<Constant>(V))
267  if (auto *FoldedC =
269  V = FoldedC;
270  return V;
271  }
272 
273  Instruction *I = cast<Instruction>(V);
274  IC.Worklist.Add(I);
275 
276  switch (I->getOpcode()) {
277  default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
278  case Instruction::And:
279  case Instruction::Or:
280  case Instruction::Xor:
281  // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
282  I->setOperand(
283  0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
284  I->setOperand(
285  1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
286  return I;
287 
288  case Instruction::Shl:
289  case Instruction::LShr:
290  return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
291  IC.Builder);
292 
293  case Instruction::Select:
294  I->setOperand(
295  1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
296  I->setOperand(
297  2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
298  return I;
299  case Instruction::PHI: {
300  // We can change a phi if we can change all operands. Note that we never
301  // get into trouble with cyclic PHIs here because we only consider
302  // instructions with a single use.
303  PHINode *PN = cast<PHINode>(I);
304  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
305  PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits,
306  isLeftShift, IC, DL));
307  return PN;
308  }
309  }
310 }
311 
312 // If this is a bitwise operator or add with a constant RHS we might be able
313 // to pull it through a shift.
315  BinaryOperator *BO) {
316  switch (BO->getOpcode()) {
317  default:
318  return false; // Do not perform transform!
319  case Instruction::Add:
320  return Shift.getOpcode() == Instruction::Shl;
321  case Instruction::Or:
322  case Instruction::Xor:
323  case Instruction::And:
324  return true;
325  }
326 }
327 
329  BinaryOperator &I) {
330  bool isLeftShift = I.getOpcode() == Instruction::Shl;
331 
332  const APInt *Op1C;
333  if (!match(Op1, m_APInt(Op1C)))
334  return nullptr;
335 
336  // See if we can propagate this shift into the input, this covers the trivial
337  // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
338  if (I.getOpcode() != Instruction::AShr &&
339  canEvaluateShifted(Op0, Op1C->getZExtValue(), isLeftShift, *this, &I)) {
340  LLVM_DEBUG(
341  dbgs() << "ICE: GetShiftedValue propagating shift through expression"
342  " to eliminate shift:\n IN: "
343  << *Op0 << "\n SH: " << I << "\n");
344 
345  return replaceInstUsesWith(
346  I, getShiftedValue(Op0, Op1C->getZExtValue(), isLeftShift, *this, DL));
347  }
348 
349  // See if we can simplify any instructions used by the instruction whose sole
350  // purpose is to compute bits we don't care about.
351  unsigned TypeBits = Op0->getType()->getScalarSizeInBits();
352 
353  assert(!Op1C->uge(TypeBits) &&
354  "Shift over the type width should have been removed already");
355 
356  if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
357  return FoldedShift;
358 
359  // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
360  if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
361  Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
362  // If 'shift2' is an ashr, we would have to get the sign bit into a funny
363  // place. Don't try to do this transformation in this case. Also, we
364  // require that the input operand is a shift-by-constant so that we have
365  // confidence that the shifts will get folded together. We could do this
366  // xform in more cases, but it is unlikely to be profitable.
367  if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
368  isa<ConstantInt>(TrOp->getOperand(1))) {
369  // Okay, we'll do this xform. Make the shift of shift.
370  Constant *ShAmt =
371  ConstantExpr::getZExt(cast<Constant>(Op1), TrOp->getType());
372  // (shift2 (shift1 & 0x00FF), c2)
373  Value *NSh = Builder.CreateBinOp(I.getOpcode(), TrOp, ShAmt, I.getName());
374 
375  // For logical shifts, the truncation has the effect of making the high
376  // part of the register be zeros. Emulate this by inserting an AND to
377  // clear the top bits as needed. This 'and' will usually be zapped by
378  // other xforms later if dead.
379  unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
380  unsigned DstSize = TI->getType()->getScalarSizeInBits();
381  APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
382 
383  // The mask we constructed says what the trunc would do if occurring
384  // between the shifts. We want to know the effect *after* the second
385  // shift. We know that it is a logical shift by a constant, so adjust the
386  // mask as appropriate.
387  if (I.getOpcode() == Instruction::Shl)
388  MaskV <<= Op1C->getZExtValue();
389  else {
390  assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
391  MaskV.lshrInPlace(Op1C->getZExtValue());
392  }
393 
394  // shift1 & 0x00FF
395  Value *And = Builder.CreateAnd(NSh,
396  ConstantInt::get(I.getContext(), MaskV),
397  TI->getName());
398 
399  // Return the value truncated to the interesting size.
400  return new TruncInst(And, I.getType());
401  }
402  }
403 
404  if (Op0->hasOneUse()) {
405  if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
406  // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
407  Value *V1, *V2;
408  ConstantInt *CC;
409  switch (Op0BO->getOpcode()) {
410  default: break;
411  case Instruction::Add:
412  case Instruction::And:
413  case Instruction::Or:
414  case Instruction::Xor: {
415  // These operators commute.
416  // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
417  if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
418  match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
419  m_Specific(Op1)))) {
420  Value *YS = // (Y << C)
421  Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
422  // (X + (Y << C))
423  Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), YS, V1,
424  Op0BO->getOperand(1)->getName());
425  unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
426 
427  APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
429  if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
430  Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
431  return BinaryOperator::CreateAnd(X, Mask);
432  }
433 
434  // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
435  Value *Op0BOOp1 = Op0BO->getOperand(1);
436  if (isLeftShift && Op0BOOp1->hasOneUse() &&
437  match(Op0BOOp1,
438  m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
439  m_ConstantInt(CC)))) {
440  Value *YS = // (Y << C)
441  Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
442  // X & (CC << C)
443  Value *XM = Builder.CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
444  V1->getName()+".mask");
445  return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
446  }
448  }
449 
450  case Instruction::Sub: {
451  // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
452  if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
453  match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
454  m_Specific(Op1)))) {
455  Value *YS = // (Y << C)
456  Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
457  // (X + (Y << C))
458  Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), V1, YS,
459  Op0BO->getOperand(0)->getName());
460  unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
461 
462  APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
464  if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
465  Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
466  return BinaryOperator::CreateAnd(X, Mask);
467  }
468 
469  // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
470  if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
471  match(Op0BO->getOperand(0),
472  m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
473  m_ConstantInt(CC))) && V2 == Op1) {
474  Value *YS = // (Y << C)
475  Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
476  // X & (CC << C)
477  Value *XM = Builder.CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
478  V1->getName()+".mask");
479 
480  return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
481  }
482 
483  break;
484  }
485  }
486 
487 
488  // If the operand is a bitwise operator with a constant RHS, and the
489  // shift is the only use, we can pull it out of the shift.
490  const APInt *Op0C;
491  if (match(Op0BO->getOperand(1), m_APInt(Op0C))) {
492  if (canShiftBinOpWithConstantRHS(I, Op0BO)) {
493  Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
494  cast<Constant>(Op0BO->getOperand(1)), Op1);
495 
496  Value *NewShift =
497  Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
498  NewShift->takeName(Op0BO);
499 
500  return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
501  NewRHS);
502  }
503  }
504 
505  // If the operand is a subtract with a constant LHS, and the shift
506  // is the only use, we can pull it out of the shift.
507  // This folds (shl (sub C1, X), C2) -> (sub (C1 << C2), (shl X, C2))
508  if (isLeftShift && Op0BO->getOpcode() == Instruction::Sub &&
509  match(Op0BO->getOperand(0), m_APInt(Op0C))) {
510  Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
511  cast<Constant>(Op0BO->getOperand(0)), Op1);
512 
513  Value *NewShift = Builder.CreateShl(Op0BO->getOperand(1), Op1);
514  NewShift->takeName(Op0BO);
515 
516  return BinaryOperator::CreateSub(NewRHS, NewShift);
517  }
518  }
519 
520  // If we have a select that conditionally executes some binary operator,
521  // see if we can pull it the select and operator through the shift.
522  //
523  // For example, turning:
524  // shl (select C, (add X, C1), X), C2
525  // Into:
526  // Y = shl X, C2
527  // select C, (add Y, C1 << C2), Y
528  Value *Cond;
529  BinaryOperator *TBO;
530  Value *FalseVal;
531  if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)),
532  m_Value(FalseVal)))) {
533  const APInt *C;
534  if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal &&
535  match(TBO->getOperand(1), m_APInt(C)) &&
537  Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
538  cast<Constant>(TBO->getOperand(1)), Op1);
539 
540  Value *NewShift =
541  Builder.CreateBinOp(I.getOpcode(), FalseVal, Op1);
542  Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift,
543  NewRHS);
544  return SelectInst::Create(Cond, NewOp, NewShift);
545  }
546  }
547 
548  BinaryOperator *FBO;
549  Value *TrueVal;
550  if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal),
551  m_OneUse(m_BinOp(FBO))))) {
552  const APInt *C;
553  if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal &&
554  match(FBO->getOperand(1), m_APInt(C)) &&
556  Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
557  cast<Constant>(FBO->getOperand(1)), Op1);
558 
559  Value *NewShift =
560  Builder.CreateBinOp(I.getOpcode(), TrueVal, Op1);
561  Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift,
562  NewRHS);
563  return SelectInst::Create(Cond, NewShift, NewOp);
564  }
565  }
566  }
567 
568  return nullptr;
569 }
570 
572  if (Value *V = SimplifyShlInst(I.getOperand(0), I.getOperand(1),
574  SQ.getWithInstruction(&I)))
575  return replaceInstUsesWith(I, V);
576 
577  if (Instruction *X = foldVectorBinop(I))
578  return X;
579 
580  if (Instruction *V = commonShiftTransforms(I))
581  return V;
582 
583  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
584  Type *Ty = I.getType();
585  const APInt *ShAmtAPInt;
586  if (match(Op1, m_APInt(ShAmtAPInt))) {
587  unsigned ShAmt = ShAmtAPInt->getZExtValue();
588  unsigned BitWidth = Ty->getScalarSizeInBits();
589 
590  // shl (zext X), ShAmt --> zext (shl X, ShAmt)
591  // This is only valid if X would have zeros shifted out.
592  Value *X;
593  if (match(Op0, m_ZExt(m_Value(X)))) {
594  unsigned SrcWidth = X->getType()->getScalarSizeInBits();
595  if (ShAmt < SrcWidth &&
596  MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I))
597  return new ZExtInst(Builder.CreateShl(X, ShAmt), Ty);
598  }
599 
600  // (X >> C) << C --> X & (-1 << C)
601  if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) {
602  APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt));
603  return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
604  }
605 
606  // FIXME: we do not yet transform non-exact shr's. The backend (DAGCombine)
607  // needs a few fixes for the rotate pattern recognition first.
608  const APInt *ShOp1;
609  if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(ShOp1))))) {
610  unsigned ShrAmt = ShOp1->getZExtValue();
611  if (ShrAmt < ShAmt) {
612  // If C1 < C2: (X >>?,exact C1) << C2 --> X << (C2 - C1)
613  Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShrAmt);
614  auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
615  NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
616  NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
617  return NewShl;
618  }
619  if (ShrAmt > ShAmt) {
620  // If C1 > C2: (X >>?exact C1) << C2 --> X >>?exact (C1 - C2)
621  Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmt);
622  auto *NewShr = BinaryOperator::Create(
623  cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
624  NewShr->setIsExact(true);
625  return NewShr;
626  }
627  }
628 
629  if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1)))) {
630  unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
631  // Oversized shifts are simplified to zero in InstSimplify.
632  if (AmtSum < BitWidth)
633  // (X << C1) << C2 --> X << (C1 + C2)
634  return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum));
635  }
636 
637  // If the shifted-out value is known-zero, then this is a NUW shift.
638  if (!I.hasNoUnsignedWrap() &&
639  MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmt), 0, &I)) {
641  return &I;
642  }
643 
644  // If the shifted-out value is all signbits, then this is a NSW shift.
645  if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) {
646  I.setHasNoSignedWrap();
647  return &I;
648  }
649  }
650 
651  // Transform (x >> y) << y to x & (-1 << y)
652  // Valid for any type of right-shift.
653  Value *X;
654  if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
655  Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
656  Value *Mask = Builder.CreateShl(AllOnes, Op1);
657  return BinaryOperator::CreateAnd(Mask, X);
658  }
659 
660  Constant *C1;
661  if (match(Op1, m_Constant(C1))) {
662  Constant *C2;
663  Value *X;
664  // (C2 << X) << C1 --> (C2 << C1) << X
665  if (match(Op0, m_OneUse(m_Shl(m_Constant(C2), m_Value(X)))))
666  return BinaryOperator::CreateShl(ConstantExpr::getShl(C2, C1), X);
667 
668  // (X * C2) << C1 --> X * (C2 << C1)
669  if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
671  }
672 
673  return nullptr;
674 }
675 
677  if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
678  SQ.getWithInstruction(&I)))
679  return replaceInstUsesWith(I, V);
680 
681  if (Instruction *X = foldVectorBinop(I))
682  return X;
683 
684  if (Instruction *R = commonShiftTransforms(I))
685  return R;
686 
687  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
688  Type *Ty = I.getType();
689  const APInt *ShAmtAPInt;
690  if (match(Op1, m_APInt(ShAmtAPInt))) {
691  unsigned ShAmt = ShAmtAPInt->getZExtValue();
692  unsigned BitWidth = Ty->getScalarSizeInBits();
693  auto *II = dyn_cast<IntrinsicInst>(Op0);
694  if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt &&
695  (II->getIntrinsicID() == Intrinsic::ctlz ||
696  II->getIntrinsicID() == Intrinsic::cttz ||
697  II->getIntrinsicID() == Intrinsic::ctpop)) {
698  // ctlz.i32(x)>>5 --> zext(x == 0)
699  // cttz.i32(x)>>5 --> zext(x == 0)
700  // ctpop.i32(x)>>5 --> zext(x == -1)
701  bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
702  Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
703  Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
704  return new ZExtInst(Cmp, Ty);
705  }
706 
707  Value *X;
708  const APInt *ShOp1;
709  if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1))) && ShOp1->ult(BitWidth)) {
710  if (ShOp1->ult(ShAmt)) {
711  unsigned ShlAmt = ShOp1->getZExtValue();
712  Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
713  if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
714  // (X <<nuw C1) >>u C2 --> X >>u (C2 - C1)
715  auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
716  NewLShr->setIsExact(I.isExact());
717  return NewLShr;
718  }
719  // (X << C1) >>u C2 --> (X >>u (C2 - C1)) & (-1 >> C2)
720  Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact());
721  APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
722  return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
723  }
724  if (ShOp1->ugt(ShAmt)) {
725  unsigned ShlAmt = ShOp1->getZExtValue();
726  Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
727  if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
728  // (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
729  auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
730  NewShl->setHasNoUnsignedWrap(true);
731  return NewShl;
732  }
733  // (X << C1) >>u C2 --> X << (C1 - C2) & (-1 >> C2)
734  Value *NewShl = Builder.CreateShl(X, ShiftDiff);
735  APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
736  return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
737  }
738  assert(*ShOp1 == ShAmt);
739  // (X << C) >>u C --> X & (-1 >>u C)
740  APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
741  return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
742  }
743 
744  if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) &&
745  (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
746  assert(ShAmt < X->getType()->getScalarSizeInBits() &&
747  "Big shift not simplified to zero?");
748  // lshr (zext iM X to iN), C --> zext (lshr X, C) to iN
749  Value *NewLShr = Builder.CreateLShr(X, ShAmt);
750  return new ZExtInst(NewLShr, Ty);
751  }
752 
753  if (match(Op0, m_SExt(m_Value(X))) &&
754  (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
755  // Are we moving the sign bit to the low bit and widening with high zeros?
756  unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits();
757  if (ShAmt == BitWidth - 1) {
758  // lshr (sext i1 X to iN), N-1 --> zext X to iN
759  if (SrcTyBitWidth == 1)
760  return new ZExtInst(X, Ty);
761 
762  // lshr (sext iM X to iN), N-1 --> zext (lshr X, M-1) to iN
763  if (Op0->hasOneUse()) {
764  Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
765  return new ZExtInst(NewLShr, Ty);
766  }
767  }
768 
769  // lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN
770  if (ShAmt == BitWidth - SrcTyBitWidth && Op0->hasOneUse()) {
771  // The new shift amount can't be more than the narrow source type.
772  unsigned NewShAmt = std::min(ShAmt, SrcTyBitWidth - 1);
773  Value *AShr = Builder.CreateAShr(X, NewShAmt);
774  return new ZExtInst(AShr, Ty);
775  }
776  }
777 
778  if (match(Op0, m_LShr(m_Value(X), m_APInt(ShOp1)))) {
779  unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
780  // Oversized shifts are simplified to zero in InstSimplify.
781  if (AmtSum < BitWidth)
782  // (X >>u C1) >>u C2 --> X >>u (C1 + C2)
783  return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
784  }
785 
786  // If the shifted-out value is known-zero, then this is an exact shift.
787  if (!I.isExact() &&
788  MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
789  I.setIsExact();
790  return &I;
791  }
792  }
793 
794  // Transform (x << y) >> y to x & (-1 >> y)
795  Value *X;
796  if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) {
797  Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
798  Value *Mask = Builder.CreateLShr(AllOnes, Op1);
799  return BinaryOperator::CreateAnd(Mask, X);
800  }
801 
802  return nullptr;
803 }
804 
806  if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
807  SQ.getWithInstruction(&I)))
808  return replaceInstUsesWith(I, V);
809 
810  if (Instruction *X = foldVectorBinop(I))
811  return X;
812 
813  if (Instruction *R = commonShiftTransforms(I))
814  return R;
815 
816  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
817  Type *Ty = I.getType();
818  unsigned BitWidth = Ty->getScalarSizeInBits();
819  const APInt *ShAmtAPInt;
820  if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) {
821  unsigned ShAmt = ShAmtAPInt->getZExtValue();
822 
823  // If the shift amount equals the difference in width of the destination
824  // and source scalar types:
825  // ashr (shl (zext X), C), C --> sext X
826  Value *X;
827  if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
828  ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
829  return new SExtInst(X, Ty);
830 
831  // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
832  // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
833  const APInt *ShOp1;
834  if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) &&
835  ShOp1->ult(BitWidth)) {
836  unsigned ShlAmt = ShOp1->getZExtValue();
837  if (ShlAmt < ShAmt) {
838  // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
839  Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
840  auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
841  NewAShr->setIsExact(I.isExact());
842  return NewAShr;
843  }
844  if (ShlAmt > ShAmt) {
845  // (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
846  Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
847  auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
848  NewShl->setHasNoSignedWrap(true);
849  return NewShl;
850  }
851  }
852 
853  if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) &&
854  ShOp1->ult(BitWidth)) {
855  unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
856  // Oversized arithmetic shifts replicate the sign bit.
857  AmtSum = std::min(AmtSum, BitWidth - 1);
858  // (X >>s C1) >>s C2 --> X >>s (C1 + C2)
859  return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
860  }
861 
862  if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) &&
863  (Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) {
864  // ashr (sext X), C --> sext (ashr X, C')
865  Type *SrcTy = X->getType();
866  ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1);
867  Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt));
868  return new SExtInst(NewSh, Ty);
869  }
870 
871  // If the shifted-out value is known-zero, then this is an exact shift.
872  if (!I.isExact() &&
873  MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
874  I.setIsExact();
875  return &I;
876  }
877  }
878 
879  // See if we can turn a signed shr into an unsigned shr.
880  if (MaskedValueIsZero(Op0, APInt::getSignMask(BitWidth), 0, &I))
881  return BinaryOperator::CreateLShr(Op0, Op1);
882 
883  return nullptr;
884 }
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:756
uint64_t CallInst * C
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
Definition: PatternMatch.h:940
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1562
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
BinaryOps getOpcode() const
Definition: InstrTypes.h:379
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:744
This class represents zero extension of integer types.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
Definition: PatternMatch.h:708
static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift, InstCombiner &IC, Instruction *CxtI)
See if we can compute the specified value, but shifted logically to the left or right by some number ...
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:647
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:89
const Value * getTrueValue() const
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:786
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:709
void Add(Instruction *I)
Add - Add the specified instruction to the worklist if it isn&#39;t already in it.
This class represents a sign extension of integer types.
Value * SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:229
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:274
Value * SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:47
This class represents the LLVM &#39;select&#39; instruction.
Instruction * commonShiftTransforms(BinaryOperator &I)
Exact_match< T > m_Exact(const T &SubPattern)
Definition: PatternMatch.h:981
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:196
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:196
The core instruction combiner logic.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if &#39;V & Mask&#39; is known to be zero.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:977
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition: PatternMatch.h:641
static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift, BinaryOperator *BO)
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1674
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag...
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:81
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:137
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:125
bool isKnownNonNegative(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Returns true if the give value is known to be non-negative.
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:384
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:291
This class represents a truncation of integer types.
Value * getOperand(unsigned i) const
Definition: User.h:169
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:635
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:61
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...
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition: PatternMatch.h:780
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
Definition: PatternMatch.h:175
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:428
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1184
This is an important base class in LLVM.
Definition: Constant.h:41
static unsigned getScalarSizeInBits(Type *Ty)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:501
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:774
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:73
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:328
Instruction * visitLShr(BinaryOperator &I)
static wasm::ValType getType(const TargetRegisterClass *RC)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
bool isExact() const
Determine whether the exact flag is set.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
TargetLibraryInfo & getTargetLibraryInfo() const
InstCombineWorklist & Worklist
A worklist of the instructions that need to be simplified.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:50
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:554
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1128
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition: Constants.h:250
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:129
static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl, Instruction *InnerShift, InstCombiner &IC, Instruction *CxtI)
Return true if we can simplify two logical (either left or right) shifts that have constant shift amo...
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:631
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.cpp:645
unsigned getNumIncomingValues() const
Return the number of incoming edges.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1292
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
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:538
Class to represent vector types.
Definition: DerivedTypes.h:424
Class for arbitrary precision integers.
Definition: APInt.h:69
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1138
const Value * getFalseValue() const
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth=0, const Instruction *CxtI=nullptr) const
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Definition: APInt.h:1254
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:481
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
Value * SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
#define I(x, y, z)
Definition: MD5.cpp:58
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
Definition: PatternMatch.h:843
static Value * getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, InstCombiner &IC, const DataLayout &DL)
When canEvaluateShifted() returns true for an expression, this function inserts the new computation t...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2299
bool isLogicalShift() const
Return true if this is a logical shift left or a logical shift right.
Definition: Instruction.h:165
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1199
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Value * foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt, bool IsOuterShl, InstCombiner::BuilderTy &Builder)
Fold OuterShift (InnerShift X, C1), C2.
LLVM Value Representation.
Definition: Value.h:72
This file provides internal interfaces used to implement the InstCombine.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1159
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:412
Instruction * visitAShr(BinaryOperator &I)
void setIncomingValue(unsigned i, Value *V)
Instruction * visitShl(BinaryOperator &I)
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
#define LLVM_DEBUG(X)
Definition: Debug.h:122
op_range incoming_values()
Instruction * FoldShiftByConstant(Value *Op0, Constant *Op1, BinaryOperator &I)
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:43
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.
Definition: Constants.cpp:1815