Coverage Report

Created: 2026-04-03 02:21

/home/runner/work/DirectXShaderCompiler/DirectXShaderCompiler/lib/Transforms/InstCombine/InstCombineShifts.cpp
Line
Count
Source (jump to first uncovered line)
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"
15
#include "llvm/Analysis/ConstantFolding.h"
16
#include "llvm/Analysis/InstructionSimplify.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
24
269k
Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
25
269k
  assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
26
269k
  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
27
28
  // See if we can fold away this shift.
29
269k
  if (SimplifyDemandedInstructionBits(I))
30
816
    return &I;
31
32
  // Try to fold constant and into select arguments.
33
268k
  if (isa<Constant>(Op0))
34
20.4k
    if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
35
20
      if (Instruction *R = FoldOpIntoSelect(I, SI))
36
20
        return R;
37
38
268k
  if (Constant *CUI = dyn_cast<Constant>(Op1))
39
217k
    if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
40
3.31k
      return Res;
41
42
  // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
43
  // Because shifts by negative values (which could occur if A were negative)
44
  // are undefined.
45
264k
  Value *A; const APInt *B;
46
264k
  if (Op1->hasOneUse() && 
match(Op1, m_SRem(m_Value(A), m_Power2(B)))33.9k
) {
47
    // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
48
    // demand the sign bit (and many others) here??
49
0
    Value *Rem = Builder->CreateAnd(A, ConstantInt::get(I.getType(), *B-1),
50
0
                                    Op1->getName());
51
0
    I.setOperand(1, Rem);
52
0
    return &I;
53
0
  }
54
55
264k
  return nullptr;
56
264k
}
57
58
/// CanEvaluateShifted - See if we can compute the specified value, but shifted
59
/// logically to the left or right by some number of bits.  This should return
60
/// true if the expression can be computed for the same cost as the current
61
/// expression tree.  This is used to eliminate extraneous shifting from things
62
/// like:
63
///      %C = shl i128 %A, 64
64
///      %D = shl i128 %B, 96
65
///      %E = or i128 %C, %D
66
///      %F = lshr i128 %E, 64
67
/// where the client will ask if E can be computed shifted right by 64-bits.  If
68
/// this succeeds, the GetShiftedValue function will be called to produce the
69
/// value.
70
static bool CanEvaluateShifted(Value *V, unsigned NumBits, bool isLeftShift,
71
205k
                               InstCombiner &IC, Instruction *CxtI) {
72
  // We can always evaluate constants shifted.
73
205k
  if (isa<Constant>(V))
74
1.62k
    return true;
75
76
203k
  Instruction *I = dyn_cast<Instruction>(V);
77
203k
  if (!I) 
return false110
;
78
79
  // If this is the opposite shift, we can directly reuse the input of the shift
80
  // if the needed bits are already zero in the input.  This allows us to reuse
81
  // the value which means that we don't care if the shift has multiple uses.
82
  //  TODO:  Handle opposite shift by exact value.
83
203k
  ConstantInt *CI = nullptr;
84
203k
  if ((isLeftShift && 
match(I, m_LShr(m_Value(), m_ConstantInt(CI)))128k
) ||
85
203k
      
(197k
!isLeftShift197k
&&
match(I, m_Shl(m_Value(), m_ConstantInt(CI)))75.0k
)) {
86
5.44k
    if (CI->getZExtValue() == NumBits) {
87
      // TODO: Check that the input bits are already zero with MaskedValueIsZero
88
#if 0
89
      // If this is a truncate of a logical shr, we can truncate it to a smaller
90
      // lshr iff we know that the bits we would otherwise be shifting in are
91
      // already zeros.
92
      uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
93
      uint32_t BitWidth = Ty->getScalarSizeInBits();
94
      if (MaskedValueIsZero(I->getOperand(0),
95
            APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
96
          CI->getLimitedValue(BitWidth) < BitWidth) {
97
        return CanEvaluateTruncated(I->getOperand(0), Ty);
98
      }
99
#endif
100
101
4.55k
    }
102
5.44k
  }
103
104
  // We can't mutate something that has multiple uses: doing so would
105
  // require duplicating the instruction in general, which isn't profitable.
106
203k
  if (!I->hasOneUse()) 
return false132k
;
107
108
70.5k
  switch (I->getOpcode()) {
109
62.3k
  default: return false;
110
3.60k
  case Instruction::And:
111
7.04k
  case Instruction::Or:
112
7.04k
  case Instruction::Xor:
113
    // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
114
7.04k
    return CanEvaluateShifted(I->getOperand(0), NumBits, isLeftShift, IC, I) &&
115
7.04k
           
CanEvaluateShifted(I->getOperand(1), NumBits, isLeftShift, IC, I)168
;
116
117
20
  case Instruction::Shl: {
118
    // We can often fold the shift into shifts-by-a-constant.
119
20
    CI = dyn_cast<ConstantInt>(I->getOperand(1));
120
20
    if (!CI) 
return false8
;
121
122
    // We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
123
12
    if (isLeftShift) 
return true0
;
124
125
    // We can always turn shl(c)+shr(c) -> and(c2).
126
12
    if (CI->getValue() == NumBits) 
return true0
;
127
128
12
    unsigned TypeWidth = I->getType()->getScalarSizeInBits();
129
130
    // We can turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but it isn't
131
    // profitable unless we know the and'd out bits are already zero.
132
12
    if (CI->getZExtValue() > NumBits) {
133
12
      unsigned LowBits = TypeWidth - CI->getZExtValue();
134
12
      if (IC.MaskedValueIsZero(I->getOperand(0),
135
12
                       APInt::getLowBitsSet(TypeWidth, NumBits) << LowBits,
136
12
                       0, CxtI))
137
0
        return true;
138
12
    }
139
140
12
    return false;
141
12
  }
142
148
  case Instruction::LShr: {
143
    // We can often fold the shift into shifts-by-a-constant.
144
148
    CI = dyn_cast<ConstantInt>(I->getOperand(1));
145
148
    if (!CI) 
return false136
;
146
147
    // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
148
12
    if (!isLeftShift) 
return true0
;
149
150
    // We can always turn lshr(c)+shl(c) -> and(c2).
151
12
    if (CI->getValue() == NumBits) return true;
152
153
0
    unsigned TypeWidth = I->getType()->getScalarSizeInBits();
154
155
    // We can always turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but it isn't
156
    // profitable unless we know the and'd out bits are already zero.
157
0
    if (CI->getValue().ult(TypeWidth) && CI->getZExtValue() > NumBits) {
158
0
      unsigned LowBits = CI->getZExtValue() - NumBits;
159
0
      if (IC.MaskedValueIsZero(I->getOperand(0),
160
0
                          APInt::getLowBitsSet(TypeWidth, NumBits) << LowBits,
161
0
                          0, CxtI))
162
0
        return true;
163
0
    }
164
165
0
    return false;
166
0
  }
167
178
  case Instruction::Select: {
168
178
    SelectInst *SI = cast<SelectInst>(I);
169
178
    return CanEvaluateShifted(SI->getTrueValue(), NumBits, isLeftShift,
170
178
                              IC, SI) &&
171
178
           
CanEvaluateShifted(SI->getFalseValue(), NumBits, isLeftShift, IC, SI)112
;
172
0
  }
173
858
  case Instruction::PHI: {
174
    // We can change a phi if we can change all operands.  Note that we never
175
    // get into trouble with cyclic PHIs here because we only consider
176
    // instructions with a single use.
177
858
    PHINode *PN = cast<PHINode>(I);
178
858
    for (Value *IncValue : PN->incoming_values())
179
2.20k
      if (!CanEvaluateShifted(IncValue, NumBits, isLeftShift,
180
2.20k
                              IC, PN))
181
690
        return false;
182
168
    return true;
183
858
  }
184
70.5k
  }
185
70.5k
}
186
187
/// GetShiftedValue - When CanEvaluateShifted returned true for an expression,
188
/// this value inserts the new computation that produces the shifted value.
189
static Value *GetShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
190
12
                              InstCombiner &IC, const DataLayout &DL) {
191
  // We can always evaluate constants shifted.
192
12
  if (Constant *C = dyn_cast<Constant>(V)) {
193
0
    if (isLeftShift)
194
0
      V = IC.Builder->CreateShl(C, NumBits);
195
0
    else
196
0
      V = IC.Builder->CreateLShr(C, NumBits);
197
    // If we got a constantexpr back, try to simplify it with TD info.
198
0
    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
199
0
      V = ConstantFoldConstantExpression(CE, DL, IC.getTargetLibraryInfo());
200
0
    return V;
201
0
  }
202
203
12
  Instruction *I = cast<Instruction>(V);
204
12
  IC.Worklist.Add(I);
205
206
12
  switch (I->getOpcode()) {
207
0
  default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
208
0
  case Instruction::And:
209
0
  case Instruction::Or:
210
0
  case Instruction::Xor:
211
    // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
212
0
    I->setOperand(
213
0
        0, GetShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
214
0
    I->setOperand(
215
0
        1, GetShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
216
0
    return I;
217
218
0
  case Instruction::Shl: {
219
0
    BinaryOperator *BO = cast<BinaryOperator>(I);
220
0
    unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
221
222
    // We only accept shifts-by-a-constant in CanEvaluateShifted.
223
0
    ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
224
225
    // We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
226
0
    if (isLeftShift) {
227
      // If this is oversized composite shift, then unsigned shifts get 0.
228
0
      unsigned NewShAmt = NumBits+CI->getZExtValue();
229
0
      if (NewShAmt >= TypeWidth)
230
0
        return Constant::getNullValue(I->getType());
231
232
0
      BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
233
0
      BO->setHasNoUnsignedWrap(false);
234
0
      BO->setHasNoSignedWrap(false);
235
0
      return I;
236
0
    }
237
238
    // We turn shl(c)+lshr(c) -> and(c2) if the input doesn't already have
239
    // zeros.
240
0
    if (CI->getValue() == NumBits) {
241
0
      APInt Mask(APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits));
242
0
      V = IC.Builder->CreateAnd(BO->getOperand(0),
243
0
                                ConstantInt::get(BO->getContext(), Mask));
244
0
      if (Instruction *VI = dyn_cast<Instruction>(V)) {
245
0
        VI->moveBefore(BO);
246
0
        VI->takeName(BO);
247
0
      }
248
0
      return V;
249
0
    }
250
251
    // We turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but only when we know that
252
    // the and won't be needed.
253
0
    assert(CI->getZExtValue() > NumBits);
254
0
    BO->setOperand(1, ConstantInt::get(BO->getType(),
255
0
                                       CI->getZExtValue() - NumBits));
256
0
    BO->setHasNoUnsignedWrap(false);
257
0
    BO->setHasNoSignedWrap(false);
258
0
    return BO;
259
0
  }
260
12
  case Instruction::LShr: {
261
12
    BinaryOperator *BO = cast<BinaryOperator>(I);
262
12
    unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
263
    // We only accept shifts-by-a-constant in CanEvaluateShifted.
264
12
    ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
265
266
    // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
267
12
    if (!isLeftShift) {
268
      // If this is oversized composite shift, then unsigned shifts get 0.
269
0
      unsigned NewShAmt = NumBits+CI->getZExtValue();
270
0
      if (NewShAmt >= TypeWidth)
271
0
        return Constant::getNullValue(BO->getType());
272
273
0
      BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
274
0
      BO->setIsExact(false);
275
0
      return I;
276
0
    }
277
278
    // We turn lshr(c)+shl(c) -> and(c2) if the input doesn't already have
279
    // zeros.
280
12
    if (CI->getValue() == NumBits) {
281
12
      APInt Mask(APInt::getHighBitsSet(TypeWidth, TypeWidth - NumBits));
282
12
      V = IC.Builder->CreateAnd(I->getOperand(0),
283
12
                                ConstantInt::get(BO->getContext(), Mask));
284
12
      if (Instruction *VI = dyn_cast<Instruction>(V)) {
285
12
        VI->moveBefore(I);
286
12
        VI->takeName(I);
287
12
      }
288
12
      return V;
289
12
    }
290
291
    // We turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but only when we know that
292
    // the and won't be needed.
293
0
    assert(CI->getZExtValue() > NumBits);
294
0
    BO->setOperand(1, ConstantInt::get(BO->getType(),
295
0
                                       CI->getZExtValue() - NumBits));
296
0
    BO->setIsExact(false);
297
0
    return BO;
298
12
  }
299
300
0
  case Instruction::Select:
301
0
    I->setOperand(
302
0
        1, GetShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
303
0
    I->setOperand(
304
0
        2, GetShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
305
0
    return I;
306
0
  case Instruction::PHI: {
307
    // We can change a phi if we can change all operands.  Note that we never
308
    // get into trouble with cyclic PHIs here because we only consider
309
    // instructions with a single use.
310
0
    PHINode *PN = cast<PHINode>(I);
311
0
    for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
312
0
      PN->setIncomingValue(i, GetShiftedValue(PN->getIncomingValue(i), NumBits,
313
0
                                              isLeftShift, IC, DL));
314
0
    return PN;
315
12
  }
316
12
  }
317
12
}
318
319
320
321
Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1,
322
217k
                                               BinaryOperator &I) {
323
217k
  bool isLeftShift = I.getOpcode() == Instruction::Shl;
324
325
217k
  ConstantInt *COp1 = nullptr;
326
217k
  if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Op1))
327
356
    COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
328
216k
  else if (ConstantVector *CV = dyn_cast<ConstantVector>(Op1))
329
0
    COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
330
216k
  else
331
216k
    COp1 = dyn_cast<ConstantInt>(Op1);
332
333
217k
  if (!COp1)
334
0
    return nullptr;
335
336
  // See if we can propagate this shift into the input, this covers the trivial
337
  // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
338
217k
  if (I.getOpcode() != Instruction::AShr &&
339
217k
      
CanEvaluateShifted(Op0, COp1->getZExtValue(), isLeftShift, *this, &I)195k
) {
340
12
    DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression"
341
12
              " to eliminate shift:\n  IN: " << *Op0 << "\n  SH: " << I <<"\n");
342
343
12
    return ReplaceInstUsesWith(
344
12
        I, GetShiftedValue(Op0, COp1->getZExtValue(), isLeftShift, *this, DL));
345
12
  }
346
347
  // See if we can simplify any instructions used by the instruction whose sole
348
  // purpose is to compute bits we don't care about.
349
217k
  uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
350
351
217k
  assert(!COp1->uge(TypeBits) &&
352
217k
         "Shift over the type width should have been removed already");
353
354
  // ((X*C1) << C2) == (X * (C1 << C2))
355
217k
  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
356
62.2k
    if (BO->getOpcode() == Instruction::Mul && 
isLeftShift5.40k
)
357
328
      if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
358
0
        return BinaryOperator::CreateMul(BO->getOperand(0),
359
0
                                        ConstantExpr::getShl(BOOp, Op1));
360
361
  // Try to fold constant and into select arguments.
362
217k
  if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
363
3.90k
    if (Instruction *R = FoldOpIntoSelect(I, SI))
364
90
      return R;
365
217k
  if (isa<PHINode>(Op0))
366
8.44k
    if (Instruction *NV = FoldOpIntoPhi(I))
367
0
      return NV;
368
369
  // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
370
217k
  if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
371
0
    Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
372
    // If 'shift2' is an ashr, we would have to get the sign bit into a funny
373
    // place.  Don't try to do this transformation in this case.  Also, we
374
    // require that the input operand is a shift-by-constant so that we have
375
    // confidence that the shifts will get folded together.  We could do this
376
    // xform in more cases, but it is unlikely to be profitable.
377
0
    if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
378
0
        isa<ConstantInt>(TrOp->getOperand(1))) {
379
      // Okay, we'll do this xform.  Make the shift of shift.
380
0
      Constant *ShAmt = ConstantExpr::getZExt(COp1, TrOp->getType());
381
      // (shift2 (shift1 & 0x00FF), c2)
382
0
      Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
383
384
      // For logical shifts, the truncation has the effect of making the high
385
      // part of the register be zeros.  Emulate this by inserting an AND to
386
      // clear the top bits as needed.  This 'and' will usually be zapped by
387
      // other xforms later if dead.
388
0
      unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
389
0
      unsigned DstSize = TI->getType()->getScalarSizeInBits();
390
0
      APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
391
392
      // The mask we constructed says what the trunc would do if occurring
393
      // between the shifts.  We want to know the effect *after* the second
394
      // shift.  We know that it is a logical shift by a constant, so adjust the
395
      // mask as appropriate.
396
0
      if (I.getOpcode() == Instruction::Shl)
397
0
        MaskV <<= COp1->getZExtValue();
398
0
      else {
399
0
        assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
400
0
        MaskV = MaskV.lshr(COp1->getZExtValue());
401
0
      }
402
403
      // shift1 & 0x00FF
404
0
      Value *And = Builder->CreateAnd(NSh,
405
0
                                      ConstantInt::get(I.getContext(), MaskV),
406
0
                                      TI->getName());
407
408
      // Return the value truncated to the interesting size.
409
0
      return new TruncInst(And, I.getType());
410
0
    }
411
0
  }
412
413
217k
  if (Op0->hasOneUse()) {
414
84.8k
    if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
415
      // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
416
33.5k
      Value *V1, *V2;
417
33.5k
      ConstantInt *CC;
418
33.5k
      switch (Op0BO->getOpcode()) {
419
16.7k
      default: break;
420
16.7k
      case Instruction::Add:
421
12.1k
      case Instruction::And:
422
15.5k
      case Instruction::Or:
423
15.5k
      case Instruction::Xor: {
424
        // These operators commute.
425
        // Turn (Y + (X >> C)) << C  ->  (X + (Y << C)) & (~0 << C)
426
15.5k
        if (isLeftShift && 
Op0BO->getOperand(1)->hasOneUse()4.67k
&&
427
15.5k
            match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
428
654
                  m_Specific(Op1)))) {
429
0
          Value *YS =         // (Y << C)
430
0
            Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
431
          // (X + (Y << C))
432
0
          Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
433
0
                                          Op0BO->getOperand(1)->getName());
434
0
          uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
435
436
0
          APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
437
0
          Constant *Mask = ConstantInt::get(I.getContext(), Bits);
438
0
          if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
439
0
            Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
440
0
          return BinaryOperator::CreateAnd(X, Mask);
441
0
        }
442
443
        // Turn (Y + ((X >> C) & CC)) << C  ->  ((X & (CC << C)) + (Y << C))
444
15.5k
        Value *Op0BOOp1 = Op0BO->getOperand(1);
445
15.5k
        if (isLeftShift && 
Op0BOOp1->hasOneUse()4.67k
&&
446
15.5k
            match(Op0BOOp1,
447
654
                  m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
448
654
                        m_ConstantInt(CC)))) {
449
0
          Value *YS =   // (Y << C)
450
0
            Builder->CreateShl(Op0BO->getOperand(0), Op1,
451
0
                                         Op0BO->getName());
452
          // X & (CC << C)
453
0
          Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
454
0
                                         V1->getName()+".mask");
455
0
          return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
456
0
        }
457
15.5k
      }
458
459
15.5k
      LLVM_FALLTHROUGH; // HLSL CHANGE
460
16.8k
      case Instruction::Sub: {
461
        // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
462
16.8k
        if (isLeftShift && 
Op0BO->getOperand(0)->hasOneUse()5.91k
&&
463
16.8k
            match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
464
3.57k
                  m_Specific(Op1)))) {
465
0
          Value *YS =  // (Y << C)
466
0
            Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
467
          // (X + (Y << C))
468
0
          Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
469
0
                                          Op0BO->getOperand(0)->getName());
470
0
          uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
471
472
0
          APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
473
0
          Constant *Mask = ConstantInt::get(I.getContext(), Bits);
474
0
          if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
475
0
            Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
476
0
          return BinaryOperator::CreateAnd(X, Mask);
477
0
        }
478
479
        // Turn (((X >> C)&CC) + Y) << C  ->  (X + (Y << C)) & (CC << C)
480
16.8k
        if (isLeftShift && 
Op0BO->getOperand(0)->hasOneUse()5.91k
&&
481
16.8k
            match(Op0BO->getOperand(0),
482
3.57k
                  m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
483
3.57k
                        m_ConstantInt(CC))) && 
V2 == Op10
) {
484
0
          Value *YS = // (Y << C)
485
0
            Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
486
          // X & (CC << C)
487
0
          Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
488
0
                                         V1->getName()+".mask");
489
490
0
          return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
491
0
        }
492
493
16.8k
        break;
494
16.8k
      }
495
33.5k
      }
496
497
498
      // If the operand is a bitwise operator with a constant RHS, and the
499
      // shift is the only use, we can pull it out of the shift.
500
33.5k
      if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
501
11.8k
        bool isValid = true;     // Valid only for And, Or, Xor
502
11.8k
        bool highBitSet = false; // Transform if high bit of constant set?
503
504
11.8k
        switch (Op0BO->getOpcode()) {
505
4.81k
        default: isValid = false; break;   // Do not perform transform!
506
4.05k
        case Instruction::Add:
507
4.05k
          isValid = isLeftShift;
508
4.05k
          break;
509
8
        case Instruction::Or:
510
8
        case Instruction::Xor:
511
8
          highBitSet = false;
512
8
          break;
513
2.92k
        case Instruction::And:
514
2.92k
          highBitSet = true;
515
2.92k
          break;
516
11.8k
        }
517
518
        // If this is a signed shift right, and the high bit is modified
519
        // by the logical operation, do not perform the transformation.
520
        // The highBitSet boolean indicates the value of the high bit of
521
        // the constant which would cause it to be modified for this
522
        // operation.
523
        //
524
11.8k
        if (isValid && 
I.getOpcode() == Instruction::AShr3.05k
)
525
0
          isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
526
527
11.8k
        if (isValid) {
528
3.05k
          Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
529
530
3.05k
          Value *NewShift =
531
3.05k
            Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
532
3.05k
          NewShift->takeName(Op0BO);
533
534
3.05k
          return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
535
3.05k
                                        NewRHS);
536
3.05k
        }
537
11.8k
      }
538
33.5k
    }
539
84.8k
  }
540
541
  // Find out if this is a shift of a shift by a constant.
542
214k
  BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
543
214k
  if (ShiftOp && 
!ShiftOp->isShift()59.1k
)
544
40.7k
    ShiftOp = nullptr;
545
546
214k
  if (ShiftOp && 
isa<ConstantInt>(ShiftOp->getOperand(1))18.4k
) {
547
548
    // This is a constant shift of a constant shift. Be careful about hiding
549
    // shl instructions behind bit masks. They are used to represent multiplies
550
    // by a constant, and it is important that simple arithmetic expressions
551
    // are still recognizable by scalar evolution.
552
    //
553
    // The transforms applied to shl are very similar to the transforms applied
554
    // to mul by constant. We can be more aggressive about optimizing right
555
    // shifts.
556
    //
557
    // Combinations of right and left shifts will still be optimized in
558
    // DAGCombine where scalar evolution no longer applies.
559
560
6.53k
    ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
561
6.53k
    uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
562
6.53k
    uint32_t ShiftAmt2 = COp1->getLimitedValue(TypeBits);
563
6.53k
    assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
564
6.53k
    if (ShiftAmt1 == 0) 
return nullptr0
; // Will be simplified in the future.
565
6.53k
    Value *X = ShiftOp->getOperand(0);
566
567
6.53k
    IntegerType *Ty = cast<IntegerType>(I.getType());
568
569
    // Check for (X << c1) << c2  and  (X >> c1) >> c2
570
6.53k
    if (I.getOpcode() == ShiftOp->getOpcode()) {
571
88
      uint32_t AmtSum = ShiftAmt1+ShiftAmt2;   // Fold into one big shift.
572
      // If this is oversized composite shift, then unsigned shifts get 0, ashr
573
      // saturates.
574
88
      if (AmtSum >= TypeBits) {
575
0
        if (I.getOpcode() != Instruction::AShr)
576
0
          return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
577
0
        AmtSum = TypeBits-1;  // Saturate to 31 for i32 ashr.
578
0
      }
579
580
88
      return BinaryOperator::Create(I.getOpcode(), X,
581
88
                                    ConstantInt::get(Ty, AmtSum));
582
88
    }
583
584
6.45k
    if (ShiftAmt1 == ShiftAmt2) {
585
      // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
586
5.65k
      if (I.getOpcode() == Instruction::LShr &&
587
5.65k
          
ShiftOp->getOpcode() == Instruction::Shl10
) {
588
10
        APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
589
10
        return BinaryOperator::CreateAnd(X,
590
10
                                        ConstantInt::get(I.getContext(), Mask));
591
10
      }
592
5.65k
    } else 
if (796
ShiftAmt1 < ShiftAmt2796
) {
593
448
      uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
594
595
      // (X >>?,exact C1) << C2 --> X << (C2-C1)
596
      // The inexact version is deferred to DAGCombine so we don't hide shl
597
      // behind a bit mask.
598
448
      if (I.getOpcode() == Instruction::Shl &&
599
448
          
ShiftOp->getOpcode() != Instruction::Shl376
&&
600
448
          
ShiftOp->isExact()376
) {
601
0
        assert(ShiftOp->getOpcode() == Instruction::LShr ||
602
0
               ShiftOp->getOpcode() == Instruction::AShr);
603
0
        ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
604
0
        BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
605
0
                                                        X, ShiftDiffCst);
606
0
        NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
607
0
        NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
608
0
        return NewShl;
609
0
      }
610
611
      // (X << C1) >>u C2  --> X >>u (C2-C1) & (-1 >> C2)
612
448
      if (I.getOpcode() == Instruction::LShr &&
613
448
          
ShiftOp->getOpcode() == Instruction::Shl24
) {
614
24
        ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
615
        // (X <<nuw C1) >>u C2 --> X >>u (C2-C1)
616
24
        if (ShiftOp->hasNoUnsignedWrap()) {
617
0
          BinaryOperator *NewLShr = BinaryOperator::Create(Instruction::LShr,
618
0
                                                           X, ShiftDiffCst);
619
0
          NewLShr->setIsExact(I.isExact());
620
0
          return NewLShr;
621
0
        }
622
24
        Value *Shift = Builder->CreateLShr(X, ShiftDiffCst);
623
624
24
        APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
625
24
        return BinaryOperator::CreateAnd(Shift,
626
24
                                         ConstantInt::get(I.getContext(),Mask));
627
24
      }
628
629
      // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
630
      // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
631
424
      if (I.getOpcode() == Instruction::AShr &&
632
424
          
ShiftOp->getOpcode() == Instruction::Shl48
) {
633
48
        if (ShiftOp->hasNoSignedWrap()) {
634
          // (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
635
0
          ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
636
0
          BinaryOperator *NewAShr = BinaryOperator::Create(Instruction::AShr,
637
0
                                                           X, ShiftDiffCst);
638
0
          NewAShr->setIsExact(I.isExact());
639
0
          return NewAShr;
640
0
        }
641
48
      }
642
424
    } else {
643
348
      assert(ShiftAmt2 < ShiftAmt1);
644
348
      uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
645
646
      // (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
647
      // The inexact version is deferred to DAGCombine so we don't hide shl
648
      // behind a bit mask.
649
348
      if (I.getOpcode() == Instruction::Shl &&
650
348
          
ShiftOp->getOpcode() != Instruction::Shl216
&&
651
348
          
ShiftOp->isExact()216
) {
652
24
        ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
653
24
        BinaryOperator *NewShr = BinaryOperator::Create(ShiftOp->getOpcode(),
654
24
                                                        X, ShiftDiffCst);
655
24
        NewShr->setIsExact(true);
656
24
        return NewShr;
657
24
      }
658
659
      // (X << C1) >>u C2  --> X << (C1-C2) & (-1 >> C2)
660
324
      if (I.getOpcode() == Instruction::LShr &&
661
324
          
ShiftOp->getOpcode() == Instruction::Shl12
) {
662
12
        ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
663
12
        if (ShiftOp->hasNoUnsignedWrap()) {
664
          // (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2)
665
0
          BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
666
0
                                                          X, ShiftDiffCst);
667
0
          NewShl->setHasNoUnsignedWrap(true);
668
0
          return NewShl;
669
0
        }
670
12
        Value *Shift = Builder->CreateShl(X, ShiftDiffCst);
671
672
12
        APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
673
12
        return BinaryOperator::CreateAnd(Shift,
674
12
                                         ConstantInt::get(I.getContext(),Mask));
675
12
      }
676
677
      // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
678
      // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
679
312
      if (I.getOpcode() == Instruction::AShr &&
680
312
          
ShiftOp->getOpcode() == Instruction::Shl120
) {
681
120
        if (ShiftOp->hasNoSignedWrap()) {
682
          // (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2)
683
0
          ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
684
0
          BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
685
0
                                                          X, ShiftDiffCst);
686
0
          NewShl->setHasNoSignedWrap(true);
687
0
          return NewShl;
688
0
        }
689
120
      }
690
312
    }
691
6.45k
  }
692
213k
  return nullptr;
693
214k
}
694
695
158k
Instruction *InstCombiner::visitShl(BinaryOperator &I) {
696
158k
  if (Value *V = SimplifyVectorOp(I))
697
0
    return ReplaceInstUsesWith(I, V);
698
699
158k
  if (Value *V =
700
158k
          SimplifyShlInst(I.getOperand(0), I.getOperand(1), I.hasNoSignedWrap(),
701
158k
                          I.hasNoUnsignedWrap(), DL, TLI, DT, AC))
702
0
    return ReplaceInstUsesWith(I, V);
703
704
158k
  if (Instruction *V = commonShiftTransforms(I))
705
1.75k
    return V;
706
707
157k
  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(I.getOperand(1))) {
708
122k
    unsigned ShAmt = Op1C->getZExtValue();
709
710
    // If the shifted-out value is known-zero, then this is a NUW shift.
711
122k
    if (!I.hasNoUnsignedWrap() &&
712
122k
        MaskedValueIsZero(I.getOperand(0),
713
112k
                          APInt::getHighBitsSet(Op1C->getBitWidth(), ShAmt),
714
112k
                          0, &I)) {
715
648
          I.setHasNoUnsignedWrap();
716
648
          return &I;
717
648
        }
718
719
    // If the shifted out value is all signbits, this is a NSW shift.
720
121k
    if (!I.hasNoSignedWrap() &&
721
121k
        
ComputeNumSignBits(I.getOperand(0), 0, &I) > ShAmt115k
) {
722
196
      I.setHasNoSignedWrap();
723
196
      return &I;
724
196
    }
725
121k
  }
726
727
  // (C1 << A) << C2 -> (C1 << C2) << A
728
156k
  Constant *C1, *C2;
729
156k
  Value *A;
730
156k
  if (match(I.getOperand(0), m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) &&
731
156k
      
match(I.getOperand(1), m_Constant(C2))8
)
732
8
    return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A);
733
734
156k
  return nullptr;
735
156k
}
736
737
80.3k
Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
738
80.3k
  if (Value *V = SimplifyVectorOp(I))
739
0
    return ReplaceInstUsesWith(I, V);
740
741
80.3k
  if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
742
80.3k
                                  DL, TLI, DT, AC))
743
0
    return ReplaceInstUsesWith(I, V);
744
745
80.3k
  if (Instruction *R = commonShiftTransforms(I))
746
2.31k
    return R;
747
748
78.0k
  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
749
750
78.0k
  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
751
69.8k
    unsigned ShAmt = Op1C->getZExtValue();
752
753
69.8k
    if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
754
0
      unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
755
      // ctlz.i32(x)>>5  --> zext(x == 0)
756
      // cttz.i32(x)>>5  --> zext(x == 0)
757
      // ctpop.i32(x)>>5 --> zext(x == -1)
758
0
      if ((II->getIntrinsicID() == Intrinsic::ctlz ||
759
0
           II->getIntrinsicID() == Intrinsic::cttz ||
760
0
           II->getIntrinsicID() == Intrinsic::ctpop) &&
761
0
          isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt) {
762
0
        bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
763
0
        Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
764
0
        Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
765
0
        return new ZExtInst(Cmp, II->getType());
766
0
      }
767
0
    }
768
769
    // If the shifted-out value is known-zero, then this is an exact shift.
770
69.8k
    if (!I.isExact() &&
771
69.8k
        MaskedValueIsZero(Op0, APInt::getLowBitsSet(Op1C->getBitWidth(), ShAmt),
772
66.1k
                          0, &I)){
773
396
      I.setIsExact();
774
396
      return &I;
775
396
    }
776
69.8k
  }
777
778
77.6k
  return nullptr;
779
78.0k
}
780
781
29.8k
Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
782
29.8k
  if (Value *V = SimplifyVectorOp(I))
783
0
    return ReplaceInstUsesWith(I, V);
784
785
29.8k
  if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
786
29.8k
                                  DL, TLI, DT, AC))
787
0
    return ReplaceInstUsesWith(I, V);
788
789
29.8k
  if (Instruction *R = commonShiftTransforms(I))
790
90
    return R;
791
792
29.7k
  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
793
794
29.7k
  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
795
21.5k
    unsigned ShAmt = Op1C->getZExtValue();
796
797
    // If the input is a SHL by the same constant (ashr (shl X, C), C), then we
798
    // have a sign-extend idiom.
799
21.5k
    Value *X;
800
21.5k
    if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1)))) {
801
      // If the input is an extension from the shifted amount value, e.g.
802
      //   %x = zext i8 %A to i32
803
      //   %y = shl i32 %x, 24
804
      //   %z = ashr %y, 24
805
      // then turn this into "z = sext i8 A to i32".
806
1.10k
      if (ZExtInst *ZI = dyn_cast<ZExtInst>(X)) {
807
0
        uint32_t SrcBits = ZI->getOperand(0)->getType()->getScalarSizeInBits();
808
0
        uint32_t DestBits = ZI->getType()->getScalarSizeInBits();
809
0
        if (Op1C->getZExtValue() == DestBits-SrcBits)
810
0
          return new SExtInst(ZI->getOperand(0), ZI->getType());
811
0
      }
812
1.10k
    }
813
814
    // If the shifted-out value is known-zero, then this is an exact shift.
815
21.5k
    if (!I.isExact() &&
816
21.5k
        MaskedValueIsZero(Op0,APInt::getLowBitsSet(Op1C->getBitWidth(),ShAmt),
817
19.6k
                          0, &I)){
818
210
      I.setIsExact();
819
210
      return &I;
820
210
    }
821
21.5k
  }
822
823
  // See if we can turn a signed shr into an unsigned shr.
824
29.5k
  if (MaskedValueIsZero(Op0,
825
29.5k
                        APInt::getSignBit(I.getType()->getScalarSizeInBits()),
826
29.5k
                        0, &I))
827
0
    return BinaryOperator::CreateLShr(Op0, Op1);
828
829
29.5k
  return nullptr;
830
29.5k
}