Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/InstructionSimplify.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
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 routines for folding instructions into simpler forms
10
// that do not require creating new instructions.  This does constant folding
11
// ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
12
// returning a constant ("and i32 %x, 0" -> "0") or an already existing value
13
// ("and i32 %x, %x" -> "%x").  All operands are assumed to have already been
14
// simplified: This is usually true and assuming it simplifies the logic (if
15
// they have not been simplified then results are correct but maybe suboptimal).
16
//
17
//===----------------------------------------------------------------------===//
18
19
#include "llvm/Analysis/InstructionSimplify.h"
20
#include "llvm/ADT/SetVector.h"
21
#include "llvm/ADT/Statistic.h"
22
#include "llvm/Analysis/AliasAnalysis.h"
23
#include "llvm/Analysis/AssumptionCache.h"
24
#include "llvm/Analysis/CaptureTracking.h"
25
#include "llvm/Analysis/CmpInstAnalysis.h"
26
#include "llvm/Analysis/ConstantFolding.h"
27
#include "llvm/Analysis/LoopAnalysisManager.h"
28
#include "llvm/Analysis/MemoryBuiltins.h"
29
#include "llvm/Analysis/ValueTracking.h"
30
#include "llvm/Analysis/VectorUtils.h"
31
#include "llvm/IR/ConstantRange.h"
32
#include "llvm/IR/DataLayout.h"
33
#include "llvm/IR/Dominators.h"
34
#include "llvm/IR/GetElementPtrTypeIterator.h"
35
#include "llvm/IR/GlobalAlias.h"
36
#include "llvm/IR/InstrTypes.h"
37
#include "llvm/IR/Instructions.h"
38
#include "llvm/IR/Operator.h"
39
#include "llvm/IR/PatternMatch.h"
40
#include "llvm/IR/ValueHandle.h"
41
#include "llvm/Support/KnownBits.h"
42
#include <algorithm>
43
using namespace llvm;
44
using namespace llvm::PatternMatch;
45
46
#define DEBUG_TYPE "instsimplify"
47
48
enum { RecursionLimit = 3 };
49
50
STATISTIC(NumExpand,  "Number of expansions");
51
STATISTIC(NumReassoc, "Number of reassociations");
52
53
static Value *SimplifyAndInst(Value *, Value *, const SimplifyQuery &, unsigned);
54
static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned);
55
static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &,
56
                             const SimplifyQuery &, unsigned);
57
static Value *SimplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &,
58
                            unsigned);
59
static Value *SimplifyFPBinOp(unsigned, Value *, Value *, const FastMathFlags &,
60
                              const SimplifyQuery &, unsigned);
61
static Value *SimplifyCmpInst(unsigned, Value *, Value *, const SimplifyQuery &,
62
                              unsigned);
63
static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
64
                               const SimplifyQuery &Q, unsigned MaxRecurse);
65
static Value *SimplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned);
66
static Value *SimplifyXorInst(Value *, Value *, const SimplifyQuery &, unsigned);
67
static Value *SimplifyCastInst(unsigned, Value *, Type *,
68
                               const SimplifyQuery &, unsigned);
69
static Value *SimplifyGEPInst(Type *, ArrayRef<Value *>, const SimplifyQuery &,
70
                              unsigned);
71
72
static Value *foldSelectWithBinaryOp(Value *Cond, Value *TrueVal,
73
2.99M
                                     Value *FalseVal) {
74
2.99M
  BinaryOperator::BinaryOps BinOpCode;
75
2.99M
  if (auto *BO = dyn_cast<BinaryOperator>(Cond))
76
133k
    BinOpCode = BO->getOpcode();
77
2.85M
  else
78
2.85M
    return nullptr;
79
133k
80
133k
  CmpInst::Predicate ExpectedPred, Pred1, Pred2;
81
133k
  if (BinOpCode == BinaryOperator::Or) {
82
17.3k
    ExpectedPred = ICmpInst::ICMP_NE;
83
116k
  } else if (BinOpCode == BinaryOperator::And) {
84
115k
    ExpectedPred = ICmpInst::ICMP_EQ;
85
115k
  } else
86
1.59k
    return nullptr;
87
132k
88
132k
  // %A = icmp eq %TV, %FV
89
132k
  // %B = icmp eq %X, %Y (and one of these is a select operand)
90
132k
  // %C = and %A, %B
91
132k
  // %D = select %C, %TV, %FV
92
132k
  // -->
93
132k
  // %FV
94
132k
95
132k
  // %A = icmp ne %TV, %FV
96
132k
  // %B = icmp ne %X, %Y (and one of these is a select operand)
97
132k
  // %C = or %A, %B
98
132k
  // %D = select %C, %TV, %FV
99
132k
  // -->
100
132k
  // %TV
101
132k
  Value *X, *Y;
102
132k
  if (!match(Cond, m_c_BinOp(m_c_ICmp(Pred1, m_Specific(TrueVal),
103
132k
                                      m_Specific(FalseVal)),
104
132k
                             m_ICmp(Pred2, m_Value(X), m_Value(Y)))) ||
105
132k
      
Pred1 != Pred229.5k
||
Pred1 != ExpectedPred312
)
106
132k
    return nullptr;
107
31
108
31
  if (X == TrueVal || 
X == FalseVal29
||
Y == TrueVal26
||
Y == FalseVal18
)
109
20
    return BinOpCode == BinaryOperator::Or ? 
TrueVal10
:
FalseVal10
;
110
11
111
11
  return nullptr;
112
11
}
113
114
/// For a boolean type or a vector of boolean type, return false or a vector
115
/// with every element false.
116
123k
static Constant *getFalse(Type *Ty) {
117
123k
  return ConstantInt::getFalse(Ty);
118
123k
}
119
120
/// For a boolean type or a vector of boolean type, return true or a vector
121
/// with every element true.
122
18.7k
static Constant *getTrue(Type *Ty) {
123
18.7k
  return ConstantInt::getTrue(Ty);
124
18.7k
}
125
126
/// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
127
static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS,
128
896k
                          Value *RHS) {
129
896k
  CmpInst *Cmp = dyn_cast<CmpInst>(V);
130
896k
  if (!Cmp)
131
145k
    return false;
132
751k
  CmpInst::Predicate CPred = Cmp->getPredicate();
133
751k
  Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
134
751k
  if (CPred == Pred && 
CLHS == LHS111k
&&
CRHS == RHS58.8k
)
135
970
    return true;
136
750k
  return CPred == CmpInst::getSwappedPredicate(Pred) && 
CLHS == RHS228k
&&
137
750k
    
CRHS == LHS146
;
138
750k
}
139
140
/// Does the given value dominate the specified phi node?
141
4.70M
static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
142
4.70M
  Instruction *I = dyn_cast<Instruction>(V);
143
4.70M
  if (!I)
144
2.66M
    // Arguments and constants dominate all instructions.
145
2.66M
    return true;
146
2.04M
147
2.04M
  // If we are processing instructions (and/or basic blocks) that have not been
148
2.04M
  // fully added to a function, the parent nodes may still be null. Simply
149
2.04M
  // return the conservative answer in these cases.
150
2.04M
  if (!I->getParent() || !P->getParent() || !I->getFunction())
151
17.1k
    return false;
152
2.02M
153
2.02M
  // If we have a DominatorTree then do a precise test.
154
2.02M
  if (DT)
155
1.81M
    return DT->dominates(I, P);
156
212k
157
212k
  // Otherwise, if the instruction is in the entry block and is not an invoke,
158
212k
  // then it obviously dominates all phi nodes.
159
212k
  if (I->getParent() == &I->getFunction()->getEntryBlock() &&
160
212k
      
!isa<InvokeInst>(I)3.11k
)
161
3.11k
    return true;
162
209k
163
209k
  return false;
164
209k
}
165
166
/// Simplify "A op (B op' C)" by distributing op over op', turning it into
167
/// "(A op B) op' (A op C)".  Here "op" is given by Opcode and "op'" is
168
/// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS.
169
/// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)".
170
/// Returns the simplified value, or null if no simplification was performed.
171
static Value *ExpandBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS,
172
                          Instruction::BinaryOps OpcodeToExpand,
173
19.5M
                          const SimplifyQuery &Q, unsigned MaxRecurse) {
174
19.5M
  // Recursion is always used, so bail out at once if we already hit the limit.
175
19.5M
  if (!MaxRecurse--)
176
2.34M
    return nullptr;
177
17.1M
178
17.1M
  // Check whether the expression has the form "(A op' B) op C".
179
17.1M
  if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS))
180
6.56M
    if (Op0->getOpcode() == OpcodeToExpand) {
181
1.41M
      // It does!  Try turning it into "(A op C) op' (B op C)".
182
1.41M
      Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
183
1.41M
      // Do "A op C" and "B op C" both simplify?
184
1.41M
      if (Value *L = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse))
185
20.5k
        if (Value *R = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
186
8.36k
          // They do! Return "L op' R" if it simplifies or is already available.
187
8.36k
          // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
188
8.36k
          if ((L == A && 
R == B1.35k
) ||
(7.79k
Instruction::isCommutative(OpcodeToExpand)7.79k
189
7.79k
                                     && L == B && 
R == A0
)) {
190
566
            ++NumExpand;
191
566
            return LHS;
192
566
          }
193
7.79k
          // Otherwise return "L op' R" if it simplifies.
194
7.79k
          if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) {
195
2.20k
            ++NumExpand;
196
2.20k
            return V;
197
2.20k
          }
198
17.1M
        }
199
1.41M
    }
200
17.1M
201
17.1M
  // Check whether the expression has the form "A op (B op' C)".
202
17.1M
  if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS))
203
2.88M
    if (Op1->getOpcode() == OpcodeToExpand) {
204
688k
      // It does!  Try turning it into "(A op B) op' (A op C)".
205
688k
      Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
206
688k
      // Do "A op B" and "A op C" both simplify?
207
688k
      if (Value *L = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse))
208
6.85k
        if (Value *R = SimplifyBinOp(Opcode, A, C, Q, MaxRecurse)) {
209
322
          // They do! Return "L op' R" if it simplifies or is already available.
210
322
          // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
211
322
          if ((L == B && 
R == C259
) ||
(320
Instruction::isCommutative(OpcodeToExpand)320
212
320
                                     && L == C && 
R == B0
)) {
213
2
            ++NumExpand;
214
2
            return RHS;
215
2
          }
216
320
          // Otherwise return "L op' R" if it simplifies.
217
320
          if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse)) {
218
63
            ++NumExpand;
219
63
            return V;
220
63
          }
221
17.1M
        }
222
688k
    }
223
17.1M
224
17.1M
  return nullptr;
225
17.1M
}
226
227
/// Generic simplifications for associative binary operations.
228
/// Returns the simpler value, or null if none was found.
229
static Value *SimplifyAssociativeBinOp(Instruction::BinaryOps Opcode,
230
                                       Value *LHS, Value *RHS,
231
                                       const SimplifyQuery &Q,
232
32.7M
                                       unsigned MaxRecurse) {
233
32.7M
  assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
234
32.7M
235
32.7M
  // Recursion is always used, so bail out at once if we already hit the limit.
236
32.7M
  if (!MaxRecurse--)
237
4.00M
    return nullptr;
238
28.7M
239
28.7M
  BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
240
28.7M
  BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
241
28.7M
242
28.7M
  // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
243
28.7M
  if (Op0 && 
Op0->getOpcode() == Opcode10.4M
) {
244
3.12M
    Value *A = Op0->getOperand(0);
245
3.12M
    Value *B = Op0->getOperand(1);
246
3.12M
    Value *C = RHS;
247
3.12M
248
3.12M
    // Does "B op C" simplify?
249
3.12M
    if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
250
344k
      // It does!  Return "A op V" if it simplifies or is already available.
251
344k
      // If V equals B then "A op V" is just the LHS.
252
344k
      if (V == B) 
return LHS13.6k
;
253
330k
      // Otherwise return "A op V" if it simplifies.
254
330k
      if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
255
29.2k
        ++NumReassoc;
256
29.2k
        return W;
257
29.2k
      }
258
28.6M
    }
259
3.12M
  }
260
28.6M
261
28.6M
  // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
262
28.6M
  if (Op1 && 
Op1->getOpcode() == Opcode5.49M
) {
263
1.74M
    Value *A = LHS;
264
1.74M
    Value *B = Op1->getOperand(0);
265
1.74M
    Value *C = Op1->getOperand(1);
266
1.74M
267
1.74M
    // Does "A op B" simplify?
268
1.74M
    if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
269
926
      // It does!  Return "V op C" if it simplifies or is already available.
270
926
      // If V equals B then "V op C" is just the RHS.
271
926
      if (V == B) 
return RHS304
;
272
622
      // Otherwise return "V op C" if it simplifies.
273
622
      if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
274
171
        ++NumReassoc;
275
171
        return W;
276
171
      }
277
28.6M
    }
278
1.74M
  }
279
28.6M
280
28.6M
  // The remaining transforms require commutativity as well as associativity.
281
28.6M
  if (!Instruction::isCommutative(Opcode))
282
0
    return nullptr;
283
28.6M
284
28.6M
  // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
285
28.6M
  if (Op0 && 
Op0->getOpcode() == Opcode10.4M
) {
286
3.08M
    Value *A = Op0->getOperand(0);
287
3.08M
    Value *B = Op0->getOperand(1);
288
3.08M
    Value *C = RHS;
289
3.08M
290
3.08M
    // Does "C op A" simplify?
291
3.08M
    if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
292
7.21k
      // It does!  Return "V op B" if it simplifies or is already available.
293
7.21k
      // If V equals A then "V op B" is just the LHS.
294
7.21k
      if (V == A) 
return LHS842
;
295
6.37k
      // Otherwise return "V op B" if it simplifies.
296
6.37k
      if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
297
3.54k
        ++NumReassoc;
298
3.54k
        return W;
299
3.54k
      }
300
28.6M
    }
301
3.08M
  }
302
28.6M
303
28.6M
  // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
304
28.6M
  if (Op1 && 
Op1->getOpcode() == Opcode5.48M
) {
305
1.74M
    Value *A = LHS;
306
1.74M
    Value *B = Op1->getOperand(0);
307
1.74M
    Value *C = Op1->getOperand(1);
308
1.74M
309
1.74M
    // Does "C op A" simplify?
310
1.74M
    if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
311
4.70k
      // It does!  Return "B op V" if it simplifies or is already available.
312
4.70k
      // If V equals C then "B op V" is just the RHS.
313
4.70k
      if (V == C) 
return RHS64
;
314
4.64k
      // Otherwise return "B op V" if it simplifies.
315
4.64k
      if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
316
12
        ++NumReassoc;
317
12
        return W;
318
12
      }
319
28.6M
    }
320
1.74M
  }
321
28.6M
322
28.6M
  return nullptr;
323
28.6M
}
324
325
/// In the case of a binary operation with a select instruction as an operand,
326
/// try to simplify the binop by seeing whether evaluating it on both branches
327
/// of the select results in the same value. Returns the common value if so,
328
/// otherwise returns null.
329
static Value *ThreadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS,
330
                                    Value *RHS, const SimplifyQuery &Q,
331
2.88M
                                    unsigned MaxRecurse) {
332
2.88M
  // Recursion is always used, so bail out at once if we already hit the limit.
333
2.88M
  if (!MaxRecurse--)
334
789k
    return nullptr;
335
2.09M
336
2.09M
  SelectInst *SI;
337
2.09M
  if (isa<SelectInst>(LHS)) {
338
1.38M
    SI = cast<SelectInst>(LHS);
339
1.38M
  } else {
340
706k
    assert(isa<SelectInst>(RHS) && "No select instruction operand!");
341
706k
    SI = cast<SelectInst>(RHS);
342
706k
  }
343
2.09M
344
2.09M
  // Evaluate the BinOp on the true and false branches of the select.
345
2.09M
  Value *TV;
346
2.09M
  Value *FV;
347
2.09M
  if (SI == LHS) {
348
1.38M
    TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
349
1.38M
    FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
350
1.38M
  } else {
351
706k
    TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
352
706k
    FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
353
706k
  }
354
2.09M
355
2.09M
  // If they simplified to the same value, then return the common value.
356
2.09M
  // If they both failed to simplify then return null.
357
2.09M
  if (TV == FV)
358
180k
    return TV;
359
1.91M
360
1.91M
  // If one branch simplified to undef, return the other one.
361
1.91M
  if (TV && 
isa<UndefValue>(TV)445k
)
362
22
    return FV;
363
1.91M
  if (FV && 
isa<UndefValue>(FV)1.47M
)
364
9
    return TV;
365
1.91M
366
1.91M
  // If applying the operation did not change the true and false select values,
367
1.91M
  // then the result of the binop is the select itself.
368
1.91M
  if (TV == SI->getTrueValue() && 
FV == SI->getFalseValue()57.4k
)
369
119
    return SI;
370
1.91M
371
1.91M
  // If one branch simplified and the other did not, and the simplified
372
1.91M
  // value is equal to the unsimplified one, return the simplified value.
373
1.91M
  // For example, select (cond, X, X & Z) & Z -> X & Z.
374
1.91M
  if ((FV && 
!TV1.47M
) ||
(445k
TV445k
&&
!FV445k
)) {
375
1.90M
    // Check that the simplified value has the form "X op Y" where "op" is the
376
1.90M
    // same as the original operation.
377
1.90M
    Instruction *Simplified = dyn_cast<Instruction>(FV ? 
FV1.46M
:
TV439k
);
378
1.90M
    if (Simplified && 
Simplified->getOpcode() == unsigned(Opcode)1.59M
) {
379
404k
      // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
380
404k
      // We already know that "op" is the same as for the simplified value.  See
381
404k
      // if the operands match too.  If so, return the simplified value.
382
404k
      Value *UnsimplifiedBranch = FV ? 
SI->getTrueValue()294k
:
SI->getFalseValue()110k
;
383
404k
      Value *UnsimplifiedLHS = SI == LHS ? 
UnsimplifiedBranch139k
:
LHS264k
;
384
404k
      Value *UnsimplifiedRHS = SI == LHS ? 
RHS139k
:
UnsimplifiedBranch264k
;
385
404k
      if (Simplified->getOperand(0) == UnsimplifiedLHS &&
386
404k
          
Simplified->getOperand(1) == UnsimplifiedRHS15
)
387
15
        return Simplified;
388
404k
      if (Simplified->isCommutative() &&
389
404k
          
Simplified->getOperand(1) == UnsimplifiedLHS404k
&&
390
404k
          
Simplified->getOperand(0) == UnsimplifiedRHS0
)
391
0
        return Simplified;
392
1.91M
    }
393
1.90M
  }
394
1.91M
395
1.91M
  return nullptr;
396
1.91M
}
397
398
/// In the case of a comparison with a select instruction, try to simplify the
399
/// comparison by seeing whether both branches of the select result in the same
400
/// value. Returns the common value if so, otherwise returns null.
401
static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
402
                                  Value *RHS, const SimplifyQuery &Q,
403
914k
                                  unsigned MaxRecurse) {
404
914k
  // Recursion is always used, so bail out at once if we already hit the limit.
405
914k
  if (!MaxRecurse--)
406
5.28k
    return nullptr;
407
908k
408
908k
  // Make sure the select is on the LHS.
409
908k
  if (!isa<SelectInst>(LHS)) {
410
278k
    std::swap(LHS, RHS);
411
278k
    Pred = CmpInst::getSwappedPredicate(Pred);
412
278k
  }
413
908k
  assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
414
908k
  SelectInst *SI = cast<SelectInst>(LHS);
415
908k
  Value *Cond = SI->getCondition();
416
908k
  Value *TV = SI->getTrueValue();
417
908k
  Value *FV = SI->getFalseValue();
418
908k
419
908k
  // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
420
908k
  // Does "cmp TV, RHS" simplify?
421
908k
  Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, Q, MaxRecurse);
422
908k
  if (TCmp == Cond) {
423
59
    // It not only simplified, it simplified to the select condition.  Replace
424
59
    // it with 'true'.
425
59
    TCmp = getTrue(Cond->getType());
426
908k
  } else if (!TCmp) {
427
847k
    // It didn't simplify.  However if "cmp TV, RHS" is equal to the select
428
847k
    // condition then we can replace it with 'true'.  Otherwise give up.
429
847k
    if (!isSameCompare(Cond, Pred, TV, RHS))
430
847k
      return nullptr;
431
80
    TCmp = getTrue(Cond->getType());
432
80
  }
433
908k
434
908k
  // Does "cmp FV, RHS" simplify?
435
908k
  Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, Q, MaxRecurse);
436
61.2k
  if (FCmp == Cond) {
437
200
    // It not only simplified, it simplified to the select condition.  Replace
438
200
    // it with 'false'.
439
200
    FCmp = getFalse(Cond->getType());
440
61.0k
  } else if (!FCmp) {
441
49.1k
    // It didn't simplify.  However if "cmp FV, RHS" is equal to the select
442
49.1k
    // condition then we can replace it with 'false'.  Otherwise give up.
443
49.1k
    if (!isSameCompare(Cond, Pred, FV, RHS))
444
48.2k
      return nullptr;
445
890
    FCmp = getFalse(Cond->getType());
446
890
  }
447
61.2k
448
61.2k
  // If both sides simplified to the same value, then use it as the result of
449
61.2k
  // the original comparison.
450
61.2k
  
if (12.9k
TCmp == FCmp12.9k
)
451
2.79k
    return TCmp;
452
10.1k
453
10.1k
  // The remaining cases only make sense if the select condition has the same
454
10.1k
  // type as the result of the comparison, so bail out if this is not so.
455
10.1k
  if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy())
456
1
    return nullptr;
457
10.1k
  // If the false value simplified to false, then the result of the compare
458
10.1k
  // is equal to "Cond && TCmp".  This also catches the case when the false
459
10.1k
  // value simplified to false and the true value to true, returning "Cond".
460
10.1k
  if (match(FCmp, m_Zero()))
461
5.64k
    if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse))
462
5.61k
      return V;
463
4.56k
  // If the true value simplified to true, then the result of the compare
464
4.56k
  // is equal to "Cond || FCmp".
465
4.56k
  if (match(TCmp, m_One()))
466
24
    if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse))
467
6
      return V;
468
4.55k
  // Finally, if the false value simplified to true and the true value to
469
4.55k
  // false, then the result of the compare is equal to "!Cond".
470
4.55k
  if (match(FCmp, m_One()) && 
match(TCmp, m_Zero())3.09k
)
471
3.06k
    if (Value *V =
472
22
        SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()),
473
22
                        Q, MaxRecurse))
474
22
      return V;
475
4.53k
476
4.53k
  return nullptr;
477
4.53k
}
478
479
/// In the case of a binary operation with an operand that is a PHI instruction,
480
/// try to simplify the binop by seeing whether evaluating it on the incoming
481
/// phi values yields the same result for every value. If so returns the common
482
/// value, otherwise returns null.
483
static Value *ThreadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS,
484
                                 Value *RHS, const SimplifyQuery &Q,
485
1.76M
                                 unsigned MaxRecurse) {
486
1.76M
  // Recursion is always used, so bail out at once if we already hit the limit.
487
1.76M
  if (!MaxRecurse--)
488
193k
    return nullptr;
489
1.57M
490
1.57M
  PHINode *PI;
491
1.57M
  if (isa<PHINode>(LHS)) {
492
1.28M
    PI = cast<PHINode>(LHS);
493
1.28M
    // Bail out if RHS and the phi may be mutually interdependent due to a loop.
494
1.28M
    if (!valueDominatesPHI(RHS, PI, Q.DT))
495
210k
      return nullptr;
496
285k
  } else {
497
285k
    assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
498
285k
    PI = cast<PHINode>(RHS);
499
285k
    // Bail out if LHS and the phi may be mutually interdependent due to a loop.
500
285k
    if (!valueDominatesPHI(LHS, PI, Q.DT))
501
180k
      return nullptr;
502
1.17M
  }
503
1.17M
504
1.17M
  // Evaluate the BinOp on the incoming phi values.
505
1.17M
  Value *CommonValue = nullptr;
506
1.43M
  for (Value *Incoming : PI->incoming_values()) {
507
1.43M
    // If the incoming value is the phi node itself, it can safely be skipped.
508
1.43M
    if (Incoming == PI) 
continue29
;
509
1.43M
    Value *V = PI == LHS ?
510
1.31M
      SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) :
511
1.43M
      
SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse)116k
;
512
1.43M
    // If the operation failed to simplify, or simplified to a different value
513
1.43M
    // to previously, then give up.
514
1.43M
    if (!V || 
(260k
CommonValue260k
&&
V != CommonValue16.9k
))
515
1.17M
      return nullptr;
516
252k
    CommonValue = V;
517
252k
  }
518
1.17M
519
1.17M
  
return CommonValue174
;
520
1.17M
}
521
522
/// In the case of a comparison with a PHI instruction, try to simplify the
523
/// comparison by seeing whether comparing with all of the incoming phi values
524
/// yields the same result every time. If so returns the common result,
525
/// otherwise returns null.
526
static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
527
3.05M
                               const SimplifyQuery &Q, unsigned MaxRecurse) {
528
3.05M
  // Recursion is always used, so bail out at once if we already hit the limit.
529
3.05M
  if (!MaxRecurse--)
530
126k
    return nullptr;
531
2.93M
532
2.93M
  // Make sure the phi is on the LHS.
533
2.93M
  if (!isa<PHINode>(LHS)) {
534
476k
    std::swap(LHS, RHS);
535
476k
    Pred = CmpInst::getSwappedPredicate(Pred);
536
476k
  }
537
2.93M
  assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
538
2.93M
  PHINode *PI = cast<PHINode>(LHS);
539
2.93M
540
2.93M
  // Bail out if RHS and the phi may be mutually interdependent due to a loop.
541
2.93M
  if (!valueDominatesPHI(RHS, PI, Q.DT))
542
791k
    return nullptr;
543
2.14M
544
2.14M
  // Evaluate the BinOp on the incoming phi values.
545
2.14M
  Value *CommonValue = nullptr;
546
3.06M
  for (Value *Incoming : PI->incoming_values()) {
547
3.06M
    // If the incoming value is the phi node itself, it can safely be skipped.
548
3.06M
    if (Incoming == PI) 
continue2.19k
;
549
3.06M
    Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse);
550
3.06M
    // If the operation failed to simplify, or simplified to a different value
551
3.06M
    // to previously, then give up.
552
3.06M
    if (!V || 
(1.15M
CommonValue1.15M
&&
V != CommonValue425k
))
553
2.13M
      return nullptr;
554
931k
    CommonValue = V;
555
931k
  }
556
2.14M
557
2.14M
  
return CommonValue4.62k
;
558
2.14M
}
559
560
static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode,
561
                                       Value *&Op0, Value *&Op1,
562
47.5M
                                       const SimplifyQuery &Q) {
563
47.5M
  if (auto *CLHS = dyn_cast<Constant>(Op0)) {
564
6.54M
    if (auto *CRHS = dyn_cast<Constant>(Op1))
565
1.47M
      return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL);
566
5.06M
567
5.06M
    // Canonicalize the constant to the RHS if this is a commutative operation.
568
5.06M
    if (Instruction::isCommutative(Opcode))
569
3.61M
      std::swap(Op0, Op1);
570
5.06M
  }
571
47.5M
  
return nullptr46.1M
;
572
47.5M
}
573
574
/// Given operands for an Add, see if we can fold the result.
575
/// If not, this returns null.
576
static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
577
17.4M
                              const SimplifyQuery &Q, unsigned MaxRecurse) {
578
17.4M
  if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q))
579
679k
    return C;
580
16.8M
581
16.8M
  // X + undef -> undef
582
16.8M
  if (match(Op1, m_Undef()))
583
15
    return Op1;
584
16.8M
585
16.8M
  // X + 0 -> X
586
16.8M
  if (match(Op1, m_Zero()))
587
113k
    return Op0;
588
16.6M
589
16.6M
  // If two operands are negative, return 0.
590
16.6M
  if (isKnownNegation(Op0, Op1))
591
3.52k
    return Constant::getNullValue(Op0->getType());
592
16.6M
593
16.6M
  // X + (Y - X) -> Y
594
16.6M
  // (Y - X) + X -> Y
595
16.6M
  // Eg: X + -X -> 0
596
16.6M
  Value *Y = nullptr;
597
16.6M
  if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
598
16.6M
      
match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))16.6M
)
599
2.36k
    return Y;
600
16.6M
601
16.6M
  // X + ~X -> -1   since   ~X = -X-1
602
16.6M
  Type *Ty = Op0->getType();
603
16.6M
  if (match(Op0, m_Not(m_Specific(Op1))) ||
604
16.6M
      
match(Op1, m_Not(m_Specific(Op0)))16.6M
)
605
82
    return Constant::getAllOnesValue(Ty);
606
16.6M
607
16.6M
  // add nsw/nuw (xor Y, signmask), signmask --> Y
608
16.6M
  // The no-wrapping add guarantees that the top bit will be set by the add.
609
16.6M
  // Therefore, the xor must be clearing the already set sign bit of Y.
610
16.6M
  if ((IsNSW || 
IsNUW12.1M
) &&
match(Op1, m_SignMask())4.79M
&&
611
16.6M
      
match(Op0, m_Xor(m_Value(Y), m_SignMask()))9
)
612
5
    return Y;
613
16.6M
614
16.6M
  // add nuw %x, -1  ->  -1, because %x can only be 0.
615
16.6M
  if (IsNUW && 
match(Op1, m_AllOnes())2.12M
)
616
31
    return Op1; // Which is -1.
617
16.6M
618
16.6M
  /// i1 add -> xor.
619
16.6M
  if (MaxRecurse && 
Op0->getType()->isIntOrIntVectorTy(1)15.0M
)
620
273
    if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
621
5
      return V;
622
16.6M
623
16.6M
  // Try some generic simplifications for associative operations.
624
16.6M
  if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q,
625
7.74k
                                          MaxRecurse))
626
7.74k
    return V;
627
16.6M
628
16.6M
  // Threading Add over selects and phi nodes is pointless, so don't bother.
629
16.6M
  // Threading over the select in "A + select(cond, B, C)" means evaluating
630
16.6M
  // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
631
16.6M
  // only if B and C are equal.  If B and C are equal then (since we assume
632
16.6M
  // that operands have already been simplified) "select(cond, B, C)" should
633
16.6M
  // have been simplified to the common value of B and C already.  Analysing
634
16.6M
  // "A+B" and "A+C" thus gains nothing, but costs compile time.  Similarly
635
16.6M
  // for threading over phi nodes.
636
16.6M
637
16.6M
  return nullptr;
638
16.6M
}
639
640
Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
641
8.58M
                             const SimplifyQuery &Query) {
642
8.58M
  return ::SimplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit);
643
8.58M
}
644
645
/// Compute the base pointer and cumulative constant offsets for V.
646
///
647
/// This strips all constant offsets off of V, leaving it the base pointer, and
648
/// accumulates the total constant offset applied in the returned constant. It
649
/// returns 0 if V is not a pointer, and returns the constant '0' if there are
650
/// no constant offsets applied.
651
///
652
/// This is very similar to GetPointerBaseWithConstantOffset except it doesn't
653
/// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc.
654
/// folding.
655
static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
656
22.9M
                                                bool AllowNonInbounds = false) {
657
22.9M
  assert(V->getType()->isPtrOrPtrVectorTy());
658
22.9M
659
22.9M
  Type *IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType();
660
22.9M
  APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth());
661
22.9M
662
22.9M
  V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
663
22.9M
  // As that strip may trace through `addrspacecast`, need to sext or trunc
664
22.9M
  // the offset calculated.
665
22.9M
  IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType();
666
22.9M
  Offset = Offset.sextOrTrunc(IntPtrTy->getIntegerBitWidth());
667
22.9M
668
22.9M
  Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset);
669
22.9M
  if (V->getType()->isVectorTy())
670
2
    return ConstantVector::getSplat(V->getType()->getVectorNumElements(),
671
2
                                    OffsetIntPtr);
672
22.9M
  return OffsetIntPtr;
673
22.9M
}
674
675
/// Compute the constant difference between two pointer values.
676
/// If the difference is not a constant, returns zero.
677
static Constant *computePointerDifference(const DataLayout &DL, Value *LHS,
678
453k
                                          Value *RHS) {
679
453k
  Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
680
453k
  Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
681
453k
682
453k
  // If LHS and RHS are not related via constant offsets to the same base
683
453k
  // value, there is nothing we can do here.
684
453k
  if (LHS != RHS)
685
453k
    return nullptr;
686
160
687
160
  // Otherwise, the difference of LHS - RHS can be computed as:
688
160
  //    LHS - RHS
689
160
  //  = (LHSOffset + Base) - (RHSOffset + Base)
690
160
  //  = LHSOffset - RHSOffset
691
160
  return ConstantExpr::getSub(LHSOffset, RHSOffset);
692
160
}
693
694
/// Given operands for a Sub, see if we can fold the result.
695
/// If not, this returns null.
696
static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
697
3.34M
                              const SimplifyQuery &Q, unsigned MaxRecurse) {
698
3.34M
  if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q))
699
23.5k
    return C;
700
3.31M
701
3.31M
  // X - undef -> undef
702
3.31M
  // undef - X -> undef
703
3.31M
  if (match(Op0, m_Undef()) || 
match(Op1, m_Undef())3.31M
)
704
2
    return UndefValue::get(Op0->getType());
705
3.31M
706
3.31M
  // X - 0 -> X
707
3.31M
  if (match(Op1, m_Zero()))
708
3.99k
    return Op0;
709
3.31M
710
3.31M
  // X - X -> 0
711
3.31M
  if (Op0 == Op1)
712
44.1k
    return Constant::getNullValue(Op0->getType());
713
3.26M
714
3.26M
  // Is this a negation?
715
3.26M
  if (match(Op0, m_Zero())) {
716
397k
    // 0 - X -> 0 if the sub is NUW.
717
397k
    if (isNUW)
718
8
      return Constant::getNullValue(Op0->getType());
719
397k
720
397k
    KnownBits Known = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
721
397k
    if (Known.Zero.isMaxSignedValue()) {
722
9
      // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
723
9
      // Op1 must be 0 because negating the minimum signed value is undefined.
724
9
      if (isNSW)
725
3
        return Constant::getNullValue(Op0->getType());
726
6
727
6
      // 0 - X -> X if X is 0 or the minimum signed value.
728
6
      return Op1;
729
6
    }
730
397k
  }
731
3.26M
732
3.26M
  // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
733
3.26M
  // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
734
3.26M
  Value *X = nullptr, *Y = nullptr, *Z = Op1;
735
3.26M
  if (MaxRecurse && 
match(Op0, m_Add(m_Value(X), m_Value(Y)))3.11M
) { // (X + Y) - Z
736
250k
    // See if "V === Y - Z" simplifies.
737
250k
    if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1))
738
37.7k
      // It does!  Now see if "X + V" simplifies.
739
37.7k
      if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) {
740
18.5k
        // It does, we successfully reassociated!
741
18.5k
        ++NumReassoc;
742
18.5k
        return W;
743
18.5k
      }
744
232k
    // See if "V === X - Z" simplifies.
745
232k
    if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1))
746
7.11k
      // It does!  Now see if "Y + V" simplifies.
747
7.11k
      if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) {
748
5.11k
        // It does, we successfully reassociated!
749
5.11k
        ++NumReassoc;
750
5.11k
        return W;
751
5.11k
      }
752
3.24M
  }
753
3.24M
754
3.24M
  // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
755
3.24M
  // For example, X - (X + 1) -> -1
756
3.24M
  X = Op0;
757
3.24M
  if (MaxRecurse && 
match(Op1, m_Add(m_Value(Y), m_Value(Z)))3.08M
) { // X - (Y + Z)
758
174k
    // See if "V === X - Y" simplifies.
759
174k
    if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1))
760
5.68k
      // It does!  Now see if "V - Z" simplifies.
761
5.68k
      if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) {
762
816
        // It does, we successfully reassociated!
763
816
        ++NumReassoc;
764
816
        return W;
765
816
      }
766
173k
    // See if "V === X - Z" simplifies.
767
173k
    if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1))
768
17.2k
      // It does!  Now see if "V - Y" simplifies.
769
17.2k
      if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) {
770
6
        // It does, we successfully reassociated!
771
6
        ++NumReassoc;
772
6
        return W;
773
6
      }
774
3.24M
  }
775
3.24M
776
3.24M
  // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
777
3.24M
  // For example, X - (X - Y) -> Y.
778
3.24M
  Z = Op0;
779
3.24M
  if (MaxRecurse && 
match(Op1, m_Sub(m_Value(X), m_Value(Y)))3.08M
) // Z - (X - Y)
780
117k
    // See if "V === Z - X" simplifies.
781
117k
    if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1))
782
30.1k
      // It does!  Now see if "V + Y" simplifies.
783
30.1k
      if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) {
784
13.8k
        // It does, we successfully reassociated!
785
13.8k
        ++NumReassoc;
786
13.8k
        return W;
787
13.8k
      }
788
3.23M
789
3.23M
  // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
790
3.23M
  if (MaxRecurse && 
match(Op0, m_Trunc(m_Value(X)))3.07M
&&
791
3.23M
      
match(Op1, m_Trunc(m_Value(Y)))16.1k
)
792
3.41k
    if (X->getType() == Y->getType())
793
3.41k
      // See if "V === X - Y" simplifies.
794
3.41k
      if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1))
795
1
        // It does!  Now see if "trunc V" simplifies.
796
1
        if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(),
797
1
                                        Q, MaxRecurse - 1))
798
1
          // It does, return the simplified "trunc V".
799
1
          return W;
800
3.23M
801
3.23M
  // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
802
3.23M
  if (match(Op0, m_PtrToInt(m_Value(X))) &&
803
3.23M
      
match(Op1, m_PtrToInt(m_Value(Y)))516k
)
804
453k
    if (Constant *Result = computePointerDifference(Q.DL, X, Y))
805
160
      return ConstantExpr::getIntegerCast(Result, Op0->getType(), true);
806
3.23M
807
3.23M
  // i1 sub -> xor.
808
3.23M
  if (MaxRecurse && 
Op0->getType()->isIntOrIntVectorTy(1)3.07M
)
809
24
    if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
810
3
      return V;
811
3.23M
812
3.23M
  // Threading Sub over selects and phi nodes is pointless, so don't bother.
813
3.23M
  // Threading over the select in "A - select(cond, B, C)" means evaluating
814
3.23M
  // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
815
3.23M
  // only if B and C are equal.  If B and C are equal then (since we assume
816
3.23M
  // that operands have already been simplified) "select(cond, B, C)" should
817
3.23M
  // have been simplified to the common value of B and C already.  Analysing
818
3.23M
  // "A-B" and "A-C" thus gains nothing, but costs compile time.  Similarly
819
3.23M
  // for threading over phi nodes.
820
3.23M
821
3.23M
  return nullptr;
822
3.23M
}
823
824
Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
825
2.10M
                             const SimplifyQuery &Q) {
826
2.10M
  return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit);
827
2.10M
}
828
829
/// Given operands for a Mul, see if we can fold the result.
830
/// If not, this returns null.
831
static Value *SimplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
832
8.08M
                              unsigned MaxRecurse) {
833
8.08M
  if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q))
834
93.1k
    return C;
835
7.99M
836
7.99M
  // X * undef -> 0
837
7.99M
  // X * 0 -> 0
838
7.99M
  if (match(Op1, m_CombineOr(m_Undef(), m_Zero())))
839
94.8k
    return Constant::getNullValue(Op0->getType());
840
7.89M
841
7.89M
  // X * 1 -> X
842
7.89M
  if (match(Op1, m_One()))
843
1.60M
    return Op0;
844
6.29M
845
6.29M
  // (X / Y) * Y -> X if the division is exact.
846
6.29M
  Value *X = nullptr;
847
6.29M
  if (Q.IIQ.UseInstrInfo &&
848
6.29M
      
(6.29M
match(Op0,
849
6.29M
             m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) ||     // (X / Y) * Y
850
6.29M
       
match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))6.28M
)) // Y * (X / Y)
851
6.91k
    return X;
852
6.28M
853
6.28M
  // i1 mul -> and.
854
6.28M
  if (MaxRecurse && 
Op0->getType()->isIntOrIntVectorTy(1)4.72M
)
855
4
    if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1))
856
2
      return V;
857
6.28M
858
6.28M
  // Try some generic simplifications for associative operations.
859
6.28M
  if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q,
860
2
                                          MaxRecurse))
861
2
    return V;
862
6.28M
863
6.28M
  // Mul distributes over Add. Try some generic simplifications based on this.
864
6.28M
  if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add,
865
0
                             Q, MaxRecurse))
866
0
    return V;
867
6.28M
868
6.28M
  // If the operation is with the result of a select instruction, check whether
869
6.28M
  // operating on either branch of the select always yields the same value.
870
6.28M
  if (isa<SelectInst>(Op0) || 
isa<SelectInst>(Op1)4.95M
)
871
2.43M
    if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q,
872
0
                                         MaxRecurse))
873
0
      return V;
874
6.28M
875
6.28M
  // If the operation is with the result of a phi instruction, check whether
876
6.28M
  // operating on all incoming values of the phi always yields the same value.
877
6.28M
  if (isa<PHINode>(Op0) || 
isa<PHINode>(Op1)5.84M
)
878
513k
    if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q,
879
0
                                      MaxRecurse))
880
0
      return V;
881
6.28M
882
6.28M
  return nullptr;
883
6.28M
}
884
885
1.39M
Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
886
1.39M
  return ::SimplifyMulInst(Op0, Op1, Q, RecursionLimit);
887
1.39M
}
888
889
/// Check for common or similar folds of integer division or integer remainder.
890
/// This applies to all 4 opcodes (sdiv/udiv/srem/urem).
891
690k
static Value *simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv) {
892
690k
  Type *Ty = Op0->getType();
893
690k
894
690k
  // X / undef -> undef
895
690k
  // X % undef -> undef
896
690k
  if (match(Op1, m_Undef()))
897
29
    return Op1;
898
690k
899
690k
  // X / 0 -> undef
900
690k
  // X % 0 -> undef
901
690k
  // We don't need to preserve faults!
902
690k
  if (match(Op1, m_Zero()))
903
81
    return UndefValue::get(Ty);
904
690k
905
690k
  // If any element of a constant divisor vector is zero or undef, the whole op
906
690k
  // is undef.
907
690k
  auto *Op1C = dyn_cast<Constant>(Op1);
908
690k
  if (Op1C && 
Ty->isVectorTy()402k
) {
909
903
    unsigned NumElts = Ty->getVectorNumElements();
910
5.39k
    for (unsigned i = 0; i != NumElts; 
++i4.48k
) {
911
4.51k
      Constant *Elt = Op1C->getAggregateElement(i);
912
4.51k
      if (Elt && (Elt->isNullValue() || 
isa<UndefValue>(Elt)4.50k
))
913
23
        return UndefValue::get(Ty);
914
4.51k
    }
915
903
  }
916
690k
917
690k
  // undef / X -> 0
918
690k
  // undef % X -> 0
919
690k
  
if (690k
match(Op0, m_Undef())690k
)
920
12
    return Constant::getNullValue(Ty);
921
690k
922
690k
  // 0 / X -> 0
923
690k
  // 0 % X -> 0
924
690k
  if (match(Op0, m_Zero()))
925
545
    return Constant::getNullValue(Op0->getType());
926
689k
927
689k
  // X / X -> 1
928
689k
  // X % X -> 0
929
689k
  if (Op0 == Op1)
930
848
    return IsDiv ? 
ConstantInt::get(Ty, 1)476
:
Constant::getNullValue(Ty)372
;
931
688k
932
688k
  // X / 1 -> X
933
688k
  // X % 1 -> 0
934
688k
  // If this is a boolean op (single-bit element type), we can't have
935
688k
  // division-by-zero or remainder-by-zero, so assume the divisor is 1.
936
688k
  // Similarly, if we're zero-extending a boolean divisor, then assume it's a 1.
937
688k
  Value *X;
938
688k
  if (match(Op1, m_One()) || 
Ty->isIntOrIntVectorTy(1)684k
||
939
688k
      
(684k
match(Op1, m_ZExt(m_Value(X)))684k
&&
X->getType()->isIntOrIntVectorTy(1)34.7k
))
940
4.23k
    return IsDiv ? 
Op02.67k
:
Constant::getNullValue(Ty)1.56k
;
941
684k
942
684k
  return nullptr;
943
684k
}
944
945
/// Given a predicate and two operands, return true if the comparison is true.
946
/// This is a helper for div/rem simplification where we return some other value
947
/// when we can prove a relationship between the operands.
948
static bool isICmpTrue(ICmpInst::Predicate Pred, Value *LHS, Value *RHS,
949
586k
                       const SimplifyQuery &Q, unsigned MaxRecurse) {
950
586k
  Value *V = SimplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse);
951
586k
  Constant *C = dyn_cast_or_null<Constant>(V);
952
586k
  return (C && 
C->isAllOnesValue()5.18k
);
953
586k
}
954
955
/// Return true if we can simplify X / Y to 0. Remainder can adapt that answer
956
/// to simplify X % Y to X.
957
static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q,
958
684k
                      unsigned MaxRecurse, bool IsSigned) {
959
684k
  // Recursion is always used, so bail out at once if we already hit the limit.
960
684k
  if (!MaxRecurse--)
961
5.65k
    return false;
962
679k
963
679k
  if (IsSigned) {
964
362k
    // |X| / |Y| --> 0
965
362k
    //
966
362k
    // We require that 1 operand is a simple constant. That could be extended to
967
362k
    // 2 variables if we computed the sign bit for each.
968
362k
    //
969
362k
    // Make sure that a constant is not the minimum signed value because taking
970
362k
    // the abs() of that is undefined.
971
362k
    Type *Ty = X->getType();
972
362k
    const APInt *C;
973
362k
    if (match(X, m_APInt(C)) && 
!C->isMinSignedValue()30.0k
) {
974
28.7k
      // Is the variable divisor magnitude always greater than the constant
975
28.7k
      // dividend magnitude?
976
28.7k
      // |Y| > |C| --> Y < -abs(C) or Y > abs(C)
977
28.7k
      Constant *PosDividendC = ConstantInt::get(Ty, C->abs());
978
28.7k
      Constant *NegDividendC = ConstantInt::get(Ty, -C->abs());
979
28.7k
      if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) ||
980
28.7k
          isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse))
981
0
        return true;
982
362k
    }
983
362k
    if (match(Y, m_APInt(C))) {
984
212k
      // Special-case: we can't take the abs() of a minimum signed value. If
985
212k
      // that's the divisor, then all we have to do is prove that the dividend
986
212k
      // is also not the minimum signed value.
987
212k
      if (C->isMinSignedValue())
988
19
        return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse);
989
212k
990
212k
      // Is the variable dividend magnitude always less than the constant
991
212k
      // divisor magnitude?
992
212k
      // |X| < |C| --> X > -abs(C) and X < abs(C)
993
212k
      Constant *PosDivisorC = ConstantInt::get(Ty, C->abs());
994
212k
      Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs());
995
212k
      if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) &&
996
212k
          
isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse)402
)
997
33
        return true;
998
362k
    }
999
362k
    return false;
1000
362k
  }
1001
316k
1002
316k
  // IsSigned == false.
1003
316k
  // Is the dividend unsigned less than the divisor?
1004
316k
  return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse);
1005
316k
}
1006
1007
/// These are simplifications common to SDiv and UDiv.
1008
static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1009
525k
                          const SimplifyQuery &Q, unsigned MaxRecurse) {
1010
525k
  if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1011
4.17k
    return C;
1012
521k
1013
521k
  if (Value *V = simplifyDivRem(Op0, Op1, true))
1014
3.71k
    return V;
1015
517k
1016
517k
  bool IsSigned = Opcode == Instruction::SDiv;
1017
517k
1018
517k
  // (X * Y) / Y -> X if the multiplication does not overflow.
1019
517k
  Value *X;
1020
517k
  if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) {
1021
300
    auto *Mul = cast<OverflowingBinaryOperator>(Op0);
1022
300
    // If the Mul does not overflow, then we are good to go.
1023
300
    if ((IsSigned && 
Q.IIQ.hasNoSignedWrap(Mul)154
) ||
1024
300
        
(274
!IsSigned274
&&
Q.IIQ.hasNoUnsignedWrap(Mul)146
))
1025
29
      return X;
1026
271
    // If X has the form X = A / Y, then X * Y cannot overflow.
1027
271
    if ((IsSigned && 
match(X, m_SDiv(m_Value(), m_Specific(Op1)))128
) ||
1028
271
        
(269
!IsSigned269
&&
match(X, m_UDiv(m_Value(), m_Specific(Op1)))143
))
1029
4
      return X;
1030
517k
  }
1031
517k
1032
517k
  // (X rem Y) / Y -> 0
1033
517k
  if ((IsSigned && 
match(Op0, m_SRem(m_Value(), m_Specific(Op1)))299k
) ||
1034
517k
      
(517k
!IsSigned517k
&&
match(Op0, m_URem(m_Value(), m_Specific(Op1)))218k
))
1035
2
    return Constant::getNullValue(Op0->getType());
1036
517k
1037
517k
  // (X /u C1) /u C2 -> 0 if C1 * C2 overflow
1038
517k
  ConstantInt *C1, *C2;
1039
517k
  if (!IsSigned && 
match(Op0, m_UDiv(m_Value(X), m_ConstantInt(C1)))218k
&&
1040
517k
      
match(Op1, m_ConstantInt(C2))22.4k
) {
1041
22.4k
    bool Overflow;
1042
22.4k
    (void)C1->getValue().umul_ov(C2->getValue(), Overflow);
1043
22.4k
    if (Overflow)
1044
1
      return Constant::getNullValue(Op0->getType());
1045
517k
  }
1046
517k
1047
517k
  // If the operation is with the result of a select instruction, check whether
1048
517k
  // operating on either branch of the select always yields the same value.
1049
517k
  if (isa<SelectInst>(Op0) || 
isa<SelectInst>(Op1)514k
)
1050
8.71k
    if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1051
0
      return V;
1052
517k
1053
517k
  // If the operation is with the result of a phi instruction, check whether
1054
517k
  // operating on all incoming values of the phi always yields the same value.
1055
517k
  if (isa<PHINode>(Op0) || 
isa<PHINode>(Op1)475k
)
1056
49.4k
    if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1057
4
      return V;
1058
517k
1059
517k
  if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1060
18
    return Constant::getNullValue(Op0->getType());
1061
517k
1062
517k
  return nullptr;
1063
517k
}
1064
1065
/// These are simplifications common to SRem and URem.
1066
static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1067
171k
                          const SimplifyQuery &Q, unsigned MaxRecurse) {
1068
171k
  if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1069
2.20k
    return C;
1070
168k
1071
168k
  if (Value *V = simplifyDivRem(Op0, Op1, false))
1072
2.06k
    return V;
1073
166k
1074
166k
  // (X % Y) % Y -> X % Y
1075
166k
  if ((Opcode == Instruction::SRem &&
1076
166k
       
match(Op0, m_SRem(m_Value(), m_Specific(Op1)))67.1k
) ||
1077
166k
      
(166k
Opcode == Instruction::URem166k
&&
1078
166k
       
match(Op0, m_URem(m_Value(), m_Specific(Op1)))99.7k
))
1079
2
    return Op0;
1080
166k
1081
166k
  // (X << Y) % X -> 0
1082
166k
  if (Q.IIQ.UseInstrInfo &&
1083
166k
      ((Opcode == Instruction::SRem &&
1084
166k
        
match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))67.1k
) ||
1085
166k
       
(166k
Opcode == Instruction::URem166k
&&
1086
166k
        
match(Op0, m_NUWShl(m_Specific(Op1), m_Value()))99.7k
)))
1087
4
    return Constant::getNullValue(Op0->getType());
1088
166k
1089
166k
  // If the operation is with the result of a select instruction, check whether
1090
166k
  // operating on either branch of the select always yields the same value.
1091
166k
  if (isa<SelectInst>(Op0) || 
isa<SelectInst>(Op1)164k
)
1092
5.56k
    if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1093
2
      return V;
1094
166k
1095
166k
  // If the operation is with the result of a phi instruction, check whether
1096
166k
  // operating on all incoming values of the phi always yields the same value.
1097
166k
  if (isa<PHINode>(Op0) || 
isa<PHINode>(Op1)154k
)
1098
19.6k
    if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1099
0
      return V;
1100
166k
1101
166k
  // If X / Y == 0, then X % Y == X.
1102
166k
  if (isDivZero(Op0, Op1, Q, MaxRecurse, Opcode == Instruction::SRem))
1103
100
    return Op0;
1104
166k
1105
166k
  return nullptr;
1106
166k
}
1107
1108
/// Given operands for an SDiv, see if we can fold the result.
1109
/// If not, this returns null.
1110
static Value *SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1111
305k
                               unsigned MaxRecurse) {
1112
305k
  // If two operands are negated and no signed overflow, return -1.
1113
305k
  if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
1114
47
    return Constant::getAllOnesValue(Op0->getType());
1115
305k
1116
305k
  return simplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse);
1117
305k
}
1118
1119
233k
Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1120
233k
  return ::SimplifySDivInst(Op0, Op1, Q, RecursionLimit);
1121
233k
}
1122
1123
/// Given operands for a UDiv, see if we can fold the result.
1124
/// If not, this returns null.
1125
static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1126
220k
                               unsigned MaxRecurse) {
1127
220k
  return simplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse);
1128
220k
}
1129
1130
173k
Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1131
173k
  return ::SimplifyUDivInst(Op0, Op1, Q, RecursionLimit);
1132
173k
}
1133
1134
/// Given operands for an SRem, see if we can fold the result.
1135
/// If not, this returns null.
1136
static Value *SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1137
69.5k
                               unsigned MaxRecurse) {
1138
69.5k
  // If the divisor is 0, the result is undefined, so assume the divisor is -1.
1139
69.5k
  // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0
1140
69.5k
  Value *X;
1141
69.5k
  if (match(Op1, m_SExt(m_Value(X))) && 
X->getType()->isIntOrIntVectorTy(1)162
)
1142
2
    return ConstantInt::getNullValue(Op0->getType());
1143
69.5k
1144
69.5k
  // If the two operands are negated, return 0.
1145
69.5k
  if (isKnownNegation(Op0, Op1))
1146
6
    return ConstantInt::getNullValue(Op0->getType());
1147
69.5k
1148
69.5k
  return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1149
69.5k
}
1150
1151
57.6k
Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1152
57.6k
  return ::SimplifySRemInst(Op0, Op1, Q, RecursionLimit);
1153
57.6k
}
1154
1155
/// Given operands for a URem, see if we can fold the result.
1156
/// If not, this returns null.
1157
static Value *SimplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1158
101k
                               unsigned MaxRecurse) {
1159
101k
  return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1160
101k
}
1161
1162
74.2k
Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1163
74.2k
  return ::SimplifyURemInst(Op0, Op1, Q, RecursionLimit);
1164
74.2k
}
1165
1166
/// Returns true if a shift by \c Amount always yields undef.
1167
4.30M
static bool isUndefShift(Value *Amount) {
1168
4.30M
  Constant *C = dyn_cast<Constant>(Amount);
1169
4.30M
  if (!C)
1170
444k
    return false;
1171
3.85M
1172
3.85M
  // X shift by undef -> undef because it may shift by the bitwidth.
1173
3.85M
  if (isa<UndefValue>(C))
1174
47
    return true;
1175
3.85M
1176
3.85M
  // Shifting by the bitwidth or more is undefined.
1177
3.85M
  if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
1178
3.83M
    if (CI->getValue().getLimitedValue() >=
1179
3.83M
        CI->getType()->getScalarSizeInBits())
1180
31
      return true;
1181
3.85M
1182
3.85M
  // If all lanes of a vector shift are undefined the whole shift is.
1183
3.85M
  if (isa<ConstantVector>(C) || 
isa<ConstantDataVector>(C)3.85M
) {
1184
17.2k
    for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; 
++I62
)
1185
17.2k
      if (!isUndefShift(C->getAggregateElement(I)))
1186
17.2k
        return false;
1187
17.2k
    
return true4
;
1188
3.83M
  }
1189
3.83M
1190
3.83M
  return false;
1191
3.83M
}
1192
1193
/// Given operands for an Shl, LShr or AShr, see if we can fold the result.
1194
/// If not, this returns null.
1195
static Value *SimplifyShift(Instruction::BinaryOps Opcode, Value *Op0,
1196
4.58M
                            Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse) {
1197
4.58M
  if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1198
299k
    return C;
1199
4.28M
1200
4.28M
  // 0 shift by X -> 0
1201
4.28M
  if (match(Op0, m_Zero()))
1202
2.29k
    return Constant::getNullValue(Op0->getType());
1203
4.28M
1204
4.28M
  // X shift by 0 -> X
1205
4.28M
  // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
1206
4.28M
  // would be poison.
1207
4.28M
  Value *X;
1208
4.28M
  if (match(Op1, m_Zero()) ||
1209
4.28M
      
(4.28M
match(Op1, m_SExt(m_Value(X)))4.28M
&&
X->getType()->isIntOrIntVectorTy(1)2.30k
))
1210
802
    return Op0;
1211
4.28M
1212
4.28M
  // Fold undefined shifts.
1213
4.28M
  if (isUndefShift(Op1))
1214
20
    return UndefValue::get(Op0->getType());
1215
4.28M
1216
4.28M
  // If the operation is with the result of a select instruction, check whether
1217
4.28M
  // operating on either branch of the select always yields the same value.
1218
4.28M
  if (isa<SelectInst>(Op0) || 
isa<SelectInst>(Op1)4.04M
)
1219
240k
    if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1220
4
      return V;
1221
4.28M
1222
4.28M
  // If the operation is with the result of a phi instruction, check whether
1223
4.28M
  // operating on all incoming values of the phi always yields the same value.
1224
4.28M
  if (isa<PHINode>(Op0) || 
isa<PHINode>(Op1)3.80M
)
1225
498k
    if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1226
21
      return V;
1227
4.28M
1228
4.28M
  // If any bits in the shift amount make that value greater than or equal to
1229
4.28M
  // the number of bits in the type, the shift is undefined.
1230
4.28M
  KnownBits Known = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1231
4.28M
  if (Known.One.getLimitedValue() >= Known.getBitWidth())
1232
5
    return UndefValue::get(Op0->getType());
1233
4.28M
1234
4.28M
  // If all valid bits in the shift amount are known zero, the first operand is
1235
4.28M
  // unchanged.
1236
4.28M
  unsigned NumValidShiftBits = Log2_32_Ceil(Known.getBitWidth());
1237
4.28M
  if (Known.countMinTrailingZeros() >= NumValidShiftBits)
1238
17
    return Op0;
1239
4.28M
1240
4.28M
  return nullptr;
1241
4.28M
}
1242
1243
/// Given operands for an Shl, LShr or AShr, see if we can
1244
/// fold the result.  If not, this returns null.
1245
static Value *SimplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0,
1246
                                 Value *Op1, bool isExact, const SimplifyQuery &Q,
1247
2.08M
                                 unsigned MaxRecurse) {
1248
2.08M
  if (Value *V = SimplifyShift(Opcode, Op0, Op1, Q, MaxRecurse))
1249
33.2k
    return V;
1250
2.05M
1251
2.05M
  // X >> X -> 0
1252
2.05M
  if (Op0 == Op1)
1253
3
    return Constant::getNullValue(Op0->getType());
1254
2.05M
1255
2.05M
  // undef >> X -> 0
1256
2.05M
  // undef >> X -> undef (if it's exact)
1257
2.05M
  if (match(Op0, m_Undef()))
1258
7
    return isExact ? 
Op02
:
Constant::getNullValue(Op0->getType())5
;
1259
2.05M
1260
2.05M
  // The low bit cannot be shifted out of an exact shift if it is set.
1261
2.05M
  if (isExact) {
1262
311k
    KnownBits Op0Known = computeKnownBits(Op0, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT);
1263
311k
    if (Op0Known.One[0])
1264
4
      return Op0;
1265
2.05M
  }
1266
2.05M
1267
2.05M
  return nullptr;
1268
2.05M
}
1269
1270
/// Given operands for an Shl, see if we can fold the result.
1271
/// If not, this returns null.
1272
static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
1273
2.49M
                              const SimplifyQuery &Q, unsigned MaxRecurse) {
1274
2.49M
  if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, Q, MaxRecurse))
1275
269k
    return V;
1276
2.22M
1277
2.22M
  // undef << X -> 0
1278
2.22M
  // undef << X -> undef if (if it's NSW/NUW)
1279
2.22M
  if (match(Op0, m_Undef()))
1280
4
    return isNSW || 
isNUW2
?
Op03
:
Constant::getNullValue(Op0->getType())1
;
1281
2.22M
1282
2.22M
  // (X >> A) << A -> X
1283
2.22M
  Value *X;
1284
2.22M
  if (Q.IIQ.UseInstrInfo &&
1285
2.22M
      
match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1))))2.22M
)
1286
1.88k
    return X;
1287
2.22M
1288
2.22M
  // shl nuw i8 C, %x  ->  C  iff C has sign bit set.
1289
2.22M
  if (isNUW && 
match(Op0, m_Negative())287k
)
1290
6
    return Op0;
1291
2.22M
  // NOTE: could use computeKnownBits() / LazyValueInfo,
1292
2.22M
  // but the cost-benefit analysis suggests it isn't worth it.
1293
2.22M
1294
2.22M
  return nullptr;
1295
2.22M
}
1296
1297
Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
1298
1.44M
                             const SimplifyQuery &Q) {
1299
1.44M
  return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit);
1300
1.44M
}
1301
1302
/// Given operands for an LShr, see if we can fold the result.
1303
/// If not, this returns null.
1304
static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
1305
1.57M
                               const SimplifyQuery &Q, unsigned MaxRecurse) {
1306
1.57M
  if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q,
1307
29.8k
                                    MaxRecurse))
1308
29.8k
      return V;
1309
1.54M
1310
1.54M
  // (X << A) >> A -> X
1311
1.54M
  Value *X;
1312
1.54M
  if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
1313
160
    return X;
1314
1.54M
1315
1.54M
  // ((X << A) | Y) >> A -> X  if effective width of Y is not larger than A.
1316
1.54M
  // We can return X as we do in the above case since OR alters no bits in X.
1317
1.54M
  // SimplifyDemandedBits in InstCombine can do more general optimization for
1318
1.54M
  // bit manipulation. This pattern aims to provide opportunities for other
1319
1.54M
  // optimizers by supporting a simple but common case in InstSimplify.
1320
1.54M
  Value *Y;
1321
1.54M
  const APInt *ShRAmt, *ShLAmt;
1322
1.54M
  if (match(Op1, m_APInt(ShRAmt)) &&
1323
1.54M
      
match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShLAmt)), m_Value(Y)))1.42M
&&
1324
1.54M
      
*ShRAmt == *ShLAmt4.72k
) {
1325
983
    const KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1326
983
    const unsigned Width = Op0->getType()->getScalarSizeInBits();
1327
983
    const unsigned EffWidthY = Width - YKnown.countMinLeadingZeros();
1328
983
    if (ShRAmt->uge(EffWidthY))
1329
896
      return X;
1330
1.53M
  }
1331
1.53M
1332
1.53M
  return nullptr;
1333
1.53M
}
1334
1335
Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
1336
1.16M
                              const SimplifyQuery &Q) {
1337
1.16M
  return ::SimplifyLShrInst(Op0, Op1, isExact, Q, RecursionLimit);
1338
1.16M
}
1339
1340
/// Given operands for an AShr, see if we can fold the result.
1341
/// If not, this returns null.
1342
static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
1343
518k
                               const SimplifyQuery &Q, unsigned MaxRecurse) {
1344
518k
  if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q,
1345
3.43k
                                    MaxRecurse))
1346
3.43k
    return V;
1347
515k
1348
515k
  // all ones >>a X -> -1
1349
515k
  // Do not return Op0 because it may contain undef elements if it's a vector.
1350
515k
  if (match(Op0, m_AllOnes()))
1351
9
    return Constant::getAllOnesValue(Op0->getType());
1352
515k
1353
515k
  // (X << A) >> A -> X
1354
515k
  Value *X;
1355
515k
  if (Q.IIQ.UseInstrInfo && 
match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1)))515k
)
1356
58
    return X;
1357
515k
1358
515k
  // Arithmetic shifting an all-sign-bit value is a no-op.
1359
515k
  unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1360
515k
  if (NumSignBits == Op0->getType()->getScalarSizeInBits())
1361
11
    return Op0;
1362
515k
1363
515k
  return nullptr;
1364
515k
}
1365
1366
Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
1367
430k
                              const SimplifyQuery &Q) {
1368
430k
  return ::SimplifyAShrInst(Op0, Op1, isExact, Q, RecursionLimit);
1369
430k
}
1370
1371
/// Commuted variants are assumed to be handled by calling this function again
1372
/// with the parameters swapped.
1373
static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp,
1374
2.95M
                                         ICmpInst *UnsignedICmp, bool IsAnd) {
1375
2.95M
  Value *X, *Y;
1376
2.95M
1377
2.95M
  ICmpInst::Predicate EqPred;
1378
2.95M
  if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) ||
1379
2.95M
      
!ICmpInst::isEquality(EqPred)1.34M
)
1380
1.67M
    return nullptr;
1381
1.27M
1382
1.27M
  ICmpInst::Predicate UnsignedPred;
1383
1.27M
  if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) &&
1384
1.27M
      
ICmpInst::isUnsigned(UnsignedPred)2.26k
)
1385
1.03k
    ;
1386
1.27M
  else if (match(UnsignedICmp,
1387
1.27M
                 m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) &&
1388
1.27M
           
ICmpInst::isUnsigned(UnsignedPred)52.5k
)
1389
1.67k
    UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1390
1.27M
  else
1391
1.27M
    return nullptr;
1392
2.70k
1393
2.70k
  // X < Y && Y != 0  -->  X < Y
1394
2.70k
  // X < Y || Y != 0  -->  Y != 0
1395
2.70k
  if (UnsignedPred == ICmpInst::ICMP_ULT && 
EqPred == ICmpInst::ICMP_NE710
)
1396
38
    return IsAnd ? 
UnsignedICmp36
:
ZeroICmp2
;
1397
2.66k
1398
2.66k
  // X >= Y || Y != 0  -->  true
1399
2.66k
  // X >= Y || Y == 0  -->  X >= Y
1400
2.66k
  if (UnsignedPred == ICmpInst::ICMP_UGE && 
!IsAnd189
) {
1401
80
    if (EqPred == ICmpInst::ICMP_NE)
1402
3
      return getTrue(UnsignedICmp->getType());
1403
77
    return UnsignedICmp;
1404
77
  }
1405
2.58k
1406
2.58k
  // X < Y && Y == 0  -->  false
1407
2.58k
  if (UnsignedPred == ICmpInst::ICMP_ULT && 
EqPred == ICmpInst::ICMP_EQ672
&&
1408
2.58k
      
IsAnd672
)
1409
10
    return getFalse(UnsignedICmp->getType());
1410
2.57k
1411
2.57k
  return nullptr;
1412
2.57k
}
1413
1414
/// Commuted variants are assumed to be handled by calling this function again
1415
/// with the parameters swapped.
1416
1.86M
static Value *simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) {
1417
1.86M
  ICmpInst::Predicate Pred0, Pred1;
1418
1.86M
  Value *A ,*B;
1419
1.86M
  if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) ||
1420
1.86M
      !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B))))
1421
1.86M
    return nullptr;
1422
559
1423
559
  // We have (icmp Pred0, A, B) & (icmp Pred1, A, B).
1424
559
  // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we
1425
559
  // can eliminate Op1 from this 'and'.
1426
559
  if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1))
1427
194
    return Op0;
1428
365
1429
365
  // Check for any combination of predicates that are guaranteed to be disjoint.
1430
365
  if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) ||
1431
365
      
(342
Pred0 == ICmpInst::ICMP_EQ342
&&
ICmpInst::isFalseWhenEqual(Pred1)34
) ||
1432
365
      
(308
Pred0 == ICmpInst::ICMP_SLT308
&&
Pred1 == ICmpInst::ICMP_SGT15
) ||
1433
365
      
(304
Pred0 == ICmpInst::ICMP_ULT304
&&
Pred1 == ICmpInst::ICMP_UGT45
))
1434
96
    return getFalse(Op0->getType());
1435
269
1436
269
  return nullptr;
1437
269
}
1438
1439
/// Commuted variants are assumed to be handled by calling this function again
1440
/// with the parameters swapped.
1441
1.08M
static Value *simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) {
1442
1.08M
  ICmpInst::Predicate Pred0, Pred1;
1443
1.08M
  Value *A ,*B;
1444
1.08M
  if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) ||
1445
1.08M
      !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B))))
1446
1.08M
    return nullptr;
1447
3.67k
1448
3.67k
  // We have (icmp Pred0, A, B) | (icmp Pred1, A, B).
1449
3.67k
  // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we
1450
3.67k
  // can eliminate Op0 from this 'or'.
1451
3.67k
  if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1))
1452
2.41k
    return Op1;
1453
1.26k
1454
1.26k
  // Check for any combination of predicates that cover the entire range of
1455
1.26k
  // possibilities.
1456
1.26k
  if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) ||
1457
1.26k
      
(1.16k
Pred0 == ICmpInst::ICMP_NE1.16k
&&
ICmpInst::isTrueWhenEqual(Pred1)17
) ||
1458
1.26k
      
(1.16k
Pred0 == ICmpInst::ICMP_SLE1.16k
&&
Pred1 == ICmpInst::ICMP_SGE15
) ||
1459
1.26k
      
(1.15k
Pred0 == ICmpInst::ICMP_ULE1.15k
&&
Pred1 == ICmpInst::ICMP_UGE13
))
1460
110
    return getTrue(Op0->getType());
1461
1.15k
1462
1.15k
  return nullptr;
1463
1.15k
}
1464
1465
/// Test if a pair of compares with a shared operand and 2 constants has an
1466
/// empty set intersection, full set union, or if one compare is a superset of
1467
/// the other.
1468
static Value *simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1,
1469
1.47M
                                                bool IsAnd) {
1470
1.47M
  // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)).
1471
1.47M
  if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
1472
1.36M
    return nullptr;
1473
106k
1474
106k
  const APInt *C0, *C1;
1475
106k
  if (!match(Cmp0->getOperand(1), m_APInt(C0)) ||
1476
106k
      
!match(Cmp1->getOperand(1), m_APInt(C1))24.3k
)
1477
91.4k
    return nullptr;
1478
14.8k
1479
14.8k
  auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0);
1480
14.8k
  auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1);
1481
14.8k
1482
14.8k
  // For and-of-compares, check if the intersection is empty:
1483
14.8k
  // (icmp X, C0) && (icmp X, C1) --> empty set --> false
1484
14.8k
  if (IsAnd && 
Range0.intersectWith(Range1).isEmptySet()5.67k
)
1485
96
    return getFalse(Cmp0->getType());
1486
14.7k
1487
14.7k
  // For or-of-compares, check if the union is full:
1488
14.7k
  // (icmp X, C0) || (icmp X, C1) --> full set --> true
1489
14.7k
  if (!IsAnd && 
Range0.unionWith(Range1).isFullSet()9.16k
)
1490
43
    return getTrue(Cmp0->getType());
1491
14.7k
1492
14.7k
  // Is one range a superset of the other?
1493
14.7k
  // If this is and-of-compares, take the smaller set:
1494
14.7k
  // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42
1495
14.7k
  // If this is or-of-compares, take the larger set:
1496
14.7k
  // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4
1497
14.7k
  if (Range0.contains(Range1))
1498
667
    return IsAnd ? 
Cmp1590
:
Cmp077
;
1499
14.0k
  if (Range1.contains(Range0))
1500
2.01k
    return IsAnd ? 
Cmp01.02k
:
Cmp1991
;
1501
12.0k
1502
12.0k
  return nullptr;
1503
12.0k
}
1504
1505
static Value *simplifyAndOrOfICmpsWithZero(ICmpInst *Cmp0, ICmpInst *Cmp1,
1506
1.47M
                                           bool IsAnd) {
1507
1.47M
  ICmpInst::Predicate P0 = Cmp0->getPredicate(), P1 = Cmp1->getPredicate();
1508
1.47M
  if (!match(Cmp0->getOperand(1), m_Zero()) ||
1509
1.47M
      
!match(Cmp1->getOperand(1), m_Zero())750k
||
P0 != P1456k
)
1510
1.10M
    return nullptr;
1511
370k
1512
370k
  if ((IsAnd && 
P0 != ICmpInst::ICMP_NE243k
) ||
(242k
!IsAnd242k
&&
P1 != ICmpInst::ICMP_EQ126k
))
1513
135k
    return nullptr;
1514
234k
1515
234k
  // We have either "(X == 0 || Y == 0)" or "(X != 0 && Y != 0)".
1516
234k
  Value *X = Cmp0->getOperand(0);
1517
234k
  Value *Y = Cmp1->getOperand(0);
1518
234k
1519
234k
  // If one of the compares is a masked version of a (not) null check, then
1520
234k
  // that compare implies the other, so we eliminate the other. Optionally, look
1521
234k
  // through a pointer-to-int cast to match a null check of a pointer type.
1522
234k
1523
234k
  // (X == 0) || (([ptrtoint] X & ?) == 0) --> ([ptrtoint] X & ?) == 0
1524
234k
  // (X == 0) || ((? & [ptrtoint] X) == 0) --> (? & [ptrtoint] X) == 0
1525
234k
  // (X != 0) && (([ptrtoint] X & ?) != 0) --> ([ptrtoint] X & ?) != 0
1526
234k
  // (X != 0) && ((? & [ptrtoint] X) != 0) --> (? & [ptrtoint] X) != 0
1527
234k
  if (match(Y, m_c_And(m_Specific(X), m_Value())) ||
1528
234k
      
match(Y, m_c_And(m_PtrToInt(m_Specific(X)), m_Value()))234k
)
1529
10
    return Cmp1;
1530
234k
1531
234k
  // (([ptrtoint] Y & ?) == 0) || (Y == 0) --> ([ptrtoint] Y & ?) == 0
1532
234k
  // ((? & [ptrtoint] Y) == 0) || (Y == 0) --> (? & [ptrtoint] Y) == 0
1533
234k
  // (([ptrtoint] Y & ?) != 0) && (Y != 0) --> ([ptrtoint] Y & ?) != 0
1534
234k
  // ((? & [ptrtoint] Y) != 0) && (Y != 0) --> (? & [ptrtoint] Y) != 0
1535
234k
  if (match(X, m_c_And(m_Specific(Y), m_Value())) ||
1536
234k
      
match(X, m_c_And(m_PtrToInt(m_Specific(Y)), m_Value()))234k
)
1537
9
    return Cmp0;
1538
234k
1539
234k
  return nullptr;
1540
234k
}
1541
1542
static Value *simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
1543
1.85M
                                        const InstrInfoQuery &IIQ) {
1544
1.85M
  // (icmp (add V, C0), C1) & (icmp V, C0)
1545
1.85M
  ICmpInst::Predicate Pred0, Pred1;
1546
1.85M
  const APInt *C0, *C1;
1547
1.85M
  Value *V;
1548
1.85M
  if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1549
1.81M
    return nullptr;
1550
42.7k
1551
42.7k
  if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1552
42.0k
    return nullptr;
1553
643
1554
643
  auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0));
1555
643
  if (AddInst->getOperand(1) != Op1->getOperand(1))
1556
631
    return nullptr;
1557
12
1558
12
  Type *ITy = Op0->getType();
1559
12
  bool isNSW = IIQ.hasNoSignedWrap(AddInst);
1560
12
  bool isNUW = IIQ.hasNoUnsignedWrap(AddInst);
1561
12
1562
12
  const APInt Delta = *C1 - *C0;
1563
12
  if (C0->isStrictlyPositive()) {
1564
12
    if (Delta == 2) {
1565
6
      if (Pred0 == ICmpInst::ICMP_ULT && 
Pred1 == ICmpInst::ICMP_SGT4
)
1566
2
        return getFalse(ITy);
1567
4
      if (Pred0 == ICmpInst::ICMP_SLT && 
Pred1 == ICmpInst::ICMP_SGT2
&&
isNSW2
)
1568
2
        return getFalse(ITy);
1569
8
    }
1570
8
    if (Delta == 1) {
1571
6
      if (Pred0 == ICmpInst::ICMP_ULE && 
Pred1 == ICmpInst::ICMP_SGT4
)
1572
2
        return getFalse(ITy);
1573
4
      if (Pred0 == ICmpInst::ICMP_SLE && 
Pred1 == ICmpInst::ICMP_SGT2
&&
isNSW2
)
1574
2
        return getFalse(ITy);
1575
4
    }
1576
8
  }
1577
4
  if (C0->getBoolValue() && isNUW) {
1578
4
    if (Delta == 2)
1579
2
      if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1580
2
        return getFalse(ITy);
1581
2
    if (Delta == 1)
1582
2
      if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1583
2
        return getFalse(ITy);
1584
0
  }
1585
0
1586
0
  return nullptr;
1587
0
}
1588
1589
static Value *simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1,
1590
930k
                                 const InstrInfoQuery &IIQ) {
1591
930k
  if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true))
1592
40
    return X;
1593
930k
  if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true))
1594
6
    return X;
1595
930k
1596
930k
  if (Value *X = simplifyAndOfICmpsWithSameOperands(Op0, Op1))
1597
149
    return X;
1598
930k
  if (Value *X = simplifyAndOfICmpsWithSameOperands(Op1, Op0))
1599
141
    return X;
1600
930k
1601
930k
  if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true))
1602
1.71k
    return X;
1603
928k
1604
928k
  if (Value *X = simplifyAndOrOfICmpsWithZero(Op0, Op1, true))
1605
8
    return X;
1606
928k
1607
928k
  if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1, IIQ))
1608
12
    return X;
1609
928k
  if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0, IIQ))
1610
0
    return X;
1611
928k
1612
928k
  return nullptr;
1613
928k
}
1614
1615
static Value *simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
1616
1.08M
                                       const InstrInfoQuery &IIQ) {
1617
1.08M
  // (icmp (add V, C0), C1) | (icmp V, C0)
1618
1.08M
  ICmpInst::Predicate Pred0, Pred1;
1619
1.08M
  const APInt *C0, *C1;
1620
1.08M
  Value *V;
1621
1.08M
  if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1622
1.04M
    return nullptr;
1623
40.2k
1624
40.2k
  if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1625
37.9k
    return nullptr;
1626
2.30k
1627
2.30k
  auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1628
2.30k
  if (AddInst->getOperand(1) != Op1->getOperand(1))
1629
2.29k
    return nullptr;
1630
12
1631
12
  Type *ITy = Op0->getType();
1632
12
  bool isNSW = IIQ.hasNoSignedWrap(AddInst);
1633
12
  bool isNUW = IIQ.hasNoUnsignedWrap(AddInst);
1634
12
1635
12
  const APInt Delta = *C1 - *C0;
1636
12
  if (C0->isStrictlyPositive()) {
1637
12
    if (Delta == 2) {
1638
6
      if (Pred0 == ICmpInst::ICMP_UGE && 
Pred1 == ICmpInst::ICMP_SLE4
)
1639
2
        return getTrue(ITy);
1640
4
      if (Pred0 == ICmpInst::ICMP_SGE && 
Pred1 == ICmpInst::ICMP_SLE2
&&
isNSW2
)
1641
2
        return getTrue(ITy);
1642
8
    }
1643
8
    if (Delta == 1) {
1644
6
      if (Pred0 == ICmpInst::ICMP_UGT && 
Pred1 == ICmpInst::ICMP_SLE4
)
1645
2
        return getTrue(ITy);
1646
4
      if (Pred0 == ICmpInst::ICMP_SGT && 
Pred1 == ICmpInst::ICMP_SLE2
&&
isNSW2
)
1647
2
        return getTrue(ITy);
1648
4
    }
1649
8
  }
1650
4
  if (C0->getBoolValue() && isNUW) {
1651
4
    if (Delta == 2)
1652
2
      if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1653
2
        return getTrue(ITy);
1654
2
    if (Delta == 1)
1655
2
      if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1656
2
        return getTrue(ITy);
1657
0
  }
1658
0
1659
0
  return nullptr;
1660
0
}
1661
1662
static Value *simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1,
1663
546k
                                const InstrInfoQuery &IIQ) {
1664
546k
  if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false))
1665
66
    return X;
1666
546k
  if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false))
1667
16
    return X;
1668
546k
1669
546k
  if (Value *X = simplifyOrOfICmpsWithSameOperands(Op0, Op1))
1670
2.49k
    return X;
1671
543k
  if (Value *X = simplifyOrOfICmpsWithSameOperands(Op1, Op0))
1672
25
    return X;
1673
543k
1674
543k
  if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false))
1675
1.11k
    return X;
1676
542k
1677
542k
  if (Value *X = simplifyAndOrOfICmpsWithZero(Op0, Op1, false))
1678
11
    return X;
1679
542k
1680
542k
  if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1, IIQ))
1681
12
    return X;
1682
542k
  if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0, IIQ))
1683
0
    return X;
1684
542k
1685
542k
  return nullptr;
1686
542k
}
1687
1688
static Value *simplifyAndOrOfFCmps(const TargetLibraryInfo *TLI,
1689
51.9k
                                   FCmpInst *LHS, FCmpInst *RHS, bool IsAnd) {
1690
51.9k
  Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1691
51.9k
  Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1692
51.9k
  if (LHS0->getType() != RHS0->getType())
1693
106
    return nullptr;
1694
51.8k
1695
51.8k
  FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1696
51.8k
  if ((PredL == FCmpInst::FCMP_ORD && 
PredR == FCmpInst::FCMP_ORD85
&&
IsAnd43
) ||
1697
51.8k
      
(51.7k
PredL == FCmpInst::FCMP_UNO51.7k
&&
PredR == FCmpInst::FCMP_UNO3.51k
&&
!IsAnd3.47k
)) {
1698
79
    // (fcmp ord NNAN, X) & (fcmp ord X, Y) --> fcmp ord X, Y
1699
79
    // (fcmp ord NNAN, X) & (fcmp ord Y, X) --> fcmp ord Y, X
1700
79
    // (fcmp ord X, NNAN) & (fcmp ord X, Y) --> fcmp ord X, Y
1701
79
    // (fcmp ord X, NNAN) & (fcmp ord Y, X) --> fcmp ord Y, X
1702
79
    // (fcmp uno NNAN, X) | (fcmp uno X, Y) --> fcmp uno X, Y
1703
79
    // (fcmp uno NNAN, X) | (fcmp uno Y, X) --> fcmp uno Y, X
1704
79
    // (fcmp uno X, NNAN) | (fcmp uno X, Y) --> fcmp uno X, Y
1705
79
    // (fcmp uno X, NNAN) | (fcmp uno Y, X) --> fcmp uno Y, X
1706
79
    if ((isKnownNeverNaN(LHS0, TLI) && 
(4
LHS1 == RHS04
||
LHS1 == RHS12
)) ||
1707
79
        
(75
isKnownNeverNaN(LHS1, TLI)75
&&
(45
LHS0 == RHS045
||
LHS0 == RHS143
)))
1708
8
      return RHS;
1709
71
1710
71
    // (fcmp ord X, Y) & (fcmp ord NNAN, X) --> fcmp ord X, Y
1711
71
    // (fcmp ord Y, X) & (fcmp ord NNAN, X) --> fcmp ord Y, X
1712
71
    // (fcmp ord X, Y) & (fcmp ord X, NNAN) --> fcmp ord X, Y
1713
71
    // (fcmp ord Y, X) & (fcmp ord X, NNAN) --> fcmp ord Y, X
1714
71
    // (fcmp uno X, Y) | (fcmp uno NNAN, X) --> fcmp uno X, Y
1715
71
    // (fcmp uno Y, X) | (fcmp uno NNAN, X) --> fcmp uno Y, X
1716
71
    // (fcmp uno X, Y) | (fcmp uno X, NNAN) --> fcmp uno X, Y
1717
71
    // (fcmp uno Y, X) | (fcmp uno X, NNAN) --> fcmp uno Y, X
1718
71
    if ((isKnownNeverNaN(RHS0, TLI) && 
(4
RHS1 == LHS04
||
RHS1 == LHS12
)) ||
1719
71
        
(67
isKnownNeverNaN(RHS1, TLI)67
&&
(49
RHS0 == LHS049
||
RHS0 == LHS147
)))
1720
8
      return LHS;
1721
51.8k
  }
1722
51.8k
1723
51.8k
  return nullptr;
1724
51.8k
}
1725
1726
static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q,
1727
8.70M
                                  Value *Op0, Value *Op1, bool IsAnd) {
1728
8.70M
  // Look through casts of the 'and' operands to find compares.
1729
8.70M
  auto *Cast0 = dyn_cast<CastInst>(Op0);
1730
8.70M
  auto *Cast1 = dyn_cast<CastInst>(Op1);
1731
8.70M
  if (Cast0 && 
Cast1620k
&&
Cast0->getOpcode() == Cast1->getOpcode()19.5k
&&
1732
8.70M
      
Cast0->getSrcTy() == Cast1->getSrcTy()18.4k
) {
1733
18.2k
    Op0 = Cast0->getOperand(0);
1734
18.2k
    Op1 = Cast1->getOperand(0);
1735
18.2k
  }
1736
8.70M
1737
8.70M
  Value *V = nullptr;
1738
8.70M
  auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
1739
8.70M
  auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
1740
8.70M
  if (ICmp0 && 
ICmp11.71M
)
1741
1.47M
    V = IsAnd ? 
simplifyAndOfICmps(ICmp0, ICmp1, Q.IIQ)930k
1742
1.47M
              : 
simplifyOrOfICmps(ICmp0, ICmp1, Q.IIQ)546k
;
1743
8.70M
1744
8.70M
  auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
1745
8.70M
  auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
1746
8.70M
  if (FCmp0 && 
FCmp164.3k
)
1747
51.9k
    V = simplifyAndOrOfFCmps(Q.TLI, FCmp0, FCmp1, IsAnd);
1748
8.70M
1749
8.70M
  if (!V)
1750
8.69M
    return nullptr;
1751
5.82k
  if (!Cast0)
1752
5.80k
    return V;
1753
18
1754
18
  // If we looked through casts, we can only handle a constant simplification
1755
18
  // because we are not allowed to create a cast instruction here.
1756
18
  if (auto *C = dyn_cast<Constant>(V))
1757
8
    return ConstantExpr::getCast(Cast0->getOpcode(), C, Cast0->getType());
1758
10
1759
10
  return nullptr;
1760
10
}
1761
1762
/// Given operands for an And, see if we can fold the result.
1763
/// If not, this returns null.
1764
static Value *SimplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1765
4.84M
                              unsigned MaxRecurse) {
1766
4.84M
  if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q))
1767
179k
    return C;
1768
4.66M
1769
4.66M
  // X & undef -> 0
1770
4.66M
  if (match(Op1, m_Undef()))
1771
116
    return Constant::getNullValue(Op0->getType());
1772
4.66M
1773
4.66M
  // X & X = X
1774
4.66M
  if (Op0 == Op1)
1775
143
    return Op0;
1776
4.66M
1777
4.66M
  // X & 0 = 0
1778
4.66M
  if (match(Op1, m_Zero()))
1779
6.79k
    return Constant::getNullValue(Op0->getType());
1780
4.65M
1781
4.65M
  // X & -1 = X
1782
4.65M
  if (match(Op1, m_AllOnes()))
1783
63.2k
    return Op0;
1784
4.59M
1785
4.59M
  // A & ~A  =  ~A & A  =  0
1786
4.59M
  if (match(Op0, m_Not(m_Specific(Op1))) ||
1787
4.59M
      
match(Op1, m_Not(m_Specific(Op0)))4.59M
)
1788
145
    return Constant::getNullValue(Op0->getType());
1789
4.59M
1790
4.59M
  // (A | ?) & A = A
1791
4.59M
  if (match(Op0, m_c_Or(m_Specific(Op1), m_Value())))
1792
488
    return Op1;
1793
4.59M
1794
4.59M
  // A & (A | ?) = A
1795
4.59M
  if (match(Op1, m_c_Or(m_Specific(Op0), m_Value())))
1796
59
    return Op0;
1797
4.59M
1798
4.59M
  // A mask that only clears known zeros of a shifted value is a no-op.
1799
4.59M
  Value *X;
1800
4.59M
  const APInt *Mask;
1801
4.59M
  const APInt *ShAmt;
1802
4.59M
  if (match(Op1, m_APInt(Mask))) {
1803
2.61M
    // If all bits in the inverted and shifted mask are clear:
1804
2.61M
    // and (shl X, ShAmt), Mask --> shl X, ShAmt
1805
2.61M
    if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) &&
1806
2.61M
        
(~(*Mask)).lshr(*ShAmt).isNullValue()78.8k
)
1807
2.98k
      return Op0;
1808
2.61M
1809
2.61M
    // If all bits in the inverted and shifted mask are clear:
1810
2.61M
    // and (lshr X, ShAmt), Mask --> lshr X, ShAmt
1811
2.61M
    if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
1812
2.61M
        
(~(*Mask)).shl(*ShAmt).isNullValue()225k
)
1813
1.51k
      return Op0;
1814
4.58M
  }
1815
4.58M
1816
4.58M
  // A & (-A) = A if A is a power of two or zero.
1817
4.58M
  if (match(Op0, m_Neg(m_Specific(Op1))) ||
1818
4.58M
      
match(Op1, m_Neg(m_Specific(Op0)))4.58M
) {
1819
260
    if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
1820
260
                               Q.DT))
1821
2
      return Op0;
1822
258
    if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
1823
258
                               Q.DT))
1824
0
      return Op1;
1825
4.58M
  }
1826
4.58M
1827
4.58M
  // This is a similar pattern used for checking if a value is a power-of-2:
1828
4.58M
  // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
1829
4.58M
  // A & (A - 1) --> 0 (if A is a power-of-2 or 0)
1830
4.58M
  if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) &&
1831
4.58M
      
isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT)750
)
1832
1
    return Constant::getNullValue(Op1->getType());
1833
4.58M
  if (match(Op1, m_Add(m_Specific(Op0), m_AllOnes())) &&
1834
4.58M
      
isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT)767
)
1835
1
    return Constant::getNullValue(Op0->getType());
1836
4.58M
1837
4.58M
  if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true))
1838
2.07k
    return V;
1839
4.58M
1840
4.58M
  // Try some generic simplifications for associative operations.
1841
4.58M
  if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q,
1842
15.1k
                                          MaxRecurse))
1843
15.1k
    return V;
1844
4.57M
1845
4.57M
  // And distributes over Or.  Try some generic simplifications based on this.
1846
4.57M
  if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or,
1847
2.66k
                             Q, MaxRecurse))
1848
2.66k
    return V;
1849
4.56M
1850
4.56M
  // And distributes over Xor.  Try some generic simplifications based on this.
1851
4.56M
  if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor,
1852
161
                             Q, MaxRecurse))
1853
161
    return V;
1854
4.56M
1855
4.56M
  // If the operation is with the result of a select instruction, check whether
1856
4.56M
  // operating on either branch of the select always yields the same value.
1857
4.56M
  if (isa<SelectInst>(Op0) || 
isa<SelectInst>(Op1)4.46M
)
1858
114k
    if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q,
1859
352
                                         MaxRecurse))
1860
352
      return V;
1861
4.56M
1862
4.56M
  // If the operation is with the result of a phi instruction, check whether
1863
4.56M
  // operating on all incoming values of the phi always yields the same value.
1864
4.56M
  if (isa<PHINode>(Op0) || 
isa<PHINode>(Op1)4.29M
)
1865
308k
    if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q,
1866
49
                                      MaxRecurse))
1867
49
      return V;
1868
4.56M
1869
4.56M
  // Assuming the effective width of Y is not larger than A, i.e. all bits
1870
4.56M
  // from X and Y are disjoint in (X << A) | Y,
1871
4.56M
  // if the mask of this AND op covers all bits of X or Y, while it covers
1872
4.56M
  // no bits from the other, we can bypass this AND op. E.g.,
1873
4.56M
  // ((X << A) | Y) & Mask -> Y,
1874
4.56M
  //     if Mask = ((1 << effective_width_of(Y)) - 1)
1875
4.56M
  // ((X << A) | Y) & Mask -> X << A,
1876
4.56M
  //     if Mask = ((1 << effective_width_of(X)) - 1) << A
1877
4.56M
  // SimplifyDemandedBits in InstCombine can optimize the general case.
1878
4.56M
  // This pattern aims to help other passes for a common case.
1879
4.56M
  Value *Y, *XShifted;
1880
4.56M
  if (match(Op1, m_APInt(Mask)) &&
1881
4.56M
      match(Op0, m_c_Or(m_CombineAnd(m_NUWShl(m_Value(X), m_APInt(ShAmt)),
1882
2.59M
                                     m_Value(XShifted)),
1883
2.59M
                        m_Value(Y)))) {
1884
3.36k
    const unsigned Width = Op0->getType()->getScalarSizeInBits();
1885
3.36k
    const unsigned ShftCnt = ShAmt->getLimitedValue(Width);
1886
3.36k
    const KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1887
3.36k
    const unsigned EffWidthY = Width - YKnown.countMinLeadingZeros();
1888
3.36k
    if (EffWidthY <= ShftCnt) {
1889
1.30k
      const KnownBits XKnown = computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI,
1890
1.30k
                                                Q.DT);
1891
1.30k
      const unsigned EffWidthX = Width - XKnown.countMinLeadingZeros();
1892
1.30k
      const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY);
1893
1.30k
      const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt;
1894
1.30k
      // If the mask is extracting all bits from X or Y as is, we can skip
1895
1.30k
      // this AND op.
1896
1.30k
      if (EffBitsY.isSubsetOf(*Mask) && 
!EffBitsX.intersects(*Mask)501
)
1897
113
        return Y;
1898
1.19k
      if (EffBitsX.isSubsetOf(*Mask) && 
!EffBitsY.intersects(*Mask)325
)
1899
7
        return XShifted;
1900
4.56M
    }
1901
3.36k
  }
1902
4.56M
1903
4.56M
  return nullptr;
1904
4.56M
}
1905
1906
2.70M
Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1907
2.70M
  return ::SimplifyAndInst(Op0, Op1, Q, RecursionLimit);
1908
2.70M
}
1909
1910
/// Given operands for an Or, see if we can fold the result.
1911
/// If not, this returns null.
1912
static Value *SimplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1913
4.30M
                             unsigned MaxRecurse) {
1914
4.30M
  if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q))
1915
149k
    return C;
1916
4.16M
1917
4.16M
  // X | undef -> -1
1918
4.16M
  // X | -1 = -1
1919
4.16M
  // Do not return Op1 because it may contain undef elements if it's a vector.
1920
4.16M
  if (match(Op1, m_Undef()) || 
match(Op1, m_AllOnes())4.15M
)
1921
1.47k
    return Constant::getAllOnesValue(Op0->getType());
1922
4.15M
1923
4.15M
  // X | X = X
1924
4.15M
  // X | 0 = X
1925
4.15M
  if (Op0 == Op1 || 
match(Op1, m_Zero())4.15M
)
1926
22.3k
    return Op0;
1927
4.13M
1928
4.13M
  // A | ~A  =  ~A | A  =  -1
1929
4.13M
  if (match(Op0, m_Not(m_Specific(Op1))) ||
1930
4.13M
      
match(Op1, m_Not(m_Specific(Op0)))4.13M
)
1931
4.33k
    return Constant::getAllOnesValue(Op0->getType());
1932
4.13M
1933
4.13M
  // (A & ?) | A = A
1934
4.13M
  if (match(Op0, m_c_And(m_Specific(Op1), m_Value())))
1935
12.2k
    return Op1;
1936
4.11M
1937
4.11M
  // A | (A & ?) = A
1938
4.11M
  if (match(Op1, m_c_And(m_Specific(Op0), m_Value())))
1939
5.31k
    return Op0;
1940
4.11M
1941
4.11M
  // ~(A & ?) | A = -1
1942
4.11M
  if (match(Op0, m_Not(m_c_And(m_Specific(Op1), m_Value()))))
1943
2
    return Constant::getAllOnesValue(Op1->getType());
1944
4.11M
1945
4.11M
  // A | ~(A & ?) = -1
1946
4.11M
  if (match(Op1, m_Not(m_c_And(m_Specific(Op1), m_Value()))))
1947
0
    return Constant::getAllOnesValue(Op0->getType());
1948
4.11M
1949
4.11M
  Value *A, *B;
1950
4.11M
  // (A & ~B) | (A ^ B) -> (A ^ B)
1951
4.11M
  // (~B & A) | (A ^ B) -> (A ^ B)
1952
4.11M
  // (A & ~B) | (B ^ A) -> (B ^ A)
1953
4.11M
  // (~B & A) | (B ^ A) -> (B ^ A)
1954
4.11M
  if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
1955
4.11M
      
(47.3k
match(Op0, m_c_And(m_Specific(A), m_Not(m_Specific(B))))47.3k
||
1956
47.3k
       
match(Op0, m_c_And(m_Not(m_Specific(A)), m_Specific(B)))47.3k
))
1957
4
    return Op1;
1958
4.11M
1959
4.11M
  // Commute the 'or' operands.
1960
4.11M
  // (A ^ B) | (A & ~B) -> (A ^ B)
1961
4.11M
  // (A ^ B) | (~B & A) -> (A ^ B)
1962
4.11M
  // (B ^ A) | (A & ~B) -> (B ^ A)
1963
4.11M
  // (B ^ A) | (~B & A) -> (B ^ A)
1964
4.11M
  if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
1965
4.11M
      
(36.0k
match(Op1, m_c_And(m_Specific(A), m_Not(m_Specific(B))))36.0k
||
1966
36.0k
       
match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B)))36.0k
))
1967
4
    return Op0;
1968
4.11M
1969
4.11M
  // (A & B) | (~A ^ B) -> (~A ^ B)
1970
4.11M
  // (B & A) | (~A ^ B) -> (~A ^ B)
1971
4.11M
  // (A & B) | (B ^ ~A) -> (B ^ ~A)
1972
4.11M
  // (B & A) | (B ^ ~A) -> (B ^ ~A)
1973
4.11M
  if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1974
4.11M
      
(824k
match(Op1, m_c_Xor(m_Specific(A), m_Not(m_Specific(B))))824k
||
1975
824k
       
match(Op1, m_c_Xor(m_Not(m_Specific(A)), m_Specific(B)))824k
))
1976
4
    return Op1;
1977
4.11M
1978
4.11M
  // (~A ^ B) | (A & B) -> (~A ^ B)
1979
4.11M
  // (~A ^ B) | (B & A) -> (~A ^ B)
1980
4.11M
  // (B ^ ~A) | (A & B) -> (B ^ ~A)
1981
4.11M
  // (B ^ ~A) | (B & A) -> (B ^ ~A)
1982
4.11M
  if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
1983
4.11M
      
(513k
match(Op0, m_c_Xor(m_Specific(A), m_Not(m_Specific(B))))513k
||
1984
513k
       
match(Op0, m_c_Xor(m_Not(m_Specific(A)), m_Specific(B)))513k
))
1985
4
    return Op0;
1986
4.11M
1987
4.11M
  if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false))
1988
3.73k
    return V;
1989
4.11M
1990
4.11M
  // Try some generic simplifications for associative operations.
1991
4.11M
  if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q,
1992
2.69k
                                          MaxRecurse))
1993
2.69k
    return V;
1994
4.10M
1995
4.10M
  // Or distributes over And.  Try some generic simplifications based on this.
1996
4.10M
  if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, Q,
1997
5
                             MaxRecurse))
1998
5
    return V;
1999
4.10M
2000
4.10M
  // If the operation is with the result of a select instruction, check whether
2001
4.10M
  // operating on either branch of the select always yields the same value.
2002
4.10M
  if (isa<SelectInst>(Op0) || 
isa<SelectInst>(Op1)4.04M
)
2003
75.8k
    if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q,
2004
12
                                         MaxRecurse))
2005
12
      return V;
2006
4.10M
2007
4.10M
  // (A & C1)|(B & C2)
2008
4.10M
  const APInt *C1, *C2;
2009
4.10M
  if (match(Op0, m_And(m_Value(A), m_APInt(C1))) &&
2010
4.10M
      
match(Op1, m_And(m_Value(B), m_APInt(C2)))655k
) {
2011
108k
    if (*C1 == ~*C2) {
2012
26.5k
      // (A & C1)|(B & C2)
2013
26.5k
      // If we have: ((V + N) & C1) | (V & C2)
2014
26.5k
      // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
2015
26.5k
      // replace with V+N.
2016
26.5k
      Value *N;
2017
26.5k
      if (C2->isMask() && // C2 == 0+1+
2018
26.5k
          
match(A, m_c_Add(m_Specific(B), m_Value(N)))8.09k
) {
2019
13
        // Add commutes, try both ways.
2020
13
        if (MaskedValueIsZero(N, *C2, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2021
13
          return A;
2022
26.5k
      }
2023
26.5k
      // Or commutes, try both ways.
2024
26.5k
      if (C1->isMask() &&
2025
26.5k
          
match(B, m_c_Add(m_Specific(A), m_Value(N)))6.79k
) {
2026
5
        // Add commutes, try both ways.
2027
5
        if (MaskedValueIsZero(N, *C1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2028
5
          return B;
2029
4.10M
      }
2030
26.5k
    }
2031
108k
  }
2032
4.10M
2033
4.10M
  // If the operation is with the result of a phi instruction, check whether
2034
4.10M
  // operating on all incoming values of the phi always yields the same value.
2035
4.10M
  if (isa<PHINode>(Op0) || 
isa<PHINode>(Op1)3.90M
)
2036
374k
    if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2037
100
      return V;
2038
4.10M
2039
4.10M
  return nullptr;
2040
4.10M
}
2041
2042
1.13M
Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2043
1.13M
  return ::SimplifyOrInst(Op0, Op1, Q, RecursionLimit);
2044
1.13M
}
2045
2046
/// Given operands for a Xor, see if we can fold the result.
2047
/// If not, this returns null.
2048
static Value *SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2049
1.09M
                              unsigned MaxRecurse) {
2050
1.09M
  if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q))
2051
38.2k
    return C;
2052
1.06M
2053
1.06M
  // A ^ undef -> undef
2054
1.06M
  if (match(Op1, m_Undef()))
2055
0
    return Op1;
2056
1.06M
2057
1.06M
  // A ^ 0 = A
2058
1.06M
  if (match(Op1, m_Zero()))
2059
22.5k
    return Op0;
2060
1.03M
2061
1.03M
  // A ^ A = 0
2062
1.03M
  if (Op0 == Op1)
2063
31
    return Constant::getNullValue(Op0->getType());
2064
1.03M
2065
1.03M
  // A ^ ~A  =  ~A ^ A  =  -1
2066
1.03M
  if (match(Op0, m_Not(m_Specific(Op1))) ||
2067
1.03M
      match(Op1, m_Not(m_Specific(Op0))))
2068
5
    return Constant::getAllOnesValue(Op0->getType());
2069
1.03M
2070
1.03M
  // Try some generic simplifications for associative operations.
2071
1.03M
  if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q,
2072
22.2k
                                          MaxRecurse))
2073
22.2k
    return V;
2074
1.01M
2075
1.01M
  // Threading Xor over selects and phi nodes is pointless, so don't bother.
2076
1.01M
  // Threading over the select in "A ^ select(cond, B, C)" means evaluating
2077
1.01M
  // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
2078
1.01M
  // only if B and C are equal.  If B and C are equal then (since we assume
2079
1.01M
  // that operands have already been simplified) "select(cond, B, C)" should
2080
1.01M
  // have been simplified to the common value of B and C already.  Analysing
2081
1.01M
  // "A^B" and "A^C" thus gains nothing, but costs compile time.  Similarly
2082
1.01M
  // for threading over phi nodes.
2083
1.01M
2084
1.01M
  return nullptr;
2085
1.01M
}
2086
2087
523k
Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2088
523k
  return ::SimplifyXorInst(Op0, Op1, Q, RecursionLimit);
2089
523k
}
2090
2091
2092
141M
static Type *GetCompareTy(Value *Op) {
2093
141M
  return CmpInst::makeCmpResultType(Op->getType());
2094
141M
}
2095
2096
/// Rummage around inside V looking for something equivalent to the comparison
2097
/// "LHS Pred RHS". Return such a value if found, otherwise return null.
2098
/// Helper function for analyzing max/min idioms.
2099
static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred,
2100
60.4k
                                         Value *LHS, Value *RHS) {
2101
60.4k
  SelectInst *SI = dyn_cast<SelectInst>(V);
2102
60.4k
  if (!SI)
2103
28.5k
    return nullptr;
2104
31.9k
  CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2105
31.9k
  if (!Cmp)
2106
53
    return nullptr;
2107
31.8k
  Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2108
31.8k
  if (Pred == Cmp->getPredicate() && 
LHS == CmpLHS1.66k
&&
RHS == CmpRHS36
)
2109
36
    return Cmp;
2110
31.8k
  if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
2111
31.8k
      
LHS == CmpRHS3.35k
&&
RHS == CmpLHS3.19k
)
2112
3.19k
    return Cmp;
2113
28.6k
  return nullptr;
2114
28.6k
}
2115
2116
// A significant optimization not implemented here is assuming that alloca
2117
// addresses are not equal to incoming argument values. They don't *alias*,
2118
// as we say, but that doesn't mean they aren't equal, so we take a
2119
// conservative approach.
2120
//
2121
// This is inspired in part by C++11 5.10p1:
2122
//   "Two pointers of the same type compare equal if and only if they are both
2123
//    null, both point to the same function, or both represent the same
2124
//    address."
2125
//
2126
// This is pretty permissive.
2127
//
2128
// It's also partly due to C11 6.5.9p6:
2129
//   "Two pointers compare equal if and only if both are null pointers, both are
2130
//    pointers to the same object (including a pointer to an object and a
2131
//    subobject at its beginning) or function, both are pointers to one past the
2132
//    last element of the same array object, or one is a pointer to one past the
2133
//    end of one array object and the other is a pointer to the start of a
2134
//    different array object that happens to immediately follow the first array
2135
//    object in the address space.)
2136
//
2137
// C11's version is more restrictive, however there's no reason why an argument
2138
// couldn't be a one-past-the-end value for a stack object in the caller and be
2139
// equal to the beginning of a stack object in the callee.
2140
//
2141
// If the C and C++ standards are ever made sufficiently restrictive in this
2142
// area, it may be possible to update LLVM's semantics accordingly and reinstate
2143
// this optimization.
2144
static Constant *
2145
computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI,
2146
                   const DominatorTree *DT, CmpInst::Predicate Pred,
2147
                   AssumptionCache *AC, const Instruction *CxtI,
2148
5.73M
                   const InstrInfoQuery &IIQ, Value *LHS, Value *RHS) {
2149
5.73M
  // First, skip past any trivial no-ops.
2150
5.73M
  LHS = LHS->stripPointerCasts();
2151
5.73M
  RHS = RHS->stripPointerCasts();
2152
5.73M
2153
5.73M
  // A non-null pointer is not equal to a null pointer.
2154
5.73M
  if (llvm::isKnownNonZero(LHS, DL, 0, nullptr, nullptr, nullptr,
2155
5.73M
                           IIQ.UseInstrInfo) &&
2156
5.73M
      
isa<ConstantPointerNull>(RHS)383k
&&
2157
5.73M
      
(1
Pred == CmpInst::ICMP_EQ1
||
Pred == CmpInst::ICMP_NE0
))
2158
1
    return ConstantInt::get(GetCompareTy(LHS),
2159
1
                            !CmpInst::isTrueWhenEqual(Pred));
2160
5.73M
2161
5.73M
  // We can only fold certain predicates on pointer comparisons.
2162
5.73M
  switch (Pred) {
2163
5.73M
  default:
2164
16
    return nullptr;
2165
5.73M
2166
5.73M
    // Equality comaprisons are easy to fold.
2167
5.73M
  case CmpInst::ICMP_EQ:
2168
5.30M
  case CmpInst::ICMP_NE:
2169
5.30M
    break;
2170
5.30M
2171
5.30M
    // We can only handle unsigned relational comparisons because 'inbounds' on
2172
5.30M
    // a GEP only protects against unsigned wrapping.
2173
5.30M
  case CmpInst::ICMP_UGT:
2174
431k
  case CmpInst::ICMP_UGE:
2175
431k
  case CmpInst::ICMP_ULT:
2176
431k
  case CmpInst::ICMP_ULE:
2177
431k
    // However, we have to switch them to their signed variants to handle
2178
431k
    // negative indices from the base pointer.
2179
431k
    Pred = ICmpInst::getSignedPredicate(Pred);
2180
431k
    break;
2181
5.73M
  }
2182
5.73M
2183
5.73M
  // Strip off any constant offsets so that we can reason about them.
2184
5.73M
  // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2185
5.73M
  // here and compare base addresses like AliasAnalysis does, however there are
2186
5.73M
  // numerous hazards. AliasAnalysis and its utilities rely on special rules
2187
5.73M
  // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2188
5.73M
  // doesn't need to guarantee pointer inequality when it says NoAlias.
2189
5.73M
  Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
2190
5.73M
  Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
2191
5.73M
2192
5.73M
  // If LHS and RHS are related via constant offsets to the same base
2193
5.73M
  // value, we can replace it with an icmp which just compares the offsets.
2194
5.73M
  if (LHS == RHS)
2195
2.23k
    return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset);
2196
5.73M
2197
5.73M
  // Various optimizations for (in)equality comparisons.
2198
5.73M
  if (Pred == CmpInst::ICMP_EQ || 
Pred == CmpInst::ICMP_NE815k
) {
2199
5.30M
    // Different non-empty allocations that exist at the same time have
2200
5.30M
    // different addresses (if the program can tell). Global variables always
2201
5.30M
    // exist, so they always exist during the lifetime of each other and all
2202
5.30M
    // allocas. Two different allocas usually have different addresses...
2203
5.30M
    //
2204
5.30M
    // However, if there's an @llvm.stackrestore dynamically in between two
2205
5.30M
    // allocas, they may have the same address. It's tempting to reduce the
2206
5.30M
    // scope of the problem by only looking at *static* allocas here. That would
2207
5.30M
    // cover the majority of allocas while significantly reducing the likelihood
2208
5.30M
    // of having an @llvm.stackrestore pop up in the middle. However, it's not
2209
5.30M
    // actually impossible for an @llvm.stackrestore to pop up in the middle of
2210
5.30M
    // an entry block. Also, if we have a block that's not attached to a
2211
5.30M
    // function, we can't tell if it's "static" under the current definition.
2212
5.30M
    // Theoretically, this problem could be fixed by creating a new kind of
2213
5.30M
    // instruction kind specifically for static allocas. Such a new instruction
2214
5.30M
    // could be required to be at the top of the entry block, thus preventing it
2215
5.30M
    // from being subject to a @llvm.stackrestore. Instcombine could even
2216
5.30M
    // convert regular allocas into these special allocas. It'd be nifty.
2217
5.30M
    // However, until then, this problem remains open.
2218
5.30M
    //
2219
5.30M
    // So, we'll assume that two non-empty allocas have different addresses
2220
5.30M
    // for now.
2221
5.30M
    //
2222
5.30M
    // With all that, if the offsets are within the bounds of their allocations
2223
5.30M
    // (and not one-past-the-end! so we can't use inbounds!), and their
2224
5.30M
    // allocations aren't the same, the pointers are not equal.
2225
5.30M
    //
2226
5.30M
    // Note that it's not necessary to check for LHS being a global variable
2227
5.30M
    // address, due to canonicalization and constant folding.
2228
5.30M
    if (isa<AllocaInst>(LHS) &&
2229
5.30M
        
(3.45k
isa<AllocaInst>(RHS)3.45k
||
isa<GlobalVariable>(RHS)3.31k
)) {
2230
147
      ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset);
2231
147
      ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset);
2232
147
      uint64_t LHSSize, RHSSize;
2233
147
      ObjectSizeOpts Opts;
2234
147
      Opts.NullIsUnknownSize =
2235
147
          NullPointerIsDefined(cast<AllocaInst>(LHS)->getFunction());
2236
147
      if (LHSOffsetCI && RHSOffsetCI &&
2237
147
          getObjectSize(LHS, LHSSize, DL, TLI, Opts) &&
2238
147
          getObjectSize(RHS, RHSSize, DL, TLI, Opts)) {
2239
147
        const APInt &LHSOffsetValue = LHSOffsetCI->getValue();
2240
147
        const APInt &RHSOffsetValue = RHSOffsetCI->getValue();
2241
147
        if (!LHSOffsetValue.isNegative() &&
2242
147
            !RHSOffsetValue.isNegative() &&
2243
147
            LHSOffsetValue.ult(LHSSize) &&
2244
147
            RHSOffsetValue.ult(RHSSize)) {
2245
147
          return ConstantInt::get(GetCompareTy(LHS),
2246
147
                                  !CmpInst::isTrueWhenEqual(Pred));
2247
147
        }
2248
0
      }
2249
0
2250
0
      // Repeat the above check but this time without depending on DataLayout
2251
0
      // or being able to compute a precise size.
2252
0
      if (!cast<PointerType>(LHS->getType())->isEmptyTy() &&
2253
0
          !cast<PointerType>(RHS->getType())->isEmptyTy() &&
2254
0
          LHSOffset->isNullValue() &&
2255
0
          RHSOffset->isNullValue())
2256
0
        return ConstantInt::get(GetCompareTy(LHS),
2257
0
                                !CmpInst::isTrueWhenEqual(Pred));
2258
5.30M
    }
2259
5.30M
2260
5.30M
    // Even if an non-inbounds GEP occurs along the path we can still optimize
2261
5.30M
    // equality comparisons concerning the result. We avoid walking the whole
2262
5.30M
    // chain again by starting where the last calls to
2263
5.30M
    // stripAndComputeConstantOffsets left off and accumulate the offsets.
2264
5.30M
    Constant *LHSNoBound = stripAndComputeConstantOffsets(DL, LHS, true);
2265
5.30M
    Constant *RHSNoBound = stripAndComputeConstantOffsets(DL, RHS, true);
2266
5.30M
    if (LHS == RHS)
2267
55
      return ConstantExpr::getICmp(Pred,
2268
55
                                   ConstantExpr::getAdd(LHSOffset, LHSNoBound),
2269
55
                                   ConstantExpr::getAdd(RHSOffset, RHSNoBound));
2270
5.30M
2271
5.30M
    // If one side of the equality comparison must come from a noalias call
2272
5.30M
    // (meaning a system memory allocation function), and the other side must
2273
5.30M
    // come from a pointer that cannot overlap with dynamically-allocated
2274
5.30M
    // memory within the lifetime of the current function (allocas, byval
2275
5.30M
    // arguments, globals), then determine the comparison result here.
2276
5.30M
    SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
2277
5.30M
    GetUnderlyingObjects(LHS, LHSUObjs, DL);
2278
5.30M
    GetUnderlyingObjects(RHS, RHSUObjs, DL);
2279
5.30M
2280
5.30M
    // Is the set of underlying objects all noalias calls?
2281
10.6M
    auto IsNAC = [](ArrayRef<const Value *> Objects) {
2282
10.6M
      return all_of(Objects, isNoAliasCall);
2283
10.6M
    };
2284
5.30M
2285
5.30M
    // Is the set of underlying objects all things which must be disjoint from
2286
5.30M
    // noalias calls. For allocas, we consider only static ones (dynamic
2287
5.30M
    // allocas might be transformed into calls to malloc not simultaneously
2288
5.30M
    // live with the compared-to allocation). For globals, we exclude symbols
2289
5.30M
    // that might be resolve lazily to symbols in another dynamically-loaded
2290
5.30M
    // library (and, thus, could be malloc'ed by the implementation).
2291
5.30M
    auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
2292
160k
      return all_of(Objects, [](const Value *V) {
2293
160k
        if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2294
23
          return AI->getParent() && AI->getFunction() && AI->isStaticAlloca();
2295
160k
        if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2296
8
          return (GV->hasLocalLinkage() || 
GV->hasHiddenVisibility()7
||
2297
8
                  
GV->hasProtectedVisibility()6
||
GV->hasGlobalUnnamedAddr()5
) &&
2298
8
                 
!GV->isThreadLocal()6
;
2299
160k
        if (const Argument *A = dyn_cast<Argument>(V))
2300
19
          return A->hasByValAttr();
2301
160k
        return false;
2302
160k
      });
2303
160k
    };
2304
5.30M
2305
5.30M
    if ((IsNAC(LHSUObjs) && 
IsAllocDisjoint(RHSUObjs)154k
) ||
2306
5.30M
        
(5.30M
IsNAC(RHSUObjs)5.30M
&&
IsAllocDisjoint(LHSUObjs)5.80k
))
2307
22
        return ConstantInt::get(GetCompareTy(LHS),
2308
22
                                !CmpInst::isTrueWhenEqual(Pred));
2309
5.30M
2310
5.30M
    // Fold comparisons for non-escaping pointer even if the allocation call
2311
5.30M
    // cannot be elided. We cannot fold malloc comparison to null. Also, the
2312
5.30M
    // dynamic allocation call could be either of the operands.
2313
5.30M
    Value *MI = nullptr;
2314
5.30M
    if (isAllocLikeFn(LHS, TLI) &&
2315
5.30M
        
llvm::isKnownNonZero(RHS, DL, 0, nullptr, CxtI, DT)25.9k
)
2316
192
      MI = LHS;
2317
5.30M
    else if (isAllocLikeFn(RHS, TLI) &&
2318
5.30M
             
llvm::isKnownNonZero(LHS, DL, 0, nullptr, CxtI, DT)3.34k
)
2319
1.14k
      MI = RHS;
2320
5.30M
    // FIXME: We should also fold the compare when the pointer escapes, but the
2321
5.30M
    // compare dominates the pointer escape
2322
5.30M
    if (MI && 
!PointerMayBeCaptured(MI, true, true)1.33k
)
2323
2
      return ConstantInt::get(GetCompareTy(LHS),
2324
2
                              CmpInst::isFalseWhenEqual(Pred));
2325
5.73M
  }
2326
5.73M
2327
5.73M
  // Otherwise, fail.
2328
5.73M
  return nullptr;
2329
5.73M
}
2330
2331
/// Fold an icmp when its operands have i1 scalar type.
2332
static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS,
2333
25.7M
                                  Value *RHS, const SimplifyQuery &Q) {
2334
25.7M
  Type *ITy = GetCompareTy(LHS); // The return type.
2335
25.7M
  Type *OpTy = LHS->getType();   // The operand type.
2336
25.7M
  if (!OpTy->isIntOrIntVectorTy(1))
2337
25.6M
    return nullptr;
2338
66.4k
2339
66.4k
  // A boolean compared to true/false can be simplified in 14 out of the 20
2340
66.4k
  // (10 predicates * 2 constants) possible combinations. Cases not handled here
2341
66.4k
  // require a 'not' of the LHS, so those must be transformed in InstCombine.
2342
66.4k
  if (match(RHS, m_Zero())) {
2343
65.4k
    switch (Pred) {
2344
65.4k
    case CmpInst::ICMP_NE:  // X !=  0 -> X
2345
25.0k
    case CmpInst::ICMP_UGT: // X >u  0 -> X
2346
25.0k
    case CmpInst::ICMP_SLT: // X <s  0 -> X
2347
25.0k
      return LHS;
2348
25.0k
2349
25.0k
    case CmpInst::ICMP_ULT: // X <u  0 -> false
2350
5
    case CmpInst::ICMP_SGT: // X >s  0 -> false
2351
5
      return getFalse(ITy);
2352
5
2353
5
    case CmpInst::ICMP_UGE: // X >=u 0 -> true
2354
3
    case CmpInst::ICMP_SLE: // X <=s 0 -> true
2355
3
      return getTrue(ITy);
2356
3
2357
40.3k
    default: break;
2358
1.03k
    }
2359
1.03k
  } else if (match(RHS, m_One())) {
2360
353
    switch (Pred) {
2361
353
    case CmpInst::ICMP_EQ:  // X ==   1 -> X
2362
205
    case CmpInst::ICMP_UGE: // X >=u  1 -> X
2363
205
    case CmpInst::ICMP_SLE: // X <=s -1 -> X
2364
205
      return LHS;
2365
205
2366
205
    case CmpInst::ICMP_UGT: // X >u   1 -> false
2367
5
    case CmpInst::ICMP_SLT: // X <s  -1 -> false
2368
5
      return getFalse(ITy);
2369
5
2370
5
    case CmpInst::ICMP_ULE: // X <=u  1 -> true
2371
4
    case CmpInst::ICMP_SGE: // X >=s -1 -> true
2372
4
      return getTrue(ITy);
2373
4
2374
139
    default: break;
2375
41.1k
    }
2376
41.1k
  }
2377
41.1k
2378
41.1k
  switch (Pred) {
2379
41.1k
  default:
2380
41.0k
    break;
2381
41.1k
  case ICmpInst::ICMP_UGE:
2382
30
    if (isImpliedCondition(RHS, LHS, Q.DL).getValueOr(false))
2383
1
      return getTrue(ITy);
2384
29
    break;
2385
32
  case ICmpInst::ICMP_SGE:
2386
32
    /// For signed comparison, the values for an i1 are 0 and -1
2387
32
    /// respectively. This maps into a truth table of:
2388
32
    /// LHS | RHS | LHS >=s RHS   | LHS implies RHS
2389
32
    ///  0  |  0  |  1 (0 >= 0)   |  1
2390
32
    ///  0  |  1  |  1 (0 >= -1)  |  1
2391
32
    ///  1  |  0  |  0 (-1 >= 0)  |  0
2392
32
    ///  1  |  1  |  1 (-1 >= -1) |  1
2393
32
    if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false))
2394
1
      return getTrue(ITy);
2395
31
    break;
2396
54
  case ICmpInst::ICMP_ULE:
2397
54
    if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false))
2398
8
      return getTrue(ITy);
2399
46
    break;
2400
41.1k
  }
2401
41.1k
2402
41.1k
  return nullptr;
2403
41.1k
}
2404
2405
/// Try hard to fold icmp with zero RHS because this is a common case.
2406
static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS,
2407
25.7M
                                   Value *RHS, const SimplifyQuery &Q) {
2408
25.7M
  if (!match(RHS, m_Zero()))
2409
12.6M
    return nullptr;
2410
13.0M
2411
13.0M
  Type *ITy = GetCompareTy(LHS); // The return type.
2412
13.0M
  switch (Pred) {
2413
13.0M
  default:
2414
0
    llvm_unreachable("Unknown ICmp predicate!");
2415
13.0M
  case ICmpInst::ICMP_ULT:
2416
5.25k
    return getFalse(ITy);
2417
13.0M
  case ICmpInst::ICMP_UGE:
2418
1.68k
    return getTrue(ITy);
2419
13.0M
  case ICmpInst::ICMP_EQ:
2420
10.2M
  case ICmpInst::ICMP_ULE:
2421
10.2M
    if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo))
2422
97.1k
      return getFalse(ITy);
2423
10.1M
    break;
2424
10.1M
  case ICmpInst::ICMP_NE:
2425
1.20M
  case ICmpInst::ICMP_UGT:
2426
1.20M
    if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo))
2427
10.5k
      return getTrue(ITy);
2428
1.19M
    break;
2429
1.19M
  case ICmpInst::ICMP_SLT: {
2430
517k
    KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2431
517k
    if (LHSKnown.isNegative())
2432
4
      return getTrue(ITy);
2433
517k
    if (LHSKnown.isNonNegative())
2434
12.0k
      return getFalse(ITy);
2435
505k
    break;
2436
505k
  }
2437
505k
  case ICmpInst::ICMP_SLE: {
2438
14.1k
    KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2439
14.1k
    if (LHSKnown.isNegative())
2440
0
      return getTrue(ITy);
2441
14.1k
    if (LHSKnown.isNonNegative() &&
2442
14.1k
        
isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)326
)
2443
119
      return getFalse(ITy);
2444
14.0k
    break;
2445
14.0k
  }
2446
15.5k
  case ICmpInst::ICMP_SGE: {
2447
15.5k
    KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2448
15.5k
    if (LHSKnown.isNegative())
2449
0
      return getFalse(ITy);
2450
15.5k
    if (LHSKnown.isNonNegative())
2451
110
      return getTrue(ITy);
2452
15.3k
    break;
2453
15.3k
  }
2454
1.06M
  case ICmpInst::ICMP_SGT: {
2455
1.06M
    KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2456
1.06M
    if (LHSKnown.isNegative())
2457
32
      return getFalse(ITy);
2458
1.06M
    if (LHSKnown.isNonNegative() &&
2459
1.06M
        
isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)21.5k
)
2460
5.66k
      return getTrue(ITy);
2461
1.06M
    break;
2462
1.06M
  }
2463
12.9M
  }
2464
12.9M
2465
12.9M
  return nullptr;
2466
12.9M
}
2467
2468
static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
2469
25.5M
                                       Value *RHS, const InstrInfoQuery &IIQ) {
2470
25.5M
  Type *ITy = GetCompareTy(RHS); // The return type.
2471
25.5M
2472
25.5M
  Value *X;
2473
25.5M
  // Sign-bit checks can be optimized to true/false after unsigned
2474
25.5M
  // floating-point casts:
2475
25.5M
  // icmp slt (bitcast (uitofp X)),  0 --> false
2476
25.5M
  // icmp sgt (bitcast (uitofp X)), -1 --> true
2477
25.5M
  if (match(LHS, m_BitCast(m_UIToFP(m_Value(X))))) {
2478
39
    if (Pred == ICmpInst::ICMP_SLT && 
match(RHS, m_Zero())9
)
2479
9
      return ConstantInt::getFalse(ITy);
2480
30
    if (Pred == ICmpInst::ICMP_SGT && 
match(RHS, m_AllOnes())9
)
2481
9
      return ConstantInt::getTrue(ITy);
2482
25.5M
  }
2483
25.5M
2484
25.5M
  const APInt *C;
2485
25.5M
  if (!match(RHS, m_APInt(C)))
2486
11.1M
    return nullptr;
2487
14.4M
2488
14.4M
  // Rule out tautological comparisons (eg., ult 0 or uge 0).
2489
14.4M
  ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
2490
14.4M
  if (RHS_CR.isEmptySet())
2491
2.28k
    return ConstantInt::getFalse(ITy);
2492
14.4M
  if (RHS_CR.isFullSet())
2493
5.61k
    return ConstantInt::getTrue(ITy);
2494
14.4M
2495
14.4M
  ConstantRange LHS_CR = computeConstantRange(LHS, IIQ.UseInstrInfo);
2496
14.4M
  if (!LHS_CR.isFullSet()) {
2497
2.98M
    if (RHS_CR.contains(LHS_CR))
2498
7.24k
      return ConstantInt::getTrue(ITy);
2499
2.98M
    if (RHS_CR.inverse().contains(LHS_CR))
2500
15.7k
      return ConstantInt::getFalse(ITy);
2501
14.4M
  }
2502
14.4M
2503
14.4M
  return nullptr;
2504
14.4M
}
2505
2506
/// TODO: A large part of this logic is duplicated in InstCombine's
2507
/// foldICmpBinOp(). We should be able to share that and avoid the code
2508
/// duplication.
2509
static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS,
2510
                                    Value *RHS, const SimplifyQuery &Q,
2511
25.5M
                                    unsigned MaxRecurse) {
2512
25.5M
  Type *ITy = GetCompareTy(LHS); // The return type.
2513
25.5M
2514
25.5M
  BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
2515
25.5M
  BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
2516
25.5M
  if (MaxRecurse && 
(25.3M
LBO25.3M
||
RBO19.1M
)) {
2517
6.76M
    // Analyze the case when either LHS or RHS is an add instruction.
2518
6.76M
    Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
2519
6.76M
    // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
2520
6.76M
    bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
2521
6.76M
    if (LBO && 
LBO->getOpcode() == Instruction::Add6.12M
) {
2522
3.02M
      A = LBO->getOperand(0);
2523
3.02M
      B = LBO->getOperand(1);
2524
3.02M
      NoLHSWrapProblem =
2525
3.02M
          ICmpInst::isEquality(Pred) ||
2526
3.02M
          
(1.81M
CmpInst::isUnsigned(Pred)1.81M
&&
2527
1.81M
           
Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))869k
) ||
2528
3.02M
          
(1.38M
CmpInst::isSigned(Pred)1.38M
&&
2529
1.38M
           
Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO))949k
);
2530
3.02M
    }
2531
6.76M
    if (RBO && 
RBO->getOpcode() == Instruction::Add1.24M
) {
2532
636k
      C = RBO->getOperand(0);
2533
636k
      D = RBO->getOperand(1);
2534
636k
      NoRHSWrapProblem =
2535
636k
          ICmpInst::isEquality(Pred) ||
2536
636k
          
(460k
CmpInst::isUnsigned(Pred)460k
&&
2537
460k
           
Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(RBO))308k
) ||
2538
636k
          
(294k
CmpInst::isSigned(Pred)294k
&&
2539
294k
           
Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RBO))152k
);
2540
636k
    }
2541
6.76M
2542
6.76M
    // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
2543
6.76M
    if ((A == RHS || 
B == RHS6.76M
) &&
NoLHSWrapProblem32.8k
)
2544
21.4k
      if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A,
2545
9.53k
                                      Constant::getNullValue(RHS->getType()), Q,
2546
9.53k
                                      MaxRecurse - 1))
2547
9.53k
        return V;
2548
6.75M
2549
6.75M
    // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
2550
6.75M
    if ((C == LHS || 
D == LHS6.74M
) &&
NoRHSWrapProblem11.7k
)
2551
7.24k
      if (Value *V =
2552
1.11k
              SimplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()),
2553
1.11k
                               C == LHS ? D : C, Q, MaxRecurse - 1))
2554
1.11k
        return V;
2555
6.75M
2556
6.75M
    // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
2557
6.75M
    if (A && 
C3.01M
&&
(93.0k
A == C93.0k
||
A == D90.6k
||
B == C90.2k
||
B == D90.0k
) &&
NoLHSWrapProblem8.61k
&&
2558
6.75M
        
NoRHSWrapProblem5.36k
) {
2559
5.17k
      // Determine Y and Z in the form icmp (X+Y), (X+Z).
2560
5.17k
      Value *Y, *Z;
2561
5.17k
      if (A == C) {
2562
159
        // C + B == C + D  ->  B == D
2563
159
        Y = B;
2564
159
        Z = D;
2565
5.01k
      } else if (A == D) {
2566
140
        // D + B == C + D  ->  B == C
2567
140
        Y = B;
2568
140
        Z = C;
2569
4.87k
      } else if (B == C) {
2570
57
        // A + C == C + D  ->  A == D
2571
57
        Y = A;
2572
57
        Z = D;
2573
4.81k
      } else {
2574
4.81k
        assert(B == D);
2575
4.81k
        // A + D == C + D  ->  A == C
2576
4.81k
        Y = A;
2577
4.81k
        Z = C;
2578
4.81k
      }
2579
5.17k
      if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
2580
121
        return V;
2581
25.5M
    }
2582
6.75M
  }
2583
25.5M
2584
25.5M
  {
2585
25.5M
    Value *Y = nullptr;
2586
25.5M
    // icmp pred (or X, Y), X
2587
25.5M
    if (LBO && 
match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))6.15M
) {
2588
9.36k
      if (Pred == ICmpInst::ICMP_ULT)
2589
1
        return getFalse(ITy);
2590
9.36k
      if (Pred == ICmpInst::ICMP_UGE)
2591
1
        return getTrue(ITy);
2592
9.36k
2593
9.36k
      if (Pred == ICmpInst::ICMP_SLT || 
Pred == ICmpInst::ICMP_SGE9.30k
) {
2594
112
        KnownBits RHSKnown = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2595
112
        KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2596
112
        if (RHSKnown.isNonNegative() && 
YKnown.isNegative()50
)
2597
2
          return Pred == ICmpInst::ICMP_SLT ? 
getTrue(ITy)1
:
getFalse(ITy)1
;
2598
110
        if (RHSKnown.isNegative() || 
YKnown.isNonNegative()104
)
2599
12
          return Pred == ICmpInst::ICMP_SLT ? 
getFalse(ITy)7
:
getTrue(ITy)5
;
2600
25.5M
      }
2601
9.36k
    }
2602
25.5M
    // icmp pred X, (or X, Y)
2603
25.5M
    if (RBO && 
match(RBO, m_c_Or(m_Value(Y), m_Specific(LHS)))1.24M
) {
2604
152
      if (Pred == ICmpInst::ICMP_ULE)
2605
1
        return getTrue(ITy);
2606
151
      if (Pred == ICmpInst::ICMP_UGT)
2607
1
        return getFalse(ITy);
2608
150
2609
150
      if (Pred == ICmpInst::ICMP_SGT || 
Pred == ICmpInst::ICMP_SLE97
) {
2610
106
        KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2611
106
        KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2612
106
        if (LHSKnown.isNonNegative() && 
YKnown.isNegative()46
)
2613
2
          return Pred == ICmpInst::ICMP_SGT ? 
getTrue(ITy)1
:
getFalse(ITy)1
;
2614
104
        if (LHSKnown.isNegative() || 
YKnown.isNonNegative()98
)
2615
10
          return Pred == ICmpInst::ICMP_SGT ? 
getFalse(ITy)5
:
getTrue(ITy)5
;
2616
25.5M
      }
2617
150
    }
2618
25.5M
  }
2619
25.5M
2620
25.5M
  // icmp pred (and X, Y), X
2621
25.5M
  if (LBO && 
match(LBO, m_c_And(m_Value(), m_Specific(RHS)))6.15M
) {
2622
106k
    if (Pred == ICmpInst::ICMP_UGT)
2623
44
      return getFalse(ITy);
2624
106k
    if (Pred == ICmpInst::ICMP_ULE)
2625
4
      return getTrue(ITy);
2626
25.5M
  }
2627
25.5M
  // icmp pred X, (and X, Y)
2628
25.5M
  if (RBO && 
match(RBO, m_c_And(m_Value(), m_Specific(LHS)))1.24M
) {
2629
74.1k
    if (Pred == ICmpInst::ICMP_UGE)
2630
4
      return getTrue(ITy);
2631
74.1k
    if (Pred == ICmpInst::ICMP_ULT)
2632
4
      return getFalse(ITy);
2633
25.5M
  }
2634
25.5M
2635
25.5M
  // 0 - (zext X) pred C
2636
25.5M
  if (!CmpInst::isUnsigned(Pred) && 
match(LHS, m_Neg(m_ZExt(m_Value())))21.1M
) {
2637
25
    if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
2638
17
      if (RHSC->getValue().isStrictlyPositive()) {
2639
10
        if (Pred == ICmpInst::ICMP_SLT)
2640
6
          return ConstantInt::getTrue(RHSC->getContext());
2641
4
        if (Pred == ICmpInst::ICMP_SGE)
2642
1
          return ConstantInt::getFalse(RHSC->getContext());
2643
3
        if (Pred == ICmpInst::ICMP_EQ)
2644
2
          return ConstantInt::getFalse(RHSC->getContext());
2645
1
        if (Pred == ICmpInst::ICMP_NE)
2646
1
          return ConstantInt::getTrue(RHSC->getContext());
2647
7
      }
2648
7
      if (RHSC->getValue().isNonNegative()) {
2649
6
        if (Pred == ICmpInst::ICMP_SLE)
2650
5
          return ConstantInt::getTrue(RHSC->getContext());
2651
1
        if (Pred == ICmpInst::ICMP_SGT)
2652
1
          return ConstantInt::getFalse(RHSC->getContext());
2653
25.5M
      }
2654
7
    }
2655
25
  }
2656
25.5M
2657
25.5M
  // icmp pred (urem X, Y), Y
2658
25.5M
  if (LBO && 
match(LBO, m_URem(m_Value(), m_Specific(RHS)))6.14M
) {
2659
120
    switch (Pred) {
2660
120
    default:
2661
0
      break;
2662
120
    case ICmpInst::ICMP_SGT:
2663
1
    case ICmpInst::ICMP_SGE: {
2664
1
      KnownBits Known = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2665
1
      if (!Known.isNonNegative())
2666
1
        break;
2667
0
      LLVM_FALLTHROUGH;
2668
0
    }
2669
37
    case ICmpInst::ICMP_EQ:
2670
37
    case ICmpInst::ICMP_UGT:
2671
37
    case ICmpInst::ICMP_UGE:
2672
37
      return getFalse(ITy);
2673
37
    case ICmpInst::ICMP_SLT:
2674
1
    case ICmpInst::ICMP_SLE: {
2675
1
      KnownBits Known = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2676
1
      if (!Known.isNonNegative())
2677
1
        break;
2678
0
      LLVM_FALLTHROUGH;
2679
0
    }
2680
81
    case ICmpInst::ICMP_NE:
2681
81
    case ICmpInst::ICMP_ULT:
2682
81
    case ICmpInst::ICMP_ULE:
2683
81
      return getTrue(ITy);
2684
25.5M
    }
2685
25.5M
  }
2686
25.5M
2687
25.5M
  // icmp pred X, (urem Y, X)
2688
25.5M
  if (RBO && 
match(RBO, m_URem(m_Value(), m_Specific(LHS)))1.24M
) {
2689
643
    switch (Pred) {
2690
643
    default:
2691
0
      break;
2692
643
    case ICmpInst::ICMP_SGT:
2693
0
    case ICmpInst::ICMP_SGE: {
2694
0
      KnownBits Known = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2695
0
      if (!Known.isNonNegative())
2696
0
        break;
2697
0
      LLVM_FALLTHROUGH;
2698
0
    }
2699
1
    case ICmpInst::ICMP_NE:
2700
1
    case ICmpInst::ICMP_UGT:
2701
1
    case ICmpInst::ICMP_UGE:
2702
1
      return getTrue(ITy);
2703
1
    case ICmpInst::ICMP_SLT:
2704
0
    case ICmpInst::ICMP_SLE: {
2705
0
      KnownBits Known = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2706
0
      if (!Known.isNonNegative())
2707
0
        break;
2708
0
      LLVM_FALLTHROUGH;
2709
0
    }
2710
642
    case ICmpInst::ICMP_EQ:
2711
642
    case ICmpInst::ICMP_ULT:
2712
642
    case ICmpInst::ICMP_ULE:
2713
642
      return getFalse(ITy);
2714
25.5M
    }
2715
25.5M
  }
2716
25.5M
2717
25.5M
  // x >> y <=u x
2718
25.5M
  // x udiv y <=u x.
2719
25.5M
  if (LBO && 
(6.14M
match(LBO, m_LShr(m_Specific(RHS), m_Value()))6.14M
||
2720
6.14M
              
match(LBO, m_UDiv(m_Specific(RHS), m_Value()))6.14M
)) {
2721
68
    // icmp pred (X op Y), X
2722
68
    if (Pred == ICmpInst::ICMP_UGT)
2723
2
      return getFalse(ITy);
2724
66
    if (Pred == ICmpInst::ICMP_ULE)
2725
2
      return getTrue(ITy);
2726
25.5M
  }
2727
25.5M
2728
25.5M
  // x >=u x >> y
2729
25.5M
  // x >=u x udiv y.
2730
25.5M
  if (RBO && 
(1.24M
match(RBO, m_LShr(m_Specific(LHS), m_Value()))1.24M
||
2731
1.24M
              
match(RBO, m_UDiv(m_Specific(LHS), m_Value()))1.24M
)) {
2732
18
    // icmp pred X, (X op Y)
2733
18
    if (Pred == ICmpInst::ICMP_ULT)
2734
7
      return getFalse(ITy);
2735
11
    if (Pred == ICmpInst::ICMP_UGE)
2736
2
      return getTrue(ITy);
2737
25.5M
  }
2738
25.5M
2739
25.5M
  // handle:
2740
25.5M
  //   CI2 << X == CI
2741
25.5M
  //   CI2 << X != CI
2742
25.5M
  //
2743
25.5M
  //   where CI2 is a power of 2 and CI isn't
2744
25.5M
  if (auto *CI = dyn_cast<ConstantInt>(RHS)) {
2745
14.3M
    const APInt *CI2Val, *CIVal = &CI->getValue();
2746
14.3M
    if (LBO && 
match(LBO, m_Shl(m_APInt(CI2Val), m_Value()))3.67M
&&
2747
14.3M
        
CI2Val->isPowerOf2()808
) {
2748
779
      if (!CIVal->isPowerOf2()) {
2749
554
        // CI2 << X can equal zero in some circumstances,
2750
554
        // this simplification is unsafe if CI is zero.
2751
554
        //
2752
554
        // We know it is safe if:
2753
554
        // - The shift is nsw, we can't shift out the one bit.
2754
554
        // - The shift is nuw, we can't shift out the one bit.
2755
554
        // - CI2 is one
2756
554
        // - CI isn't zero
2757
554
        if (Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
2758
554
            Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
2759
554
            
CI2Val->isOneValue()552
||
!CI->isZero()128
) {
2760
429
          if (Pred == ICmpInst::ICMP_EQ)
2761
2
            return ConstantInt::getFalse(RHS->getContext());
2762
427
          if (Pred == ICmpInst::ICMP_NE)
2763
1
            return ConstantInt::getTrue(RHS->getContext());
2764
776
        }
2765
554
      }
2766
776
      if (CIVal->isSignMask() && 
CI2Val->isOneValue()5
) {
2767
5
        if (Pred == ICmpInst::ICMP_UGT)
2768
2
          return ConstantInt::getFalse(RHS->getContext());
2769
3
        if (Pred == ICmpInst::ICMP_ULE)
2770
1
          return ConstantInt::getTrue(RHS->getContext());
2771
25.5M
      }
2772
776
    }
2773
14.3M
  }
2774
25.5M
2775
25.5M
  if (MaxRecurse && 
LBO25.3M
&&
RBO6.11M
&&
LBO->getOpcode() == RBO->getOpcode()604k
&&
2776
25.5M
      
LBO->getOperand(1) == RBO->getOperand(1)204k
) {
2777
19.6k
    switch (LBO->getOpcode()) {
2778
19.6k
    default:
2779
10.9k
      break;
2780
19.6k
    case Instruction::UDiv:
2781
1.29k
    case Instruction::LShr:
2782
1.29k
      if (ICmpInst::isSigned(Pred) || 
!Q.IIQ.isExact(LBO)1.28k
||
2783
1.29k
          
!Q.IIQ.isExact(RBO)1
)
2784
1.29k
        break;
2785
1
      if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
2786
1
                                      RBO->getOperand(0), Q, MaxRecurse - 1))
2787
1
          return V;
2788
0
      break;
2789
2.71k
    case Instruction::SDiv:
2790
2.71k
      if (!ICmpInst::isEquality(Pred) || 
!Q.IIQ.isExact(LBO)5
||
2791
2.71k
          
!Q.IIQ.isExact(RBO)5
)
2792
2.71k
        break;
2793
4
      if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
2794
1
                                      RBO->getOperand(0), Q, MaxRecurse - 1))
2795
1
        return V;
2796
3
      break;
2797
4.25k
    case Instruction::AShr:
2798
4.25k
      if (!Q.IIQ.isExact(LBO) || 
!Q.IIQ.isExact(RBO)2.67k
)
2799
1.58k
        break;
2800
2.67k
      if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
2801
0
                                      RBO->getOperand(0), Q, MaxRecurse - 1))
2802
0
        return V;
2803
2.67k
      break;
2804
2.67k
    case Instruction::Shl: {
2805
444
      bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && 
Q.IIQ.hasNoUnsignedWrap(RBO)17
;
2806
444
      bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && 
Q.IIQ.hasNoSignedWrap(RBO)85
;
2807
444
      if (!NUW && !NSW)
2808
363
        break;
2809
81
      if (!NSW && 
ICmpInst::isSigned(Pred)0
)
2810
0
        break;
2811
81
      if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
2812
0
                                      RBO->getOperand(0), Q, MaxRecurse - 1))
2813
0
        return V;
2814
81
      break;
2815
81
    }
2816
19.6k
    }
2817
19.6k
  }
2818
25.5M
  return nullptr;
2819
25.5M
}
2820
2821
/// Simplify integer comparisons where at least one operand of the compare
2822
/// matches an integer min/max idiom.
2823
static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS,
2824
                                     Value *RHS, const SimplifyQuery &Q,
2825
25.5M
                                     unsigned MaxRecurse) {
2826
25.5M
  Type *ITy = GetCompareTy(LHS); // The return type.
2827
25.5M
  Value *A, *B;
2828
25.5M
  CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
2829
25.5M
  CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
2830
25.5M
2831
25.5M
  // Signed variants on "max(a,b)>=a -> true".
2832
25.5M
  if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && 
(226k
A == RHS226k
||
B == RHS226k
)) {
2833
57.7k
    if (A != RHS)
2834
57.6k
      std::swap(A, B);       // smax(A, B) pred A.
2835
57.7k
    EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
2836
57.7k
    // We analyze this as smax(A, B) pred A.
2837
57.7k
    P = Pred;
2838
25.4M
  } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
2839
25.4M
             
(114k
A == LHS114k
||
B == LHS114k
)) {
2840
248
    if (A != LHS)
2841
151
      std::swap(A, B);       // A pred smax(A, B).
2842
248
    EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
2843
248
    // We analyze this as smax(A, B) swapped-pred A.
2844
248
    P = CmpInst::getSwappedPredicate(Pred);
2845
25.4M
  } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
2846
25.4M
             
(11.6k
A == RHS11.6k
||
B == RHS11.5k
)) {
2847
271
    if (A != RHS)
2848
178
      std::swap(A, B);       // smin(A, B) pred A.
2849
271
    EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
2850
271
    // We analyze this as smax(-A, -B) swapped-pred -A.
2851
271
    // Note that we do not need to actually form -A or -B thanks to EqP.
2852
271
    P = CmpInst::getSwappedPredicate(Pred);
2853
25.4M
  } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
2854
25.4M
             
(6.00k
A == LHS6.00k
||
B == LHS5.93k
)) {
2855
127
    if (A != LHS)
2856
60
      std::swap(A, B);       // A pred smin(A, B).
2857
127
    EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
2858
127
    // We analyze this as smax(-A, -B) pred -A.
2859
127
    // Note that we do not need to actually form -A or -B thanks to EqP.
2860
127
    P = Pred;
2861
127
  }
2862
25.5M
  if (P != CmpInst::BAD_ICMP_PREDICATE) {
2863
58.3k
    // Cases correspond to "max(A, B) p A".
2864
58.3k
    switch (P) {
2865
58.3k
    default:
2866
26.5k
      break;
2867
58.3k
    case CmpInst::ICMP_EQ:
2868
26.7k
    case CmpInst::ICMP_SLE:
2869
26.7k
      // Equivalent to "A EqP B".  This may be the same as the condition tested
2870
26.7k
      // in the max/min; if so, we can just return that.
2871
26.7k
      if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
2872
2
        return V;
2873
26.7k
      if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
2874
1
        return V;
2875
26.7k
      // Otherwise, see if "A EqP B" simplifies.
2876
26.7k
      if (MaxRecurse)
2877
26.7k
        if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
2878
0
          return V;
2879
26.7k
      break;
2880
26.7k
    case CmpInst::ICMP_NE:
2881
4.87k
    case CmpInst::ICMP_SGT: {
2882
4.87k
      CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
2883
4.87k
      // Equivalent to "A InvEqP B".  This may be the same as the condition
2884
4.87k
      // tested in the max/min; if so, we can just return that.
2885
4.87k
      if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
2886
3.15k
        return V;
2887
1.72k
      if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
2888
35
        return V;
2889
1.68k
      // Otherwise, see if "A InvEqP B" simplifies.
2890
1.68k
      if (MaxRecurse)
2891
1.68k
        if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
2892
0
          return V;
2893
1.68k
      break;
2894
1.68k
    }
2895
1.68k
    case CmpInst::ICMP_SGE:
2896
8
      // Always true.
2897
8
      return getTrue(ITy);
2898
1.68k
    case CmpInst::ICMP_SLT:
2899
190
      // Always false.
2900
190
      return getFalse(ITy);
2901
25.4M
    }
2902
25.4M
  }
2903
25.4M
2904
25.4M
  // Unsigned variants on "max(a,b)>=a -> true".
2905
25.4M
  P = CmpInst::BAD_ICMP_PREDICATE;
2906
25.4M
  if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && 
(96.4k
A == RHS96.4k
||
B == RHS96.3k
)) {
2907
146
    if (A != RHS)
2908
33
      std::swap(A, B);       // umax(A, B) pred A.
2909
146
    EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
2910
146
    // We analyze this as umax(A, B) pred A.
2911
146
    P = Pred;
2912
25.4M
  } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
2913
25.4M
             
(56.6k
A == LHS56.6k
||
B == LHS56.5k
)) {
2914
69
    if (A != LHS)
2915
27
      std::swap(A, B);       // A pred umax(A, B).
2916
69
    EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
2917
69
    // We analyze this as umax(A, B) swapped-pred A.
2918
69
    P = CmpInst::getSwappedPredicate(Pred);
2919
25.4M
  } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
2920
25.4M
             
(22.7k
A == RHS22.7k
||
B == RHS22.6k
)) {
2921
98
    if (A != RHS)
2922
61
      std::swap(A, B);       // umin(A, B) pred A.
2923
98
    EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
2924
98
    // We analyze this as umax(-A, -B) swapped-pred -A.
2925
98
    // Note that we do not need to actually form -A or -B thanks to EqP.
2926
98
    P = CmpInst::getSwappedPredicate(Pred);
2927
25.4M
  } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
2928
25.4M
             
(8.71k
A == LHS8.71k
||
B == LHS8.66k
)) {
2929
67
    if (A != LHS)
2930
21
      std::swap(A, B);       // A pred umin(A, B).
2931
67
    EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
2932
67
    // We analyze this as umax(-A, -B) pred -A.
2933
67
    // Note that we do not need to actually form -A or -B thanks to EqP.
2934
67
    P = Pred;
2935
67
  }
2936
25.4M
  if (P != CmpInst::BAD_ICMP_PREDICATE) {
2937
380
    // Cases correspond to "max(A, B) p A".
2938
380
    switch (P) {
2939
380
    default:
2940
30
      break;
2941
380
    case CmpInst::ICMP_EQ:
2942
25
    case CmpInst::ICMP_ULE:
2943
25
      // Equivalent to "A EqP B".  This may be the same as the condition tested
2944
25
      // in the max/min; if so, we can just return that.
2945
25
      if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
2946
1
        return V;
2947
24
      if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
2948
1
        return V;
2949
23
      // Otherwise, see if "A EqP B" simplifies.
2950
23
      if (MaxRecurse)
2951
23
        if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
2952
0
          return V;
2953
23
      break;
2954
141
    case CmpInst::ICMP_NE:
2955
141
    case CmpInst::ICMP_UGT: {
2956
141
      CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
2957
141
      // Equivalent to "A InvEqP B".  This may be the same as the condition
2958
141
      // tested in the max/min; if so, we can just return that.
2959
141
      if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
2960
7
        return V;
2961
134
      if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
2962
31
        return V;
2963
103
      // Otherwise, see if "A InvEqP B" simplifies.
2964
103
      if (MaxRecurse)
2965
103
        if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
2966
0
          return V;
2967
103
      break;
2968
103
    }
2969
103
    case CmpInst::ICMP_UGE:
2970
8
      // Always true.
2971
8
      return getTrue(ITy);
2972
176
    case CmpInst::ICMP_ULT:
2973
176
      // Always false.
2974
176
      return getFalse(ITy);
2975
25.4M
    }
2976
25.4M
  }
2977
25.4M
2978
25.4M
  // Variants on "max(x,y) >= min(x,z)".
2979
25.4M
  Value *C, *D;
2980
25.4M
  if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
2981
25.4M
      
match(RHS, m_SMin(m_Value(C), m_Value(D)))223k
&&
2982
25.4M
      
(691
A == C691
||
A == D638
||
B == C638
||
B == D638
)) {
2983
53
    // max(x, ?) pred min(x, ?).
2984
53
    if (Pred == CmpInst::ICMP_SGE)
2985
1
      // Always true.
2986
1
      return getTrue(ITy);
2987
52
    if (Pred == CmpInst::ICMP_SLT)
2988
1
      // Always false.
2989
1
      return getFalse(ITy);
2990
25.4M
  } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
2991
25.4M
             
match(RHS, m_SMax(m_Value(C), m_Value(D)))11.5k
&&
2992
25.4M
             
(505
A == C505
||
A == D299
||
B == C299
||
B == D299
)) {
2993
206
    // min(x, ?) pred max(x, ?).
2994
206
    if (Pred == CmpInst::ICMP_SLE)
2995
1
      // Always true.
2996
1
      return getTrue(ITy);
2997
205
    if (Pred == CmpInst::ICMP_SGT)
2998
5
      // Always false.
2999
5
      return getFalse(ITy);
3000
25.4M
  } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
3001
25.4M
             
match(RHS, m_UMin(m_Value(C), m_Value(D)))96.3k
&&
3002
25.4M
             
(988
A == C988
||
A == D950
||
B == C950
||
B == D950
)) {
3003
38
    // max(x, ?) pred min(x, ?).
3004
38
    if (Pred == CmpInst::ICMP_UGE)
3005
1
      // Always true.
3006
1
      return getTrue(ITy);
3007
37
    if (Pred == CmpInst::ICMP_ULT)
3008
1
      // Always false.
3009
1
      return getFalse(ITy);
3010
25.4M
  } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
3011
25.4M
             
match(RHS, m_UMax(m_Value(C), m_Value(D)))22.6k
&&
3012
25.4M
             
(31
A == C31
||
A == D0
||
B == C0
||
B == D0
)) {
3013
31
    // min(x, ?) pred max(x, ?).
3014
31
    if (Pred == CmpInst::ICMP_ULE)
3015
1
      // Always true.
3016
1
      return getTrue(ITy);
3017
30
    if (Pred == CmpInst::ICMP_UGT)
3018
3
      // Always false.
3019
3
      return getFalse(ITy);
3020
25.4M
  }
3021
25.4M
3022
25.4M
  return nullptr;
3023
25.4M
}
3024
3025
/// Given operands for an ICmpInst, see if we can fold the result.
3026
/// If not, this returns null.
3027
static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3028
27.1M
                               const SimplifyQuery &Q, unsigned MaxRecurse) {
3029
27.1M
  CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
3030
27.1M
  assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
3031
27.1M
3032
27.1M
  if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3033
1.60M
    if (Constant *CRHS = dyn_cast<Constant>(RHS))
3034
1.36M
      return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3035
245k
3036
245k
    // If we have a constant, make sure it is on the RHS.
3037
245k
    std::swap(LHS, RHS);
3038
245k
    Pred = CmpInst::getSwappedPredicate(Pred);
3039
245k
  }
3040
27.1M
  assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X");
3041
25.8M
3042
25.8M
  Type *ITy = GetCompareTy(LHS); // The return type.
3043
25.8M
3044
25.8M
  // For EQ and NE, we can always pick a value for the undef to make the
3045
25.8M
  // predicate pass or fail, so we can return undef.
3046
25.8M
  // Matches behavior in llvm::ConstantFoldCompareInstruction.
3047
25.8M
  if (isa<UndefValue>(RHS) && 
ICmpInst::isEquality(Pred)761
)
3048
183
    return UndefValue::get(ITy);
3049
25.8M
3050
25.8M
  // icmp X, X -> true/false
3051
25.8M
  // icmp X, undef -> true/false because undef could be X.
3052
25.8M
  if (LHS == RHS || 
isa<UndefValue>(RHS)25.7M
)
3053
42.6k
    return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
3054
25.7M
3055
25.7M
  if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3056
25.3k
    return V;
3057
25.7M
3058
25.7M
  if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3059
132k
    return V;
3060
25.5M
3061
25.5M
  if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
3062
30.8k
    return V;
3063
25.5M
3064
25.5M
  // If both operands have range metadata, use the metadata
3065
25.5M
  // to simplify the comparison.
3066
25.5M
  if (isa<Instruction>(RHS) && 
isa<Instruction>(LHS)5.56M
) {
3067
5.53M
    auto RHS_Instr = cast<Instruction>(RHS);
3068
5.53M
    auto LHS_Instr = cast<Instruction>(LHS);
3069
5.53M
3070
5.53M
    if (Q.IIQ.getMetadata(RHS_Instr, LLVMContext::MD_range) &&
3071
5.53M
        
Q.IIQ.getMetadata(LHS_Instr, LLVMContext::MD_range)642
) {
3072
528
      auto RHS_CR = getConstantRangeFromMetadata(
3073
528
          *RHS_Instr->getMetadata(LLVMContext::MD_range));
3074
528
      auto LHS_CR = getConstantRangeFromMetadata(
3075
528
          *LHS_Instr->getMetadata(LLVMContext::MD_range));
3076
528
3077
528
      auto Satisfied_CR = ConstantRange::makeSatisfyingICmpRegion(Pred, RHS_CR);
3078
528
      if (Satisfied_CR.contains(LHS_CR))
3079
1
        return ConstantInt::getTrue(RHS->getContext());
3080
527
3081
527
      auto InversedSatisfied_CR = ConstantRange::makeSatisfyingICmpRegion(
3082
527
                CmpInst::getInversePredicate(Pred), RHS_CR);
3083
527
      if (InversedSatisfied_CR.contains(LHS_CR))
3084
1
        return ConstantInt::getFalse(RHS->getContext());
3085
25.5M
    }
3086
5.53M
  }
3087
25.5M
3088
25.5M
  // Compare of cast, for example (zext X) != 0 -> X != 0
3089
25.5M
  if (isa<CastInst>(LHS) && 
(1.00M
isa<Constant>(RHS)1.00M
||
isa<CastInst>(RHS)303k
)) {
3090
832k
    Instruction *LI = cast<CastInst>(LHS);
3091
832k
    Value *SrcOp = LI->getOperand(0);
3092
832k
    Type *SrcTy = SrcOp->getType();
3093
832k
    Type *DstTy = LI->getType();
3094
832k
3095
832k
    // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
3096
832k
    // if the integer type is the same size as the pointer type.
3097
832k
    if (MaxRecurse && 
isa<PtrToIntInst>(LI)828k
&&
3098
832k
        
Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()20.2k
) {
3099
19.7k
      if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
3100
18.4k
        // Transfer the cast to the constant.
3101
18.4k
        if (Value *V = SimplifyICmpInst(Pred, SrcOp,
3102
142
                                        ConstantExpr::getIntToPtr(RHSC, SrcTy),
3103
142
                                        Q, MaxRecurse-1))
3104
142
          return V;
3105
1.28k
      } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3106
976
        if (RI->getOperand(0)->getType() == SrcTy)
3107
850
          // Compare without the cast.
3108
850
          if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
3109
2
                                          Q, MaxRecurse-1))
3110
2
            return V;
3111
832k
      }
3112
19.7k
    }
3113
832k
3114
832k
    if (isa<ZExtInst>(LHS)) {
3115
181k
      // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3116
181k
      // same type.
3117
181k
      if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3118
11.1k
        if (MaxRecurse && 
SrcTy == RI->getOperand(0)->getType()10.8k
)
3119
7.85k
          // Compare X and Y.  Note that signed predicates become unsigned.
3120
7.85k
          if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
3121
1
                                          SrcOp, RI->getOperand(0), Q,
3122
1
                                          MaxRecurse-1))
3123
1
            return V;
3124
169k
      }
3125
169k
      // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3126
169k
      // too.  If not, then try to deduce the result of the comparison.
3127
169k
      else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3128
169k
        // Compute the constant that would happen if we truncated to SrcTy then
3129
169k
        // reextended to DstTy.
3130
169k
        Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
3131
169k
        Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
3132
169k
3133
169k
        // If the re-extended constant didn't change then this is effectively
3134
169k
        // also a case of comparing two zero-extended values.
3135
169k
        if (RExt == CI && 
MaxRecurse164k
)
3136
164k
          if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
3137
38.8k
                                        SrcOp, Trunc, Q, MaxRecurse-1))
3138
38.8k
            return V;
3139
130k
3140
130k
        // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3141
130k
        // there.  Use this to work out the result of the comparison.
3142
130k
        if (RExt != CI) {
3143
4.16k
          switch (Pred) {
3144
4.16k
          
default: 0
llvm_unreachable0
("Unknown ICmp predicate!");
3145
4.16k
          // LHS <u RHS.
3146
4.16k
          case ICmpInst::ICMP_EQ:
3147
3.08k
          case ICmpInst::ICMP_UGT:
3148
3.08k
          case ICmpInst::ICMP_UGE:
3149
3.08k
            return ConstantInt::getFalse(CI->getContext());
3150
3.08k
3151
3.08k
          case ICmpInst::ICMP_NE:
3152
548
          case ICmpInst::ICMP_ULT:
3153
548
          case ICmpInst::ICMP_ULE:
3154
548
            return ConstantInt::getTrue(CI->getContext());
3155
548
3156
548
          // LHS is non-negative.  If RHS is negative then LHS >s LHS.  If RHS
3157
548
          // is non-negative then LHS <s RHS.
3158
548
          case ICmpInst::ICMP_SGT:
3159
474
          case ICmpInst::ICMP_SGE:
3160
474
            return CI->getValue().isNegative() ?
3161
437
              ConstantInt::getTrue(CI->getContext()) :
3162
474
              
ConstantInt::getFalse(CI->getContext())37
;
3163
474
3164
474
          case ICmpInst::ICMP_SLT:
3165
55
          case ICmpInst::ICMP_SLE:
3166
55
            return CI->getValue().isNegative() ?
3167
36
              ConstantInt::getFalse(CI->getContext()) :
3168
55
              
ConstantInt::getTrue(CI->getContext())19
;
3169
789k
          }
3170
789k
        }
3171
130k
      }
3172
181k
    }
3173
789k
3174
789k
    if (isa<SExtInst>(LHS)) {
3175
111k
      // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
3176
111k
      // same type.
3177
111k
      if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3178
5.25k
        if (MaxRecurse && 
SrcTy == RI->getOperand(0)->getType()5.21k
)
3179
5.15k
          // Compare X and Y.  Note that the predicate does not change.
3180
5.15k
          if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
3181
482
                                          Q, MaxRecurse-1))
3182
482
            return V;
3183
106k
      }
3184
106k
      // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
3185
106k
      // too.  If not, then try to deduce the result of the comparison.
3186
106k
      else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3187
104k
        // Compute the constant that would happen if we truncated to SrcTy then
3188
104k
        // reextended to DstTy.
3189
104k
        Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
3190
104k
        Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
3191
104k
3192
104k
        // If the re-extended constant didn't change then this is effectively
3193
104k
        // also a case of comparing two sign-extended values.
3194
104k
        if (RExt == CI && 
MaxRecurse102k
)
3195
102k
          if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1))
3196
6.29k
            return V;
3197
98.6k
3198
98.6k
        // Otherwise the upper bits of LHS are all equal, while RHS has varying
3199
98.6k
        // bits there.  Use this to work out the result of the comparison.
3200
98.6k
        if (RExt != CI) {
3201
2.07k
          switch (Pred) {
3202
2.07k
          
default: 0
llvm_unreachable0
("Unknown ICmp predicate!");
3203
2.07k
          case ICmpInst::ICMP_EQ:
3204
621
            return ConstantInt::getFalse(CI->getContext());
3205
2.07k
          case ICmpInst::ICMP_NE:
3206
40
            return ConstantInt::getTrue(CI->getContext());
3207
2.07k
3208
2.07k
          // If RHS is non-negative then LHS <s RHS.  If RHS is negative then
3209
2.07k
          // LHS >s RHS.
3210
2.07k
          case ICmpInst::ICMP_SGT:
3211
16
          case ICmpInst::ICMP_SGE:
3212
16
            return CI->getValue().isNegative() ?
3213
9
              ConstantInt::getTrue(CI->getContext()) :
3214
16
              
ConstantInt::getFalse(CI->getContext())7
;
3215
17
          case ICmpInst::ICMP_SLT:
3216
17
          case ICmpInst::ICMP_SLE:
3217
17
            return CI->getValue().isNegative() ?
3218
8
              ConstantInt::getFalse(CI->getContext()) :
3219
17
              
ConstantInt::getTrue(CI->getContext())9
;
3220
17
3221
17
          // If LHS is non-negative then LHS <u RHS.  If LHS is negative then
3222
17
          // LHS >u RHS.
3223
414
          case ICmpInst::ICMP_UGT:
3224
414
          case ICmpInst::ICMP_UGE:
3225
414
            // Comparison is true iff the LHS <s 0.
3226
414
            if (MaxRecurse)
3227
413
              if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
3228
1
                                              Constant::getNullValue(SrcTy),
3229
1
                                              Q, MaxRecurse-1))
3230
1
                return V;
3231
413
            break;
3232
970
          case ICmpInst::ICMP_ULT:
3233
970
          case ICmpInst::ICMP_ULE:
3234
970
            // Comparison is true iff the LHS >=s 0.
3235
970
            if (MaxRecurse)
3236
970
              if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
3237
1
                                              Constant::getNullValue(SrcTy),
3238
1
                                              Q, MaxRecurse-1))
3239
1
                return V;
3240
969
            break;
3241
2.07k
          }
3242
2.07k
        }
3243
98.6k
      }
3244
111k
    }
3245
789k
  }
3246
25.5M
3247
25.5M
  // icmp eq|ne X, Y -> false|true if X != Y
3248
25.5M
  if (ICmpInst::isEquality(Pred) &&
3249
25.5M
      
isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo)16.2M
) {
3250
6.21k
    return Pred == ICmpInst::ICMP_NE ? 
getTrue(ITy)172
:
getFalse(ITy)6.04k
;
3251
6.21k
  }
3252
25.5M
3253
25.5M
  if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
3254
11.6k
    return V;
3255
25.5M
3256
25.5M
  if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
3257
3.62k
    return V;
3258
25.4M
3259
25.4M
  // Simplify comparisons of related pointers using a powerful, recursive
3260
25.4M
  // GEP-walk when we have target data available..
3261
25.4M
  if (LHS->getType()->isPointerTy())
3262
5.73M
    if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.AC, Q.CxtI,
3263
2.45k
                                     Q.IIQ, LHS, RHS))
3264
2.45k
      return C;
3265
25.4M
  if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
3266
25.1k
    if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
3267
1.11k
      if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
3268
1.11k
              Q.DL.getTypeSizeInBits(CLHS->getType()) &&
3269
1.11k
          Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) ==
3270
1.04k
              Q.DL.getTypeSizeInBits(CRHS->getType()))
3271
1.04k
        if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.AC, Q.CxtI,
3272
1
                                         Q.IIQ, CLHS->getPointerOperand(),
3273
1
                                         CRHS->getPointerOperand()))
3274
1
          return C;
3275
25.4M
3276
25.4M
  if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) {
3277
468k
    if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {
3278
170k
      if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
3279
170k
          
GLHS->hasAllConstantIndices()2.07k
&&
GRHS->hasAllConstantIndices()413
&&
3280
170k
          
(16
ICmpInst::isEquality(Pred)16
||
3281
16
           (GLHS->isInBounds() && 
GRHS->isInBounds()4
&&
3282
16
            
Pred == ICmpInst::getSignedPredicate(Pred)4
))) {
3283
4
        // The bases are equal and the indices are constant.  Build a constant
3284
4
        // expression GEP with the same indices and a null base pointer to see
3285
4
        // what constant folding can make out of it.
3286
4
        Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType());
3287
4
        SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end());
3288
4
        Constant *NewLHS = ConstantExpr::getGetElementPtr(
3289
4
            GLHS->getSourceElementType(), Null, IndicesLHS);
3290
4
3291
4
        SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end());
3292
4
        Constant *NewRHS = ConstantExpr::getGetElementPtr(
3293
4
            GLHS->getSourceElementType(), Null, IndicesRHS);
3294
4
        return ConstantExpr::getICmp(Pred, NewLHS, NewRHS);
3295
4
      }
3296
25.4M
    }
3297
468k
  }
3298
25.4M
3299
25.4M
  // If the comparison is with the result of a select instruction, check whether
3300
25.4M
  // comparing with either branch of the select always yields the same value.
3301
25.4M
  if (isa<SelectInst>(LHS) || 
isa<SelectInst>(RHS)24.8M
)
3302
904k
    if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
3303
8.35k
      return V;
3304
25.4M
3305
25.4M
  // If the comparison is with the result of a phi instruction, check whether
3306
25.4M
  // doing the compare with each incoming phi value yields a common result.
3307
25.4M
  if (isa<PHINode>(LHS) || 
isa<PHINode>(RHS)22.9M
)
3308
2.97M
    if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
3309
4.61k
      return V;
3310
25.4M
3311
25.4M
  return nullptr;
3312
25.4M
}
3313
3314
Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3315
21.8M
                              const SimplifyQuery &Q) {
3316
21.8M
  return ::SimplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
3317
21.8M
}
3318
3319
/// Given operands for an FCmpInst, see if we can fold the result.
3320
/// If not, this returns null.
3321
static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3322
                               FastMathFlags FMF, const SimplifyQuery &Q,
3323
461k
                               unsigned MaxRecurse) {
3324
461k
  CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
3325
461k
  assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
3326
461k
3327
461k
  if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3328
6.76k
    if (Constant *CRHS = dyn_cast<Constant>(RHS))
3329
5.44k
      return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3330
1.31k
3331
1.31k
    // If we have a constant, make sure it is on the RHS.
3332
1.31k
    std::swap(LHS, RHS);
3333
1.31k
    Pred = CmpInst::getSwappedPredicate(Pred);
3334
1.31k
  }
3335
461k
3336
461k
  // Fold trivial predicates.
3337
461k
  Type *RetTy = GetCompareTy(LHS);
3338
455k
  if (Pred == FCmpInst::FCMP_FALSE)
3339
35
    return getFalse(RetTy);
3340
455k
  if (Pred == FCmpInst::FCMP_TRUE)
3341
35
    return getTrue(RetTy);
3342
455k
3343
455k
  // Fold (un)ordered comparison if we can determine there are no NaNs.
3344
455k
  if (Pred == FCmpInst::FCMP_UNO || 
Pred == FCmpInst::FCMP_ORD428k
)
3345
28.3k
    if (FMF.noNaNs() ||
3346
28.3k
        
(28.3k
isKnownNeverNaN(LHS, Q.TLI)28.3k
&&
isKnownNeverNaN(RHS, Q.TLI)1.59k
))
3347
1.60k
      return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD);
3348
454k
3349
454k
  // NaN is unordered; NaN is not ordered.
3350
454k
  assert((FCmpInst::isOrdered(Pred) || FCmpInst::isUnordered(Pred)) &&
3351
454k
         "Comparison must be either ordered or unordered");
3352
454k
  if (match(RHS, m_NaN()))
3353
17
    return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
3354
454k
3355
454k
  // fcmp pred x, undef  and  fcmp pred undef, x
3356
454k
  // fold to true if unordered, false if ordered
3357
454k
  if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) {
3358
107
    // Choosing NaN for the undef will always make unordered comparison succeed
3359
107
    // and ordered comparison fail.
3360
107
    return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
3361
107
  }
3362
454k
3363
454k
  // fcmp x,x -> true/false.  Not all compares are foldable.
3364
454k
  if (LHS == RHS) {
3365
2.72k
    if (CmpInst::isTrueWhenEqual(Pred))
3366
2
      return getTrue(RetTy);
3367
2.71k
    if (CmpInst::isFalseWhenEqual(Pred))
3368
124
      return getFalse(RetTy);
3369
453k
  }
3370
453k
3371
453k
  // Handle fcmp with constant RHS.
3372
453k
  // TODO: Use match with a specific FP value, so these work with vectors with
3373
453k
  // undef lanes.
3374
453k
  const APFloat *C;
3375
453k
  if (match(RHS, m_APFloat(C))) {
3376
336k
    // Check whether the constant is an infinity.
3377
336k
    if (C->isInfinity()) {
3378
23.0k
      if (C->isNegative()) {
3379
111
        switch (Pred) {
3380
111
        case FCmpInst::FCMP_OLT:
3381
1
          // No value is ordered and less than negative infinity.
3382
1
          return getFalse(RetTy);
3383
111
        case FCmpInst::FCMP_UGE:
3384
1
          // All values are unordered with or at least negative infinity.
3385
1
          return getTrue(RetTy);
3386
111
        default:
3387
109
          break;
3388
22.8k
        }
3389
22.8k
      } else {
3390
22.8k
        switch (Pred) {
3391
22.8k
        case FCmpInst::FCMP_OGT:
3392
20
          // No value is ordered and greater than infinity.
3393
20
          return getFalse(RetTy);
3394
22.8k
        case FCmpInst::FCMP_ULE:
3395
3
          // All values are unordered with and at most infinity.
3396
3
          return getTrue(RetTy);
3397
22.8k
        default:
3398
22.8k
          break;
3399
336k
        }
3400
336k
      }
3401
23.0k
    }
3402
336k
    if (C->isNegative() && 
!C->isNegZero()14.9k
) {
3403
14.8k
      assert(!C->isNaN() && "Unexpected NaN constant!");
3404
14.8k
      // TODO: We can catch more cases by using a range check rather than
3405
14.8k
      //       relying on CannotBeOrderedLessThanZero.
3406
14.8k
      switch (Pred) {
3407
14.8k
      case FCmpInst::FCMP_UGE:
3408
524
      case FCmpInst::FCMP_UGT:
3409
524
      case FCmpInst::FCMP_UNE:
3410
524
        // (X >= 0) implies (X > C) when (C < 0)
3411
524
        if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
3412
5
          return getTrue(RetTy);
3413
519
        break;
3414
12.5k
      case FCmpInst::FCMP_OEQ:
3415
12.5k
      case FCmpInst::FCMP_OLE:
3416
12.5k
      case FCmpInst::FCMP_OLT:
3417
12.5k
        // (X >= 0) implies !(X < C) when (C < 0)
3418
12.5k
        if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
3419
11
          return getFalse(RetTy);
3420
12.5k
        break;
3421
12.5k
      default:
3422
1.79k
        break;
3423
336k
      }
3424
336k
    }
3425
336k
3426
336k
    // Check comparison of [minnum/maxnum with constant] with other constant.
3427
336k
    const APFloat *C2;
3428
336k
    if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) &&
3429
336k
         
C2->compare(*C) == APFloat::cmpLessThan14
) ||
3430
336k
        
(336k
match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2)))336k
&&
3431
336k
         
C2->compare(*C) == APFloat::cmpGreaterThan14
)) {
3432
24
      bool IsMaxNum =
3433
24
          cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
3434
24
      // The ordered relationship and minnum/maxnum guarantee that we do not
3435
24
      // have NaN constants, so ordered/unordered preds are handled the same.
3436
24
      switch (Pred) {
3437
24
      
case FCmpInst::FCMP_OEQ: 4
case FCmpInst::FCMP_UEQ:
3438
4
        // minnum(X, LesserC)  == C --> false
3439
4
        // maxnum(X, GreaterC) == C --> false
3440
4
        return getFalse(RetTy);
3441
4
      case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_UNE:
3442
4
        // minnum(X, LesserC)  != C --> true
3443
4
        // maxnum(X, GreaterC) != C --> true
3444
4
        return getTrue(RetTy);
3445
8
      case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_UGE:
3446
8
      case FCmpInst::FCMP_OGT: case FCmpInst::FCMP_UGT:
3447
8
        // minnum(X, LesserC)  >= C --> false
3448
8
        // minnum(X, LesserC)  >  C --> false
3449
8
        // maxnum(X, GreaterC) >= C --> true
3450
8
        // maxnum(X, GreaterC) >  C --> true
3451
8
        return ConstantInt::get(RetTy, IsMaxNum);
3452
8
      case FCmpInst::FCMP_OLE: case FCmpInst::FCMP_ULE:
3453
8
      case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_ULT:
3454
8
        // minnum(X, LesserC)  <= C --> true
3455
8
        // minnum(X, LesserC)  <  C --> true
3456
8
        // maxnum(X, GreaterC) <= C --> false
3457
8
        // maxnum(X, GreaterC) <  C --> false
3458
8
        return ConstantInt::get(RetTy, !IsMaxNum);
3459
8
      default:
3460
0
        // TRUE/FALSE/ORD/UNO should be handled before this.
3461
0
        llvm_unreachable("Unexpected fcmp predicate");
3462
453k
      }
3463
453k
    }
3464
336k
  }
3465
453k
3466
453k
  if (match(RHS, m_AnyZeroFP())) {
3467
72.5k
    switch (Pred) {
3468
72.5k
    case FCmpInst::FCMP_OGE:
3469
3.42k
    case FCmpInst::FCMP_ULT:
3470
3.42k
      // Positive or zero X >= 0.0 --> true
3471
3.42k
      // Positive or zero X <  0.0 --> false
3472
3.42k
      if ((FMF.noNaNs() || 
isKnownNeverNaN(LHS, Q.TLI)3.40k
) &&
3473
3.42k
          
CannotBeOrderedLessThanZero(LHS, Q.TLI)67
)
3474
19
        return Pred == FCmpInst::FCMP_OGE ? 
getTrue(RetTy)9
:
getFalse(RetTy)10
;
3475
3.40k
      break;
3476
12.8k
    case FCmpInst::FCMP_UGE:
3477
12.8k
    case FCmpInst::FCMP_OLT:
3478
12.8k
      // Positive or zero or nan X >= 0.0 --> true
3479
12.8k
      // Positive or zero or nan X <  0.0 --> false
3480
12.8k
      if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
3481
174
        return Pred == FCmpInst::FCMP_UGE ? 
getTrue(RetTy)10
:
getFalse(RetTy)164
;
3482
12.6k
      break;
3483
56.3k
    default:
3484
56.3k
      break;
3485
453k
    }
3486
453k
  }
3487
453k
3488
453k
  // If the comparison is with the result of a select instruction, check whether
3489
453k
  // comparing with either branch of the select always yields the same value.
3490
453k
  if (isa<SelectInst>(LHS) || 
isa<SelectInst>(RHS)448k
)
3491
9.40k
    if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
3492
89
      return V;
3493
453k
3494
453k
  // If the comparison is with the result of a phi instruction, check whether
3495
453k
  // doing the compare with each incoming phi value yields a common result.
3496
453k
  if (isa<PHINode>(LHS) || 
isa<PHINode>(RHS)383k
)
3497
80.1k
    if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
3498
5
      return V;
3499
453k
3500
453k
  return nullptr;
3501
453k
}
3502
3503
Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3504
372k
                              FastMathFlags FMF, const SimplifyQuery &Q) {
3505
372k
  return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit);
3506
372k
}
3507
3508
/// See if V simplifies when its operand Op is replaced with RepOp.
3509
static const Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
3510
                                           const SimplifyQuery &Q,
3511
4.11M
                                           unsigned MaxRecurse) {
3512
4.11M
  // Trivial replacement.
3513
4.11M
  if (V == Op)
3514
215k
    return RepOp;
3515
3.89M
3516
3.89M
  // We cannot replace a constant, and shouldn't even try.
3517
3.89M
  if (isa<Constant>(Op))
3518
1.77M
    return nullptr;
3519
2.12M
3520
2.12M
  auto *I = dyn_cast<Instruction>(V);
3521
2.12M
  if (!I)
3522
1.15M
    return nullptr;
3523
965k
3524
965k
  // If this is a binary operator, try to simplify it with the replaced op.
3525
965k
  if (auto *B = dyn_cast<BinaryOperator>(I)) {
3526
143k
    // Consider:
3527
143k
    //   %cmp = icmp eq i32 %x, 2147483647
3528
143k
    //   %add = add nsw i32 %x, 1
3529
143k
    //   %sel = select i1 %cmp, i32 -2147483648, i32 %add
3530
143k
    //
3531
143k
    // We can't replace %sel with %add unless we strip away the flags.
3532
143k
    if (isa<OverflowingBinaryOperator>(B))
3533
109k
      if (Q.IIQ.hasNoSignedWrap(B) || 
Q.IIQ.hasNoUnsignedWrap(B)91.8k
)
3534
18.3k
        return nullptr;
3535
125k
    if (isa<PossiblyExactOperator>(B) && 
Q.IIQ.isExact(B)9.30k
)
3536
6.61k
      return nullptr;
3537
118k
3538
118k
    if (MaxRecurse) {
3539
118k
      if (B->getOperand(0) == Op)
3540
5.68k
        return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), Q,
3541
5.68k
                             MaxRecurse - 1);
3542
113k
      if (B->getOperand(1) == Op)
3543
7.21k
        return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, Q,
3544
7.21k
                             MaxRecurse - 1);
3545
927k
    }
3546
118k
  }
3547
927k
3548
927k
  // Same for CmpInsts.
3549
927k
  if (CmpInst *C = dyn_cast<CmpInst>(I)) {
3550
2.45k
    if (MaxRecurse) {
3551
2.45k
      if (C->getOperand(0) == Op)
3552
827
        return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), Q,
3553
827
                               MaxRecurse - 1);
3554
1.62k
      if (C->getOperand(1) == Op)
3555
23
        return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, Q,
3556
23
                               MaxRecurse - 1);
3557
926k
    }
3558
2.45k
  }
3559
926k
3560
926k
  // Same for GEPs.
3561
926k
  if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
3562
358k
    if (MaxRecurse) {
3563
358k
      SmallVector<Value *, 8> NewOps(GEP->getNumOperands());
3564
358k
      transform(GEP->operands(), NewOps.begin(),
3565
952k
                [&](Value *V) { return V == Op ? 
RepOp11.8k
:
V940k
; });
3566
358k
      return SimplifyGEPInst(GEP->getSourceElementType(), NewOps, Q,
3567
358k
                             MaxRecurse - 1);
3568
358k
    }
3569
568k
  }
3570
568k
3571
568k
  // TODO: We could hand off more cases to instsimplify here.
3572
568k
3573
568k
  // If all operands are constant after substituting Op for RepOp then we can
3574
568k
  // constant fold the instruction.
3575
568k
  if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) {
3576
435k
    // Build a list of all constant operands.
3577
435k
    SmallVector<Constant *, 8> ConstOps;
3578
483k
    for (unsigned i = 0, e = I->getNumOperands(); i != e; 
++i48.1k
) {
3579
466k
      if (I->getOperand(i) == Op)
3580
2.67k
        ConstOps.push_back(CRepOp);
3581
463k
      else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i)))
3582
45.5k
        ConstOps.push_back(COp);
3583
418k
      else
3584
418k
        break;
3585
466k
    }
3586
435k
3587
435k
    // All operands were constants, fold it.
3588
435k
    if (ConstOps.size() == I->getNumOperands()) {
3589
16.9k
      if (CmpInst *C = dyn_cast<CmpInst>(I))
3590
0
        return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0],
3591
0
                                               ConstOps[1], Q.DL, Q.TLI);
3592
16.9k
3593
16.9k
      if (LoadInst *LI = dyn_cast<LoadInst>(I))
3594
13.0k
        if (!LI->isVolatile())
3595
13.0k
          return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL);
3596
3.88k
3597
3.88k
      return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI);
3598
3.88k
    }
3599
435k
  }
3600
551k
3601
551k
  return nullptr;
3602
551k
}
3603
3604
/// Try to simplify a select instruction when its condition operand is an
3605
/// integer comparison where one operand of the compare is a constant.
3606
static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
3607
495k
                                    const APInt *Y, bool TrueWhenUnset) {
3608
495k
  const APInt *C;
3609
495k
3610
495k
  // (X & Y) == 0 ? X & ~Y : X  --> X
3611
495k
  // (X & Y) != 0 ? X & ~Y : X  --> X & ~Y
3612
495k
  if (FalseVal == X && 
match(TrueVal, m_And(m_Specific(X), m_APInt(C)))103k
&&
3613
495k
      
*Y == ~*C28
)
3614
11
    return TrueWhenUnset ? 
FalseVal5
:
TrueVal6
;
3615
495k
3616
495k
  // (X & Y) == 0 ? X : X & ~Y  --> X & ~Y
3617
495k
  // (X & Y) != 0 ? X : X & ~Y  --> X
3618
495k
  if (TrueVal == X && 
match(FalseVal, m_And(m_Specific(X), m_APInt(C)))42.8k
&&
3619
495k
      
*Y == ~*C11
)
3620
11
    return TrueWhenUnset ? 
FalseVal6
:
TrueVal5
;
3621
495k
3622
495k
  if (Y->isPowerOf2()) {
3623
377k
    // (X & Y) == 0 ? X | Y : X  --> X | Y
3624
377k
    // (X & Y) != 0 ? X | Y : X  --> X
3625
377k
    if (FalseVal == X && 
match(TrueVal, m_Or(m_Specific(X), m_APInt(C)))102k
&&
3626
377k
        
*Y == *C10
)
3627
8
      return TrueWhenUnset ? 
TrueVal5
:
FalseVal3
;
3628
377k
3629
377k
    // (X & Y) == 0 ? X : X | Y  --> X
3630
377k
    // (X & Y) != 0 ? X : X | Y  --> X | Y
3631
377k
    if (TrueVal == X && 
match(FalseVal, m_Or(m_Specific(X), m_APInt(C)))1.92k
&&
3632
377k
        
*Y == *C7
)
3633
7
      return TrueWhenUnset ? 
TrueVal3
:
FalseVal4
;
3634
495k
  }
3635
495k
3636
495k
  return nullptr;
3637
495k
}
3638
3639
/// An alternative way to test if a bit is set or not uses sgt/slt instead of
3640
/// eq/ne.
3641
static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
3642
                                           ICmpInst::Predicate Pred,
3643
2.68M
                                           Value *TrueVal, Value *FalseVal) {
3644
2.68M
  Value *X;
3645
2.68M
  APInt Mask;
3646
2.68M
  if (!decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask))
3647
2.30M
    return nullptr;
3648
378k
3649
378k
  return simplifySelectBitTest(TrueVal, FalseVal, X, &Mask,
3650
378k
                               Pred == ICmpInst::ICMP_EQ);
3651
378k
}
3652
3653
/// Try to simplify a select instruction when its condition operand is an
3654
/// integer comparison.
3655
static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
3656
                                         Value *FalseVal, const SimplifyQuery &Q,
3657
2.99M
                                         unsigned MaxRecurse) {
3658
2.99M
  ICmpInst::Predicate Pred;
3659
2.99M
  Value *CmpLHS, *CmpRHS;
3660
2.99M
  if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
3661
313k
    return nullptr;
3662
2.68M
3663
2.68M
  if (ICmpInst::isEquality(Pred) && 
match(CmpRHS, m_Zero())1.03M
) {
3664
738k
    Value *X;
3665
738k
    const APInt *Y;
3666
738k
    if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y))))
3667
116k
      if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
3668
12
                                           Pred == ICmpInst::ICMP_EQ))
3669
12
        return V;
3670
738k
3671
738k
    // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
3672
738k
    Value *ShAmt;
3673
738k
    auto isFsh = m_CombineOr(m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(),
3674
738k
                                                          m_Value(ShAmt)),
3675
738k
                             m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X),
3676
738k
                                                          m_Value(ShAmt)));
3677
738k
    // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
3678
738k
    // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
3679
738k
    if (match(TrueVal, isFsh) && 
FalseVal == X16
&&
CmpLHS == ShAmt16
&&
3680
738k
        
Pred == ICmpInst::ICMP_EQ16
)
3681
8
      return X;
3682
738k
    // (ShAmt != 0) ? X : fshl(X, *, ShAmt) --> X
3683
738k
    // (ShAmt != 0) ? X : fshr(*, X, ShAmt) --> X
3684
738k
    if (match(FalseVal, isFsh) && 
TrueVal == X20
&&
CmpLHS == ShAmt18
&&
3685
738k
        
Pred == ICmpInst::ICMP_NE18
)
3686
8
      return X;
3687
738k
3688
738k
    // Test for a zero-shift-guard-op around rotates. These are used to
3689
738k
    // avoid UB from oversized shifts in raw IR rotate patterns, but the
3690
738k
    // intrinsics do not have that problem.
3691
738k
    // We do not allow this transform for the general funnel shift case because
3692
738k
    // that would not preserve the poison safety of the original code.
3693
738k
    auto isRotate = m_CombineOr(m_Intrinsic<Intrinsic::fshl>(m_Value(X),
3694
738k
                                                             m_Deferred(X),
3695
738k
                                                             m_Value(ShAmt)),
3696
738k
                                m_Intrinsic<Intrinsic::fshr>(m_Value(X),
3697
738k
                                                             m_Deferred(X),
3698
738k
                                                             m_Value(ShAmt)));
3699
738k
    // (ShAmt != 0) ? fshl(X, X, ShAmt) : X --> fshl(X, X, ShAmt)
3700
738k
    // (ShAmt != 0) ? fshr(X, X, ShAmt) : X --> fshr(X, X, ShAmt)
3701
738k
    if (match(TrueVal, isRotate) && 
FalseVal == X4
&&
CmpLHS == ShAmt4
&&
3702
738k
        
Pred == ICmpInst::ICMP_NE4
)
3703
4
      return TrueVal;
3704
738k
    // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
3705
738k
    // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
3706
738k
    if (match(FalseVal, isRotate) && 
TrueVal == X6
&&
CmpLHS == ShAmt6
&&
3707
738k
        
Pred == ICmpInst::ICMP_EQ6
)
3708
6
      return FalseVal;
3709
2.68M
  }
3710
2.68M
3711
2.68M
  // Check for other compares that behave like bit test.
3712
2.68M
  if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, CmpRHS, Pred,
3713
25
                                              TrueVal, FalseVal))
3714
25
    return V;
3715
2.68M
3716
2.68M
  // If we have an equality comparison, then we know the value in one of the
3717
2.68M
  // arms of the select. See if substituting this value into the arm and
3718
2.68M
  // simplifying the result yields the same value as the other arm.
3719
2.68M
  if (Pred == ICmpInst::ICMP_EQ) {
3720
947k
    if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) ==
3721
947k
            TrueVal ||
3722
947k
        SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) ==
3723
943k
            TrueVal)
3724
3.40k
      return FalseVal;
3725
943k
    if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) ==
3726
943k
            FalseVal ||
3727
943k
        SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) ==
3728
943k
            FalseVal)
3729
2
      return FalseVal;
3730
1.73M
  } else if (Pred == ICmpInst::ICMP_NE) {
3731
84.2k
    if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, MaxRecurse) ==
3732
84.2k
            FalseVal ||
3733
84.2k
        SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, MaxRecurse) ==
3734
84.1k
            FalseVal)
3735
61
      return TrueVal;
3736
84.1k
    if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, MaxRecurse) ==
3737
84.1k
            TrueVal ||
3738
84.1k
        SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, MaxRecurse) ==
3739
84.1k
            TrueVal)
3740
0
      return TrueVal;
3741
2.67M
  }
3742
2.67M
3743
2.67M
  return nullptr;
3744
2.67M
}
3745
3746
/// Try to simplify a select instruction when its condition operand is a
3747
/// floating-point comparison.
3748
2.99M
static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F) {
3749
2.99M
  FCmpInst::Predicate Pred;
3750
2.99M
  if (!match(Cond, m_FCmp(Pred, m_Specific(T), m_Specific(F))) &&
3751
2.99M
      
!match(Cond, m_FCmp(Pred, m_Specific(F), m_Specific(T)))2.97M
)
3752
2.96M
    return nullptr;
3753
21.7k
3754
21.7k
  // TODO: The transform may not be valid with -0.0. An incomplete way of
3755
21.7k
  // testing for that possibility is to check if at least one operand is a
3756
21.7k
  // non-zero constant.
3757
21.7k
  const APFloat *C;
3758
21.7k
  if ((match(T, m_APFloat(C)) && 
C->isNonZero()5.09k
) ||
3759
21.7k
      
(17.7k
match(F, m_APFloat(C))17.7k
&&
C->isNonZero()2.02k
)) {
3760
5.64k
    // (T == F) ? T : F --> F
3761
5.64k
    // (F == T) ? T : F --> F
3762
5.64k
    if (Pred == FCmpInst::FCMP_OEQ)
3763
4
      return F;
3764
5.64k
3765
5.64k
    // (T != F) ? T : F --> T
3766
5.64k
    // (F != T) ? T : F --> T
3767
5.64k
    if (Pred == FCmpInst::FCMP_UNE)
3768
4
      return T;
3769
21.7k
  }
3770
21.7k
3771
21.7k
  return nullptr;
3772
21.7k
}
3773
3774
/// Given operands for a SelectInst, see if we can fold the result.
3775
/// If not, this returns null.
3776
static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
3777
3.00M
                                 const SimplifyQuery &Q, unsigned MaxRecurse) {
3778
3.00M
  if (auto *CondC = dyn_cast<Constant>(Cond)) {
3779
11.4k
    if (auto *TrueC = dyn_cast<Constant>(TrueVal))
3780
8.43k
      if (auto *FalseC = dyn_cast<Constant>(FalseVal))
3781
1.60k
        return ConstantFoldSelectInstruction(CondC, TrueC, FalseC);
3782
9.82k
3783
9.82k
    // select undef, X, Y -> X or Y
3784
9.82k
    if (isa<UndefValue>(CondC))
3785
52
      return isa<Constant>(FalseVal) ? 
FalseVal10
:
TrueVal42
;
3786
9.76k
3787
9.76k
    // TODO: Vector constants with undef elements don't simplify.
3788
9.76k
3789
9.76k
    // select true, X, Y  -> X
3790
9.76k
    if (CondC->isAllOnesValue())
3791
2.10k
      return TrueVal;
3792
7.66k
    // select false, X, Y -> Y
3793
7.66k
    if (CondC->isNullValue())
3794
7.62k
      return FalseVal;
3795
2.99M
  }
3796
2.99M
3797
2.99M
  // select ?, X, X -> X
3798
2.99M
  if (TrueVal == FalseVal)
3799
216
    return TrueVal;
3800
2.99M
3801
2.99M
  if (isa<UndefValue>(TrueVal))   // select ?, undef, X -> X
3802
35
    return FalseVal;
3803
2.99M
  if (isa<UndefValue>(FalseVal))   // select ?, X, undef -> X
3804
72
    return TrueVal;
3805
2.99M
3806
2.99M
  if (Value *V =
3807
3.52k
          simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
3808
3.52k
    return V;
3809
2.99M
3810
2.99M
  if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal))
3811
8
    return V;
3812
2.99M
3813
2.99M
  if (Value *V = foldSelectWithBinaryOp(Cond, TrueVal, FalseVal))
3814
20
    return V;
3815
2.99M
3816
2.99M
  Optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL);
3817
2.99M
  if (Imp)
3818
194
    return *Imp ? 
TrueVal156
:
FalseVal38
;
3819
2.99M
3820
2.99M
  return nullptr;
3821
2.99M
}
3822
3823
Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
3824
3.00M
                                const SimplifyQuery &Q) {
3825
3.00M
  return ::SimplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit);
3826
3.00M
}
3827
3828
/// Given operands for an GetElementPtrInst, see if we can fold the result.
3829
/// If not, this returns null.
3830
static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
3831
38.1M
                              const SimplifyQuery &Q, unsigned) {
3832
38.1M
  // The type of the GEP pointer operand.
3833
38.1M
  unsigned AS =
3834
38.1M
      cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace();
3835
38.1M
3836
38.1M
  // getelementptr P -> P.
3837
38.1M
  if (Ops.size() == 1)
3838
70
    return Ops[0];
3839
38.1M
3840
38.1M
  // Compute the (pointer) type returned by the GEP instruction.
3841
38.1M
  Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Ops.slice(1));
3842
38.1M
  Type *GEPTy = PointerType::get(LastType, AS);
3843
38.1M
  if (VectorType *VT = dyn_cast<VectorType>(Ops[0]->getType()))
3844
2.34k
    GEPTy = VectorType::get(GEPTy, VT->getNumElements());
3845
38.1M
  else if (VectorType *VT = dyn_cast<VectorType>(Ops[1]->getType()))
3846
1.39k
    GEPTy = VectorType::get(GEPTy, VT->getNumElements());
3847
38.1M
3848
38.1M
  if (isa<UndefValue>(Ops[0]))
3849
2.48k
    return UndefValue::get(GEPTy);
3850
38.1M
3851
38.1M
  if (Ops.size() == 2) {
3852
9.66M
    // getelementptr P, 0 -> P.
3853
9.66M
    if (match(Ops[1], m_Zero()) && 
Ops[0]->getType() == GEPTy122k
)
3854
122k
      return Ops[0];
3855
9.53M
3856
9.53M
    Type *Ty = SrcTy;
3857
9.53M
    if (Ty->isSized()) {
3858
9.53M
      Value *P;
3859
9.53M
      uint64_t C;
3860
9.53M
      uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
3861
9.53M
      // getelementptr P, N -> P if P points to a type of zero size.
3862
9.53M
      if (TyAllocSize == 0 && 
Ops[0]->getType() == GEPTy3
)
3863
2
        return Ops[0];
3864
9.53M
3865
9.53M
      // The following transforms are only safe if the ptrtoint cast
3866
9.53M
      // doesn't truncate the pointers.
3867
9.53M
      if (Ops[1]->getType()->getScalarSizeInBits() ==
3868
9.53M
          Q.DL.getIndexSizeInBits(AS)) {
3869
9.42M
        auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * {
3870
572
          if (match(P, m_Zero()))
3871
3
            return Constant::getNullValue(GEPTy);
3872
569
          Value *Temp;
3873
569
          if (match(P, m_PtrToInt(m_Value(Temp))))
3874
105
            if (Temp->getType() == GEPTy)
3875
103
              return Temp;
3876
466
          return nullptr;
3877
466
        };
3878
9.42M
3879
9.42M
        // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
3880
9.42M
        if (TyAllocSize == 1 &&
3881
9.42M
            
match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))))5.08M
)
3882
132
          if (Value *R = PtrToIntOrZero(P))
3883
64
            return R;
3884
9.42M
3885
9.42M
        // getelementptr V, (ashr (sub P, V), C) -> Q
3886
9.42M
        // if P points to a type of size 1 << C.
3887
9.42M
        if (match(Ops[1],
3888
9.42M
                  m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))),
3889
9.42M
                         m_ConstantInt(C))) &&
3890
9.42M
            
TyAllocSize == 1ULL << C80
)
3891
80
          if (Value *R = PtrToIntOrZero(P))
3892
7
            return R;
3893
9.42M
3894
9.42M
        // getelementptr V, (sdiv (sub P, V), C) -> Q
3895
9.42M
        // if P points to a type of size C.
3896
9.42M
        if (match(Ops[1],
3897
9.42M
                  m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))),
3898
9.42M
                         m_SpecificInt(TyAllocSize))))
3899
360
          if (Value *R = PtrToIntOrZero(P))
3900
35
            return R;
3901
37.9M
      }
3902
9.53M
    }
3903
9.53M
  }
3904
37.9M
3905
37.9M
  if (Q.DL.getTypeAllocSize(LastType) == 1 &&
3906
37.9M
      all_of(Ops.slice(1).drop_back(1),
3907
8.70M
             [](Value *Idx) 
{ return match(Idx, m_Zero()); }4.59M
)) {
3908
7.98M
    unsigned IdxWidth =
3909
7.98M
        Q.DL.getIndexSizeInBits(Ops[0]->getType()->getPointerAddressSpace());
3910
7.98M
    if (Q.DL.getTypeSizeInBits(Ops.back()->getType()) == IdxWidth) {
3911
6.95M
      APInt BasePtrOffset(IdxWidth, 0);
3912
6.95M
      Value *StrippedBasePtr =
3913
6.95M
          Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL,
3914
6.95M
                                                            BasePtrOffset);
3915
6.95M
3916
6.95M
      // gep (gep V, C), (sub 0, V) -> C
3917
6.95M
      if (match(Ops.back(),
3918
6.95M
                m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) {
3919
3
        auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
3920
3
        return ConstantExpr::getIntToPtr(CI, GEPTy);
3921
3
      }
3922
6.95M
      // gep (gep V, C), (xor V, -1) -> C-1
3923
6.95M
      if (match(Ops.back(),
3924
6.95M
                m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes()))) {
3925
1
        auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
3926
1
        return ConstantExpr::getIntToPtr(CI, GEPTy);
3927
1
      }
3928
37.9M
    }
3929
7.98M
  }
3930
37.9M
3931
37.9M
  // Check to see if this is constant foldable.
3932
40.5M
  
if (37.9M
!all_of(Ops, [](Value *V) 37.9M
{ return isa<Constant>(V); }))
3933
37.9M
    return nullptr;
3934
72.0k
3935
72.0k
  auto *CE = ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ops[0]),
3936
72.0k
                                            Ops.slice(1));
3937
72.0k
  if (auto *CEFolded = ConstantFoldConstant(CE, Q.DL))
3938
71.0k
    return CEFolded;
3939
1.01k
  return CE;
3940
1.01k
}
3941
3942
Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
3943
37.7M
                             const SimplifyQuery &Q) {
3944
37.7M
  return ::SimplifyGEPInst(SrcTy, Ops, Q, RecursionLimit);
3945
37.7M
}
3946
3947
/// Given operands for an InsertValueInst, see if we can fold the result.
3948
/// If not, this returns null.
3949
static Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
3950
                                      ArrayRef<unsigned> Idxs, const SimplifyQuery &Q,
3951
84.8k
                                      unsigned) {
3952
84.8k
  if (Constant *CAgg = dyn_cast<Constant>(Agg))
3953
34.3k
    if (Constant *CVal = dyn_cast<Constant>(Val))
3954
1.01k
      return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
3955
83.7k
3956
83.7k
  // insertvalue x, undef, n -> x
3957
83.7k
  if (match(Val, m_Undef()))
3958
166
    return Agg;
3959
83.6k
3960
83.6k
  // insertvalue x, (extractvalue y, n), n
3961
83.6k
  if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
3962
10.8k
    if (EV->getAggregateOperand()->getType() == Agg->getType() &&
3963
10.8k
        
EV->getIndices() == Idxs8.91k
) {
3964
8.91k
      // insertvalue undef, (extractvalue y, n), n -> y
3965
8.91k
      if (match(Agg, m_Undef()))
3966
4.34k
        return EV->getAggregateOperand();
3967
4.57k
3968
4.57k
      // insertvalue y, (extractvalue y, n), n -> y
3969
4.57k
      if (Agg == EV->getAggregateOperand())
3970
4.53k
        return Agg;
3971
74.7k
    }
3972
74.7k
3973
74.7k
  return nullptr;
3974
74.7k
}
3975
3976
Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val,
3977
                                     ArrayRef<unsigned> Idxs,
3978
84.8k
                                     const SimplifyQuery &Q) {
3979
84.8k
  return ::SimplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
3980
84.8k
}
3981
3982
Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
3983
190k
                                       const SimplifyQuery &Q) {
3984
190k
  // Try to constant fold.
3985
190k
  auto *VecC = dyn_cast<Constant>(Vec);
3986
190k
  auto *ValC = dyn_cast<Constant>(Val);
3987
190k
  auto *IdxC = dyn_cast<Constant>(Idx);
3988
190k
  if (VecC && 
ValC111k
&&
IdxC2.81k
)
3989
2.64k
    return ConstantFoldInsertElementInstruction(VecC, ValC, IdxC);
3990
188k
3991
188k
  // Fold into undef if index is out of bounds.
3992
188k
  if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
3993
186k
    uint64_t NumElements = cast<VectorType>(Vec->getType())->getNumElements();
3994
186k
    if (CI->uge(NumElements))
3995
9
      return UndefValue::get(Vec->getType());
3996
188k
  }
3997
188k
3998
188k
  // If index is undef, it might be out of bounds (see above case)
3999
188k
  if (isa<UndefValue>(Idx))
4000
14
    return UndefValue::get(Vec->getType());
4001
188k
4002
188k
  // Inserting an undef scalar? Assume it is the same value as the existing
4003
188k
  // vector element.
4004
188k
  if (isa<UndefValue>(Val))
4005
47
    return Vec;
4006
188k
4007
188k
  // If we are extracting a value from a vector, then inserting it into the same
4008
188k
  // place, that's the input vector:
4009
188k
  // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
4010
188k
  if (match(Val, m_ExtractElement(m_Specific(Vec), m_Specific(Idx))))
4011
5
    return Vec;
4012
188k
4013
188k
  return nullptr;
4014
188k
}
4015
4016
/// Given operands for an ExtractValueInst, see if we can fold the result.
4017
/// If not, this returns null.
4018
static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
4019
776k
                                       const SimplifyQuery &, unsigned) {
4020
776k
  if (auto *CAgg = dyn_cast<Constant>(Agg))
4021
439
    return ConstantFoldExtractValueInstruction(CAgg, Idxs);
4022
775k
4023
775k
  // extractvalue x, (insertvalue y, elt, n), n -> elt
4024
775k
  unsigned NumIdxs = Idxs.size();
4025
795k
  for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
4026
775k
       
IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())20.0k
) {
4027
47.5k
    ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
4028
47.5k
    unsigned NumInsertValueIdxs = InsertValueIdxs.size();
4029
47.5k
    unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
4030
47.5k
    if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
4031
47.5k
        Idxs.slice(0, NumCommonIdxs)) {
4032
27.5k
      if (NumIdxs == NumInsertValueIdxs)
4033
14.9k
        return IVI->getInsertedValueOperand();
4034
12.5k
      break;
4035
12.5k
    }
4036
47.5k
  }
4037
775k
4038
775k
  
return nullptr760k
;
4039
775k
}
4040
4041
Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
4042
776k
                                      const SimplifyQuery &Q) {
4043
776k
  return ::SimplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit);
4044
776k
}
4045
4046
/// Given operands for an ExtractElementInst, see if we can fold the result.
4047
/// If not, this returns null.
4048
static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &,
4049
167k
                                         unsigned) {
4050
167k
  if (auto *CVec = dyn_cast<Constant>(Vec)) {
4051
3.65k
    if (auto *CIdx = dyn_cast<Constant>(Idx))
4052
3.16k
      return ConstantFoldExtractElementInstruction(CVec, CIdx);
4053
490
4054
490
    // The index is not relevant if our vector is a splat.
4055
490
    if (auto *Splat = CVec->getSplatValue())
4056
3
      return Splat;
4057
487
4058
487
    if (isa<UndefValue>(Vec))
4059
1
      return UndefValue::get(Vec->getType()->getVectorElementType());
4060
164k
  }
4061
164k
4062
164k
  // If extracting a specified index from the vector, see if we can recursively
4063
164k
  // find a previously computed scalar that was inserted into the vector.
4064
164k
  if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
4065
162k
    if (IdxC->getValue().uge(Vec->getType()->getVectorNumElements()))
4066
8
      // definitely out of bounds, thus undefined result
4067
8
      return UndefValue::get(Vec->getType()->getVectorElementType());
4068
162k
    if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
4069
338
      return Elt;
4070
164k
  }
4071
164k
4072
164k
  // An undef extract index can be arbitrarily chosen to be an out-of-range
4073
164k
  // index value, which would result in the instruction being undef.
4074
164k
  if (isa<UndefValue>(Idx))
4075
6
    return UndefValue::get(Vec->getType()->getVectorElementType());
4076
164k
4077
164k
  return nullptr;
4078
164k
}
4079
4080
Value *llvm::SimplifyExtractElementInst(Value *Vec, Value *Idx,
4081
167k
                                        const SimplifyQuery &Q) {
4082
167k
  return ::SimplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
4083
167k
}
4084
4085
/// See if we can fold the given phi. If not, returns null.
4086
84.4M
static Value *SimplifyPHINode(PHINode *PN, const SimplifyQuery &Q) {
4087
84.4M
  // If all of the PHI's incoming values are the same then replace the PHI node
4088
84.4M
  // with the common value.
4089
84.4M
  Value *CommonValue = nullptr;
4090
84.4M
  bool HasUndefInput = false;
4091
170M
  for (Value *Incoming : PN->incoming_values()) {
4092
170M
    // If the incoming value is the phi node itself, it can safely be skipped.
4093
170M
    if (Incoming == PN) 
continue37.6k
;
4094
170M
    if (isa<UndefValue>(Incoming)) {
4095
235k
      // Remember that we saw an undef value, but otherwise ignore them.
4096
235k
      HasUndefInput = true;
4097
235k
      continue;
4098
235k
    }
4099
170M
    if (CommonValue && 
Incoming != CommonValue85.9M
)
4100
83.0M
      return nullptr;  // Not the same, bail out.
4101
87.3M
    CommonValue = Incoming;
4102
87.3M
  }
4103
84.4M
4104
84.4M
  // If CommonValue is null then all of the incoming values were either undef or
4105
84.4M
  // equal to the phi node itself.
4106
84.4M
  
if (1.39M
!CommonValue1.39M
)
4107
1.50k
    return UndefValue::get(PN->getType());
4108
1.39M
4109
1.39M
  // If we have a PHI node like phi(X, undef, X), where X is defined by some
4110
1.39M
  // instruction, we cannot return X as the result of the PHI node unless it
4111
1.39M
  // dominates the PHI block.
4112
1.39M
  if (HasUndefInput)
4113
204k
    return valueDominatesPHI(CommonValue, PN, Q.DT) ? 
CommonValue3.70k
:
nullptr200k
;
4114
1.19M
4115
1.19M
  return CommonValue;
4116
1.19M
}
4117
4118
static Value *SimplifyCastInst(unsigned CastOpc, Value *Op,
4119
10.6M
                               Type *Ty, const SimplifyQuery &Q, unsigned MaxRecurse) {
4120
10.6M
  if (auto *C = dyn_cast<Constant>(Op))
4121
86.1k
    return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL);
4122
10.5M
4123
10.5M
  if (auto *CI = dyn_cast<CastInst>(Op)) {
4124
269k
    auto *Src = CI->getOperand(0);
4125
269k
    Type *SrcTy = Src->getType();
4126
269k
    Type *MidTy = CI->getType();
4127
269k
    Type *DstTy = Ty;
4128
269k
    if (Src->getType() == Ty) {
4129
157k
      auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode());
4130
157k
      auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
4131
157k
      Type *SrcIntPtrTy =
4132
157k
          SrcTy->isPtrOrPtrVectorTy() ? 
Q.DL.getIntPtrType(SrcTy)39.6k
:
nullptr118k
;
4133
157k
      Type *MidIntPtrTy =
4134
157k
          MidTy->isPtrOrPtrVectorTy() ? 
Q.DL.getIntPtrType(MidTy)17.6k
:
nullptr140k
;
4135
157k
      Type *DstIntPtrTy =
4136
157k
          DstTy->isPtrOrPtrVectorTy() ? 
Q.DL.getIntPtrType(DstTy)39.6k
:
nullptr118k
;
4137
157k
      if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy,
4138
157k
                                         SrcIntPtrTy, MidIntPtrTy,
4139
157k
                                         DstIntPtrTy) == Instruction::BitCast)
4140
119k
        return Src;
4141
10.4M
    }
4142
269k
  }
4143
10.4M
4144
10.4M
  // bitcast x -> x
4145
10.4M
  if (CastOpc == Instruction::BitCast)
4146
3.94M
    if (Op->getType() == Ty)
4147
2.85k
      return Op;
4148
10.4M
4149
10.4M
  return nullptr;
4150
10.4M
}
4151
4152
Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
4153
10.6M
                              const SimplifyQuery &Q) {
4154
10.6M
  return ::SimplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit);
4155
10.6M
}
4156
4157
/// For the given destination element of a shuffle, peek through shuffles to
4158
/// match a root vector source operand that contains that element in the same
4159
/// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
4160
static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
4161
                                   int MaskVal, Value *RootVec,
4162
417k
                                   unsigned MaxRecurse) {
4163
417k
  if (!MaxRecurse--)
4164
117
    return nullptr;
4165
417k
4166
417k
  // Bail out if any mask value is undefined. That kind of shuffle may be
4167
417k
  // simplified further based on demanded bits or other folds.
4168
417k
  if (MaskVal == -1)
4169
6
    return nullptr;
4170
417k
4171
417k
  // The mask value chooses which source operand we need to look at next.
4172
417k
  int InVecNumElts = Op0->getType()->getVectorNumElements();
4173
417k
  int RootElt = MaskVal;
4174
417k
  Value *SourceOp = Op0;
4175
417k
  if (MaskVal >= InVecNumElts) {
4176
10.2k
    RootElt = MaskVal - InVecNumElts;
4177
10.2k
    SourceOp = Op1;
4178
10.2k
  }
4179
417k
4180
417k
  // If the source operand is a shuffle itself, look through it to find the
4181
417k
  // matching root vector.
4182
417k
  if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
4183
27.4k
    return foldIdentityShuffles(
4184
27.4k
        DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
4185
27.4k
        SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
4186
27.4k
  }
4187
390k
4188
390k
  // TODO: Look through bitcasts? What if the bitcast changes the vector element
4189
390k
  // size?
4190
390k
4191
390k
  // The source operand is not a shuffle. Initialize the root vector value for
4192
390k
  // this shuffle if that has not been done yet.
4193
390k
  if (!RootVec)
4194
290k
    RootVec = SourceOp;
4195
390k
4196
390k
  // Give up as soon as a source operand does not match the existing root value.
4197
390k
  if (RootVec != SourceOp)
4198
4.26k
    return nullptr;
4199
385k
4200
385k
  // The element must be coming from the same lane in the source vector
4201
385k
  // (although it may have crossed lanes in intermediate shuffles).
4202
385k
  if (RootElt != DestElt)
4203
214k
    return nullptr;
4204
171k
4205
171k
  return RootVec;
4206
171k
}
4207
4208
static Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask,
4209
                                        Type *RetTy, const SimplifyQuery &Q,
4210
308k
                                        unsigned MaxRecurse) {
4211
308k
  if (isa<UndefValue>(Mask))
4212
2
    return UndefValue::get(RetTy);
4213
308k
4214
308k
  Type *InVecTy = Op0->getType();
4215
308k
  unsigned MaskNumElts = Mask->getType()->getVectorNumElements();
4216
308k
  unsigned InVecNumElts = InVecTy->getVectorNumElements();
4217
308k
4218
308k
  SmallVector<int, 32> Indices;
4219
308k
  ShuffleVectorInst::getShuffleMask(Mask, Indices);
4220
308k
  assert(MaskNumElts == Indices.size() &&
4221
308k
         "Size of Indices not same as number of mask elements?");
4222
308k
4223
308k
  // Canonicalization: If mask does not select elements from an input vector,
4224
308k
  // replace that input vector with undef.
4225
308k
  bool MaskSelects0 = false, MaskSelects1 = false;
4226
1.68M
  for (unsigned i = 0; i != MaskNumElts; 
++i1.37M
) {
4227
1.37M
    if (Indices[i] == -1)
4228
57.4k
      continue;
4229
1.31M
    if ((unsigned)Indices[i] < InVecNumElts)
4230
1.05M
      MaskSelects0 = true;
4231
256k
    else
4232
256k
      MaskSelects1 = true;
4233
1.31M
  }
4234
308k
  if (!MaskSelects0)
4235
1.87k
    Op0 = UndefValue::get(InVecTy);
4236
308k
  if (!MaskSelects1)
4237
258k
    Op1 = UndefValue::get(InVecTy);
4238
308k
4239
308k
  auto *Op0Const = dyn_cast<Constant>(Op0);
4240
308k
  auto *Op1Const = dyn_cast<Constant>(Op1);
4241
308k
4242
308k
  // If all operands are constant, constant fold the shuffle.
4243
308k
  if (Op0Const && 
Op1Const6.97k
)
4244
3.84k
    return ConstantFoldShuffleVectorInstruction(Op0Const, Op1Const, Mask);
4245
304k
4246
304k
  // Canonicalization: if only one input vector is constant, it shall be the
4247
304k
  // second one.
4248
304k
  if (Op0Const && 
!Op1Const3.13k
) {
4249
3.13k
    std::swap(Op0, Op1);
4250
3.13k
    ShuffleVectorInst::commuteShuffleMask(Indices, InVecNumElts);
4251
3.13k
  }
4252
304k
4253
304k
  // A shuffle of a splat is always the splat itself. Legal if the shuffle's
4254
304k
  // value type is same as the input vectors' type.
4255
304k
  if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
4256
12.6k
    if (isa<UndefValue>(Op1) && 
RetTy == InVecTy7.09k
&&
4257
12.6k
        
OpShuf->getMask()->getSplatValue()1.61k
)
4258
23
      return Op0;
4259
304k
4260
304k
  // Don't fold a shuffle with undef mask elements. This may get folded in a
4261
304k
  // better way using demanded bits or other analysis.
4262
304k
  // TODO: Should we allow this?
4263
304k
  if (find(Indices, -1) != Indices.end())
4264
14.1k
    return nullptr;
4265
290k
4266
290k
  // Check if every element of this shuffle can be mapped back to the
4267
290k
  // corresponding element of a single root vector. If so, we don't need this
4268
290k
  // shuffle. This handles simple identity shuffles as well as chains of
4269
290k
  // shuffles that may widen/narrow and/or move elements across lanes and back.
4270
290k
  Value *RootVec = nullptr;
4271
393k
  for (unsigned i = 0; i != MaskNumElts; 
++i103k
) {
4272
390k
    // Note that recursion is limited for each vector element, so if any element
4273
390k
    // exceeds the limit, this will fail to simplify.
4274
390k
    RootVec =
4275
390k
        foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse);
4276
390k
4277
390k
    // We can't replace a widening/narrowing shuffle with one of its operands.
4278
390k
    if (!RootVec || 
RootVec->getType() != RetTy171k
)
4279
287k
      return nullptr;
4280
390k
  }
4281
290k
  
return RootVec3.11k
;
4282
290k
}
4283
4284
/// Given operands for a ShuffleVectorInst, fold the result or return null.
4285
Value *llvm::SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask,
4286
308k
                                       Type *RetTy, const SimplifyQuery &Q) {
4287
308k
  return ::SimplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit);
4288
308k
}
4289
4290
static Constant *foldConstant(Instruction::UnaryOps Opcode,
4291
324
                              Value *&Op, const SimplifyQuery &Q) {
4292
324
  if (auto *C = dyn_cast<Constant>(Op))
4293
10
    return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL);
4294
314
  return nullptr;
4295
314
}
4296
4297
/// Given the operand for an FNeg, see if we can fold the result.  If not, this
4298
/// returns null.
4299
static Value *simplifyFNegInst(Value *Op, FastMathFlags FMF,
4300
324
                               const SimplifyQuery &Q, unsigned MaxRecurse) {
4301
324
  if (Constant *C = foldConstant(Instruction::FNeg, Op, Q))
4302
10
    return C;
4303
314
4304
314
  Value *X;
4305
314
  // fneg (fneg X) ==> X
4306
314
  if (match(Op, m_FNeg(m_Value(X))))
4307
9
    return X;
4308
305
4309
305
  return nullptr;
4310
305
}
4311
4312
Value *llvm::SimplifyFNegInst(Value *Op, FastMathFlags FMF,
4313
324
                              const SimplifyQuery &Q) {
4314
324
  return ::simplifyFNegInst(Op, FMF, Q, RecursionLimit);
4315
324
}
4316
4317
107
static Constant *propagateNaN(Constant *In) {
4318
107
  // If the input is a vector with undef elements, just return a default NaN.
4319
107
  if (!In->isNaN())
4320
1
    return ConstantFP::getNaN(In->getType());
4321
106
4322
106
  // Propagate the existing NaN constant when possible.
4323
106
  // TODO: Should we quiet a signaling NaN?
4324
106
  return In;
4325
106
}
4326
4327
3.12M
static Constant *simplifyFPBinop(Value *Op0, Value *Op1) {
4328
3.12M
  if (isa<UndefValue>(Op0) || 
isa<UndefValue>(Op1)3.12M
)
4329
229
    return ConstantFP::getNaN(Op0->getType());
4330
3.12M
4331
3.12M
  if (match(Op0, m_NaN()))
4332
14
    return propagateNaN(cast<Constant>(Op0));
4333
3.12M
  if (match(Op1, m_NaN()))
4334
93
    return propagateNaN(cast<Constant>(Op1));
4335
3.12M
4336
3.12M
  return nullptr;
4337
3.12M
}
4338
4339
/// Given operands for an FAdd, see if we can fold the result.  If not, this
4340
/// returns null.
4341
static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4342
1.11M
                               const SimplifyQuery &Q, unsigned MaxRecurse) {
4343
1.11M
  if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
4344
1.23k
    return C;
4345
1.11M
4346
1.11M
  if (Constant *C = simplifyFPBinop(Op0, Op1))
4347
28
    return C;
4348
1.11M
4349
1.11M
  // fadd X, -0 ==> X
4350
1.11M
  if (match(Op1, m_NegZeroFP()))
4351
6
    return Op0;
4352
1.11M
4353
1.11M
  // fadd X, 0 ==> X, when we know X is not -0
4354
1.11M
  if (match(Op1, m_PosZeroFP()) &&
4355
1.11M
      
(7.28k
FMF.noSignedZeros()7.28k
||
CannotBeNegativeZero(Op0, Q.TLI)7.24k
))
4356
61
    return Op0;
4357
1.11M
4358
1.11M
  // With nnan: -X + X --> 0.0 (and commuted variant)
4359
1.11M
  // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
4360
1.11M
  // Negative zeros are allowed because we always end up with positive zero:
4361
1.11M
  // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
4362
1.11M
  // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
4363
1.11M
  // X =  0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
4364
1.11M
  // X =  0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
4365
1.11M
  if (FMF.noNaNs()) {
4366
3.58k
    if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
4367
3.58k
        
match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0)))3.58k
)
4368
6
      return ConstantFP::getNullValue(Op0->getType());
4369
3.58k
4370
3.58k
    if (match(Op0, m_FNeg(m_Specific(Op1))) ||
4371
3.58k
        
match(Op1, m_FNeg(m_Specific(Op0)))3.58k
)
4372
2
      return ConstantFP::getNullValue(Op0->getType());
4373
1.11M
  }
4374
1.11M
4375
1.11M
  // (X - Y) + Y --> X
4376
1.11M
  // Y + (X - Y) --> X
4377
1.11M
  Value *X;
4378
1.11M
  if (FMF.noSignedZeros() && 
FMF.allowReassoc()3.28k
&&
4379
1.11M
      
(3.24k
match(Op0, m_FSub(m_Value(X), m_Specific(Op1)))3.24k
||
4380
3.24k
       
match(Op1, m_FSub(m_Value(X), m_Specific(Op0)))3.24k
))
4381
2
    return X;
4382
1.11M
4383
1.11M
  return nullptr;
4384
1.11M
}
4385
4386
/// Given operands for an FSub, see if we can fold the result.  If not, this
4387
/// returns null.
4388
static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4389
317k
                               const SimplifyQuery &Q, unsigned MaxRecurse) {
4390
317k
  if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
4391
1.43k
    return C;
4392
315k
4393
315k
  if (Constant *C = simplifyFPBinop(Op0, Op1))
4394
149
    return C;
4395
315k
4396
315k
  // fsub X, +0 ==> X
4397
315k
  if (match(Op1, m_PosZeroFP()))
4398
57
    return Op0;
4399
315k
4400
315k
  // fsub X, -0 ==> X, when we know X is not -0
4401
315k
  if (match(Op1, m_NegZeroFP()) &&
4402
315k
      
(1
FMF.noSignedZeros()1
||
CannotBeNegativeZero(Op0, Q.TLI)0
))
4403
1
    return Op0;
4404
315k
4405
315k
  // fsub -0.0, (fsub -0.0, X) ==> X
4406
315k
  // fsub -0.0, (fneg X) ==> X
4407
315k
  Value *X;
4408
315k
  if (match(Op0, m_NegZeroFP()) &&
4409
315k
      
match(Op1, m_FNeg(m_Value(X)))67.2k
)
4410
61
    return X;
4411
315k
4412
315k
  // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
4413
315k
  // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.
4414
315k
  if (FMF.noSignedZeros() && 
match(Op0, m_AnyZeroFP())1.34k
&&
4415
315k
      
(364
match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X)))364
||
4416
364
       
match(Op1, m_FNeg(m_Value(X)))361
))
4417
5
    return X;
4418
315k
4419
315k
  // fsub nnan x, x ==> 0.0
4420
315k
  if (FMF.noNaNs() && 
Op0 == Op11.23k
)
4421
1
    return Constant::getNullValue(Op0->getType());
4422
315k
4423
315k
  // Y - (Y - X) --> X
4424
315k
  // (X + Y) - Y --> X
4425
315k
  if (FMF.noSignedZeros() && 
FMF.allowReassoc()1.34k
&&
4426
315k
      
(1.27k
match(Op1, m_FSub(m_Specific(Op0), m_Value(X)))1.27k
||
4427
1.27k
       
match(Op0, m_c_FAdd(m_Specific(Op1), m_Value(X)))1.27k
))
4428
4
    return X;
4429
315k
4430
315k
  return nullptr;
4431
315k
}
4432
4433
/// Given the operands for an FMul, see if we can fold the result
4434
static Value *SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4435
1.13M
                               const SimplifyQuery &Q, unsigned MaxRecurse) {
4436
1.13M
  if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
4437
3.73k
    return C;
4438
1.12M
4439
1.12M
  if (Constant *C = simplifyFPBinop(Op0, Op1))
4440
138
    return C;
4441
1.12M
4442
1.12M
  // fmul X, 1.0 ==> X
4443
1.12M
  if (match(Op1, m_FPOne()))
4444
858
    return Op0;
4445
1.12M
4446
1.12M
  // fmul nnan nsz X, 0 ==> 0
4447
1.12M
  if (FMF.noNaNs() && 
FMF.noSignedZeros()5.02k
&&
match(Op1, m_AnyZeroFP())4.93k
)
4448
15
    return ConstantFP::getNullValue(Op0->getType());
4449
1.12M
4450
1.12M
  // sqrt(X) * sqrt(X) --> X, if we can:
4451
1.12M
  // 1. Remove the intermediate rounding (reassociate).
4452
1.12M
  // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
4453
1.12M
  // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
4454
1.12M
  Value *X;
4455
1.12M
  if (Op0 == Op1 && 
match(Op0, m_Intrinsic<Intrinsic::sqrt>(m_Value(X)))69.6k
&&
4456
1.12M
      
FMF.allowReassoc()49
&&
FMF.noNaNs()4
&&
FMF.noSignedZeros()3
)
4457
2
    return X;
4458
1.12M
4459
1.12M
  return nullptr;
4460
1.12M
}
4461
4462
Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4463
953k
                              const SimplifyQuery &Q) {
4464
953k
  return ::SimplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit);
4465
953k
}
4466
4467
4468
Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4469
285k
                              const SimplifyQuery &Q) {
4470
285k
  return ::SimplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit);
4471
285k
}
4472
4473
Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4474
978k
                              const SimplifyQuery &Q) {
4475
978k
  return ::SimplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit);
4476
978k
}
4477
4478
static Value *SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4479
569k
                               const SimplifyQuery &Q, unsigned) {
4480
569k
  if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
4481
1.47k
    return C;
4482
567k
4483
567k
  if (Constant *C = simplifyFPBinop(Op0, Op1))
4484
13
    return C;
4485
567k
4486
567k
  // X / 1.0 -> X
4487
567k
  if (match(Op1, m_FPOne()))
4488
152
    return Op0;
4489
567k
4490
567k
  // 0 / X -> 0
4491
567k
  // Requires that NaNs are off (X could be zero) and signed zeroes are
4492
567k
  // ignored (X could be positive or negative, so the output sign is unknown).
4493
567k
  if (FMF.noNaNs() && 
FMF.noSignedZeros()1.36k
&&
match(Op0, m_AnyZeroFP())1.29k
)
4494
2
    return ConstantFP::getNullValue(Op0->getType());
4495
567k
4496
567k
  if (FMF.noNaNs()) {
4497
1.36k
    // X / X -> 1.0 is legal when NaNs are ignored.
4498
1.36k
    // We can ignore infinities because INF/INF is NaN.
4499
1.36k
    if (Op0 == Op1)
4500
3
      return ConstantFP::get(Op0->getType(), 1.0);
4501
1.36k
4502
1.36k
    // (X * Y) / Y --> X if we can reassociate to the above form.
4503
1.36k
    Value *X;
4504
1.36k
    if (FMF.allowReassoc() && 
match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1)))1.28k
)
4505
2
      return X;
4506
1.36k
4507
1.36k
    // -X /  X -> -1.0 and
4508
1.36k
    //  X / -X -> -1.0 are legal when NaNs are ignored.
4509
1.36k
    // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
4510
1.36k
    if (match(Op0, m_FNegNSZ(m_Specific(Op1))) ||
4511
1.36k
        
match(Op1, m_FNegNSZ(m_Specific(Op0)))1.36k
)
4512
5
      return ConstantFP::get(Op0->getType(), -1.0);
4513
567k
  }
4514
567k
4515
567k
  return nullptr;
4516
567k
}
4517
4518
Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4519
542k
                              const SimplifyQuery &Q) {
4520
542k
  return ::SimplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit);
4521
542k
}
4522
4523
static Value *SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4524
1.04k
                               const SimplifyQuery &Q, unsigned) {
4525
1.04k
  if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
4526
42
    return C;
4527
1.00k
4528
1.00k
  if (Constant *C = simplifyFPBinop(Op0, Op1))
4529
8
    return C;
4530
993
4531
993
  // Unlike fdiv, the result of frem always matches the sign of the dividend.
4532
993
  // The constant match may include undef elements in a vector, so return a full
4533
993
  // zero constant as the result.
4534
993
  if (FMF.noNaNs()) {
4535
12
    // +0 % X -> 0
4536
12
    if (match(Op0, m_PosZeroFP()))
4537
2
      return ConstantFP::getNullValue(Op0->getType());
4538
10
    // -0 % X -> -0
4539
10
    if (match(Op0, m_NegZeroFP()))
4540
2
      return ConstantFP::getNegativeZero(Op0->getType());
4541
989
  }
4542
989
4543
989
  return nullptr;
4544
989
}
4545
4546
Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
4547
883
                              const SimplifyQuery &Q) {
4548
883
  return ::SimplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit);
4549
883
}
4550
4551
//=== Helper functions for higher up the class hierarchy.
4552
4553
/// Given the operand for a UnaryOperator, see if we can fold the result.
4554
/// If not, this returns null.
4555
static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q,
4556
0
                           unsigned MaxRecurse) {
4557
0
  switch (Opcode) {
4558
0
  case Instruction::FNeg:
4559
0
    return simplifyFNegInst(Op, FastMathFlags(), Q, MaxRecurse);
4560
0
  default:
4561
0
    llvm_unreachable("Unexpected opcode");
4562
0
  }
4563
0
}
4564
4565
/// Given the operand for a UnaryOperator, see if we can fold the result.
4566
/// If not, this returns null.
4567
/// In contrast to SimplifyUnOp, try to use FastMathFlag when folding the
4568
/// result. In case we don't need FastMathFlags, simply fall to SimplifyUnOp.
4569
static Value *simplifyFPUnOp(unsigned Opcode, Value *Op,
4570
                             const FastMathFlags &FMF,
4571
                             const SimplifyQuery &Q, unsigned MaxRecurse) {
4572
  switch (Opcode) {
4573
  case Instruction::FNeg:
4574
    return simplifyFNegInst(Op, FMF, Q, MaxRecurse);
4575
  default:
4576
    return simplifyUnOp(Opcode, Op, Q, MaxRecurse);
4577
  }
4578
}
4579
4580
0
Value *llvm::SimplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) {
4581
0
  return ::simplifyUnOp(Opcode, Op, Q, RecursionLimit);
4582
0
}
4583
4584
Value *llvm::SimplifyFPUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
4585
0
                            const SimplifyQuery &Q) {
4586
0
  return ::simplifyFPUnOp(Opcode, Op, FMF, Q, RecursionLimit);
4587
0
}
4588
4589
/// Given operands for a BinaryOperator, see if we can fold the result.
4590
/// If not, this returns null.
4591
static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
4592
24.4M
                            const SimplifyQuery &Q, unsigned MaxRecurse) {
4593
24.4M
  switch (Opcode) {
4594
24.4M
  case Instruction::Add:
4595
8.90M
    return SimplifyAddInst(LHS, RHS, false, false, Q, MaxRecurse);
4596
24.4M
  case Instruction::Sub:
4597
1.23M
    return SimplifySubInst(LHS, RHS, false, false, Q, MaxRecurse);
4598
24.4M
  case Instruction::Mul:
4599
6.69M
    return SimplifyMulInst(LHS, RHS, Q, MaxRecurse);
4600
24.4M
  case Instruction::SDiv:
4601
71.9k
    return SimplifySDivInst(LHS, RHS, Q, MaxRecurse);
4602
24.4M
  case Instruction::UDiv:
4603
47.1k
    return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse);
4604
24.4M
  case Instruction::SRem:
4605
11.8k
    return SimplifySRemInst(LHS, RHS, Q, MaxRecurse);
4606
24.4M
  case Instruction::URem:
4607
27.4k
    return SimplifyURemInst(LHS, RHS, Q, MaxRecurse);
4608
24.4M
  case Instruction::Shl:
4609
1.05M
    return SimplifyShlInst(LHS, RHS, false, false, Q, MaxRecurse);
4610
24.4M
  case Instruction::LShr:
4611
408k
    return SimplifyLShrInst(LHS, RHS, false, Q, MaxRecurse);
4612
24.4M
  case Instruction::AShr:
4613
87.8k
    return SimplifyAShrInst(LHS, RHS, false, Q, MaxRecurse);
4614
24.4M
  case Instruction::And:
4615
2.13M
    return SimplifyAndInst(LHS, RHS, Q, MaxRecurse);
4616
24.4M
  case Instruction::Or:
4617
3.17M
    return SimplifyOrInst(LHS, RHS, Q, MaxRecurse);
4618
24.4M
  case Instruction::Xor:
4619
572k
    return SimplifyXorInst(LHS, RHS, Q, MaxRecurse);
4620
24.4M
  case Instruction::FAdd:
4621
1.55k
    return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4622
24.4M
  case Instruction::FSub:
4623
40
    return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4624
24.4M
  case Instruction::FMul:
4625
2.64k
    return SimplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4626
24.4M
  case Instruction::FDiv:
4627
9
    return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4628
24.4M
  case Instruction::FRem:
4629
160
    return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4630
24.4M
  default:
4631
0
    llvm_unreachable("Unexpected opcode");
4632
24.4M
  }
4633
24.4M
}
4634
4635
/// Given operands for a BinaryOperator, see if we can fold the result.
4636
/// If not, this returns null.
4637
/// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
4638
/// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
4639
static Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
4640
                              const FastMathFlags &FMF, const SimplifyQuery &Q,
4641
372k
                              unsigned MaxRecurse) {
4642
372k
  switch (Opcode) {
4643
372k
  case Instruction::FAdd:
4644
163k
    return SimplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse);
4645
372k
  case Instruction::FSub:
4646
32.0k
    return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
4647
372k
  case Instruction::FMul:
4648
149k
    return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
4649
372k
  case Instruction::FDiv:
4650
26.9k
    return SimplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
4651
372k
  default:
4652
160
    return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
4653
372k
  }
4654
372k
}
4655
4656
Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
4657
5.53M
                           const SimplifyQuery &Q) {
4658
5.53M
  return ::SimplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit);
4659
5.53M
}
4660
4661
Value *llvm::SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
4662
372k
                             FastMathFlags FMF, const SimplifyQuery &Q) {
4663
372k
  return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit);
4664
372k
}
4665
4666
/// Given operands for a CmpInst, see if we can fold the result.
4667
static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
4668
4.40M
                              const SimplifyQuery &Q, unsigned MaxRecurse) {
4669
4.40M
  if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
4670
4.31M
    return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
4671
89.0k
  return SimplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse);
4672
89.0k
}
4673
4674
Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
4675
364k
                             const SimplifyQuery &Q) {
4676
364k
  return ::SimplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
4677
364k
}
4678
4679
481k
static bool IsIdempotent(Intrinsic::ID ID) {
4680
481k
  switch (ID) {
4681
481k
  
default: return false411k
;
4682
481k
4683
481k
  // Unary idempotent: f(f(x)) = f(x)
4684
481k
  case Intrinsic::fabs:
4685
70.0k
  case Intrinsic::floor:
4686
70.0k
  case Intrinsic::ceil:
4687
70.0k
  case Intrinsic::trunc:
4688
70.0k
  case Intrinsic::rint:
4689
70.0k
  case Intrinsic::nearbyint:
4690
70.0k
  case Intrinsic::round:
4691
70.0k
  case Intrinsic::canonicalize:
4692
70.0k
    return true;
4693
481k
  }
4694
481k
}
4695
4696
static Value *SimplifyRelativeLoad(Constant *Ptr, Constant *Offset,
4697
9
                                   const DataLayout &DL) {
4698
9
  GlobalValue *PtrSym;
4699
9
  APInt PtrOffset;
4700
9
  if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL))
4701
1
    return nullptr;
4702
8
4703
8
  Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext());
4704
8
  Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
4705
8
  Type *Int32PtrTy = Int32Ty->getPointerTo();
4706
8
  Type *Int64Ty = Type::getInt64Ty(Ptr->getContext());
4707
8
4708
8
  auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
4709
8
  if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64)
4710
0
    return nullptr;
4711
8
4712
8
  uint64_t OffsetInt = OffsetConstInt->getSExtValue();
4713
8
  if (OffsetInt % 4 != 0)
4714
1
    return nullptr;
4715
7
4716
7
  Constant *C = ConstantExpr::getGetElementPtr(
4717
7
      Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy),
4718
7
      ConstantInt::get(Int64Ty, OffsetInt / 4));
4719
7
  Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL);
4720
7
  if (!Loaded)
4721
0
    return nullptr;
4722
7
4723
7
  auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
4724
7
  if (!LoadedCE)
4725
0
    return nullptr;
4726
7
4727
7
  if (LoadedCE->getOpcode() == Instruction::Trunc) {
4728
6
    LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
4729
6
    if (!LoadedCE)
4730
0
      return nullptr;
4731
7
  }
4732
7
4733
7
  if (LoadedCE->getOpcode() != Instruction::Sub)
4734
1
    return nullptr;
4735
6
4736
6
  auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
4737
6
  if (!LoadedLHS || 
LoadedLHS->getOpcode() != Instruction::PtrToInt5
)
4738
1
    return nullptr;
4739
5
  auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
4740
5
4741
5
  Constant *LoadedRHS = LoadedCE->getOperand(1);
4742
5
  GlobalValue *LoadedRHSSym;
4743
5
  APInt LoadedRHSOffset;
4744
5
  if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset,
4745
5
                                  DL) ||
4746
5
      
PtrSym != LoadedRHSSym4
||
PtrOffset != LoadedRHSOffset4
)
4747
1
    return nullptr;
4748
4
4749
4
  return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy);
4750
4
}
4751
4752
static Value *simplifyUnaryIntrinsic(Function *F, Value *Op0,
4753
481k
                                     const SimplifyQuery &Q) {
4754
481k
  // Idempotent functions return the same result when called repeatedly.
4755
481k
  Intrinsic::ID IID = F->getIntrinsicID();
4756
481k
  if (IsIdempotent(IID))
4757
70.0k
    if (auto *II = dyn_cast<IntrinsicInst>(Op0))
4758
920
      if (II->getIntrinsicID() == IID)
4759
20
        return II;
4760
481k
4761
481k
  Value *X;
4762
481k
  switch (IID) {
4763
481k
  case Intrinsic::fabs:
4764
53.4k
    if (SignBitMustBeZero(Op0, Q.TLI)) 
return Op048
;
4765
53.4k
    break;
4766
53.4k
  case Intrinsic::bswap:
4767
8.10k
    // bswap(bswap(x)) -> x
4768
8.10k
    if (match(Op0, m_BSwap(m_Value(X)))) 
return X1
;
4769
8.10k
    break;
4770
8.10k
  case Intrinsic::bitreverse:
4771
645
    // bitreverse(bitreverse(x)) -> x
4772
645
    if (match(Op0, m_BitReverse(m_Value(X)))) 
return X2
;
4773
643
    break;
4774
5.56k
  case Intrinsic::exp:
4775
5.56k
    // exp(log(x)) -> x
4776
5.56k
    if (Q.CxtI->hasAllowReassoc() &&
4777
5.56k
        
match(Op0, m_Intrinsic<Intrinsic::log>(m_Value(X)))17
)
return X3
;
4778
5.55k
    break;
4779
5.55k
  case Intrinsic::exp2:
4780
555
    // exp2(log2(x)) -> x
4781
555
    if (Q.CxtI->hasAllowReassoc() &&
4782
555
        
match(Op0, m_Intrinsic<Intrinsic::log2>(m_Value(X)))22
)
return X3
;
4783
552
    break;
4784
3.92k
  case Intrinsic::log:
4785
3.92k
    // log(exp(x)) -> x
4786
3.92k
    if (Q.CxtI->hasAllowReassoc() &&
4787
3.92k
        
match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X)))4
)
return X3
;
4788
3.91k
    break;
4789
3.91k
  case Intrinsic::log2:
4790
154
    // log2(exp2(x)) -> x
4791
154
    if (Q.CxtI->hasAllowReassoc() &&
4792
154
        
(10
match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X)))10
||
4793
10
         match(Op0, m_Intrinsic<Intrinsic::pow>(m_SpecificFP(2.0),
4794
7
                                                m_Value(X))))) 
return X5
;
4795
149
    break;
4796
1.12k
  case Intrinsic::log10:
4797
1.12k
    // log10(pow(10.0, x)) -> x
4798
1.12k
    if (Q.CxtI->hasAllowReassoc() &&
4799
1.12k
        match(Op0, m_Intrinsic<Intrinsic::pow>(m_SpecificFP(10.0),
4800
2
                                               m_Value(X)))) return X;
4801
1.12k
    break;
4802
14.9k
  case Intrinsic::floor:
4803
14.9k
  case Intrinsic::trunc:
4804
14.9k
  case Intrinsic::ceil:
4805
14.9k
  case Intrinsic::round:
4806
14.9k
  case Intrinsic::nearbyint:
4807
14.9k
  case Intrinsic::rint: {
4808
14.9k
    // floor (sitofp x) -> sitofp x
4809
14.9k
    // floor (uitofp x) -> uitofp x
4810
14.9k
    //
4811
14.9k
    // Converting from int always results in a finite integral number or
4812
14.9k
    // infinity. For either of those inputs, these rounding functions always
4813
14.9k
    // return the same value, so the rounding can be eliminated.
4814
14.9k
    if (match(Op0, m_SIToFP(m_Value())) || 
match(Op0, m_UIToFP(m_Value()))14.9k
)
4815
15
      return Op0;
4816
14.9k
    break;
4817
14.9k
  }
4818
393k
  default:
4819
393k
    break;
4820
481k
  }
4821
481k
4822
481k
  return nullptr;
4823
481k
}
4824
4825
static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
4826
3.32M
                                      const SimplifyQuery &Q) {
4827
3.32M
  Intrinsic::ID IID = F->getIntrinsicID();
4828
3.32M
  Type *ReturnType = F->getReturnType();
4829
3.32M
  switch (IID) {
4830
3.32M
  case Intrinsic::usub_with_overflow:
4831
547
  case Intrinsic::ssub_with_overflow:
4832
547
    // X - X -> { 0, false }
4833
547
    if (Op0 == Op1)
4834
4
      return Constant::getNullValue(ReturnType);
4835
543
    LLVM_FALLTHROUGH;
4836
1.22k
  case Intrinsic::uadd_with_overflow:
4837
1.22k
  case Intrinsic::sadd_with_overflow:
4838
1.22k
    // X - undef -> { undef, false }
4839
1.22k
    // undef - X -> { undef, false }
4840
1.22k
    // X + undef -> { undef, false }
4841
1.22k
    // undef + x -> { undef, false }
4842
1.22k
    if (isa<UndefValue>(Op0) || 
isa<UndefValue>(Op1)1.21k
) {
4843
17
      return ConstantStruct::get(
4844
17
          cast<StructType>(ReturnType),
4845
17
          {UndefValue::get(ReturnType->getStructElementType(0)),
4846
17
           Constant::getNullValue(ReturnType->getStructElementType(1))});
4847
17
    }
4848
1.20k
    break;
4849
18.4k
  case Intrinsic::umul_with_overflow:
4850
18.4k
  case Intrinsic::smul_with_overflow:
4851
18.4k
    // 0 * X -> { 0, false }
4852
18.4k
    // X * 0 -> { 0, false }
4853
18.4k
    if (match(Op0, m_Zero()) || 
match(Op1, m_Zero())18.4k
)
4854
9
      return Constant::getNullValue(ReturnType);
4855
18.4k
    // undef * X -> { 0, false }
4856
18.4k
    // X * undef -> { 0, false }
4857
18.4k
    if (match(Op0, m_Undef()) || 
match(Op1, m_Undef())18.4k
)
4858
8
      return Constant::getNullValue(ReturnType);
4859
18.4k
    break;
4860
18.4k
  case Intrinsic::uadd_sat:
4861
1.30k
    // sat(MAX + X) -> MAX
4862
1.30k
    // sat(X + MAX) -> MAX
4863
1.30k
    if (match(Op0, m_AllOnes()) || 
match(Op1, m_AllOnes())1.30k
)
4864
4
      return Constant::getAllOnesValue(ReturnType);
4865
1.30k
    LLVM_FALLTHROUGH;
4866
1.50k
  case Intrinsic::sadd_sat:
4867
1.50k
    // sat(X + undef) -> -1
4868
1.50k
    // sat(undef + X) -> -1
4869
1.50k
    // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
4870
1.50k
    // For signed: Assume undef is ~X, in which case X + ~X = -1.
4871
1.50k
    if (match(Op0, m_Undef()) || 
match(Op1, m_Undef())1.49k
)
4872
10
      return Constant::getAllOnesValue(ReturnType);
4873
1.49k
4874
1.49k
    // X + 0 -> X
4875
1.49k
    if (match(Op1, m_Zero()))
4876
6
      return Op0;
4877
1.48k
    // 0 + X -> X
4878
1.48k
    if (match(Op0, m_Zero()))
4879
4
      return Op1;
4880
1.48k
    break;
4881
1.48k
  case Intrinsic::usub_sat:
4882
942
    // sat(0 - X) -> 0, sat(X - MAX) -> 0
4883
942
    if (match(Op0, m_Zero()) || 
match(Op1, m_AllOnes())938
)
4884
6
      return Constant::getNullValue(ReturnType);
4885
936
    LLVM_FALLTHROUGH;
4886
1.05k
  case Intrinsic::ssub_sat:
4887
1.05k
    // X - X -> 0, X - undef -> 0, undef - X -> 0
4888
1.05k
    if (Op0 == Op1 || 
match(Op0, m_Undef())1.04k
||
match(Op1, m_Undef())1.04k
)
4889
14
      return Constant::getNullValue(ReturnType);
4890
1.04k
    // X - 0 -> X
4891
1.04k
    if (match(Op1, m_Zero()))
4892
4
      return Op0;
4893
1.03k
    break;
4894
1.03k
  case Intrinsic::load_relative:
4895
9
    if (auto *C0 = dyn_cast<Constant>(Op0))
4896
9
      if (auto *C1 = dyn_cast<Constant>(Op1))
4897
9
        return SimplifyRelativeLoad(C0, C1, Q.DL);
4898
0
    break;
4899
35
  case Intrinsic::powi:
4900
35
    if (auto *Power = dyn_cast<ConstantInt>(Op1)) {
4901
14
      // powi(x, 0) -> 1.0
4902
14
      if (Power->isZero())
4903
1
        return ConstantFP::get(Op0->getType(), 1.0);
4904
13
      // powi(x, 1) -> x
4905
13
      if (Power->isOne())
4906
1
        return Op0;
4907
33
    }
4908
33
    break;
4909
6.08k
  case Intrinsic::maxnum:
4910
6.08k
  case Intrinsic::minnum:
4911
6.08k
  case Intrinsic::maximum:
4912
6.08k
  case Intrinsic::minimum: {
4913
6.08k
    // If the arguments are the same, this is a no-op.
4914
6.08k
    if (Op0 == Op1) 
return Op0116
;
4915
5.96k
4916
5.96k
    // If one argument is undef, return the other argument.
4917
5.96k
    if (match(Op0, m_Undef()))
4918
24
      return Op1;
4919
5.94k
    if (match(Op1, m_Undef()))
4920
30
      return Op0;
4921
5.91k
4922
5.91k
    // If one argument is NaN, return other or NaN appropriately.
4923
5.91k
    bool PropagateNaN = IID == Intrinsic::minimum || 
IID == Intrinsic::maximum5.74k
;
4924
5.91k
    if (match(Op0, m_NaN()))
4925
21
      return PropagateNaN ? 
Op06
:
Op115
;
4926
5.89k
    if (match(Op1, m_NaN()))
4927
29
      return PropagateNaN ? 
Op16
:
Op023
;
4928
5.86k
4929
5.86k
    // Min/max of the same operation with common operand:
4930
5.86k
    // m(m(X, Y)), X --> m(X, Y) (4 commuted variants)
4931
5.86k
    if (auto *M0 = dyn_cast<IntrinsicInst>(Op0))
4932
2.87k
      if (M0->getIntrinsicID() == IID &&
4933
2.87k
          
(142
M0->getOperand(0) == Op1142
||
M0->getOperand(1) == Op1138
))
4934
8
        return Op0;
4935
5.85k
    if (auto *M1 = dyn_cast<IntrinsicInst>(Op1))
4936
1.65k
      if (M1->getIntrinsicID() == IID &&
4937
1.65k
          
(79
M1->getOperand(0) == Op079
||
M1->getOperand(1) == Op075
))
4938
8
        return Op1;
4939
5.84k
4940
5.84k
    // min(X, -Inf) --> -Inf (and commuted variant)
4941
5.84k
    // max(X, +Inf) --> +Inf (and commuted variant)
4942
5.84k
    bool UseNegInf = IID == Intrinsic::minnum || 
IID == Intrinsic::minimum3.61k
;
4943
5.84k
    const APFloat *C;
4944
5.84k
    if ((match(Op0, m_APFloat(C)) && 
C->isInfinity()131
&&
4945
5.84k
         
C->isNegative() == UseNegInf7
) ||
4946
5.84k
        
(5.84k
match(Op1, m_APFloat(C))5.84k
&&
C->isInfinity()2.15k
&&
4947
5.84k
         
C->isNegative() == UseNegInf4
))
4948
8
      return ConstantFP::getInfinity(ReturnType, UseNegInf);
4949
5.84k
4950
5.84k
    // TODO: minnum(nnan x, inf) -> x
4951
5.84k
    // TODO: minnum(nnan ninf x, flt_max) -> x
4952
5.84k
    // TODO: maxnum(nnan x, -inf) -> x
4953
5.84k
    // TODO: maxnum(nnan ninf x, -flt_max) -> x
4954
5.84k
    break;
4955
5.84k
  }
4956
3.30M
  default:
4957
3.30M
    break;
4958
3.32M
  }
4959
3.32M
4960
3.32M
  return nullptr;
4961
3.32M
}
4962
4963
4.99M
static Val