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  const APInt &C) {
317  bool IsValid = true; // Valid only for And, Or Xor,
318  bool HighBitSet = false; // Transform ifhigh bit of constant set?
319 
320  switch (BO->getOpcode()) {
321  default: IsValid = false; break; // Do not perform transform!
322  case Instruction::Add:
323  IsValid = Shift.getOpcode() == Instruction::Shl;
324  break;
325  case Instruction::Or:
326  case Instruction::Xor:
327  HighBitSet = false;
328  break;
329  case Instruction::And:
330  HighBitSet = true;
331  break;
332  }
333 
334  // If this is a signed shift right, and the high bit is modified
335  // by the logical operation, do not perform the transformation.
336  // The HighBitSet boolean indicates the value of the high bit of
337  // the constant which would cause it to be modified for this
338  // operation.
339  //
340  if (IsValid && Shift.getOpcode() == Instruction::AShr)
341  IsValid = C.isNegative() == HighBitSet;
342 
343  return IsValid;
344 }
345 
347  BinaryOperator &I) {
348  bool isLeftShift = I.getOpcode() == Instruction::Shl;
349 
350  const APInt *Op1C;
351  if (!match(Op1, m_APInt(Op1C)))
352  return nullptr;
353 
354  // See if we can propagate this shift into the input, this covers the trivial
355  // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
356  if (I.getOpcode() != Instruction::AShr &&
357  canEvaluateShifted(Op0, Op1C->getZExtValue(), isLeftShift, *this, &I)) {
358  LLVM_DEBUG(
359  dbgs() << "ICE: GetShiftedValue propagating shift through expression"
360  " to eliminate shift:\n IN: "
361  << *Op0 << "\n SH: " << I << "\n");
362 
363  return replaceInstUsesWith(
364  I, getShiftedValue(Op0, Op1C->getZExtValue(), isLeftShift, *this, DL));
365  }
366 
367  // See if we can simplify any instructions used by the instruction whose sole
368  // purpose is to compute bits we don't care about.
369  unsigned TypeBits = Op0->getType()->getScalarSizeInBits();
370 
371  assert(!Op1C->uge(TypeBits) &&
372  "Shift over the type width should have been removed already");
373 
374  if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
375  return FoldedShift;
376 
377  // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
378  if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
379  Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
380  // If 'shift2' is an ashr, we would have to get the sign bit into a funny
381  // place. Don't try to do this transformation in this case. Also, we
382  // require that the input operand is a shift-by-constant so that we have
383  // confidence that the shifts will get folded together. We could do this
384  // xform in more cases, but it is unlikely to be profitable.
385  if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
386  isa<ConstantInt>(TrOp->getOperand(1))) {
387  // Okay, we'll do this xform. Make the shift of shift.
388  Constant *ShAmt =
389  ConstantExpr::getZExt(cast<Constant>(Op1), TrOp->getType());
390  // (shift2 (shift1 & 0x00FF), c2)
391  Value *NSh = Builder.CreateBinOp(I.getOpcode(), TrOp, ShAmt, I.getName());
392 
393  // For logical shifts, the truncation has the effect of making the high
394  // part of the register be zeros. Emulate this by inserting an AND to
395  // clear the top bits as needed. This 'and' will usually be zapped by
396  // other xforms later if dead.
397  unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
398  unsigned DstSize = TI->getType()->getScalarSizeInBits();
399  APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
400 
401  // The mask we constructed says what the trunc would do if occurring
402  // between the shifts. We want to know the effect *after* the second
403  // shift. We know that it is a logical shift by a constant, so adjust the
404  // mask as appropriate.
405  if (I.getOpcode() == Instruction::Shl)
406  MaskV <<= Op1C->getZExtValue();
407  else {
408  assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
409  MaskV.lshrInPlace(Op1C->getZExtValue());
410  }
411 
412  // shift1 & 0x00FF
413  Value *And = Builder.CreateAnd(NSh,
414  ConstantInt::get(I.getContext(), MaskV),
415  TI->getName());
416 
417  // Return the value truncated to the interesting size.
418  return new TruncInst(And, I.getType());
419  }
420  }
421 
422  if (Op0->hasOneUse()) {
423  if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
424  // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
425  Value *V1, *V2;
426  ConstantInt *CC;
427  switch (Op0BO->getOpcode()) {
428  default: break;
429  case Instruction::Add:
430  case Instruction::And:
431  case Instruction::Or:
432  case Instruction::Xor: {
433  // These operators commute.
434  // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
435  if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
436  match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
437  m_Specific(Op1)))) {
438  Value *YS = // (Y << C)
439  Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
440  // (X + (Y << C))
441  Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), YS, V1,
442  Op0BO->getOperand(1)->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 (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
453  Value *Op0BOOp1 = Op0BO->getOperand(1);
454  if (isLeftShift && Op0BOOp1->hasOneUse() &&
455  match(Op0BOOp1,
456  m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
457  m_ConstantInt(CC)))) {
458  Value *YS = // (Y << C)
459  Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
460  // X & (CC << C)
461  Value *XM = Builder.CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
462  V1->getName()+".mask");
463  return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
464  }
466  }
467 
468  case Instruction::Sub: {
469  // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
470  if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
471  match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
472  m_Specific(Op1)))) {
473  Value *YS = // (Y << C)
474  Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
475  // (X + (Y << C))
476  Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), V1, YS,
477  Op0BO->getOperand(0)->getName());
478  unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
479 
480  APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
482  if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
483  Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
484  return BinaryOperator::CreateAnd(X, Mask);
485  }
486 
487  // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
488  if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
489  match(Op0BO->getOperand(0),
490  m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
491  m_ConstantInt(CC))) && V2 == Op1) {
492  Value *YS = // (Y << C)
493  Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
494  // X & (CC << C)
495  Value *XM = Builder.CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
496  V1->getName()+".mask");
497 
498  return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
499  }
500 
501  break;
502  }
503  }
504 
505 
506  // If the operand is a bitwise operator with a constant RHS, and the
507  // shift is the only use, we can pull it out of the shift.
508  const APInt *Op0C;
509  if (match(Op0BO->getOperand(1), m_APInt(Op0C))) {
510  if (canShiftBinOpWithConstantRHS(I, Op0BO, *Op0C)) {
511  Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
512  cast<Constant>(Op0BO->getOperand(1)), Op1);
513 
514  Value *NewShift =
515  Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
516  NewShift->takeName(Op0BO);
517 
518  return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
519  NewRHS);
520  }
521  }
522 
523  // If the operand is a subtract with a constant LHS, and the shift
524  // is the only use, we can pull it out of the shift.
525  // This folds (shl (sub C1, X), C2) -> (sub (C1 << C2), (shl X, C2))
526  if (isLeftShift && Op0BO->getOpcode() == Instruction::Sub &&
527  match(Op0BO->getOperand(0), m_APInt(Op0C))) {
528  Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
529  cast<Constant>(Op0BO->getOperand(0)), Op1);
530 
531  Value *NewShift = Builder.CreateShl(Op0BO->getOperand(1), Op1);
532  NewShift->takeName(Op0BO);
533 
534  return BinaryOperator::CreateSub(NewRHS, NewShift);
535  }
536  }
537 
538  // If we have a select that conditionally executes some binary operator,
539  // see if we can pull it the select and operator through the shift.
540  //
541  // For example, turning:
542  // shl (select C, (add X, C1), X), C2
543  // Into:
544  // Y = shl X, C2
545  // select C, (add Y, C1 << C2), Y
546  Value *Cond;
547  BinaryOperator *TBO;
548  Value *FalseVal;
549  if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)),
550  m_Value(FalseVal)))) {
551  const APInt *C;
552  if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal &&
553  match(TBO->getOperand(1), m_APInt(C)) &&
554  canShiftBinOpWithConstantRHS(I, TBO, *C)) {
555  Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
556  cast<Constant>(TBO->getOperand(1)), Op1);
557 
558  Value *NewShift =
559  Builder.CreateBinOp(I.getOpcode(), FalseVal, Op1);
560  Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift,
561  NewRHS);
562  return SelectInst::Create(Cond, NewOp, NewShift);
563  }
564  }
565 
566  BinaryOperator *FBO;
567  Value *TrueVal;
568  if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal),
569  m_OneUse(m_BinOp(FBO))))) {
570  const APInt *C;
571  if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal &&
572  match(FBO->getOperand(1), m_APInt(C)) &&
573  canShiftBinOpWithConstantRHS(I, FBO, *C)) {
574  Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
575  cast<Constant>(FBO->getOperand(1)), Op1);
576 
577  Value *NewShift =
578  Builder.CreateBinOp(I.getOpcode(), TrueVal, Op1);
579  Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift,
580  NewRHS);
581  return SelectInst::Create(Cond, NewShift, NewOp);
582  }
583  }
584  }
585 
586  return nullptr;
587 }
588 
590  if (Value *V = SimplifyShlInst(I.getOperand(0), I.getOperand(1),
592  SQ.getWithInstruction(&I)))
593  return replaceInstUsesWith(I, V);
594 
595  if (Instruction *X = foldVectorBinop(I))
596  return X;
597 
598  if (Instruction *V = commonShiftTransforms(I))
599  return V;
600 
601  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
602  Type *Ty = I.getType();
603  const APInt *ShAmtAPInt;
604  if (match(Op1, m_APInt(ShAmtAPInt))) {
605  unsigned ShAmt = ShAmtAPInt->getZExtValue();
606  unsigned BitWidth = Ty->getScalarSizeInBits();
607 
608  // shl (zext X), ShAmt --> zext (shl X, ShAmt)
609  // This is only valid if X would have zeros shifted out.
610  Value *X;
611  if (match(Op0, m_ZExt(m_Value(X)))) {
612  unsigned SrcWidth = X->getType()->getScalarSizeInBits();
613  if (ShAmt < SrcWidth &&
614  MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I))
615  return new ZExtInst(Builder.CreateShl(X, ShAmt), Ty);
616  }
617 
618  // (X >> C) << C --> X & (-1 << C)
619  if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) {
620  APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt));
621  return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
622  }
623 
624  // FIXME: we do not yet transform non-exact shr's. The backend (DAGCombine)
625  // needs a few fixes for the rotate pattern recognition first.
626  const APInt *ShOp1;
627  if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(ShOp1))))) {
628  unsigned ShrAmt = ShOp1->getZExtValue();
629  if (ShrAmt < ShAmt) {
630  // If C1 < C2: (X >>?,exact C1) << C2 --> X << (C2 - C1)
631  Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShrAmt);
632  auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
633  NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
634  NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
635  return NewShl;
636  }
637  if (ShrAmt > ShAmt) {
638  // If C1 > C2: (X >>?exact C1) << C2 --> X >>?exact (C1 - C2)
639  Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmt);
640  auto *NewShr = BinaryOperator::Create(
641  cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
642  NewShr->setIsExact(true);
643  return NewShr;
644  }
645  }
646 
647  if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1)))) {
648  unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
649  // Oversized shifts are simplified to zero in InstSimplify.
650  if (AmtSum < BitWidth)
651  // (X << C1) << C2 --> X << (C1 + C2)
652  return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum));
653  }
654 
655  // If the shifted-out value is known-zero, then this is a NUW shift.
656  if (!I.hasNoUnsignedWrap() &&
657  MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmt), 0, &I)) {
659  return &I;
660  }
661 
662  // If the shifted-out value is all signbits, then this is a NSW shift.
663  if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) {
664  I.setHasNoSignedWrap();
665  return &I;
666  }
667  }
668 
669  // Transform (x >> y) << y to x & (-1 << y)
670  // Valid for any type of right-shift.
671  Value *X;
672  if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
673  Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
674  Value *Mask = Builder.CreateShl(AllOnes, Op1);
675  return BinaryOperator::CreateAnd(Mask, X);
676  }
677 
678  Constant *C1;
679  if (match(Op1, m_Constant(C1))) {
680  Constant *C2;
681  Value *X;
682  // (C2 << X) << C1 --> (C2 << C1) << X
683  if (match(Op0, m_OneUse(m_Shl(m_Constant(C2), m_Value(X)))))
684  return BinaryOperator::CreateShl(ConstantExpr::getShl(C2, C1), X);
685 
686  // (X * C2) << C1 --> X * (C2 << C1)
687  if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
689  }
690 
691  return nullptr;
692 }
693 
695  if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
696  SQ.getWithInstruction(&I)))
697  return replaceInstUsesWith(I, V);
698 
699  if (Instruction *X = foldVectorBinop(I))
700  return X;
701 
702  if (Instruction *R = commonShiftTransforms(I))
703  return R;
704 
705  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
706  Type *Ty = I.getType();
707  const APInt *ShAmtAPInt;
708  if (match(Op1, m_APInt(ShAmtAPInt))) {
709  unsigned ShAmt = ShAmtAPInt->getZExtValue();
710  unsigned BitWidth = Ty->getScalarSizeInBits();
711  auto *II = dyn_cast<IntrinsicInst>(Op0);
712  if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt &&
713  (II->getIntrinsicID() == Intrinsic::ctlz ||
714  II->getIntrinsicID() == Intrinsic::cttz ||
715  II->getIntrinsicID() == Intrinsic::ctpop)) {
716  // ctlz.i32(x)>>5 --> zext(x == 0)
717  // cttz.i32(x)>>5 --> zext(x == 0)
718  // ctpop.i32(x)>>5 --> zext(x == -1)
719  bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
720  Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
721  Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
722  return new ZExtInst(Cmp, Ty);
723  }
724 
725  Value *X;
726  const APInt *ShOp1;
727  if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1))) && ShOp1->ult(BitWidth)) {
728  if (ShOp1->ult(ShAmt)) {
729  unsigned ShlAmt = ShOp1->getZExtValue();
730  Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
731  if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
732  // (X <<nuw C1) >>u C2 --> X >>u (C2 - C1)
733  auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
734  NewLShr->setIsExact(I.isExact());
735  return NewLShr;
736  }
737  // (X << C1) >>u C2 --> (X >>u (C2 - C1)) & (-1 >> C2)
738  Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact());
739  APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
740  return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
741  }
742  if (ShOp1->ugt(ShAmt)) {
743  unsigned ShlAmt = ShOp1->getZExtValue();
744  Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
745  if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
746  // (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
747  auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
748  NewShl->setHasNoUnsignedWrap(true);
749  return NewShl;
750  }
751  // (X << C1) >>u C2 --> X << (C1 - C2) & (-1 >> C2)
752  Value *NewShl = Builder.CreateShl(X, ShiftDiff);
753  APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
754  return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
755  }
756  assert(*ShOp1 == ShAmt);
757  // (X << C) >>u C --> X & (-1 >>u C)
758  APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
759  return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
760  }
761 
762  if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) &&
763  (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
764  assert(ShAmt < X->getType()->getScalarSizeInBits() &&
765  "Big shift not simplified to zero?");
766  // lshr (zext iM X to iN), C --> zext (lshr X, C) to iN
767  Value *NewLShr = Builder.CreateLShr(X, ShAmt);
768  return new ZExtInst(NewLShr, Ty);
769  }
770 
771  if (match(Op0, m_SExt(m_Value(X))) &&
772  (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
773  // Are we moving the sign bit to the low bit and widening with high zeros?
774  unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits();
775  if (ShAmt == BitWidth - 1) {
776  // lshr (sext i1 X to iN), N-1 --> zext X to iN
777  if (SrcTyBitWidth == 1)
778  return new ZExtInst(X, Ty);
779 
780  // lshr (sext iM X to iN), N-1 --> zext (lshr X, M-1) to iN
781  if (Op0->hasOneUse()) {
782  Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
783  return new ZExtInst(NewLShr, Ty);
784  }
785  }
786 
787  // lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN
788  if (ShAmt == BitWidth - SrcTyBitWidth && Op0->hasOneUse()) {
789  // The new shift amount can't be more than the narrow source type.
790  unsigned NewShAmt = std::min(ShAmt, SrcTyBitWidth - 1);
791  Value *AShr = Builder.CreateAShr(X, NewShAmt);
792  return new ZExtInst(AShr, Ty);
793  }
794  }
795 
796  if (match(Op0, m_LShr(m_Value(X), m_APInt(ShOp1)))) {
797  unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
798  // Oversized shifts are simplified to zero in InstSimplify.
799  if (AmtSum < BitWidth)
800  // (X >>u C1) >>u C2 --> X >>u (C1 + C2)
801  return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
802  }
803 
804  // If the shifted-out value is known-zero, then this is an exact shift.
805  if (!I.isExact() &&
806  MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
807  I.setIsExact();
808  return &I;
809  }
810  }
811 
812  // Transform (x << y) >> y to x & (-1 >> y)
813  Value *X;
814  if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) {
815  Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
816  Value *Mask = Builder.CreateLShr(AllOnes, Op1);
817  return BinaryOperator::CreateAnd(Mask, X);
818  }
819 
820  return nullptr;
821 }
822 
824  if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
825  SQ.getWithInstruction(&I)))
826  return replaceInstUsesWith(I, V);
827 
828  if (Instruction *X = foldVectorBinop(I))
829  return X;
830 
831  if (Instruction *R = commonShiftTransforms(I))
832  return R;
833 
834  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
835  Type *Ty = I.getType();
836  unsigned BitWidth = Ty->getScalarSizeInBits();
837  const APInt *ShAmtAPInt;
838  if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) {
839  unsigned ShAmt = ShAmtAPInt->getZExtValue();
840 
841  // If the shift amount equals the difference in width of the destination
842  // and source scalar types:
843  // ashr (shl (zext X), C), C --> sext X
844  Value *X;
845  if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
846  ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
847  return new SExtInst(X, Ty);
848 
849  // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
850  // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
851  const APInt *ShOp1;
852  if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) &&
853  ShOp1->ult(BitWidth)) {
854  unsigned ShlAmt = ShOp1->getZExtValue();
855  if (ShlAmt < ShAmt) {
856  // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
857  Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
858  auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
859  NewAShr->setIsExact(I.isExact());
860  return NewAShr;
861  }
862  if (ShlAmt > ShAmt) {
863  // (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
864  Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
865  auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
866  NewShl->setHasNoSignedWrap(true);
867  return NewShl;
868  }
869  }
870 
871  if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) &&
872  ShOp1->ult(BitWidth)) {
873  unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
874  // Oversized arithmetic shifts replicate the sign bit.
875  AmtSum = std::min(AmtSum, BitWidth - 1);
876  // (X >>s C1) >>s C2 --> X >>s (C1 + C2)
877  return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
878  }
879 
880  if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) &&
881  (Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) {
882  // ashr (sext X), C --> sext (ashr X, C')
883  Type *SrcTy = X->getType();
884  ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1);
885  Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt));
886  return new SExtInst(NewSh, Ty);
887  }
888 
889  // If the shifted-out value is known-zero, then this is an exact shift.
890  if (!I.isExact() &&
891  MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
892  I.setIsExact();
893  return &I;
894  }
895  }
896 
897  // See if we can turn a signed shr into an unsigned shr.
898  if (MaskedValueIsZero(Op0, APInt::getSignMask(BitWidth), 0, &I))
899  return BinaryOperator::CreateLShr(Op0, Op1);
900 
901  return nullptr;
902 }
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition: PatternMatch.h:748
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:932
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:316
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition: PatternMatch.h:736
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:700
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:778
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:973
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
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:742
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
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
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:363
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:772
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:766
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
static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift, BinaryOperator *BO, const APInt &C)
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:835
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:322
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2318
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