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