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
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/Analysis/AssumptionCache.h"
26 #include "llvm/Analysis/CaptureTracking.h"
27 #include "llvm/Analysis/CmpInstAnalysis.h"
28 #include "llvm/Analysis/ConstantFolding.h"
29 #include "llvm/Analysis/FloatingPointPredicateUtils.h"
30 #include "llvm/Analysis/InstSimplifyFolder.h"
31 #include "llvm/Analysis/Loads.h"
32 #include "llvm/Analysis/LoopAnalysisManager.h"
33 #include "llvm/Analysis/MemoryBuiltins.h"
34 #include "llvm/Analysis/OverflowInstAnalysis.h"
35 #include "llvm/Analysis/TargetLibraryInfo.h"
36 #include "llvm/Analysis/ValueTracking.h"
37 #include "llvm/Analysis/VectorUtils.h"
38 #include "llvm/IR/ConstantRange.h"
39 #include "llvm/IR/DataLayout.h"
40 #include "llvm/IR/Dominators.h"
41 #include "llvm/IR/InstrTypes.h"
42 #include "llvm/IR/Instructions.h"
43 #include "llvm/IR/Operator.h"
44 #include "llvm/IR/PatternMatch.h"
45 #include "llvm/IR/Statepoint.h"
46 #include "llvm/Support/KnownBits.h"
47 #include "llvm/Support/KnownFPClass.h"
48 #include <algorithm>
49 #include <optional>
50 using namespace llvm;
51 using namespace llvm::PatternMatch;
52
53 #define DEBUG_TYPE "instsimplify"
54
55 enum { RecursionLimit = 3 };
56
57 STATISTIC(NumExpand, "Number of expansions");
58 STATISTIC(NumReassoc, "Number of reassociations");
59
60 static Value *simplifyAndInst(Value *, Value *, const SimplifyQuery &,
61 unsigned);
62 static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned);
63 static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &,
64 const SimplifyQuery &, unsigned);
65 static Value *simplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &,
66 unsigned);
67 static Value *simplifyBinOp(unsigned, Value *, Value *, const FastMathFlags &,
68 const SimplifyQuery &, unsigned);
69 static Value *simplifyCmpInst(CmpPredicate, Value *, Value *,
70 const SimplifyQuery &, unsigned);
71 static Value *simplifyICmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
72 const SimplifyQuery &Q, unsigned MaxRecurse);
73 static Value *simplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned);
74 static Value *simplifyXorInst(Value *, Value *, const SimplifyQuery &,
75 unsigned);
76 static Value *simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &,
77 unsigned);
78 static Value *simplifyGEPInst(Type *, Value *, ArrayRef<Value *>,
79 GEPNoWrapFlags, const SimplifyQuery &, unsigned);
80 static Value *simplifySelectInst(Value *, Value *, Value *,
81 const SimplifyQuery &, unsigned);
82 static Value *simplifyInstructionWithOperands(Instruction *I,
83 ArrayRef<Value *> NewOps,
84 const SimplifyQuery &SQ,
85 unsigned MaxRecurse);
86
87 /// For a boolean type or a vector of boolean type, return false or a vector
88 /// with every element false.
getFalse(Type * Ty)89 static Constant *getFalse(Type *Ty) { return ConstantInt::getFalse(Ty); }
90
91 /// For a boolean type or a vector of boolean type, return true or a vector
92 /// with every element true.
getTrue(Type * Ty)93 static Constant *getTrue(Type *Ty) { return ConstantInt::getTrue(Ty); }
94
95 /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
isSameCompare(Value * V,CmpPredicate Pred,Value * LHS,Value * RHS)96 static bool isSameCompare(Value *V, CmpPredicate Pred, Value *LHS, Value *RHS) {
97 CmpInst *Cmp = dyn_cast<CmpInst>(V);
98 if (!Cmp)
99 return false;
100 CmpInst::Predicate CPred = Cmp->getPredicate();
101 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
102 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
103 return true;
104 return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
105 CRHS == LHS;
106 }
107
108 /// Simplify comparison with true or false branch of select:
109 /// %sel = select i1 %cond, i32 %tv, i32 %fv
110 /// %cmp = icmp sle i32 %sel, %rhs
111 /// Compose new comparison by substituting %sel with either %tv or %fv
112 /// and see if it simplifies.
simplifyCmpSelCase(CmpPredicate Pred,Value * LHS,Value * RHS,Value * Cond,const SimplifyQuery & Q,unsigned MaxRecurse,Constant * TrueOrFalse)113 static Value *simplifyCmpSelCase(CmpPredicate Pred, Value *LHS, Value *RHS,
114 Value *Cond, const SimplifyQuery &Q,
115 unsigned MaxRecurse, Constant *TrueOrFalse) {
116 Value *SimplifiedCmp = simplifyCmpInst(Pred, LHS, RHS, Q, MaxRecurse);
117 if (SimplifiedCmp == Cond) {
118 // %cmp simplified to the select condition (%cond).
119 return TrueOrFalse;
120 } else if (!SimplifiedCmp && isSameCompare(Cond, Pred, LHS, RHS)) {
121 // It didn't simplify. However, if composed comparison is equivalent
122 // to the select condition (%cond) then we can replace it.
123 return TrueOrFalse;
124 }
125 return SimplifiedCmp;
126 }
127
128 /// Simplify comparison with true branch of select
simplifyCmpSelTrueCase(CmpPredicate Pred,Value * LHS,Value * RHS,Value * Cond,const SimplifyQuery & Q,unsigned MaxRecurse)129 static Value *simplifyCmpSelTrueCase(CmpPredicate Pred, Value *LHS, Value *RHS,
130 Value *Cond, const SimplifyQuery &Q,
131 unsigned MaxRecurse) {
132 return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
133 getTrue(Cond->getType()));
134 }
135
136 /// Simplify comparison with false branch of select
simplifyCmpSelFalseCase(CmpPredicate Pred,Value * LHS,Value * RHS,Value * Cond,const SimplifyQuery & Q,unsigned MaxRecurse)137 static Value *simplifyCmpSelFalseCase(CmpPredicate Pred, Value *LHS, Value *RHS,
138 Value *Cond, const SimplifyQuery &Q,
139 unsigned MaxRecurse) {
140 return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
141 getFalse(Cond->getType()));
142 }
143
144 /// We know comparison with both branches of select can be simplified, but they
145 /// are not equal. This routine handles some logical simplifications.
handleOtherCmpSelSimplifications(Value * TCmp,Value * FCmp,Value * Cond,const SimplifyQuery & Q,unsigned MaxRecurse)146 static Value *handleOtherCmpSelSimplifications(Value *TCmp, Value *FCmp,
147 Value *Cond,
148 const SimplifyQuery &Q,
149 unsigned MaxRecurse) {
150 // If the false value simplified to false, then the result of the compare
151 // is equal to "Cond && TCmp". This also catches the case when the false
152 // value simplified to false and the true value to true, returning "Cond".
153 // Folding select to and/or isn't poison-safe in general; impliesPoison
154 // checks whether folding it does not convert a well-defined value into
155 // poison.
156 if (match(FCmp, m_Zero()) && impliesPoison(TCmp, Cond))
157 if (Value *V = simplifyAndInst(Cond, TCmp, Q, MaxRecurse))
158 return V;
159 // If the true value simplified to true, then the result of the compare
160 // is equal to "Cond || FCmp".
161 if (match(TCmp, m_One()) && impliesPoison(FCmp, Cond))
162 if (Value *V = simplifyOrInst(Cond, FCmp, Q, MaxRecurse))
163 return V;
164 // Finally, if the false value simplified to true and the true value to
165 // false, then the result of the compare is equal to "!Cond".
166 if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
167 if (Value *V = simplifyXorInst(
168 Cond, Constant::getAllOnesValue(Cond->getType()), Q, MaxRecurse))
169 return V;
170 return nullptr;
171 }
172
173 /// Does the given value dominate the specified phi node?
valueDominatesPHI(Value * V,PHINode * P,const DominatorTree * DT)174 static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
175 Instruction *I = dyn_cast<Instruction>(V);
176 if (!I)
177 // Arguments and constants dominate all instructions.
178 return true;
179
180 // If we have a DominatorTree then do a precise test.
181 if (DT)
182 return DT->dominates(I, P);
183
184 // Otherwise, if the instruction is in the entry block and is not an invoke,
185 // then it obviously dominates all phi nodes.
186 if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I) &&
187 !isa<CallBrInst>(I))
188 return true;
189
190 return false;
191 }
192
193 /// Try to simplify a binary operator of form "V op OtherOp" where V is
194 /// "(B0 opex B1)" by distributing 'op' across 'opex' as
195 /// "(B0 op OtherOp) opex (B1 op OtherOp)".
expandBinOp(Instruction::BinaryOps Opcode,Value * V,Value * OtherOp,Instruction::BinaryOps OpcodeToExpand,const SimplifyQuery & Q,unsigned MaxRecurse)196 static Value *expandBinOp(Instruction::BinaryOps Opcode, Value *V,
197 Value *OtherOp, Instruction::BinaryOps OpcodeToExpand,
198 const SimplifyQuery &Q, unsigned MaxRecurse) {
199 auto *B = dyn_cast<BinaryOperator>(V);
200 if (!B || B->getOpcode() != OpcodeToExpand)
201 return nullptr;
202 Value *B0 = B->getOperand(0), *B1 = B->getOperand(1);
203 Value *L =
204 simplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(), MaxRecurse);
205 if (!L)
206 return nullptr;
207 Value *R =
208 simplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(), MaxRecurse);
209 if (!R)
210 return nullptr;
211
212 // Does the expanded pair of binops simplify to the existing binop?
213 if ((L == B0 && R == B1) ||
214 (Instruction::isCommutative(OpcodeToExpand) && L == B1 && R == B0)) {
215 ++NumExpand;
216 return B;
217 }
218
219 // Otherwise, return "L op' R" if it simplifies.
220 Value *S = simplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse);
221 if (!S)
222 return nullptr;
223
224 ++NumExpand;
225 return S;
226 }
227
228 /// Try to simplify binops of form "A op (B op' C)" or the commuted variant by
229 /// distributing op over op'.
expandCommutativeBinOp(Instruction::BinaryOps Opcode,Value * L,Value * R,Instruction::BinaryOps OpcodeToExpand,const SimplifyQuery & Q,unsigned MaxRecurse)230 static Value *expandCommutativeBinOp(Instruction::BinaryOps Opcode, Value *L,
231 Value *R,
232 Instruction::BinaryOps OpcodeToExpand,
233 const SimplifyQuery &Q,
234 unsigned MaxRecurse) {
235 // Recursion is always used, so bail out at once if we already hit the limit.
236 if (!MaxRecurse--)
237 return nullptr;
238
239 if (Value *V = expandBinOp(Opcode, L, R, OpcodeToExpand, Q, MaxRecurse))
240 return V;
241 if (Value *V = expandBinOp(Opcode, R, L, OpcodeToExpand, Q, MaxRecurse))
242 return V;
243 return nullptr;
244 }
245
246 /// Generic simplifications for associative binary operations.
247 /// Returns the simpler value, or null if none was found.
simplifyAssociativeBinOp(Instruction::BinaryOps Opcode,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)248 static Value *simplifyAssociativeBinOp(Instruction::BinaryOps Opcode,
249 Value *LHS, Value *RHS,
250 const SimplifyQuery &Q,
251 unsigned MaxRecurse) {
252 assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
253
254 // Recursion is always used, so bail out at once if we already hit the limit.
255 if (!MaxRecurse--)
256 return nullptr;
257
258 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
259 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
260
261 // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
262 if (Op0 && Op0->getOpcode() == Opcode) {
263 Value *A = Op0->getOperand(0);
264 Value *B = Op0->getOperand(1);
265 Value *C = RHS;
266
267 // Does "B op C" simplify?
268 if (Value *V = simplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
269 // It does! Return "A op V" if it simplifies or is already available.
270 // If V equals B then "A op V" is just the LHS.
271 if (V == B)
272 return LHS;
273 // Otherwise return "A op V" if it simplifies.
274 if (Value *W = simplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
275 ++NumReassoc;
276 return W;
277 }
278 }
279 }
280
281 // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
282 if (Op1 && Op1->getOpcode() == Opcode) {
283 Value *A = LHS;
284 Value *B = Op1->getOperand(0);
285 Value *C = Op1->getOperand(1);
286
287 // Does "A op B" simplify?
288 if (Value *V = simplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
289 // It does! Return "V op C" if it simplifies or is already available.
290 // If V equals B then "V op C" is just the RHS.
291 if (V == B)
292 return RHS;
293 // Otherwise return "V op C" if it simplifies.
294 if (Value *W = simplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
295 ++NumReassoc;
296 return W;
297 }
298 }
299 }
300
301 // The remaining transforms require commutativity as well as associativity.
302 if (!Instruction::isCommutative(Opcode))
303 return nullptr;
304
305 // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
306 if (Op0 && Op0->getOpcode() == Opcode) {
307 Value *A = Op0->getOperand(0);
308 Value *B = Op0->getOperand(1);
309 Value *C = RHS;
310
311 // Does "C op A" simplify?
312 if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
313 // It does! Return "V op B" if it simplifies or is already available.
314 // If V equals A then "V op B" is just the LHS.
315 if (V == A)
316 return LHS;
317 // Otherwise return "V op B" if it simplifies.
318 if (Value *W = simplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
319 ++NumReassoc;
320 return W;
321 }
322 }
323 }
324
325 // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
326 if (Op1 && Op1->getOpcode() == Opcode) {
327 Value *A = LHS;
328 Value *B = Op1->getOperand(0);
329 Value *C = Op1->getOperand(1);
330
331 // Does "C op A" simplify?
332 if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
333 // It does! Return "B op V" if it simplifies or is already available.
334 // If V equals C then "B op V" is just the RHS.
335 if (V == C)
336 return RHS;
337 // Otherwise return "B op V" if it simplifies.
338 if (Value *W = simplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
339 ++NumReassoc;
340 return W;
341 }
342 }
343 }
344
345 return nullptr;
346 }
347
348 /// In the case of a binary operation with a select instruction as an operand,
349 /// try to simplify the binop by seeing whether evaluating it on both branches
350 /// of the select results in the same value. Returns the common value if so,
351 /// otherwise returns null.
threadBinOpOverSelect(Instruction::BinaryOps Opcode,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)352 static Value *threadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS,
353 Value *RHS, const SimplifyQuery &Q,
354 unsigned MaxRecurse) {
355 // Recursion is always used, so bail out at once if we already hit the limit.
356 if (!MaxRecurse--)
357 return nullptr;
358
359 SelectInst *SI;
360 if (isa<SelectInst>(LHS)) {
361 SI = cast<SelectInst>(LHS);
362 } else {
363 assert(isa<SelectInst>(RHS) && "No select instruction operand!");
364 SI = cast<SelectInst>(RHS);
365 }
366
367 // Evaluate the BinOp on the true and false branches of the select.
368 Value *TV;
369 Value *FV;
370 if (SI == LHS) {
371 TV = simplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
372 FV = simplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
373 } else {
374 TV = simplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
375 FV = simplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
376 }
377
378 // If they simplified to the same value, then return the common value.
379 // If they both failed to simplify then return null.
380 if (TV == FV)
381 return TV;
382
383 // If one branch simplified to undef, return the other one.
384 if (TV && Q.isUndefValue(TV))
385 return FV;
386 if (FV && Q.isUndefValue(FV))
387 return TV;
388
389 // If applying the operation did not change the true and false select values,
390 // then the result of the binop is the select itself.
391 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
392 return SI;
393
394 // If one branch simplified and the other did not, and the simplified
395 // value is equal to the unsimplified one, return the simplified value.
396 // For example, select (cond, X, X & Z) & Z -> X & Z.
397 if ((FV && !TV) || (TV && !FV)) {
398 // Check that the simplified value has the form "X op Y" where "op" is the
399 // same as the original operation.
400 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
401 if (Simplified && Simplified->getOpcode() == unsigned(Opcode) &&
402 !Simplified->hasPoisonGeneratingFlags()) {
403 // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
404 // We already know that "op" is the same as for the simplified value. See
405 // if the operands match too. If so, return the simplified value.
406 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
407 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
408 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
409 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
410 Simplified->getOperand(1) == UnsimplifiedRHS)
411 return Simplified;
412 if (Simplified->isCommutative() &&
413 Simplified->getOperand(1) == UnsimplifiedLHS &&
414 Simplified->getOperand(0) == UnsimplifiedRHS)
415 return Simplified;
416 }
417 }
418
419 return nullptr;
420 }
421
422 /// In the case of a comparison with a select instruction, try to simplify the
423 /// comparison by seeing whether both branches of the select result in the same
424 /// value. Returns the common value if so, otherwise returns null.
425 /// For example, if we have:
426 /// %tmp = select i1 %cmp, i32 1, i32 2
427 /// %cmp1 = icmp sle i32 %tmp, 3
428 /// We can simplify %cmp1 to true, because both branches of select are
429 /// less than 3. We compose new comparison by substituting %tmp with both
430 /// branches of select and see if it can be simplified.
threadCmpOverSelect(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)431 static Value *threadCmpOverSelect(CmpPredicate Pred, Value *LHS, Value *RHS,
432 const SimplifyQuery &Q, unsigned MaxRecurse) {
433 // Recursion is always used, so bail out at once if we already hit the limit.
434 if (!MaxRecurse--)
435 return nullptr;
436
437 // Make sure the select is on the LHS.
438 if (!isa<SelectInst>(LHS)) {
439 std::swap(LHS, RHS);
440 Pred = CmpInst::getSwappedPredicate(Pred);
441 }
442 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
443 SelectInst *SI = cast<SelectInst>(LHS);
444 Value *Cond = SI->getCondition();
445 Value *TV = SI->getTrueValue();
446 Value *FV = SI->getFalseValue();
447
448 // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
449 // Does "cmp TV, RHS" simplify?
450 Value *TCmp = simplifyCmpSelTrueCase(Pred, TV, RHS, Cond, Q, MaxRecurse);
451 if (!TCmp)
452 return nullptr;
453
454 // Does "cmp FV, RHS" simplify?
455 Value *FCmp = simplifyCmpSelFalseCase(Pred, FV, RHS, Cond, Q, MaxRecurse);
456 if (!FCmp)
457 return nullptr;
458
459 // If both sides simplified to the same value, then use it as the result of
460 // the original comparison.
461 if (TCmp == FCmp)
462 return TCmp;
463
464 // The remaining cases only make sense if the select condition has the same
465 // type as the result of the comparison, so bail out if this is not so.
466 if (Cond->getType()->isVectorTy() == RHS->getType()->isVectorTy())
467 return handleOtherCmpSelSimplifications(TCmp, FCmp, Cond, Q, MaxRecurse);
468
469 return nullptr;
470 }
471
472 /// In the case of a binary operation with an operand that is a PHI instruction,
473 /// try to simplify the binop by seeing whether evaluating it on the incoming
474 /// phi values yields the same result for every value. If so returns the common
475 /// value, otherwise returns null.
threadBinOpOverPHI(Instruction::BinaryOps Opcode,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)476 static Value *threadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS,
477 Value *RHS, const SimplifyQuery &Q,
478 unsigned MaxRecurse) {
479 // Recursion is always used, so bail out at once if we already hit the limit.
480 if (!MaxRecurse--)
481 return nullptr;
482
483 PHINode *PI;
484 if (isa<PHINode>(LHS)) {
485 PI = cast<PHINode>(LHS);
486 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
487 if (!valueDominatesPHI(RHS, PI, Q.DT))
488 return nullptr;
489 } else {
490 assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
491 PI = cast<PHINode>(RHS);
492 // Bail out if LHS and the phi may be mutually interdependent due to a loop.
493 if (!valueDominatesPHI(LHS, PI, Q.DT))
494 return nullptr;
495 }
496
497 // Evaluate the BinOp on the incoming phi values.
498 Value *CommonValue = nullptr;
499 for (Use &Incoming : PI->incoming_values()) {
500 // If the incoming value is the phi node itself, it can safely be skipped.
501 if (Incoming == PI)
502 continue;
503 Instruction *InTI = PI->getIncomingBlock(Incoming)->getTerminator();
504 Value *V = PI == LHS
505 ? simplifyBinOp(Opcode, Incoming, RHS,
506 Q.getWithInstruction(InTI), MaxRecurse)
507 : simplifyBinOp(Opcode, LHS, Incoming,
508 Q.getWithInstruction(InTI), MaxRecurse);
509 // If the operation failed to simplify, or simplified to a different value
510 // to previously, then give up.
511 if (!V || (CommonValue && V != CommonValue))
512 return nullptr;
513 CommonValue = V;
514 }
515
516 return CommonValue;
517 }
518
519 /// In the case of a comparison with a PHI instruction, try to simplify the
520 /// comparison by seeing whether comparing with all of the incoming phi values
521 /// yields the same result every time. If so returns the common result,
522 /// otherwise returns null.
threadCmpOverPHI(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)523 static Value *threadCmpOverPHI(CmpPredicate Pred, Value *LHS, Value *RHS,
524 const SimplifyQuery &Q, unsigned MaxRecurse) {
525 // Recursion is always used, so bail out at once if we already hit the limit.
526 if (!MaxRecurse--)
527 return nullptr;
528
529 // Make sure the phi is on the LHS.
530 if (!isa<PHINode>(LHS)) {
531 std::swap(LHS, RHS);
532 Pred = CmpInst::getSwappedPredicate(Pred);
533 }
534 assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
535 PHINode *PI = cast<PHINode>(LHS);
536
537 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
538 if (!valueDominatesPHI(RHS, PI, Q.DT))
539 return nullptr;
540
541 // Evaluate the BinOp on the incoming phi values.
542 Value *CommonValue = nullptr;
543 for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u) {
544 Value *Incoming = PI->getIncomingValue(u);
545 Instruction *InTI = PI->getIncomingBlock(u)->getTerminator();
546 // If the incoming value is the phi node itself, it can safely be skipped.
547 if (Incoming == PI)
548 continue;
549 // Change the context instruction to the "edge" that flows into the phi.
550 // This is important because that is where incoming is actually "evaluated"
551 // even though it is used later somewhere else.
552 Value *V = simplifyCmpInst(Pred, Incoming, RHS, Q.getWithInstruction(InTI),
553 MaxRecurse);
554 // If the operation failed to simplify, or simplified to a different value
555 // to previously, then give up.
556 if (!V || (CommonValue && V != CommonValue))
557 return nullptr;
558 CommonValue = V;
559 }
560
561 return CommonValue;
562 }
563
foldOrCommuteConstant(Instruction::BinaryOps Opcode,Value * & Op0,Value * & Op1,const SimplifyQuery & Q)564 static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode,
565 Value *&Op0, Value *&Op1,
566 const SimplifyQuery &Q) {
567 if (auto *CLHS = dyn_cast<Constant>(Op0)) {
568 if (auto *CRHS = dyn_cast<Constant>(Op1)) {
569 switch (Opcode) {
570 default:
571 break;
572 case Instruction::FAdd:
573 case Instruction::FSub:
574 case Instruction::FMul:
575 case Instruction::FDiv:
576 case Instruction::FRem:
577 if (Q.CxtI != nullptr)
578 return ConstantFoldFPInstOperands(Opcode, CLHS, CRHS, Q.DL, Q.CxtI);
579 }
580 return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL);
581 }
582
583 // Canonicalize the constant to the RHS if this is a commutative operation.
584 if (Instruction::isCommutative(Opcode))
585 std::swap(Op0, Op1);
586 }
587 return nullptr;
588 }
589
590 /// Given operands for an Add, see if we can fold the result.
591 /// If not, this returns null.
simplifyAddInst(Value * Op0,Value * Op1,bool IsNSW,bool IsNUW,const SimplifyQuery & Q,unsigned MaxRecurse)592 static Value *simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
593 const SimplifyQuery &Q, unsigned MaxRecurse) {
594 if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q))
595 return C;
596
597 // X + poison -> poison
598 if (isa<PoisonValue>(Op1))
599 return Op1;
600
601 // X + undef -> undef
602 if (Q.isUndefValue(Op1))
603 return Op1;
604
605 // X + 0 -> X
606 if (match(Op1, m_Zero()))
607 return Op0;
608
609 // If two operands are negative, return 0.
610 if (isKnownNegation(Op0, Op1))
611 return Constant::getNullValue(Op0->getType());
612
613 // X + (Y - X) -> Y
614 // (Y - X) + X -> Y
615 // Eg: X + -X -> 0
616 Value *Y = nullptr;
617 if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
618 match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
619 return Y;
620
621 // X + ~X -> -1 since ~X = -X-1
622 Type *Ty = Op0->getType();
623 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
624 return Constant::getAllOnesValue(Ty);
625
626 // add nsw/nuw (xor Y, signmask), signmask --> Y
627 // The no-wrapping add guarantees that the top bit will be set by the add.
628 // Therefore, the xor must be clearing the already set sign bit of Y.
629 if ((IsNSW || IsNUW) && match(Op1, m_SignMask()) &&
630 match(Op0, m_Xor(m_Value(Y), m_SignMask())))
631 return Y;
632
633 // add nuw %x, -1 -> -1, because %x can only be 0.
634 if (IsNUW && match(Op1, m_AllOnes()))
635 return Op1; // Which is -1.
636
637 /// i1 add -> xor.
638 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
639 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
640 return V;
641
642 // Try some generic simplifications for associative operations.
643 if (Value *V =
644 simplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, MaxRecurse))
645 return V;
646
647 // Threading Add over selects and phi nodes is pointless, so don't bother.
648 // Threading over the select in "A + select(cond, B, C)" means evaluating
649 // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
650 // only if B and C are equal. If B and C are equal then (since we assume
651 // that operands have already been simplified) "select(cond, B, C)" should
652 // have been simplified to the common value of B and C already. Analysing
653 // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly
654 // for threading over phi nodes.
655
656 return nullptr;
657 }
658
simplifyAddInst(Value * Op0,Value * Op1,bool IsNSW,bool IsNUW,const SimplifyQuery & Query)659 Value *llvm::simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
660 const SimplifyQuery &Query) {
661 return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit);
662 }
663
664 /// Compute the base pointer and cumulative constant offsets for V.
665 ///
666 /// This strips all constant offsets off of V, leaving it the base pointer, and
667 /// accumulates the total constant offset applied in the returned constant.
668 /// It returns zero if there are no constant offsets applied.
669 ///
670 /// This is very similar to stripAndAccumulateConstantOffsets(), except it
671 /// normalizes the offset bitwidth to the stripped pointer type, not the
672 /// original pointer type.
stripAndComputeConstantOffsets(const DataLayout & DL,Value * & V,bool AllowNonInbounds=false)673 static APInt stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
674 bool AllowNonInbounds = false) {
675 assert(V->getType()->isPtrOrPtrVectorTy());
676
677 APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType()));
678 V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
679 // As that strip may trace through `addrspacecast`, need to sext or trunc
680 // the offset calculated.
681 return Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(V->getType()));
682 }
683
684 /// Compute the constant difference between two pointer values.
685 /// If the difference is not a constant, returns zero.
computePointerDifference(const DataLayout & DL,Value * LHS,Value * RHS)686 static Constant *computePointerDifference(const DataLayout &DL, Value *LHS,
687 Value *RHS) {
688 APInt LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
689 APInt RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
690
691 // If LHS and RHS are not related via constant offsets to the same base
692 // value, there is nothing we can do here.
693 if (LHS != RHS)
694 return nullptr;
695
696 // Otherwise, the difference of LHS - RHS can be computed as:
697 // LHS - RHS
698 // = (LHSOffset + Base) - (RHSOffset + Base)
699 // = LHSOffset - RHSOffset
700 Constant *Res = ConstantInt::get(LHS->getContext(), LHSOffset - RHSOffset);
701 if (auto *VecTy = dyn_cast<VectorType>(LHS->getType()))
702 Res = ConstantVector::getSplat(VecTy->getElementCount(), Res);
703 return Res;
704 }
705
706 /// Test if there is a dominating equivalence condition for the
707 /// two operands. If there is, try to reduce the binary operation
708 /// between the two operands.
709 /// Example: Op0 - Op1 --> 0 when Op0 == Op1
simplifyByDomEq(unsigned Opcode,Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)710 static Value *simplifyByDomEq(unsigned Opcode, Value *Op0, Value *Op1,
711 const SimplifyQuery &Q, unsigned MaxRecurse) {
712 // Recursive run it can not get any benefit
713 if (MaxRecurse != RecursionLimit)
714 return nullptr;
715
716 std::optional<bool> Imp =
717 isImpliedByDomCondition(CmpInst::ICMP_EQ, Op0, Op1, Q.CxtI, Q.DL);
718 if (Imp && *Imp) {
719 Type *Ty = Op0->getType();
720 switch (Opcode) {
721 case Instruction::Sub:
722 case Instruction::Xor:
723 case Instruction::URem:
724 case Instruction::SRem:
725 return Constant::getNullValue(Ty);
726
727 case Instruction::SDiv:
728 case Instruction::UDiv:
729 return ConstantInt::get(Ty, 1);
730
731 case Instruction::And:
732 case Instruction::Or:
733 // Could be either one - choose Op1 since that's more likely a constant.
734 return Op1;
735 default:
736 break;
737 }
738 }
739 return nullptr;
740 }
741
742 /// Given operands for a Sub, see if we can fold the result.
743 /// If not, this returns null.
simplifySubInst(Value * Op0,Value * Op1,bool IsNSW,bool IsNUW,const SimplifyQuery & Q,unsigned MaxRecurse)744 static Value *simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
745 const SimplifyQuery &Q, unsigned MaxRecurse) {
746 if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q))
747 return C;
748
749 // X - poison -> poison
750 // poison - X -> poison
751 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
752 return PoisonValue::get(Op0->getType());
753
754 // X - undef -> undef
755 // undef - X -> undef
756 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
757 return UndefValue::get(Op0->getType());
758
759 // X - 0 -> X
760 if (match(Op1, m_Zero()))
761 return Op0;
762
763 // X - X -> 0
764 if (Op0 == Op1)
765 return Constant::getNullValue(Op0->getType());
766
767 // Is this a negation?
768 if (match(Op0, m_Zero())) {
769 // 0 - X -> 0 if the sub is NUW.
770 if (IsNUW)
771 return Constant::getNullValue(Op0->getType());
772
773 KnownBits Known = computeKnownBits(Op1, Q);
774 if (Known.Zero.isMaxSignedValue()) {
775 // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
776 // Op1 must be 0 because negating the minimum signed value is undefined.
777 if (IsNSW)
778 return Constant::getNullValue(Op0->getType());
779
780 // 0 - X -> X if X is 0 or the minimum signed value.
781 return Op1;
782 }
783 }
784
785 // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
786 // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
787 Value *X = nullptr, *Y = nullptr, *Z = Op1;
788 if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
789 // See if "V === Y - Z" simplifies.
790 if (Value *V = simplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse - 1))
791 // It does! Now see if "X + V" simplifies.
792 if (Value *W = simplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse - 1)) {
793 // It does, we successfully reassociated!
794 ++NumReassoc;
795 return W;
796 }
797 // See if "V === X - Z" simplifies.
798 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
799 // It does! Now see if "Y + V" simplifies.
800 if (Value *W = simplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse - 1)) {
801 // It does, we successfully reassociated!
802 ++NumReassoc;
803 return W;
804 }
805 }
806
807 // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
808 // For example, X - (X + 1) -> -1
809 X = Op0;
810 if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
811 // See if "V === X - Y" simplifies.
812 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
813 // It does! Now see if "V - Z" simplifies.
814 if (Value *W = simplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse - 1)) {
815 // It does, we successfully reassociated!
816 ++NumReassoc;
817 return W;
818 }
819 // See if "V === X - Z" simplifies.
820 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
821 // It does! Now see if "V - Y" simplifies.
822 if (Value *W = simplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse - 1)) {
823 // It does, we successfully reassociated!
824 ++NumReassoc;
825 return W;
826 }
827 }
828
829 // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
830 // For example, X - (X - Y) -> Y.
831 Z = Op0;
832 if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
833 // See if "V === Z - X" simplifies.
834 if (Value *V = simplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse - 1))
835 // It does! Now see if "V + Y" simplifies.
836 if (Value *W = simplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse - 1)) {
837 // It does, we successfully reassociated!
838 ++NumReassoc;
839 return W;
840 }
841
842 // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
843 if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) &&
844 match(Op1, m_Trunc(m_Value(Y))))
845 if (X->getType() == Y->getType())
846 // See if "V === X - Y" simplifies.
847 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
848 // It does! Now see if "trunc V" simplifies.
849 if (Value *W = simplifyCastInst(Instruction::Trunc, V, Op0->getType(),
850 Q, MaxRecurse - 1))
851 // It does, return the simplified "trunc V".
852 return W;
853
854 // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
855 if (match(Op0, m_PtrToInt(m_Value(X))) && match(Op1, m_PtrToInt(m_Value(Y))))
856 if (Constant *Result = computePointerDifference(Q.DL, X, Y))
857 return ConstantFoldIntegerCast(Result, Op0->getType(), /*IsSigned*/ true,
858 Q.DL);
859
860 // i1 sub -> xor.
861 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
862 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
863 return V;
864
865 // Threading Sub over selects and phi nodes is pointless, so don't bother.
866 // Threading over the select in "A - select(cond, B, C)" means evaluating
867 // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
868 // only if B and C are equal. If B and C are equal then (since we assume
869 // that operands have already been simplified) "select(cond, B, C)" should
870 // have been simplified to the common value of B and C already. Analysing
871 // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
872 // for threading over phi nodes.
873
874 if (Value *V = simplifyByDomEq(Instruction::Sub, Op0, Op1, Q, MaxRecurse))
875 return V;
876
877 // (sub nuw C_Mask, (xor X, C_Mask)) -> X
878 if (IsNUW) {
879 Value *X;
880 if (match(Op1, m_Xor(m_Value(X), m_Specific(Op0))) &&
881 match(Op0, m_LowBitMask()))
882 return X;
883 }
884
885 return nullptr;
886 }
887
simplifySubInst(Value * Op0,Value * Op1,bool IsNSW,bool IsNUW,const SimplifyQuery & Q)888 Value *llvm::simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
889 const SimplifyQuery &Q) {
890 return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
891 }
892
893 /// Given operands for a Mul, see if we can fold the result.
894 /// If not, this returns null.
simplifyMulInst(Value * Op0,Value * Op1,bool IsNSW,bool IsNUW,const SimplifyQuery & Q,unsigned MaxRecurse)895 static Value *simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
896 const SimplifyQuery &Q, unsigned MaxRecurse) {
897 if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q))
898 return C;
899
900 // X * poison -> poison
901 if (isa<PoisonValue>(Op1))
902 return Op1;
903
904 // X * undef -> 0
905 // X * 0 -> 0
906 if (Q.isUndefValue(Op1) || match(Op1, m_Zero()))
907 return Constant::getNullValue(Op0->getType());
908
909 // X * 1 -> X
910 if (match(Op1, m_One()))
911 return Op0;
912
913 // (X / Y) * Y -> X if the division is exact.
914 Value *X = nullptr;
915 if (Q.IIQ.UseInstrInfo &&
916 (match(Op0,
917 m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
918 match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))) // Y * (X / Y)
919 return X;
920
921 if (Op0->getType()->isIntOrIntVectorTy(1)) {
922 // mul i1 nsw is a special-case because -1 * -1 is poison (+1 is not
923 // representable). All other cases reduce to 0, so just return 0.
924 if (IsNSW)
925 return ConstantInt::getNullValue(Op0->getType());
926
927 // Treat "mul i1" as "and i1".
928 if (MaxRecurse)
929 if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1))
930 return V;
931 }
932
933 // Try some generic simplifications for associative operations.
934 if (Value *V =
935 simplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
936 return V;
937
938 // Mul distributes over Add. Try some generic simplifications based on this.
939 if (Value *V = expandCommutativeBinOp(Instruction::Mul, Op0, Op1,
940 Instruction::Add, Q, MaxRecurse))
941 return V;
942
943 // If the operation is with the result of a select instruction, check whether
944 // operating on either branch of the select always yields the same value.
945 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
946 if (Value *V =
947 threadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
948 return V;
949
950 // If the operation is with the result of a phi instruction, check whether
951 // operating on all incoming values of the phi always yields the same value.
952 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
953 if (Value *V =
954 threadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
955 return V;
956
957 return nullptr;
958 }
959
simplifyMulInst(Value * Op0,Value * Op1,bool IsNSW,bool IsNUW,const SimplifyQuery & Q)960 Value *llvm::simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
961 const SimplifyQuery &Q) {
962 return ::simplifyMulInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
963 }
964
965 /// Given a predicate and two operands, return true if the comparison is true.
966 /// This is a helper for div/rem simplification where we return some other value
967 /// when we can prove a relationship between the operands.
isICmpTrue(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)968 static bool isICmpTrue(CmpPredicate Pred, Value *LHS, Value *RHS,
969 const SimplifyQuery &Q, unsigned MaxRecurse) {
970 Value *V = simplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse);
971 Constant *C = dyn_cast_or_null<Constant>(V);
972 return (C && C->isAllOnesValue());
973 }
974
975 /// Return true if we can simplify X / Y to 0. Remainder can adapt that answer
976 /// to simplify X % Y to X.
isDivZero(Value * X,Value * Y,const SimplifyQuery & Q,unsigned MaxRecurse,bool IsSigned)977 static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q,
978 unsigned MaxRecurse, bool IsSigned) {
979 // Recursion is always used, so bail out at once if we already hit the limit.
980 if (!MaxRecurse--)
981 return false;
982
983 if (IsSigned) {
984 // (X srem Y) sdiv Y --> 0
985 if (match(X, m_SRem(m_Value(), m_Specific(Y))))
986 return true;
987
988 // |X| / |Y| --> 0
989 //
990 // We require that 1 operand is a simple constant. That could be extended to
991 // 2 variables if we computed the sign bit for each.
992 //
993 // Make sure that a constant is not the minimum signed value because taking
994 // the abs() of that is undefined.
995 Type *Ty = X->getType();
996 const APInt *C;
997 if (match(X, m_APInt(C)) && !C->isMinSignedValue()) {
998 // Is the variable divisor magnitude always greater than the constant
999 // dividend magnitude?
1000 // |Y| > |C| --> Y < -abs(C) or Y > abs(C)
1001 Constant *PosDividendC = ConstantInt::get(Ty, C->abs());
1002 Constant *NegDividendC = ConstantInt::get(Ty, -C->abs());
1003 if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) ||
1004 isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse))
1005 return true;
1006 }
1007 if (match(Y, m_APInt(C))) {
1008 // Special-case: we can't take the abs() of a minimum signed value. If
1009 // that's the divisor, then all we have to do is prove that the dividend
1010 // is also not the minimum signed value.
1011 if (C->isMinSignedValue())
1012 return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse);
1013
1014 // Is the variable dividend magnitude always less than the constant
1015 // divisor magnitude?
1016 // |X| < |C| --> X > -abs(C) and X < abs(C)
1017 Constant *PosDivisorC = ConstantInt::get(Ty, C->abs());
1018 Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs());
1019 if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) &&
1020 isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse))
1021 return true;
1022 }
1023 return false;
1024 }
1025
1026 // IsSigned == false.
1027
1028 // Is the unsigned dividend known to be less than a constant divisor?
1029 // TODO: Convert this (and above) to range analysis
1030 // ("computeConstantRangeIncludingKnownBits")?
1031 const APInt *C;
1032 if (match(Y, m_APInt(C)) && computeKnownBits(X, Q).getMaxValue().ult(*C))
1033 return true;
1034
1035 // Try again for any divisor:
1036 // Is the dividend unsigned less than the divisor?
1037 return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse);
1038 }
1039
1040 /// Check for common or similar folds of integer division or integer remainder.
1041 /// This applies to all 4 opcodes (sdiv/udiv/srem/urem).
simplifyDivRem(Instruction::BinaryOps Opcode,Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)1042 static Value *simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0,
1043 Value *Op1, const SimplifyQuery &Q,
1044 unsigned MaxRecurse) {
1045 bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
1046 bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
1047
1048 Type *Ty = Op0->getType();
1049
1050 // X / undef -> poison
1051 // X % undef -> poison
1052 if (Q.isUndefValue(Op1) || isa<PoisonValue>(Op1))
1053 return PoisonValue::get(Ty);
1054
1055 // X / 0 -> poison
1056 // X % 0 -> poison
1057 // We don't need to preserve faults!
1058 if (match(Op1, m_Zero()))
1059 return PoisonValue::get(Ty);
1060
1061 // poison / X -> poison
1062 // poison % X -> poison
1063 if (isa<PoisonValue>(Op0))
1064 return Op0;
1065
1066 // undef / X -> 0
1067 // undef % X -> 0
1068 if (Q.isUndefValue(Op0))
1069 return Constant::getNullValue(Ty);
1070
1071 // 0 / X -> 0
1072 // 0 % X -> 0
1073 if (match(Op0, m_Zero()))
1074 return Constant::getNullValue(Op0->getType());
1075
1076 // X / X -> 1
1077 // X % X -> 0
1078 if (Op0 == Op1)
1079 return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty);
1080
1081 KnownBits Known = computeKnownBits(Op1, Q);
1082 // X / 0 -> poison
1083 // X % 0 -> poison
1084 // If the divisor is known to be zero, just return poison. This can happen in
1085 // some cases where its provable indirectly the denominator is zero but it's
1086 // not trivially simplifiable (i.e known zero through a phi node).
1087 if (Known.isZero())
1088 return PoisonValue::get(Ty);
1089
1090 // X / 1 -> X
1091 // X % 1 -> 0
1092 // If the divisor can only be zero or one, we can't have division-by-zero
1093 // or remainder-by-zero, so assume the divisor is 1.
1094 // e.g. 1, zext (i8 X), sdiv X (Y and 1)
1095 if (Known.countMinLeadingZeros() == Known.getBitWidth() - 1)
1096 return IsDiv ? Op0 : Constant::getNullValue(Ty);
1097
1098 // If X * Y does not overflow, then:
1099 // X * Y / Y -> X
1100 // X * Y % Y -> 0
1101 Value *X;
1102 if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) {
1103 auto *Mul = cast<OverflowingBinaryOperator>(Op0);
1104 // The multiplication can't overflow if it is defined not to, or if
1105 // X == A / Y for some A.
1106 if ((IsSigned && Q.IIQ.hasNoSignedWrap(Mul)) ||
1107 (!IsSigned && Q.IIQ.hasNoUnsignedWrap(Mul)) ||
1108 (IsSigned && match(X, m_SDiv(m_Value(), m_Specific(Op1)))) ||
1109 (!IsSigned && match(X, m_UDiv(m_Value(), m_Specific(Op1))))) {
1110 return IsDiv ? X : Constant::getNullValue(Op0->getType());
1111 }
1112 }
1113
1114 if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1115 return IsDiv ? Constant::getNullValue(Op0->getType()) : Op0;
1116
1117 if (Value *V = simplifyByDomEq(Opcode, Op0, Op1, Q, MaxRecurse))
1118 return V;
1119
1120 // If the operation is with the result of a select instruction, check whether
1121 // operating on either branch of the select always yields the same value.
1122 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1123 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1124 return V;
1125
1126 // If the operation is with the result of a phi instruction, check whether
1127 // operating on all incoming values of the phi always yields the same value.
1128 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1129 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1130 return V;
1131
1132 return nullptr;
1133 }
1134
1135 /// These are simplifications common to SDiv and UDiv.
simplifyDiv(Instruction::BinaryOps Opcode,Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q,unsigned MaxRecurse)1136 static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1137 bool IsExact, const SimplifyQuery &Q,
1138 unsigned MaxRecurse) {
1139 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1140 return C;
1141
1142 if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse))
1143 return V;
1144
1145 const APInt *DivC;
1146 if (IsExact && match(Op1, m_APInt(DivC))) {
1147 // If this is an exact divide by a constant, then the dividend (Op0) must
1148 // have at least as many trailing zeros as the divisor to divide evenly. If
1149 // it has less trailing zeros, then the result must be poison.
1150 if (DivC->countr_zero()) {
1151 KnownBits KnownOp0 = computeKnownBits(Op0, Q);
1152 if (KnownOp0.countMaxTrailingZeros() < DivC->countr_zero())
1153 return PoisonValue::get(Op0->getType());
1154 }
1155
1156 // udiv exact (mul nsw X, C), C --> X
1157 // sdiv exact (mul nuw X, C), C --> X
1158 // where C is not a power of 2.
1159 Value *X;
1160 if (!DivC->isPowerOf2() &&
1161 (Opcode == Instruction::UDiv
1162 ? match(Op0, m_NSWMul(m_Value(X), m_Specific(Op1)))
1163 : match(Op0, m_NUWMul(m_Value(X), m_Specific(Op1)))))
1164 return X;
1165 }
1166
1167 return nullptr;
1168 }
1169
1170 /// These are simplifications common to SRem and URem.
simplifyRem(Instruction::BinaryOps Opcode,Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)1171 static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1172 const SimplifyQuery &Q, unsigned MaxRecurse) {
1173 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1174 return C;
1175
1176 if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse))
1177 return V;
1178
1179 // (X << Y) % X -> 0
1180 if (Q.IIQ.UseInstrInfo) {
1181 if ((Opcode == Instruction::SRem &&
1182 match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))) ||
1183 (Opcode == Instruction::URem &&
1184 match(Op0, m_NUWShl(m_Specific(Op1), m_Value()))))
1185 return Constant::getNullValue(Op0->getType());
1186
1187 const APInt *C0;
1188 if (match(Op1, m_APInt(C0))) {
1189 // (srem (mul nsw X, C1), C0) -> 0 if C1 s% C0 == 0
1190 // (urem (mul nuw X, C1), C0) -> 0 if C1 u% C0 == 0
1191 if (Opcode == Instruction::SRem
1192 ? match(Op0,
1193 m_NSWMul(m_Value(), m_CheckedInt([C0](const APInt &C) {
1194 return C.srem(*C0).isZero();
1195 })))
1196 : match(Op0,
1197 m_NUWMul(m_Value(), m_CheckedInt([C0](const APInt &C) {
1198 return C.urem(*C0).isZero();
1199 }))))
1200 return Constant::getNullValue(Op0->getType());
1201 }
1202 }
1203 return nullptr;
1204 }
1205
1206 /// Given operands for an SDiv, see if we can fold the result.
1207 /// If not, this returns null.
simplifySDivInst(Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q,unsigned MaxRecurse)1208 static Value *simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
1209 const SimplifyQuery &Q, unsigned MaxRecurse) {
1210 // If two operands are negated and no signed overflow, return -1.
1211 if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
1212 return Constant::getAllOnesValue(Op0->getType());
1213
1214 return simplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1215 }
1216
simplifySDivInst(Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q)1217 Value *llvm::simplifySDivInst(Value *Op0, Value *Op1, bool IsExact,
1218 const SimplifyQuery &Q) {
1219 return ::simplifySDivInst(Op0, Op1, IsExact, Q, RecursionLimit);
1220 }
1221
1222 /// Given operands for a UDiv, see if we can fold the result.
1223 /// If not, this returns null.
simplifyUDivInst(Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q,unsigned MaxRecurse)1224 static Value *simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact,
1225 const SimplifyQuery &Q, unsigned MaxRecurse) {
1226 return simplifyDiv(Instruction::UDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1227 }
1228
simplifyUDivInst(Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q)1229 Value *llvm::simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact,
1230 const SimplifyQuery &Q) {
1231 return ::simplifyUDivInst(Op0, Op1, IsExact, Q, RecursionLimit);
1232 }
1233
1234 /// Given operands for an SRem, see if we can fold the result.
1235 /// If not, this returns null.
simplifySRemInst(Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)1236 static Value *simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1237 unsigned MaxRecurse) {
1238 // If the divisor is 0, the result is undefined, so assume the divisor is -1.
1239 // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0
1240 Value *X;
1241 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
1242 return ConstantInt::getNullValue(Op0->getType());
1243
1244 // If the two operands are negated, return 0.
1245 if (isKnownNegation(Op0, Op1))
1246 return ConstantInt::getNullValue(Op0->getType());
1247
1248 return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1249 }
1250
simplifySRemInst(Value * Op0,Value * Op1,const SimplifyQuery & Q)1251 Value *llvm::simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1252 return ::simplifySRemInst(Op0, Op1, Q, RecursionLimit);
1253 }
1254
1255 /// Given operands for a URem, see if we can fold the result.
1256 /// If not, this returns null.
simplifyURemInst(Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)1257 static Value *simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1258 unsigned MaxRecurse) {
1259 return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1260 }
1261
simplifyURemInst(Value * Op0,Value * Op1,const SimplifyQuery & Q)1262 Value *llvm::simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1263 return ::simplifyURemInst(Op0, Op1, Q, RecursionLimit);
1264 }
1265
1266 /// Returns true if a shift by \c Amount always yields poison.
isPoisonShift(Value * Amount,const SimplifyQuery & Q)1267 static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q) {
1268 Constant *C = dyn_cast<Constant>(Amount);
1269 if (!C)
1270 return false;
1271
1272 // X shift by undef -> poison because it may shift by the bitwidth.
1273 if (Q.isUndefValue(C))
1274 return true;
1275
1276 // Shifting by the bitwidth or more is poison. This covers scalars and
1277 // fixed/scalable vectors with splat constants.
1278 const APInt *AmountC;
1279 if (match(C, m_APInt(AmountC)) && AmountC->uge(AmountC->getBitWidth()))
1280 return true;
1281
1282 // Try harder for fixed-length vectors:
1283 // If all lanes of a vector shift are poison, the whole shift is poison.
1284 if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) {
1285 for (unsigned I = 0,
1286 E = cast<FixedVectorType>(C->getType())->getNumElements();
1287 I != E; ++I)
1288 if (!isPoisonShift(C->getAggregateElement(I), Q))
1289 return false;
1290 return true;
1291 }
1292
1293 return false;
1294 }
1295
1296 /// Given operands for an Shl, LShr or AShr, see if we can fold the result.
1297 /// If not, this returns null.
simplifyShift(Instruction::BinaryOps Opcode,Value * Op0,Value * Op1,bool IsNSW,const SimplifyQuery & Q,unsigned MaxRecurse)1298 static Value *simplifyShift(Instruction::BinaryOps Opcode, Value *Op0,
1299 Value *Op1, bool IsNSW, const SimplifyQuery &Q,
1300 unsigned MaxRecurse) {
1301 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1302 return C;
1303
1304 // poison shift by X -> poison
1305 if (isa<PoisonValue>(Op0))
1306 return Op0;
1307
1308 // 0 shift by X -> 0
1309 if (match(Op0, m_Zero()))
1310 return Constant::getNullValue(Op0->getType());
1311
1312 // X shift by 0 -> X
1313 // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
1314 // would be poison.
1315 Value *X;
1316 if (match(Op1, m_Zero()) ||
1317 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
1318 return Op0;
1319
1320 // Fold undefined shifts.
1321 if (isPoisonShift(Op1, Q))
1322 return PoisonValue::get(Op0->getType());
1323
1324 // If the operation is with the result of a select instruction, check whether
1325 // operating on either branch of the select always yields the same value.
1326 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1327 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1328 return V;
1329
1330 // If the operation is with the result of a phi instruction, check whether
1331 // operating on all incoming values of the phi always yields the same value.
1332 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1333 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1334 return V;
1335
1336 // If any bits in the shift amount make that value greater than or equal to
1337 // the number of bits in the type, the shift is undefined.
1338 KnownBits KnownAmt = computeKnownBits(Op1, Q);
1339 if (KnownAmt.getMinValue().uge(KnownAmt.getBitWidth()))
1340 return PoisonValue::get(Op0->getType());
1341
1342 // If all valid bits in the shift amount are known zero, the first operand is
1343 // unchanged.
1344 unsigned NumValidShiftBits = Log2_32_Ceil(KnownAmt.getBitWidth());
1345 if (KnownAmt.countMinTrailingZeros() >= NumValidShiftBits)
1346 return Op0;
1347
1348 // Check for nsw shl leading to a poison value.
1349 if (IsNSW) {
1350 assert(Opcode == Instruction::Shl && "Expected shl for nsw instruction");
1351 KnownBits KnownVal = computeKnownBits(Op0, Q);
1352 KnownBits KnownShl = KnownBits::shl(KnownVal, KnownAmt);
1353
1354 if (KnownVal.Zero.isSignBitSet())
1355 KnownShl.Zero.setSignBit();
1356 if (KnownVal.One.isSignBitSet())
1357 KnownShl.One.setSignBit();
1358
1359 if (KnownShl.hasConflict())
1360 return PoisonValue::get(Op0->getType());
1361 }
1362
1363 return nullptr;
1364 }
1365
1366 /// Given operands for an LShr or AShr, see if we can fold the result. If not,
1367 /// this returns null.
simplifyRightShift(Instruction::BinaryOps Opcode,Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q,unsigned MaxRecurse)1368 static Value *simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0,
1369 Value *Op1, bool IsExact,
1370 const SimplifyQuery &Q, unsigned MaxRecurse) {
1371 if (Value *V =
1372 simplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse))
1373 return V;
1374
1375 // X >> X -> 0
1376 if (Op0 == Op1)
1377 return Constant::getNullValue(Op0->getType());
1378
1379 // undef >> X -> 0
1380 // undef >> X -> undef (if it's exact)
1381 if (Q.isUndefValue(Op0))
1382 return IsExact ? Op0 : Constant::getNullValue(Op0->getType());
1383
1384 // The low bit cannot be shifted out of an exact shift if it is set.
1385 // TODO: Generalize by counting trailing zeros (see fold for exact division).
1386 if (IsExact) {
1387 KnownBits Op0Known = computeKnownBits(Op0, Q);
1388 if (Op0Known.One[0])
1389 return Op0;
1390 }
1391
1392 return nullptr;
1393 }
1394
1395 /// Given operands for an Shl, see if we can fold the result.
1396 /// If not, this returns null.
simplifyShlInst(Value * Op0,Value * Op1,bool IsNSW,bool IsNUW,const SimplifyQuery & Q,unsigned MaxRecurse)1397 static Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
1398 const SimplifyQuery &Q, unsigned MaxRecurse) {
1399 if (Value *V =
1400 simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
1401 return V;
1402
1403 Type *Ty = Op0->getType();
1404 // undef << X -> 0
1405 // undef << X -> undef if (if it's NSW/NUW)
1406 if (Q.isUndefValue(Op0))
1407 return IsNSW || IsNUW ? Op0 : Constant::getNullValue(Ty);
1408
1409 // (X >> A) << A -> X
1410 Value *X;
1411 if (Q.IIQ.UseInstrInfo &&
1412 match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
1413 return X;
1414
1415 // shl nuw i8 C, %x -> C iff C has sign bit set.
1416 if (IsNUW && match(Op0, m_Negative()))
1417 return Op0;
1418 // NOTE: could use computeKnownBits() / LazyValueInfo,
1419 // but the cost-benefit analysis suggests it isn't worth it.
1420
1421 // "nuw" guarantees that only zeros are shifted out, and "nsw" guarantees
1422 // that the sign-bit does not change, so the only input that does not
1423 // produce poison is 0, and "0 << (bitwidth-1) --> 0".
1424 if (IsNSW && IsNUW &&
1425 match(Op1, m_SpecificInt(Ty->getScalarSizeInBits() - 1)))
1426 return Constant::getNullValue(Ty);
1427
1428 return nullptr;
1429 }
1430
simplifyShlInst(Value * Op0,Value * Op1,bool IsNSW,bool IsNUW,const SimplifyQuery & Q)1431 Value *llvm::simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
1432 const SimplifyQuery &Q) {
1433 return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit);
1434 }
1435
1436 /// Given operands for an LShr, see if we can fold the result.
1437 /// If not, this returns null.
simplifyLShrInst(Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q,unsigned MaxRecurse)1438 static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
1439 const SimplifyQuery &Q, unsigned MaxRecurse) {
1440 if (Value *V = simplifyRightShift(Instruction::LShr, Op0, Op1, IsExact, Q,
1441 MaxRecurse))
1442 return V;
1443
1444 // (X << A) >> A -> X
1445 Value *X;
1446 if (Q.IIQ.UseInstrInfo && match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
1447 return X;
1448
1449 // ((X << A) | Y) >> A -> X if effective width of Y is not larger than A.
1450 // We can return X as we do in the above case since OR alters no bits in X.
1451 // SimplifyDemandedBits in InstCombine can do more general optimization for
1452 // bit manipulation. This pattern aims to provide opportunities for other
1453 // optimizers by supporting a simple but common case in InstSimplify.
1454 Value *Y;
1455 const APInt *ShRAmt, *ShLAmt;
1456 if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(ShRAmt)) &&
1457 match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShLAmt)), m_Value(Y))) &&
1458 *ShRAmt == *ShLAmt) {
1459 const KnownBits YKnown = computeKnownBits(Y, Q);
1460 const unsigned EffWidthY = YKnown.countMaxActiveBits();
1461 if (ShRAmt->uge(EffWidthY))
1462 return X;
1463 }
1464
1465 return nullptr;
1466 }
1467
simplifyLShrInst(Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q)1468 Value *llvm::simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
1469 const SimplifyQuery &Q) {
1470 return ::simplifyLShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
1471 }
1472
1473 /// Given operands for an AShr, see if we can fold the result.
1474 /// If not, this returns null.
simplifyAShrInst(Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q,unsigned MaxRecurse)1475 static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
1476 const SimplifyQuery &Q, unsigned MaxRecurse) {
1477 if (Value *V = simplifyRightShift(Instruction::AShr, Op0, Op1, IsExact, Q,
1478 MaxRecurse))
1479 return V;
1480
1481 // -1 >>a X --> -1
1482 // (-1 << X) a>> X --> -1
1483 // We could return the original -1 constant to preserve poison elements.
1484 if (match(Op0, m_AllOnes()) ||
1485 match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1))))
1486 return Constant::getAllOnesValue(Op0->getType());
1487
1488 // (X << A) >> A -> X
1489 Value *X;
1490 if (Q.IIQ.UseInstrInfo && match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1))))
1491 return X;
1492
1493 // Arithmetic shifting an all-sign-bit value is a no-op.
1494 unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, Q.AC, Q.CxtI, Q.DT);
1495 if (NumSignBits == Op0->getType()->getScalarSizeInBits())
1496 return Op0;
1497
1498 return nullptr;
1499 }
1500
simplifyAShrInst(Value * Op0,Value * Op1,bool IsExact,const SimplifyQuery & Q)1501 Value *llvm::simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
1502 const SimplifyQuery &Q) {
1503 return ::simplifyAShrInst(Op0, Op1, IsExact, Q, RecursionLimit);
1504 }
1505
1506 /// Commuted variants are assumed to be handled by calling this function again
1507 /// with the parameters swapped.
simplifyUnsignedRangeCheck(ICmpInst * ZeroICmp,ICmpInst * UnsignedICmp,bool IsAnd,const SimplifyQuery & Q)1508 static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp,
1509 ICmpInst *UnsignedICmp, bool IsAnd,
1510 const SimplifyQuery &Q) {
1511 Value *X, *Y;
1512
1513 CmpPredicate EqPred;
1514 if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) ||
1515 !ICmpInst::isEquality(EqPred))
1516 return nullptr;
1517
1518 CmpPredicate UnsignedPred;
1519
1520 Value *A, *B;
1521 // Y = (A - B);
1522 if (match(Y, m_Sub(m_Value(A), m_Value(B)))) {
1523 if (match(UnsignedICmp,
1524 m_c_ICmp(UnsignedPred, m_Specific(A), m_Specific(B))) &&
1525 ICmpInst::isUnsigned(UnsignedPred)) {
1526 // A >=/<= B || (A - B) != 0 <--> true
1527 if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1528 UnsignedPred == ICmpInst::ICMP_ULE) &&
1529 EqPred == ICmpInst::ICMP_NE && !IsAnd)
1530 return ConstantInt::getTrue(UnsignedICmp->getType());
1531 // A </> B && (A - B) == 0 <--> false
1532 if ((UnsignedPred == ICmpInst::ICMP_ULT ||
1533 UnsignedPred == ICmpInst::ICMP_UGT) &&
1534 EqPred == ICmpInst::ICMP_EQ && IsAnd)
1535 return ConstantInt::getFalse(UnsignedICmp->getType());
1536
1537 // A </> B && (A - B) != 0 <--> A </> B
1538 // A </> B || (A - B) != 0 <--> (A - B) != 0
1539 if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
1540 UnsignedPred == ICmpInst::ICMP_UGT))
1541 return IsAnd ? UnsignedICmp : ZeroICmp;
1542
1543 // A <=/>= B && (A - B) == 0 <--> (A - B) == 0
1544 // A <=/>= B || (A - B) == 0 <--> A <=/>= B
1545 if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
1546 UnsignedPred == ICmpInst::ICMP_UGE))
1547 return IsAnd ? ZeroICmp : UnsignedICmp;
1548 }
1549
1550 // Given Y = (A - B)
1551 // Y >= A && Y != 0 --> Y >= A iff B != 0
1552 // Y < A || Y == 0 --> Y < A iff B != 0
1553 if (match(UnsignedICmp,
1554 m_c_ICmp(UnsignedPred, m_Specific(Y), m_Specific(A)))) {
1555 if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
1556 EqPred == ICmpInst::ICMP_NE && isKnownNonZero(B, Q))
1557 return UnsignedICmp;
1558 if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
1559 EqPred == ICmpInst::ICMP_EQ && isKnownNonZero(B, Q))
1560 return UnsignedICmp;
1561 }
1562 }
1563
1564 if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) &&
1565 ICmpInst::isUnsigned(UnsignedPred))
1566 ;
1567 else if (match(UnsignedICmp,
1568 m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) &&
1569 ICmpInst::isUnsigned(UnsignedPred))
1570 UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1571 else
1572 return nullptr;
1573
1574 // X > Y && Y == 0 --> Y == 0 iff X != 0
1575 // X > Y || Y == 0 --> X > Y iff X != 0
1576 if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1577 isKnownNonZero(X, Q))
1578 return IsAnd ? ZeroICmp : UnsignedICmp;
1579
1580 // X <= Y && Y != 0 --> X <= Y iff X != 0
1581 // X <= Y || Y != 0 --> Y != 0 iff X != 0
1582 if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1583 isKnownNonZero(X, Q))
1584 return IsAnd ? UnsignedICmp : ZeroICmp;
1585
1586 // The transforms below here are expected to be handled more generally with
1587 // simplifyAndOrOfICmpsWithLimitConst() or in InstCombine's
1588 // foldAndOrOfICmpsWithConstEq(). If we are looking to trim optimizer overlap,
1589 // these are candidates for removal.
1590
1591 // X < Y && Y != 0 --> X < Y
1592 // X < Y || Y != 0 --> Y != 0
1593 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1594 return IsAnd ? UnsignedICmp : ZeroICmp;
1595
1596 // X >= Y && Y == 0 --> Y == 0
1597 // X >= Y || Y == 0 --> X >= Y
1598 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
1599 return IsAnd ? ZeroICmp : UnsignedICmp;
1600
1601 // X < Y && Y == 0 --> false
1602 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1603 IsAnd)
1604 return getFalse(UnsignedICmp->getType());
1605
1606 // X >= Y || Y != 0 --> true
1607 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
1608 !IsAnd)
1609 return getTrue(UnsignedICmp->getType());
1610
1611 return nullptr;
1612 }
1613
1614 /// Test if a pair of compares with a shared operand and 2 constants has an
1615 /// empty set intersection, full set union, or if one compare is a superset of
1616 /// the other.
simplifyAndOrOfICmpsWithConstants(ICmpInst * Cmp0,ICmpInst * Cmp1,bool IsAnd)1617 static Value *simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1,
1618 bool IsAnd) {
1619 // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)).
1620 if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
1621 return nullptr;
1622
1623 const APInt *C0, *C1;
1624 if (!match(Cmp0->getOperand(1), m_APInt(C0)) ||
1625 !match(Cmp1->getOperand(1), m_APInt(C1)))
1626 return nullptr;
1627
1628 auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0);
1629 auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1);
1630
1631 // For and-of-compares, check if the intersection is empty:
1632 // (icmp X, C0) && (icmp X, C1) --> empty set --> false
1633 if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1634 return getFalse(Cmp0->getType());
1635
1636 // For or-of-compares, check if the union is full:
1637 // (icmp X, C0) || (icmp X, C1) --> full set --> true
1638 if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1639 return getTrue(Cmp0->getType());
1640
1641 // Is one range a superset of the other?
1642 // If this is and-of-compares, take the smaller set:
1643 // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42
1644 // If this is or-of-compares, take the larger set:
1645 // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4
1646 if (Range0.contains(Range1))
1647 return IsAnd ? Cmp1 : Cmp0;
1648 if (Range1.contains(Range0))
1649 return IsAnd ? Cmp0 : Cmp1;
1650
1651 return nullptr;
1652 }
1653
simplifyAndOfICmpsWithAdd(ICmpInst * Op0,ICmpInst * Op1,const InstrInfoQuery & IIQ)1654 static Value *simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
1655 const InstrInfoQuery &IIQ) {
1656 // (icmp (add V, C0), C1) & (icmp V, C0)
1657 CmpPredicate Pred0, Pred1;
1658 const APInt *C0, *C1;
1659 Value *V;
1660 if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1661 return nullptr;
1662
1663 if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1664 return nullptr;
1665
1666 auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0));
1667 if (AddInst->getOperand(1) != Op1->getOperand(1))
1668 return nullptr;
1669
1670 Type *ITy = Op0->getType();
1671 bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
1672 bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);
1673
1674 const APInt Delta = *C1 - *C0;
1675 if (C0->isStrictlyPositive()) {
1676 if (Delta == 2) {
1677 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1678 return getFalse(ITy);
1679 if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1680 return getFalse(ITy);
1681 }
1682 if (Delta == 1) {
1683 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1684 return getFalse(ITy);
1685 if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1686 return getFalse(ITy);
1687 }
1688 }
1689 if (C0->getBoolValue() && IsNUW) {
1690 if (Delta == 2)
1691 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1692 return getFalse(ITy);
1693 if (Delta == 1)
1694 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1695 return getFalse(ITy);
1696 }
1697
1698 return nullptr;
1699 }
1700
1701 /// Try to simplify and/or of icmp with ctpop intrinsic.
simplifyAndOrOfICmpsWithCtpop(ICmpInst * Cmp0,ICmpInst * Cmp1,bool IsAnd)1702 static Value *simplifyAndOrOfICmpsWithCtpop(ICmpInst *Cmp0, ICmpInst *Cmp1,
1703 bool IsAnd) {
1704 CmpPredicate Pred0, Pred1;
1705 Value *X;
1706 const APInt *C;
1707 if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
1708 m_APInt(C))) ||
1709 !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())) || C->isZero())
1710 return nullptr;
1711
1712 // (ctpop(X) == C) || (X != 0) --> X != 0 where C > 0
1713 if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE)
1714 return Cmp1;
1715 // (ctpop(X) != C) && (X == 0) --> X == 0 where C > 0
1716 if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ)
1717 return Cmp1;
1718
1719 return nullptr;
1720 }
1721
simplifyAndOfICmps(ICmpInst * Op0,ICmpInst * Op1,const SimplifyQuery & Q)1722 static Value *simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1,
1723 const SimplifyQuery &Q) {
1724 if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true, Q))
1725 return X;
1726 if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true, Q))
1727 return X;
1728
1729 if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true))
1730 return X;
1731
1732 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, true))
1733 return X;
1734 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, true))
1735 return X;
1736
1737 if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1738 return X;
1739 if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1740 return X;
1741
1742 return nullptr;
1743 }
1744
simplifyOrOfICmpsWithAdd(ICmpInst * Op0,ICmpInst * Op1,const InstrInfoQuery & IIQ)1745 static Value *simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
1746 const InstrInfoQuery &IIQ) {
1747 // (icmp (add V, C0), C1) | (icmp V, C0)
1748 CmpPredicate Pred0, Pred1;
1749 const APInt *C0, *C1;
1750 Value *V;
1751 if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1752 return nullptr;
1753
1754 if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1755 return nullptr;
1756
1757 auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1758 if (AddInst->getOperand(1) != Op1->getOperand(1))
1759 return nullptr;
1760
1761 Type *ITy = Op0->getType();
1762 bool IsNSW = IIQ.hasNoSignedWrap(AddInst);
1763 bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst);
1764
1765 const APInt Delta = *C1 - *C0;
1766 if (C0->isStrictlyPositive()) {
1767 if (Delta == 2) {
1768 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1769 return getTrue(ITy);
1770 if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1771 return getTrue(ITy);
1772 }
1773 if (Delta == 1) {
1774 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1775 return getTrue(ITy);
1776 if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1777 return getTrue(ITy);
1778 }
1779 }
1780 if (C0->getBoolValue() && IsNUW) {
1781 if (Delta == 2)
1782 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1783 return getTrue(ITy);
1784 if (Delta == 1)
1785 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1786 return getTrue(ITy);
1787 }
1788
1789 return nullptr;
1790 }
1791
simplifyOrOfICmps(ICmpInst * Op0,ICmpInst * Op1,const SimplifyQuery & Q)1792 static Value *simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1,
1793 const SimplifyQuery &Q) {
1794 if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false, Q))
1795 return X;
1796 if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false, Q))
1797 return X;
1798
1799 if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false))
1800 return X;
1801
1802 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, false))
1803 return X;
1804 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, false))
1805 return X;
1806
1807 if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1808 return X;
1809 if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1810 return X;
1811
1812 return nullptr;
1813 }
1814
simplifyAndOrOfFCmps(const SimplifyQuery & Q,FCmpInst * LHS,FCmpInst * RHS,bool IsAnd)1815 static Value *simplifyAndOrOfFCmps(const SimplifyQuery &Q, FCmpInst *LHS,
1816 FCmpInst *RHS, bool IsAnd) {
1817 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1818 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1819 if (LHS0->getType() != RHS0->getType())
1820 return nullptr;
1821
1822 FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1823 auto AbsOrSelfLHS0 = m_CombineOr(m_Specific(LHS0), m_FAbs(m_Specific(LHS0)));
1824 if ((PredL == FCmpInst::FCMP_ORD || PredL == FCmpInst::FCMP_UNO) &&
1825 ((FCmpInst::isOrdered(PredR) && IsAnd) ||
1826 (FCmpInst::isUnordered(PredR) && !IsAnd))) {
1827 // (fcmp ord X, 0) & (fcmp o** X/abs(X), Y) --> fcmp o** X/abs(X), Y
1828 // (fcmp uno X, 0) & (fcmp o** X/abs(X), Y) --> false
1829 // (fcmp uno X, 0) | (fcmp u** X/abs(X), Y) --> fcmp u** X/abs(X), Y
1830 // (fcmp ord X, 0) | (fcmp u** X/abs(X), Y) --> true
1831 if ((match(RHS0, AbsOrSelfLHS0) || match(RHS1, AbsOrSelfLHS0)) &&
1832 match(LHS1, m_PosZeroFP()))
1833 return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1834 ? static_cast<Value *>(RHS)
1835 : ConstantInt::getBool(LHS->getType(), !IsAnd);
1836 }
1837
1838 auto AbsOrSelfRHS0 = m_CombineOr(m_Specific(RHS0), m_FAbs(m_Specific(RHS0)));
1839 if ((PredR == FCmpInst::FCMP_ORD || PredR == FCmpInst::FCMP_UNO) &&
1840 ((FCmpInst::isOrdered(PredL) && IsAnd) ||
1841 (FCmpInst::isUnordered(PredL) && !IsAnd))) {
1842 // (fcmp o** X/abs(X), Y) & (fcmp ord X, 0) --> fcmp o** X/abs(X), Y
1843 // (fcmp o** X/abs(X), Y) & (fcmp uno X, 0) --> false
1844 // (fcmp u** X/abs(X), Y) | (fcmp uno X, 0) --> fcmp u** X/abs(X), Y
1845 // (fcmp u** X/abs(X), Y) | (fcmp ord X, 0) --> true
1846 if ((match(LHS0, AbsOrSelfRHS0) || match(LHS1, AbsOrSelfRHS0)) &&
1847 match(RHS1, m_PosZeroFP()))
1848 return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1849 ? static_cast<Value *>(LHS)
1850 : ConstantInt::getBool(LHS->getType(), !IsAnd);
1851 }
1852
1853 return nullptr;
1854 }
1855
simplifyAndOrOfCmps(const SimplifyQuery & Q,Value * Op0,Value * Op1,bool IsAnd)1856 static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0,
1857 Value *Op1, bool IsAnd) {
1858 // Look through casts of the 'and' operands to find compares.
1859 auto *Cast0 = dyn_cast<CastInst>(Op0);
1860 auto *Cast1 = dyn_cast<CastInst>(Op1);
1861 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1862 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1863 Op0 = Cast0->getOperand(0);
1864 Op1 = Cast1->getOperand(0);
1865 }
1866
1867 Value *V = nullptr;
1868 auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
1869 auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
1870 if (ICmp0 && ICmp1)
1871 V = IsAnd ? simplifyAndOfICmps(ICmp0, ICmp1, Q)
1872 : simplifyOrOfICmps(ICmp0, ICmp1, Q);
1873
1874 auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
1875 auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
1876 if (FCmp0 && FCmp1)
1877 V = simplifyAndOrOfFCmps(Q, FCmp0, FCmp1, IsAnd);
1878
1879 if (!V)
1880 return nullptr;
1881 if (!Cast0)
1882 return V;
1883
1884 // If we looked through casts, we can only handle a constant simplification
1885 // because we are not allowed to create a cast instruction here.
1886 if (auto *C = dyn_cast<Constant>(V))
1887 return ConstantFoldCastOperand(Cast0->getOpcode(), C, Cast0->getType(),
1888 Q.DL);
1889
1890 return nullptr;
1891 }
1892
1893 static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
1894 const SimplifyQuery &Q,
1895 bool AllowRefinement,
1896 SmallVectorImpl<Instruction *> *DropFlags,
1897 unsigned MaxRecurse);
1898
simplifyAndOrWithICmpEq(unsigned Opcode,Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)1899 static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1,
1900 const SimplifyQuery &Q,
1901 unsigned MaxRecurse) {
1902 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1903 "Must be and/or");
1904 CmpPredicate Pred;
1905 Value *A, *B;
1906 if (!match(Op0, m_ICmp(Pred, m_Value(A), m_Value(B))) ||
1907 !ICmpInst::isEquality(Pred))
1908 return nullptr;
1909
1910 auto Simplify = [&](Value *Res) -> Value * {
1911 Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, Res->getType());
1912
1913 // and (icmp eq a, b), x implies (a==b) inside x.
1914 // or (icmp ne a, b), x implies (a==b) inside x.
1915 // If x simplifies to true/false, we can simplify the and/or.
1916 if (Pred ==
1917 (Opcode == Instruction::And ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
1918 if (Res == Absorber)
1919 return Absorber;
1920 if (Res == ConstantExpr::getBinOpIdentity(Opcode, Res->getType()))
1921 return Op0;
1922 return nullptr;
1923 }
1924
1925 // If we have and (icmp ne a, b), x and for a==b we can simplify x to false,
1926 // then we can drop the icmp, as x will already be false in the case where
1927 // the icmp is false. Similar for or and true.
1928 if (Res == Absorber)
1929 return Op1;
1930 return nullptr;
1931 };
1932
1933 // In the final case (Res == Absorber with inverted predicate), it is safe to
1934 // refine poison during simplification, but not undef. For simplicity always
1935 // disable undef-based folds here.
1936 if (Value *Res = simplifyWithOpReplaced(Op1, A, B, Q.getWithoutUndef(),
1937 /* AllowRefinement */ true,
1938 /* DropFlags */ nullptr, MaxRecurse))
1939 return Simplify(Res);
1940 if (Value *Res = simplifyWithOpReplaced(Op1, B, A, Q.getWithoutUndef(),
1941 /* AllowRefinement */ true,
1942 /* DropFlags */ nullptr, MaxRecurse))
1943 return Simplify(Res);
1944
1945 return nullptr;
1946 }
1947
1948 /// Given a bitwise logic op, check if the operands are add/sub with a common
1949 /// source value and inverted constant (identity: C - X -> ~(X + ~C)).
simplifyLogicOfAddSub(Value * Op0,Value * Op1,Instruction::BinaryOps Opcode)1950 static Value *simplifyLogicOfAddSub(Value *Op0, Value *Op1,
1951 Instruction::BinaryOps Opcode) {
1952 assert(Op0->getType() == Op1->getType() && "Mismatched binop types");
1953 assert(BinaryOperator::isBitwiseLogicOp(Opcode) && "Expected logic op");
1954 Value *X;
1955 Constant *C1, *C2;
1956 if ((match(Op0, m_Add(m_Value(X), m_Constant(C1))) &&
1957 match(Op1, m_Sub(m_Constant(C2), m_Specific(X)))) ||
1958 (match(Op1, m_Add(m_Value(X), m_Constant(C1))) &&
1959 match(Op0, m_Sub(m_Constant(C2), m_Specific(X))))) {
1960 if (ConstantExpr::getNot(C1) == C2) {
1961 // (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0
1962 // (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1
1963 // (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1
1964 Type *Ty = Op0->getType();
1965 return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
1966 : ConstantInt::getAllOnesValue(Ty);
1967 }
1968 }
1969 return nullptr;
1970 }
1971
1972 // Commutative patterns for and that will be tried with both operand orders.
simplifyAndCommutative(Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)1973 static Value *simplifyAndCommutative(Value *Op0, Value *Op1,
1974 const SimplifyQuery &Q,
1975 unsigned MaxRecurse) {
1976 // ~A & A = 0
1977 if (match(Op0, m_Not(m_Specific(Op1))))
1978 return Constant::getNullValue(Op0->getType());
1979
1980 // (A | ?) & A = A
1981 if (match(Op0, m_c_Or(m_Specific(Op1), m_Value())))
1982 return Op1;
1983
1984 // (X | ~Y) & (X | Y) --> X
1985 Value *X, *Y;
1986 if (match(Op0, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) &&
1987 match(Op1, m_c_Or(m_Specific(X), m_Specific(Y))))
1988 return X;
1989
1990 // If we have a multiplication overflow check that is being 'and'ed with a
1991 // check that one of the multipliers is not zero, we can omit the 'and', and
1992 // only keep the overflow check.
1993 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, true))
1994 return Op1;
1995
1996 // -A & A = A if A is a power of two or zero.
1997 if (match(Op0, m_Neg(m_Specific(Op1))) &&
1998 isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI, Q.DT))
1999 return Op1;
2000
2001 // This is a similar pattern used for checking if a value is a power-of-2:
2002 // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
2003 if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) &&
2004 isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI, Q.DT))
2005 return Constant::getNullValue(Op1->getType());
2006
2007 // (x << N) & ((x << M) - 1) --> 0, where x is known to be a power of 2 and
2008 // M <= N.
2009 const APInt *Shift1, *Shift2;
2010 if (match(Op0, m_Shl(m_Value(X), m_APInt(Shift1))) &&
2011 match(Op1, m_Add(m_Shl(m_Specific(X), m_APInt(Shift2)), m_AllOnes())) &&
2012 isKnownToBeAPowerOfTwo(X, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI) &&
2013 Shift1->uge(*Shift2))
2014 return Constant::getNullValue(Op0->getType());
2015
2016 if (Value *V =
2017 simplifyAndOrWithICmpEq(Instruction::And, Op0, Op1, Q, MaxRecurse))
2018 return V;
2019
2020 return nullptr;
2021 }
2022
2023 /// Given operands for an And, see if we can fold the result.
2024 /// If not, this returns null.
simplifyAndInst(Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)2025 static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2026 unsigned MaxRecurse) {
2027 if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q))
2028 return C;
2029
2030 // X & poison -> poison
2031 if (isa<PoisonValue>(Op1))
2032 return Op1;
2033
2034 // X & undef -> 0
2035 if (Q.isUndefValue(Op1))
2036 return Constant::getNullValue(Op0->getType());
2037
2038 // X & X = X
2039 if (Op0 == Op1)
2040 return Op0;
2041
2042 // X & 0 = 0
2043 if (match(Op1, m_Zero()))
2044 return Constant::getNullValue(Op0->getType());
2045
2046 // X & -1 = X
2047 if (match(Op1, m_AllOnes()))
2048 return Op0;
2049
2050 if (Value *Res = simplifyAndCommutative(Op0, Op1, Q, MaxRecurse))
2051 return Res;
2052 if (Value *Res = simplifyAndCommutative(Op1, Op0, Q, MaxRecurse))
2053 return Res;
2054
2055 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::And))
2056 return V;
2057
2058 // A mask that only clears known zeros of a shifted value is a no-op.
2059 const APInt *Mask;
2060 const APInt *ShAmt;
2061 Value *X, *Y;
2062 if (match(Op1, m_APInt(Mask))) {
2063 // If all bits in the inverted and shifted mask are clear:
2064 // and (shl X, ShAmt), Mask --> shl X, ShAmt
2065 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) &&
2066 (~(*Mask)).lshr(*ShAmt).isZero())
2067 return Op0;
2068
2069 // If all bits in the inverted and shifted mask are clear:
2070 // and (lshr X, ShAmt), Mask --> lshr X, ShAmt
2071 if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
2072 (~(*Mask)).shl(*ShAmt).isZero())
2073 return Op0;
2074 }
2075
2076 // and 2^x-1, 2^C --> 0 where x <= C.
2077 const APInt *PowerC;
2078 Value *Shift;
2079 if (match(Op1, m_Power2(PowerC)) &&
2080 match(Op0, m_Add(m_Value(Shift), m_AllOnes())) &&
2081 isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ false, Q.AC, Q.CxtI,
2082 Q.DT)) {
2083 KnownBits Known = computeKnownBits(Shift, Q);
2084 // Use getActiveBits() to make use of the additional power of two knowledge
2085 if (PowerC->getActiveBits() >= Known.getMaxValue().getActiveBits())
2086 return ConstantInt::getNullValue(Op1->getType());
2087 }
2088
2089 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true))
2090 return V;
2091
2092 // Try some generic simplifications for associative operations.
2093 if (Value *V =
2094 simplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, MaxRecurse))
2095 return V;
2096
2097 // And distributes over Or. Try some generic simplifications based on this.
2098 if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2099 Instruction::Or, Q, MaxRecurse))
2100 return V;
2101
2102 // And distributes over Xor. Try some generic simplifications based on this.
2103 if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2104 Instruction::Xor, Q, MaxRecurse))
2105 return V;
2106
2107 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2108 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2109 // A & (A && B) -> A && B
2110 if (match(Op1, m_Select(m_Specific(Op0), m_Value(), m_Zero())))
2111 return Op1;
2112 else if (match(Op0, m_Select(m_Specific(Op1), m_Value(), m_Zero())))
2113 return Op0;
2114 }
2115 // If the operation is with the result of a select instruction, check
2116 // whether operating on either branch of the select always yields the same
2117 // value.
2118 if (Value *V =
2119 threadBinOpOverSelect(Instruction::And, Op0, Op1, Q, MaxRecurse))
2120 return V;
2121 }
2122
2123 // If the operation is with the result of a phi instruction, check whether
2124 // operating on all incoming values of the phi always yields the same value.
2125 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2126 if (Value *V =
2127 threadBinOpOverPHI(Instruction::And, Op0, Op1, Q, MaxRecurse))
2128 return V;
2129
2130 // Assuming the effective width of Y is not larger than A, i.e. all bits
2131 // from X and Y are disjoint in (X << A) | Y,
2132 // if the mask of this AND op covers all bits of X or Y, while it covers
2133 // no bits from the other, we can bypass this AND op. E.g.,
2134 // ((X << A) | Y) & Mask -> Y,
2135 // if Mask = ((1 << effective_width_of(Y)) - 1)
2136 // ((X << A) | Y) & Mask -> X << A,
2137 // if Mask = ((1 << effective_width_of(X)) - 1) << A
2138 // SimplifyDemandedBits in InstCombine can optimize the general case.
2139 // This pattern aims to help other passes for a common case.
2140 Value *XShifted;
2141 if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(Mask)) &&
2142 match(Op0, m_c_Or(m_CombineAnd(m_NUWShl(m_Value(X), m_APInt(ShAmt)),
2143 m_Value(XShifted)),
2144 m_Value(Y)))) {
2145 const unsigned Width = Op0->getType()->getScalarSizeInBits();
2146 const unsigned ShftCnt = ShAmt->getLimitedValue(Width);
2147 const KnownBits YKnown = computeKnownBits(Y, Q);
2148 const unsigned EffWidthY = YKnown.countMaxActiveBits();
2149 if (EffWidthY <= ShftCnt) {
2150 const KnownBits XKnown = computeKnownBits(X, Q);
2151 const unsigned EffWidthX = XKnown.countMaxActiveBits();
2152 const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY);
2153 const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt;
2154 // If the mask is extracting all bits from X or Y as is, we can skip
2155 // this AND op.
2156 if (EffBitsY.isSubsetOf(*Mask) && !EffBitsX.intersects(*Mask))
2157 return Y;
2158 if (EffBitsX.isSubsetOf(*Mask) && !EffBitsY.intersects(*Mask))
2159 return XShifted;
2160 }
2161 }
2162
2163 // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
2164 // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
2165 BinaryOperator *Or;
2166 if (match(Op0, m_c_Xor(m_Value(X),
2167 m_CombineAnd(m_BinOp(Or),
2168 m_c_Or(m_Deferred(X), m_Value(Y))))) &&
2169 match(Op1, m_c_Xor(m_Specific(Or), m_Specific(Y))))
2170 return Constant::getNullValue(Op0->getType());
2171
2172 const APInt *C1;
2173 Value *A;
2174 // (A ^ C) & (A ^ ~C) -> 0
2175 if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2176 match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2177 return Constant::getNullValue(Op0->getType());
2178
2179 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2180 if (std::optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL)) {
2181 // If Op0 is true implies Op1 is true, then Op0 is a subset of Op1.
2182 if (*Implied == true)
2183 return Op0;
2184 // If Op0 is true implies Op1 is false, then they are not true together.
2185 if (*Implied == false)
2186 return ConstantInt::getFalse(Op0->getType());
2187 }
2188 if (std::optional<bool> Implied = isImpliedCondition(Op1, Op0, Q.DL)) {
2189 // If Op1 is true implies Op0 is true, then Op1 is a subset of Op0.
2190 if (*Implied)
2191 return Op1;
2192 // If Op1 is true implies Op0 is false, then they are not true together.
2193 if (!*Implied)
2194 return ConstantInt::getFalse(Op1->getType());
2195 }
2196 }
2197
2198 if (Value *V = simplifyByDomEq(Instruction::And, Op0, Op1, Q, MaxRecurse))
2199 return V;
2200
2201 return nullptr;
2202 }
2203
simplifyAndInst(Value * Op0,Value * Op1,const SimplifyQuery & Q)2204 Value *llvm::simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2205 return ::simplifyAndInst(Op0, Op1, Q, RecursionLimit);
2206 }
2207
2208 // TODO: Many of these folds could use LogicalAnd/LogicalOr.
simplifyOrLogic(Value * X,Value * Y)2209 static Value *simplifyOrLogic(Value *X, Value *Y) {
2210 assert(X->getType() == Y->getType() && "Expected same type for 'or' ops");
2211 Type *Ty = X->getType();
2212
2213 // X | ~X --> -1
2214 if (match(Y, m_Not(m_Specific(X))))
2215 return ConstantInt::getAllOnesValue(Ty);
2216
2217 // X | ~(X & ?) = -1
2218 if (match(Y, m_Not(m_c_And(m_Specific(X), m_Value()))))
2219 return ConstantInt::getAllOnesValue(Ty);
2220
2221 // X | (X & ?) --> X
2222 if (match(Y, m_c_And(m_Specific(X), m_Value())))
2223 return X;
2224
2225 Value *A, *B;
2226
2227 // (A ^ B) | (A | B) --> A | B
2228 // (A ^ B) | (B | A) --> B | A
2229 if (match(X, m_Xor(m_Value(A), m_Value(B))) &&
2230 match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2231 return Y;
2232
2233 // ~(A ^ B) | (A | B) --> -1
2234 // ~(A ^ B) | (B | A) --> -1
2235 if (match(X, m_Not(m_Xor(m_Value(A), m_Value(B)))) &&
2236 match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2237 return ConstantInt::getAllOnesValue(Ty);
2238
2239 // (A & ~B) | (A ^ B) --> A ^ B
2240 // (~B & A) | (A ^ B) --> A ^ B
2241 // (A & ~B) | (B ^ A) --> B ^ A
2242 // (~B & A) | (B ^ A) --> B ^ A
2243 if (match(X, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2244 match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2245 return Y;
2246
2247 // (~A ^ B) | (A & B) --> ~A ^ B
2248 // (B ^ ~A) | (A & B) --> B ^ ~A
2249 // (~A ^ B) | (B & A) --> ~A ^ B
2250 // (B ^ ~A) | (B & A) --> B ^ ~A
2251 if (match(X, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2252 match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2253 return X;
2254
2255 // (~A | B) | (A ^ B) --> -1
2256 // (~A | B) | (B ^ A) --> -1
2257 // (B | ~A) | (A ^ B) --> -1
2258 // (B | ~A) | (B ^ A) --> -1
2259 if (match(X, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2260 match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2261 return ConstantInt::getAllOnesValue(Ty);
2262
2263 // (~A & B) | ~(A | B) --> ~A
2264 // (~A & B) | ~(B | A) --> ~A
2265 // (B & ~A) | ~(A | B) --> ~A
2266 // (B & ~A) | ~(B | A) --> ~A
2267 Value *NotA;
2268 if (match(X, m_c_And(m_CombineAnd(m_Value(NotA), m_Not(m_Value(A))),
2269 m_Value(B))) &&
2270 match(Y, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
2271 return NotA;
2272 // The same is true of Logical And
2273 // TODO: This could share the logic of the version above if there was a
2274 // version of LogicalAnd that allowed more than just i1 types.
2275 if (match(X, m_c_LogicalAnd(m_CombineAnd(m_Value(NotA), m_Not(m_Value(A))),
2276 m_Value(B))) &&
2277 match(Y, m_Not(m_c_LogicalOr(m_Specific(A), m_Specific(B)))))
2278 return NotA;
2279
2280 // ~(A ^ B) | (A & B) --> ~(A ^ B)
2281 // ~(A ^ B) | (B & A) --> ~(A ^ B)
2282 Value *NotAB;
2283 if (match(X, m_CombineAnd(m_Not(m_Xor(m_Value(A), m_Value(B))),
2284 m_Value(NotAB))) &&
2285 match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2286 return NotAB;
2287
2288 // ~(A & B) | (A ^ B) --> ~(A & B)
2289 // ~(A & B) | (B ^ A) --> ~(A & B)
2290 if (match(X, m_CombineAnd(m_Not(m_And(m_Value(A), m_Value(B))),
2291 m_Value(NotAB))) &&
2292 match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2293 return NotAB;
2294
2295 return nullptr;
2296 }
2297
2298 /// Given operands for an Or, see if we can fold the result.
2299 /// If not, this returns null.
simplifyOrInst(Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)2300 static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2301 unsigned MaxRecurse) {
2302 if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q))
2303 return C;
2304
2305 // X | poison -> poison
2306 if (isa<PoisonValue>(Op1))
2307 return Op1;
2308
2309 // X | undef -> -1
2310 // X | -1 = -1
2311 // Do not return Op1 because it may contain undef elements if it's a vector.
2312 if (Q.isUndefValue(Op1) || match(Op1, m_AllOnes()))
2313 return Constant::getAllOnesValue(Op0->getType());
2314
2315 // X | X = X
2316 // X | 0 = X
2317 if (Op0 == Op1 || match(Op1, m_Zero()))
2318 return Op0;
2319
2320 if (Value *R = simplifyOrLogic(Op0, Op1))
2321 return R;
2322 if (Value *R = simplifyOrLogic(Op1, Op0))
2323 return R;
2324
2325 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Or))
2326 return V;
2327
2328 // Rotated -1 is still -1:
2329 // (-1 << X) | (-1 >> (C - X)) --> -1
2330 // (-1 >> X) | (-1 << (C - X)) --> -1
2331 // ...with C <= bitwidth (and commuted variants).
2332 Value *X, *Y;
2333 if ((match(Op0, m_Shl(m_AllOnes(), m_Value(X))) &&
2334 match(Op1, m_LShr(m_AllOnes(), m_Value(Y)))) ||
2335 (match(Op1, m_Shl(m_AllOnes(), m_Value(X))) &&
2336 match(Op0, m_LShr(m_AllOnes(), m_Value(Y))))) {
2337 const APInt *C;
2338 if ((match(X, m_Sub(m_APInt(C), m_Specific(Y))) ||
2339 match(Y, m_Sub(m_APInt(C), m_Specific(X)))) &&
2340 C->ule(X->getType()->getScalarSizeInBits())) {
2341 return ConstantInt::getAllOnesValue(X->getType());
2342 }
2343 }
2344
2345 // A funnel shift (rotate) can be decomposed into simpler shifts. See if we
2346 // are mixing in another shift that is redundant with the funnel shift.
2347
2348 // (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y
2349 // (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y
2350 if (match(Op0,
2351 m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) &&
2352 match(Op1, m_Shl(m_Specific(X), m_Specific(Y))))
2353 return Op0;
2354 if (match(Op1,
2355 m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) &&
2356 match(Op0, m_Shl(m_Specific(X), m_Specific(Y))))
2357 return Op1;
2358
2359 // (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y
2360 // (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y
2361 if (match(Op0,
2362 m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) &&
2363 match(Op1, m_LShr(m_Specific(X), m_Specific(Y))))
2364 return Op0;
2365 if (match(Op1,
2366 m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) &&
2367 match(Op0, m_LShr(m_Specific(X), m_Specific(Y))))
2368 return Op1;
2369
2370 if (Value *V =
2371 simplifyAndOrWithICmpEq(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2372 return V;
2373 if (Value *V =
2374 simplifyAndOrWithICmpEq(Instruction::Or, Op1, Op0, Q, MaxRecurse))
2375 return V;
2376
2377 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false))
2378 return V;
2379
2380 // If we have a multiplication overflow check that is being 'and'ed with a
2381 // check that one of the multipliers is not zero, we can omit the 'and', and
2382 // only keep the overflow check.
2383 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, false))
2384 return Op1;
2385 if (isCheckForZeroAndMulWithOverflow(Op1, Op0, false))
2386 return Op0;
2387
2388 // Try some generic simplifications for associative operations.
2389 if (Value *V =
2390 simplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2391 return V;
2392
2393 // Or distributes over And. Try some generic simplifications based on this.
2394 if (Value *V = expandCommutativeBinOp(Instruction::Or, Op0, Op1,
2395 Instruction::And, Q, MaxRecurse))
2396 return V;
2397
2398 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2399 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2400 // A | (A || B) -> A || B
2401 if (match(Op1, m_Select(m_Specific(Op0), m_One(), m_Value())))
2402 return Op1;
2403 else if (match(Op0, m_Select(m_Specific(Op1), m_One(), m_Value())))
2404 return Op0;
2405 }
2406 // If the operation is with the result of a select instruction, check
2407 // whether operating on either branch of the select always yields the same
2408 // value.
2409 if (Value *V =
2410 threadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2411 return V;
2412 }
2413
2414 // (A & C1)|(B & C2)
2415 Value *A, *B;
2416 const APInt *C1, *C2;
2417 if (match(Op0, m_And(m_Value(A), m_APInt(C1))) &&
2418 match(Op1, m_And(m_Value(B), m_APInt(C2)))) {
2419 if (*C1 == ~*C2) {
2420 // (A & C1)|(B & C2)
2421 // If we have: ((V + N) & C1) | (V & C2)
2422 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
2423 // replace with V+N.
2424 Value *N;
2425 if (C2->isMask() && // C2 == 0+1+
2426 match(A, m_c_Add(m_Specific(B), m_Value(N)))) {
2427 // Add commutes, try both ways.
2428 if (MaskedValueIsZero(N, *C2, Q))
2429 return A;
2430 }
2431 // Or commutes, try both ways.
2432 if (C1->isMask() && match(B, m_c_Add(m_Specific(A), m_Value(N)))) {
2433 // Add commutes, try both ways.
2434 if (MaskedValueIsZero(N, *C1, Q))
2435 return B;
2436 }
2437 }
2438 }
2439
2440 // If the operation is with the result of a phi instruction, check whether
2441 // operating on all incoming values of the phi always yields the same value.
2442 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2443 if (Value *V = threadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2444 return V;
2445
2446 // (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one.
2447 if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) &&
2448 match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1))))
2449 return Constant::getAllOnesValue(Op0->getType());
2450
2451 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2452 if (std::optional<bool> Implied =
2453 isImpliedCondition(Op0, Op1, Q.DL, false)) {
2454 // If Op0 is false implies Op1 is false, then Op1 is a subset of Op0.
2455 if (*Implied == false)
2456 return Op0;
2457 // If Op0 is false implies Op1 is true, then at least one is always true.
2458 if (*Implied == true)
2459 return ConstantInt::getTrue(Op0->getType());
2460 }
2461 if (std::optional<bool> Implied =
2462 isImpliedCondition(Op1, Op0, Q.DL, false)) {
2463 // If Op1 is false implies Op0 is false, then Op0 is a subset of Op1.
2464 if (*Implied == false)
2465 return Op1;
2466 // If Op1 is false implies Op0 is true, then at least one is always true.
2467 if (*Implied == true)
2468 return ConstantInt::getTrue(Op1->getType());
2469 }
2470 }
2471
2472 if (Value *V = simplifyByDomEq(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2473 return V;
2474
2475 return nullptr;
2476 }
2477
simplifyOrInst(Value * Op0,Value * Op1,const SimplifyQuery & Q)2478 Value *llvm::simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2479 return ::simplifyOrInst(Op0, Op1, Q, RecursionLimit);
2480 }
2481
2482 /// Given operands for a Xor, see if we can fold the result.
2483 /// If not, this returns null.
simplifyXorInst(Value * Op0,Value * Op1,const SimplifyQuery & Q,unsigned MaxRecurse)2484 static Value *simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2485 unsigned MaxRecurse) {
2486 if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q))
2487 return C;
2488
2489 // X ^ poison -> poison
2490 if (isa<PoisonValue>(Op1))
2491 return Op1;
2492
2493 // A ^ undef -> undef
2494 if (Q.isUndefValue(Op1))
2495 return Op1;
2496
2497 // A ^ 0 = A
2498 if (match(Op1, m_Zero()))
2499 return Op0;
2500
2501 // A ^ A = 0
2502 if (Op0 == Op1)
2503 return Constant::getNullValue(Op0->getType());
2504
2505 // A ^ ~A = ~A ^ A = -1
2506 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
2507 return Constant::getAllOnesValue(Op0->getType());
2508
2509 auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
2510 Value *A, *B;
2511 // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
2512 if (match(X, m_c_And(m_Not(m_Value(A)), m_Value(B))) &&
2513 match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2514 return A;
2515
2516 // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
2517 // The 'not' op must contain a complete -1 operand (no undef elements for
2518 // vector) for the transform to be safe.
2519 Value *NotA;
2520 if (match(X, m_c_Or(m_CombineAnd(m_Not(m_Value(A)), m_Value(NotA)),
2521 m_Value(B))) &&
2522 match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2523 return NotA;
2524
2525 return nullptr;
2526 };
2527 if (Value *R = foldAndOrNot(Op0, Op1))
2528 return R;
2529 if (Value *R = foldAndOrNot(Op1, Op0))
2530 return R;
2531
2532 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Xor))
2533 return V;
2534
2535 // Try some generic simplifications for associative operations.
2536 if (Value *V =
2537 simplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2538 return V;
2539
2540 // Threading Xor over selects and phi nodes is pointless, so don't bother.
2541 // Threading over the select in "A ^ select(cond, B, C)" means evaluating
2542 // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
2543 // only if B and C are equal. If B and C are equal then (since we assume
2544 // that operands have already been simplified) "select(cond, B, C)" should
2545 // have been simplified to the common value of B and C already. Analysing
2546 // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
2547 // for threading over phi nodes.
2548
2549 if (Value *V = simplifyByDomEq(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2550 return V;
2551
2552 // (xor (sub nuw C_Mask, X), C_Mask) -> X
2553 {
2554 Value *X;
2555 if (match(Op0, m_NUWSub(m_Specific(Op1), m_Value(X))) &&
2556 match(Op1, m_LowBitMask()))
2557 return X;
2558 }
2559
2560 return nullptr;
2561 }
2562
simplifyXorInst(Value * Op0,Value * Op1,const SimplifyQuery & Q)2563 Value *llvm::simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2564 return ::simplifyXorInst(Op0, Op1, Q, RecursionLimit);
2565 }
2566
getCompareTy(Value * Op)2567 static Type *getCompareTy(Value *Op) {
2568 return CmpInst::makeCmpResultType(Op->getType());
2569 }
2570
2571 /// Rummage around inside V looking for something equivalent to the comparison
2572 /// "LHS Pred RHS". Return such a value if found, otherwise return null.
2573 /// Helper function for analyzing max/min idioms.
extractEquivalentCondition(Value * V,CmpPredicate Pred,Value * LHS,Value * RHS)2574 static Value *extractEquivalentCondition(Value *V, CmpPredicate Pred,
2575 Value *LHS, Value *RHS) {
2576 SelectInst *SI = dyn_cast<SelectInst>(V);
2577 if (!SI)
2578 return nullptr;
2579 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2580 if (!Cmp)
2581 return nullptr;
2582 Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2583 if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2584 return Cmp;
2585 if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
2586 LHS == CmpRHS && RHS == CmpLHS)
2587 return Cmp;
2588 return nullptr;
2589 }
2590
2591 /// Return true if the underlying object (storage) must be disjoint from
2592 /// storage returned by any noalias return call.
isAllocDisjoint(const Value * V)2593 static bool isAllocDisjoint(const Value *V) {
2594 // For allocas, we consider only static ones (dynamic
2595 // allocas might be transformed into calls to malloc not simultaneously
2596 // live with the compared-to allocation). For globals, we exclude symbols
2597 // that might be resolve lazily to symbols in another dynamically-loaded
2598 // library (and, thus, could be malloc'ed by the implementation).
2599 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2600 return AI->isStaticAlloca();
2601 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2602 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2603 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2604 !GV->isThreadLocal();
2605 if (const Argument *A = dyn_cast<Argument>(V))
2606 return A->hasByValAttr();
2607 return false;
2608 }
2609
2610 /// Return true if V1 and V2 are each the base of some distict storage region
2611 /// [V, object_size(V)] which do not overlap. Note that zero sized regions
2612 /// *are* possible, and that zero sized regions do not overlap with any other.
haveNonOverlappingStorage(const Value * V1,const Value * V2)2613 static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) {
2614 // Global variables always exist, so they always exist during the lifetime
2615 // of each other and all allocas. Global variables themselves usually have
2616 // non-overlapping storage, but since their addresses are constants, the
2617 // case involving two globals does not reach here and is instead handled in
2618 // constant folding.
2619 //
2620 // Two different allocas usually have different addresses...
2621 //
2622 // However, if there's an @llvm.stackrestore dynamically in between two
2623 // allocas, they may have the same address. It's tempting to reduce the
2624 // scope of the problem by only looking at *static* allocas here. That would
2625 // cover the majority of allocas while significantly reducing the likelihood
2626 // of having an @llvm.stackrestore pop up in the middle. However, it's not
2627 // actually impossible for an @llvm.stackrestore to pop up in the middle of
2628 // an entry block. Also, if we have a block that's not attached to a
2629 // function, we can't tell if it's "static" under the current definition.
2630 // Theoretically, this problem could be fixed by creating a new kind of
2631 // instruction kind specifically for static allocas. Such a new instruction
2632 // could be required to be at the top of the entry block, thus preventing it
2633 // from being subject to a @llvm.stackrestore. Instcombine could even
2634 // convert regular allocas into these special allocas. It'd be nifty.
2635 // However, until then, this problem remains open.
2636 //
2637 // So, we'll assume that two non-empty allocas have different addresses
2638 // for now.
2639 auto isByValArg = [](const Value *V) {
2640 const Argument *A = dyn_cast<Argument>(V);
2641 return A && A->hasByValAttr();
2642 };
2643
2644 // Byval args are backed by store which does not overlap with each other,
2645 // allocas, or globals.
2646 if (isByValArg(V1))
2647 return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
2648 if (isByValArg(V2))
2649 return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
2650
2651 return isa<AllocaInst>(V1) &&
2652 (isa<AllocaInst>(V2) || isa<GlobalVariable>(V2));
2653 }
2654
2655 // A significant optimization not implemented here is assuming that alloca
2656 // addresses are not equal to incoming argument values. They don't *alias*,
2657 // as we say, but that doesn't mean they aren't equal, so we take a
2658 // conservative approach.
2659 //
2660 // This is inspired in part by C++11 5.10p1:
2661 // "Two pointers of the same type compare equal if and only if they are both
2662 // null, both point to the same function, or both represent the same
2663 // address."
2664 //
2665 // This is pretty permissive.
2666 //
2667 // It's also partly due to C11 6.5.9p6:
2668 // "Two pointers compare equal if and only if both are null pointers, both are
2669 // pointers to the same object (including a pointer to an object and a
2670 // subobject at its beginning) or function, both are pointers to one past the
2671 // last element of the same array object, or one is a pointer to one past the
2672 // end of one array object and the other is a pointer to the start of a
2673 // different array object that happens to immediately follow the first array
2674 // object in the address space.)
2675 //
2676 // C11's version is more restrictive, however there's no reason why an argument
2677 // couldn't be a one-past-the-end value for a stack object in the caller and be
2678 // equal to the beginning of a stack object in the callee.
2679 //
2680 // If the C and C++ standards are ever made sufficiently restrictive in this
2681 // area, it may be possible to update LLVM's semantics accordingly and reinstate
2682 // this optimization.
computePointerICmp(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q)2683 static Constant *computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS,
2684 const SimplifyQuery &Q) {
2685 assert(LHS->getType() == RHS->getType() && "Must have same types");
2686 const DataLayout &DL = Q.DL;
2687 const TargetLibraryInfo *TLI = Q.TLI;
2688
2689 // We fold equality and unsigned predicates on pointer comparisons, but forbid
2690 // signed predicates since a GEP with inbounds could cross the sign boundary.
2691 if (CmpInst::isSigned(Pred))
2692 return nullptr;
2693
2694 // We have to switch to a signed predicate to handle negative indices from
2695 // the base pointer.
2696 Pred = ICmpInst::getSignedPredicate(Pred);
2697
2698 // Strip off any constant offsets so that we can reason about them.
2699 // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2700 // here and compare base addresses like AliasAnalysis does, however there are
2701 // numerous hazards. AliasAnalysis and its utilities rely on special rules
2702 // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2703 // doesn't need to guarantee pointer inequality when it says NoAlias.
2704
2705 // Even if an non-inbounds GEP occurs along the path we can still optimize
2706 // equality comparisons concerning the result.
2707 bool AllowNonInbounds = ICmpInst::isEquality(Pred);
2708 unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType());
2709 APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2710 LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds);
2711 RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds);
2712
2713 // If LHS and RHS are related via constant offsets to the same base
2714 // value, we can replace it with an icmp which just compares the offsets.
2715 if (LHS == RHS)
2716 return ConstantInt::get(getCompareTy(LHS),
2717 ICmpInst::compare(LHSOffset, RHSOffset, Pred));
2718
2719 // Various optimizations for (in)equality comparisons.
2720 if (ICmpInst::isEquality(Pred)) {
2721 // Different non-empty allocations that exist at the same time have
2722 // different addresses (if the program can tell). If the offsets are
2723 // within the bounds of their allocations (and not one-past-the-end!
2724 // so we can't use inbounds!), and their allocations aren't the same,
2725 // the pointers are not equal.
2726 if (haveNonOverlappingStorage(LHS, RHS)) {
2727 uint64_t LHSSize, RHSSize;
2728 ObjectSizeOpts Opts;
2729 Opts.EvalMode = ObjectSizeOpts::Mode::Min;
2730 auto *F = [](Value *V) -> Function * {
2731 if (auto *I = dyn_cast<Instruction>(V))
2732 return I->getFunction();
2733 if (auto *A = dyn_cast<Argument>(V))
2734 return A->getParent();
2735 return nullptr;
2736 }(LHS);
2737 Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true;
2738 if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) && LHSSize != 0 &&
2739 getObjectSize(RHS, RHSSize, DL, TLI, Opts) && RHSSize != 0) {
2740 APInt Dist = LHSOffset - RHSOffset;
2741 if (Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize))
2742 return ConstantInt::get(getCompareTy(LHS),
2743 !CmpInst::isTrueWhenEqual(Pred));
2744 }
2745 }
2746
2747 // If one side of the equality comparison must come from a noalias call
2748 // (meaning a system memory allocation function), and the other side must
2749 // come from a pointer that cannot overlap with dynamically-allocated
2750 // memory within the lifetime of the current function (allocas, byval
2751 // arguments, globals), then determine the comparison result here.
2752 SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
2753 getUnderlyingObjects(LHS, LHSUObjs);
2754 getUnderlyingObjects(RHS, RHSUObjs);
2755
2756 // Is the set of underlying objects all noalias calls?
2757 auto IsNAC = [](ArrayRef<const Value *> Objects) {
2758 return all_of(Objects, isNoAliasCall);
2759 };
2760
2761 // Is the set of underlying objects all things which must be disjoint from
2762 // noalias calls. We assume that indexing from such disjoint storage
2763 // into the heap is undefined, and thus offsets can be safely ignored.
2764 auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
2765 return all_of(Objects, ::isAllocDisjoint);
2766 };
2767
2768 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2769 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2770 return ConstantInt::get(getCompareTy(LHS),
2771 !CmpInst::isTrueWhenEqual(Pred));
2772
2773 // Fold comparisons for non-escaping pointer even if the allocation call
2774 // cannot be elided. We cannot fold malloc comparison to null. Also, the
2775 // dynamic allocation call could be either of the operands. Note that
2776 // the other operand can not be based on the alloc - if it were, then
2777 // the cmp itself would be a capture.
2778 Value *MI = nullptr;
2779 if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonZero(RHS, Q))
2780 MI = LHS;
2781 else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonZero(LHS, Q))
2782 MI = RHS;
2783 if (MI) {
2784 // FIXME: This is incorrect, see PR54002. While we can assume that the
2785 // allocation is at an address that makes the comparison false, this
2786 // requires that *all* comparisons to that address be false, which
2787 // InstSimplify cannot guarantee.
2788 struct CustomCaptureTracker : public CaptureTracker {
2789 bool Captured = false;
2790 void tooManyUses() override { Captured = true; }
2791 Action captured(const Use *U, UseCaptureInfo CI) override {
2792 // TODO(captures): Use UseCaptureInfo.
2793 if (auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2794 // Comparison against value stored in global variable. Given the
2795 // pointer does not escape, its value cannot be guessed and stored
2796 // separately in a global variable.
2797 unsigned OtherIdx = 1 - U->getOperandNo();
2798 auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2799 if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2800 return Continue;
2801 }
2802
2803 Captured = true;
2804 return Stop;
2805 }
2806 };
2807 CustomCaptureTracker Tracker;
2808 PointerMayBeCaptured(MI, &Tracker);
2809 if (!Tracker.Captured)
2810 return ConstantInt::get(getCompareTy(LHS),
2811 CmpInst::isFalseWhenEqual(Pred));
2812 }
2813 }
2814
2815 // Otherwise, fail.
2816 return nullptr;
2817 }
2818
2819 /// Fold an icmp when its operands have i1 scalar type.
simplifyICmpOfBools(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q)2820 static Value *simplifyICmpOfBools(CmpPredicate Pred, Value *LHS, Value *RHS,
2821 const SimplifyQuery &Q) {
2822 Type *ITy = getCompareTy(LHS); // The return type.
2823 Type *OpTy = LHS->getType(); // The operand type.
2824 if (!OpTy->isIntOrIntVectorTy(1))
2825 return nullptr;
2826
2827 // A boolean compared to true/false can be reduced in 14 out of the 20
2828 // (10 predicates * 2 constants) possible combinations. The other
2829 // 6 cases require a 'not' of the LHS.
2830
2831 auto ExtractNotLHS = [](Value *V) -> Value * {
2832 Value *X;
2833 if (match(V, m_Not(m_Value(X))))
2834 return X;
2835 return nullptr;
2836 };
2837
2838 if (match(RHS, m_Zero())) {
2839 switch (Pred) {
2840 case CmpInst::ICMP_NE: // X != 0 -> X
2841 case CmpInst::ICMP_UGT: // X >u 0 -> X
2842 case CmpInst::ICMP_SLT: // X <s 0 -> X
2843 return LHS;
2844
2845 case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X
2846 case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
2847 case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
2848 if (Value *X = ExtractNotLHS(LHS))
2849 return X;
2850 break;
2851
2852 case CmpInst::ICMP_ULT: // X <u 0 -> false
2853 case CmpInst::ICMP_SGT: // X >s 0 -> false
2854 return getFalse(ITy);
2855
2856 case CmpInst::ICMP_UGE: // X >=u 0 -> true
2857 case CmpInst::ICMP_SLE: // X <=s 0 -> true
2858 return getTrue(ITy);
2859
2860 default:
2861 break;
2862 }
2863 } else if (match(RHS, m_One())) {
2864 switch (Pred) {
2865 case CmpInst::ICMP_EQ: // X == 1 -> X
2866 case CmpInst::ICMP_UGE: // X >=u 1 -> X
2867 case CmpInst::ICMP_SLE: // X <=s -1 -> X
2868 return LHS;
2869
2870 case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X
2871 case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X
2872 case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X
2873 if (Value *X = ExtractNotLHS(LHS))
2874 return X;
2875 break;
2876
2877 case CmpInst::ICMP_UGT: // X >u 1 -> false
2878 case CmpInst::ICMP_SLT: // X <s -1 -> false
2879 return getFalse(ITy);
2880
2881 case CmpInst::ICMP_ULE: // X <=u 1 -> true
2882 case CmpInst::ICMP_SGE: // X >=s -1 -> true
2883 return getTrue(ITy);
2884
2885 default:
2886 break;
2887 }
2888 }
2889
2890 switch (Pred) {
2891 default:
2892 break;
2893 case ICmpInst::ICMP_UGE:
2894 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2895 return getTrue(ITy);
2896 break;
2897 case ICmpInst::ICMP_SGE:
2898 /// For signed comparison, the values for an i1 are 0 and -1
2899 /// respectively. This maps into a truth table of:
2900 /// LHS | RHS | LHS >=s RHS | LHS implies RHS
2901 /// 0 | 0 | 1 (0 >= 0) | 1
2902 /// 0 | 1 | 1 (0 >= -1) | 1
2903 /// 1 | 0 | 0 (-1 >= 0) | 0
2904 /// 1 | 1 | 1 (-1 >= -1) | 1
2905 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2906 return getTrue(ITy);
2907 break;
2908 case ICmpInst::ICMP_ULE:
2909 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2910 return getTrue(ITy);
2911 break;
2912 case ICmpInst::ICMP_SLE:
2913 /// SLE follows the same logic as SGE with the LHS and RHS swapped.
2914 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2915 return getTrue(ITy);
2916 break;
2917 }
2918
2919 return nullptr;
2920 }
2921
2922 /// Try hard to fold icmp with zero RHS because this is a common case.
simplifyICmpWithZero(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q)2923 static Value *simplifyICmpWithZero(CmpPredicate Pred, Value *LHS, Value *RHS,
2924 const SimplifyQuery &Q) {
2925 if (!match(RHS, m_Zero()))
2926 return nullptr;
2927
2928 Type *ITy = getCompareTy(LHS); // The return type.
2929 switch (Pred) {
2930 default:
2931 llvm_unreachable("Unknown ICmp predicate!");
2932 case ICmpInst::ICMP_ULT:
2933 return getFalse(ITy);
2934 case ICmpInst::ICMP_UGE:
2935 return getTrue(ITy);
2936 case ICmpInst::ICMP_EQ:
2937 case ICmpInst::ICMP_ULE:
2938 if (isKnownNonZero(LHS, Q))
2939 return getFalse(ITy);
2940 break;
2941 case ICmpInst::ICMP_NE:
2942 case ICmpInst::ICMP_UGT:
2943 if (isKnownNonZero(LHS, Q))
2944 return getTrue(ITy);
2945 break;
2946 case ICmpInst::ICMP_SLT: {
2947 KnownBits LHSKnown = computeKnownBits(LHS, Q);
2948 if (LHSKnown.isNegative())
2949 return getTrue(ITy);
2950 if (LHSKnown.isNonNegative())
2951 return getFalse(ITy);
2952 break;
2953 }
2954 case ICmpInst::ICMP_SLE: {
2955 KnownBits LHSKnown = computeKnownBits(LHS, Q);
2956 if (LHSKnown.isNegative())
2957 return getTrue(ITy);
2958 if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
2959 return getFalse(ITy);
2960 break;
2961 }
2962 case ICmpInst::ICMP_SGE: {
2963 KnownBits LHSKnown = computeKnownBits(LHS, Q);
2964 if (LHSKnown.isNegative())
2965 return getFalse(ITy);
2966 if (LHSKnown.isNonNegative())
2967 return getTrue(ITy);
2968 break;
2969 }
2970 case ICmpInst::ICMP_SGT: {
2971 KnownBits LHSKnown = computeKnownBits(LHS, Q);
2972 if (LHSKnown.isNegative())
2973 return getFalse(ITy);
2974 if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q))
2975 return getTrue(ITy);
2976 break;
2977 }
2978 }
2979
2980 return nullptr;
2981 }
2982
simplifyICmpWithConstant(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q)2983 static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS,
2984 Value *RHS, const SimplifyQuery &Q) {
2985 Type *ITy = getCompareTy(RHS); // The return type.
2986
2987 Value *X;
2988 const APInt *C;
2989 if (!match(RHS, m_APIntAllowPoison(C)))
2990 return nullptr;
2991
2992 // Sign-bit checks can be optimized to true/false after unsigned
2993 // floating-point casts:
2994 // icmp slt (bitcast (uitofp X)), 0 --> false
2995 // icmp sgt (bitcast (uitofp X)), -1 --> true
2996 if (match(LHS, m_ElementWiseBitCast(m_UIToFP(m_Value(X))))) {
2997 bool TrueIfSigned;
2998 if (isSignBitCheck(Pred, *C, TrueIfSigned))
2999 return ConstantInt::getBool(ITy, !TrueIfSigned);
3000 }
3001
3002 // Rule out tautological comparisons (eg., ult 0 or uge 0).
3003 ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
3004 if (RHS_CR.isEmptySet())
3005 return ConstantInt::getFalse(ITy);
3006 if (RHS_CR.isFullSet())
3007 return ConstantInt::getTrue(ITy);
3008
3009 ConstantRange LHS_CR =
3010 computeConstantRange(LHS, CmpInst::isSigned(Pred), Q.IIQ.UseInstrInfo);
3011 if (!LHS_CR.isFullSet()) {
3012 if (RHS_CR.contains(LHS_CR))
3013 return ConstantInt::getTrue(ITy);
3014 if (RHS_CR.inverse().contains(LHS_CR))
3015 return ConstantInt::getFalse(ITy);
3016 }
3017
3018 // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
3019 // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
3020 const APInt *MulC;
3021 if (Q.IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
3022 ((match(LHS, m_NUWMul(m_Value(), m_APIntAllowPoison(MulC))) &&
3023 *MulC != 0 && C->urem(*MulC) != 0) ||
3024 (match(LHS, m_NSWMul(m_Value(), m_APIntAllowPoison(MulC))) &&
3025 *MulC != 0 && C->srem(*MulC) != 0)))
3026 return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
3027
3028 if (Pred == ICmpInst::ICMP_UGE && C->isOne() && isKnownNonZero(LHS, Q))
3029 return ConstantInt::getTrue(ITy);
3030
3031 return nullptr;
3032 }
3033
3034 enum class MonotonicType { GreaterEq, LowerEq };
3035
3036 /// Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq).
getUnsignedMonotonicValues(SmallPtrSetImpl<Value * > & Res,Value * V,MonotonicType Type,const SimplifyQuery & Q,unsigned Depth=0)3037 static void getUnsignedMonotonicValues(SmallPtrSetImpl<Value *> &Res, Value *V,
3038 MonotonicType Type,
3039 const SimplifyQuery &Q,
3040 unsigned Depth = 0) {
3041 if (!Res.insert(V).second)
3042 return;
3043
3044 // Can be increased if useful.
3045 if (++Depth > 1)
3046 return;
3047
3048 auto *I = dyn_cast<Instruction>(V);
3049 if (!I)
3050 return;
3051
3052 Value *X, *Y;
3053 if (Type == MonotonicType::GreaterEq) {
3054 if (match(I, m_Or(m_Value(X), m_Value(Y))) ||
3055 match(I, m_Intrinsic<Intrinsic::uadd_sat>(m_Value(X), m_Value(Y)))) {
3056 getUnsignedMonotonicValues(Res, X, Type, Q, Depth);
3057 getUnsignedMonotonicValues(Res, Y, Type, Q, Depth);
3058 }
3059 // X * Y >= X --> true
3060 if (match(I, m_NUWMul(m_Value(X), m_Value(Y)))) {
3061 if (isKnownNonZero(X, Q))
3062 getUnsignedMonotonicValues(Res, Y, Type, Q, Depth);
3063 if (isKnownNonZero(Y, Q))
3064 getUnsignedMonotonicValues(Res, X, Type, Q, Depth);
3065 }
3066 } else {
3067 assert(Type == MonotonicType::LowerEq);
3068 switch (I->getOpcode()) {
3069 case Instruction::And:
3070 getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Q, Depth);
3071 getUnsignedMonotonicValues(Res, I->getOperand(1), Type, Q, Depth);
3072 break;
3073 case Instruction::URem:
3074 case Instruction::UDiv:
3075 case Instruction::LShr:
3076 getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Q, Depth);
3077 break;
3078 case Instruction::Call:
3079 if (match(I, m_Intrinsic<Intrinsic::usub_sat>(m_Value(X))))
3080 getUnsignedMonotonicValues(Res, X, Type, Q, Depth);
3081 break;
3082 default:
3083 break;
3084 }
3085 }
3086 }
3087
simplifyICmpUsingMonotonicValues(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q)3088 static Value *simplifyICmpUsingMonotonicValues(CmpPredicate Pred, Value *LHS,
3089 Value *RHS,
3090 const SimplifyQuery &Q) {
3091 if (Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_ULT)
3092 return nullptr;
3093
3094 // We have LHS uge GreaterValues and LowerValues uge RHS. If any of the
3095 // GreaterValues and LowerValues are the same, it follows that LHS uge RHS.
3096 SmallPtrSet<Value *, 4> GreaterValues;
3097 SmallPtrSet<Value *, 4> LowerValues;
3098 getUnsignedMonotonicValues(GreaterValues, LHS, MonotonicType::GreaterEq, Q);
3099 getUnsignedMonotonicValues(LowerValues, RHS, MonotonicType::LowerEq, Q);
3100 for (Value *GV : GreaterValues)
3101 if (LowerValues.contains(GV))
3102 return ConstantInt::getBool(getCompareTy(LHS),
3103 Pred == ICmpInst::ICMP_UGE);
3104 return nullptr;
3105 }
3106
simplifyICmpWithBinOpOnLHS(CmpPredicate Pred,BinaryOperator * LBO,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)3107 static Value *simplifyICmpWithBinOpOnLHS(CmpPredicate Pred, BinaryOperator *LBO,
3108 Value *RHS, const SimplifyQuery &Q,
3109 unsigned MaxRecurse) {
3110 Type *ITy = getCompareTy(RHS); // The return type.
3111
3112 Value *Y = nullptr;
3113 // icmp pred (or X, Y), X
3114 if (match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) {
3115 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3116 KnownBits RHSKnown = computeKnownBits(RHS, Q);
3117 KnownBits YKnown = computeKnownBits(Y, Q);
3118 if (RHSKnown.isNonNegative() && YKnown.isNegative())
3119 return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
3120 if (RHSKnown.isNegative() || YKnown.isNonNegative())
3121 return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy);
3122 }
3123 }
3124
3125 // icmp pred (urem X, Y), Y
3126 if (match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
3127 switch (Pred) {
3128 default:
3129 break;
3130 case ICmpInst::ICMP_SGT:
3131 case ICmpInst::ICMP_SGE: {
3132 KnownBits Known = computeKnownBits(RHS, Q);
3133 if (!Known.isNonNegative())
3134 break;
3135 [[fallthrough]];
3136 }
3137 case ICmpInst::ICMP_EQ:
3138 case ICmpInst::ICMP_UGT:
3139 case ICmpInst::ICMP_UGE:
3140 return getFalse(ITy);
3141 case ICmpInst::ICMP_SLT:
3142 case ICmpInst::ICMP_SLE: {
3143 KnownBits Known = computeKnownBits(RHS, Q);
3144 if (!Known.isNonNegative())
3145 break;
3146 [[fallthrough]];
3147 }
3148 case ICmpInst::ICMP_NE:
3149 case ICmpInst::ICMP_ULT:
3150 case ICmpInst::ICMP_ULE:
3151 return getTrue(ITy);
3152 }
3153 }
3154
3155 // If x is nonzero:
3156 // x >>u C <u x --> true for C != 0.
3157 // x >>u C != x --> true for C != 0.
3158 // x >>u C >=u x --> false for C != 0.
3159 // x >>u C == x --> false for C != 0.
3160 // x udiv C <u x --> true for C != 1.
3161 // x udiv C != x --> true for C != 1.
3162 // x udiv C >=u x --> false for C != 1.
3163 // x udiv C == x --> false for C != 1.
3164 // TODO: allow non-constant shift amount/divisor
3165 const APInt *C;
3166 if ((match(LBO, m_LShr(m_Specific(RHS), m_APInt(C))) && *C != 0) ||
3167 (match(LBO, m_UDiv(m_Specific(RHS), m_APInt(C))) && *C != 1)) {
3168 if (isKnownNonZero(RHS, Q)) {
3169 switch (Pred) {
3170 default:
3171 break;
3172 case ICmpInst::ICMP_EQ:
3173 case ICmpInst::ICMP_UGE:
3174 case ICmpInst::ICMP_UGT:
3175 return getFalse(ITy);
3176 case ICmpInst::ICMP_NE:
3177 case ICmpInst::ICMP_ULT:
3178 case ICmpInst::ICMP_ULE:
3179 return getTrue(ITy);
3180 }
3181 }
3182 }
3183
3184 // (x*C1)/C2 <= x for C1 <= C2.
3185 // This holds even if the multiplication overflows: Assume that x != 0 and
3186 // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and
3187 // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
3188 //
3189 // Additionally, either the multiplication and division might be represented
3190 // as shifts:
3191 // (x*C1)>>C2 <= x for C1 < 2**C2.
3192 // (x<<C1)/C2 <= x for 2**C1 < C2.
3193 const APInt *C1, *C2;
3194 if ((match(LBO, m_UDiv(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3195 C1->ule(*C2)) ||
3196 (match(LBO, m_LShr(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3197 C1->ule(APInt(C2->getBitWidth(), 1) << *C2)) ||
3198 (match(LBO, m_UDiv(m_Shl(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3199 (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2))) {
3200 if (Pred == ICmpInst::ICMP_UGT)
3201 return getFalse(ITy);
3202 if (Pred == ICmpInst::ICMP_ULE)
3203 return getTrue(ITy);
3204 }
3205
3206 // (sub C, X) == X, C is odd --> false
3207 // (sub C, X) != X, C is odd --> true
3208 if (match(LBO, m_Sub(m_APIntAllowPoison(C), m_Specific(RHS))) &&
3209 (*C & 1) == 1 && ICmpInst::isEquality(Pred))
3210 return (Pred == ICmpInst::ICMP_EQ) ? getFalse(ITy) : getTrue(ITy);
3211
3212 return nullptr;
3213 }
3214
3215 // If only one of the icmp's operands has NSW flags, try to prove that:
3216 //
3217 // icmp slt (x + C1), (x +nsw C2)
3218 //
3219 // is equivalent to:
3220 //
3221 // icmp slt C1, C2
3222 //
3223 // which is true if x + C2 has the NSW flags set and:
3224 // *) C1 < C2 && C1 >= 0, or
3225 // *) C2 < C1 && C1 <= 0.
3226 //
trySimplifyICmpWithAdds(CmpPredicate Pred,Value * LHS,Value * RHS,const InstrInfoQuery & IIQ)3227 static bool trySimplifyICmpWithAdds(CmpPredicate Pred, Value *LHS, Value *RHS,
3228 const InstrInfoQuery &IIQ) {
3229 // TODO: only support icmp slt for now.
3230 if (Pred != CmpInst::ICMP_SLT || !IIQ.UseInstrInfo)
3231 return false;
3232
3233 // Canonicalize nsw add as RHS.
3234 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3235 std::swap(LHS, RHS);
3236 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3237 return false;
3238
3239 Value *X;
3240 const APInt *C1, *C2;
3241 if (!match(LHS, m_Add(m_Value(X), m_APInt(C1))) ||
3242 !match(RHS, m_Add(m_Specific(X), m_APInt(C2))))
3243 return false;
3244
3245 return (C1->slt(*C2) && C1->isNonNegative()) ||
3246 (C2->slt(*C1) && C1->isNonPositive());
3247 }
3248
3249 /// TODO: A large part of this logic is duplicated in InstCombine's
3250 /// foldICmpBinOp(). We should be able to share that and avoid the code
3251 /// duplication.
simplifyICmpWithBinOp(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)3252 static Value *simplifyICmpWithBinOp(CmpPredicate Pred, Value *LHS, Value *RHS,
3253 const SimplifyQuery &Q,
3254 unsigned MaxRecurse) {
3255 BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
3256 BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
3257 if (MaxRecurse && (LBO || RBO)) {
3258 // Analyze the case when either LHS or RHS is an add instruction.
3259 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
3260 // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
3261 bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
3262 if (LBO && LBO->getOpcode() == Instruction::Add) {
3263 A = LBO->getOperand(0);
3264 B = LBO->getOperand(1);
3265 NoLHSWrapProblem =
3266 ICmpInst::isEquality(Pred) ||
3267 (CmpInst::isUnsigned(Pred) &&
3268 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))) ||
3269 (CmpInst::isSigned(Pred) &&
3270 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)));
3271 }
3272 if (RBO && RBO->getOpcode() == Instruction::Add) {
3273 C = RBO->getOperand(0);
3274 D = RBO->getOperand(1);
3275 NoRHSWrapProblem =
3276 ICmpInst::isEquality(Pred) ||
3277 (CmpInst::isUnsigned(Pred) &&
3278 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(RBO))) ||
3279 (CmpInst::isSigned(Pred) &&
3280 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RBO)));
3281 }
3282
3283 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
3284 if ((A == RHS || B == RHS) && NoLHSWrapProblem)
3285 if (Value *V = simplifyICmpInst(Pred, A == RHS ? B : A,
3286 Constant::getNullValue(RHS->getType()), Q,
3287 MaxRecurse - 1))
3288 return V;
3289
3290 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
3291 if ((C == LHS || D == LHS) && NoRHSWrapProblem)
3292 if (Value *V =
3293 simplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()),
3294 C == LHS ? D : C, Q, MaxRecurse - 1))
3295 return V;
3296
3297 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
3298 bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3299 trySimplifyICmpWithAdds(Pred, LHS, RHS, Q.IIQ);
3300 if (A && C && (A == C || A == D || B == C || B == D) && CanSimplify) {
3301 // Determine Y and Z in the form icmp (X+Y), (X+Z).
3302 Value *Y, *Z;
3303 if (A == C) {
3304 // C + B == C + D -> B == D
3305 Y = B;
3306 Z = D;
3307 } else if (A == D) {
3308 // D + B == C + D -> B == C
3309 Y = B;
3310 Z = C;
3311 } else if (B == C) {
3312 // A + C == C + D -> A == D
3313 Y = A;
3314 Z = D;
3315 } else {
3316 assert(B == D);
3317 // A + D == C + D -> A == C
3318 Y = A;
3319 Z = C;
3320 }
3321 if (Value *V = simplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
3322 return V;
3323 }
3324 }
3325
3326 if (LBO)
3327 if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse))
3328 return V;
3329
3330 if (RBO)
3331 if (Value *V = simplifyICmpWithBinOpOnLHS(
3332 ICmpInst::getSwappedPredicate(Pred), RBO, LHS, Q, MaxRecurse))
3333 return V;
3334
3335 // 0 - (zext X) pred C
3336 if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) {
3337 const APInt *C;
3338 if (match(RHS, m_APInt(C))) {
3339 if (C->isStrictlyPositive()) {
3340 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3341 return ConstantInt::getTrue(getCompareTy(RHS));
3342 if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3343 return ConstantInt::getFalse(getCompareTy(RHS));
3344 }
3345 if (C->isNonNegative()) {
3346 if (Pred == ICmpInst::ICMP_SLE)
3347 return ConstantInt::getTrue(getCompareTy(RHS));
3348 if (Pred == ICmpInst::ICMP_SGT)
3349 return ConstantInt::getFalse(getCompareTy(RHS));
3350 }
3351 }
3352 }
3353
3354 // If C2 is a power-of-2 and C is not:
3355 // (C2 << X) == C --> false
3356 // (C2 << X) != C --> true
3357 const APInt *C;
3358 if (match(LHS, m_Shl(m_Power2(), m_Value())) &&
3359 match(RHS, m_APIntAllowPoison(C)) && !C->isPowerOf2()) {
3360 // C2 << X can equal zero in some circumstances.
3361 // This simplification might be unsafe if C is zero.
3362 //
3363 // We know it is safe if:
3364 // - The shift is nsw. We can't shift out the one bit.
3365 // - The shift is nuw. We can't shift out the one bit.
3366 // - C2 is one.
3367 // - C isn't zero.
3368 if (Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3369 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3370 match(LHS, m_Shl(m_One(), m_Value())) || !C->isZero()) {
3371 if (Pred == ICmpInst::ICMP_EQ)
3372 return ConstantInt::getFalse(getCompareTy(RHS));
3373 if (Pred == ICmpInst::ICMP_NE)
3374 return ConstantInt::getTrue(getCompareTy(RHS));
3375 }
3376 }
3377
3378 // If C is a power-of-2:
3379 // (C << X) >u 0x8000 --> false
3380 // (C << X) <=u 0x8000 --> true
3381 if (match(LHS, m_Shl(m_Power2(), m_Value())) && match(RHS, m_SignMask())) {
3382 if (Pred == ICmpInst::ICMP_UGT)
3383 return ConstantInt::getFalse(getCompareTy(RHS));
3384 if (Pred == ICmpInst::ICMP_ULE)
3385 return ConstantInt::getTrue(getCompareTy(RHS));
3386 }
3387
3388 if (!MaxRecurse || !LBO || !RBO || LBO->getOpcode() != RBO->getOpcode())
3389 return nullptr;
3390
3391 if (LBO->getOperand(0) == RBO->getOperand(0)) {
3392 switch (LBO->getOpcode()) {
3393 default:
3394 break;
3395 case Instruction::Shl: {
3396 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3397 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3398 if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3399 !isKnownNonZero(LBO->getOperand(0), Q))
3400 break;
3401 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(1),
3402 RBO->getOperand(1), Q, MaxRecurse - 1))
3403 return V;
3404 break;
3405 }
3406 // If C1 & C2 == C1, A = X and/or C1, B = X and/or C2:
3407 // icmp ule A, B -> true
3408 // icmp ugt A, B -> false
3409 // icmp sle A, B -> true (C1 and C2 are the same sign)
3410 // icmp sgt A, B -> false (C1 and C2 are the same sign)
3411 case Instruction::And:
3412 case Instruction::Or: {
3413 const APInt *C1, *C2;
3414 if (ICmpInst::isRelational(Pred) &&
3415 match(LBO->getOperand(1), m_APInt(C1)) &&
3416 match(RBO->getOperand(1), m_APInt(C2))) {
3417 if (!C1->isSubsetOf(*C2)) {
3418 std::swap(C1, C2);
3419 Pred = ICmpInst::getSwappedPredicate(Pred);
3420 }
3421 if (C1->isSubsetOf(*C2)) {
3422 if (Pred == ICmpInst::ICMP_ULE)
3423 return ConstantInt::getTrue(getCompareTy(LHS));
3424 if (Pred == ICmpInst::ICMP_UGT)
3425 return ConstantInt::getFalse(getCompareTy(LHS));
3426 if (C1->isNonNegative() == C2->isNonNegative()) {
3427 if (Pred == ICmpInst::ICMP_SLE)
3428 return ConstantInt::getTrue(getCompareTy(LHS));
3429 if (Pred == ICmpInst::ICMP_SGT)
3430 return ConstantInt::getFalse(getCompareTy(LHS));
3431 }
3432 }
3433 }
3434 break;
3435 }
3436 }
3437 }
3438
3439 if (LBO->getOperand(1) == RBO->getOperand(1)) {
3440 switch (LBO->getOpcode()) {
3441 default:
3442 break;
3443 case Instruction::UDiv:
3444 case Instruction::LShr:
3445 if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(LBO) ||
3446 !Q.IIQ.isExact(RBO))
3447 break;
3448 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3449 RBO->getOperand(0), Q, MaxRecurse - 1))
3450 return V;
3451 break;
3452 case Instruction::SDiv:
3453 if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO) ||
3454 !Q.IIQ.isExact(RBO))
3455 break;
3456 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3457 RBO->getOperand(0), Q, MaxRecurse - 1))
3458 return V;
3459 break;
3460 case Instruction::AShr:
3461 if (!Q.IIQ.isExact(LBO) || !Q.IIQ.isExact(RBO))
3462 break;
3463 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3464 RBO->getOperand(0), Q, MaxRecurse - 1))
3465 return V;
3466 break;
3467 case Instruction::Shl: {
3468 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3469 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3470 if (!NUW && !NSW)
3471 break;
3472 if (!NSW && ICmpInst::isSigned(Pred))
3473 break;
3474 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3475 RBO->getOperand(0), Q, MaxRecurse - 1))
3476 return V;
3477 break;
3478 }
3479 }
3480 }
3481 return nullptr;
3482 }
3483
3484 /// simplify integer comparisons where at least one operand of the compare
3485 /// matches an integer min/max idiom.
simplifyICmpWithMinMax(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)3486 static Value *simplifyICmpWithMinMax(CmpPredicate Pred, Value *LHS, Value *RHS,
3487 const SimplifyQuery &Q,
3488 unsigned MaxRecurse) {
3489 Type *ITy = getCompareTy(LHS); // The return type.
3490 Value *A, *B;
3491 CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
3492 CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
3493
3494 // Signed variants on "max(a,b)>=a -> true".
3495 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3496 if (A != RHS)
3497 std::swap(A, B); // smax(A, B) pred A.
3498 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3499 // We analyze this as smax(A, B) pred A.
3500 P = Pred;
3501 } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
3502 (A == LHS || B == LHS)) {
3503 if (A != LHS)
3504 std::swap(A, B); // A pred smax(A, B).
3505 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3506 // We analyze this as smax(A, B) swapped-pred A.
3507 P = CmpInst::getSwappedPredicate(Pred);
3508 } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
3509 (A == RHS || B == RHS)) {
3510 if (A != RHS)
3511 std::swap(A, B); // smin(A, B) pred A.
3512 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3513 // We analyze this as smax(-A, -B) swapped-pred -A.
3514 // Note that we do not need to actually form -A or -B thanks to EqP.
3515 P = CmpInst::getSwappedPredicate(Pred);
3516 } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
3517 (A == LHS || B == LHS)) {
3518 if (A != LHS)
3519 std::swap(A, B); // A pred smin(A, B).
3520 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3521 // We analyze this as smax(-A, -B) pred -A.
3522 // Note that we do not need to actually form -A or -B thanks to EqP.
3523 P = Pred;
3524 }
3525 if (P != CmpInst::BAD_ICMP_PREDICATE) {
3526 // Cases correspond to "max(A, B) p A".
3527 switch (P) {
3528 default:
3529 break;
3530 case CmpInst::ICMP_EQ:
3531 case CmpInst::ICMP_SLE:
3532 // Equivalent to "A EqP B". This may be the same as the condition tested
3533 // in the max/min; if so, we can just return that.
3534 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3535 return V;
3536 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3537 return V;
3538 // Otherwise, see if "A EqP B" simplifies.
3539 if (MaxRecurse)
3540 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3541 return V;
3542 break;
3543 case CmpInst::ICMP_NE:
3544 case CmpInst::ICMP_SGT: {
3545 CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
3546 // Equivalent to "A InvEqP B". This may be the same as the condition
3547 // tested in the max/min; if so, we can just return that.
3548 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3549 return V;
3550 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3551 return V;
3552 // Otherwise, see if "A InvEqP B" simplifies.
3553 if (MaxRecurse)
3554 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3555 return V;
3556 break;
3557 }
3558 case CmpInst::ICMP_SGE:
3559 // Always true.
3560 return getTrue(ITy);
3561 case CmpInst::ICMP_SLT:
3562 // Always false.
3563 return getFalse(ITy);
3564 }
3565 }
3566
3567 // Unsigned variants on "max(a,b)>=a -> true".
3568 P = CmpInst::BAD_ICMP_PREDICATE;
3569 if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3570 if (A != RHS)
3571 std::swap(A, B); // umax(A, B) pred A.
3572 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3573 // We analyze this as umax(A, B) pred A.
3574 P = Pred;
3575 } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
3576 (A == LHS || B == LHS)) {
3577 if (A != LHS)
3578 std::swap(A, B); // A pred umax(A, B).
3579 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3580 // We analyze this as umax(A, B) swapped-pred A.
3581 P = CmpInst::getSwappedPredicate(Pred);
3582 } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
3583 (A == RHS || B == RHS)) {
3584 if (A != RHS)
3585 std::swap(A, B); // umin(A, B) pred A.
3586 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3587 // We analyze this as umax(-A, -B) swapped-pred -A.
3588 // Note that we do not need to actually form -A or -B thanks to EqP.
3589 P = CmpInst::getSwappedPredicate(Pred);
3590 } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
3591 (A == LHS || B == LHS)) {
3592 if (A != LHS)
3593 std::swap(A, B); // A pred umin(A, B).
3594 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3595 // We analyze this as umax(-A, -B) pred -A.
3596 // Note that we do not need to actually form -A or -B thanks to EqP.
3597 P = Pred;
3598 }
3599 if (P != CmpInst::BAD_ICMP_PREDICATE) {
3600 // Cases correspond to "max(A, B) p A".
3601 switch (P) {
3602 default:
3603 break;
3604 case CmpInst::ICMP_EQ:
3605 case CmpInst::ICMP_ULE:
3606 // Equivalent to "A EqP B". This may be the same as the condition tested
3607 // in the max/min; if so, we can just return that.
3608 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3609 return V;
3610 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3611 return V;
3612 // Otherwise, see if "A EqP B" simplifies.
3613 if (MaxRecurse)
3614 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3615 return V;
3616 break;
3617 case CmpInst::ICMP_NE:
3618 case CmpInst::ICMP_UGT: {
3619 CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
3620 // Equivalent to "A InvEqP B". This may be the same as the condition
3621 // tested in the max/min; if so, we can just return that.
3622 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3623 return V;
3624 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3625 return V;
3626 // Otherwise, see if "A InvEqP B" simplifies.
3627 if (MaxRecurse)
3628 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3629 return V;
3630 break;
3631 }
3632 case CmpInst::ICMP_UGE:
3633 return getTrue(ITy);
3634 case CmpInst::ICMP_ULT:
3635 return getFalse(ITy);
3636 }
3637 }
3638
3639 // Comparing 1 each of min/max with a common operand?
3640 // Canonicalize min operand to RHS.
3641 if (match(LHS, m_UMin(m_Value(), m_Value())) ||
3642 match(LHS, m_SMin(m_Value(), m_Value()))) {
3643 std::swap(LHS, RHS);
3644 Pred = ICmpInst::getSwappedPredicate(Pred);
3645 }
3646
3647 Value *C, *D;
3648 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
3649 match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
3650 (A == C || A == D || B == C || B == D)) {
3651 // smax(A, B) >=s smin(A, D) --> true
3652 if (Pred == CmpInst::ICMP_SGE)
3653 return getTrue(ITy);
3654 // smax(A, B) <s smin(A, D) --> false
3655 if (Pred == CmpInst::ICMP_SLT)
3656 return getFalse(ITy);
3657 } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
3658 match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
3659 (A == C || A == D || B == C || B == D)) {
3660 // umax(A, B) >=u umin(A, D) --> true
3661 if (Pred == CmpInst::ICMP_UGE)
3662 return getTrue(ITy);
3663 // umax(A, B) <u umin(A, D) --> false
3664 if (Pred == CmpInst::ICMP_ULT)
3665 return getFalse(ITy);
3666 }
3667
3668 return nullptr;
3669 }
3670
simplifyICmpWithDominatingAssume(CmpPredicate Predicate,Value * LHS,Value * RHS,const SimplifyQuery & Q)3671 static Value *simplifyICmpWithDominatingAssume(CmpPredicate Predicate,
3672 Value *LHS, Value *RHS,
3673 const SimplifyQuery &Q) {
3674 // Gracefully handle instructions that have not been inserted yet.
3675 if (!Q.AC || !Q.CxtI)
3676 return nullptr;
3677
3678 for (Value *AssumeBaseOp : {LHS, RHS}) {
3679 for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) {
3680 if (!AssumeVH)
3681 continue;
3682
3683 CallInst *Assume = cast<CallInst>(AssumeVH);
3684 if (std::optional<bool> Imp = isImpliedCondition(
3685 Assume->getArgOperand(0), Predicate, LHS, RHS, Q.DL))
3686 if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT))
3687 return ConstantInt::get(getCompareTy(LHS), *Imp);
3688 }
3689 }
3690
3691 return nullptr;
3692 }
3693
simplifyICmpWithIntrinsicOnLHS(CmpPredicate Pred,Value * LHS,Value * RHS)3694 static Value *simplifyICmpWithIntrinsicOnLHS(CmpPredicate Pred, Value *LHS,
3695 Value *RHS) {
3696 auto *II = dyn_cast<IntrinsicInst>(LHS);
3697 if (!II)
3698 return nullptr;
3699
3700 switch (II->getIntrinsicID()) {
3701 case Intrinsic::uadd_sat:
3702 // uadd.sat(X, Y) uge X + Y
3703 if (match(RHS, m_c_Add(m_Specific(II->getArgOperand(0)),
3704 m_Specific(II->getArgOperand(1))))) {
3705 if (Pred == ICmpInst::ICMP_UGE)
3706 return ConstantInt::getTrue(getCompareTy(II));
3707 if (Pred == ICmpInst::ICMP_ULT)
3708 return ConstantInt::getFalse(getCompareTy(II));
3709 }
3710 return nullptr;
3711 case Intrinsic::usub_sat:
3712 // usub.sat(X, Y) ule X - Y
3713 if (match(RHS, m_Sub(m_Specific(II->getArgOperand(0)),
3714 m_Specific(II->getArgOperand(1))))) {
3715 if (Pred == ICmpInst::ICMP_ULE)
3716 return ConstantInt::getTrue(getCompareTy(II));
3717 if (Pred == ICmpInst::ICMP_UGT)
3718 return ConstantInt::getFalse(getCompareTy(II));
3719 }
3720 return nullptr;
3721 default:
3722 return nullptr;
3723 }
3724 }
3725
3726 /// Helper method to get range from metadata or attribute.
getRange(Value * V,const InstrInfoQuery & IIQ)3727 static std::optional<ConstantRange> getRange(Value *V,
3728 const InstrInfoQuery &IIQ) {
3729 if (Instruction *I = dyn_cast<Instruction>(V))
3730 if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range))
3731 return getConstantRangeFromMetadata(*MD);
3732
3733 if (const Argument *A = dyn_cast<Argument>(V))
3734 return A->getRange();
3735 else if (const CallBase *CB = dyn_cast<CallBase>(V))
3736 return CB->getRange();
3737
3738 return std::nullopt;
3739 }
3740
3741 /// Given operands for an ICmpInst, see if we can fold the result.
3742 /// If not, this returns null.
simplifyICmpInst(CmpPredicate Pred,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)3743 static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
3744 const SimplifyQuery &Q, unsigned MaxRecurse) {
3745 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
3746
3747 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3748 if (Constant *CRHS = dyn_cast<Constant>(RHS))
3749 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3750
3751 // If we have a constant, make sure it is on the RHS.
3752 std::swap(LHS, RHS);
3753 Pred = CmpInst::getSwappedPredicate(Pred);
3754 }
3755 assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X");
3756
3757 Type *ITy = getCompareTy(LHS); // The return type.
3758
3759 // icmp poison, X -> poison
3760 if (isa<PoisonValue>(RHS))
3761 return PoisonValue::get(ITy);
3762
3763 // For EQ and NE, we can always pick a value for the undef to make the
3764 // predicate pass or fail, so we can return undef.
3765 // Matches behavior in llvm::ConstantFoldCompareInstruction.
3766 if (Q.isUndefValue(RHS) && ICmpInst::isEquality(Pred))
3767 return UndefValue::get(ITy);
3768
3769 // icmp X, X -> true/false
3770 // icmp X, undef -> true/false because undef could be X.
3771 if (LHS == RHS || Q.isUndefValue(RHS))
3772 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
3773
3774 if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3775 return V;
3776
3777 // TODO: Sink/common this with other potentially expensive calls that use
3778 // ValueTracking? See comment below for isKnownNonEqual().
3779 if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3780 return V;
3781
3782 if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q))
3783 return V;
3784
3785 // If both operands have range metadata, use the metadata
3786 // to simplify the comparison.
3787 if (std::optional<ConstantRange> RhsCr = getRange(RHS, Q.IIQ))
3788 if (std::optional<ConstantRange> LhsCr = getRange(LHS, Q.IIQ)) {
3789 if (LhsCr->icmp(Pred, *RhsCr))
3790 return ConstantInt::getTrue(ITy);
3791
3792 if (LhsCr->icmp(CmpInst::getInversePredicate(Pred), *RhsCr))
3793 return ConstantInt::getFalse(ITy);
3794 }
3795
3796 // Compare of cast, for example (zext X) != 0 -> X != 0
3797 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
3798 Instruction *LI = cast<CastInst>(LHS);
3799 Value *SrcOp = LI->getOperand(0);
3800 Type *SrcTy = SrcOp->getType();
3801 Type *DstTy = LI->getType();
3802
3803 // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
3804 // if the integer type is the same size as the pointer type.
3805 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3806 Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
3807 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
3808 // Transfer the cast to the constant.
3809 if (Value *V = simplifyICmpInst(Pred, SrcOp,
3810 ConstantExpr::getIntToPtr(RHSC, SrcTy),
3811 Q, MaxRecurse - 1))
3812 return V;
3813 } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3814 if (RI->getOperand(0)->getType() == SrcTy)
3815 // Compare without the cast.
3816 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3817 MaxRecurse - 1))
3818 return V;
3819 }
3820 }
3821
3822 if (isa<ZExtInst>(LHS)) {
3823 // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3824 // same type.
3825 if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3826 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3827 // Compare X and Y. Note that signed predicates become unsigned.
3828 if (Value *V =
3829 simplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), SrcOp,
3830 RI->getOperand(0), Q, MaxRecurse - 1))
3831 return V;
3832 }
3833 // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
3834 else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3835 if (SrcOp == RI->getOperand(0)) {
3836 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3837 return ConstantInt::getTrue(ITy);
3838 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3839 return ConstantInt::getFalse(ITy);
3840 }
3841 }
3842 // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3843 // too. If not, then try to deduce the result of the comparison.
3844 else if (match(RHS, m_ImmConstant())) {
3845 Constant *C = dyn_cast<Constant>(RHS);
3846 assert(C != nullptr);
3847
3848 // Compute the constant that would happen if we truncated to SrcTy then
3849 // reextended to DstTy.
3850 Constant *Trunc =
3851 ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3852 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3853 Constant *RExt =
3854 ConstantFoldCastOperand(CastInst::ZExt, Trunc, DstTy, Q.DL);
3855 assert(RExt && "Constant-fold of ImmConstant should not fail");
3856 Constant *AnyEq =
3857 ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3858 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3859
3860 // If the re-extended constant didn't change any of the elements then
3861 // this is effectively also a case of comparing two zero-extended
3862 // values.
3863 if (AnyEq->isAllOnesValue() && MaxRecurse)
3864 if (Value *V = simplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
3865 SrcOp, Trunc, Q, MaxRecurse - 1))
3866 return V;
3867
3868 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3869 // there. Use this to work out the result of the comparison.
3870 if (AnyEq->isNullValue()) {
3871 switch (Pred) {
3872 default:
3873 llvm_unreachable("Unknown ICmp predicate!");
3874 // LHS <u RHS.
3875 case ICmpInst::ICMP_EQ:
3876 case ICmpInst::ICMP_UGT:
3877 case ICmpInst::ICMP_UGE:
3878 return Constant::getNullValue(ITy);
3879
3880 case ICmpInst::ICMP_NE:
3881 case ICmpInst::ICMP_ULT:
3882 case ICmpInst::ICMP_ULE:
3883 return Constant::getAllOnesValue(ITy);
3884
3885 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
3886 // is non-negative then LHS <s RHS.
3887 case ICmpInst::ICMP_SGT:
3888 case ICmpInst::ICMP_SGE:
3889 return ConstantFoldCompareInstOperands(
3890 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3891 Q.DL);
3892 case ICmpInst::ICMP_SLT:
3893 case ICmpInst::ICMP_SLE:
3894 return ConstantFoldCompareInstOperands(
3895 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3896 Q.DL);
3897 }
3898 }
3899 }
3900 }
3901
3902 if (isa<SExtInst>(LHS)) {
3903 // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
3904 // same type.
3905 if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3906 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3907 // Compare X and Y. Note that the predicate does not change.
3908 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3909 MaxRecurse - 1))
3910 return V;
3911 }
3912 // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
3913 else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3914 if (SrcOp == RI->getOperand(0)) {
3915 if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3916 return ConstantInt::getTrue(ITy);
3917 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3918 return ConstantInt::getFalse(ITy);
3919 }
3920 }
3921 // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
3922 // too. If not, then try to deduce the result of the comparison.
3923 else if (match(RHS, m_ImmConstant())) {
3924 Constant *C = cast<Constant>(RHS);
3925
3926 // Compute the constant that would happen if we truncated to SrcTy then
3927 // reextended to DstTy.
3928 Constant *Trunc =
3929 ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3930 assert(Trunc && "Constant-fold of ImmConstant should not fail");
3931 Constant *RExt =
3932 ConstantFoldCastOperand(CastInst::SExt, Trunc, DstTy, Q.DL);
3933 assert(RExt && "Constant-fold of ImmConstant should not fail");
3934 Constant *AnyEq =
3935 ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3936 assert(AnyEq && "Constant-fold of ImmConstant should not fail");
3937
3938 // If the re-extended constant didn't change then this is effectively
3939 // also a case of comparing two sign-extended values.
3940 if (AnyEq->isAllOnesValue() && MaxRecurse)
3941 if (Value *V =
3942 simplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1))
3943 return V;
3944
3945 // Otherwise the upper bits of LHS are all equal, while RHS has varying
3946 // bits there. Use this to work out the result of the comparison.
3947 if (AnyEq->isNullValue()) {
3948 switch (Pred) {
3949 default:
3950 llvm_unreachable("Unknown ICmp predicate!");
3951 case ICmpInst::ICMP_EQ:
3952 return Constant::getNullValue(ITy);
3953 case ICmpInst::ICMP_NE:
3954 return Constant::getAllOnesValue(ITy);
3955
3956 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
3957 // LHS >s RHS.
3958 case ICmpInst::ICMP_SGT:
3959 case ICmpInst::ICMP_SGE:
3960 return ConstantFoldCompareInstOperands(
3961 ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()),
3962 Q.DL);
3963 case ICmpInst::ICMP_SLT:
3964 case ICmpInst::ICMP_SLE:
3965 return ConstantFoldCompareInstOperands(
3966 ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()),
3967 Q.DL);
3968
3969 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
3970 // LHS >u RHS.
3971 case ICmpInst::ICMP_UGT:
3972 case ICmpInst::ICMP_UGE:
3973 // Comparison is true iff the LHS <s 0.
3974 if (MaxRecurse)
3975 if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
3976 Constant::getNullValue(SrcTy), Q,
3977 MaxRecurse - 1))
3978 return V;
3979 break;
3980 case ICmpInst::ICMP_ULT:
3981 case ICmpInst::ICMP_ULE:
3982 // Comparison is true iff the LHS >=s 0.
3983 if (MaxRecurse)
3984 if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
3985 Constant::getNullValue(SrcTy), Q,
3986 MaxRecurse - 1))
3987 return V;
3988 break;
3989 }
3990 }
3991 }
3992 }
3993 }
3994
3995 // icmp eq|ne X, Y -> false|true if X != Y
3996 // This is potentially expensive, and we have already computedKnownBits for
3997 // compares with 0 above here, so only try this for a non-zero compare.
3998 if (ICmpInst::isEquality(Pred) && !match(RHS, m_Zero()) &&
3999 isKnownNonEqual(LHS, RHS, Q)) {
4000 return Pred == ICmpInst::ICMP_NE ? getTrue(ITy) : getFalse(ITy);
4001 }
4002
4003 if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
4004 return V;
4005
4006 if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
4007 return V;
4008
4009 if (Value *V = simplifyICmpWithIntrinsicOnLHS(Pred, LHS, RHS))
4010 return V;
4011 if (Value *V = simplifyICmpWithIntrinsicOnLHS(
4012 ICmpInst::getSwappedPredicate(Pred), RHS, LHS))
4013 return V;
4014
4015 if (Value *V = simplifyICmpUsingMonotonicValues(Pred, LHS, RHS, Q))
4016 return V;
4017 if (Value *V = simplifyICmpUsingMonotonicValues(
4018 ICmpInst::getSwappedPredicate(Pred), RHS, LHS, Q))
4019 return V;
4020
4021 if (Value *V = simplifyICmpWithDominatingAssume(Pred, LHS, RHS, Q))
4022 return V;
4023
4024 if (std::optional<bool> Res =
4025 isImpliedByDomCondition(Pred, LHS, RHS, Q.CxtI, Q.DL))
4026 return ConstantInt::getBool(ITy, *Res);
4027
4028 // Simplify comparisons of related pointers using a powerful, recursive
4029 // GEP-walk when we have target data available..
4030 if (LHS->getType()->isPointerTy())
4031 if (auto *C = computePointerICmp(Pred, LHS, RHS, Q))
4032 return C;
4033 if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
4034 if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
4035 if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
4036 Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
4037 Q.DL.getTypeSizeInBits(CLHS->getType()))
4038 if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
4039 CRHS->getPointerOperand(), Q))
4040 return C;
4041
4042 // If the comparison is with the result of a select instruction, check whether
4043 // comparing with either branch of the select always yields the same value.
4044 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4045 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4046 return V;
4047
4048 // If the comparison is with the result of a phi instruction, check whether
4049 // doing the compare with each incoming phi value yields a common result.
4050 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4051 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4052 return V;
4053
4054 return nullptr;
4055 }
4056
simplifyICmpInst(CmpPredicate Predicate,Value * LHS,Value * RHS,const SimplifyQuery & Q)4057 Value *llvm::simplifyICmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
4058 const SimplifyQuery &Q) {
4059 return ::simplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
4060 }
4061
4062 /// Given operands for an FCmpInst, see if we can fold the result.
4063 /// If not, this returns null.
simplifyFCmpInst(CmpPredicate Pred,Value * LHS,Value * RHS,FastMathFlags FMF,const SimplifyQuery & Q,unsigned MaxRecurse)4064 static Value *simplifyFCmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
4065 FastMathFlags FMF, const SimplifyQuery &Q,
4066 unsigned MaxRecurse) {
4067 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
4068
4069 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
4070 if (Constant *CRHS = dyn_cast<Constant>(RHS))
4071 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI,
4072 Q.CxtI);
4073
4074 // If we have a constant, make sure it is on the RHS.
4075 std::swap(LHS, RHS);
4076 Pred = CmpInst::getSwappedPredicate(Pred);
4077 }
4078
4079 // Fold trivial predicates.
4080 Type *RetTy = getCompareTy(LHS);
4081 if (Pred == FCmpInst::FCMP_FALSE)
4082 return getFalse(RetTy);
4083 if (Pred == FCmpInst::FCMP_TRUE)
4084 return getTrue(RetTy);
4085
4086 // fcmp pred x, poison and fcmp pred poison, x
4087 // fold to poison
4088 if (isa<PoisonValue>(LHS) || isa<PoisonValue>(RHS))
4089 return PoisonValue::get(RetTy);
4090
4091 // fcmp pred x, undef and fcmp pred undef, x
4092 // fold to true if unordered, false if ordered
4093 if (Q.isUndefValue(LHS) || Q.isUndefValue(RHS)) {
4094 // Choosing NaN for the undef will always make unordered comparison succeed
4095 // and ordered comparison fail.
4096 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4097 }
4098
4099 // fcmp x,x -> true/false. Not all compares are foldable.
4100 if (LHS == RHS) {
4101 if (CmpInst::isTrueWhenEqual(Pred))
4102 return getTrue(RetTy);
4103 if (CmpInst::isFalseWhenEqual(Pred))
4104 return getFalse(RetTy);
4105 }
4106
4107 // Fold (un)ordered comparison if we can determine there are no NaNs.
4108 //
4109 // This catches the 2 variable input case, constants are handled below as a
4110 // class-like compare.
4111 if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
4112 KnownFPClass RHSClass = computeKnownFPClass(RHS, fcAllFlags, Q);
4113 KnownFPClass LHSClass = computeKnownFPClass(LHS, fcAllFlags, Q);
4114
4115 if (FMF.noNaNs() ||
4116 (RHSClass.isKnownNeverNaN() && LHSClass.isKnownNeverNaN()))
4117 return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD);
4118
4119 if (RHSClass.isKnownAlwaysNaN() || LHSClass.isKnownAlwaysNaN())
4120 return ConstantInt::get(RetTy, Pred == CmpInst::FCMP_UNO);
4121 }
4122
4123 const APFloat *C = nullptr;
4124 match(RHS, m_APFloatAllowPoison(C));
4125 std::optional<KnownFPClass> FullKnownClassLHS;
4126
4127 // Lazily compute the possible classes for LHS. Avoid computing it twice if
4128 // RHS is a 0.
4129 auto computeLHSClass = [=, &FullKnownClassLHS](FPClassTest InterestedFlags =
4130 fcAllFlags) {
4131 if (FullKnownClassLHS)
4132 return *FullKnownClassLHS;
4133 return computeKnownFPClass(LHS, FMF, InterestedFlags, Q);
4134 };
4135
4136 if (C && Q.CxtI) {
4137 // Fold out compares that express a class test.
4138 //
4139 // FIXME: Should be able to perform folds without context
4140 // instruction. Always pass in the context function?
4141
4142 const Function *ParentF = Q.CxtI->getFunction();
4143 auto [ClassVal, ClassTest] = fcmpToClassTest(Pred, *ParentF, LHS, C);
4144 if (ClassVal) {
4145 FullKnownClassLHS = computeLHSClass();
4146 if ((FullKnownClassLHS->KnownFPClasses & ClassTest) == fcNone)
4147 return getFalse(RetTy);
4148 if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) == fcNone)
4149 return getTrue(RetTy);
4150 }
4151 }
4152
4153 // Handle fcmp with constant RHS.
4154 if (C) {
4155 // TODO: If we always required a context function, we wouldn't need to
4156 // special case nans.
4157 if (C->isNaN())
4158 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
4159
4160 // TODO: Need version fcmpToClassTest which returns implied class when the
4161 // compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but
4162 // isn't implementable as a class call.
4163 if (C->isNegative() && !C->isNegZero()) {
4164 FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4165
4166 // TODO: We can catch more cases by using a range check rather than
4167 // relying on CannotBeOrderedLessThanZero.
4168 switch (Pred) {
4169 case FCmpInst::FCMP_UGE:
4170 case FCmpInst::FCMP_UGT:
4171 case FCmpInst::FCMP_UNE: {
4172 KnownFPClass KnownClass = computeLHSClass(Interested);
4173
4174 // (X >= 0) implies (X > C) when (C < 0)
4175 if (KnownClass.cannotBeOrderedLessThanZero())
4176 return getTrue(RetTy);
4177 break;
4178 }
4179 case FCmpInst::FCMP_OEQ:
4180 case FCmpInst::FCMP_OLE:
4181 case FCmpInst::FCMP_OLT: {
4182 KnownFPClass KnownClass = computeLHSClass(Interested);
4183
4184 // (X >= 0) implies !(X < C) when (C < 0)
4185 if (KnownClass.cannotBeOrderedLessThanZero())
4186 return getFalse(RetTy);
4187 break;
4188 }
4189 default:
4190 break;
4191 }
4192 }
4193 // Check comparison of [minnum/maxnum with constant] with other constant.
4194 const APFloat *C2;
4195 if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) &&
4196 *C2 < *C) ||
4197 (match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2))) &&
4198 *C2 > *C)) {
4199 bool IsMaxNum =
4200 cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
4201 // The ordered relationship and minnum/maxnum guarantee that we do not
4202 // have NaN constants, so ordered/unordered preds are handled the same.
4203 switch (Pred) {
4204 case FCmpInst::FCMP_OEQ:
4205 case FCmpInst::FCMP_UEQ:
4206 // minnum(X, LesserC) == C --> false
4207 // maxnum(X, GreaterC) == C --> false
4208 return getFalse(RetTy);
4209 case FCmpInst::FCMP_ONE:
4210 case FCmpInst::FCMP_UNE:
4211 // minnum(X, LesserC) != C --> true
4212 // maxnum(X, GreaterC) != C --> true
4213 return getTrue(RetTy);
4214 case FCmpInst::FCMP_OGE:
4215 case FCmpInst::FCMP_UGE:
4216 case FCmpInst::FCMP_OGT:
4217 case FCmpInst::FCMP_UGT:
4218 // minnum(X, LesserC) >= C --> false
4219 // minnum(X, LesserC) > C --> false
4220 // maxnum(X, GreaterC) >= C --> true
4221 // maxnum(X, GreaterC) > C --> true
4222 return ConstantInt::get(RetTy, IsMaxNum);
4223 case FCmpInst::FCMP_OLE:
4224 case FCmpInst::FCMP_ULE:
4225 case FCmpInst::FCMP_OLT:
4226 case FCmpInst::FCMP_ULT:
4227 // minnum(X, LesserC) <= C --> true
4228 // minnum(X, LesserC) < C --> true
4229 // maxnum(X, GreaterC) <= C --> false
4230 // maxnum(X, GreaterC) < C --> false
4231 return ConstantInt::get(RetTy, !IsMaxNum);
4232 default:
4233 // TRUE/FALSE/ORD/UNO should be handled before this.
4234 llvm_unreachable("Unexpected fcmp predicate");
4235 }
4236 }
4237 }
4238
4239 // TODO: Could fold this with above if there were a matcher which returned all
4240 // classes in a non-splat vector.
4241 if (match(RHS, m_AnyZeroFP())) {
4242 switch (Pred) {
4243 case FCmpInst::FCMP_OGE:
4244 case FCmpInst::FCMP_ULT: {
4245 FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4246 if (!FMF.noNaNs())
4247 Interested |= fcNan;
4248
4249 KnownFPClass Known = computeLHSClass(Interested);
4250
4251 // Positive or zero X >= 0.0 --> true
4252 // Positive or zero X < 0.0 --> false
4253 if ((FMF.noNaNs() || Known.isKnownNeverNaN()) &&
4254 Known.cannotBeOrderedLessThanZero())
4255 return Pred == FCmpInst::FCMP_OGE ? getTrue(RetTy) : getFalse(RetTy);
4256 break;
4257 }
4258 case FCmpInst::FCMP_UGE:
4259 case FCmpInst::FCMP_OLT: {
4260 FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask;
4261 KnownFPClass Known = computeLHSClass(Interested);
4262
4263 // Positive or zero or nan X >= 0.0 --> true
4264 // Positive or zero or nan X < 0.0 --> false
4265 if (Known.cannotBeOrderedLessThanZero())
4266 return Pred == FCmpInst::FCMP_UGE ? getTrue(RetTy) : getFalse(RetTy);
4267 break;
4268 }
4269 default:
4270 break;
4271 }
4272 }
4273
4274 // If the comparison is with the result of a select instruction, check whether
4275 // comparing with either branch of the select always yields the same value.
4276 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4277 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4278 return V;
4279
4280 // If the comparison is with the result of a phi instruction, check whether
4281 // doing the compare with each incoming phi value yields a common result.
4282 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4283 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4284 return V;
4285
4286 return nullptr;
4287 }
4288
simplifyFCmpInst(CmpPredicate Predicate,Value * LHS,Value * RHS,FastMathFlags FMF,const SimplifyQuery & Q)4289 Value *llvm::simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
4290 FastMathFlags FMF, const SimplifyQuery &Q) {
4291 return ::simplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit);
4292 }
4293
simplifyWithOpsReplaced(Value * V,ArrayRef<std::pair<Value *,Value * >> Ops,const SimplifyQuery & Q,bool AllowRefinement,SmallVectorImpl<Instruction * > * DropFlags,unsigned MaxRecurse)4294 static Value *simplifyWithOpsReplaced(Value *V,
4295 ArrayRef<std::pair<Value *, Value *>> Ops,
4296 const SimplifyQuery &Q,
4297 bool AllowRefinement,
4298 SmallVectorImpl<Instruction *> *DropFlags,
4299 unsigned MaxRecurse) {
4300 assert((AllowRefinement || !Q.CanUseUndef) &&
4301 "If AllowRefinement=false then CanUseUndef=false");
4302 for (const auto &OpAndRepOp : Ops) {
4303 // We cannot replace a constant, and shouldn't even try.
4304 if (isa<Constant>(OpAndRepOp.first))
4305 return nullptr;
4306
4307 // Trivial replacement.
4308 if (V == OpAndRepOp.first)
4309 return OpAndRepOp.second;
4310 }
4311
4312 if (!MaxRecurse--)
4313 return nullptr;
4314
4315 auto *I = dyn_cast<Instruction>(V);
4316 if (!I)
4317 return nullptr;
4318
4319 // The arguments of a phi node might refer to a value from a previous
4320 // cycle iteration.
4321 if (isa<PHINode>(I))
4322 return nullptr;
4323
4324 // Don't fold away llvm.is.constant checks based on assumptions.
4325 if (match(I, m_Intrinsic<Intrinsic::is_constant>()))
4326 return nullptr;
4327
4328 // Don't simplify freeze.
4329 if (isa<FreezeInst>(I))
4330 return nullptr;
4331
4332 for (const auto &OpAndRepOp : Ops) {
4333 // For vector types, the simplification must hold per-lane, so forbid
4334 // potentially cross-lane operations like shufflevector.
4335 if (OpAndRepOp.first->getType()->isVectorTy() &&
4336 !isNotCrossLaneOperation(I))
4337 return nullptr;
4338 }
4339
4340 // Replace Op with RepOp in instruction operands.
4341 SmallVector<Value *, 8> NewOps;
4342 bool AnyReplaced = false;
4343 for (Value *InstOp : I->operands()) {
4344 if (Value *NewInstOp = simplifyWithOpsReplaced(
4345 InstOp, Ops, Q, AllowRefinement, DropFlags, MaxRecurse)) {
4346 NewOps.push_back(NewInstOp);
4347 AnyReplaced = InstOp != NewInstOp;
4348 } else {
4349 NewOps.push_back(InstOp);
4350 }
4351
4352 // Bail out if any operand is undef and SimplifyQuery disables undef
4353 // simplification. Constant folding currently doesn't respect this option.
4354 if (isa<UndefValue>(NewOps.back()) && !Q.CanUseUndef)
4355 return nullptr;
4356 }
4357
4358 if (!AnyReplaced)
4359 return nullptr;
4360
4361 if (!AllowRefinement) {
4362 // General InstSimplify functions may refine the result, e.g. by returning
4363 // a constant for a potentially poison value. To avoid this, implement only
4364 // a few non-refining but profitable transforms here.
4365
4366 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
4367 unsigned Opcode = BO->getOpcode();
4368 // id op x -> x, x op id -> x
4369 // Exclude floats, because x op id may produce a different NaN value.
4370 if (!BO->getType()->isFPOrFPVectorTy()) {
4371 if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, I->getType()))
4372 return NewOps[1];
4373 if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, I->getType(),
4374 /* RHS */ true))
4375 return NewOps[0];
4376 }
4377
4378 // x & x -> x, x | x -> x
4379 if ((Opcode == Instruction::And || Opcode == Instruction::Or) &&
4380 NewOps[0] == NewOps[1]) {
4381 // or disjoint x, x results in poison.
4382 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(BO)) {
4383 if (PDI->isDisjoint()) {
4384 if (!DropFlags)
4385 return nullptr;
4386 DropFlags->push_back(BO);
4387 }
4388 }
4389 return NewOps[0];
4390 }
4391
4392 // x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison
4393 // by assumption and this case never wraps, so nowrap flags can be
4394 // ignored.
4395 if ((Opcode == Instruction::Sub || Opcode == Instruction::Xor) &&
4396 NewOps[0] == NewOps[1] &&
4397 any_of(Ops, [=](const auto &Rep) { return NewOps[0] == Rep.second; }))
4398 return Constant::getNullValue(I->getType());
4399
4400 // If we are substituting an absorber constant into a binop and extra
4401 // poison can't leak if we remove the select -- because both operands of
4402 // the binop are based on the same value -- then it may be safe to replace
4403 // the value with the absorber constant. Examples:
4404 // (Op == 0) ? 0 : (Op & -Op) --> Op & -Op
4405 // (Op == 0) ? 0 : (Op * (binop Op, C)) --> Op * (binop Op, C)
4406 // (Op == -1) ? -1 : (Op | (binop C, Op) --> Op | (binop C, Op)
4407 Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, I->getType());
4408 if ((NewOps[0] == Absorber || NewOps[1] == Absorber) &&
4409 any_of(Ops,
4410 [=](const auto &Rep) { return impliesPoison(BO, Rep.first); }))
4411 return Absorber;
4412 }
4413
4414 if (isa<GetElementPtrInst>(I)) {
4415 // getelementptr x, 0 -> x.
4416 // This never returns poison, even if inbounds is set.
4417 if (NewOps.size() == 2 && match(NewOps[1], m_Zero()))
4418 return NewOps[0];
4419 }
4420 } else {
4421 // The simplification queries below may return the original value. Consider:
4422 // %div = udiv i32 %arg, %arg2
4423 // %mul = mul nsw i32 %div, %arg2
4424 // %cmp = icmp eq i32 %mul, %arg
4425 // %sel = select i1 %cmp, i32 %div, i32 undef
4426 // Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which
4427 // simplifies back to %arg. This can only happen because %mul does not
4428 // dominate %div. To ensure a consistent return value contract, we make sure
4429 // that this case returns nullptr as well.
4430 auto PreventSelfSimplify = [V](Value *Simplified) {
4431 return Simplified != V ? Simplified : nullptr;
4432 };
4433
4434 return PreventSelfSimplify(
4435 ::simplifyInstructionWithOperands(I, NewOps, Q, MaxRecurse));
4436 }
4437
4438 // If all operands are constant after substituting Op for RepOp then we can
4439 // constant fold the instruction.
4440 SmallVector<Constant *, 8> ConstOps;
4441 for (Value *NewOp : NewOps) {
4442 if (Constant *ConstOp = dyn_cast<Constant>(NewOp))
4443 ConstOps.push_back(ConstOp);
4444 else
4445 return nullptr;
4446 }
4447
4448 // Consider:
4449 // %cmp = icmp eq i32 %x, 2147483647
4450 // %add = add nsw i32 %x, 1
4451 // %sel = select i1 %cmp, i32 -2147483648, i32 %add
4452 //
4453 // We can't replace %sel with %add unless we strip away the flags (which
4454 // will be done in InstCombine).
4455 // TODO: This may be unsound, because it only catches some forms of
4456 // refinement.
4457 if (!AllowRefinement) {
4458 if (canCreatePoison(cast<Operator>(I), !DropFlags)) {
4459 // abs cannot create poison if the value is known to never be int_min.
4460 if (auto *II = dyn_cast<IntrinsicInst>(I);
4461 II && II->getIntrinsicID() == Intrinsic::abs) {
4462 if (!ConstOps[0]->isNotMinSignedValue())
4463 return nullptr;
4464 } else
4465 return nullptr;
4466 }
4467 Constant *Res = ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI,
4468 /*AllowNonDeterministic=*/false);
4469 if (DropFlags && Res && I->hasPoisonGeneratingAnnotations())
4470 DropFlags->push_back(I);
4471 return Res;
4472 }
4473
4474 return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI,
4475 /*AllowNonDeterministic=*/false);
4476 }
4477
simplifyWithOpReplaced(Value * V,Value * Op,Value * RepOp,const SimplifyQuery & Q,bool AllowRefinement,SmallVectorImpl<Instruction * > * DropFlags,unsigned MaxRecurse)4478 static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4479 const SimplifyQuery &Q,
4480 bool AllowRefinement,
4481 SmallVectorImpl<Instruction *> *DropFlags,
4482 unsigned MaxRecurse) {
4483 return simplifyWithOpsReplaced(V, {{Op, RepOp}}, Q, AllowRefinement,
4484 DropFlags, MaxRecurse);
4485 }
4486
simplifyWithOpReplaced(Value * V,Value * Op,Value * RepOp,const SimplifyQuery & Q,bool AllowRefinement,SmallVectorImpl<Instruction * > * DropFlags)4487 Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4488 const SimplifyQuery &Q,
4489 bool AllowRefinement,
4490 SmallVectorImpl<Instruction *> *DropFlags) {
4491 // If refinement is disabled, also disable undef simplifications (which are
4492 // always refinements) in SimplifyQuery.
4493 if (!AllowRefinement)
4494 return ::simplifyWithOpReplaced(V, Op, RepOp, Q.getWithoutUndef(),
4495 AllowRefinement, DropFlags, RecursionLimit);
4496 return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement, DropFlags,
4497 RecursionLimit);
4498 }
4499
4500 /// Try to simplify a select instruction when its condition operand is an
4501 /// integer comparison where one operand of the compare is a constant.
simplifySelectBitTest(Value * TrueVal,Value * FalseVal,Value * X,const APInt * Y,bool TrueWhenUnset)4502 static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
4503 const APInt *Y, bool TrueWhenUnset) {
4504 const APInt *C;
4505
4506 // (X & Y) == 0 ? X & ~Y : X --> X
4507 // (X & Y) != 0 ? X & ~Y : X --> X & ~Y
4508 if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) &&
4509 *Y == ~*C)
4510 return TrueWhenUnset ? FalseVal : TrueVal;
4511
4512 // (X & Y) == 0 ? X : X & ~Y --> X & ~Y
4513 // (X & Y) != 0 ? X : X & ~Y --> X
4514 if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) &&
4515 *Y == ~*C)
4516 return TrueWhenUnset ? FalseVal : TrueVal;
4517
4518 if (Y->isPowerOf2()) {
4519 // (X & Y) == 0 ? X | Y : X --> X | Y
4520 // (X & Y) != 0 ? X | Y : X --> X
4521 if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) &&
4522 *Y == *C) {
4523 // We can't return the or if it has the disjoint flag.
4524 if (TrueWhenUnset && cast<PossiblyDisjointInst>(TrueVal)->isDisjoint())
4525 return nullptr;
4526 return TrueWhenUnset ? TrueVal : FalseVal;
4527 }
4528
4529 // (X & Y) == 0 ? X : X | Y --> X
4530 // (X & Y) != 0 ? X : X | Y --> X | Y
4531 if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) &&
4532 *Y == *C) {
4533 // We can't return the or if it has the disjoint flag.
4534 if (!TrueWhenUnset && cast<PossiblyDisjointInst>(FalseVal)->isDisjoint())
4535 return nullptr;
4536 return TrueWhenUnset ? TrueVal : FalseVal;
4537 }
4538 }
4539
4540 return nullptr;
4541 }
4542
simplifyCmpSelOfMaxMin(Value * CmpLHS,Value * CmpRHS,CmpPredicate Pred,Value * TVal,Value * FVal)4543 static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS,
4544 CmpPredicate Pred, Value *TVal,
4545 Value *FVal) {
4546 // Canonicalize common cmp+sel operand as CmpLHS.
4547 if (CmpRHS == TVal || CmpRHS == FVal) {
4548 std::swap(CmpLHS, CmpRHS);
4549 Pred = ICmpInst::getSwappedPredicate(Pred);
4550 }
4551
4552 // Canonicalize common cmp+sel operand as TVal.
4553 if (CmpLHS == FVal) {
4554 std::swap(TVal, FVal);
4555 Pred = ICmpInst::getInversePredicate(Pred);
4556 }
4557
4558 // A vector select may be shuffling together elements that are equivalent
4559 // based on the max/min/select relationship.
4560 Value *X = CmpLHS, *Y = CmpRHS;
4561 bool PeekedThroughSelectShuffle = false;
4562 auto *Shuf = dyn_cast<ShuffleVectorInst>(FVal);
4563 if (Shuf && Shuf->isSelect()) {
4564 if (Shuf->getOperand(0) == Y)
4565 FVal = Shuf->getOperand(1);
4566 else if (Shuf->getOperand(1) == Y)
4567 FVal = Shuf->getOperand(0);
4568 else
4569 return nullptr;
4570 PeekedThroughSelectShuffle = true;
4571 }
4572
4573 // (X pred Y) ? X : max/min(X, Y)
4574 auto *MMI = dyn_cast<MinMaxIntrinsic>(FVal);
4575 if (!MMI || TVal != X ||
4576 !match(FVal, m_c_MaxOrMin(m_Specific(X), m_Specific(Y))))
4577 return nullptr;
4578
4579 // (X > Y) ? X : max(X, Y) --> max(X, Y)
4580 // (X >= Y) ? X : max(X, Y) --> max(X, Y)
4581 // (X < Y) ? X : min(X, Y) --> min(X, Y)
4582 // (X <= Y) ? X : min(X, Y) --> min(X, Y)
4583 //
4584 // The equivalence allows a vector select (shuffle) of max/min and Y. Ex:
4585 // (X > Y) ? X : (Z ? max(X, Y) : Y)
4586 // If Z is true, this reduces as above, and if Z is false:
4587 // (X > Y) ? X : Y --> max(X, Y)
4588 ICmpInst::Predicate MMPred = MMI->getPredicate();
4589 if (MMPred == CmpInst::getStrictPredicate(Pred))
4590 return MMI;
4591
4592 // Other transforms are not valid with a shuffle.
4593 if (PeekedThroughSelectShuffle)
4594 return nullptr;
4595
4596 // (X == Y) ? X : max/min(X, Y) --> max/min(X, Y)
4597 if (Pred == CmpInst::ICMP_EQ)
4598 return MMI;
4599
4600 // (X != Y) ? X : max/min(X, Y) --> X
4601 if (Pred == CmpInst::ICMP_NE)
4602 return X;
4603
4604 // (X < Y) ? X : max(X, Y) --> X
4605 // (X <= Y) ? X : max(X, Y) --> X
4606 // (X > Y) ? X : min(X, Y) --> X
4607 // (X >= Y) ? X : min(X, Y) --> X
4608 ICmpInst::Predicate InvPred = CmpInst::getInversePredicate(Pred);
4609 if (MMPred == CmpInst::getStrictPredicate(InvPred))
4610 return X;
4611
4612 return nullptr;
4613 }
4614
4615 /// An alternative way to test if a bit is set or not.
4616 /// uses e.g. sgt/slt or trunc instead of eq/ne.
simplifySelectWithBitTest(Value * CondVal,Value * TrueVal,Value * FalseVal)4617 static Value *simplifySelectWithBitTest(Value *CondVal, Value *TrueVal,
4618 Value *FalseVal) {
4619 if (auto Res = decomposeBitTest(CondVal))
4620 return simplifySelectBitTest(TrueVal, FalseVal, Res->X, &Res->Mask,
4621 Res->Pred == ICmpInst::ICMP_EQ);
4622
4623 return nullptr;
4624 }
4625
4626 /// Try to simplify a select instruction when its condition operand is an
4627 /// integer equality or floating-point equivalence comparison.
simplifySelectWithEquivalence(ArrayRef<std::pair<Value *,Value * >> Replacements,Value * TrueVal,Value * FalseVal,const SimplifyQuery & Q,unsigned MaxRecurse)4628 static Value *simplifySelectWithEquivalence(
4629 ArrayRef<std::pair<Value *, Value *>> Replacements, Value *TrueVal,
4630 Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse) {
4631 Value *SimplifiedFalseVal =
4632 simplifyWithOpsReplaced(FalseVal, Replacements, Q.getWithoutUndef(),
4633 /* AllowRefinement */ false,
4634 /* DropFlags */ nullptr, MaxRecurse);
4635 if (!SimplifiedFalseVal)
4636 SimplifiedFalseVal = FalseVal;
4637
4638 Value *SimplifiedTrueVal =
4639 simplifyWithOpsReplaced(TrueVal, Replacements, Q,
4640 /* AllowRefinement */ true,
4641 /* DropFlags */ nullptr, MaxRecurse);
4642 if (!SimplifiedTrueVal)
4643 SimplifiedTrueVal = TrueVal;
4644
4645 if (SimplifiedFalseVal == SimplifiedTrueVal)
4646 return FalseVal;
4647
4648 return nullptr;
4649 }
4650
4651 /// Try to simplify a select instruction when its condition operand is an
4652 /// integer comparison.
simplifySelectWithICmpCond(Value * CondVal,Value * TrueVal,Value * FalseVal,const SimplifyQuery & Q,unsigned MaxRecurse)4653 static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4654 Value *FalseVal,
4655 const SimplifyQuery &Q,
4656 unsigned MaxRecurse) {
4657 CmpPredicate Pred;
4658 Value *CmpLHS, *CmpRHS;
4659 if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4660 return nullptr;
4661
4662 if (Value *V = simplifyCmpSelOfMaxMin(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal))
4663 return V;
4664
4665 // Canonicalize ne to eq predicate.
4666 if (Pred == ICmpInst::ICMP_NE) {
4667 Pred = ICmpInst::ICMP_EQ;
4668 std::swap(TrueVal, FalseVal);
4669 }
4670
4671 // Check for integer min/max with a limit constant:
4672 // X > MIN_INT ? X : MIN_INT --> X
4673 // X < MAX_INT ? X : MAX_INT --> X
4674 if (TrueVal->getType()->isIntOrIntVectorTy()) {
4675 Value *X, *Y;
4676 SelectPatternFlavor SPF =
4677 matchDecomposedSelectPattern(cast<ICmpInst>(CondVal), TrueVal, FalseVal,
4678 X, Y)
4679 .Flavor;
4680 if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) {
4681 APInt LimitC = getMinMaxLimit(getInverseMinMaxFlavor(SPF),
4682 X->getType()->getScalarSizeInBits());
4683 if (match(Y, m_SpecificInt(LimitC)))
4684 return X;
4685 }
4686 }
4687
4688 if (Pred == ICmpInst::ICMP_EQ && match(CmpRHS, m_Zero())) {
4689 Value *X;
4690 const APInt *Y;
4691 if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y))))
4692 if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
4693 /*TrueWhenUnset=*/true))
4694 return V;
4695
4696 // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
4697 Value *ShAmt;
4698 auto isFsh = m_CombineOr(m_FShl(m_Value(X), m_Value(), m_Value(ShAmt)),
4699 m_FShr(m_Value(), m_Value(X), m_Value(ShAmt)));
4700 // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
4701 // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
4702 if (match(TrueVal, isFsh) && FalseVal == X && CmpLHS == ShAmt)
4703 return X;
4704
4705 // Test for a zero-shift-guard-op around rotates. These are used to
4706 // avoid UB from oversized shifts in raw IR rotate patterns, but the
4707 // intrinsics do not have that problem.
4708 // We do not allow this transform for the general funnel shift case because
4709 // that would not preserve the poison safety of the original code.
4710 auto isRotate =
4711 m_CombineOr(m_FShl(m_Value(X), m_Deferred(X), m_Value(ShAmt)),
4712 m_FShr(m_Value(X), m_Deferred(X), m_Value(ShAmt)));
4713 // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4714 // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4715 if (match(FalseVal, isRotate) && TrueVal == X && CmpLHS == ShAmt &&
4716 Pred == ICmpInst::ICMP_EQ)
4717 return FalseVal;
4718
4719 // X == 0 ? abs(X) : -abs(X) --> -abs(X)
4720 // X == 0 ? -abs(X) : abs(X) --> abs(X)
4721 if (match(TrueVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))) &&
4722 match(FalseVal, m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))))
4723 return FalseVal;
4724 if (match(TrueVal,
4725 m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))) &&
4726 match(FalseVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))))
4727 return FalseVal;
4728 }
4729
4730 // If we have a scalar equality comparison, then we know the value in one of
4731 // the arms of the select. See if substituting this value into the arm and
4732 // simplifying the result yields the same value as the other arm.
4733 if (Pred == ICmpInst::ICMP_EQ) {
4734 if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4735 canReplacePointersIfEqual(CmpLHS, CmpRHS, Q.DL))
4736 if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
4737 FalseVal, Q, MaxRecurse))
4738 return V;
4739 if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4740 canReplacePointersIfEqual(CmpRHS, CmpLHS, Q.DL))
4741 if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
4742 FalseVal, Q, MaxRecurse))
4743 return V;
4744
4745 Value *X;
4746 Value *Y;
4747 // select((X | Y) == 0 ? X : 0) --> 0 (commuted 2 ways)
4748 if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) &&
4749 match(CmpRHS, m_Zero())) {
4750 // (X | Y) == 0 implies X == 0 and Y == 0.
4751 if (Value *V = simplifySelectWithEquivalence(
4752 {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4753 return V;
4754 }
4755
4756 // select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways)
4757 if (match(CmpLHS, m_And(m_Value(X), m_Value(Y))) &&
4758 match(CmpRHS, m_AllOnes())) {
4759 // (X & Y) == -1 implies X == -1 and Y == -1.
4760 if (Value *V = simplifySelectWithEquivalence(
4761 {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4762 return V;
4763 }
4764 }
4765
4766 return nullptr;
4767 }
4768
4769 /// Try to simplify a select instruction when its condition operand is a
4770 /// floating-point comparison.
simplifySelectWithFCmp(Value * Cond,Value * T,Value * F,const SimplifyQuery & Q,unsigned MaxRecurse)4771 static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F,
4772 const SimplifyQuery &Q,
4773 unsigned MaxRecurse) {
4774 CmpPredicate Pred;
4775 Value *CmpLHS, *CmpRHS;
4776 if (!match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4777 return nullptr;
4778 FCmpInst *I = cast<FCmpInst>(Cond);
4779
4780 bool IsEquiv = I->isEquivalence();
4781 if (I->isEquivalence(/*Invert=*/true)) {
4782 std::swap(T, F);
4783 Pred = FCmpInst::getInversePredicate(Pred);
4784 IsEquiv = true;
4785 }
4786
4787 // This transforms is safe if at least one operand is known to not be zero.
4788 // Otherwise, the select can change the sign of a zero operand.
4789 if (IsEquiv) {
4790 if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, T, F, Q,
4791 MaxRecurse))
4792 return V;
4793 if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, T, F, Q,
4794 MaxRecurse))
4795 return V;
4796 }
4797
4798 // Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped.
4799 if (CmpLHS == F && CmpRHS == T)
4800 std::swap(CmpLHS, CmpRHS);
4801
4802 if (CmpLHS != T || CmpRHS != F)
4803 return nullptr;
4804
4805 // This transform is also safe if we do not have (do not care about) -0.0.
4806 if (Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros()) {
4807 // (T == F) ? T : F --> F
4808 if (Pred == FCmpInst::FCMP_OEQ)
4809 return F;
4810
4811 // (T != F) ? T : F --> T
4812 if (Pred == FCmpInst::FCMP_UNE)
4813 return T;
4814 }
4815
4816 return nullptr;
4817 }
4818
4819 /// Given operands for a SelectInst, see if we can fold the result.
4820 /// If not, this returns null.
simplifySelectInst(Value * Cond,Value * TrueVal,Value * FalseVal,const SimplifyQuery & Q,unsigned MaxRecurse)4821 static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4822 const SimplifyQuery &Q, unsigned MaxRecurse) {
4823 if (auto *CondC = dyn_cast<Constant>(Cond)) {
4824 if (auto *TrueC = dyn_cast<Constant>(TrueVal))
4825 if (auto *FalseC = dyn_cast<Constant>(FalseVal))
4826 if (Constant *C = ConstantFoldSelectInstruction(CondC, TrueC, FalseC))
4827 return C;
4828
4829 // select poison, X, Y -> poison
4830 if (isa<PoisonValue>(CondC))
4831 return PoisonValue::get(TrueVal->getType());
4832
4833 // select undef, X, Y -> X or Y
4834 if (Q.isUndefValue(CondC))
4835 return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
4836
4837 // select true, X, Y --> X
4838 // select false, X, Y --> Y
4839 // For vectors, allow undef/poison elements in the condition to match the
4840 // defined elements, so we can eliminate the select.
4841 if (match(CondC, m_One()))
4842 return TrueVal;
4843 if (match(CondC, m_Zero()))
4844 return FalseVal;
4845 }
4846
4847 assert(Cond->getType()->isIntOrIntVectorTy(1) &&
4848 "Select must have bool or bool vector condition");
4849 assert(TrueVal->getType() == FalseVal->getType() &&
4850 "Select must have same types for true/false ops");
4851
4852 if (Cond->getType() == TrueVal->getType()) {
4853 // select i1 Cond, i1 true, i1 false --> i1 Cond
4854 if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
4855 return Cond;
4856
4857 // (X && Y) ? X : Y --> Y (commuted 2 ways)
4858 if (match(Cond, m_c_LogicalAnd(m_Specific(TrueVal), m_Specific(FalseVal))))
4859 return FalseVal;
4860
4861 // (X || Y) ? X : Y --> X (commuted 2 ways)
4862 if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal))))
4863 return TrueVal;
4864
4865 // (X || Y) ? false : X --> false (commuted 2 ways)
4866 if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) &&
4867 match(TrueVal, m_ZeroInt()))
4868 return ConstantInt::getFalse(Cond->getType());
4869
4870 // Match patterns that end in logical-and.
4871 if (match(FalseVal, m_ZeroInt())) {
4872 // !(X || Y) && X --> false (commuted 2 ways)
4873 if (match(Cond, m_Not(m_c_LogicalOr(m_Specific(TrueVal), m_Value()))))
4874 return ConstantInt::getFalse(Cond->getType());
4875 // X && !(X || Y) --> false (commuted 2 ways)
4876 if (match(TrueVal, m_Not(m_c_LogicalOr(m_Specific(Cond), m_Value()))))
4877 return ConstantInt::getFalse(Cond->getType());
4878
4879 // (X || Y) && Y --> Y (commuted 2 ways)
4880 if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Value())))
4881 return TrueVal;
4882 // Y && (X || Y) --> Y (commuted 2 ways)
4883 if (match(TrueVal, m_c_LogicalOr(m_Specific(Cond), m_Value())))
4884 return Cond;
4885
4886 // (X || Y) && (X || !Y) --> X (commuted 8 ways)
4887 Value *X, *Y;
4888 if (match(Cond, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
4889 match(TrueVal, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
4890 return X;
4891 if (match(TrueVal, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
4892 match(Cond, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
4893 return X;
4894 }
4895
4896 // Match patterns that end in logical-or.
4897 if (match(TrueVal, m_One())) {
4898 // !(X && Y) || X --> true (commuted 2 ways)
4899 if (match(Cond, m_Not(m_c_LogicalAnd(m_Specific(FalseVal), m_Value()))))
4900 return ConstantInt::getTrue(Cond->getType());
4901 // X || !(X && Y) --> true (commuted 2 ways)
4902 if (match(FalseVal, m_Not(m_c_LogicalAnd(m_Specific(Cond), m_Value()))))
4903 return ConstantInt::getTrue(Cond->getType());
4904
4905 // (X && Y) || Y --> Y (commuted 2 ways)
4906 if (match(Cond, m_c_LogicalAnd(m_Specific(FalseVal), m_Value())))
4907 return FalseVal;
4908 // Y || (X && Y) --> Y (commuted 2 ways)
4909 if (match(FalseVal, m_c_LogicalAnd(m_Specific(Cond), m_Value())))
4910 return Cond;
4911 }
4912 }
4913
4914 // select ?, X, X -> X
4915 if (TrueVal == FalseVal)
4916 return TrueVal;
4917
4918 if (Cond == TrueVal) {
4919 // select i1 X, i1 X, i1 false --> X (logical-and)
4920 if (match(FalseVal, m_ZeroInt()))
4921 return Cond;
4922 // select i1 X, i1 X, i1 true --> true
4923 if (match(FalseVal, m_One()))
4924 return ConstantInt::getTrue(Cond->getType());
4925 }
4926 if (Cond == FalseVal) {
4927 // select i1 X, i1 true, i1 X --> X (logical-or)
4928 if (match(TrueVal, m_One()))
4929 return Cond;
4930 // select i1 X, i1 false, i1 X --> false
4931 if (match(TrueVal, m_ZeroInt()))
4932 return ConstantInt::getFalse(Cond->getType());
4933 }
4934
4935 // If the true or false value is poison, we can fold to the other value.
4936 // If the true or false value is undef, we can fold to the other value as
4937 // long as the other value isn't poison.
4938 // select ?, poison, X -> X
4939 // select ?, undef, X -> X
4940 if (isa<PoisonValue>(TrueVal) ||
4941 (Q.isUndefValue(TrueVal) && impliesPoison(FalseVal, Cond)))
4942 return FalseVal;
4943 // select ?, X, poison -> X
4944 // select ?, X, undef -> X
4945 if (isa<PoisonValue>(FalseVal) ||
4946 (Q.isUndefValue(FalseVal) && impliesPoison(TrueVal, Cond)))
4947 return TrueVal;
4948
4949 // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
4950 Constant *TrueC, *FalseC;
4951 if (isa<FixedVectorType>(TrueVal->getType()) &&
4952 match(TrueVal, m_Constant(TrueC)) &&
4953 match(FalseVal, m_Constant(FalseC))) {
4954 unsigned NumElts =
4955 cast<FixedVectorType>(TrueC->getType())->getNumElements();
4956 SmallVector<Constant *, 16> NewC;
4957 for (unsigned i = 0; i != NumElts; ++i) {
4958 // Bail out on incomplete vector constants.
4959 Constant *TEltC = TrueC->getAggregateElement(i);
4960 Constant *FEltC = FalseC->getAggregateElement(i);
4961 if (!TEltC || !FEltC)
4962 break;
4963
4964 // If the elements match (undef or not), that value is the result. If only
4965 // one element is undef, choose the defined element as the safe result.
4966 if (TEltC == FEltC)
4967 NewC.push_back(TEltC);
4968 else if (isa<PoisonValue>(TEltC) ||
4969 (Q.isUndefValue(TEltC) && isGuaranteedNotToBePoison(FEltC)))
4970 NewC.push_back(FEltC);
4971 else if (isa<PoisonValue>(FEltC) ||
4972 (Q.isUndefValue(FEltC) && isGuaranteedNotToBePoison(TEltC)))
4973 NewC.push_back(TEltC);
4974 else
4975 break;
4976 }
4977 if (NewC.size() == NumElts)
4978 return ConstantVector::get(NewC);
4979 }
4980
4981 if (Value *V =
4982 simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
4983 return V;
4984
4985 if (Value *V = simplifySelectWithBitTest(Cond, TrueVal, FalseVal))
4986 return V;
4987
4988 if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q, MaxRecurse))
4989 return V;
4990
4991 std::optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL);
4992 if (Imp)
4993 return *Imp ? TrueVal : FalseVal;
4994
4995 return nullptr;
4996 }
4997
simplifySelectInst(Value * Cond,Value * TrueVal,Value * FalseVal,const SimplifyQuery & Q)4998 Value *llvm::simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4999 const SimplifyQuery &Q) {
5000 return ::simplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit);
5001 }
5002
5003 /// Given operands for an GetElementPtrInst, see if we can fold the result.
5004 /// If not, this returns null.
simplifyGEPInst(Type * SrcTy,Value * Ptr,ArrayRef<Value * > Indices,GEPNoWrapFlags NW,const SimplifyQuery & Q,unsigned)5005 static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
5006 ArrayRef<Value *> Indices, GEPNoWrapFlags NW,
5007 const SimplifyQuery &Q, unsigned) {
5008 // The type of the GEP pointer operand.
5009 unsigned AS =
5010 cast<PointerType>(Ptr->getType()->getScalarType())->getAddressSpace();
5011
5012 // getelementptr P -> P.
5013 if (Indices.empty())
5014 return Ptr;
5015
5016 // Compute the (pointer) type returned by the GEP instruction.
5017 Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Indices);
5018 Type *GEPTy = Ptr->getType();
5019 if (!GEPTy->isVectorTy()) {
5020 for (Value *Op : Indices) {
5021 // If one of the operands is a vector, the result type is a vector of
5022 // pointers. All vector operands must have the same number of elements.
5023 if (VectorType *VT = dyn_cast<VectorType>(Op->getType())) {
5024 GEPTy = VectorType::get(GEPTy, VT->getElementCount());
5025 break;
5026 }
5027 }
5028 }
5029
5030 // All-zero GEP is a no-op, unless it performs a vector splat.
5031 if (Ptr->getType() == GEPTy &&
5032 all_of(Indices, [](const auto *V) { return match(V, m_Zero()); }))
5033 return Ptr;
5034
5035 // getelementptr poison, idx -> poison
5036 // getelementptr baseptr, poison -> poison
5037 if (isa<PoisonValue>(Ptr) ||
5038 any_of(Indices, [](const auto *V) { return isa<PoisonValue>(V); }))
5039 return PoisonValue::get(GEPTy);
5040
5041 // getelementptr undef, idx -> undef
5042 if (Q.isUndefValue(Ptr))
5043 return UndefValue::get(GEPTy);
5044
5045 bool IsScalableVec =
5046 SrcTy->isScalableTy() || any_of(Indices, [](const Value *V) {
5047 return isa<ScalableVectorType>(V->getType());
5048 });
5049
5050 if (Indices.size() == 1) {
5051 Type *Ty = SrcTy;
5052 if (!IsScalableVec && Ty->isSized()) {
5053 Value *P;
5054 uint64_t C;
5055 uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
5056 // getelementptr P, N -> P if P points to a type of zero size.
5057 if (TyAllocSize == 0 && Ptr->getType() == GEPTy)
5058 return Ptr;
5059
5060 // The following transforms are only safe if the ptrtoint cast
5061 // doesn't truncate the pointers.
5062 if (Indices[0]->getType()->getScalarSizeInBits() ==
5063 Q.DL.getPointerSizeInBits(AS)) {
5064 auto CanSimplify = [GEPTy, &P, Ptr]() -> bool {
5065 return P->getType() == GEPTy &&
5066 getUnderlyingObject(P) == getUnderlyingObject(Ptr);
5067 };
5068 // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
5069 if (TyAllocSize == 1 &&
5070 match(Indices[0],
5071 m_Sub(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Specific(Ptr)))) &&
5072 CanSimplify())
5073 return P;
5074
5075 // getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of
5076 // size 1 << C.
5077 if (match(Indices[0], m_AShr(m_Sub(m_PtrToInt(m_Value(P)),
5078 m_PtrToInt(m_Specific(Ptr))),
5079 m_ConstantInt(C))) &&
5080 TyAllocSize == 1ULL << C && CanSimplify())
5081 return P;
5082
5083 // getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of
5084 // size C.
5085 if (match(Indices[0], m_SDiv(m_Sub(m_PtrToInt(m_Value(P)),
5086 m_PtrToInt(m_Specific(Ptr))),
5087 m_SpecificInt(TyAllocSize))) &&
5088 CanSimplify())
5089 return P;
5090 }
5091 }
5092 }
5093
5094 if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 &&
5095 all_of(Indices.drop_back(1),
5096 [](Value *Idx) { return match(Idx, m_Zero()); })) {
5097 unsigned IdxWidth =
5098 Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
5099 if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
5100 APInt BasePtrOffset(IdxWidth, 0);
5101 Value *StrippedBasePtr =
5102 Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.DL, BasePtrOffset);
5103
5104 // Avoid creating inttoptr of zero here: While LLVMs treatment of
5105 // inttoptr is generally conservative, this particular case is folded to
5106 // a null pointer, which will have incorrect provenance.
5107
5108 // gep (gep V, C), (sub 0, V) -> C
5109 if (match(Indices.back(),
5110 m_Neg(m_PtrToInt(m_Specific(StrippedBasePtr)))) &&
5111 !BasePtrOffset.isZero()) {
5112 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
5113 return ConstantExpr::getIntToPtr(CI, GEPTy);
5114 }
5115 // gep (gep V, C), (xor V, -1) -> C-1
5116 if (match(Indices.back(),
5117 m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes())) &&
5118 !BasePtrOffset.isOne()) {
5119 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
5120 return ConstantExpr::getIntToPtr(CI, GEPTy);
5121 }
5122 }
5123 }
5124
5125 // Check to see if this is constant foldable.
5126 if (!isa<Constant>(Ptr) ||
5127 !all_of(Indices, [](Value *V) { return isa<Constant>(V); }))
5128 return nullptr;
5129
5130 if (!ConstantExpr::isSupportedGetElementPtr(SrcTy))
5131 return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr), std::nullopt,
5132 Indices);
5133
5134 auto *CE =
5135 ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices, NW);
5136 return ConstantFoldConstant(CE, Q.DL);
5137 }
5138
simplifyGEPInst(Type * SrcTy,Value * Ptr,ArrayRef<Value * > Indices,GEPNoWrapFlags NW,const SimplifyQuery & Q)5139 Value *llvm::simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
5140 GEPNoWrapFlags NW, const SimplifyQuery &Q) {
5141 return ::simplifyGEPInst(SrcTy, Ptr, Indices, NW, Q, RecursionLimit);
5142 }
5143
5144 /// Given operands for an InsertValueInst, see if we can fold the result.
5145 /// If not, this returns null.
simplifyInsertValueInst(Value * Agg,Value * Val,ArrayRef<unsigned> Idxs,const SimplifyQuery & Q,unsigned)5146 static Value *simplifyInsertValueInst(Value *Agg, Value *Val,
5147 ArrayRef<unsigned> Idxs,
5148 const SimplifyQuery &Q, unsigned) {
5149 if (Constant *CAgg = dyn_cast<Constant>(Agg))
5150 if (Constant *CVal = dyn_cast<Constant>(Val))
5151 return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
5152
5153 // insertvalue x, poison, n -> x
5154 // insertvalue x, undef, n -> x if x cannot be poison
5155 if (isa<PoisonValue>(Val) ||
5156 (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Agg)))
5157 return Agg;
5158
5159 // insertvalue x, (extractvalue y, n), n
5160 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
5161 if (EV->getAggregateOperand()->getType() == Agg->getType() &&
5162 EV->getIndices() == Idxs) {
5163 // insertvalue poison, (extractvalue y, n), n -> y
5164 // insertvalue undef, (extractvalue y, n), n -> y if y cannot be poison
5165 if (isa<PoisonValue>(Agg) ||
5166 (Q.isUndefValue(Agg) &&
5167 isGuaranteedNotToBePoison(EV->getAggregateOperand())))
5168 return EV->getAggregateOperand();
5169
5170 // insertvalue y, (extractvalue y, n), n -> y
5171 if (Agg == EV->getAggregateOperand())
5172 return Agg;
5173 }
5174
5175 return nullptr;
5176 }
5177
simplifyInsertValueInst(Value * Agg,Value * Val,ArrayRef<unsigned> Idxs,const SimplifyQuery & Q)5178 Value *llvm::simplifyInsertValueInst(Value *Agg, Value *Val,
5179 ArrayRef<unsigned> Idxs,
5180 const SimplifyQuery &Q) {
5181 return ::simplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
5182 }
5183
simplifyInsertElementInst(Value * Vec,Value * Val,Value * Idx,const SimplifyQuery & Q)5184 Value *llvm::simplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
5185 const SimplifyQuery &Q) {
5186 // Try to constant fold.
5187 auto *VecC = dyn_cast<Constant>(Vec);
5188 auto *ValC = dyn_cast<Constant>(Val);
5189 auto *IdxC = dyn_cast<Constant>(Idx);
5190 if (VecC && ValC && IdxC)
5191 return ConstantExpr::getInsertElement(VecC, ValC, IdxC);
5192
5193 // For fixed-length vector, fold into poison if index is out of bounds.
5194 if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
5195 if (isa<FixedVectorType>(Vec->getType()) &&
5196 CI->uge(cast<FixedVectorType>(Vec->getType())->getNumElements()))
5197 return PoisonValue::get(Vec->getType());
5198 }
5199
5200 // If index is undef, it might be out of bounds (see above case)
5201 if (Q.isUndefValue(Idx))
5202 return PoisonValue::get(Vec->getType());
5203
5204 // If the scalar is poison, or it is undef and there is no risk of
5205 // propagating poison from the vector value, simplify to the vector value.
5206 if (isa<PoisonValue>(Val) ||
5207 (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec)))
5208 return Vec;
5209
5210 // Inserting the splatted value into a constant splat does nothing.
5211 if (VecC && ValC && VecC->getSplatValue() == ValC)
5212 return Vec;
5213
5214 // If we are extracting a value from a vector, then inserting it into the same
5215 // place, that's the input vector:
5216 // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
5217 if (match(Val, m_ExtractElt(m_Specific(Vec), m_Specific(Idx))))
5218 return Vec;
5219
5220 return nullptr;
5221 }
5222
5223 /// Given operands for an ExtractValueInst, see if we can fold the result.
5224 /// If not, this returns null.
simplifyExtractValueInst(Value * Agg,ArrayRef<unsigned> Idxs,const SimplifyQuery &,unsigned)5225 static Value *simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
5226 const SimplifyQuery &, unsigned) {
5227 if (auto *CAgg = dyn_cast<Constant>(Agg))
5228 return ConstantFoldExtractValueInstruction(CAgg, Idxs);
5229
5230 // extractvalue x, (insertvalue y, elt, n), n -> elt
5231 unsigned NumIdxs = Idxs.size();
5232 for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
5233 IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
5234 ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
5235 unsigned NumInsertValueIdxs = InsertValueIdxs.size();
5236 unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
5237 if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
5238 Idxs.slice(0, NumCommonIdxs)) {
5239 if (NumIdxs == NumInsertValueIdxs)
5240 return IVI->getInsertedValueOperand();
5241 break;
5242 }
5243 }
5244
5245 return nullptr;
5246 }
5247
simplifyExtractValueInst(Value * Agg,ArrayRef<unsigned> Idxs,const SimplifyQuery & Q)5248 Value *llvm::simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
5249 const SimplifyQuery &Q) {
5250 return ::simplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit);
5251 }
5252
5253 /// Given operands for an ExtractElementInst, see if we can fold the result.
5254 /// If not, this returns null.
simplifyExtractElementInst(Value * Vec,Value * Idx,const SimplifyQuery & Q,unsigned)5255 static Value *simplifyExtractElementInst(Value *Vec, Value *Idx,
5256 const SimplifyQuery &Q, unsigned) {
5257 auto *VecVTy = cast<VectorType>(Vec->getType());
5258 if (auto *CVec = dyn_cast<Constant>(Vec)) {
5259 if (auto *CIdx = dyn_cast<Constant>(Idx))
5260 return ConstantExpr::getExtractElement(CVec, CIdx);
5261
5262 if (Q.isUndefValue(Vec))
5263 return UndefValue::get(VecVTy->getElementType());
5264 }
5265
5266 // An undef extract index can be arbitrarily chosen to be an out-of-range
5267 // index value, which would result in the instruction being poison.
5268 if (Q.isUndefValue(Idx))
5269 return PoisonValue::get(VecVTy->getElementType());
5270
5271 // If extracting a specified index from the vector, see if we can recursively
5272 // find a previously computed scalar that was inserted into the vector.
5273 if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
5274 // For fixed-length vector, fold into undef if index is out of bounds.
5275 unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
5276 if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts))
5277 return PoisonValue::get(VecVTy->getElementType());
5278 // Handle case where an element is extracted from a splat.
5279 if (IdxC->getValue().ult(MinNumElts))
5280 if (auto *Splat = getSplatValue(Vec))
5281 return Splat;
5282 if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
5283 return Elt;
5284 } else {
5285 // extractelt x, (insertelt y, elt, n), n -> elt
5286 // If the possibly-variable indices are trivially known to be equal
5287 // (because they are the same operand) then use the value that was
5288 // inserted directly.
5289 auto *IE = dyn_cast<InsertElementInst>(Vec);
5290 if (IE && IE->getOperand(2) == Idx)
5291 return IE->getOperand(1);
5292
5293 // The index is not relevant if our vector is a splat.
5294 if (Value *Splat = getSplatValue(Vec))
5295 return Splat;
5296 }
5297 return nullptr;
5298 }
5299
simplifyExtractElementInst(Value * Vec,Value * Idx,const SimplifyQuery & Q)5300 Value *llvm::simplifyExtractElementInst(Value *Vec, Value *Idx,
5301 const SimplifyQuery &Q) {
5302 return ::simplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
5303 }
5304
5305 /// See if we can fold the given phi. If not, returns null.
simplifyPHINode(PHINode * PN,ArrayRef<Value * > IncomingValues,const SimplifyQuery & Q)5306 static Value *simplifyPHINode(PHINode *PN, ArrayRef<Value *> IncomingValues,
5307 const SimplifyQuery &Q) {
5308 // WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE
5309 // here, because the PHI we may succeed simplifying to was not
5310 // def-reachable from the original PHI!
5311
5312 // If all of the PHI's incoming values are the same then replace the PHI node
5313 // with the common value.
5314 Value *CommonValue = nullptr;
5315 bool HasPoisonInput = false;
5316 bool HasUndefInput = false;
5317 for (Value *Incoming : IncomingValues) {
5318 // If the incoming value is the phi node itself, it can safely be skipped.
5319 if (Incoming == PN)
5320 continue;
5321 if (isa<PoisonValue>(Incoming)) {
5322 HasPoisonInput = true;
5323 continue;
5324 }
5325 if (Q.isUndefValue(Incoming)) {
5326 // Remember that we saw an undef value, but otherwise ignore them.
5327 HasUndefInput = true;
5328 continue;
5329 }
5330 if (CommonValue && Incoming != CommonValue)
5331 return nullptr; // Not the same, bail out.
5332 CommonValue = Incoming;
5333 }
5334
5335 // If CommonValue is null then all of the incoming values were either undef,
5336 // poison or equal to the phi node itself.
5337 if (!CommonValue)
5338 return HasUndefInput ? UndefValue::get(PN->getType())
5339 : PoisonValue::get(PN->getType());
5340
5341 if (HasPoisonInput || HasUndefInput) {
5342 // If we have a PHI node like phi(X, undef, X), where X is defined by some
5343 // instruction, we cannot return X as the result of the PHI node unless it
5344 // dominates the PHI block.
5345 if (!valueDominatesPHI(CommonValue, PN, Q.DT))
5346 return nullptr;
5347
5348 // Make sure we do not replace an undef value with poison.
5349 if (HasUndefInput &&
5350 !isGuaranteedNotToBePoison(CommonValue, Q.AC, Q.CxtI, Q.DT))
5351 return nullptr;
5352 return CommonValue;
5353 }
5354
5355 return CommonValue;
5356 }
5357
simplifyCastInst(unsigned CastOpc,Value * Op,Type * Ty,const SimplifyQuery & Q,unsigned MaxRecurse)5358 static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5359 const SimplifyQuery &Q, unsigned MaxRecurse) {
5360 if (auto *C = dyn_cast<Constant>(Op))
5361 return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL);
5362
5363 if (auto *CI = dyn_cast<CastInst>(Op)) {
5364 auto *Src = CI->getOperand(0);
5365 Type *SrcTy = Src->getType();
5366 Type *MidTy = CI->getType();
5367 Type *DstTy = Ty;
5368 if (Src->getType() == Ty) {
5369 auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode());
5370 auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
5371 Type *SrcIntPtrTy =
5372 SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr;
5373 Type *MidIntPtrTy =
5374 MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr;
5375 Type *DstIntPtrTy =
5376 DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr;
5377 if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy,
5378 SrcIntPtrTy, MidIntPtrTy,
5379 DstIntPtrTy) == Instruction::BitCast)
5380 return Src;
5381 }
5382 }
5383
5384 // bitcast x -> x
5385 if (CastOpc == Instruction::BitCast)
5386 if (Op->getType() == Ty)
5387 return Op;
5388
5389 // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
5390 Value *Ptr, *X;
5391 if (CastOpc == Instruction::PtrToInt &&
5392 match(Op, m_PtrAdd(m_Value(Ptr),
5393 m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) &&
5394 X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
5395 return X;
5396
5397 return nullptr;
5398 }
5399
simplifyCastInst(unsigned CastOpc,Value * Op,Type * Ty,const SimplifyQuery & Q)5400 Value *llvm::simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
5401 const SimplifyQuery &Q) {
5402 return ::simplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit);
5403 }
5404
5405 /// For the given destination element of a shuffle, peek through shuffles to
5406 /// match a root vector source operand that contains that element in the same
5407 /// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
foldIdentityShuffles(int DestElt,Value * Op0,Value * Op1,int MaskVal,Value * RootVec,unsigned MaxRecurse)5408 static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
5409 int MaskVal, Value *RootVec,
5410 unsigned MaxRecurse) {
5411 if (!MaxRecurse--)
5412 return nullptr;
5413
5414 // Bail out if any mask value is undefined. That kind of shuffle may be
5415 // simplified further based on demanded bits or other folds.
5416 if (MaskVal == -1)
5417 return nullptr;
5418
5419 // The mask value chooses which source operand we need to look at next.
5420 int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
5421 int RootElt = MaskVal;
5422 Value *SourceOp = Op0;
5423 if (MaskVal >= InVecNumElts) {
5424 RootElt = MaskVal - InVecNumElts;
5425 SourceOp = Op1;
5426 }
5427
5428 // If the source operand is a shuffle itself, look through it to find the
5429 // matching root vector.
5430 if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
5431 return foldIdentityShuffles(
5432 DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
5433 SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
5434 }
5435
5436 // The source operand is not a shuffle. Initialize the root vector value for
5437 // this shuffle if that has not been done yet.
5438 if (!RootVec)
5439 RootVec = SourceOp;
5440
5441 // Give up as soon as a source operand does not match the existing root value.
5442 if (RootVec != SourceOp)
5443 return nullptr;
5444
5445 // The element must be coming from the same lane in the source vector
5446 // (although it may have crossed lanes in intermediate shuffles).
5447 if (RootElt != DestElt)
5448 return nullptr;
5449
5450 return RootVec;
5451 }
5452
simplifyShuffleVectorInst(Value * Op0,Value * Op1,ArrayRef<int> Mask,Type * RetTy,const SimplifyQuery & Q,unsigned MaxRecurse)5453 static Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1,
5454 ArrayRef<int> Mask, Type *RetTy,
5455 const SimplifyQuery &Q,
5456 unsigned MaxRecurse) {
5457 if (all_of(Mask, [](int Elem) { return Elem == PoisonMaskElem; }))
5458 return PoisonValue::get(RetTy);
5459
5460 auto *InVecTy = cast<VectorType>(Op0->getType());
5461 unsigned MaskNumElts = Mask.size();
5462 ElementCount InVecEltCount = InVecTy->getElementCount();
5463
5464 bool Scalable = InVecEltCount.isScalable();
5465
5466 SmallVector<int, 32> Indices;
5467 Indices.assign(Mask.begin(), Mask.end());
5468
5469 // Canonicalization: If mask does not select elements from an input vector,
5470 // replace that input vector with poison.
5471 if (!Scalable) {
5472 bool MaskSelects0 = false, MaskSelects1 = false;
5473 unsigned InVecNumElts = InVecEltCount.getKnownMinValue();
5474 for (unsigned i = 0; i != MaskNumElts; ++i) {
5475 if (Indices[i] == -1)
5476 continue;
5477 if ((unsigned)Indices[i] < InVecNumElts)
5478 MaskSelects0 = true;
5479 else
5480 MaskSelects1 = true;
5481 }
5482 if (!MaskSelects0)
5483 Op0 = PoisonValue::get(InVecTy);
5484 if (!MaskSelects1)
5485 Op1 = PoisonValue::get(InVecTy);
5486 }
5487
5488 auto *Op0Const = dyn_cast<Constant>(Op0);
5489 auto *Op1Const = dyn_cast<Constant>(Op1);
5490
5491 // If all operands are constant, constant fold the shuffle. This
5492 // transformation depends on the value of the mask which is not known at
5493 // compile time for scalable vectors
5494 if (Op0Const && Op1Const)
5495 return ConstantExpr::getShuffleVector(Op0Const, Op1Const, Mask);
5496
5497 // Canonicalization: if only one input vector is constant, it shall be the
5498 // second one. This transformation depends on the value of the mask which
5499 // is not known at compile time for scalable vectors
5500 if (!Scalable && Op0Const && !Op1Const) {
5501 std::swap(Op0, Op1);
5502 ShuffleVectorInst::commuteShuffleMask(Indices,
5503 InVecEltCount.getKnownMinValue());
5504 }
5505
5506 // A splat of an inserted scalar constant becomes a vector constant:
5507 // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
5508 // NOTE: We may have commuted above, so analyze the updated Indices, not the
5509 // original mask constant.
5510 // NOTE: This transformation depends on the value of the mask which is not
5511 // known at compile time for scalable vectors
5512 Constant *C;
5513 ConstantInt *IndexC;
5514 if (!Scalable && match(Op0, m_InsertElt(m_Value(), m_Constant(C),
5515 m_ConstantInt(IndexC)))) {
5516 // Match a splat shuffle mask of the insert index allowing undef elements.
5517 int InsertIndex = IndexC->getZExtValue();
5518 if (all_of(Indices, [InsertIndex](int MaskElt) {
5519 return MaskElt == InsertIndex || MaskElt == -1;
5520 })) {
5521 assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat");
5522
5523 // Shuffle mask poisons become poison constant result elements.
5524 SmallVector<Constant *, 16> VecC(MaskNumElts, C);
5525 for (unsigned i = 0; i != MaskNumElts; ++i)
5526 if (Indices[i] == -1)
5527 VecC[i] = PoisonValue::get(C->getType());
5528 return ConstantVector::get(VecC);
5529 }
5530 }
5531
5532 // A shuffle of a splat is always the splat itself. Legal if the shuffle's
5533 // value type is same as the input vectors' type.
5534 if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
5535 if (Q.isUndefValue(Op1) && RetTy == InVecTy &&
5536 all_equal(OpShuf->getShuffleMask()))
5537 return Op0;
5538
5539 // All remaining transformation depend on the value of the mask, which is
5540 // not known at compile time for scalable vectors.
5541 if (Scalable)
5542 return nullptr;
5543
5544 // Don't fold a shuffle with undef mask elements. This may get folded in a
5545 // better way using demanded bits or other analysis.
5546 // TODO: Should we allow this?
5547 if (is_contained(Indices, -1))
5548 return nullptr;
5549
5550 // Check if every element of this shuffle can be mapped back to the
5551 // corresponding element of a single root vector. If so, we don't need this
5552 // shuffle. This handles simple identity shuffles as well as chains of
5553 // shuffles that may widen/narrow and/or move elements across lanes and back.
5554 Value *RootVec = nullptr;
5555 for (unsigned i = 0; i != MaskNumElts; ++i) {
5556 // Note that recursion is limited for each vector element, so if any element
5557 // exceeds the limit, this will fail to simplify.
5558 RootVec =
5559 foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse);
5560
5561 // We can't replace a widening/narrowing shuffle with one of its operands.
5562 if (!RootVec || RootVec->getType() != RetTy)
5563 return nullptr;
5564 }
5565 return RootVec;
5566 }
5567
5568 /// Given operands for a ShuffleVectorInst, fold the result or return null.
simplifyShuffleVectorInst(Value * Op0,Value * Op1,ArrayRef<int> Mask,Type * RetTy,const SimplifyQuery & Q)5569 Value *llvm::simplifyShuffleVectorInst(Value *Op0, Value *Op1,
5570 ArrayRef<int> Mask, Type *RetTy,
5571 const SimplifyQuery &Q) {
5572 return ::simplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit);
5573 }
5574
foldConstant(Instruction::UnaryOps Opcode,Value * & Op,const SimplifyQuery & Q)5575 static Constant *foldConstant(Instruction::UnaryOps Opcode, Value *&Op,
5576 const SimplifyQuery &Q) {
5577 if (auto *C = dyn_cast<Constant>(Op))
5578 return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL);
5579 return nullptr;
5580 }
5581
5582 /// Given the operand for an FNeg, see if we can fold the result. If not, this
5583 /// returns null.
simplifyFNegInst(Value * Op,FastMathFlags FMF,const SimplifyQuery & Q,unsigned MaxRecurse)5584 static Value *simplifyFNegInst(Value *Op, FastMathFlags FMF,
5585 const SimplifyQuery &Q, unsigned MaxRecurse) {
5586 if (Constant *C = foldConstant(Instruction::FNeg, Op, Q))
5587 return C;
5588
5589 Value *X;
5590 // fneg (fneg X) ==> X
5591 if (match(Op, m_FNeg(m_Value(X))))
5592 return X;
5593
5594 return nullptr;
5595 }
5596
simplifyFNegInst(Value * Op,FastMathFlags FMF,const SimplifyQuery & Q)5597 Value *llvm::simplifyFNegInst(Value *Op, FastMathFlags FMF,
5598 const SimplifyQuery &Q) {
5599 return ::simplifyFNegInst(Op, FMF, Q, RecursionLimit);
5600 }
5601
5602 /// Try to propagate existing NaN values when possible. If not, replace the
5603 /// constant or elements in the constant with a canonical NaN.
propagateNaN(Constant * In)5604 static Constant *propagateNaN(Constant *In) {
5605 Type *Ty = In->getType();
5606 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
5607 unsigned NumElts = VecTy->getNumElements();
5608 SmallVector<Constant *, 32> NewC(NumElts);
5609 for (unsigned i = 0; i != NumElts; ++i) {
5610 Constant *EltC = In->getAggregateElement(i);
5611 // Poison elements propagate. NaN propagates except signaling is quieted.
5612 // Replace unknown or undef elements with canonical NaN.
5613 if (EltC && isa<PoisonValue>(EltC))
5614 NewC[i] = EltC;
5615 else if (EltC && EltC->isNaN())
5616 NewC[i] = ConstantFP::get(
5617 EltC->getType(), cast<ConstantFP>(EltC)->getValue().makeQuiet());
5618 else
5619 NewC[i] = ConstantFP::getNaN(VecTy->getElementType());
5620 }
5621 return ConstantVector::get(NewC);
5622 }
5623
5624 // If it is not a fixed vector, but not a simple NaN either, return a
5625 // canonical NaN.
5626 if (!In->isNaN())
5627 return ConstantFP::getNaN(Ty);
5628
5629 // If we known this is a NaN, and it's scalable vector, we must have a splat
5630 // on our hands. Grab that before splatting a QNaN constant.
5631 if (isa<ScalableVectorType>(Ty)) {
5632 auto *Splat = In->getSplatValue();
5633 assert(Splat && Splat->isNaN() &&
5634 "Found a scalable-vector NaN but not a splat");
5635 In = Splat;
5636 }
5637
5638 // Propagate an existing QNaN constant. If it is an SNaN, make it quiet, but
5639 // preserve the sign/payload.
5640 return ConstantFP::get(Ty, cast<ConstantFP>(In)->getValue().makeQuiet());
5641 }
5642
5643 /// Perform folds that are common to any floating-point operation. This implies
5644 /// transforms based on poison/undef/NaN because the operation itself makes no
5645 /// difference to the result.
simplifyFPOp(ArrayRef<Value * > Ops,FastMathFlags FMF,const SimplifyQuery & Q,fp::ExceptionBehavior ExBehavior,RoundingMode Rounding)5646 static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
5647 const SimplifyQuery &Q,
5648 fp::ExceptionBehavior ExBehavior,
5649 RoundingMode Rounding) {
5650 // Poison is independent of anything else. It always propagates from an
5651 // operand to a math result.
5652 if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
5653 return PoisonValue::get(Ops[0]->getType());
5654
5655 for (Value *V : Ops) {
5656 bool IsNan = match(V, m_NaN());
5657 bool IsInf = match(V, m_Inf());
5658 bool IsUndef = Q.isUndefValue(V);
5659
5660 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
5661 // (an undef operand can be chosen to be Nan/Inf), then the result of
5662 // this operation is poison.
5663 if (FMF.noNaNs() && (IsNan || IsUndef))
5664 return PoisonValue::get(V->getType());
5665 if (FMF.noInfs() && (IsInf || IsUndef))
5666 return PoisonValue::get(V->getType());
5667
5668 if (isDefaultFPEnvironment(ExBehavior, Rounding)) {
5669 // Undef does not propagate because undef means that all bits can take on
5670 // any value. If this is undef * NaN for example, then the result values
5671 // (at least the exponent bits) are limited. Assume the undef is a
5672 // canonical NaN and propagate that.
5673 if (IsUndef)
5674 return ConstantFP::getNaN(V->getType());
5675 if (IsNan)
5676 return propagateNaN(cast<Constant>(V));
5677 } else if (ExBehavior != fp::ebStrict) {
5678 if (IsNan)
5679 return propagateNaN(cast<Constant>(V));
5680 }
5681 }
5682 return nullptr;
5683 }
5684
5685 /// Given operands for an FAdd, see if we can fold the result. If not, this
5686 /// returns null.
5687 static Value *
simplifyFAddInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,unsigned MaxRecurse,fp::ExceptionBehavior ExBehavior=fp::ebIgnore,RoundingMode Rounding=RoundingMode::NearestTiesToEven)5688 simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5689 const SimplifyQuery &Q, unsigned MaxRecurse,
5690 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5691 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5692 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5693 if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
5694 return C;
5695
5696 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5697 return C;
5698
5699 // fadd X, -0 ==> X
5700 // With strict/constrained FP, we have these possible edge cases that do
5701 // not simplify to Op0:
5702 // fadd SNaN, -0.0 --> QNaN
5703 // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative)
5704 if (canIgnoreSNaN(ExBehavior, FMF) &&
5705 (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
5706 FMF.noSignedZeros()))
5707 if (match(Op1, m_NegZeroFP()))
5708 return Op0;
5709
5710 // fadd X, 0 ==> X, when we know X is not -0
5711 if (canIgnoreSNaN(ExBehavior, FMF))
5712 if (match(Op1, m_PosZeroFP()) &&
5713 (FMF.noSignedZeros() || cannotBeNegativeZero(Op0, Q)))
5714 return Op0;
5715
5716 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5717 return nullptr;
5718
5719 if (FMF.noNaNs()) {
5720 // With nnan: X + {+/-}Inf --> {+/-}Inf
5721 if (match(Op1, m_Inf()))
5722 return Op1;
5723
5724 // With nnan: -X + X --> 0.0 (and commuted variant)
5725 // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
5726 // Negative zeros are allowed because we always end up with positive zero:
5727 // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5728 // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5729 // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
5730 // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
5731 if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
5732 match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
5733 return ConstantFP::getZero(Op0->getType());
5734
5735 if (match(Op0, m_FNeg(m_Specific(Op1))) ||
5736 match(Op1, m_FNeg(m_Specific(Op0))))
5737 return ConstantFP::getZero(Op0->getType());
5738 }
5739
5740 // (X - Y) + Y --> X
5741 // Y + (X - Y) --> X
5742 Value *X;
5743 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5744 (match(Op0, m_FSub(m_Value(X), m_Specific(Op1))) ||
5745 match(Op1, m_FSub(m_Value(X), m_Specific(Op0)))))
5746 return X;
5747
5748 return nullptr;
5749 }
5750
5751 /// Given operands for an FSub, see if we can fold the result. If not, this
5752 /// returns null.
5753 static Value *
simplifyFSubInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,unsigned MaxRecurse,fp::ExceptionBehavior ExBehavior=fp::ebIgnore,RoundingMode Rounding=RoundingMode::NearestTiesToEven)5754 simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5755 const SimplifyQuery &Q, unsigned MaxRecurse,
5756 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5757 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5758 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5759 if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
5760 return C;
5761
5762 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5763 return C;
5764
5765 // fsub X, +0 ==> X
5766 if (canIgnoreSNaN(ExBehavior, FMF) &&
5767 (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
5768 FMF.noSignedZeros()))
5769 if (match(Op1, m_PosZeroFP()))
5770 return Op0;
5771
5772 // fsub X, -0 ==> X, when we know X is not -0
5773 if (canIgnoreSNaN(ExBehavior, FMF))
5774 if (match(Op1, m_NegZeroFP()) &&
5775 (FMF.noSignedZeros() || cannotBeNegativeZero(Op0, Q)))
5776 return Op0;
5777
5778 // fsub -0.0, (fsub -0.0, X) ==> X
5779 // fsub -0.0, (fneg X) ==> X
5780 Value *X;
5781 if (canIgnoreSNaN(ExBehavior, FMF))
5782 if (match(Op0, m_NegZeroFP()) && match(Op1, m_FNeg(m_Value(X))))
5783 return X;
5784
5785 // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
5786 // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.
5787 if (canIgnoreSNaN(ExBehavior, FMF))
5788 if (FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()) &&
5789 (match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X))) ||
5790 match(Op1, m_FNeg(m_Value(X)))))
5791 return X;
5792
5793 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5794 return nullptr;
5795
5796 if (FMF.noNaNs()) {
5797 // fsub nnan x, x ==> 0.0
5798 if (Op0 == Op1)
5799 return Constant::getNullValue(Op0->getType());
5800
5801 // With nnan: {+/-}Inf - X --> {+/-}Inf
5802 if (match(Op0, m_Inf()))
5803 return Op0;
5804
5805 // With nnan: X - {+/-}Inf --> {-/+}Inf
5806 if (match(Op1, m_Inf()))
5807 return foldConstant(Instruction::FNeg, Op1, Q);
5808 }
5809
5810 // Y - (Y - X) --> X
5811 // (X + Y) - Y --> X
5812 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5813 (match(Op1, m_FSub(m_Specific(Op0), m_Value(X))) ||
5814 match(Op0, m_c_FAdd(m_Specific(Op1), m_Value(X)))))
5815 return X;
5816
5817 return nullptr;
5818 }
5819
simplifyFMAFMul(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,unsigned MaxRecurse,fp::ExceptionBehavior ExBehavior,RoundingMode Rounding)5820 static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
5821 const SimplifyQuery &Q, unsigned MaxRecurse,
5822 fp::ExceptionBehavior ExBehavior,
5823 RoundingMode Rounding) {
5824 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5825 return C;
5826
5827 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5828 return nullptr;
5829
5830 // Canonicalize special constants as operand 1.
5831 if (match(Op0, m_FPOne()) || match(Op0, m_AnyZeroFP()))
5832 std::swap(Op0, Op1);
5833
5834 // X * 1.0 --> X
5835 if (match(Op1, m_FPOne()))
5836 return Op0;
5837
5838 if (match(Op1, m_AnyZeroFP())) {
5839 // X * 0.0 --> 0.0 (with nnan and nsz)
5840 if (FMF.noNaNs() && FMF.noSignedZeros())
5841 return ConstantFP::getZero(Op0->getType());
5842
5843 KnownFPClass Known = computeKnownFPClass(Op0, FMF, fcInf | fcNan, Q);
5844 if (Known.isKnownNever(fcInf | fcNan)) {
5845 // if nsz is set, return 0.0
5846 if (FMF.noSignedZeros())
5847 return ConstantFP::getZero(Op0->getType());
5848 // +normal number * (-)0.0 --> (-)0.0
5849 if (Known.SignBit == false)
5850 return Op1;
5851 // -normal number * (-)0.0 --> -(-)0.0
5852 if (Known.SignBit == true)
5853 return foldConstant(Instruction::FNeg, Op1, Q);
5854 }
5855 }
5856
5857 // sqrt(X) * sqrt(X) --> X, if we can:
5858 // 1. Remove the intermediate rounding (reassociate).
5859 // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
5860 // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
5861 Value *X;
5862 if (Op0 == Op1 && match(Op0, m_Sqrt(m_Value(X))) && FMF.allowReassoc() &&
5863 FMF.noNaNs() && FMF.noSignedZeros())
5864 return X;
5865
5866 return nullptr;
5867 }
5868
5869 /// Given the operands for an FMul, see if we can fold the result
5870 static Value *
simplifyFMulInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,unsigned MaxRecurse,fp::ExceptionBehavior ExBehavior=fp::ebIgnore,RoundingMode Rounding=RoundingMode::NearestTiesToEven)5871 simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5872 const SimplifyQuery &Q, unsigned MaxRecurse,
5873 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5874 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5875 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5876 if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
5877 return C;
5878
5879 // Now apply simplifications that do not require rounding.
5880 return simplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding);
5881 }
5882
simplifyFAddInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,fp::ExceptionBehavior ExBehavior,RoundingMode Rounding)5883 Value *llvm::simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5884 const SimplifyQuery &Q,
5885 fp::ExceptionBehavior ExBehavior,
5886 RoundingMode Rounding) {
5887 return ::simplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5888 Rounding);
5889 }
5890
simplifyFSubInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,fp::ExceptionBehavior ExBehavior,RoundingMode Rounding)5891 Value *llvm::simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5892 const SimplifyQuery &Q,
5893 fp::ExceptionBehavior ExBehavior,
5894 RoundingMode Rounding) {
5895 return ::simplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5896 Rounding);
5897 }
5898
simplifyFMulInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,fp::ExceptionBehavior ExBehavior,RoundingMode Rounding)5899 Value *llvm::simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5900 const SimplifyQuery &Q,
5901 fp::ExceptionBehavior ExBehavior,
5902 RoundingMode Rounding) {
5903 return ::simplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5904 Rounding);
5905 }
5906
simplifyFMAFMul(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,fp::ExceptionBehavior ExBehavior,RoundingMode Rounding)5907 Value *llvm::simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
5908 const SimplifyQuery &Q,
5909 fp::ExceptionBehavior ExBehavior,
5910 RoundingMode Rounding) {
5911 return ::simplifyFMAFMul(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5912 Rounding);
5913 }
5914
5915 static Value *
simplifyFDivInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,unsigned,fp::ExceptionBehavior ExBehavior=fp::ebIgnore,RoundingMode Rounding=RoundingMode::NearestTiesToEven)5916 simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5917 const SimplifyQuery &Q, unsigned,
5918 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5919 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5920 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5921 if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
5922 return C;
5923
5924 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5925 return C;
5926
5927 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5928 return nullptr;
5929
5930 // X / 1.0 -> X
5931 if (match(Op1, m_FPOne()))
5932 return Op0;
5933
5934 // 0 / X -> 0
5935 // Requires that NaNs are off (X could be zero) and signed zeroes are
5936 // ignored (X could be positive or negative, so the output sign is unknown).
5937 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
5938 return ConstantFP::getZero(Op0->getType());
5939
5940 if (FMF.noNaNs()) {
5941 // X / X -> 1.0 is legal when NaNs are ignored.
5942 // We can ignore infinities because INF/INF is NaN.
5943 if (Op0 == Op1)
5944 return ConstantFP::get(Op0->getType(), 1.0);
5945
5946 // (X * Y) / Y --> X if we can reassociate to the above form.
5947 Value *X;
5948 if (FMF.allowReassoc() && match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1))))
5949 return X;
5950
5951 // -X / X -> -1.0 and
5952 // X / -X -> -1.0 are legal when NaNs are ignored.
5953 // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
5954 if (match(Op0, m_FNegNSZ(m_Specific(Op1))) ||
5955 match(Op1, m_FNegNSZ(m_Specific(Op0))))
5956 return ConstantFP::get(Op0->getType(), -1.0);
5957
5958 // nnan ninf X / [-]0.0 -> poison
5959 if (FMF.noInfs() && match(Op1, m_AnyZeroFP()))
5960 return PoisonValue::get(Op1->getType());
5961 }
5962
5963 return nullptr;
5964 }
5965
simplifyFDivInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,fp::ExceptionBehavior ExBehavior,RoundingMode Rounding)5966 Value *llvm::simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5967 const SimplifyQuery &Q,
5968 fp::ExceptionBehavior ExBehavior,
5969 RoundingMode Rounding) {
5970 return ::simplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5971 Rounding);
5972 }
5973
5974 static Value *
simplifyFRemInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,unsigned,fp::ExceptionBehavior ExBehavior=fp::ebIgnore,RoundingMode Rounding=RoundingMode::NearestTiesToEven)5975 simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5976 const SimplifyQuery &Q, unsigned,
5977 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5978 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5979 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5980 if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
5981 return C;
5982
5983 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5984 return C;
5985
5986 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5987 return nullptr;
5988
5989 // Unlike fdiv, the result of frem always matches the sign of the dividend.
5990 // The constant match may include undef elements in a vector, so return a full
5991 // zero constant as the result.
5992 if (FMF.noNaNs()) {
5993 // +0 % X -> 0
5994 if (match(Op0, m_PosZeroFP()))
5995 return ConstantFP::getZero(Op0->getType());
5996 // -0 % X -> -0
5997 if (match(Op0, m_NegZeroFP()))
5998 return ConstantFP::getNegativeZero(Op0->getType());
5999 }
6000
6001 return nullptr;
6002 }
6003
simplifyFRemInst(Value * Op0,Value * Op1,FastMathFlags FMF,const SimplifyQuery & Q,fp::ExceptionBehavior ExBehavior,RoundingMode Rounding)6004 Value *llvm::simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
6005 const SimplifyQuery &Q,
6006 fp::ExceptionBehavior ExBehavior,
6007 RoundingMode Rounding) {
6008 return ::simplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
6009 Rounding);
6010 }
6011
6012 //=== Helper functions for higher up the class hierarchy.
6013
6014 /// Given the operand for a UnaryOperator, see if we can fold the result.
6015 /// If not, this returns null.
simplifyUnOp(unsigned Opcode,Value * Op,const SimplifyQuery & Q,unsigned MaxRecurse)6016 static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q,
6017 unsigned MaxRecurse) {
6018 switch (Opcode) {
6019 case Instruction::FNeg:
6020 return simplifyFNegInst(Op, FastMathFlags(), Q, MaxRecurse);
6021 default:
6022 llvm_unreachable("Unexpected opcode");
6023 }
6024 }
6025
6026 /// Given the operand for a UnaryOperator, see if we can fold the result.
6027 /// If not, this returns null.
6028 /// Try to use FastMathFlags when folding the result.
simplifyFPUnOp(unsigned Opcode,Value * Op,const FastMathFlags & FMF,const SimplifyQuery & Q,unsigned MaxRecurse)6029 static Value *simplifyFPUnOp(unsigned Opcode, Value *Op,
6030 const FastMathFlags &FMF, const SimplifyQuery &Q,
6031 unsigned MaxRecurse) {
6032 switch (Opcode) {
6033 case Instruction::FNeg:
6034 return simplifyFNegInst(Op, FMF, Q, MaxRecurse);
6035 default:
6036 return simplifyUnOp(Opcode, Op, Q, MaxRecurse);
6037 }
6038 }
6039
simplifyUnOp(unsigned Opcode,Value * Op,const SimplifyQuery & Q)6040 Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) {
6041 return ::simplifyUnOp(Opcode, Op, Q, RecursionLimit);
6042 }
6043
simplifyUnOp(unsigned Opcode,Value * Op,FastMathFlags FMF,const SimplifyQuery & Q)6044 Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
6045 const SimplifyQuery &Q) {
6046 return ::simplifyFPUnOp(Opcode, Op, FMF, Q, RecursionLimit);
6047 }
6048
6049 /// Given operands for a BinaryOperator, see if we can fold the result.
6050 /// If not, this returns null.
simplifyBinOp(unsigned Opcode,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)6051 static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6052 const SimplifyQuery &Q, unsigned MaxRecurse) {
6053 switch (Opcode) {
6054 case Instruction::Add:
6055 return simplifyAddInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6056 MaxRecurse);
6057 case Instruction::Sub:
6058 return simplifySubInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6059 MaxRecurse);
6060 case Instruction::Mul:
6061 return simplifyMulInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6062 MaxRecurse);
6063 case Instruction::SDiv:
6064 return simplifySDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6065 case Instruction::UDiv:
6066 return simplifyUDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6067 case Instruction::SRem:
6068 return simplifySRemInst(LHS, RHS, Q, MaxRecurse);
6069 case Instruction::URem:
6070 return simplifyURemInst(LHS, RHS, Q, MaxRecurse);
6071 case Instruction::Shl:
6072 return simplifyShlInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q,
6073 MaxRecurse);
6074 case Instruction::LShr:
6075 return simplifyLShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6076 case Instruction::AShr:
6077 return simplifyAShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse);
6078 case Instruction::And:
6079 return simplifyAndInst(LHS, RHS, Q, MaxRecurse);
6080 case Instruction::Or:
6081 return simplifyOrInst(LHS, RHS, Q, MaxRecurse);
6082 case Instruction::Xor:
6083 return simplifyXorInst(LHS, RHS, Q, MaxRecurse);
6084 case Instruction::FAdd:
6085 return simplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6086 case Instruction::FSub:
6087 return simplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6088 case Instruction::FMul:
6089 return simplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6090 case Instruction::FDiv:
6091 return simplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6092 case Instruction::FRem:
6093 return simplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6094 default:
6095 llvm_unreachable("Unexpected opcode");
6096 }
6097 }
6098
6099 /// Given operands for a BinaryOperator, see if we can fold the result.
6100 /// If not, this returns null.
6101 /// Try to use FastMathFlags when folding the result.
simplifyBinOp(unsigned Opcode,Value * LHS,Value * RHS,const FastMathFlags & FMF,const SimplifyQuery & Q,unsigned MaxRecurse)6102 static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6103 const FastMathFlags &FMF, const SimplifyQuery &Q,
6104 unsigned MaxRecurse) {
6105 switch (Opcode) {
6106 case Instruction::FAdd:
6107 return simplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse);
6108 case Instruction::FSub:
6109 return simplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
6110 case Instruction::FMul:
6111 return simplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
6112 case Instruction::FDiv:
6113 return simplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
6114 default:
6115 return simplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
6116 }
6117 }
6118
simplifyBinOp(unsigned Opcode,Value * LHS,Value * RHS,const SimplifyQuery & Q)6119 Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6120 const SimplifyQuery &Q) {
6121 return ::simplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit);
6122 }
6123
simplifyBinOp(unsigned Opcode,Value * LHS,Value * RHS,FastMathFlags FMF,const SimplifyQuery & Q)6124 Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
6125 FastMathFlags FMF, const SimplifyQuery &Q) {
6126 return ::simplifyBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit);
6127 }
6128
6129 /// Given operands for a CmpInst, see if we can fold the result.
simplifyCmpInst(CmpPredicate Predicate,Value * LHS,Value * RHS,const SimplifyQuery & Q,unsigned MaxRecurse)6130 static Value *simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
6131 const SimplifyQuery &Q, unsigned MaxRecurse) {
6132 if (CmpInst::isIntPredicate(Predicate))
6133 return simplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
6134 return simplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse);
6135 }
6136
simplifyCmpInst(CmpPredicate Predicate,Value * LHS,Value * RHS,const SimplifyQuery & Q)6137 Value *llvm::simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
6138 const SimplifyQuery &Q) {
6139 return ::simplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
6140 }
6141
isIdempotent(Intrinsic::ID ID)6142 static bool isIdempotent(Intrinsic::ID ID) {
6143 switch (ID) {
6144 default:
6145 return false;
6146
6147 // Unary idempotent: f(f(x)) = f(x)
6148 case Intrinsic::fabs:
6149 case Intrinsic::floor:
6150 case Intrinsic::ceil:
6151 case Intrinsic::trunc:
6152 case Intrinsic::rint:
6153 case Intrinsic::nearbyint:
6154 case Intrinsic::round:
6155 case Intrinsic::roundeven:
6156 case Intrinsic::canonicalize:
6157 case Intrinsic::arithmetic_fence:
6158 return true;
6159 }
6160 }
6161
6162 /// Return true if the intrinsic rounds a floating-point value to an integral
6163 /// floating-point value (not an integer type).
removesFPFraction(Intrinsic::ID ID)6164 static bool removesFPFraction(Intrinsic::ID ID) {
6165 switch (ID) {
6166 default:
6167 return false;
6168
6169 case Intrinsic::floor:
6170 case Intrinsic::ceil:
6171 case Intrinsic::trunc:
6172 case Intrinsic::rint:
6173 case Intrinsic::nearbyint:
6174 case Intrinsic::round:
6175 case Intrinsic::roundeven:
6176 return true;
6177 }
6178 }
6179
simplifyRelativeLoad(Constant * Ptr,Constant * Offset,const DataLayout & DL)6180 static Value *simplifyRelativeLoad(Constant *Ptr, Constant *Offset,
6181 const DataLayout &DL) {
6182 GlobalValue *PtrSym;
6183 APInt PtrOffset;
6184 if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL))
6185 return nullptr;
6186
6187 Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
6188
6189 auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
6190 if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64)
6191 return nullptr;
6192
6193 APInt OffsetInt = OffsetConstInt->getValue().sextOrTrunc(
6194 DL.getIndexTypeSizeInBits(Ptr->getType()));
6195 if (OffsetInt.srem(4) != 0)
6196 return nullptr;
6197
6198 Constant *Loaded =
6199 ConstantFoldLoadFromConstPtr(Ptr, Int32Ty, std::move(OffsetInt), DL);
6200 if (!Loaded)
6201 return nullptr;
6202
6203 auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
6204 if (!LoadedCE)
6205 return nullptr;
6206
6207 if (LoadedCE->getOpcode() == Instruction::Trunc) {
6208 LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6209 if (!LoadedCE)
6210 return nullptr;
6211 }
6212
6213 if (LoadedCE->getOpcode() != Instruction::Sub)
6214 return nullptr;
6215
6216 auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6217 if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
6218 return nullptr;
6219 auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
6220
6221 Constant *LoadedRHS = LoadedCE->getOperand(1);
6222 GlobalValue *LoadedRHSSym;
6223 APInt LoadedRHSOffset;
6224 if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset,
6225 DL) ||
6226 PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
6227 return nullptr;
6228
6229 return LoadedLHSPtr;
6230 }
6231
6232 // TODO: Need to pass in FastMathFlags
simplifyLdexp(Value * Op0,Value * Op1,const SimplifyQuery & Q,bool IsStrict)6233 static Value *simplifyLdexp(Value *Op0, Value *Op1, const SimplifyQuery &Q,
6234 bool IsStrict) {
6235 // ldexp(poison, x) -> poison
6236 // ldexp(x, poison) -> poison
6237 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
6238 return Op0;
6239
6240 // ldexp(undef, x) -> nan
6241 if (Q.isUndefValue(Op0))
6242 return ConstantFP::getNaN(Op0->getType());
6243
6244 if (!IsStrict) {
6245 // TODO: Could insert a canonicalize for strict
6246
6247 // ldexp(x, undef) -> x
6248 if (Q.isUndefValue(Op1))
6249 return Op0;
6250 }
6251
6252 const APFloat *C = nullptr;
6253 match(Op0, PatternMatch::m_APFloat(C));
6254
6255 // These cases should be safe, even with strictfp.
6256 // ldexp(0.0, x) -> 0.0
6257 // ldexp(-0.0, x) -> -0.0
6258 // ldexp(inf, x) -> inf
6259 // ldexp(-inf, x) -> -inf
6260 if (C && (C->isZero() || C->isInfinity()))
6261 return Op0;
6262
6263 // These are canonicalization dropping, could do it if we knew how we could
6264 // ignore denormal flushes and target handling of nan payload bits.
6265 if (IsStrict)
6266 return nullptr;
6267
6268 // TODO: Could quiet this with strictfp if the exception mode isn't strict.
6269 if (C && C->isNaN())
6270 return ConstantFP::get(Op0->getType(), C->makeQuiet());
6271
6272 // ldexp(x, 0) -> x
6273
6274 // TODO: Could fold this if we know the exception mode isn't
6275 // strict, we know the denormal mode and other target modes.
6276 if (match(Op1, PatternMatch::m_ZeroInt()))
6277 return Op0;
6278
6279 return nullptr;
6280 }
6281
simplifyUnaryIntrinsic(Function * F,Value * Op0,const SimplifyQuery & Q,const CallBase * Call)6282 static Value *simplifyUnaryIntrinsic(Function *F, Value *Op0,
6283 const SimplifyQuery &Q,
6284 const CallBase *Call) {
6285 // Idempotent functions return the same result when called repeatedly.
6286 Intrinsic::ID IID = F->getIntrinsicID();
6287 if (isIdempotent(IID))
6288 if (auto *II = dyn_cast<IntrinsicInst>(Op0))
6289 if (II->getIntrinsicID() == IID)
6290 return II;
6291
6292 if (removesFPFraction(IID)) {
6293 // Converting from int or calling a rounding function always results in a
6294 // finite integral number or infinity. For those inputs, rounding functions
6295 // always return the same value, so the (2nd) rounding is eliminated. Ex:
6296 // floor (sitofp x) -> sitofp x
6297 // round (ceil x) -> ceil x
6298 auto *II = dyn_cast<IntrinsicInst>(Op0);
6299 if ((II && removesFPFraction(II->getIntrinsicID())) ||
6300 match(Op0, m_SIToFP(m_Value())) || match(Op0, m_UIToFP(m_Value())))
6301 return Op0;
6302 }
6303
6304 Value *X;
6305 switch (IID) {
6306 case Intrinsic::fabs:
6307 if (computeKnownFPSignBit(Op0, Q) == false)
6308 return Op0;
6309 break;
6310 case Intrinsic::bswap:
6311 // bswap(bswap(x)) -> x
6312 if (match(Op0, m_BSwap(m_Value(X))))
6313 return X;
6314 break;
6315 case Intrinsic::bitreverse:
6316 // bitreverse(bitreverse(x)) -> x
6317 if (match(Op0, m_BitReverse(m_Value(X))))
6318 return X;
6319 break;
6320 case Intrinsic::ctpop: {
6321 // ctpop(X) -> 1 iff X is non-zero power of 2.
6322 if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ false, Q.AC, Q.CxtI, Q.DT))
6323 return ConstantInt::get(Op0->getType(), 1);
6324 // If everything but the lowest bit is zero, that bit is the pop-count. Ex:
6325 // ctpop(and X, 1) --> and X, 1
6326 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
6327 if (MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, BitWidth - 1),
6328 Q))
6329 return Op0;
6330 break;
6331 }
6332 case Intrinsic::exp:
6333 // exp(log(x)) -> x
6334 if (Call->hasAllowReassoc() &&
6335 match(Op0, m_Intrinsic<Intrinsic::log>(m_Value(X))))
6336 return X;
6337 break;
6338 case Intrinsic::exp2:
6339 // exp2(log2(x)) -> x
6340 if (Call->hasAllowReassoc() &&
6341 match(Op0, m_Intrinsic<Intrinsic::log2>(m_Value(X))))
6342 return X;
6343 break;
6344 case Intrinsic::exp10:
6345 // exp10(log10(x)) -> x
6346 if (Call->hasAllowReassoc() &&
6347 match(Op0, m_Intrinsic<Intrinsic::log10>(m_Value(X))))
6348 return X;
6349 break;
6350 case Intrinsic::log:
6351 // log(exp(x)) -> x
6352 if (Call->hasAllowReassoc() &&
6353 match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))))
6354 return X;
6355 break;
6356 case Intrinsic::log2:
6357 // log2(exp2(x)) -> x
6358 if (Call->hasAllowReassoc() &&
6359 (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) ||
6360 match(Op0,
6361 m_Intrinsic<Intrinsic::pow>(m_SpecificFP(2.0), m_Value(X)))))
6362 return X;
6363 break;
6364 case Intrinsic::log10:
6365 // log10(pow(10.0, x)) -> x
6366 // log10(exp10(x)) -> x
6367 if (Call->hasAllowReassoc() &&
6368 (match(Op0, m_Intrinsic<Intrinsic::exp10>(m_Value(X))) ||
6369 match(Op0,
6370 m_Intrinsic<Intrinsic::pow>(m_SpecificFP(10.0), m_Value(X)))))
6371 return X;
6372 break;
6373 case Intrinsic::vector_reverse:
6374 // vector.reverse(vector.reverse(x)) -> x
6375 if (match(Op0, m_VecReverse(m_Value(X))))
6376 return X;
6377 // vector.reverse(splat(X)) -> splat(X)
6378 if (isSplatValue(Op0))
6379 return Op0;
6380 break;
6381 default:
6382 break;
6383 }
6384
6385 return nullptr;
6386 }
6387
6388 /// Given a min/max intrinsic, see if it can be removed based on having an
6389 /// operand that is another min/max intrinsic with shared operand(s). The caller
6390 /// is expected to swap the operand arguments to handle commutation.
foldMinMaxSharedOp(Intrinsic::ID IID,Value * Op0,Value * Op1)6391 static Value *foldMinMaxSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1) {
6392 Value *X, *Y;
6393 if (!match(Op0, m_MaxOrMin(m_Value(X), m_Value(Y))))
6394 return nullptr;
6395
6396 auto *MM0 = dyn_cast<IntrinsicInst>(Op0);
6397 if (!MM0)
6398 return nullptr;
6399 Intrinsic::ID IID0 = MM0->getIntrinsicID();
6400
6401 if (Op1 == X || Op1 == Y ||
6402 match(Op1, m_c_MaxOrMin(m_Specific(X), m_Specific(Y)))) {
6403 // max (max X, Y), X --> max X, Y
6404 if (IID0 == IID)
6405 return MM0;
6406 // max (min X, Y), X --> X
6407 if (IID0 == getInverseMinMaxIntrinsic(IID))
6408 return Op1;
6409 }
6410 return nullptr;
6411 }
6412
6413 /// Given a min/max intrinsic, see if it can be removed based on having an
6414 /// operand that is another min/max intrinsic with shared operand(s). The caller
6415 /// is expected to swap the operand arguments to handle commutation.
foldMinimumMaximumSharedOp(Intrinsic::ID IID,Value * Op0,Value * Op1)6416 static Value *foldMinimumMaximumSharedOp(Intrinsic::ID IID, Value *Op0,
6417 Value *Op1) {
6418 assert((IID == Intrinsic::maxnum || IID == Intrinsic::minnum ||
6419 IID == Intrinsic::maximum || IID == Intrinsic::minimum) &&
6420 "Unsupported intrinsic");
6421
6422 auto *M0 = dyn_cast<IntrinsicInst>(Op0);
6423 // If Op0 is not the same intrinsic as IID, do not process.
6424 // This is a difference with integer min/max handling. We do not process the
6425 // case like max(min(X,Y),min(X,Y)) => min(X,Y). But it can be handled by GVN.
6426 if (!M0 || M0->getIntrinsicID() != IID)
6427 return nullptr;
6428 Value *X0 = M0->getOperand(0);
6429 Value *Y0 = M0->getOperand(1);
6430 // Simple case, m(m(X,Y), X) => m(X, Y)
6431 // m(m(X,Y), Y) => m(X, Y)
6432 // For minimum/maximum, X is NaN => m(NaN, Y) == NaN and m(NaN, NaN) == NaN.
6433 // For minimum/maximum, Y is NaN => m(X, NaN) == NaN and m(NaN, NaN) == NaN.
6434 // For minnum/maxnum, X is NaN => m(NaN, Y) == Y and m(Y, Y) == Y.
6435 // For minnum/maxnum, Y is NaN => m(X, NaN) == X and m(X, NaN) == X.
6436 if (X0 == Op1 || Y0 == Op1)
6437 return M0;
6438
6439 auto *M1 = dyn_cast<IntrinsicInst>(Op1);
6440 if (!M1)
6441 return nullptr;
6442 Value *X1 = M1->getOperand(0);
6443 Value *Y1 = M1->getOperand(1);
6444 Intrinsic::ID IID1 = M1->getIntrinsicID();
6445 // we have a case m(m(X,Y),m'(X,Y)) taking into account m' is commutative.
6446 // if m' is m or inversion of m => m(m(X,Y),m'(X,Y)) == m(X,Y).
6447 // For minimum/maximum, X is NaN => m(NaN,Y) == m'(NaN, Y) == NaN.
6448 // For minimum/maximum, Y is NaN => m(X,NaN) == m'(X, NaN) == NaN.
6449 // For minnum/maxnum, X is NaN => m(NaN,Y) == m'(NaN, Y) == Y.
6450 // For minnum/maxnum, Y is NaN => m(X,NaN) == m'(X, NaN) == X.
6451 if ((X0 == X1 && Y0 == Y1) || (X0 == Y1 && Y0 == X1))
6452 if (IID1 == IID || getInverseMinMaxIntrinsic(IID1) == IID)
6453 return M0;
6454
6455 return nullptr;
6456 }
6457
simplifyBinaryIntrinsic(Intrinsic::ID IID,Type * ReturnType,Value * Op0,Value * Op1,const SimplifyQuery & Q,const CallBase * Call)6458 Value *llvm::simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType,
6459 Value *Op0, Value *Op1,
6460 const SimplifyQuery &Q,
6461 const CallBase *Call) {
6462 unsigned BitWidth = ReturnType->getScalarSizeInBits();
6463 switch (IID) {
6464 case Intrinsic::abs:
6465 // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here.
6466 // It is always ok to pick the earlier abs. We'll just lose nsw if its only
6467 // on the outer abs.
6468 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(), m_Value())))
6469 return Op0;
6470 break;
6471
6472 case Intrinsic::cttz: {
6473 Value *X;
6474 if (match(Op0, m_Shl(m_One(), m_Value(X))))
6475 return X;
6476 break;
6477 }
6478 case Intrinsic::ctlz: {
6479 Value *X;
6480 if (match(Op0, m_LShr(m_Negative(), m_Value(X))))
6481 return X;
6482 if (match(Op0, m_AShr(m_Negative(), m_Value())))
6483 return Constant::getNullValue(ReturnType);
6484 break;
6485 }
6486 case Intrinsic::ptrmask: {
6487 // NOTE: We can't apply this simplifications based on the value of Op1
6488 // because we need to preserve provenance.
6489 if (Q.isUndefValue(Op0) || match(Op0, m_Zero()))
6490 return Constant::getNullValue(Op0->getType());
6491
6492 assert(Op1->getType()->getScalarSizeInBits() ==
6493 Q.DL.getIndexTypeSizeInBits(Op0->getType()) &&
6494 "Invalid mask width");
6495 // If index-width (mask size) is less than pointer-size then mask is
6496 // 1-extended.
6497 if (match(Op1, m_PtrToInt(m_Specific(Op0))))
6498 return Op0;
6499
6500 // NOTE: We may have attributes associated with the return value of the
6501 // llvm.ptrmask intrinsic that will be lost when we just return the
6502 // operand. We should try to preserve them.
6503 if (match(Op1, m_AllOnes()) || Q.isUndefValue(Op1))
6504 return Op0;
6505
6506 Constant *C;
6507 if (match(Op1, m_ImmConstant(C))) {
6508 KnownBits PtrKnown = computeKnownBits(Op0, Q);
6509 // See if we only masking off bits we know are already zero due to
6510 // alignment.
6511 APInt IrrelevantPtrBits =
6512 PtrKnown.Zero.zextOrTrunc(C->getType()->getScalarSizeInBits());
6513 C = ConstantFoldBinaryOpOperands(
6514 Instruction::Or, C, ConstantInt::get(C->getType(), IrrelevantPtrBits),
6515 Q.DL);
6516 if (C != nullptr && C->isAllOnesValue())
6517 return Op0;
6518 }
6519 break;
6520 }
6521 case Intrinsic::smax:
6522 case Intrinsic::smin:
6523 case Intrinsic::umax:
6524 case Intrinsic::umin: {
6525 // If the arguments are the same, this is a no-op.
6526 if (Op0 == Op1)
6527 return Op0;
6528
6529 // Canonicalize immediate constant operand as Op1.
6530 if (match(Op0, m_ImmConstant()))
6531 std::swap(Op0, Op1);
6532
6533 // Assume undef is the limit value.
6534 if (Q.isUndefValue(Op1))
6535 return ConstantInt::get(
6536 ReturnType, MinMaxIntrinsic::getSaturationPoint(IID, BitWidth));
6537
6538 const APInt *C;
6539 if (match(Op1, m_APIntAllowPoison(C))) {
6540 // Clamp to limit value. For example:
6541 // umax(i8 %x, i8 255) --> 255
6542 if (*C == MinMaxIntrinsic::getSaturationPoint(IID, BitWidth))
6543 return ConstantInt::get(ReturnType, *C);
6544
6545 // If the constant op is the opposite of the limit value, the other must
6546 // be larger/smaller or equal. For example:
6547 // umin(i8 %x, i8 255) --> %x
6548 if (*C == MinMaxIntrinsic::getSaturationPoint(
6549 getInverseMinMaxIntrinsic(IID), BitWidth))
6550 return Op0;
6551
6552 // Remove nested call if constant operands allow it. Example:
6553 // max (max X, 7), 5 -> max X, 7
6554 auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0);
6555 if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
6556 // TODO: loosen undef/splat restrictions for vector constants.
6557 Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1);
6558 const APInt *InnerC;
6559 if ((match(M00, m_APInt(InnerC)) || match(M01, m_APInt(InnerC))) &&
6560 ICmpInst::compare(*InnerC, *C,
6561 ICmpInst::getNonStrictPredicate(
6562 MinMaxIntrinsic::getPredicate(IID))))
6563 return Op0;
6564 }
6565 }
6566
6567 if (Value *V = foldMinMaxSharedOp(IID, Op0, Op1))
6568 return V;
6569 if (Value *V = foldMinMaxSharedOp(IID, Op1, Op0))
6570 return V;
6571
6572 ICmpInst::Predicate Pred =
6573 ICmpInst::getNonStrictPredicate(MinMaxIntrinsic::getPredicate(IID));
6574 if (isICmpTrue(Pred, Op0, Op1, Q.getWithoutUndef(), RecursionLimit))
6575 return Op0;
6576 if (isICmpTrue(Pred, Op1, Op0, Q.getWithoutUndef(), RecursionLimit))
6577 return Op1;
6578
6579 break;
6580 }
6581 case Intrinsic::scmp:
6582 case Intrinsic::ucmp: {
6583 // Fold to a constant if the relationship between operands can be
6584 // established with certainty
6585 if (isICmpTrue(CmpInst::ICMP_EQ, Op0, Op1, Q, RecursionLimit))
6586 return Constant::getNullValue(ReturnType);
6587
6588 ICmpInst::Predicate PredGT =
6589 IID == Intrinsic::scmp ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
6590 if (isICmpTrue(PredGT, Op0, Op1, Q, RecursionLimit))
6591 return ConstantInt::get(ReturnType, 1);
6592
6593 ICmpInst::Predicate PredLT =
6594 IID == Intrinsic::scmp ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
6595 if (isICmpTrue(PredLT, Op0, Op1, Q, RecursionLimit))
6596 return ConstantInt::getSigned(ReturnType, -1);
6597
6598 break;
6599 }
6600 case Intrinsic::usub_with_overflow:
6601 case Intrinsic::ssub_with_overflow:
6602 // X - X -> { 0, false }
6603 // X - undef -> { 0, false }
6604 // undef - X -> { 0, false }
6605 if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6606 return Constant::getNullValue(ReturnType);
6607 break;
6608 case Intrinsic::uadd_with_overflow:
6609 case Intrinsic::sadd_with_overflow:
6610 // X + undef -> { -1, false }
6611 // undef + x -> { -1, false }
6612 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)) {
6613 return ConstantStruct::get(
6614 cast<StructType>(ReturnType),
6615 {Constant::getAllOnesValue(ReturnType->getStructElementType(0)),
6616 Constant::getNullValue(ReturnType->getStructElementType(1))});
6617 }
6618 break;
6619 case Intrinsic::umul_with_overflow:
6620 case Intrinsic::smul_with_overflow:
6621 // 0 * X -> { 0, false }
6622 // X * 0 -> { 0, false }
6623 if (match(Op0, m_Zero()) || match(Op1, m_Zero()))
6624 return Constant::getNullValue(ReturnType);
6625 // undef * X -> { 0, false }
6626 // X * undef -> { 0, false }
6627 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6628 return Constant::getNullValue(ReturnType);
6629 break;
6630 case Intrinsic::uadd_sat:
6631 // sat(MAX + X) -> MAX
6632 // sat(X + MAX) -> MAX
6633 if (match(Op0, m_AllOnes()) || match(Op1, m_AllOnes()))
6634 return Constant::getAllOnesValue(ReturnType);
6635 [[fallthrough]];
6636 case Intrinsic::sadd_sat:
6637 // sat(X + undef) -> -1
6638 // sat(undef + X) -> -1
6639 // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
6640 // For signed: Assume undef is ~X, in which case X + ~X = -1.
6641 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6642 return Constant::getAllOnesValue(ReturnType);
6643
6644 // X + 0 -> X
6645 if (match(Op1, m_Zero()))
6646 return Op0;
6647 // 0 + X -> X
6648 if (match(Op0, m_Zero()))
6649 return Op1;
6650 break;
6651 case Intrinsic::usub_sat:
6652 // sat(0 - X) -> 0, sat(X - MAX) -> 0
6653 if (match(Op0, m_Zero()) || match(Op1, m_AllOnes()))
6654 return Constant::getNullValue(ReturnType);
6655 [[fallthrough]];
6656 case Intrinsic::ssub_sat:
6657 // X - X -> 0, X - undef -> 0, undef - X -> 0
6658 if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
6659 return Constant::getNullValue(ReturnType);
6660 // X - 0 -> X
6661 if (match(Op1, m_Zero()))
6662 return Op0;
6663 break;
6664 case Intrinsic::load_relative:
6665 if (auto *C0 = dyn_cast<Constant>(Op0))
6666 if (auto *C1 = dyn_cast<Constant>(Op1))
6667 return simplifyRelativeLoad(C0, C1, Q.DL);
6668 break;
6669 case Intrinsic::powi:
6670 if (auto *Power = dyn_cast<ConstantInt>(Op1)) {
6671 // powi(x, 0) -> 1.0
6672 if (Power->isZero())
6673 return ConstantFP::get(Op0->getType(), 1.0);
6674 // powi(x, 1) -> x
6675 if (Power->isOne())
6676 return Op0;
6677 }
6678 break;
6679 case Intrinsic::ldexp:
6680 return simplifyLdexp(Op0, Op1, Q, false);
6681 case Intrinsic::copysign:
6682 // copysign X, X --> X
6683 if (Op0 == Op1)
6684 return Op0;
6685 // copysign -X, X --> X
6686 // copysign X, -X --> -X
6687 if (match(Op0, m_FNeg(m_Specific(Op1))) ||
6688 match(Op1, m_FNeg(m_Specific(Op0))))
6689 return Op1;
6690 break;
6691 case Intrinsic::is_fpclass: {
6692 uint64_t Mask = cast<ConstantInt>(Op1)->getZExtValue();
6693 // If all tests are made, it doesn't matter what the value is.
6694 if ((Mask & fcAllFlags) == fcAllFlags)
6695 return ConstantInt::get(ReturnType, true);
6696 if ((Mask & fcAllFlags) == 0)
6697 return ConstantInt::get(ReturnType, false);
6698 if (Q.isUndefValue(Op0))
6699 return UndefValue::get(ReturnType);
6700 break;
6701 }
6702 case Intrinsic::maxnum:
6703 case Intrinsic::minnum:
6704 case Intrinsic::maximum:
6705 case Intrinsic::minimum: {
6706 // If the arguments are the same, this is a no-op.
6707 if (Op0 == Op1)
6708 return Op0;
6709
6710 // Canonicalize constant operand as Op1.
6711 if (isa<Constant>(Op0))
6712 std::swap(Op0, Op1);
6713
6714 // If an argument is undef, return the other argument.
6715 if (Q.isUndefValue(Op1))
6716 return Op0;
6717
6718 bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
6719 bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum;
6720
6721 // minnum(X, nan) -> X
6722 // maxnum(X, nan) -> X
6723 // minimum(X, nan) -> nan
6724 // maximum(X, nan) -> nan
6725 if (match(Op1, m_NaN()))
6726 return PropagateNaN ? propagateNaN(cast<Constant>(Op1)) : Op0;
6727
6728 // In the following folds, inf can be replaced with the largest finite
6729 // float, if the ninf flag is set.
6730 const APFloat *C;
6731 if (match(Op1, m_APFloat(C)) &&
6732 (C->isInfinity() || (Call && Call->hasNoInfs() && C->isLargest()))) {
6733 // minnum(X, -inf) -> -inf
6734 // maxnum(X, +inf) -> +inf
6735 // minimum(X, -inf) -> -inf if nnan
6736 // maximum(X, +inf) -> +inf if nnan
6737 if (C->isNegative() == IsMin &&
6738 (!PropagateNaN || (Call && Call->hasNoNaNs())))
6739 return ConstantFP::get(ReturnType, *C);
6740
6741 // minnum(X, +inf) -> X if nnan
6742 // maxnum(X, -inf) -> X if nnan
6743 // minimum(X, +inf) -> X
6744 // maximum(X, -inf) -> X
6745 if (C->isNegative() != IsMin &&
6746 (PropagateNaN || (Call && Call->hasNoNaNs())))
6747 return Op0;
6748 }
6749
6750 // Min/max of the same operation with common operand:
6751 // m(m(X, Y)), X --> m(X, Y) (4 commuted variants)
6752 if (Value *V = foldMinimumMaximumSharedOp(IID, Op0, Op1))
6753 return V;
6754 if (Value *V = foldMinimumMaximumSharedOp(IID, Op1, Op0))
6755 return V;
6756
6757 break;
6758 }
6759 case Intrinsic::vector_extract: {
6760 // (extract_vector (insert_vector _, X, 0), 0) -> X
6761 unsigned IdxN = cast<ConstantInt>(Op1)->getZExtValue();
6762 Value *X = nullptr;
6763 if (match(Op0, m_Intrinsic<Intrinsic::vector_insert>(m_Value(), m_Value(X),
6764 m_Zero())) &&
6765 IdxN == 0 && X->getType() == ReturnType)
6766 return X;
6767
6768 break;
6769 }
6770 default:
6771 break;
6772 }
6773
6774 return nullptr;
6775 }
6776
simplifyIntrinsic(CallBase * Call,Value * Callee,ArrayRef<Value * > Args,const SimplifyQuery & Q)6777 static Value *simplifyIntrinsic(CallBase *Call, Value *Callee,
6778 ArrayRef<Value *> Args,
6779 const SimplifyQuery &Q) {
6780 // Operand bundles should not be in Args.
6781 assert(Call->arg_size() == Args.size());
6782 unsigned NumOperands = Args.size();
6783 Function *F = cast<Function>(Callee);
6784 Intrinsic::ID IID = F->getIntrinsicID();
6785
6786 if (IID != Intrinsic::not_intrinsic && intrinsicPropagatesPoison(IID) &&
6787 any_of(Args, IsaPred<PoisonValue>))
6788 return PoisonValue::get(F->getReturnType());
6789 // Most of the intrinsics with no operands have some kind of side effect.
6790 // Don't simplify.
6791 if (!NumOperands) {
6792 switch (IID) {
6793 case Intrinsic::vscale: {
6794 Type *RetTy = F->getReturnType();
6795 ConstantRange CR = getVScaleRange(Call->getFunction(), 64);
6796 if (const APInt *C = CR.getSingleElement())
6797 return ConstantInt::get(RetTy, C->getZExtValue());
6798 return nullptr;
6799 }
6800 default:
6801 return nullptr;
6802 }
6803 }
6804
6805 if (NumOperands == 1)
6806 return simplifyUnaryIntrinsic(F, Args[0], Q, Call);
6807
6808 if (NumOperands == 2)
6809 return simplifyBinaryIntrinsic(IID, F->getReturnType(), Args[0], Args[1], Q,
6810 Call);
6811
6812 // Handle intrinsics with 3 or more arguments.
6813 switch (IID) {
6814 case Intrinsic::masked_load:
6815 case Intrinsic::masked_gather: {
6816 Value *MaskArg = Args[2];
6817 Value *PassthruArg = Args[3];
6818 // If the mask is all zeros or undef, the "passthru" argument is the result.
6819 if (maskIsAllZeroOrUndef(MaskArg))
6820 return PassthruArg;
6821 return nullptr;
6822 }
6823 case Intrinsic::fshl:
6824 case Intrinsic::fshr: {
6825 Value *Op0 = Args[0], *Op1 = Args[1], *ShAmtArg = Args[2];
6826
6827 // If both operands are undef, the result is undef.
6828 if (Q.isUndefValue(Op0) && Q.isUndefValue(Op1))
6829 return UndefValue::get(F->getReturnType());
6830
6831 // If shift amount is undef, assume it is zero.
6832 if (Q.isUndefValue(ShAmtArg))
6833 return Args[IID == Intrinsic::fshl ? 0 : 1];
6834
6835 const APInt *ShAmtC;
6836 if (match(ShAmtArg, m_APInt(ShAmtC))) {
6837 // If there's effectively no shift, return the 1st arg or 2nd arg.
6838 APInt BitWidth = APInt(ShAmtC->getBitWidth(), ShAmtC->getBitWidth());
6839 if (ShAmtC->urem(BitWidth).isZero())
6840 return Args[IID == Intrinsic::fshl ? 0 : 1];
6841 }
6842
6843 // Rotating zero by anything is zero.
6844 if (match(Op0, m_Zero()) && match(Op1, m_Zero()))
6845 return ConstantInt::getNullValue(F->getReturnType());
6846
6847 // Rotating -1 by anything is -1.
6848 if (match(Op0, m_AllOnes()) && match(Op1, m_AllOnes()))
6849 return ConstantInt::getAllOnesValue(F->getReturnType());
6850
6851 return nullptr;
6852 }
6853 case Intrinsic::experimental_constrained_fma: {
6854 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6855 if (Value *V = simplifyFPOp(Args, {}, Q, *FPI->getExceptionBehavior(),
6856 *FPI->getRoundingMode()))
6857 return V;
6858 return nullptr;
6859 }
6860 case Intrinsic::fma:
6861 case Intrinsic::fmuladd: {
6862 if (Value *V = simplifyFPOp(Args, {}, Q, fp::ebIgnore,
6863 RoundingMode::NearestTiesToEven))
6864 return V;
6865 return nullptr;
6866 }
6867 case Intrinsic::smul_fix:
6868 case Intrinsic::smul_fix_sat: {
6869 Value *Op0 = Args[0];
6870 Value *Op1 = Args[1];
6871 Value *Op2 = Args[2];
6872 Type *ReturnType = F->getReturnType();
6873
6874 // Canonicalize constant operand as Op1 (ConstantFolding handles the case
6875 // when both Op0 and Op1 are constant so we do not care about that special
6876 // case here).
6877 if (isa<Constant>(Op0))
6878 std::swap(Op0, Op1);
6879
6880 // X * 0 -> 0
6881 if (match(Op1, m_Zero()))
6882 return Constant::getNullValue(ReturnType);
6883
6884 // X * undef -> 0
6885 if (Q.isUndefValue(Op1))
6886 return Constant::getNullValue(ReturnType);
6887
6888 // X * (1 << Scale) -> X
6889 APInt ScaledOne =
6890 APInt::getOneBitSet(ReturnType->getScalarSizeInBits(),
6891 cast<ConstantInt>(Op2)->getZExtValue());
6892 if (ScaledOne.isNonNegative() && match(Op1, m_SpecificInt(ScaledOne)))
6893 return Op0;
6894
6895 return nullptr;
6896 }
6897 case Intrinsic::vector_insert: {
6898 Value *Vec = Args[0];
6899 Value *SubVec = Args[1];
6900 Value *Idx = Args[2];
6901 Type *ReturnType = F->getReturnType();
6902
6903 // (insert_vector Y, (extract_vector X, 0), 0) -> X
6904 // where: Y is X, or Y is undef
6905 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
6906 Value *X = nullptr;
6907 if (match(SubVec,
6908 m_Intrinsic<Intrinsic::vector_extract>(m_Value(X), m_Zero())) &&
6909 (Q.isUndefValue(Vec) || Vec == X) && IdxN == 0 &&
6910 X->getType() == ReturnType)
6911 return X;
6912
6913 return nullptr;
6914 }
6915 case Intrinsic::experimental_constrained_fadd: {
6916 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6917 return simplifyFAddInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6918 *FPI->getExceptionBehavior(),
6919 *FPI->getRoundingMode());
6920 }
6921 case Intrinsic::experimental_constrained_fsub: {
6922 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6923 return simplifyFSubInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6924 *FPI->getExceptionBehavior(),
6925 *FPI->getRoundingMode());
6926 }
6927 case Intrinsic::experimental_constrained_fmul: {
6928 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6929 return simplifyFMulInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6930 *FPI->getExceptionBehavior(),
6931 *FPI->getRoundingMode());
6932 }
6933 case Intrinsic::experimental_constrained_fdiv: {
6934 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6935 return simplifyFDivInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6936 *FPI->getExceptionBehavior(),
6937 *FPI->getRoundingMode());
6938 }
6939 case Intrinsic::experimental_constrained_frem: {
6940 auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6941 return simplifyFRemInst(Args[0], Args[1], FPI->getFastMathFlags(), Q,
6942 *FPI->getExceptionBehavior(),
6943 *FPI->getRoundingMode());
6944 }
6945 case Intrinsic::experimental_constrained_ldexp:
6946 return simplifyLdexp(Args[0], Args[1], Q, true);
6947 case Intrinsic::experimental_gc_relocate: {
6948 GCRelocateInst &GCR = *cast<GCRelocateInst>(Call);
6949 Value *DerivedPtr = GCR.getDerivedPtr();
6950 Value *BasePtr = GCR.getBasePtr();
6951
6952 // Undef is undef, even after relocation.
6953 if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) {
6954 return UndefValue::get(GCR.getType());
6955 }
6956
6957 if (auto *PT = dyn_cast<PointerType>(GCR.getType())) {
6958 // For now, the assumption is that the relocation of null will be null
6959 // for most any collector. If this ever changes, a corresponding hook
6960 // should be added to GCStrategy and this code should check it first.
6961 if (isa<ConstantPointerNull>(DerivedPtr)) {
6962 // Use null-pointer of gc_relocate's type to replace it.
6963 return ConstantPointerNull::get(PT);
6964 }
6965 }
6966 return nullptr;
6967 }
6968 case Intrinsic::experimental_vp_reverse: {
6969 Value *Vec = Call->getArgOperand(0);
6970 Value *Mask = Call->getArgOperand(1);
6971 Value *EVL = Call->getArgOperand(2);
6972
6973 Value *X;
6974 // vp.reverse(vp.reverse(X)) == X (with all ones mask and matching EVL)
6975 if (match(Mask, m_AllOnes()) &&
6976 match(Vec, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
6977 m_Value(X), m_AllOnes(), m_Specific(EVL))))
6978 return X;
6979
6980 // vp.reverse(splat(X)) -> splat(X) (regardless of mask and EVL)
6981 if (isSplatValue(Vec))
6982 return Vec;
6983 return nullptr;
6984 }
6985 default:
6986 return nullptr;
6987 }
6988 }
6989
tryConstantFoldCall(CallBase * Call,Value * Callee,ArrayRef<Value * > Args,const SimplifyQuery & Q)6990 static Value *tryConstantFoldCall(CallBase *Call, Value *Callee,
6991 ArrayRef<Value *> Args,
6992 const SimplifyQuery &Q) {
6993 auto *F = dyn_cast<Function>(Callee);
6994 if (!F || !canConstantFoldCallTo(Call, F))
6995 return nullptr;
6996
6997 SmallVector<Constant *, 4> ConstantArgs;
6998 ConstantArgs.reserve(Args.size());
6999 for (Value *Arg : Args) {
7000 Constant *C = dyn_cast<Constant>(Arg);
7001 if (!C) {
7002 if (isa<MetadataAsValue>(Arg))
7003 continue;
7004 return nullptr;
7005 }
7006 ConstantArgs.push_back(C);
7007 }
7008
7009 return ConstantFoldCall(Call, F, ConstantArgs, Q.TLI);
7010 }
7011
simplifyCall(CallBase * Call,Value * Callee,ArrayRef<Value * > Args,const SimplifyQuery & Q)7012 Value *llvm::simplifyCall(CallBase *Call, Value *Callee, ArrayRef<Value *> Args,
7013 const SimplifyQuery &Q) {
7014 // Args should not contain operand bundle operands.
7015 assert(Call->arg_size() == Args.size());
7016
7017 // musttail calls can only be simplified if they are also DCEd.
7018 // As we can't guarantee this here, don't simplify them.
7019 if (Call->isMustTailCall())
7020 return nullptr;
7021
7022 // call undef -> poison
7023 // call null -> poison
7024 if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee))
7025 return PoisonValue::get(Call->getType());
7026
7027 if (Value *V = tryConstantFoldCall(Call, Callee, Args, Q))
7028 return V;
7029
7030 auto *F = dyn_cast<Function>(Callee);
7031 if (F && F->isIntrinsic())
7032 if (Value *Ret = simplifyIntrinsic(Call, Callee, Args, Q))
7033 return Ret;
7034
7035 return nullptr;
7036 }
7037
simplifyConstrainedFPCall(CallBase * Call,const SimplifyQuery & Q)7038 Value *llvm::simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q) {
7039 assert(isa<ConstrainedFPIntrinsic>(Call));
7040 SmallVector<Value *, 4> Args(Call->args());
7041 if (Value *V = tryConstantFoldCall(Call, Call->getCalledOperand(), Args, Q))
7042 return V;
7043 if (Value *Ret = simplifyIntrinsic(Call, Call->getCalledOperand(), Args, Q))
7044 return Ret;
7045 return nullptr;
7046 }
7047
7048 /// Given operands for a Freeze, see if we can fold the result.
simplifyFreezeInst(Value * Op0,const SimplifyQuery & Q)7049 static Value *simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
7050 // Use a utility function defined in ValueTracking.
7051 if (llvm::isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT))
7052 return Op0;
7053 // We have room for improvement.
7054 return nullptr;
7055 }
7056
simplifyFreezeInst(Value * Op0,const SimplifyQuery & Q)7057 Value *llvm::simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
7058 return ::simplifyFreezeInst(Op0, Q);
7059 }
7060
simplifyLoadInst(LoadInst * LI,Value * PtrOp,const SimplifyQuery & Q)7061 Value *llvm::simplifyLoadInst(LoadInst *LI, Value *PtrOp,
7062 const SimplifyQuery &Q) {
7063 if (LI->isVolatile())
7064 return nullptr;
7065
7066 if (auto *PtrOpC = dyn_cast<Constant>(PtrOp))
7067 return ConstantFoldLoadFromConstPtr(PtrOpC, LI->getType(), Q.DL);
7068
7069 // We can only fold the load if it is from a constant global with definitive
7070 // initializer. Skip expensive logic if this is not the case.
7071 auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(PtrOp));
7072 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7073 return nullptr;
7074
7075 // If GlobalVariable's initializer is uniform, then return the constant
7076 // regardless of its offset.
7077 if (Constant *C = ConstantFoldLoadFromUniformValue(GV->getInitializer(),
7078 LI->getType(), Q.DL))
7079 return C;
7080
7081 // Try to convert operand into a constant by stripping offsets while looking
7082 // through invariant.group intrinsics.
7083 APInt Offset(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
7084 PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
7085 Q.DL, Offset, /* AllowNonInbounts */ true,
7086 /* AllowInvariantGroup */ true);
7087 if (PtrOp == GV) {
7088 // Index size may have changed due to address space casts.
7089 Offset = Offset.sextOrTrunc(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()));
7090 return ConstantFoldLoadFromConstPtr(GV, LI->getType(), std::move(Offset),
7091 Q.DL);
7092 }
7093
7094 return nullptr;
7095 }
7096
7097 /// See if we can compute a simplified version of this instruction.
7098 /// If not, this returns null.
7099
simplifyInstructionWithOperands(Instruction * I,ArrayRef<Value * > NewOps,const SimplifyQuery & SQ,unsigned MaxRecurse)7100 static Value *simplifyInstructionWithOperands(Instruction *I,
7101 ArrayRef<Value *> NewOps,
7102 const SimplifyQuery &SQ,
7103 unsigned MaxRecurse) {
7104 assert(I->getFunction() && "instruction should be inserted in a function");
7105 assert((!SQ.CxtI || SQ.CxtI->getFunction() == I->getFunction()) &&
7106 "context instruction should be in the same function");
7107
7108 const SimplifyQuery Q = SQ.CxtI ? SQ : SQ.getWithInstruction(I);
7109
7110 switch (I->getOpcode()) {
7111 default:
7112 if (llvm::all_of(NewOps, [](Value *V) { return isa<Constant>(V); })) {
7113 SmallVector<Constant *, 8> NewConstOps(NewOps.size());
7114 transform(NewOps, NewConstOps.begin(),
7115 [](Value *V) { return cast<Constant>(V); });
7116 return ConstantFoldInstOperands(I, NewConstOps, Q.DL, Q.TLI);
7117 }
7118 return nullptr;
7119 case Instruction::FNeg:
7120 return simplifyFNegInst(NewOps[0], I->getFastMathFlags(), Q, MaxRecurse);
7121 case Instruction::FAdd:
7122 return simplifyFAddInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7123 MaxRecurse);
7124 case Instruction::Add:
7125 return simplifyAddInst(
7126 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7127 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7128 case Instruction::FSub:
7129 return simplifyFSubInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7130 MaxRecurse);
7131 case Instruction::Sub:
7132 return simplifySubInst(
7133 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7134 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7135 case Instruction::FMul:
7136 return simplifyFMulInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7137 MaxRecurse);
7138 case Instruction::Mul:
7139 return simplifyMulInst(
7140 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7141 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7142 case Instruction::SDiv:
7143 return simplifySDivInst(NewOps[0], NewOps[1],
7144 Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7145 MaxRecurse);
7146 case Instruction::UDiv:
7147 return simplifyUDivInst(NewOps[0], NewOps[1],
7148 Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7149 MaxRecurse);
7150 case Instruction::FDiv:
7151 return simplifyFDivInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7152 MaxRecurse);
7153 case Instruction::SRem:
7154 return simplifySRemInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7155 case Instruction::URem:
7156 return simplifyURemInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7157 case Instruction::FRem:
7158 return simplifyFRemInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q,
7159 MaxRecurse);
7160 case Instruction::Shl:
7161 return simplifyShlInst(
7162 NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
7163 Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse);
7164 case Instruction::LShr:
7165 return simplifyLShrInst(NewOps[0], NewOps[1],
7166 Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7167 MaxRecurse);
7168 case Instruction::AShr:
7169 return simplifyAShrInst(NewOps[0], NewOps[1],
7170 Q.IIQ.isExact(cast<BinaryOperator>(I)), Q,
7171 MaxRecurse);
7172 case Instruction::And:
7173 return simplifyAndInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7174 case Instruction::Or:
7175 return simplifyOrInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7176 case Instruction::Xor:
7177 return simplifyXorInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7178 case Instruction::ICmp:
7179 return simplifyICmpInst(cast<ICmpInst>(I)->getCmpPredicate(), NewOps[0],
7180 NewOps[1], Q, MaxRecurse);
7181 case Instruction::FCmp:
7182 return simplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), NewOps[0],
7183 NewOps[1], I->getFastMathFlags(), Q, MaxRecurse);
7184 case Instruction::Select:
7185 return simplifySelectInst(NewOps[0], NewOps[1], NewOps[2], Q, MaxRecurse);
7186 case Instruction::GetElementPtr: {
7187 auto *GEPI = cast<GetElementPtrInst>(I);
7188 return simplifyGEPInst(GEPI->getSourceElementType(), NewOps[0],
7189 ArrayRef(NewOps).slice(1), GEPI->getNoWrapFlags(), Q,
7190 MaxRecurse);
7191 }
7192 case Instruction::InsertValue: {
7193 InsertValueInst *IV = cast<InsertValueInst>(I);
7194 return simplifyInsertValueInst(NewOps[0], NewOps[1], IV->getIndices(), Q,
7195 MaxRecurse);
7196 }
7197 case Instruction::InsertElement:
7198 return simplifyInsertElementInst(NewOps[0], NewOps[1], NewOps[2], Q);
7199 case Instruction::ExtractValue: {
7200 auto *EVI = cast<ExtractValueInst>(I);
7201 return simplifyExtractValueInst(NewOps[0], EVI->getIndices(), Q,
7202 MaxRecurse);
7203 }
7204 case Instruction::ExtractElement:
7205 return simplifyExtractElementInst(NewOps[0], NewOps[1], Q, MaxRecurse);
7206 case Instruction::ShuffleVector: {
7207 auto *SVI = cast<ShuffleVectorInst>(I);
7208 return simplifyShuffleVectorInst(NewOps[0], NewOps[1],
7209 SVI->getShuffleMask(), SVI->getType(), Q,
7210 MaxRecurse);
7211 }
7212 case Instruction::PHI:
7213 return simplifyPHINode(cast<PHINode>(I), NewOps, Q);
7214 case Instruction::Call:
7215 return simplifyCall(
7216 cast<CallInst>(I), NewOps.back(),
7217 NewOps.drop_back(1 + cast<CallInst>(I)->getNumTotalBundleOperands()), Q);
7218 case Instruction::Freeze:
7219 return llvm::simplifyFreezeInst(NewOps[0], Q);
7220 #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
7221 #include "llvm/IR/Instruction.def"
7222 #undef HANDLE_CAST_INST
7223 return simplifyCastInst(I->getOpcode(), NewOps[0], I->getType(), Q,
7224 MaxRecurse);
7225 case Instruction::Alloca:
7226 // No simplifications for Alloca and it can't be constant folded.
7227 return nullptr;
7228 case Instruction::Load:
7229 return simplifyLoadInst(cast<LoadInst>(I), NewOps[0], Q);
7230 }
7231 }
7232
simplifyInstructionWithOperands(Instruction * I,ArrayRef<Value * > NewOps,const SimplifyQuery & SQ)7233 Value *llvm::simplifyInstructionWithOperands(Instruction *I,
7234 ArrayRef<Value *> NewOps,
7235 const SimplifyQuery &SQ) {
7236 assert(NewOps.size() == I->getNumOperands() &&
7237 "Number of operands should match the instruction!");
7238 return ::simplifyInstructionWithOperands(I, NewOps, SQ, RecursionLimit);
7239 }
7240
simplifyInstruction(Instruction * I,const SimplifyQuery & SQ)7241 Value *llvm::simplifyInstruction(Instruction *I, const SimplifyQuery &SQ) {
7242 SmallVector<Value *, 8> Ops(I->operands());
7243 Value *Result = ::simplifyInstructionWithOperands(I, Ops, SQ, RecursionLimit);
7244
7245 /// If called on unreachable code, the instruction may simplify to itself.
7246 /// Make life easier for users by detecting that case here, and returning a
7247 /// safe value instead.
7248 return Result == I ? PoisonValue::get(I->getType()) : Result;
7249 }
7250
7251 /// Implementation of recursive simplification through an instruction's
7252 /// uses.
7253 ///
7254 /// This is the common implementation of the recursive simplification routines.
7255 /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
7256 /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of
7257 /// instructions to process and attempt to simplify it using
7258 /// InstructionSimplify. Recursively visited users which could not be
7259 /// simplified themselves are to the optional UnsimplifiedUsers set for
7260 /// further processing by the caller.
7261 ///
7262 /// This routine returns 'true' only when *it* simplifies something. The passed
7263 /// in simplified value does not count toward this.
replaceAndRecursivelySimplifyImpl(Instruction * I,Value * SimpleV,const TargetLibraryInfo * TLI,const DominatorTree * DT,AssumptionCache * AC,SmallSetVector<Instruction *,8> * UnsimplifiedUsers=nullptr)7264 static bool replaceAndRecursivelySimplifyImpl(
7265 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
7266 const DominatorTree *DT, AssumptionCache *AC,
7267 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr) {
7268 bool Simplified = false;
7269 SmallSetVector<Instruction *, 8> Worklist;
7270 const DataLayout &DL = I->getDataLayout();
7271
7272 // If we have an explicit value to collapse to, do that round of the
7273 // simplification loop by hand initially.
7274 if (SimpleV) {
7275 for (User *U : I->users())
7276 if (U != I)
7277 Worklist.insert(cast<Instruction>(U));
7278
7279 // Replace the instruction with its simplified value.
7280 I->replaceAllUsesWith(SimpleV);
7281
7282 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7283 I->eraseFromParent();
7284 } else {
7285 Worklist.insert(I);
7286 }
7287
7288 // Note that we must test the size on each iteration, the worklist can grow.
7289 for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
7290 I = Worklist[Idx];
7291
7292 // See if this instruction simplifies.
7293 SimpleV = simplifyInstruction(I, {DL, TLI, DT, AC});
7294 if (!SimpleV) {
7295 if (UnsimplifiedUsers)
7296 UnsimplifiedUsers->insert(I);
7297 continue;
7298 }
7299
7300 Simplified = true;
7301
7302 // Stash away all the uses of the old instruction so we can check them for
7303 // recursive simplifications after a RAUW. This is cheaper than checking all
7304 // uses of To on the recursive step in most cases.
7305 for (User *U : I->users())
7306 Worklist.insert(cast<Instruction>(U));
7307
7308 // Replace the instruction with its simplified value.
7309 I->replaceAllUsesWith(SimpleV);
7310
7311 if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects())
7312 I->eraseFromParent();
7313 }
7314 return Simplified;
7315 }
7316
replaceAndRecursivelySimplify(Instruction * I,Value * SimpleV,const TargetLibraryInfo * TLI,const DominatorTree * DT,AssumptionCache * AC,SmallSetVector<Instruction *,8> * UnsimplifiedUsers)7317 bool llvm::replaceAndRecursivelySimplify(
7318 Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
7319 const DominatorTree *DT, AssumptionCache *AC,
7320 SmallSetVector<Instruction *, 8> *UnsimplifiedUsers) {
7321 assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
7322 assert(SimpleV && "Must provide a simplified value.");
7323 return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC,
7324 UnsimplifiedUsers);
7325 }
7326
7327 namespace llvm {
getBestSimplifyQuery(Pass & P,Function & F)7328 const SimplifyQuery getBestSimplifyQuery(Pass &P, Function &F) {
7329 auto *DTWP = P.getAnalysisIfAvailable<DominatorTreeWrapperPass>();
7330 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
7331 auto *TLIWP = P.getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
7332 auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr;
7333 auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>();
7334 auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr;
7335 return {F.getDataLayout(), TLI, DT, AC};
7336 }
7337
getBestSimplifyQuery(LoopStandardAnalysisResults & AR,const DataLayout & DL)7338 const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &AR,
7339 const DataLayout &DL) {
7340 return {DL, &AR.TLI, &AR.DT, &AR.AC};
7341 }
7342
7343 template <class T, class... TArgs>
getBestSimplifyQuery(AnalysisManager<T,TArgs...> & AM,Function & F)7344 const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &AM,
7345 Function &F) {
7346 auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F);
7347 auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F);
7348 auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F);
7349 return {F.getDataLayout(), TLI, DT, AC};
7350 }
7351 template const SimplifyQuery getBestSimplifyQuery(AnalysisManager<Function> &,
7352 Function &);
7353
isUndefValue(Value * V) const7354 bool SimplifyQuery::isUndefValue(Value *V) const {
7355 if (!CanUseUndef)
7356 return false;
7357
7358 return match(V, m_Undef());
7359 }
7360
7361 } // namespace llvm
7362
anchor()7363 void InstSimplifyFolder::anchor() {}
7364