/data/zyw/opt-ci/actions-runner/_work/llvm-opt-benchmark/llvm-opt-benchmark/llvm/llvm-project/llvm/lib/Analysis/InstructionSimplify.cpp
Line | Count | Source |
1 | | //===- InstructionSimplify.cpp - Fold instruction operands ----------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements routines for folding instructions into simpler forms |
10 | | // that do not require creating new instructions. This does constant folding |
11 | | // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either |
12 | | // returning a constant ("and i32 %x, 0" -> "0") or an already existing value |
13 | | // ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been |
14 | | // simplified: This is usually true and assuming it simplifies the logic (if |
15 | | // they have not been simplified then results are correct but maybe suboptimal). |
16 | | // |
17 | | //===----------------------------------------------------------------------===// |
18 | | |
19 | | #include "llvm/Analysis/InstructionSimplify.h" |
20 | | |
21 | | #include "llvm/ADT/STLExtras.h" |
22 | | #include "llvm/ADT/SetVector.h" |
23 | | #include "llvm/ADT/Statistic.h" |
24 | | #include "llvm/Analysis/AliasAnalysis.h" |
25 | | #include "llvm/Analysis/AssumptionCache.h" |
26 | | #include "llvm/Analysis/CaptureTracking.h" |
27 | | #include "llvm/Analysis/CmpInstAnalysis.h" |
28 | | #include "llvm/Analysis/ConstantFolding.h" |
29 | | #include "llvm/Analysis/FloatingPointPredicateUtils.h" |
30 | | #include "llvm/Analysis/InstSimplifyFolder.h" |
31 | | #include "llvm/Analysis/Loads.h" |
32 | | #include "llvm/Analysis/LoopAnalysisManager.h" |
33 | | #include "llvm/Analysis/MemoryBuiltins.h" |
34 | | #include "llvm/Analysis/OverflowInstAnalysis.h" |
35 | | #include "llvm/Analysis/TargetLibraryInfo.h" |
36 | | #include "llvm/Analysis/ValueTracking.h" |
37 | | #include "llvm/Analysis/VectorUtils.h" |
38 | | #include "llvm/IR/ConstantRange.h" |
39 | | #include "llvm/IR/DataLayout.h" |
40 | | #include "llvm/IR/Dominators.h" |
41 | | #include "llvm/IR/InstrTypes.h" |
42 | | #include "llvm/IR/Instructions.h" |
43 | | #include "llvm/IR/Operator.h" |
44 | | #include "llvm/IR/PatternMatch.h" |
45 | | #include "llvm/IR/Statepoint.h" |
46 | | #include "llvm/Support/KnownBits.h" |
47 | | #include "llvm/Support/KnownFPClass.h" |
48 | | #include <algorithm> |
49 | | #include <optional> |
50 | | using namespace llvm; |
51 | | using namespace llvm::PatternMatch; |
52 | | |
53 | | #define DEBUG_TYPE "instsimplify" |
54 | | |
55 | | enum { RecursionLimit = 3 }; |
56 | | |
57 | | STATISTIC(NumExpand, "Number of expansions"); |
58 | | STATISTIC(NumReassoc, "Number of reassociations"); |
59 | | |
60 | | static Value *simplifyAndInst(Value *, Value *, const SimplifyQuery &, |
61 | | unsigned); |
62 | | static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned); |
63 | | static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &, |
64 | | const SimplifyQuery &, unsigned); |
65 | | static Value *simplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &, |
66 | | unsigned); |
67 | | static Value *simplifyBinOp(unsigned, Value *, Value *, const FastMathFlags &, |
68 | | const SimplifyQuery &, unsigned); |
69 | | static Value *simplifyCmpInst(CmpPredicate, Value *, Value *, |
70 | | const SimplifyQuery &, unsigned); |
71 | | static Value *simplifyICmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, |
72 | | const SimplifyQuery &Q, unsigned MaxRecurse); |
73 | | static Value *simplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned); |
74 | | static Value *simplifyXorInst(Value *, Value *, const SimplifyQuery &, |
75 | | unsigned); |
76 | | static Value *simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &, |
77 | | unsigned); |
78 | | static Value *simplifyGEPInst(Type *, Value *, ArrayRef<Value *>, |
79 | | GEPNoWrapFlags, const SimplifyQuery &, unsigned); |
80 | | static Value *simplifySelectInst(Value *, Value *, Value *, |
81 | | const SimplifyQuery &, unsigned); |
82 | | static Value *simplifyInstructionWithOperands(Instruction *I, |
83 | | ArrayRef<Value *> NewOps, |
84 | | const SimplifyQuery &SQ, |
85 | | unsigned MaxRecurse); |
86 | | |
87 | | /// For a boolean type or a vector of boolean type, return false or a vector |
88 | | /// with every element false. |
89 | 8.54M | static Constant *getFalse(Type *Ty) { return ConstantInt::getFalse(Ty); } |
90 | | |
91 | | /// For a boolean type or a vector of boolean type, return true or a vector |
92 | | /// with every element true. |
93 | 12.7M | static Constant *getTrue(Type *Ty) { return ConstantInt::getTrue(Ty); } |
94 | | |
95 | | /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? |
96 | 9.28M | static bool isSameCompare(Value *V, CmpPredicate Pred, Value *LHS, Value *RHS) { |
97 | 9.28M | CmpInst *Cmp = dyn_cast<CmpInst>(V); |
98 | 9.28M | if (!Cmp) |
99 | 1.15M | return false; |
100 | 8.13M | CmpInst::Predicate CPred = Cmp->getPredicate(); |
101 | 8.13M | Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1); |
102 | 8.13M | if (CPred == Pred && CLHS == LHS1.59M && CRHS == RHS197k ) |
103 | 14.7k | return true; |
104 | 8.11M | return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS1.57M && |
105 | 8.11M | CRHS == LHS7.60k ; |
106 | 8.13M | } |
107 | | |
108 | | /// Simplify comparison with true or false branch of select: |
109 | | /// %sel = select i1 %cond, i32 %tv, i32 %fv |
110 | | /// %cmp = icmp sle i32 %sel, %rhs |
111 | | /// Compose new comparison by substituting %sel with either %tv or %fv |
112 | | /// and see if it simplifies. |
113 | | static Value *simplifyCmpSelCase(CmpPredicate Pred, Value *LHS, Value *RHS, |
114 | | Value *Cond, const SimplifyQuery &Q, |
115 | 13.8M | unsigned MaxRecurse, Constant *TrueOrFalse) { |
116 | 13.8M | Value *SimplifiedCmp = simplifyCmpInst(Pred, LHS, RHS, Q, MaxRecurse); |
117 | 13.8M | if (SimplifiedCmp == Cond) { |
118 | | // %cmp simplified to the select condition (%cond). |
119 | 5.02k | return TrueOrFalse; |
120 | 13.8M | } else if (!SimplifiedCmp && isSameCompare(Cond, Pred, LHS, RHS)9.28M ) { |
121 | | // It didn't simplify. However, if composed comparison is equivalent |
122 | | // to the select condition (%cond) then we can replace it. |
123 | 18.0k | return TrueOrFalse; |
124 | 18.0k | } |
125 | 13.8M | return SimplifiedCmp; |
126 | 13.8M | } |
127 | | |
128 | | /// Simplify comparison with true branch of select |
129 | | static Value *simplifyCmpSelTrueCase(CmpPredicate Pred, Value *LHS, Value *RHS, |
130 | | Value *Cond, const SimplifyQuery &Q, |
131 | 10.1M | unsigned MaxRecurse) { |
132 | 10.1M | return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse, |
133 | 10.1M | getTrue(Cond->getType())); |
134 | 10.1M | } |
135 | | |
136 | | /// Simplify comparison with false branch of select |
137 | | static Value *simplifyCmpSelFalseCase(CmpPredicate Pred, Value *LHS, Value *RHS, |
138 | | Value *Cond, const SimplifyQuery &Q, |
139 | 3.75M | unsigned MaxRecurse) { |
140 | 3.75M | return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse, |
141 | 3.75M | getFalse(Cond->getType())); |
142 | 3.75M | } |
143 | | |
144 | | /// We know comparison with both branches of select can be simplified, but they |
145 | | /// are not equal. This routine handles some logical simplifications. |
146 | | static Value *handleOtherCmpSelSimplifications(Value *TCmp, Value *FCmp, |
147 | | Value *Cond, |
148 | | const SimplifyQuery &Q, |
149 | 833k | unsigned MaxRecurse) { |
150 | | // If the false value simplified to false, then the result of the compare |
151 | | // is equal to "Cond && TCmp". This also catches the case when the false |
152 | | // value simplified to false and the true value to true, returning "Cond". |
153 | | // Folding select to and/or isn't poison-safe in general; impliesPoison |
154 | | // checks whether folding it does not convert a well-defined value into |
155 | | // poison. |
156 | 833k | if (match(FCmp, m_Zero()) && impliesPoison(TCmp, Cond)377k ) |
157 | 376k | if (Value *V = simplifyAndInst(Cond, TCmp, Q, MaxRecurse)) |
158 | 375k | return V; |
159 | | // If the true value simplified to true, then the result of the compare |
160 | | // is equal to "Cond || FCmp". |
161 | 457k | if (match(TCmp, m_One()) && impliesPoison(FCmp, Cond)2.25k ) |
162 | 1.95k | if (Value *V = simplifyOrInst(Cond, FCmp, Q, MaxRecurse)) |
163 | 457 | return V; |
164 | | // Finally, if the false value simplified to true and the true value to |
165 | | // false, then the result of the compare is equal to "!Cond". |
166 | 457k | if (match(FCmp, m_One()) && match(TCmp, m_Zero())447k ) |
167 | 444k | if (Value *V = simplifyXorInst( |
168 | 444k | Cond, Constant::getAllOnesValue(Cond->getType()), Q, MaxRecurse)) |
169 | 115 | return V; |
170 | 457k | return nullptr; |
171 | 457k | } |
172 | | |
173 | | /// Does the given value dominate the specified phi node? |
174 | 104M | static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
175 | 104M | Instruction *I = dyn_cast<Instruction>(V); |
176 | 104M | if (!I) |
177 | | // Arguments and constants dominate all instructions. |
178 | 61.9M | return true; |
179 | | |
180 | | // If we have a DominatorTree then do a precise test. |
181 | 42.6M | if (DT) |
182 | 36.9M | return DT->dominates(I, P); |
183 | | |
184 | | // Otherwise, if the instruction is in the entry block and is not an invoke, |
185 | | // then it obviously dominates all phi nodes. |
186 | 5.70M | if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I)717k && |
187 | 5.70M | !isa<CallBrInst>(I)715k ) |
188 | 715k | return true; |
189 | | |
190 | 4.98M | return false; |
191 | 5.70M | } |
192 | | |
193 | | /// Try to simplify a binary operator of form "V op OtherOp" where V is |
194 | | /// "(B0 opex B1)" by distributing 'op' across 'opex' as |
195 | | /// "(B0 op OtherOp) opex (B1 op OtherOp)". |
196 | | static Value *expandBinOp(Instruction::BinaryOps Opcode, Value *V, |
197 | | Value *OtherOp, Instruction::BinaryOps OpcodeToExpand, |
198 | 612M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
199 | 612M | auto *B = dyn_cast<BinaryOperator>(V); |
200 | 612M | if (!B || B->getOpcode() != OpcodeToExpand159M ) |
201 | 570M | return nullptr; |
202 | 41.6M | Value *B0 = B->getOperand(0), *B1 = B->getOperand(1); |
203 | 41.6M | Value *L = |
204 | 41.6M | simplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(), MaxRecurse); |
205 | 41.6M | if (!L) |
206 | 40.6M | return nullptr; |
207 | 986k | Value *R = |
208 | 986k | simplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(), MaxRecurse); |
209 | 986k | if (!R) |
210 | 684k | return nullptr; |
211 | | |
212 | | // Does the expanded pair of binops simplify to the existing binop? |
213 | 302k | if ((L == B0 && R == B1127k ) || |
214 | 302k | (232k Instruction::isCommutative(OpcodeToExpand)232k && L == B1232k && R == B0514 )) { |
215 | 69.4k | ++NumExpand; |
216 | 69.4k | return B; |
217 | 69.4k | } |
218 | | |
219 | | // Otherwise, return "L op' R" if it simplifies. |
220 | 232k | Value *S = simplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse); |
221 | 232k | if (!S) |
222 | 10.2k | return nullptr; |
223 | | |
224 | 222k | ++NumExpand; |
225 | 222k | return S; |
226 | 232k | } |
227 | | |
228 | | /// Try to simplify binops of form "A op (B op' C)" or the commuted variant by |
229 | | /// distributing op over op'. |
230 | | static Value *expandCommutativeBinOp(Instruction::BinaryOps Opcode, Value *L, |
231 | | Value *R, |
232 | | Instruction::BinaryOps OpcodeToExpand, |
233 | | const SimplifyQuery &Q, |
234 | 326M | unsigned MaxRecurse) { |
235 | | // Recursion is always used, so bail out at once if we already hit the limit. |
236 | 326M | if (!MaxRecurse--) |
237 | 20.4M | return nullptr; |
238 | | |
239 | 306M | if (Value *V = expandBinOp(Opcode, L, R, OpcodeToExpand, Q, MaxRecurse)) |
240 | 291k | return V; |
241 | 305M | if (Value *V = expandBinOp(Opcode, R, L, OpcodeToExpand, Q, MaxRecurse)) |
242 | 326 | return V; |
243 | 305M | return nullptr; |
244 | 305M | } |
245 | | |
246 | | /// Generic simplifications for associative binary operations. |
247 | | /// Returns the simpler value, or null if none was found. |
248 | | static Value *simplifyAssociativeBinOp(Instruction::BinaryOps Opcode, |
249 | | Value *LHS, Value *RHS, |
250 | | const SimplifyQuery &Q, |
251 | 485M | unsigned MaxRecurse) { |
252 | 485M | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
253 | | |
254 | | // Recursion is always used, so bail out at once if we already hit the limit. |
255 | 485M | if (!MaxRecurse--) |
256 | 35.6M | return nullptr; |
257 | | |
258 | 449M | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
259 | 449M | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
260 | | |
261 | | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
262 | 449M | if (Op0 && Op0->getOpcode() == Opcode143M ) { |
263 | 29.5M | Value *A = Op0->getOperand(0); |
264 | 29.5M | Value *B = Op0->getOperand(1); |
265 | 29.5M | Value *C = RHS; |
266 | | |
267 | | // Does "B op C" simplify? |
268 | 29.5M | if (Value *V = simplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { |
269 | | // It does! Return "A op V" if it simplifies or is already available. |
270 | | // If V equals B then "A op V" is just the LHS. |
271 | 5.72M | if (V == B) |
272 | 332k | return LHS; |
273 | | // Otherwise return "A op V" if it simplifies. |
274 | 5.38M | if (Value *W = simplifyBinOp(Opcode, A, V, Q, MaxRecurse)) { |
275 | 537k | ++NumReassoc; |
276 | 537k | return W; |
277 | 537k | } |
278 | 5.38M | } |
279 | 29.5M | } |
280 | | |
281 | | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
282 | 449M | if (Op1 && Op1->getOpcode() == Opcode83.5M ) { |
283 | 18.1M | Value *A = LHS; |
284 | 18.1M | Value *B = Op1->getOperand(0); |
285 | 18.1M | Value *C = Op1->getOperand(1); |
286 | | |
287 | | // Does "A op B" simplify? |
288 | 18.1M | if (Value *V = simplifyBinOp(Opcode, A, B, Q, MaxRecurse)) { |
289 | | // It does! Return "V op C" if it simplifies or is already available. |
290 | | // If V equals B then "V op C" is just the RHS. |
291 | 53.9k | if (V == B) |
292 | 2.26k | return RHS; |
293 | | // Otherwise return "V op C" if it simplifies. |
294 | 51.6k | if (Value *W = simplifyBinOp(Opcode, V, C, Q, MaxRecurse)) { |
295 | 10.4k | ++NumReassoc; |
296 | 10.4k | return W; |
297 | 10.4k | } |
298 | 51.6k | } |
299 | 18.1M | } |
300 | | |
301 | | // The remaining transforms require commutativity as well as associativity. |
302 | 449M | if (!Instruction::isCommutative(Opcode)) |
303 | 0 | return nullptr; |
304 | | |
305 | | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
306 | 449M | if (Op0 && Op0->getOpcode() == Opcode142M ) { |
307 | 28.6M | Value *A = Op0->getOperand(0); |
308 | 28.6M | Value *B = Op0->getOperand(1); |
309 | 28.6M | Value *C = RHS; |
310 | | |
311 | | // Does "C op A" simplify? |
312 | 28.6M | if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
313 | | // It does! Return "V op B" if it simplifies or is already available. |
314 | | // If V equals A then "V op B" is just the LHS. |
315 | 130k | if (V == A) |
316 | 21.6k | return LHS; |
317 | | // Otherwise return "V op B" if it simplifies. |
318 | 108k | if (Value *W = simplifyBinOp(Opcode, V, B, Q, MaxRecurse)) { |
319 | 11.8k | ++NumReassoc; |
320 | 11.8k | return W; |
321 | 11.8k | } |
322 | 108k | } |
323 | 28.6M | } |
324 | | |
325 | | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
326 | 449M | if (Op1 && Op1->getOpcode() == Opcode83.5M ) { |
327 | 18.1M | Value *A = LHS; |
328 | 18.1M | Value *B = Op1->getOperand(0); |
329 | 18.1M | Value *C = Op1->getOperand(1); |
330 | | |
331 | | // Does "C op A" simplify? |
332 | 18.1M | if (Value *V = simplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { |
333 | | // It does! Return "B op V" if it simplifies or is already available. |
334 | | // If V equals C then "B op V" is just the RHS. |
335 | 58.5k | if (V == C) |
336 | 1.11k | return RHS; |
337 | | // Otherwise return "B op V" if it simplifies. |
338 | 57.4k | if (Value *W = simplifyBinOp(Opcode, B, V, Q, MaxRecurse)) { |
339 | 12.4k | ++NumReassoc; |
340 | 12.4k | return W; |
341 | 12.4k | } |
342 | 57.4k | } |
343 | 18.1M | } |
344 | | |
345 | 449M | return nullptr; |
346 | 449M | } |
347 | | |
348 | | /// In the case of a binary operation with a select instruction as an operand, |
349 | | /// try to simplify the binop by seeing whether evaluating it on both branches |
350 | | /// of the select results in the same value. Returns the common value if so, |
351 | | /// otherwise returns null. |
352 | | static Value *threadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS, |
353 | | Value *RHS, const SimplifyQuery &Q, |
354 | 8.80M | unsigned MaxRecurse) { |
355 | | // Recursion is always used, so bail out at once if we already hit the limit. |
356 | 8.80M | if (!MaxRecurse--) |
357 | 1.03M | return nullptr; |
358 | | |
359 | 7.77M | SelectInst *SI; |
360 | 7.77M | if (isa<SelectInst>(LHS)) { |
361 | 6.15M | SI = cast<SelectInst>(LHS); |
362 | 6.15M | } else { |
363 | 1.62M | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
364 | 1.62M | SI = cast<SelectInst>(RHS); |
365 | 1.62M | } |
366 | | |
367 | | // Evaluate the BinOp on the true and false branches of the select. |
368 | 7.77M | Value *TV; |
369 | 7.77M | Value *FV; |
370 | 7.77M | if (SI == LHS) { |
371 | 6.15M | TV = simplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse); |
372 | 6.15M | FV = simplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse); |
373 | 6.15M | } else { |
374 | 1.62M | TV = simplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse); |
375 | 1.62M | FV = simplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse); |
376 | 1.62M | } |
377 | | |
378 | | // If they simplified to the same value, then return the common value. |
379 | | // If they both failed to simplify then return null. |
380 | 7.77M | if (TV == FV) |
381 | 1.93M | return TV; |
382 | | |
383 | | // If one branch simplified to undef, return the other one. |
384 | 5.84M | if (TV && Q.isUndefValue(TV)3.83M ) |
385 | 180 | return FV; |
386 | 5.84M | if (FV && Q.isUndefValue(FV)3.07M ) |
387 | 893 | return TV; |
388 | | |
389 | | // If applying the operation did not change the true and false select values, |
390 | | // then the result of the binop is the select itself. |
391 | 5.84M | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()249k ) |
392 | 54.0k | return SI; |
393 | | |
394 | | // If one branch simplified and the other did not, and the simplified |
395 | | // value is equal to the unsimplified one, return the simplified value. |
396 | | // For example, select (cond, X, X & Z) & Z -> X & Z. |
397 | 5.79M | if ((FV && !TV3.01M ) || (3.77M TV3.77M && !FV3.77M )) { |
398 | | // Check that the simplified value has the form "X op Y" where "op" is the |
399 | | // same as the original operation. |
400 | 4.79M | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV2.01M : TV2.77M ); |
401 | 4.79M | if (Simplified && Simplified->getOpcode() == unsigned(Opcode)2.31M && |
402 | 4.79M | !Simplified->hasPoisonGeneratingFlags()435k ) { |
403 | | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
404 | | // We already know that "op" is the same as for the simplified value. See |
405 | | // if the operands match too. If so, return the simplified value. |
406 | 82.5k | Value *UnsimplifiedBranch = FV ? SI->getTrueValue()37.9k : SI->getFalseValue()44.6k ; |
407 | 82.5k | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch31.7k : LHS50.8k ; |
408 | 82.5k | Value *UnsimplifiedRHS = SI == LHS ? RHS31.7k : UnsimplifiedBranch50.8k ; |
409 | 82.5k | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
410 | 82.5k | Simplified->getOperand(1) == UnsimplifiedRHS5.96k ) |
411 | 2.21k | return Simplified; |
412 | 80.3k | if (Simplified->isCommutative() && |
413 | 80.3k | Simplified->getOperand(1) == UnsimplifiedLHS79.0k && |
414 | 80.3k | Simplified->getOperand(0) == UnsimplifiedRHS12 ) |
415 | 11 | return Simplified; |
416 | 80.3k | } |
417 | 4.79M | } |
418 | | |
419 | 5.78M | return nullptr; |
420 | 5.79M | } |
421 | | |
422 | | /// In the case of a comparison with a select instruction, try to simplify the |
423 | | /// comparison by seeing whether both branches of the select result in the same |
424 | | /// value. Returns the common value if so, otherwise returns null. |
425 | | /// For example, if we have: |
426 | | /// %tmp = select i1 %cmp, i32 1, i32 2 |
427 | | /// %cmp1 = icmp sle i32 %tmp, 3 |
428 | | /// We can simplify %cmp1 to true, because both branches of select are |
429 | | /// less than 3. We compose new comparison by substituting %tmp with both |
430 | | /// branches of select and see if it can be simplified. |
431 | | static Value *threadCmpOverSelect(CmpPredicate Pred, Value *LHS, Value *RHS, |
432 | 10.9M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
433 | | // Recursion is always used, so bail out at once if we already hit the limit. |
434 | 10.9M | if (!MaxRecurse--) |
435 | 857k | return nullptr; |
436 | | |
437 | | // Make sure the select is on the LHS. |
438 | 10.1M | if (!isa<SelectInst>(LHS)) { |
439 | 1.95M | std::swap(LHS, RHS); |
440 | 1.95M | Pred = CmpInst::getSwappedPredicate(Pred); |
441 | 1.95M | } |
442 | 10.1M | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
443 | 10.1M | SelectInst *SI = cast<SelectInst>(LHS); |
444 | 10.1M | Value *Cond = SI->getCondition(); |
445 | 10.1M | Value *TV = SI->getTrueValue(); |
446 | 10.1M | Value *FV = SI->getFalseValue(); |
447 | | |
448 | | // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. |
449 | | // Does "cmp TV, RHS" simplify? |
450 | 10.1M | Value *TCmp = simplifyCmpSelTrueCase(Pred, TV, RHS, Cond, Q, MaxRecurse); |
451 | 10.1M | if (!TCmp) |
452 | 6.35M | return nullptr; |
453 | | |
454 | | // Does "cmp FV, RHS" simplify? |
455 | 3.75M | Value *FCmp = simplifyCmpSelFalseCase(Pred, FV, RHS, Cond, Q, MaxRecurse); |
456 | 3.75M | if (!FCmp) |
457 | 2.91M | return nullptr; |
458 | | |
459 | | // If both sides simplified to the same value, then use it as the result of |
460 | | // the original comparison. |
461 | 843k | if (TCmp == FCmp) |
462 | 10.0k | return TCmp; |
463 | | |
464 | | // The remaining cases only make sense if the select condition has the same |
465 | | // type as the result of the comparison, so bail out if this is not so. |
466 | 833k | if (Cond->getType()->isVectorTy() == RHS->getType()->isVectorTy()) |
467 | 833k | return handleOtherCmpSelSimplifications(TCmp, FCmp, Cond, Q, MaxRecurse); |
468 | | |
469 | 0 | return nullptr; |
470 | 833k | } |
471 | | |
472 | | /// In the case of a binary operation with an operand that is a PHI instruction, |
473 | | /// try to simplify the binop by seeing whether evaluating it on the incoming |
474 | | /// phi values yields the same result for every value. If so returns the common |
475 | | /// value, otherwise returns null. |
476 | | static Value *threadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS, |
477 | | Value *RHS, const SimplifyQuery &Q, |
478 | 30.7M | unsigned MaxRecurse) { |
479 | | // Recursion is always used, so bail out at once if we already hit the limit. |
480 | 30.7M | if (!MaxRecurse--) |
481 | 1.97M | return nullptr; |
482 | | |
483 | 28.7M | PHINode *PI; |
484 | 28.7M | if (isa<PHINode>(LHS)) { |
485 | 24.5M | PI = cast<PHINode>(LHS); |
486 | | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
487 | 24.5M | if (!valueDominatesPHI(RHS, PI, Q.DT)) |
488 | 3.51M | return nullptr; |
489 | 24.5M | } else { |
490 | 4.26M | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
491 | 4.26M | PI = cast<PHINode>(RHS); |
492 | | // Bail out if LHS and the phi may be mutually interdependent due to a loop. |
493 | 4.26M | if (!valueDominatesPHI(LHS, PI, Q.DT)) |
494 | 3.54M | return nullptr; |
495 | 4.26M | } |
496 | | |
497 | | // Evaluate the BinOp on the incoming phi values. |
498 | 21.7M | Value *CommonValue = nullptr; |
499 | 26.4M | for (Use &Incoming : PI->incoming_values()) { |
500 | | // If the incoming value is the phi node itself, it can safely be skipped. |
501 | 26.4M | if (Incoming == PI) |
502 | 9.92k | continue; |
503 | 26.4M | Instruction *InTI = PI->getIncomingBlock(Incoming)->getTerminator(); |
504 | 26.4M | Value *V = PI == LHS |
505 | 26.4M | ? simplifyBinOp(Opcode, Incoming, RHS, |
506 | 25.4M | Q.getWithInstruction(InTI), MaxRecurse) |
507 | 26.4M | : simplifyBinOp(Opcode, LHS, Incoming, |
508 | 1.03M | Q.getWithInstruction(InTI), MaxRecurse); |
509 | | // If the operation failed to simplify, or simplified to a different value |
510 | | // to previously, then give up. |
511 | 26.4M | if (!V || (5.09M CommonValue5.09M && V != CommonValue587k )) |
512 | 21.6M | return nullptr; |
513 | 4.74M | CommonValue = V; |
514 | 4.74M | } |
515 | | |
516 | 31.1k | return CommonValue; |
517 | 21.7M | } |
518 | | |
519 | | /// In the case of a comparison with a PHI instruction, try to simplify the |
520 | | /// comparison by seeing whether comparing with all of the incoming phi values |
521 | | /// yields the same result every time. If so returns the common result, |
522 | | /// otherwise returns null. |
523 | | static Value *threadCmpOverPHI(CmpPredicate Pred, Value *LHS, Value *RHS, |
524 | 73.1M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
525 | | // Recursion is always used, so bail out at once if we already hit the limit. |
526 | 73.1M | if (!MaxRecurse--) |
527 | 3.08M | return nullptr; |
528 | | |
529 | | // Make sure the phi is on the LHS. |
530 | 70.0M | if (!isa<PHINode>(LHS)) { |
531 | 7.23M | std::swap(LHS, RHS); |
532 | 7.23M | Pred = CmpInst::getSwappedPredicate(Pred); |
533 | 7.23M | } |
534 | 70.0M | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
535 | 70.0M | PHINode *PI = cast<PHINode>(LHS); |
536 | | |
537 | | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
538 | 70.0M | if (!valueDominatesPHI(RHS, PI, Q.DT)) |
539 | 15.2M | return nullptr; |
540 | | |
541 | | // Evaluate the BinOp on the incoming phi values. |
542 | 54.8M | Value *CommonValue = nullptr; |
543 | 69.3M | for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u14.5M ) { |
544 | 69.1M | Value *Incoming = PI->getIncomingValue(u); |
545 | 69.1M | Instruction *InTI = PI->getIncomingBlock(u)->getTerminator(); |
546 | | // If the incoming value is the phi node itself, it can safely be skipped. |
547 | 69.1M | if (Incoming == PI) |
548 | 140k | continue; |
549 | | // Change the context instruction to the "edge" that flows into the phi. |
550 | | // This is important because that is where incoming is actually "evaluated" |
551 | | // even though it is used later somewhere else. |
552 | 69.0M | Value *V = simplifyCmpInst(Pred, Incoming, RHS, Q.getWithInstruction(InTI), |
553 | 69.0M | MaxRecurse); |
554 | | // If the operation failed to simplify, or simplified to a different value |
555 | | // to previously, then give up. |
556 | 69.0M | if (!V || (18.2M CommonValue18.2M && V != CommonValue6.25M )) |
557 | 54.5M | return nullptr; |
558 | 14.4M | CommonValue = V; |
559 | 14.4M | } |
560 | | |
561 | 208k | return CommonValue; |
562 | 54.8M | } |
563 | | |
564 | | static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode, |
565 | | Value *&Op0, Value *&Op1, |
566 | 710M | const SimplifyQuery &Q) { |
567 | 710M | if (auto *CLHS = dyn_cast<Constant>(Op0)) { |
568 | 68.9M | if (auto *CRHS = dyn_cast<Constant>(Op1)) { |
569 | 23.9M | switch (Opcode) { |
570 | 23.8M | default: |
571 | 23.8M | break; |
572 | 23.8M | case Instruction::FAdd: |
573 | 19.8k | case Instruction::FSub: |
574 | 121k | case Instruction::FMul: |
575 | 139k | case Instruction::FDiv: |
576 | 140k | case Instruction::FRem: |
577 | 140k | if (Q.CxtI != nullptr) |
578 | 125k | return ConstantFoldFPInstOperands(Opcode, CLHS, CRHS, Q.DL, Q.CxtI); |
579 | 23.9M | } |
580 | 23.8M | return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL); |
581 | 23.9M | } |
582 | | |
583 | | // Canonicalize the constant to the RHS if this is a commutative operation. |
584 | 44.9M | if (Instruction::isCommutative(Opcode)) |
585 | 28.8M | std::swap(Op0, Op1); |
586 | 44.9M | } |
587 | 686M | return nullptr; |
588 | 710M | } |
589 | | |
590 | | /// Given operands for an Add, see if we can fold the result. |
591 | | /// If not, this returns null. |
592 | | static Value *simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
593 | 222M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
594 | 222M | if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q)) |
595 | 7.14M | return C; |
596 | | |
597 | | // X + poison -> poison |
598 | 214M | if (isa<PoisonValue>(Op1)) |
599 | 32 | return Op1; |
600 | | |
601 | | // X + undef -> undef |
602 | 214M | if (Q.isUndefValue(Op1)) |
603 | 24.7k | return Op1; |
604 | | |
605 | | // X + 0 -> X |
606 | 214M | if (match(Op1, m_Zero())) |
607 | 769k | return Op0; |
608 | | |
609 | | // If two operands are negative, return 0. |
610 | 214M | if (isKnownNegation(Op0, Op1)) |
611 | 8.41k | return Constant::getNullValue(Op0->getType()); |
612 | | |
613 | | // X + (Y - X) -> Y |
614 | | // (Y - X) + X -> Y |
615 | | // Eg: X + -X -> 0 |
616 | 214M | Value *Y = nullptr; |
617 | 214M | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
618 | 214M | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))214M ) |
619 | 141k | return Y; |
620 | | |
621 | | // X + ~X -> -1 since ~X = -X-1 |
622 | 214M | Type *Ty = Op0->getType(); |
623 | 214M | if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0)))214M ) |
624 | 1.17k | return Constant::getAllOnesValue(Ty); |
625 | | |
626 | | // add nsw/nuw (xor Y, signmask), signmask --> Y |
627 | | // The no-wrapping add guarantees that the top bit will be set by the add. |
628 | | // Therefore, the xor must be clearing the already set sign bit of Y. |
629 | 214M | if ((IsNSW || IsNUW157M ) && match(Op1, m_SignMask())67.6M && |
630 | 214M | match(Op0, m_Xor(m_Value(Y), m_SignMask()))727 ) |
631 | 0 | return Y; |
632 | | |
633 | | // add nuw %x, -1 -> -1, because %x can only be 0. |
634 | 214M | if (IsNUW && match(Op1, m_AllOnes())32.2M ) |
635 | 87 | return Op1; // Which is -1. |
636 | | |
637 | | /// i1 add -> xor. |
638 | 214M | if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1)206M ) |
639 | 27.2k | if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1)) |
640 | 148 | return V; |
641 | | |
642 | | // Try some generic simplifications for associative operations. |
643 | 214M | if (Value *V = |
644 | 214M | simplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, MaxRecurse)) |
645 | 59.8k | return V; |
646 | | |
647 | | // Threading Add over selects and phi nodes is pointless, so don't bother. |
648 | | // Threading over the select in "A + select(cond, B, C)" means evaluating |
649 | | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
650 | | // only if B and C are equal. If B and C are equal then (since we assume |
651 | | // that operands have already been simplified) "select(cond, B, C)" should |
652 | | // have been simplified to the common value of B and C already. Analysing |
653 | | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
654 | | // for threading over phi nodes. |
655 | | |
656 | 213M | return nullptr; |
657 | 214M | } |
658 | | |
659 | | Value *llvm::simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
660 | 75.2M | const SimplifyQuery &Query) { |
661 | 75.2M | return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit); |
662 | 75.2M | } |
663 | | |
664 | | /// Compute the base pointer and cumulative constant offsets for V. |
665 | | /// |
666 | | /// This strips all constant offsets off of V, leaving it the base pointer, and |
667 | | /// accumulates the total constant offset applied in the returned constant. |
668 | | /// It returns zero if there are no constant offsets applied. |
669 | | /// |
670 | | /// This is very similar to stripAndAccumulateConstantOffsets(), except it |
671 | | /// normalizes the offset bitwidth to the stripped pointer type, not the |
672 | | /// original pointer type. |
673 | | static APInt stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, |
674 | 57.8M | bool AllowNonInbounds = false) { |
675 | 57.8M | assert(V->getType()->isPtrOrPtrVectorTy()); |
676 | | |
677 | 57.8M | APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType())); |
678 | 57.8M | V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds); |
679 | | // As that strip may trace through `addrspacecast`, need to sext or trunc |
680 | | // the offset calculated. |
681 | 57.8M | return Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(V->getType())); |
682 | 57.8M | } |
683 | | |
684 | | /// Compute the constant difference between two pointer values. |
685 | | /// If the difference is not a constant, returns zero. |
686 | | static Constant *computePointerDifference(const DataLayout &DL, Value *LHS, |
687 | 28.9M | Value *RHS) { |
688 | 28.9M | APInt LHSOffset = stripAndComputeConstantOffsets(DL, LHS); |
689 | 28.9M | APInt RHSOffset = stripAndComputeConstantOffsets(DL, RHS); |
690 | | |
691 | | // If LHS and RHS are not related via constant offsets to the same base |
692 | | // value, there is nothing we can do here. |
693 | 28.9M | if (LHS != RHS) |
694 | 28.8M | return nullptr; |
695 | | |
696 | | // Otherwise, the difference of LHS - RHS can be computed as: |
697 | | // LHS - RHS |
698 | | // = (LHSOffset + Base) - (RHSOffset + Base) |
699 | | // = LHSOffset - RHSOffset |
700 | 26.3k | Constant *Res = ConstantInt::get(LHS->getContext(), LHSOffset - RHSOffset); |
701 | 26.3k | if (auto *VecTy = dyn_cast<VectorType>(LHS->getType())) |
702 | 0 | Res = ConstantVector::getSplat(VecTy->getElementCount(), Res); |
703 | 26.3k | return Res; |
704 | 28.9M | } |
705 | | |
706 | | /// Test if there is a dominating equivalence condition for the |
707 | | /// two operands. If there is, try to reduce the binary operation |
708 | | /// between the two operands. |
709 | | /// Example: Op0 - Op1 --> 0 when Op0 == Op1 |
710 | | static Value *simplifyByDomEq(unsigned Opcode, Value *Op0, Value *Op1, |
711 | 312M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
712 | | // Recursive run it can not get any benefit |
713 | 312M | if (MaxRecurse != RecursionLimit) |
714 | 108M | return nullptr; |
715 | | |
716 | 204M | std::optional<bool> Imp = |
717 | 204M | isImpliedByDomCondition(CmpInst::ICMP_EQ, Op0, Op1, Q.CxtI, Q.DL); |
718 | 204M | if (Imp && *Imp1.87M ) { |
719 | 5.43k | Type *Ty = Op0->getType(); |
720 | 5.43k | switch (Opcode) { |
721 | 4.08k | case Instruction::Sub: |
722 | 4.10k | case Instruction::Xor: |
723 | 4.10k | case Instruction::URem: |
724 | 4.10k | case Instruction::SRem: |
725 | 4.10k | return Constant::getNullValue(Ty); |
726 | | |
727 | 4 | case Instruction::SDiv: |
728 | 4 | case Instruction::UDiv: |
729 | 4 | return ConstantInt::get(Ty, 1); |
730 | | |
731 | 501 | case Instruction::And: |
732 | 1.32k | case Instruction::Or: |
733 | | // Could be either one - choose Op1 since that's more likely a constant. |
734 | 1.32k | return Op1; |
735 | 0 | default: |
736 | 0 | break; |
737 | 5.43k | } |
738 | 5.43k | } |
739 | 204M | return nullptr; |
740 | 204M | } |
741 | | |
742 | | /// Given operands for a Sub, see if we can fold the result. |
743 | | /// If not, this returns null. |
744 | | static Value *simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
745 | 65.6M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
746 | 65.6M | if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q)) |
747 | 642k | return C; |
748 | | |
749 | | // X - poison -> poison |
750 | | // poison - X -> poison |
751 | 65.0M | if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1)65.0M ) |
752 | 18 | return PoisonValue::get(Op0->getType()); |
753 | | |
754 | | // X - undef -> undef |
755 | | // undef - X -> undef |
756 | 65.0M | if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)65.0M ) |
757 | 61 | return UndefValue::get(Op0->getType()); |
758 | | |
759 | | // X - 0 -> X |
760 | 65.0M | if (match(Op1, m_Zero())) |
761 | 193k | return Op0; |
762 | | |
763 | | // X - X -> 0 |
764 | 64.8M | if (Op0 == Op1) |
765 | 232k | return Constant::getNullValue(Op0->getType()); |
766 | | |
767 | | // Is this a negation? |
768 | 64.6M | if (match(Op0, m_Zero())) { |
769 | | // 0 - X -> 0 if the sub is NUW. |
770 | 4.92M | if (IsNUW) |
771 | 1.97k | return Constant::getNullValue(Op0->getType()); |
772 | | |
773 | 4.91M | KnownBits Known = computeKnownBits(Op1, Q); |
774 | 4.91M | if (Known.Zero.isMaxSignedValue()) { |
775 | | // Op1 is either 0 or the minimum signed value. If the sub is NSW, then |
776 | | // Op1 must be 0 because negating the minimum signed value is undefined. |
777 | 4 | if (IsNSW) |
778 | 0 | return Constant::getNullValue(Op0->getType()); |
779 | | |
780 | | // 0 - X -> X if X is 0 or the minimum signed value. |
781 | 4 | return Op1; |
782 | 4 | } |
783 | 4.91M | } |
784 | | |
785 | | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
786 | | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
787 | 64.6M | Value *X = nullptr, *Y = nullptr, *Z = Op1; |
788 | 64.6M | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))62.8M ) { // (X + Y) - Z |
789 | | // See if "V === Y - Z" simplifies. |
790 | 2.89M | if (Value *V = simplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse - 1)) |
791 | | // It does! Now see if "X + V" simplifies. |
792 | 212k | if (Value *W = simplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse - 1)) { |
793 | | // It does, we successfully reassociated! |
794 | 132k | ++NumReassoc; |
795 | 132k | return W; |
796 | 132k | } |
797 | | // See if "V === X - Z" simplifies. |
798 | 2.76M | if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1)) |
799 | | // It does! Now see if "Y + V" simplifies. |
800 | 143k | if (Value *W = simplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse - 1)) { |
801 | | // It does, we successfully reassociated! |
802 | 119k | ++NumReassoc; |
803 | 119k | return W; |
804 | 119k | } |
805 | 2.76M | } |
806 | | |
807 | | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
808 | | // For example, X - (X + 1) -> -1 |
809 | 64.3M | X = Op0; |
810 | 64.3M | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))62.5M ) { // X - (Y + Z) |
811 | | // See if "V === X - Y" simplifies. |
812 | 2.41M | if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1)) |
813 | | // It does! Now see if "V - Z" simplifies. |
814 | 16.0k | if (Value *W = simplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse - 1)) { |
815 | | // It does, we successfully reassociated! |
816 | 2.68k | ++NumReassoc; |
817 | 2.68k | return W; |
818 | 2.68k | } |
819 | | // See if "V === X - Z" simplifies. |
820 | 2.41M | if (Value *V = simplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1)) |
821 | | // It does! Now see if "V - Y" simplifies. |
822 | 250k | if (Value *W = simplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse - 1)) { |
823 | | // It does, we successfully reassociated! |
824 | 199 | ++NumReassoc; |
825 | 199 | return W; |
826 | 199 | } |
827 | 2.41M | } |
828 | | |
829 | | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
830 | | // For example, X - (X - Y) -> Y. |
831 | 64.3M | Z = Op0; |
832 | 64.3M | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))62.5M ) // Z - (X - Y) |
833 | | // See if "V === Z - X" simplifies. |
834 | 1.07M | if (Value *V = simplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse - 1)) |
835 | | // It does! Now see if "V + Y" simplifies. |
836 | 39.6k | if (Value *W = simplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse - 1)) { |
837 | | // It does, we successfully reassociated! |
838 | 14.0k | ++NumReassoc; |
839 | 14.0k | return W; |
840 | 14.0k | } |
841 | | |
842 | | // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. |
843 | 64.3M | if (MaxRecurse && match(Op0, m_Trunc(m_Value(X)))62.5M && |
844 | 64.3M | match(Op1, m_Trunc(m_Value(Y)))513k ) |
845 | 86.7k | if (X->getType() == Y->getType()) |
846 | | // See if "V === X - Y" simplifies. |
847 | 86.4k | if (Value *V = simplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1)) |
848 | | // It does! Now see if "trunc V" simplifies. |
849 | 72 | if (Value *W = simplifyCastInst(Instruction::Trunc, V, Op0->getType(), |
850 | 72 | Q, MaxRecurse - 1)) |
851 | | // It does, return the simplified "trunc V". |
852 | 25 | return W; |
853 | | |
854 | | // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). |
855 | 64.3M | if (match(Op0, m_PtrToInt(m_Value(X))) && match(Op1, m_PtrToInt(m_Value(Y)))29.2M ) |
856 | 28.9M | if (Constant *Result = computePointerDifference(Q.DL, X, Y)) |
857 | 26.3k | return ConstantFoldIntegerCast(Result, Op0->getType(), /*IsSigned*/ true, |
858 | 26.3k | Q.DL); |
859 | | |
860 | | // i1 sub -> xor. |
861 | 64.3M | if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1)62.5M ) |
862 | 2 | if (Value *V = simplifyXorInst(Op0, Op1, Q, MaxRecurse - 1)) |
863 | 0 | return V; |
864 | | |
865 | | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
866 | | // Threading over the select in "A - select(cond, B, C)" means evaluating |
867 | | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
868 | | // only if B and C are equal. If B and C are equal then (since we assume |
869 | | // that operands have already been simplified) "select(cond, B, C)" should |
870 | | // have been simplified to the common value of B and C already. Analysing |
871 | | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
872 | | // for threading over phi nodes. |
873 | | |
874 | 64.3M | if (Value *V = simplifyByDomEq(Instruction::Sub, Op0, Op1, Q, MaxRecurse)) |
875 | 4.08k | return V; |
876 | | |
877 | | // (sub nuw C_Mask, (xor X, C_Mask)) -> X |
878 | 64.3M | if (IsNUW) { |
879 | 3.48M | Value *X; |
880 | 3.48M | if (match(Op1, m_Xor(m_Value(X), m_Specific(Op0))) && |
881 | 3.48M | match(Op0, m_LowBitMask())213 ) |
882 | 213 | return X; |
883 | 3.48M | } |
884 | | |
885 | 64.3M | return nullptr; |
886 | 64.3M | } |
887 | | |
888 | | Value *llvm::simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
889 | 22.0M | const SimplifyQuery &Q) { |
890 | 22.0M | return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit); |
891 | 22.0M | } |
892 | | |
893 | | /// Given operands for a Mul, see if we can fold the result. |
894 | | /// If not, this returns null. |
895 | | static Value *simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
896 | 40.8M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
897 | 40.8M | if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q)) |
898 | 1.69M | return C; |
899 | | |
900 | | // X * poison -> poison |
901 | 39.1M | if (isa<PoisonValue>(Op1)) |
902 | 183 | return Op1; |
903 | | |
904 | | // X * undef -> 0 |
905 | | // X * 0 -> 0 |
906 | 39.1M | if (Q.isUndefValue(Op1) || match(Op1, m_Zero())39.1M ) |
907 | 1.11M | return Constant::getNullValue(Op0->getType()); |
908 | | |
909 | | // X * 1 -> X |
910 | 38.0M | if (match(Op1, m_One())) |
911 | 576k | return Op0; |
912 | | |
913 | | // (X / Y) * Y -> X if the division is exact. |
914 | 37.4M | Value *X = nullptr; |
915 | 37.4M | if (Q.IIQ.UseInstrInfo && |
916 | 37.4M | (match(Op0, |
917 | 37.4M | m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y |
918 | 37.4M | match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))37.3M )) // Y * (X / Y) |
919 | 65.0k | return X; |
920 | | |
921 | 37.3M | if (Op0->getType()->isIntOrIntVectorTy(1)) { |
922 | | // mul i1 nsw is a special-case because -1 * -1 is poison (+1 is not |
923 | | // representable). All other cases reduce to 0, so just return 0. |
924 | 0 | if (IsNSW) |
925 | 0 | return ConstantInt::getNullValue(Op0->getType()); |
926 | | |
927 | | // Treat "mul i1" as "and i1". |
928 | 0 | if (MaxRecurse) |
929 | 0 | if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1)) |
930 | 0 | return V; |
931 | 0 | } |
932 | | |
933 | | // Try some generic simplifications for associative operations. |
934 | 37.3M | if (Value *V = |
935 | 37.3M | simplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, MaxRecurse)) |
936 | 158 | return V; |
937 | | |
938 | | // Mul distributes over Add. Try some generic simplifications based on this. |
939 | 37.3M | if (Value *V = expandCommutativeBinOp(Instruction::Mul, Op0, Op1, |
940 | 37.3M | Instruction::Add, Q, MaxRecurse)) |
941 | 121 | return V; |
942 | | |
943 | | // If the operation is with the result of a select instruction, check whether |
944 | | // operating on either branch of the select always yields the same value. |
945 | 37.3M | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)36.8M ) |
946 | 781k | if (Value *V = |
947 | 781k | threadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, MaxRecurse)) |
948 | 0 | return V; |
949 | | |
950 | | // If the operation is with the result of a phi instruction, check whether |
951 | | // operating on all incoming values of the phi always yields the same value. |
952 | 37.3M | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)31.7M ) |
953 | 6.93M | if (Value *V = |
954 | 6.93M | threadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, MaxRecurse)) |
955 | 53 | return V; |
956 | | |
957 | 37.3M | return nullptr; |
958 | 37.3M | } |
959 | | |
960 | | Value *llvm::simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
961 | 8.80M | const SimplifyQuery &Q) { |
962 | 8.80M | return ::simplifyMulInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit); |
963 | 8.80M | } |
964 | | |
965 | | /// Given a predicate and two operands, return true if the comparison is true. |
966 | | /// This is a helper for div/rem simplification where we return some other value |
967 | | /// when we can prove a relationship between the operands. |
968 | | static bool isICmpTrue(CmpPredicate Pred, Value *LHS, Value *RHS, |
969 | 38.8M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
970 | 38.8M | Value *V = simplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse); |
971 | 38.8M | Constant *C = dyn_cast_or_null<Constant>(V); |
972 | 38.8M | return (C && C->isAllOnesValue()545k ); |
973 | 38.8M | } |
974 | | |
975 | | /// Return true if we can simplify X / Y to 0. Remainder can adapt that answer |
976 | | /// to simplify X % Y to X. |
977 | | static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q, |
978 | 15.3M | unsigned MaxRecurse, bool IsSigned) { |
979 | | // Recursion is always used, so bail out at once if we already hit the limit. |
980 | 15.3M | if (!MaxRecurse--) |
981 | 35.0k | return false; |
982 | | |
983 | 15.3M | if (IsSigned) { |
984 | | // (X srem Y) sdiv Y --> 0 |
985 | 6.72M | if (match(X, m_SRem(m_Value(), m_Specific(Y)))) |
986 | 189 | return true; |
987 | | |
988 | | // |X| / |Y| --> 0 |
989 | | // |
990 | | // We require that 1 operand is a simple constant. That could be extended to |
991 | | // 2 variables if we computed the sign bit for each. |
992 | | // |
993 | | // Make sure that a constant is not the minimum signed value because taking |
994 | | // the abs() of that is undefined. |
995 | 6.72M | Type *Ty = X->getType(); |
996 | 6.72M | const APInt *C; |
997 | 6.72M | if (match(X, m_APInt(C)) && !C->isMinSignedValue()146k ) { |
998 | | // Is the variable divisor magnitude always greater than the constant |
999 | | // dividend magnitude? |
1000 | | // |Y| > |C| --> Y < -abs(C) or Y > abs(C) |
1001 | 144k | Constant *PosDividendC = ConstantInt::get(Ty, C->abs()); |
1002 | 144k | Constant *NegDividendC = ConstantInt::get(Ty, -C->abs()); |
1003 | 144k | if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) || |
1004 | 144k | isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse)) |
1005 | 5 | return true; |
1006 | 144k | } |
1007 | 6.72M | if (match(Y, m_APInt(C))) { |
1008 | | // Special-case: we can't take the abs() of a minimum signed value. If |
1009 | | // that's the divisor, then all we have to do is prove that the dividend |
1010 | | // is also not the minimum signed value. |
1011 | 5.87M | if (C->isMinSignedValue()) |
1012 | 20 | return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse); |
1013 | | |
1014 | | // Is the variable dividend magnitude always less than the constant |
1015 | | // divisor magnitude? |
1016 | | // |X| < |C| --> X > -abs(C) and X < abs(C) |
1017 | 5.87M | Constant *PosDivisorC = ConstantInt::get(Ty, C->abs()); |
1018 | 5.87M | Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs()); |
1019 | 5.87M | if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) && |
1020 | 5.87M | isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse)26.6k ) |
1021 | 1.08k | return true; |
1022 | 5.87M | } |
1023 | 6.72M | return false; |
1024 | 6.72M | } |
1025 | | |
1026 | | // IsSigned == false. |
1027 | | |
1028 | | // Is the unsigned dividend known to be less than a constant divisor? |
1029 | | // TODO: Convert this (and above) to range analysis |
1030 | | // ("computeConstantRangeIncludingKnownBits")? |
1031 | 8.57M | const APInt *C; |
1032 | 8.57M | if (match(Y, m_APInt(C)) && computeKnownBits(X, Q).getMaxValue().ult(*C)5.65M ) |
1033 | 10.6k | return true; |
1034 | | |
1035 | | // Try again for any divisor: |
1036 | | // Is the dividend unsigned less than the divisor? |
1037 | 8.56M | return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse); |
1038 | 8.57M | } |
1039 | | |
1040 | | /// Check for common or similar folds of integer division or integer remainder. |
1041 | | /// This applies to all 4 opcodes (sdiv/udiv/srem/urem). |
1042 | | static Value *simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0, |
1043 | | Value *Op1, const SimplifyQuery &Q, |
1044 | 15.3M | unsigned MaxRecurse) { |
1045 | 15.3M | bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv9.38M ); |
1046 | 15.3M | bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem9.38M ); |
1047 | | |
1048 | 15.3M | Type *Ty = Op0->getType(); |
1049 | | |
1050 | | // X / undef -> poison |
1051 | | // X % undef -> poison |
1052 | 15.3M | if (Q.isUndefValue(Op1) || isa<PoisonValue>(Op1)15.3M ) |
1053 | 56 | return PoisonValue::get(Ty); |
1054 | | |
1055 | | // X / 0 -> poison |
1056 | | // X % 0 -> poison |
1057 | | // We don't need to preserve faults! |
1058 | 15.3M | if (match(Op1, m_Zero())) |
1059 | 288 | return PoisonValue::get(Ty); |
1060 | | |
1061 | | // poison / X -> poison |
1062 | | // poison % X -> poison |
1063 | 15.3M | if (isa<PoisonValue>(Op0)) |
1064 | 50 | return Op0; |
1065 | | |
1066 | | // undef / X -> 0 |
1067 | | // undef % X -> 0 |
1068 | 15.3M | if (Q.isUndefValue(Op0)) |
1069 | 65 | return Constant::getNullValue(Ty); |
1070 | | |
1071 | | // 0 / X -> 0 |
1072 | | // 0 % X -> 0 |
1073 | 15.3M | if (match(Op0, m_Zero())) |
1074 | 24.7k | return Constant::getNullValue(Op0->getType()); |
1075 | | |
1076 | | // X / X -> 1 |
1077 | | // X % X -> 0 |
1078 | 15.3M | if (Op0 == Op1) |
1079 | 920 | return IsDiv ? ConstantInt::get(Ty, 1)568 : Constant::getNullValue(Ty)352 ; |
1080 | | |
1081 | 15.3M | KnownBits Known = computeKnownBits(Op1, Q); |
1082 | | // X / 0 -> poison |
1083 | | // X % 0 -> poison |
1084 | | // If the divisor is known to be zero, just return poison. This can happen in |
1085 | | // some cases where its provable indirectly the denominator is zero but it's |
1086 | | // not trivially simplifiable (i.e known zero through a phi node). |
1087 | 15.3M | if (Known.isZero()) |
1088 | 22 | return PoisonValue::get(Ty); |
1089 | | |
1090 | | // X / 1 -> X |
1091 | | // X % 1 -> 0 |
1092 | | // If the divisor can only be zero or one, we can't have division-by-zero |
1093 | | // or remainder-by-zero, so assume the divisor is 1. |
1094 | | // e.g. 1, zext (i8 X), sdiv X (Y and 1) |
1095 | 15.3M | if (Known.countMinLeadingZeros() == Known.getBitWidth() - 1) |
1096 | 25.1k | return IsDiv ? Op021.5k : Constant::getNullValue(Ty)3.56k ; |
1097 | | |
1098 | | // If X * Y does not overflow, then: |
1099 | | // X * Y / Y -> X |
1100 | | // X * Y % Y -> 0 |
1101 | 15.3M | Value *X; |
1102 | 15.3M | if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) { |
1103 | 18.6k | auto *Mul = cast<OverflowingBinaryOperator>(Op0); |
1104 | | // The multiplication can't overflow if it is defined not to, or if |
1105 | | // X == A / Y for some A. |
1106 | 18.6k | if ((IsSigned && Q.IIQ.hasNoSignedWrap(Mul)8.26k ) || |
1107 | 18.6k | (15.4k !IsSigned15.4k && Q.IIQ.hasNoUnsignedWrap(Mul)10.4k ) || |
1108 | 18.6k | (11.5k IsSigned11.5k && match(X, m_SDiv(m_Value(), m_Specific(Op1)))5.05k ) || |
1109 | 18.6k | (11.5k !IsSigned11.5k && match(X, m_UDiv(m_Value(), m_Specific(Op1)))6.49k )) { |
1110 | 7.18k | return IsDiv ? X6.93k : Constant::getNullValue(Op0->getType())253 ; |
1111 | 7.18k | } |
1112 | 18.6k | } |
1113 | | |
1114 | 15.3M | if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned)) |
1115 | 12.7k | return IsDiv ? Constant::getNullValue(Op0->getType())7.27k : Op05.51k ; |
1116 | | |
1117 | 15.3M | if (Value *V = simplifyByDomEq(Opcode, Op0, Op1, Q, MaxRecurse)) |
1118 | 5 | return V; |
1119 | | |
1120 | | // If the operation is with the result of a select instruction, check whether |
1121 | | // operating on either branch of the select always yields the same value. |
1122 | 15.3M | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)15.2M ) |
1123 | 58.1k | if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
1124 | 17 | return V; |
1125 | | |
1126 | | // If the operation is with the result of a phi instruction, check whether |
1127 | | // operating on all incoming values of the phi always yields the same value. |
1128 | 15.3M | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)12.5M ) |
1129 | 2.92M | if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
1130 | 60 | return V; |
1131 | | |
1132 | 15.3M | return nullptr; |
1133 | 15.3M | } |
1134 | | |
1135 | | /// These are simplifications common to SDiv and UDiv. |
1136 | | static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
1137 | | bool IsExact, const SimplifyQuery &Q, |
1138 | 10.3M | unsigned MaxRecurse) { |
1139 | 10.3M | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
1140 | 101k | return C; |
1141 | | |
1142 | 10.2M | if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse)) |
1143 | 47.5k | return V; |
1144 | | |
1145 | 10.1M | const APInt *DivC; |
1146 | 10.1M | if (IsExact && match(Op1, m_APInt(DivC))3.23M ) { |
1147 | | // If this is an exact divide by a constant, then the dividend (Op0) must |
1148 | | // have at least as many trailing zeros as the divisor to divide evenly. If |
1149 | | // it has less trailing zeros, then the result must be poison. |
1150 | 3.23M | if (DivC->countr_zero()) { |
1151 | 3.22M | KnownBits KnownOp0 = computeKnownBits(Op0, Q); |
1152 | 3.22M | if (KnownOp0.countMaxTrailingZeros() < DivC->countr_zero()) |
1153 | 0 | return PoisonValue::get(Op0->getType()); |
1154 | 3.22M | } |
1155 | | |
1156 | | // udiv exact (mul nsw X, C), C --> X |
1157 | | // sdiv exact (mul nuw X, C), C --> X |
1158 | | // where C is not a power of 2. |
1159 | 3.23M | Value *X; |
1160 | 3.23M | if (!DivC->isPowerOf2() && |
1161 | 3.23M | (2.76M Opcode == Instruction::UDiv2.76M |
1162 | 2.76M | ? match(Op0, m_NSWMul(m_Value(X), m_Specific(Op1)))552k |
1163 | 2.76M | : match(Op0, m_NUWMul(m_Value(X), m_Specific(Op1)))2.21M )) |
1164 | 345 | return X; |
1165 | 3.23M | } |
1166 | | |
1167 | 10.1M | return nullptr; |
1168 | 10.1M | } |
1169 | | |
1170 | | /// These are simplifications common to SRem and URem. |
1171 | | static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, |
1172 | 5.21M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
1173 | 5.21M | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
1174 | 31.3k | return C; |
1175 | | |
1176 | 5.18M | if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q, MaxRecurse)) |
1177 | 23.7k | return V; |
1178 | | |
1179 | | // (X << Y) % X -> 0 |
1180 | 5.16M | if (Q.IIQ.UseInstrInfo) { |
1181 | 5.16M | if ((Opcode == Instruction::SRem && |
1182 | 5.16M | match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))744k ) || |
1183 | 5.16M | (Opcode == Instruction::URem && |
1184 | 5.16M | match(Op0, m_NUWShl(m_Specific(Op1), m_Value()))4.41M )) |
1185 | 0 | return Constant::getNullValue(Op0->getType()); |
1186 | | |
1187 | 5.16M | const APInt *C0; |
1188 | 5.16M | if (match(Op1, m_APInt(C0))) { |
1189 | | // (srem (mul nsw X, C1), C0) -> 0 if C1 s% C0 == 0 |
1190 | | // (urem (mul nuw X, C1), C0) -> 0 if C1 u% C0 == 0 |
1191 | 2.48M | if (Opcode == Instruction::SRem |
1192 | 2.48M | ? match(Op0, |
1193 | 449k | m_NSWMul(m_Value(), m_CheckedInt([C0](const APInt &C) { |
1194 | 2.48k | return C.srem(*C0).isZero(); |
1195 | 2.48k | }))) |
1196 | 2.48M | : match(Op0, |
1197 | 2.03M | m_NUWMul(m_Value(), m_CheckedInt([C0](const APInt &C) { |
1198 | 4.22k | return C.urem(*C0).isZero(); |
1199 | 4.22k | })))) |
1200 | 52 | return Constant::getNullValue(Op0->getType()); |
1201 | 2.48M | } |
1202 | 5.16M | } |
1203 | 5.16M | return nullptr; |
1204 | 5.16M | } |
1205 | | |
1206 | | /// Given operands for an SDiv, see if we can fold the result. |
1207 | | /// If not, this returns null. |
1208 | | static Value *simplifySDivInst(Value *Op0, Value *Op1, bool IsExact, |
1209 | 6.06M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
1210 | | // If two operands are negated and no signed overflow, return -1. |
1211 | 6.06M | if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true)) |
1212 | 1 | return Constant::getAllOnesValue(Op0->getType()); |
1213 | | |
1214 | 6.06M | return simplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse); |
1215 | 6.06M | } |
1216 | | |
1217 | | Value *llvm::simplifySDivInst(Value *Op0, Value *Op1, bool IsExact, |
1218 | 2.35M | const SimplifyQuery &Q) { |
1219 | 2.35M | return ::simplifySDivInst(Op0, Op1, IsExact, Q, RecursionLimit); |
1220 | 2.35M | } |
1221 | | |
1222 | | /// Given operands for a UDiv, see if we can fold the result. |
1223 | | /// If not, this returns null. |
1224 | | static Value *simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact, |
1225 | 4.24M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
1226 | 4.24M | return simplifyDiv(Instruction::UDiv, Op0, Op1, IsExact, Q, MaxRecurse); |
1227 | 4.24M | } |
1228 | | |
1229 | | Value *llvm::simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact, |
1230 | 1.38M | const SimplifyQuery &Q) { |
1231 | 1.38M | return ::simplifyUDivInst(Op0, Op1, IsExact, Q, RecursionLimit); |
1232 | 1.38M | } |
1233 | | |
1234 | | /// Given operands for an SRem, see if we can fold the result. |
1235 | | /// If not, this returns null. |
1236 | | static Value *simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
1237 | 767k | unsigned MaxRecurse) { |
1238 | | // If the divisor is 0, the result is undefined, so assume the divisor is -1. |
1239 | | // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0 |
1240 | 767k | Value *X; |
1241 | 767k | if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)10.8k ) |
1242 | 0 | return ConstantInt::getNullValue(Op0->getType()); |
1243 | | |
1244 | | // If the two operands are negated, return 0. |
1245 | 767k | if (isKnownNegation(Op0, Op1)) |
1246 | 0 | return ConstantInt::getNullValue(Op0->getType()); |
1247 | | |
1248 | 767k | return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse); |
1249 | 767k | } |
1250 | | |
1251 | 281k | Value *llvm::simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
1252 | 281k | return ::simplifySRemInst(Op0, Op1, Q, RecursionLimit); |
1253 | 281k | } |
1254 | | |
1255 | | /// Given operands for a URem, see if we can fold the result. |
1256 | | /// If not, this returns null. |
1257 | | static Value *simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
1258 | 4.44M | unsigned MaxRecurse) { |
1259 | 4.44M | return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse); |
1260 | 4.44M | } |
1261 | | |
1262 | 1.39M | Value *llvm::simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
1263 | 1.39M | return ::simplifyURemInst(Op0, Op1, Q, RecursionLimit); |
1264 | 1.39M | } |
1265 | | |
1266 | | /// Returns true if a shift by \c Amount always yields poison. |
1267 | 85.7M | static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q) { |
1268 | 85.7M | Constant *C = dyn_cast<Constant>(Amount); |
1269 | 85.7M | if (!C) |
1270 | 8.18M | return false; |
1271 | | |
1272 | | // X shift by undef -> poison because it may shift by the bitwidth. |
1273 | 77.5M | if (Q.isUndefValue(C)) |
1274 | 22 | return true; |
1275 | | |
1276 | | // Shifting by the bitwidth or more is poison. This covers scalars and |
1277 | | // fixed/scalable vectors with splat constants. |
1278 | 77.5M | const APInt *AmountC; |
1279 | 77.5M | if (match(C, m_APInt(AmountC)) && AmountC->uge(AmountC->getBitWidth())77.5M ) |
1280 | 387 | return true; |
1281 | | |
1282 | | // Try harder for fixed-length vectors: |
1283 | | // If all lanes of a vector shift are poison, the whole shift is poison. |
1284 | 77.5M | if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)77.5M ) { |
1285 | 179k | for (unsigned I = 0, |
1286 | 179k | E = cast<FixedVectorType>(C->getType())->getNumElements(); |
1287 | 179k | I != E; ++I0 ) |
1288 | 179k | if (!isPoisonShift(C->getAggregateElement(I), Q)) |
1289 | 179k | return false; |
1290 | 0 | return true; |
1291 | 179k | } |
1292 | | |
1293 | 77.3M | return false; |
1294 | 77.5M | } |
1295 | | |
1296 | | /// Given operands for an Shl, LShr or AShr, see if we can fold the result. |
1297 | | /// If not, this returns null. |
1298 | | static Value *simplifyShift(Instruction::BinaryOps Opcode, Value *Op0, |
1299 | | Value *Op1, bool IsNSW, const SimplifyQuery &Q, |
1300 | 90.3M | unsigned MaxRecurse) { |
1301 | 90.3M | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) |
1302 | 4.70M | return C; |
1303 | | |
1304 | | // poison shift by X -> poison |
1305 | 85.6M | if (isa<PoisonValue>(Op0)) |
1306 | 24 | return Op0; |
1307 | | |
1308 | | // 0 shift by X -> 0 |
1309 | 85.6M | if (match(Op0, m_Zero())) |
1310 | 35.1k | return Constant::getNullValue(Op0->getType()); |
1311 | | |
1312 | | // X shift by 0 -> X |
1313 | | // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones |
1314 | | // would be poison. |
1315 | 85.5M | Value *X; |
1316 | 85.5M | if (match(Op1, m_Zero()) || |
1317 | 85.5M | (85.5M match(Op1, m_SExt(m_Value(X)))85.5M && X->getType()->isIntOrIntVectorTy(1)7.49k )) |
1318 | 44.2k | return Op0; |
1319 | | |
1320 | | // Fold undefined shifts. |
1321 | 85.5M | if (isPoisonShift(Op1, Q)) |
1322 | 409 | return PoisonValue::get(Op0->getType()); |
1323 | | |
1324 | | // If the operation is with the result of a select instruction, check whether |
1325 | | // operating on either branch of the select always yields the same value. |
1326 | 85.5M | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)83.1M ) |
1327 | 2.41M | if (Value *V = threadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse)) |
1328 | 857 | return V; |
1329 | | |
1330 | | // If the operation is with the result of a phi instruction, check whether |
1331 | | // operating on all incoming values of the phi always yields the same value. |
1332 | 85.5M | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)77.2M ) |
1333 | 8.64M | if (Value *V = threadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse)) |
1334 | 13.0k | return V; |
1335 | | |
1336 | | // If any bits in the shift amount make that value greater than or equal to |
1337 | | // the number of bits in the type, the shift is undefined. |
1338 | 85.5M | KnownBits KnownAmt = computeKnownBits(Op1, Q); |
1339 | 85.5M | if (KnownAmt.getMinValue().uge(KnownAmt.getBitWidth())) |
1340 | 61 | return PoisonValue::get(Op0->getType()); |
1341 | | |
1342 | | // If all valid bits in the shift amount are known zero, the first operand is |
1343 | | // unchanged. |
1344 | 85.5M | unsigned NumValidShiftBits = Log2_32_Ceil(KnownAmt.getBitWidth()); |
1345 | 85.5M | if (KnownAmt.countMinTrailingZeros() >= NumValidShiftBits) |
1346 | 1.39k | return Op0; |
1347 | | |
1348 | | // Check for nsw shl leading to a poison value. |
1349 | 85.5M | if (IsNSW) { |
1350 | 15.5M | assert(Opcode == Instruction::Shl && "Expected shl for nsw instruction"); |
1351 | 15.5M | KnownBits KnownVal = computeKnownBits(Op0, Q); |
1352 | 15.5M | KnownBits KnownShl = KnownBits::shl(KnownVal, KnownAmt); |
1353 | | |
1354 | 15.5M | if (KnownVal.Zero.isSignBitSet()) |
1355 | 10.0M | KnownShl.Zero.setSignBit(); |
1356 | 15.5M | if (KnownVal.One.isSignBitSet()) |
1357 | 539k | KnownShl.One.setSignBit(); |
1358 | | |
1359 | 15.5M | if (KnownShl.hasConflict()) |
1360 | 0 | return PoisonValue::get(Op0->getType()); |
1361 | 15.5M | } |
1362 | | |
1363 | 85.5M | return nullptr; |
1364 | 85.5M | } |
1365 | | |
1366 | | /// Given operands for an LShr or AShr, see if we can fold the result. If not, |
1367 | | /// this returns null. |
1368 | | static Value *simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0, |
1369 | | Value *Op1, bool IsExact, |
1370 | 44.6M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
1371 | 44.6M | if (Value *V = |
1372 | 44.6M | simplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse)) |
1373 | 812k | return V; |
1374 | | |
1375 | | // X >> X -> 0 |
1376 | 43.8M | if (Op0 == Op1) |
1377 | 0 | return Constant::getNullValue(Op0->getType()); |
1378 | | |
1379 | | // undef >> X -> 0 |
1380 | | // undef >> X -> undef (if it's exact) |
1381 | 43.8M | if (Q.isUndefValue(Op0)) |
1382 | 12 | return IsExact ? Op00 : Constant::getNullValue(Op0->getType()); |
1383 | | |
1384 | | // The low bit cannot be shifted out of an exact shift if it is set. |
1385 | | // TODO: Generalize by counting trailing zeros (see fold for exact division). |
1386 | 43.8M | if (IsExact) { |
1387 | 8.34M | KnownBits Op0Known = computeKnownBits(Op0, Q); |
1388 | 8.34M | if (Op0Known.One[0]) |
1389 | 2 | return Op0; |
1390 | 8.34M | } |
1391 | | |
1392 | 43.8M | return nullptr; |
1393 | 43.8M | } |
1394 | | |
1395 | | /// Given operands for an Shl, see if we can fold the result. |
1396 | | /// If not, this returns null. |
1397 | | static Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
1398 | 45.6M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
1399 | 45.6M | if (Value *V = |
1400 | 45.6M | simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse)) |
1401 | 3.99M | return V; |
1402 | | |
1403 | 41.6M | Type *Ty = Op0->getType(); |
1404 | | // undef << X -> 0 |
1405 | | // undef << X -> undef if (if it's NSW/NUW) |
1406 | 41.6M | if (Q.isUndefValue(Op0)) |
1407 | 3 | return IsNSW || IsNUW ? Op00 : Constant::getNullValue(Ty); |
1408 | | |
1409 | | // (X >> A) << A -> X |
1410 | 41.6M | Value *X; |
1411 | 41.6M | if (Q.IIQ.UseInstrInfo && |
1412 | 41.6M | match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1))))) |
1413 | 99.4k | return X; |
1414 | | |
1415 | | // shl nuw i8 C, %x -> C iff C has sign bit set. |
1416 | 41.5M | if (IsNUW && match(Op0, m_Negative())16.3M ) |
1417 | 0 | return Op0; |
1418 | | // NOTE: could use computeKnownBits() / LazyValueInfo, |
1419 | | // but the cost-benefit analysis suggests it isn't worth it. |
1420 | | |
1421 | | // "nuw" guarantees that only zeros are shifted out, and "nsw" guarantees |
1422 | | // that the sign-bit does not change, so the only input that does not |
1423 | | // produce poison is 0, and "0 << (bitwidth-1) --> 0". |
1424 | 41.5M | if (IsNSW && IsNUW15.5M && |
1425 | 41.5M | match(Op1, m_SpecificInt(Ty->getScalarSizeInBits() - 1))11.7M ) |
1426 | 0 | return Constant::getNullValue(Ty); |
1427 | | |
1428 | 41.5M | return nullptr; |
1429 | 41.5M | } |
1430 | | |
1431 | | Value *llvm::simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, |
1432 | 15.6M | const SimplifyQuery &Q) { |
1433 | 15.6M | return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q, RecursionLimit); |
1434 | 15.6M | } |
1435 | | |
1436 | | /// Given operands for an LShr, see if we can fold the result. |
1437 | | /// If not, this returns null. |
1438 | | static Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, |
1439 | 33.5M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
1440 | 33.5M | if (Value *V = simplifyRightShift(Instruction::LShr, Op0, Op1, IsExact, Q, |
1441 | 33.5M | MaxRecurse)) |
1442 | 705k | return V; |
1443 | | |
1444 | | // (X << A) >> A -> X |
1445 | 32.8M | Value *X; |
1446 | 32.8M | if (Q.IIQ.UseInstrInfo && match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1)))) |
1447 | 14.5k | return X; |
1448 | | |
1449 | | // ((X << A) | Y) >> A -> X if effective width of Y is not larger than A. |
1450 | | // We can return X as we do in the above case since OR alters no bits in X. |
1451 | | // SimplifyDemandedBits in InstCombine can do more general optimization for |
1452 | | // bit manipulation. This pattern aims to provide opportunities for other |
1453 | | // optimizers by supporting a simple but common case in InstSimplify. |
1454 | 32.8M | Value *Y; |
1455 | 32.8M | const APInt *ShRAmt, *ShLAmt; |
1456 | 32.8M | if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(ShRAmt)) && |
1457 | 32.8M | match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShLAmt)), m_Value(Y)))30.1M && |
1458 | 32.8M | *ShRAmt == *ShLAmt241k ) { |
1459 | 68.0k | const KnownBits YKnown = computeKnownBits(Y, Q); |
1460 | 68.0k | const unsigned EffWidthY = YKnown.countMaxActiveBits(); |
1461 | 68.0k | if (ShRAmt->uge(EffWidthY)) |
1462 | 63.1k | return X; |
1463 | 68.0k | } |
1464 | | |
1465 | 32.7M | return nullptr; |
1466 | 32.8M | } |
1467 | | |
1468 | | Value *llvm::simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, |
1469 | 12.4M | const SimplifyQuery &Q) { |
1470 | 12.4M | return ::simplifyLShrInst(Op0, Op1, IsExact, Q, RecursionLimit); |
1471 | 12.4M | } |
1472 | | |
1473 | | /// Given operands for an AShr, see if we can fold the result. |
1474 | | /// If not, this returns null. |
1475 | | static Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, |
1476 | 11.0M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
1477 | 11.0M | if (Value *V = simplifyRightShift(Instruction::AShr, Op0, Op1, IsExact, Q, |
1478 | 11.0M | MaxRecurse)) |
1479 | 107k | return V; |
1480 | | |
1481 | | // -1 >>a X --> -1 |
1482 | | // (-1 << X) a>> X --> -1 |
1483 | | // We could return the original -1 constant to preserve poison elements. |
1484 | 10.9M | if (match(Op0, m_AllOnes()) || |
1485 | 10.9M | match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1)))10.9M ) |
1486 | 76 | return Constant::getAllOnesValue(Op0->getType()); |
1487 | | |
1488 | | // (X << A) >> A -> X |
1489 | 10.9M | Value *X; |
1490 | 10.9M | if (Q.IIQ.UseInstrInfo && match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1)))) |
1491 | 9.27k | return X; |
1492 | | |
1493 | | // Arithmetic shifting an all-sign-bit value is a no-op. |
1494 | 10.9M | unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, Q.AC, Q.CxtI, Q.DT); |
1495 | 10.9M | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
1496 | 156 | return Op0; |
1497 | | |
1498 | 10.9M | return nullptr; |
1499 | 10.9M | } |
1500 | | |
1501 | | Value *llvm::simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, |
1502 | 4.33M | const SimplifyQuery &Q) { |
1503 | 4.33M | return ::simplifyAShrInst(Op0, Op1, IsExact, Q, RecursionLimit); |
1504 | 4.33M | } |
1505 | | |
1506 | | /// Commuted variants are assumed to be handled by calling this function again |
1507 | | /// with the parameters swapped. |
1508 | | static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, |
1509 | | ICmpInst *UnsignedICmp, bool IsAnd, |
1510 | 15.5M | const SimplifyQuery &Q) { |
1511 | 15.5M | Value *X, *Y; |
1512 | | |
1513 | 15.5M | CmpPredicate EqPred; |
1514 | 15.5M | if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) || |
1515 | 15.5M | !ICmpInst::isEquality(EqPred)5.48M ) |
1516 | 10.3M | return nullptr; |
1517 | | |
1518 | 5.24M | CmpPredicate UnsignedPred; |
1519 | | |
1520 | 5.24M | Value *A, *B; |
1521 | | // Y = (A - B); |
1522 | 5.24M | if (match(Y, m_Sub(m_Value(A), m_Value(B)))) { |
1523 | 18.4k | if (match(UnsignedICmp, |
1524 | 18.4k | m_c_ICmp(UnsignedPred, m_Specific(A), m_Specific(B))) && |
1525 | 18.4k | ICmpInst::isUnsigned(UnsignedPred)18 ) { |
1526 | | // A >=/<= B || (A - B) != 0 <--> true |
1527 | 18 | if ((UnsignedPred == ICmpInst::ICMP_UGE || |
1528 | 18 | UnsignedPred == ICmpInst::ICMP_ULE) && |
1529 | 18 | EqPred == ICmpInst::ICMP_NE1 && !IsAnd0 ) |
1530 | 0 | return ConstantInt::getTrue(UnsignedICmp->getType()); |
1531 | | // A </> B && (A - B) == 0 <--> false |
1532 | 18 | if ((UnsignedPred == ICmpInst::ICMP_ULT || |
1533 | 18 | UnsignedPred == ICmpInst::ICMP_UGT1 ) && |
1534 | 18 | EqPred == ICmpInst::ICMP_EQ17 && IsAnd17 ) |
1535 | 0 | return ConstantInt::getFalse(UnsignedICmp->getType()); |
1536 | | |
1537 | | // A </> B && (A - B) != 0 <--> A </> B |
1538 | | // A </> B || (A - B) != 0 <--> (A - B) != 0 |
1539 | 18 | if (EqPred == ICmpInst::ICMP_NE && (0 UnsignedPred == ICmpInst::ICMP_ULT0 || |
1540 | 0 | UnsignedPred == ICmpInst::ICMP_UGT)) |
1541 | 0 | return IsAnd ? UnsignedICmp : ZeroICmp; |
1542 | | |
1543 | | // A <=/>= B && (A - B) == 0 <--> (A - B) == 0 |
1544 | | // A <=/>= B || (A - B) == 0 <--> A <=/>= B |
1545 | 18 | if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE || |
1546 | 18 | UnsignedPred == ICmpInst::ICMP_UGE17 )) |
1547 | 1 | return IsAnd ? ZeroICmp0 : UnsignedICmp; |
1548 | 18 | } |
1549 | | |
1550 | | // Given Y = (A - B) |
1551 | | // Y >= A && Y != 0 --> Y >= A iff B != 0 |
1552 | | // Y < A || Y == 0 --> Y < A iff B != 0 |
1553 | 18.4k | if (match(UnsignedICmp, |
1554 | 18.4k | m_c_ICmp(UnsignedPred, m_Specific(Y), m_Specific(A)))) { |
1555 | 4 | if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd0 && |
1556 | 4 | EqPred == ICmpInst::ICMP_NE0 && isKnownNonZero(B, Q)0 ) |
1557 | 0 | return UnsignedICmp; |
1558 | 4 | if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd3 && |
1559 | 4 | EqPred == ICmpInst::ICMP_EQ0 && isKnownNonZero(B, Q)0 ) |
1560 | 0 | return UnsignedICmp; |
1561 | 4 | } |
1562 | 18.4k | } |
1563 | | |
1564 | 5.24M | if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) && |
1565 | 5.24M | ICmpInst::isUnsigned(UnsignedPred)37.2k ) |
1566 | 9.36k | ; |
1567 | 5.23M | else if (match(UnsignedICmp, |
1568 | 5.23M | m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) && |
1569 | 5.23M | ICmpInst::isUnsigned(UnsignedPred)418k ) |
1570 | 37.2k | UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); |
1571 | 5.19M | else |
1572 | 5.19M | return nullptr; |
1573 | | |
1574 | | // X > Y && Y == 0 --> Y == 0 iff X != 0 |
1575 | | // X > Y || Y == 0 --> X > Y iff X != 0 |
1576 | 46.6k | if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ25.3k && |
1577 | 46.6k | isKnownNonZero(X, Q)5.27k ) |
1578 | 1 | return IsAnd ? ZeroICmp : UnsignedICmp0 ; |
1579 | | |
1580 | | // X <= Y && Y != 0 --> X <= Y iff X != 0 |
1581 | | // X <= Y || Y != 0 --> Y != 0 iff X != 0 |
1582 | 46.6k | if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE8.84k && |
1583 | 46.6k | isKnownNonZero(X, Q)949 ) |
1584 | 0 | return IsAnd ? UnsignedICmp : ZeroICmp; |
1585 | | |
1586 | | // The transforms below here are expected to be handled more generally with |
1587 | | // simplifyAndOrOfICmpsWithLimitConst() or in InstCombine's |
1588 | | // foldAndOrOfICmpsWithConstEq(). If we are looking to trim optimizer overlap, |
1589 | | // these are candidates for removal. |
1590 | | |
1591 | | // X < Y && Y != 0 --> X < Y |
1592 | | // X < Y || Y != 0 --> Y != 0 |
1593 | 46.6k | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE11.5k ) |
1594 | 0 | return IsAnd ? UnsignedICmp : ZeroICmp; |
1595 | | |
1596 | | // X >= Y && Y == 0 --> Y == 0 |
1597 | | // X >= Y || Y == 0 --> X >= Y |
1598 | 46.6k | if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ826 ) |
1599 | 1 | return IsAnd ? ZeroICmp : UnsignedICmp0 ; |
1600 | | |
1601 | | // X < Y && Y == 0 --> false |
1602 | 46.6k | if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ11.5k && |
1603 | 46.6k | IsAnd11.5k ) |
1604 | 3 | return getFalse(UnsignedICmp->getType()); |
1605 | | |
1606 | | // X >= Y || Y != 0 --> true |
1607 | 46.6k | if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE825 && |
1608 | 46.6k | !IsAnd825 ) |
1609 | 0 | return getTrue(UnsignedICmp->getType()); |
1610 | | |
1611 | 46.6k | return nullptr; |
1612 | 46.6k | } |
1613 | | |
1614 | | /// Test if a pair of compares with a shared operand and 2 constants has an |
1615 | | /// empty set intersection, full set union, or if one compare is a superset of |
1616 | | /// the other. |
1617 | | static Value *simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1, |
1618 | 7.77M | bool IsAnd) { |
1619 | | // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)). |
1620 | 7.77M | if (Cmp0->getOperand(0) != Cmp1->getOperand(0)) |
1621 | 6.18M | return nullptr; |
1622 | | |
1623 | 1.59M | const APInt *C0, *C1; |
1624 | 1.59M | if (!match(Cmp0->getOperand(1), m_APInt(C0)) || |
1625 | 1.59M | !match(Cmp1->getOperand(1), m_APInt(C1))736k ) |
1626 | 1.03M | return nullptr; |
1627 | | |
1628 | 556k | auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0); |
1629 | 556k | auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1); |
1630 | | |
1631 | | // For and-of-compares, check if the intersection is empty: |
1632 | | // (icmp X, C0) && (icmp X, C1) --> empty set --> false |
1633 | 556k | if (IsAnd && Range0.intersectWith(Range1).isEmptySet()170k ) |
1634 | 196 | return getFalse(Cmp0->getType()); |
1635 | | |
1636 | | // For or-of-compares, check if the union is full: |
1637 | | // (icmp X, C0) || (icmp X, C1) --> full set --> true |
1638 | 556k | if (!IsAnd && Range0.unionWith(Range1).isFullSet()386k ) |
1639 | 254 | return getTrue(Cmp0->getType()); |
1640 | | |
1641 | | // Is one range a superset of the other? |
1642 | | // If this is and-of-compares, take the smaller set: |
1643 | | // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42 |
1644 | | // If this is or-of-compares, take the larger set: |
1645 | | // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4 |
1646 | 556k | if (Range0.contains(Range1)) |
1647 | 3.18k | return IsAnd ? Cmp11.21k : Cmp01.96k ; |
1648 | 552k | if (Range1.contains(Range0)) |
1649 | 3.21k | return IsAnd ? Cmp094 : Cmp13.11k ; |
1650 | | |
1651 | 549k | return nullptr; |
1652 | 552k | } |
1653 | | |
1654 | | static Value *simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1, |
1655 | 7.14M | const InstrInfoQuery &IIQ) { |
1656 | | // (icmp (add V, C0), C1) & (icmp V, C0) |
1657 | 7.14M | CmpPredicate Pred0, Pred1; |
1658 | 7.14M | const APInt *C0, *C1; |
1659 | 7.14M | Value *V; |
1660 | 7.14M | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1)))) |
1661 | 6.96M | return nullptr; |
1662 | | |
1663 | 175k | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
1664 | 143k | return nullptr; |
1665 | | |
1666 | 32.1k | auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0)); |
1667 | 32.1k | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
1668 | 32.1k | return nullptr; |
1669 | | |
1670 | 0 | Type *ITy = Op0->getType(); |
1671 | 0 | bool IsNSW = IIQ.hasNoSignedWrap(AddInst); |
1672 | 0 | bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst); |
1673 | |
|
1674 | 0 | const APInt Delta = *C1 - *C0; |
1675 | 0 | if (C0->isStrictlyPositive()) { |
1676 | 0 | if (Delta == 2) { |
1677 | 0 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT) |
1678 | 0 | return getFalse(ITy); |
1679 | 0 | if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW) |
1680 | 0 | return getFalse(ITy); |
1681 | 0 | } |
1682 | 0 | if (Delta == 1) { |
1683 | 0 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT) |
1684 | 0 | return getFalse(ITy); |
1685 | 0 | if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW) |
1686 | 0 | return getFalse(ITy); |
1687 | 0 | } |
1688 | 0 | } |
1689 | 0 | if (C0->getBoolValue() && IsNUW) { |
1690 | 0 | if (Delta == 2) |
1691 | 0 | if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT) |
1692 | 0 | return getFalse(ITy); |
1693 | 0 | if (Delta == 1) |
1694 | 0 | if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT) |
1695 | 0 | return getFalse(ITy); |
1696 | 0 | } |
1697 | | |
1698 | 0 | return nullptr; |
1699 | 0 | } |
1700 | | |
1701 | | /// Try to simplify and/or of icmp with ctpop intrinsic. |
1702 | | static Value *simplifyAndOrOfICmpsWithCtpop(ICmpInst *Cmp0, ICmpInst *Cmp1, |
1703 | 15.5M | bool IsAnd) { |
1704 | 15.5M | CmpPredicate Pred0, Pred1; |
1705 | 15.5M | Value *X; |
1706 | 15.5M | const APInt *C; |
1707 | 15.5M | if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)), |
1708 | 15.5M | m_APInt(C))) || |
1709 | 15.5M | !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt()))1.98k || C->isZero()33 ) |
1710 | 15.5M | return nullptr; |
1711 | | |
1712 | | // (ctpop(X) == C) || (X != 0) --> X != 0 where C > 0 |
1713 | 33 | if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ26 && Pred1 == ICmpInst::ICMP_NE0 ) |
1714 | 0 | return Cmp1; |
1715 | | // (ctpop(X) != C) && (X == 0) --> X == 0 where C > 0 |
1716 | 33 | if (IsAnd && Pred0 == ICmpInst::ICMP_NE7 && Pred1 == ICmpInst::ICMP_EQ0 ) |
1717 | 0 | return Cmp1; |
1718 | | |
1719 | 33 | return nullptr; |
1720 | 33 | } |
1721 | | |
1722 | | static Value *simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1, |
1723 | 3.57M | const SimplifyQuery &Q) { |
1724 | 3.57M | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true, Q)) |
1725 | 5 | return X; |
1726 | 3.57M | if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true, Q)) |
1727 | 0 | return X; |
1728 | | |
1729 | 3.57M | if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true)) |
1730 | 1.50k | return X; |
1731 | | |
1732 | 3.57M | if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, true)) |
1733 | 0 | return X; |
1734 | 3.57M | if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, true)) |
1735 | 0 | return X; |
1736 | | |
1737 | 3.57M | if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1, Q.IIQ)) |
1738 | 0 | return X; |
1739 | 3.57M | if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0, Q.IIQ)) |
1740 | 0 | return X; |
1741 | | |
1742 | 3.57M | return nullptr; |
1743 | 3.57M | } |
1744 | | |
1745 | | static Value *simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1, |
1746 | 8.40M | const InstrInfoQuery &IIQ) { |
1747 | | // (icmp (add V, C0), C1) | (icmp V, C0) |
1748 | 8.40M | CmpPredicate Pred0, Pred1; |
1749 | 8.40M | const APInt *C0, *C1; |
1750 | 8.40M | Value *V; |
1751 | 8.40M | if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1)))) |
1752 | 6.89M | return nullptr; |
1753 | | |
1754 | 1.50M | if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value()))) |
1755 | 1.35M | return nullptr; |
1756 | | |
1757 | 151k | auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0)); |
1758 | 151k | if (AddInst->getOperand(1) != Op1->getOperand(1)) |
1759 | 151k | return nullptr; |
1760 | | |
1761 | 317 | Type *ITy = Op0->getType(); |
1762 | 317 | bool IsNSW = IIQ.hasNoSignedWrap(AddInst); |
1763 | 317 | bool IsNUW = IIQ.hasNoUnsignedWrap(AddInst); |
1764 | | |
1765 | 317 | const APInt Delta = *C1 - *C0; |
1766 | 317 | if (C0->isStrictlyPositive()) { |
1767 | 310 | if (Delta == 2) { |
1768 | 0 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE) |
1769 | 0 | return getTrue(ITy); |
1770 | 0 | if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW) |
1771 | 0 | return getTrue(ITy); |
1772 | 0 | } |
1773 | 310 | if (Delta == 1) { |
1774 | 308 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE0 ) |
1775 | 0 | return getTrue(ITy); |
1776 | 308 | if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE0 && IsNSW0 ) |
1777 | 0 | return getTrue(ITy); |
1778 | 308 | } |
1779 | 310 | } |
1780 | 317 | if (C0->getBoolValue() && IsNUW) { |
1781 | 0 | if (Delta == 2) |
1782 | 0 | if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE) |
1783 | 0 | return getTrue(ITy); |
1784 | 0 | if (Delta == 1) |
1785 | 0 | if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE) |
1786 | 0 | return getTrue(ITy); |
1787 | 0 | } |
1788 | | |
1789 | 317 | return nullptr; |
1790 | 317 | } |
1791 | | |
1792 | | static Value *simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1, |
1793 | 4.20M | const SimplifyQuery &Q) { |
1794 | 4.20M | if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false, Q)) |
1795 | 0 | return X; |
1796 | 4.20M | if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false, Q)) |
1797 | 1 | return X; |
1798 | | |
1799 | 4.20M | if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false)) |
1800 | 5.34k | return X; |
1801 | | |
1802 | 4.20M | if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op0, Op1, false)) |
1803 | 0 | return X; |
1804 | 4.20M | if (Value *X = simplifyAndOrOfICmpsWithCtpop(Op1, Op0, false)) |
1805 | 0 | return X; |
1806 | | |
1807 | 4.20M | if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1, Q.IIQ)) |
1808 | 0 | return X; |
1809 | 4.20M | if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0, Q.IIQ)) |
1810 | 0 | return X; |
1811 | | |
1812 | 4.20M | return nullptr; |
1813 | 4.20M | } |
1814 | | |
1815 | | static Value *simplifyAndOrOfFCmps(const SimplifyQuery &Q, FCmpInst *LHS, |
1816 | 576k | FCmpInst *RHS, bool IsAnd) { |
1817 | 576k | Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1); |
1818 | 576k | Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1); |
1819 | 576k | if (LHS0->getType() != RHS0->getType()) |
1820 | 5.33k | return nullptr; |
1821 | | |
1822 | 571k | FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
1823 | 571k | auto AbsOrSelfLHS0 = m_CombineOr(m_Specific(LHS0), m_FAbs(m_Specific(LHS0))); |
1824 | 571k | if ((PredL == FCmpInst::FCMP_ORD || PredL == FCmpInst::FCMP_UNO517k ) && |
1825 | 571k | (87.6k (87.6k FCmpInst::isOrdered(PredR)87.6k && IsAnd28.9k ) || |
1826 | 87.6k | (85.9k FCmpInst::isUnordered(PredR)85.9k && !IsAnd58.7k ))) { |
1827 | | // (fcmp ord X, 0) & (fcmp o** X/abs(X), Y) --> fcmp o** X/abs(X), Y |
1828 | | // (fcmp uno X, 0) & (fcmp o** X/abs(X), Y) --> false |
1829 | | // (fcmp uno X, 0) | (fcmp u** X/abs(X), Y) --> fcmp u** X/abs(X), Y |
1830 | | // (fcmp ord X, 0) | (fcmp u** X/abs(X), Y) --> true |
1831 | 2.32k | if ((match(RHS0, AbsOrSelfLHS0) || match(RHS1, AbsOrSelfLHS0)2.23k ) && |
1832 | 2.32k | match(LHS1, m_PosZeroFP())300 ) |
1833 | 288 | return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR) |
1834 | 288 | ? static_cast<Value *>(RHS)174 |
1835 | 288 | : ConstantInt::getBool(LHS->getType(), !IsAnd)114 ; |
1836 | 2.32k | } |
1837 | | |
1838 | 571k | auto AbsOrSelfRHS0 = m_CombineOr(m_Specific(RHS0), m_FAbs(m_Specific(RHS0))); |
1839 | 571k | if ((PredR == FCmpInst::FCMP_ORD || PredR == FCmpInst::FCMP_UNO569k ) && |
1840 | 571k | (9.27k (9.27k FCmpInst::isOrdered(PredL)9.27k && IsAnd2.77k ) || |
1841 | 9.27k | (8.51k FCmpInst::isUnordered(PredL)8.51k && !IsAnd6.49k ))) { |
1842 | | // (fcmp o** X/abs(X), Y) & (fcmp ord X, 0) --> fcmp o** X/abs(X), Y |
1843 | | // (fcmp o** X/abs(X), Y) & (fcmp uno X, 0) --> false |
1844 | | // (fcmp u** X/abs(X), Y) | (fcmp uno X, 0) --> fcmp u** X/abs(X), Y |
1845 | | // (fcmp u** X/abs(X), Y) | (fcmp ord X, 0) --> true |
1846 | 1.06k | if ((match(LHS0, AbsOrSelfRHS0) || match(LHS1, AbsOrSelfRHS0)1.04k ) && |
1847 | 1.06k | match(RHS1, m_PosZeroFP())213 ) |
1848 | 177 | return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR) |
1849 | 177 | ? static_cast<Value *>(LHS) |
1850 | 177 | : ConstantInt::getBool(LHS->getType(), !IsAnd)0 ; |
1851 | 1.06k | } |
1852 | | |
1853 | 571k | return nullptr; |
1854 | 571k | } |
1855 | | |
1856 | | static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0, |
1857 | 193M | Value *Op1, bool IsAnd) { |
1858 | | // Look through casts of the 'and' operands to find compares. |
1859 | 193M | auto *Cast0 = dyn_cast<CastInst>(Op0); |
1860 | 193M | auto *Cast1 = dyn_cast<CastInst>(Op1); |
1861 | 193M | if (Cast0 && Cast129.8M && Cast0->getOpcode() == Cast1->getOpcode()769k && |
1862 | 193M | Cast0->getSrcTy() == Cast1->getSrcTy()494k ) { |
1863 | 469k | Op0 = Cast0->getOperand(0); |
1864 | 469k | Op1 = Cast1->getOperand(0); |
1865 | 469k | } |
1866 | | |
1867 | 193M | Value *V = nullptr; |
1868 | 193M | auto *ICmp0 = dyn_cast<ICmpInst>(Op0); |
1869 | 193M | auto *ICmp1 = dyn_cast<ICmpInst>(Op1); |
1870 | 193M | if (ICmp0 && ICmp110.2M ) |
1871 | 7.77M | V = IsAnd ? simplifyAndOfICmps(ICmp0, ICmp1, Q)3.57M |
1872 | 7.77M | : simplifyOrOfICmps(ICmp0, ICmp1, Q)4.20M ; |
1873 | | |
1874 | 193M | auto *FCmp0 = dyn_cast<FCmpInst>(Op0); |
1875 | 193M | auto *FCmp1 = dyn_cast<FCmpInst>(Op1); |
1876 | 193M | if (FCmp0 && FCmp1780k ) |
1877 | 576k | V = simplifyAndOrOfFCmps(Q, FCmp0, FCmp1, IsAnd); |
1878 | | |
1879 | 193M | if (!V) |
1880 | 193M | return nullptr; |
1881 | 7.31k | if (!Cast0) |
1882 | 7.31k | return V; |
1883 | | |
1884 | | // If we looked through casts, we can only handle a constant simplification |
1885 | | // because we are not allowed to create a cast instruction here. |
1886 | 3 | if (auto *C = dyn_cast<Constant>(V)) |
1887 | 3 | return ConstantFoldCastOperand(Cast0->getOpcode(), C, Cast0->getType(), |
1888 | 3 | Q.DL); |
1889 | | |
1890 | 0 | return nullptr; |
1891 | 3 | } |
1892 | | |
1893 | | static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
1894 | | const SimplifyQuery &Q, |
1895 | | bool AllowRefinement, |
1896 | | SmallVectorImpl<Instruction *> *DropFlags, |
1897 | | unsigned MaxRecurse); |
1898 | | |
1899 | | static Value *simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1, |
1900 | | const SimplifyQuery &Q, |
1901 | 387M | unsigned MaxRecurse) { |
1902 | 387M | assert((Opcode == Instruction::And || Opcode == Instruction::Or) && |
1903 | 387M | "Must be and/or"); |
1904 | 387M | CmpPredicate Pred; |
1905 | 387M | Value *A, *B; |
1906 | 387M | if (!match(Op0, m_ICmp(Pred, m_Value(A), m_Value(B))) || |
1907 | 387M | !ICmpInst::isEquality(Pred)19.4M ) |
1908 | 374M | return nullptr; |
1909 | | |
1910 | 13.1M | auto Simplify = [&](Value *Res) -> Value * { |
1911 | 1.95M | Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, Res->getType()); |
1912 | | |
1913 | | // and (icmp eq a, b), x implies (a==b) inside x. |
1914 | | // or (icmp ne a, b), x implies (a==b) inside x. |
1915 | | // If x simplifies to true/false, we can simplify the and/or. |
1916 | 1.95M | if (Pred == |
1917 | 1.95M | (Opcode == Instruction::And ? ICmpInst::ICMP_EQ360k : ICmpInst::ICMP_NE1.59M )) { |
1918 | 45.3k | if (Res == Absorber) |
1919 | 6.01k | return Absorber; |
1920 | 39.3k | if (Res == ConstantExpr::getBinOpIdentity(Opcode, Res->getType())) |
1921 | 37.6k | return Op0; |
1922 | 1.74k | return nullptr; |
1923 | 39.3k | } |
1924 | | |
1925 | | // If we have and (icmp ne a, b), x and for a==b we can simplify x to false, |
1926 | | // then we can drop the icmp, as x will already be false in the case where |
1927 | | // the icmp is false. Similar for or and true. |
1928 | 1.91M | if (Res == Absorber) |
1929 | 29.6k | return Op1; |
1930 | 1.88M | return nullptr; |
1931 | 1.91M | }; |
1932 | | |
1933 | | // In the final case (Res == Absorber with inverted predicate), it is safe to |
1934 | | // refine poison during simplification, but not undef. For simplicity always |
1935 | | // disable undef-based folds here. |
1936 | 13.1M | if (Value *Res = simplifyWithOpReplaced(Op1, A, B, Q.getWithoutUndef(), |
1937 | 13.1M | /* AllowRefinement */ true, |
1938 | 13.1M | /* DropFlags */ nullptr, MaxRecurse)) |
1939 | 1.94M | return Simplify(Res); |
1940 | 11.2M | if (Value *Res = simplifyWithOpReplaced(Op1, B, A, Q.getWithoutUndef(), |
1941 | 11.2M | /* AllowRefinement */ true, |
1942 | 11.2M | /* DropFlags */ nullptr, MaxRecurse)) |
1943 | 13.8k | return Simplify(Res); |
1944 | | |
1945 | 11.2M | return nullptr; |
1946 | 11.2M | } |
1947 | | |
1948 | | /// Given a bitwise logic op, check if the operands are add/sub with a common |
1949 | | /// source value and inverted constant (identity: C - X -> ~(X + ~C)). |
1950 | | static Value *simplifyLogicOfAddSub(Value *Op0, Value *Op1, |
1951 | 234M | Instruction::BinaryOps Opcode) { |
1952 | 234M | assert(Op0->getType() == Op1->getType() && "Mismatched binop types"); |
1953 | 234M | assert(BinaryOperator::isBitwiseLogicOp(Opcode) && "Expected logic op"); |
1954 | 234M | Value *X; |
1955 | 234M | Constant *C1, *C2; |
1956 | 234M | if ((match(Op0, m_Add(m_Value(X), m_Constant(C1))) && |
1957 | 234M | match(Op1, m_Sub(m_Constant(C2), m_Specific(X)))10.2M ) || |
1958 | 234M | (234M match(Op1, m_Add(m_Value(X), m_Constant(C1)))234M && |
1959 | 234M | match(Op0, m_Sub(m_Constant(C2), m_Specific(X)))4.37M )) { |
1960 | 5.15k | if (ConstantExpr::getNot(C1) == C2) { |
1961 | | // (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0 |
1962 | | // (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1 |
1963 | | // (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1 |
1964 | 359 | Type *Ty = Op0->getType(); |
1965 | 359 | return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)34 |
1966 | 359 | : ConstantInt::getAllOnesValue(Ty)325 ; |
1967 | 359 | } |
1968 | 5.15k | } |
1969 | 234M | return nullptr; |
1970 | 234M | } |
1971 | | |
1972 | | // Commutative patterns for and that will be tried with both operand orders. |
1973 | | static Value *simplifyAndCommutative(Value *Op0, Value *Op1, |
1974 | | const SimplifyQuery &Q, |
1975 | 195M | unsigned MaxRecurse) { |
1976 | | // ~A & A = 0 |
1977 | 195M | if (match(Op0, m_Not(m_Specific(Op1)))) |
1978 | 1.37k | return Constant::getNullValue(Op0->getType()); |
1979 | | |
1980 | | // (A | ?) & A = A |
1981 | 195M | if (match(Op0, m_c_Or(m_Specific(Op1), m_Value()))) |
1982 | 60.0k | return Op1; |
1983 | | |
1984 | | // (X | ~Y) & (X | Y) --> X |
1985 | 195M | Value *X, *Y; |
1986 | 195M | if (match(Op0, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) && |
1987 | 195M | match(Op1, m_c_Or(m_Specific(X), m_Specific(Y)))10.4k ) |
1988 | 0 | return X; |
1989 | | |
1990 | | // If we have a multiplication overflow check that is being 'and'ed with a |
1991 | | // check that one of the multipliers is not zero, we can omit the 'and', and |
1992 | | // only keep the overflow check. |
1993 | 195M | if (isCheckForZeroAndMulWithOverflow(Op0, Op1, true)) |
1994 | 2 | return Op1; |
1995 | | |
1996 | | // -A & A = A if A is a power of two or zero. |
1997 | 195M | if (match(Op0, m_Neg(m_Specific(Op1))) && |
1998 | 195M | isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI, Q.DT)21.3k ) |
1999 | 414 | return Op1; |
2000 | | |
2001 | | // This is a similar pattern used for checking if a value is a power-of-2: |
2002 | | // (A - 1) & A --> 0 (if A is a power-of-2 or 0) |
2003 | 195M | if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) && |
2004 | 195M | isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI, Q.DT)909k ) |
2005 | 19 | return Constant::getNullValue(Op1->getType()); |
2006 | | |
2007 | | // (x << N) & ((x << M) - 1) --> 0, where x is known to be a power of 2 and |
2008 | | // M <= N. |
2009 | 195M | const APInt *Shift1, *Shift2; |
2010 | 195M | if (match(Op0, m_Shl(m_Value(X), m_APInt(Shift1))) && |
2011 | 195M | match(Op1, m_Add(m_Shl(m_Specific(X), m_APInt(Shift2)), m_AllOnes()))1.94M && |
2012 | 195M | isKnownToBeAPowerOfTwo(X, Q.DL, /*OrZero*/ true, Q.AC, Q.CxtI)30 && |
2013 | 195M | Shift1->uge(*Shift2)0 ) |
2014 | 0 | return Constant::getNullValue(Op0->getType()); |
2015 | | |
2016 | 195M | if (Value *V = |
2017 | 195M | simplifyAndOrWithICmpEq(Instruction::And, Op0, Op1, Q, MaxRecurse)) |
2018 | 26.9k | return V; |
2019 | | |
2020 | 195M | return nullptr; |
2021 | 195M | } |
2022 | | |
2023 | | /// Given operands for an And, see if we can fold the result. |
2024 | | /// If not, this returns null. |
2025 | | static Value *simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
2026 | 103M | unsigned MaxRecurse) { |
2027 | 103M | if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q)) |
2028 | 4.11M | return C; |
2029 | | |
2030 | | // X & poison -> poison |
2031 | 99.8M | if (isa<PoisonValue>(Op1)) |
2032 | 1.14k | return Op1; |
2033 | | |
2034 | | // X & undef -> 0 |
2035 | 99.8M | if (Q.isUndefValue(Op1)) |
2036 | 19.2k | return Constant::getNullValue(Op0->getType()); |
2037 | | |
2038 | | // X & X = X |
2039 | 99.8M | if (Op0 == Op1) |
2040 | 10.9k | return Op0; |
2041 | | |
2042 | | // X & 0 = 0 |
2043 | 99.7M | if (match(Op1, m_Zero())) |
2044 | 782k | return Constant::getNullValue(Op0->getType()); |
2045 | | |
2046 | | // X & -1 = X |
2047 | 99.0M | if (match(Op1, m_AllOnes())) |
2048 | 1.11M | return Op0; |
2049 | | |
2050 | 97.9M | if (Value *Res = simplifyAndCommutative(Op0, Op1, Q, MaxRecurse)) |
2051 | 75.4k | return Res; |
2052 | 97.8M | if (Value *Res = simplifyAndCommutative(Op1, Op0, Q, MaxRecurse)) |
2053 | 13.3k | return Res; |
2054 | | |
2055 | 97.8M | if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::And)) |
2056 | 34 | return V; |
2057 | | |
2058 | | // A mask that only clears known zeros of a shifted value is a no-op. |
2059 | 97.8M | const APInt *Mask; |
2060 | 97.8M | const APInt *ShAmt; |
2061 | 97.8M | Value *X, *Y; |
2062 | 97.8M | if (match(Op1, m_APInt(Mask))) { |
2063 | | // If all bits in the inverted and shifted mask are clear: |
2064 | | // and (shl X, ShAmt), Mask --> shl X, ShAmt |
2065 | 76.1M | if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) && |
2066 | 76.1M | (~(*Mask)).lshr(*ShAmt).isZero()1.74M ) |
2067 | 172k | return Op0; |
2068 | | |
2069 | | // If all bits in the inverted and shifted mask are clear: |
2070 | | // and (lshr X, ShAmt), Mask --> lshr X, ShAmt |
2071 | 75.9M | if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) && |
2072 | 75.9M | (~(*Mask)).shl(*ShAmt).isZero()4.10M ) |
2073 | 43.9k | return Op0; |
2074 | 75.9M | } |
2075 | | |
2076 | | // and 2^x-1, 2^C --> 0 where x <= C. |
2077 | 97.5M | const APInt *PowerC; |
2078 | 97.5M | Value *Shift; |
2079 | 97.5M | if (match(Op1, m_Power2(PowerC)) && |
2080 | 97.5M | match(Op0, m_Add(m_Value(Shift), m_AllOnes()))27.1M && |
2081 | 97.5M | isKnownToBeAPowerOfTwo(Shift, Q.DL, /*OrZero*/ false, Q.AC, Q.CxtI, |
2082 | 24.7k | Q.DT)) { |
2083 | 143 | KnownBits Known = computeKnownBits(Shift, Q); |
2084 | | // Use getActiveBits() to make use of the additional power of two knowledge |
2085 | 143 | if (PowerC->getActiveBits() >= Known.getMaxValue().getActiveBits()) |
2086 | 4 | return ConstantInt::getNullValue(Op1->getType()); |
2087 | 143 | } |
2088 | | |
2089 | 97.5M | if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true)) |
2090 | 1.95k | return V; |
2091 | | |
2092 | | // Try some generic simplifications for associative operations. |
2093 | 97.5M | if (Value *V = |
2094 | 97.5M | simplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, MaxRecurse)) |
2095 | 651k | return V; |
2096 | | |
2097 | | // And distributes over Or. Try some generic simplifications based on this. |
2098 | 96.9M | if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1, |
2099 | 96.9M | Instruction::Or, Q, MaxRecurse)) |
2100 | 288k | return V; |
2101 | | |
2102 | | // And distributes over Xor. Try some generic simplifications based on this. |
2103 | 96.6M | if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1, |
2104 | 96.6M | Instruction::Xor, Q, MaxRecurse)) |
2105 | 3.58k | return V; |
2106 | | |
2107 | 96.6M | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)96.0M ) { |
2108 | 797k | if (Op0->getType()->isIntOrIntVectorTy(1)) { |
2109 | | // A & (A && B) -> A && B |
2110 | 79.6k | if (match(Op1, m_Select(m_Specific(Op0), m_Value(), m_Zero()))) |
2111 | 141 | return Op1; |
2112 | 79.5k | else if (match(Op0, m_Select(m_Specific(Op1), m_Value(), m_Zero()))) |
2113 | 388 | return Op0; |
2114 | 79.6k | } |
2115 | | // If the operation is with the result of a select instruction, check |
2116 | | // whether operating on either branch of the select always yields the same |
2117 | | // value. |
2118 | 797k | if (Value *V = |
2119 | 797k | threadBinOpOverSelect(Instruction::And, Op0, Op1, Q, MaxRecurse)) |
2120 | 65.4k | return V; |
2121 | 797k | } |
2122 | | |
2123 | | // If the operation is with the result of a phi instruction, check whether |
2124 | | // operating on all incoming values of the phi always yields the same value. |
2125 | 96.5M | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)90.7M ) |
2126 | 7.20M | if (Value *V = |
2127 | 7.20M | threadBinOpOverPHI(Instruction::And, Op0, Op1, Q, MaxRecurse)) |
2128 | 7.47k | return V; |
2129 | | |
2130 | | // Assuming the effective width of Y is not larger than A, i.e. all bits |
2131 | | // from X and Y are disjoint in (X << A) | Y, |
2132 | | // if the mask of this AND op covers all bits of X or Y, while it covers |
2133 | | // no bits from the other, we can bypass this AND op. E.g., |
2134 | | // ((X << A) | Y) & Mask -> Y, |
2135 | | // if Mask = ((1 << effective_width_of(Y)) - 1) |
2136 | | // ((X << A) | Y) & Mask -> X << A, |
2137 | | // if Mask = ((1 << effective_width_of(X)) - 1) << A |
2138 | | // SimplifyDemandedBits in InstCombine can optimize the general case. |
2139 | | // This pattern aims to help other passes for a common case. |
2140 | 96.5M | Value *XShifted; |
2141 | 96.5M | if (Q.IIQ.UseInstrInfo && match(Op1, m_APInt(Mask)) && |
2142 | 96.5M | match(Op0, m_c_Or(m_CombineAnd(m_NUWShl(m_Value(X), m_APInt(ShAmt)), |
2143 | 74.8M | m_Value(XShifted)), |
2144 | 74.8M | m_Value(Y)))) { |
2145 | 165k | const unsigned Width = Op0->getType()->getScalarSizeInBits(); |
2146 | 165k | const unsigned ShftCnt = ShAmt->getLimitedValue(Width); |
2147 | 165k | const KnownBits YKnown = computeKnownBits(Y, Q); |
2148 | 165k | const unsigned EffWidthY = YKnown.countMaxActiveBits(); |
2149 | 165k | if (EffWidthY <= ShftCnt) { |
2150 | 105k | const KnownBits XKnown = computeKnownBits(X, Q); |
2151 | 105k | const unsigned EffWidthX = XKnown.countMaxActiveBits(); |
2152 | 105k | const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY); |
2153 | 105k | const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt; |
2154 | | // If the mask is extracting all bits from X or Y as is, we can skip |
2155 | | // this AND op. |
2156 | 105k | if (EffBitsY.isSubsetOf(*Mask) && !EffBitsX.intersects(*Mask)52.7k ) |
2157 | 9.25k | return Y; |
2158 | 96.0k | if (EffBitsX.isSubsetOf(*Mask) && !EffBitsY.intersects(*Mask)14.7k ) |
2159 | 4.38k | return XShifted; |
2160 | 96.0k | } |
2161 | 165k | } |
2162 | | |
2163 | | // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0 |
2164 | | // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0 |
2165 | 96.5M | BinaryOperator *Or; |
2166 | 96.5M | if (match(Op0, m_c_Xor(m_Value(X), |
2167 | 96.5M | m_CombineAnd(m_BinOp(Or), |
2168 | 96.5M | m_c_Or(m_Deferred(X), m_Value(Y))))) && |
2169 | 96.5M | match(Op1, m_c_Xor(m_Specific(Or), m_Specific(Y)))42 ) |
2170 | 0 | return Constant::getNullValue(Op0->getType()); |
2171 | | |
2172 | 96.5M | const APInt *C1; |
2173 | 96.5M | Value *A; |
2174 | | // (A ^ C) & (A ^ ~C) -> 0 |
2175 | 96.5M | if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) && |
2176 | 96.5M | match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1)))391k ) |
2177 | 0 | return Constant::getNullValue(Op0->getType()); |
2178 | | |
2179 | 96.5M | if (Op0->getType()->isIntOrIntVectorTy(1)) { |
2180 | 6.71M | if (std::optional<bool> Implied = isImpliedCondition(Op0, Op1, Q.DL)) { |
2181 | | // If Op0 is true implies Op1 is true, then Op0 is a subset of Op1. |
2182 | 561 | if (*Implied == true) |
2183 | 451 | return Op0; |
2184 | | // If Op0 is true implies Op1 is false, then they are not true together. |
2185 | 110 | if (*Implied == false) |
2186 | 110 | return ConstantInt::getFalse(Op0->getType()); |
2187 | 110 | } |
2188 | 6.71M | if (std::optional<bool> Implied = isImpliedCondition(Op1, Op0, Q.DL)) { |
2189 | | // If Op1 is true implies Op0 is true, then Op1 is a subset of Op0. |
2190 | 847 | if (*Implied) |
2191 | 847 | return Op1; |
2192 | | // If Op1 is true implies Op0 is false, then they are not true together. |
2193 | 0 | if (!*Implied) |
2194 | 0 | return ConstantInt::getFalse(Op1->getType()); |
2195 | 0 | } |
2196 | 6.71M | } |
2197 | | |
2198 | 96.5M | if (Value *V = simplifyByDomEq(Instruction::And, Op0, Op1, Q, MaxRecurse)) |
2199 | 501 | return V; |
2200 | | |
2201 | 96.5M | return nullptr; |
2202 | 96.5M | } |
2203 | | |
2204 | 34.3M | Value *llvm::simplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
2205 | 34.3M | return ::simplifyAndInst(Op0, Op1, Q, RecursionLimit); |
2206 | 34.3M | } |
2207 | | |
2208 | | // TODO: Many of these folds could use LogicalAnd/LogicalOr. |
2209 | 192M | static Value *simplifyOrLogic(Value *X, Value *Y) { |
2210 | 192M | assert(X->getType() == Y->getType() && "Expected same type for 'or' ops"); |
2211 | 192M | Type *Ty = X->getType(); |
2212 | | |
2213 | | // X | ~X --> -1 |
2214 | 192M | if (match(Y, m_Not(m_Specific(X)))) |
2215 | 12.4k | return ConstantInt::getAllOnesValue(Ty); |
2216 | | |
2217 | | // X | ~(X & ?) = -1 |
2218 | 192M | if (match(Y, m_Not(m_c_And(m_Specific(X), m_Value())))) |
2219 | 433 | return ConstantInt::getAllOnesValue(Ty); |
2220 | | |
2221 | | // X | (X & ?) --> X |
2222 | 192M | if (match(Y, m_c_And(m_Specific(X), m_Value()))) |
2223 | 485k | return X; |
2224 | | |
2225 | 191M | Value *A, *B; |
2226 | | |
2227 | | // (A ^ B) | (A | B) --> A | B |
2228 | | // (A ^ B) | (B | A) --> B | A |
2229 | 191M | if (match(X, m_Xor(m_Value(A), m_Value(B))) && |
2230 | 191M | match(Y, m_c_Or(m_Specific(A), m_Specific(B)))578k ) |
2231 | 172 | return Y; |
2232 | | |
2233 | | // ~(A ^ B) | (A | B) --> -1 |
2234 | | // ~(A ^ B) | (B | A) --> -1 |
2235 | 191M | if (match(X, m_Not(m_Xor(m_Value(A), m_Value(B)))) && |
2236 | 191M | match(Y, m_c_Or(m_Specific(A), m_Specific(B)))3.00k ) |
2237 | 0 | return ConstantInt::getAllOnesValue(Ty); |
2238 | | |
2239 | | // (A & ~B) | (A ^ B) --> A ^ B |
2240 | | // (~B & A) | (A ^ B) --> A ^ B |
2241 | | // (A & ~B) | (B ^ A) --> B ^ A |
2242 | | // (~B & A) | (B ^ A) --> B ^ A |
2243 | 191M | if (match(X, m_c_And(m_Value(A), m_Not(m_Value(B)))) && |
2244 | 191M | match(Y, m_c_Xor(m_Specific(A), m_Specific(B)))235k ) |
2245 | 0 | return Y; |
2246 | | |
2247 | | // (~A ^ B) | (A & B) --> ~A ^ B |
2248 | | // (B ^ ~A) | (A & B) --> B ^ ~A |
2249 | | // (~A ^ B) | (B & A) --> ~A ^ B |
2250 | | // (B ^ ~A) | (B & A) --> B ^ ~A |
2251 | 191M | if (match(X, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) && |
2252 | 191M | match(Y, m_c_And(m_Specific(A), m_Specific(B)))212 ) |
2253 | 0 | return X; |
2254 | | |
2255 | | // (~A | B) | (A ^ B) --> -1 |
2256 | | // (~A | B) | (B ^ A) --> -1 |
2257 | | // (B | ~A) | (A ^ B) --> -1 |
2258 | | // (B | ~A) | (B ^ A) --> -1 |
2259 | 191M | if (match(X, m_c_Or(m_Not(m_Value(A)), m_Value(B))) && |
2260 | 191M | match(Y, m_c_Xor(m_Specific(A), m_Specific(B)))8.91k ) |
2261 | 0 | return ConstantInt::getAllOnesValue(Ty); |
2262 | | |
2263 | | // (~A & B) | ~(A | B) --> ~A |
2264 | | // (~A & B) | ~(B | A) --> ~A |
2265 | | // (B & ~A) | ~(A | B) --> ~A |
2266 | | // (B & ~A) | ~(B | A) --> ~A |
2267 | 191M | Value *NotA; |
2268 | 191M | if (match(X, m_c_And(m_CombineAnd(m_Value(NotA), m_Not(m_Value(A))), |
2269 | 191M | m_Value(B))) && |
2270 | 191M | match(Y, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))235k ) |
2271 | 0 | return NotA; |
2272 | | // The same is true of Logical And |
2273 | | // TODO: This could share the logic of the version above if there was a |
2274 | | // version of LogicalAnd that allowed more than just i1 types. |
2275 | 191M | if (match(X, m_c_LogicalAnd(m_CombineAnd(m_Value(NotA), m_Not(m_Value(A))), |
2276 | 191M | m_Value(B))) && |
2277 | 191M | match(Y, m_Not(m_c_LogicalOr(m_Specific(A), m_Specific(B))))4.99k ) |
2278 | 0 | return NotA; |
2279 | | |
2280 | | // ~(A ^ B) | (A & B) --> ~(A ^ B) |
2281 | | // ~(A ^ B) | (B & A) --> ~(A ^ B) |
2282 | 191M | Value *NotAB; |
2283 | 191M | if (match(X, m_CombineAnd(m_Not(m_Xor(m_Value(A), m_Value(B))), |
2284 | 191M | m_Value(NotAB))) && |
2285 | 191M | match(Y, m_c_And(m_Specific(A), m_Specific(B)))3.00k ) |
2286 | 0 | return NotAB; |
2287 | | |
2288 | | // ~(A & B) | (A ^ B) --> ~(A & B) |
2289 | | // ~(A & B) | (B ^ A) --> ~(A & B) |
2290 | 191M | if (match(X, m_CombineAnd(m_Not(m_And(m_Value(A), m_Value(B))), |
2291 | 191M | m_Value(NotAB))) && |
2292 | 191M | match(Y, m_c_Xor(m_Specific(A), m_Specific(B)))10.3k ) |
2293 | 0 | return NotAB; |
2294 | | |
2295 | 191M | return nullptr; |
2296 | 191M | } |
2297 | | |
2298 | | /// Given operands for an Or, see if we can fold the result. |
2299 | | /// If not, this returns null. |
2300 | | static Value *simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
2301 | 104M | unsigned MaxRecurse) { |
2302 | 104M | if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q)) |
2303 | 4.94M | return C; |
2304 | | |
2305 | | // X | poison -> poison |
2306 | 99.6M | if (isa<PoisonValue>(Op1)) |
2307 | 165 | return Op1; |
2308 | | |
2309 | | // X | undef -> -1 |
2310 | | // X | -1 = -1 |
2311 | | // Do not return Op1 because it may contain undef elements if it's a vector. |
2312 | 99.6M | if (Q.isUndefValue(Op1) || match(Op1, m_AllOnes())99.6M ) |
2313 | 104k | return Constant::getAllOnesValue(Op0->getType()); |
2314 | | |
2315 | | // X | X = X |
2316 | | // X | 0 = X |
2317 | 99.5M | if (Op0 == Op1 || match(Op1, m_Zero())99.4M ) |
2318 | 3.12M | return Op0; |
2319 | | |
2320 | 96.3M | if (Value *R = simplifyOrLogic(Op0, Op1)) |
2321 | 414k | return R; |
2322 | 95.9M | if (Value *R = simplifyOrLogic(Op1, Op0)) |
2323 | 83.6k | return R; |
2324 | | |
2325 | 95.8M | if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Or)) |
2326 | 325 | return V; |
2327 | | |
2328 | | // Rotated -1 is still -1: |
2329 | | // (-1 << X) | (-1 >> (C - X)) --> -1 |
2330 | | // (-1 >> X) | (-1 << (C - X)) --> -1 |
2331 | | // ...with C <= bitwidth (and commuted variants). |
2332 | 95.8M | Value *X, *Y; |
2333 | 95.8M | if ((match(Op0, m_Shl(m_AllOnes(), m_Value(X))) && |
2334 | 95.8M | match(Op1, m_LShr(m_AllOnes(), m_Value(Y)))11.5k ) || |
2335 | 95.8M | (match(Op1, m_Shl(m_AllOnes(), m_Value(X))) && |
2336 | 95.8M | match(Op0, m_LShr(m_AllOnes(), m_Value(Y)))65.5k )) { |
2337 | 0 | const APInt *C; |
2338 | 0 | if ((match(X, m_Sub(m_APInt(C), m_Specific(Y))) || |
2339 | 0 | match(Y, m_Sub(m_APInt(C), m_Specific(X)))) && |
2340 | 0 | C->ule(X->getType()->getScalarSizeInBits())) { |
2341 | 0 | return ConstantInt::getAllOnesValue(X->getType()); |
2342 | 0 | } |
2343 | 0 | } |
2344 | | |
2345 | | // A funnel shift (rotate) can be decomposed into simpler shifts. See if we |
2346 | | // are mixing in another shift that is redundant with the funnel shift. |
2347 | | |
2348 | | // (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y |
2349 | | // (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y |
2350 | 95.8M | if (match(Op0, |
2351 | 95.8M | m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) && |
2352 | 95.8M | match(Op1, m_Shl(m_Specific(X), m_Specific(Y)))40.5k ) |
2353 | 0 | return Op0; |
2354 | 95.8M | if (match(Op1, |
2355 | 95.8M | m_Intrinsic<Intrinsic::fshl>(m_Value(X), m_Value(), m_Value(Y))) && |
2356 | 95.8M | match(Op0, m_Shl(m_Specific(X), m_Specific(Y)))28.3k ) |
2357 | 0 | return Op1; |
2358 | | |
2359 | | // (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y |
2360 | | // (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y |
2361 | 95.8M | if (match(Op0, |
2362 | 95.8M | m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) && |
2363 | 95.8M | match(Op1, m_LShr(m_Specific(X), m_Specific(Y)))3.03k ) |
2364 | 0 | return Op0; |
2365 | 95.8M | if (match(Op1, |
2366 | 95.8M | m_Intrinsic<Intrinsic::fshr>(m_Value(), m_Value(X), m_Value(Y))) && |
2367 | 95.8M | match(Op0, m_LShr(m_Specific(X), m_Specific(Y)))1.05k ) |
2368 | 0 | return Op1; |
2369 | | |
2370 | 95.8M | if (Value *V = |
2371 | 95.8M | simplifyAndOrWithICmpEq(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
2372 | 40.7k | return V; |
2373 | 95.8M | if (Value *V = |
2374 | 95.8M | simplifyAndOrWithICmpEq(Instruction::Or, Op1, Op0, Q, MaxRecurse)) |
2375 | 5.63k | return V; |
2376 | | |
2377 | 95.8M | if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false)) |
2378 | 5.35k | return V; |
2379 | | |
2380 | | // If we have a multiplication overflow check that is being 'and'ed with a |
2381 | | // check that one of the multipliers is not zero, we can omit the 'and', and |
2382 | | // only keep the overflow check. |
2383 | 95.8M | if (isCheckForZeroAndMulWithOverflow(Op0, Op1, false)) |
2384 | 0 | return Op1; |
2385 | 95.8M | if (isCheckForZeroAndMulWithOverflow(Op1, Op0, false)) |
2386 | 0 | return Op0; |
2387 | | |
2388 | | // Try some generic simplifications for associative operations. |
2389 | 95.8M | if (Value *V = |
2390 | 95.8M | simplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
2391 | 63.9k | return V; |
2392 | | |
2393 | | // Or distributes over And. Try some generic simplifications based on this. |
2394 | 95.7M | if (Value *V = expandCommutativeBinOp(Instruction::Or, Op0, Op1, |
2395 | 95.7M | Instruction::And, Q, MaxRecurse)) |
2396 | 406 | return V; |
2397 | | |
2398 | 95.7M | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)92.3M ) { |
2399 | 4.75M | if (Op0->getType()->isIntOrIntVectorTy(1)) { |
2400 | | // A | (A || B) -> A || B |
2401 | 76.1k | if (match(Op1, m_Select(m_Specific(Op0), m_One(), m_Value()))) |
2402 | 2 | return Op1; |
2403 | 76.1k | else if (match(Op0, m_Select(m_Specific(Op1), m_One(), m_Value()))) |
2404 | 11 | return Op0; |
2405 | 76.1k | } |
2406 | | // If the operation is with the result of a select instruction, check |
2407 | | // whether operating on either branch of the select always yields the same |
2408 | | // value. |
2409 | 4.75M | if (Value *V = |
2410 | 4.75M | threadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
2411 | 50.6k | return V; |
2412 | 4.75M | } |
2413 | | |
2414 | | // (A & C1)|(B & C2) |
2415 | 95.7M | Value *A, *B; |
2416 | 95.7M | const APInt *C1, *C2; |
2417 | 95.7M | if (match(Op0, m_And(m_Value(A), m_APInt(C1))) && |
2418 | 95.7M | match(Op1, m_And(m_Value(B), m_APInt(C2)))15.6M ) { |
2419 | 4.06M | if (*C1 == ~*C2) { |
2420 | | // (A & C1)|(B & C2) |
2421 | | // If we have: ((V + N) & C1) | (V & C2) |
2422 | | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
2423 | | // replace with V+N. |
2424 | 1.78M | Value *N; |
2425 | 1.78M | if (C2->isMask() && // C2 == 0+1+ |
2426 | 1.78M | match(A, m_c_Add(m_Specific(B), m_Value(N)))277k ) { |
2427 | | // Add commutes, try both ways. |
2428 | 27 | if (MaskedValueIsZero(N, *C2, Q)) |
2429 | 27 | return A; |
2430 | 27 | } |
2431 | | // Or commutes, try both ways. |
2432 | 1.78M | if (C1->isMask() && match(B, m_c_Add(m_Specific(A), m_Value(N)))163k ) { |
2433 | | // Add commutes, try both ways. |
2434 | 44 | if (MaskedValueIsZero(N, *C1, Q)) |
2435 | 44 | return B; |
2436 | 44 | } |
2437 | 1.78M | } |
2438 | 4.06M | } |
2439 | | |
2440 | | // If the operation is with the result of a phi instruction, check whether |
2441 | | // operating on all incoming values of the phi always yields the same value. |
2442 | 95.7M | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)92.0M ) |
2443 | 5.04M | if (Value *V = threadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
2444 | 10.4k | return V; |
2445 | | |
2446 | | // (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one. |
2447 | 95.7M | if (match(Op0, m_Xor(m_Value(A), m_APInt(C1))) && |
2448 | 95.7M | match(Op1, m_Xor(m_Specific(A), m_SpecificInt(~*C1)))187k ) |
2449 | 0 | return Constant::getAllOnesValue(Op0->getType()); |
2450 | | |
2451 | 95.7M | if (Op0->getType()->isIntOrIntVectorTy(1)) { |
2452 | 6.29M | if (std::optional<bool> Implied = |
2453 | 6.29M | isImpliedCondition(Op0, Op1, Q.DL, false)) { |
2454 | | // If Op0 is false implies Op1 is false, then Op1 is a subset of Op0. |
2455 | 3.63k | if (*Implied == false) |
2456 | 3.57k | return Op0; |
2457 | | // If Op0 is false implies Op1 is true, then at least one is always true. |
2458 | 59 | if (*Implied == true) |
2459 | 59 | return ConstantInt::getTrue(Op0->getType()); |
2460 | 59 | } |
2461 | 6.28M | if (std::optional<bool> Implied = |
2462 | 6.28M | isImpliedCondition(Op1, Op0, Q.DL, false)) { |
2463 | | // If Op1 is false implies Op0 is false, then Op0 is a subset of Op1. |
2464 | 916 | if (*Implied == false) |
2465 | 916 | return Op1; |
2466 | | // If Op1 is false implies Op0 is true, then at least one is always true. |
2467 | 0 | if (*Implied == true) |
2468 | 0 | return ConstantInt::getTrue(Op1->getType()); |
2469 | 0 | } |
2470 | 6.28M | } |
2471 | | |
2472 | 95.7M | if (Value *V = simplifyByDomEq(Instruction::Or, Op0, Op1, Q, MaxRecurse)) |
2473 | 822 | return V; |
2474 | | |
2475 | 95.7M | return nullptr; |
2476 | 95.7M | } |
2477 | | |
2478 | 12.5M | Value *llvm::simplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
2479 | 12.5M | return ::simplifyOrInst(Op0, Op1, Q, RecursionLimit); |
2480 | 12.5M | } |
2481 | | |
2482 | | /// Given operands for a Xor, see if we can fold the result. |
2483 | | /// If not, this returns null. |
2484 | | static Value *simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
2485 | 41.3M | unsigned MaxRecurse) { |
2486 | 41.3M | if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q)) |
2487 | 321k | return C; |
2488 | | |
2489 | | // X ^ poison -> poison |
2490 | 41.0M | if (isa<PoisonValue>(Op1)) |
2491 | 2 | return Op1; |
2492 | | |
2493 | | // A ^ undef -> undef |
2494 | 41.0M | if (Q.isUndefValue(Op1)) |
2495 | 3 | return Op1; |
2496 | | |
2497 | | // A ^ 0 = A |
2498 | 41.0M | if (match(Op1, m_Zero())) |
2499 | 210k | return Op0; |
2500 | | |
2501 | | // A ^ A = 0 |
2502 | 40.8M | if (Op0 == Op1) |
2503 | 43.1k | return Constant::getNullValue(Op0->getType()); |
2504 | | |
2505 | | // A ^ ~A = ~A ^ A = -1 |
2506 | 40.7M | if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0)))40.7M ) |
2507 | 16 | return Constant::getAllOnesValue(Op0->getType()); |
2508 | | |
2509 | 81.5M | auto foldAndOrNot = [](Value *X, Value *Y) -> Value * 40.7M { |
2510 | 81.5M | Value *A, *B; |
2511 | | // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants. |
2512 | 81.5M | if (match(X, m_c_And(m_Not(m_Value(A)), m_Value(B))) && |
2513 | 81.5M | match(Y, m_c_Or(m_Specific(A), m_Specific(B)))1.68M ) |
2514 | 0 | return A; |
2515 | | |
2516 | | // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants. |
2517 | | // The 'not' op must contain a complete -1 operand (no undef elements for |
2518 | | // vector) for the transform to be safe. |
2519 | 81.5M | Value *NotA; |
2520 | 81.5M | if (match(X, m_c_Or(m_CombineAnd(m_Not(m_Value(A)), m_Value(NotA)), |
2521 | 81.5M | m_Value(B))) && |
2522 | 81.5M | match(Y, m_c_And(m_Specific(A), m_Specific(B)))134k ) |
2523 | 0 | return NotA; |
2524 | | |
2525 | 81.5M | return nullptr; |
2526 | 81.5M | }; |
2527 | 40.7M | if (Value *R = foldAndOrNot(Op0, Op1)) |
2528 | 0 | return R; |
2529 | 40.7M | if (Value *R = foldAndOrNot(Op1, Op0)) |
2530 | 0 | return R; |
2531 | | |
2532 | 40.7M | if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Xor)) |
2533 | 0 | return V; |
2534 | | |
2535 | | // Try some generic simplifications for associative operations. |
2536 | 40.7M | if (Value *V = |
2537 | 40.7M | simplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, MaxRecurse)) |
2538 | 154k | return V; |
2539 | | |
2540 | | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
2541 | | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
2542 | | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
2543 | | // only if B and C are equal. If B and C are equal then (since we assume |
2544 | | // that operands have already been simplified) "select(cond, B, C)" should |
2545 | | // have been simplified to the common value of B and C already. Analysing |
2546 | | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
2547 | | // for threading over phi nodes. |
2548 | | |
2549 | 40.6M | if (Value *V = simplifyByDomEq(Instruction::Xor, Op0, Op1, Q, MaxRecurse)) |
2550 | 20 | return V; |
2551 | | |
2552 | | // (xor (sub nuw C_Mask, X), C_Mask) -> X |
2553 | 40.6M | { |
2554 | 40.6M | Value *X; |
2555 | 40.6M | if (match(Op0, m_NUWSub(m_Specific(Op1), m_Value(X))) && |
2556 | 40.6M | match(Op1, m_LowBitMask())2 ) |
2557 | 2 | return X; |
2558 | 40.6M | } |
2559 | | |
2560 | 40.6M | return nullptr; |
2561 | 40.6M | } |
2562 | | |
2563 | 7.78M | Value *llvm::simplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) { |
2564 | 7.78M | return ::simplifyXorInst(Op0, Op1, Q, RecursionLimit); |
2565 | 7.78M | } |
2566 | | |
2567 | 2.86G | static Type *getCompareTy(Value *Op) { |
2568 | 2.86G | return CmpInst::makeCmpResultType(Op->getType()); |
2569 | 2.86G | } |
2570 | | |
2571 | | /// Rummage around inside V looking for something equivalent to the comparison |
2572 | | /// "LHS Pred RHS". Return such a value if found, otherwise return null. |
2573 | | /// Helper function for analyzing max/min idioms. |
2574 | | static Value *extractEquivalentCondition(Value *V, CmpPredicate Pred, |
2575 | 256k | Value *LHS, Value *RHS) { |
2576 | 256k | SelectInst *SI = dyn_cast<SelectInst>(V); |
2577 | 256k | if (!SI) |
2578 | 249k | return nullptr; |
2579 | 7.74k | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
2580 | 7.74k | if (!Cmp) |
2581 | 60 | return nullptr; |
2582 | 7.68k | Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1); |
2583 | 7.68k | if (Pred == Cmp->getPredicate() && LHS == CmpLHS3.01k && RHS == CmpRHS2.02k ) |
2584 | 2.02k | return Cmp; |
2585 | 5.65k | if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) && |
2586 | 5.65k | LHS == CmpRHS981 && RHS == CmpLHS691 ) |
2587 | 691 | return Cmp; |
2588 | 4.96k | return nullptr; |
2589 | 5.65k | } |
2590 | | |
2591 | | /// Return true if the underlying object (storage) must be disjoint from |
2592 | | /// storage returned by any noalias return call. |
2593 | 5.15M | static bool isAllocDisjoint(const Value *V) { |
2594 | | // For allocas, we consider only static ones (dynamic |
2595 | | // allocas might be transformed into calls to malloc not simultaneously |
2596 | | // live with the compared-to allocation). For globals, we exclude symbols |
2597 | | // that might be resolve lazily to symbols in another dynamically-loaded |
2598 | | // library (and, thus, could be malloc'ed by the implementation). |
2599 | 5.15M | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
2600 | 6.62k | return AI->isStaticAlloca(); |
2601 | 5.14M | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
2602 | 1.88k | return (GV->hasLocalLinkage() || GV->hasHiddenVisibility()1.87k || |
2603 | 1.88k | GV->hasProtectedVisibility()1.82k || GV->hasGlobalUnnamedAddr()1.82k ) && |
2604 | 1.88k | !GV->isThreadLocal()61 ; |
2605 | 5.14M | if (const Argument *A = dyn_cast<Argument>(V)) |
2606 | 6.11k | return A->hasByValAttr(); |
2607 | 5.13M | return false; |
2608 | 5.14M | } |
2609 | | |
2610 | | /// Return true if V1 and V2 are each the base of some distict storage region |
2611 | | /// [V, object_size(V)] which do not overlap. Note that zero sized regions |
2612 | | /// *are* possible, and that zero sized regions do not overlap with any other. |
2613 | 173M | static bool haveNonOverlappingStorage(const Value *V1, const Value *V2) { |
2614 | | // Global variables always exist, so they always exist during the lifetime |
2615 | | // of each other and all allocas. Global variables themselves usually have |
2616 | | // non-overlapping storage, but since their addresses are constants, the |
2617 | | // case involving two globals does not reach here and is instead handled in |
2618 | | // constant folding. |
2619 | | // |
2620 | | // Two different allocas usually have different addresses... |
2621 | | // |
2622 | | // However, if there's an @llvm.stackrestore dynamically in between two |
2623 | | // allocas, they may have the same address. It's tempting to reduce the |
2624 | | // scope of the problem by only looking at *static* allocas here. That would |
2625 | | // cover the majority of allocas while significantly reducing the likelihood |
2626 | | // of having an @llvm.stackrestore pop up in the middle. However, it's not |
2627 | | // actually impossible for an @llvm.stackrestore to pop up in the middle of |
2628 | | // an entry block. Also, if we have a block that's not attached to a |
2629 | | // function, we can't tell if it's "static" under the current definition. |
2630 | | // Theoretically, this problem could be fixed by creating a new kind of |
2631 | | // instruction kind specifically for static allocas. Such a new instruction |
2632 | | // could be required to be at the top of the entry block, thus preventing it |
2633 | | // from being subject to a @llvm.stackrestore. Instcombine could even |
2634 | | // convert regular allocas into these special allocas. It'd be nifty. |
2635 | | // However, until then, this problem remains open. |
2636 | | // |
2637 | | // So, we'll assume that two non-empty allocas have different addresses |
2638 | | // for now. |
2639 | 346M | auto isByValArg = [](const Value *V) { |
2640 | 346M | const Argument *A = dyn_cast<Argument>(V); |
2641 | 346M | return A && A->hasByValAttr()31.6M ; |
2642 | 346M | }; |
2643 | | |
2644 | | // Byval args are backed by store which does not overlap with each other, |
2645 | | // allocas, or globals. |
2646 | 173M | if (isByValArg(V1)) |
2647 | 5 | return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2); |
2648 | 173M | if (isByValArg(V2)) |
2649 | 13 | return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1); |
2650 | | |
2651 | 173M | return isa<AllocaInst>(V1) && |
2652 | 173M | (320k isa<AllocaInst>(V2)320k || isa<GlobalVariable>(V2)305k ); |
2653 | 173M | } |
2654 | | |
2655 | | // A significant optimization not implemented here is assuming that alloca |
2656 | | // addresses are not equal to incoming argument values. They don't *alias*, |
2657 | | // as we say, but that doesn't mean they aren't equal, so we take a |
2658 | | // conservative approach. |
2659 | | // |
2660 | | // This is inspired in part by C++11 5.10p1: |
2661 | | // "Two pointers of the same type compare equal if and only if they are both |
2662 | | // null, both point to the same function, or both represent the same |
2663 | | // address." |
2664 | | // |
2665 | | // This is pretty permissive. |
2666 | | // |
2667 | | // It's also partly due to C11 6.5.9p6: |
2668 | | // "Two pointers compare equal if and only if both are null pointers, both are |
2669 | | // pointers to the same object (including a pointer to an object and a |
2670 | | // subobject at its beginning) or function, both are pointers to one past the |
2671 | | // last element of the same array object, or one is a pointer to one past the |
2672 | | // end of one array object and the other is a pointer to the start of a |
2673 | | // different array object that happens to immediately follow the first array |
2674 | | // object in the address space.) |
2675 | | // |
2676 | | // C11's version is more restrictive, however there's no reason why an argument |
2677 | | // couldn't be a one-past-the-end value for a stack object in the caller and be |
2678 | | // equal to the beginning of a stack object in the callee. |
2679 | | // |
2680 | | // If the C and C++ standards are ever made sufficiently restrictive in this |
2681 | | // area, it may be possible to update LLVM's semantics accordingly and reinstate |
2682 | | // this optimization. |
2683 | | static Constant *computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS, |
2684 | 179M | const SimplifyQuery &Q) { |
2685 | 179M | assert(LHS->getType() == RHS->getType() && "Must have same types"); |
2686 | 179M | const DataLayout &DL = Q.DL; |
2687 | 179M | const TargetLibraryInfo *TLI = Q.TLI; |
2688 | | |
2689 | | // We fold equality and unsigned predicates on pointer comparisons, but forbid |
2690 | | // signed predicates since a GEP with inbounds could cross the sign boundary. |
2691 | 179M | if (CmpInst::isSigned(Pred)) |
2692 | 32.5k | return nullptr; |
2693 | | |
2694 | | // We have to switch to a signed predicate to handle negative indices from |
2695 | | // the base pointer. |
2696 | 179M | Pred = ICmpInst::getSignedPredicate(Pred); |
2697 | | |
2698 | | // Strip off any constant offsets so that we can reason about them. |
2699 | | // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets |
2700 | | // here and compare base addresses like AliasAnalysis does, however there are |
2701 | | // numerous hazards. AliasAnalysis and its utilities rely on special rules |
2702 | | // governing loads and stores which don't apply to icmps. Also, AliasAnalysis |
2703 | | // doesn't need to guarantee pointer inequality when it says NoAlias. |
2704 | | |
2705 | | // Even if an non-inbounds GEP occurs along the path we can still optimize |
2706 | | // equality comparisons concerning the result. |
2707 | 179M | bool AllowNonInbounds = ICmpInst::isEquality(Pred); |
2708 | 179M | unsigned IndexSize = DL.getIndexTypeSizeInBits(LHS->getType()); |
2709 | 179M | APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0); |
2710 | 179M | LHS = LHS->stripAndAccumulateConstantOffsets(DL, LHSOffset, AllowNonInbounds); |
2711 | 179M | RHS = RHS->stripAndAccumulateConstantOffsets(DL, RHSOffset, AllowNonInbounds); |
2712 | | |
2713 | | // If LHS and RHS are related via constant offsets to the same base |
2714 | | // value, we can replace it with an icmp which just compares the offsets. |
2715 | 179M | if (LHS == RHS) |
2716 | 286k | return ConstantInt::get(getCompareTy(LHS), |
2717 | 286k | ICmpInst::compare(LHSOffset, RHSOffset, Pred)); |
2718 | | |
2719 | | // Various optimizations for (in)equality comparisons. |
2720 | 179M | if (ICmpInst::isEquality(Pred)) { |
2721 | | // Different non-empty allocations that exist at the same time have |
2722 | | // different addresses (if the program can tell). If the offsets are |
2723 | | // within the bounds of their allocations (and not one-past-the-end! |
2724 | | // so we can't use inbounds!), and their allocations aren't the same, |
2725 | | // the pointers are not equal. |
2726 | 173M | if (haveNonOverlappingStorage(LHS, RHS)) { |
2727 | 15.0k | uint64_t LHSSize, RHSSize; |
2728 | 15.0k | ObjectSizeOpts Opts; |
2729 | 15.0k | Opts.EvalMode = ObjectSizeOpts::Mode::Min; |
2730 | 15.0k | auto *F = [](Value *V) -> Function * { |
2731 | 15.0k | if (auto *I = dyn_cast<Instruction>(V)) |
2732 | 15.0k | return I->getFunction(); |
2733 | 0 | if (auto *A = dyn_cast<Argument>(V)) |
2734 | 0 | return A->getParent(); |
2735 | 0 | return nullptr; |
2736 | 0 | }(LHS); |
2737 | 15.0k | Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true0 ; |
2738 | 15.0k | if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) && LHSSize != 0 && |
2739 | 15.0k | getObjectSize(RHS, RHSSize, DL, TLI, Opts) && RHSSize != 0) { |
2740 | 15.0k | APInt Dist = LHSOffset - RHSOffset; |
2741 | 15.0k | if (Dist.isNonNegative() ? Dist.ult(LHSSize)14.0k : (-Dist).ult(RHSSize)1.08k ) |
2742 | 15.0k | return ConstantInt::get(getCompareTy(LHS), |
2743 | 15.0k | !CmpInst::isTrueWhenEqual(Pred)); |
2744 | 15.0k | } |
2745 | 15.0k | } |
2746 | | |
2747 | | // If one side of the equality comparison must come from a noalias call |
2748 | | // (meaning a system memory allocation function), and the other side must |
2749 | | // come from a pointer that cannot overlap with dynamically-allocated |
2750 | | // memory within the lifetime of the current function (allocas, byval |
2751 | | // arguments, globals), then determine the comparison result here. |
2752 | 173M | SmallVector<const Value *, 8> LHSUObjs, RHSUObjs; |
2753 | 173M | getUnderlyingObjects(LHS, LHSUObjs); |
2754 | 173M | getUnderlyingObjects(RHS, RHSUObjs); |
2755 | | |
2756 | | // Is the set of underlying objects all noalias calls? |
2757 | 346M | auto IsNAC = [](ArrayRef<const Value *> Objects) { |
2758 | 346M | return all_of(Objects, isNoAliasCall); |
2759 | 346M | }; |
2760 | | |
2761 | | // Is the set of underlying objects all things which must be disjoint from |
2762 | | // noalias calls. We assume that indexing from such disjoint storage |
2763 | | // into the heap is undefined, and thus offsets can be safely ignored. |
2764 | 173M | auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) { |
2765 | 5.15M | return all_of(Objects, ::isAllocDisjoint); |
2766 | 5.15M | }; |
2767 | | |
2768 | 173M | if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)4.46M ) || |
2769 | 173M | (173M IsNAC(RHSUObjs)173M && IsAllocDisjoint(LHSUObjs)683k )) |
2770 | 6.60k | return ConstantInt::get(getCompareTy(LHS), |
2771 | 6.60k | !CmpInst::isTrueWhenEqual(Pred)); |
2772 | | |
2773 | | // Fold comparisons for non-escaping pointer even if the allocation call |
2774 | | // cannot be elided. We cannot fold malloc comparison to null. Also, the |
2775 | | // dynamic allocation call could be either of the operands. Note that |
2776 | | // the other operand can not be based on the alloc - if it were, then |
2777 | | // the cmp itself would be a capture. |
2778 | 173M | Value *MI = nullptr; |
2779 | 173M | if (isAllocLikeFn(LHS, TLI) && llvm::isKnownNonZero(RHS, Q)1.35M ) |
2780 | 33.7k | MI = LHS; |
2781 | 173M | else if (isAllocLikeFn(RHS, TLI) && llvm::isKnownNonZero(LHS, Q)185k ) |
2782 | 40.1k | MI = RHS; |
2783 | 173M | if (MI) { |
2784 | | // FIXME: This is incorrect, see PR54002. While we can assume that the |
2785 | | // allocation is at an address that makes the comparison false, this |
2786 | | // requires that *all* comparisons to that address be false, which |
2787 | | // InstSimplify cannot guarantee. |
2788 | 73.9k | struct CustomCaptureTracker : public CaptureTracker { |
2789 | 73.9k | bool Captured = false; |
2790 | 73.9k | void tooManyUses() override { Captured = true; }1.44k |
2791 | 73.9k | Action captured(const Use *U, UseCaptureInfo CI) override { |
2792 | | // TODO(captures): Use UseCaptureInfo. |
2793 | 72.4k | if (auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) { |
2794 | | // Comparison against value stored in global variable. Given the |
2795 | | // pointer does not escape, its value cannot be guessed and stored |
2796 | | // separately in a global variable. |
2797 | 14.9k | unsigned OtherIdx = 1 - U->getOperandNo(); |
2798 | 14.9k | auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx)); |
2799 | 14.9k | if (LI && isa<GlobalVariable>(LI->getPointerOperand())116 ) |
2800 | 0 | return Continue; |
2801 | 14.9k | } |
2802 | | |
2803 | 72.4k | Captured = true; |
2804 | 72.4k | return Stop; |
2805 | 72.4k | } |
2806 | 73.9k | }; |
2807 | 73.9k | CustomCaptureTracker Tracker; |
2808 | 73.9k | PointerMayBeCaptured(MI, &Tracker); |
2809 | 73.9k | if (!Tracker.Captured) |
2810 | 0 | return ConstantInt::get(getCompareTy(LHS), |
2811 | 0 | CmpInst::isFalseWhenEqual(Pred)); |
2812 | 73.9k | } |
2813 | 173M | } |
2814 | | |
2815 | | // Otherwise, fail. |
2816 | 179M | return nullptr; |
2817 | 179M | } |
2818 | | |
2819 | | /// Fold an icmp when its operands have i1 scalar type. |
2820 | | static Value *simplifyICmpOfBools(CmpPredicate Pred, Value *LHS, Value *RHS, |
2821 | 617M | const SimplifyQuery &Q) { |
2822 | 617M | Type *ITy = getCompareTy(LHS); // The return type. |
2823 | 617M | Type *OpTy = LHS->getType(); // The operand type. |
2824 | 617M | if (!OpTy->isIntOrIntVectorTy(1)) |
2825 | 615M | return nullptr; |
2826 | | |
2827 | | // A boolean compared to true/false can be reduced in 14 out of the 20 |
2828 | | // (10 predicates * 2 constants) possible combinations. The other |
2829 | | // 6 cases require a 'not' of the LHS. |
2830 | | |
2831 | 1.57M | auto ExtractNotLHS = [](Value *V) -> Value * { |
2832 | 964k | Value *X; |
2833 | 964k | if (match(V, m_Not(m_Value(X)))) |
2834 | 101k | return X; |
2835 | 862k | return nullptr; |
2836 | 964k | }; |
2837 | | |
2838 | 1.57M | if (match(RHS, m_Zero())) { |
2839 | 896k | switch (Pred) { |
2840 | 342k | case CmpInst::ICMP_NE: // X != 0 -> X |
2841 | 344k | case CmpInst::ICMP_UGT: // X >u 0 -> X |
2842 | 359k | case CmpInst::ICMP_SLT: // X <s 0 -> X |
2843 | 359k | return LHS; |
2844 | | |
2845 | 532k | case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X |
2846 | 532k | case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X |
2847 | 535k | case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X |
2848 | 535k | if (Value *X = ExtractNotLHS(LHS)) |
2849 | 62.2k | return X; |
2850 | 473k | break; |
2851 | | |
2852 | 473k | case CmpInst::ICMP_ULT: // X <u 0 -> false |
2853 | 1.55k | case CmpInst::ICMP_SGT: // X >s 0 -> false |
2854 | 1.55k | return getFalse(ITy); |
2855 | | |
2856 | 4 | case CmpInst::ICMP_UGE: // X >=u 0 -> true |
2857 | 9 | case CmpInst::ICMP_SLE: // X <=s 0 -> true |
2858 | 9 | return getTrue(ITy); |
2859 | | |
2860 | 0 | default: |
2861 | 0 | break; |
2862 | 896k | } |
2863 | 896k | } else if (680k match(RHS, m_One())680k ) { |
2864 | 632k | switch (Pred) { |
2865 | 192k | case CmpInst::ICMP_EQ: // X == 1 -> X |
2866 | 194k | case CmpInst::ICMP_UGE: // X >=u 1 -> X |
2867 | 194k | case CmpInst::ICMP_SLE: // X <=s -1 -> X |
2868 | 194k | return LHS; |
2869 | | |
2870 | 5.36k | case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X |
2871 | 428k | case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X |
2872 | 428k | case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X |
2873 | 428k | if (Value *X = ExtractNotLHS(LHS)) |
2874 | 39.1k | return X; |
2875 | 389k | break; |
2876 | | |
2877 | 389k | case CmpInst::ICMP_UGT: // X >u 1 -> false |
2878 | 530 | case CmpInst::ICMP_SLT: // X <s -1 -> false |
2879 | 530 | return getFalse(ITy); |
2880 | | |
2881 | 9.23k | case CmpInst::ICMP_ULE: // X <=u 1 -> true |
2882 | 9.23k | case CmpInst::ICMP_SGE: // X >=s -1 -> true |
2883 | 9.23k | return getTrue(ITy); |
2884 | | |
2885 | 0 | default: |
2886 | 0 | break; |
2887 | 632k | } |
2888 | 632k | } |
2889 | | |
2890 | 910k | switch (Pred) { |
2891 | 907k | default: |
2892 | 907k | break; |
2893 | 907k | case ICmpInst::ICMP_UGE: |
2894 | 60 | if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false)) |
2895 | 0 | return getTrue(ITy); |
2896 | 60 | break; |
2897 | 2.78k | case ICmpInst::ICMP_SGE: |
2898 | | /// For signed comparison, the values for an i1 are 0 and -1 |
2899 | | /// respectively. This maps into a truth table of: |
2900 | | /// LHS | RHS | LHS >=s RHS | LHS implies RHS |
2901 | | /// 0 | 0 | 1 (0 >= 0) | 1 |
2902 | | /// 0 | 1 | 1 (0 >= -1) | 1 |
2903 | | /// 1 | 0 | 0 (-1 >= 0) | 0 |
2904 | | /// 1 | 1 | 1 (-1 >= -1) | 1 |
2905 | 2.78k | if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false)) |
2906 | 0 | return getTrue(ITy); |
2907 | 2.78k | break; |
2908 | 2.78k | case ICmpInst::ICMP_ULE: |
2909 | 215 | if (isImpliedCondition(LHS, RHS, Q.DL).value_or(false)) |
2910 | 0 | return getTrue(ITy); |
2911 | 215 | break; |
2912 | 215 | case ICmpInst::ICMP_SLE: |
2913 | | /// SLE follows the same logic as SGE with the LHS and RHS swapped. |
2914 | 0 | if (isImpliedCondition(RHS, LHS, Q.DL).value_or(false)) |
2915 | 0 | return getTrue(ITy); |
2916 | 0 | break; |
2917 | 910k | } |
2918 | | |
2919 | 910k | return nullptr; |
2920 | 910k | } |
2921 | | |
2922 | | /// Try hard to fold icmp with zero RHS because this is a common case. |
2923 | | static Value *simplifyICmpWithZero(CmpPredicate Pred, Value *LHS, Value *RHS, |
2924 | 616M | const SimplifyQuery &Q) { |
2925 | 616M | if (!match(RHS, m_Zero())) |
2926 | 362M | return nullptr; |
2927 | | |
2928 | 254M | Type *ITy = getCompareTy(LHS); // The return type. |
2929 | 254M | switch (Pred) { |
2930 | 0 | default: |
2931 | 0 | llvm_unreachable("Unknown ICmp predicate!"); |
2932 | 145k | case ICmpInst::ICMP_ULT: |
2933 | 145k | return getFalse(ITy); |
2934 | 31.2k | case ICmpInst::ICMP_UGE: |
2935 | 31.2k | return getTrue(ITy); |
2936 | 196M | case ICmpInst::ICMP_EQ: |
2937 | 196M | case ICmpInst::ICMP_ULE: |
2938 | 196M | if (isKnownNonZero(LHS, Q)) |
2939 | 3.97M | return getFalse(ITy); |
2940 | 192M | break; |
2941 | 192M | case ICmpInst::ICMP_NE: |
2942 | 31.7M | case ICmpInst::ICMP_UGT: |
2943 | 31.7M | if (isKnownNonZero(LHS, Q)) |
2944 | 2.35M | return getTrue(ITy); |
2945 | 29.3M | break; |
2946 | 29.3M | case ICmpInst::ICMP_SLT: { |
2947 | 8.69M | KnownBits LHSKnown = computeKnownBits(LHS, Q); |
2948 | 8.69M | if (LHSKnown.isNegative()) |
2949 | 11.1k | return getTrue(ITy); |
2950 | 8.68M | if (LHSKnown.isNonNegative()) |
2951 | 66.9k | return getFalse(ITy); |
2952 | 8.62M | break; |
2953 | 8.68M | } |
2954 | 8.62M | case ICmpInst::ICMP_SLE: { |
2955 | 453k | KnownBits LHSKnown = computeKnownBits(LHS, Q); |
2956 | 453k | if (LHSKnown.isNegative()) |
2957 | 312 | return getTrue(ITy); |
2958 | 452k | if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q)9.50k ) |
2959 | 2.56k | return getFalse(ITy); |
2960 | 450k | break; |
2961 | 452k | } |
2962 | 585k | case ICmpInst::ICMP_SGE: { |
2963 | 585k | KnownBits LHSKnown = computeKnownBits(LHS, Q); |
2964 | 585k | if (LHSKnown.isNegative()) |
2965 | 395 | return getFalse(ITy); |
2966 | 584k | if (LHSKnown.isNonNegative()) |
2967 | 7.22k | return getTrue(ITy); |
2968 | 577k | break; |
2969 | 584k | } |
2970 | 15.9M | case ICmpInst::ICMP_SGT: { |
2971 | 15.9M | KnownBits LHSKnown = computeKnownBits(LHS, Q); |
2972 | 15.9M | if (LHSKnown.isNegative()) |
2973 | 716 | return getFalse(ITy); |
2974 | 15.9M | if (LHSKnown.isNonNegative() && isKnownNonZero(LHS, Q)371k ) |
2975 | 78.8k | return getTrue(ITy); |
2976 | 15.8M | break; |
2977 | 15.9M | } |
2978 | 254M | } |
2979 | | |
2980 | 247M | return nullptr; |
2981 | 254M | } |
2982 | | |
2983 | | static Value *simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS, |
2984 | 609M | Value *RHS, const SimplifyQuery &Q) { |
2985 | 609M | Type *ITy = getCompareTy(RHS); // The return type. |
2986 | | |
2987 | 609M | Value *X; |
2988 | 609M | const APInt *C; |
2989 | 609M | if (!match(RHS, m_APIntAllowPoison(C))) |
2990 | 272M | return nullptr; |
2991 | | |
2992 | | // Sign-bit checks can be optimized to true/false after unsigned |
2993 | | // floating-point casts: |
2994 | | // icmp slt (bitcast (uitofp X)), 0 --> false |
2995 | | // icmp sgt (bitcast (uitofp X)), -1 --> true |
2996 | 337M | if (match(LHS, m_ElementWiseBitCast(m_UIToFP(m_Value(X))))) { |
2997 | 1.12k | bool TrueIfSigned; |
2998 | 1.12k | if (isSignBitCheck(Pred, *C, TrueIfSigned)) |
2999 | 0 | return ConstantInt::getBool(ITy, !TrueIfSigned); |
3000 | 1.12k | } |
3001 | | |
3002 | | // Rule out tautological comparisons (eg., ult 0 or uge 0). |
3003 | 337M | ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C); |
3004 | 337M | if (RHS_CR.isEmptySet()) |
3005 | 166k | return ConstantInt::getFalse(ITy); |
3006 | 337M | if (RHS_CR.isFullSet()) |
3007 | 4.29k | return ConstantInt::getTrue(ITy); |
3008 | | |
3009 | 337M | ConstantRange LHS_CR = |
3010 | 337M | computeConstantRange(LHS, CmpInst::isSigned(Pred), Q.IIQ.UseInstrInfo); |
3011 | 337M | if (!LHS_CR.isFullSet()) { |
3012 | 85.4M | if (RHS_CR.contains(LHS_CR)) |
3013 | 167k | return ConstantInt::getTrue(ITy); |
3014 | 85.3M | if (RHS_CR.inverse().contains(LHS_CR)) |
3015 | 281k | return ConstantInt::getFalse(ITy); |
3016 | 85.3M | } |
3017 | | |
3018 | | // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC) |
3019 | | // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC) |
3020 | 337M | const APInt *MulC; |
3021 | 337M | if (Q.IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) && |
3022 | 337M | (200M (200M match(LHS, m_NUWMul(m_Value(), m_APIntAllowPoison(MulC)))200M && |
3023 | 200M | *MulC != 025.0k && C->urem(*MulC) != 025.0k ) || |
3024 | 200M | (200M match(LHS, m_NSWMul(m_Value(), m_APIntAllowPoison(MulC)))200M && |
3025 | 200M | *MulC != 037.5k && C->srem(*MulC) != 037.5k ))) |
3026 | 432 | return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE); |
3027 | | |
3028 | 337M | if (Pred == ICmpInst::ICMP_UGE && C->isOne()5.22M && isKnownNonZero(LHS, Q)1.64M ) |
3029 | 29.6k | return ConstantInt::getTrue(ITy); |
3030 | | |
3031 | 337M | return nullptr; |
3032 | 337M | } |
3033 | | |
3034 | | enum class MonotonicType { GreaterEq, LowerEq }; |
3035 | | |
3036 | | /// Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq). |
3037 | | static void getUnsignedMonotonicValues(SmallPtrSetImpl<Value *> &Res, Value *V, |
3038 | | MonotonicType Type, |
3039 | | const SimplifyQuery &Q, |
3040 | 256M | unsigned Depth = 0) { |
3041 | 256M | if (!Res.insert(V).second) |
3042 | 118 | return; |
3043 | | |
3044 | | // Can be increased if useful. |
3045 | 256M | if (++Depth > 1) |
3046 | 5.25M | return; |
3047 | | |
3048 | 251M | auto *I = dyn_cast<Instruction>(V); |
3049 | 251M | if (!I) |
3050 | 92.5M | return; |
3051 | | |
3052 | 158M | Value *X, *Y; |
3053 | 158M | if (Type == MonotonicType::GreaterEq) { |
3054 | 95.7M | if (match(I, m_Or(m_Value(X), m_Value(Y))) || |
3055 | 95.7M | match(I, m_Intrinsic<Intrinsic::uadd_sat>(m_Value(X), m_Value(Y)))95.0M ) { |
3056 | 670k | getUnsignedMonotonicValues(Res, X, Type, Q, Depth); |
3057 | 670k | getUnsignedMonotonicValues(Res, Y, Type, Q, Depth); |
3058 | 670k | } |
3059 | | // X * Y >= X --> true |
3060 | 95.7M | if (match(I, m_NUWMul(m_Value(X), m_Value(Y)))) { |
3061 | 83.1k | if (isKnownNonZero(X, Q)) |
3062 | 22.9k | getUnsignedMonotonicValues(Res, Y, Type, Q, Depth); |
3063 | 83.1k | if (isKnownNonZero(Y, Q)) |
3064 | 59.0k | getUnsignedMonotonicValues(Res, X, Type, Q, Depth); |
3065 | 83.1k | } |
3066 | 95.7M | } else { |
3067 | 63.2M | assert(Type == MonotonicType::LowerEq); |
3068 | 63.2M | switch (I->getOpcode()) { |
3069 | 1.00M | case Instruction::And: |
3070 | 1.00M | getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Q, Depth); |
3071 | 1.00M | getUnsignedMonotonicValues(Res, I->getOperand(1), Type, Q, Depth); |
3072 | 1.00M | break; |
3073 | 37.7k | case Instruction::URem: |
3074 | 870k | case Instruction::UDiv: |
3075 | 1.73M | case Instruction::LShr: |
3076 | 1.73M | getUnsignedMonotonicValues(Res, I->getOperand(0), Type, Q, Depth); |
3077 | 1.73M | break; |
3078 | 3.98M | case Instruction::Call: |
3079 | 3.98M | if (match(I, m_Intrinsic<Intrinsic::usub_sat>(m_Value(X)))) |
3080 | 91.7k | getUnsignedMonotonicValues(Res, X, Type, Q, Depth); |
3081 | 3.98M | break; |
3082 | 56.5M | default: |
3083 | 56.5M | break; |
3084 | 63.2M | } |
3085 | 63.2M | } |
3086 | 158M | } |
3087 | | |
3088 | | static Value *simplifyICmpUsingMonotonicValues(CmpPredicate Pred, Value *LHS, |
3089 | | Value *RHS, |
3090 | 1.21G | const SimplifyQuery &Q) { |
3091 | 1.21G | if (Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_ULT1.19G ) |
3092 | 1.08G | return nullptr; |
3093 | | |
3094 | | // We have LHS uge GreaterValues and LowerValues uge RHS. If any of the |
3095 | | // GreaterValues and LowerValues are the same, it follows that LHS uge RHS. |
3096 | 125M | SmallPtrSet<Value *, 4> GreaterValues; |
3097 | 125M | SmallPtrSet<Value *, 4> LowerValues; |
3098 | 125M | getUnsignedMonotonicValues(GreaterValues, LHS, MonotonicType::GreaterEq, Q); |
3099 | 125M | getUnsignedMonotonicValues(LowerValues, RHS, MonotonicType::LowerEq, Q); |
3100 | 125M | for (Value *GV : GreaterValues) |
3101 | 127M | if (LowerValues.contains(GV)) |
3102 | 1.50k | return ConstantInt::getBool(getCompareTy(LHS), |
3103 | 1.50k | Pred == ICmpInst::ICMP_UGE); |
3104 | 125M | return nullptr; |
3105 | 125M | } |
3106 | | |
3107 | | static Value *simplifyICmpWithBinOpOnLHS(CmpPredicate Pred, BinaryOperator *LBO, |
3108 | | Value *RHS, const SimplifyQuery &Q, |
3109 | 150M | unsigned MaxRecurse) { |
3110 | 150M | Type *ITy = getCompareTy(RHS); // The return type. |
3111 | | |
3112 | 150M | Value *Y = nullptr; |
3113 | | // icmp pred (or X, Y), X |
3114 | 150M | if (match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) { |
3115 | 35.2k | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE34.5k ) { |
3116 | 873 | KnownBits RHSKnown = computeKnownBits(RHS, Q); |
3117 | 873 | KnownBits YKnown = computeKnownBits(Y, Q); |
3118 | 873 | if (RHSKnown.isNonNegative() && YKnown.isNegative()485 ) |
3119 | 0 | return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy); |
3120 | 873 | if (RHSKnown.isNegative() || YKnown.isNonNegative()) |
3121 | 414 | return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy)323 : getTrue(ITy)91 ; |
3122 | 873 | } |
3123 | 35.2k | } |
3124 | | |
3125 | | // icmp pred (urem X, Y), Y |
3126 | 150M | if (match(LBO, m_URem(m_Value(), m_Specific(RHS)))) { |
3127 | 1.48k | switch (Pred) { |
3128 | 0 | default: |
3129 | 0 | break; |
3130 | 0 | case ICmpInst::ICMP_SGT: |
3131 | 0 | case ICmpInst::ICMP_SGE: { |
3132 | 0 | KnownBits Known = computeKnownBits(RHS, Q); |
3133 | 0 | if (!Known.isNonNegative()) |
3134 | 0 | break; |
3135 | 0 | [[fallthrough]]; |
3136 | 0 | } |
3137 | 106 | case ICmpInst::ICMP_EQ: |
3138 | 115 | case ICmpInst::ICMP_UGT: |
3139 | 177 | case ICmpInst::ICMP_UGE: |
3140 | 177 | return getFalse(ITy); |
3141 | 71 | case ICmpInst::ICMP_SLT: |
3142 | 71 | case ICmpInst::ICMP_SLE: { |
3143 | 71 | KnownBits Known = computeKnownBits(RHS, Q); |
3144 | 71 | if (!Known.isNonNegative()) |
3145 | 59 | break; |
3146 | 12 | [[fallthrough]]; |
3147 | 12 | } |
3148 | 12 | case ICmpInst::ICMP_NE: |
3149 | 1.25k | case ICmpInst::ICMP_ULT: |
3150 | 1.25k | case ICmpInst::ICMP_ULE: |
3151 | 1.25k | return getTrue(ITy); |
3152 | 1.48k | } |
3153 | 1.48k | } |
3154 | | |
3155 | | // If x is nonzero: |
3156 | | // x >>u C <u x --> true for C != 0. |
3157 | | // x >>u C != x --> true for C != 0. |
3158 | | // x >>u C >=u x --> false for C != 0. |
3159 | | // x >>u C == x --> false for C != 0. |
3160 | | // x udiv C <u x --> true for C != 1. |
3161 | | // x udiv C != x --> true for C != 1. |
3162 | | // x udiv C >=u x --> false for C != 1. |
3163 | | // x udiv C == x --> false for C != 1. |
3164 | | // TODO: allow non-constant shift amount/divisor |
3165 | 150M | const APInt *C; |
3166 | 150M | if ((match(LBO, m_LShr(m_Specific(RHS), m_APInt(C))) && *C != 04.32k ) || |
3167 | 150M | (150M match(LBO, m_UDiv(m_Specific(RHS), m_APInt(C)))150M && *C != 1455 )) { |
3168 | 4.78k | if (isKnownNonZero(RHS, Q)) { |
3169 | 3.09k | switch (Pred) { |
3170 | 9 | default: |
3171 | 9 | break; |
3172 | 55 | case ICmpInst::ICMP_EQ: |
3173 | 68 | case ICmpInst::ICMP_UGE: |
3174 | 166 | case ICmpInst::ICMP_UGT: |
3175 | 166 | return getFalse(ITy); |
3176 | 0 | case ICmpInst::ICMP_NE: |
3177 | 2.81k | case ICmpInst::ICMP_ULT: |
3178 | 2.91k | case ICmpInst::ICMP_ULE: |
3179 | 2.91k | return getTrue(ITy); |
3180 | 3.09k | } |
3181 | 3.09k | } |
3182 | 4.78k | } |
3183 | | |
3184 | | // (x*C1)/C2 <= x for C1 <= C2. |
3185 | | // This holds even if the multiplication overflows: Assume that x != 0 and |
3186 | | // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and |
3187 | | // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x. |
3188 | | // |
3189 | | // Additionally, either the multiplication and division might be represented |
3190 | | // as shifts: |
3191 | | // (x*C1)>>C2 <= x for C1 < 2**C2. |
3192 | | // (x<<C1)/C2 <= x for 2**C1 < C2. |
3193 | 150M | const APInt *C1, *C2; |
3194 | 150M | if ((match(LBO, m_UDiv(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) && |
3195 | 150M | C1->ule(*C2)68 ) || |
3196 | 150M | (150M match(LBO, m_LShr(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2)))150M && |
3197 | 150M | C1->ule(APInt(C2->getBitWidth(), 1) << *C2)119 ) || |
3198 | 150M | (150M match(LBO, m_UDiv(m_Shl(m_Specific(RHS), m_APInt(C1)), m_APInt(C2)))150M && |
3199 | 150M | (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2)2 )) { |
3200 | 69 | if (Pred == ICmpInst::ICMP_UGT) |
3201 | 27 | return getFalse(ITy); |
3202 | 42 | if (Pred == ICmpInst::ICMP_ULE) |
3203 | 1 | return getTrue(ITy); |
3204 | 42 | } |
3205 | | |
3206 | | // (sub C, X) == X, C is odd --> false |
3207 | | // (sub C, X) != X, C is odd --> true |
3208 | 150M | if (match(LBO, m_Sub(m_APIntAllowPoison(C), m_Specific(RHS))) && |
3209 | 150M | (*C & 1) == 111.9k && ICmpInst::isEquality(Pred)792 ) |
3210 | 12 | return (Pred == ICmpInst::ICMP_EQ) ? getFalse(ITy) : getTrue(ITy)0 ; |
3211 | | |
3212 | 150M | return nullptr; |
3213 | 150M | } |
3214 | | |
3215 | | // If only one of the icmp's operands has NSW flags, try to prove that: |
3216 | | // |
3217 | | // icmp slt (x + C1), (x +nsw C2) |
3218 | | // |
3219 | | // is equivalent to: |
3220 | | // |
3221 | | // icmp slt C1, C2 |
3222 | | // |
3223 | | // which is true if x + C2 has the NSW flags set and: |
3224 | | // *) C1 < C2 && C1 >= 0, or |
3225 | | // *) C2 < C1 && C1 <= 0. |
3226 | | // |
3227 | | static bool trySimplifyICmpWithAdds(CmpPredicate Pred, Value *LHS, Value *RHS, |
3228 | 136M | const InstrInfoQuery &IIQ) { |
3229 | | // TODO: only support icmp slt for now. |
3230 | 136M | if (Pred != CmpInst::ICMP_SLT || !IIQ.UseInstrInfo6.69M ) |
3231 | 130M | return false; |
3232 | | |
3233 | | // Canonicalize nsw add as RHS. |
3234 | 6.69M | if (!match(RHS, m_NSWAdd(m_Value(), m_Value()))) |
3235 | 6.27M | std::swap(LHS, RHS); |
3236 | 6.69M | if (!match(RHS, m_NSWAdd(m_Value(), m_Value()))) |
3237 | 3.35M | return false; |
3238 | | |
3239 | 3.34M | Value *X; |
3240 | 3.34M | const APInt *C1, *C2; |
3241 | 3.34M | if (!match(LHS, m_Add(m_Value(X), m_APInt(C1))) || |
3242 | 3.34M | !match(RHS, m_Add(m_Specific(X), m_APInt(C2)))3.96k ) |
3243 | 3.34M | return false; |
3244 | | |
3245 | 87 | return (C1->slt(*C2) && C1->isNonNegative()86 ) || |
3246 | 87 | (1 C2->slt(*C1)1 && C1->isNonPositive()1 ); |
3247 | 3.34M | } |
3248 | | |
3249 | | /// TODO: A large part of this logic is duplicated in InstCombine's |
3250 | | /// foldICmpBinOp(). We should be able to share that and avoid the code |
3251 | | /// duplication. |
3252 | | static Value *simplifyICmpWithBinOp(CmpPredicate Pred, Value *LHS, Value *RHS, |
3253 | | const SimplifyQuery &Q, |
3254 | 607M | unsigned MaxRecurse) { |
3255 | 607M | BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS); |
3256 | 607M | BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS); |
3257 | 607M | if (MaxRecurse && (602M LBO602M || RBO472M )) { |
3258 | | // Analyze the case when either LHS or RHS is an add instruction. |
3259 | 138M | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
3260 | | // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). |
3261 | 138M | bool NoLHSWrapProblem = false, NoRHSWrapProblem = false; |
3262 | 138M | if (LBO && LBO->getOpcode() == Instruction::Add129M ) { |
3263 | 50.1M | A = LBO->getOperand(0); |
3264 | 50.1M | B = LBO->getOperand(1); |
3265 | 50.1M | NoLHSWrapProblem = |
3266 | 50.1M | ICmpInst::isEquality(Pred) || |
3267 | 50.1M | (30.2M CmpInst::isUnsigned(Pred)30.2M && |
3268 | 30.2M | Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))21.2M ) || |
3269 | 50.1M | (26.2M CmpInst::isSigned(Pred)26.2M && |
3270 | 26.2M | Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO))9.07M ); |
3271 | 50.1M | } |
3272 | 138M | if (RBO && RBO->getOpcode() == Instruction::Add19.9M ) { |
3273 | 5.54M | C = RBO->getOperand(0); |
3274 | 5.54M | D = RBO->getOperand(1); |
3275 | 5.54M | NoRHSWrapProblem = |
3276 | 5.54M | ICmpInst::isEquality(Pred) || |
3277 | 5.54M | (4.50M CmpInst::isUnsigned(Pred)4.50M && |
3278 | 4.50M | Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(RBO))2.43M ) || |
3279 | 5.54M | (4.02M CmpInst::isSigned(Pred)4.02M && |
3280 | 4.02M | Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RBO))2.06M ); |
3281 | 5.54M | } |
3282 | | |
3283 | | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
3284 | 138M | if ((A == RHS || B == RHS137M ) && NoLHSWrapProblem2.66M ) |
3285 | 357k | if (Value *V = simplifyICmpInst(Pred, A == RHS ? B : A, |
3286 | 357k | Constant::getNullValue(RHS->getType()), Q, |
3287 | 357k | MaxRecurse - 1)) |
3288 | 49.6k | return V; |
3289 | | |
3290 | | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
3291 | 137M | if ((C == LHS || D == LHS137M ) && NoRHSWrapProblem176k ) |
3292 | 84.1k | if (Value *V = |
3293 | 84.1k | simplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()), |
3294 | 84.1k | C == LHS ? D : C, Q, MaxRecurse - 1)) |
3295 | 16.9k | return V; |
3296 | | |
3297 | | // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. |
3298 | 137M | bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem30.9M ) || |
3299 | 137M | trySimplifyICmpWithAdds(Pred, LHS, RHS, Q.IIQ)136M ; |
3300 | 137M | if (A && C50.0M && (1.76M A == C1.76M || A == D1.75M || B == C1.72M || B == D1.70M ) && CanSimplify284k ) { |
3301 | | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
3302 | 116k | Value *Y, *Z; |
3303 | 116k | if (A == C) { |
3304 | | // C + B == C + D -> B == D |
3305 | 6.93k | Y = B; |
3306 | 6.93k | Z = D; |
3307 | 109k | } else if (A == D) { |
3308 | | // D + B == C + D -> B == C |
3309 | 4.19k | Y = B; |
3310 | 4.19k | Z = C; |
3311 | 105k | } else if (B == C) { |
3312 | | // A + C == C + D -> A == D |
3313 | 7.98k | Y = A; |
3314 | 7.98k | Z = D; |
3315 | 97.7k | } else { |
3316 | 97.7k | assert(B == D); |
3317 | | // A + D == C + D -> A == C |
3318 | 97.7k | Y = A; |
3319 | 97.7k | Z = C; |
3320 | 97.7k | } |
3321 | 116k | if (Value *V = simplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1)) |
3322 | 1.78k | return V; |
3323 | 116k | } |
3324 | 137M | } |
3325 | | |
3326 | 607M | if (LBO) |
3327 | 130M | if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse)) |
3328 | 4.50k | return V; |
3329 | | |
3330 | 607M | if (RBO) |
3331 | 20.0M | if (Value *V = simplifyICmpWithBinOpOnLHS( |
3332 | 20.0M | ICmpInst::getSwappedPredicate(Pred), RBO, LHS, Q, MaxRecurse)) |
3333 | 463 | return V; |
3334 | | |
3335 | | // 0 - (zext X) pred C |
3336 | 607M | if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))481M ) { |
3337 | 4.39k | const APInt *C; |
3338 | 4.39k | if (match(RHS, m_APInt(C))) { |
3339 | 4.14k | if (C->isStrictlyPositive()) { |
3340 | 111 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE92 ) |
3341 | 19 | return ConstantInt::getTrue(getCompareTy(RHS)); |
3342 | 92 | if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ) |
3343 | 0 | return ConstantInt::getFalse(getCompareTy(RHS)); |
3344 | 92 | } |
3345 | 4.12k | if (C->isNonNegative()) { |
3346 | 3.39k | if (Pred == ICmpInst::ICMP_SLE) |
3347 | 20 | return ConstantInt::getTrue(getCompareTy(RHS)); |
3348 | 3.37k | if (Pred == ICmpInst::ICMP_SGT) |
3349 | 125 | return ConstantInt::getFalse(getCompareTy(RHS)); |
3350 | 3.37k | } |
3351 | 4.12k | } |
3352 | 4.39k | } |
3353 | | |
3354 | | // If C2 is a power-of-2 and C is not: |
3355 | | // (C2 << X) == C --> false |
3356 | | // (C2 << X) != C --> true |
3357 | 607M | const APInt *C; |
3358 | 607M | if (match(LHS, m_Shl(m_Power2(), m_Value())) && |
3359 | 607M | match(RHS, m_APIntAllowPoison(C))129k && !C->isPowerOf2()98.7k ) { |
3360 | | // C2 << X can equal zero in some circumstances. |
3361 | | // This simplification might be unsafe if C is zero. |
3362 | | // |
3363 | | // We know it is safe if: |
3364 | | // - The shift is nsw. We can't shift out the one bit. |
3365 | | // - The shift is nuw. We can't shift out the one bit. |
3366 | | // - C2 is one. |
3367 | | // - C isn't zero. |
3368 | 36.8k | if (Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)) || |
3369 | 36.8k | Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))36.5k || |
3370 | 36.8k | match(LHS, m_Shl(m_One(), m_Value()))3.05k || !C->isZero()2.13k ) { |
3371 | 35.8k | if (Pred == ICmpInst::ICMP_EQ) |
3372 | 8 | return ConstantInt::getFalse(getCompareTy(RHS)); |
3373 | 35.8k | if (Pred == ICmpInst::ICMP_NE) |
3374 | 4 | return ConstantInt::getTrue(getCompareTy(RHS)); |
3375 | 35.8k | } |
3376 | 36.8k | } |
3377 | | |
3378 | | // If C is a power-of-2: |
3379 | | // (C << X) >u 0x8000 --> false |
3380 | | // (C << X) <=u 0x8000 --> true |
3381 | 607M | if (match(LHS, m_Shl(m_Power2(), m_Value())) && match(RHS, m_SignMask())129k ) { |
3382 | 14 | if (Pred == ICmpInst::ICMP_UGT) |
3383 | 0 | return ConstantInt::getFalse(getCompareTy(RHS)); |
3384 | 14 | if (Pred == ICmpInst::ICMP_ULE) |
3385 | 0 | return ConstantInt::getTrue(getCompareTy(RHS)); |
3386 | 14 | } |
3387 | | |
3388 | 607M | if (!MaxRecurse || !LBO602M || !RBO129M || LBO->getOpcode() != RBO->getOpcode()11.7M ) |
3389 | 602M | return nullptr; |
3390 | | |
3391 | 5.00M | if (LBO->getOperand(0) == RBO->getOperand(0)) { |
3392 | 53.2k | switch (LBO->getOpcode()) { |
3393 | 32.9k | default: |
3394 | 32.9k | break; |
3395 | 32.9k | case Instruction::Shl: { |
3396 | 12.7k | bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO)1.10k ; |
3397 | 12.7k | bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO)4.90k ; |
3398 | 12.7k | if (!NUW || (1.05k ICmpInst::isSigned(Pred)1.05k && !NSW131 ) || |
3399 | 12.7k | !isKnownNonZero(LBO->getOperand(0), Q)922 ) |
3400 | 11.9k | break; |
3401 | 837 | if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(1), |
3402 | 837 | RBO->getOperand(1), Q, MaxRecurse - 1)) |
3403 | 8 | return V; |
3404 | 829 | break; |
3405 | 837 | } |
3406 | | // If C1 & C2 == C1, A = X and/or C1, B = X and/or C2: |
3407 | | // icmp ule A, B -> true |
3408 | | // icmp ugt A, B -> false |
3409 | | // icmp sle A, B -> true (C1 and C2 are the same sign) |
3410 | | // icmp sgt A, B -> false (C1 and C2 are the same sign) |
3411 | 7.43k | case Instruction::And: |
3412 | 7.54k | case Instruction::Or: { |
3413 | 7.54k | const APInt *C1, *C2; |
3414 | 7.54k | if (ICmpInst::isRelational(Pred) && |
3415 | 7.54k | match(LBO->getOperand(1), m_APInt(C1))6.30k && |
3416 | 7.54k | match(RBO->getOperand(1), m_APInt(C2))2.13k ) { |
3417 | 2.13k | if (!C1->isSubsetOf(*C2)) { |
3418 | 1.55k | std::swap(C1, C2); |
3419 | 1.55k | Pred = ICmpInst::getSwappedPredicate(Pred); |
3420 | 1.55k | } |
3421 | 2.13k | if (C1->isSubsetOf(*C2)) { |
3422 | 2.04k | if (Pred == ICmpInst::ICMP_ULE) |
3423 | 2 | return ConstantInt::getTrue(getCompareTy(LHS)); |
3424 | 2.03k | if (Pred == ICmpInst::ICMP_UGT) |
3425 | 0 | return ConstantInt::getFalse(getCompareTy(LHS)); |
3426 | 2.03k | if (C1->isNonNegative() == C2->isNonNegative()) { |
3427 | 2.03k | if (Pred == ICmpInst::ICMP_SLE) |
3428 | 0 | return ConstantInt::getTrue(getCompareTy(LHS)); |
3429 | 2.03k | if (Pred == ICmpInst::ICMP_SGT) |
3430 | 0 | return ConstantInt::getFalse(getCompareTy(LHS)); |
3431 | 2.03k | } |
3432 | 2.03k | } |
3433 | 2.13k | } |
3434 | 7.53k | break; |
3435 | 7.54k | } |
3436 | 53.2k | } |
3437 | 53.2k | } |
3438 | | |
3439 | 5.00M | if (LBO->getOperand(1) == RBO->getOperand(1)) { |
3440 | 1.58M | switch (LBO->getOpcode()) { |
3441 | 1.18M | default: |
3442 | 1.18M | break; |
3443 | 1.18M | case Instruction::UDiv: |
3444 | 159k | case Instruction::LShr: |
3445 | 159k | if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(LBO)145k || |
3446 | 159k | !Q.IIQ.isExact(RBO)12.1k ) |
3447 | 147k | break; |
3448 | 11.9k | if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0), |
3449 | 11.9k | RBO->getOperand(0), Q, MaxRecurse - 1)) |
3450 | 11 | return V; |
3451 | 11.9k | break; |
3452 | 38.9k | case Instruction::SDiv: |
3453 | 38.9k | if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO)5.29k || |
3454 | 38.9k | !Q.IIQ.isExact(RBO)2.10k ) |
3455 | 36.8k | break; |
3456 | 2.10k | if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0), |
3457 | 2.10k | RBO->getOperand(0), Q, MaxRecurse - 1)) |
3458 | 0 | return V; |
3459 | 2.10k | break; |
3460 | 131k | case Instruction::AShr: |
3461 | 131k | if (!Q.IIQ.isExact(LBO) || !Q.IIQ.isExact(RBO)117k ) |
3462 | 14.9k | break; |
3463 | 116k | if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0), |
3464 | 116k | RBO->getOperand(0), Q, MaxRecurse - 1)) |
3465 | 30 | return V; |
3466 | 116k | break; |
3467 | 116k | case Instruction::Shl: { |
3468 | 70.3k | bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO)48.2k ; |
3469 | 70.3k | bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO)38.5k ; |
3470 | 70.3k | if (!NUW && !NSW24.7k ) |
3471 | 20.1k | break; |
3472 | 50.1k | if (!NSW && ICmpInst::isSigned(Pred)42.7k ) |
3473 | 27 | break; |
3474 | 50.1k | if (Value *V = simplifyICmpInst(Pred, LBO->getOperand(0), |
3475 | 50.1k | RBO->getOperand(0), Q, MaxRecurse - 1)) |
3476 | 379 | return V; |
3477 | 49.7k | break; |
3478 | 50.1k | } |
3479 | 1.58M | } |
3480 | 1.58M | } |
3481 | 5.00M | return nullptr; |
3482 | 5.00M | } |
3483 | | |
3484 | | /// simplify integer comparisons where at least one operand of the compare |
3485 | | /// matches an integer min/max idiom. |
3486 | | static Value *simplifyICmpWithMinMax(CmpPredicate Pred, Value *LHS, Value *RHS, |
3487 | | const SimplifyQuery &Q, |
3488 | 607M | unsigned MaxRecurse) { |
3489 | 607M | Type *ITy = getCompareTy(LHS); // The return type. |
3490 | 607M | Value *A, *B; |
3491 | 607M | CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE; |
3492 | 607M | CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B". |
3493 | | |
3494 | | // Signed variants on "max(a,b)>=a -> true". |
3495 | 607M | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (2.13M A == RHS2.13M || B == RHS2.13M )) { |
3496 | 25.6k | if (A != RHS) |
3497 | 24.2k | std::swap(A, B); // smax(A, B) pred A. |
3498 | 25.6k | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
3499 | | // We analyze this as smax(A, B) pred A. |
3500 | 25.6k | P = Pred; |
3501 | 607M | } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) && |
3502 | 607M | (204k A == LHS204k || B == LHS203k )) { |
3503 | 2.04k | if (A != LHS) |
3504 | 544 | std::swap(A, B); // A pred smax(A, B). |
3505 | 2.04k | EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B". |
3506 | | // We analyze this as smax(A, B) swapped-pred A. |
3507 | 2.04k | P = CmpInst::getSwappedPredicate(Pred); |
3508 | 607M | } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) && |
3509 | 607M | (767k A == RHS767k || B == RHS762k )) { |
3510 | 14.4k | if (A != RHS) |
3511 | 9.43k | std::swap(A, B); // smin(A, B) pred A. |
3512 | 14.4k | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
3513 | | // We analyze this as smax(-A, -B) swapped-pred -A. |
3514 | | // Note that we do not need to actually form -A or -B thanks to EqP. |
3515 | 14.4k | P = CmpInst::getSwappedPredicate(Pred); |
3516 | 607M | } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) && |
3517 | 607M | (576k A == LHS576k || B == LHS576k )) { |
3518 | 1.15k | if (A != LHS) |
3519 | 527 | std::swap(A, B); // A pred smin(A, B). |
3520 | 1.15k | EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B". |
3521 | | // We analyze this as smax(-A, -B) pred -A. |
3522 | | // Note that we do not need to actually form -A or -B thanks to EqP. |
3523 | 1.15k | P = Pred; |
3524 | 1.15k | } |
3525 | 607M | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
3526 | | // Cases correspond to "max(A, B) p A". |
3527 | 43.3k | switch (P) { |
3528 | 1.22k | default: |
3529 | 1.22k | break; |
3530 | 18.4k | case CmpInst::ICMP_EQ: |
3531 | 20.6k | case CmpInst::ICMP_SLE: |
3532 | | // Equivalent to "A EqP B". This may be the same as the condition tested |
3533 | | // in the max/min; if so, we can just return that. |
3534 | 20.6k | if (Value *V = extractEquivalentCondition(LHS, EqP, A, B)) |
3535 | 4 | return V; |
3536 | 20.6k | if (Value *V = extractEquivalentCondition(RHS, EqP, A, B)) |
3537 | 0 | return V; |
3538 | | // Otherwise, see if "A EqP B" simplifies. |
3539 | 20.6k | if (MaxRecurse) |
3540 | 20.0k | if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
3541 | 7 | return V; |
3542 | 20.6k | break; |
3543 | 20.6k | case CmpInst::ICMP_NE: |
3544 | 19.9k | case CmpInst::ICMP_SGT: { |
3545 | 19.9k | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
3546 | | // Equivalent to "A InvEqP B". This may be the same as the condition |
3547 | | // tested in the max/min; if so, we can just return that. |
3548 | 19.9k | if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B)) |
3549 | 504 | return V; |
3550 | 19.3k | if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B)) |
3551 | 34 | return V; |
3552 | | // Otherwise, see if "A InvEqP B" simplifies. |
3553 | 19.3k | if (MaxRecurse) |
3554 | 18.8k | if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
3555 | 8 | return V; |
3556 | 19.3k | break; |
3557 | 19.3k | } |
3558 | 19.3k | case CmpInst::ICMP_SGE: |
3559 | | // Always true. |
3560 | 219 | return getTrue(ITy); |
3561 | 1.33k | case CmpInst::ICMP_SLT: |
3562 | | // Always false. |
3563 | 1.33k | return getFalse(ITy); |
3564 | 43.3k | } |
3565 | 43.3k | } |
3566 | | |
3567 | | // Unsigned variants on "max(a,b)>=a -> true". |
3568 | 607M | P = CmpInst::BAD_ICMP_PREDICATE; |
3569 | 607M | if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (1.22M A == RHS1.22M || B == RHS1.22M )) { |
3570 | 43.8k | if (A != RHS) |
3571 | 40.7k | std::swap(A, B); // umax(A, B) pred A. |
3572 | 43.8k | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
3573 | | // We analyze this as umax(A, B) pred A. |
3574 | 43.8k | P = Pred; |
3575 | 607M | } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) && |
3576 | 607M | (315k A == LHS315k || B == LHS313k )) { |
3577 | 3.49k | if (A != LHS) |
3578 | 1.11k | std::swap(A, B); // A pred umax(A, B). |
3579 | 3.49k | EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B". |
3580 | | // We analyze this as umax(A, B) swapped-pred A. |
3581 | 3.49k | P = CmpInst::getSwappedPredicate(Pred); |
3582 | 607M | } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) && |
3583 | 607M | (3.73M A == RHS3.73M || B == RHS3.72M )) { |
3584 | 45.0k | if (A != RHS) |
3585 | 29.6k | std::swap(A, B); // umin(A, B) pred A. |
3586 | 45.0k | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
3587 | | // We analyze this as umax(-A, -B) swapped-pred -A. |
3588 | | // Note that we do not need to actually form -A or -B thanks to EqP. |
3589 | 45.0k | P = CmpInst::getSwappedPredicate(Pred); |
3590 | 607M | } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) && |
3591 | 607M | (636k A == LHS636k || B == LHS629k )) { |
3592 | 9.91k | if (A != LHS) |
3593 | 3.23k | std::swap(A, B); // A pred umin(A, B). |
3594 | 9.91k | EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B". |
3595 | | // We analyze this as umax(-A, -B) pred -A. |
3596 | | // Note that we do not need to actually form -A or -B thanks to EqP. |
3597 | 9.91k | P = Pred; |
3598 | 9.91k | } |
3599 | 607M | if (P != CmpInst::BAD_ICMP_PREDICATE) { |
3600 | | // Cases correspond to "max(A, B) p A". |
3601 | 102k | switch (P) { |
3602 | 8.82k | default: |
3603 | 8.82k | break; |
3604 | 28.4k | case CmpInst::ICMP_EQ: |
3605 | 31.1k | case CmpInst::ICMP_ULE: |
3606 | | // Equivalent to "A EqP B". This may be the same as the condition tested |
3607 | | // in the max/min; if so, we can just return that. |
3608 | 31.1k | if (Value *V = extractEquivalentCondition(LHS, EqP, A, B)) |
3609 | 0 | return V; |
3610 | 31.1k | if (Value *V = extractEquivalentCondition(RHS, EqP, A, B)) |
3611 | 0 | return V; |
3612 | | // Otherwise, see if "A EqP B" simplifies. |
3613 | 31.1k | if (MaxRecurse) |
3614 | 30.9k | if (Value *V = simplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1)) |
3615 | 180 | return V; |
3616 | 30.9k | break; |
3617 | 30.9k | case CmpInst::ICMP_NE: |
3618 | 57.1k | case CmpInst::ICMP_UGT: { |
3619 | 57.1k | CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP); |
3620 | | // Equivalent to "A InvEqP B". This may be the same as the condition |
3621 | | // tested in the max/min; if so, we can just return that. |
3622 | 57.1k | if (Value *V = extractEquivalentCondition(LHS, InvEqP, A, B)) |
3623 | 381 | return V; |
3624 | 56.7k | if (Value *V = extractEquivalentCondition(RHS, InvEqP, A, B)) |
3625 | 1.79k | return V; |
3626 | | // Otherwise, see if "A InvEqP B" simplifies. |
3627 | 54.9k | if (MaxRecurse) |
3628 | 54.7k | if (Value *V = simplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1)) |
3629 | 429 | return V; |
3630 | 54.5k | break; |
3631 | 54.9k | } |
3632 | 54.5k | case CmpInst::ICMP_UGE: |
3633 | 764 | return getTrue(ITy); |
3634 | 4.37k | case CmpInst::ICMP_ULT: |
3635 | 4.37k | return getFalse(ITy); |
3636 | 102k | } |
3637 | 102k | } |
3638 | | |
3639 | | // Comparing 1 each of min/max with a common operand? |
3640 | | // Canonicalize min operand to RHS. |
3641 | 607M | if (match(LHS, m_UMin(m_Value(), m_Value())) || |
3642 | 607M | match(LHS, m_SMin(m_Value(), m_Value()))604M ) { |
3643 | 4.50M | std::swap(LHS, RHS); |
3644 | 4.50M | Pred = ICmpInst::getSwappedPredicate(Pred); |
3645 | 4.50M | } |
3646 | | |
3647 | 607M | Value *C, *D; |
3648 | 607M | if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && |
3649 | 607M | match(RHS, m_SMin(m_Value(C), m_Value(D)))2.14M && |
3650 | 607M | (42.0k A == C42.0k || A == D41.4k || B == C41.2k || B == D41.2k )) { |
3651 | | // smax(A, B) >=s smin(A, D) --> true |
3652 | 843 | if (Pred == CmpInst::ICMP_SGE) |
3653 | 15 | return getTrue(ITy); |
3654 | | // smax(A, B) <s smin(A, D) --> false |
3655 | 828 | if (Pred == CmpInst::ICMP_SLT) |
3656 | 150 | return getFalse(ITy); |
3657 | 607M | } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && |
3658 | 607M | match(RHS, m_UMin(m_Value(C), m_Value(D)))1.24M && |
3659 | 607M | (66.5k A == C66.5k || A == D64.9k || B == C64.7k || B == D64.7k )) { |
3660 | | // umax(A, B) >=u umin(A, D) --> true |
3661 | 1.84k | if (Pred == CmpInst::ICMP_UGE) |
3662 | 16 | return getTrue(ITy); |
3663 | | // umax(A, B) <u umin(A, D) --> false |
3664 | 1.83k | if (Pred == CmpInst::ICMP_ULT) |
3665 | 50 | return getFalse(ITy); |
3666 | 1.83k | } |
3667 | | |
3668 | 607M | return nullptr; |
3669 | 607M | } |
3670 | | |
3671 | | static Value *simplifyICmpWithDominatingAssume(CmpPredicate Predicate, |
3672 | | Value *LHS, Value *RHS, |
3673 | 607M | const SimplifyQuery &Q) { |
3674 | | // Gracefully handle instructions that have not been inserted yet. |
3675 | 607M | if (!Q.AC || !Q.CxtI523M ) |
3676 | 89.2M | return nullptr; |
3677 | | |
3678 | 1.03G | for (Value *AssumeBaseOp : {LHS, RHS})518M { |
3679 | 1.03G | for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) { |
3680 | 45.9M | if (!AssumeVH) |
3681 | 3.22M | continue; |
3682 | | |
3683 | 42.7M | CallInst *Assume = cast<CallInst>(AssumeVH); |
3684 | 42.7M | if (std::optional<bool> Imp = isImpliedCondition( |
3685 | 42.7M | Assume->getArgOperand(0), Predicate, LHS, RHS, Q.DL)) |
3686 | 35.1M | if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT)) |
3687 | 119k | return ConstantInt::get(getCompareTy(LHS), *Imp); |
3688 | 42.7M | } |
3689 | 1.03G | } |
3690 | | |
3691 | 518M | return nullptr; |
3692 | 518M | } |
3693 | | |
3694 | | static Value *simplifyICmpWithIntrinsicOnLHS(CmpPredicate Pred, Value *LHS, |
3695 | 1.21G | Value *RHS) { |
3696 | 1.21G | auto *II = dyn_cast<IntrinsicInst>(LHS); |
3697 | 1.21G | if (!II) |
3698 | 1.20G | return nullptr; |
3699 | | |
3700 | 11.2M | switch (II->getIntrinsicID()) { |
3701 | 95.9k | case Intrinsic::uadd_sat: |
3702 | | // uadd.sat(X, Y) uge X + Y |
3703 | 95.9k | if (match(RHS, m_c_Add(m_Specific(II->getArgOperand(0)), |
3704 | 95.9k | m_Specific(II->getArgOperand(1))))) { |
3705 | 0 | if (Pred == ICmpInst::ICMP_UGE) |
3706 | 0 | return ConstantInt::getTrue(getCompareTy(II)); |
3707 | 0 | if (Pred == ICmpInst::ICMP_ULT) |
3708 | 0 | return ConstantInt::getFalse(getCompareTy(II)); |
3709 | 0 | } |
3710 | 95.9k | return nullptr; |
3711 | 486k | case Intrinsic::usub_sat: |
3712 | | // usub.sat(X, Y) ule X - Y |
3713 | 486k | if (match(RHS, m_Sub(m_Specific(II->getArgOperand(0)), |
3714 | 486k | m_Specific(II->getArgOperand(1))))) { |
3715 | 19 | if (Pred == ICmpInst::ICMP_ULE) |
3716 | 4 | return ConstantInt::getTrue(getCompareTy(II)); |
3717 | 15 | if (Pred == ICmpInst::ICMP_UGT) |
3718 | 6 | return ConstantInt::getFalse(getCompareTy(II)); |
3719 | 15 | } |
3720 | 486k | return nullptr; |
3721 | 10.6M | default: |
3722 | 10.6M | return nullptr; |
3723 | 11.2M | } |
3724 | 11.2M | } |
3725 | | |
3726 | | /// Helper method to get range from metadata or attribute. |
3727 | | static std::optional<ConstantRange> getRange(Value *V, |
3728 | 611M | const InstrInfoQuery &IIQ) { |
3729 | 611M | if (Instruction *I = dyn_cast<Instruction>(V)) |
3730 | 157M | if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range)) |
3731 | 2.10M | return getConstantRangeFromMetadata(*MD); |
3732 | | |
3733 | 609M | if (const Argument *A = dyn_cast<Argument>(V)) |
3734 | 20.8M | return A->getRange(); |
3735 | 588M | else if (const CallBase *CB = dyn_cast<CallBase>(V)) |
3736 | 6.13M | return CB->getRange(); |
3737 | | |
3738 | 582M | return std::nullopt; |
3739 | 609M | } |
3740 | | |
3741 | | /// Given operands for an ICmpInst, see if we can fold the result. |
3742 | | /// If not, this returns null. |
3743 | | static Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, |
3744 | 640M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
3745 | 640M | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
3746 | | |
3747 | 640M | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
3748 | 36.2M | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
3749 | 21.8M | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI); |
3750 | | |
3751 | | // If we have a constant, make sure it is on the RHS. |
3752 | 14.3M | std::swap(LHS, RHS); |
3753 | 14.3M | Pred = CmpInst::getSwappedPredicate(Pred); |
3754 | 14.3M | } |
3755 | 640M | assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X"); |
3756 | | |
3757 | 619M | Type *ITy = getCompareTy(LHS); // The return type. |
3758 | | |
3759 | | // icmp poison, X -> poison |
3760 | 619M | if (isa<PoisonValue>(RHS)) |
3761 | 11.8k | return PoisonValue::get(ITy); |
3762 | | |
3763 | | // For EQ and NE, we can always pick a value for the undef to make the |
3764 | | // predicate pass or fail, so we can return undef. |
3765 | | // Matches behavior in llvm::ConstantFoldCompareInstruction. |
3766 | 619M | if (Q.isUndefValue(RHS) && ICmpInst::isEquality(Pred)40.6k ) |
3767 | 10.0k | return UndefValue::get(ITy); |
3768 | | |
3769 | | // icmp X, X -> true/false |
3770 | | // icmp X, undef -> true/false because undef could be X. |
3771 | 618M | if (LHS == RHS || Q.isUndefValue(RHS)617M ) |
3772 | 1.68M | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
3773 | | |
3774 | 617M | if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q)) |
3775 | 666k | return V; |
3776 | | |
3777 | | // TODO: Sink/common this with other potentially expensive calls that use |
3778 | | // ValueTracking? See comment below for isKnownNonEqual(). |
3779 | 616M | if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q)) |
3780 | 6.67M | return V; |
3781 | | |
3782 | 609M | if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q)) |
3783 | 649k | return V; |
3784 | | |
3785 | | // If both operands have range metadata, use the metadata |
3786 | | // to simplify the comparison. |
3787 | 609M | if (std::optional<ConstantRange> RhsCr = getRange(RHS, Q.IIQ)) |
3788 | 2.01M | if (std::optional<ConstantRange> LhsCr = getRange(LHS, Q.IIQ)) { |
3789 | 719k | if (LhsCr->icmp(Pred, *RhsCr)) |
3790 | 69 | return ConstantInt::getTrue(ITy); |
3791 | | |
3792 | 719k | if (LhsCr->icmp(CmpInst::getInversePredicate(Pred), *RhsCr)) |
3793 | 244 | return ConstantInt::getFalse(ITy); |
3794 | 719k | } |
3795 | | |
3796 | | // Compare of cast, for example (zext X) != 0 -> X != 0 |
3797 | 609M | if (isa<CastInst>(LHS) && (17.4M isa<Constant>(RHS)17.4M || isa<CastInst>(RHS)2.55M )) { |
3798 | 15.9M | Instruction *LI = cast<CastInst>(LHS); |
3799 | 15.9M | Value *SrcOp = LI->getOperand(0); |
3800 | 15.9M | Type *SrcTy = SrcOp->getType(); |
3801 | 15.9M | Type *DstTy = LI->getType(); |
3802 | | |
3803 | | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
3804 | | // if the integer type is the same size as the pointer type. |
3805 | 15.9M | if (MaxRecurse && isa<PtrToIntInst>(LI)15.8M && |
3806 | 15.9M | Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()1.39M ) { |
3807 | 1.39M | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
3808 | | // Transfer the cast to the constant. |
3809 | 1.27M | if (Value *V = simplifyICmpInst(Pred, SrcOp, |
3810 | 1.27M | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
3811 | 1.27M | Q, MaxRecurse - 1)) |
3812 | 390 | return V; |
3813 | 1.27M | } else if (PtrToIntInst *122k RI122k = dyn_cast<PtrToIntInst>(RHS)) { |
3814 | 121k | if (RI->getOperand(0)->getType() == SrcTy) |
3815 | | // Compare without the cast. |
3816 | 121k | if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q, |
3817 | 121k | MaxRecurse - 1)) |
3818 | 128 | return V; |
3819 | 121k | } |
3820 | 1.39M | } |
3821 | | |
3822 | 15.9M | if (isa<ZExtInst>(LHS)) { |
3823 | | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
3824 | | // same type. |
3825 | 3.60M | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
3826 | 298k | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()297k ) |
3827 | | // Compare X and Y. Note that signed predicates become unsigned. |
3828 | 280k | if (Value *V = |
3829 | 280k | simplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), SrcOp, |
3830 | 280k | RI->getOperand(0), Q, MaxRecurse - 1)) |
3831 | 3.73k | return V; |
3832 | 298k | } |
3833 | | // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true. |
3834 | 3.30M | else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
3835 | 12.8k | if (SrcOp == RI->getOperand(0)) { |
3836 | 111 | if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE) |
3837 | 0 | return ConstantInt::getTrue(ITy); |
3838 | 111 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT) |
3839 | 97 | return ConstantInt::getFalse(ITy); |
3840 | 111 | } |
3841 | 12.8k | } |
3842 | | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
3843 | | // too. If not, then try to deduce the result of the comparison. |
3844 | 3.29M | else if (match(RHS, m_ImmConstant())) { |
3845 | 3.28M | Constant *C = dyn_cast<Constant>(RHS); |
3846 | 3.28M | assert(C != nullptr); |
3847 | | |
3848 | | // Compute the constant that would happen if we truncated to SrcTy then |
3849 | | // reextended to DstTy. |
3850 | 3.28M | Constant *Trunc = |
3851 | 3.28M | ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL); |
3852 | 3.28M | assert(Trunc && "Constant-fold of ImmConstant should not fail"); |
3853 | 3.28M | Constant *RExt = |
3854 | 3.28M | ConstantFoldCastOperand(CastInst::ZExt, Trunc, DstTy, Q.DL); |
3855 | 3.28M | assert(RExt && "Constant-fold of ImmConstant should not fail"); |
3856 | 3.28M | Constant *AnyEq = |
3857 | 3.28M | ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL); |
3858 | 3.28M | assert(AnyEq && "Constant-fold of ImmConstant should not fail"); |
3859 | | |
3860 | | // If the re-extended constant didn't change any of the elements then |
3861 | | // this is effectively also a case of comparing two zero-extended |
3862 | | // values. |
3863 | 3.28M | if (AnyEq->isAllOnesValue() && MaxRecurse3.17M ) |
3864 | 3.14M | if (Value *V = simplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
3865 | 3.14M | SrcOp, Trunc, Q, MaxRecurse - 1)) |
3866 | 641k | return V; |
3867 | | |
3868 | | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
3869 | | // there. Use this to work out the result of the comparison. |
3870 | 2.64M | if (AnyEq->isNullValue()) { |
3871 | 107k | switch (Pred) { |
3872 | 0 | default: |
3873 | 0 | llvm_unreachable("Unknown ICmp predicate!"); |
3874 | | // LHS <u RHS. |
3875 | 50.2k | case ICmpInst::ICMP_EQ: |
3876 | 59.1k | case ICmpInst::ICMP_UGT: |
3877 | 59.6k | case ICmpInst::ICMP_UGE: |
3878 | 59.6k | return Constant::getNullValue(ITy); |
3879 | | |
3880 | 13.3k | case ICmpInst::ICMP_NE: |
3881 | 35.2k | case ICmpInst::ICMP_ULT: |
3882 | 35.6k | case ICmpInst::ICMP_ULE: |
3883 | 35.6k | return Constant::getAllOnesValue(ITy); |
3884 | | |
3885 | | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
3886 | | // is non-negative then LHS <s RHS. |
3887 | 11.2k | case ICmpInst::ICMP_SGT: |
3888 | 11.4k | case ICmpInst::ICMP_SGE: |
3889 | 11.4k | return ConstantFoldCompareInstOperands( |
3890 | 11.4k | ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()), |
3891 | 11.4k | Q.DL); |
3892 | 788 | case ICmpInst::ICMP_SLT: |
3893 | 857 | case ICmpInst::ICMP_SLE: |
3894 | 857 | return ConstantFoldCompareInstOperands( |
3895 | 857 | ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()), |
3896 | 857 | Q.DL); |
3897 | 107k | } |
3898 | 107k | } |
3899 | 2.64M | } |
3900 | 3.60M | } |
3901 | | |
3902 | 15.1M | if (isa<SExtInst>(LHS)) { |
3903 | | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
3904 | | // same type. |
3905 | 1.08M | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
3906 | 121k | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()118k ) |
3907 | | // Compare X and Y. Note that the predicate does not change. |
3908 | 118k | if (Value *V = simplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q, |
3909 | 118k | MaxRecurse - 1)) |
3910 | 1.18k | return V; |
3911 | 121k | } |
3912 | | // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true. |
3913 | 967k | else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
3914 | 27.9k | if (SrcOp == RI->getOperand(0)) { |
3915 | 199 | if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE) |
3916 | 0 | return ConstantInt::getTrue(ITy); |
3917 | 199 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT) |
3918 | 5 | return ConstantInt::getFalse(ITy); |
3919 | 199 | } |
3920 | 27.9k | } |
3921 | | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
3922 | | // too. If not, then try to deduce the result of the comparison. |
3923 | 939k | else if (match(RHS, m_ImmConstant())) { |
3924 | 937k | Constant *C = cast<Constant>(RHS); |
3925 | | |
3926 | | // Compute the constant that would happen if we truncated to SrcTy then |
3927 | | // reextended to DstTy. |
3928 | 937k | Constant *Trunc = |
3929 | 937k | ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL); |
3930 | 937k | assert(Trunc && "Constant-fold of ImmConstant should not fail"); |
3931 | 937k | Constant *RExt = |
3932 | 937k | ConstantFoldCastOperand(CastInst::SExt, Trunc, DstTy, Q.DL); |
3933 | 937k | assert(RExt && "Constant-fold of ImmConstant should not fail"); |
3934 | 937k | Constant *AnyEq = |
3935 | 937k | ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL); |
3936 | 937k | assert(AnyEq && "Constant-fold of ImmConstant should not fail"); |
3937 | | |
3938 | | // If the re-extended constant didn't change then this is effectively |
3939 | | // also a case of comparing two sign-extended values. |
3940 | 937k | if (AnyEq->isAllOnesValue() && MaxRecurse901k ) |
3941 | 897k | if (Value *V = |
3942 | 897k | simplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1)) |
3943 | 63.8k | return V; |
3944 | | |
3945 | | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
3946 | | // bits there. Use this to work out the result of the comparison. |
3947 | 873k | if (AnyEq->isNullValue()) { |
3948 | 36.4k | switch (Pred) { |
3949 | 0 | default: |
3950 | 0 | llvm_unreachable("Unknown ICmp predicate!"); |
3951 | 2.99k | case ICmpInst::ICMP_EQ: |
3952 | 2.99k | return Constant::getNullValue(ITy); |
3953 | 1.39k | case ICmpInst::ICMP_NE: |
3954 | 1.39k | return Constant::getAllOnesValue(ITy); |
3955 | | |
3956 | | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
3957 | | // LHS >s RHS. |
3958 | 631 | case ICmpInst::ICMP_SGT: |
3959 | 742 | case ICmpInst::ICMP_SGE: |
3960 | 742 | return ConstantFoldCompareInstOperands( |
3961 | 742 | ICmpInst::ICMP_SLT, C, Constant::getNullValue(C->getType()), |
3962 | 742 | Q.DL); |
3963 | 4.66k | case ICmpInst::ICMP_SLT: |
3964 | 5.20k | case ICmpInst::ICMP_SLE: |
3965 | 5.20k | return ConstantFoldCompareInstOperands( |
3966 | 5.20k | ICmpInst::ICMP_SGE, C, Constant::getNullValue(C->getType()), |
3967 | 5.20k | Q.DL); |
3968 | | |
3969 | | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
3970 | | // LHS >u RHS. |
3971 | 16.9k | case ICmpInst::ICMP_UGT: |
3972 | 18.3k | case ICmpInst::ICMP_UGE: |
3973 | | // Comparison is true iff the LHS <s 0. |
3974 | 18.3k | if (MaxRecurse) |
3975 | 18.3k | if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
3976 | 18.3k | Constant::getNullValue(SrcTy), Q, |
3977 | 18.3k | MaxRecurse - 1)) |
3978 | 436 | return V; |
3979 | 17.8k | break; |
3980 | 17.8k | case ICmpInst::ICMP_ULT: |
3981 | 7.82k | case ICmpInst::ICMP_ULE: |
3982 | | // Comparison is true iff the LHS >=s 0. |
3983 | 7.82k | if (MaxRecurse) |
3984 | 7.80k | if (Value *V = simplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
3985 | 7.80k | Constant::getNullValue(SrcTy), Q, |
3986 | 7.80k | MaxRecurse - 1)) |
3987 | 106 | return V; |
3988 | 7.72k | break; |
3989 | 36.4k | } |
3990 | 36.4k | } |
3991 | 873k | } |
3992 | 1.08M | } |
3993 | 15.1M | } |
3994 | | |
3995 | | // icmp eq|ne X, Y -> false|true if X != Y |
3996 | | // This is potentially expensive, and we have already computedKnownBits for |
3997 | | // compares with 0 above here, so only try this for a non-zero compare. |
3998 | 608M | if (ICmpInst::isEquality(Pred) && !match(RHS, m_Zero())405M && |
3999 | 608M | isKnownNonEqual(LHS, RHS, Q)185M ) { |
4000 | 673k | return Pred == ICmpInst::ICMP_NE ? getTrue(ITy)97.8k : getFalse(ITy)576k ; |
4001 | 673k | } |
4002 | | |
4003 | 607M | if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse)) |
4004 | 73.9k | return V; |
4005 | | |
4006 | 607M | if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse)) |
4007 | 10.2k | return V; |
4008 | | |
4009 | 607M | if (Value *V = simplifyICmpWithIntrinsicOnLHS(Pred, LHS, RHS)) |
4010 | 10 | return V; |
4011 | 607M | if (Value *V = simplifyICmpWithIntrinsicOnLHS( |
4012 | 607M | ICmpInst::getSwappedPredicate(Pred), RHS, LHS)) |
4013 | 0 | return V; |
4014 | | |
4015 | 607M | if (Value *V = simplifyICmpUsingMonotonicValues(Pred, LHS, RHS, Q)) |
4016 | 570 | return V; |
4017 | 607M | if (Value *V = simplifyICmpUsingMonotonicValues( |
4018 | 607M | ICmpInst::getSwappedPredicate(Pred), RHS, LHS, Q)) |
4019 | 935 | return V; |
4020 | | |
4021 | 607M | if (Value *V = simplifyICmpWithDominatingAssume(Pred, LHS, RHS, Q)) |
4022 | 119k | return V; |
4023 | | |
4024 | 607M | if (std::optional<bool> Res = |
4025 | 607M | isImpliedByDomCondition(Pred, LHS, RHS, Q.CxtI, Q.DL)) |
4026 | 1.05M | return ConstantInt::getBool(ITy, *Res); |
4027 | | |
4028 | | // Simplify comparisons of related pointers using a powerful, recursive |
4029 | | // GEP-walk when we have target data available.. |
4030 | 606M | if (LHS->getType()->isPointerTy()) |
4031 | 179M | if (auto *C = computePointerICmp(Pred, LHS, RHS, Q)) |
4032 | 308k | return C; |
4033 | 606M | if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS)) |
4034 | 1.64M | if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS)) |
4035 | 121k | if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() && |
4036 | 121k | Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) == |
4037 | 121k | Q.DL.getTypeSizeInBits(CLHS->getType())) |
4038 | 121k | if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(), |
4039 | 121k | CRHS->getPointerOperand(), Q)) |
4040 | 0 | return C; |
4041 | | |
4042 | | // If the comparison is with the result of a select instruction, check whether |
4043 | | // comparing with either branch of the select always yields the same value. |
4044 | 606M | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)597M ) |
4045 | 10.2M | if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
4046 | 383k | return V; |
4047 | | |
4048 | | // If the comparison is with the result of a phi instruction, check whether |
4049 | | // doing the compare with each incoming phi value yields a common result. |
4050 | 605M | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)540M ) |
4051 | 72.1M | if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
4052 | 208k | return V; |
4053 | | |
4054 | 605M | return nullptr; |
4055 | 605M | } |
4056 | | |
4057 | | Value *llvm::simplifyICmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, |
4058 | 262M | const SimplifyQuery &Q) { |
4059 | 262M | return ::simplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit); |
4060 | 262M | } |
4061 | | |
4062 | | /// Given operands for an FCmpInst, see if we can fold the result. |
4063 | | /// If not, this returns null. |
4064 | | static Value *simplifyFCmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, |
4065 | | FastMathFlags FMF, const SimplifyQuery &Q, |
4066 | 8.30M | unsigned MaxRecurse) { |
4067 | 8.30M | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
4068 | | |
4069 | 8.30M | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
4070 | 157k | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
4071 | 119k | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI, |
4072 | 119k | Q.CxtI); |
4073 | | |
4074 | | // If we have a constant, make sure it is on the RHS. |
4075 | 37.4k | std::swap(LHS, RHS); |
4076 | 37.4k | Pred = CmpInst::getSwappedPredicate(Pred); |
4077 | 37.4k | } |
4078 | | |
4079 | | // Fold trivial predicates. |
4080 | 8.18M | Type *RetTy = getCompareTy(LHS); |
4081 | 8.18M | if (Pred == FCmpInst::FCMP_FALSE) |
4082 | 0 | return getFalse(RetTy); |
4083 | 8.18M | if (Pred == FCmpInst::FCMP_TRUE) |
4084 | 0 | return getTrue(RetTy); |
4085 | | |
4086 | | // fcmp pred x, poison and fcmp pred poison, x |
4087 | | // fold to poison |
4088 | 8.18M | if (isa<PoisonValue>(LHS) || isa<PoisonValue>(RHS)) |
4089 | 242 | return PoisonValue::get(RetTy); |
4090 | | |
4091 | | // fcmp pred x, undef and fcmp pred undef, x |
4092 | | // fold to true if unordered, false if ordered |
4093 | 8.18M | if (Q.isUndefValue(LHS) || Q.isUndefValue(RHS)) { |
4094 | | // Choosing NaN for the undef will always make unordered comparison succeed |
4095 | | // and ordered comparison fail. |
4096 | 388 | return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred)); |
4097 | 388 | } |
4098 | | |
4099 | | // fcmp x,x -> true/false. Not all compares are foldable. |
4100 | 8.18M | if (LHS == RHS) { |
4101 | 12.4k | if (CmpInst::isTrueWhenEqual(Pred)) |
4102 | 2 | return getTrue(RetTy); |
4103 | 12.4k | if (CmpInst::isFalseWhenEqual(Pred)) |
4104 | 3.69k | return getFalse(RetTy); |
4105 | 12.4k | } |
4106 | | |
4107 | | // Fold (un)ordered comparison if we can determine there are no NaNs. |
4108 | | // |
4109 | | // This catches the 2 variable input case, constants are handled below as a |
4110 | | // class-like compare. |
4111 | 8.17M | if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO8.03M ) { |
4112 | 415k | KnownFPClass RHSClass = computeKnownFPClass(RHS, fcAllFlags, Q); |
4113 | 415k | KnownFPClass LHSClass = computeKnownFPClass(LHS, fcAllFlags, Q); |
4114 | | |
4115 | 415k | if (FMF.noNaNs() || |
4116 | 415k | (RHSClass.isKnownNeverNaN() && LHSClass.isKnownNeverNaN()400k )) |
4117 | 3.89k | return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD); |
4118 | | |
4119 | 411k | if (RHSClass.isKnownAlwaysNaN() || LHSClass.isKnownAlwaysNaN()411k ) |
4120 | 66 | return ConstantInt::get(RetTy, Pred == CmpInst::FCMP_UNO); |
4121 | 411k | } |
4122 | | |
4123 | 8.17M | const APFloat *C = nullptr; |
4124 | 8.17M | match(RHS, m_APFloatAllowPoison(C)); |
4125 | 8.17M | std::optional<KnownFPClass> FullKnownClassLHS; |
4126 | | |
4127 | | // Lazily compute the possible classes for LHS. Avoid computing it twice if |
4128 | | // RHS is a 0. |
4129 | 8.17M | auto computeLHSClass = [=, &FullKnownClassLHS](FPClassTest InterestedFlags = |
4130 | 8.17M | fcAllFlags) { |
4131 | 2.99M | if (FullKnownClassLHS) |
4132 | 504k | return *FullKnownClassLHS; |
4133 | 2.49M | return computeKnownFPClass(LHS, FMF, InterestedFlags, Q); |
4134 | 2.99M | }; |
4135 | | |
4136 | 8.17M | if (C && Q.CxtI3.81M ) { |
4137 | | // Fold out compares that express a class test. |
4138 | | // |
4139 | | // FIXME: Should be able to perform folds without context |
4140 | | // instruction. Always pass in the context function? |
4141 | | |
4142 | 3.74M | const Function *ParentF = Q.CxtI->getFunction(); |
4143 | 3.74M | auto [ClassVal, ClassTest] = fcmpToClassTest(Pred, *ParentF, LHS, C); |
4144 | 3.74M | if (ClassVal) { |
4145 | 2.36M | FullKnownClassLHS = computeLHSClass(); |
4146 | 2.36M | if ((FullKnownClassLHS->KnownFPClasses & ClassTest) == fcNone) |
4147 | 7.42k | return getFalse(RetTy); |
4148 | 2.35M | if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) == fcNone) |
4149 | 1.68k | return getTrue(RetTy); |
4150 | 2.35M | } |
4151 | 3.74M | } |
4152 | | |
4153 | | // Handle fcmp with constant RHS. |
4154 | 8.16M | if (C) { |
4155 | | // TODO: If we always required a context function, we wouldn't need to |
4156 | | // special case nans. |
4157 | 3.80M | if (C->isNaN()) |
4158 | 43 | return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred)); |
4159 | | |
4160 | | // TODO: Need version fcmpToClassTest which returns implied class when the |
4161 | | // compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but |
4162 | | // isn't implementable as a class call. |
4163 | 3.80M | if (C->isNegative() && !C->isNegZero()189k ) { |
4164 | 188k | FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask; |
4165 | | |
4166 | | // TODO: We can catch more cases by using a range check rather than |
4167 | | // relying on CannotBeOrderedLessThanZero. |
4168 | 188k | switch (Pred) { |
4169 | 4.46k | case FCmpInst::FCMP_UGE: |
4170 | 17.7k | case FCmpInst::FCMP_UGT: |
4171 | 21.7k | case FCmpInst::FCMP_UNE: { |
4172 | 21.7k | KnownFPClass KnownClass = computeLHSClass(Interested); |
4173 | | |
4174 | | // (X >= 0) implies (X > C) when (C < 0) |
4175 | 21.7k | if (KnownClass.cannotBeOrderedLessThanZero()) |
4176 | 130 | return getTrue(RetTy); |
4177 | 21.6k | break; |
4178 | 21.7k | } |
4179 | 39.6k | case FCmpInst::FCMP_OEQ: |
4180 | 43.4k | case FCmpInst::FCMP_OLE: |
4181 | 104k | case FCmpInst::FCMP_OLT: { |
4182 | 104k | KnownFPClass KnownClass = computeLHSClass(Interested); |
4183 | | |
4184 | | // (X >= 0) implies !(X < C) when (C < 0) |
4185 | 104k | if (KnownClass.cannotBeOrderedLessThanZero()) |
4186 | 111 | return getFalse(RetTy); |
4187 | 104k | break; |
4188 | 104k | } |
4189 | 104k | default: |
4190 | 62.0k | break; |
4191 | 188k | } |
4192 | 188k | } |
4193 | | // Check comparison of [minnum/maxnum with constant] with other constant. |
4194 | 3.80M | const APFloat *C2; |
4195 | 3.80M | if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) && |
4196 | 3.80M | *C2 < *C397 ) || |
4197 | 3.80M | (match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2))) && |
4198 | 3.80M | *C2 > *C115 )) { |
4199 | 3 | bool IsMaxNum = |
4200 | 3 | cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum; |
4201 | | // The ordered relationship and minnum/maxnum guarantee that we do not |
4202 | | // have NaN constants, so ordered/unordered preds are handled the same. |
4203 | 3 | switch (Pred) { |
4204 | 1 | case FCmpInst::FCMP_OEQ: |
4205 | 1 | case FCmpInst::FCMP_UEQ: |
4206 | | // minnum(X, LesserC) == C --> false |
4207 | | // maxnum(X, GreaterC) == C --> false |
4208 | 1 | return getFalse(RetTy); |
4209 | 0 | case FCmpInst::FCMP_ONE: |
4210 | 0 | case FCmpInst::FCMP_UNE: |
4211 | | // minnum(X, LesserC) != C --> true |
4212 | | // maxnum(X, GreaterC) != C --> true |
4213 | 0 | return getTrue(RetTy); |
4214 | 0 | case FCmpInst::FCMP_OGE: |
4215 | 0 | case FCmpInst::FCMP_UGE: |
4216 | 2 | case FCmpInst::FCMP_OGT: |
4217 | 2 | case FCmpInst::FCMP_UGT: |
4218 | | // minnum(X, LesserC) >= C --> false |
4219 | | // minnum(X, LesserC) > C --> false |
4220 | | // maxnum(X, GreaterC) >= C --> true |
4221 | | // maxnum(X, GreaterC) > C --> true |
4222 | 2 | return ConstantInt::get(RetTy, IsMaxNum); |
4223 | 0 | case FCmpInst::FCMP_OLE: |
4224 | 0 | case FCmpInst::FCMP_ULE: |
4225 | 0 | case FCmpInst::FCMP_OLT: |
4226 | 0 | case FCmpInst::FCMP_ULT: |
4227 | | // minnum(X, LesserC) <= C --> true |
4228 | | // minnum(X, LesserC) < C --> true |
4229 | | // maxnum(X, GreaterC) <= C --> false |
4230 | | // maxnum(X, GreaterC) < C --> false |
4231 | 0 | return ConstantInt::get(RetTy, !IsMaxNum); |
4232 | 0 | default: |
4233 | | // TRUE/FALSE/ORD/UNO should be handled before this. |
4234 | 0 | llvm_unreachable("Unexpected fcmp predicate"); |
4235 | 3 | } |
4236 | 3 | } |
4237 | 3.80M | } |
4238 | | |
4239 | | // TODO: Could fold this with above if there were a matcher which returned all |
4240 | | // classes in a non-splat vector. |
4241 | 8.16M | if (match(RHS, m_AnyZeroFP())) { |
4242 | 1.95M | switch (Pred) { |
4243 | 88.4k | case FCmpInst::FCMP_OGE: |
4244 | 159k | case FCmpInst::FCMP_ULT: { |
4245 | 159k | FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask; |
4246 | 159k | if (!FMF.noNaNs()) |
4247 | 159k | Interested |= fcNan; |
4248 | | |
4249 | 159k | KnownFPClass Known = computeLHSClass(Interested); |
4250 | | |
4251 | | // Positive or zero X >= 0.0 --> true |
4252 | | // Positive or zero X < 0.0 --> false |
4253 | 159k | if ((FMF.noNaNs() || Known.isKnownNeverNaN()159k ) && |
4254 | 159k | Known.cannotBeOrderedLessThanZero()4.55k ) |
4255 | 5 | return Pred == FCmpInst::FCMP_OGE ? getTrue(RetTy)0 : getFalse(RetTy); |
4256 | 159k | break; |
4257 | 159k | } |
4258 | 159k | case FCmpInst::FCMP_UGE: |
4259 | 348k | case FCmpInst::FCMP_OLT: { |
4260 | 348k | FPClassTest Interested = KnownFPClass::OrderedLessThanZeroMask; |
4261 | 348k | KnownFPClass Known = computeLHSClass(Interested); |
4262 | | |
4263 | | // Positive or zero or nan X >= 0.0 --> true |
4264 | | // Positive or zero or nan X < 0.0 --> false |
4265 | 348k | if (Known.cannotBeOrderedLessThanZero()) |
4266 | 20 | return Pred == FCmpInst::FCMP_UGE ? getTrue(RetTy)0 : getFalse(RetTy); |
4267 | 347k | break; |
4268 | 348k | } |
4269 | 1.45M | default: |
4270 | 1.45M | break; |
4271 | 1.95M | } |
4272 | 1.95M | } |
4273 | | |
4274 | | // If the comparison is with the result of a select instruction, check whether |
4275 | | // comparing with either branch of the select always yields the same value. |
4276 | 8.16M | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)7.66M ) |
4277 | 678k | if (Value *V = threadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse)) |
4278 | 2.61k | return V; |
4279 | | |
4280 | | // If the comparison is with the result of a phi instruction, check whether |
4281 | | // doing the compare with each incoming phi value yields a common result. |
4282 | 8.16M | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)7.55M ) |
4283 | 975k | if (Value *V = threadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse)) |
4284 | 57 | return V; |
4285 | | |
4286 | 8.16M | return nullptr; |
4287 | 8.16M | } |
4288 | | |
4289 | | Value *llvm::simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, |
4290 | 3.43M | FastMathFlags FMF, const SimplifyQuery &Q) { |
4291 | 3.43M | return ::simplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit); |
4292 | 3.43M | } |
4293 | | |
4294 | | static Value *simplifyWithOpsReplaced(Value *V, |
4295 | | ArrayRef<std::pair<Value *, Value *>> Ops, |
4296 | | const SimplifyQuery &Q, |
4297 | | bool AllowRefinement, |
4298 | | SmallVectorImpl<Instruction *> *DropFlags, |
4299 | 334M | unsigned MaxRecurse) { |
4300 | 334M | assert((AllowRefinement || !Q.CanUseUndef) && |
4301 | 334M | "If AllowRefinement=false then CanUseUndef=false"); |
4302 | 335M | for (const auto &OpAndRepOp : Ops) { |
4303 | | // We cannot replace a constant, and shouldn't even try. |
4304 | 335M | if (isa<Constant>(OpAndRepOp.first)) |
4305 | 39.0M | return nullptr; |
4306 | | |
4307 | | // Trivial replacement. |
4308 | 296M | if (V == OpAndRepOp.first) |
4309 | 9.68M | return OpAndRepOp.second; |
4310 | 296M | } |
4311 | | |
4312 | 286M | if (!MaxRecurse--) |
4313 | 65.2M | return nullptr; |
4314 | | |
4315 | 221M | auto *I = dyn_cast<Instruction>(V); |
4316 | 221M | if (!I) |
4317 | 89.6M | return nullptr; |
4318 | | |
4319 | | // The arguments of a phi node might refer to a value from a previous |
4320 | | // cycle iteration. |
4321 | 131M | if (isa<PHINode>(I)) |
4322 | 10.7M | return nullptr; |
4323 | | |
4324 | | // Don't fold away llvm.is.constant checks based on assumptions. |
4325 | 120M | if (match(I, m_Intrinsic<Intrinsic::is_constant>())) |
4326 | 442k | return nullptr; |
4327 | | |
4328 | | // Don't simplify freeze. |
4329 | 120M | if (isa<FreezeInst>(I)) |
4330 | 147k | return nullptr; |
4331 | | |
4332 | 120M | for (const auto &OpAndRepOp : Ops)120M { |
4333 | | // For vector types, the simplification must hold per-lane, so forbid |
4334 | | // potentially cross-lane operations like shufflevector. |
4335 | 120M | if (OpAndRepOp.first->getType()->isVectorTy() && |
4336 | 120M | !isNotCrossLaneOperation(I)292k ) |
4337 | 49.3k | return nullptr; |
4338 | 120M | } |
4339 | | |
4340 | | // Replace Op with RepOp in instruction operands. |
4341 | 120M | SmallVector<Value *, 8> NewOps; |
4342 | 120M | bool AnyReplaced = false; |
4343 | 209M | for (Value *InstOp : I->operands()) { |
4344 | 209M | if (Value *NewInstOp = simplifyWithOpsReplaced( |
4345 | 209M | InstOp, Ops, Q, AllowRefinement, DropFlags, MaxRecurse)) { |
4346 | 9.91M | NewOps.push_back(NewInstOp); |
4347 | 9.91M | AnyReplaced = InstOp != NewInstOp; |
4348 | 199M | } else { |
4349 | 199M | NewOps.push_back(InstOp); |
4350 | 199M | } |
4351 | | |
4352 | | // Bail out if any operand is undef and SimplifyQuery disables undef |
4353 | | // simplification. Constant folding currently doesn't respect this option. |
4354 | 209M | if (isa<UndefValue>(NewOps.back()) && !Q.CanUseUndef179k ) |
4355 | 123k | return nullptr; |
4356 | 209M | } |
4357 | | |
4358 | 119M | if (!AnyReplaced) |
4359 | 110M | return nullptr; |
4360 | | |
4361 | 9.77M | if (!AllowRefinement) { |
4362 | | // General InstSimplify functions may refine the result, e.g. by returning |
4363 | | // a constant for a potentially poison value. To avoid this, implement only |
4364 | | // a few non-refining but profitable transforms here. |
4365 | | |
4366 | 4.65M | if (auto *BO = dyn_cast<BinaryOperator>(I)) { |
4367 | 1.16M | unsigned Opcode = BO->getOpcode(); |
4368 | | // id op x -> x, x op id -> x |
4369 | | // Exclude floats, because x op id may produce a different NaN value. |
4370 | 1.16M | if (!BO->getType()->isFPOrFPVectorTy()) { |
4371 | 1.14M | if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, I->getType())) |
4372 | 232k | return NewOps[1]; |
4373 | 909k | if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, I->getType(), |
4374 | 909k | /* RHS */ true)) |
4375 | 144k | return NewOps[0]; |
4376 | 909k | } |
4377 | | |
4378 | | // x & x -> x, x | x -> x |
4379 | 792k | if ((Opcode == Instruction::And || Opcode == Instruction::Or730k ) && |
4380 | 792k | NewOps[0] == NewOps[1]72.0k ) { |
4381 | | // or disjoint x, x results in poison. |
4382 | 1.71k | if (auto *PDI = dyn_cast<PossiblyDisjointInst>(BO)) { |
4383 | 451 | if (PDI->isDisjoint()) { |
4384 | 0 | if (!DropFlags) |
4385 | 0 | return nullptr; |
4386 | 0 | DropFlags->push_back(BO); |
4387 | 0 | } |
4388 | 451 | } |
4389 | 1.71k | return NewOps[0]; |
4390 | 1.71k | } |
4391 | | |
4392 | | // x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison |
4393 | | // by assumption and this case never wraps, so nowrap flags can be |
4394 | | // ignored. |
4395 | 790k | if ((Opcode == Instruction::Sub || Opcode == Instruction::Xor733k ) && |
4396 | 790k | NewOps[0] == NewOps[1]73.0k && |
4397 | 790k | any_of(Ops, [=](const auto &Rep) 27.8k { return NewOps[0] == Rep.second; }27.8k )) |
4398 | 26.2k | return Constant::getNullValue(I->getType()); |
4399 | | |
4400 | | // If we are substituting an absorber constant into a binop and extra |
4401 | | // poison can't leak if we remove the select -- because both operands of |
4402 | | // the binop are based on the same value -- then it may be safe to replace |
4403 | | // the value with the absorber constant. Examples: |
4404 | | // (Op == 0) ? 0 : (Op & -Op) --> Op & -Op |
4405 | | // (Op == 0) ? 0 : (Op * (binop Op, C)) --> Op * (binop Op, C) |
4406 | | // (Op == -1) ? -1 : (Op | (binop C, Op) --> Op | (binop C, Op) |
4407 | 764k | Constant *Absorber = ConstantExpr::getBinOpAbsorber(Opcode, I->getType()); |
4408 | 764k | if ((NewOps[0] == Absorber || NewOps[1] == Absorber728k ) && |
4409 | 764k | any_of(Ops, |
4410 | 50.8k | [=](const auto &Rep) { return impliesPoison(BO, Rep.first); })) |
4411 | 25.3k | return Absorber; |
4412 | 764k | } |
4413 | | |
4414 | 4.22M | if (isa<GetElementPtrInst>(I)) { |
4415 | | // getelementptr x, 0 -> x. |
4416 | | // This never returns poison, even if inbounds is set. |
4417 | 757k | if (NewOps.size() == 2 && match(NewOps[1], m_Zero())746k ) |
4418 | 91.5k | return NewOps[0]; |
4419 | 757k | } |
4420 | 5.11M | } else { |
4421 | | // The simplification queries below may return the original value. Consider: |
4422 | | // %div = udiv i32 %arg, %arg2 |
4423 | | // %mul = mul nsw i32 %div, %arg2 |
4424 | | // %cmp = icmp eq i32 %mul, %arg |
4425 | | // %sel = select i1 %cmp, i32 %div, i32 undef |
4426 | | // Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which |
4427 | | // simplifies back to %arg. This can only happen because %mul does not |
4428 | | // dominate %div. To ensure a consistent return value contract, we make sure |
4429 | | // that this case returns nullptr as well. |
4430 | 5.11M | auto PreventSelfSimplify = [V](Value *Simplified) { |
4431 | 5.11M | return Simplified != V ? Simplified5.11M : nullptr1.78k ; |
4432 | 5.11M | }; |
4433 | | |
4434 | 5.11M | return PreventSelfSimplify( |
4435 | 5.11M | ::simplifyInstructionWithOperands(I, NewOps, Q, MaxRecurse)); |
4436 | 5.11M | } |
4437 | | |
4438 | | // If all operands are constant after substituting Op for RepOp then we can |
4439 | | // constant fold the instruction. |
4440 | 4.13M | SmallVector<Constant *, 8> ConstOps; |
4441 | 6.52M | for (Value *NewOp : NewOps) { |
4442 | 6.52M | if (Constant *ConstOp = dyn_cast<Constant>(NewOp)) |
4443 | 4.53M | ConstOps.push_back(ConstOp); |
4444 | 1.98M | else |
4445 | 1.98M | return nullptr; |
4446 | 6.52M | } |
4447 | | |
4448 | | // Consider: |
4449 | | // %cmp = icmp eq i32 %x, 2147483647 |
4450 | | // %add = add nsw i32 %x, 1 |
4451 | | // %sel = select i1 %cmp, i32 -2147483648, i32 %add |
4452 | | // |
4453 | | // We can't replace %sel with %add unless we strip away the flags (which |
4454 | | // will be done in InstCombine). |
4455 | | // TODO: This may be unsound, because it only catches some forms of |
4456 | | // refinement. |
4457 | 2.14M | if (!AllowRefinement) { |
4458 | 2.14M | if (canCreatePoison(cast<Operator>(I), !DropFlags)) { |
4459 | | // abs cannot create poison if the value is known to never be int_min. |
4460 | 563k | if (auto *II = dyn_cast<IntrinsicInst>(I); |
4461 | 563k | II && II->getIntrinsicID() == Intrinsic::abs110k ) { |
4462 | 818 | if (!ConstOps[0]->isNotMinSignedValue()) |
4463 | 422 | return nullptr; |
4464 | 818 | } else |
4465 | 562k | return nullptr; |
4466 | 563k | } |
4467 | 1.58M | Constant *Res = ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI, |
4468 | 1.58M | /*AllowNonDeterministic=*/false); |
4469 | 1.58M | if (DropFlags && Res630k && I->hasPoisonGeneratingAnnotations()630k ) |
4470 | 215k | DropFlags->push_back(I); |
4471 | 1.58M | return Res; |
4472 | 2.14M | } |
4473 | | |
4474 | 0 | return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI, |
4475 | 0 | /*AllowNonDeterministic=*/false); |
4476 | 2.14M | } |
4477 | | |
4478 | | static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
4479 | | const SimplifyQuery &Q, |
4480 | | bool AllowRefinement, |
4481 | | SmallVectorImpl<Instruction *> *DropFlags, |
4482 | 72.1M | unsigned MaxRecurse) { |
4483 | 72.1M | return simplifyWithOpsReplaced(V, {{Op, RepOp}}, Q, AllowRefinement, |
4484 | 72.1M | DropFlags, MaxRecurse); |
4485 | 72.1M | } |
4486 | | |
4487 | | Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
4488 | | const SimplifyQuery &Q, |
4489 | | bool AllowRefinement, |
4490 | 47.7M | SmallVectorImpl<Instruction *> *DropFlags) { |
4491 | | // If refinement is disabled, also disable undef simplifications (which are |
4492 | | // always refinements) in SimplifyQuery. |
4493 | 47.7M | if (!AllowRefinement) |
4494 | 7.81M | return ::simplifyWithOpReplaced(V, Op, RepOp, Q.getWithoutUndef(), |
4495 | 7.81M | AllowRefinement, DropFlags, RecursionLimit); |
4496 | 39.9M | return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement, DropFlags, |
4497 | 39.9M | RecursionLimit); |
4498 | 47.7M | } |
4499 | | |
4500 | | /// Try to simplify a select instruction when its condition operand is an |
4501 | | /// integer comparison where one operand of the compare is a constant. |
4502 | | static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, |
4503 | 7.49M | const APInt *Y, bool TrueWhenUnset) { |
4504 | 7.49M | const APInt *C; |
4505 | | |
4506 | | // (X & Y) == 0 ? X & ~Y : X --> X |
4507 | | // (X & Y) != 0 ? X & ~Y : X --> X & ~Y |
4508 | 7.49M | if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C)))146k && |
4509 | 7.49M | *Y == ~*C681 ) |
4510 | 34 | return TrueWhenUnset ? FalseVal17 : TrueVal17 ; |
4511 | | |
4512 | | // (X & Y) == 0 ? X : X & ~Y --> X & ~Y |
4513 | | // (X & Y) != 0 ? X : X & ~Y --> X |
4514 | 7.49M | if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C)))320k && |
4515 | 7.49M | *Y == ~*C2.51k ) |
4516 | 85 | return TrueWhenUnset ? FalseVal : TrueVal0 ; |
4517 | | |
4518 | 7.49M | if (Y->isPowerOf2()) { |
4519 | | // (X & Y) == 0 ? X | Y : X --> X | Y |
4520 | | // (X & Y) != 0 ? X | Y : X --> X |
4521 | 5.84M | if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C)))58.6k && |
4522 | 5.84M | *Y == *C358 ) { |
4523 | | // We can't return the or if it has the disjoint flag. |
4524 | 92 | if (TrueWhenUnset && cast<PossiblyDisjointInst>(TrueVal)->isDisjoint()) |
4525 | 85 | return nullptr; |
4526 | 7 | return TrueWhenUnset ? TrueVal : FalseVal0 ; |
4527 | 92 | } |
4528 | | |
4529 | | // (X & Y) == 0 ? X : X | Y --> X |
4530 | | // (X & Y) != 0 ? X : X | Y --> X | Y |
4531 | 5.84M | if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C)))28.5k && |
4532 | 5.84M | *Y == *C1.66k ) { |
4533 | | // We can't return the or if it has the disjoint flag. |
4534 | 0 | if (!TrueWhenUnset && cast<PossiblyDisjointInst>(FalseVal)->isDisjoint()) |
4535 | 0 | return nullptr; |
4536 | 0 | return TrueWhenUnset ? TrueVal : FalseVal; |
4537 | 0 | } |
4538 | 5.84M | } |
4539 | | |
4540 | 7.49M | return nullptr; |
4541 | 7.49M | } |
4542 | | |
4543 | | static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS, |
4544 | | CmpPredicate Pred, Value *TVal, |
4545 | 28.7M | Value *FVal) { |
4546 | | // Canonicalize common cmp+sel operand as CmpLHS. |
4547 | 28.7M | if (CmpRHS == TVal || CmpRHS == FVal27.1M ) { |
4548 | 2.33M | std::swap(CmpLHS, CmpRHS); |
4549 | 2.33M | Pred = ICmpInst::getSwappedPredicate(Pred); |
4550 | 2.33M | } |
4551 | | |
4552 | | // Canonicalize common cmp+sel operand as TVal. |
4553 | 28.7M | if (CmpLHS == FVal) { |
4554 | 2.47M | std::swap(TVal, FVal); |
4555 | 2.47M | Pred = ICmpInst::getInversePredicate(Pred); |
4556 | 2.47M | } |
4557 | | |
4558 | | // A vector select may be shuffling together elements that are equivalent |
4559 | | // based on the max/min/select relationship. |
4560 | 28.7M | Value *X = CmpLHS, *Y = CmpRHS; |
4561 | 28.7M | bool PeekedThroughSelectShuffle = false; |
4562 | 28.7M | auto *Shuf = dyn_cast<ShuffleVectorInst>(FVal); |
4563 | 28.7M | if (Shuf && Shuf->isSelect()241 ) { |
4564 | 2 | if (Shuf->getOperand(0) == Y) |
4565 | 0 | FVal = Shuf->getOperand(1); |
4566 | 2 | else if (Shuf->getOperand(1) == Y) |
4567 | 0 | FVal = Shuf->getOperand(0); |
4568 | 2 | else |
4569 | 2 | return nullptr; |
4570 | 0 | PeekedThroughSelectShuffle = true; |
4571 | 0 | } |
4572 | | |
4573 | | // (X pred Y) ? X : max/min(X, Y) |
4574 | 28.7M | auto *MMI = dyn_cast<MinMaxIntrinsic>(FVal); |
4575 | 28.7M | if (!MMI || TVal != X1.58M || |
4576 | 28.7M | !match(FVal, m_c_MaxOrMin(m_Specific(X), m_Specific(Y)))93.5k ) |
4577 | 28.7M | return nullptr; |
4578 | | |
4579 | | // (X > Y) ? X : max(X, Y) --> max(X, Y) |
4580 | | // (X >= Y) ? X : max(X, Y) --> max(X, Y) |
4581 | | // (X < Y) ? X : min(X, Y) --> min(X, Y) |
4582 | | // (X <= Y) ? X : min(X, Y) --> min(X, Y) |
4583 | | // |
4584 | | // The equivalence allows a vector select (shuffle) of max/min and Y. Ex: |
4585 | | // (X > Y) ? X : (Z ? max(X, Y) : Y) |
4586 | | // If Z is true, this reduces as above, and if Z is false: |
4587 | | // (X > Y) ? X : Y --> max(X, Y) |
4588 | 27 | ICmpInst::Predicate MMPred = MMI->getPredicate(); |
4589 | 27 | if (MMPred == CmpInst::getStrictPredicate(Pred)) |
4590 | 10 | return MMI; |
4591 | | |
4592 | | // Other transforms are not valid with a shuffle. |
4593 | 17 | if (PeekedThroughSelectShuffle) |
4594 | 0 | return nullptr; |
4595 | | |
4596 | | // (X == Y) ? X : max/min(X, Y) --> max/min(X, Y) |
4597 | 17 | if (Pred == CmpInst::ICMP_EQ) |
4598 | 10 | return MMI; |
4599 | | |
4600 | | // (X != Y) ? X : max/min(X, Y) --> X |
4601 | 7 | if (Pred == CmpInst::ICMP_NE) |
4602 | 0 | return X; |
4603 | | |
4604 | | // (X < Y) ? X : max(X, Y) --> X |
4605 | | // (X <= Y) ? X : max(X, Y) --> X |
4606 | | // (X > Y) ? X : min(X, Y) --> X |
4607 | | // (X >= Y) ? X : min(X, Y) --> X |
4608 | 7 | ICmpInst::Predicate InvPred = CmpInst::getInversePredicate(Pred); |
4609 | 7 | if (MMPred == CmpInst::getStrictPredicate(InvPred)) |
4610 | 7 | return X; |
4611 | | |
4612 | 0 | return nullptr; |
4613 | 7 | } |
4614 | | |
4615 | | /// An alternative way to test if a bit is set or not. |
4616 | | /// uses e.g. sgt/slt or trunc instead of eq/ne. |
4617 | | static Value *simplifySelectWithBitTest(Value *CondVal, Value *TrueVal, |
4618 | 37.3M | Value *FalseVal) { |
4619 | 37.3M | if (auto Res = decomposeBitTest(CondVal)) |
4620 | 5.67M | return simplifySelectBitTest(TrueVal, FalseVal, Res->X, &Res->Mask, |
4621 | 5.67M | Res->Pred == ICmpInst::ICMP_EQ); |
4622 | | |
4623 | 31.6M | return nullptr; |
4624 | 37.3M | } |
4625 | | |
4626 | | /// Try to simplify a select instruction when its condition operand is an |
4627 | | /// integer equality or floating-point equivalence comparison. |
4628 | | static Value *simplifySelectWithEquivalence( |
4629 | | ArrayRef<std::pair<Value *, Value *>> Replacements, Value *TrueVal, |
4630 | 26.5M | Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse) { |
4631 | 26.5M | Value *SimplifiedFalseVal = |
4632 | 26.5M | simplifyWithOpsReplaced(FalseVal, Replacements, Q.getWithoutUndef(), |
4633 | 26.5M | /* AllowRefinement */ false, |
4634 | 26.5M | /* DropFlags */ nullptr, MaxRecurse); |
4635 | 26.5M | if (!SimplifiedFalseVal) |
4636 | 24.3M | SimplifiedFalseVal = FalseVal; |
4637 | | |
4638 | 26.5M | Value *SimplifiedTrueVal = |
4639 | 26.5M | simplifyWithOpsReplaced(TrueVal, Replacements, Q, |
4640 | 26.5M | /* AllowRefinement */ true, |
4641 | 26.5M | /* DropFlags */ nullptr, MaxRecurse); |
4642 | 26.5M | if (!SimplifiedTrueVal) |
4643 | 26.5M | SimplifiedTrueVal = TrueVal; |
4644 | | |
4645 | 26.5M | if (SimplifiedFalseVal == SimplifiedTrueVal) |
4646 | 103k | return FalseVal; |
4647 | | |
4648 | 26.4M | return nullptr; |
4649 | 26.5M | } |
4650 | | |
4651 | | /// Try to simplify a select instruction when its condition operand is an |
4652 | | /// integer comparison. |
4653 | | static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, |
4654 | | Value *FalseVal, |
4655 | | const SimplifyQuery &Q, |
4656 | 37.4M | unsigned MaxRecurse) { |
4657 | 37.4M | CmpPredicate Pred; |
4658 | 37.4M | Value *CmpLHS, *CmpRHS; |
4659 | 37.4M | if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) |
4660 | 8.68M | return nullptr; |
4661 | | |
4662 | 28.7M | if (Value *V = simplifyCmpSelOfMaxMin(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal)) |
4663 | 27 | return V; |
4664 | | |
4665 | | // Canonicalize ne to eq predicate. |
4666 | 28.7M | if (Pred == ICmpInst::ICMP_NE) { |
4667 | 2.15M | Pred = ICmpInst::ICMP_EQ; |
4668 | 2.15M | std::swap(TrueVal, FalseVal); |
4669 | 2.15M | } |
4670 | | |
4671 | | // Check for integer min/max with a limit constant: |
4672 | | // X > MIN_INT ? X : MIN_INT --> X |
4673 | | // X < MAX_INT ? X : MAX_INT --> X |
4674 | 28.7M | if (TrueVal->getType()->isIntOrIntVectorTy()) { |
4675 | 20.6M | Value *X, *Y; |
4676 | 20.6M | SelectPatternFlavor SPF = |
4677 | 20.6M | matchDecomposedSelectPattern(cast<ICmpInst>(CondVal), TrueVal, FalseVal, |
4678 | 20.6M | X, Y) |
4679 | 20.6M | .Flavor; |
4680 | 20.6M | if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)379k ) { |
4681 | 161k | APInt LimitC = getMinMaxLimit(getInverseMinMaxFlavor(SPF), |
4682 | 161k | X->getType()->getScalarSizeInBits()); |
4683 | 161k | if (match(Y, m_SpecificInt(LimitC))) |
4684 | 144 | return X; |
4685 | 161k | } |
4686 | 20.6M | } |
4687 | | |
4688 | 28.7M | if (Pred == ICmpInst::ICMP_EQ && match(CmpRHS, m_Zero())16.5M ) { |
4689 | 8.75M | Value *X; |
4690 | 8.75M | const APInt *Y; |
4691 | 8.75M | if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y)))) |
4692 | 1.82M | if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y, |
4693 | 1.82M | /*TrueWhenUnset=*/true)) |
4694 | 71 | return V; |
4695 | | |
4696 | | // Test for a bogus zero-shift-guard-op around funnel-shift or rotate. |
4697 | 8.75M | Value *ShAmt; |
4698 | 8.75M | auto isFsh = m_CombineOr(m_FShl(m_Value(X), m_Value(), m_Value(ShAmt)), |
4699 | 8.75M | m_FShr(m_Value(), m_Value(X), m_Value(ShAmt))); |
4700 | | // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X |
4701 | | // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X |
4702 | 8.75M | if (match(TrueVal, isFsh) && FalseVal == X28 && CmpLHS == ShAmt24 ) |
4703 | 0 | return X; |
4704 | | |
4705 | | // Test for a zero-shift-guard-op around rotates. These are used to |
4706 | | // avoid UB from oversized shifts in raw IR rotate patterns, but the |
4707 | | // intrinsics do not have that problem. |
4708 | | // We do not allow this transform for the general funnel shift case because |
4709 | | // that would not preserve the poison safety of the original code. |
4710 | 8.75M | auto isRotate = |
4711 | 8.75M | m_CombineOr(m_FShl(m_Value(X), m_Deferred(X), m_Value(ShAmt)), |
4712 | 8.75M | m_FShr(m_Value(X), m_Deferred(X), m_Value(ShAmt))); |
4713 | | // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt) |
4714 | | // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt) |
4715 | 8.75M | if (match(FalseVal, isRotate) && TrueVal == X797 && CmpLHS == ShAmt795 && |
4716 | 8.75M | Pred == ICmpInst::ICMP_EQ0 ) |
4717 | 0 | return FalseVal; |
4718 | | |
4719 | | // X == 0 ? abs(X) : -abs(X) --> -abs(X) |
4720 | | // X == 0 ? -abs(X) : abs(X) --> abs(X) |
4721 | 8.75M | if (match(TrueVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))) && |
4722 | 8.75M | match(FalseVal, m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))))0 ) |
4723 | 0 | return FalseVal; |
4724 | 8.75M | if (match(TrueVal, |
4725 | 8.75M | m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))) && |
4726 | 8.75M | match(FalseVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))0 ) |
4727 | 0 | return FalseVal; |
4728 | 8.75M | } |
4729 | | |
4730 | | // If we have a scalar equality comparison, then we know the value in one of |
4731 | | // the arms of the select. See if substituting this value into the arm and |
4732 | | // simplifying the result yields the same value as the other arm. |
4733 | 28.7M | if (Pred == ICmpInst::ICMP_EQ) { |
4734 | 16.5M | if (CmpLHS->getType()->isIntOrIntVectorTy() || |
4735 | 16.5M | canReplacePointersIfEqual(CmpLHS, CmpRHS, Q.DL)5.21M ) |
4736 | 14.9M | if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal, |
4737 | 14.9M | FalseVal, Q, MaxRecurse)) |
4738 | 103k | return V; |
4739 | 16.4M | if (CmpLHS->getType()->isIntOrIntVectorTy() || |
4740 | 16.4M | canReplacePointersIfEqual(CmpRHS, CmpLHS, Q.DL)5.15M ) |
4741 | 11.4M | if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal, |
4742 | 11.4M | FalseVal, Q, MaxRecurse)) |
4743 | 31 | return V; |
4744 | | |
4745 | 16.4M | Value *X; |
4746 | 16.4M | Value *Y; |
4747 | | // select((X | Y) == 0 ? X : 0) --> 0 (commuted 2 ways) |
4748 | 16.4M | if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) && |
4749 | 16.4M | match(CmpRHS, m_Zero())54.5k ) { |
4750 | | // (X | Y) == 0 implies X == 0 and Y == 0. |
4751 | 38.5k | if (Value *V = simplifySelectWithEquivalence( |
4752 | 38.5k | {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse)) |
4753 | 6 | return V; |
4754 | 38.5k | } |
4755 | | |
4756 | | // select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways) |
4757 | 16.4M | if (match(CmpLHS, m_And(m_Value(X), m_Value(Y))) && |
4758 | 16.4M | match(CmpRHS, m_AllOnes())2.54M ) { |
4759 | | // (X & Y) == -1 implies X == -1 and Y == -1. |
4760 | 432 | if (Value *V = simplifySelectWithEquivalence( |
4761 | 432 | {{X, CmpRHS}, {Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse)) |
4762 | 0 | return V; |
4763 | 432 | } |
4764 | 16.4M | } |
4765 | | |
4766 | 28.6M | return nullptr; |
4767 | 28.7M | } |
4768 | | |
4769 | | /// Try to simplify a select instruction when its condition operand is a |
4770 | | /// floating-point comparison. |
4771 | | static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F, |
4772 | | const SimplifyQuery &Q, |
4773 | 37.3M | unsigned MaxRecurse) { |
4774 | 37.3M | CmpPredicate Pred; |
4775 | 37.3M | Value *CmpLHS, *CmpRHS; |
4776 | 37.3M | if (!match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) |
4777 | 34.5M | return nullptr; |
4778 | 2.77M | FCmpInst *I = cast<FCmpInst>(Cond); |
4779 | | |
4780 | 2.77M | bool IsEquiv = I->isEquivalence(); |
4781 | 2.77M | if (I->isEquivalence(/*Invert=*/true)) { |
4782 | 4.91k | std::swap(T, F); |
4783 | 4.91k | Pred = FCmpInst::getInversePredicate(Pred); |
4784 | 4.91k | IsEquiv = true; |
4785 | 4.91k | } |
4786 | | |
4787 | | // This transforms is safe if at least one operand is known to not be zero. |
4788 | | // Otherwise, the select can change the sign of a zero operand. |
4789 | 2.77M | if (IsEquiv) { |
4790 | 42.9k | if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, T, F, Q, |
4791 | 42.9k | MaxRecurse)) |
4792 | 16 | return V; |
4793 | 42.9k | if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, T, F, Q, |
4794 | 42.9k | MaxRecurse)) |
4795 | 0 | return V; |
4796 | 42.9k | } |
4797 | | |
4798 | | // Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped. |
4799 | 2.77M | if (CmpLHS == F && CmpRHS == T800k ) |
4800 | 751k | std::swap(CmpLHS, CmpRHS); |
4801 | | |
4802 | 2.77M | if (CmpLHS != T || CmpRHS != F1.57M ) |
4803 | 1.42M | return nullptr; |
4804 | | |
4805 | | // This transform is also safe if we do not have (do not care about) -0.0. |
4806 | 1.35M | if (Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros()1.35M ) { |
4807 | | // (T == F) ? T : F --> F |
4808 | 73.5k | if (Pred == FCmpInst::FCMP_OEQ) |
4809 | 0 | return F; |
4810 | | |
4811 | | // (T != F) ? T : F --> T |
4812 | 73.5k | if (Pred == FCmpInst::FCMP_UNE) |
4813 | 0 | return T; |
4814 | 73.5k | } |
4815 | | |
4816 | 1.35M | return nullptr; |
4817 | 1.35M | } |
4818 | | |
4819 | | /// Given operands for a SelectInst, see if we can fold the result. |
4820 | | /// If not, this returns null. |
4821 | | static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
4822 | 38.1M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
4823 | 38.1M | if (auto *CondC = dyn_cast<Constant>(Cond)) { |
4824 | 476k | if (auto *TrueC = dyn_cast<Constant>(TrueVal)) |
4825 | 306k | if (auto *FalseC = dyn_cast<Constant>(FalseVal)) |
4826 | 191k | if (Constant *C = ConstantFoldSelectInstruction(CondC, TrueC, FalseC)) |
4827 | 191k | return C; |
4828 | | |
4829 | | // select poison, X, Y -> poison |
4830 | 284k | if (isa<PoisonValue>(CondC)) |
4831 | 21 | return PoisonValue::get(TrueVal->getType()); |
4832 | | |
4833 | | // select undef, X, Y -> X or Y |
4834 | 284k | if (Q.isUndefValue(CondC)) |
4835 | 100 | return isa<Constant>(FalseVal) ? FalseVal6 : TrueVal94 ; |
4836 | | |
4837 | | // select true, X, Y --> X |
4838 | | // select false, X, Y --> Y |
4839 | | // For vectors, allow undef/poison elements in the condition to match the |
4840 | | // defined elements, so we can eliminate the select. |
4841 | 284k | if (match(CondC, m_One())) |
4842 | 120k | return TrueVal; |
4843 | 164k | if (match(CondC, m_Zero())) |
4844 | 162k | return FalseVal; |
4845 | 164k | } |
4846 | | |
4847 | 38.1M | assert(Cond->getType()->isIntOrIntVectorTy(1) && |
4848 | 37.6M | "Select must have bool or bool vector condition"); |
4849 | 37.6M | assert(TrueVal->getType() == FalseVal->getType() && |
4850 | 37.6M | "Select must have same types for true/false ops"); |
4851 | | |
4852 | 37.6M | if (Cond->getType() == TrueVal->getType()) { |
4853 | | // select i1 Cond, i1 true, i1 false --> i1 Cond |
4854 | 9.31M | if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt())3.77M ) |
4855 | 140k | return Cond; |
4856 | | |
4857 | | // (X && Y) ? X : Y --> Y (commuted 2 ways) |
4858 | 9.17M | if (match(Cond, m_c_LogicalAnd(m_Specific(TrueVal), m_Specific(FalseVal)))) |
4859 | 0 | return FalseVal; |
4860 | | |
4861 | | // (X || Y) ? X : Y --> X (commuted 2 ways) |
4862 | 9.17M | if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal)))) |
4863 | 0 | return TrueVal; |
4864 | | |
4865 | | // (X || Y) ? false : X --> false (commuted 2 ways) |
4866 | 9.17M | if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) && |
4867 | 9.17M | match(TrueVal, m_ZeroInt())43 ) |
4868 | 1 | return ConstantInt::getFalse(Cond->getType()); |
4869 | | |
4870 | | // Match patterns that end in logical-and. |
4871 | 9.17M | if (match(FalseVal, m_ZeroInt())) { |
4872 | | // !(X || Y) && X --> false (commuted 2 ways) |
4873 | 5.21M | if (match(Cond, m_Not(m_c_LogicalOr(m_Specific(TrueVal), m_Value())))) |
4874 | 0 | return ConstantInt::getFalse(Cond->getType()); |
4875 | | // X && !(X || Y) --> false (commuted 2 ways) |
4876 | 5.21M | if (match(TrueVal, m_Not(m_c_LogicalOr(m_Specific(Cond), m_Value())))) |
4877 | 0 | return ConstantInt::getFalse(Cond->getType()); |
4878 | | |
4879 | | // (X || Y) && Y --> Y (commuted 2 ways) |
4880 | 5.21M | if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Value()))) |
4881 | 78 | return TrueVal; |
4882 | | // Y && (X || Y) --> Y (commuted 2 ways) |
4883 | 5.21M | if (match(TrueVal, m_c_LogicalOr(m_Specific(Cond), m_Value()))) |
4884 | 5 | return Cond; |
4885 | | |
4886 | | // (X || Y) && (X || !Y) --> X (commuted 8 ways) |
4887 | 5.21M | Value *X, *Y; |
4888 | 5.21M | if (match(Cond, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) && |
4889 | 5.21M | match(TrueVal, m_c_LogicalOr(m_Specific(X), m_Specific(Y)))2.83k ) |
4890 | 6 | return X; |
4891 | 5.21M | if (match(TrueVal, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) && |
4892 | 5.21M | match(Cond, m_c_LogicalOr(m_Specific(X), m_Specific(Y)))727 ) |
4893 | 0 | return X; |
4894 | 5.21M | } |
4895 | | |
4896 | | // Match patterns that end in logical-or. |
4897 | 9.17M | if (match(TrueVal, m_One())) { |
4898 | | // !(X && Y) || X --> true (commuted 2 ways) |
4899 | 3.62M | if (match(Cond, m_Not(m_c_LogicalAnd(m_Specific(FalseVal), m_Value())))) |
4900 | 4 | return ConstantInt::getTrue(Cond->getType()); |
4901 | | // X || !(X && Y) --> true (commuted 2 ways) |
4902 | 3.62M | if (match(FalseVal, m_Not(m_c_LogicalAnd(m_Specific(Cond), m_Value())))) |
4903 | 0 | return ConstantInt::getTrue(Cond->getType()); |
4904 | | |
4905 | | // (X && Y) || Y --> Y (commuted 2 ways) |
4906 | 3.62M | if (match(Cond, m_c_LogicalAnd(m_Specific(FalseVal), m_Value()))) |
4907 | 68 | return FalseVal; |
4908 | | // Y || (X && Y) --> Y (commuted 2 ways) |
4909 | 3.62M | if (match(FalseVal, m_c_LogicalAnd(m_Specific(Cond), m_Value()))) |
4910 | 5 | return Cond; |
4911 | 3.62M | } |
4912 | 9.17M | } |
4913 | | |
4914 | | // select ?, X, X -> X |
4915 | 37.4M | if (TrueVal == FalseVal) |
4916 | 42.8k | return TrueVal; |
4917 | | |
4918 | 37.4M | if (Cond == TrueVal) { |
4919 | | // select i1 X, i1 X, i1 false --> X (logical-and) |
4920 | 2.39k | if (match(FalseVal, m_ZeroInt())) |
4921 | 2.35k | return Cond; |
4922 | | // select i1 X, i1 X, i1 true --> true |
4923 | 42 | if (match(FalseVal, m_One())) |
4924 | 8 | return ConstantInt::getTrue(Cond->getType()); |
4925 | 42 | } |
4926 | 37.4M | if (Cond == FalseVal) { |
4927 | | // select i1 X, i1 true, i1 X --> X (logical-or) |
4928 | 1.36k | if (match(TrueVal, m_One())) |
4929 | 679 | return Cond; |
4930 | | // select i1 X, i1 false, i1 X --> false |
4931 | 685 | if (match(TrueVal, m_ZeroInt())) |
4932 | 1 | return ConstantInt::getFalse(Cond->getType()); |
4933 | 685 | } |
4934 | | |
4935 | | // If the true or false value is poison, we can fold to the other value. |
4936 | | // If the true or false value is undef, we can fold to the other value as |
4937 | | // long as the other value isn't poison. |
4938 | | // select ?, poison, X -> X |
4939 | | // select ?, undef, X -> X |
4940 | 37.4M | if (isa<PoisonValue>(TrueVal) || |
4941 | 37.4M | (37.4M Q.isUndefValue(TrueVal)37.4M && impliesPoison(FalseVal, Cond)383k )) |
4942 | 14.7k | return FalseVal; |
4943 | | // select ?, X, poison -> X |
4944 | | // select ?, X, undef -> X |
4945 | 37.4M | if (isa<PoisonValue>(FalseVal) || |
4946 | 37.4M | (37.4M Q.isUndefValue(FalseVal)37.4M && impliesPoison(TrueVal, Cond)391k )) |
4947 | 7.13k | return TrueVal; |
4948 | | |
4949 | | // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC'' |
4950 | 37.4M | Constant *TrueC, *FalseC; |
4951 | 37.4M | if (isa<FixedVectorType>(TrueVal->getType()) && |
4952 | 37.4M | match(TrueVal, m_Constant(TrueC))238k && |
4953 | 37.4M | match(FalseVal, m_Constant(FalseC))71.4k ) { |
4954 | 43.1k | unsigned NumElts = |
4955 | 43.1k | cast<FixedVectorType>(TrueC->getType())->getNumElements(); |
4956 | 43.1k | SmallVector<Constant *, 16> NewC; |
4957 | 43.1k | for (unsigned i = 0; i != NumElts; ++i24 ) { |
4958 | | // Bail out on incomplete vector constants. |
4959 | 43.1k | Constant *TEltC = TrueC->getAggregateElement(i); |
4960 | 43.1k | Constant *FEltC = FalseC->getAggregateElement(i); |
4961 | 43.1k | if (!TEltC || !FEltC) |
4962 | 0 | break; |
4963 | | |
4964 | | // If the elements match (undef or not), that value is the result. If only |
4965 | | // one element is undef, choose the defined element as the safe result. |
4966 | 43.1k | if (TEltC == FEltC) |
4967 | 24 | NewC.push_back(TEltC); |
4968 | 43.1k | else if (isa<PoisonValue>(TEltC) || |
4969 | 43.1k | (Q.isUndefValue(TEltC) && isGuaranteedNotToBePoison(FEltC)0 )) |
4970 | 0 | NewC.push_back(FEltC); |
4971 | 43.1k | else if (isa<PoisonValue>(FEltC) || |
4972 | 43.1k | (Q.isUndefValue(FEltC) && isGuaranteedNotToBePoison(TEltC)0 )) |
4973 | 0 | NewC.push_back(TEltC); |
4974 | 43.1k | else |
4975 | 43.1k | break; |
4976 | 43.1k | } |
4977 | 43.1k | if (NewC.size() == NumElts) |
4978 | 0 | return ConstantVector::get(NewC); |
4979 | 43.1k | } |
4980 | | |
4981 | 37.4M | if (Value *V = |
4982 | 37.4M | simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse)) |
4983 | 103k | return V; |
4984 | | |
4985 | 37.3M | if (Value *V = simplifySelectWithBitTest(Cond, TrueVal, FalseVal)) |
4986 | 55 | return V; |
4987 | | |
4988 | 37.3M | if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q, MaxRecurse)) |
4989 | 16 | return V; |
4990 | | |
4991 | 37.3M | std::optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL); |
4992 | 37.3M | if (Imp) |
4993 | 20.9k | return *Imp ? TrueVal7.61k : FalseVal13.3k ; |
4994 | | |
4995 | 37.3M | return nullptr; |
4996 | 37.3M | } |
4997 | | |
4998 | | Value *llvm::simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, |
4999 | 19.9M | const SimplifyQuery &Q) { |
5000 | 19.9M | return ::simplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit); |
5001 | 19.9M | } |
5002 | | |
5003 | | /// Given operands for an GetElementPtrInst, see if we can fold the result. |
5004 | | /// If not, this returns null. |
5005 | | static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr, |
5006 | | ArrayRef<Value *> Indices, GEPNoWrapFlags NW, |
5007 | 805M | const SimplifyQuery &Q, unsigned) { |
5008 | | // The type of the GEP pointer operand. |
5009 | 805M | unsigned AS = |
5010 | 805M | cast<PointerType>(Ptr->getType()->getScalarType())->getAddressSpace(); |
5011 | | |
5012 | | // getelementptr P -> P. |
5013 | 805M | if (Indices.empty()) |
5014 | 0 | return Ptr; |
5015 | | |
5016 | | // Compute the (pointer) type returned by the GEP instruction. |
5017 | 805M | Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Indices); |
5018 | 805M | Type *GEPTy = Ptr->getType(); |
5019 | 805M | if (!GEPTy->isVectorTy()) { |
5020 | 875M | for (Value *Op : Indices) { |
5021 | | // If one of the operands is a vector, the result type is a vector of |
5022 | | // pointers. All vector operands must have the same number of elements. |
5023 | 875M | if (VectorType *VT = dyn_cast<VectorType>(Op->getType())) { |
5024 | 83 | GEPTy = VectorType::get(GEPTy, VT->getElementCount()); |
5025 | 83 | break; |
5026 | 83 | } |
5027 | 875M | } |
5028 | 805M | } |
5029 | | |
5030 | | // All-zero GEP is a no-op, unless it performs a vector splat. |
5031 | 805M | if (Ptr->getType() == GEPTy && |
5032 | 872M | all_of(Indices, [](const auto *V) 805M { return match(V, m_Zero()); })) |
5033 | 15.8M | return Ptr; |
5034 | | |
5035 | | // getelementptr poison, idx -> poison |
5036 | | // getelementptr baseptr, poison -> poison |
5037 | 789M | if (isa<PoisonValue>(Ptr) || |
5038 | 845M | any_of(Indices, [](const auto *V) 789M { return isa<PoisonValue>(V); })) |
5039 | 6.59k | return PoisonValue::get(GEPTy); |
5040 | | |
5041 | | // getelementptr undef, idx -> undef |
5042 | 789M | if (Q.isUndefValue(Ptr)) |
5043 | 2.53k | return UndefValue::get(GEPTy); |
5044 | | |
5045 | 789M | bool IsScalableVec = |
5046 | 845M | SrcTy->isScalableTy()789M || any_of(Indices, [](const Value *V) 789M { |
5047 | 845M | return isa<ScalableVectorType>(V->getType()); |
5048 | 845M | }); |
5049 | | |
5050 | 789M | if (Indices.size() == 1) { |
5051 | 733M | Type *Ty = SrcTy; |
5052 | 733M | if (!IsScalableVec && Ty->isSized()) { |
5053 | 733M | Value *P; |
5054 | 733M | uint64_t C; |
5055 | 733M | uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty); |
5056 | | // getelementptr P, N -> P if P points to a type of zero size. |
5057 | 733M | if (TyAllocSize == 0 && Ptr->getType() == GEPTy0 ) |
5058 | 0 | return Ptr; |
5059 | | |
5060 | | // The following transforms are only safe if the ptrtoint cast |
5061 | | // doesn't truncate the pointers. |
5062 | 733M | if (Indices[0]->getType()->getScalarSizeInBits() == |
5063 | 733M | Q.DL.getPointerSizeInBits(AS)) { |
5064 | 732M | auto CanSimplify = [GEPTy, &P, Ptr]() -> bool { |
5065 | 568k | return P->getType() == GEPTy && |
5066 | 568k | getUnderlyingObject(P) == getUnderlyingObject(Ptr); |
5067 | 568k | }; |
5068 | | // getelementptr V, (sub P, V) -> P if P points to a type of size 1. |
5069 | 732M | if (TyAllocSize == 1 && |
5070 | 732M | match(Indices[0], |
5071 | 676M | m_Sub(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Specific(Ptr)))) && |
5072 | 732M | CanSimplify()535k ) |
5073 | 6.03k | return P; |
5074 | | |
5075 | | // getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of |
5076 | | // size 1 << C. |
5077 | 732M | if (match(Indices[0], m_AShr(m_Sub(m_PtrToInt(m_Value(P)), |
5078 | 732M | m_PtrToInt(m_Specific(Ptr))), |
5079 | 732M | m_ConstantInt(C))) && |
5080 | 732M | TyAllocSize == 1ULL << C26.7k && CanSimplify()24.0k ) |
5081 | 5 | return P; |
5082 | | |
5083 | | // getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of |
5084 | | // size C. |
5085 | 732M | if (match(Indices[0], m_SDiv(m_Sub(m_PtrToInt(m_Value(P)), |
5086 | 732M | m_PtrToInt(m_Specific(Ptr))), |
5087 | 732M | m_SpecificInt(TyAllocSize))) && |
5088 | 732M | CanSimplify()8.48k ) |
5089 | 0 | return P; |
5090 | 732M | } |
5091 | 733M | } |
5092 | 733M | } |
5093 | | |
5094 | 789M | if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 && |
5095 | 789M | all_of(Indices.drop_back(1), |
5096 | 683M | [](Value *Idx) { return match(Idx, m_Zero()); }6.71M )) { |
5097 | 683M | unsigned IdxWidth = |
5098 | 683M | Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace()); |
5099 | 683M | if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) { |
5100 | 680M | APInt BasePtrOffset(IdxWidth, 0); |
5101 | 680M | Value *StrippedBasePtr = |
5102 | 680M | Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.DL, BasePtrOffset); |
5103 | | |
5104 | | // Avoid creating inttoptr of zero here: While LLVMs treatment of |
5105 | | // inttoptr is generally conservative, this particular case is folded to |
5106 | | // a null pointer, which will have incorrect provenance. |
5107 | | |
5108 | | // gep (gep V, C), (sub 0, V) -> C |
5109 | 680M | if (match(Indices.back(), |
5110 | 680M | m_Neg(m_PtrToInt(m_Specific(StrippedBasePtr)))) && |
5111 | 680M | !BasePtrOffset.isZero()857 ) { |
5112 | 0 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset); |
5113 | 0 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
5114 | 0 | } |
5115 | | // gep (gep V, C), (xor V, -1) -> C-1 |
5116 | 680M | if (match(Indices.back(), |
5117 | 680M | m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes())) && |
5118 | 680M | !BasePtrOffset.isOne()34 ) { |
5119 | 34 | auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1); |
5120 | 34 | return ConstantExpr::getIntToPtr(CI, GEPTy); |
5121 | 34 | } |
5122 | 680M | } |
5123 | 683M | } |
5124 | | |
5125 | | // Check to see if this is constant foldable. |
5126 | 789M | if (!isa<Constant>(Ptr) || |
5127 | 789M | !all_of(Indices, [](Value *V) 7.04M { return isa<Constant>(V); }11.9M )) |
5128 | 788M | return nullptr; |
5129 | | |
5130 | 793k | if (!ConstantExpr::isSupportedGetElementPtr(SrcTy)) |
5131 | 0 | return ConstantFoldGetElementPtr(SrcTy, cast<Constant>(Ptr), std::nullopt, |
5132 | 0 | Indices); |
5133 | | |
5134 | 793k | auto *CE = |
5135 | 793k | ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices, NW); |
5136 | 793k | return ConstantFoldConstant(CE, Q.DL); |
5137 | 793k | } |
5138 | | |
5139 | | Value *llvm::simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices, |
5140 | 414M | GEPNoWrapFlags NW, const SimplifyQuery &Q) { |
5141 | 414M | return ::simplifyGEPInst(SrcTy, Ptr, Indices, NW, Q, RecursionLimit); |
5142 | 414M | } |
5143 | | |
5144 | | /// Given operands for an InsertValueInst, see if we can fold the result. |
5145 | | /// If not, this returns null. |
5146 | | static Value *simplifyInsertValueInst(Value *Agg, Value *Val, |
5147 | | ArrayRef<unsigned> Idxs, |
5148 | 15.4M | const SimplifyQuery &Q, unsigned) { |
5149 | 15.4M | if (Constant *CAgg = dyn_cast<Constant>(Agg)) |
5150 | 7.94M | if (Constant *CVal = dyn_cast<Constant>(Val)) |
5151 | 154k | return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); |
5152 | | |
5153 | | // insertvalue x, poison, n -> x |
5154 | | // insertvalue x, undef, n -> x if x cannot be poison |
5155 | 15.3M | if (isa<PoisonValue>(Val) || |
5156 | 15.3M | (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Agg)9.21k )) |
5157 | 0 | return Agg; |
5158 | | |
5159 | | // insertvalue x, (extractvalue y, n), n |
5160 | 15.3M | if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val)) |
5161 | 4.20M | if (EV->getAggregateOperand()->getType() == Agg->getType() && |
5162 | 4.20M | EV->getIndices() == Idxs4.10M ) { |
5163 | | // insertvalue poison, (extractvalue y, n), n -> y |
5164 | | // insertvalue undef, (extractvalue y, n), n -> y if y cannot be poison |
5165 | 4.09M | if (isa<PoisonValue>(Agg) || |
5166 | 4.09M | (2.05M Q.isUndefValue(Agg)2.05M && |
5167 | 2.05M | isGuaranteedNotToBePoison(EV->getAggregateOperand())0 )) |
5168 | 2.04M | return EV->getAggregateOperand(); |
5169 | | |
5170 | | // insertvalue y, (extractvalue y, n), n -> y |
5171 | 2.05M | if (Agg == EV->getAggregateOperand()) |
5172 | 2.03M | return Agg; |
5173 | 2.05M | } |
5174 | | |
5175 | 11.2M | return nullptr; |
5176 | 15.3M | } |
5177 | | |
5178 | | Value *llvm::simplifyInsertValueInst(Value *Agg, Value *Val, |
5179 | | ArrayRef<unsigned> Idxs, |
5180 | 4.70M | const SimplifyQuery &Q) { |
5181 | 4.70M | return ::simplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit); |
5182 | 4.70M | } |
5183 | | |
5184 | | Value *llvm::simplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx, |
5185 | 2.61M | const SimplifyQuery &Q) { |
5186 | | // Try to constant fold. |
5187 | 2.61M | auto *VecC = dyn_cast<Constant>(Vec); |
5188 | 2.61M | auto *ValC = dyn_cast<Constant>(Val); |
5189 | 2.61M | auto *IdxC = dyn_cast<Constant>(Idx); |
5190 | 2.61M | if (VecC && ValC1.74M && IdxC48.3k ) |
5191 | 48.3k | return ConstantExpr::getInsertElement(VecC, ValC, IdxC); |
5192 | | |
5193 | | // For fixed-length vector, fold into poison if index is out of bounds. |
5194 | 2.56M | if (auto *CI = dyn_cast<ConstantInt>(Idx)) { |
5195 | 2.56M | if (isa<FixedVectorType>(Vec->getType()) && |
5196 | 2.56M | CI->uge(cast<FixedVectorType>(Vec->getType())->getNumElements())) |
5197 | 0 | return PoisonValue::get(Vec->getType()); |
5198 | 2.56M | } |
5199 | | |
5200 | | // If index is undef, it might be out of bounds (see above case) |
5201 | 2.56M | if (Q.isUndefValue(Idx)) |
5202 | 0 | return PoisonValue::get(Vec->getType()); |
5203 | | |
5204 | | // If the scalar is poison, or it is undef and there is no risk of |
5205 | | // propagating poison from the vector value, simplify to the vector value. |
5206 | 2.56M | if (isa<PoisonValue>(Val) || |
5207 | 2.56M | (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec)1.16k )) |
5208 | 0 | return Vec; |
5209 | | |
5210 | | // Inserting the splatted value into a constant splat does nothing. |
5211 | 2.56M | if (VecC && ValC1.70M && VecC->getSplatValue() == ValC0 ) |
5212 | 0 | return Vec; |
5213 | | |
5214 | | // If we are extracting a value from a vector, then inserting it into the same |
5215 | | // place, that's the input vector: |
5216 | | // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec |
5217 | 2.56M | if (match(Val, m_ExtractElt(m_Specific(Vec), m_Specific(Idx)))) |
5218 | 16 | return Vec; |
5219 | | |
5220 | 2.56M | return nullptr; |
5221 | 2.56M | } |
5222 | | |
5223 | | /// Given operands for an ExtractValueInst, see if we can fold the result. |
5224 | | /// If not, this returns null. |
5225 | | static Value *simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
5226 | 60.1M | const SimplifyQuery &, unsigned) { |
5227 | 60.1M | if (auto *CAgg = dyn_cast<Constant>(Agg)) |
5228 | 104k | return ConstantFoldExtractValueInstruction(CAgg, Idxs); |
5229 | | |
5230 | | // extractvalue x, (insertvalue y, elt, n), n -> elt |
5231 | 60.0M | unsigned NumIdxs = Idxs.size(); |
5232 | 60.8M | for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr; |
5233 | 60.0M | IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())753k ) { |
5234 | 2.12M | ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices(); |
5235 | 2.12M | unsigned NumInsertValueIdxs = InsertValueIdxs.size(); |
5236 | 2.12M | unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs); |
5237 | 2.12M | if (InsertValueIdxs.slice(0, NumCommonIdxs) == |
5238 | 2.12M | Idxs.slice(0, NumCommonIdxs)) { |
5239 | 1.36M | if (NumIdxs == NumInsertValueIdxs) |
5240 | 1.36M | return IVI->getInsertedValueOperand(); |
5241 | 0 | break; |
5242 | 1.36M | } |
5243 | 2.12M | } |
5244 | | |
5245 | 58.7M | return nullptr; |
5246 | 60.0M | } |
5247 | | |
5248 | | Value *llvm::simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs, |
5249 | 29.1M | const SimplifyQuery &Q) { |
5250 | 29.1M | return ::simplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit); |
5251 | 29.1M | } |
5252 | | |
5253 | | /// Given operands for an ExtractElementInst, see if we can fold the result. |
5254 | | /// If not, this returns null. |
5255 | | static Value *simplifyExtractElementInst(Value *Vec, Value *Idx, |
5256 | 1.45M | const SimplifyQuery &Q, unsigned) { |
5257 | 1.45M | auto *VecVTy = cast<VectorType>(Vec->getType()); |
5258 | 1.45M | if (auto *CVec = dyn_cast<Constant>(Vec)) { |
5259 | 2.04k | if (auto *CIdx = dyn_cast<Constant>(Idx)) |
5260 | 2.04k | return ConstantExpr::getExtractElement(CVec, CIdx); |
5261 | | |
5262 | 0 | if (Q.isUndefValue(Vec)) |
5263 | 0 | return UndefValue::get(VecVTy->getElementType()); |
5264 | 0 | } |
5265 | | |
5266 | | // An undef extract index can be arbitrarily chosen to be an out-of-range |
5267 | | // index value, which would result in the instruction being poison. |
5268 | 1.45M | if (Q.isUndefValue(Idx)) |
5269 | 0 | return PoisonValue::get(VecVTy->getElementType()); |
5270 | | |
5271 | | // If extracting a specified index from the vector, see if we can recursively |
5272 | | // find a previously computed scalar that was inserted into the vector. |
5273 | 1.45M | if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) { |
5274 | | // For fixed-length vector, fold into undef if index is out of bounds. |
5275 | 1.45M | unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue(); |
5276 | 1.45M | if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts)) |
5277 | 0 | return PoisonValue::get(VecVTy->getElementType()); |
5278 | | // Handle case where an element is extracted from a splat. |
5279 | 1.45M | if (IdxC->getValue().ult(MinNumElts)) |
5280 | 1.45M | if (auto *Splat = getSplatValue(Vec)) |
5281 | 75 | return Splat; |
5282 | 1.45M | if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue())) |
5283 | 47.4k | return Elt; |
5284 | 1.45M | } else { |
5285 | | // extractelt x, (insertelt y, elt, n), n -> elt |
5286 | | // If the possibly-variable indices are trivially known to be equal |
5287 | | // (because they are the same operand) then use the value that was |
5288 | | // inserted directly. |
5289 | 425 | auto *IE = dyn_cast<InsertElementInst>(Vec); |
5290 | 425 | if (IE && IE->getOperand(2) == Idx9 ) |
5291 | 0 | return IE->getOperand(1); |
5292 | | |
5293 | | // The index is not relevant if our vector is a splat. |
5294 | 425 | if (Value *Splat = getSplatValue(Vec)) |
5295 | 0 | return Splat; |
5296 | 425 | } |
5297 | 1.40M | return nullptr; |
5298 | 1.45M | } |
5299 | | |
5300 | | Value *llvm::simplifyExtractElementInst(Value *Vec, Value *Idx, |
5301 | 690k | const SimplifyQuery &Q) { |
5302 | 690k | return ::simplifyExtractElementInst(Vec, Idx, Q, RecursionLimit); |
5303 | 690k | } |
5304 | | |
5305 | | /// See if we can fold the given phi. If not, returns null. |
5306 | | static Value *simplifyPHINode(PHINode *PN, ArrayRef<Value *> IncomingValues, |
5307 | 424M | const SimplifyQuery &Q) { |
5308 | | // WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE |
5309 | | // here, because the PHI we may succeed simplifying to was not |
5310 | | // def-reachable from the original PHI! |
5311 | | |
5312 | | // If all of the PHI's incoming values are the same then replace the PHI node |
5313 | | // with the common value. |
5314 | 424M | Value *CommonValue = nullptr; |
5315 | 424M | bool HasPoisonInput = false; |
5316 | 424M | bool HasUndefInput = false; |
5317 | 856M | for (Value *Incoming : IncomingValues) { |
5318 | | // If the incoming value is the phi node itself, it can safely be skipped. |
5319 | 856M | if (Incoming == PN) |
5320 | 465k | continue; |
5321 | 855M | if (isa<PoisonValue>(Incoming)) { |
5322 | 674k | HasPoisonInput = true; |
5323 | 674k | continue; |
5324 | 674k | } |
5325 | 855M | if (Q.isUndefValue(Incoming)) { |
5326 | | // Remember that we saw an undef value, but otherwise ignore them. |
5327 | 7.58M | HasUndefInput = true; |
5328 | 7.58M | continue; |
5329 | 7.58M | } |
5330 | 847M | if (CommonValue && Incoming != CommonValue423M ) |
5331 | 392M | return nullptr; // Not the same, bail out. |
5332 | 455M | CommonValue = Incoming; |
5333 | 455M | } |
5334 | | |
5335 | | // If CommonValue is null then all of the incoming values were either undef, |
5336 | | // poison or equal to the phi node itself. |
5337 | 31.8M | if (!CommonValue) |
5338 | 201k | return HasUndefInput ? UndefValue::get(PN->getType())183k |
5339 | 201k | : PoisonValue::get(PN->getType())18.6k ; |
5340 | | |
5341 | 31.6M | if (HasPoisonInput || HasUndefInput31.2M ) { |
5342 | | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
5343 | | // instruction, we cannot return X as the result of the PHI node unless it |
5344 | | // dominates the PHI block. |
5345 | 5.87M | if (!valueDominatesPHI(CommonValue, PN, Q.DT)) |
5346 | 5.20M | return nullptr; |
5347 | | |
5348 | | // Make sure we do not replace an undef value with poison. |
5349 | 663k | if (HasUndefInput && |
5350 | 663k | !isGuaranteedNotToBePoison(CommonValue, Q.AC, Q.CxtI, Q.DT)499k ) |
5351 | 308k | return nullptr; |
5352 | 354k | return CommonValue; |
5353 | 663k | } |
5354 | | |
5355 | 25.8M | return CommonValue; |
5356 | 31.6M | } |
5357 | | |
5358 | | static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, |
5359 | 105M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
5360 | 105M | if (auto *C = dyn_cast<Constant>(Op)) |
5361 | 3.64M | return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL); |
5362 | | |
5363 | 101M | if (auto *CI = dyn_cast<CastInst>(Op)) { |
5364 | 3.35M | auto *Src = CI->getOperand(0); |
5365 | 3.35M | Type *SrcTy = Src->getType(); |
5366 | 3.35M | Type *MidTy = CI->getType(); |
5367 | 3.35M | Type *DstTy = Ty; |
5368 | 3.35M | if (Src->getType() == Ty) { |
5369 | 1.85M | auto FirstOp = CI->getOpcode(); |
5370 | 1.85M | auto SecondOp = static_cast<Instruction::CastOps>(CastOpc); |
5371 | 1.85M | Type *SrcIntPtrTy = |
5372 | 1.85M | SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy)176k : nullptr1.67M ; |
5373 | 1.85M | Type *MidIntPtrTy = |
5374 | 1.85M | MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy)76.1k : nullptr1.77M ; |
5375 | 1.85M | Type *DstIntPtrTy = |
5376 | 1.85M | DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy)176k : nullptr1.67M ; |
5377 | 1.85M | if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy, |
5378 | 1.85M | SrcIntPtrTy, MidIntPtrTy, |
5379 | 1.85M | DstIntPtrTy) == Instruction::BitCast) |
5380 | 1.16M | return Src; |
5381 | 1.85M | } |
5382 | 3.35M | } |
5383 | | |
5384 | | // bitcast x -> x |
5385 | 100M | if (CastOpc == Instruction::BitCast) |
5386 | 2.16M | if (Op->getType() == Ty) |
5387 | 2 | return Op; |
5388 | | |
5389 | | // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X |
5390 | 100M | Value *Ptr, *X; |
5391 | 100M | if (CastOpc == Instruction::PtrToInt && |
5392 | 100M | match(Op, m_PtrAdd(m_Value(Ptr), |
5393 | 32.0M | m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr))))) && |
5394 | 100M | X->getType() == Ty3.98k && Ty == Q.DL.getIndexType(Ptr->getType())3.98k ) |
5395 | 3.98k | return X; |
5396 | | |
5397 | 100M | return nullptr; |
5398 | 100M | } |
5399 | | |
5400 | | Value *llvm::simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, |
5401 | 1.34M | const SimplifyQuery &Q) { |
5402 | 1.34M | return ::simplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit); |
5403 | 1.34M | } |
5404 | | |
5405 | | /// For the given destination element of a shuffle, peek through shuffles to |
5406 | | /// match a root vector source operand that contains that element in the same |
5407 | | /// vector lane (ie, the same mask index), so we can eliminate the shuffle(s). |
5408 | | static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1, |
5409 | | int MaskVal, Value *RootVec, |
5410 | 5.37M | unsigned MaxRecurse) { |
5411 | 5.37M | if (!MaxRecurse--) |
5412 | 27.9k | return nullptr; |
5413 | | |
5414 | | // Bail out if any mask value is undefined. That kind of shuffle may be |
5415 | | // simplified further based on demanded bits or other folds. |
5416 | 5.34M | if (MaskVal == -1) |
5417 | 1 | return nullptr; |
5418 | | |
5419 | | // The mask value chooses which source operand we need to look at next. |
5420 | 5.34M | int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements(); |
5421 | 5.34M | int RootElt = MaskVal; |
5422 | 5.34M | Value *SourceOp = Op0; |
5423 | 5.34M | if (MaskVal >= InVecNumElts) { |
5424 | 303k | RootElt = MaskVal - InVecNumElts; |
5425 | 303k | SourceOp = Op1; |
5426 | 303k | } |
5427 | | |
5428 | | // If the source operand is a shuffle itself, look through it to find the |
5429 | | // matching root vector. |
5430 | 5.34M | if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) { |
5431 | 628k | return foldIdentityShuffles( |
5432 | 628k | DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1), |
5433 | 628k | SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse); |
5434 | 628k | } |
5435 | | |
5436 | | // The source operand is not a shuffle. Initialize the root vector value for |
5437 | | // this shuffle if that has not been done yet. |
5438 | 4.71M | if (!RootVec) |
5439 | 2.79M | RootVec = SourceOp; |
5440 | | |
5441 | | // Give up as soon as a source operand does not match the existing root value. |
5442 | 4.71M | if (RootVec != SourceOp) |
5443 | 240k | return nullptr; |
5444 | | |
5445 | | // The element must be coming from the same lane in the source vector |
5446 | | // (although it may have crossed lanes in intermediate shuffles). |
5447 | 4.47M | if (RootElt != DestElt) |
5448 | 2.26M | return nullptr; |
5449 | | |
5450 | 2.20M | return RootVec; |
5451 | 4.47M | } |
5452 | | |
5453 | | static Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1, |
5454 | | ArrayRef<int> Mask, Type *RetTy, |
5455 | | const SimplifyQuery &Q, |
5456 | 3.13M | unsigned MaxRecurse) { |
5457 | 3.18M | if (all_of(Mask, [](int Elem) 3.13M { return Elem == PoisonMaskElem; })) |
5458 | 0 | return PoisonValue::get(RetTy); |
5459 | | |
5460 | 3.13M | auto *InVecTy = cast<VectorType>(Op0->getType()); |
5461 | 3.13M | unsigned MaskNumElts = Mask.size(); |
5462 | 3.13M | ElementCount InVecEltCount = InVecTy->getElementCount(); |
5463 | | |
5464 | 3.13M | bool Scalable = InVecEltCount.isScalable(); |
5465 | | |
5466 | 3.13M | SmallVector<int, 32> Indices; |
5467 | 3.13M | Indices.assign(Mask.begin(), Mask.end()); |
5468 | | |
5469 | | // Canonicalization: If mask does not select elements from an input vector, |
5470 | | // replace that input vector with poison. |
5471 | 3.13M | if (!Scalable) { |
5472 | 3.13M | bool MaskSelects0 = false, MaskSelects1 = false; |
5473 | 3.13M | unsigned InVecNumElts = InVecEltCount.getKnownMinValue(); |
5474 | 23.4M | for (unsigned i = 0; i != MaskNumElts; ++i20.3M ) { |
5475 | 20.3M | if (Indices[i] == -1) |
5476 | 938k | continue; |
5477 | 19.3M | if ((unsigned)Indices[i] < InVecNumElts) |
5478 | 16.1M | MaskSelects0 = true; |
5479 | 3.27M | else |
5480 | 3.27M | MaskSelects1 = true; |
5481 | 19.3M | } |
5482 | 3.13M | if (!MaskSelects0) |
5483 | 2.05k | Op0 = PoisonValue::get(InVecTy); |
5484 | 3.13M | if (!MaskSelects1) |
5485 | 2.33M | Op1 = PoisonValue::get(InVecTy); |
5486 | 3.13M | } |
5487 | | |
5488 | 3.13M | auto *Op0Const = dyn_cast<Constant>(Op0); |
5489 | 3.13M | auto *Op1Const = dyn_cast<Constant>(Op1); |
5490 | | |
5491 | | // If all operands are constant, constant fold the shuffle. This |
5492 | | // transformation depends on the value of the mask which is not known at |
5493 | | // compile time for scalable vectors |
5494 | 3.13M | if (Op0Const && Op1Const31.5k ) |
5495 | 20.9k | return ConstantExpr::getShuffleVector(Op0Const, Op1Const, Mask); |
5496 | | |
5497 | | // Canonicalization: if only one input vector is constant, it shall be the |
5498 | | // second one. This transformation depends on the value of the mask which |
5499 | | // is not known at compile time for scalable vectors |
5500 | 3.10M | if (!Scalable && Op0Const && !Op1Const10.5k ) { |
5501 | 10.5k | std::swap(Op0, Op1); |
5502 | 10.5k | ShuffleVectorInst::commuteShuffleMask(Indices, |
5503 | 10.5k | InVecEltCount.getKnownMinValue()); |
5504 | 10.5k | } |
5505 | | |
5506 | | // A splat of an inserted scalar constant becomes a vector constant: |
5507 | | // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...> |
5508 | | // NOTE: We may have commuted above, so analyze the updated Indices, not the |
5509 | | // original mask constant. |
5510 | | // NOTE: This transformation depends on the value of the mask which is not |
5511 | | // known at compile time for scalable vectors |
5512 | 3.10M | Constant *C; |
5513 | 3.10M | ConstantInt *IndexC; |
5514 | 3.10M | if (!Scalable && match(Op0, m_InsertElt(m_Value(), m_Constant(C), |
5515 | 3.10M | m_ConstantInt(IndexC)))) { |
5516 | | // Match a splat shuffle mask of the insert index allowing undef elements. |
5517 | 1.03k | int InsertIndex = IndexC->getZExtValue(); |
5518 | 1.63k | if (all_of(Indices, [InsertIndex](int MaskElt) 1.03k { |
5519 | 1.63k | return MaskElt == InsertIndex || MaskElt == -1924 ; |
5520 | 1.63k | })) { |
5521 | 116 | assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat"); |
5522 | | |
5523 | | // Shuffle mask poisons become poison constant result elements. |
5524 | 116 | SmallVector<Constant *, 16> VecC(MaskNumElts, C); |
5525 | 828 | for (unsigned i = 0; i != MaskNumElts; ++i712 ) |
5526 | 712 | if (Indices[i] == -1) |
5527 | 3 | VecC[i] = PoisonValue::get(C->getType()); |
5528 | 116 | return ConstantVector::get(VecC); |
5529 | 116 | } |
5530 | 1.03k | } |
5531 | | |
5532 | | // A shuffle of a splat is always the splat itself. Legal if the shuffle's |
5533 | | // value type is same as the input vectors' type. |
5534 | 3.10M | if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0)) |
5535 | 382k | if (Q.isUndefValue(Op1) && RetTy == InVecTy62.9k && |
5536 | 382k | all_equal(OpShuf->getShuffleMask())40.4k ) |
5537 | 14 | return Op0; |
5538 | | |
5539 | | // All remaining transformation depend on the value of the mask, which is |
5540 | | // not known at compile time for scalable vectors. |
5541 | 3.10M | if (Scalable) |
5542 | 0 | return nullptr; |
5543 | | |
5544 | | // Don't fold a shuffle with undef mask elements. This may get folded in a |
5545 | | // better way using demanded bits or other analysis. |
5546 | | // TODO: Should we allow this? |
5547 | 3.10M | if (is_contained(Indices, -1)) |
5548 | 286k | return nullptr; |
5549 | | |
5550 | | // Check if every element of this shuffle can be mapped back to the |
5551 | | // corresponding element of a single root vector. If so, we don't need this |
5552 | | // shuffle. This handles simple identity shuffles as well as chains of |
5553 | | // shuffles that may widen/narrow and/or move elements across lanes and back. |
5554 | 2.82M | Value *RootVec = nullptr; |
5555 | 4.74M | for (unsigned i = 0; i != MaskNumElts; ++i1.92M ) { |
5556 | | // Note that recursion is limited for each vector element, so if any element |
5557 | | // exceeds the limit, this will fail to simplify. |
5558 | 4.74M | RootVec = |
5559 | 4.74M | foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse); |
5560 | | |
5561 | | // We can't replace a widening/narrowing shuffle with one of its operands. |
5562 | 4.74M | if (!RootVec || RootVec->getType() != RetTy2.20M ) |
5563 | 2.82M | return nullptr; |
5564 | 4.74M | } |
5565 | 2.58k | return RootVec; |
5566 | 2.82M | } |
5567 | | |
5568 | | /// Given operands for a ShuffleVectorInst, fold the result or return null. |
5569 | | Value *llvm::simplifyShuffleVectorInst(Value *Op0, Value *Op1, |
5570 | | ArrayRef<int> Mask, Type *RetTy, |
5571 | 1.46M | const SimplifyQuery &Q) { |
5572 | 1.46M | return ::simplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit); |
5573 | 1.46M | } |
5574 | | |
5575 | | static Constant *foldConstant(Instruction::UnaryOps Opcode, Value *&Op, |
5576 | 1.35M | const SimplifyQuery &Q) { |
5577 | 1.35M | if (auto *C = dyn_cast<Constant>(Op)) |
5578 | 5.81k | return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL); |
5579 | 1.35M | return nullptr; |
5580 | 1.35M | } |
5581 | | |
5582 | | /// Given the operand for an FNeg, see if we can fold the result. If not, this |
5583 | | /// returns null. |
5584 | | static Value *simplifyFNegInst(Value *Op, FastMathFlags FMF, |
5585 | 1.35M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
5586 | 1.35M | if (Constant *C = foldConstant(Instruction::FNeg, Op, Q)) |
5587 | 5.80k | return C; |
5588 | | |
5589 | 1.35M | Value *X; |
5590 | | // fneg (fneg X) ==> X |
5591 | 1.35M | if (match(Op, m_FNeg(m_Value(X)))) |
5592 | 1.65k | return X; |
5593 | | |
5594 | 1.35M | return nullptr; |
5595 | 1.35M | } |
5596 | | |
5597 | | Value *llvm::simplifyFNegInst(Value *Op, FastMathFlags FMF, |
5598 | 762k | const SimplifyQuery &Q) { |
5599 | 762k | return ::simplifyFNegInst(Op, FMF, Q, RecursionLimit); |
5600 | 762k | } |
5601 | | |
5602 | | /// Try to propagate existing NaN values when possible. If not, replace the |
5603 | | /// constant or elements in the constant with a canonical NaN. |
5604 | 2.44k | static Constant *propagateNaN(Constant *In) { |
5605 | 2.44k | Type *Ty = In->getType(); |
5606 | 2.44k | if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) { |
5607 | 1 | unsigned NumElts = VecTy->getNumElements(); |
5608 | 1 | SmallVector<Constant *, 32> NewC(NumElts); |
5609 | 17 | for (unsigned i = 0; i != NumElts; ++i16 ) { |
5610 | 16 | Constant *EltC = In->getAggregateElement(i); |
5611 | | // Poison elements propagate. NaN propagates except signaling is quieted. |
5612 | | // Replace unknown or undef elements with canonical NaN. |
5613 | 16 | if (EltC && isa<PoisonValue>(EltC)) |
5614 | 0 | NewC[i] = EltC; |
5615 | 16 | else if (EltC && EltC->isNaN()) |
5616 | 16 | NewC[i] = ConstantFP::get( |
5617 | 16 | EltC->getType(), cast<ConstantFP>(EltC)->getValue().makeQuiet()); |
5618 | 0 | else |
5619 | 0 | NewC[i] = ConstantFP::getNaN(VecTy->getElementType()); |
5620 | 16 | } |
5621 | 1 | return ConstantVector::get(NewC); |
5622 | 1 | } |
5623 | | |
5624 | | // If it is not a fixed vector, but not a simple NaN either, return a |
5625 | | // canonical NaN. |
5626 | 2.43k | if (!In->isNaN()) |
5627 | 0 | return ConstantFP::getNaN(Ty); |
5628 | | |
5629 | | // If we known this is a NaN, and it's scalable vector, we must have a splat |
5630 | | // on our hands. Grab that before splatting a QNaN constant. |
5631 | 2.43k | if (isa<ScalableVectorType>(Ty)) { |
5632 | 0 | auto *Splat = In->getSplatValue(); |
5633 | 0 | assert(Splat && Splat->isNaN() && |
5634 | 0 | "Found a scalable-vector NaN but not a splat"); |
5635 | 0 | In = Splat; |
5636 | 0 | } |
5637 | | |
5638 | | // Propagate an existing QNaN constant. If it is an SNaN, make it quiet, but |
5639 | | // preserve the sign/payload. |
5640 | 2.43k | return ConstantFP::get(Ty, cast<ConstantFP>(In)->getValue().makeQuiet()); |
5641 | 2.43k | } |
5642 | | |
5643 | | /// Perform folds that are common to any floating-point operation. This implies |
5644 | | /// transforms based on poison/undef/NaN because the operation itself makes no |
5645 | | /// difference to the result. |
5646 | | static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF, |
5647 | | const SimplifyQuery &Q, |
5648 | | fp::ExceptionBehavior ExBehavior, |
5649 | 32.3M | RoundingMode Rounding) { |
5650 | | // Poison is independent of anything else. It always propagates from an |
5651 | | // operand to a math result. |
5652 | 68.7M | if (any_of(Ops, [](Value *V) 32.3M { return match(V, m_Poison()); })) |
5653 | 0 | return PoisonValue::get(Ops[0]->getType()); |
5654 | | |
5655 | 68.7M | for (Value *V : Ops)32.3M { |
5656 | 68.7M | bool IsNan = match(V, m_NaN()); |
5657 | 68.7M | bool IsInf = match(V, m_Inf()); |
5658 | 68.7M | bool IsUndef = Q.isUndefValue(V); |
5659 | | |
5660 | | // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand |
5661 | | // (an undef operand can be chosen to be Nan/Inf), then the result of |
5662 | | // this operation is poison. |
5663 | 68.7M | if (FMF.noNaNs() && (3.21M IsNan3.21M || IsUndef3.20M )) |
5664 | 1.81k | return PoisonValue::get(V->getType()); |
5665 | 68.7M | if (FMF.noInfs() && (3.20M IsInf3.20M || IsUndef3.20M )) |
5666 | 1 | return PoisonValue::get(V->getType()); |
5667 | | |
5668 | 68.7M | if (isDefaultFPEnvironment(ExBehavior, Rounding)) { |
5669 | | // Undef does not propagate because undef means that all bits can take on |
5670 | | // any value. If this is undef * NaN for example, then the result values |
5671 | | // (at least the exponent bits) are limited. Assume the undef is a |
5672 | | // canonical NaN and propagate that. |
5673 | 68.7M | if (IsUndef) |
5674 | 47 | return ConstantFP::getNaN(V->getType()); |
5675 | 68.7M | if (IsNan) |
5676 | 2.44k | return propagateNaN(cast<Constant>(V)); |
5677 | 68.7M | } else if (0 ExBehavior != fp::ebStrict0 ) { |
5678 | 0 | if (IsNan) |
5679 | 0 | return propagateNaN(cast<Constant>(V)); |
5680 | 0 | } |
5681 | 68.7M | } |
5682 | 32.3M | return nullptr; |
5683 | 32.3M | } |
5684 | | |
5685 | | /// Given operands for an FAdd, see if we can fold the result. If not, this |
5686 | | /// returns null. |
5687 | | static Value * |
5688 | | simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5689 | | const SimplifyQuery &Q, unsigned MaxRecurse, |
5690 | | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
5691 | 7.77M | RoundingMode Rounding = RoundingMode::NearestTiesToEven) { |
5692 | 7.77M | if (isDefaultFPEnvironment(ExBehavior, Rounding)) |
5693 | 7.77M | if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q)) |
5694 | 12.5k | return C; |
5695 | | |
5696 | 7.76M | if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding)) |
5697 | 626 | return C; |
5698 | | |
5699 | | // fadd X, -0 ==> X |
5700 | | // With strict/constrained FP, we have these possible edge cases that do |
5701 | | // not simplify to Op0: |
5702 | | // fadd SNaN, -0.0 --> QNaN |
5703 | | // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative) |
5704 | 7.76M | if (canIgnoreSNaN(ExBehavior, FMF) && |
5705 | 7.76M | (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) || |
5706 | 7.76M | FMF.noSignedZeros()0 )) |
5707 | 7.76M | if (match(Op1, m_NegZeroFP())) |
5708 | 659 | return Op0; |
5709 | | |
5710 | | // fadd X, 0 ==> X, when we know X is not -0 |
5711 | 7.76M | if (canIgnoreSNaN(ExBehavior, FMF)) |
5712 | 7.76M | if (match(Op1, m_PosZeroFP()) && |
5713 | 7.76M | (146k FMF.noSignedZeros()146k || cannotBeNegativeZero(Op0, Q)135k )) |
5714 | 17.5k | return Op0; |
5715 | | |
5716 | 7.74M | if (!isDefaultFPEnvironment(ExBehavior, Rounding)) |
5717 | 0 | return nullptr; |
5718 | | |
5719 | 7.74M | if (FMF.noNaNs()) { |
5720 | | // With nnan: X + {+/-}Inf --> {+/-}Inf |
5721 | 585k | if (match(Op1, m_Inf())) |
5722 | 0 | return Op1; |
5723 | | |
5724 | | // With nnan: -X + X --> 0.0 (and commuted variant) |
5725 | | // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN. |
5726 | | // Negative zeros are allowed because we always end up with positive zero: |
5727 | | // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0 |
5728 | | // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0 |
5729 | | // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0 |
5730 | | // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0 |
5731 | 585k | if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) || |
5732 | 585k | match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0)))) |
5733 | 0 | return ConstantFP::getZero(Op0->getType()); |
5734 | | |
5735 | 585k | if (match(Op0, m_FNeg(m_Specific(Op1))) || |
5736 | 585k | match(Op1, m_FNeg(m_Specific(Op0)))) |
5737 | 0 | return ConstantFP::getZero(Op0->getType()); |
5738 | 585k | } |
5739 | | |
5740 | | // (X - Y) + Y --> X |
5741 | | // Y + (X - Y) --> X |
5742 | 7.74M | Value *X; |
5743 | 7.74M | if (FMF.noSignedZeros() && FMF.allowReassoc()966k && |
5744 | 7.74M | (821k match(Op0, m_FSub(m_Value(X), m_Specific(Op1)))821k || |
5745 | 821k | match(Op1, m_FSub(m_Value(X), m_Specific(Op0)))821k )) |
5746 | 20 | return X; |
5747 | | |
5748 | 7.74M | return nullptr; |
5749 | 7.74M | } |
5750 | | |
5751 | | /// Given operands for an FSub, see if we can fold the result. If not, this |
5752 | | /// returns null. |
5753 | | static Value * |
5754 | | simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5755 | | const SimplifyQuery &Q, unsigned MaxRecurse, |
5756 | | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
5757 | 3.38M | RoundingMode Rounding = RoundingMode::NearestTiesToEven) { |
5758 | 3.38M | if (isDefaultFPEnvironment(ExBehavior, Rounding)) |
5759 | 3.38M | if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q)) |
5760 | 7.33k | return C; |
5761 | | |
5762 | 3.37M | if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding)) |
5763 | 937 | return C; |
5764 | | |
5765 | | // fsub X, +0 ==> X |
5766 | 3.37M | if (canIgnoreSNaN(ExBehavior, FMF) && |
5767 | 3.37M | (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) || |
5768 | 3.37M | FMF.noSignedZeros()0 )) |
5769 | 3.37M | if (match(Op1, m_PosZeroFP())) |
5770 | 24.6k | return Op0; |
5771 | | |
5772 | | // fsub X, -0 ==> X, when we know X is not -0 |
5773 | 3.35M | if (canIgnoreSNaN(ExBehavior, FMF)) |
5774 | 3.35M | if (match(Op1, m_NegZeroFP()) && |
5775 | 3.35M | (71 FMF.noSignedZeros()71 || cannotBeNegativeZero(Op0, Q)59 )) |
5776 | 13 | return Op0; |
5777 | | |
5778 | | // fsub -0.0, (fsub -0.0, X) ==> X |
5779 | | // fsub -0.0, (fneg X) ==> X |
5780 | 3.35M | Value *X; |
5781 | 3.35M | if (canIgnoreSNaN(ExBehavior, FMF)) |
5782 | 3.35M | if (match(Op0, m_NegZeroFP()) && match(Op1, m_FNeg(m_Value(X)))17 ) |
5783 | 0 | return X; |
5784 | | |
5785 | | // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored. |
5786 | | // fsub 0.0, (fneg X) ==> X if signed zeros are ignored. |
5787 | 3.35M | if (canIgnoreSNaN(ExBehavior, FMF)) |
5788 | 3.35M | if (FMF.noSignedZeros() && match(Op0, m_AnyZeroFP())377k && |
5789 | 3.35M | (3.26k match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X)))3.26k || |
5790 | 3.26k | match(Op1, m_FNeg(m_Value(X))))) |
5791 | 0 | return X; |
5792 | | |
5793 | 3.35M | if (!isDefaultFPEnvironment(ExBehavior, Rounding)) |
5794 | 0 | return nullptr; |
5795 | | |
5796 | 3.35M | if (FMF.noNaNs()) { |
5797 | | // fsub nnan x, x ==> 0.0 |
5798 | 145k | if (Op0 == Op1) |
5799 | 12 | return Constant::getNullValue(Op0->getType()); |
5800 | | |
5801 | | // With nnan: {+/-}Inf - X --> {+/-}Inf |
5802 | 145k | if (match(Op0, m_Inf())) |
5803 | 0 | return Op0; |
5804 | | |
5805 | | // With nnan: X - {+/-}Inf --> {-/+}Inf |
5806 | 145k | if (match(Op1, m_Inf())) |
5807 | 0 | return foldConstant(Instruction::FNeg, Op1, Q); |
5808 | 145k | } |
5809 | | |
5810 | | // Y - (Y - X) --> X |
5811 | | // (X + Y) - Y --> X |
5812 | 3.35M | if (FMF.noSignedZeros() && FMF.allowReassoc()377k && |
5813 | 3.35M | (273k match(Op1, m_FSub(m_Specific(Op0), m_Value(X)))273k || |
5814 | 273k | match(Op0, m_c_FAdd(m_Specific(Op1), m_Value(X)))273k )) |
5815 | 219 | return X; |
5816 | | |
5817 | 3.35M | return nullptr; |
5818 | 3.35M | } |
5819 | | |
5820 | | static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF, |
5821 | | const SimplifyQuery &Q, unsigned MaxRecurse, |
5822 | | fp::ExceptionBehavior ExBehavior, |
5823 | 14.9M | RoundingMode Rounding) { |
5824 | 14.9M | if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding)) |
5825 | 2.13k | return C; |
5826 | | |
5827 | 14.9M | if (!isDefaultFPEnvironment(ExBehavior, Rounding)) |
5828 | 0 | return nullptr; |
5829 | | |
5830 | | // Canonicalize special constants as operand 1. |
5831 | 14.9M | if (match(Op0, m_FPOne()) || match(Op0, m_AnyZeroFP())) |
5832 | 3 | std::swap(Op0, Op1); |
5833 | | |
5834 | | // X * 1.0 --> X |
5835 | 14.9M | if (match(Op1, m_FPOne())) |
5836 | 36.8k | return Op0; |
5837 | | |
5838 | 14.9M | if (match(Op1, m_AnyZeroFP())) { |
5839 | | // X * 0.0 --> 0.0 (with nnan and nsz) |
5840 | 152k | if (FMF.noNaNs() && FMF.noSignedZeros()225 ) |
5841 | 225 | return ConstantFP::getZero(Op0->getType()); |
5842 | | |
5843 | 152k | KnownFPClass Known = computeKnownFPClass(Op0, FMF, fcInf | fcNan, Q); |
5844 | 152k | if (Known.isKnownNever(fcInf | fcNan)) { |
5845 | | // if nsz is set, return 0.0 |
5846 | 10.1k | if (FMF.noSignedZeros()) |
5847 | 47 | return ConstantFP::getZero(Op0->getType()); |
5848 | | // +normal number * (-)0.0 --> (-)0.0 |
5849 | 10.1k | if (Known.SignBit == false) |
5850 | 285 | return Op1; |
5851 | | // -normal number * (-)0.0 --> -(-)0.0 |
5852 | 9.82k | if (Known.SignBit == true) |
5853 | 13 | return foldConstant(Instruction::FNeg, Op1, Q); |
5854 | 9.82k | } |
5855 | 152k | } |
5856 | | |
5857 | | // sqrt(X) * sqrt(X) --> X, if we can: |
5858 | | // 1. Remove the intermediate rounding (reassociate). |
5859 | | // 2. Ignore non-zero negative numbers because sqrt would produce NAN. |
5860 | | // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0. |
5861 | 14.9M | Value *X; |
5862 | 14.9M | if (Op0 == Op1 && match(Op0, m_Sqrt(m_Value(X)))1.33M && FMF.allowReassoc()1.67k && |
5863 | 14.9M | FMF.noNaNs()155 && FMF.noSignedZeros()112 ) |
5864 | 112 | return X; |
5865 | | |
5866 | 14.9M | return nullptr; |
5867 | 14.9M | } |
5868 | | |
5869 | | /// Given the operands for an FMul, see if we can fold the result |
5870 | | static Value * |
5871 | | simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5872 | | const SimplifyQuery &Q, unsigned MaxRecurse, |
5873 | | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
5874 | 13.1M | RoundingMode Rounding = RoundingMode::NearestTiesToEven) { |
5875 | 13.1M | if (isDefaultFPEnvironment(ExBehavior, Rounding)) |
5876 | 13.1M | if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q)) |
5877 | 101k | return C; |
5878 | | |
5879 | | // Now apply simplifications that do not require rounding. |
5880 | 13.0M | return simplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding); |
5881 | 13.1M | } |
5882 | | |
5883 | | Value *llvm::simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5884 | | const SimplifyQuery &Q, |
5885 | | fp::ExceptionBehavior ExBehavior, |
5886 | 2.98M | RoundingMode Rounding) { |
5887 | 2.98M | return ::simplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior, |
5888 | 2.98M | Rounding); |
5889 | 2.98M | } |
5890 | | |
5891 | | Value *llvm::simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5892 | | const SimplifyQuery &Q, |
5893 | | fp::ExceptionBehavior ExBehavior, |
5894 | 1.35M | RoundingMode Rounding) { |
5895 | 1.35M | return ::simplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior, |
5896 | 1.35M | Rounding); |
5897 | 1.35M | } |
5898 | | |
5899 | | Value *llvm::simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5900 | | const SimplifyQuery &Q, |
5901 | | fp::ExceptionBehavior ExBehavior, |
5902 | 6.06M | RoundingMode Rounding) { |
5903 | 6.06M | return ::simplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior, |
5904 | 6.06M | Rounding); |
5905 | 6.06M | } |
5906 | | |
5907 | | Value *llvm::simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF, |
5908 | | const SimplifyQuery &Q, |
5909 | | fp::ExceptionBehavior ExBehavior, |
5910 | 1.91M | RoundingMode Rounding) { |
5911 | 1.91M | return ::simplifyFMAFMul(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior, |
5912 | 1.91M | Rounding); |
5913 | 1.91M | } |
5914 | | |
5915 | | static Value * |
5916 | | simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5917 | | const SimplifyQuery &Q, unsigned, |
5918 | | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
5919 | 2.29M | RoundingMode Rounding = RoundingMode::NearestTiesToEven) { |
5920 | 2.29M | if (isDefaultFPEnvironment(ExBehavior, Rounding)) |
5921 | 2.29M | if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q)) |
5922 | 18.1k | return C; |
5923 | | |
5924 | 2.27M | if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding)) |
5925 | 556 | return C; |
5926 | | |
5927 | 2.27M | if (!isDefaultFPEnvironment(ExBehavior, Rounding)) |
5928 | 0 | return nullptr; |
5929 | | |
5930 | | // X / 1.0 -> X |
5931 | 2.27M | if (match(Op1, m_FPOne())) |
5932 | 1.67k | return Op0; |
5933 | | |
5934 | | // 0 / X -> 0 |
5935 | | // Requires that NaNs are off (X could be zero) and signed zeroes are |
5936 | | // ignored (X could be positive or negative, so the output sign is unknown). |
5937 | 2.26M | if (FMF.noNaNs() && FMF.noSignedZeros()57.8k && match(Op0, m_AnyZeroFP())57.8k ) |
5938 | 28 | return ConstantFP::getZero(Op0->getType()); |
5939 | | |
5940 | 2.26M | if (FMF.noNaNs()) { |
5941 | | // X / X -> 1.0 is legal when NaNs are ignored. |
5942 | | // We can ignore infinities because INF/INF is NaN. |
5943 | 57.7k | if (Op0 == Op1) |
5944 | 0 | return ConstantFP::get(Op0->getType(), 1.0); |
5945 | | |
5946 | | // (X * Y) / Y --> X if we can reassociate to the above form. |
5947 | 57.7k | Value *X; |
5948 | 57.7k | if (FMF.allowReassoc() && match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1)))56.7k ) |
5949 | 0 | return X; |
5950 | | |
5951 | | // -X / X -> -1.0 and |
5952 | | // X / -X -> -1.0 are legal when NaNs are ignored. |
5953 | | // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored. |
5954 | 57.7k | if (match(Op0, m_FNegNSZ(m_Specific(Op1))) || |
5955 | 57.7k | match(Op1, m_FNegNSZ(m_Specific(Op0)))) |
5956 | 0 | return ConstantFP::get(Op0->getType(), -1.0); |
5957 | | |
5958 | | // nnan ninf X / [-]0.0 -> poison |
5959 | 57.7k | if (FMF.noInfs() && match(Op1, m_AnyZeroFP())) |
5960 | 0 | return PoisonValue::get(Op1->getType()); |
5961 | 57.7k | } |
5962 | | |
5963 | 2.26M | return nullptr; |
5964 | 2.26M | } |
5965 | | |
5966 | | Value *llvm::simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5967 | | const SimplifyQuery &Q, |
5968 | | fp::ExceptionBehavior ExBehavior, |
5969 | 974k | RoundingMode Rounding) { |
5970 | 974k | return ::simplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior, |
5971 | 974k | Rounding); |
5972 | 974k | } |
5973 | | |
5974 | | static Value * |
5975 | | simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
5976 | | const SimplifyQuery &Q, unsigned, |
5977 | | fp::ExceptionBehavior ExBehavior = fp::ebIgnore, |
5978 | 3.37k | RoundingMode Rounding = RoundingMode::NearestTiesToEven) { |
5979 | 3.37k | if (isDefaultFPEnvironment(ExBehavior, Rounding)) |
5980 | 3.37k | if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q)) |
5981 | 29 | return C; |
5982 | | |
5983 | 3.34k | if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding)) |
5984 | 1 | return C; |
5985 | | |
5986 | 3.34k | if (!isDefaultFPEnvironment(ExBehavior, Rounding)) |
5987 | 0 | return nullptr; |
5988 | | |
5989 | | // Unlike fdiv, the result of frem always matches the sign of the dividend. |
5990 | | // The constant match may include undef elements in a vector, so return a full |
5991 | | // zero constant as the result. |
5992 | 3.34k | if (FMF.noNaNs()) { |
5993 | | // +0 % X -> 0 |
5994 | 255 | if (match(Op0, m_PosZeroFP())) |
5995 | 0 | return ConstantFP::getZero(Op0->getType()); |
5996 | | // -0 % X -> -0 |
5997 | 255 | if (match(Op0, m_NegZeroFP())) |
5998 | 0 | return ConstantFP::getNegativeZero(Op0->getType()); |
5999 | 255 | } |
6000 | | |
6001 | 3.34k | return nullptr; |
6002 | 3.34k | } |
6003 | | |
6004 | | Value *llvm::simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, |
6005 | | const SimplifyQuery &Q, |
6006 | | fp::ExceptionBehavior ExBehavior, |
6007 | 1.47k | RoundingMode Rounding) { |
6008 | 1.47k | return ::simplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior, |
6009 | 1.47k | Rounding); |
6010 | 1.47k | } |
6011 | | |
6012 | | //=== Helper functions for higher up the class hierarchy. |
6013 | | |
6014 | | /// Given the operand for a UnaryOperator, see if we can fold the result. |
6015 | | /// If not, this returns null. |
6016 | | static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q, |
6017 | 0 | unsigned MaxRecurse) { |
6018 | 0 | switch (Opcode) { |
6019 | 0 | case Instruction::FNeg: |
6020 | 0 | return simplifyFNegInst(Op, FastMathFlags(), Q, MaxRecurse); |
6021 | 0 | default: |
6022 | 0 | llvm_unreachable("Unexpected opcode"); |
6023 | 0 | } |
6024 | 0 | } |
6025 | | |
6026 | | /// Given the operand for a UnaryOperator, see if we can fold the result. |
6027 | | /// If not, this returns null. |
6028 | | /// Try to use FastMathFlags when folding the result. |
6029 | | static Value *simplifyFPUnOp(unsigned Opcode, Value *Op, |
6030 | | const FastMathFlags &FMF, const SimplifyQuery &Q, |
6031 | 155 | unsigned MaxRecurse) { |
6032 | 155 | switch (Opcode) { |
6033 | 155 | case Instruction::FNeg: |
6034 | 155 | return simplifyFNegInst(Op, FMF, Q, MaxRecurse); |
6035 | 0 | default: |
6036 | 0 | return simplifyUnOp(Opcode, Op, Q, MaxRecurse); |
6037 | 155 | } |
6038 | 155 | } |
6039 | | |
6040 | 0 | Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) { |
6041 | 0 | return ::simplifyUnOp(Opcode, Op, Q, RecursionLimit); |
6042 | 0 | } |
6043 | | |
6044 | | Value *llvm::simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF, |
6045 | 155 | const SimplifyQuery &Q) { |
6046 | 155 | return ::simplifyFPUnOp(Opcode, Op, FMF, Q, RecursionLimit); |
6047 | 155 | } |
6048 | | |
6049 | | /// Given operands for a BinaryOperator, see if we can fold the result. |
6050 | | /// If not, this returns null. |
6051 | | static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
6052 | 299M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
6053 | 299M | switch (Opcode) { |
6054 | 78.4M | case Instruction::Add: |
6055 | 78.4M | return simplifyAddInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q, |
6056 | 78.4M | MaxRecurse); |
6057 | 22.0M | case Instruction::Sub: |
6058 | 22.0M | return simplifySubInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q, |
6059 | 22.0M | MaxRecurse); |
6060 | 23.5M | case Instruction::Mul: |
6061 | 23.5M | return simplifyMulInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q, |
6062 | 23.5M | MaxRecurse); |
6063 | 1.33M | case Instruction::SDiv: |
6064 | 1.33M | return simplifySDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse); |
6065 | 1.60M | case Instruction::UDiv: |
6066 | 1.60M | return simplifyUDivInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse); |
6067 | 191k | case Instruction::SRem: |
6068 | 191k | return simplifySRemInst(LHS, RHS, Q, MaxRecurse); |
6069 | 1.65M | case Instruction::URem: |
6070 | 1.65M | return simplifyURemInst(LHS, RHS, Q, MaxRecurse); |
6071 | 15.6M | case Instruction::Shl: |
6072 | 15.6M | return simplifyShlInst(LHS, RHS, /* IsNSW */ false, /* IsNUW */ false, Q, |
6073 | 15.6M | MaxRecurse); |
6074 | 9.48M | case Instruction::LShr: |
6075 | 9.48M | return simplifyLShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse); |
6076 | 2.58M | case Instruction::AShr: |
6077 | 2.58M | return simplifyAShrInst(LHS, RHS, /* IsExact */ false, Q, MaxRecurse); |
6078 | 34.5M | case Instruction::And: |
6079 | 34.5M | return simplifyAndInst(LHS, RHS, Q, MaxRecurse); |
6080 | 80.6M | case Instruction::Or: |
6081 | 80.6M | return simplifyOrInst(LHS, RHS, Q, MaxRecurse); |
6082 | 26.6M | case Instruction::Xor: |
6083 | 26.6M | return simplifyXorInst(LHS, RHS, Q, MaxRecurse); |
6084 | 323k | case Instruction::FAdd: |
6085 | 323k | return simplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
6086 | 52.6k | case Instruction::FSub: |
6087 | 52.6k | return simplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
6088 | 342k | case Instruction::FMul: |
6089 | 342k | return simplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
6090 | 96.6k | case Instruction::FDiv: |
6091 | 96.6k | return simplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
6092 | 605 | case Instruction::FRem: |
6093 | 605 | return simplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
6094 | 0 | default: |
6095 | 0 | llvm_unreachable("Unexpected opcode"); |
6096 | 299M | } |
6097 | 299M | } |
6098 | | |
6099 | | /// Given operands for a BinaryOperator, see if we can fold the result. |
6100 | | /// If not, this returns null. |
6101 | | /// Try to use FastMathFlags when folding the result. |
6102 | | static Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
6103 | | const FastMathFlags &FMF, const SimplifyQuery &Q, |
6104 | 5.20M | unsigned MaxRecurse) { |
6105 | 5.20M | switch (Opcode) { |
6106 | 963k | case Instruction::FAdd: |
6107 | 963k | return simplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse); |
6108 | 513k | case Instruction::FSub: |
6109 | 513k | return simplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse); |
6110 | 1.59M | case Instruction::FMul: |
6111 | 1.59M | return simplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse); |
6112 | 249k | case Instruction::FDiv: |
6113 | 249k | return simplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse); |
6114 | 1.88M | default: |
6115 | 1.88M | return simplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse); |
6116 | 5.20M | } |
6117 | 5.20M | } |
6118 | | |
6119 | | Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
6120 | 100M | const SimplifyQuery &Q) { |
6121 | 100M | return ::simplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit); |
6122 | 100M | } |
6123 | | |
6124 | | Value *llvm::simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
6125 | 5.20M | FastMathFlags FMF, const SimplifyQuery &Q) { |
6126 | 5.20M | return ::simplifyBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit); |
6127 | 5.20M | } |
6128 | | |
6129 | | /// Given operands for a CmpInst, see if we can fold the result. |
6130 | | static Value *simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, |
6131 | 98.7M | const SimplifyQuery &Q, unsigned MaxRecurse) { |
6132 | 98.7M | if (CmpInst::isIntPredicate(Predicate)) |
6133 | 97.4M | return simplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse); |
6134 | 1.35M | return simplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse); |
6135 | 98.7M | } |
6136 | | |
6137 | | Value *llvm::simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, |
6138 | 15.8M | const SimplifyQuery &Q) { |
6139 | 15.8M | return ::simplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit); |
6140 | 15.8M | } |
6141 | | |
6142 | 28.7M | static bool isIdempotent(Intrinsic::ID ID) { |
6143 | 28.7M | switch (ID) { |
6144 | 27.4M | default: |
6145 | 27.4M | return false; |
6146 | | |
6147 | | // Unary idempotent: f(f(x)) = f(x) |
6148 | 1.12M | case Intrinsic::fabs: |
6149 | 1.23M | case Intrinsic::floor: |
6150 | 1.27M | case Intrinsic::ceil: |
6151 | 1.27M | case Intrinsic::trunc: |
6152 | 1.28M | case Intrinsic::rint: |
6153 | 1.28M | case Intrinsic::nearbyint: |
6154 | 1.31M | case Intrinsic::round: |
6155 | 1.31M | case Intrinsic::roundeven: |
6156 | 1.31M | case Intrinsic::canonicalize: |
6157 | 1.31M | case Intrinsic::arithmetic_fence: |
6158 | 1.31M | return true; |
6159 | 28.7M | } |
6160 | 28.7M | } |
6161 | | |
6162 | | /// Return true if the intrinsic rounds a floating-point value to an integral |
6163 | | /// floating-point value (not an integer type). |
6164 | 28.7M | static bool removesFPFraction(Intrinsic::ID ID) { |
6165 | 28.7M | switch (ID) { |
6166 | 28.5M | default: |
6167 | 28.5M | return false; |
6168 | | |
6169 | 110k | case Intrinsic::floor: |
6170 | 149k | case Intrinsic::ceil: |
6171 | 154k | case Intrinsic::trunc: |
6172 | 159k | case Intrinsic::rint: |
6173 | 159k | case Intrinsic::nearbyint: |
6174 | 190k | case Intrinsic::round: |
6175 | 190k | case Intrinsic::roundeven: |
6176 | 190k | return true; |
6177 | 28.7M | } |
6178 | 28.7M | } |
6179 | | |
6180 | | static Value *simplifyRelativeLoad(Constant *Ptr, Constant *Offset, |
6181 | 0 | const DataLayout &DL) { |
6182 | 0 | GlobalValue *PtrSym; |
6183 | 0 | APInt PtrOffset; |
6184 | 0 | if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL)) |
6185 | 0 | return nullptr; |
6186 | | |
6187 | 0 | Type *Int32Ty = Type::getInt32Ty(Ptr->getContext()); |
6188 | |
|
6189 | 0 | auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset); |
6190 | 0 | if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64) |
6191 | 0 | return nullptr; |
6192 | | |
6193 | 0 | APInt OffsetInt = OffsetConstInt->getValue().sextOrTrunc( |
6194 | 0 | DL.getIndexTypeSizeInBits(Ptr->getType())); |
6195 | 0 | if (OffsetInt.srem(4) != 0) |
6196 | 0 | return nullptr; |
6197 | | |
6198 | 0 | Constant *Loaded = |
6199 | 0 | ConstantFoldLoadFromConstPtr(Ptr, Int32Ty, std::move(OffsetInt), DL); |
6200 | 0 | if (!Loaded) |
6201 | 0 | return nullptr; |
6202 | | |
6203 | 0 | auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded); |
6204 | 0 | if (!LoadedCE) |
6205 | 0 | return nullptr; |
6206 | | |
6207 | 0 | if (LoadedCE->getOpcode() == Instruction::Trunc) { |
6208 | 0 | LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
6209 | 0 | if (!LoadedCE) |
6210 | 0 | return nullptr; |
6211 | 0 | } |
6212 | | |
6213 | 0 | if (LoadedCE->getOpcode() != Instruction::Sub) |
6214 | 0 | return nullptr; |
6215 | | |
6216 | 0 | auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0)); |
6217 | 0 | if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt) |
6218 | 0 | return nullptr; |
6219 | 0 | auto *LoadedLHSPtr = LoadedLHS->getOperand(0); |
6220 | |
|
6221 | 0 | Constant *LoadedRHS = LoadedCE->getOperand(1); |
6222 | 0 | GlobalValue *LoadedRHSSym; |
6223 | 0 | APInt LoadedRHSOffset; |
6224 | 0 | if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset, |
6225 | 0 | DL) || |
6226 | 0 | PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset) |
6227 | 0 | return nullptr; |
6228 | | |
6229 | 0 | return LoadedLHSPtr; |
6230 | 0 | } |
6231 | | |
6232 | | // TODO: Need to pass in FastMathFlags |
6233 | | static Value *simplifyLdexp(Value *Op0, Value *Op1, const SimplifyQuery &Q, |
6234 | 164 | bool IsStrict) { |
6235 | | // ldexp(poison, x) -> poison |
6236 | | // ldexp(x, poison) -> poison |
6237 | 164 | if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1)) |
6238 | 0 | return Op0; |
6239 | | |
6240 | | // ldexp(undef, x) -> nan |
6241 | 164 | if (Q.isUndefValue(Op0)) |
6242 | 0 | return ConstantFP::getNaN(Op0->getType()); |
6243 | | |
6244 | 164 | if (!IsStrict) { |
6245 | | // TODO: Could insert a canonicalize for strict |
6246 | | |
6247 | | // ldexp(x, undef) -> x |
6248 | 164 | if (Q.isUndefValue(Op1)) |
6249 | 0 | return Op0; |
6250 | 164 | } |
6251 | | |
6252 | 164 | const APFloat *C = nullptr; |
6253 | 164 | match(Op0, PatternMatch::m_APFloat(C)); |
6254 | | |
6255 | | // These cases should be safe, even with strictfp. |
6256 | | // ldexp(0.0, x) -> 0.0 |
6257 | | // ldexp(-0.0, x) -> -0.0 |
6258 | | // ldexp(inf, x) -> inf |
6259 | | // ldexp(-inf, x) -> -inf |
6260 | 164 | if (C && (158 C->isZero()158 || C->isInfinity()158 )) |
6261 | 0 | return Op0; |
6262 | | |
6263 | | // These are canonicalization dropping, could do it if we knew how we could |
6264 | | // ignore denormal flushes and target handling of nan payload bits. |
6265 | 164 | if (IsStrict) |
6266 | 0 | return nullptr; |
6267 | | |
6268 | | // TODO: Could quiet this with strictfp if the exception mode isn't strict. |
6269 | 164 | if (C && C->isNaN()158 ) |
6270 | 0 | return ConstantFP::get(Op0->getType(), C->makeQuiet()); |
6271 | | |
6272 | | // ldexp(x, 0) -> x |
6273 | | |
6274 | | // TODO: Could fold this if we know the exception mode isn't |
6275 | | // strict, we know the denormal mode and other target modes. |
6276 | 164 | if (match(Op1, PatternMatch::m_ZeroInt())) |
6277 | 0 | return Op0; |
6278 | | |
6279 | 164 | return nullptr; |
6280 | 164 | } |
6281 | | |
6282 | | static Value *simplifyUnaryIntrinsic(Function *F, Value *Op0, |
6283 | | const SimplifyQuery &Q, |
6284 | 28.7M | const CallBase *Call) { |
6285 | | // Idempotent functions return the same result when called repeatedly. |
6286 | 28.7M | Intrinsic::ID IID = F->getIntrinsicID(); |
6287 | 28.7M | if (isIdempotent(IID)) |
6288 | 1.31M | if (auto *II = dyn_cast<IntrinsicInst>(Op0)) |
6289 | 44.2k | if (II->getIntrinsicID() == IID) |
6290 | 551 | return II; |
6291 | | |
6292 | 28.7M | if (removesFPFraction(IID)) { |
6293 | | // Converting from int or calling a rounding function always results in a |
6294 | | // finite integral number or infinity. For those inputs, rounding functions |
6295 | | // always return the same value, so the (2nd) rounding is eliminated. Ex: |
6296 | | // floor (sitofp x) -> sitofp x |
6297 | | // round (ceil x) -> ceil x |
6298 | 190k | auto *II = dyn_cast<IntrinsicInst>(Op0); |
6299 | 190k | if ((II && removesFPFraction(II->getIntrinsicID())8.71k ) || |
6300 | 190k | match(Op0, m_SIToFP(m_Value()))190k || match(Op0, m_UIToFP(m_Value()))190k ) |
6301 | 204 | return Op0; |
6302 | 190k | } |
6303 | | |
6304 | 28.7M | Value *X; |
6305 | 28.7M | switch (IID) { |
6306 | 1.12M | case Intrinsic::fabs: |
6307 | 1.12M | if (computeKnownFPSignBit(Op0, Q) == false) |
6308 | 2.01k | return Op0; |
6309 | 1.11M | break; |
6310 | 1.11M | case Intrinsic::bswap: |
6311 | | // bswap(bswap(x)) -> x |
6312 | 988k | if (match(Op0, m_BSwap(m_Value(X)))) |
6313 | 510 | return X; |
6314 | 988k | break; |
6315 | 988k | case Intrinsic::bitreverse: |
6316 | | // bitreverse(bitreverse(x)) -> x |
6317 | 6.62k | if (match(Op0, m_BitReverse(m_Value(X)))) |
6318 | 1 | return X; |
6319 | 6.62k | break; |
6320 | 383k | case Intrinsic::ctpop: { |
6321 | | // ctpop(X) -> 1 iff X is non-zero power of 2. |
6322 | 383k | if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ false, Q.AC, Q.CxtI, Q.DT)) |
6323 | 16 | return ConstantInt::get(Op0->getType(), 1); |
6324 | | // If everything but the lowest bit is zero, that bit is the pop-count. Ex: |
6325 | | // ctpop(and X, 1) --> and X, 1 |
6326 | 383k | unsigned BitWidth = Op0->getType()->getScalarSizeInBits(); |
6327 | 383k | if (MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, BitWidth - 1), |
6328 | 383k | Q)) |
6329 | 5 | return Op0; |
6330 | 383k | break; |
6331 | 383k | } |
6332 | 383k | case Intrinsic::exp: |
6333 | | // exp(log(x)) -> x |
6334 | 17.5k | if (Call->hasAllowReassoc() && |
6335 | 17.5k | match(Op0, m_Intrinsic<Intrinsic::log>(m_Value(X)))10.5k ) |
6336 | 0 | return X; |
6337 | 17.5k | break; |
6338 | 17.5k | case Intrinsic::exp2: |
6339 | | // exp2(log2(x)) -> x |
6340 | 5.13k | if (Call->hasAllowReassoc() && |
6341 | 5.13k | match(Op0, m_Intrinsic<Intrinsic::log2>(m_Value(X)))2.61k ) |
6342 | 2 | return X; |
6343 | 5.13k | break; |
6344 | 5.13k | case Intrinsic::exp10: |
6345 | | // exp10(log10(x)) -> x |
6346 | 0 | if (Call->hasAllowReassoc() && |
6347 | 0 | match(Op0, m_Intrinsic<Intrinsic::log10>(m_Value(X)))) |
6348 | 0 | return X; |
6349 | 0 | break; |
6350 | 33.4k | case Intrinsic::log: |
6351 | | // log(exp(x)) -> x |
6352 | 33.4k | if (Call->hasAllowReassoc() && |
6353 | 33.4k | match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X)))7.20k ) |
6354 | 0 | return X; |
6355 | 33.4k | break; |
6356 | 33.4k | case Intrinsic::log2: |
6357 | | // log2(exp2(x)) -> x |
6358 | 4.59k | if (Call->hasAllowReassoc() && |
6359 | 4.59k | (1.81k match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X)))1.81k || |
6360 | 1.81k | match(Op0, |
6361 | 1.81k | m_Intrinsic<Intrinsic::pow>(m_SpecificFP(2.0), m_Value(X))))) |
6362 | 0 | return X; |
6363 | 4.59k | break; |
6364 | 4.70k | case Intrinsic::log10: |
6365 | | // log10(pow(10.0, x)) -> x |
6366 | | // log10(exp10(x)) -> x |
6367 | 4.70k | if (Call->hasAllowReassoc() && |
6368 | 4.70k | (564 match(Op0, m_Intrinsic<Intrinsic::exp10>(m_Value(X)))564 || |
6369 | 564 | match(Op0, |
6370 | 564 | m_Intrinsic<Intrinsic::pow>(m_SpecificFP(10.0), m_Value(X))))) |
6371 | 0 | return X; |
6372 | 4.70k | break; |
6373 | 4.70k | case Intrinsic::vector_reverse: |
6374 | | // vector.reverse(vector.reverse(x)) -> x |
6375 | 0 | if (match(Op0, m_VecReverse(m_Value(X)))) |
6376 | 0 | return X; |
6377 | | // vector.reverse(splat(X)) -> splat(X) |
6378 | 0 | if (isSplatValue(Op0)) |
6379 | 0 | return Op0; |
6380 | 0 | break; |
6381 | 26.2M | default: |
6382 | 26.2M | break; |
6383 | 28.7M | } |
6384 | | |
6385 | 28.7M | return nullptr; |
6386 | 28.7M | } |
6387 | | |
6388 | | /// Given a min/max intrinsic, see if it can be removed based on having an |
6389 | | /// operand that is another min/max intrinsic with shared operand(s). The caller |
6390 | | /// is expected to swap the operand arguments to handle commutation. |
6391 | 23.1M | static Value *foldMinMaxSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1) { |
6392 | 23.1M | Value *X, *Y; |
6393 | 23.1M | if (!match(Op0, m_MaxOrMin(m_Value(X), m_Value(Y)))) |
6394 | 21.6M | return nullptr; |
6395 | | |
6396 | 1.52M | auto *MM0 = dyn_cast<IntrinsicInst>(Op0); |
6397 | 1.52M | if (!MM0) |
6398 | 1.95k | return nullptr; |
6399 | 1.51M | Intrinsic::ID IID0 = MM0->getIntrinsicID(); |
6400 | | |
6401 | 1.51M | if (Op1 == X || Op1 == Y1.51M || |
6402 | 1.51M | match(Op1, m_c_MaxOrMin(m_Specific(X), m_Specific(Y)))1.51M ) { |
6403 | | // max (max X, Y), X --> max X, Y |
6404 | 459 | if (IID0 == IID) |
6405 | 300 | return MM0; |
6406 | | // max (min X, Y), X --> X |
6407 | 159 | if (IID0 == getInverseMinMaxIntrinsic(IID)) |
6408 | 51 | return Op1; |
6409 | 159 | } |
6410 | 1.51M | return nullptr; |
6411 | 1.51M | } |
6412 | | |
6413 | | /// Given a min/max intrinsic, see if it can be removed based on having an |
6414 | | /// operand that is another min/max intrinsic with shared operand(s). The caller |
6415 | | /// is expected to swap the operand arguments to handle commutation. |
6416 | | static Value *foldMinimumMaximumSharedOp(Intrinsic::ID IID, Value *Op0, |
6417 | 142k | Value *Op1) { |
6418 | 142k | assert((IID == Intrinsic::maxnum || IID == Intrinsic::minnum || |
6419 | 142k | IID == Intrinsic::maximum || IID == Intrinsic::minimum) && |
6420 | 142k | "Unsupported intrinsic"); |
6421 | | |
6422 | 142k | auto *M0 = dyn_cast<IntrinsicInst>(Op0); |
6423 | | // If Op0 is not the same intrinsic as IID, do not process. |
6424 | | // This is a difference with integer min/max handling. We do not process the |
6425 | | // case like max(min(X,Y),min(X,Y)) => min(X,Y). But it can be handled by GVN. |
6426 | 142k | if (!M0 || M0->getIntrinsicID() != IID19.6k ) |
6427 | 134k | return nullptr; |
6428 | 8.33k | Value *X0 = M0->getOperand(0); |
6429 | 8.33k | Value *Y0 = M0->getOperand(1); |
6430 | | // Simple case, m(m(X,Y), X) => m(X, Y) |
6431 | | // m(m(X,Y), Y) => m(X, Y) |
6432 | | // For minimum/maximum, X is NaN => m(NaN, Y) == NaN and m(NaN, NaN) == NaN. |
6433 | | // For minimum/maximum, Y is NaN => m(X, NaN) == NaN and m(NaN, NaN) == NaN. |
6434 | | // For minnum/maxnum, X is NaN => m(NaN, Y) == Y and m(Y, Y) == Y. |
6435 | | // For minnum/maxnum, Y is NaN => m(X, NaN) == X and m(X, NaN) == X. |
6436 | 8.33k | if (X0 == Op1 || Y0 == Op1) |
6437 | 1 | return M0; |
6438 | | |
6439 | 8.33k | auto *M1 = dyn_cast<IntrinsicInst>(Op1); |
6440 | 8.33k | if (!M1) |
6441 | 7.78k | return nullptr; |
6442 | 551 | Value *X1 = M1->getOperand(0); |
6443 | 551 | Value *Y1 = M1->getOperand(1); |
6444 | 551 | Intrinsic::ID IID1 = M1->getIntrinsicID(); |
6445 | | // we have a case m(m(X,Y),m'(X,Y)) taking into account m' is commutative. |
6446 | | // if m' is m or inversion of m => m(m(X,Y),m'(X,Y)) == m(X,Y). |
6447 | | // For minimum/maximum, X is NaN => m(NaN,Y) == m'(NaN, Y) == NaN. |
6448 | | // For minimum/maximum, Y is NaN => m(X,NaN) == m'(X, NaN) == NaN. |
6449 | | // For minnum/maxnum, X is NaN => m(NaN,Y) == m'(NaN, Y) == Y. |
6450 | | // For minnum/maxnum, Y is NaN => m(X,NaN) == m'(X, NaN) == X. |
6451 | 551 | if ((X0 == X1 && Y0 == Y14 ) || (X0 == Y1 && Y0 == X10 )) |
6452 | 0 | if (IID1 == IID || getInverseMinMaxIntrinsic(IID1) == IID) |
6453 | 0 | return M0; |
6454 | | |
6455 | 551 | return nullptr; |
6456 | 551 | } |
6457 | | |
6458 | | Value *llvm::simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType, |
6459 | | Value *Op0, Value *Op1, |
6460 | | const SimplifyQuery &Q, |
6461 | 107M | const CallBase *Call) { |
6462 | 107M | unsigned BitWidth = ReturnType->getScalarSizeInBits(); |
6463 | 107M | switch (IID) { |
6464 | 276k | case Intrinsic::abs: |
6465 | | // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here. |
6466 | | // It is always ok to pick the earlier abs. We'll just lose nsw if its only |
6467 | | // on the outer abs. |
6468 | 276k | if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(), m_Value()))) |
6469 | 11 | return Op0; |
6470 | 276k | break; |
6471 | | |
6472 | 1.59M | case Intrinsic::cttz: { |
6473 | 1.59M | Value *X; |
6474 | 1.59M | if (match(Op0, m_Shl(m_One(), m_Value(X)))) |
6475 | 194 | return X; |
6476 | 1.59M | break; |
6477 | 1.59M | } |
6478 | 1.59M | case Intrinsic::ctlz: { |
6479 | 970k | Value *X; |
6480 | 970k | if (match(Op0, m_LShr(m_Negative(), m_Value(X)))) |
6481 | 29 | return X; |
6482 | 970k | if (match(Op0, m_AShr(m_Negative(), m_Value()))) |
6483 | 0 | return Constant::getNullValue(ReturnType); |
6484 | 970k | break; |
6485 | 970k | } |
6486 | 970k | case Intrinsic::ptrmask: { |
6487 | | // NOTE: We can't apply this simplifications based on the value of Op1 |
6488 | | // because we need to preserve provenance. |
6489 | 2.45k | if (Q.isUndefValue(Op0) || match(Op0, m_Zero())) |
6490 | 0 | return Constant::getNullValue(Op0->getType()); |
6491 | | |
6492 | 2.45k | assert(Op1->getType()->getScalarSizeInBits() == |
6493 | 2.45k | Q.DL.getIndexTypeSizeInBits(Op0->getType()) && |
6494 | 2.45k | "Invalid mask width"); |
6495 | | // If index-width (mask size) is less than pointer-size then mask is |
6496 | | // 1-extended. |
6497 | 2.45k | if (match(Op1, m_PtrToInt(m_Specific(Op0)))) |
6498 | 0 | return Op0; |
6499 | | |
6500 | | // NOTE: We may have attributes associated with the return value of the |
6501 | | // llvm.ptrmask intrinsic that will be lost when we just return the |
6502 | | // operand. We should try to preserve them. |
6503 | 2.45k | if (match(Op1, m_AllOnes()) || Q.isUndefValue(Op1)) |
6504 | 0 | return Op0; |
6505 | | |
6506 | 2.45k | Constant *C; |
6507 | 2.45k | if (match(Op1, m_ImmConstant(C))) { |
6508 | 2.15k | KnownBits PtrKnown = computeKnownBits(Op0, Q); |
6509 | | // See if we only masking off bits we know are already zero due to |
6510 | | // alignment. |
6511 | 2.15k | APInt IrrelevantPtrBits = |
6512 | 2.15k | PtrKnown.Zero.zextOrTrunc(C->getType()->getScalarSizeInBits()); |
6513 | 2.15k | C = ConstantFoldBinaryOpOperands( |
6514 | 2.15k | Instruction::Or, C, ConstantInt::get(C->getType(), IrrelevantPtrBits), |
6515 | 2.15k | Q.DL); |
6516 | 2.15k | if (C != nullptr && C->isAllOnesValue()) |
6517 | 10 | return Op0; |
6518 | 2.15k | } |
6519 | 2.44k | break; |
6520 | 2.45k | } |
6521 | 1.66M | case Intrinsic::smax: |
6522 | 3.57M | case Intrinsic::smin: |
6523 | 6.47M | case Intrinsic::umax: |
6524 | 11.6M | case Intrinsic::umin: { |
6525 | | // If the arguments are the same, this is a no-op. |
6526 | 11.6M | if (Op0 == Op1) |
6527 | 25.2k | return Op0; |
6528 | | |
6529 | | // Canonicalize immediate constant operand as Op1. |
6530 | 11.5M | if (match(Op0, m_ImmConstant())) |
6531 | 80.8k | std::swap(Op0, Op1); |
6532 | | |
6533 | | // Assume undef is the limit value. |
6534 | 11.5M | if (Q.isUndefValue(Op1)) |
6535 | 39 | return ConstantInt::get( |
6536 | 39 | ReturnType, MinMaxIntrinsic::getSaturationPoint(IID, BitWidth)); |
6537 | | |
6538 | 11.5M | const APInt *C; |
6539 | 11.5M | if (match(Op1, m_APIntAllowPoison(C))) { |
6540 | | // Clamp to limit value. For example: |
6541 | | // umax(i8 %x, i8 255) --> 255 |
6542 | 6.79M | if (*C == MinMaxIntrinsic::getSaturationPoint(IID, BitWidth)) |
6543 | 7.87k | return ConstantInt::get(ReturnType, *C); |
6544 | | |
6545 | | // If the constant op is the opposite of the limit value, the other must |
6546 | | // be larger/smaller or equal. For example: |
6547 | | // umin(i8 %x, i8 255) --> %x |
6548 | 6.79M | if (*C == MinMaxIntrinsic::getSaturationPoint( |
6549 | 6.79M | getInverseMinMaxIntrinsic(IID), BitWidth)) |
6550 | 7.62k | return Op0; |
6551 | | |
6552 | | // Remove nested call if constant operands allow it. Example: |
6553 | | // max (max X, 7), 5 -> max X, 7 |
6554 | 6.78M | auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0); |
6555 | 6.78M | if (MinMax0 && MinMax0->getIntrinsicID() == IID1.16M ) { |
6556 | | // TODO: loosen undef/splat restrictions for vector constants. |
6557 | 143k | Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1); |
6558 | 143k | const APInt *InnerC; |
6559 | 143k | if ((match(M00, m_APInt(InnerC)) || match(M01, m_APInt(InnerC))143k ) && |
6560 | 143k | ICmpInst::compare(*InnerC, *C, |
6561 | 762 | ICmpInst::getNonStrictPredicate( |
6562 | 762 | MinMaxIntrinsic::getPredicate(IID)))) |
6563 | 603 | return Op0; |
6564 | 143k | } |
6565 | 6.78M | } |
6566 | | |
6567 | 11.5M | if (Value *V = foldMinMaxSharedOp(IID, Op0, Op1)) |
6568 | 177 | return V; |
6569 | 11.5M | if (Value *V = foldMinMaxSharedOp(IID, Op1, Op0)) |
6570 | 174 | return V; |
6571 | | |
6572 | 11.5M | ICmpInst::Predicate Pred = |
6573 | 11.5M | ICmpInst::getNonStrictPredicate(MinMaxIntrinsic::getPredicate(IID)); |
6574 | 11.5M | if (isICmpTrue(Pred, Op0, Op1, Q.getWithoutUndef(), RecursionLimit)) |
6575 | 17.4k | return Op0; |
6576 | 11.5M | if (isICmpTrue(Pred, Op1, Op0, Q.getWithoutUndef(), RecursionLimit)) |
6577 | 1.62k | return Op1; |
6578 | | |
6579 | 11.5M | break; |
6580 | 11.5M | } |
6581 | 11.5M | case Intrinsic::scmp: |
6582 | 330k | case Intrinsic::ucmp: { |
6583 | | // Fold to a constant if the relationship between operands can be |
6584 | | // established with certainty |
6585 | 330k | if (isICmpTrue(CmpInst::ICMP_EQ, Op0, Op1, Q, RecursionLimit)) |
6586 | 48 | return Constant::getNullValue(ReturnType); |
6587 | | |
6588 | 330k | ICmpInst::Predicate PredGT = |
6589 | 330k | IID == Intrinsic::scmp ? ICmpInst::ICMP_SGT133k : ICmpInst::ICMP_UGT197k ; |
6590 | 330k | if (isICmpTrue(PredGT, Op0, Op1, Q, RecursionLimit)) |
6591 | 3 | return ConstantInt::get(ReturnType, 1); |
6592 | | |
6593 | 330k | ICmpInst::Predicate PredLT = |
6594 | 330k | IID == Intrinsic::scmp ? ICmpInst::ICMP_SLT133k : ICmpInst::ICMP_ULT197k ; |
6595 | 330k | if (isICmpTrue(PredLT, Op0, Op1, Q, RecursionLimit)) |
6596 | 17 | return ConstantInt::getSigned(ReturnType, -1); |
6597 | | |
6598 | 330k | break; |
6599 | 330k | } |
6600 | 330k | case Intrinsic::usub_with_overflow: |
6601 | 13.0k | case Intrinsic::ssub_with_overflow: |
6602 | | // X - X -> { 0, false } |
6603 | | // X - undef -> { 0, false } |
6604 | | // undef - X -> { 0, false } |
6605 | 13.0k | if (Op0 == Op1 || Q.isUndefValue(Op0)13.0k || Q.isUndefValue(Op1)13.0k ) |
6606 | 2 | return Constant::getNullValue(ReturnType); |
6607 | 13.0k | break; |
6608 | 483k | case Intrinsic::uadd_with_overflow: |
6609 | 520k | case Intrinsic::sadd_with_overflow: |
6610 | | // X + undef -> { -1, false } |
6611 | | // undef + x -> { -1, false } |
6612 | 520k | if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)520k ) { |
6613 | 28 | return ConstantStruct::get( |
6614 | 28 | cast<StructType>(ReturnType), |
6615 | 28 | {Constant::getAllOnesValue(ReturnType->getStructElementType(0)), |
6616 | 28 | Constant::getNullValue(ReturnType->getStructElementType(1))}); |
6617 | 28 | } |
6618 | 520k | break; |
6619 | 520k | case Intrinsic::umul_with_overflow: |
6620 | 230k | case Intrinsic::smul_with_overflow: |
6621 | | // 0 * X -> { 0, false } |
6622 | | // X * 0 -> { 0, false } |
6623 | 230k | if (match(Op0, m_Zero()) || match(Op1, m_Zero())230k ) |
6624 | 18 | return Constant::getNullValue(ReturnType); |
6625 | | // undef * X -> { 0, false } |
6626 | | // X * undef -> { 0, false } |
6627 | 230k | if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)) |
6628 | 0 | return Constant::getNullValue(ReturnType); |
6629 | 230k | break; |
6630 | 230k | case Intrinsic::uadd_sat: |
6631 | | // sat(MAX + X) -> MAX |
6632 | | // sat(X + MAX) -> MAX |
6633 | 149k | if (match(Op0, m_AllOnes()) || match(Op1, m_AllOnes())149k ) |
6634 | 109 | return Constant::getAllOnesValue(ReturnType); |
6635 | 149k | [[fallthrough]]; |
6636 | 174k | case Intrinsic::sadd_sat: |
6637 | | // sat(X + undef) -> -1 |
6638 | | // sat(undef + X) -> -1 |
6639 | | // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1). |
6640 | | // For signed: Assume undef is ~X, in which case X + ~X = -1. |
6641 | 174k | if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)) |
6642 | 4 | return Constant::getAllOnesValue(ReturnType); |
6643 | | |
6644 | | // X + 0 -> X |
6645 | 174k | if (match(Op1, m_Zero())) |
6646 | 233 | return Op0; |
6647 | | // 0 + X -> X |
6648 | 173k | if (match(Op0, m_Zero())) |
6649 | 312 | return Op1; |
6650 | 173k | break; |
6651 | 566k | case Intrinsic::usub_sat: |
6652 | | // sat(0 - X) -> 0, sat(X - MAX) -> 0 |
6653 | 566k | if (match(Op0, m_Zero()) || match(Op1, m_AllOnes())566k ) |
6654 | 165 | return Constant::getNullValue(ReturnType); |
6655 | 566k | [[fallthrough]]; |
6656 | 570k | case Intrinsic::ssub_sat: |
6657 | | // X - X -> 0, X - undef -> 0, undef - X -> 0 |
6658 | 570k | if (Op0 == Op1 || Q.isUndefValue(Op0)570k || Q.isUndefValue(Op1)570k ) |
6659 | 35 | return Constant::getNullValue(ReturnType); |
6660 | | // X - 0 -> X |
6661 | 570k | if (match(Op1, m_Zero())) |
6662 | 609 | return Op0; |
6663 | 570k | break; |
6664 | 570k | case Intrinsic::load_relative: |
6665 | 86 | if (auto *C0 = dyn_cast<Constant>(Op0)) |
6666 | 86 | if (auto *C1 = dyn_cast<Constant>(Op1)) |
6667 | 0 | return simplifyRelativeLoad(C0, C1, Q.DL); |
6668 | 86 | break; |
6669 | 3.82k | case Intrinsic::powi: |
6670 | 3.82k | if (auto *Power = dyn_cast<ConstantInt>(Op1)) { |
6671 | | // powi(x, 0) -> 1.0 |
6672 | 750 | if (Power->isZero()) |
6673 | 0 | return ConstantFP::get(Op0->getType(), 1.0); |
6674 | | // powi(x, 1) -> x |
6675 | 750 | if (Power->isOne()) |
6676 | 0 | return Op0; |
6677 | 750 | } |
6678 | 3.82k | break; |
6679 | 3.82k | case Intrinsic::ldexp: |
6680 | 164 | return simplifyLdexp(Op0, Op1, Q, false); |
6681 | 59.2k | case Intrinsic::copysign: |
6682 | | // copysign X, X --> X |
6683 | 59.2k | if (Op0 == Op1) |
6684 | 0 | return Op0; |
6685 | | // copysign -X, X --> X |
6686 | | // copysign X, -X --> -X |
6687 | 59.2k | if (match(Op0, m_FNeg(m_Specific(Op1))) || |
6688 | 59.2k | match(Op1, m_FNeg(m_Specific(Op0)))) |
6689 | 0 | return Op1; |
6690 | 59.2k | break; |
6691 | 59.2k | case Intrinsic::is_fpclass: { |
6692 | 20.9k | uint64_t Mask = cast<ConstantInt>(Op1)->getZExtValue(); |
6693 | | // If all tests are made, it doesn't matter what the value is. |
6694 | 20.9k | if ((Mask & fcAllFlags) == fcAllFlags) |
6695 | 1 | return ConstantInt::get(ReturnType, true); |
6696 | 20.9k | if ((Mask & fcAllFlags) == 0) |
6697 | 3 | return ConstantInt::get(ReturnType, false); |
6698 | 20.9k | if (Q.isUndefValue(Op0)) |
6699 | 0 | return UndefValue::get(ReturnType); |
6700 | 20.9k | break; |
6701 | 20.9k | } |
6702 | 42.9k | case Intrinsic::maxnum: |
6703 | 71.2k | case Intrinsic::minnum: |
6704 | 71.2k | case Intrinsic::maximum: |
6705 | 71.2k | case Intrinsic::minimum: { |
6706 | | // If the arguments are the same, this is a no-op. |
6707 | 71.2k | if (Op0 == Op1) |
6708 | 0 | return Op0; |
6709 | | |
6710 | | // Canonicalize constant operand as Op1. |
6711 | 71.2k | if (isa<Constant>(Op0)) |
6712 | 639 | std::swap(Op0, Op1); |
6713 | | |
6714 | | // If an argument is undef, return the other argument. |
6715 | 71.2k | if (Q.isUndefValue(Op1)) |
6716 | 0 | return Op0; |
6717 | | |
6718 | 71.2k | bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum; |
6719 | 71.2k | bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum; |
6720 | | |
6721 | | // minnum(X, nan) -> X |
6722 | | // maxnum(X, nan) -> X |
6723 | | // minimum(X, nan) -> nan |
6724 | | // maximum(X, nan) -> nan |
6725 | 71.2k | if (match(Op1, m_NaN())) |
6726 | 0 | return PropagateNaN ? propagateNaN(cast<Constant>(Op1)) : Op0; |
6727 | | |
6728 | | // In the following folds, inf can be replaced with the largest finite |
6729 | | // float, if the ninf flag is set. |
6730 | 71.2k | const APFloat *C; |
6731 | 71.2k | if (match(Op1, m_APFloat(C)) && |
6732 | 71.2k | (27.2k C->isInfinity()27.2k || (26.7k Call26.7k && Call->hasNoInfs()26.7k && C->isLargest()3.27k ))) { |
6733 | | // minnum(X, -inf) -> -inf |
6734 | | // maxnum(X, +inf) -> +inf |
6735 | | // minimum(X, -inf) -> -inf if nnan |
6736 | | // maximum(X, +inf) -> +inf if nnan |
6737 | 446 | if (C->isNegative() == IsMin && |
6738 | 446 | (0 !PropagateNaN0 || (0 Call0 && Call->hasNoNaNs()0 ))) |
6739 | 0 | return ConstantFP::get(ReturnType, *C); |
6740 | | |
6741 | | // minnum(X, +inf) -> X if nnan |
6742 | | // maxnum(X, -inf) -> X if nnan |
6743 | | // minimum(X, +inf) -> X |
6744 | | // maximum(X, -inf) -> X |
6745 | 446 | if (C->isNegative() != IsMin && |
6746 | 446 | (PropagateNaN || (Call && Call->hasNoNaNs()))) |
6747 | 0 | return Op0; |
6748 | 446 | } |
6749 | | |
6750 | | // Min/max of the same operation with common operand: |
6751 | | // m(m(X, Y)), X --> m(X, Y) (4 commuted variants) |
6752 | 71.2k | if (Value *V = foldMinimumMaximumSharedOp(IID, Op0, Op1)) |
6753 | 1 | return V; |
6754 | 71.2k | if (Value *V = foldMinimumMaximumSharedOp(IID, Op1, Op0)) |
6755 | 0 | return V; |
6756 | | |
6757 | 71.2k | break; |
6758 | 71.2k | } |
6759 | 71.2k | case Intrinsic::vector_extract: { |
6760 | | // (extract_vector (insert_vector _, X, 0), 0) -> X |
6761 | 0 | unsigned IdxN = cast<ConstantInt>(Op1)->getZExtValue(); |
6762 | 0 | Value *X = nullptr; |
6763 | 0 | if (match(Op0, m_Intrinsic<Intrinsic::vector_insert>(m_Value(), m_Value(X), |
6764 | 0 | m_Zero())) && |
6765 | 0 | IdxN == 0 && X->getType() == ReturnType) |
6766 | 0 | return X; |
6767 | | |
6768 | 0 | break; |
6769 | 0 | } |
6770 | 91.1M | default: |
6771 | 91.1M | break; |
6772 | 107M | } |
6773 | | |
6774 | 107M | return nullptr; |
6775 | 107M | } |
6776 | | |
6777 | | static Value *simplifyIntrinsic(CallBase *Call, Value *Callee, |
6778 | | ArrayRef<Value *> Args, |
6779 | 174M | const SimplifyQuery &Q) { |
6780 | | // Operand bundles should not be in Args. |
6781 | 174M | assert(Call->arg_size() == Args.size()); |
6782 | 174M | unsigned NumOperands = Args.size(); |
6783 | 174M | Function *F = cast<Function>(Callee); |
6784 | 174M | Intrinsic::ID IID = F->getIntrinsicID(); |
6785 | | |
6786 | 174M | if (IID != Intrinsic::not_intrinsic && intrinsicPropagatesPoison(IID) && |
6787 | 174M | any_of(Args, IsaPred<PoisonValue>)18.2M ) |
6788 | 32 | return PoisonValue::get(F->getReturnType()); |
6789 | | // Most of the intrinsics with no operands have some kind of side effect. |
6790 | | // Don't simplify. |
6791 | 174M | if (!NumOperands) { |
6792 | 554k | switch (IID) { |
6793 | 0 | case Intrinsic::vscale: { |
6794 | 0 | Type *RetTy = F->getReturnType(); |
6795 | 0 | ConstantRange CR = getVScaleRange(Call->getFunction(), 64); |
6796 | 0 | if (const APInt *C = CR.getSingleElement()) |
6797 | 0 | return ConstantInt::get(RetTy, C->getZExtValue()); |
6798 | 0 | return nullptr; |
6799 | 0 | } |
6800 | 554k | default: |
6801 | 554k | return nullptr; |
6802 | 554k | } |
6803 | 554k | } |
6804 | | |
6805 | 174M | if (NumOperands == 1) |
6806 | 28.7M | return simplifyUnaryIntrinsic(F, Args[0], Q, Call); |
6807 | | |
6808 | 145M | if (NumOperands == 2) |
6809 | 107M | return simplifyBinaryIntrinsic(IID, F->getReturnType(), Args[0], Args[1], Q, |
6810 | 107M | Call); |
6811 | | |
6812 | | // Handle intrinsics with 3 or more arguments. |
6813 | 37.7M | switch (IID) { |
6814 | 5.07k | case Intrinsic::masked_load: |
6815 | 5.34k | case Intrinsic::masked_gather: { |
6816 | 5.34k | Value *MaskArg = Args[2]; |
6817 | 5.34k | Value *PassthruArg = Args[3]; |
6818 | | // If the mask is all zeros or undef, the "passthru" argument is the result. |
6819 | 5.34k | if (maskIsAllZeroOrUndef(MaskArg)) |
6820 | 0 | return PassthruArg; |
6821 | 5.34k | return nullptr; |
6822 | 5.34k | } |
6823 | 1.77M | case Intrinsic::fshl: |
6824 | 1.84M | case Intrinsic::fshr: { |
6825 | 1.84M | Value *Op0 = Args[0], *Op1 = Args[1], *ShAmtArg = Args[2]; |
6826 | | |
6827 | | // If both operands are undef, the result is undef. |
6828 | 1.84M | if (Q.isUndefValue(Op0) && Q.isUndefValue(Op1)131 ) |
6829 | 2 | return UndefValue::get(F->getReturnType()); |
6830 | | |
6831 | | // If shift amount is undef, assume it is zero. |
6832 | 1.84M | if (Q.isUndefValue(ShAmtArg)) |
6833 | 0 | return Args[IID == Intrinsic::fshl ? 0 : 1]; |
6834 | | |
6835 | 1.84M | const APInt *ShAmtC; |
6836 | 1.84M | if (match(ShAmtArg, m_APInt(ShAmtC))) { |
6837 | | // If there's effectively no shift, return the 1st arg or 2nd arg. |
6838 | 1.79M | APInt BitWidth = APInt(ShAmtC->getBitWidth(), ShAmtC->getBitWidth()); |
6839 | 1.79M | if (ShAmtC->urem(BitWidth).isZero()) |
6840 | 0 | return Args[IID == Intrinsic::fshl ? 0 : 1]; |
6841 | 1.79M | } |
6842 | | |
6843 | | // Rotating zero by anything is zero. |
6844 | 1.84M | if (match(Op0, m_Zero()) && match(Op1, m_Zero())716 ) |
6845 | 0 | return ConstantInt::getNullValue(F->getReturnType()); |
6846 | | |
6847 | | // Rotating -1 by anything is -1. |
6848 | 1.84M | if (match(Op0, m_AllOnes()) && match(Op1, m_AllOnes())2 ) |
6849 | 2 | return ConstantInt::getAllOnesValue(F->getReturnType()); |
6850 | | |
6851 | 1.84M | return nullptr; |
6852 | 1.84M | } |
6853 | 0 | case Intrinsic::experimental_constrained_fma: { |
6854 | 0 | auto *FPI = cast<ConstrainedFPIntrinsic>(Call); |
6855 | 0 | if (Value *V = simplifyFPOp(Args, {}, Q, *FPI->getExceptionBehavior(), |
6856 | 0 | *FPI->getRoundingMode())) |
6857 | 0 | return V; |
6858 | 0 | return nullptr; |
6859 | 0 | } |
6860 | 866k | case Intrinsic::fma: |
6861 | 4.01M | case Intrinsic::fmuladd: { |
6862 | 4.01M | if (Value *V = simplifyFPOp(Args, {}, Q, fp::ebIgnore, |
6863 | 4.01M | RoundingMode::NearestTiesToEven)) |
6864 | 40 | return V; |
6865 | 4.01M | return nullptr; |
6866 | 4.01M | } |
6867 | 0 | case Intrinsic::smul_fix: |
6868 | 0 | case Intrinsic::smul_fix_sat: { |
6869 | 0 | Value *Op0 = Args[0]; |
6870 | 0 | Value *Op1 = Args[1]; |
6871 | 0 | Value *Op2 = Args[2]; |
6872 | 0 | Type *ReturnType = F->getReturnType(); |
6873 | | |
6874 | | // Canonicalize constant operand as Op1 (ConstantFolding handles the case |
6875 | | // when both Op0 and Op1 are constant so we do not care about that special |
6876 | | // case here). |
6877 | 0 | if (isa<Constant>(Op0)) |
6878 | 0 | std::swap(Op0, Op1); |
6879 | | |
6880 | | // X * 0 -> 0 |
6881 | 0 | if (match(Op1, m_Zero())) |
6882 | 0 | return Constant::getNullValue(ReturnType); |
6883 | | |
6884 | | // X * undef -> 0 |
6885 | 0 | if (Q.isUndefValue(Op1)) |
6886 | 0 | return Constant::getNullValue(ReturnType); |
6887 | | |
6888 | | // X * (1 << Scale) -> X |
6889 | 0 | APInt ScaledOne = |
6890 | 0 | APInt::getOneBitSet(ReturnType->getScalarSizeInBits(), |
6891 | 0 | cast<ConstantInt>(Op2)->getZExtValue()); |
6892 | 0 | if (ScaledOne.isNonNegative() && match(Op1, m_SpecificInt(ScaledOne))) |
6893 | 0 | return Op0; |
6894 | | |
6895 | 0 | return nullptr; |
6896 | 0 | } |
6897 | 0 | case Intrinsic::vector_insert: { |
6898 | 0 | Value *Vec = Args[0]; |
6899 | 0 | Value *SubVec = Args[1]; |
6900 | 0 | Value *Idx = Args[2]; |
6901 | 0 | Type *ReturnType = F->getReturnType(); |
6902 | | |
6903 | | // (insert_vector Y, (extract_vector X, 0), 0) -> X |
6904 | | // where: Y is X, or Y is undef |
6905 | 0 | unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue(); |
6906 | 0 | Value *X = nullptr; |
6907 | 0 | if (match(SubVec, |
6908 | 0 | m_Intrinsic<Intrinsic::vector_extract>(m_Value(X), m_Zero())) && |
6909 | 0 | (Q.isUndefValue(Vec) || Vec == X) && IdxN == 0 && |
6910 | 0 | X->getType() == ReturnType) |
6911 | 0 | return X; |
6912 | | |
6913 | 0 | return nullptr; |
6914 | 0 | } |
6915 | 0 | case Intrinsic::experimental_constrained_fadd: { |
6916 | 0 | auto *FPI = cast<ConstrainedFPIntrinsic>(Call); |
6917 | 0 | return simplifyFAddInst(Args[0], Args[1], FPI->getFastMathFlags(), Q, |
6918 | 0 | *FPI->getExceptionBehavior(), |
6919 | 0 | *FPI->getRoundingMode()); |
6920 | 0 | } |
6921 | 0 | case Intrinsic::experimental_constrained_fsub: { |
6922 | 0 | auto *FPI = cast<ConstrainedFPIntrinsic>(Call); |
6923 | 0 | return simplifyFSubInst(Args[0], Args[1], FPI->getFastMathFlags(), Q, |
6924 | 0 | *FPI->getExceptionBehavior(), |
6925 | 0 | *FPI->getRoundingMode()); |
6926 | 0 | } |
6927 | 0 | case Intrinsic::experimental_constrained_fmul: { |
6928 | 0 | auto *FPI = cast<ConstrainedFPIntrinsic>(Call); |
6929 | 0 | return simplifyFMulInst(Args[0], Args[1], FPI->getFastMathFlags(), Q, |
6930 | 0 | *FPI->getExceptionBehavior(), |
6931 | 0 | *FPI->getRoundingMode()); |
6932 | 0 | } |
6933 | 0 | case Intrinsic::experimental_constrained_fdiv: { |
6934 | 0 | auto *FPI = cast<ConstrainedFPIntrinsic>(Call); |
6935 | 0 | return simplifyFDivInst(Args[0], Args[1], FPI->getFastMathFlags(), Q, |
6936 | 0 | *FPI->getExceptionBehavior(), |
6937 | 0 | *FPI->getRoundingMode()); |
6938 | 0 | } |
6939 | 0 | case Intrinsic::experimental_constrained_frem: { |
6940 | 0 | auto *FPI = cast<ConstrainedFPIntrinsic>(Call); |
6941 | 0 | return simplifyFRemInst(Args[0], Args[1], FPI->getFastMathFlags(), Q, |
6942 | 0 | *FPI->getExceptionBehavior(), |
6943 | 0 | *FPI->getRoundingMode()); |
6944 | 0 | } |
6945 | 0 | case Intrinsic::experimental_constrained_ldexp: |
6946 | 0 | return simplifyLdexp(Args[0], Args[1], Q, true); |
6947 | 0 | case Intrinsic::experimental_gc_relocate: { |
6948 | 0 | GCRelocateInst &GCR = *cast<GCRelocateInst>(Call); |
6949 | 0 | Value *DerivedPtr = GCR.getDerivedPtr(); |
6950 | 0 | Value *BasePtr = GCR.getBasePtr(); |
6951 | | |
6952 | | // Undef is undef, even after relocation. |
6953 | 0 | if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) { |
6954 | 0 | return UndefValue::get(GCR.getType()); |
6955 | 0 | } |
6956 | | |
6957 | 0 | if (auto *PT = dyn_cast<PointerType>(GCR.getType())) { |
6958 | | // For now, the assumption is that the relocation of null will be null |
6959 | | // for most any collector. If this ever changes, a corresponding hook |
6960 | | // should be added to GCStrategy and this code should check it first. |
6961 | 0 | if (isa<ConstantPointerNull>(DerivedPtr)) { |
6962 | | // Use null-pointer of gc_relocate's type to replace it. |
6963 | 0 | return ConstantPointerNull::get(PT); |
6964 | 0 | } |
6965 | 0 | } |
6966 | 0 | return nullptr; |
6967 | 0 | } |
6968 | 0 | case Intrinsic::experimental_vp_reverse: { |
6969 | 0 | Value *Vec = Call->getArgOperand(0); |
6970 | 0 | Value *Mask = Call->getArgOperand(1); |
6971 | 0 | Value *EVL = Call->getArgOperand(2); |
6972 | |
|
6973 | 0 | Value *X; |
6974 | | // vp.reverse(vp.reverse(X)) == X (with all ones mask and matching EVL) |
6975 | 0 | if (match(Mask, m_AllOnes()) && |
6976 | 0 | match(Vec, m_Intrinsic<Intrinsic::experimental_vp_reverse>( |
6977 | 0 | m_Value(X), m_AllOnes(), m_Specific(EVL)))) |
6978 | 0 | return X; |
6979 | | |
6980 | | // vp.reverse(splat(X)) -> splat(X) (regardless of mask and EVL) |
6981 | 0 | if (isSplatValue(Vec)) |
6982 | 0 | return Vec; |
6983 | 0 | return nullptr; |
6984 | 0 | } |
6985 | 31.8M | default: |
6986 | 31.8M | return nullptr; |
6987 | 37.7M | } |
6988 | 37.7M | } |
6989 | | |
6990 | | static Value *tryConstantFoldCall(CallBase *Call, Value *Callee, |
6991 | | ArrayRef<Value *> Args, |
6992 | 411M | const SimplifyQuery &Q) { |
6993 | 411M | auto *F = dyn_cast<Function>(Callee); |
6994 | 411M | if (!F || !canConstantFoldCallTo(Call, F)399M ) |
6995 | 385M | return nullptr; |
6996 | | |
6997 | 26.4M | SmallVector<Constant *, 4> ConstantArgs; |
6998 | 26.4M | ConstantArgs.reserve(Args.size()); |
6999 | 26.7M | for (Value *Arg : Args) { |
7000 | 26.7M | Constant *C = dyn_cast<Constant>(Arg); |
7001 | 26.7M | if (!C) { |
7002 | 26.3M | if (isa<MetadataAsValue>(Arg)) |
7003 | 0 | continue; |
7004 | 26.3M | return nullptr; |
7005 | 26.3M | } |
7006 | 383k | ConstantArgs.push_back(C); |
7007 | 383k | } |
7008 | | |
7009 | 82.7k | return ConstantFoldCall(Call, F, ConstantArgs, Q.TLI); |
7010 | 26.4M | } |
7011 | | |
7012 | | Value *llvm::simplifyCall(CallBase *Call, Value *Callee, ArrayRef<Value *> Args, |
7013 | 411M | const SimplifyQuery &Q) { |
7014 | | // Args should not contain operand bundle operands. |
7015 | 411M | assert(Call->arg_size() == Args.size()); |
7016 | | |
7017 | | // musttail calls can only be simplified if they are also DCEd. |
7018 | | // As we can't guarantee this here, don't simplify them. |
7019 | 411M | if (Call->isMustTailCall()) |
7020 | 6.31k | return nullptr; |
7021 | | |
7022 | | // call undef -> poison |
7023 | | // call null -> poison |
7024 | 411M | if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee)) |
7025 | 976 | return PoisonValue::get(Call->getType()); |
7026 | | |
7027 | 411M | if (Value *V = tryConstantFoldCall(Call, Callee, Args, Q)) |
7028 | 65.8k | return V; |
7029 | | |
7030 | 411M | auto *F = dyn_cast<Function>(Callee); |
7031 | 411M | if (F && F->isIntrinsic()399M ) |
7032 | 174M | if (Value *Ret = simplifyIntrinsic(Call, Callee, Args, Q)) |
7033 | 65.9k | return Ret; |
7034 | | |
7035 | 411M | return nullptr; |
7036 | 411M | } |
7037 | | |
7038 | 0 | Value *llvm::simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q) { |
7039 | 0 | assert(isa<ConstrainedFPIntrinsic>(Call)); |
7040 | 0 | SmallVector<Value *, 4> Args(Call->args()); |
7041 | 0 | if (Value *V = tryConstantFoldCall(Call, Call->getCalledOperand(), Args, Q)) |
7042 | 0 | return V; |
7043 | 0 | if (Value *Ret = simplifyIntrinsic(Call, Call->getCalledOperand(), Args, Q)) |
7044 | 0 | return Ret; |
7045 | 0 | return nullptr; |
7046 | 0 | } |
7047 | | |
7048 | | /// Given operands for a Freeze, see if we can fold the result. |
7049 | 956k | static Value *simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) { |
7050 | | // Use a utility function defined in ValueTracking. |
7051 | 956k | if (llvm::isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT)) |
7052 | 15.5k | return Op0; |
7053 | | // We have room for improvement. |
7054 | 940k | return nullptr; |
7055 | 956k | } |
7056 | | |
7057 | 956k | Value *llvm::simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) { |
7058 | 956k | return ::simplifyFreezeInst(Op0, Q); |
7059 | 956k | } |
7060 | | |
7061 | | Value *llvm::simplifyLoadInst(LoadInst *LI, Value *PtrOp, |
7062 | 721M | const SimplifyQuery &Q) { |
7063 | 721M | if (LI->isVolatile()) |
7064 | 2.09M | return nullptr; |
7065 | | |
7066 | 719M | if (auto *PtrOpC = dyn_cast<Constant>(PtrOp)) |
7067 | 28.8M | return ConstantFoldLoadFromConstPtr(PtrOpC, LI->getType(), Q.DL); |
7068 | | |
7069 | | // We can only fold the load if it is from a constant global with definitive |
7070 | | // initializer. Skip expensive logic if this is not the case. |
7071 | 690M | auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(PtrOp)); |
7072 | 690M | if (!GV || !GV->isConstant()6.16M || !GV->hasDefinitiveInitializer()4.61M ) |
7073 | 686M | return nullptr; |
7074 | | |
7075 | | // If GlobalVariable's initializer is uniform, then return the constant |
7076 | | // regardless of its offset. |
7077 | 4.29M | if (Constant *C = ConstantFoldLoadFromUniformValue(GV->getInitializer(), |
7078 | 4.29M | LI->getType(), Q.DL)) |
7079 | 75 | return C; |
7080 | | |
7081 | | // Try to convert operand into a constant by stripping offsets while looking |
7082 | | // through invariant.group intrinsics. |
7083 | 4.29M | APInt Offset(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()), 0); |
7084 | 4.29M | PtrOp = PtrOp->stripAndAccumulateConstantOffsets( |
7085 | 4.29M | Q.DL, Offset, /* AllowNonInbounts */ true, |
7086 | 4.29M | /* AllowInvariantGroup */ true); |
7087 | 4.29M | if (PtrOp == GV) { |
7088 | | // Index size may have changed due to address space casts. |
7089 | 126 | Offset = Offset.sextOrTrunc(Q.DL.getIndexTypeSizeInBits(PtrOp->getType())); |
7090 | 126 | return ConstantFoldLoadFromConstPtr(GV, LI->getType(), std::move(Offset), |
7091 | 126 | Q.DL); |
7092 | 126 | } |
7093 | | |
7094 | 4.29M | return nullptr; |
7095 | 4.29M | } |
7096 | | |
7097 | | /// See if we can compute a simplified version of this instruction. |
7098 | | /// If not, this returns null. |
7099 | | |
7100 | | static Value *simplifyInstructionWithOperands(Instruction *I, |
7101 | | ArrayRef<Value *> NewOps, |
7102 | | const SimplifyQuery &SQ, |
7103 | 2.92G | unsigned MaxRecurse) { |
7104 | 2.92G | assert(I->getFunction() && "instruction should be inserted in a function"); |
7105 | 2.92G | assert((!SQ.CxtI || SQ.CxtI->getFunction() == I->getFunction()) && |
7106 | 2.92G | "context instruction should be in the same function"); |
7107 | | |
7108 | 2.92G | const SimplifyQuery Q = SQ.CxtI ? SQ322M : SQ.getWithInstruction(I)2.60G ; |
7109 | | |
7110 | 2.92G | switch (I->getOpcode()) { |
7111 | 753M | default: |
7112 | 753M | if (llvm::all_of(NewOps, [](Value *V) { return isa<Constant>(V); }724M )) { |
7113 | 119M | SmallVector<Constant *, 8> NewConstOps(NewOps.size()); |
7114 | 119M | transform(NewOps, NewConstOps.begin(), |
7115 | 119M | [](Value *V) { return cast<Constant>(V); }17.4M ); |
7116 | 119M | return ConstantFoldInstOperands(I, NewConstOps, Q.DL, Q.TLI); |
7117 | 119M | } |
7118 | 634M | return nullptr; |
7119 | 595k | case Instruction::FNeg: |
7120 | 595k | return simplifyFNegInst(NewOps[0], I->getFastMathFlags(), Q, MaxRecurse); |
7121 | 3.50M | case Instruction::FAdd: |
7122 | 3.50M | return simplifyFAddInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q, |
7123 | 3.50M | MaxRecurse); |
7124 | 68.3M | case Instruction::Add: |
7125 | 68.3M | return simplifyAddInst( |
7126 | 68.3M | NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)), |
7127 | 68.3M | Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse); |
7128 | 1.46M | case Instruction::FSub: |
7129 | 1.46M | return simplifyFSubInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q, |
7130 | 1.46M | MaxRecurse); |
7131 | 21.5M | case Instruction::Sub: |
7132 | 21.5M | return simplifySubInst( |
7133 | 21.5M | NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)), |
7134 | 21.5M | Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse); |
7135 | 5.12M | case Instruction::FMul: |
7136 | 5.12M | return simplifyFMulInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q, |
7137 | 5.12M | MaxRecurse); |
7138 | 8.50M | case Instruction::Mul: |
7139 | 8.50M | return simplifyMulInst( |
7140 | 8.50M | NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)), |
7141 | 8.50M | Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse); |
7142 | 2.37M | case Instruction::SDiv: |
7143 | 2.37M | return simplifySDivInst(NewOps[0], NewOps[1], |
7144 | 2.37M | Q.IIQ.isExact(cast<BinaryOperator>(I)), Q, |
7145 | 2.37M | MaxRecurse); |
7146 | 1.25M | case Instruction::UDiv: |
7147 | 1.25M | return simplifyUDivInst(NewOps[0], NewOps[1], |
7148 | 1.25M | Q.IIQ.isExact(cast<BinaryOperator>(I)), Q, |
7149 | 1.25M | MaxRecurse); |
7150 | 969k | case Instruction::FDiv: |
7151 | 969k | return simplifyFDivInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q, |
7152 | 969k | MaxRecurse); |
7153 | 295k | case Instruction::SRem: |
7154 | 295k | return simplifySRemInst(NewOps[0], NewOps[1], Q, MaxRecurse); |
7155 | 1.40M | case Instruction::URem: |
7156 | 1.40M | return simplifyURemInst(NewOps[0], NewOps[1], Q, MaxRecurse); |
7157 | 1.29k | case Instruction::FRem: |
7158 | 1.29k | return simplifyFRemInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q, |
7159 | 1.29k | MaxRecurse); |
7160 | 14.3M | case Instruction::Shl: |
7161 | 14.3M | return simplifyShlInst( |
7162 | 14.3M | NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)), |
7163 | 14.3M | Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q, MaxRecurse); |
7164 | 11.6M | case Instruction::LShr: |
7165 | 11.6M | return simplifyLShrInst(NewOps[0], NewOps[1], |
7166 | 11.6M | Q.IIQ.isExact(cast<BinaryOperator>(I)), Q, |
7167 | 11.6M | MaxRecurse); |
7168 | 4.16M | case Instruction::AShr: |
7169 | 4.16M | return simplifyAShrInst(NewOps[0], NewOps[1], |
7170 | 4.16M | Q.IIQ.isExact(cast<BinaryOperator>(I)), Q, |
7171 | 4.16M | MaxRecurse); |
7172 | 34.6M | case Instruction::And: |
7173 | 34.6M | return simplifyAndInst(NewOps[0], NewOps[1], Q, MaxRecurse); |
7174 | 11.3M | case Instruction::Or: |
7175 | 11.3M | return simplifyOrInst(NewOps[0], NewOps[1], Q, MaxRecurse); |
7176 | 6.44M | case Instruction::Xor: |
7177 | 6.44M | return simplifyXorInst(NewOps[0], NewOps[1], Q, MaxRecurse); |
7178 | 235M | case Instruction::ICmp: |
7179 | 235M | return simplifyICmpInst(cast<ICmpInst>(I)->getCmpPredicate(), NewOps[0], |
7180 | 235M | NewOps[1], Q, MaxRecurse); |
7181 | 3.51M | case Instruction::FCmp: |
7182 | 3.51M | return simplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), NewOps[0], |
7183 | 3.51M | NewOps[1], I->getFastMathFlags(), Q, MaxRecurse); |
7184 | 18.1M | case Instruction::Select: |
7185 | 18.1M | return simplifySelectInst(NewOps[0], NewOps[1], NewOps[2], Q, MaxRecurse); |
7186 | 390M | case Instruction::GetElementPtr: { |
7187 | 390M | auto *GEPI = cast<GetElementPtrInst>(I); |
7188 | 390M | return simplifyGEPInst(GEPI->getSourceElementType(), NewOps[0], |
7189 | 390M | ArrayRef(NewOps).slice(1), GEPI->getNoWrapFlags(), Q, |
7190 | 390M | MaxRecurse); |
7191 | 753M | } |
7192 | 10.7M | case Instruction::InsertValue: { |
7193 | 10.7M | InsertValueInst *IV = cast<InsertValueInst>(I); |
7194 | 10.7M | return simplifyInsertValueInst(NewOps[0], NewOps[1], IV->getIndices(), Q, |
7195 | 10.7M | MaxRecurse); |
7196 | 753M | } |
7197 | 1.38M | case Instruction::InsertElement: |
7198 | 1.38M | return simplifyInsertElementInst(NewOps[0], NewOps[1], NewOps[2], Q); |
7199 | 30.9M | case Instruction::ExtractValue: { |
7200 | 30.9M | auto *EVI = cast<ExtractValueInst>(I); |
7201 | 30.9M | return simplifyExtractValueInst(NewOps[0], EVI->getIndices(), Q, |
7202 | 30.9M | MaxRecurse); |
7203 | 753M | } |
7204 | 767k | case Instruction::ExtractElement: |
7205 | 767k | return simplifyExtractElementInst(NewOps[0], NewOps[1], Q, MaxRecurse); |
7206 | 1.66M | case Instruction::ShuffleVector: { |
7207 | 1.66M | auto *SVI = cast<ShuffleVectorInst>(I); |
7208 | 1.66M | return simplifyShuffleVectorInst(NewOps[0], NewOps[1], |
7209 | 1.66M | SVI->getShuffleMask(), SVI->getType(), Q, |
7210 | 1.66M | MaxRecurse); |
7211 | 753M | } |
7212 | 424M | case Instruction::PHI: |
7213 | 424M | return simplifyPHINode(cast<PHINode>(I), NewOps, Q); |
7214 | 330M | case Instruction::Call: |
7215 | 330M | return simplifyCall( |
7216 | 330M | cast<CallInst>(I), NewOps.back(), |
7217 | 330M | NewOps.drop_back(1 + cast<CallInst>(I)->getNumTotalBundleOperands()), Q); |
7218 | 413k | case Instruction::Freeze: |
7219 | 413k | return llvm::simplifyFreezeInst(NewOps[0], Q); |
7220 | 933M | #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: |
7221 | 103M | #include "llvm/IR/Instruction.def"413k |
7222 | 103M | #undef HANDLE_CAST_INST |
7223 | 103M | return simplifyCastInst(I->getOpcode(), NewOps[0], I->getType(), Q, |
7224 | 103M | MaxRecurse); |
7225 | 53.7M | case Instruction::Alloca: |
7226 | | // No simplifications for Alloca and it can't be constant folded. |
7227 | 53.7M | return nullptr; |
7228 | 368M | case Instruction::Load: |
7229 | 368M | return simplifyLoadInst(cast<LoadInst>(I), NewOps[0], Q); |
7230 | 2.92G | } |
7231 | 2.92G | } |
7232 | | |
7233 | | Value *llvm::simplifyInstructionWithOperands(Instruction *I, |
7234 | | ArrayRef<Value *> NewOps, |
7235 | 22.1M | const SimplifyQuery &SQ) { |
7236 | 22.1M | assert(NewOps.size() == I->getNumOperands() && |
7237 | 22.1M | "Number of operands should match the instruction!"); |
7238 | 22.1M | return ::simplifyInstructionWithOperands(I, NewOps, SQ, RecursionLimit); |
7239 | 22.1M | } |
7240 | | |
7241 | 2.89G | Value *llvm::simplifyInstruction(Instruction *I, const SimplifyQuery &SQ) { |
7242 | 2.89G | SmallVector<Value *, 8> Ops(I->operands()); |
7243 | 2.89G | Value *Result = ::simplifyInstructionWithOperands(I, Ops, SQ, RecursionLimit); |
7244 | | |
7245 | | /// If called on unreachable code, the instruction may simplify to itself. |
7246 | | /// Make life easier for users by detecting that case here, and returning a |
7247 | | /// safe value instead. |
7248 | 2.89G | return Result == I ? PoisonValue::get(I->getType())2 : Result2.89G ; |
7249 | 2.89G | } |
7250 | | |
7251 | | /// Implementation of recursive simplification through an instruction's |
7252 | | /// uses. |
7253 | | /// |
7254 | | /// This is the common implementation of the recursive simplification routines. |
7255 | | /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to |
7256 | | /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of |
7257 | | /// instructions to process and attempt to simplify it using |
7258 | | /// InstructionSimplify. Recursively visited users which could not be |
7259 | | /// simplified themselves are to the optional UnsimplifiedUsers set for |
7260 | | /// further processing by the caller. |
7261 | | /// |
7262 | | /// This routine returns 'true' only when *it* simplifies something. The passed |
7263 | | /// in simplified value does not count toward this. |
7264 | | static bool replaceAndRecursivelySimplifyImpl( |
7265 | | Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI, |
7266 | | const DominatorTree *DT, AssumptionCache *AC, |
7267 | 47.6k | SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr) { |
7268 | 47.6k | bool Simplified = false; |
7269 | 47.6k | SmallSetVector<Instruction *, 8> Worklist; |
7270 | 47.6k | const DataLayout &DL = I->getDataLayout(); |
7271 | | |
7272 | | // If we have an explicit value to collapse to, do that round of the |
7273 | | // simplification loop by hand initially. |
7274 | 47.6k | if (SimpleV) { |
7275 | 47.6k | for (User *U : I->users()) |
7276 | 47.9k | if (U != I) |
7277 | 47.9k | Worklist.insert(cast<Instruction>(U)); |
7278 | | |
7279 | | // Replace the instruction with its simplified value. |
7280 | 47.6k | I->replaceAllUsesWith(SimpleV); |
7281 | | |
7282 | 47.6k | if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects()) |
7283 | 47.6k | I->eraseFromParent(); |
7284 | 47.6k | } else { |
7285 | 0 | Worklist.insert(I); |
7286 | 0 | } |
7287 | | |
7288 | | // Note that we must test the size on each iteration, the worklist can grow. |
7289 | 125k | for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx78.2k ) { |
7290 | 78.2k | I = Worklist[Idx]; |
7291 | | |
7292 | | // See if this instruction simplifies. |
7293 | 78.2k | SimpleV = simplifyInstruction(I, {DL, TLI, DT, AC}); |
7294 | 78.2k | if (!SimpleV) { |
7295 | 55.3k | if (UnsimplifiedUsers) |
7296 | 55.3k | UnsimplifiedUsers->insert(I); |
7297 | 55.3k | continue; |
7298 | 55.3k | } |
7299 | | |
7300 | 22.9k | Simplified = true; |
7301 | | |
7302 | | // Stash away all the uses of the old instruction so we can check them for |
7303 | | // recursive simplifications after a RAUW. This is cheaper than checking all |
7304 | | // uses of To on the recursive step in most cases. |
7305 | 22.9k | for (User *U : I->users()) |
7306 | 30.3k | Worklist.insert(cast<Instruction>(U)); |
7307 | | |
7308 | | // Replace the instruction with its simplified value. |
7309 | 22.9k | I->replaceAllUsesWith(SimpleV); |
7310 | | |
7311 | 22.9k | if (!I->isEHPad() && !I->isTerminator() && !I->mayHaveSideEffects()) |
7312 | 22.9k | I->eraseFromParent(); |
7313 | 22.9k | } |
7314 | 47.6k | return Simplified; |
7315 | 47.6k | } |
7316 | | |
7317 | | bool llvm::replaceAndRecursivelySimplify( |
7318 | | Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI, |
7319 | | const DominatorTree *DT, AssumptionCache *AC, |
7320 | 47.6k | SmallSetVector<Instruction *, 8> *UnsimplifiedUsers) { |
7321 | 47.6k | assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!"); |
7322 | 47.6k | assert(SimpleV && "Must provide a simplified value."); |
7323 | 47.6k | return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC, |
7324 | 47.6k | UnsimplifiedUsers); |
7325 | 47.6k | } |
7326 | | |
7327 | | namespace llvm { |
7328 | 0 | const SimplifyQuery getBestSimplifyQuery(Pass &P, Function &F) { |
7329 | 0 | auto *DTWP = P.getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
7330 | 0 | auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
7331 | 0 | auto *TLIWP = P.getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>(); |
7332 | 0 | auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr; |
7333 | 0 | auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>(); |
7334 | 0 | auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr; |
7335 | 0 | return {F.getDataLayout(), TLI, DT, AC}; |
7336 | 0 | } |
7337 | | |
7338 | | const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &AR, |
7339 | 7.22M | const DataLayout &DL) { |
7340 | 7.22M | return {DL, &AR.TLI, &AR.DT, &AR.AC}; |
7341 | 7.22M | } |
7342 | | |
7343 | | template <class T, class... TArgs> |
7344 | | const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &AM, |
7345 | 41.2M | Function &F) { |
7346 | 41.2M | auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F); |
7347 | 41.2M | auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F); |
7348 | 41.2M | auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F); |
7349 | 41.2M | return {F.getDataLayout(), TLI, DT, AC}; |
7350 | 41.2M | } |
7351 | | template const SimplifyQuery getBestSimplifyQuery(AnalysisManager<Function> &, |
7352 | | Function &); |
7353 | | |
7354 | 3.90G | bool SimplifyQuery::isUndefValue(Value *V) const { |
7355 | 3.90G | if (!CanUseUndef) |
7356 | 137M | return false; |
7357 | | |
7358 | 3.77G | return match(V, m_Undef()); |
7359 | 3.90G | } |
7360 | | |
7361 | | } // namespace llvm |
7362 | | |
7363 | 0 | void InstSimplifyFolder::anchor() {} |