Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/Analysis/InstructionSimplify.cpp
Warning:line 6152, column 9
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name InstructionSimplify.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Analysis -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/Analysis -I include -I /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-09-04-125545-48738-1 -x c++ /build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/Analysis/InstructionSimplify.cpp

/build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/llvm/lib/Analysis/InstructionSimplify.cpp

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/InstSimplifyFolder.h"
30#include "llvm/Analysis/LoopAnalysisManager.h"
31#include "llvm/Analysis/MemoryBuiltins.h"
32#include "llvm/Analysis/OverflowInstAnalysis.h"
33#include "llvm/Analysis/ValueTracking.h"
34#include "llvm/Analysis/VectorUtils.h"
35#include "llvm/IR/ConstantRange.h"
36#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/Dominators.h"
38#include "llvm/IR/InstrTypes.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/Operator.h"
41#include "llvm/IR/PatternMatch.h"
42#include "llvm/Support/KnownBits.h"
43#include <algorithm>
44using namespace llvm;
45using namespace llvm::PatternMatch;
46
47#define DEBUG_TYPE"instsimplify" "instsimplify"
48
49enum { RecursionLimit = 3 };
50
51STATISTIC(NumExpand, "Number of expansions")static llvm::Statistic NumExpand = {"instsimplify", "NumExpand"
, "Number of expansions"}
;
52STATISTIC(NumReassoc, "Number of reassociations")static llvm::Statistic NumReassoc = {"instsimplify", "NumReassoc"
, "Number of reassociations"}
;
53
54static Value *simplifyAndInst(Value *, Value *, const SimplifyQuery &,
55 unsigned);
56static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned);
57static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &,
58 const SimplifyQuery &, unsigned);
59static Value *simplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &,
60 unsigned);
61static Value *simplifyBinOp(unsigned, Value *, Value *, const FastMathFlags &,
62 const SimplifyQuery &, unsigned);
63static Value *simplifyCmpInst(unsigned, Value *, Value *, const SimplifyQuery &,
64 unsigned);
65static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
66 const SimplifyQuery &Q, unsigned MaxRecurse);
67static Value *simplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned);
68static Value *simplifyXorInst(Value *, Value *, const SimplifyQuery &,
69 unsigned);
70static Value *simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &,
71 unsigned);
72static Value *simplifyGEPInst(Type *, Value *, ArrayRef<Value *>, bool,
73 const SimplifyQuery &, unsigned);
74static Value *simplifySelectInst(Value *, Value *, Value *,
75 const SimplifyQuery &, unsigned);
76
77static Value *foldSelectWithBinaryOp(Value *Cond, Value *TrueVal,
78 Value *FalseVal) {
79 BinaryOperator::BinaryOps BinOpCode;
80 if (auto *BO = dyn_cast<BinaryOperator>(Cond))
81 BinOpCode = BO->getOpcode();
82 else
83 return nullptr;
84
85 CmpInst::Predicate ExpectedPred, Pred1, Pred2;
86 if (BinOpCode == BinaryOperator::Or) {
87 ExpectedPred = ICmpInst::ICMP_NE;
88 } else if (BinOpCode == BinaryOperator::And) {
89 ExpectedPred = ICmpInst::ICMP_EQ;
90 } else
91 return nullptr;
92
93 // %A = icmp eq %TV, %FV
94 // %B = icmp eq %X, %Y (and one of these is a select operand)
95 // %C = and %A, %B
96 // %D = select %C, %TV, %FV
97 // -->
98 // %FV
99
100 // %A = icmp ne %TV, %FV
101 // %B = icmp ne %X, %Y (and one of these is a select operand)
102 // %C = or %A, %B
103 // %D = select %C, %TV, %FV
104 // -->
105 // %TV
106 Value *X, *Y;
107 if (!match(Cond, m_c_BinOp(m_c_ICmp(Pred1, m_Specific(TrueVal),
108 m_Specific(FalseVal)),
109 m_ICmp(Pred2, m_Value(X), m_Value(Y)))) ||
110 Pred1 != Pred2 || Pred1 != ExpectedPred)
111 return nullptr;
112
113 if (X == TrueVal || X == FalseVal || Y == TrueVal || Y == FalseVal)
114 return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal;
115
116 return nullptr;
117}
118
119/// For a boolean type or a vector of boolean type, return false or a vector
120/// with every element false.
121static Constant *getFalse(Type *Ty) { return ConstantInt::getFalse(Ty); }
122
123/// For a boolean type or a vector of boolean type, return true or a vector
124/// with every element true.
125static Constant *getTrue(Type *Ty) { return ConstantInt::getTrue(Ty); }
126
127/// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
128static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS,
129 Value *RHS) {
130 CmpInst *Cmp = dyn_cast<CmpInst>(V);
131 if (!Cmp)
132 return false;
133 CmpInst::Predicate CPred = Cmp->getPredicate();
134 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
135 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
136 return true;
137 return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
138 CRHS == LHS;
139}
140
141/// Simplify comparison with true or false branch of select:
142/// %sel = select i1 %cond, i32 %tv, i32 %fv
143/// %cmp = icmp sle i32 %sel, %rhs
144/// Compose new comparison by substituting %sel with either %tv or %fv
145/// and see if it simplifies.
146static Value *simplifyCmpSelCase(CmpInst::Predicate Pred, Value *LHS,
147 Value *RHS, Value *Cond,
148 const SimplifyQuery &Q, unsigned MaxRecurse,
149 Constant *TrueOrFalse) {
150 Value *SimplifiedCmp = simplifyCmpInst(Pred, LHS, RHS, Q, MaxRecurse);
151 if (SimplifiedCmp == Cond) {
152 // %cmp simplified to the select condition (%cond).
153 return TrueOrFalse;
154 } else if (!SimplifiedCmp && isSameCompare(Cond, Pred, LHS, RHS)) {
155 // It didn't simplify. However, if composed comparison is equivalent
156 // to the select condition (%cond) then we can replace it.
157 return TrueOrFalse;
158 }
159 return SimplifiedCmp;
160}
161
162/// Simplify comparison with true branch of select
163static Value *simplifyCmpSelTrueCase(CmpInst::Predicate Pred, Value *LHS,
164 Value *RHS, Value *Cond,
165 const SimplifyQuery &Q,
166 unsigned MaxRecurse) {
167 return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
168 getTrue(Cond->getType()));
169}
170
171/// Simplify comparison with false branch of select
172static Value *simplifyCmpSelFalseCase(CmpInst::Predicate Pred, Value *LHS,
173 Value *RHS, Value *Cond,
174 const SimplifyQuery &Q,
175 unsigned MaxRecurse) {
176 return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
177 getFalse(Cond->getType()));
178}
179
180/// We know comparison with both branches of select can be simplified, but they
181/// are not equal. This routine handles some logical simplifications.
182static Value *handleOtherCmpSelSimplifications(Value *TCmp, Value *FCmp,
183 Value *Cond,
184 const SimplifyQuery &Q,
185 unsigned MaxRecurse) {
186 // If the false value simplified to false, then the result of the compare
187 // is equal to "Cond && TCmp". This also catches the case when the false
188 // value simplified to false and the true value to true, returning "Cond".
189 // Folding select to and/or isn't poison-safe in general; impliesPoison
190 // checks whether folding it does not convert a well-defined value into
191 // poison.
192 if (match(FCmp, m_Zero()) && impliesPoison(TCmp, Cond))
193 if (Value *V = simplifyAndInst(Cond, TCmp, Q, MaxRecurse))
194 return V;
195 // If the true value simplified to true, then the result of the compare
196 // is equal to "Cond || FCmp".
197 if (match(TCmp, m_One()) && impliesPoison(FCmp, Cond))
198 if (Value *V = simplifyOrInst(Cond, FCmp, Q, MaxRecurse))
199 return V;
200 // Finally, if the false value simplified to true and the true value to
201 // false, then the result of the compare is equal to "!Cond".
202 if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
203 if (Value *V = simplifyXorInst(
204 Cond, Constant::getAllOnesValue(Cond->getType()), Q, MaxRecurse))
205 return V;
206 return nullptr;
207}
208
209/// Does the given value dominate the specified phi node?
210static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
211 Instruction *I = dyn_cast<Instruction>(V);
212 if (!I)
213 // Arguments and constants dominate all instructions.
214 return true;
215
216 // If we are processing instructions (and/or basic blocks) that have not been
217 // fully added to a function, the parent nodes may still be null. Simply
218 // return the conservative answer in these cases.
219 if (!I->getParent() || !P->getParent() || !I->getFunction())
220 return false;
221
222 // If we have a DominatorTree then do a precise test.
223 if (DT)
224 return DT->dominates(I, P);
225
226 // Otherwise, if the instruction is in the entry block and is not an invoke,
227 // then it obviously dominates all phi nodes.
228 if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I) &&
229 !isa<CallBrInst>(I))
230 return true;
231
232 return false;
233}
234
235/// Try to simplify a binary operator of form "V op OtherOp" where V is
236/// "(B0 opex B1)" by distributing 'op' across 'opex' as
237/// "(B0 op OtherOp) opex (B1 op OtherOp)".
238static Value *expandBinOp(Instruction::BinaryOps Opcode, Value *V,
239 Value *OtherOp, Instruction::BinaryOps OpcodeToExpand,
240 const SimplifyQuery &Q, unsigned MaxRecurse) {
241 auto *B = dyn_cast<BinaryOperator>(V);
242 if (!B || B->getOpcode() != OpcodeToExpand)
243 return nullptr;
244 Value *B0 = B->getOperand(0), *B1 = B->getOperand(1);
245 Value *L =
246 simplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(), MaxRecurse);
247 if (!L)
248 return nullptr;
249 Value *R =
250 simplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(), MaxRecurse);
251 if (!R)
252 return nullptr;
253
254 // Does the expanded pair of binops simplify to the existing binop?
255 if ((L == B0 && R == B1) ||
256 (Instruction::isCommutative(OpcodeToExpand) && L == B1 && R == B0)) {
257 ++NumExpand;
258 return B;
259 }
260
261 // Otherwise, return "L op' R" if it simplifies.
262 Value *S = simplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse);
263 if (!S)
264 return nullptr;
265
266 ++NumExpand;
267 return S;
268}
269
270/// Try to simplify binops of form "A op (B op' C)" or the commuted variant by
271/// distributing op over op'.
272static Value *expandCommutativeBinOp(Instruction::BinaryOps Opcode, Value *L,
273 Value *R,
274 Instruction::BinaryOps OpcodeToExpand,
275 const SimplifyQuery &Q,
276 unsigned MaxRecurse) {
277 // Recursion is always used, so bail out at once if we already hit the limit.
278 if (!MaxRecurse--)
279 return nullptr;
280
281 if (Value *V = expandBinOp(Opcode, L, R, OpcodeToExpand, Q, MaxRecurse))
282 return V;
283 if (Value *V = expandBinOp(Opcode, R, L, OpcodeToExpand, Q, MaxRecurse))
284 return V;
285 return nullptr;
286}
287
288/// Generic simplifications for associative binary operations.
289/// Returns the simpler value, or null if none was found.
290static Value *simplifyAssociativeBinOp(Instruction::BinaryOps Opcode,
291 Value *LHS, Value *RHS,
292 const SimplifyQuery &Q,
293 unsigned MaxRecurse) {
294 assert(Instruction::isAssociative(Opcode) && "Not an associative operation!")(static_cast <bool> (Instruction::isAssociative(Opcode)
&& "Not an associative operation!") ? void (0) : __assert_fail
("Instruction::isAssociative(Opcode) && \"Not an associative operation!\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 294, __extension__
__PRETTY_FUNCTION__))
;
295
296 // Recursion is always used, so bail out at once if we already hit the limit.
297 if (!MaxRecurse--)
298 return nullptr;
299
300 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
301 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
302
303 // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
304 if (Op0 && Op0->getOpcode() == Opcode) {
305 Value *A = Op0->getOperand(0);
306 Value *B = Op0->getOperand(1);
307 Value *C = RHS;
308
309 // Does "B op C" simplify?
310 if (Value *V = simplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
311 // It does! Return "A op V" if it simplifies or is already available.
312 // If V equals B then "A op V" is just the LHS.
313 if (V == B)
314 return LHS;
315 // Otherwise return "A op V" if it simplifies.
316 if (Value *W = simplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
317 ++NumReassoc;
318 return W;
319 }
320 }
321 }
322
323 // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
324 if (Op1 && Op1->getOpcode() == Opcode) {
325 Value *A = LHS;
326 Value *B = Op1->getOperand(0);
327 Value *C = Op1->getOperand(1);
328
329 // Does "A op B" simplify?
330 if (Value *V = simplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
331 // It does! Return "V op C" if it simplifies or is already available.
332 // If V equals B then "V op C" is just the RHS.
333 if (V == B)
334 return RHS;
335 // Otherwise return "V op C" if it simplifies.
336 if (Value *W = simplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
337 ++NumReassoc;
338 return W;
339 }
340 }
341 }
342
343 // The remaining transforms require commutativity as well as associativity.
344 if (!Instruction::isCommutative(Opcode))
345 return nullptr;
346
347 // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
348 if (Op0 && Op0->getOpcode() == Opcode) {
349 Value *A = Op0->getOperand(0);
350 Value *B = Op0->getOperand(1);
351 Value *C = RHS;
352
353 // Does "C op A" simplify?
354 if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
355 // It does! Return "V op B" if it simplifies or is already available.
356 // If V equals A then "V op B" is just the LHS.
357 if (V == A)
358 return LHS;
359 // Otherwise return "V op B" if it simplifies.
360 if (Value *W = simplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
361 ++NumReassoc;
362 return W;
363 }
364 }
365 }
366
367 // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
368 if (Op1 && Op1->getOpcode() == Opcode) {
369 Value *A = LHS;
370 Value *B = Op1->getOperand(0);
371 Value *C = Op1->getOperand(1);
372
373 // Does "C op A" simplify?
374 if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
375 // It does! Return "B op V" if it simplifies or is already available.
376 // If V equals C then "B op V" is just the RHS.
377 if (V == C)
378 return RHS;
379 // Otherwise return "B op V" if it simplifies.
380 if (Value *W = simplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
381 ++NumReassoc;
382 return W;
383 }
384 }
385 }
386
387 return nullptr;
388}
389
390/// In the case of a binary operation with a select instruction as an operand,
391/// try to simplify the binop by seeing whether evaluating it on both branches
392/// of the select results in the same value. Returns the common value if so,
393/// otherwise returns null.
394static Value *threadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS,
395 Value *RHS, const SimplifyQuery &Q,
396 unsigned MaxRecurse) {
397 // Recursion is always used, so bail out at once if we already hit the limit.
398 if (!MaxRecurse--)
399 return nullptr;
400
401 SelectInst *SI;
402 if (isa<SelectInst>(LHS)) {
403 SI = cast<SelectInst>(LHS);
404 } else {
405 assert(isa<SelectInst>(RHS) && "No select instruction operand!")(static_cast <bool> (isa<SelectInst>(RHS) &&
"No select instruction operand!") ? void (0) : __assert_fail
("isa<SelectInst>(RHS) && \"No select instruction operand!\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 405, __extension__
__PRETTY_FUNCTION__))
;
406 SI = cast<SelectInst>(RHS);
407 }
408
409 // Evaluate the BinOp on the true and false branches of the select.
410 Value *TV;
411 Value *FV;
412 if (SI == LHS) {
413 TV = simplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
414 FV = simplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
415 } else {
416 TV = simplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
417 FV = simplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
418 }
419
420 // If they simplified to the same value, then return the common value.
421 // If they both failed to simplify then return null.
422 if (TV == FV)
423 return TV;
424
425 // If one branch simplified to undef, return the other one.
426 if (TV && Q.isUndefValue(TV))
427 return FV;
428 if (FV && Q.isUndefValue(FV))
429 return TV;
430
431 // If applying the operation did not change the true and false select values,
432 // then the result of the binop is the select itself.
433 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
434 return SI;
435
436 // If one branch simplified and the other did not, and the simplified
437 // value is equal to the unsimplified one, return the simplified value.
438 // For example, select (cond, X, X & Z) & Z -> X & Z.
439 if ((FV && !TV) || (TV && !FV)) {
440 // Check that the simplified value has the form "X op Y" where "op" is the
441 // same as the original operation.
442 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
443 if (Simplified && Simplified->getOpcode() == unsigned(Opcode)) {
444 // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
445 // We already know that "op" is the same as for the simplified value. See
446 // if the operands match too. If so, return the simplified value.
447 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
448 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
449 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
450 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
451 Simplified->getOperand(1) == UnsimplifiedRHS)
452 return Simplified;
453 if (Simplified->isCommutative() &&
454 Simplified->getOperand(1) == UnsimplifiedLHS &&
455 Simplified->getOperand(0) == UnsimplifiedRHS)
456 return Simplified;
457 }
458 }
459
460 return nullptr;
461}
462
463/// In the case of a comparison with a select instruction, try to simplify the
464/// comparison by seeing whether both branches of the select result in the same
465/// value. Returns the common value if so, otherwise returns null.
466/// For example, if we have:
467/// %tmp = select i1 %cmp, i32 1, i32 2
468/// %cmp1 = icmp sle i32 %tmp, 3
469/// We can simplify %cmp1 to true, because both branches of select are
470/// less than 3. We compose new comparison by substituting %tmp with both
471/// branches of select and see if it can be simplified.
472static Value *threadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
473 Value *RHS, const SimplifyQuery &Q,
474 unsigned MaxRecurse) {
475 // Recursion is always used, so bail out at once if we already hit the limit.
476 if (!MaxRecurse--)
477 return nullptr;
478
479 // Make sure the select is on the LHS.
480 if (!isa<SelectInst>(LHS)) {
481 std::swap(LHS, RHS);
482 Pred = CmpInst::getSwappedPredicate(Pred);
483 }
484 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!")(static_cast <bool> (isa<SelectInst>(LHS) &&
"Not comparing with a select instruction!") ? void (0) : __assert_fail
("isa<SelectInst>(LHS) && \"Not comparing with a select instruction!\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 484, __extension__
__PRETTY_FUNCTION__))
;
485 SelectInst *SI = cast<SelectInst>(LHS);
486 Value *Cond = SI->getCondition();
487 Value *TV = SI->getTrueValue();
488 Value *FV = SI->getFalseValue();
489
490 // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
491 // Does "cmp TV, RHS" simplify?
492 Value *TCmp = simplifyCmpSelTrueCase(Pred, TV, RHS, Cond, Q, MaxRecurse);
493 if (!TCmp)
494 return nullptr;
495
496 // Does "cmp FV, RHS" simplify?
497 Value *FCmp = simplifyCmpSelFalseCase(Pred, FV, RHS, Cond, Q, MaxRecurse);
498 if (!FCmp)
499 return nullptr;
500
501 // If both sides simplified to the same value, then use it as the result of
502 // the original comparison.
503 if (TCmp == FCmp)
504 return TCmp;
505
506 // The remaining cases only make sense if the select condition has the same
507 // type as the result of the comparison, so bail out if this is not so.
508 if (Cond->getType()->isVectorTy() == RHS->getType()->isVectorTy())
509 return handleOtherCmpSelSimplifications(TCmp, FCmp, Cond, Q, MaxRecurse);
510
511 return nullptr;
512}
513
514/// In the case of a binary operation with an operand that is a PHI instruction,
515/// try to simplify the binop by seeing whether evaluating it on the incoming
516/// phi values yields the same result for every value. If so returns the common
517/// value, otherwise returns null.
518static Value *threadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS,
519 Value *RHS, const SimplifyQuery &Q,
520 unsigned MaxRecurse) {
521 // Recursion is always used, so bail out at once if we already hit the limit.
522 if (!MaxRecurse--)
523 return nullptr;
524
525 PHINode *PI;
526 if (isa<PHINode>(LHS)) {
527 PI = cast<PHINode>(LHS);
528 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
529 if (!valueDominatesPHI(RHS, PI, Q.DT))
530 return nullptr;
531 } else {
532 assert(isa<PHINode>(RHS) && "No PHI instruction operand!")(static_cast <bool> (isa<PHINode>(RHS) &&
"No PHI instruction operand!") ? void (0) : __assert_fail ("isa<PHINode>(RHS) && \"No PHI instruction operand!\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 532, __extension__
__PRETTY_FUNCTION__))
;
533 PI = cast<PHINode>(RHS);
534 // Bail out if LHS and the phi may be mutually interdependent due to a loop.
535 if (!valueDominatesPHI(LHS, PI, Q.DT))
536 return nullptr;
537 }
538
539 // Evaluate the BinOp on the incoming phi values.
540 Value *CommonValue = nullptr;
541 for (Value *Incoming : PI->incoming_values()) {
542 // If the incoming value is the phi node itself, it can safely be skipped.
543 if (Incoming == PI)
544 continue;
545 Value *V = PI == LHS ? simplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse)
546 : simplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse);
547 // If the operation failed to simplify, or simplified to a different value
548 // to previously, then give up.
549 if (!V || (CommonValue && V != CommonValue))
550 return nullptr;
551 CommonValue = V;
552 }
553
554 return CommonValue;
555}
556
557/// In the case of a comparison with a PHI instruction, try to simplify the
558/// comparison by seeing whether comparing with all of the incoming phi values
559/// yields the same result every time. If so returns the common result,
560/// otherwise returns null.
561static Value *threadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
562 const SimplifyQuery &Q, unsigned MaxRecurse) {
563 // Recursion is always used, so bail out at once if we already hit the limit.
564 if (!MaxRecurse--)
565 return nullptr;
566
567 // Make sure the phi is on the LHS.
568 if (!isa<PHINode>(LHS)) {
569 std::swap(LHS, RHS);
570 Pred = CmpInst::getSwappedPredicate(Pred);
571 }
572 assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!")(static_cast <bool> (isa<PHINode>(LHS) &&
"Not comparing with a phi instruction!") ? void (0) : __assert_fail
("isa<PHINode>(LHS) && \"Not comparing with a phi instruction!\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 572, __extension__
__PRETTY_FUNCTION__))
;
573 PHINode *PI = cast<PHINode>(LHS);
574
575 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
576 if (!valueDominatesPHI(RHS, PI, Q.DT))
577 return nullptr;
578
579 // Evaluate the BinOp on the incoming phi values.
580 Value *CommonValue = nullptr;
581 for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u) {
582 Value *Incoming = PI->getIncomingValue(u);
583 Instruction *InTI = PI->getIncomingBlock(u)->getTerminator();
584 // If the incoming value is the phi node itself, it can safely be skipped.
585 if (Incoming == PI)
586 continue;
587 // Change the context instruction to the "edge" that flows into the phi.
588 // This is important because that is where incoming is actually "evaluated"
589 // even though it is used later somewhere else.
590 Value *V = simplifyCmpInst(Pred, Incoming, RHS, Q.getWithInstruction(InTI),
591 MaxRecurse);
592 // If the operation failed to simplify, or simplified to a different value
593 // to previously, then give up.
594 if (!V || (CommonValue && V != CommonValue))
595 return nullptr;
596 CommonValue = V;
597 }
598
599 return CommonValue;
600}
601
602static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode,
603 Value *&Op0, Value *&Op1,
604 const SimplifyQuery &Q) {
605 if (auto *CLHS = dyn_cast<Constant>(Op0)) {
606 if (auto *CRHS = dyn_cast<Constant>(Op1)) {
607 switch (Opcode) {
608 default:
609 break;
610 case Instruction::FAdd:
611 case Instruction::FSub:
612 case Instruction::FMul:
613 case Instruction::FDiv:
614 case Instruction::FRem:
615 if (Q.CxtI != nullptr)
616 return ConstantFoldFPInstOperands(Opcode, CLHS, CRHS, Q.DL, Q.CxtI);
617 }
618 return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL);
619 }
620
621 // Canonicalize the constant to the RHS if this is a commutative operation.
622 if (Instruction::isCommutative(Opcode))
623 std::swap(Op0, Op1);
624 }
625 return nullptr;
626}
627
628/// Given operands for an Add, see if we can fold the result.
629/// If not, this returns null.
630static Value *simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
631 const SimplifyQuery &Q, unsigned MaxRecurse) {
632 if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q))
633 return C;
634
635 // X + poison -> poison
636 if (isa<PoisonValue>(Op1))
637 return Op1;
638
639 // X + undef -> undef
640 if (Q.isUndefValue(Op1))
641 return Op1;
642
643 // X + 0 -> X
644 if (match(Op1, m_Zero()))
645 return Op0;
646
647 // If two operands are negative, return 0.
648 if (isKnownNegation(Op0, Op1))
649 return Constant::getNullValue(Op0->getType());
650
651 // X + (Y - X) -> Y
652 // (Y - X) + X -> Y
653 // Eg: X + -X -> 0
654 Value *Y = nullptr;
655 if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
656 match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
657 return Y;
658
659 // X + ~X -> -1 since ~X = -X-1
660 Type *Ty = Op0->getType();
661 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
662 return Constant::getAllOnesValue(Ty);
663
664 // add nsw/nuw (xor Y, signmask), signmask --> Y
665 // The no-wrapping add guarantees that the top bit will be set by the add.
666 // Therefore, the xor must be clearing the already set sign bit of Y.
667 if ((IsNSW || IsNUW) && match(Op1, m_SignMask()) &&
668 match(Op0, m_Xor(m_Value(Y), m_SignMask())))
669 return Y;
670
671 // add nuw %x, -1 -> -1, because %x can only be 0.
672 if (IsNUW && match(Op1, m_AllOnes()))
673 return Op1; // Which is -1.
674
675 /// i1 add -> xor.
676 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
677 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
678 return V;
679
680 // Try some generic simplifications for associative operations.
681 if (Value *V =
682 simplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, MaxRecurse))
683 return V;
684
685 // Threading Add over selects and phi nodes is pointless, so don't bother.
686 // Threading over the select in "A + select(cond, B, C)" means evaluating
687 // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
688 // only if B and C are equal. If B and C are equal then (since we assume
689 // that operands have already been simplified) "select(cond, B, C)" should
690 // have been simplified to the common value of B and C already. Analysing
691 // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly
692 // for threading over phi nodes.
693
694 return nullptr;
695}
696
697Value *llvm::simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
698 const SimplifyQuery &Query) {
699 return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit);
700}
701
702/// Compute the base pointer and cumulative constant offsets for V.
703///
704/// This strips all constant offsets off of V, leaving it the base pointer, and
705/// accumulates the total constant offset applied in the returned constant.
706/// It returns zero if there are no constant offsets applied.
707///
708/// This is very similar to stripAndAccumulateConstantOffsets(), except it
709/// normalizes the offset bitwidth to the stripped pointer type, not the
710/// original pointer type.
711static APInt stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
712 bool AllowNonInbounds = false) {
713 assert(V->getType()->isPtrOrPtrVectorTy())(static_cast <bool> (V->getType()->isPtrOrPtrVectorTy
()) ? void (0) : __assert_fail ("V->getType()->isPtrOrPtrVectorTy()"
, "llvm/lib/Analysis/InstructionSimplify.cpp", 713, __extension__
__PRETTY_FUNCTION__))
;
714
715 APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType()));
716 V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
717 // As that strip may trace through `addrspacecast`, need to sext or trunc
718 // the offset calculated.
719 return Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(V->getType()));
720}
721
722/// Compute the constant difference between two pointer values.
723/// If the difference is not a constant, returns zero.
724static Constant *computePointerDifference(const DataLayout &DL, Value *LHS,
725 Value *RHS) {
726 APInt LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
727 APInt RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
728
729 // If LHS and RHS are not related via constant offsets to the same base
730 // value, there is nothing we can do here.
731 if (LHS != RHS)
732 return nullptr;
733
734 // Otherwise, the difference of LHS - RHS can be computed as:
735 // LHS - RHS
736 // = (LHSOffset + Base) - (RHSOffset + Base)
737 // = LHSOffset - RHSOffset
738 Constant *Res = ConstantInt::get(LHS->getContext(), LHSOffset - RHSOffset);
739 if (auto *VecTy = dyn_cast<VectorType>(LHS->getType()))
740 Res = ConstantVector::getSplat(VecTy->getElementCount(), Res);
741 return Res;
742}
743
744/// Given operands for a Sub, see if we can fold the result.
745/// If not, this returns null.
746static Value *simplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
747 const SimplifyQuery &Q, unsigned MaxRecurse) {
748 if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q))
749 return C;
750
751 // X - poison -> poison
752 // poison - X -> poison
753 if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
754 return PoisonValue::get(Op0->getType());
755
756 // X - undef -> undef
757 // undef - X -> undef
758 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
759 return UndefValue::get(Op0->getType());
760
761 // X - 0 -> X
762 if (match(Op1, m_Zero()))
763 return Op0;
764
765 // X - X -> 0
766 if (Op0 == Op1)
767 return Constant::getNullValue(Op0->getType());
768
769 // Is this a negation?
770 if (match(Op0, m_Zero())) {
771 // 0 - X -> 0 if the sub is NUW.
772 if (isNUW)
773 return Constant::getNullValue(Op0->getType());
774
775 KnownBits Known = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
776 if (Known.Zero.isMaxSignedValue()) {
777 // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
778 // Op1 must be 0 because negating the minimum signed value is undefined.
779 if (isNSW)
780 return Constant::getNullValue(Op0->getType());
781
782 // 0 - X -> X if X is 0 or the minimum signed value.
783 return Op1;
784 }
785 }
786
787 // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
788 // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
789 Value *X = nullptr, *Y = nullptr, *Z = Op1;
790 if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
791 // See if "V === Y - Z" simplifies.
792 if (Value *V = simplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse - 1))
793 // It does! Now see if "X + V" simplifies.
794 if (Value *W = simplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse - 1)) {
795 // It does, we successfully reassociated!
796 ++NumReassoc;
797 return W;
798 }
799 // See if "V === X - Z" simplifies.
800 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
801 // It does! Now see if "Y + V" simplifies.
802 if (Value *W = simplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse - 1)) {
803 // It does, we successfully reassociated!
804 ++NumReassoc;
805 return W;
806 }
807 }
808
809 // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
810 // For example, X - (X + 1) -> -1
811 X = Op0;
812 if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
813 // See if "V === X - Y" simplifies.
814 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
815 // It does! Now see if "V - Z" simplifies.
816 if (Value *W = simplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse - 1)) {
817 // It does, we successfully reassociated!
818 ++NumReassoc;
819 return W;
820 }
821 // See if "V === X - Z" simplifies.
822 if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1))
823 // It does! Now see if "V - Y" simplifies.
824 if (Value *W = simplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse - 1)) {
825 // It does, we successfully reassociated!
826 ++NumReassoc;
827 return W;
828 }
829 }
830
831 // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
832 // For example, X - (X - Y) -> Y.
833 Z = Op0;
834 if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
835 // See if "V === Z - X" simplifies.
836 if (Value *V = simplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse - 1))
837 // It does! Now see if "V + Y" simplifies.
838 if (Value *W = simplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse - 1)) {
839 // It does, we successfully reassociated!
840 ++NumReassoc;
841 return W;
842 }
843
844 // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
845 if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) &&
846 match(Op1, m_Trunc(m_Value(Y))))
847 if (X->getType() == Y->getType())
848 // See if "V === X - Y" simplifies.
849 if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1))
850 // It does! Now see if "trunc V" simplifies.
851 if (Value *W = simplifyCastInst(Instruction::Trunc, V, Op0->getType(),
852 Q, MaxRecurse - 1))
853 // It does, return the simplified "trunc V".
854 return W;
855
856 // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
857 if (match(Op0, m_PtrToInt(m_Value(X))) && match(Op1, m_PtrToInt(m_Value(Y))))
858 if (Constant *Result = computePointerDifference(Q.DL, X, Y))
859 return ConstantExpr::getIntegerCast(Result, Op0->getType(), true);
860
861 // i1 sub -> xor.
862 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
863 if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1))
864 return V;
865
866 // Threading Sub over selects and phi nodes is pointless, so don't bother.
867 // Threading over the select in "A - select(cond, B, C)" means evaluating
868 // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
869 // only if B and C are equal. If B and C are equal then (since we assume
870 // that operands have already been simplified) "select(cond, B, C)" should
871 // have been simplified to the common value of B and C already. Analysing
872 // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
873 // for threading over phi nodes.
874
875 return nullptr;
876}
877
878Value *llvm::simplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
879 const SimplifyQuery &Q) {
880 return ::simplifySubInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit);
881}
882
883/// Given operands for a Mul, see if we can fold the result.
884/// If not, this returns null.
885static Value *simplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
886 unsigned MaxRecurse) {
887 if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q))
888 return C;
889
890 // X * poison -> poison
891 if (isa<PoisonValue>(Op1))
892 return Op1;
893
894 // X * undef -> 0
895 // X * 0 -> 0
896 if (Q.isUndefValue(Op1) || match(Op1, m_Zero()))
897 return Constant::getNullValue(Op0->getType());
898
899 // X * 1 -> X
900 if (match(Op1, m_One()))
901 return Op0;
902
903 // (X / Y) * Y -> X if the division is exact.
904 Value *X = nullptr;
905 if (Q.IIQ.UseInstrInfo &&
906 (match(Op0,
907 m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
908 match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))) // Y * (X / Y)
909 return X;
910
911 // i1 mul -> and.
912 if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
913 if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1))
914 return V;
915
916 // Try some generic simplifications for associative operations.
917 if (Value *V =
918 simplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
919 return V;
920
921 // Mul distributes over Add. Try some generic simplifications based on this.
922 if (Value *V = expandCommutativeBinOp(Instruction::Mul, Op0, Op1,
923 Instruction::Add, Q, MaxRecurse))
924 return V;
925
926 // If the operation is with the result of a select instruction, check whether
927 // operating on either branch of the select always yields the same value.
928 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
929 if (Value *V =
930 threadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
931 return V;
932
933 // If the operation is with the result of a phi instruction, check whether
934 // operating on all incoming values of the phi always yields the same value.
935 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
936 if (Value *V =
937 threadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, MaxRecurse))
938 return V;
939
940 return nullptr;
941}
942
943Value *llvm::simplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
944 return ::simplifyMulInst(Op0, Op1, Q, RecursionLimit);
945}
946
947/// Check for common or similar folds of integer division or integer remainder.
948/// This applies to all 4 opcodes (sdiv/udiv/srem/urem).
949static Value *simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0,
950 Value *Op1, const SimplifyQuery &Q) {
951 bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
952 bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
953
954 Type *Ty = Op0->getType();
955
956 // X / undef -> poison
957 // X % undef -> poison
958 if (Q.isUndefValue(Op1) || isa<PoisonValue>(Op1))
959 return PoisonValue::get(Ty);
960
961 // X / 0 -> poison
962 // X % 0 -> poison
963 // We don't need to preserve faults!
964 if (match(Op1, m_Zero()))
965 return PoisonValue::get(Ty);
966
967 // If any element of a constant divisor fixed width vector is zero or undef
968 // the behavior is undefined and we can fold the whole op to poison.
969 auto *Op1C = dyn_cast<Constant>(Op1);
970 auto *VTy = dyn_cast<FixedVectorType>(Ty);
971 if (Op1C && VTy) {
972 unsigned NumElts = VTy->getNumElements();
973 for (unsigned i = 0; i != NumElts; ++i) {
974 Constant *Elt = Op1C->getAggregateElement(i);
975 if (Elt && (Elt->isNullValue() || Q.isUndefValue(Elt)))
976 return PoisonValue::get(Ty);
977 }
978 }
979
980 // poison / X -> poison
981 // poison % X -> poison
982 if (isa<PoisonValue>(Op0))
983 return Op0;
984
985 // undef / X -> 0
986 // undef % X -> 0
987 if (Q.isUndefValue(Op0))
988 return Constant::getNullValue(Ty);
989
990 // 0 / X -> 0
991 // 0 % X -> 0
992 if (match(Op0, m_Zero()))
993 return Constant::getNullValue(Op0->getType());
994
995 // X / X -> 1
996 // X % X -> 0
997 if (Op0 == Op1)
998 return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty);
999
1000 // X / 1 -> X
1001 // X % 1 -> 0
1002 // If this is a boolean op (single-bit element type), we can't have
1003 // division-by-zero or remainder-by-zero, so assume the divisor is 1.
1004 // Similarly, if we're zero-extending a boolean divisor, then assume it's a 1.
1005 Value *X;
1006 if (match(Op1, m_One()) || Ty->isIntOrIntVectorTy(1) ||
1007 (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
1008 return IsDiv ? Op0 : Constant::getNullValue(Ty);
1009
1010 // If X * Y does not overflow, then:
1011 // X * Y / Y -> X
1012 // X * Y % Y -> 0
1013 if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) {
1014 auto *Mul = cast<OverflowingBinaryOperator>(Op0);
1015 // The multiplication can't overflow if it is defined not to, or if
1016 // X == A / Y for some A.
1017 if ((IsSigned && Q.IIQ.hasNoSignedWrap(Mul)) ||
1018 (!IsSigned && Q.IIQ.hasNoUnsignedWrap(Mul)) ||
1019 (IsSigned && match(X, m_SDiv(m_Value(), m_Specific(Op1)))) ||
1020 (!IsSigned && match(X, m_UDiv(m_Value(), m_Specific(Op1))))) {
1021 return IsDiv ? X : Constant::getNullValue(Op0->getType());
1022 }
1023 }
1024
1025 return nullptr;
1026}
1027
1028/// Given a predicate and two operands, return true if the comparison is true.
1029/// This is a helper for div/rem simplification where we return some other value
1030/// when we can prove a relationship between the operands.
1031static bool isICmpTrue(ICmpInst::Predicate Pred, Value *LHS, Value *RHS,
1032 const SimplifyQuery &Q, unsigned MaxRecurse) {
1033 Value *V = simplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse);
1034 Constant *C = dyn_cast_or_null<Constant>(V);
1035 return (C && C->isAllOnesValue());
1036}
1037
1038/// Return true if we can simplify X / Y to 0. Remainder can adapt that answer
1039/// to simplify X % Y to X.
1040static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q,
1041 unsigned MaxRecurse, bool IsSigned) {
1042 // Recursion is always used, so bail out at once if we already hit the limit.
1043 if (!MaxRecurse--)
1044 return false;
1045
1046 if (IsSigned) {
1047 // |X| / |Y| --> 0
1048 //
1049 // We require that 1 operand is a simple constant. That could be extended to
1050 // 2 variables if we computed the sign bit for each.
1051 //
1052 // Make sure that a constant is not the minimum signed value because taking
1053 // the abs() of that is undefined.
1054 Type *Ty = X->getType();
1055 const APInt *C;
1056 if (match(X, m_APInt(C)) && !C->isMinSignedValue()) {
1057 // Is the variable divisor magnitude always greater than the constant
1058 // dividend magnitude?
1059 // |Y| > |C| --> Y < -abs(C) or Y > abs(C)
1060 Constant *PosDividendC = ConstantInt::get(Ty, C->abs());
1061 Constant *NegDividendC = ConstantInt::get(Ty, -C->abs());
1062 if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) ||
1063 isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse))
1064 return true;
1065 }
1066 if (match(Y, m_APInt(C))) {
1067 // Special-case: we can't take the abs() of a minimum signed value. If
1068 // that's the divisor, then all we have to do is prove that the dividend
1069 // is also not the minimum signed value.
1070 if (C->isMinSignedValue())
1071 return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse);
1072
1073 // Is the variable dividend magnitude always less than the constant
1074 // divisor magnitude?
1075 // |X| < |C| --> X > -abs(C) and X < abs(C)
1076 Constant *PosDivisorC = ConstantInt::get(Ty, C->abs());
1077 Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs());
1078 if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) &&
1079 isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse))
1080 return true;
1081 }
1082 return false;
1083 }
1084
1085 // IsSigned == false.
1086
1087 // Is the unsigned dividend known to be less than a constant divisor?
1088 // TODO: Convert this (and above) to range analysis
1089 // ("computeConstantRangeIncludingKnownBits")?
1090 const APInt *C;
1091 if (match(Y, m_APInt(C)) &&
1092 computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI, Q.DT).getMaxValue().ult(*C))
1093 return true;
1094
1095 // Try again for any divisor:
1096 // Is the dividend unsigned less than the divisor?
1097 return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse);
1098}
1099
1100/// These are simplifications common to SDiv and UDiv.
1101static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1102 const SimplifyQuery &Q, unsigned MaxRecurse) {
1103 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1104 return C;
1105
1106 if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q))
1107 return V;
1108
1109 bool IsSigned = Opcode == Instruction::SDiv;
1110
1111 // (X rem Y) / Y -> 0
1112 if ((IsSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
1113 (!IsSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
1114 return Constant::getNullValue(Op0->getType());
1115
1116 // (X /u C1) /u C2 -> 0 if C1 * C2 overflow
1117 ConstantInt *C1, *C2;
1118 if (!IsSigned && match(Op0, m_UDiv(m_Value(), m_ConstantInt(C1))) &&
1119 match(Op1, m_ConstantInt(C2))) {
1120 bool Overflow;
1121 (void)C1->getValue().umul_ov(C2->getValue(), Overflow);
1122 if (Overflow)
1123 return Constant::getNullValue(Op0->getType());
1124 }
1125
1126 // If the operation is with the result of a select instruction, check whether
1127 // operating on either branch of the select always yields the same value.
1128 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1129 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1130 return V;
1131
1132 // If the operation is with the result of a phi instruction, check whether
1133 // operating on all incoming values of the phi always yields the same value.
1134 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1135 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1136 return V;
1137
1138 if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1139 return Constant::getNullValue(Op0->getType());
1140
1141 return nullptr;
1142}
1143
1144/// These are simplifications common to SRem and URem.
1145static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
1146 const SimplifyQuery &Q, unsigned MaxRecurse) {
1147 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1148 return C;
1149
1150 if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q))
1151 return V;
1152
1153 // (X % Y) % Y -> X % Y
1154 if ((Opcode == Instruction::SRem &&
1155 match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
1156 (Opcode == Instruction::URem &&
1157 match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
1158 return Op0;
1159
1160 // (X << Y) % X -> 0
1161 if (Q.IIQ.UseInstrInfo &&
1162 ((Opcode == Instruction::SRem &&
1163 match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))) ||
1164 (Opcode == Instruction::URem &&
1165 match(Op0, m_NUWShl(m_Specific(Op1), m_Value())))))
1166 return Constant::getNullValue(Op0->getType());
1167
1168 // If the operation is with the result of a select instruction, check whether
1169 // operating on either branch of the select always yields the same value.
1170 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1171 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1172 return V;
1173
1174 // If the operation is with the result of a phi instruction, check whether
1175 // operating on all incoming values of the phi always yields the same value.
1176 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1177 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1178 return V;
1179
1180 // If X / Y == 0, then X % Y == X.
1181 if (isDivZero(Op0, Op1, Q, MaxRecurse, Opcode == Instruction::SRem))
1182 return Op0;
1183
1184 return nullptr;
1185}
1186
1187/// Given operands for an SDiv, see if we can fold the result.
1188/// If not, this returns null.
1189static Value *simplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1190 unsigned MaxRecurse) {
1191 // If two operands are negated and no signed overflow, return -1.
1192 if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
1193 return Constant::getAllOnesValue(Op0->getType());
1194
1195 return simplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse);
1196}
1197
1198Value *llvm::simplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1199 return ::simplifySDivInst(Op0, Op1, Q, RecursionLimit);
1200}
1201
1202/// Given operands for a UDiv, see if we can fold the result.
1203/// If not, this returns null.
1204static Value *simplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1205 unsigned MaxRecurse) {
1206 return simplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse);
1207}
1208
1209Value *llvm::simplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1210 return ::simplifyUDivInst(Op0, Op1, Q, RecursionLimit);
1211}
1212
1213/// Given operands for an SRem, see if we can fold the result.
1214/// If not, this returns null.
1215static Value *simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1216 unsigned MaxRecurse) {
1217 // If the divisor is 0, the result is undefined, so assume the divisor is -1.
1218 // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0
1219 Value *X;
1220 if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
1221 return ConstantInt::getNullValue(Op0->getType());
1222
1223 // If the two operands are negated, return 0.
1224 if (isKnownNegation(Op0, Op1))
1225 return ConstantInt::getNullValue(Op0->getType());
1226
1227 return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1228}
1229
1230Value *llvm::simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1231 return ::simplifySRemInst(Op0, Op1, Q, RecursionLimit);
1232}
1233
1234/// Given operands for a URem, see if we can fold the result.
1235/// If not, this returns null.
1236static Value *simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1237 unsigned MaxRecurse) {
1238 return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1239}
1240
1241Value *llvm::simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1242 return ::simplifyURemInst(Op0, Op1, Q, RecursionLimit);
1243}
1244
1245/// Returns true if a shift by \c Amount always yields poison.
1246static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q) {
1247 Constant *C = dyn_cast<Constant>(Amount);
1248 if (!C)
1249 return false;
1250
1251 // X shift by undef -> poison because it may shift by the bitwidth.
1252 if (Q.isUndefValue(C))
1253 return true;
1254
1255 // Shifting by the bitwidth or more is poison. This covers scalars and
1256 // fixed/scalable vectors with splat constants.
1257 const APInt *AmountC;
1258 if (match(C, m_APInt(AmountC)) && AmountC->uge(AmountC->getBitWidth()))
1259 return true;
1260
1261 // Try harder for fixed-length vectors:
1262 // If all lanes of a vector shift are poison, the whole shift is poison.
1263 if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) {
1264 for (unsigned I = 0,
1265 E = cast<FixedVectorType>(C->getType())->getNumElements();
1266 I != E; ++I)
1267 if (!isPoisonShift(C->getAggregateElement(I), Q))
1268 return false;
1269 return true;
1270 }
1271
1272 return false;
1273}
1274
1275/// Given operands for an Shl, LShr or AShr, see if we can fold the result.
1276/// If not, this returns null.
1277static Value *simplifyShift(Instruction::BinaryOps Opcode, Value *Op0,
1278 Value *Op1, bool IsNSW, const SimplifyQuery &Q,
1279 unsigned MaxRecurse) {
1280 if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
1281 return C;
1282
1283 // poison shift by X -> poison
1284 if (isa<PoisonValue>(Op0))
1285 return Op0;
1286
1287 // 0 shift by X -> 0
1288 if (match(Op0, m_Zero()))
1289 return Constant::getNullValue(Op0->getType());
1290
1291 // X shift by 0 -> X
1292 // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
1293 // would be poison.
1294 Value *X;
1295 if (match(Op1, m_Zero()) ||
1296 (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
1297 return Op0;
1298
1299 // Fold undefined shifts.
1300 if (isPoisonShift(Op1, Q))
1301 return PoisonValue::get(Op0->getType());
1302
1303 // If the operation is with the result of a select instruction, check whether
1304 // operating on either branch of the select always yields the same value.
1305 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1306 if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
1307 return V;
1308
1309 // If the operation is with the result of a phi instruction, check whether
1310 // operating on all incoming values of the phi always yields the same value.
1311 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1312 if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
1313 return V;
1314
1315 // If any bits in the shift amount make that value greater than or equal to
1316 // the number of bits in the type, the shift is undefined.
1317 KnownBits KnownAmt = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1318 if (KnownAmt.getMinValue().uge(KnownAmt.getBitWidth()))
1319 return PoisonValue::get(Op0->getType());
1320
1321 // If all valid bits in the shift amount are known zero, the first operand is
1322 // unchanged.
1323 unsigned NumValidShiftBits = Log2_32_Ceil(KnownAmt.getBitWidth());
1324 if (KnownAmt.countMinTrailingZeros() >= NumValidShiftBits)
1325 return Op0;
1326
1327 // Check for nsw shl leading to a poison value.
1328 if (IsNSW) {
1329 assert(Opcode == Instruction::Shl && "Expected shl for nsw instruction")(static_cast <bool> (Opcode == Instruction::Shl &&
"Expected shl for nsw instruction") ? void (0) : __assert_fail
("Opcode == Instruction::Shl && \"Expected shl for nsw instruction\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 1329, __extension__
__PRETTY_FUNCTION__))
;
1330 KnownBits KnownVal = computeKnownBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1331 KnownBits KnownShl = KnownBits::shl(KnownVal, KnownAmt);
1332
1333 if (KnownVal.Zero.isSignBitSet())
1334 KnownShl.Zero.setSignBit();
1335 if (KnownVal.One.isSignBitSet())
1336 KnownShl.One.setSignBit();
1337
1338 if (KnownShl.hasConflict())
1339 return PoisonValue::get(Op0->getType());
1340 }
1341
1342 return nullptr;
1343}
1344
1345/// Given operands for an Shl, LShr or AShr, see if we can
1346/// fold the result. If not, this returns null.
1347static Value *simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0,
1348 Value *Op1, bool isExact,
1349 const SimplifyQuery &Q, unsigned MaxRecurse) {
1350 if (Value *V =
1351 simplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse))
1352 return V;
1353
1354 // X >> X -> 0
1355 if (Op0 == Op1)
1356 return Constant::getNullValue(Op0->getType());
1357
1358 // undef >> X -> 0
1359 // undef >> X -> undef (if it's exact)
1360 if (Q.isUndefValue(Op0))
1361 return isExact ? Op0 : Constant::getNullValue(Op0->getType());
1362
1363 // The low bit cannot be shifted out of an exact shift if it is set.
1364 if (isExact) {
1365 KnownBits Op0Known =
1366 computeKnownBits(Op0, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT);
1367 if (Op0Known.One[0])
1368 return Op0;
1369 }
1370
1371 return nullptr;
1372}
1373
1374/// Given operands for an Shl, see if we can fold the result.
1375/// If not, this returns null.
1376static Value *simplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
1377 const SimplifyQuery &Q, unsigned MaxRecurse) {
1378 if (Value *V =
1379 simplifyShift(Instruction::Shl, Op0, Op1, isNSW, Q, MaxRecurse))
1380 return V;
1381
1382 // undef << X -> 0
1383 // undef << X -> undef if (if it's NSW/NUW)
1384 if (Q.isUndefValue(Op0))
1385 return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType());
1386
1387 // (X >> A) << A -> X
1388 Value *X;
1389 if (Q.IIQ.UseInstrInfo &&
1390 match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
1391 return X;
1392
1393 // shl nuw i8 C, %x -> C iff C has sign bit set.
1394 if (isNUW && match(Op0, m_Negative()))
1395 return Op0;
1396 // NOTE: could use computeKnownBits() / LazyValueInfo,
1397 // but the cost-benefit analysis suggests it isn't worth it.
1398
1399 return nullptr;
1400}
1401
1402Value *llvm::simplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
1403 const SimplifyQuery &Q) {
1404 return ::simplifyShlInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit);
1405}
1406
1407/// Given operands for an LShr, see if we can fold the result.
1408/// If not, this returns null.
1409static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
1410 const SimplifyQuery &Q, unsigned MaxRecurse) {
1411 if (Value *V = simplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q,
1412 MaxRecurse))
1413 return V;
1414
1415 // (X << A) >> A -> X
1416 Value *X;
1417 if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
1418 return X;
1419
1420 // ((X << A) | Y) >> A -> X if effective width of Y is not larger than A.
1421 // We can return X as we do in the above case since OR alters no bits in X.
1422 // SimplifyDemandedBits in InstCombine can do more general optimization for
1423 // bit manipulation. This pattern aims to provide opportunities for other
1424 // optimizers by supporting a simple but common case in InstSimplify.
1425 Value *Y;
1426 const APInt *ShRAmt, *ShLAmt;
1427 if (match(Op1, m_APInt(ShRAmt)) &&
1428 match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShLAmt)), m_Value(Y))) &&
1429 *ShRAmt == *ShLAmt) {
1430 const KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1431 const unsigned EffWidthY = YKnown.countMaxActiveBits();
1432 if (ShRAmt->uge(EffWidthY))
1433 return X;
1434 }
1435
1436 return nullptr;
1437}
1438
1439Value *llvm::simplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
1440 const SimplifyQuery &Q) {
1441 return ::simplifyLShrInst(Op0, Op1, isExact, Q, RecursionLimit);
1442}
1443
1444/// Given operands for an AShr, see if we can fold the result.
1445/// If not, this returns null.
1446static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
1447 const SimplifyQuery &Q, unsigned MaxRecurse) {
1448 if (Value *V = simplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q,
1449 MaxRecurse))
1450 return V;
1451
1452 // -1 >>a X --> -1
1453 // (-1 << X) a>> X --> -1
1454 // Do not return Op0 because it may contain undef elements if it's a vector.
1455 if (match(Op0, m_AllOnes()) ||
1456 match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1))))
1457 return Constant::getAllOnesValue(Op0->getType());
1458
1459 // (X << A) >> A -> X
1460 Value *X;
1461 if (Q.IIQ.UseInstrInfo && match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1))))
1462 return X;
1463
1464 // Arithmetic shifting an all-sign-bit value is a no-op.
1465 unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1466 if (NumSignBits == Op0->getType()->getScalarSizeInBits())
1467 return Op0;
1468
1469 return nullptr;
1470}
1471
1472Value *llvm::simplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
1473 const SimplifyQuery &Q) {
1474 return ::simplifyAShrInst(Op0, Op1, isExact, Q, RecursionLimit);
1475}
1476
1477/// Commuted variants are assumed to be handled by calling this function again
1478/// with the parameters swapped.
1479static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp,
1480 ICmpInst *UnsignedICmp, bool IsAnd,
1481 const SimplifyQuery &Q) {
1482 Value *X, *Y;
1483
1484 ICmpInst::Predicate EqPred;
1485 if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) ||
1486 !ICmpInst::isEquality(EqPred))
1487 return nullptr;
1488
1489 ICmpInst::Predicate UnsignedPred;
1490
1491 Value *A, *B;
1492 // Y = (A - B);
1493 if (match(Y, m_Sub(m_Value(A), m_Value(B)))) {
1494 if (match(UnsignedICmp,
1495 m_c_ICmp(UnsignedPred, m_Specific(A), m_Specific(B))) &&
1496 ICmpInst::isUnsigned(UnsignedPred)) {
1497 // A >=/<= B || (A - B) != 0 <--> true
1498 if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1499 UnsignedPred == ICmpInst::ICMP_ULE) &&
1500 EqPred == ICmpInst::ICMP_NE && !IsAnd)
1501 return ConstantInt::getTrue(UnsignedICmp->getType());
1502 // A </> B && (A - B) == 0 <--> false
1503 if ((UnsignedPred == ICmpInst::ICMP_ULT ||
1504 UnsignedPred == ICmpInst::ICMP_UGT) &&
1505 EqPred == ICmpInst::ICMP_EQ && IsAnd)
1506 return ConstantInt::getFalse(UnsignedICmp->getType());
1507
1508 // A </> B && (A - B) != 0 <--> A </> B
1509 // A </> B || (A - B) != 0 <--> (A - B) != 0
1510 if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
1511 UnsignedPred == ICmpInst::ICMP_UGT))
1512 return IsAnd ? UnsignedICmp : ZeroICmp;
1513
1514 // A <=/>= B && (A - B) == 0 <--> (A - B) == 0
1515 // A <=/>= B || (A - B) == 0 <--> A <=/>= B
1516 if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
1517 UnsignedPred == ICmpInst::ICMP_UGE))
1518 return IsAnd ? ZeroICmp : UnsignedICmp;
1519 }
1520
1521 // Given Y = (A - B)
1522 // Y >= A && Y != 0 --> Y >= A iff B != 0
1523 // Y < A || Y == 0 --> Y < A iff B != 0
1524 if (match(UnsignedICmp,
1525 m_c_ICmp(UnsignedPred, m_Specific(Y), m_Specific(A)))) {
1526 if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
1527 EqPred == ICmpInst::ICMP_NE &&
1528 isKnownNonZero(B, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT))
1529 return UnsignedICmp;
1530 if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
1531 EqPred == ICmpInst::ICMP_EQ &&
1532 isKnownNonZero(B, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT))
1533 return UnsignedICmp;
1534 }
1535 }
1536
1537 if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) &&
1538 ICmpInst::isUnsigned(UnsignedPred))
1539 ;
1540 else if (match(UnsignedICmp,
1541 m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) &&
1542 ICmpInst::isUnsigned(UnsignedPred))
1543 UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1544 else
1545 return nullptr;
1546
1547 // X > Y && Y == 0 --> Y == 0 iff X != 0
1548 // X > Y || Y == 0 --> X > Y iff X != 0
1549 if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1550 isKnownNonZero(X, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT))
1551 return IsAnd ? ZeroICmp : UnsignedICmp;
1552
1553 // X <= Y && Y != 0 --> X <= Y iff X != 0
1554 // X <= Y || Y != 0 --> Y != 0 iff X != 0
1555 if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1556 isKnownNonZero(X, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT))
1557 return IsAnd ? UnsignedICmp : ZeroICmp;
1558
1559 // The transforms below here are expected to be handled more generally with
1560 // simplifyAndOrOfICmpsWithLimitConst() or in InstCombine's
1561 // foldAndOrOfICmpsWithConstEq(). If we are looking to trim optimizer overlap,
1562 // these are candidates for removal.
1563
1564 // X < Y && Y != 0 --> X < Y
1565 // X < Y || Y != 0 --> Y != 0
1566 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1567 return IsAnd ? UnsignedICmp : ZeroICmp;
1568
1569 // X >= Y && Y == 0 --> Y == 0
1570 // X >= Y || Y == 0 --> X >= Y
1571 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
1572 return IsAnd ? ZeroICmp : UnsignedICmp;
1573
1574 // X < Y && Y == 0 --> false
1575 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1576 IsAnd)
1577 return getFalse(UnsignedICmp->getType());
1578
1579 // X >= Y || Y != 0 --> true
1580 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
1581 !IsAnd)
1582 return getTrue(UnsignedICmp->getType());
1583
1584 return nullptr;
1585}
1586
1587/// Test if a pair of compares with a shared operand and 2 constants has an
1588/// empty set intersection, full set union, or if one compare is a superset of
1589/// the other.
1590static Value *simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1,
1591 bool IsAnd) {
1592 // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)).
1593 if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
1594 return nullptr;
1595
1596 const APInt *C0, *C1;
1597 if (!match(Cmp0->getOperand(1), m_APInt(C0)) ||
1598 !match(Cmp1->getOperand(1), m_APInt(C1)))
1599 return nullptr;
1600
1601 auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0);
1602 auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1);
1603
1604 // For and-of-compares, check if the intersection is empty:
1605 // (icmp X, C0) && (icmp X, C1) --> empty set --> false
1606 if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1607 return getFalse(Cmp0->getType());
1608
1609 // For or-of-compares, check if the union is full:
1610 // (icmp X, C0) || (icmp X, C1) --> full set --> true
1611 if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1612 return getTrue(Cmp0->getType());
1613
1614 // Is one range a superset of the other?
1615 // If this is and-of-compares, take the smaller set:
1616 // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42
1617 // If this is or-of-compares, take the larger set:
1618 // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4
1619 if (Range0.contains(Range1))
1620 return IsAnd ? Cmp1 : Cmp0;
1621 if (Range1.contains(Range0))
1622 return IsAnd ? Cmp0 : Cmp1;
1623
1624 return nullptr;
1625}
1626
1627static Value *simplifyAndOrOfICmpsWithZero(ICmpInst *Cmp0, ICmpInst *Cmp1,
1628 bool IsAnd) {
1629 ICmpInst::Predicate P0 = Cmp0->getPredicate(), P1 = Cmp1->getPredicate();
1630 if (!match(Cmp0->getOperand(1), m_Zero()) ||
1631 !match(Cmp1->getOperand(1), m_Zero()) || P0 != P1)
1632 return nullptr;
1633
1634 if ((IsAnd && P0 != ICmpInst::ICMP_NE) || (!IsAnd && P1 != ICmpInst::ICMP_EQ))
1635 return nullptr;
1636
1637 // We have either "(X == 0 || Y == 0)" or "(X != 0 && Y != 0)".
1638 Value *X = Cmp0->getOperand(0);
1639 Value *Y = Cmp1->getOperand(0);
1640
1641 // If one of the compares is a masked version of a (not) null check, then
1642 // that compare implies the other, so we eliminate the other. Optionally, look
1643 // through a pointer-to-int cast to match a null check of a pointer type.
1644
1645 // (X == 0) || (([ptrtoint] X & ?) == 0) --> ([ptrtoint] X & ?) == 0
1646 // (X == 0) || ((? & [ptrtoint] X) == 0) --> (? & [ptrtoint] X) == 0
1647 // (X != 0) && (([ptrtoint] X & ?) != 0) --> ([ptrtoint] X & ?) != 0
1648 // (X != 0) && ((? & [ptrtoint] X) != 0) --> (? & [ptrtoint] X) != 0
1649 if (match(Y, m_c_And(m_Specific(X), m_Value())) ||
1650 match(Y, m_c_And(m_PtrToInt(m_Specific(X)), m_Value())))
1651 return Cmp1;
1652
1653 // (([ptrtoint] Y & ?) == 0) || (Y == 0) --> ([ptrtoint] Y & ?) == 0
1654 // ((? & [ptrtoint] Y) == 0) || (Y == 0) --> (? & [ptrtoint] Y) == 0
1655 // (([ptrtoint] Y & ?) != 0) && (Y != 0) --> ([ptrtoint] Y & ?) != 0
1656 // ((? & [ptrtoint] Y) != 0) && (Y != 0) --> (? & [ptrtoint] Y) != 0
1657 if (match(X, m_c_And(m_Specific(Y), m_Value())) ||
1658 match(X, m_c_And(m_PtrToInt(m_Specific(Y)), m_Value())))
1659 return Cmp0;
1660
1661 return nullptr;
1662}
1663
1664static Value *simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
1665 const InstrInfoQuery &IIQ) {
1666 // (icmp (add V, C0), C1) & (icmp V, C0)
1667 ICmpInst::Predicate Pred0, Pred1;
1668 const APInt *C0, *C1;
1669 Value *V;
1670 if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1671 return nullptr;
1672
1673 if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1674 return nullptr;
1675
1676 auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0));
1677 if (AddInst->getOperand(1) != Op1->getOperand(1))
1678 return nullptr;
1679
1680 Type *ITy = Op0->getType();
1681 bool isNSW = IIQ.hasNoSignedWrap(AddInst);
1682 bool isNUW = IIQ.hasNoUnsignedWrap(AddInst);
1683
1684 const APInt Delta = *C1 - *C0;
1685 if (C0->isStrictlyPositive()) {
1686 if (Delta == 2) {
1687 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1688 return getFalse(ITy);
1689 if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW)
1690 return getFalse(ITy);
1691 }
1692 if (Delta == 1) {
1693 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1694 return getFalse(ITy);
1695 if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW)
1696 return getFalse(ITy);
1697 }
1698 }
1699 if (C0->getBoolValue() && isNUW) {
1700 if (Delta == 2)
1701 if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1702 return getFalse(ITy);
1703 if (Delta == 1)
1704 if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1705 return getFalse(ITy);
1706 }
1707
1708 return nullptr;
1709}
1710
1711/// Try to eliminate compares with signed or unsigned min/max constants.
1712static Value *simplifyAndOrOfICmpsWithLimitConst(ICmpInst *Cmp0, ICmpInst *Cmp1,
1713 bool IsAnd) {
1714 // Canonicalize an equality compare as Cmp0.
1715 if (Cmp1->isEquality())
1716 std::swap(Cmp0, Cmp1);
1717 if (!Cmp0->isEquality())
1718 return nullptr;
1719
1720 // The non-equality compare must include a common operand (X). Canonicalize
1721 // the common operand as operand 0 (the predicate is swapped if the common
1722 // operand was operand 1).
1723 ICmpInst::Predicate Pred0 = Cmp0->getPredicate();
1724 Value *X = Cmp0->getOperand(0);
1725 ICmpInst::Predicate Pred1;
1726 bool HasNotOp = match(Cmp1, m_c_ICmp(Pred1, m_Not(m_Specific(X)), m_Value()));
1727 if (!HasNotOp && !match(Cmp1, m_c_ICmp(Pred1, m_Specific(X), m_Value())))
1728 return nullptr;
1729 if (ICmpInst::isEquality(Pred1))
1730 return nullptr;
1731
1732 // The equality compare must be against a constant. Flip bits if we matched
1733 // a bitwise not. Convert a null pointer constant to an integer zero value.
1734 APInt MinMaxC;
1735 const APInt *C;
1736 if (match(Cmp0->getOperand(1), m_APInt(C)))
1737 MinMaxC = HasNotOp ? ~*C : *C;
1738 else if (isa<ConstantPointerNull>(Cmp0->getOperand(1)))
1739 MinMaxC = APInt::getZero(8);
1740 else
1741 return nullptr;
1742
1743 // DeMorganize if this is 'or': P0 || P1 --> !P0 && !P1.
1744 if (!IsAnd) {
1745 Pred0 = ICmpInst::getInversePredicate(Pred0);
1746 Pred1 = ICmpInst::getInversePredicate(Pred1);
1747 }
1748
1749 // Normalize to unsigned compare and unsigned min/max value.
1750 // Example for 8-bit: -128 + 128 -> 0; 127 + 128 -> 255
1751 if (ICmpInst::isSigned(Pred1)) {
1752 Pred1 = ICmpInst::getUnsignedPredicate(Pred1);
1753 MinMaxC += APInt::getSignedMinValue(MinMaxC.getBitWidth());
1754 }
1755
1756 // (X != MAX) && (X < Y) --> X < Y
1757 // (X == MAX) || (X >= Y) --> X >= Y
1758 if (MinMaxC.isMaxValue())
1759 if (Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_ULT)
1760 return Cmp1;
1761
1762 // (X != MIN) && (X > Y) --> X > Y
1763 // (X == MIN) || (X <= Y) --> X <= Y
1764 if (MinMaxC.isMinValue())
1765 if (Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_UGT)
1766 return Cmp1;
1767
1768 return nullptr;
1769}
1770
1771/// Try to simplify and/or of icmp with ctpop intrinsic.
1772static Value *simplifyAndOrOfICmpsWithCtpop(ICmpInst *Cmp0, ICmpInst *Cmp1,
1773 bool IsAnd) {
1774 ICmpInst::Predicate Pred0, Pred1;
1775 Value *X;
1776 const APInt *C;
1777 if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
1778 m_APInt(C))) ||
1779 !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())) || C->isZero())
1780 return nullptr;
1781
1782 // (ctpop(X) == C) || (X != 0) --> X != 0 where C > 0
1783 if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE)
1784 return Cmp1;
1785 // (ctpop(X) != C) && (X == 0) --> X == 0 where C > 0
1786 if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ)
1787 return Cmp1;
1788
1789 return nullptr;
1790}
1791
1792static Value *simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1,
1793 const SimplifyQuery &Q) {
1794 if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true, Q))
1795 return X;
1796 if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true, Q))
1797 return X;
1798
1799 if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true))
1800 return X;
1801
1802 if (Value *X = simplifyAndOrOfICmpsWithLimitConst(Op0, Op1, true))
1803 return X;
1804
1805 if (Value *X = simplifyAndOrOfICmpsWithZero(Op0, Op1, true))
1806 return X;
1807
1808 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, true))
1809 return X;
1810 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, true))
1811 return X;
1812
1813 if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1814 return X;
1815 if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1816 return X;
1817
1818 return nullptr;
1819}
1820
1821static Value *simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
1822 const InstrInfoQuery &IIQ) {
1823 // (icmp (add V, C0), C1) | (icmp V, C0)
1824 ICmpInst::Predicate Pred0, Pred1;
1825 const APInt *C0, *C1;
1826 Value *V;
1827 if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
1828 return nullptr;
1829
1830 if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
1831 return nullptr;
1832
1833 auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
1834 if (AddInst->getOperand(1) != Op1->getOperand(1))
1835 return nullptr;
1836
1837 Type *ITy = Op0->getType();
1838 bool isNSW = IIQ.hasNoSignedWrap(AddInst);
1839 bool isNUW = IIQ.hasNoUnsignedWrap(AddInst);
1840
1841 const APInt Delta = *C1 - *C0;
1842 if (C0->isStrictlyPositive()) {
1843 if (Delta == 2) {
1844 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1845 return getTrue(ITy);
1846 if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW)
1847 return getTrue(ITy);
1848 }
1849 if (Delta == 1) {
1850 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1851 return getTrue(ITy);
1852 if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW)
1853 return getTrue(ITy);
1854 }
1855 }
1856 if (C0->getBoolValue() && isNUW) {
1857 if (Delta == 2)
1858 if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1859 return getTrue(ITy);
1860 if (Delta == 1)
1861 if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1862 return getTrue(ITy);
1863 }
1864
1865 return nullptr;
1866}
1867
1868static Value *simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1,
1869 const SimplifyQuery &Q) {
1870 if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false, Q))
1871 return X;
1872 if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false, Q))
1873 return X;
1874
1875 if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false))
1876 return X;
1877
1878 if (Value *X = simplifyAndOrOfICmpsWithLimitConst(Op0, Op1, false))
1879 return X;
1880
1881 if (Value *X = simplifyAndOrOfICmpsWithZero(Op0, Op1, false))
1882 return X;
1883
1884 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, false))
1885 return X;
1886 if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, false))
1887 return X;
1888
1889 if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1, Q.IIQ))
1890 return X;
1891 if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0, Q.IIQ))
1892 return X;
1893
1894 return nullptr;
1895}
1896
1897static Value *simplifyAndOrOfFCmps(const TargetLibraryInfo *TLI, FCmpInst *LHS,
1898 FCmpInst *RHS, bool IsAnd) {
1899 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1900 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1901 if (LHS0->getType() != RHS0->getType())
1902 return nullptr;
1903
1904 FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1905 if ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) ||
1906 (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO && !IsAnd)) {
1907 // (fcmp ord NNAN, X) & (fcmp ord X, Y) --> fcmp ord X, Y
1908 // (fcmp ord NNAN, X) & (fcmp ord Y, X) --> fcmp ord Y, X
1909 // (fcmp ord X, NNAN) & (fcmp ord X, Y) --> fcmp ord X, Y
1910 // (fcmp ord X, NNAN) & (fcmp ord Y, X) --> fcmp ord Y, X
1911 // (fcmp uno NNAN, X) | (fcmp uno X, Y) --> fcmp uno X, Y
1912 // (fcmp uno NNAN, X) | (fcmp uno Y, X) --> fcmp uno Y, X
1913 // (fcmp uno X, NNAN) | (fcmp uno X, Y) --> fcmp uno X, Y
1914 // (fcmp uno X, NNAN) | (fcmp uno Y, X) --> fcmp uno Y, X
1915 if ((isKnownNeverNaN(LHS0, TLI) && (LHS1 == RHS0 || LHS1 == RHS1)) ||
1916 (isKnownNeverNaN(LHS1, TLI) && (LHS0 == RHS0 || LHS0 == RHS1)))
1917 return RHS;
1918
1919 // (fcmp ord X, Y) & (fcmp ord NNAN, X) --> fcmp ord X, Y
1920 // (fcmp ord Y, X) & (fcmp ord NNAN, X) --> fcmp ord Y, X
1921 // (fcmp ord X, Y) & (fcmp ord X, NNAN) --> fcmp ord X, Y
1922 // (fcmp ord Y, X) & (fcmp ord X, NNAN) --> fcmp ord Y, X
1923 // (fcmp uno X, Y) | (fcmp uno NNAN, X) --> fcmp uno X, Y
1924 // (fcmp uno Y, X) | (fcmp uno NNAN, X) --> fcmp uno Y, X
1925 // (fcmp uno X, Y) | (fcmp uno X, NNAN) --> fcmp uno X, Y
1926 // (fcmp uno Y, X) | (fcmp uno X, NNAN) --> fcmp uno Y, X
1927 if ((isKnownNeverNaN(RHS0, TLI) && (RHS1 == LHS0 || RHS1 == LHS1)) ||
1928 (isKnownNeverNaN(RHS1, TLI) && (RHS0 == LHS0 || RHS0 == LHS1)))
1929 return LHS;
1930 }
1931
1932 return nullptr;
1933}
1934
1935static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0,
1936 Value *Op1, bool IsAnd) {
1937 // Look through casts of the 'and' operands to find compares.
1938 auto *Cast0 = dyn_cast<CastInst>(Op0);
1939 auto *Cast1 = dyn_cast<CastInst>(Op1);
1940 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1941 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1942 Op0 = Cast0->getOperand(0);
1943 Op1 = Cast1->getOperand(0);
1944 }
1945
1946 Value *V = nullptr;
1947 auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
1948 auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
1949 if (ICmp0 && ICmp1)
1950 V = IsAnd ? simplifyAndOfICmps(ICmp0, ICmp1, Q)
1951 : simplifyOrOfICmps(ICmp0, ICmp1, Q);
1952
1953 auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
1954 auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
1955 if (FCmp0 && FCmp1)
1956 V = simplifyAndOrOfFCmps(Q.TLI, FCmp0, FCmp1, IsAnd);
1957
1958 if (!V)
1959 return nullptr;
1960 if (!Cast0)
1961 return V;
1962
1963 // If we looked through casts, we can only handle a constant simplification
1964 // because we are not allowed to create a cast instruction here.
1965 if (auto *C = dyn_cast<Constant>(V))
1966 return ConstantExpr::getCast(Cast0->getOpcode(), C, Cast0->getType());
1967
1968 return nullptr;
1969}
1970
1971/// Given a bitwise logic op, check if the operands are add/sub with a common
1972/// source value and inverted constant (identity: C - X -> ~(X + ~C)).
1973static Value *simplifyLogicOfAddSub(Value *Op0, Value *Op1,
1974 Instruction::BinaryOps Opcode) {
1975 assert(Op0->getType() == Op1->getType() && "Mismatched binop types")(static_cast <bool> (Op0->getType() == Op1->getType
() && "Mismatched binop types") ? void (0) : __assert_fail
("Op0->getType() == Op1->getType() && \"Mismatched binop types\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 1975, __extension__
__PRETTY_FUNCTION__))
;
1976 assert(BinaryOperator::isBitwiseLogicOp(Opcode) && "Expected logic op")(static_cast <bool> (BinaryOperator::isBitwiseLogicOp(Opcode
) && "Expected logic op") ? void (0) : __assert_fail (
"BinaryOperator::isBitwiseLogicOp(Opcode) && \"Expected logic op\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 1976, __extension__
__PRETTY_FUNCTION__))
;
1977 Value *X;
1978 Constant *C1, *C2;
1979 if ((match(Op0, m_Add(m_Value(X), m_Constant(C1))) &&
1980 match(Op1, m_Sub(m_Constant(C2), m_Specific(X)))) ||
1981 (match(Op1, m_Add(m_Value(X), m_Constant(C1))) &&
1982 match(Op0, m_Sub(m_Constant(C2), m_Specific(X))))) {
1983 if (ConstantExpr::getNot(C1) == C2) {
1984 // (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0
1985 // (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1
1986 // (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1
1987 Type *Ty = Op0->getType();
1988 return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
1989 : ConstantInt::getAllOnesValue(Ty);
1990 }
1991 }
1992 return nullptr;
1993}
1994
1995/// Given operands for an And, see if we can fold the result.
1996/// If not, this returns null.
1997static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
1998 unsigned MaxRecurse) {
1999 if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q))
2000 return C;
2001
2002 // X & poison -> poison
2003 if (isa<PoisonValue>(Op1))
2004 return Op1;
2005
2006 // X & undef -> 0
2007 if (Q.isUndefValue(Op1))
2008 return Constant::getNullValue(Op0->getType());
2009
2010 // X & X = X
2011 if (Op0 == Op1)
2012 return Op0;
2013
2014 // X & 0 = 0
2015 if (match(Op1, m_Zero()))
2016 return Constant::getNullValue(Op0->getType());
2017
2018 // X & -1 = X
2019 if (match(Op1, m_AllOnes()))
2020 return Op0;
2021
2022 // A & ~A = ~A & A = 0
2023 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
2024 return Constant::getNullValue(Op0->getType());
2025
2026 // (A | ?) & A = A
2027 if (match(Op0, m_c_Or(m_Specific(Op1), m_Value())))
2028 return Op1;
2029
2030 // A & (A | ?) = A
2031 if (match(Op1, m_c_Or(m_Specific(Op0), m_Value())))
2032 return Op0;
2033
2034 // (X | Y) & (X | ~Y) --> X (commuted 8 ways)
2035 Value *X, *Y;
2036 if (match(Op0, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) &&
2037 match(Op1, m_c_Or(m_Deferred(X), m_Deferred(Y))))
2038 return X;
2039 if (match(Op1, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) &&
2040 match(Op0, m_c_Or(m_Deferred(X), m_Deferred(Y))))
2041 return X;
2042
2043 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::And))
2044 return V;
2045
2046 // A mask that only clears known zeros of a shifted value is a no-op.
2047 const APInt *Mask;
2048 const APInt *ShAmt;
2049 if (match(Op1, m_APInt(Mask))) {
2050 // If all bits in the inverted and shifted mask are clear:
2051 // and (shl X, ShAmt), Mask --> shl X, ShAmt
2052 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) &&
2053 (~(*Mask)).lshr(*ShAmt).isZero())
2054 return Op0;
2055
2056 // If all bits in the inverted and shifted mask are clear:
2057 // and (lshr X, ShAmt), Mask --> lshr X, ShAmt
2058 if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
2059 (~(*Mask)).shl(*ShAmt).isZero())
2060 return Op0;
2061 }
2062
2063 // If we have a multiplication overflow check that is being 'and'ed with a
2064 // check that one of the multipliers is not zero, we can omit the 'and', and
2065 // only keep the overflow check.
2066 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, true))
2067 return Op1;
2068 if (isCheckForZeroAndMulWithOverflow(Op1, Op0, true))
2069 return Op0;
2070
2071 // A & (-A) = A if A is a power of two or zero.
2072 if (match(Op0, m_Neg(m_Specific(Op1))) ||
2073 match(Op1, m_Neg(m_Specific(Op0)))) {
2074 if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
2075 Q.DT))
2076 return Op0;
2077 if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
2078 Q.DT))
2079 return Op1;
2080 }
2081
2082 // This is a similar pattern used for checking if a value is a power-of-2:
2083 // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
2084 // A & (A - 1) --> 0 (if A is a power-of-2 or 0)
2085 if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) &&
2086 isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
2087 return Constant::getNullValue(Op1->getType());
2088 if (match(Op1, m_Add(m_Specific(Op0), m_AllOnes())) &&
2089 isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
2090 return Constant::getNullValue(Op0->getType());
2091
2092 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true))
2093 return V;
2094
2095 // Try some generic simplifications for associative operations.
2096 if (Value *V =
2097 simplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, MaxRecurse))
2098 return V;
2099
2100 // And distributes over Or. Try some generic simplifications based on this.
2101 if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2102 Instruction::Or, Q, MaxRecurse))
2103 return V;
2104
2105 // And distributes over Xor. Try some generic simplifications based on this.
2106 if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
2107 Instruction::Xor, Q, MaxRecurse))
2108 return V;
2109
2110 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2111 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2112 // A & (A && B) -> A && B
2113 if (match(Op1, m_Select(m_Specific(Op0), m_Value(), m_Zero())))
2114 return Op1;
2115 else if (match(Op0, m_Select(m_Specific(Op1), m_Value(), m_Zero())))
2116 return Op0;
2117 }
2118 // If the operation is with the result of a select instruction, check
2119 // whether operating on either branch of the select always yields the same
2120 // value.
2121 if (Value *V =
2122 threadBinOpOverSelect(Instruction::And, Op0, Op1, Q, MaxRecurse))
2123 return V;
2124 }
2125
2126 // If the operation is with the result of a phi instruction, check whether
2127 // operating on all incoming values of the phi always yields the same value.
2128 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2129 if (Value *V =
2130 threadBinOpOverPHI(Instruction::And, Op0, Op1, Q, MaxRecurse))
2131 return V;
2132
2133 // Assuming the effective width of Y is not larger than A, i.e. all bits
2134 // from X and Y are disjoint in (X << A) | Y,
2135 // if the mask of this AND op covers all bits of X or Y, while it covers
2136 // no bits from the other, we can bypass this AND op. E.g.,
2137 // ((X << A) | Y) & Mask -> Y,
2138 // if Mask = ((1 << effective_width_of(Y)) - 1)
2139 // ((X << A) | Y) & Mask -> X << A,
2140 // if Mask = ((1 << effective_width_of(X)) - 1) << A
2141 // SimplifyDemandedBits in InstCombine can optimize the general case.
2142 // This pattern aims to help other passes for a common case.
2143 Value *XShifted;
2144 if (match(Op1, m_APInt(Mask)) &&
2145 match(Op0, m_c_Or(m_CombineAnd(m_NUWShl(m_Value(X), m_APInt(ShAmt)),
2146 m_Value(XShifted)),
2147 m_Value(Y)))) {
2148 const unsigned Width = Op0->getType()->getScalarSizeInBits();
2149 const unsigned ShftCnt = ShAmt->getLimitedValue(Width);
2150 const KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2151 const unsigned EffWidthY = YKnown.countMaxActiveBits();
2152 if (EffWidthY <= ShftCnt) {
2153 const KnownBits XKnown = computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2154 const unsigned EffWidthX = XKnown.countMaxActiveBits();
2155 const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY);
2156 const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt;
2157 // If the mask is extracting all bits from X or Y as is, we can skip
2158 // this AND op.
2159 if (EffBitsY.isSubsetOf(*Mask) && !EffBitsX.intersects(*Mask))
2160 return Y;
2161 if (EffBitsX.isSubsetOf(*Mask) && !EffBitsY.intersects(*Mask))
2162 return XShifted;
2163 }
2164 }
2165
2166 // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
2167 // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
2168 BinaryOperator *Or;
2169 if (match(Op0, m_c_Xor(m_Value(X),
2170 m_CombineAnd(m_BinOp(Or),
2171 m_c_Or(m_Deferred(X), m_Value(Y))))) &&
2172 match(Op1, m_c_Xor(m_Specific(Or), m_Specific(Y))))
2173 return Constant::getNullValue(Op0->getType());
2174
2175 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2176 if (Optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL)) {
2177 // If Op0 is true implies Op1 is true, then Op0 is a subset of Op1.
2178 if (*Implied == true)
2179 return Op0;
2180 // If Op0 is true implies Op1 is false, then they are not true together.
2181 if (*Implied == false)
2182 return ConstantInt::getFalse(Op0->getType());
2183 }
2184 if (Optional<bool> Implied = isImpliedCondition(Op1, Op0, Q.DL)) {
2185 // If Op1 is true implies Op0 is true, then Op1 is a subset of Op0.
2186 if (Implied.value())
2187 return Op1;
2188 // If Op1 is true implies Op0 is false, then they are not true together.
2189 if (!Implied.value())
2190 return ConstantInt::getFalse(Op1->getType());
2191 }
2192 }
2193
2194 return nullptr;
2195}
2196
2197Value *llvm::simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2198 return ::simplifyAndInst(Op0, Op1, Q, RecursionLimit);
2199}
2200
2201static Value *simplifyOrLogic(Value *X, Value *Y) {
2202 assert(X->getType() == Y->getType() && "Expected same type for 'or' ops")(static_cast <bool> (X->getType() == Y->getType()
&& "Expected same type for 'or' ops") ? void (0) : __assert_fail
("X->getType() == Y->getType() && \"Expected same type for 'or' ops\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 2202, __extension__
__PRETTY_FUNCTION__))
;
2203 Type *Ty = X->getType();
2204
2205 // X | ~X --> -1
2206 if (match(Y, m_Not(m_Specific(X))))
2207 return ConstantInt::getAllOnesValue(Ty);
2208
2209 // X | ~(X & ?) = -1
2210 if (match(Y, m_Not(m_c_And(m_Specific(X), m_Value()))))
2211 return ConstantInt::getAllOnesValue(Ty);
2212
2213 // X | (X & ?) --> X
2214 if (match(Y, m_c_And(m_Specific(X), m_Value())))
2215 return X;
2216
2217 Value *A, *B;
2218
2219 // (A ^ B) | (A | B) --> A | B
2220 // (A ^ B) | (B | A) --> B | A
2221 if (match(X, m_Xor(m_Value(A), m_Value(B))) &&
2222 match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2223 return Y;
2224
2225 // ~(A ^ B) | (A | B) --> -1
2226 // ~(A ^ B) | (B | A) --> -1
2227 if (match(X, m_Not(m_Xor(m_Value(A), m_Value(B)))) &&
2228 match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2229 return ConstantInt::getAllOnesValue(Ty);
2230
2231 // (A & ~B) | (A ^ B) --> A ^ B
2232 // (~B & A) | (A ^ B) --> A ^ B
2233 // (A & ~B) | (B ^ A) --> B ^ A
2234 // (~B & A) | (B ^ A) --> B ^ A
2235 if (match(X, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2236 match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2237 return Y;
2238
2239 // (~A ^ B) | (A & B) --> ~A ^ B
2240 // (B ^ ~A) | (A & B) --> B ^ ~A
2241 // (~A ^ B) | (B & A) --> ~A ^ B
2242 // (B ^ ~A) | (B & A) --> B ^ ~A
2243 if (match(X, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2244 match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2245 return X;
2246
2247 // (~A | B) | (A ^ B) --> -1
2248 // (~A | B) | (B ^ A) --> -1
2249 // (B | ~A) | (A ^ B) --> -1
2250 // (B | ~A) | (B ^ A) --> -1
2251 if (match(X, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2252 match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2253 return ConstantInt::getAllOnesValue(Ty);
2254
2255 // (~A & B) | ~(A | B) --> ~A
2256 // (~A & B) | ~(B | A) --> ~A
2257 // (B & ~A) | ~(A | B) --> ~A
2258 // (B & ~A) | ~(B | A) --> ~A
2259 Value *NotA;
2260 if (match(X,
2261 m_c_And(m_CombineAnd(m_Value(NotA), m_NotForbidUndef(m_Value(A))),
2262 m_Value(B))) &&
2263 match(Y, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
2264 return NotA;
2265
2266 // ~(A ^ B) | (A & B) --> ~(A ^ B)
2267 // ~(A ^ B) | (B & A) --> ~(A ^ B)
2268 Value *NotAB;
2269 if (match(X, m_CombineAnd(m_NotForbidUndef(m_Xor(m_Value(A), m_Value(B))),
2270 m_Value(NotAB))) &&
2271 match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2272 return NotAB;
2273
2274 // ~(A & B) | (A ^ B) --> ~(A & B)
2275 // ~(A & B) | (B ^ A) --> ~(A & B)
2276 if (match(X, m_CombineAnd(m_NotForbidUndef(m_And(m_Value(A), m_Value(B))),
2277 m_Value(NotAB))) &&
2278 match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
2279 return NotAB;
2280
2281 return nullptr;
2282}
2283
2284/// Given operands for an Or, see if we can fold the result.
2285/// If not, this returns null.
2286static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2287 unsigned MaxRecurse) {
2288 if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q))
2289 return C;
2290
2291 // X | poison -> poison
2292 if (isa<PoisonValue>(Op1))
2293 return Op1;
2294
2295 // X | undef -> -1
2296 // X | -1 = -1
2297 // Do not return Op1 because it may contain undef elements if it's a vector.
2298 if (Q.isUndefValue(Op1) || match(Op1, m_AllOnes()))
2299 return Constant::getAllOnesValue(Op0->getType());
2300
2301 // X | X = X
2302 // X | 0 = X
2303 if (Op0 == Op1 || match(Op1, m_Zero()))
2304 return Op0;
2305
2306 if (Value *R = simplifyOrLogic(Op0, Op1))
2307 return R;
2308 if (Value *R = simplifyOrLogic(Op1, Op0))
2309 return R;
2310
2311 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Or))
2312 return V;
2313
2314 // Rotated -1 is still -1:
2315 // (-1 << X) | (-1 >> (C - X)) --> -1
2316 // (-1 >> X) | (-1 << (C - X)) --> -1
2317 // ...with C <= bitwidth (and commuted variants).
2318 Value *X, *Y;
2319 if ((match(Op0, m_Shl(m_AllOnes(), m_Value(X))) &&
2320 match(Op1, m_LShr(m_AllOnes(), m_Value(Y)))) ||
2321 (match(Op1, m_Shl(m_AllOnes(), m_Value(X))) &&
2322 match(Op0, m_LShr(m_AllOnes(), m_Value(Y))))) {
2323 const APInt *C;
2324 if ((match(X, m_Sub(m_APInt(C), m_Specific(Y))) ||
2325 match(Y, m_Sub(m_APInt(C), m_Specific(X)))) &&
2326 C->ule(X->getType()->getScalarSizeInBits())) {
2327 return ConstantInt::getAllOnesValue(X->getType());
2328 }
2329 }
2330
2331 // A funnel shift (rotate) can be decomposed into simpler shifts. See if we
2332 // are mixing in another shift that is redundant with the funnel shift.
2333
2334 // (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y
2335 // (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y
2336 if (match(Op0,
2337 m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) &&
2338 match(Op1, m_Shl(m_Specific(X), m_Specific(Y))))
2339 return Op0;
2340 if (match(Op1,
2341 m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) &&
2342 match(Op0, m_Shl(m_Specific(X), m_Specific(Y))))
2343 return Op1;
2344
2345 // (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y
2346 // (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y
2347 if (match(Op0,
2348 m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) &&
2349 match(Op1, m_LShr(m_Specific(X), m_Specific(Y))))
2350 return Op0;
2351 if (match(Op1,
2352 m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) &&
2353 match(Op0, m_LShr(m_Specific(X), m_Specific(Y))))
2354 return Op1;
2355
2356 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false))
2357 return V;
2358
2359 // If we have a multiplication overflow check that is being 'and'ed with a
2360 // check that one of the multipliers is not zero, we can omit the 'and', and
2361 // only keep the overflow check.
2362 if (isCheckForZeroAndMulWithOverflow(Op0, Op1, false))
2363 return Op1;
2364 if (isCheckForZeroAndMulWithOverflow(Op1, Op0, false))
2365 return Op0;
2366
2367 // Try some generic simplifications for associative operations.
2368 if (Value *V =
2369 simplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2370 return V;
2371
2372 // Or distributes over And. Try some generic simplifications based on this.
2373 if (Value *V = expandCommutativeBinOp(Instruction::Or, Op0, Op1,
2374 Instruction::And, Q, MaxRecurse))
2375 return V;
2376
2377 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2378 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2379 // A | (A || B) -> A || B
2380 if (match(Op1, m_Select(m_Specific(Op0), m_One(), m_Value())))
2381 return Op1;
2382 else if (match(Op0, m_Select(m_Specific(Op1), m_One(), m_Value())))
2383 return Op0;
2384 }
2385 // If the operation is with the result of a select instruction, check
2386 // whether operating on either branch of the select always yields the same
2387 // value.
2388 if (Value *V =
2389 threadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2390 return V;
2391 }
2392
2393 // (A & C1)|(B & C2)
2394 Value *A, *B;
2395 const APInt *C1, *C2;
2396 if (match(Op0, m_And(m_Value(A), m_APInt(C1))) &&
2397 match(Op1, m_And(m_Value(B), m_APInt(C2)))) {
2398 if (*C1 == ~*C2) {
2399 // (A & C1)|(B & C2)
2400 // If we have: ((V + N) & C1) | (V & C2)
2401 // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
2402 // replace with V+N.
2403 Value *N;
2404 if (C2->isMask() && // C2 == 0+1+
2405 match(A, m_c_Add(m_Specific(B), m_Value(N)))) {
2406 // Add commutes, try both ways.
2407 if (MaskedValueIsZero(N, *C2, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2408 return A;
2409 }
2410 // Or commutes, try both ways.
2411 if (C1->isMask() && match(B, m_c_Add(m_Specific(A), m_Value(N)))) {
2412 // Add commutes, try both ways.
2413 if (MaskedValueIsZero(N, *C1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2414 return B;
2415 }
2416 }
2417 }
2418
2419 // If the operation is with the result of a phi instruction, check whether
2420 // operating on all incoming values of the phi always yields the same value.
2421 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2422 if (Value *V = threadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
2423 return V;
2424
2425 if (Op0->getType()->isIntOrIntVectorTy(1)) {
2426 if (Optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL, false)) {
2427 // If Op0 is false implies Op1 is false, then Op1 is a subset of Op0.
2428 if (*Implied == false)
2429 return Op0;
2430 // If Op0 is false implies Op1 is true, then at least one is always true.
2431 if (*Implied == true)
2432 return ConstantInt::getTrue(Op0->getType());
2433 }
2434 if (Optional<bool> Implied = isImpliedCondition(Op1, Op0, Q.DL, false)) {
2435 // If Op1 is false implies Op0 is false, then Op0 is a subset of Op1.
2436 if (*Implied == false)
2437 return Op1;
2438 // If Op1 is false implies Op0 is true, then at least one is always true.
2439 if (*Implied == true)
2440 return ConstantInt::getTrue(Op1->getType());
2441 }
2442 }
2443
2444 return nullptr;
2445}
2446
2447Value *llvm::simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2448 return ::simplifyOrInst(Op0, Op1, Q, RecursionLimit);
2449}
2450
2451/// Given operands for a Xor, see if we can fold the result.
2452/// If not, this returns null.
2453static Value *simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
2454 unsigned MaxRecurse) {
2455 if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q))
2456 return C;
2457
2458 // X ^ poison -> poison
2459 if (isa<PoisonValue>(Op1))
2460 return Op1;
2461
2462 // A ^ undef -> undef
2463 if (Q.isUndefValue(Op1))
2464 return Op1;
2465
2466 // A ^ 0 = A
2467 if (match(Op1, m_Zero()))
2468 return Op0;
2469
2470 // A ^ A = 0
2471 if (Op0 == Op1)
2472 return Constant::getNullValue(Op0->getType());
2473
2474 // A ^ ~A = ~A ^ A = -1
2475 if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
2476 return Constant::getAllOnesValue(Op0->getType());
2477
2478 auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
2479 Value *A, *B;
2480 // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
2481 if (match(X, m_c_And(m_Not(m_Value(A)), m_Value(B))) &&
2482 match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
2483 return A;
2484
2485 // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
2486 // The 'not' op must contain a complete -1 operand (no undef elements for
2487 // vector) for the transform to be safe.
2488 Value *NotA;
2489 if (match(X,
2490 m_c_Or(m_CombineAnd(m_NotForbidUndef(m_Value(A)), m_Value(NotA)),
2491 m_Value(B))) &&
2492 match(Y, m_c_And(m_Specific(A), m_Specific(B))))
2493 return NotA;
2494
2495 return nullptr;
2496 };
2497 if (Value *R = foldAndOrNot(Op0, Op1))
2498 return R;
2499 if (Value *R = foldAndOrNot(Op1, Op0))
2500 return R;
2501
2502 if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Xor))
2503 return V;
2504
2505 // Try some generic simplifications for associative operations.
2506 if (Value *V =
2507 simplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, MaxRecurse))
2508 return V;
2509
2510 // Threading Xor over selects and phi nodes is pointless, so don't bother.
2511 // Threading over the select in "A ^ select(cond, B, C)" means evaluating
2512 // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
2513 // only if B and C are equal. If B and C are equal then (since we assume
2514 // that operands have already been simplified) "select(cond, B, C)" should
2515 // have been simplified to the common value of B and C already. Analysing
2516 // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
2517 // for threading over phi nodes.
2518
2519 return nullptr;
2520}
2521
2522Value *llvm::simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
2523 return ::simplifyXorInst(Op0, Op1, Q, RecursionLimit);
2524}
2525
2526static Type *getCompareTy(Value *Op) {
2527 return CmpInst::makeCmpResultType(Op->getType());
2528}
2529
2530/// Rummage around inside V looking for something equivalent to the comparison
2531/// "LHS Pred RHS". Return such a value if found, otherwise return null.
2532/// Helper function for analyzing max/min idioms.
2533static Value *extractEquivalentCondition(Value *V, CmpInst::Predicate Pred,
2534 Value *LHS, Value *RHS) {
2535 SelectInst *SI = dyn_cast<SelectInst>(V);
2536 if (!SI)
2537 return nullptr;
2538 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2539 if (!Cmp)
2540 return nullptr;
2541 Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2542 if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2543 return Cmp;
2544 if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
2545 LHS == CmpRHS && RHS == CmpLHS)
2546 return Cmp;
2547 return nullptr;
2548}
2549
2550/// Return true if the underlying object (storage) must be disjoint from
2551/// storage returned by any noalias return call.
2552static bool isAllocDisjoint(const Value *V) {
2553 // For allocas, we consider only static ones (dynamic
2554 // allocas might be transformed into calls to malloc not simultaneously
2555 // live with the compared-to allocation). For globals, we exclude symbols
2556 // that might be resolve lazily to symbols in another dynamically-loaded
2557 // library (and, thus, could be malloc'ed by the implementation).
2558 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2559 return AI->getParent() && AI->getFunction() && AI->isStaticAlloca();
2560 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2561 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2562 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2563 !GV->isThreadLocal();
2564 if (const Argument *A = dyn_cast<Argument>(V))
2565 return A->hasByValAttr();
2566 return false;
2567}
2568
2569/// Return true if V1 and V2 are each the base of some distict storage region
2570/// [V, object_size(V)] which do not overlap. Note that zero sized regions
2571/// *are* possible, and that zero sized regions do not overlap with any other.
2572static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) {
2573 // Global variables always exist, so they always exist during the lifetime
2574 // of each other and all allocas. Global variables themselves usually have
2575 // non-overlapping storage, but since their addresses are constants, the
2576 // case involving two globals does not reach here and is instead handled in
2577 // constant folding.
2578 //
2579 // Two different allocas usually have different addresses...
2580 //
2581 // However, if there's an @llvm.stackrestore dynamically in between two
2582 // allocas, they may have the same address. It's tempting to reduce the
2583 // scope of the problem by only looking at *static* allocas here. That would
2584 // cover the majority of allocas while significantly reducing the likelihood
2585 // of having an @llvm.stackrestore pop up in the middle. However, it's not
2586 // actually impossible for an @llvm.stackrestore to pop up in the middle of
2587 // an entry block. Also, if we have a block that's not attached to a
2588 // function, we can't tell if it's "static" under the current definition.
2589 // Theoretically, this problem could be fixed by creating a new kind of
2590 // instruction kind specifically for static allocas. Such a new instruction
2591 // could be required to be at the top of the entry block, thus preventing it
2592 // from being subject to a @llvm.stackrestore. Instcombine could even
2593 // convert regular allocas into these special allocas. It'd be nifty.
2594 // However, until then, this problem remains open.
2595 //
2596 // So, we'll assume that two non-empty allocas have different addresses
2597 // for now.
2598 auto isByValArg = [](const Value *V) {
2599 const Argument *A = dyn_cast<Argument>(V);
2600 return A && A->hasByValAttr();
2601 };
2602
2603 // Byval args are backed by store which does not overlap with each other,
2604 // allocas, or globals.
2605 if (isByValArg(V1))
2606 return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
2607 if (isByValArg(V2))
2608 return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
2609
2610 return isa<AllocaInst>(V1) &&
2611 (isa<AllocaInst>(V2) || isa<GlobalVariable>(V2));
2612}
2613
2614// A significant optimization not implemented here is assuming that alloca
2615// addresses are not equal to incoming argument values. They don't *alias*,
2616// as we say, but that doesn't mean they aren't equal, so we take a
2617// conservative approach.
2618//
2619// This is inspired in part by C++11 5.10p1:
2620// "Two pointers of the same type compare equal if and only if they are both
2621// null, both point to the same function, or both represent the same
2622// address."
2623//
2624// This is pretty permissive.
2625//
2626// It's also partly due to C11 6.5.9p6:
2627// "Two pointers compare equal if and only if both are null pointers, both are
2628// pointers to the same object (including a pointer to an object and a
2629// subobject at its beginning) or function, both are pointers to one past the
2630// last element of the same array object, or one is a pointer to one past the
2631// end of one array object and the other is a pointer to the start of a
2632// different array object that happens to immediately follow the first array
2633// object in the address space.)
2634//
2635// C11's version is more restrictive, however there's no reason why an argument
2636// couldn't be a one-past-the-end value for a stack object in the caller and be
2637// equal to the beginning of a stack object in the callee.
2638//
2639// If the C and C++ standards are ever made sufficiently restrictive in this
2640// area, it may be possible to update LLVM's semantics accordingly and reinstate
2641// this optimization.
2642static Constant *computePointerICmp(CmpInst::Predicate Pred, Value *LHS,
2643 Value *RHS, const SimplifyQuery &Q) {
2644 const DataLayout &DL = Q.DL;
2645 const TargetLibraryInfo *TLI = Q.TLI;
2646 const DominatorTree *DT = Q.DT;
2647 const Instruction *CxtI = Q.CxtI;
2648 const InstrInfoQuery &IIQ = Q.IIQ;
2649
2650 // First, skip past any trivial no-ops.
2651 LHS = LHS->stripPointerCasts();
2652 RHS = RHS->stripPointerCasts();
2653
2654 // A non-null pointer is not equal to a null pointer.
2655 if (isa<ConstantPointerNull>(RHS) && ICmpInst::isEquality(Pred) &&
2656 llvm::isKnownNonZero(LHS, DL, 0, nullptr, nullptr, nullptr,
2657 IIQ.UseInstrInfo))
2658 return ConstantInt::get(getCompareTy(LHS), !CmpInst::isTrueWhenEqual(Pred));
2659
2660 // We can only fold certain predicates on pointer comparisons.
2661 switch (Pred) {
2662 default:
2663 return nullptr;
2664
2665 // Equality comparisons are easy to fold.
2666 case CmpInst::ICMP_EQ:
2667 case CmpInst::ICMP_NE:
2668 break;
2669
2670 // We can only handle unsigned relational comparisons because 'inbounds' on
2671 // a GEP only protects against unsigned wrapping.
2672 case CmpInst::ICMP_UGT:
2673 case CmpInst::ICMP_UGE:
2674 case CmpInst::ICMP_ULT:
2675 case CmpInst::ICMP_ULE:
2676 // However, we have to switch them to their signed variants to handle
2677 // negative indices from the base pointer.
2678 Pred = ICmpInst::getSignedPredicate(Pred);
2679 break;
2680 }
2681
2682 // Strip off any constant offsets so that we can reason about them.
2683 // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
2684 // here and compare base addresses like AliasAnalysis does, however there are
2685 // numerous hazards. AliasAnalysis and its utilities rely on special rules
2686 // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
2687 // doesn't need to guarantee pointer inequality when it says NoAlias.
2688
2689 // Even if an non-inbounds GEP occurs along the path we can still optimize
2690 // equality comparisons concerning the result.
2691 bool AllowNonInbounds = ICmpInst::isEquality(Pred);
2692 APInt LHSOffset = stripAndComputeConstantOffsets(DL, LHS, AllowNonInbounds);
2693 APInt RHSOffset = stripAndComputeConstantOffsets(DL, RHS, AllowNonInbounds);
2694
2695 // If LHS and RHS are related via constant offsets to the same base
2696 // value, we can replace it with an icmp which just compares the offsets.
2697 if (LHS == RHS)
2698 return ConstantInt::get(getCompareTy(LHS),
2699 ICmpInst::compare(LHSOffset, RHSOffset, Pred));
2700
2701 // Various optimizations for (in)equality comparisons.
2702 if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
2703 // Different non-empty allocations that exist at the same time have
2704 // different addresses (if the program can tell). If the offsets are
2705 // within the bounds of their allocations (and not one-past-the-end!
2706 // so we can't use inbounds!), and their allocations aren't the same,
2707 // the pointers are not equal.
2708 if (haveNonOverlappingStorage(LHS, RHS)) {
2709 uint64_t LHSSize, RHSSize;
2710 ObjectSizeOpts Opts;
2711 Opts.EvalMode = ObjectSizeOpts::Mode::Min;
2712 auto *F = [](Value *V) -> Function * {
2713 if (auto *I = dyn_cast<Instruction>(V))
2714 return I->getFunction();
2715 if (auto *A = dyn_cast<Argument>(V))
2716 return A->getParent();
2717 return nullptr;
2718 }(LHS);
2719 Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true;
2720 if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) &&
2721 getObjectSize(RHS, RHSSize, DL, TLI, Opts) &&
2722 !LHSOffset.isNegative() && !RHSOffset.isNegative() &&
2723 LHSOffset.ult(LHSSize) && RHSOffset.ult(RHSSize)) {
2724 return ConstantInt::get(getCompareTy(LHS),
2725 !CmpInst::isTrueWhenEqual(Pred));
2726 }
2727 }
2728
2729 // If one side of the equality comparison must come from a noalias call
2730 // (meaning a system memory allocation function), and the other side must
2731 // come from a pointer that cannot overlap with dynamically-allocated
2732 // memory within the lifetime of the current function (allocas, byval
2733 // arguments, globals), then determine the comparison result here.
2734 SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
2735 getUnderlyingObjects(LHS, LHSUObjs);
2736 getUnderlyingObjects(RHS, RHSUObjs);
2737
2738 // Is the set of underlying objects all noalias calls?
2739 auto IsNAC = [](ArrayRef<const Value *> Objects) {
2740 return all_of(Objects, isNoAliasCall);
2741 };
2742
2743 // Is the set of underlying objects all things which must be disjoint from
2744 // noalias calls. We assume that indexing from such disjoint storage
2745 // into the heap is undefined, and thus offsets can be safely ignored.
2746 auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
2747 return all_of(Objects, ::isAllocDisjoint);
2748 };
2749
2750 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2751 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2752 return ConstantInt::get(getCompareTy(LHS),
2753 !CmpInst::isTrueWhenEqual(Pred));
2754
2755 // Fold comparisons for non-escaping pointer even if the allocation call
2756 // cannot be elided. We cannot fold malloc comparison to null. Also, the
2757 // dynamic allocation call could be either of the operands. Note that
2758 // the other operand can not be based on the alloc - if it were, then
2759 // the cmp itself would be a capture.
2760 Value *MI = nullptr;
2761 if (isAllocLikeFn(LHS, TLI) &&
2762 llvm::isKnownNonZero(RHS, DL, 0, nullptr, CxtI, DT))
2763 MI = LHS;
2764 else if (isAllocLikeFn(RHS, TLI) &&
2765 llvm::isKnownNonZero(LHS, DL, 0, nullptr, CxtI, DT))
2766 MI = RHS;
2767 // FIXME: We should also fold the compare when the pointer escapes, but the
2768 // compare dominates the pointer escape
2769 if (MI && !PointerMayBeCaptured(MI, true, true))
2770 return ConstantInt::get(getCompareTy(LHS),
2771 CmpInst::isFalseWhenEqual(Pred));
2772 }
2773
2774 // Otherwise, fail.
2775 return nullptr;
2776}
2777
2778/// Fold an icmp when its operands have i1 scalar type.
2779static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS,
2780 Value *RHS, const SimplifyQuery &Q) {
2781 Type *ITy = getCompareTy(LHS); // The return type.
2782 Type *OpTy = LHS->getType(); // The operand type.
2783 if (!OpTy->isIntOrIntVectorTy(1))
2784 return nullptr;
2785
2786 // A boolean compared to true/false can be reduced in 14 out of the 20
2787 // (10 predicates * 2 constants) possible combinations. The other
2788 // 6 cases require a 'not' of the LHS.
2789
2790 auto ExtractNotLHS = [](Value *V) -> Value * {
2791 Value *X;
2792 if (match(V, m_Not(m_Value(X))))
2793 return X;
2794 return nullptr;
2795 };
2796
2797 if (match(RHS, m_Zero())) {
2798 switch (Pred) {
2799 case CmpInst::ICMP_NE: // X != 0 -> X
2800 case CmpInst::ICMP_UGT: // X >u 0 -> X
2801 case CmpInst::ICMP_SLT: // X <s 0 -> X
2802 return LHS;
2803
2804 case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X
2805 case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
2806 case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
2807 if (Value *X = ExtractNotLHS(LHS))
2808 return X;
2809 break;
2810
2811 case CmpInst::ICMP_ULT: // X <u 0 -> false
2812 case CmpInst::ICMP_SGT: // X >s 0 -> false
2813 return getFalse(ITy);
2814
2815 case CmpInst::ICMP_UGE: // X >=u 0 -> true
2816 case CmpInst::ICMP_SLE: // X <=s 0 -> true
2817 return getTrue(ITy);
2818
2819 default:
2820 break;
2821 }
2822 } else if (match(RHS, m_One())) {
2823 switch (Pred) {
2824 case CmpInst::ICMP_EQ: // X == 1 -> X
2825 case CmpInst::ICMP_UGE: // X >=u 1 -> X
2826 case CmpInst::ICMP_SLE: // X <=s -1 -> X
2827 return LHS;
2828
2829 case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X
2830 case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X
2831 case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X
2832 if (Value *X = ExtractNotLHS(LHS))
2833 return X;
2834 break;
2835
2836 case CmpInst::ICMP_UGT: // X >u 1 -> false
2837 case CmpInst::ICMP_SLT: // X <s -1 -> false
2838 return getFalse(ITy);
2839
2840 case CmpInst::ICMP_ULE: // X <=u 1 -> true
2841 case CmpInst::ICMP_SGE: // X >=s -1 -> true
2842 return getTrue(ITy);
2843
2844 default:
2845 break;
2846 }
2847 }
2848
2849 switch (Pred) {
2850 default:
2851 break;
2852 case ICmpInst::ICMP_UGE:
2853 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2854 return getTrue(ITy);
2855 break;
2856 case ICmpInst::ICMP_SGE:
2857 /// For signed comparison, the values for an i1 are 0 and -1
2858 /// respectively. This maps into a truth table of:
2859 /// LHS | RHS | LHS >=s RHS | LHS implies RHS
2860 /// 0 | 0 | 1 (0 >= 0) | 1
2861 /// 0 | 1 | 1 (0 >= -1) | 1
2862 /// 1 | 0 | 0 (-1 >= 0) | 0
2863 /// 1 | 1 | 1 (-1 >= -1) | 1
2864 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2865 return getTrue(ITy);
2866 break;
2867 case ICmpInst::ICMP_ULE:
2868 if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false))
2869 return getTrue(ITy);
2870 break;
2871 case ICmpInst::ICMP_SLE:
2872 /// SLE follows the same logic as SGE with the LHS and RHS swapped.
2873 if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false))
2874 return getTrue(ITy);
2875 break;
2876 }
2877
2878 return nullptr;
2879}
2880
2881/// Try hard to fold icmp with zero RHS because this is a common case.
2882static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS,
2883 Value *RHS, const SimplifyQuery &Q) {
2884 if (!match(RHS, m_Zero()))
2885 return nullptr;
2886
2887 Type *ITy = getCompareTy(LHS); // The return type.
2888 switch (Pred) {
2889 default:
2890 llvm_unreachable("Unknown ICmp predicate!")::llvm::llvm_unreachable_internal("Unknown ICmp predicate!", "llvm/lib/Analysis/InstructionSimplify.cpp"
, 2890)
;
2891 case ICmpInst::ICMP_ULT:
2892 return getFalse(ITy);
2893 case ICmpInst::ICMP_UGE:
2894 return getTrue(ITy);
2895 case ICmpInst::ICMP_EQ:
2896 case ICmpInst::ICMP_ULE:
2897 if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo))
2898 return getFalse(ITy);
2899 break;
2900 case ICmpInst::ICMP_NE:
2901 case ICmpInst::ICMP_UGT:
2902 if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo))
2903 return getTrue(ITy);
2904 break;
2905 case ICmpInst::ICMP_SLT: {
2906 KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2907 if (LHSKnown.isNegative())
2908 return getTrue(ITy);
2909 if (LHSKnown.isNonNegative())
2910 return getFalse(ITy);
2911 break;
2912 }
2913 case ICmpInst::ICMP_SLE: {
2914 KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2915 if (LHSKnown.isNegative())
2916 return getTrue(ITy);
2917 if (LHSKnown.isNonNegative() &&
2918 isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2919 return getFalse(ITy);
2920 break;
2921 }
2922 case ICmpInst::ICMP_SGE: {
2923 KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2924 if (LHSKnown.isNegative())
2925 return getFalse(ITy);
2926 if (LHSKnown.isNonNegative())
2927 return getTrue(ITy);
2928 break;
2929 }
2930 case ICmpInst::ICMP_SGT: {
2931 KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
2932 if (LHSKnown.isNegative())
2933 return getFalse(ITy);
2934 if (LHSKnown.isNonNegative() &&
2935 isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2936 return getTrue(ITy);
2937 break;
2938 }
2939 }
2940
2941 return nullptr;
2942}
2943
2944static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
2945 Value *RHS, const InstrInfoQuery &IIQ) {
2946 Type *ITy = getCompareTy(RHS); // The return type.
2947
2948 Value *X;
2949 // Sign-bit checks can be optimized to true/false after unsigned
2950 // floating-point casts:
2951 // icmp slt (bitcast (uitofp X)), 0 --> false
2952 // icmp sgt (bitcast (uitofp X)), -1 --> true
2953 if (match(LHS, m_BitCast(m_UIToFP(m_Value(X))))) {
2954 if (Pred == ICmpInst::ICMP_SLT && match(RHS, m_Zero()))
2955 return ConstantInt::getFalse(ITy);
2956 if (Pred == ICmpInst::ICMP_SGT && match(RHS, m_AllOnes()))
2957 return ConstantInt::getTrue(ITy);
2958 }
2959
2960 const APInt *C;
2961 if (!match(RHS, m_APIntAllowUndef(C)))
2962 return nullptr;
2963
2964 // Rule out tautological comparisons (eg., ult 0 or uge 0).
2965 ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
2966 if (RHS_CR.isEmptySet())
2967 return ConstantInt::getFalse(ITy);
2968 if (RHS_CR.isFullSet())
2969 return ConstantInt::getTrue(ITy);
2970
2971 ConstantRange LHS_CR =
2972 computeConstantRange(LHS, CmpInst::isSigned(Pred), IIQ.UseInstrInfo);
2973 if (!LHS_CR.isFullSet()) {
2974 if (RHS_CR.contains(LHS_CR))
2975 return ConstantInt::getTrue(ITy);
2976 if (RHS_CR.inverse().contains(LHS_CR))
2977 return ConstantInt::getFalse(ITy);
2978 }
2979
2980 // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
2981 // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
2982 const APInt *MulC;
2983 if (ICmpInst::isEquality(Pred) &&
2984 ((match(LHS, m_NUWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
2985 *MulC != 0 && C->urem(*MulC) != 0) ||
2986 (match(LHS, m_NSWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
2987 *MulC != 0 && C->srem(*MulC) != 0)))
2988 return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
2989
2990 return nullptr;
2991}
2992
2993static Value *simplifyICmpWithBinOpOnLHS(CmpInst::Predicate Pred,
2994 BinaryOperator *LBO, Value *RHS,
2995 const SimplifyQuery &Q,
2996 unsigned MaxRecurse) {
2997 Type *ITy = getCompareTy(RHS); // The return type.
2998
2999 Value *Y = nullptr;
3000 // icmp pred (or X, Y), X
3001 if (match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) {
3002 if (Pred == ICmpInst::ICMP_ULT)
3003 return getFalse(ITy);
3004 if (Pred == ICmpInst::ICMP_UGE)
3005 return getTrue(ITy);
3006
3007 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3008 KnownBits RHSKnown = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
3009 KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
3010 if (RHSKnown.isNonNegative() && YKnown.isNegative())
3011 return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
3012 if (RHSKnown.isNegative() || YKnown.isNonNegative())
3013 return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy);
3014 }
3015 }
3016
3017 // icmp pred (and X, Y), X
3018 if (match(LBO, m_c_And(m_Value(), m_Specific(RHS)))) {
3019 if (Pred == ICmpInst::ICMP_UGT)
3020 return getFalse(ITy);
3021 if (Pred == ICmpInst::ICMP_ULE)
3022 return getTrue(ITy);
3023 }
3024
3025 // icmp pred (urem X, Y), Y
3026 if (match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
3027 switch (Pred) {
3028 default:
3029 break;
3030 case ICmpInst::ICMP_SGT:
3031 case ICmpInst::ICMP_SGE: {
3032 KnownBits Known = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
3033 if (!Known.isNonNegative())
3034 break;
3035 [[fallthrough]];
3036 }
3037 case ICmpInst::ICMP_EQ:
3038 case ICmpInst::ICMP_UGT:
3039 case ICmpInst::ICMP_UGE:
3040 return getFalse(ITy);
3041 case ICmpInst::ICMP_SLT:
3042 case ICmpInst::ICMP_SLE: {
3043 KnownBits Known = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
3044 if (!Known.isNonNegative())
3045 break;
3046 [[fallthrough]];
3047 }
3048 case ICmpInst::ICMP_NE:
3049 case ICmpInst::ICMP_ULT:
3050 case ICmpInst::ICMP_ULE:
3051 return getTrue(ITy);
3052 }
3053 }
3054
3055 // icmp pred (urem X, Y), X
3056 if (match(LBO, m_URem(m_Specific(RHS), m_Value()))) {
3057 if (Pred == ICmpInst::ICMP_ULE)
3058 return getTrue(ITy);
3059 if (Pred == ICmpInst::ICMP_UGT)
3060 return getFalse(ITy);
3061 }
3062
3063 // x >>u y <=u x --> true.
3064 // x >>u y >u x --> false.
3065 // x udiv y <=u x --> true.
3066 // x udiv y >u x --> false.
3067 if (match(LBO, m_LShr(m_Specific(RHS), m_Value())) ||
3068 match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) {
3069 // icmp pred (X op Y), X
3070 if (Pred == ICmpInst::ICMP_UGT)
3071 return getFalse(ITy);
3072 if (Pred == ICmpInst::ICMP_ULE)
3073 return getTrue(ITy);
3074 }
3075
3076 // If x is nonzero:
3077 // x >>u C <u x --> true for C != 0.
3078 // x >>u C != x --> true for C != 0.
3079 // x >>u C >=u x --> false for C != 0.
3080 // x >>u C == x --> false for C != 0.
3081 // x udiv C <u x --> true for C != 1.
3082 // x udiv C != x --> true for C != 1.
3083 // x udiv C >=u x --> false for C != 1.
3084 // x udiv C == x --> false for C != 1.
3085 // TODO: allow non-constant shift amount/divisor
3086 const APInt *C;
3087 if ((match(LBO, m_LShr(m_Specific(RHS), m_APInt(C))) && *C != 0) ||
3088 (match(LBO, m_UDiv(m_Specific(RHS), m_APInt(C))) && *C != 1)) {
3089 if (isKnownNonZero(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) {
3090 switch (Pred) {
3091 default:
3092 break;
3093 case ICmpInst::ICMP_EQ:
3094 case ICmpInst::ICMP_UGE:
3095 return getFalse(ITy);
3096 case ICmpInst::ICMP_NE:
3097 case ICmpInst::ICMP_ULT:
3098 return getTrue(ITy);
3099 case ICmpInst::ICMP_UGT:
3100 case ICmpInst::ICMP_ULE:
3101 // UGT/ULE are handled by the more general case just above
3102 llvm_unreachable("Unexpected UGT/ULE, should have been handled")::llvm::llvm_unreachable_internal("Unexpected UGT/ULE, should have been handled"
, "llvm/lib/Analysis/InstructionSimplify.cpp", 3102)
;
3103 }
3104 }
3105 }
3106
3107 // (x*C1)/C2 <= x for C1 <= C2.
3108 // This holds even if the multiplication overflows: Assume that x != 0 and
3109 // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and
3110 // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
3111 //
3112 // Additionally, either the multiplication and division might be represented
3113 // as shifts:
3114 // (x*C1)>>C2 <= x for C1 < 2**C2.
3115 // (x<<C1)/C2 <= x for 2**C1 < C2.
3116 const APInt *C1, *C2;
3117 if ((match(LBO, m_UDiv(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3118 C1->ule(*C2)) ||
3119 (match(LBO, m_LShr(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3120 C1->ule(APInt(C2->getBitWidth(), 1) << *C2)) ||
3121 (match(LBO, m_UDiv(m_Shl(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
3122 (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2))) {
3123 if (Pred == ICmpInst::ICMP_UGT)
3124 return getFalse(ITy);
3125 if (Pred == ICmpInst::ICMP_ULE)
3126 return getTrue(ITy);
3127 }
3128
3129 return nullptr;
3130}
3131
3132// If only one of the icmp's operands has NSW flags, try to prove that:
3133//
3134// icmp slt (x + C1), (x +nsw C2)
3135//
3136// is equivalent to:
3137//
3138// icmp slt C1, C2
3139//
3140// which is true if x + C2 has the NSW flags set and:
3141// *) C1 < C2 && C1 >= 0, or
3142// *) C2 < C1 && C1 <= 0.
3143//
3144static bool trySimplifyICmpWithAdds(CmpInst::Predicate Pred, Value *LHS,
3145 Value *RHS) {
3146 // TODO: only support icmp slt for now.
3147 if (Pred != CmpInst::ICMP_SLT)
3148 return false;
3149
3150 // Canonicalize nsw add as RHS.
3151 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3152 std::swap(LHS, RHS);
3153 if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
3154 return false;
3155
3156 Value *X;
3157 const APInt *C1, *C2;
3158 if (!match(LHS, m_c_Add(m_Value(X), m_APInt(C1))) ||
3159 !match(RHS, m_c_Add(m_Specific(X), m_APInt(C2))))
3160 return false;
3161
3162 return (C1->slt(*C2) && C1->isNonNegative()) ||
3163 (C2->slt(*C1) && C1->isNonPositive());
3164}
3165
3166/// TODO: A large part of this logic is duplicated in InstCombine's
3167/// foldICmpBinOp(). We should be able to share that and avoid the code
3168/// duplication.
3169static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS,
3170 Value *RHS, const SimplifyQuery &Q,
3171 unsigned MaxRecurse) {
3172 BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
3173 BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
3174 if (MaxRecurse && (LBO || RBO)) {
3175 // Analyze the case when either LHS or RHS is an add instruction.
3176 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
3177 // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
3178 bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
3179 if (LBO && LBO->getOpcode() == Instruction::Add) {
3180 A = LBO->getOperand(0);
3181 B = LBO->getOperand(1);
3182 NoLHSWrapProblem =
3183 ICmpInst::isEquality(Pred) ||
3184 (CmpInst::isUnsigned(Pred) &&
3185 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))) ||
3186 (CmpInst::isSigned(Pred) &&
3187 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)));
3188 }
3189 if (RBO && RBO->getOpcode() == Instruction::Add) {
3190 C = RBO->getOperand(0);
3191 D = RBO->getOperand(1);
3192 NoRHSWrapProblem =
3193 ICmpInst::isEquality(Pred) ||
3194 (CmpInst::isUnsigned(Pred) &&
3195 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(RBO))) ||
3196 (CmpInst::isSigned(Pred) &&
3197 Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RBO)));
3198 }
3199
3200 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
3201 if ((A == RHS || B == RHS) && NoLHSWrapProblem)
3202 if (Value *V = simplifyICmpInst(Pred, A == RHS ? B : A,
3203 Constant::getNullValue(RHS->getType()), Q,
3204 MaxRecurse - 1))
3205 return V;
3206
3207 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
3208 if ((C == LHS || D == LHS) && NoRHSWrapProblem)
3209 if (Value *V =
3210 simplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()),
3211 C == LHS ? D : C, Q, MaxRecurse - 1))
3212 return V;
3213
3214 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
3215 bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3216 trySimplifyICmpWithAdds(Pred, LHS, RHS);
3217 if (A && C && (A == C || A == D || B == C || B == D) && CanSimplify) {
3218 // Determine Y and Z in the form icmp (X+Y), (X+Z).
3219 Value *Y, *Z;
3220 if (A == C) {
3221 // C + B == C + D -> B == D
3222 Y = B;
3223 Z = D;
3224 } else if (A == D) {
3225 // D + B == C + D -> B == C
3226 Y = B;
3227 Z = C;
3228 } else if (B == C) {
3229 // A + C == C + D -> A == D
3230 Y = A;
3231 Z = D;
3232 } else {
3233 assert(B == D)(static_cast <bool> (B == D) ? void (0) : __assert_fail
("B == D", "llvm/lib/Analysis/InstructionSimplify.cpp", 3233
, __extension__ __PRETTY_FUNCTION__))
;
3234 // A + D == C + D -> A == C
3235 Y = A;
3236 Z = C;
3237 }
3238 if (Value *V = simplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
3239 return V;
3240 }
3241 }
3242
3243 if (LBO)
3244 if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse))
3245 return V;
3246
3247 if (RBO)
3248 if (Value *V = simplifyICmpWithBinOpOnLHS(
3249 ICmpInst::getSwappedPredicate(Pred), RBO, LHS, Q, MaxRecurse))
3250 return V;
3251
3252 // 0 - (zext X) pred C
3253 if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) {
3254 const APInt *C;
3255 if (match(RHS, m_APInt(C))) {
3256 if (C->isStrictlyPositive()) {
3257 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3258 return ConstantInt::getTrue(getCompareTy(RHS));
3259 if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3260 return ConstantInt::getFalse(getCompareTy(RHS));
3261 }
3262 if (C->isNonNegative()) {
3263 if (Pred == ICmpInst::ICMP_SLE)
3264 return ConstantInt::getTrue(getCompareTy(RHS));
3265 if (Pred == ICmpInst::ICMP_SGT)
3266 return ConstantInt::getFalse(getCompareTy(RHS));
3267 }
3268 }
3269 }
3270
3271 // If C2 is a power-of-2 and C is not:
3272 // (C2 << X) == C --> false
3273 // (C2 << X) != C --> true
3274 const APInt *C;
3275 if (match(LHS, m_Shl(m_Power2(), m_Value())) &&
3276 match(RHS, m_APIntAllowUndef(C)) && !C->isPowerOf2()) {
3277 // C2 << X can equal zero in some circumstances.
3278 // This simplification might be unsafe if C is zero.
3279 //
3280 // We know it is safe if:
3281 // - The shift is nsw. We can't shift out the one bit.
3282 // - The shift is nuw. We can't shift out the one bit.
3283 // - C2 is one.
3284 // - C isn't zero.
3285 if (Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3286 Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
3287 match(LHS, m_Shl(m_One(), m_Value())) || !C->isZero()) {
3288 if (Pred == ICmpInst::ICMP_EQ)
3289 return ConstantInt::getFalse(getCompareTy(RHS));
3290 if (Pred == ICmpInst::ICMP_NE)
3291 return ConstantInt::getTrue(getCompareTy(RHS));
3292 }
3293 }
3294
3295 // TODO: This is overly constrained. LHS can be any power-of-2.
3296 // (1 << X) >u 0x8000 --> false
3297 // (1 << X) <=u 0x8000 --> true
3298 if (match(LHS, m_Shl(m_One(), m_Value())) && match(RHS, m_SignMask())) {
3299 if (Pred == ICmpInst::ICMP_UGT)
3300 return ConstantInt::getFalse(getCompareTy(RHS));
3301 if (Pred == ICmpInst::ICMP_ULE)
3302 return ConstantInt::getTrue(getCompareTy(RHS));
3303 }
3304
3305 if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() &&
3306 LBO->getOperand(1) == RBO->getOperand(1)) {
3307 switch (LBO->getOpcode()) {
3308 default:
3309 break;
3310 case Instruction::UDiv:
3311 case Instruction::LShr:
3312 if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(LBO) ||
3313 !Q.IIQ.isExact(RBO))
3314 break;
3315 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3316 RBO->getOperand(0), Q, MaxRecurse - 1))
3317 return V;
3318 break;
3319 case Instruction::SDiv:
3320 if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO) ||
3321 !Q.IIQ.isExact(RBO))
3322 break;
3323 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3324 RBO->getOperand(0), Q, MaxRecurse - 1))
3325 return V;
3326 break;
3327 case Instruction::AShr:
3328 if (!Q.IIQ.isExact(LBO) || !Q.IIQ.isExact(RBO))
3329 break;
3330 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3331 RBO->getOperand(0), Q, MaxRecurse - 1))
3332 return V;
3333 break;
3334 case Instruction::Shl: {
3335 bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
3336 bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
3337 if (!NUW && !NSW)
3338 break;
3339 if (!NSW && ICmpInst::isSigned(Pred))
3340 break;
3341 if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0),
3342 RBO->getOperand(0), Q, MaxRecurse - 1))
3343 return V;
3344 break;
3345 }
3346 }
3347 }
3348 return nullptr;
3349}
3350
3351/// simplify integer comparisons where at least one operand of the compare
3352/// matches an integer min/max idiom.
3353static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS,
3354 Value *RHS, const SimplifyQuery &Q,
3355 unsigned MaxRecurse) {
3356 Type *ITy = getCompareTy(LHS); // The return type.
3357 Value *A, *B;
3358 CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
3359 CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
3360
3361 // Signed variants on "max(a,b)>=a -> true".
3362 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3363 if (A != RHS)
3364 std::swap(A, B); // smax(A, B) pred A.
3365 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3366 // We analyze this as smax(A, B) pred A.
3367 P = Pred;
3368 } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
3369 (A == LHS || B == LHS)) {
3370 if (A != LHS)
3371 std::swap(A, B); // A pred smax(A, B).
3372 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
3373 // We analyze this as smax(A, B) swapped-pred A.
3374 P = CmpInst::getSwappedPredicate(Pred);
3375 } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
3376 (A == RHS || B == RHS)) {
3377 if (A != RHS)
3378 std::swap(A, B); // smin(A, B) pred A.
3379 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3380 // We analyze this as smax(-A, -B) swapped-pred -A.
3381 // Note that we do not need to actually form -A or -B thanks to EqP.
3382 P = CmpInst::getSwappedPredicate(Pred);
3383 } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
3384 (A == LHS || B == LHS)) {
3385 if (A != LHS)
3386 std::swap(A, B); // A pred smin(A, B).
3387 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
3388 // We analyze this as smax(-A, -B) pred -A.
3389 // Note that we do not need to actually form -A or -B thanks to EqP.
3390 P = Pred;
3391 }
3392 if (P != CmpInst::BAD_ICMP_PREDICATE) {
3393 // Cases correspond to "max(A, B) p A".
3394 switch (P) {
3395 default:
3396 break;
3397 case CmpInst::ICMP_EQ:
3398 case CmpInst::ICMP_SLE:
3399 // Equivalent to "A EqP B". This may be the same as the condition tested
3400 // in the max/min; if so, we can just return that.
3401 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3402 return V;
3403 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3404 return V;
3405 // Otherwise, see if "A EqP B" simplifies.
3406 if (MaxRecurse)
3407 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3408 return V;
3409 break;
3410 case CmpInst::ICMP_NE:
3411 case CmpInst::ICMP_SGT: {
3412 CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
3413 // Equivalent to "A InvEqP B". This may be the same as the condition
3414 // tested in the max/min; if so, we can just return that.
3415 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3416 return V;
3417 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3418 return V;
3419 // Otherwise, see if "A InvEqP B" simplifies.
3420 if (MaxRecurse)
3421 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3422 return V;
3423 break;
3424 }
3425 case CmpInst::ICMP_SGE:
3426 // Always true.
3427 return getTrue(ITy);
3428 case CmpInst::ICMP_SLT:
3429 // Always false.
3430 return getFalse(ITy);
3431 }
3432 }
3433
3434 // Unsigned variants on "max(a,b)>=a -> true".
3435 P = CmpInst::BAD_ICMP_PREDICATE;
3436 if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
3437 if (A != RHS)
3438 std::swap(A, B); // umax(A, B) pred A.
3439 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3440 // We analyze this as umax(A, B) pred A.
3441 P = Pred;
3442 } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
3443 (A == LHS || B == LHS)) {
3444 if (A != LHS)
3445 std::swap(A, B); // A pred umax(A, B).
3446 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
3447 // We analyze this as umax(A, B) swapped-pred A.
3448 P = CmpInst::getSwappedPredicate(Pred);
3449 } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
3450 (A == RHS || B == RHS)) {
3451 if (A != RHS)
3452 std::swap(A, B); // umin(A, B) pred A.
3453 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3454 // We analyze this as umax(-A, -B) swapped-pred -A.
3455 // Note that we do not need to actually form -A or -B thanks to EqP.
3456 P = CmpInst::getSwappedPredicate(Pred);
3457 } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
3458 (A == LHS || B == LHS)) {
3459 if (A != LHS)
3460 std::swap(A, B); // A pred umin(A, B).
3461 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
3462 // We analyze this as umax(-A, -B) pred -A.
3463 // Note that we do not need to actually form -A or -B thanks to EqP.
3464 P = Pred;
3465 }
3466 if (P != CmpInst::BAD_ICMP_PREDICATE) {
3467 // Cases correspond to "max(A, B) p A".
3468 switch (P) {
3469 default:
3470 break;
3471 case CmpInst::ICMP_EQ:
3472 case CmpInst::ICMP_ULE:
3473 // Equivalent to "A EqP B". This may be the same as the condition tested
3474 // in the max/min; if so, we can just return that.
3475 if (Value *V = extractEquivalentCondition(LHS, EqP, A, B))
3476 return V;
3477 if (Value *V = extractEquivalentCondition(RHS, EqP, A, B))
3478 return V;
3479 // Otherwise, see if "A EqP B" simplifies.
3480 if (MaxRecurse)
3481 if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
3482 return V;
3483 break;
3484 case CmpInst::ICMP_NE:
3485 case CmpInst::ICMP_UGT: {
3486 CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
3487 // Equivalent to "A InvEqP B". This may be the same as the condition
3488 // tested in the max/min; if so, we can just return that.
3489 if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B))
3490 return V;
3491 if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B))
3492 return V;
3493 // Otherwise, see if "A InvEqP B" simplifies.
3494 if (MaxRecurse)
3495 if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
3496 return V;
3497 break;
3498 }
3499 case CmpInst::ICMP_UGE:
3500 return getTrue(ITy);
3501 case CmpInst::ICMP_ULT:
3502 return getFalse(ITy);
3503 }
3504 }
3505
3506 // Comparing 1 each of min/max with a common operand?
3507 // Canonicalize min operand to RHS.
3508 if (match(LHS, m_UMin(m_Value(), m_Value())) ||
3509 match(LHS, m_SMin(m_Value(), m_Value()))) {
3510 std::swap(LHS, RHS);
3511 Pred = ICmpInst::getSwappedPredicate(Pred);
3512 }
3513
3514 Value *C, *D;
3515 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
3516 match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
3517 (A == C || A == D || B == C || B == D)) {
3518 // smax(A, B) >=s smin(A, D) --> true
3519 if (Pred == CmpInst::ICMP_SGE)
3520 return getTrue(ITy);
3521 // smax(A, B) <s smin(A, D) --> false
3522 if (Pred == CmpInst::ICMP_SLT)
3523 return getFalse(ITy);
3524 } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
3525 match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
3526 (A == C || A == D || B == C || B == D)) {
3527 // umax(A, B) >=u umin(A, D) --> true
3528 if (Pred == CmpInst::ICMP_UGE)
3529 return getTrue(ITy);
3530 // umax(A, B) <u umin(A, D) --> false
3531 if (Pred == CmpInst::ICMP_ULT)
3532 return getFalse(ITy);
3533 }
3534
3535 return nullptr;
3536}
3537
3538static Value *simplifyICmpWithDominatingAssume(CmpInst::Predicate Predicate,
3539 Value *LHS, Value *RHS,
3540 const SimplifyQuery &Q) {
3541 // Gracefully handle instructions that have not been inserted yet.
3542 if (!Q.AC || !Q.CxtI || !Q.CxtI->getParent())
3543 return nullptr;
3544
3545 for (Value *AssumeBaseOp : {LHS, RHS}) {
3546 for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) {
3547 if (!AssumeVH)
3548 continue;
3549
3550 CallInst *Assume = cast<CallInst>(AssumeVH);
3551 if (Optional<bool> Imp = isImpliedCondition(Assume->getArgOperand(0),
3552 Predicate, LHS, RHS, Q.DL))
3553 if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT))
3554 return ConstantInt::get(getCompareTy(LHS), *Imp);
3555 }
3556 }
3557
3558 return nullptr;
3559}
3560
3561/// Given operands for an ICmpInst, see if we can fold the result.
3562/// If not, this returns null.
3563static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3564 const SimplifyQuery &Q, unsigned MaxRecurse) {
3565 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
3566 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!")(static_cast <bool> (CmpInst::isIntPredicate(Pred) &&
"Not an integer compare!") ? void (0) : __assert_fail ("CmpInst::isIntPredicate(Pred) && \"Not an integer compare!\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 3566, __extension__
__PRETTY_FUNCTION__))
;
3567
3568 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3569 if (Constant *CRHS = dyn_cast<Constant>(RHS))
3570 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
3571
3572 // If we have a constant, make sure it is on the RHS.
3573 std::swap(LHS, RHS);
3574 Pred = CmpInst::getSwappedPredicate(Pred);
3575 }
3576 assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X")(static_cast <bool> (!isa<UndefValue>(LHS) &&
"Unexpected icmp undef,%X") ? void (0) : __assert_fail ("!isa<UndefValue>(LHS) && \"Unexpected icmp undef,%X\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 3576, __extension__
__PRETTY_FUNCTION__))
;
3577
3578 Type *ITy = getCompareTy(LHS); // The return type.
3579
3580 // icmp poison, X -> poison
3581 if (isa<PoisonValue>(RHS))
3582 return PoisonValue::get(ITy);
3583
3584 // For EQ and NE, we can always pick a value for the undef to make the
3585 // predicate pass or fail, so we can return undef.
3586 // Matches behavior in llvm::ConstantFoldCompareInstruction.
3587 if (Q.isUndefValue(RHS) && ICmpInst::isEquality(Pred))
3588 return UndefValue::get(ITy);
3589
3590 // icmp X, X -> true/false
3591 // icmp X, undef -> true/false because undef could be X.
3592 if (LHS == RHS || Q.isUndefValue(RHS))
3593 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
3594
3595 if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
3596 return V;
3597
3598 // TODO: Sink/common this with other potentially expensive calls that use
3599 // ValueTracking? See comment below for isKnownNonEqual().
3600 if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
3601 return V;
3602
3603 if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
3604 return V;
3605
3606 // If both operands have range metadata, use the metadata
3607 // to simplify the comparison.
3608 if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) {
3609 auto RHS_Instr = cast<Instruction>(RHS);
3610 auto LHS_Instr = cast<Instruction>(LHS);
3611
3612 if (Q.IIQ.getMetadata(RHS_Instr, LLVMContext::MD_range) &&
3613 Q.IIQ.getMetadata(LHS_Instr, LLVMContext::MD_range)) {
3614 auto RHS_CR = getConstantRangeFromMetadata(
3615 *RHS_Instr->getMetadata(LLVMContext::MD_range));
3616 auto LHS_CR = getConstantRangeFromMetadata(
3617 *LHS_Instr->getMetadata(LLVMContext::MD_range));
3618
3619 if (LHS_CR.icmp(Pred, RHS_CR))
3620 return ConstantInt::getTrue(RHS->getContext());
3621
3622 if (LHS_CR.icmp(CmpInst::getInversePredicate(Pred), RHS_CR))
3623 return ConstantInt::getFalse(RHS->getContext());
3624 }
3625 }
3626
3627 // Compare of cast, for example (zext X) != 0 -> X != 0
3628 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
3629 Instruction *LI = cast<CastInst>(LHS);
3630 Value *SrcOp = LI->getOperand(0);
3631 Type *SrcTy = SrcOp->getType();
3632 Type *DstTy = LI->getType();
3633
3634 // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
3635 // if the integer type is the same size as the pointer type.
3636 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3637 Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
3638 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
3639 // Transfer the cast to the constant.
3640 if (Value *V = simplifyICmpInst(Pred, SrcOp,
3641 ConstantExpr::getIntToPtr(RHSC, SrcTy),
3642 Q, MaxRecurse - 1))
3643 return V;
3644 } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3645 if (RI->getOperand(0)->getType() == SrcTy)
3646 // Compare without the cast.
3647 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3648 MaxRecurse - 1))
3649 return V;
3650 }
3651 }
3652
3653 if (isa<ZExtInst>(LHS)) {
3654 // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
3655 // same type.
3656 if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3657 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3658 // Compare X and Y. Note that signed predicates become unsigned.
3659 if (Value *V =
3660 simplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), SrcOp,
3661 RI->getOperand(0), Q, MaxRecurse - 1))
3662 return V;
3663 }
3664 // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
3665 else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3666 if (SrcOp == RI->getOperand(0)) {
3667 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3668 return ConstantInt::getTrue(ITy);
3669 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3670 return ConstantInt::getFalse(ITy);
3671 }
3672 }
3673 // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
3674 // too. If not, then try to deduce the result of the comparison.
3675 else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3676 // Compute the constant that would happen if we truncated to SrcTy then
3677 // reextended to DstTy.
3678 Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
3679 Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
3680
3681 // If the re-extended constant didn't change then this is effectively
3682 // also a case of comparing two zero-extended values.
3683 if (RExt == CI && MaxRecurse)
3684 if (Value *V = simplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
3685 SrcOp, Trunc, Q, MaxRecurse - 1))
3686 return V;
3687
3688 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
3689 // there. Use this to work out the result of the comparison.
3690 if (RExt != CI) {
3691 switch (Pred) {
3692 default:
3693 llvm_unreachable("Unknown ICmp predicate!")::llvm::llvm_unreachable_internal("Unknown ICmp predicate!", "llvm/lib/Analysis/InstructionSimplify.cpp"
, 3693)
;
3694 // LHS <u RHS.
3695 case ICmpInst::ICMP_EQ:
3696 case ICmpInst::ICMP_UGT:
3697 case ICmpInst::ICMP_UGE:
3698 return ConstantInt::getFalse(CI->getContext());
3699
3700 case ICmpInst::ICMP_NE:
3701 case ICmpInst::ICMP_ULT:
3702 case ICmpInst::ICMP_ULE:
3703 return ConstantInt::getTrue(CI->getContext());
3704
3705 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
3706 // is non-negative then LHS <s RHS.
3707 case ICmpInst::ICMP_SGT:
3708 case ICmpInst::ICMP_SGE:
3709 return CI->getValue().isNegative()
3710 ? ConstantInt::getTrue(CI->getContext())
3711 : ConstantInt::getFalse(CI->getContext());
3712
3713 case ICmpInst::ICMP_SLT:
3714 case ICmpInst::ICMP_SLE:
3715 return CI->getValue().isNegative()
3716 ? ConstantInt::getFalse(CI->getContext())
3717 : ConstantInt::getTrue(CI->getContext());
3718 }
3719 }
3720 }
3721 }
3722
3723 if (isa<SExtInst>(LHS)) {
3724 // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
3725 // same type.
3726 if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3727 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3728 // Compare X and Y. Note that the predicate does not change.
3729 if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q,
3730 MaxRecurse - 1))
3731 return V;
3732 }
3733 // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
3734 else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3735 if (SrcOp == RI->getOperand(0)) {
3736 if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3737 return ConstantInt::getTrue(ITy);
3738 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3739 return ConstantInt::getFalse(ITy);
3740 }
3741 }
3742 // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
3743 // too. If not, then try to deduce the result of the comparison.
3744 else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3745 // Compute the constant that would happen if we truncated to SrcTy then
3746 // reextended to DstTy.
3747 Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
3748 Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
3749
3750 // If the re-extended constant didn't change then this is effectively
3751 // also a case of comparing two sign-extended values.
3752 if (RExt == CI && MaxRecurse)
3753 if (Value *V =
3754 simplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1))
3755 return V;
3756
3757 // Otherwise the upper bits of LHS are all equal, while RHS has varying
3758 // bits there. Use this to work out the result of the comparison.
3759 if (RExt != CI) {
3760 switch (Pred) {
3761 default:
3762 llvm_unreachable("Unknown ICmp predicate!")::llvm::llvm_unreachable_internal("Unknown ICmp predicate!", "llvm/lib/Analysis/InstructionSimplify.cpp"
, 3762)
;
3763 case ICmpInst::ICMP_EQ:
3764 return ConstantInt::getFalse(CI->getContext());
3765 case ICmpInst::ICMP_NE:
3766 return ConstantInt::getTrue(CI->getContext());
3767
3768 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
3769 // LHS >s RHS.
3770 case ICmpInst::ICMP_SGT:
3771 case ICmpInst::ICMP_SGE:
3772 return CI->getValue().isNegative()
3773 ? ConstantInt::getTrue(CI->getContext())
3774 : ConstantInt::getFalse(CI->getContext());
3775 case ICmpInst::ICMP_SLT:
3776 case ICmpInst::ICMP_SLE:
3777 return CI->getValue().isNegative()
3778 ? ConstantInt::getFalse(CI->getContext())
3779 : ConstantInt::getTrue(CI->getContext());
3780
3781 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
3782 // LHS >u RHS.
3783 case ICmpInst::ICMP_UGT:
3784 case ICmpInst::ICMP_UGE:
3785 // Comparison is true iff the LHS <s 0.
3786 if (MaxRecurse)
3787 if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
3788 Constant::getNullValue(SrcTy), Q,
3789 MaxRecurse - 1))
3790 return V;
3791 break;
3792 case ICmpInst::ICMP_ULT:
3793 case ICmpInst::ICMP_ULE:
3794 // Comparison is true iff the LHS >=s 0.
3795 if (MaxRecurse)
3796 if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
3797 Constant::getNullValue(SrcTy), Q,
3798 MaxRecurse - 1))
3799 return V;
3800 break;
3801 }
3802 }
3803 }
3804 }
3805 }
3806
3807 // icmp eq|ne X, Y -> false|true if X != Y
3808 // This is potentially expensive, and we have already computedKnownBits for
3809 // compares with 0 above here, so only try this for a non-zero compare.
3810 if (ICmpInst::isEquality(Pred) && !match(RHS, m_Zero()) &&
3811 isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo)) {
3812 return Pred == ICmpInst::ICMP_NE ? getTrue(ITy) : getFalse(ITy);
3813 }
3814
3815 if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
3816 return V;
3817
3818 if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
3819 return V;
3820
3821 if (Value *V = simplifyICmpWithDominatingAssume(Pred, LHS, RHS, Q))
3822 return V;
3823
3824 // Simplify comparisons of related pointers using a powerful, recursive
3825 // GEP-walk when we have target data available..
3826 if (LHS->getType()->isPointerTy())
3827 if (auto *C = computePointerICmp(Pred, LHS, RHS, Q))
3828 return C;
3829 if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
3830 if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
3831 if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
3832 Q.DL.getTypeSizeInBits(CLHS->getType()) &&
3833 Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) ==
3834 Q.DL.getTypeSizeInBits(CRHS->getType()))
3835 if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
3836 CRHS->getPointerOperand(), Q))
3837 return C;
3838
3839 // If the comparison is with the result of a select instruction, check whether
3840 // comparing with either branch of the select always yields the same value.
3841 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
3842 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
3843 return V;
3844
3845 // If the comparison is with the result of a phi instruction, check whether
3846 // doing the compare with each incoming phi value yields a common result.
3847 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
3848 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
3849 return V;
3850
3851 return nullptr;
3852}
3853
3854Value *llvm::simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3855 const SimplifyQuery &Q) {
3856 return ::simplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
3857}
3858
3859/// Given operands for an FCmpInst, see if we can fold the result.
3860/// If not, this returns null.
3861static Value *simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
3862 FastMathFlags FMF, const SimplifyQuery &Q,
3863 unsigned MaxRecurse) {
3864 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
3865 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!")(static_cast <bool> (CmpInst::isFPPredicate(Pred) &&
"Not an FP compare!") ? void (0) : __assert_fail ("CmpInst::isFPPredicate(Pred) && \"Not an FP compare!\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 3865, __extension__
__PRETTY_FUNCTION__))
;
3866
3867 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
3868 if (Constant *CRHS = dyn_cast<Constant>(RHS))
3869 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI,
3870 Q.CxtI);
3871
3872 // If we have a constant, make sure it is on the RHS.
3873 std::swap(LHS, RHS);
3874 Pred = CmpInst::getSwappedPredicate(Pred);
3875 }
3876
3877 // Fold trivial predicates.
3878 Type *RetTy = getCompareTy(LHS);
3879 if (Pred == FCmpInst::FCMP_FALSE)
3880 return getFalse(RetTy);
3881 if (Pred == FCmpInst::FCMP_TRUE)
3882 return getTrue(RetTy);
3883
3884 // Fold (un)ordered comparison if we can determine there are no NaNs.
3885 if (Pred == FCmpInst::FCMP_UNO || Pred == FCmpInst::FCMP_ORD)
3886 if (FMF.noNaNs() ||
3887 (isKnownNeverNaN(LHS, Q.TLI) && isKnownNeverNaN(RHS, Q.TLI)))
3888 return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD);
3889
3890 // NaN is unordered; NaN is not ordered.
3891 assert((FCmpInst::isOrdered(Pred) || FCmpInst::isUnordered(Pred)) &&(static_cast <bool> ((FCmpInst::isOrdered(Pred) || FCmpInst
::isUnordered(Pred)) && "Comparison must be either ordered or unordered"
) ? void (0) : __assert_fail ("(FCmpInst::isOrdered(Pred) || FCmpInst::isUnordered(Pred)) && \"Comparison must be either ordered or unordered\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 3892, __extension__
__PRETTY_FUNCTION__))
3892 "Comparison must be either ordered or unordered")(static_cast <bool> ((FCmpInst::isOrdered(Pred) || FCmpInst
::isUnordered(Pred)) && "Comparison must be either ordered or unordered"
) ? void (0) : __assert_fail ("(FCmpInst::isOrdered(Pred) || FCmpInst::isUnordered(Pred)) && \"Comparison must be either ordered or unordered\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 3892, __extension__
__PRETTY_FUNCTION__))
;
3893 if (match(RHS, m_NaN()))
3894 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
3895
3896 // fcmp pred x, poison and fcmp pred poison, x
3897 // fold to poison
3898 if (isa<PoisonValue>(LHS) || isa<PoisonValue>(RHS))
3899 return PoisonValue::get(RetTy);
3900
3901 // fcmp pred x, undef and fcmp pred undef, x
3902 // fold to true if unordered, false if ordered
3903 if (Q.isUndefValue(LHS) || Q.isUndefValue(RHS)) {
3904 // Choosing NaN for the undef will always make unordered comparison succeed
3905 // and ordered comparison fail.
3906 return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
3907 }
3908
3909 // fcmp x,x -> true/false. Not all compares are foldable.
3910 if (LHS == RHS) {
3911 if (CmpInst::isTrueWhenEqual(Pred))
3912 return getTrue(RetTy);
3913 if (CmpInst::isFalseWhenEqual(Pred))
3914 return getFalse(RetTy);
3915 }
3916
3917 // Handle fcmp with constant RHS.
3918 // TODO: Use match with a specific FP value, so these work with vectors with
3919 // undef lanes.
3920 const APFloat *C;
3921 if (match(RHS, m_APFloat(C))) {
3922 // Check whether the constant is an infinity.
3923 if (C->isInfinity()) {
3924 if (C->isNegative()) {
3925 switch (Pred) {
3926 case FCmpInst::FCMP_OLT:
3927 // No value is ordered and less than negative infinity.
3928 return getFalse(RetTy);
3929 case FCmpInst::FCMP_UGE:
3930 // All values are unordered with or at least negative infinity.
3931 return getTrue(RetTy);
3932 default:
3933 break;
3934 }
3935 } else {
3936 switch (Pred) {
3937 case FCmpInst::FCMP_OGT:
3938 // No value is ordered and greater than infinity.
3939 return getFalse(RetTy);
3940 case FCmpInst::FCMP_ULE:
3941 // All values are unordered with and at most infinity.
3942 return getTrue(RetTy);
3943 default:
3944 break;
3945 }
3946 }
3947
3948 // LHS == Inf
3949 if (Pred == FCmpInst::FCMP_OEQ && isKnownNeverInfinity(LHS, Q.TLI))
3950 return getFalse(RetTy);
3951 // LHS != Inf
3952 if (Pred == FCmpInst::FCMP_UNE && isKnownNeverInfinity(LHS, Q.TLI))
3953 return getTrue(RetTy);
3954 // LHS == Inf || LHS == NaN
3955 if (Pred == FCmpInst::FCMP_UEQ && isKnownNeverInfinity(LHS, Q.TLI) &&
3956 isKnownNeverNaN(LHS, Q.TLI))
3957 return getFalse(RetTy);
3958 // LHS != Inf && LHS != NaN
3959 if (Pred == FCmpInst::FCMP_ONE && isKnownNeverInfinity(LHS, Q.TLI) &&
3960 isKnownNeverNaN(LHS, Q.TLI))
3961 return getTrue(RetTy);
3962 }
3963 if (C->isNegative() && !C->isNegZero()) {
3964 assert(!C->isNaN() && "Unexpected NaN constant!")(static_cast <bool> (!C->isNaN() && "Unexpected NaN constant!"
) ? void (0) : __assert_fail ("!C->isNaN() && \"Unexpected NaN constant!\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 3964, __extension__
__PRETTY_FUNCTION__))
;
3965 // TODO: We can catch more cases by using a range check rather than
3966 // relying on CannotBeOrderedLessThanZero.
3967 switch (Pred) {
3968 case FCmpInst::FCMP_UGE:
3969 case FCmpInst::FCMP_UGT:
3970 case FCmpInst::FCMP_UNE:
3971 // (X >= 0) implies (X > C) when (C < 0)
3972 if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
3973 return getTrue(RetTy);
3974 break;
3975 case FCmpInst::FCMP_OEQ:
3976 case FCmpInst::FCMP_OLE:
3977 case FCmpInst::FCMP_OLT:
3978 // (X >= 0) implies !(X < C) when (C < 0)
3979 if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
3980 return getFalse(RetTy);
3981 break;
3982 default:
3983 break;
3984 }
3985 }
3986
3987 // Check comparison of [minnum/maxnum with constant] with other constant.
3988 const APFloat *C2;
3989 if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) &&
3990 *C2 < *C) ||
3991 (match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2))) &&
3992 *C2 > *C)) {
3993 bool IsMaxNum =
3994 cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
3995 // The ordered relationship and minnum/maxnum guarantee that we do not
3996 // have NaN constants, so ordered/unordered preds are handled the same.
3997 switch (Pred) {
3998 case FCmpInst::FCMP_OEQ:
3999 case FCmpInst::FCMP_UEQ:
4000 // minnum(X, LesserC) == C --> false
4001 // maxnum(X, GreaterC) == C --> false
4002 return getFalse(RetTy);
4003 case FCmpInst::FCMP_ONE:
4004 case FCmpInst::FCMP_UNE:
4005 // minnum(X, LesserC) != C --> true
4006 // maxnum(X, GreaterC) != C --> true
4007 return getTrue(RetTy);
4008 case FCmpInst::FCMP_OGE:
4009 case FCmpInst::FCMP_UGE:
4010 case FCmpInst::FCMP_OGT:
4011 case FCmpInst::FCMP_UGT:
4012 // minnum(X, LesserC) >= C --> false
4013 // minnum(X, LesserC) > C --> false
4014 // maxnum(X, GreaterC) >= C --> true
4015 // maxnum(X, GreaterC) > C --> true
4016 return ConstantInt::get(RetTy, IsMaxNum);
4017 case FCmpInst::FCMP_OLE:
4018 case FCmpInst::FCMP_ULE:
4019 case FCmpInst::FCMP_OLT:
4020 case FCmpInst::FCMP_ULT:
4021 // minnum(X, LesserC) <= C --> true
4022 // minnum(X, LesserC) < C --> true
4023 // maxnum(X, GreaterC) <= C --> false
4024 // maxnum(X, GreaterC) < C --> false
4025 return ConstantInt::get(RetTy, !IsMaxNum);
4026 default:
4027 // TRUE/FALSE/ORD/UNO should be handled before this.
4028 llvm_unreachable("Unexpected fcmp predicate")::llvm::llvm_unreachable_internal("Unexpected fcmp predicate"
, "llvm/lib/Analysis/InstructionSimplify.cpp", 4028)
;
4029 }
4030 }
4031 }
4032
4033 if (match(RHS, m_AnyZeroFP())) {
4034 switch (Pred) {
4035 case FCmpInst::FCMP_OGE:
4036 case FCmpInst::FCMP_ULT:
4037 // Positive or zero X >= 0.0 --> true
4038 // Positive or zero X < 0.0 --> false
4039 if ((FMF.noNaNs() || isKnownNeverNaN(LHS, Q.TLI)) &&
4040 CannotBeOrderedLessThanZero(LHS, Q.TLI))
4041 return Pred == FCmpInst::FCMP_OGE ? getTrue(RetTy) : getFalse(RetTy);
4042 break;
4043 case FCmpInst::FCMP_UGE:
4044 case FCmpInst::FCMP_OLT:
4045 // Positive or zero or nan X >= 0.0 --> true
4046 // Positive or zero or nan X < 0.0 --> false
4047 if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
4048 return Pred == FCmpInst::FCMP_UGE ? getTrue(RetTy) : getFalse(RetTy);
4049 break;
4050 default:
4051 break;
4052 }
4053 }
4054
4055 // If the comparison is with the result of a select instruction, check whether
4056 // comparing with either branch of the select always yields the same value.
4057 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
4058 if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
4059 return V;
4060
4061 // If the comparison is with the result of a phi instruction, check whether
4062 // doing the compare with each incoming phi value yields a common result.
4063 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
4064 if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
4065 return V;
4066
4067 return nullptr;
4068}
4069
4070Value *llvm::simplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
4071 FastMathFlags FMF, const SimplifyQuery &Q) {
4072 return ::simplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit);
4073}
4074
4075static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4076 const SimplifyQuery &Q,
4077 bool AllowRefinement,
4078 unsigned MaxRecurse) {
4079 assert(!Op->getType()->isVectorTy() && "This is not safe for vectors")(static_cast <bool> (!Op->getType()->isVectorTy()
&& "This is not safe for vectors") ? void (0) : __assert_fail
("!Op->getType()->isVectorTy() && \"This is not safe for vectors\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 4079, __extension__
__PRETTY_FUNCTION__))
;
4080
4081 // Trivial replacement.
4082 if (V == Op)
4083 return RepOp;
4084
4085 // We cannot replace a constant, and shouldn't even try.
4086 if (isa<Constant>(Op))
4087 return nullptr;
4088
4089 auto *I = dyn_cast<Instruction>(V);
4090 if (!I || !is_contained(I->operands(), Op))
4091 return nullptr;
4092
4093 // Replace Op with RepOp in instruction operands.
4094 SmallVector<Value *, 8> NewOps(I->getNumOperands());
4095 transform(I->operands(), NewOps.begin(),
4096 [&](Value *V) { return V == Op ? RepOp : V; });
4097
4098 if (!AllowRefinement) {
4099 // General InstSimplify functions may refine the result, e.g. by returning
4100 // a constant for a potentially poison value. To avoid this, implement only
4101 // a few non-refining but profitable transforms here.
4102
4103 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
4104 unsigned Opcode = BO->getOpcode();
4105 // id op x -> x, x op id -> x
4106 if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, I->getType()))
4107 return NewOps[1];
4108 if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, I->getType(),
4109 /* RHS */ true))
4110 return NewOps[0];
4111
4112 // x & x -> x, x | x -> x
4113 if ((Opcode == Instruction::And || Opcode == Instruction::Or) &&
4114 NewOps[0] == NewOps[1])
4115 return NewOps[0];
4116 }
4117
4118 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
4119 // getelementptr x, 0 -> x
4120 if (NewOps.size() == 2 && match(NewOps[1], m_Zero()) &&
4121 !GEP->isInBounds())
4122 return NewOps[0];
4123 }
4124 } else if (MaxRecurse) {
4125 // The simplification queries below may return the original value. Consider:
4126 // %div = udiv i32 %arg, %arg2
4127 // %mul = mul nsw i32 %div, %arg2
4128 // %cmp = icmp eq i32 %mul, %arg
4129 // %sel = select i1 %cmp, i32 %div, i32 undef
4130 // Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which
4131 // simplifies back to %arg. This can only happen because %mul does not
4132 // dominate %div. To ensure a consistent return value contract, we make sure
4133 // that this case returns nullptr as well.
4134 auto PreventSelfSimplify = [V](Value *Simplified) {
4135 return Simplified != V ? Simplified : nullptr;
4136 };
4137
4138 if (auto *B = dyn_cast<BinaryOperator>(I))
4139 return PreventSelfSimplify(simplifyBinOp(B->getOpcode(), NewOps[0],
4140 NewOps[1], Q, MaxRecurse - 1));
4141
4142 if (CmpInst *C = dyn_cast<CmpInst>(I))
4143 return PreventSelfSimplify(simplifyCmpInst(C->getPredicate(), NewOps[0],
4144 NewOps[1], Q, MaxRecurse - 1));
4145
4146 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
4147 return PreventSelfSimplify(simplifyGEPInst(
4148 GEP->getSourceElementType(), NewOps[0], makeArrayRef(NewOps).slice(1),
4149 GEP->isInBounds(), Q, MaxRecurse - 1));
4150
4151 if (isa<SelectInst>(I))
4152 return PreventSelfSimplify(simplifySelectInst(
4153 NewOps[0], NewOps[1], NewOps[2], Q, MaxRecurse - 1));
4154 // TODO: We could hand off more cases to instsimplify here.
4155 }
4156
4157 // If all operands are constant after substituting Op for RepOp then we can
4158 // constant fold the instruction.
4159 SmallVector<Constant *, 8> ConstOps;
4160 for (Value *NewOp : NewOps) {
4161 if (Constant *ConstOp = dyn_cast<Constant>(NewOp))
4162 ConstOps.push_back(ConstOp);
4163 else
4164 return nullptr;
4165 }
4166
4167 // Consider:
4168 // %cmp = icmp eq i32 %x, 2147483647
4169 // %add = add nsw i32 %x, 1
4170 // %sel = select i1 %cmp, i32 -2147483648, i32 %add
4171 //
4172 // We can't replace %sel with %add unless we strip away the flags (which
4173 // will be done in InstCombine).
4174 // TODO: This may be unsound, because it only catches some forms of
4175 // refinement.
4176 if (!AllowRefinement && canCreatePoison(cast<Operator>(I)))
4177 return nullptr;
4178
4179 return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI);
4180}
4181
4182Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
4183 const SimplifyQuery &Q,
4184 bool AllowRefinement) {
4185 return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement,
4186 RecursionLimit);
4187}
4188
4189/// Try to simplify a select instruction when its condition operand is an
4190/// integer comparison where one operand of the compare is a constant.
4191static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
4192 const APInt *Y, bool TrueWhenUnset) {
4193 const APInt *C;
4194
4195 // (X & Y) == 0 ? X & ~Y : X --> X
4196 // (X & Y) != 0 ? X & ~Y : X --> X & ~Y
4197 if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) &&
4198 *Y == ~*C)
4199 return TrueWhenUnset ? FalseVal : TrueVal;
4200
4201 // (X & Y) == 0 ? X : X & ~Y --> X & ~Y
4202 // (X & Y) != 0 ? X : X & ~Y --> X
4203 if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) &&
4204 *Y == ~*C)
4205 return TrueWhenUnset ? FalseVal : TrueVal;
4206
4207 if (Y->isPowerOf2()) {
4208 // (X & Y) == 0 ? X | Y : X --> X | Y
4209 // (X & Y) != 0 ? X | Y : X --> X
4210 if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) &&
4211 *Y == *C)
4212 return TrueWhenUnset ? TrueVal : FalseVal;
4213
4214 // (X & Y) == 0 ? X : X | Y --> X
4215 // (X & Y) != 0 ? X : X | Y --> X | Y
4216 if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) &&
4217 *Y == *C)
4218 return TrueWhenUnset ? TrueVal : FalseVal;
4219 }
4220
4221 return nullptr;
4222}
4223
4224/// An alternative way to test if a bit is set or not uses sgt/slt instead of
4225/// eq/ne.
4226static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
4227 ICmpInst::Predicate Pred,
4228 Value *TrueVal, Value *FalseVal) {
4229 Value *X;
4230 APInt Mask;
4231 if (!decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask))
4232 return nullptr;
4233
4234 return simplifySelectBitTest(TrueVal, FalseVal, X, &Mask,
4235 Pred == ICmpInst::ICMP_EQ);
4236}
4237
4238/// Try to simplify a select instruction when its condition operand is an
4239/// integer comparison.
4240static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
4241 Value *FalseVal,
4242 const SimplifyQuery &Q,
4243 unsigned MaxRecurse) {
4244 ICmpInst::Predicate Pred;
4245 Value *CmpLHS, *CmpRHS;
4246 if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4247 return nullptr;
4248
4249 // Canonicalize ne to eq predicate.
4250 if (Pred == ICmpInst::ICMP_NE) {
4251 Pred = ICmpInst::ICMP_EQ;
4252 std::swap(TrueVal, FalseVal);
4253 }
4254
4255 // Check for integer min/max with a limit constant:
4256 // X > MIN_INT ? X : MIN_INT --> X
4257 // X < MAX_INT ? X : MAX_INT --> X
4258 if (TrueVal->getType()->isIntOrIntVectorTy()) {
4259 Value *X, *Y;
4260 SelectPatternFlavor SPF =
4261 matchDecomposedSelectPattern(cast<ICmpInst>(CondVal), TrueVal, FalseVal,
4262 X, Y)
4263 .Flavor;
4264 if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) {
4265 APInt LimitC = getMinMaxLimit(getInverseMinMaxFlavor(SPF),
4266 X->getType()->getScalarSizeInBits());
4267 if (match(Y, m_SpecificInt(LimitC)))
4268 return X;
4269 }
4270 }
4271
4272 if (Pred == ICmpInst::ICMP_EQ && match(CmpRHS, m_Zero())) {
4273 Value *X;
4274 const APInt *Y;
4275 if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y))))
4276 if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
4277 /*TrueWhenUnset=*/true))
4278 return V;
4279
4280 // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
4281 Value *ShAmt;
4282 auto isFsh = m_CombineOr(m_FShl(m_Value(X), m_Value(), m_Value(ShAmt)),
4283 m_FShr(m_Value(), m_Value(X), m_Value(ShAmt)));
4284 // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
4285 // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
4286 if (match(TrueVal, isFsh) && FalseVal == X && CmpLHS == ShAmt)
4287 return X;
4288
4289 // Test for a zero-shift-guard-op around rotates. These are used to
4290 // avoid UB from oversized shifts in raw IR rotate patterns, but the
4291 // intrinsics do not have that problem.
4292 // We do not allow this transform for the general funnel shift case because
4293 // that would not preserve the poison safety of the original code.
4294 auto isRotate =
4295 m_CombineOr(m_FShl(m_Value(X), m_Deferred(X), m_Value(ShAmt)),
4296 m_FShr(m_Value(X), m_Deferred(X), m_Value(ShAmt)));
4297 // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
4298 // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
4299 if (match(FalseVal, isRotate) && TrueVal == X && CmpLHS == ShAmt &&
4300 Pred == ICmpInst::ICMP_EQ)
4301 return FalseVal;
4302
4303 // X == 0 ? abs(X) : -abs(X) --> -abs(X)
4304 // X == 0 ? -abs(X) : abs(X) --> abs(X)
4305 if (match(TrueVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))) &&
4306 match(FalseVal, m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))))
4307 return FalseVal;
4308 if (match(TrueVal,
4309 m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))) &&
4310 match(FalseVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))))
4311 return FalseVal;
4312 }
4313
4314 // Check for other compares that behave like bit test.
4315 if (Value *V =
4316 simplifySelectWithFakeICmpEq(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal))
4317 return V;
4318
4319 // If we have a scalar equality comparison, then we know the value in one of
4320 // the arms of the select. See if substituting this value into the arm and
4321 // simplifying the result yields the same value as the other arm.
4322 // Note that the equivalence/replacement opportunity does not hold for vectors
4323 // because each element of a vector select is chosen independently.
4324 if (Pred == ICmpInst::ICMP_EQ && !CondVal->getType()->isVectorTy()) {
4325 if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q,
4326 /* AllowRefinement */ false,
4327 MaxRecurse) == TrueVal ||
4328 simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q,
4329 /* AllowRefinement */ false,
4330 MaxRecurse) == TrueVal)
4331 return FalseVal;
4332 if (simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q,
4333 /* AllowRefinement */ true,
4334 MaxRecurse) == FalseVal ||
4335 simplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q,
4336 /* AllowRefinement */ true,
4337 MaxRecurse) == FalseVal)
4338 return FalseVal;
4339 }
4340
4341 return nullptr;
4342}
4343
4344/// Try to simplify a select instruction when its condition operand is a
4345/// floating-point comparison.
4346static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F,
4347 const SimplifyQuery &Q) {
4348 FCmpInst::Predicate Pred;
4349 if (!match(Cond, m_FCmp(Pred, m_Specific(T), m_Specific(F))) &&
4350 !match(Cond, m_FCmp(Pred, m_Specific(F), m_Specific(T))))
4351 return nullptr;
4352
4353 // This transform is safe if we do not have (do not care about) -0.0 or if
4354 // at least one operand is known to not be -0.0. Otherwise, the select can
4355 // change the sign of a zero operand.
4356 bool HasNoSignedZeros =
4357 Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros();
4358 const APFloat *C;
4359 if (HasNoSignedZeros || (match(T, m_APFloat(C)) && C->isNonZero()) ||
4360 (match(F, m_APFloat(C)) && C->isNonZero())) {
4361 // (T == F) ? T : F --> F
4362 // (F == T) ? T : F --> F
4363 if (Pred == FCmpInst::FCMP_OEQ)
4364 return F;
4365
4366 // (T != F) ? T : F --> T
4367 // (F != T) ? T : F --> T
4368 if (Pred == FCmpInst::FCMP_UNE)
4369 return T;
4370 }
4371
4372 return nullptr;
4373}
4374
4375/// Given operands for a SelectInst, see if we can fold the result.
4376/// If not, this returns null.
4377static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4378 const SimplifyQuery &Q, unsigned MaxRecurse) {
4379 if (auto *CondC = dyn_cast<Constant>(Cond)) {
4380 if (auto *TrueC = dyn_cast<Constant>(TrueVal))
4381 if (auto *FalseC = dyn_cast<Constant>(FalseVal))
4382 return ConstantFoldSelectInstruction(CondC, TrueC, FalseC);
4383
4384 // select poison, X, Y -> poison
4385 if (isa<PoisonValue>(CondC))
4386 return PoisonValue::get(TrueVal->getType());
4387
4388 // select undef, X, Y -> X or Y
4389 if (Q.isUndefValue(CondC))
4390 return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
4391
4392 // select true, X, Y --> X
4393 // select false, X, Y --> Y
4394 // For vectors, allow undef/poison elements in the condition to match the
4395 // defined elements, so we can eliminate the select.
4396 if (match(CondC, m_One()))
4397 return TrueVal;
4398 if (match(CondC, m_Zero()))
4399 return FalseVal;
4400 }
4401
4402 assert(Cond->getType()->isIntOrIntVectorTy(1) &&(static_cast <bool> (Cond->getType()->isIntOrIntVectorTy
(1) && "Select must have bool or bool vector condition"
) ? void (0) : __assert_fail ("Cond->getType()->isIntOrIntVectorTy(1) && \"Select must have bool or bool vector condition\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 4403, __extension__
__PRETTY_FUNCTION__))
4403 "Select must have bool or bool vector condition")(static_cast <bool> (Cond->getType()->isIntOrIntVectorTy
(1) && "Select must have bool or bool vector condition"
) ? void (0) : __assert_fail ("Cond->getType()->isIntOrIntVectorTy(1) && \"Select must have bool or bool vector condition\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 4403, __extension__
__PRETTY_FUNCTION__))
;
4404 assert(TrueVal->getType() == FalseVal->getType() &&(static_cast <bool> (TrueVal->getType() == FalseVal->
getType() && "Select must have same types for true/false ops"
) ? void (0) : __assert_fail ("TrueVal->getType() == FalseVal->getType() && \"Select must have same types for true/false ops\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 4405, __extension__
__PRETTY_FUNCTION__))
4405 "Select must have same types for true/false ops")(static_cast <bool> (TrueVal->getType() == FalseVal->
getType() && "Select must have same types for true/false ops"
) ? void (0) : __assert_fail ("TrueVal->getType() == FalseVal->getType() && \"Select must have same types for true/false ops\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 4405, __extension__
__PRETTY_FUNCTION__))
;
4406
4407 if (Cond->getType() == TrueVal->getType()) {
4408 // select i1 Cond, i1 true, i1 false --> i1 Cond
4409 if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
4410 return Cond;
4411
4412 // (X || Y) && (X || !Y) --> X (commuted 8 ways)
4413 Value *X, *Y;
4414 if (match(FalseVal, m_ZeroInt())) {
4415 if (match(Cond, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
4416 match(TrueVal, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
4417 return X;
4418 if (match(TrueVal, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
4419 match(Cond, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
4420 return X;
4421 }
4422 }
4423
4424 // select ?, X, X -> X
4425 if (TrueVal == FalseVal)
4426 return TrueVal;
4427
4428 // If the true or false value is poison, we can fold to the other value.
4429 // If the true or false value is undef, we can fold to the other value as
4430 // long as the other value isn't poison.
4431 // select ?, poison, X -> X
4432 // select ?, undef, X -> X
4433 if (isa<PoisonValue>(TrueVal) ||
4434 (Q.isUndefValue(TrueVal) &&
4435 isGuaranteedNotToBePoison(FalseVal, Q.AC, Q.CxtI, Q.DT)))
4436 return FalseVal;
4437 // select ?, X, poison -> X
4438 // select ?, X, undef -> X
4439 if (isa<PoisonValue>(FalseVal) ||
4440 (Q.isUndefValue(FalseVal) &&
4441 isGuaranteedNotToBePoison(TrueVal, Q.AC, Q.CxtI, Q.DT)))
4442 return TrueVal;
4443
4444 // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
4445 Constant *TrueC, *FalseC;
4446 if (isa<FixedVectorType>(TrueVal->getType()) &&
4447 match(TrueVal, m_Constant(TrueC)) &&
4448 match(FalseVal, m_Constant(FalseC))) {
4449 unsigned NumElts =
4450 cast<FixedVectorType>(TrueC->getType())->getNumElements();
4451 SmallVector<Constant *, 16> NewC;
4452 for (unsigned i = 0; i != NumElts; ++i) {
4453 // Bail out on incomplete vector constants.
4454 Constant *TEltC = TrueC->getAggregateElement(i);
4455 Constant *FEltC = FalseC->getAggregateElement(i);
4456 if (!TEltC || !FEltC)
4457 break;
4458
4459 // If the elements match (undef or not), that value is the result. If only
4460 // one element is undef, choose the defined element as the safe result.
4461 if (TEltC == FEltC)
4462 NewC.push_back(TEltC);
4463 else if (isa<PoisonValue>(TEltC) ||
4464 (Q.isUndefValue(TEltC) && isGuaranteedNotToBePoison(FEltC)))
4465 NewC.push_back(FEltC);
4466 else if (isa<PoisonValue>(FEltC) ||
4467 (Q.isUndefValue(FEltC) && isGuaranteedNotToBePoison(TEltC)))
4468 NewC.push_back(TEltC);
4469 else
4470 break;
4471 }
4472 if (NewC.size() == NumElts)
4473 return ConstantVector::get(NewC);
4474 }
4475
4476 if (Value *V =
4477 simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
4478 return V;
4479
4480 if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q))
4481 return V;
4482
4483 if (Value *V = foldSelectWithBinaryOp(Cond, TrueVal, FalseVal))
4484 return V;
4485
4486 Optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL);
4487 if (Imp)
4488 return *Imp ? TrueVal : FalseVal;
4489
4490 return nullptr;
4491}
4492
4493Value *llvm::simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
4494 const SimplifyQuery &Q) {
4495 return ::simplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit);
4496}
4497
4498/// Given operands for an GetElementPtrInst, see if we can fold the result.
4499/// If not, this returns null.
4500static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
4501 ArrayRef<Value *> Indices, bool InBounds,
4502 const SimplifyQuery &Q, unsigned) {
4503 // The type of the GEP pointer operand.
4504 unsigned AS =
4505 cast<PointerType>(Ptr->getType()->getScalarType())->getAddressSpace();
4506
4507 // getelementptr P -> P.
4508 if (Indices.empty())
4509 return Ptr;
4510
4511 // Compute the (pointer) type returned by the GEP instruction.
4512 Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Indices);
4513 Type *GEPTy = PointerType::get(LastType, AS);
4514 if (VectorType *VT = dyn_cast<VectorType>(Ptr->getType()))
4515 GEPTy = VectorType::get(GEPTy, VT->getElementCount());
4516 else {
4517 for (Value *Op : Indices) {
4518 // If one of the operands is a vector, the result type is a vector of
4519 // pointers. All vector operands must have the same number of elements.
4520 if (VectorType *VT = dyn_cast<VectorType>(Op->getType())) {
4521 GEPTy = VectorType::get(GEPTy, VT->getElementCount());
4522 break;
4523 }
4524 }
4525 }
4526
4527 // For opaque pointers an all-zero GEP is a no-op. For typed pointers,
4528 // it may be equivalent to a bitcast.
4529 if (Ptr->getType()->getScalarType()->isOpaquePointerTy() &&
4530 Ptr->getType() == GEPTy &&
4531 all_of(Indices, [](const auto *V) { return match(V, m_Zero()); }))
4532 return Ptr;
4533
4534 // getelementptr poison, idx -> poison
4535 // getelementptr baseptr, poison -> poison
4536 if (isa<PoisonValue>(Ptr) ||
4537 any_of(Indices, [](const auto *V) { return isa<PoisonValue>(V); }))
4538 return PoisonValue::get(GEPTy);
4539
4540 if (Q.isUndefValue(Ptr))
4541 // If inbounds, we can choose an out-of-bounds pointer as a base pointer.
4542 return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy);
4543
4544 bool IsScalableVec =
4545 isa<ScalableVectorType>(SrcTy) || any_of(Indices, [](const Value *V) {
4546 return isa<ScalableVectorType>(V->getType());
4547 });
4548
4549 if (Indices.size() == 1) {
4550 // getelementptr P, 0 -> P.
4551 if (match(Indices[0], m_Zero()) && Ptr->getType() == GEPTy)
4552 return Ptr;
4553
4554 Type *Ty = SrcTy;
4555 if (!IsScalableVec && Ty->isSized()) {
4556 Value *P;
4557 uint64_t C;
4558 uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
4559 // getelementptr P, N -> P if P points to a type of zero size.
4560 if (TyAllocSize == 0 && Ptr->getType() == GEPTy)
4561 return Ptr;
4562
4563 // The following transforms are only safe if the ptrtoint cast
4564 // doesn't truncate the pointers.
4565 if (Indices[0]->getType()->getScalarSizeInBits() ==
4566 Q.DL.getPointerSizeInBits(AS)) {
4567 auto CanSimplify = [GEPTy, &P, Ptr]() -> bool {
4568 return P->getType() == GEPTy &&
4569 getUnderlyingObject(P) == getUnderlyingObject(Ptr);
4570 };
4571 // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
4572 if (TyAllocSize == 1 &&
4573 match(Indices[0],
4574 m_Sub(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Specific(Ptr)))) &&
4575 CanSimplify())
4576 return P;
4577
4578 // getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of
4579 // size 1 << C.
4580 if (match(Indices[0], m_AShr(m_Sub(m_PtrToInt(m_Value(P)),
4581 m_PtrToInt(m_Specific(Ptr))),
4582 m_ConstantInt(C))) &&
4583 TyAllocSize == 1ULL << C && CanSimplify())
4584 return P;
4585
4586 // getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of
4587 // size C.
4588 if (match(Indices[0], m_SDiv(m_Sub(m_PtrToInt(m_Value(P)),
4589 m_PtrToInt(m_Specific(Ptr))),
4590 m_SpecificInt(TyAllocSize))) &&
4591 CanSimplify())
4592 return P;
4593 }
4594 }
4595 }
4596
4597 if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 &&
4598 all_of(Indices.drop_back(1),
4599 [](Value *Idx) { return match(Idx, m_Zero()); })) {
4600 unsigned IdxWidth =
4601 Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
4602 if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
4603 APInt BasePtrOffset(IdxWidth, 0);
4604 Value *StrippedBasePtr =
4605 Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.DL, BasePtrOffset);
4606
4607 // Avoid creating inttoptr of zero here: While LLVMs treatment of
4608 // inttoptr is generally conservative, this particular case is folded to
4609 // a null pointer, which will have incorrect provenance.
4610
4611 // gep (gep V, C), (sub 0, V) -> C
4612 if (match(Indices.back(),
4613 m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr)))) &&
4614 !BasePtrOffset.isZero()) {
4615 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
4616 return ConstantExpr::getIntToPtr(CI, GEPTy);
4617 }
4618 // gep (gep V, C), (xor V, -1) -> C-1
4619 if (match(Indices.back(),
4620 m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes())) &&
4621 !BasePtrOffset.isOne()) {
4622 auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
4623 return ConstantExpr::getIntToPtr(CI, GEPTy);
4624 }
4625 }
4626 }
4627
4628 // Check to see if this is constant foldable.
4629 if (!isa<Constant>(Ptr) ||
4630 !all_of(Indices, [](Value *V) { return isa<Constant>(V); }))
4631 return nullptr;
4632
4633 auto *CE = ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices,
4634 InBounds);
4635 return ConstantFoldConstant(CE, Q.DL);
4636}
4637
4638Value *llvm::simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
4639 bool InBounds, const SimplifyQuery &Q) {
4640 return ::simplifyGEPInst(SrcTy, Ptr, Indices, InBounds, Q, RecursionLimit);
4641}
4642
4643/// Given operands for an InsertValueInst, see if we can fold the result.
4644/// If not, this returns null.
4645static Value *simplifyInsertValueInst(Value *Agg, Value *Val,
4646 ArrayRef<unsigned> Idxs,
4647 const SimplifyQuery &Q, unsigned) {
4648 if (Constant *CAgg = dyn_cast<Constant>(Agg))
4649 if (Constant *CVal = dyn_cast<Constant>(Val))
4650 return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
4651
4652 // insertvalue x, undef, n -> x
4653 if (Q.isUndefValue(Val))
4654 return Agg;
4655
4656 // insertvalue x, (extractvalue y, n), n
4657 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
4658 if (EV->getAggregateOperand()->getType() == Agg->getType() &&
4659 EV->getIndices() == Idxs) {
4660 // insertvalue undef, (extractvalue y, n), n -> y
4661 if (Q.isUndefValue(Agg))
4662 return EV->getAggregateOperand();
4663
4664 // insertvalue y, (extractvalue y, n), n -> y
4665 if (Agg == EV->getAggregateOperand())
4666 return Agg;
4667 }
4668
4669 return nullptr;
4670}
4671
4672Value *llvm::simplifyInsertValueInst(Value *Agg, Value *Val,
4673 ArrayRef<unsigned> Idxs,
4674 const SimplifyQuery &Q) {
4675 return ::simplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
4676}
4677
4678Value *llvm::simplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
4679 const SimplifyQuery &Q) {
4680 // Try to constant fold.
4681 auto *VecC = dyn_cast<Constant>(Vec);
4682 auto *ValC = dyn_cast<Constant>(Val);
4683 auto *IdxC = dyn_cast<Constant>(Idx);
4684 if (VecC && ValC && IdxC)
4685 return ConstantExpr::getInsertElement(VecC, ValC, IdxC);
4686
4687 // For fixed-length vector, fold into poison if index is out of bounds.
4688 if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
4689 if (isa<FixedVectorType>(Vec->getType()) &&
4690 CI->uge(cast<FixedVectorType>(Vec->getType())->getNumElements()))
4691 return PoisonValue::get(Vec->getType());
4692 }
4693
4694 // If index is undef, it might be out of bounds (see above case)
4695 if (Q.isUndefValue(Idx))
4696 return PoisonValue::get(Vec->getType());
4697
4698 // If the scalar is poison, or it is undef and there is no risk of
4699 // propagating poison from the vector value, simplify to the vector value.
4700 if (isa<PoisonValue>(Val) ||
4701 (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec)))
4702 return Vec;
4703
4704 // If we are extracting a value from a vector, then inserting it into the same
4705 // place, that's the input vector:
4706 // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
4707 if (match(Val, m_ExtractElt(m_Specific(Vec), m_Specific(Idx))))
4708 return Vec;
4709
4710 return nullptr;
4711}
4712
4713/// Given operands for an ExtractValueInst, see if we can fold the result.
4714/// If not, this returns null.
4715static Value *simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
4716 const SimplifyQuery &, unsigned) {
4717 if (auto *CAgg = dyn_cast<Constant>(Agg))
4718 return ConstantFoldExtractValueInstruction(CAgg, Idxs);
4719
4720 // extractvalue x, (insertvalue y, elt, n), n -> elt
4721 unsigned NumIdxs = Idxs.size();
4722 for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
4723 IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
4724 ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
4725 unsigned NumInsertValueIdxs = InsertValueIdxs.size();
4726 unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
4727 if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
4728 Idxs.slice(0, NumCommonIdxs)) {
4729 if (NumIdxs == NumInsertValueIdxs)
4730 return IVI->getInsertedValueOperand();
4731 break;
4732 }
4733 }
4734
4735 return nullptr;
4736}
4737
4738Value *llvm::simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
4739 const SimplifyQuery &Q) {
4740 return ::simplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit);
4741}
4742
4743/// Given operands for an ExtractElementInst, see if we can fold the result.
4744/// If not, this returns null.
4745static Value *simplifyExtractElementInst(Value *Vec, Value *Idx,
4746 const SimplifyQuery &Q, unsigned) {
4747 auto *VecVTy = cast<VectorType>(Vec->getType());
4748 if (auto *CVec = dyn_cast<Constant>(Vec)) {
4749 if (auto *CIdx = dyn_cast<Constant>(Idx))
4750 return ConstantExpr::getExtractElement(CVec, CIdx);
4751
4752 if (Q.isUndefValue(Vec))
4753 return UndefValue::get(VecVTy->getElementType());
4754 }
4755
4756 // An undef extract index can be arbitrarily chosen to be an out-of-range
4757 // index value, which would result in the instruction being poison.
4758 if (Q.isUndefValue(Idx))
4759 return PoisonValue::get(VecVTy->getElementType());
4760
4761 // If extracting a specified index from the vector, see if we can recursively
4762 // find a previously computed scalar that was inserted into the vector.
4763 if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
4764 // For fixed-length vector, fold into undef if index is out of bounds.
4765 unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
4766 if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts))
4767 return PoisonValue::get(VecVTy->getElementType());
4768 // Handle case where an element is extracted from a splat.
4769 if (IdxC->getValue().ult(MinNumElts))
4770 if (auto *Splat = getSplatValue(Vec))
4771 return Splat;
4772 if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
4773 return Elt;
4774 } else {
4775 // The index is not relevant if our vector is a splat.
4776 if (Value *Splat = getSplatValue(Vec))
4777 return Splat;
4778 }
4779 return nullptr;
4780}
4781
4782Value *llvm::simplifyExtractElementInst(Value *Vec, Value *Idx,
4783 const SimplifyQuery &Q) {
4784 return ::simplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
4785}
4786
4787/// See if we can fold the given phi. If not, returns null.
4788static Value *simplifyPHINode(PHINode *PN, ArrayRef<Value *> IncomingValues,
4789 const SimplifyQuery &Q) {
4790 // WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE
4791 // here, because the PHI we may succeed simplifying to was not
4792 // def-reachable from the original PHI!
4793
4794 // If all of the PHI's incoming values are the same then replace the PHI node
4795 // with the common value.
4796 Value *CommonValue = nullptr;
4797 bool HasUndefInput = false;
4798 for (Value *Incoming : IncomingValues) {
4799 // If the incoming value is the phi node itself, it can safely be skipped.
4800 if (Incoming == PN)
4801 continue;
4802 if (Q.isUndefValue(Incoming)) {
4803 // Remember that we saw an undef value, but otherwise ignore them.
4804 HasUndefInput = true;
4805 continue;
4806 }
4807 if (CommonValue && Incoming != CommonValue)
4808 return nullptr; // Not the same, bail out.
4809 CommonValue = Incoming;
4810 }
4811
4812 // If CommonValue is null then all of the incoming values were either undef or
4813 // equal to the phi node itself.
4814 if (!CommonValue)
4815 return UndefValue::get(PN->getType());
4816
4817 if (HasUndefInput) {
4818 // If we have a PHI node like phi(X, undef, X), where X is defined by some
4819 // instruction, we cannot return X as the result of the PHI node unless it
4820 // dominates the PHI block.
4821 return valueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr;
4822 }
4823
4824 return CommonValue;
4825}
4826
4827static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
4828 const SimplifyQuery &Q, unsigned MaxRecurse) {
4829 if (auto *C = dyn_cast<Constant>(Op))
4830 return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL);
4831
4832 if (auto *CI = dyn_cast<CastInst>(Op)) {
4833 auto *Src = CI->getOperand(0);
4834 Type *SrcTy = Src->getType();
4835 Type *MidTy = CI->getType();
4836 Type *DstTy = Ty;
4837 if (Src->getType() == Ty) {
4838 auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode());
4839 auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
4840 Type *SrcIntPtrTy =
4841 SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr;
4842 Type *MidIntPtrTy =
4843 MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr;
4844 Type *DstIntPtrTy =
4845 DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr;
4846 if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy,
4847 SrcIntPtrTy, MidIntPtrTy,
4848 DstIntPtrTy) == Instruction::BitCast)
4849 return Src;
4850 }
4851 }
4852
4853 // bitcast x -> x
4854 if (CastOpc == Instruction::BitCast)
4855 if (Op->getType() == Ty)
4856 return Op;
4857
4858 return nullptr;
4859}
4860
4861Value *llvm::simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
4862 const SimplifyQuery &Q) {
4863 return ::simplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit);
4864}
4865
4866/// For the given destination element of a shuffle, peek through shuffles to
4867/// match a root vector source operand that contains that element in the same
4868/// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
4869static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
4870 int MaskVal, Value *RootVec,
4871 unsigned MaxRecurse) {
4872 if (!MaxRecurse--)
4873 return nullptr;
4874
4875 // Bail out if any mask value is undefined. That kind of shuffle may be
4876 // simplified further based on demanded bits or other folds.
4877 if (MaskVal == -1)
4878 return nullptr;
4879
4880 // The mask value chooses which source operand we need to look at next.
4881 int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
4882 int RootElt = MaskVal;
4883 Value *SourceOp = Op0;
4884 if (MaskVal >= InVecNumElts) {
4885 RootElt = MaskVal - InVecNumElts;
4886 SourceOp = Op1;
4887 }
4888
4889 // If the source operand is a shuffle itself, look through it to find the
4890 // matching root vector.
4891 if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
4892 return foldIdentityShuffles(
4893 DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
4894 SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
4895 }
4896
4897 // TODO: Look through bitcasts? What if the bitcast changes the vector element
4898 // size?
4899
4900 // The source operand is not a shuffle. Initialize the root vector value for
4901 // this shuffle if that has not been done yet.
4902 if (!RootVec)
4903 RootVec = SourceOp;
4904
4905 // Give up as soon as a source operand does not match the existing root value.
4906 if (RootVec != SourceOp)
4907 return nullptr;
4908
4909 // The element must be coming from the same lane in the source vector
4910 // (although it may have crossed lanes in intermediate shuffles).
4911 if (RootElt != DestElt)
4912 return nullptr;
4913
4914 return RootVec;
4915}
4916
4917static Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1,
4918 ArrayRef<int> Mask, Type *RetTy,
4919 const SimplifyQuery &Q,
4920 unsigned MaxRecurse) {
4921 if (all_of(Mask, [](int Elem) { return Elem == UndefMaskElem; }))
4922 return UndefValue::get(RetTy);
4923
4924 auto *InVecTy = cast<VectorType>(Op0->getType());
4925 unsigned MaskNumElts = Mask.size();
4926 ElementCount InVecEltCount = InVecTy->getElementCount();
4927
4928 bool Scalable = InVecEltCount.isScalable();
4929
4930 SmallVector<int, 32> Indices;
4931 Indices.assign(Mask.begin(), Mask.end());
4932
4933 // Canonicalization: If mask does not select elements from an input vector,
4934 // replace that input vector with poison.
4935 if (!Scalable) {
4936 bool MaskSelects0 = false, MaskSelects1 = false;
4937 unsigned InVecNumElts = InVecEltCount.getKnownMinValue();
4938 for (unsigned i = 0; i != MaskNumElts; ++i) {
4939 if (Indices[i] == -1)
4940 continue;
4941 if ((unsigned)Indices[i] < InVecNumElts)
4942 MaskSelects0 = true;
4943 else
4944 MaskSelects1 = true;
4945 }
4946 if (!MaskSelects0)
4947 Op0 = PoisonValue::get(InVecTy);
4948 if (!MaskSelects1)
4949 Op1 = PoisonValue::get(InVecTy);
4950 }
4951
4952 auto *Op0Const = dyn_cast<Constant>(Op0);
4953 auto *Op1Const = dyn_cast<Constant>(Op1);
4954
4955 // If all operands are constant, constant fold the shuffle. This
4956 // transformation depends on the value of the mask which is not known at
4957 // compile time for scalable vectors
4958 if (Op0Const && Op1Const)
4959 return ConstantExpr::getShuffleVector(Op0Const, Op1Const, Mask);
4960
4961 // Canonicalization: if only one input vector is constant, it shall be the
4962 // second one. This transformation depends on the value of the mask which
4963 // is not known at compile time for scalable vectors
4964 if (!Scalable && Op0Const && !Op1Const) {
4965 std::swap(Op0, Op1);
4966 ShuffleVectorInst::commuteShuffleMask(Indices,
4967 InVecEltCount.getKnownMinValue());
4968 }
4969
4970 // A splat of an inserted scalar constant becomes a vector constant:
4971 // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
4972 // NOTE: We may have commuted above, so analyze the updated Indices, not the
4973 // original mask constant.
4974 // NOTE: This transformation depends on the value of the mask which is not
4975 // known at compile time for scalable vectors
4976 Constant *C;
4977 ConstantInt *IndexC;
4978 if (!Scalable && match(Op0, m_InsertElt(m_Value(), m_Constant(C),
4979 m_ConstantInt(IndexC)))) {
4980 // Match a splat shuffle mask of the insert index allowing undef elements.
4981 int InsertIndex = IndexC->getZExtValue();
4982 if (all_of(Indices, [InsertIndex](int MaskElt) {
4983 return MaskElt == InsertIndex || MaskElt == -1;
4984 })) {
4985 assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat")(static_cast <bool> (isa<UndefValue>(Op1) &&
"Expected undef operand 1 for splat") ? void (0) : __assert_fail
("isa<UndefValue>(Op1) && \"Expected undef operand 1 for splat\""
, "llvm/lib/Analysis/InstructionSimplify.cpp", 4985, __extension__
__PRETTY_FUNCTION__))
;
4986
4987 // Shuffle mask undefs become undefined constant result elements.
4988 SmallVector<Constant *, 16> VecC(MaskNumElts, C);
4989 for (unsigned i = 0; i != MaskNumElts; ++i)
4990 if (Indices[i] == -1)
4991 VecC[i] = UndefValue::get(C->getType());
4992 return ConstantVector::get(VecC);
4993 }
4994 }
4995
4996 // A shuffle of a splat is always the splat itself. Legal if the shuffle's
4997 // value type is same as the input vectors' type.
4998 if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
4999 if (Q.isUndefValue(Op1) && RetTy == InVecTy &&
5000 all_equal(OpShuf->getShuffleMask()))
5001 return Op0;
5002
5003 // All remaining transformation depend on the value of the mask, which is
5004 // not known at compile time for scalable vectors.
5005 if (Scalable)
5006 return nullptr;
5007
5008 // Don't fold a shuffle with undef mask elements. This may get folded in a
5009 // better way using demanded bits or other analysis.
5010 // TODO: Should we allow this?
5011 if (is_contained(Indices, -1))
5012 return nullptr;
5013
5014 // Check if every element of this shuffle can be mapped back to the
5015 // corresponding element of a single root vector. If so, we don't need this
5016 // shuffle. This handles simple identity shuffles as well as chains of
5017 // shuffles that may widen/narrow and/or move elements across lanes and back.
5018 Value *RootVec = nullptr;
5019 for (unsigned i = 0; i != MaskNumElts; ++i) {
5020 // Note that recursion is limited for each vector element, so if any element
5021 // exceeds the limit, this will fail to simplify.
5022 RootVec =
5023 foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse);
5024
5025 // We can't replace a widening/narrowing shuffle with one of its operands.
5026 if (!RootVec || RootVec->getType() != RetTy)
5027 return nullptr;
5028 }
5029 return RootVec;
5030}
5031
5032/// Given operands for a ShuffleVectorInst, fold the result or return null.
5033Value *llvm::simplifyShuffleVectorInst(Value *Op0, Value *Op1,
5034 ArrayRef<int> Mask, Type *RetTy,
5035 const SimplifyQuery &Q) {
5036 return ::simplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit);
5037}
5038
5039static Constant *foldConstant(Instruction::UnaryOps Opcode, Value *&Op,
5040 const SimplifyQuery &Q) {
5041 if (auto *C = dyn_cast<Constant>(Op))
5042 return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL);
5043 return nullptr;
5044}
5045
5046/// Given the operand for an FNeg, see if we can fold the result. If not, this
5047/// returns null.
5048static Value *simplifyFNegInst(Value *Op, FastMathFlags FMF,
5049 const SimplifyQuery &Q, unsigned MaxRecurse) {
5050 if (Constant *C = foldConstant(Instruction::FNeg, Op, Q))
5051 return C;
5052
5053 Value *X;
5054 // fneg (fneg X) ==> X
5055 if (match(Op, m_FNeg(m_Value(X))))
5056 return X;
5057
5058 return nullptr;
5059}
5060
5061Value *llvm::simplifyFNegInst(Value *Op, FastMathFlags FMF,
5062 const SimplifyQuery &Q) {
5063 return ::simplifyFNegInst(Op, FMF, Q, RecursionLimit);
5064}
5065
5066static Constant *propagateNaN(Constant *In) {
5067 // If the input is a vector with undef elements, just return a default NaN.
5068 if (!In->isNaN())
5069 return ConstantFP::getNaN(In->getType());
5070
5071 // Propagate the existing NaN constant when possible.
5072 // TODO: Should we quiet a signaling NaN?
5073 return In;
5074}
5075
5076/// Perform folds that are common to any floating-point operation. This implies
5077/// transforms based on poison/undef/NaN because the operation itself makes no
5078/// difference to the result.
5079static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
5080 const SimplifyQuery &Q,
5081 fp::ExceptionBehavior ExBehavior,
5082 RoundingMode Rounding) {
5083 // Poison is independent of anything else. It always propagates from an
5084 // operand to a math result.
5085 if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
5086 return PoisonValue::get(Ops[0]->getType());
5087
5088 for (Value *V : Ops) {
5089 bool IsNan = match(V, m_NaN());
5090 bool IsInf = match(V, m_Inf());
5091 bool IsUndef = Q.isUndefValue(V);
5092
5093 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
5094 // (an undef operand can be chosen to be Nan/Inf), then the result of
5095 // this operation is poison.
5096 if (FMF.noNaNs() && (IsNan || IsUndef))
5097 return PoisonValue::get(V->getType());
5098 if (FMF.noInfs() && (IsInf || IsUndef))
5099 return PoisonValue::get(V->getType());
5100
5101 if (isDefaultFPEnvironment(ExBehavior, Rounding)) {
5102 if (IsUndef || IsNan)
5103 return propagateNaN(cast<Constant>(V));
5104 } else if (ExBehavior != fp::ebStrict) {
5105 if (IsNan)
5106 return propagateNaN(cast<Constant>(V));
5107 }
5108 }
5109 return nullptr;
5110}
5111
5112/// Given operands for an FAdd, see if we can fold the result. If not, this
5113/// returns null.
5114static Value *
5115simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5116 const SimplifyQuery &Q, unsigned MaxRecurse,
5117 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5118 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5119 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5120 if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
5121 return C;
5122
5123 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5124 return C;
5125
5126 // fadd X, -0 ==> X
5127 // With strict/constrained FP, we have these possible edge cases that do
5128 // not simplify to Op0:
5129 // fadd SNaN, -0.0 --> QNaN
5130 // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative)
5131 if (canIgnoreSNaN(ExBehavior, FMF) &&
5132 (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
5133 FMF.noSignedZeros()))
5134 if (match(Op1, m_NegZeroFP()))
5135 return Op0;
5136
5137 // fadd X, 0 ==> X, when we know X is not -0
5138 if (canIgnoreSNaN(ExBehavior, FMF))
5139 if (match(Op1, m_PosZeroFP()) &&
5140 (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
5141 return Op0;
5142
5143 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5144 return nullptr;
5145
5146 // With nnan: -X + X --> 0.0 (and commuted variant)
5147 // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
5148 // Negative zeros are allowed because we always end up with positive zero:
5149 // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5150 // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
5151 // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
5152 // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
5153 if (FMF.noNaNs()) {
5154 if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
5155 match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
5156 return ConstantFP::getNullValue(Op0->getType());
5157
5158 if (match(Op0, m_FNeg(m_Specific(Op1))) ||
5159 match(Op1, m_FNeg(m_Specific(Op0))))
5160 return ConstantFP::getNullValue(Op0->getType());
5161 }
5162
5163 // (X - Y) + Y --> X
5164 // Y + (X - Y) --> X
5165 Value *X;
5166 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5167 (match(Op0, m_FSub(m_Value(X), m_Specific(Op1))) ||
5168 match(Op1, m_FSub(m_Value(X), m_Specific(Op0)))))
5169 return X;
5170
5171 return nullptr;
5172}
5173
5174/// Given operands for an FSub, see if we can fold the result. If not, this
5175/// returns null.
5176static Value *
5177simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5178 const SimplifyQuery &Q, unsigned MaxRecurse,
5179 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5180 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5181 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5182 if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
5183 return C;
5184
5185 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5186 return C;
5187
5188 // fsub X, +0 ==> X
5189 if (canIgnoreSNaN(ExBehavior, FMF) &&
5190 (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
5191 FMF.noSignedZeros()))
5192 if (match(Op1, m_PosZeroFP()))
5193 return Op0;
5194
5195 // fsub X, -0 ==> X, when we know X is not -0
5196 if (canIgnoreSNaN(ExBehavior, FMF))
5197 if (match(Op1, m_NegZeroFP()) &&
5198 (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
5199 return Op0;
5200
5201 // fsub -0.0, (fsub -0.0, X) ==> X
5202 // fsub -0.0, (fneg X) ==> X
5203 Value *X;
5204 if (canIgnoreSNaN(ExBehavior, FMF))
5205 if (match(Op0, m_NegZeroFP()) && match(Op1, m_FNeg(m_Value(X))))
5206 return X;
5207
5208 // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
5209 // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.
5210 if (canIgnoreSNaN(ExBehavior, FMF))
5211 if (FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()) &&
5212 (match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X))) ||
5213 match(Op1, m_FNeg(m_Value(X)))))
5214 return X;
5215
5216 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5217 return nullptr;
5218
5219 // fsub nnan x, x ==> 0.0
5220 if (FMF.noNaNs() && Op0 == Op1)
5221 return Constant::getNullValue(Op0->getType());
5222
5223 // Y - (Y - X) --> X
5224 // (X + Y) - Y --> X
5225 if (FMF.noSignedZeros() && FMF.allowReassoc() &&
5226 (match(Op1, m_FSub(m_Specific(Op0), m_Value(X))) ||
5227 match(Op0, m_c_FAdd(m_Specific(Op1), m_Value(X)))))
5228 return X;
5229
5230 return nullptr;
5231}
5232
5233static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
5234 const SimplifyQuery &Q, unsigned MaxRecurse,
5235 fp::ExceptionBehavior ExBehavior,
5236 RoundingMode Rounding) {
5237 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5238 return C;
5239
5240 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5241 return nullptr;
5242
5243 // fmul X, 1.0 ==> X
5244 if (match(Op1, m_FPOne()))
5245 return Op0;
5246
5247 // fmul 1.0, X ==> X
5248 if (match(Op0, m_FPOne()))
5249 return Op1;
5250
5251 // fmul nnan nsz X, 0 ==> 0
5252 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZeroFP()))
5253 return ConstantFP::getNullValue(Op0->getType());
5254
5255 // fmul nnan nsz 0, X ==> 0
5256 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
5257 return ConstantFP::getNullValue(Op1->getType());
5258
5259 // sqrt(X) * sqrt(X) --> X, if we can:
5260 // 1. Remove the intermediate rounding (reassociate).
5261 // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
5262 // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
5263 Value *X;
5264 if (Op0 == Op1 && match(Op0, m_Sqrt(m_Value(X))) && FMF.allowReassoc() &&
5265 FMF.noNaNs() && FMF.noSignedZeros())
5266 return X;
5267
5268 return nullptr;
5269}
5270
5271/// Given the operands for an FMul, see if we can fold the result
5272static Value *
5273simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5274 const SimplifyQuery &Q, unsigned MaxRecurse,
5275 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5276 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5277 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5278 if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
5279 return C;
5280
5281 // Now apply simplifications that do not require rounding.
5282 return simplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding);
5283}
5284
5285Value *llvm::simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5286 const SimplifyQuery &Q,
5287 fp::ExceptionBehavior ExBehavior,
5288 RoundingMode Rounding) {
5289 return ::simplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5290 Rounding);
5291}
5292
5293Value *llvm::simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5294 const SimplifyQuery &Q,
5295 fp::ExceptionBehavior ExBehavior,
5296 RoundingMode Rounding) {
5297 return ::simplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5298 Rounding);
5299}
5300
5301Value *llvm::simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5302 const SimplifyQuery &Q,
5303 fp::ExceptionBehavior ExBehavior,
5304 RoundingMode Rounding) {
5305 return ::simplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5306 Rounding);
5307}
5308
5309Value *llvm::simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
5310 const SimplifyQuery &Q,
5311 fp::ExceptionBehavior ExBehavior,
5312 RoundingMode Rounding) {
5313 return ::simplifyFMAFMul(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5314 Rounding);
5315}
5316
5317static Value *
5318simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5319 const SimplifyQuery &Q, unsigned,
5320 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5321 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5322 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5323 if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
5324 return C;
5325
5326 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5327 return C;
5328
5329 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5330 return nullptr;
5331
5332 // X / 1.0 -> X
5333 if (match(Op1, m_FPOne()))
5334 return Op0;
5335
5336 // 0 / X -> 0
5337 // Requires that NaNs are off (X could be zero) and signed zeroes are
5338 // ignored (X could be positive or negative, so the output sign is unknown).
5339 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
5340 return ConstantFP::getNullValue(Op0->getType());
5341
5342 if (FMF.noNaNs()) {
5343 // X / X -> 1.0 is legal when NaNs are ignored.
5344 // We can ignore infinities because INF/INF is NaN.
5345 if (Op0 == Op1)
5346 return ConstantFP::get(Op0->getType(), 1.0);
5347
5348 // (X * Y) / Y --> X if we can reassociate to the above form.
5349 Value *X;
5350 if (FMF.allowReassoc() && match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1))))
5351 return X;
5352
5353 // -X / X -> -1.0 and
5354 // X / -X -> -1.0 are legal when NaNs are ignored.
5355 // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
5356 if (match(Op0, m_FNegNSZ(m_Specific(Op1))) ||
5357 match(Op1, m_FNegNSZ(m_Specific(Op0))))
5358 return ConstantFP::get(Op0->getType(), -1.0);
5359 }
5360
5361 return nullptr;
5362}
5363
5364Value *llvm::simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5365 const SimplifyQuery &Q,
5366 fp::ExceptionBehavior ExBehavior,
5367 RoundingMode Rounding) {
5368 return ::simplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5369 Rounding);
5370}
5371
5372static Value *
5373simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5374 const SimplifyQuery &Q, unsigned,
5375 fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
5376 RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5377 if (isDefaultFPEnvironment(ExBehavior, Rounding))
5378 if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
5379 return C;
5380
5381 if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
5382 return C;
5383
5384 if (!isDefaultFPEnvironment(ExBehavior, Rounding))
5385 return nullptr;
5386
5387 // Unlike fdiv, the result of frem always matches the sign of the dividend.
5388 // The constant match may include undef elements in a vector, so return a full
5389 // zero constant as the result.
5390 if (FMF.noNaNs()) {
5391 // +0 % X -> 0
5392 if (match(Op0, m_PosZeroFP()))
5393 return ConstantFP::getNullValue(Op0->getType());
5394 // -0 % X -> -0
5395 if (match(Op0, m_NegZeroFP()))
5396 return ConstantFP::getNegativeZero(Op0->getType());
5397 }
5398
5399 return nullptr;
5400}
5401
5402Value *llvm::simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
5403 const SimplifyQuery &Q,
5404 fp::ExceptionBehavior ExBehavior,
5405 RoundingMode Rounding) {
5406 return ::simplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
5407 Rounding);
5408}
5409
5410//=== Helper functions for higher up the class hierarchy.
5411
5412/// Given the operand for a UnaryOperator, see if we can fold the result.
5413/// If not, this returns null.
5414static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q,
5415 unsigned MaxRecurse) {
5416 switch (Opcode) {
5417 case Instruction::FNeg:
5418 return simplifyFNegInst(Op, FastMathFlags(), Q, MaxRecurse);
5419 default:
5420 llvm_unreachable("Unexpected opcode")::llvm::llvm_unreachable_internal("Unexpected opcode", "llvm/lib/Analysis/InstructionSimplify.cpp"
, 5420)
;
5421 }
5422}
5423
5424/// Given the operand for a UnaryOperator, see if we can fold the result.
5425/// If not, this returns null.
5426/// Try to use FastMathFlags when folding the result.
5427static Value *simplifyFPUnOp(unsigned Opcode, Value *Op,
5428 const FastMathFlags &FMF, const SimplifyQuery &Q,
5429 unsigned MaxRecurse) {
5430 switch (Opcode) {
5431 case Instruction::FNeg:
5432 return simplifyFNegInst(Op, FMF, Q, MaxRecurse);
5433 default:
5434 return simplifyUnOp(Opcode, Op, Q, MaxRecurse);
5435 }
5436}
5437
5438Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) {
5439 return ::simplifyUnOp(Opcode, Op, Q, RecursionLimit);
5440}
5441
5442Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
5443 const SimplifyQuery &Q) {
5444 return ::simplifyFPUnOp(Opcode, Op, FMF, Q, RecursionLimit);
5445}
5446
5447/// Given operands for a BinaryOperator, see if we can fold the result.
5448/// If not, this returns null.
5449static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
5450 const SimplifyQuery &Q, unsigned MaxRecurse) {
5451 switch (Opcode) {
5452 case Instruction::Add:
5453 return simplifyAddInst(LHS, RHS, false, false, Q, MaxRecurse);
5454 case Instruction::Sub:
5455 return simplifySubInst(LHS, RHS, false, false, Q, MaxRecurse);
5456 case Instruction::Mul:
5457 return simplifyMulInst(LHS, RHS, Q, MaxRecurse);
5458 case Instruction::SDiv:
5459 return simplifySDivInst(LHS, RHS, Q, MaxRecurse);
5460 case Instruction::UDiv:
5461 return simplifyUDivInst(LHS, RHS, Q, MaxRecurse);
5462 case Instruction::SRem:
5463 return simplifySRemInst(LHS, RHS, Q, MaxRecurse);
5464 case Instruction::URem:
5465 return simplifyURemInst(LHS, RHS, Q, MaxRecurse);
5466 case Instruction::Shl:
5467 return simplifyShlInst(LHS, RHS, false, false, Q, MaxRecurse);
5468 case Instruction::LShr:
5469 return simplifyLShrInst(LHS, RHS, false, Q, MaxRecurse);
5470 case Instruction::AShr:
5471 return simplifyAShrInst(LHS, RHS, false, Q, MaxRecurse);
5472 case Instruction::And:
5473 return simplifyAndInst(LHS, RHS, Q, MaxRecurse);
5474 case Instruction::Or:
5475 return simplifyOrInst(LHS, RHS, Q, MaxRecurse);
5476 case Instruction::Xor:
5477 return simplifyXorInst(LHS, RHS, Q, MaxRecurse);
5478 case Instruction::FAdd:
5479 return simplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
5480 case Instruction::FSub:
5481 return simplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
5482 case Instruction::FMul:
5483 return simplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
5484 case Instruction::FDiv:
5485 return simplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
5486 case Instruction::FRem:
5487 return simplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
5488 default:
5489 llvm_unreachable("Unexpected opcode")::llvm::llvm_unreachable_internal("Unexpected opcode", "llvm/lib/Analysis/InstructionSimplify.cpp"
, 5489)
;
5490 }
5491}
5492
5493/// Given operands for a BinaryOperator, see if we can fold the result.
5494/// If not, this returns null.
5495/// Try to use FastMathFlags when folding the result.
5496static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
5497 const FastMathFlags &FMF, const SimplifyQuery &Q,
5498 unsigned MaxRecurse) {
5499 switch (Opcode) {
5500 case Instruction::FAdd:
5501 return simplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse);
5502 case Instruction::FSub:
5503 return simplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
5504 case Instruction::FMul:
5505 return simplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
5506 case Instruction::FDiv:
5507 return simplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
5508 default:
5509 return simplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
5510 }
5511}
5512
5513Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
5514 const SimplifyQuery &Q) {
5515 return ::simplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit);
5516}
5517
5518Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
5519 FastMathFlags FMF, const SimplifyQuery &Q) {
5520 return ::simplifyBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit);
5521}
5522
5523/// Given operands for a CmpInst, see if we can fold the result.
5524static Value *simplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
5525 const SimplifyQuery &Q, unsigned MaxRecurse) {
5526 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
5527 return simplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
5528 return simplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse);
5529}
5530
5531Value *llvm::simplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
5532 const SimplifyQuery &Q) {
5533 return ::simplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
5534}
5535
5536static bool isIdempotent(Intrinsic::ID ID) {
5537 switch (ID) {
5538 default:
5539 return false;
5540
5541 // Unary idempotent: f(f(x)) = f(x)
5542 case Intrinsic::fabs:
5543 case Intrinsic::floor:
5544 case Intrinsic::ceil:
5545 case Intrinsic::trunc:
5546 case Intrinsic::rint:
5547 case Intrinsic::nearbyint:
5548 case Intrinsic::round:
5549 case Intrinsic::roundeven:
5550 case Intrinsic::canonicalize:
5551 return true;
5552 }
5553}
5554
5555/// Return true if the intrinsic rounds a floating-point value to an integral
5556/// floating-point value (not an integer type).
5557static bool removesFPFraction(Intrinsic::ID ID) {
5558 switch (ID) {
5559 default:
5560 return false;
5561
5562 case Intrinsic::floor:
5563 case Intrinsic::ceil:
5564 case Intrinsic::trunc:
5565 case Intrinsic::rint:
5566 case Intrinsic::nearbyint:
5567 case Intrinsic::round:
5568 case Intrinsic::roundeven:
5569 return true;
5570 }
5571}
5572
5573static Value *simplifyRelativeLoad(Constant *Ptr, Constant *Offset,
5574 const DataLayout &DL) {
5575 GlobalValue *PtrSym;
5576 APInt PtrOffset;
5577 if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL))
5578 return nullptr;
5579
5580 Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext());
5581 Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
5582 Type *Int32PtrTy = Int32Ty->getPointerTo();
5583 Type *Int64Ty = Type::getInt64Ty(Ptr->getContext());
5584
5585 auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
5586 if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64)
5587 return nullptr;
5588
5589 uint64_t OffsetInt = OffsetConstInt->getSExtValue();
5590 if (OffsetInt % 4 != 0)
5591 return nullptr;
5592
5593 Constant *C = ConstantExpr::getGetElementPtr(
5594 Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy),
5595 ConstantInt::get(Int64Ty, OffsetInt / 4));
5596 Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL);
5597 if (!Loaded)
5598 return nullptr;
5599
5600 auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
5601 if (!LoadedCE)
5602 return nullptr;
5603
5604 if (LoadedCE->getOpcode() == Instruction::Trunc) {
5605 LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
5606 if (!LoadedCE)
5607 return nullptr;
5608 }
5609
5610 if (LoadedCE->getOpcode() != Instruction::Sub)
5611 return nullptr;
5612
5613 auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
5614 if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
5615 return nullptr;
5616 auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
5617
5618 Constant *LoadedRHS = LoadedCE->getOperand(1);
5619 GlobalValue *LoadedRHSSym;
5620 APInt LoadedRHSOffset;
5621 if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset,
5622 DL) ||
5623 PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
5624 return nullptr;
5625
5626 return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy);
5627}
5628
5629static Value *simplifyUnaryIntrinsic(Function *F, Value *Op0,
5630 const SimplifyQuery &Q) {
5631 // Idempotent functions return the same result when called repeatedly.
5632 Intrinsic::ID IID = F->getIntrinsicID();
5633 if (isIdempotent(IID))
5634 if (auto *II = dyn_cast<IntrinsicInst>(Op0))
5635 if (II->getIntrinsicID() == IID)
5636 return II;
5637
5638 if (removesFPFraction(IID)) {
5639 // Converting from int or calling a rounding function always results in a
5640 // finite integral number or infinity. For those inputs, rounding functions
5641 // always return the same value, so the (2nd) rounding is eliminated. Ex:
5642 // floor (sitofp x) -> sitofp x
5643 // round (ceil x) -> ceil x
5644 auto *II = dyn_cast<IntrinsicInst>(Op0);
5645 if ((II && removesFPFraction(II->getIntrinsicID())) ||
5646 match(Op0, m_SIToFP(m_Value())) || match(Op0, m_UIToFP(m_Value())))
5647 return Op0;
5648 }
5649
5650 Value *X;
5651 switch (IID) {
5652 case Intrinsic::fabs:
5653 if (SignBitMustBeZero(Op0, Q.TLI))
5654 return Op0;
5655 break;
5656 case Intrinsic::bswap:
5657 // bswap(bswap(x)) -> x
5658 if (match(Op0, m_BSwap(m_Value(X))))
5659 return X;
5660 break;
5661 case Intrinsic::bitreverse:
5662 // bitreverse(bitreverse(x)) -> x
5663 if (match(Op0, m_BitReverse(m_Value(X))))
5664 return X;
5665 break;
5666 case Intrinsic::ctpop: {
5667 // If everything but the lowest bit is zero, that bit is the pop-count. Ex:
5668 // ctpop(and X, 1) --> and X, 1
5669 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
5670 if (MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, BitWidth - 1),
5671 Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
5672 return Op0;
5673 break;
5674 }
5675 case Intrinsic::exp:
5676 // exp(log(x)) -> x
5677 if (Q.CxtI->hasAllowReassoc() &&
5678 match(Op0, m_Intrinsic<Intrinsic::log>(m_Value(X))))
5679 return X;
5680 break;
5681 case Intrinsic::exp2:
5682 // exp2(log2(x)) -> x
5683 if (Q.CxtI->hasAllowReassoc() &&
5684 match(Op0, m_Intrinsic<Intrinsic::log2>(m_Value(X))))
5685 return X;
5686 break;
5687 case Intrinsic::log:
5688 // log(exp(x)) -> x
5689 if (Q.CxtI->hasAllowReassoc() &&
5690 match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))))
5691 return X;
5692 break;
5693 case Intrinsic::log2:
5694 // log2(exp2(x)) -> x
5695 if (Q.CxtI->hasAllowReassoc() &&
5696 (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) ||
5697 match(Op0,
5698 m_Intrinsic<Intrinsic::pow>(m_SpecificFP(2.0), m_Value(X)))))
5699 return X;
5700 break;
5701 case Intrinsic::log10:
5702 // log10(pow(10.0, x)) -> x
5703 if (Q.CxtI->hasAllowReassoc() &&
5704 match(Op0, m_Intrinsic<Intrinsic::pow>(m_SpecificFP(10.0), m_Value(X))))
5705 return X;
5706 break;
5707 case Intrinsic::experimental_vector_reverse:
5708 // experimental.vector.reverse(experimental.vector.reverse(x)) -> x
5709 if (match(Op0,
5710 m_Intrinsic<Intrinsic::experimental_vector_reverse>(m_Value(X))))
5711 return X;
5712 // experimental.vector.reverse(splat(X)) -> splat(X)
5713 if (isSplatValue(Op0))
5714 return Op0;
5715 break;
5716 default:
5717 break;
5718 }
5719
5720 return nullptr;
5721}
5722
5723/// Given a min/max intrinsic, see if it can be removed based on having an
5724/// operand that is another min/max intrinsic with shared operand(s). The caller
5725/// is expected to swap the operand arguments to handle commutation.
5726static Value *foldMinMaxSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1) {
5727 Value *X, *Y;
5728 if (!match(Op0, m_MaxOrMin(m_Value(X), m_Value(Y))))
5729 return nullptr;
5730
5731 auto *MM0 = dyn_cast<IntrinsicInst>(Op0);
5732 if (!MM0)
5733 return nullptr;
5734 Intrinsic::ID IID0 = MM0->getIntrinsicID();
5735
5736 if (Op1 == X || Op1 == Y ||
5737 match(Op1, m_c_MaxOrMin(m_Specific(X), m_Specific(Y)))) {
5738 // max (max X, Y), X --> max X, Y
5739 if (IID0 == IID)
5740 return MM0;
5741 // max (min X, Y), X --> X
5742 if (IID0 == getInverseMinMaxIntrinsic(IID))
5743 return Op1;
5744 }
5745 return nullptr;
5746}
5747
5748static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
5749 const SimplifyQuery &Q) {
5750 Intrinsic::ID IID = F->getIntrinsicID();
5751 Type *ReturnType = F->getReturnType();
5752 unsigned BitWidth = ReturnType->getScalarSizeInBits();
5753 switch (IID) {
5754 case Intrinsic::abs:
5755 // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here.
5756 // It is always ok to pick the earlier abs. We'll just lose nsw if its only
5757 // on the outer abs.
5758 if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(), m_Value())))
5759 return Op0;
5760 break;
5761
5762 case Intrinsic::cttz: {
5763 Value *X;
5764 if (match(Op0, m_Shl(m_One(), m_Value(X))))
5765 return X;
5766 break;
5767 }
5768 case Intrinsic::ctlz: {
5769 Value *X;
5770 if (match(Op0, m_LShr(m_Negative(), m_Value(X))))
5771 return X;
5772 if (match(Op0, m_AShr(m_Negative(), m_Value())))
5773 return Constant::getNullValue(ReturnType);
5774 break;
5775 }
5776 case Intrinsic::smax:
5777 case Intrinsic::smin:
5778 case Intrinsic::umax:
5779 case Intrinsic::umin: {
5780 // If the arguments are the same, this is a no-op.
5781 if (Op0 == Op1)
5782 return Op0;
5783
5784 // Canonicalize constant operand as Op1.
5785 if (isa<Constant>(Op0))
5786 std::swap(Op0, Op1);
5787
5788 // Assume undef is the limit value.
5789 if (Q.isUndefValue(Op1))
5790 return ConstantInt::get(
5791 ReturnType, MinMaxIntrinsic::getSaturationPoint(IID, BitWidth));
5792
5793 const APInt *C;
5794 if (match(Op1, m_APIntAllowUndef(C))) {
5795 // Clamp to limit value. For example:
5796 // umax(i8 %x, i8 255) --> 255
5797 if (*C == MinMaxIntrinsic::getSaturationPoint(IID, BitWidth))
5798 return ConstantInt::get(ReturnType, *C);
5799
5800 // If the constant op is the opposite of the limit value, the other must
5801 // be larger/smaller or equal. For example:
5802 // umin(i8 %x, i8 255) --> %x
5803 if (*C == MinMaxIntrinsic::getSaturationPoint(
5804 getInverseMinMaxIntrinsic(IID), BitWidth))
5805 return Op0;
5806
5807 // Remove nested call if constant operands allow it. Example:
5808 // max (max X, 7), 5 -> max X, 7
5809 auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0);
5810 if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
5811 // TODO: loosen undef/splat restrictions for vector constants.
5812 Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1);
5813 const APInt *InnerC;
5814 if ((match(M00, m_APInt(InnerC)) || match(M01, m_APInt(InnerC))) &&
5815 ICmpInst::compare(*InnerC, *C,
5816 ICmpInst::getNonStrictPredicate(
5817 MinMaxIntrinsic::getPredicate(IID))))
5818 return Op0;
5819 }
5820 }
5821
5822 if (Value *V = foldMinMaxSharedOp(IID, Op0, Op1))
5823 return V;
5824 if (Value *V = foldMinMaxSharedOp(IID, Op1, Op0))
5825 return V;
5826
5827 ICmpInst::Predicate Pred =
5828 ICmpInst::getNonStrictPredicate(MinMaxIntrinsic::getPredicate(IID));
5829 if (isICmpTrue(Pred, Op0, Op1, Q.getWithoutUndef(), RecursionLimit))
5830 return Op0;
5831 if (isICmpTrue(Pred, Op1, Op0, Q.getWithoutUndef(), RecursionLimit))
5832 return Op1;
5833
5834 if (Optional<bool> Imp =
5835 isImpliedByDomCondition(Pred, Op0, Op1, Q.CxtI, Q.DL))
5836 return *Imp ? Op0 : Op1;
5837 if (Optional<bool> Imp =
5838 isImpliedByDomCondition(Pred, Op1, Op0, Q.CxtI, Q.DL))
5839 return *Imp ? Op1 : Op0;
5840
5841 break;
5842 }
5843 case Intrinsic::usub_with_overflow:
5844 case Intrinsic::ssub_with_overflow:
5845 // X - X -> { 0, false }
5846 // X - undef -> { 0, false }
5847 // undef - X -> { 0, false }
5848 if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
5849 return Constant::getNullValue(ReturnType);
5850 break;
5851 case Intrinsic::uadd_with_overflow:
5852 case Intrinsic::sadd_with_overflow:
5853 // X + undef -> { -1, false }
5854 // undef + x -> { -1, false }
5855 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)) {
5856 return ConstantStruct::get(
5857 cast<StructType>(ReturnType),
5858 {Constant::getAllOnesValue(ReturnType->getStructElementType(0)),
5859 Constant::getNullValue(ReturnType->getStructElementType(1))});
5860 }
5861 break;
5862 case Intrinsic::umul_with_overflow:
5863 case Intrinsic::smul_with_overflow:
5864 // 0 * X -> { 0, false }
5865 // X * 0 -> { 0, false }
5866 if (match(Op0, m_Zero()) || match(Op1, m_Zero()))
5867 return Constant::getNullValue(ReturnType);
5868 // undef * X -> { 0, false }
5869 // X * undef -> { 0, false }
5870 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
5871 return Constant::getNullValue(ReturnType);
5872 break;
5873 case Intrinsic::uadd_sat:
5874 // sat(MAX + X) -> MAX
5875 // sat(X + MAX) -> MAX
5876 if (match(Op0, m_AllOnes()) || match(Op1, m_AllOnes()))
5877 return Constant::getAllOnesValue(ReturnType);
5878 [[fallthrough]];
5879 case Intrinsic::sadd_sat:
5880 // sat(X + undef) -> -1
5881 // sat(undef + X) -> -1
5882 // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
5883 // For signed: Assume undef is ~X, in which case X + ~X = -1.
5884 if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
5885 return Constant::getAllOnesValue(ReturnType);
5886
5887 // X + 0 -> X
5888 if (match(Op1, m_Zero()))
5889 return Op0;
5890 // 0 + X -> X
5891 if (match(Op0, m_Zero()))
5892 return Op1;
5893 break;
5894 case Intrinsic::usub_sat:
5895 // sat(0 - X) -> 0, sat(X - MAX) -> 0
5896 if (match(Op0, m_Zero()) || match(Op1, m_AllOnes()))
5897 return Constant::getNullValue(ReturnType);
5898 [[fallthrough]];
5899 case Intrinsic::ssub_sat:
5900 // X - X -> 0, X - undef -> 0, undef - X -> 0
5901 if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
5902 return Constant::getNullValue(ReturnType);
5903 // X - 0 -> X
5904 if (match(Op1, m_Zero()))
5905 return Op0;
5906 break;
5907 case Intrinsic::load_relative:
5908 if (auto *C0 = dyn_cast<Constant>(Op0))
5909 if (auto *C1 = dyn_cast<Constant>(Op1))
5910 return simplifyRelativeLoad(C0, C1, Q.DL);
5911 break;
5912 case Intrinsic::powi:
5913 if (auto *Power = dyn_cast<ConstantInt>(Op1)) {
5914 // powi(x, 0) -> 1.0
5915 if (Power->isZero())
5916 return ConstantFP::get(Op0->getType(), 1.0);
5917 // powi(x, 1) -> x
5918 if (Power->isOne())
5919 return Op0;
5920 }
5921 break;
5922 case Intrinsic::copysign:
5923 // copysign X, X --> X
5924 if (Op0 == Op1)
5925 return Op0;
5926 // copysign -X, X --> X
5927 // copysign X, -X --> -X
5928 if (match(Op0, m_FNeg(m_Specific(Op1))) ||
5929 match(Op1, m_FNeg(m_Specific(Op0))))
5930 return Op1;
5931 break;
5932 case Intrinsic::maxnum:
5933 case Intrinsic::minnum:
5934 case Intrinsic::maximum:
5935 case Intrinsic::minimum: {
5936 // If the arguments are the same, this is a no-op.
5937 if (Op0 == Op1)
5938 return Op0;
5939
5940 // Canonicalize constant operand as Op1.
5941 if (isa<Constant>(Op0))
5942 std::swap(Op0, Op1);
5943
5944 // If an argument is undef, return the other argument.
5945 if (Q.isUndefValue(Op1))
5946 return Op0;
5947
5948 bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
5949 bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum;
5950
5951 // minnum(X, nan) -> X
5952 // maxnum(X, nan) -> X
5953 // minimum(X, nan) -> nan
5954 // maximum(X, nan) -> nan
5955 if (match(Op1, m_NaN()))
5956 return PropagateNaN ? propagateNaN(cast<Constant>(Op1)) : Op0;
5957
5958 // In the following folds, inf can be replaced with the largest finite
5959 // float, if the ninf flag is set.
5960 const APFloat *C;
5961 if (match(Op1, m_APFloat(C)) &&
5962 (C->isInfinity() || (Q.CxtI->hasNoInfs() && C->isLargest()))) {