LLVM 20.0.0git
InstCombineAndOrXor.cpp
Go to the documentation of this file.
1//===- InstCombineAndOrXor.cpp --------------------------------------------===//
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 the visitAnd, visitOr, and visitXor functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
17#include "llvm/IR/Intrinsics.h"
21
22using namespace llvm;
23using namespace PatternMatch;
24
25#define DEBUG_TYPE "instcombine"
26
27/// This is the complement of getICmpCode, which turns an opcode and two
28/// operands into either a constant true or false, or a brand new ICmp
29/// instruction. The sign is passed in to determine which kind of predicate to
30/// use in the new icmp instruction.
31static Value *getNewICmpValue(unsigned Code, bool Sign, Value *LHS, Value *RHS,
32 InstCombiner::BuilderTy &Builder) {
33 ICmpInst::Predicate NewPred;
34 if (Constant *TorF = getPredForICmpCode(Code, Sign, LHS->getType(), NewPred))
35 return TorF;
36 return Builder.CreateICmp(NewPred, LHS, RHS);
37}
38
39/// This is the complement of getFCmpCode, which turns an opcode and two
40/// operands into either a FCmp instruction, or a true/false constant.
41static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS,
42 InstCombiner::BuilderTy &Builder, FMFSource FMF) {
43 FCmpInst::Predicate NewPred;
44 if (Constant *TorF = getPredForFCmpCode(Code, LHS->getType(), NewPred))
45 return TorF;
46 return Builder.CreateFCmpFMF(NewPred, LHS, RHS, FMF);
47}
48
49/// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
50/// (V < Lo || V >= Hi). This method expects that Lo < Hi. IsSigned indicates
51/// whether to treat V, Lo, and Hi as signed or not.
53 const APInt &Hi, bool isSigned,
54 bool Inside) {
55 assert((isSigned ? Lo.slt(Hi) : Lo.ult(Hi)) &&
56 "Lo is not < Hi in range emission code!");
57
58 Type *Ty = V->getType();
59
60 // V >= Min && V < Hi --> V < Hi
61 // V < Min || V >= Hi --> V >= Hi
63 if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) {
64 Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred;
65 return Builder.CreateICmp(Pred, V, ConstantInt::get(Ty, Hi));
66 }
67
68 // V >= Lo && V < Hi --> V - Lo u< Hi - Lo
69 // V < Lo || V >= Hi --> V - Lo u>= Hi - Lo
70 Value *VMinusLo =
71 Builder.CreateSub(V, ConstantInt::get(Ty, Lo), V->getName() + ".off");
72 Constant *HiMinusLo = ConstantInt::get(Ty, Hi - Lo);
73 return Builder.CreateICmp(Pred, VMinusLo, HiMinusLo);
74}
75
76/// Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns
77/// that can be simplified.
78/// One of A and B is considered the mask. The other is the value. This is
79/// described as the "AMask" or "BMask" part of the enum. If the enum contains
80/// only "Mask", then both A and B can be considered masks. If A is the mask,
81/// then it was proven that (A & C) == C. This is trivial if C == A or C == 0.
82/// If both A and C are constants, this proof is also easy.
83/// For the following explanations, we assume that A is the mask.
84///
85/// "AllOnes" declares that the comparison is true only if (A & B) == A or all
86/// bits of A are set in B.
87/// Example: (icmp eq (A & 3), 3) -> AMask_AllOnes
88///
89/// "AllZeros" declares that the comparison is true only if (A & B) == 0 or all
90/// bits of A are cleared in B.
91/// Example: (icmp eq (A & 3), 0) -> Mask_AllZeroes
92///
93/// "Mixed" declares that (A & B) == C and C might or might not contain any
94/// number of one bits and zero bits.
95/// Example: (icmp eq (A & 3), 1) -> AMask_Mixed
96///
97/// "Not" means that in above descriptions "==" should be replaced by "!=".
98/// Example: (icmp ne (A & 3), 3) -> AMask_NotAllOnes
99///
100/// If the mask A contains a single bit, then the following is equivalent:
101/// (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
102/// (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
113 BMask_NotMixed = 512
115
116/// Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C)
117/// satisfies.
118static unsigned getMaskedICmpType(Value *A, Value *B, Value *C,
119 ICmpInst::Predicate Pred) {
120 const APInt *ConstA = nullptr, *ConstB = nullptr, *ConstC = nullptr;
121 match(A, m_APInt(ConstA));
122 match(B, m_APInt(ConstB));
123 match(C, m_APInt(ConstC));
124 bool IsEq = (Pred == ICmpInst::ICMP_EQ);
125 bool IsAPow2 = ConstA && ConstA->isPowerOf2();
126 bool IsBPow2 = ConstB && ConstB->isPowerOf2();
127 unsigned MaskVal = 0;
128 if (ConstC && ConstC->isZero()) {
129 // if C is zero, then both A and B qualify as mask
130 MaskVal |= (IsEq ? (Mask_AllZeros | AMask_Mixed | BMask_Mixed)
132 if (IsAPow2)
133 MaskVal |= (IsEq ? (AMask_NotAllOnes | AMask_NotMixed)
135 if (IsBPow2)
136 MaskVal |= (IsEq ? (BMask_NotAllOnes | BMask_NotMixed)
138 return MaskVal;
139 }
140
141 if (A == C) {
142 MaskVal |= (IsEq ? (AMask_AllOnes | AMask_Mixed)
144 if (IsAPow2)
145 MaskVal |= (IsEq ? (Mask_NotAllZeros | AMask_NotMixed)
147 } else if (ConstA && ConstC && ConstC->isSubsetOf(*ConstA)) {
148 MaskVal |= (IsEq ? AMask_Mixed : AMask_NotMixed);
149 }
150
151 if (B == C) {
152 MaskVal |= (IsEq ? (BMask_AllOnes | BMask_Mixed)
154 if (IsBPow2)
155 MaskVal |= (IsEq ? (Mask_NotAllZeros | BMask_NotMixed)
157 } else if (ConstB && ConstC && ConstC->isSubsetOf(*ConstB)) {
158 MaskVal |= (IsEq ? BMask_Mixed : BMask_NotMixed);
159 }
160
161 return MaskVal;
162}
163
164/// Convert an analysis of a masked ICmp into its equivalent if all boolean
165/// operations had the opposite sense. Since each "NotXXX" flag (recording !=)
166/// is adjacent to the corresponding normal flag (recording ==), this just
167/// involves swapping those bits over.
168static unsigned conjugateICmpMask(unsigned Mask) {
169 unsigned NewMask;
170 NewMask = (Mask & (AMask_AllOnes | BMask_AllOnes | Mask_AllZeros |
172 << 1;
173
174 NewMask |= (Mask & (AMask_NotAllOnes | BMask_NotAllOnes | Mask_NotAllZeros |
176 >> 1;
177
178 return NewMask;
179}
180
181// Adapts the external decomposeBitTestICmp for local use.
183 Value *&X, Value *&Y, Value *&Z) {
184 auto Res = llvm::decomposeBitTest(Cond, /*LookThroughTrunc=*/true,
185 /*AllowNonZeroC=*/true);
186 if (!Res)
187 return false;
188
189 Pred = Res->Pred;
190 X = Res->X;
191 Y = ConstantInt::get(X->getType(), Res->Mask);
192 Z = ConstantInt::get(X->getType(), Res->C);
193 return true;
194}
195
196/// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E).
197/// Return the pattern classes (from MaskedICmpType) for the left hand side and
198/// the right hand side as a pair.
199/// LHS and RHS are the left hand side and the right hand side ICmps and PredL
200/// and PredR are their predicates, respectively.
201static std::optional<std::pair<unsigned, unsigned>>
203 Value *LHS, Value *RHS, ICmpInst::Predicate &PredL,
204 ICmpInst::Predicate &PredR) {
205
206 // Here comes the tricky part:
207 // LHS might be of the form L11 & L12 == X, X == L21 & L22,
208 // and L11 & L12 == L21 & L22. The same goes for RHS.
209 // Now we must find those components L** and R**, that are equal, so
210 // that we can extract the parameters A, B, C, D, and E for the canonical
211 // above.
212
213 // Check whether the icmp can be decomposed into a bit test.
214 Value *L1, *L11, *L12, *L2, *L21, *L22;
215 if (decomposeBitTestICmp(LHS, PredL, L11, L12, L2)) {
216 L21 = L22 = L1 = nullptr;
217 } else {
218 auto *LHSCMP = dyn_cast<ICmpInst>(LHS);
219 if (!LHSCMP)
220 return std::nullopt;
221
222 // Don't allow pointers. Splat vectors are fine.
223 if (!LHSCMP->getOperand(0)->getType()->isIntOrIntVectorTy())
224 return std::nullopt;
225
226 PredL = LHSCMP->getPredicate();
227 L1 = LHSCMP->getOperand(0);
228 L2 = LHSCMP->getOperand(1);
229 // Look for ANDs in the LHS icmp.
230 if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
231 // Any icmp can be viewed as being trivially masked; if it allows us to
232 // remove one, it's worth it.
233 L11 = L1;
235 }
236
237 if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
238 L21 = L2;
240 }
241 }
242
243 // Bail if LHS was a icmp that can't be decomposed into an equality.
244 if (!ICmpInst::isEquality(PredL))
245 return std::nullopt;
246
247 Value *R11, *R12, *R2;
248 if (decomposeBitTestICmp(RHS, PredR, R11, R12, R2)) {
249 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
250 A = R11;
251 D = R12;
252 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
253 A = R12;
254 D = R11;
255 } else {
256 return std::nullopt;
257 }
258 E = R2;
259 } else {
260 auto *RHSCMP = dyn_cast<ICmpInst>(RHS);
261 if (!RHSCMP)
262 return std::nullopt;
263 // Don't allow pointers. Splat vectors are fine.
264 if (!RHSCMP->getOperand(0)->getType()->isIntOrIntVectorTy())
265 return std::nullopt;
266
267 PredR = RHSCMP->getPredicate();
268
269 Value *R1 = RHSCMP->getOperand(0);
270 R2 = RHSCMP->getOperand(1);
271 bool Ok = false;
272 if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) {
273 // As before, model no mask as a trivial mask if it'll let us do an
274 // optimization.
275 R11 = R1;
277 }
278
279 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
280 A = R11;
281 D = R12;
282 E = R2;
283 Ok = true;
284 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
285 A = R12;
286 D = R11;
287 E = R2;
288 Ok = true;
289 }
290
291 // Avoid matching against the -1 value we created for unmasked operand.
292 if (Ok && match(A, m_AllOnes()))
293 Ok = false;
294
295 // Look for ANDs on the right side of the RHS icmp.
296 if (!Ok) {
297 if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) {
298 R11 = R2;
299 R12 = Constant::getAllOnesValue(R2->getType());
300 }
301
302 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
303 A = R11;
304 D = R12;
305 E = R1;
306 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
307 A = R12;
308 D = R11;
309 E = R1;
310 } else {
311 return std::nullopt;
312 }
313 }
314 }
315
316 // Bail if RHS was a icmp that can't be decomposed into an equality.
317 if (!ICmpInst::isEquality(PredR))
318 return std::nullopt;
319
320 if (L11 == A) {
321 B = L12;
322 C = L2;
323 } else if (L12 == A) {
324 B = L11;
325 C = L2;
326 } else if (L21 == A) {
327 B = L22;
328 C = L1;
329 } else if (L22 == A) {
330 B = L21;
331 C = L1;
332 }
333
334 unsigned LeftType = getMaskedICmpType(A, B, C, PredL);
335 unsigned RightType = getMaskedICmpType(A, D, E, PredR);
336 return std::optional<std::pair<unsigned, unsigned>>(
337 std::make_pair(LeftType, RightType));
338}
339
340/// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single
341/// (icmp(A & X) ==/!= Y), where the left-hand side is of type Mask_NotAllZeros
342/// and the right hand side is of type BMask_Mixed. For example,
343/// (icmp (A & 12) != 0) & (icmp (A & 15) == 8) -> (icmp (A & 15) == 8).
344/// Also used for logical and/or, must be poison safe.
346 Value *LHS, Value *RHS, bool IsAnd, Value *A, Value *B, Value *D, Value *E,
348 InstCombiner::BuilderTy &Builder) {
349 // We are given the canonical form:
350 // (icmp ne (A & B), 0) & (icmp eq (A & D), E).
351 // where D & E == E.
352 //
353 // If IsAnd is false, we get it in negated form:
354 // (icmp eq (A & B), 0) | (icmp ne (A & D), E) ->
355 // !((icmp ne (A & B), 0) & (icmp eq (A & D), E)).
356 //
357 // We currently handle the case of B, C, D, E are constant.
358 //
359 const APInt *BCst, *DCst, *OrigECst;
360 if (!match(B, m_APInt(BCst)) || !match(D, m_APInt(DCst)) ||
361 !match(E, m_APInt(OrigECst)))
362 return nullptr;
363
365
366 // Update E to the canonical form when D is a power of two and RHS is
367 // canonicalized as,
368 // (icmp ne (A & D), 0) -> (icmp eq (A & D), D) or
369 // (icmp ne (A & D), D) -> (icmp eq (A & D), 0).
370 APInt ECst = *OrigECst;
371 if (PredR != NewCC)
372 ECst ^= *DCst;
373
374 // If B or D is zero, skip because if LHS or RHS can be trivially folded by
375 // other folding rules and this pattern won't apply any more.
376 if (*BCst == 0 || *DCst == 0)
377 return nullptr;
378
379 // If B and D don't intersect, ie. (B & D) == 0, try to fold isNaN idiom:
380 // (icmp ne (A & FractionBits), 0) & (icmp eq (A & ExpBits), ExpBits)
381 // -> isNaN(A)
382 // Otherwise, we cannot deduce anything from it.
383 if (!BCst->intersects(*DCst)) {
384 Value *Src;
385 if (*DCst == ECst && match(A, m_ElementWiseBitCast(m_Value(Src))) &&
387 Attribute::StrictFP)) {
388 Type *Ty = Src->getType()->getScalarType();
389 if (!Ty->isIEEELikeFPTy())
390 return nullptr;
391
393 if (ECst != ExpBits)
394 return nullptr;
395 APInt FractionBits = ~ExpBits;
396 FractionBits.clearSignBit();
397 if (*BCst != FractionBits)
398 return nullptr;
399
400 return Builder.CreateFCmp(IsAnd ? FCmpInst::FCMP_UNO : FCmpInst::FCMP_ORD,
401 Src, ConstantFP::getZero(Src->getType()));
402 }
403 return nullptr;
404 }
405
406 // If the following two conditions are met:
407 //
408 // 1. mask B covers only a single bit that's not covered by mask D, that is,
409 // (B & (B ^ D)) is a power of 2 (in other words, B minus the intersection of
410 // B and D has only one bit set) and,
411 //
412 // 2. RHS (and E) indicates that the rest of B's bits are zero (in other
413 // words, the intersection of B and D is zero), that is, ((B & D) & E) == 0
414 //
415 // then that single bit in B must be one and thus the whole expression can be
416 // folded to
417 // (A & (B | D)) == (B & (B ^ D)) | E.
418 //
419 // For example,
420 // (icmp ne (A & 12), 0) & (icmp eq (A & 7), 1) -> (icmp eq (A & 15), 9)
421 // (icmp ne (A & 15), 0) & (icmp eq (A & 7), 0) -> (icmp eq (A & 15), 8)
422 if ((((*BCst & *DCst) & ECst) == 0) &&
423 (*BCst & (*BCst ^ *DCst)).isPowerOf2()) {
424 APInt BorD = *BCst | *DCst;
425 APInt BandBxorDorE = (*BCst & (*BCst ^ *DCst)) | ECst;
426 Value *NewMask = ConstantInt::get(A->getType(), BorD);
427 Value *NewMaskedValue = ConstantInt::get(A->getType(), BandBxorDorE);
428 Value *NewAnd = Builder.CreateAnd(A, NewMask);
429 return Builder.CreateICmp(NewCC, NewAnd, NewMaskedValue);
430 }
431
432 auto IsSubSetOrEqual = [](const APInt *C1, const APInt *C2) {
433 return (*C1 & *C2) == *C1;
434 };
435 auto IsSuperSetOrEqual = [](const APInt *C1, const APInt *C2) {
436 return (*C1 & *C2) == *C2;
437 };
438
439 // In the following, we consider only the cases where B is a superset of D, B
440 // is a subset of D, or B == D because otherwise there's at least one bit
441 // covered by B but not D, in which case we can't deduce much from it, so
442 // no folding (aside from the single must-be-one bit case right above.)
443 // For example,
444 // (icmp ne (A & 14), 0) & (icmp eq (A & 3), 1) -> no folding.
445 if (!IsSubSetOrEqual(BCst, DCst) && !IsSuperSetOrEqual(BCst, DCst))
446 return nullptr;
447
448 // At this point, either B is a superset of D, B is a subset of D or B == D.
449
450 // If E is zero, if B is a subset of (or equal to) D, LHS and RHS contradict
451 // and the whole expression becomes false (or true if negated), otherwise, no
452 // folding.
453 // For example,
454 // (icmp ne (A & 3), 0) & (icmp eq (A & 7), 0) -> false.
455 // (icmp ne (A & 15), 0) & (icmp eq (A & 3), 0) -> no folding.
456 if (ECst.isZero()) {
457 if (IsSubSetOrEqual(BCst, DCst))
458 return ConstantInt::get(LHS->getType(), !IsAnd);
459 return nullptr;
460 }
461
462 // At this point, B, D, E aren't zero and (B & D) == B, (B & D) == D or B ==
463 // D. If B is a superset of (or equal to) D, since E is not zero, LHS is
464 // subsumed by RHS (RHS implies LHS.) So the whole expression becomes
465 // RHS. For example,
466 // (icmp ne (A & 255), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
467 // (icmp ne (A & 15), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
468 if (IsSuperSetOrEqual(BCst, DCst)) {
469 // We can't guarantee that samesign hold after this fold.
470 if (auto *ICmp = dyn_cast<ICmpInst>(RHS))
471 ICmp->setSameSign(false);
472 return RHS;
473 }
474 // Otherwise, B is a subset of D. If B and E have a common bit set,
475 // ie. (B & E) != 0, then LHS is subsumed by RHS. For example.
476 // (icmp ne (A & 12), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
477 assert(IsSubSetOrEqual(BCst, DCst) && "Precondition due to above code");
478 if ((*BCst & ECst) != 0) {
479 // We can't guarantee that samesign hold after this fold.
480 if (auto *ICmp = dyn_cast<ICmpInst>(RHS))
481 ICmp->setSameSign(false);
482 return RHS;
483 }
484 // Otherwise, LHS and RHS contradict and the whole expression becomes false
485 // (or true if negated.) For example,
486 // (icmp ne (A & 7), 0) & (icmp eq (A & 15), 8) -> false.
487 // (icmp ne (A & 6), 0) & (icmp eq (A & 15), 8) -> false.
488 return ConstantInt::get(LHS->getType(), !IsAnd);
489}
490
491/// Try to fold (icmp(A & B) ==/!= 0) &/| (icmp(A & D) ==/!= E) into a single
492/// (icmp(A & X) ==/!= Y), where the left-hand side and the right hand side
493/// aren't of the common mask pattern type.
494/// Also used for logical and/or, must be poison safe.
496 Value *LHS, Value *RHS, bool IsAnd, Value *A, Value *B, Value *C, Value *D,
498 unsigned LHSMask, unsigned RHSMask, InstCombiner::BuilderTy &Builder) {
500 "Expected equality predicates for masked type of icmps.");
501 // Handle Mask_NotAllZeros-BMask_Mixed cases.
502 // (icmp ne/eq (A & B), C) &/| (icmp eq/ne (A & D), E), or
503 // (icmp eq/ne (A & B), C) &/| (icmp ne/eq (A & D), E)
504 // which gets swapped to
505 // (icmp ne/eq (A & D), E) &/| (icmp eq/ne (A & B), C).
506 if (!IsAnd) {
507 LHSMask = conjugateICmpMask(LHSMask);
508 RHSMask = conjugateICmpMask(RHSMask);
509 }
510 if ((LHSMask & Mask_NotAllZeros) && (RHSMask & BMask_Mixed)) {
512 LHS, RHS, IsAnd, A, B, D, E, PredL, PredR, Builder)) {
513 return V;
514 }
515 } else if ((LHSMask & BMask_Mixed) && (RHSMask & Mask_NotAllZeros)) {
517 RHS, LHS, IsAnd, A, D, B, C, PredR, PredL, Builder)) {
518 return V;
519 }
520 }
521 return nullptr;
522}
523
524/// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
525/// into a single (icmp(A & X) ==/!= Y).
526static Value *foldLogOpOfMaskedICmps(Value *LHS, Value *RHS, bool IsAnd,
527 bool IsLogical,
529 const SimplifyQuery &Q) {
530 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr;
531 ICmpInst::Predicate PredL, PredR;
532 std::optional<std::pair<unsigned, unsigned>> MaskPair =
533 getMaskedTypeForICmpPair(A, B, C, D, E, LHS, RHS, PredL, PredR);
534 if (!MaskPair)
535 return nullptr;
537 "Expected equality predicates for masked type of icmps.");
538 unsigned LHSMask = MaskPair->first;
539 unsigned RHSMask = MaskPair->second;
540 unsigned Mask = LHSMask & RHSMask;
541 if (Mask == 0) {
542 // Even if the two sides don't share a common pattern, check if folding can
543 // still happen.
545 LHS, RHS, IsAnd, A, B, C, D, E, PredL, PredR, LHSMask, RHSMask,
546 Builder))
547 return V;
548 return nullptr;
549 }
550
551 // In full generality:
552 // (icmp (A & B) Op C) | (icmp (A & D) Op E)
553 // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ]
554 //
555 // If the latter can be converted into (icmp (A & X) Op Y) then the former is
556 // equivalent to (icmp (A & X) !Op Y).
557 //
558 // Therefore, we can pretend for the rest of this function that we're dealing
559 // with the conjunction, provided we flip the sense of any comparisons (both
560 // input and output).
561
562 // In most cases we're going to produce an EQ for the "&&" case.
564 if (!IsAnd) {
565 // Convert the masking analysis into its equivalent with negated
566 // comparisons.
567 Mask = conjugateICmpMask(Mask);
568 }
569
570 if (Mask & Mask_AllZeros) {
571 // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
572 // -> (icmp eq (A & (B|D)), 0)
573 if (IsLogical && !isGuaranteedNotToBeUndefOrPoison(D))
574 return nullptr; // TODO: Use freeze?
575 Value *NewOr = Builder.CreateOr(B, D);
576 Value *NewAnd = Builder.CreateAnd(A, NewOr);
577 // We can't use C as zero because we might actually handle
578 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
579 // with B and D, having a single bit set.
580 Value *Zero = Constant::getNullValue(A->getType());
581 return Builder.CreateICmp(NewCC, NewAnd, Zero);
582 }
583 if (Mask & BMask_AllOnes) {
584 // (icmp eq (A & B), B) & (icmp eq (A & D), D)
585 // -> (icmp eq (A & (B|D)), (B|D))
586 if (IsLogical && !isGuaranteedNotToBeUndefOrPoison(D))
587 return nullptr; // TODO: Use freeze?
588 Value *NewOr = Builder.CreateOr(B, D);
589 Value *NewAnd = Builder.CreateAnd(A, NewOr);
590 return Builder.CreateICmp(NewCC, NewAnd, NewOr);
591 }
592 if (Mask & AMask_AllOnes) {
593 // (icmp eq (A & B), A) & (icmp eq (A & D), A)
594 // -> (icmp eq (A & (B&D)), A)
595 if (IsLogical && !isGuaranteedNotToBeUndefOrPoison(D))
596 return nullptr; // TODO: Use freeze?
597 Value *NewAnd1 = Builder.CreateAnd(B, D);
598 Value *NewAnd2 = Builder.CreateAnd(A, NewAnd1);
599 return Builder.CreateICmp(NewCC, NewAnd2, A);
600 }
601
602 const APInt *ConstB, *ConstD;
603 if (match(B, m_APInt(ConstB)) && match(D, m_APInt(ConstD))) {
604 if (Mask & (Mask_NotAllZeros | BMask_NotAllOnes)) {
605 // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and
606 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
607 // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
608 // Only valid if one of the masks is a superset of the other (check "B&D"
609 // is the same as either B or D).
610 APInt NewMask = *ConstB & *ConstD;
611 if (NewMask == *ConstB)
612 return LHS;
613 if (NewMask == *ConstD)
614 return RHS;
615 }
616
617 if (Mask & AMask_NotAllOnes) {
618 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
619 // -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
620 // Only valid if one of the masks is a superset of the other (check "B|D"
621 // is the same as either B or D).
622 APInt NewMask = *ConstB | *ConstD;
623 if (NewMask == *ConstB)
624 return LHS;
625 if (NewMask == *ConstD)
626 return RHS;
627 }
628
629 if (Mask & (BMask_Mixed | BMask_NotMixed)) {
630 // Mixed:
631 // (icmp eq (A & B), C) & (icmp eq (A & D), E)
632 // We already know that B & C == C && D & E == E.
633 // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
634 // C and E, which are shared by both the mask B and the mask D, don't
635 // contradict, then we can transform to
636 // -> (icmp eq (A & (B|D)), (C|E))
637 // Currently, we only handle the case of B, C, D, and E being constant.
638 // We can't simply use C and E because we might actually handle
639 // (icmp ne (A & B), B) & (icmp eq (A & D), D)
640 // with B and D, having a single bit set.
641
642 // NotMixed:
643 // (icmp ne (A & B), C) & (icmp ne (A & D), E)
644 // -> (icmp ne (A & (B & D)), (C & E))
645 // Check the intersection (B & D) for inequality.
646 // Assume that (B & D) == B || (B & D) == D, i.e B/D is a subset of D/B
647 // and (B & D) & (C ^ E) == 0, bits of C and E, which are shared by both
648 // the B and the D, don't contradict. Note that we can assume (~B & C) ==
649 // 0 && (~D & E) == 0, previous operation should delete these icmps if it
650 // hadn't been met.
651
652 const APInt *OldConstC, *OldConstE;
653 if (!match(C, m_APInt(OldConstC)) || !match(E, m_APInt(OldConstE)))
654 return nullptr;
655
656 auto FoldBMixed = [&](ICmpInst::Predicate CC, bool IsNot) -> Value * {
658 const APInt ConstC = PredL != CC ? *ConstB ^ *OldConstC : *OldConstC;
659 const APInt ConstE = PredR != CC ? *ConstD ^ *OldConstE : *OldConstE;
660
661 if (((*ConstB & *ConstD) & (ConstC ^ ConstE)).getBoolValue())
662 return IsNot ? nullptr : ConstantInt::get(LHS->getType(), !IsAnd);
663
664 if (IsNot && !ConstB->isSubsetOf(*ConstD) &&
665 !ConstD->isSubsetOf(*ConstB))
666 return nullptr;
667
668 APInt BD, CE;
669 if (IsNot) {
670 BD = *ConstB & *ConstD;
671 CE = ConstC & ConstE;
672 } else {
673 BD = *ConstB | *ConstD;
674 CE = ConstC | ConstE;
675 }
676 Value *NewAnd = Builder.CreateAnd(A, BD);
677 Value *CEVal = ConstantInt::get(A->getType(), CE);
678 return Builder.CreateICmp(CC, CEVal, NewAnd);
679 };
680
681 if (Mask & BMask_Mixed)
682 return FoldBMixed(NewCC, false);
683 if (Mask & BMask_NotMixed) // can be else also
684 return FoldBMixed(NewCC, true);
685 }
686 }
687
688 // (icmp eq (A & B), 0) | (icmp eq (A & D), 0)
689 // -> (icmp ne (A & (B|D)), (B|D))
690 // (icmp ne (A & B), 0) & (icmp ne (A & D), 0)
691 // -> (icmp eq (A & (B|D)), (B|D))
692 // iff B and D is known to be a power of two
693 if (Mask & Mask_NotAllZeros &&
694 isKnownToBeAPowerOfTwo(B, /*OrZero=*/false, /*Depth=*/0, Q) &&
695 isKnownToBeAPowerOfTwo(D, /*OrZero=*/false, /*Depth=*/0, Q)) {
696 // If this is a logical and/or, then we must prevent propagation of a
697 // poison value from the RHS by inserting freeze.
698 if (IsLogical)
699 D = Builder.CreateFreeze(D);
700 Value *Mask = Builder.CreateOr(B, D);
701 Value *Masked = Builder.CreateAnd(A, Mask);
702 return Builder.CreateICmp(NewCC, Masked, Mask);
703 }
704 return nullptr;
705}
706
707/// Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
708/// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
709/// If \p Inverted is true then the check is for the inverted range, e.g.
710/// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
712 bool Inverted) {
713 // Check the lower range comparison, e.g. x >= 0
714 // InstCombine already ensured that if there is a constant it's on the RHS.
715 ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1));
716 if (!RangeStart)
717 return nullptr;
718
719 ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() :
720 Cmp0->getPredicate());
721
722 // Accept x > -1 or x >= 0 (after potentially inverting the predicate).
723 if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) ||
724 (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero())))
725 return nullptr;
726
727 ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() :
728 Cmp1->getPredicate());
729
730 Value *Input = Cmp0->getOperand(0);
731 Value *Cmp1Op0 = Cmp1->getOperand(0);
732 Value *Cmp1Op1 = Cmp1->getOperand(1);
733 Value *RangeEnd;
734 if (match(Cmp1Op0, m_SExtOrSelf(m_Specific(Input)))) {
735 // For the upper range compare we have: icmp x, n
736 Input = Cmp1Op0;
737 RangeEnd = Cmp1Op1;
738 } else if (match(Cmp1Op1, m_SExtOrSelf(m_Specific(Input)))) {
739 // For the upper range compare we have: icmp n, x
740 Input = Cmp1Op1;
741 RangeEnd = Cmp1Op0;
742 Pred1 = ICmpInst::getSwappedPredicate(Pred1);
743 } else {
744 return nullptr;
745 }
746
747 // Check the upper range comparison, e.g. x < n
748 ICmpInst::Predicate NewPred;
749 switch (Pred1) {
750 case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break;
751 case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break;
752 default: return nullptr;
753 }
754
755 // This simplification is only valid if the upper range is not negative.
756 KnownBits Known = computeKnownBits(RangeEnd, /*Depth=*/0, Cmp1);
757 if (!Known.isNonNegative())
758 return nullptr;
759
760 if (Inverted)
761 NewPred = ICmpInst::getInversePredicate(NewPred);
762
763 return Builder.CreateICmp(NewPred, Input, RangeEnd);
764}
765
766// (or (icmp eq X, 0), (icmp eq X, Pow2OrZero))
767// -> (icmp eq (and X, Pow2OrZero), X)
768// (and (icmp ne X, 0), (icmp ne X, Pow2OrZero))
769// -> (icmp ne (and X, Pow2OrZero), X)
770static Value *
772 ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
773 const SimplifyQuery &Q) {
775 // Make sure we have right compares for our op.
776 if (LHS->getPredicate() != Pred || RHS->getPredicate() != Pred)
777 return nullptr;
778
779 // Make it so we can match LHS against the (icmp eq/ne X, 0) just for
780 // simplicity.
781 if (match(RHS->getOperand(1), m_Zero()))
782 std::swap(LHS, RHS);
783
784 Value *Pow2, *Op;
785 // Match the desired pattern:
786 // LHS: (icmp eq/ne X, 0)
787 // RHS: (icmp eq/ne X, Pow2OrZero)
788 // Skip if Pow2OrZero is 1. Either way it gets folded to (icmp ugt X, 1) but
789 // this form ends up slightly less canonical.
790 // We could potentially be more sophisticated than requiring LHS/RHS
791 // be one-use. We don't create additional instructions if only one
792 // of them is one-use. So cases where one is one-use and the other
793 // is two-use might be profitable.
794 if (!match(LHS, m_OneUse(m_ICmp(Pred, m_Value(Op), m_Zero()))) ||
795 !match(RHS, m_OneUse(m_c_ICmp(Pred, m_Specific(Op), m_Value(Pow2)))) ||
796 match(Pow2, m_One()) ||
797 !isKnownToBeAPowerOfTwo(Pow2, Q.DL, /*OrZero=*/true, /*Depth=*/0, Q.AC,
798 Q.CxtI, Q.DT))
799 return nullptr;
800
801 Value *And = Builder.CreateAnd(Op, Pow2);
802 return Builder.CreateICmp(Pred, And, Op);
803}
804
805/// General pattern:
806/// X & Y
807///
808/// Where Y is checking that all the high bits (covered by a mask 4294967168)
809/// are uniform, i.e. %arg & 4294967168 can be either 4294967168 or 0
810/// Pattern can be one of:
811/// %t = add i32 %arg, 128
812/// %r = icmp ult i32 %t, 256
813/// Or
814/// %t0 = shl i32 %arg, 24
815/// %t1 = ashr i32 %t0, 24
816/// %r = icmp eq i32 %t1, %arg
817/// Or
818/// %t0 = trunc i32 %arg to i8
819/// %t1 = sext i8 %t0 to i32
820/// %r = icmp eq i32 %t1, %arg
821/// This pattern is a signed truncation check.
822///
823/// And X is checking that some bit in that same mask is zero.
824/// I.e. can be one of:
825/// %r = icmp sgt i32 %arg, -1
826/// Or
827/// %t = and i32 %arg, 2147483648
828/// %r = icmp eq i32 %t, 0
829///
830/// Since we are checking that all the bits in that mask are the same,
831/// and a particular bit is zero, what we are really checking is that all the
832/// masked bits are zero.
833/// So this should be transformed to:
834/// %r = icmp ult i32 %arg, 128
836 Instruction &CxtI,
837 InstCombiner::BuilderTy &Builder) {
838 assert(CxtI.getOpcode() == Instruction::And);
839
840 // Match icmp ult (add %arg, C01), C1 (C1 == C01 << 1; powers of two)
841 auto tryToMatchSignedTruncationCheck = [](ICmpInst *ICmp, Value *&X,
842 APInt &SignBitMask) -> bool {
843 const APInt *I01, *I1; // powers of two; I1 == I01 << 1
845 m_Add(m_Value(X), m_Power2(I01)),
846 m_Power2(I1))) &&
847 I1->ugt(*I01) && I01->shl(1) == *I1))
848 return false;
849 // Which bit is the new sign bit as per the 'signed truncation' pattern?
850 SignBitMask = *I01;
851 return true;
852 };
853
854 // One icmp needs to be 'signed truncation check'.
855 // We need to match this first, else we will mismatch commutative cases.
856 Value *X1;
857 APInt HighestBit;
858 ICmpInst *OtherICmp;
859 if (tryToMatchSignedTruncationCheck(ICmp1, X1, HighestBit))
860 OtherICmp = ICmp0;
861 else if (tryToMatchSignedTruncationCheck(ICmp0, X1, HighestBit))
862 OtherICmp = ICmp1;
863 else
864 return nullptr;
865
866 assert(HighestBit.isPowerOf2() && "expected to be power of two (non-zero)");
867
868 // Try to match/decompose into: icmp eq (X & Mask), 0
869 auto tryToDecompose = [](ICmpInst *ICmp, Value *&X,
870 APInt &UnsetBitsMask) -> bool {
871 CmpPredicate Pred = ICmp->getPredicate();
872 // Can it be decomposed into icmp eq (X & Mask), 0 ?
873 auto Res =
875 Pred, /*LookThroughTrunc=*/false);
876 if (Res && Res->Pred == ICmpInst::ICMP_EQ) {
877 X = Res->X;
878 UnsetBitsMask = Res->Mask;
879 return true;
880 }
881
882 // Is it icmp eq (X & Mask), 0 already?
883 const APInt *Mask;
884 if (match(ICmp, m_ICmp(Pred, m_And(m_Value(X), m_APInt(Mask)), m_Zero())) &&
885 Pred == ICmpInst::ICMP_EQ) {
886 UnsetBitsMask = *Mask;
887 return true;
888 }
889 return false;
890 };
891
892 // And the other icmp needs to be decomposable into a bit test.
893 Value *X0;
894 APInt UnsetBitsMask;
895 if (!tryToDecompose(OtherICmp, X0, UnsetBitsMask))
896 return nullptr;
897
898 assert(!UnsetBitsMask.isZero() && "empty mask makes no sense.");
899
900 // Are they working on the same value?
901 Value *X;
902 if (X1 == X0) {
903 // Ok as is.
904 X = X1;
905 } else if (match(X0, m_Trunc(m_Specific(X1)))) {
906 UnsetBitsMask = UnsetBitsMask.zext(X1->getType()->getScalarSizeInBits());
907 X = X1;
908 } else
909 return nullptr;
910
911 // So which bits should be uniform as per the 'signed truncation check'?
912 // (all the bits starting with (i.e. including) HighestBit)
913 APInt SignBitsMask = ~(HighestBit - 1U);
914
915 // UnsetBitsMask must have some common bits with SignBitsMask,
916 if (!UnsetBitsMask.intersects(SignBitsMask))
917 return nullptr;
918
919 // Does UnsetBitsMask contain any bits outside of SignBitsMask?
920 if (!UnsetBitsMask.isSubsetOf(SignBitsMask)) {
921 APInt OtherHighestBit = (~UnsetBitsMask) + 1U;
922 if (!OtherHighestBit.isPowerOf2())
923 return nullptr;
924 HighestBit = APIntOps::umin(HighestBit, OtherHighestBit);
925 }
926 // Else, if it does not, then all is ok as-is.
927
928 // %r = icmp ult %X, SignBit
929 return Builder.CreateICmpULT(X, ConstantInt::get(X->getType(), HighestBit),
930 CxtI.getName() + ".simplified");
931}
932
933/// Fold (icmp eq ctpop(X) 1) | (icmp eq X 0) into (icmp ult ctpop(X) 2) and
934/// fold (icmp ne ctpop(X) 1) & (icmp ne X 0) into (icmp ugt ctpop(X) 1).
935/// Also used for logical and/or, must be poison safe if range attributes are
936/// dropped.
937static Value *foldIsPowerOf2OrZero(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd,
939 InstCombinerImpl &IC) {
940 CmpPredicate Pred0, Pred1;
941 Value *X;
942 if (!match(Cmp0, m_ICmp(Pred0, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
943 m_SpecificInt(1))) ||
944 !match(Cmp1, m_ICmp(Pred1, m_Specific(X), m_ZeroInt())))
945 return nullptr;
946
947 auto *CtPop = cast<Instruction>(Cmp0->getOperand(0));
948 if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_NE) {
949 // Drop range attributes and re-infer them in the next iteration.
950 CtPop->dropPoisonGeneratingAnnotations();
951 IC.addToWorklist(CtPop);
952 return Builder.CreateICmpUGT(CtPop, ConstantInt::get(CtPop->getType(), 1));
953 }
954 if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_EQ) {
955 // Drop range attributes and re-infer them in the next iteration.
956 CtPop->dropPoisonGeneratingAnnotations();
957 IC.addToWorklist(CtPop);
958 return Builder.CreateICmpULT(CtPop, ConstantInt::get(CtPop->getType(), 2));
959 }
960
961 return nullptr;
962}
963
964/// Reduce a pair of compares that check if a value has exactly 1 bit set.
965/// Also used for logical and/or, must be poison safe if range attributes are
966/// dropped.
967static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
969 InstCombinerImpl &IC) {
970 // Handle 'and' / 'or' commutation: make the equality check the first operand.
971 if (JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_NE)
972 std::swap(Cmp0, Cmp1);
973 else if (!JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_EQ)
974 std::swap(Cmp0, Cmp1);
975
976 // (X != 0) && (ctpop(X) u< 2) --> ctpop(X) == 1
977 Value *X;
978 if (JoinedByAnd &&
981 m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)),
982 m_SpecificInt(2)))) {
983 auto *CtPop = cast<Instruction>(Cmp1->getOperand(0));
984 // Drop range attributes and re-infer them in the next iteration.
985 CtPop->dropPoisonGeneratingAnnotations();
986 IC.addToWorklist(CtPop);
987 return Builder.CreateICmpEQ(CtPop, ConstantInt::get(CtPop->getType(), 1));
988 }
989 // (X == 0) || (ctpop(X) u> 1) --> ctpop(X) != 1
990 if (!JoinedByAnd &&
993 m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)),
994 m_SpecificInt(1)))) {
995 auto *CtPop = cast<Instruction>(Cmp1->getOperand(0));
996 // Drop range attributes and re-infer them in the next iteration.
997 CtPop->dropPoisonGeneratingAnnotations();
998 IC.addToWorklist(CtPop);
999 return Builder.CreateICmpNE(CtPop, ConstantInt::get(CtPop->getType(), 1));
1000 }
1001 return nullptr;
1002}
1003
1004/// Try to fold (icmp(A & B) == 0) & (icmp(A & D) != E) into (icmp A u< D) iff
1005/// B is a contiguous set of ones starting from the most significant bit
1006/// (negative power of 2), D and E are equal, and D is a contiguous set of ones
1007/// starting at the most significant zero bit in B. Parameter B supports masking
1008/// using undef/poison in either scalar or vector values.
1010 Value *A, Value *B, Value *D, Value *E, ICmpInst::Predicate PredL,
1013 "Expected equality predicates for masked type of icmps.");
1014 if (PredL != ICmpInst::ICMP_EQ || PredR != ICmpInst::ICMP_NE)
1015 return nullptr;
1016
1017 if (!match(B, m_NegatedPower2()) || !match(D, m_ShiftedMask()) ||
1018 !match(E, m_ShiftedMask()))
1019 return nullptr;
1020
1021 // Test scalar arguments for conversion. B has been validated earlier to be a
1022 // negative power of two and thus is guaranteed to have one or more contiguous
1023 // ones starting from the MSB followed by zero or more contiguous zeros. D has
1024 // been validated earlier to be a shifted set of one or more contiguous ones.
1025 // In order to match, B leading ones and D leading zeros should be equal. The
1026 // predicate that B be a negative power of 2 prevents the condition of there
1027 // ever being zero leading ones. Thus 0 == 0 cannot occur. The predicate that
1028 // D always be a shifted mask prevents the condition of D equaling 0. This
1029 // prevents matching the condition where B contains the maximum number of
1030 // leading one bits (-1) and D contains the maximum number of leading zero
1031 // bits (0).
1032 auto isReducible = [](const Value *B, const Value *D, const Value *E) {
1033 const APInt *BCst, *DCst, *ECst;
1034 return match(B, m_APIntAllowPoison(BCst)) && match(D, m_APInt(DCst)) &&
1035 match(E, m_APInt(ECst)) && *DCst == *ECst &&
1036 (isa<PoisonValue>(B) ||
1037 (BCst->countLeadingOnes() == DCst->countLeadingZeros()));
1038 };
1039
1040 // Test vector type arguments for conversion.
1041 if (const auto *BVTy = dyn_cast<VectorType>(B->getType())) {
1042 const auto *BFVTy = dyn_cast<FixedVectorType>(BVTy);
1043 const auto *BConst = dyn_cast<Constant>(B);
1044 const auto *DConst = dyn_cast<Constant>(D);
1045 const auto *EConst = dyn_cast<Constant>(E);
1046
1047 if (!BFVTy || !BConst || !DConst || !EConst)
1048 return nullptr;
1049
1050 for (unsigned I = 0; I != BFVTy->getNumElements(); ++I) {
1051 const auto *BElt = BConst->getAggregateElement(I);
1052 const auto *DElt = DConst->getAggregateElement(I);
1053 const auto *EElt = EConst->getAggregateElement(I);
1054
1055 if (!BElt || !DElt || !EElt)
1056 return nullptr;
1057 if (!isReducible(BElt, DElt, EElt))
1058 return nullptr;
1059 }
1060 } else {
1061 // Test scalar type arguments for conversion.
1062 if (!isReducible(B, D, E))
1063 return nullptr;
1064 }
1065 return Builder.CreateICmp(ICmpInst::ICMP_ULT, A, D);
1066}
1067
1068/// Try to fold ((icmp X u< P) & (icmp(X & M) != M)) or ((icmp X s> -1) &
1069/// (icmp(X & M) != M)) into (icmp X u< M). Where P is a power of 2, M < P, and
1070/// M is a contiguous shifted mask starting at the right most significant zero
1071/// bit in P. SGT is supported as when P is the largest representable power of
1072/// 2, an earlier optimization converts the expression into (icmp X s> -1).
1073/// Parameter P supports masking using undef/poison in either scalar or vector
1074/// values.
1076 bool JoinedByAnd,
1077 InstCombiner::BuilderTy &Builder) {
1078 if (!JoinedByAnd)
1079 return nullptr;
1080 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr;
1081 ICmpInst::Predicate CmpPred0, CmpPred1;
1082 // Assuming P is a 2^n, getMaskedTypeForICmpPair will normalize (icmp X u<
1083 // 2^n) into (icmp (X & ~(2^n-1)) == 0) and (icmp X s> -1) into (icmp (X &
1084 // SignMask) == 0).
1085 std::optional<std::pair<unsigned, unsigned>> MaskPair =
1086 getMaskedTypeForICmpPair(A, B, C, D, E, Cmp0, Cmp1, CmpPred0, CmpPred1);
1087 if (!MaskPair)
1088 return nullptr;
1089
1090 const auto compareBMask = BMask_NotMixed | BMask_NotAllOnes;
1091 unsigned CmpMask0 = MaskPair->first;
1092 unsigned CmpMask1 = MaskPair->second;
1093 if ((CmpMask0 & Mask_AllZeros) && (CmpMask1 == compareBMask)) {
1094 if (Value *V = foldNegativePower2AndShiftedMask(A, B, D, E, CmpPred0,
1095 CmpPred1, Builder))
1096 return V;
1097 } else if ((CmpMask0 == compareBMask) && (CmpMask1 & Mask_AllZeros)) {
1098 if (Value *V = foldNegativePower2AndShiftedMask(A, D, B, C, CmpPred1,
1099 CmpPred0, Builder))
1100 return V;
1101 }
1102 return nullptr;
1103}
1104
1105/// Commuted variants are assumed to be handled by calling this function again
1106/// with the parameters swapped.
1108 ICmpInst *UnsignedICmp, bool IsAnd,
1109 const SimplifyQuery &Q,
1110 InstCombiner::BuilderTy &Builder) {
1111 Value *ZeroCmpOp;
1112 CmpPredicate EqPred;
1113 if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(ZeroCmpOp), m_Zero())) ||
1114 !ICmpInst::isEquality(EqPred))
1115 return nullptr;
1116
1117 CmpPredicate UnsignedPred;
1118
1119 Value *A, *B;
1120 if (match(UnsignedICmp,
1121 m_c_ICmp(UnsignedPred, m_Specific(ZeroCmpOp), m_Value(A))) &&
1122 match(ZeroCmpOp, m_c_Add(m_Specific(A), m_Value(B))) &&
1123 (ZeroICmp->hasOneUse() || UnsignedICmp->hasOneUse())) {
1124 auto GetKnownNonZeroAndOther = [&](Value *&NonZero, Value *&Other) {
1125 if (!isKnownNonZero(NonZero, Q))
1126 std::swap(NonZero, Other);
1127 return isKnownNonZero(NonZero, Q);
1128 };
1129
1130 // Given ZeroCmpOp = (A + B)
1131 // ZeroCmpOp < A && ZeroCmpOp != 0 --> (0-X) < Y iff
1132 // ZeroCmpOp >= A || ZeroCmpOp == 0 --> (0-X) >= Y iff
1133 // with X being the value (A/B) that is known to be non-zero,
1134 // and Y being remaining value.
1135 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE &&
1136 IsAnd && GetKnownNonZeroAndOther(B, A))
1137 return Builder.CreateICmpULT(Builder.CreateNeg(B), A);
1138 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ &&
1139 !IsAnd && GetKnownNonZeroAndOther(B, A))
1140 return Builder.CreateICmpUGE(Builder.CreateNeg(B), A);
1141 }
1142
1143 return nullptr;
1144}
1145
1146struct IntPart {
1148 unsigned StartBit;
1149 unsigned NumBits;
1150};
1151
1152/// Match an extraction of bits from an integer.
1153static std::optional<IntPart> matchIntPart(Value *V) {
1154 Value *X;
1155 if (!match(V, m_OneUse(m_Trunc(m_Value(X)))))
1156 return std::nullopt;
1157
1158 unsigned NumOriginalBits = X->getType()->getScalarSizeInBits();
1159 unsigned NumExtractedBits = V->getType()->getScalarSizeInBits();
1160 Value *Y;
1161 const APInt *Shift;
1162 // For a trunc(lshr Y, Shift) pattern, make sure we're only extracting bits
1163 // from Y, not any shifted-in zeroes.
1164 if (match(X, m_OneUse(m_LShr(m_Value(Y), m_APInt(Shift)))) &&
1165 Shift->ule(NumOriginalBits - NumExtractedBits))
1166 return {{Y, (unsigned)Shift->getZExtValue(), NumExtractedBits}};
1167 return {{X, 0, NumExtractedBits}};
1168}
1169
1170/// Materialize an extraction of bits from an integer in IR.
1171static Value *extractIntPart(const IntPart &P, IRBuilderBase &Builder) {
1172 Value *V = P.From;
1173 if (P.StartBit)
1174 V = Builder.CreateLShr(V, P.StartBit);
1175 Type *TruncTy = V->getType()->getWithNewBitWidth(P.NumBits);
1176 if (TruncTy != V->getType())
1177 V = Builder.CreateTrunc(V, TruncTy);
1178 return V;
1179}
1180
1181/// (icmp eq X0, Y0) & (icmp eq X1, Y1) -> icmp eq X01, Y01
1182/// (icmp ne X0, Y0) | (icmp ne X1, Y1) -> icmp ne X01, Y01
1183/// where X0, X1 and Y0, Y1 are adjacent parts extracted from an integer.
1184Value *InstCombinerImpl::foldEqOfParts(Value *Cmp0, Value *Cmp1, bool IsAnd) {
1185 if (!Cmp0->hasOneUse() || !Cmp1->hasOneUse())
1186 return nullptr;
1187
1189 auto GetMatchPart = [&](Value *CmpV,
1190 unsigned OpNo) -> std::optional<IntPart> {
1191 assert(CmpV->getType()->isIntOrIntVectorTy(1) && "Must be bool");
1192
1193 Value *X, *Y;
1194 // icmp ne (and x, 1), (and y, 1) <=> trunc (xor x, y) to i1
1195 // icmp eq (and x, 1), (and y, 1) <=> not (trunc (xor x, y) to i1)
1196 if (Pred == CmpInst::ICMP_NE
1197 ? match(CmpV, m_Trunc(m_Xor(m_Value(X), m_Value(Y))))
1198 : match(CmpV, m_Not(m_Trunc(m_Xor(m_Value(X), m_Value(Y))))))
1199 return {{OpNo == 0 ? X : Y, 0, 1}};
1200
1201 auto *Cmp = dyn_cast<ICmpInst>(CmpV);
1202 if (!Cmp)
1203 return std::nullopt;
1204
1205 if (Pred == Cmp->getPredicate())
1206 return matchIntPart(Cmp->getOperand(OpNo));
1207
1208 const APInt *C;
1209 // (icmp eq (lshr x, C), (lshr y, C)) gets optimized to:
1210 // (icmp ult (xor x, y), 1 << C) so also look for that.
1211 if (Pred == CmpInst::ICMP_EQ && Cmp->getPredicate() == CmpInst::ICMP_ULT) {
1212 if (!match(Cmp->getOperand(1), m_Power2(C)) ||
1213 !match(Cmp->getOperand(0), m_Xor(m_Value(), m_Value())))
1214 return std::nullopt;
1215 }
1216
1217 // (icmp ne (lshr x, C), (lshr y, C)) gets optimized to:
1218 // (icmp ugt (xor x, y), (1 << C) - 1) so also look for that.
1219 else if (Pred == CmpInst::ICMP_NE &&
1220 Cmp->getPredicate() == CmpInst::ICMP_UGT) {
1221 if (!match(Cmp->getOperand(1), m_LowBitMask(C)) ||
1222 !match(Cmp->getOperand(0), m_Xor(m_Value(), m_Value())))
1223 return std::nullopt;
1224 } else {
1225 return std::nullopt;
1226 }
1227
1228 unsigned From = Pred == CmpInst::ICMP_NE ? C->popcount() : C->countr_zero();
1229 Instruction *I = cast<Instruction>(Cmp->getOperand(0));
1230 return {{I->getOperand(OpNo), From, C->getBitWidth() - From}};
1231 };
1232
1233 std::optional<IntPart> L0 = GetMatchPart(Cmp0, 0);
1234 std::optional<IntPart> R0 = GetMatchPart(Cmp0, 1);
1235 std::optional<IntPart> L1 = GetMatchPart(Cmp1, 0);
1236 std::optional<IntPart> R1 = GetMatchPart(Cmp1, 1);
1237 if (!L0 || !R0 || !L1 || !R1)
1238 return nullptr;
1239
1240 // Make sure the LHS/RHS compare a part of the same value, possibly after
1241 // an operand swap.
1242 if (L0->From != L1->From || R0->From != R1->From) {
1243 if (L0->From != R1->From || R0->From != L1->From)
1244 return nullptr;
1245 std::swap(L1, R1);
1246 }
1247
1248 // Make sure the extracted parts are adjacent, canonicalizing to L0/R0 being
1249 // the low part and L1/R1 being the high part.
1250 if (L0->StartBit + L0->NumBits != L1->StartBit ||
1251 R0->StartBit + R0->NumBits != R1->StartBit) {
1252 if (L1->StartBit + L1->NumBits != L0->StartBit ||
1253 R1->StartBit + R1->NumBits != R0->StartBit)
1254 return nullptr;
1255 std::swap(L0, L1);
1256 std::swap(R0, R1);
1257 }
1258
1259 // We can simplify to a comparison of these larger parts of the integers.
1260 IntPart L = {L0->From, L0->StartBit, L0->NumBits + L1->NumBits};
1261 IntPart R = {R0->From, R0->StartBit, R0->NumBits + R1->NumBits};
1264 return Builder.CreateICmp(Pred, LValue, RValue);
1265}
1266
1267/// Reduce logic-of-compares with equality to a constant by substituting a
1268/// common operand with the constant. Callers are expected to call this with
1269/// Cmp0/Cmp1 switched to handle logic op commutativity.
1271 bool IsAnd, bool IsLogical,
1272 InstCombiner::BuilderTy &Builder,
1273 const SimplifyQuery &Q) {
1274 // Match an equality compare with a non-poison constant as Cmp0.
1275 // Also, give up if the compare can be constant-folded to avoid looping.
1276 CmpPredicate Pred0;
1277 Value *X;
1278 Constant *C;
1279 if (!match(Cmp0, m_ICmp(Pred0, m_Value(X), m_Constant(C))) ||
1280 !isGuaranteedNotToBeUndefOrPoison(C) || isa<Constant>(X))
1281 return nullptr;
1282 if ((IsAnd && Pred0 != ICmpInst::ICMP_EQ) ||
1283 (!IsAnd && Pred0 != ICmpInst::ICMP_NE))
1284 return nullptr;
1285
1286 // The other compare must include a common operand (X). Canonicalize the
1287 // common operand as operand 1 (Pred1 is swapped if the common operand was
1288 // operand 0).
1289 Value *Y;
1290 CmpPredicate Pred1;
1291 if (!match(Cmp1, m_c_ICmp(Pred1, m_Value(Y), m_Specific(X))))
1292 return nullptr;
1293
1294 // Replace variable with constant value equivalence to remove a variable use:
1295 // (X == C) && (Y Pred1 X) --> (X == C) && (Y Pred1 C)
1296 // (X != C) || (Y Pred1 X) --> (X != C) || (Y Pred1 C)
1297 // Can think of the 'or' substitution with the 'and' bool equivalent:
1298 // A || B --> A || (!A && B)
1299 Value *SubstituteCmp = simplifyICmpInst(Pred1, Y, C, Q);
1300 if (!SubstituteCmp) {
1301 // If we need to create a new instruction, require that the old compare can
1302 // be removed.
1303 if (!Cmp1->hasOneUse())
1304 return nullptr;
1305 SubstituteCmp = Builder.CreateICmp(Pred1, Y, C);
1306 }
1307 if (IsLogical)
1308 return IsAnd ? Builder.CreateLogicalAnd(Cmp0, SubstituteCmp)
1309 : Builder.CreateLogicalOr(Cmp0, SubstituteCmp);
1310 return Builder.CreateBinOp(IsAnd ? Instruction::And : Instruction::Or, Cmp0,
1311 SubstituteCmp);
1312}
1313
1314/// Fold (icmp Pred1 V1, C1) & (icmp Pred2 V2, C2)
1315/// or (icmp Pred1 V1, C1) | (icmp Pred2 V2, C2)
1316/// into a single comparison using range-based reasoning.
1317/// NOTE: This is also used for logical and/or, must be poison-safe!
1318Value *InstCombinerImpl::foldAndOrOfICmpsUsingRanges(ICmpInst *ICmp1,
1319 ICmpInst *ICmp2,
1320 bool IsAnd) {
1321 CmpPredicate Pred1, Pred2;
1322 Value *V1, *V2;
1323 const APInt *C1, *C2;
1324 if (!match(ICmp1, m_ICmp(Pred1, m_Value(V1), m_APInt(C1))) ||
1325 !match(ICmp2, m_ICmp(Pred2, m_Value(V2), m_APInt(C2))))
1326 return nullptr;
1327
1328 // Look through add of a constant offset on V1, V2, or both operands. This
1329 // allows us to interpret the V + C' < C'' range idiom into a proper range.
1330 const APInt *Offset1 = nullptr, *Offset2 = nullptr;
1331 if (V1 != V2) {
1332 Value *X;
1333 if (match(V1, m_Add(m_Value(X), m_APInt(Offset1))))
1334 V1 = X;
1335 if (match(V2, m_Add(m_Value(X), m_APInt(Offset2))))
1336 V2 = X;
1337 }
1338
1339 if (V1 != V2)
1340 return nullptr;
1341
1343 IsAnd ? ICmpInst::getInverseCmpPredicate(Pred1) : Pred1, *C1);
1344 if (Offset1)
1345 CR1 = CR1.subtract(*Offset1);
1346
1348 IsAnd ? ICmpInst::getInverseCmpPredicate(Pred2) : Pred2, *C2);
1349 if (Offset2)
1350 CR2 = CR2.subtract(*Offset2);
1351
1352 Type *Ty = V1->getType();
1353 Value *NewV = V1;
1354 std::optional<ConstantRange> CR = CR1.exactUnionWith(CR2);
1355 if (!CR) {
1356 if (!(ICmp1->hasOneUse() && ICmp2->hasOneUse()) || CR1.isWrappedSet() ||
1357 CR2.isWrappedSet())
1358 return nullptr;
1359
1360 // Check whether we have equal-size ranges that only differ by one bit.
1361 // In that case we can apply a mask to map one range onto the other.
1362 APInt LowerDiff = CR1.getLower() ^ CR2.getLower();
1363 APInt UpperDiff = (CR1.getUpper() - 1) ^ (CR2.getUpper() - 1);
1364 APInt CR1Size = CR1.getUpper() - CR1.getLower();
1365 if (!LowerDiff.isPowerOf2() || LowerDiff != UpperDiff ||
1366 CR1Size != CR2.getUpper() - CR2.getLower())
1367 return nullptr;
1368
1369 CR = CR1.getLower().ult(CR2.getLower()) ? CR1 : CR2;
1370 NewV = Builder.CreateAnd(NewV, ConstantInt::get(Ty, ~LowerDiff));
1371 }
1372
1373 if (IsAnd)
1374 CR = CR->inverse();
1375
1376 CmpInst::Predicate NewPred;
1377 APInt NewC, Offset;
1378 CR->getEquivalentICmp(NewPred, NewC, Offset);
1379
1380 if (Offset != 0)
1381 NewV = Builder.CreateAdd(NewV, ConstantInt::get(Ty, Offset));
1382 return Builder.CreateICmp(NewPred, NewV, ConstantInt::get(Ty, NewC));
1383}
1384
1385/// Ignore all operations which only change the sign of a value, returning the
1386/// underlying magnitude value.
1388 match(Val, m_FNeg(m_Value(Val)));
1389 match(Val, m_FAbs(m_Value(Val)));
1390 match(Val, m_CopySign(m_Value(Val), m_Value()));
1391 return Val;
1392}
1393
1394/// Matches canonical form of isnan, fcmp ord x, 0
1396 return P == FCmpInst::FCMP_ORD && match(RHS, m_AnyZeroFP());
1397}
1398
1399/// Matches fcmp u__ x, +/-inf
1401 Value *RHS) {
1402 return FCmpInst::isUnordered(P) && match(RHS, m_Inf());
1403}
1404
1405/// and (fcmp ord x, 0), (fcmp u* x, inf) -> fcmp o* x, inf
1406///
1407/// Clang emits this pattern for doing an isfinite check in __builtin_isnormal.
1409 FCmpInst *RHS) {
1410 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1411 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1412 FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1413
1414 if (!matchIsNotNaN(PredL, LHS0, LHS1) ||
1415 !matchUnorderedInfCompare(PredR, RHS0, RHS1))
1416 return nullptr;
1417
1418 return Builder.CreateFCmpFMF(FCmpInst::getOrderedPredicate(PredR), RHS0, RHS1,
1420}
1421
1422Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
1423 bool IsAnd, bool IsLogicalSelect) {
1424 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1425 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1426 FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1427
1428 if (LHS0 == RHS1 && RHS0 == LHS1) {
1429 // Swap RHS operands to match LHS.
1430 PredR = FCmpInst::getSwappedPredicate(PredR);
1431 std::swap(RHS0, RHS1);
1432 }
1433
1434 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
1435 // Suppose the relation between x and y is R, where R is one of
1436 // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for
1437 // testing the desired relations.
1438 //
1439 // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1440 // bool(R & CC0) && bool(R & CC1)
1441 // = bool((R & CC0) & (R & CC1))
1442 // = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency
1443 //
1444 // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1445 // bool(R & CC0) || bool(R & CC1)
1446 // = bool((R & CC0) | (R & CC1))
1447 // = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;)
1448 if (LHS0 == RHS0 && LHS1 == RHS1) {
1449 unsigned FCmpCodeL = getFCmpCode(PredL);
1450 unsigned FCmpCodeR = getFCmpCode(PredR);
1451 unsigned NewPred = IsAnd ? FCmpCodeL & FCmpCodeR : FCmpCodeL | FCmpCodeR;
1452
1453 // Intersect the fast math flags.
1454 // TODO: We can union the fast math flags unless this is a logical select.
1455 return getFCmpValue(NewPred, LHS0, LHS1, Builder,
1456 FMFSource::intersect(LHS, RHS));
1457 }
1458
1459 // This transform is not valid for a logical select.
1460 if (!IsLogicalSelect &&
1461 ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) ||
1462 (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO &&
1463 !IsAnd))) {
1464 if (LHS0->getType() != RHS0->getType())
1465 return nullptr;
1466
1467 // FCmp canonicalization ensures that (fcmp ord/uno X, X) and
1468 // (fcmp ord/uno X, C) will be transformed to (fcmp X, +0.0).
1469 if (match(LHS1, m_PosZeroFP()) && match(RHS1, m_PosZeroFP())) {
1470 // Ignore the constants because they are obviously not NANs:
1471 // (fcmp ord x, 0.0) & (fcmp ord y, 0.0) -> (fcmp ord x, y)
1472 // (fcmp uno x, 0.0) | (fcmp uno y, 0.0) -> (fcmp uno x, y)
1473 return Builder.CreateFCmpFMF(PredL, LHS0, RHS0,
1474 FMFSource::intersect(LHS, RHS));
1475 }
1476 }
1477
1478 if (IsAnd && stripSignOnlyFPOps(LHS0) == stripSignOnlyFPOps(RHS0)) {
1479 // and (fcmp ord x, 0), (fcmp u* x, inf) -> fcmp o* x, inf
1480 // and (fcmp ord x, 0), (fcmp u* fabs(x), inf) -> fcmp o* x, inf
1481 if (Value *Left = matchIsFiniteTest(Builder, LHS, RHS))
1482 return Left;
1483 if (Value *Right = matchIsFiniteTest(Builder, RHS, LHS))
1484 return Right;
1485 }
1486
1487 // Turn at least two fcmps with constants into llvm.is.fpclass.
1488 //
1489 // If we can represent a combined value test with one class call, we can
1490 // potentially eliminate 4-6 instructions. If we can represent a test with a
1491 // single fcmp with fneg and fabs, that's likely a better canonical form.
1492 if (LHS->hasOneUse() && RHS->hasOneUse()) {
1493 auto [ClassValRHS, ClassMaskRHS] =
1494 fcmpToClassTest(PredR, *RHS->getFunction(), RHS0, RHS1);
1495 if (ClassValRHS) {
1496 auto [ClassValLHS, ClassMaskLHS] =
1497 fcmpToClassTest(PredL, *LHS->getFunction(), LHS0, LHS1);
1498 if (ClassValLHS == ClassValRHS) {
1499 unsigned CombinedMask = IsAnd ? (ClassMaskLHS & ClassMaskRHS)
1500 : (ClassMaskLHS | ClassMaskRHS);
1501 return Builder.CreateIntrinsic(
1502 Intrinsic::is_fpclass, {ClassValLHS->getType()},
1503 {ClassValLHS, Builder.getInt32(CombinedMask)});
1504 }
1505 }
1506 }
1507
1508 // Canonicalize the range check idiom:
1509 // and (fcmp olt/ole/ult/ule x, C), (fcmp ogt/oge/ugt/uge x, -C)
1510 // --> fabs(x) olt/ole/ult/ule C
1511 // or (fcmp ogt/oge/ugt/uge x, C), (fcmp olt/ole/ult/ule x, -C)
1512 // --> fabs(x) ogt/oge/ugt/uge C
1513 // TODO: Generalize to handle a negated variable operand?
1514 const APFloat *LHSC, *RHSC;
1515 if (LHS0 == RHS0 && LHS->hasOneUse() && RHS->hasOneUse() &&
1516 FCmpInst::getSwappedPredicate(PredL) == PredR &&
1517 match(LHS1, m_APFloatAllowPoison(LHSC)) &&
1518 match(RHS1, m_APFloatAllowPoison(RHSC)) &&
1519 LHSC->bitwiseIsEqual(neg(*RHSC))) {
1520 auto IsLessThanOrLessEqual = [](FCmpInst::Predicate Pred) {
1521 switch (Pred) {
1522 case FCmpInst::FCMP_OLT:
1523 case FCmpInst::FCMP_OLE:
1524 case FCmpInst::FCMP_ULT:
1525 case FCmpInst::FCMP_ULE:
1526 return true;
1527 default:
1528 return false;
1529 }
1530 };
1531 if (IsLessThanOrLessEqual(IsAnd ? PredR : PredL)) {
1532 std::swap(LHSC, RHSC);
1533 std::swap(PredL, PredR);
1534 }
1535 if (IsLessThanOrLessEqual(IsAnd ? PredL : PredR)) {
1536 FastMathFlags NewFlag = LHS->getFastMathFlags();
1537 if (!IsLogicalSelect)
1538 NewFlag |= RHS->getFastMathFlags();
1539
1540 Value *FAbs =
1541 Builder.CreateUnaryIntrinsic(Intrinsic::fabs, LHS0, NewFlag);
1542 return Builder.CreateFCmpFMF(
1543 PredL, FAbs, ConstantFP::get(LHS0->getType(), *LHSC), NewFlag);
1544 }
1545 }
1546
1547 return nullptr;
1548}
1549
1550/// Match an fcmp against a special value that performs a test possible by
1551/// llvm.is.fpclass.
1552static bool matchIsFPClassLikeFCmp(Value *Op, Value *&ClassVal,
1553 uint64_t &ClassMask) {
1554 auto *FCmp = dyn_cast<FCmpInst>(Op);
1555 if (!FCmp || !FCmp->hasOneUse())
1556 return false;
1557
1558 std::tie(ClassVal, ClassMask) =
1559 fcmpToClassTest(FCmp->getPredicate(), *FCmp->getParent()->getParent(),
1560 FCmp->getOperand(0), FCmp->getOperand(1));
1561 return ClassVal != nullptr;
1562}
1563
1564/// or (is_fpclass x, mask0), (is_fpclass x, mask1)
1565/// -> is_fpclass x, (mask0 | mask1)
1566/// and (is_fpclass x, mask0), (is_fpclass x, mask1)
1567/// -> is_fpclass x, (mask0 & mask1)
1568/// xor (is_fpclass x, mask0), (is_fpclass x, mask1)
1569/// -> is_fpclass x, (mask0 ^ mask1)
1570Instruction *InstCombinerImpl::foldLogicOfIsFPClass(BinaryOperator &BO,
1571 Value *Op0, Value *Op1) {
1572 Value *ClassVal0 = nullptr;
1573 Value *ClassVal1 = nullptr;
1574 uint64_t ClassMask0, ClassMask1;
1575
1576 // Restrict to folding one fcmp into one is.fpclass for now, don't introduce a
1577 // new class.
1578 //
1579 // TODO: Support forming is.fpclass out of 2 separate fcmps when codegen is
1580 // better.
1581
1582 bool IsLHSClass =
1583 match(Op0, m_OneUse(m_Intrinsic<Intrinsic::is_fpclass>(
1584 m_Value(ClassVal0), m_ConstantInt(ClassMask0))));
1585 bool IsRHSClass =
1586 match(Op1, m_OneUse(m_Intrinsic<Intrinsic::is_fpclass>(
1587 m_Value(ClassVal1), m_ConstantInt(ClassMask1))));
1588 if ((((IsLHSClass || matchIsFPClassLikeFCmp(Op0, ClassVal0, ClassMask0)) &&
1589 (IsRHSClass || matchIsFPClassLikeFCmp(Op1, ClassVal1, ClassMask1)))) &&
1590 ClassVal0 == ClassVal1) {
1591 unsigned NewClassMask;
1592 switch (BO.getOpcode()) {
1593 case Instruction::And:
1594 NewClassMask = ClassMask0 & ClassMask1;
1595 break;
1596 case Instruction::Or:
1597 NewClassMask = ClassMask0 | ClassMask1;
1598 break;
1599 case Instruction::Xor:
1600 NewClassMask = ClassMask0 ^ ClassMask1;
1601 break;
1602 default:
1603 llvm_unreachable("not a binary logic operator");
1604 }
1605
1606 if (IsLHSClass) {
1607 auto *II = cast<IntrinsicInst>(Op0);
1608 II->setArgOperand(
1609 1, ConstantInt::get(II->getArgOperand(1)->getType(), NewClassMask));
1610 return replaceInstUsesWith(BO, II);
1611 }
1612
1613 if (IsRHSClass) {
1614 auto *II = cast<IntrinsicInst>(Op1);
1615 II->setArgOperand(
1616 1, ConstantInt::get(II->getArgOperand(1)->getType(), NewClassMask));
1617 return replaceInstUsesWith(BO, II);
1618 }
1619
1620 CallInst *NewClass =
1621 Builder.CreateIntrinsic(Intrinsic::is_fpclass, {ClassVal0->getType()},
1622 {ClassVal0, Builder.getInt32(NewClassMask)});
1623 return replaceInstUsesWith(BO, NewClass);
1624 }
1625
1626 return nullptr;
1627}
1628
1629/// Look for the pattern that conditionally negates a value via math operations:
1630/// cond.splat = sext i1 cond
1631/// sub = add cond.splat, x
1632/// xor = xor sub, cond.splat
1633/// and rewrite it to do the same, but via logical operations:
1634/// value.neg = sub 0, value
1635/// cond = select i1 neg, value.neg, value
1636Instruction *InstCombinerImpl::canonicalizeConditionalNegationViaMathToSelect(
1637 BinaryOperator &I) {
1638 assert(I.getOpcode() == BinaryOperator::Xor && "Only for xor!");
1639 Value *Cond, *X;
1640 // As per complexity ordering, `xor` is not commutative here.
1641 if (!match(&I, m_c_BinOp(m_OneUse(m_Value()), m_Value())) ||
1642 !match(I.getOperand(1), m_SExt(m_Value(Cond))) ||
1643 !Cond->getType()->isIntOrIntVectorTy(1) ||
1644 !match(I.getOperand(0), m_c_Add(m_SExt(m_Specific(Cond)), m_Value(X))))
1645 return nullptr;
1646 return SelectInst::Create(Cond, Builder.CreateNeg(X, X->getName() + ".neg"),
1647 X);
1648}
1649
1650/// This a limited reassociation for a special case (see above) where we are
1651/// checking if two values are either both NAN (unordered) or not-NAN (ordered).
1652/// This could be handled more generally in '-reassociation', but it seems like
1653/// an unlikely pattern for a large number of logic ops and fcmps.
1655 InstCombiner::BuilderTy &Builder) {
1656 Instruction::BinaryOps Opcode = BO.getOpcode();
1657 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1658 "Expecting and/or op for fcmp transform");
1659
1660 // There are 4 commuted variants of the pattern. Canonicalize operands of this
1661 // logic op so an fcmp is operand 0 and a matching logic op is operand 1.
1662 Value *Op0 = BO.getOperand(0), *Op1 = BO.getOperand(1), *X;
1663 if (match(Op1, m_FCmp(m_Value(), m_AnyZeroFP())))
1664 std::swap(Op0, Op1);
1665
1666 // Match inner binop and the predicate for combining 2 NAN checks into 1.
1667 Value *BO10, *BO11;
1668 FCmpInst::Predicate NanPred = Opcode == Instruction::And ? FCmpInst::FCMP_ORD
1670 if (!match(Op0, m_SpecificFCmp(NanPred, m_Value(X), m_AnyZeroFP())) ||
1671 !match(Op1, m_BinOp(Opcode, m_Value(BO10), m_Value(BO11))))
1672 return nullptr;
1673
1674 // The inner logic op must have a matching fcmp operand.
1675 Value *Y;
1676 if (!match(BO10, m_SpecificFCmp(NanPred, m_Value(Y), m_AnyZeroFP())) ||
1677 X->getType() != Y->getType())
1678 std::swap(BO10, BO11);
1679
1680 if (!match(BO10, m_SpecificFCmp(NanPred, m_Value(Y), m_AnyZeroFP())) ||
1681 X->getType() != Y->getType())
1682 return nullptr;
1683
1684 // and (fcmp ord X, 0), (and (fcmp ord Y, 0), Z) --> and (fcmp ord X, Y), Z
1685 // or (fcmp uno X, 0), (or (fcmp uno Y, 0), Z) --> or (fcmp uno X, Y), Z
1686 Value *NewFCmp = Builder.CreateFCmp(NanPred, X, Y);
1687 if (auto *NewFCmpInst = dyn_cast<FCmpInst>(NewFCmp)) {
1688 // Intersect FMF from the 2 source fcmps.
1689 NewFCmpInst->copyIRFlags(Op0);
1690 NewFCmpInst->andIRFlags(BO10);
1691 }
1692 return BinaryOperator::Create(Opcode, NewFCmp, BO11);
1693}
1694
1695/// Match variations of De Morgan's Laws:
1696/// (~A & ~B) == (~(A | B))
1697/// (~A | ~B) == (~(A & B))
1699 InstCombiner &IC) {
1700 const Instruction::BinaryOps Opcode = I.getOpcode();
1701 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1702 "Trying to match De Morgan's Laws with something other than and/or");
1703
1704 // Flip the logic operation.
1705 const Instruction::BinaryOps FlippedOpcode =
1706 (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1707
1708 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1709 Value *A, *B;
1710 if (match(Op0, m_OneUse(m_Not(m_Value(A)))) &&
1711 match(Op1, m_OneUse(m_Not(m_Value(B)))) &&
1712 !IC.isFreeToInvert(A, A->hasOneUse()) &&
1713 !IC.isFreeToInvert(B, B->hasOneUse())) {
1714 Value *AndOr =
1715 IC.Builder.CreateBinOp(FlippedOpcode, A, B, I.getName() + ".demorgan");
1716 return BinaryOperator::CreateNot(AndOr);
1717 }
1718
1719 // The 'not' ops may require reassociation.
1720 // (A & ~B) & ~C --> A & ~(B | C)
1721 // (~B & A) & ~C --> A & ~(B | C)
1722 // (A | ~B) | ~C --> A | ~(B & C)
1723 // (~B | A) | ~C --> A | ~(B & C)
1724 Value *C;
1725 if (match(Op0, m_OneUse(m_c_BinOp(Opcode, m_Value(A), m_Not(m_Value(B))))) &&
1726 match(Op1, m_Not(m_Value(C)))) {
1727 Value *FlippedBO = IC.Builder.CreateBinOp(FlippedOpcode, B, C);
1728 return BinaryOperator::Create(Opcode, A, IC.Builder.CreateNot(FlippedBO));
1729 }
1730
1731 return nullptr;
1732}
1733
1734bool InstCombinerImpl::shouldOptimizeCast(CastInst *CI) {
1735 Value *CastSrc = CI->getOperand(0);
1736
1737 // Noop casts and casts of constants should be eliminated trivially.
1738 if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc))
1739 return false;
1740
1741 // If this cast is paired with another cast that can be eliminated, we prefer
1742 // to have it eliminated.
1743 if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1744 if (isEliminableCastPair(PrecedingCI, CI))
1745 return false;
1746
1747 return true;
1748}
1749
1750/// Fold {and,or,xor} (cast X), C.
1752 InstCombinerImpl &IC) {
1753 Constant *C = dyn_cast<Constant>(Logic.getOperand(1));
1754 if (!C)
1755 return nullptr;
1756
1757 auto LogicOpc = Logic.getOpcode();
1758 Type *DestTy = Logic.getType();
1759 Type *SrcTy = Cast->getSrcTy();
1760
1761 // Move the logic operation ahead of a zext or sext if the constant is
1762 // unchanged in the smaller source type. Performing the logic in a smaller
1763 // type may provide more information to later folds, and the smaller logic
1764 // instruction may be cheaper (particularly in the case of vectors).
1765 Value *X;
1766 if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
1767 if (Constant *TruncC = IC.getLosslessUnsignedTrunc(C, SrcTy)) {
1768 // LogicOpc (zext X), C --> zext (LogicOpc X, C)
1769 Value *NewOp = IC.Builder.CreateBinOp(LogicOpc, X, TruncC);
1770 return new ZExtInst(NewOp, DestTy);
1771 }
1772 }
1773
1774 if (match(Cast, m_OneUse(m_SExtLike(m_Value(X))))) {
1775 if (Constant *TruncC = IC.getLosslessSignedTrunc(C, SrcTy)) {
1776 // LogicOpc (sext X), C --> sext (LogicOpc X, C)
1777 Value *NewOp = IC.Builder.CreateBinOp(LogicOpc, X, TruncC);
1778 return new SExtInst(NewOp, DestTy);
1779 }
1780 }
1781
1782 return nullptr;
1783}
1784
1785/// Fold {and,or,xor} (cast X), Y.
1786Instruction *InstCombinerImpl::foldCastedBitwiseLogic(BinaryOperator &I) {
1787 auto LogicOpc = I.getOpcode();
1788 assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding");
1789
1790 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1791
1792 // fold bitwise(A >> BW - 1, zext(icmp)) (BW is the scalar bits of the
1793 // type of A)
1794 // -> bitwise(zext(A < 0), zext(icmp))
1795 // -> zext(bitwise(A < 0, icmp))
1796 auto FoldBitwiseICmpZeroWithICmp = [&](Value *Op0,
1797 Value *Op1) -> Instruction * {
1798 Value *A;
1799 bool IsMatched =
1800 match(Op0,
1802 m_Value(A),
1803 m_SpecificInt(Op0->getType()->getScalarSizeInBits() - 1)))) &&
1804 match(Op1, m_OneUse(m_ZExt(m_ICmp(m_Value(), m_Value()))));
1805
1806 if (!IsMatched)
1807 return nullptr;
1808
1809 auto *ICmpL =
1811 auto *ICmpR = cast<ZExtInst>(Op1)->getOperand(0);
1812 auto *BitwiseOp = Builder.CreateBinOp(LogicOpc, ICmpL, ICmpR);
1813
1814 return new ZExtInst(BitwiseOp, Op0->getType());
1815 };
1816
1817 if (auto *Ret = FoldBitwiseICmpZeroWithICmp(Op0, Op1))
1818 return Ret;
1819
1820 if (auto *Ret = FoldBitwiseICmpZeroWithICmp(Op1, Op0))
1821 return Ret;
1822
1823 CastInst *Cast0 = dyn_cast<CastInst>(Op0);
1824 if (!Cast0)
1825 return nullptr;
1826
1827 // This must be a cast from an integer or integer vector source type to allow
1828 // transformation of the logic operation to the source type.
1829 Type *DestTy = I.getType();
1830 Type *SrcTy = Cast0->getSrcTy();
1831 if (!SrcTy->isIntOrIntVectorTy())
1832 return nullptr;
1833
1834 if (Instruction *Ret = foldLogicCastConstant(I, Cast0, *this))
1835 return Ret;
1836
1837 CastInst *Cast1 = dyn_cast<CastInst>(Op1);
1838 if (!Cast1)
1839 return nullptr;
1840
1841 // Both operands of the logic operation are casts. The casts must be the
1842 // same kind for reduction.
1843 Instruction::CastOps CastOpcode = Cast0->getOpcode();
1844 if (CastOpcode != Cast1->getOpcode())
1845 return nullptr;
1846
1847 // If the source types do not match, but the casts are matching extends, we
1848 // can still narrow the logic op.
1849 if (SrcTy != Cast1->getSrcTy()) {
1850 Value *X, *Y;
1851 if (match(Cast0, m_OneUse(m_ZExtOrSExt(m_Value(X)))) &&
1852 match(Cast1, m_OneUse(m_ZExtOrSExt(m_Value(Y))))) {
1853 // Cast the narrower source to the wider source type.
1854 unsigned XNumBits = X->getType()->getScalarSizeInBits();
1855 unsigned YNumBits = Y->getType()->getScalarSizeInBits();
1856 if (XNumBits < YNumBits)
1857 X = Builder.CreateCast(CastOpcode, X, Y->getType());
1858 else
1859 Y = Builder.CreateCast(CastOpcode, Y, X->getType());
1860 // Do the logic op in the intermediate width, then widen more.
1861 Value *NarrowLogic = Builder.CreateBinOp(LogicOpc, X, Y);
1862 return CastInst::Create(CastOpcode, NarrowLogic, DestTy);
1863 }
1864
1865 // Give up for other cast opcodes.
1866 return nullptr;
1867 }
1868
1869 Value *Cast0Src = Cast0->getOperand(0);
1870 Value *Cast1Src = Cast1->getOperand(0);
1871
1872 // fold logic(cast(A), cast(B)) -> cast(logic(A, B))
1873 if ((Cast0->hasOneUse() || Cast1->hasOneUse()) &&
1874 shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
1875 Value *NewOp = Builder.CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1876 I.getName());
1877 return CastInst::Create(CastOpcode, NewOp, DestTy);
1878 }
1879
1880 return nullptr;
1881}
1882
1884 InstCombiner::BuilderTy &Builder) {
1885 assert(I.getOpcode() == Instruction::And);
1886 Value *Op0 = I.getOperand(0);
1887 Value *Op1 = I.getOperand(1);
1888 Value *A, *B;
1889
1890 // Operand complexity canonicalization guarantees that the 'or' is Op0.
1891 // (A | B) & ~(A & B) --> A ^ B
1892 // (A | B) & ~(B & A) --> A ^ B
1893 if (match(&I, m_BinOp(m_Or(m_Value(A), m_Value(B)),
1895 return BinaryOperator::CreateXor(A, B);
1896
1897 // (A | ~B) & (~A | B) --> ~(A ^ B)
1898 // (A | ~B) & (B | ~A) --> ~(A ^ B)
1899 // (~B | A) & (~A | B) --> ~(A ^ B)
1900 // (~B | A) & (B | ~A) --> ~(A ^ B)
1901 if (Op0->hasOneUse() || Op1->hasOneUse())
1904 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1905
1906 return nullptr;
1907}
1908
1910 InstCombiner::BuilderTy &Builder) {
1911 assert(I.getOpcode() == Instruction::Or);
1912 Value *Op0 = I.getOperand(0);
1913 Value *Op1 = I.getOperand(1);
1914 Value *A, *B;
1915
1916 // Operand complexity canonicalization guarantees that the 'and' is Op0.
1917 // (A & B) | ~(A | B) --> ~(A ^ B)
1918 // (A & B) | ~(B | A) --> ~(A ^ B)
1919 if (Op0->hasOneUse() || Op1->hasOneUse())
1920 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1922 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1923
1924 // Operand complexity canonicalization guarantees that the 'xor' is Op0.
1925 // (A ^ B) | ~(A | B) --> ~(A & B)
1926 // (A ^ B) | ~(B | A) --> ~(A & B)
1927 if (Op0->hasOneUse() || Op1->hasOneUse())
1928 if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
1930 return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
1931
1932 // (A & ~B) | (~A & B) --> A ^ B
1933 // (A & ~B) | (B & ~A) --> A ^ B
1934 // (~B & A) | (~A & B) --> A ^ B
1935 // (~B & A) | (B & ~A) --> A ^ B
1936 if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
1938 return BinaryOperator::CreateXor(A, B);
1939
1940 return nullptr;
1941}
1942
1943/// Return true if a constant shift amount is always less than the specified
1944/// bit-width. If not, the shift could create poison in the narrower type.
1945static bool canNarrowShiftAmt(Constant *C, unsigned BitWidth) {
1946 APInt Threshold(C->getType()->getScalarSizeInBits(), BitWidth);
1947 return match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold));
1948}
1949
1950/// Try to use narrower ops (sink zext ops) for an 'and' with binop operand and
1951/// a common zext operand: and (binop (zext X), C), (zext X).
1952Instruction *InstCombinerImpl::narrowMaskedBinOp(BinaryOperator &And) {
1953 // This transform could also apply to {or, and, xor}, but there are better
1954 // folds for those cases, so we don't expect those patterns here. AShr is not
1955 // handled because it should always be transformed to LShr in this sequence.
1956 // The subtract transform is different because it has a constant on the left.
1957 // Add/mul commute the constant to RHS; sub with constant RHS becomes add.
1958 Value *Op0 = And.getOperand(0), *Op1 = And.getOperand(1);
1959 Constant *C;
1960 if (!match(Op0, m_OneUse(m_Add(m_Specific(Op1), m_Constant(C)))) &&
1961 !match(Op0, m_OneUse(m_Mul(m_Specific(Op1), m_Constant(C)))) &&
1962 !match(Op0, m_OneUse(m_LShr(m_Specific(Op1), m_Constant(C)))) &&
1963 !match(Op0, m_OneUse(m_Shl(m_Specific(Op1), m_Constant(C)))) &&
1964 !match(Op0, m_OneUse(m_Sub(m_Constant(C), m_Specific(Op1)))))
1965 return nullptr;
1966
1967 Value *X;
1968 if (!match(Op1, m_ZExt(m_Value(X))) || Op1->hasNUsesOrMore(3))
1969 return nullptr;
1970
1971 Type *Ty = And.getType();
1972 if (!isa<VectorType>(Ty) && !shouldChangeType(Ty, X->getType()))
1973 return nullptr;
1974
1975 // If we're narrowing a shift, the shift amount must be safe (less than the
1976 // width) in the narrower type. If the shift amount is greater, instsimplify
1977 // usually handles that case, but we can't guarantee/assert it.
1978 Instruction::BinaryOps Opc = cast<BinaryOperator>(Op0)->getOpcode();
1979 if (Opc == Instruction::LShr || Opc == Instruction::Shl)
1980 if (!canNarrowShiftAmt(C, X->getType()->getScalarSizeInBits()))
1981 return nullptr;
1982
1983 // and (sub C, (zext X)), (zext X) --> zext (and (sub C', X), X)
1984 // and (binop (zext X), C), (zext X) --> zext (and (binop X, C'), X)
1985 Value *NewC = ConstantExpr::getTrunc(C, X->getType());
1986 Value *NewBO = Opc == Instruction::Sub ? Builder.CreateBinOp(Opc, NewC, X)
1987 : Builder.CreateBinOp(Opc, X, NewC);
1988 return new ZExtInst(Builder.CreateAnd(NewBO, X), Ty);
1989}
1990
1991/// Try folding relatively complex patterns for both And and Or operations
1992/// with all And and Or swapped.
1994 InstCombiner::BuilderTy &Builder) {
1995 const Instruction::BinaryOps Opcode = I.getOpcode();
1996 assert(Opcode == Instruction::And || Opcode == Instruction::Or);
1997
1998 // Flip the logic operation.
1999 const Instruction::BinaryOps FlippedOpcode =
2000 (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
2001
2002 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2003 Value *A, *B, *C, *X, *Y, *Dummy;
2004
2005 // Match following expressions:
2006 // (~(A | B) & C)
2007 // (~(A & B) | C)
2008 // Captures X = ~(A | B) or ~(A & B)
2009 const auto matchNotOrAnd =
2010 [Opcode, FlippedOpcode](Value *Op, auto m_A, auto m_B, auto m_C,
2011 Value *&X, bool CountUses = false) -> bool {
2012 if (CountUses && !Op->hasOneUse())
2013 return false;
2014
2015 if (match(Op, m_c_BinOp(FlippedOpcode,
2017 m_Not(m_c_BinOp(Opcode, m_A, m_B))),
2018 m_C)))
2019 return !CountUses || X->hasOneUse();
2020
2021 return false;
2022 };
2023
2024 // (~(A | B) & C) | ... --> ...
2025 // (~(A & B) | C) & ... --> ...
2026 // TODO: One use checks are conservative. We just need to check that a total
2027 // number of multiple used values does not exceed reduction
2028 // in operations.
2029 if (matchNotOrAnd(Op0, m_Value(A), m_Value(B), m_Value(C), X)) {
2030 // (~(A | B) & C) | (~(A | C) & B) --> (B ^ C) & ~A
2031 // (~(A & B) | C) & (~(A & C) | B) --> ~((B ^ C) & A)
2032 if (matchNotOrAnd(Op1, m_Specific(A), m_Specific(C), m_Specific(B), Dummy,
2033 true)) {
2034 Value *Xor = Builder.CreateXor(B, C);
2035 return (Opcode == Instruction::Or)
2036 ? BinaryOperator::CreateAnd(Xor, Builder.CreateNot(A))
2038 }
2039
2040 // (~(A | B) & C) | (~(B | C) & A) --> (A ^ C) & ~B
2041 // (~(A & B) | C) & (~(B & C) | A) --> ~((A ^ C) & B)
2042 if (matchNotOrAnd(Op1, m_Specific(B), m_Specific(C), m_Specific(A), Dummy,
2043 true)) {
2044 Value *Xor = Builder.CreateXor(A, C);
2045 return (Opcode == Instruction::Or)
2046 ? BinaryOperator::CreateAnd(Xor, Builder.CreateNot(B))
2048 }
2049
2050 // (~(A | B) & C) | ~(A | C) --> ~((B & C) | A)
2051 // (~(A & B) | C) & ~(A & C) --> ~((B | C) & A)
2052 if (match(Op1, m_OneUse(m_Not(m_OneUse(
2053 m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))))
2055 Opcode, Builder.CreateBinOp(FlippedOpcode, B, C), A));
2056
2057 // (~(A | B) & C) | ~(B | C) --> ~((A & C) | B)
2058 // (~(A & B) | C) & ~(B & C) --> ~((A | C) & B)
2059 if (match(Op1, m_OneUse(m_Not(m_OneUse(
2060 m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)))))))
2062 Opcode, Builder.CreateBinOp(FlippedOpcode, A, C), B));
2063
2064 // (~(A | B) & C) | ~(C | (A ^ B)) --> ~((A | B) & (C | (A ^ B)))
2065 // Note, the pattern with swapped and/or is not handled because the
2066 // result is more undefined than a source:
2067 // (~(A & B) | C) & ~(C & (A ^ B)) --> (A ^ B ^ C) | ~(A | C) is invalid.
2068 if (Opcode == Instruction::Or && Op0->hasOneUse() &&
2070 m_Value(Y),
2071 m_c_BinOp(Opcode, m_Specific(C),
2072 m_c_Xor(m_Specific(A), m_Specific(B)))))))) {
2073 // X = ~(A | B)
2074 // Y = (C | (A ^ B)
2075 Value *Or = cast<BinaryOperator>(X)->getOperand(0);
2076 return BinaryOperator::CreateNot(Builder.CreateAnd(Or, Y));
2077 }
2078 }
2079
2080 // (~A & B & C) | ... --> ...
2081 // (~A | B | C) | ... --> ...
2082 // TODO: One use checks are conservative. We just need to check that a total
2083 // number of multiple used values does not exceed reduction
2084 // in operations.
2085 if (match(Op0,
2086 m_OneUse(m_c_BinOp(FlippedOpcode,
2087 m_BinOp(FlippedOpcode, m_Value(B), m_Value(C)),
2088 m_CombineAnd(m_Value(X), m_Not(m_Value(A)))))) ||
2090 FlippedOpcode,
2091 m_c_BinOp(FlippedOpcode, m_Value(C),
2093 m_Value(B))))) {
2094 // X = ~A
2095 // (~A & B & C) | ~(A | B | C) --> ~(A | (B ^ C))
2096 // (~A | B | C) & ~(A & B & C) --> (~A | (B ^ C))
2097 if (match(Op1, m_OneUse(m_Not(m_c_BinOp(
2098 Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)),
2099 m_Specific(C))))) ||
2101 Opcode, m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)),
2102 m_Specific(A))))) ||
2104 Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)),
2105 m_Specific(B)))))) {
2106 Value *Xor = Builder.CreateXor(B, C);
2107 return (Opcode == Instruction::Or)
2109 : BinaryOperator::CreateOr(Xor, X);
2110 }
2111
2112 // (~A & B & C) | ~(A | B) --> (C | ~B) & ~A
2113 // (~A | B | C) & ~(A & B) --> (C & ~B) | ~A
2114 if (match(Op1, m_OneUse(m_Not(m_OneUse(
2115 m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)))))))
2117 FlippedOpcode, Builder.CreateBinOp(Opcode, C, Builder.CreateNot(B)),
2118 X);
2119
2120 // (~A & B & C) | ~(A | C) --> (B | ~C) & ~A
2121 // (~A | B | C) & ~(A & C) --> (B & ~C) | ~A
2122 if (match(Op1, m_OneUse(m_Not(m_OneUse(
2123 m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))))
2125 FlippedOpcode, Builder.CreateBinOp(Opcode, B, Builder.CreateNot(C)),
2126 X);
2127 }
2128
2129 return nullptr;
2130}
2131
2132/// Try to reassociate a pair of binops so that values with one use only are
2133/// part of the same instruction. This may enable folds that are limited with
2134/// multi-use restrictions and makes it more likely to match other patterns that
2135/// are looking for a common operand.
2137 InstCombinerImpl::BuilderTy &Builder) {
2138 Instruction::BinaryOps Opcode = BO.getOpcode();
2139 Value *X, *Y, *Z;
2140 if (match(&BO,
2141 m_c_BinOp(Opcode, m_OneUse(m_BinOp(Opcode, m_Value(X), m_Value(Y))),
2142 m_OneUse(m_Value(Z))))) {
2143 if (!isa<Constant>(X) && !isa<Constant>(Y) && !isa<Constant>(Z)) {
2144 // (X op Y) op Z --> (Y op Z) op X
2145 if (!X->hasOneUse()) {
2146 Value *YZ = Builder.CreateBinOp(Opcode, Y, Z);
2147 return BinaryOperator::Create(Opcode, YZ, X);
2148 }
2149 // (X op Y) op Z --> (X op Z) op Y
2150 if (!Y->hasOneUse()) {
2151 Value *XZ = Builder.CreateBinOp(Opcode, X, Z);
2152 return BinaryOperator::Create(Opcode, XZ, Y);
2153 }
2154 }
2155 }
2156
2157 return nullptr;
2158}
2159
2160// Match
2161// (X + C2) | C
2162// (X + C2) ^ C
2163// (X + C2) & C
2164// and convert to do the bitwise logic first:
2165// (X | C) + C2
2166// (X ^ C) + C2
2167// (X & C) + C2
2168// iff bits affected by logic op are lower than last bit affected by math op
2170 InstCombiner::BuilderTy &Builder) {
2171 Type *Ty = I.getType();
2172 Instruction::BinaryOps OpC = I.getOpcode();
2173 Value *Op0 = I.getOperand(0);
2174 Value *Op1 = I.getOperand(1);
2175 Value *X;
2176 const APInt *C, *C2;
2177
2178 if (!(match(Op0, m_OneUse(m_Add(m_Value(X), m_APInt(C2)))) &&
2179 match(Op1, m_APInt(C))))
2180 return nullptr;
2181
2182 unsigned Width = Ty->getScalarSizeInBits();
2183 unsigned LastOneMath = Width - C2->countr_zero();
2184
2185 switch (OpC) {
2186 case Instruction::And:
2187 if (C->countl_one() < LastOneMath)
2188 return nullptr;
2189 break;
2190 case Instruction::Xor:
2191 case Instruction::Or:
2192 if (C->countl_zero() < LastOneMath)
2193 return nullptr;
2194 break;
2195 default:
2196 llvm_unreachable("Unexpected BinaryOp!");
2197 }
2198
2199 Value *NewBinOp = Builder.CreateBinOp(OpC, X, ConstantInt::get(Ty, *C));
2200 return BinaryOperator::CreateWithCopiedFlags(Instruction::Add, NewBinOp,
2201 ConstantInt::get(Ty, *C2), Op0);
2202}
2203
2204// binop(shift(ShiftedC1, ShAmt), shift(ShiftedC2, add(ShAmt, AddC))) ->
2205// shift(binop(ShiftedC1, shift(ShiftedC2, AddC)), ShAmt)
2206// where both shifts are the same and AddC is a valid shift amount.
2207Instruction *InstCombinerImpl::foldBinOpOfDisplacedShifts(BinaryOperator &I) {
2208 assert((I.isBitwiseLogicOp() || I.getOpcode() == Instruction::Add) &&
2209 "Unexpected opcode");
2210
2211 Value *ShAmt;
2212 Constant *ShiftedC1, *ShiftedC2, *AddC;
2213 Type *Ty = I.getType();
2214 unsigned BitWidth = Ty->getScalarSizeInBits();
2215 if (!match(&I, m_c_BinOp(m_Shift(m_ImmConstant(ShiftedC1), m_Value(ShAmt)),
2216 m_Shift(m_ImmConstant(ShiftedC2),
2217 m_AddLike(m_Deferred(ShAmt),
2218 m_ImmConstant(AddC))))))
2219 return nullptr;
2220
2221 // Make sure the add constant is a valid shift amount.
2222 if (!match(AddC,
2224 return nullptr;
2225
2226 // Avoid constant expressions.
2227 auto *Op0Inst = dyn_cast<Instruction>(I.getOperand(0));
2228 auto *Op1Inst = dyn_cast<Instruction>(I.getOperand(1));
2229 if (!Op0Inst || !Op1Inst)
2230 return nullptr;
2231
2232 // Both shifts must be the same.
2233 Instruction::BinaryOps ShiftOp =
2234 static_cast<Instruction::BinaryOps>(Op0Inst->getOpcode());
2235 if (ShiftOp != Op1Inst->getOpcode())
2236 return nullptr;
2237
2238 // For adds, only left shifts are supported.
2239 if (I.getOpcode() == Instruction::Add && ShiftOp != Instruction::Shl)
2240 return nullptr;
2241
2242 Value *NewC = Builder.CreateBinOp(
2243 I.getOpcode(), ShiftedC1, Builder.CreateBinOp(ShiftOp, ShiftedC2, AddC));
2244 return BinaryOperator::Create(ShiftOp, NewC, ShAmt);
2245}
2246
2247// Fold and/or/xor with two equal intrinsic IDs:
2248// bitwise(fshl (A, B, ShAmt), fshl(C, D, ShAmt))
2249// -> fshl(bitwise(A, C), bitwise(B, D), ShAmt)
2250// bitwise(fshr (A, B, ShAmt), fshr(C, D, ShAmt))
2251// -> fshr(bitwise(A, C), bitwise(B, D), ShAmt)
2252// bitwise(bswap(A), bswap(B)) -> bswap(bitwise(A, B))
2253// bitwise(bswap(A), C) -> bswap(bitwise(A, bswap(C)))
2254// bitwise(bitreverse(A), bitreverse(B)) -> bitreverse(bitwise(A, B))
2255// bitwise(bitreverse(A), C) -> bitreverse(bitwise(A, bitreverse(C)))
2256static Instruction *
2258 InstCombiner::BuilderTy &Builder) {
2259 assert(I.isBitwiseLogicOp() && "Should and/or/xor");
2260 if (!I.getOperand(0)->hasOneUse())
2261 return nullptr;
2262 IntrinsicInst *X = dyn_cast<IntrinsicInst>(I.getOperand(0));
2263 if (!X)
2264 return nullptr;
2265
2266 IntrinsicInst *Y = dyn_cast<IntrinsicInst>(I.getOperand(1));
2267 if (Y && (!Y->hasOneUse() || X->getIntrinsicID() != Y->getIntrinsicID()))
2268 return nullptr;
2269
2270 Intrinsic::ID IID = X->getIntrinsicID();
2271 const APInt *RHSC;
2272 // Try to match constant RHS.
2273 if (!Y && (!(IID == Intrinsic::bswap || IID == Intrinsic::bitreverse) ||
2274 !match(I.getOperand(1), m_APInt(RHSC))))
2275 return nullptr;
2276
2277 switch (IID) {
2278 case Intrinsic::fshl:
2279 case Intrinsic::fshr: {
2280 if (X->getOperand(2) != Y->getOperand(2))
2281 return nullptr;
2282 Value *NewOp0 =
2283 Builder.CreateBinOp(I.getOpcode(), X->getOperand(0), Y->getOperand(0));
2284 Value *NewOp1 =
2285 Builder.CreateBinOp(I.getOpcode(), X->getOperand(1), Y->getOperand(1));
2286 Function *F =
2287 Intrinsic::getOrInsertDeclaration(I.getModule(), IID, I.getType());
2288 return CallInst::Create(F, {NewOp0, NewOp1, X->getOperand(2)});
2289 }
2290 case Intrinsic::bswap:
2291 case Intrinsic::bitreverse: {
2292 Value *NewOp0 = Builder.CreateBinOp(
2293 I.getOpcode(), X->getOperand(0),
2294 Y ? Y->getOperand(0)
2295 : ConstantInt::get(I.getType(), IID == Intrinsic::bswap
2296 ? RHSC->byteSwap()
2297 : RHSC->reverseBits()));
2298 Function *F =
2299 Intrinsic::getOrInsertDeclaration(I.getModule(), IID, I.getType());
2300 return CallInst::Create(F, {NewOp0});
2301 }
2302 default:
2303 return nullptr;
2304 }
2305}
2306
2307// Try to simplify V by replacing occurrences of Op with RepOp, but only look
2308// through bitwise operations. In particular, for X | Y we try to replace Y with
2309// 0 inside X and for X & Y we try to replace Y with -1 inside X.
2310// Return the simplified result of X if successful, and nullptr otherwise.
2311// If SimplifyOnly is true, no new instructions will be created.
2313 bool SimplifyOnly,
2314 InstCombinerImpl &IC,
2315 unsigned Depth = 0) {
2316 if (Op == RepOp)
2317 return nullptr;
2318
2319 if (V == Op)
2320 return RepOp;
2321
2322 auto *I = dyn_cast<BinaryOperator>(V);
2323 if (!I || !I->isBitwiseLogicOp() || Depth >= 3)
2324 return nullptr;
2325
2326 if (!I->hasOneUse())
2327 SimplifyOnly = true;
2328
2329 Value *NewOp0 = simplifyAndOrWithOpReplaced(I->getOperand(0), Op, RepOp,
2330 SimplifyOnly, IC, Depth + 1);
2331 Value *NewOp1 = simplifyAndOrWithOpReplaced(I->getOperand(1), Op, RepOp,
2332 SimplifyOnly, IC, Depth + 1);
2333 if (!NewOp0 && !NewOp1)
2334 return nullptr;
2335
2336 if (!NewOp0)
2337 NewOp0 = I->getOperand(0);
2338 if (!NewOp1)
2339 NewOp1 = I->getOperand(1);
2340
2341 if (Value *Res = simplifyBinOp(I->getOpcode(), NewOp0, NewOp1,
2343 return Res;
2344
2345 if (SimplifyOnly)
2346 return nullptr;
2347 return IC.Builder.CreateBinOp(I->getOpcode(), NewOp0, NewOp1);
2348}
2349
2350/// Reassociate and/or expressions to see if we can fold the inner and/or ops.
2351/// TODO: Make this recursive; it's a little tricky because an arbitrary
2352/// number of and/or instructions might have to be created.
2353Value *InstCombinerImpl::reassociateBooleanAndOr(Value *LHS, Value *X, Value *Y,
2354 Instruction &I, bool IsAnd,
2355 bool RHSIsLogical) {
2356 Instruction::BinaryOps Opcode = IsAnd ? Instruction::And : Instruction::Or;
2357 // LHS bop (X lop Y) --> (LHS bop X) lop Y
2358 // LHS bop (X bop Y) --> (LHS bop X) bop Y
2359 if (Value *Res = foldBooleanAndOr(LHS, X, I, IsAnd, /*IsLogical=*/false))
2360 return RHSIsLogical ? Builder.CreateLogicalOp(Opcode, Res, Y)
2361 : Builder.CreateBinOp(Opcode, Res, Y);
2362 // LHS bop (X bop Y) --> X bop (LHS bop Y)
2363 // LHS bop (X lop Y) --> X lop (LHS bop Y)
2364 if (Value *Res = foldBooleanAndOr(LHS, Y, I, IsAnd, /*IsLogical=*/false))
2365 return RHSIsLogical ? Builder.CreateLogicalOp(Opcode, X, Res)
2366 : Builder.CreateBinOp(Opcode, X, Res);
2367 return nullptr;
2368}
2369
2370// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2371// here. We should standardize that construct where it is needed or choose some
2372// other way to ensure that commutated variants of patterns are not missed.
2374 Type *Ty = I.getType();
2375
2376 if (Value *V = simplifyAndInst(I.getOperand(0), I.getOperand(1),
2378 return replaceInstUsesWith(I, V);
2379
2381 return &I;
2382
2384 return X;
2385
2387 return Phi;
2388
2389 // See if we can simplify any instructions used by the instruction whose sole
2390 // purpose is to compute bits we don't care about.
2392 return &I;
2393
2394 // Do this before using distributive laws to catch simple and/or/not patterns.
2396 return Xor;
2397
2399 return X;
2400
2401 // (A|B)&(A|C) -> A|(B&C) etc
2403 return replaceInstUsesWith(I, V);
2404
2406 return R;
2407
2408 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2409
2410 Value *X, *Y;
2411 const APInt *C;
2412 if ((match(Op0, m_OneUse(m_LogicalShift(m_One(), m_Value(X)))) ||
2413 (match(Op0, m_OneUse(m_Shl(m_APInt(C), m_Value(X)))) && (*C)[0])) &&
2414 match(Op1, m_One())) {
2415 // (1 >> X) & 1 --> zext(X == 0)
2416 // (C << X) & 1 --> zext(X == 0), when C is odd
2417 Value *IsZero = Builder.CreateICmpEQ(X, ConstantInt::get(Ty, 0));
2418 return new ZExtInst(IsZero, Ty);
2419 }
2420
2421 // (-(X & 1)) & Y --> (X & 1) == 0 ? 0 : Y
2422 Value *Neg;
2423 if (match(&I,
2425 m_OneUse(m_Neg(m_And(m_Value(), m_One())))),
2426 m_Value(Y)))) {
2427 Value *Cmp = Builder.CreateIsNull(Neg);
2429 }
2430
2431 // Canonicalize:
2432 // (X +/- Y) & Y --> ~X & Y when Y is a power of 2.
2435 m_Sub(m_Value(X), m_Deferred(Y)))))) &&
2436 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, /*Depth*/ 0, &I))
2437 return BinaryOperator::CreateAnd(Builder.CreateNot(X), Y);
2438
2439 if (match(Op1, m_APInt(C))) {
2440 const APInt *XorC;
2441 if (match(Op0, m_OneUse(m_Xor(m_Value(X), m_APInt(XorC))))) {
2442 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
2443 Constant *NewC = ConstantInt::get(Ty, *C & *XorC);
2444 Value *And = Builder.CreateAnd(X, Op1);
2445 And->takeName(Op0);
2446 return BinaryOperator::CreateXor(And, NewC);
2447 }
2448
2449 const APInt *OrC;
2450 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_APInt(OrC))))) {
2451 // (X | C1) & C2 --> (X & C2^(C1&C2)) | (C1&C2)
2452 // NOTE: This reduces the number of bits set in the & mask, which
2453 // can expose opportunities for store narrowing for scalars.
2454 // NOTE: SimplifyDemandedBits should have already removed bits from C1
2455 // that aren't set in C2. Meaning we can replace (C1&C2) with C1 in
2456 // above, but this feels safer.
2457 APInt Together = *C & *OrC;
2458 Value *And = Builder.CreateAnd(X, ConstantInt::get(Ty, Together ^ *C));
2459 And->takeName(Op0);
2460 return BinaryOperator::CreateOr(And, ConstantInt::get(Ty, Together));
2461 }
2462
2463 unsigned Width = Ty->getScalarSizeInBits();
2464 const APInt *ShiftC;
2465 if (match(Op0, m_OneUse(m_SExt(m_AShr(m_Value(X), m_APInt(ShiftC))))) &&
2466 ShiftC->ult(Width)) {
2467 if (*C == APInt::getLowBitsSet(Width, Width - ShiftC->getZExtValue())) {
2468 // We are clearing high bits that were potentially set by sext+ashr:
2469 // and (sext (ashr X, ShiftC)), C --> lshr (sext X), ShiftC
2470 Value *Sext = Builder.CreateSExt(X, Ty);
2471 Constant *ShAmtC = ConstantInt::get(Ty, ShiftC->zext(Width));
2472 return BinaryOperator::CreateLShr(Sext, ShAmtC);
2473 }
2474 }
2475
2476 // If this 'and' clears the sign-bits added by ashr, replace with lshr:
2477 // and (ashr X, ShiftC), C --> lshr X, ShiftC
2478 if (match(Op0, m_AShr(m_Value(X), m_APInt(ShiftC))) && ShiftC->ult(Width) &&
2479 C->isMask(Width - ShiftC->getZExtValue()))
2480 return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, *ShiftC));
2481
2482 const APInt *AddC;
2483 if (match(Op0, m_Add(m_Value(X), m_APInt(AddC)))) {
2484 // If we are masking the result of the add down to exactly one bit and
2485 // the constant we are adding has no bits set below that bit, then the
2486 // add is flipping a single bit. Example:
2487 // (X + 4) & 4 --> (X & 4) ^ 4
2488 if (Op0->hasOneUse() && C->isPowerOf2() && (*AddC & (*C - 1)) == 0) {
2489 assert((*C & *AddC) != 0 && "Expected common bit");
2490 Value *NewAnd = Builder.CreateAnd(X, Op1);
2491 return BinaryOperator::CreateXor(NewAnd, Op1);
2492 }
2493 }
2494
2495 // ((C1 OP zext(X)) & C2) -> zext((C1 OP X) & C2) if C2 fits in the
2496 // bitwidth of X and OP behaves well when given trunc(C1) and X.
2497 auto isNarrowableBinOpcode = [](BinaryOperator *B) {
2498 switch (B->getOpcode()) {
2499 case Instruction::Xor:
2500 case Instruction::Or:
2501 case Instruction::Mul:
2502 case Instruction::Add:
2503 case Instruction::Sub:
2504 return true;
2505 default:
2506 return false;
2507 }
2508 };
2509 BinaryOperator *BO;
2510 if (match(Op0, m_OneUse(m_BinOp(BO))) && isNarrowableBinOpcode(BO)) {
2511 Instruction::BinaryOps BOpcode = BO->getOpcode();
2512 Value *X;
2513 const APInt *C1;
2514 // TODO: The one-use restrictions could be relaxed a little if the AND
2515 // is going to be removed.
2516 // Try to narrow the 'and' and a binop with constant operand:
2517 // and (bo (zext X), C1), C --> zext (and (bo X, TruncC1), TruncC)
2518 if (match(BO, m_c_BinOp(m_OneUse(m_ZExt(m_Value(X))), m_APInt(C1))) &&
2519 C->isIntN(X->getType()->getScalarSizeInBits())) {
2520 unsigned XWidth = X->getType()->getScalarSizeInBits();
2521 Constant *TruncC1 = ConstantInt::get(X->getType(), C1->trunc(XWidth));
2522 Value *BinOp = isa<ZExtInst>(BO->getOperand(0))
2523 ? Builder.CreateBinOp(BOpcode, X, TruncC1)
2524 : Builder.CreateBinOp(BOpcode, TruncC1, X);
2525 Constant *TruncC = ConstantInt::get(X->getType(), C->trunc(XWidth));
2526 Value *And = Builder.CreateAnd(BinOp, TruncC);
2527 return new ZExtInst(And, Ty);
2528 }
2529
2530 // Similar to above: if the mask matches the zext input width, then the
2531 // 'and' can be eliminated, so we can truncate the other variable op:
2532 // and (bo (zext X), Y), C --> zext (bo X, (trunc Y))
2533 if (isa<Instruction>(BO->getOperand(0)) &&
2534 match(BO->getOperand(0), m_OneUse(m_ZExt(m_Value(X)))) &&
2535 C->isMask(X->getType()->getScalarSizeInBits())) {
2536 Y = BO->getOperand(1);
2537 Value *TrY = Builder.CreateTrunc(Y, X->getType(), Y->getName() + ".tr");
2538 Value *NewBO =
2539 Builder.CreateBinOp(BOpcode, X, TrY, BO->getName() + ".narrow");
2540 return new ZExtInst(NewBO, Ty);
2541 }
2542 // and (bo Y, (zext X)), C --> zext (bo (trunc Y), X)
2543 if (isa<Instruction>(BO->getOperand(1)) &&
2544 match(BO->getOperand(1), m_OneUse(m_ZExt(m_Value(X)))) &&
2545 C->isMask(X->getType()->getScalarSizeInBits())) {
2546 Y = BO->getOperand(0);
2547 Value *TrY = Builder.CreateTrunc(Y, X->getType(), Y->getName() + ".tr");
2548 Value *NewBO =
2549 Builder.CreateBinOp(BOpcode, TrY, X, BO->getName() + ".narrow");
2550 return new ZExtInst(NewBO, Ty);
2551 }
2552 }
2553
2554 // This is intentionally placed after the narrowing transforms for
2555 // efficiency (transform directly to the narrow logic op if possible).
2556 // If the mask is only needed on one incoming arm, push the 'and' op up.
2557 if (match(Op0, m_OneUse(m_Xor(m_Value(X), m_Value(Y)))) ||
2558 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2559 APInt NotAndMask(~(*C));
2560 BinaryOperator::BinaryOps BinOp = cast<BinaryOperator>(Op0)->getOpcode();
2561 if (MaskedValueIsZero(X, NotAndMask, 0, &I)) {
2562 // Not masking anything out for the LHS, move mask to RHS.
2563 // and ({x}or X, Y), C --> {x}or X, (and Y, C)
2564 Value *NewRHS = Builder.CreateAnd(Y, Op1, Y->getName() + ".masked");
2565 return BinaryOperator::Create(BinOp, X, NewRHS);
2566 }
2567 if (!isa<Constant>(Y) && MaskedValueIsZero(Y, NotAndMask, 0, &I)) {
2568 // Not masking anything out for the RHS, move mask to LHS.
2569 // and ({x}or X, Y), C --> {x}or (and X, C), Y
2570 Value *NewLHS = Builder.CreateAnd(X, Op1, X->getName() + ".masked");
2571 return BinaryOperator::Create(BinOp, NewLHS, Y);
2572 }
2573 }
2574
2575 // When the mask is a power-of-2 constant and op0 is a shifted-power-of-2
2576 // constant, test if the shift amount equals the offset bit index:
2577 // (ShiftC << X) & C --> X == (log2(C) - log2(ShiftC)) ? C : 0
2578 // (ShiftC >> X) & C --> X == (log2(ShiftC) - log2(C)) ? C : 0
2579 if (C->isPowerOf2() &&
2580 match(Op0, m_OneUse(m_LogicalShift(m_Power2(ShiftC), m_Value(X))))) {
2581 int Log2ShiftC = ShiftC->exactLogBase2();
2582 int Log2C = C->exactLogBase2();
2583 bool IsShiftLeft =
2584 cast<BinaryOperator>(Op0)->getOpcode() == Instruction::Shl;
2585 int BitNum = IsShiftLeft ? Log2C - Log2ShiftC : Log2ShiftC - Log2C;
2586 assert(BitNum >= 0 && "Expected demanded bits to handle impossible mask");
2587 Value *Cmp = Builder.CreateICmpEQ(X, ConstantInt::get(Ty, BitNum));
2588 return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C),
2590 }
2591
2592 Constant *C1, *C2;
2593 const APInt *C3 = C;
2594 Value *X;
2595 if (C3->isPowerOf2()) {
2596 Constant *Log2C3 = ConstantInt::get(Ty, C3->countr_zero());
2598 m_ImmConstant(C2)))) &&
2599 match(C1, m_Power2())) {
2601 Constant *LshrC = ConstantExpr::getAdd(C2, Log2C3);
2602 KnownBits KnownLShrc = computeKnownBits(LshrC, 0, nullptr);
2603 if (KnownLShrc.getMaxValue().ult(Width)) {
2604 // iff C1,C3 is pow2 and C2 + cttz(C3) < BitWidth:
2605 // ((C1 << X) >> C2) & C3 -> X == (cttz(C3)+C2-cttz(C1)) ? C3 : 0
2606 Constant *CmpC = ConstantExpr::getSub(LshrC, Log2C1);
2607 Value *Cmp = Builder.CreateICmpEQ(X, CmpC);
2608 return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C3),
2610 }
2611 }
2612
2614 m_ImmConstant(C2)))) &&
2615 match(C1, m_Power2())) {
2617 Constant *Cmp =
2619 if (Cmp && Cmp->isZeroValue()) {
2620 // iff C1,C3 is pow2 and Log2(C3) >= C2:
2621 // ((C1 >> X) << C2) & C3 -> X == (cttz(C1)+C2-cttz(C3)) ? C3 : 0
2622 Constant *ShlC = ConstantExpr::getAdd(C2, Log2C1);
2623 Constant *CmpC = ConstantExpr::getSub(ShlC, Log2C3);
2624 Value *Cmp = Builder.CreateICmpEQ(X, CmpC);
2625 return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C3),
2627 }
2628 }
2629 }
2630 }
2631
2632 // If we are clearing the sign bit of a floating-point value, convert this to
2633 // fabs, then cast back to integer.
2634 //
2635 // This is a generous interpretation for noimplicitfloat, this is not a true
2636 // floating-point operation.
2637 //
2638 // Assumes any IEEE-represented type has the sign bit in the high bit.
2639 // TODO: Unify with APInt matcher. This version allows undef unlike m_APInt
2640 Value *CastOp;
2641 if (match(Op0, m_ElementWiseBitCast(m_Value(CastOp))) &&
2642 match(Op1, m_MaxSignedValue()) &&
2644 Attribute::NoImplicitFloat)) {
2645 Type *EltTy = CastOp->getType()->getScalarType();
2646 if (EltTy->isFloatingPointTy() && EltTy->isIEEE()) {
2647 Value *FAbs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, CastOp);
2648 return new BitCastInst(FAbs, I.getType());
2649 }
2650 }
2651
2652 // and(shl(zext(X), Y), SignMask) -> and(sext(X), SignMask)
2653 // where Y is a valid shift amount.
2655 m_SignMask())) &&
2659 Ty->getScalarSizeInBits() -
2660 X->getType()->getScalarSizeInBits())))) {
2661 auto *SExt = Builder.CreateSExt(X, Ty, X->getName() + ".signext");
2662 return BinaryOperator::CreateAnd(SExt, Op1);
2663 }
2664
2665 if (Instruction *Z = narrowMaskedBinOp(I))
2666 return Z;
2667
2668 if (I.getType()->isIntOrIntVectorTy(1)) {
2669 if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
2670 if (auto *R =
2671 foldAndOrOfSelectUsingImpliedCond(Op1, *SI0, /* IsAnd */ true))
2672 return R;
2673 }
2674 if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
2675 if (auto *R =
2676 foldAndOrOfSelectUsingImpliedCond(Op0, *SI1, /* IsAnd */ true))
2677 return R;
2678 }
2679 }
2680
2681 if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
2682 return FoldedLogic;
2683
2684 if (Instruction *DeMorgan = matchDeMorgansLaws(I, *this))
2685 return DeMorgan;
2686
2687 {
2688 Value *A, *B, *C;
2689 // A & ~(A ^ B) --> A & B
2690 if (match(Op1, m_Not(m_c_Xor(m_Specific(Op0), m_Value(B)))))
2691 return BinaryOperator::CreateAnd(Op0, B);
2692 // ~(A ^ B) & A --> A & B
2693 if (match(Op0, m_Not(m_c_Xor(m_Specific(Op1), m_Value(B)))))
2694 return BinaryOperator::CreateAnd(Op1, B);
2695
2696 // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
2697 if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
2698 match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A)))) {
2699 Value *NotC = Op1->hasOneUse()
2701 : getFreelyInverted(C, C->hasOneUse(), &Builder);
2702 if (NotC != nullptr)
2703 return BinaryOperator::CreateAnd(Op0, NotC);
2704 }
2705
2706 // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
2707 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))) &&
2708 match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) {
2709 Value *NotC = Op0->hasOneUse()
2711 : getFreelyInverted(C, C->hasOneUse(), &Builder);
2712 if (NotC != nullptr)
2713 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
2714 }
2715
2716 // (A | B) & (~A ^ B) -> A & B
2717 // (A | B) & (B ^ ~A) -> A & B
2718 // (B | A) & (~A ^ B) -> A & B
2719 // (B | A) & (B ^ ~A) -> A & B
2720 if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2721 match(Op0, m_c_Or(m_Specific(A), m_Specific(B))))
2722 return BinaryOperator::CreateAnd(A, B);
2723
2724 // (~A ^ B) & (A | B) -> A & B
2725 // (~A ^ B) & (B | A) -> A & B
2726 // (B ^ ~A) & (A | B) -> A & B
2727 // (B ^ ~A) & (B | A) -> A & B
2728 if (match(Op0, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2729 match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
2730 return BinaryOperator::CreateAnd(A, B);
2731
2732 // (~A | B) & (A ^ B) -> ~A & B
2733 // (~A | B) & (B ^ A) -> ~A & B
2734 // (B | ~A) & (A ^ B) -> ~A & B
2735 // (B | ~A) & (B ^ A) -> ~A & B
2736 if (match(Op0, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2738 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
2739
2740 // (A ^ B) & (~A | B) -> ~A & B
2741 // (B ^ A) & (~A | B) -> ~A & B
2742 // (A ^ B) & (B | ~A) -> ~A & B
2743 // (B ^ A) & (B | ~A) -> ~A & B
2744 if (match(Op1, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2746 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
2747 }
2748
2749 if (Value *Res =
2750 foldBooleanAndOr(Op0, Op1, I, /*IsAnd=*/true, /*IsLogical=*/false))
2751 return replaceInstUsesWith(I, Res);
2752
2753 if (match(Op1, m_OneUse(m_LogicalAnd(m_Value(X), m_Value(Y))))) {
2754 bool IsLogical = isa<SelectInst>(Op1);
2755 if (auto *V = reassociateBooleanAndOr(Op0, X, Y, I, /*IsAnd=*/true,
2756 /*RHSIsLogical=*/IsLogical))
2757 return replaceInstUsesWith(I, V);
2758 }
2759 if (match(Op0, m_OneUse(m_LogicalAnd(m_Value(X), m_Value(Y))))) {
2760 bool IsLogical = isa<SelectInst>(Op0);
2761 if (auto *V = reassociateBooleanAndOr(Op1, X, Y, I, /*IsAnd=*/true,
2762 /*RHSIsLogical=*/IsLogical))
2763 return replaceInstUsesWith(I, V);
2764 }
2765
2766 if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder))
2767 return FoldedFCmps;
2768
2769 if (Instruction *CastedAnd = foldCastedBitwiseLogic(I))
2770 return CastedAnd;
2771
2772 if (Instruction *Sel = foldBinopOfSextBoolToSelect(I))
2773 return Sel;
2774
2775 // and(sext(A), B) / and(B, sext(A)) --> A ? B : 0, where A is i1 or <N x i1>.
2776 // TODO: Move this into foldBinopOfSextBoolToSelect as a more generalized fold
2777 // with binop identity constant. But creating a select with non-constant
2778 // arm may not be reversible due to poison semantics. Is that a good
2779 // canonicalization?
2780 Value *A, *B;
2781 if (match(&I, m_c_And(m_SExt(m_Value(A)), m_Value(B))) &&
2782 A->getType()->isIntOrIntVectorTy(1))
2784
2785 // Similarly, a 'not' of the bool translates to a swap of the select arms:
2786 // ~sext(A) & B / B & ~sext(A) --> A ? 0 : B
2787 if (match(&I, m_c_And(m_Not(m_SExt(m_Value(A))), m_Value(B))) &&
2788 A->getType()->isIntOrIntVectorTy(1))
2790
2791 // and(zext(A), B) -> A ? (B & 1) : 0
2792 if (match(&I, m_c_And(m_OneUse(m_ZExt(m_Value(A))), m_Value(B))) &&
2793 A->getType()->isIntOrIntVectorTy(1))
2794 return SelectInst::Create(A, Builder.CreateAnd(B, ConstantInt::get(Ty, 1)),
2796
2797 // (-1 + A) & B --> A ? 0 : B where A is 0/1.
2799 m_Value(B)))) {
2800 if (A->getType()->isIntOrIntVectorTy(1))
2802 if (computeKnownBits(A, /* Depth */ 0, &I).countMaxActiveBits() <= 1) {
2803 return SelectInst::Create(
2806 }
2807 }
2808
2809 // (iN X s>> (N-1)) & Y --> (X s< 0) ? Y : 0 -- with optional sext
2812 m_Value(Y))) &&
2813 *C == X->getType()->getScalarSizeInBits() - 1) {
2814 Value *IsNeg = Builder.CreateIsNeg(X, "isneg");
2816 }
2817 // If there's a 'not' of the shifted value, swap the select operands:
2818 // ~(iN X s>> (N-1)) & Y --> (X s< 0) ? 0 : Y -- with optional sext
2821 m_Value(Y))) &&
2822 *C == X->getType()->getScalarSizeInBits() - 1) {
2823 Value *IsNeg = Builder.CreateIsNeg(X, "isneg");
2825 }
2826
2827 // (~x) & y --> ~(x | (~y)) iff that gets rid of inversions
2829 return &I;
2830
2831 // An and recurrence w/loop invariant step is equivelent to (and start, step)
2832 PHINode *PN = nullptr;
2833 Value *Start = nullptr, *Step = nullptr;
2834 if (matchSimpleRecurrence(&I, PN, Start, Step) && DT.dominates(Step, PN))
2835 return replaceInstUsesWith(I, Builder.CreateAnd(Start, Step));
2836
2838 return R;
2839
2840 if (Instruction *Canonicalized = canonicalizeLogicFirst(I, Builder))
2841 return Canonicalized;
2842
2843 if (Instruction *Folded = foldLogicOfIsFPClass(I, Op0, Op1))
2844 return Folded;
2845
2846 if (Instruction *Res = foldBinOpOfDisplacedShifts(I))
2847 return Res;
2848
2850 return Res;
2851
2852 if (Value *V =
2854 /*SimplifyOnly*/ false, *this))
2855 return BinaryOperator::CreateAnd(V, Op1);
2856 if (Value *V =
2858 /*SimplifyOnly*/ false, *this))
2859 return BinaryOperator::CreateAnd(Op0, V);
2860
2861 return nullptr;
2862}
2863
2865 bool MatchBSwaps,
2866 bool MatchBitReversals) {
2868 if (!recognizeBSwapOrBitReverseIdiom(&I, MatchBSwaps, MatchBitReversals,
2869 Insts))
2870 return nullptr;
2871 Instruction *LastInst = Insts.pop_back_val();
2872 LastInst->removeFromParent();
2873
2874 for (auto *Inst : Insts) {
2875 Inst->setDebugLoc(I.getDebugLoc());
2876 Worklist.push(Inst);
2877 }
2878 return LastInst;
2879}
2880
2881std::optional<std::pair<Intrinsic::ID, SmallVector<Value *, 3>>>
2883 // TODO: Can we reduce the code duplication between this and the related
2884 // rotate matching code under visitSelect and visitTrunc?
2885 assert(Or.getOpcode() == BinaryOperator::Or && "Expecting or instruction");
2886
2887 unsigned Width = Or.getType()->getScalarSizeInBits();
2888
2889 Instruction *Or0, *Or1;
2890 if (!match(Or.getOperand(0), m_Instruction(Or0)) ||
2891 !match(Or.getOperand(1), m_Instruction(Or1)))
2892 return std::nullopt;
2893
2894 bool IsFshl = true; // Sub on LSHR.
2895 SmallVector<Value *, 3> FShiftArgs;
2896
2897 // First, find an or'd pair of opposite shifts:
2898 // or (lshr ShVal0, ShAmt0), (shl ShVal1, ShAmt1)
2899 if (isa<BinaryOperator>(Or0) && isa<BinaryOperator>(Or1)) {
2900 Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1;
2901 if (!match(Or0,
2902 m_OneUse(m_LogicalShift(m_Value(ShVal0), m_Value(ShAmt0)))) ||
2903 !match(Or1,
2904 m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1)))) ||
2905 Or0->getOpcode() == Or1->getOpcode())
2906 return std::nullopt;
2907
2908 // Canonicalize to or(shl(ShVal0, ShAmt0), lshr(ShVal1, ShAmt1)).
2909 if (Or0->getOpcode() == BinaryOperator::LShr) {
2910 std::swap(Or0, Or1);
2911 std::swap(ShVal0, ShVal1);
2912 std::swap(ShAmt0, ShAmt1);
2913 }
2914 assert(Or0->getOpcode() == BinaryOperator::Shl &&
2915 Or1->getOpcode() == BinaryOperator::LShr &&
2916 "Illegal or(shift,shift) pair");
2917
2918 // Match the shift amount operands for a funnel shift pattern. This always
2919 // matches a subtraction on the R operand.
2920 auto matchShiftAmount = [&](Value *L, Value *R, unsigned Width) -> Value * {
2921 // Check for constant shift amounts that sum to the bitwidth.
2922 const APInt *LI, *RI;
2923 if (match(L, m_APIntAllowPoison(LI)) && match(R, m_APIntAllowPoison(RI)))
2924 if (LI->ult(Width) && RI->ult(Width) && (*LI + *RI) == Width)
2925 return ConstantInt::get(L->getType(), *LI);
2926
2927 Constant *LC, *RC;
2928 if (match(L, m_Constant(LC)) && match(R, m_Constant(RC)) &&
2929 match(L,
2930 m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2931 match(R,
2932 m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2934 return ConstantExpr::mergeUndefsWith(LC, RC);
2935
2936 // (shl ShVal, X) | (lshr ShVal, (Width - x)) iff X < Width.
2937 // We limit this to X < Width in case the backend re-expands the
2938 // intrinsic, and has to reintroduce a shift modulo operation (InstCombine
2939 // might remove it after this fold). This still doesn't guarantee that the
2940 // final codegen will match this original pattern.
2941 if (match(R, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(L))))) {
2942 KnownBits KnownL = computeKnownBits(L, /*Depth*/ 0, &Or);
2943 return KnownL.getMaxValue().ult(Width) ? L : nullptr;
2944 }
2945
2946 // For non-constant cases, the following patterns currently only work for
2947 // rotation patterns.
2948 // TODO: Add general funnel-shift compatible patterns.
2949 if (ShVal0 != ShVal1)
2950 return nullptr;
2951
2952 // For non-constant cases we don't support non-pow2 shift masks.
2953 // TODO: Is it worth matching urem as well?
2954 if (!isPowerOf2_32(Width))
2955 return nullptr;
2956
2957 // The shift amount may be masked with negation:
2958 // (shl ShVal, (X & (Width - 1))) | (lshr ShVal, ((-X) & (Width - 1)))
2959 Value *X;
2960 unsigned Mask = Width - 1;
2961 if (match(L, m_And(m_Value(X), m_SpecificInt(Mask))) &&
2962 match(R, m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask))))
2963 return X;
2964
2965 // (shl ShVal, X) | (lshr ShVal, ((-X) & (Width - 1)))
2966 if (match(R, m_And(m_Neg(m_Specific(L)), m_SpecificInt(Mask))))
2967 return L;
2968
2969 // Similar to above, but the shift amount may be extended after masking,
2970 // so return the extended value as the parameter for the intrinsic.
2971 if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
2972 match(R,
2974 m_SpecificInt(Mask))))
2975 return L;
2976
2977 if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
2979 return L;
2980
2981 return nullptr;
2982 };
2983
2984 Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, Width);
2985 if (!ShAmt) {
2986 ShAmt = matchShiftAmount(ShAmt1, ShAmt0, Width);
2987 IsFshl = false; // Sub on SHL.
2988 }
2989 if (!ShAmt)
2990 return std::nullopt;
2991
2992 FShiftArgs = {ShVal0, ShVal1, ShAmt};
2993 } else if (isa<ZExtInst>(Or0) || isa<ZExtInst>(Or1)) {
2994 // If there are two 'or' instructions concat variables in opposite order:
2995 //
2996 // Slot1 and Slot2 are all zero bits.
2997 // | Slot1 | Low | Slot2 | High |
2998 // LowHigh = or (shl (zext Low), ZextLowShlAmt), (zext High)
2999 // | Slot2 | High | Slot1 | Low |
3000 // HighLow = or (shl (zext High), ZextHighShlAmt), (zext Low)
3001 //
3002 // the latter 'or' can be safely convert to
3003 // -> HighLow = fshl LowHigh, LowHigh, ZextHighShlAmt
3004 // if ZextLowShlAmt + ZextHighShlAmt == Width.
3005 if (!isa<ZExtInst>(Or1))
3006 std::swap(Or0, Or1);
3007
3008 Value *High, *ZextHigh, *Low;
3009 const APInt *ZextHighShlAmt;
3010 if (!match(Or0,
3011 m_OneUse(m_Shl(m_Value(ZextHigh), m_APInt(ZextHighShlAmt)))))
3012 return std::nullopt;
3013
3014 if (!match(Or1, m_ZExt(m_Value(Low))) ||
3015 !match(ZextHigh, m_ZExt(m_Value(High))))
3016 return std::nullopt;
3017
3018 unsigned HighSize = High->getType()->getScalarSizeInBits();
3019 unsigned LowSize = Low->getType()->getScalarSizeInBits();
3020 // Make sure High does not overlap with Low and most significant bits of
3021 // High aren't shifted out.
3022 if (ZextHighShlAmt->ult(LowSize) || ZextHighShlAmt->ugt(Width - HighSize))
3023 return std::nullopt;
3024
3025 for (User *U : ZextHigh->users()) {
3026 Value *X, *Y;
3027 if (!match(U, m_Or(m_Value(X), m_Value(Y))))
3028 continue;
3029
3030 if (!isa<ZExtInst>(Y))
3031 std::swap(X, Y);
3032
3033 const APInt *ZextLowShlAmt;
3034 if (!match(X, m_Shl(m_Specific(Or1), m_APInt(ZextLowShlAmt))) ||
3035 !match(Y, m_Specific(ZextHigh)) || !DT.dominates(U, &Or))
3036 continue;
3037
3038 // HighLow is good concat. If sum of two shifts amount equals to Width,
3039 // LowHigh must also be a good concat.
3040 if (*ZextLowShlAmt + *ZextHighShlAmt != Width)
3041 continue;
3042
3043 // Low must not overlap with High and most significant bits of Low must
3044 // not be shifted out.
3045 assert(ZextLowShlAmt->uge(HighSize) &&
3046 ZextLowShlAmt->ule(Width - LowSize) && "Invalid concat");
3047
3048 FShiftArgs = {U, U, ConstantInt::get(Or0->getType(), *ZextHighShlAmt)};
3049 break;
3050 }
3051 }
3052
3053 if (FShiftArgs.empty())
3054 return std::nullopt;
3055
3056 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
3057 return std::make_pair(IID, FShiftArgs);
3058}
3059
3060/// Match UB-safe variants of the funnel shift intrinsic.
3062 if (auto Opt = IC.convertOrOfShiftsToFunnelShift(Or)) {
3063 auto [IID, FShiftArgs] = *Opt;
3064 Function *F =
3065 Intrinsic::getOrInsertDeclaration(Or.getModule(), IID, Or.getType());
3066 return CallInst::Create(F, FShiftArgs);
3067 }
3068
3069 return nullptr;
3070}
3071
3072/// Attempt to combine or(zext(x),shl(zext(y),bw/2) concat packing patterns.
3074 InstCombiner::BuilderTy &Builder) {
3075 assert(Or.getOpcode() == Instruction::Or && "bswap requires an 'or'");
3076 Value *Op0 = Or.getOperand(0), *Op1 = Or.getOperand(1);
3077 Type *Ty = Or.getType();
3078
3079 unsigned Width = Ty->getScalarSizeInBits();
3080 if ((Width & 1) != 0)
3081 return nullptr;
3082 unsigned HalfWidth = Width / 2;
3083
3084 // Canonicalize zext (lower half) to LHS.
3085 if (!isa<ZExtInst>(Op0))
3086 std::swap(Op0, Op1);
3087
3088 // Find lower/upper half.
3089 Value *LowerSrc, *ShlVal, *UpperSrc;
3090 const APInt *C;
3091 if (!match(Op0, m_OneUse(m_ZExt(m_Value(LowerSrc)))) ||
3092 !match(Op1, m_OneUse(m_Shl(m_Value(ShlVal), m_APInt(C)))) ||
3093 !match(ShlVal, m_OneUse(m_ZExt(m_Value(UpperSrc)))))
3094 return nullptr;
3095 if (*C != HalfWidth || LowerSrc->getType() != UpperSrc->getType() ||
3096 LowerSrc->getType()->getScalarSizeInBits() != HalfWidth)
3097 return nullptr;
3098
3099 auto ConcatIntrinsicCalls = [&](Intrinsic::ID id, Value *Lo, Value *Hi) {
3100 Value *NewLower = Builder.CreateZExt(Lo, Ty);
3101 Value *NewUpper = Builder.CreateZExt(Hi, Ty);
3102 NewUpper = Builder.CreateShl(NewUpper, HalfWidth);
3103 Value *BinOp = Builder.CreateOr(NewLower, NewUpper);
3104 return Builder.CreateIntrinsic(id, Ty, BinOp);
3105 };
3106
3107 // BSWAP: Push the concat down, swapping the lower/upper sources.
3108 // concat(bswap(x),bswap(y)) -> bswap(concat(x,y))
3109 Value *LowerBSwap, *UpperBSwap;
3110 if (match(LowerSrc, m_BSwap(m_Value(LowerBSwap))) &&
3111 match(UpperSrc, m_BSwap(m_Value(UpperBSwap))))
3112 return ConcatIntrinsicCalls(Intrinsic::bswap, UpperBSwap, LowerBSwap);
3113
3114 // BITREVERSE: Push the concat down, swapping the lower/upper sources.
3115 // concat(bitreverse(x),bitreverse(y)) -> bitreverse(concat(x,y))
3116 Value *LowerBRev, *UpperBRev;
3117 if (match(LowerSrc, m_BitReverse(m_Value(LowerBRev))) &&
3118 match(UpperSrc, m_BitReverse(m_Value(UpperBRev))))
3119 return ConcatIntrinsicCalls(Intrinsic::bitreverse, UpperBRev, LowerBRev);
3120
3121 return nullptr;
3122}
3123
3124/// If all elements of two constant vectors are 0/-1 and inverses, return true.
3126 unsigned NumElts = cast<FixedVectorType>(C1->getType())->getNumElements();
3127 for (unsigned i = 0; i != NumElts; ++i) {
3128 Constant *EltC1 = C1->getAggregateElement(i);
3129 Constant *EltC2 = C2->getAggregateElement(i);
3130 if (!EltC1 || !EltC2)
3131 return false;
3132
3133 // One element must be all ones, and the other must be all zeros.
3134 if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) ||
3135 (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes()))))
3136 return false;
3137 }
3138 return true;
3139}
3140
3141/// We have an expression of the form (A & C) | (B & D). If A is a scalar or
3142/// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
3143/// B, it can be used as the condition operand of a select instruction.
3144/// We will detect (A & C) | ~(B | D) when the flag ABIsTheSame enabled.
3145Value *InstCombinerImpl::getSelectCondition(Value *A, Value *B,
3146 bool ABIsTheSame) {
3147 // We may have peeked through bitcasts in the caller.
3148 // Exit immediately if we don't have (vector) integer types.
3149 Type *Ty = A->getType();
3150 if (!Ty->isIntOrIntVectorTy() || !B->getType()->isIntOrIntVectorTy())
3151 return nullptr;
3152
3153 // If A is the 'not' operand of B and has enough signbits, we have our answer.
3154 if (ABIsTheSame ? (A == B) : match(B, m_Not(m_Specific(A)))) {
3155 // If these are scalars or vectors of i1, A can be used directly.
3156 if (Ty->isIntOrIntVectorTy(1))
3157 return A;
3158
3159 // If we look through a vector bitcast, the caller will bitcast the operands
3160 // to match the condition's number of bits (N x i1).
3161 // To make this poison-safe, disallow bitcast from wide element to narrow
3162 // element. That could allow poison in lanes where it was not present in the
3163 // original code.
3165 if (A->getType()->isIntOrIntVectorTy()) {
3166 unsigned NumSignBits = ComputeNumSignBits(A);
3167 if (NumSignBits == A->getType()->getScalarSizeInBits() &&
3168 NumSignBits <= Ty->getScalarSizeInBits())
3169 return Builder.CreateTrunc(A, CmpInst::makeCmpResultType(A->getType()));
3170 }
3171 return nullptr;
3172 }
3173
3174 // TODO: add support for sext and constant case
3175 if (ABIsTheSame)
3176 return nullptr;
3177
3178 // If both operands are constants, see if the constants are inverse bitmasks.
3179 Constant *AConst, *BConst;
3180 if (match(A, m_Constant(AConst)) && match(B, m_Constant(BConst)))
3181 if (AConst == ConstantExpr::getNot(BConst) &&
3184
3185 // Look for more complex patterns. The 'not' op may be hidden behind various
3186 // casts. Look through sexts and bitcasts to find the booleans.
3187 Value *Cond;
3188 Value *NotB;
3189 if (match(A, m_SExt(m_Value(Cond))) &&
3190 Cond->getType()->isIntOrIntVectorTy(1)) {
3191 // A = sext i1 Cond; B = sext (not (i1 Cond))
3192 if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
3193 return Cond;
3194
3195 // A = sext i1 Cond; B = not ({bitcast} (sext (i1 Cond)))
3196 // TODO: The one-use checks are unnecessary or misplaced. If the caller
3197 // checked for uses on logic ops/casts, that should be enough to
3198 // make this transform worthwhile.
3199 if (match(B, m_OneUse(m_Not(m_Value(NotB))))) {
3200 NotB = peekThroughBitcast(NotB, true);
3201 if (match(NotB, m_SExt(m_Specific(Cond))))
3202 return Cond;
3203 }
3204 }
3205
3206 // All scalar (and most vector) possibilities should be handled now.
3207 // Try more matches that only apply to non-splat constant vectors.
3208 if (!Ty->isVectorTy())
3209 return nullptr;
3210
3211 // If both operands are xor'd with constants using the same sexted boolean
3212 // operand, see if the constants are inverse bitmasks.
3213 // TODO: Use ConstantExpr::getNot()?
3214 if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AConst)))) &&
3215 match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BConst)))) &&
3216 Cond->getType()->isIntOrIntVectorTy(1) &&
3217 areInverseVectorBitmasks(AConst, BConst)) {
3219 return Builder.CreateXor(Cond, AConst);
3220 }
3221 return nullptr;
3222}
3223
3224/// We have an expression of the form (A & B) | (C & D). Try to simplify this
3225/// to "A' ? B : D", where A' is a boolean or vector of booleans.
3226/// When InvertFalseVal is set to true, we try to match the pattern
3227/// where we have peeked through a 'not' op and A and C are the same:
3228/// (A & B) | ~(A | D) --> (A & B) | (~A & ~D) --> A' ? B : ~D
3229Value *InstCombinerImpl::matchSelectFromAndOr(Value *A, Value *B, Value *C,
3230 Value *D, bool InvertFalseVal) {
3231 // The potential condition of the select may be bitcasted. In that case, look
3232 // through its bitcast and the corresponding bitcast of the 'not' condition.
3233 Type *OrigType = A->getType();
3234 A = peekThroughBitcast(A, true);
3235 C = peekThroughBitcast(C, true);
3236 if (Value *Cond = getSelectCondition(A, C, InvertFalseVal)) {
3237 // ((bc Cond) & B) | ((bc ~Cond) & D) --> bc (select Cond, (bc B), (bc D))
3238 // If this is a vector, we may need to cast to match the condition's length.
3239 // The bitcasts will either all exist or all not exist. The builder will
3240 // not create unnecessary casts if the types already match.
3241 Type *SelTy = A->getType();
3242 if (auto *VecTy = dyn_cast<VectorType>(Cond->getType())) {
3243 // For a fixed or scalable vector get N from <{vscale x} N x iM>
3244 unsigned Elts = VecTy->getElementCount().getKnownMinValue();
3245 // For a fixed or scalable vector, get the size in bits of N x iM; for a
3246 // scalar this is just M.
3247 unsigned SelEltSize = SelTy->getPrimitiveSizeInBits().getKnownMinValue();
3248 Type *EltTy = Builder.getIntNTy(SelEltSize / Elts);
3249 SelTy = VectorType::get(EltTy, VecTy->getElementCount());
3250 }
3251 Value *BitcastB = Builder.CreateBitCast(B, SelTy);
3252 if (InvertFalseVal)
3253 D = Builder.CreateNot(D);
3254 Value *BitcastD = Builder.CreateBitCast(D, SelTy);
3255 Value *Select = Builder.CreateSelect(Cond, BitcastB, BitcastD);
3256 return Builder.CreateBitCast(Select, OrigType);
3257 }
3258
3259 return nullptr;
3260}
3261
3262// (icmp eq X, C) | (icmp ult Other, (X - C)) -> (icmp ule Other, (X - (C + 1)))
3263// (icmp ne X, C) & (icmp uge Other, (X - C)) -> (icmp ugt Other, (X - (C + 1)))
3265 bool IsAnd, bool IsLogical,
3266 IRBuilderBase &Builder) {
3267 Value *LHS0 = LHS->getOperand(0);
3268 Value *RHS0 = RHS->getOperand(0);
3269 Value *RHS1 = RHS->getOperand(1);
3270
3271 ICmpInst::Predicate LPred =
3272 IsAnd ? LHS->getInversePredicate() : LHS->getPredicate();
3273 ICmpInst::Predicate RPred =
3274 IsAnd ? RHS->getInversePredicate() : RHS->getPredicate();
3275
3276 const APInt *CInt;
3277 if (LPred != ICmpInst::ICMP_EQ ||
3278 !match(LHS->getOperand(1), m_APIntAllowPoison(CInt)) ||
3279 !LHS0->getType()->isIntOrIntVectorTy() ||
3280 !(LHS->hasOneUse() || RHS->hasOneUse()))
3281 return nullptr;
3282
3283 auto MatchRHSOp = [LHS0, CInt](const Value *RHSOp) {
3284 return match(RHSOp,
3285 m_Add(m_Specific(LHS0), m_SpecificIntAllowPoison(-*CInt))) ||
3286 (CInt->isZero() && RHSOp == LHS0);
3287 };
3288
3289 Value *Other;
3290 if (RPred == ICmpInst::ICMP_ULT && MatchRHSOp(RHS1))
3291 Other = RHS0;
3292 else if (RPred == ICmpInst::ICMP_UGT && MatchRHSOp(RHS0))
3293 Other = RHS1;
3294 else
3295 return nullptr;
3296
3297 if (IsLogical)
3298 Other = Builder.CreateFreeze(Other);
3299
3300 return Builder.CreateICmp(
3302 Builder.CreateSub(LHS0, ConstantInt::get(LHS0->getType(), *CInt + 1)),
3303 Other);
3304}
3305
3306/// Fold (icmp)&(icmp) or (icmp)|(icmp) if possible.
3307/// If IsLogical is true, then the and/or is in select form and the transform
3308/// must be poison-safe.
3309Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
3310 Instruction &I, bool IsAnd,
3311 bool IsLogical) {
3313
3314 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
3315 Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
3316 Value *LHS1 = LHS->getOperand(1), *RHS1 = RHS->getOperand(1);
3317
3318 const APInt *LHSC = nullptr, *RHSC = nullptr;
3319 match(LHS1, m_APInt(LHSC));
3320 match(RHS1, m_APInt(RHSC));
3321
3322 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
3323 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
3324 if (predicatesFoldable(PredL, PredR)) {
3325 if (LHS0 == RHS1 && LHS1 == RHS0) {
3326 PredL = ICmpInst::getSwappedPredicate(PredL);
3327 std::swap(LHS0, LHS1);
3328 }
3329 if (LHS0 == RHS0 && LHS1 == RHS1) {
3330 unsigned Code = IsAnd ? getICmpCode(PredL) & getICmpCode(PredR)
3331 : getICmpCode(PredL) | getICmpCode(PredR);
3332 bool IsSigned = LHS->isSigned() || RHS->isSigned();
3333 return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder);
3334 }
3335 }
3336
3337 // handle (roughly):
3338 // (icmp ne (A & B), C) | (icmp ne (A & D), E)
3339 // (icmp eq (A & B), C) & (icmp eq (A & D), E)
3340 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, IsAnd, IsLogical, Builder, Q))
3341 return V;
3342
3343 if (Value *V =
3344 foldAndOrOfICmpEqConstantAndICmp(LHS, RHS, IsAnd, IsLogical, Builder))
3345 return V;
3346 // We can treat logical like bitwise here, because both operands are used on
3347 // the LHS, and as such poison from both will propagate.
3348 if (Value *V = foldAndOrOfICmpEqConstantAndICmp(RHS, LHS, IsAnd,
3349 /*IsLogical*/ false, Builder))
3350 return V;
3351
3352 if (Value *V =
3353 foldAndOrOfICmpsWithConstEq(LHS, RHS, IsAnd, IsLogical, Builder, Q))
3354 return V;
3355 // We can convert this case to bitwise and, because both operands are used
3356 // on the LHS, and as such poison from both will propagate.
3357 if (Value *V = foldAndOrOfICmpsWithConstEq(RHS, LHS, IsAnd,
3358 /*IsLogical=*/false, Builder, Q)) {
3359 // If RHS is still used, we should drop samesign flag.
3360 if (IsLogical && RHS->hasSameSign() && !RHS->use_empty()) {
3361 RHS->setSameSign(false);
3362 addToWorklist(RHS);
3363 }
3364 return V;
3365 }
3366
3367 if (Value *V = foldIsPowerOf2OrZero(LHS, RHS, IsAnd, Builder, *this))
3368 return V;
3369 if (Value *V = foldIsPowerOf2OrZero(RHS, LHS, IsAnd, Builder, *this))
3370 return V;
3371
3372 // TODO: One of these directions is fine with logical and/or, the other could
3373 // be supported by inserting freeze.
3374 if (!IsLogical) {
3375 // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
3376 // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
3377 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/!IsAnd))
3378 return V;
3379
3380 // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
3381 // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
3382 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/!IsAnd))
3383 return V;
3384 }
3385
3386 // TODO: Add conjugated or fold, check whether it is safe for logical and/or.
3387 if (IsAnd && !IsLogical)
3388 if (Value *V = foldSignedTruncationCheck(LHS, RHS, I, Builder))
3389 return V;
3390
3391 if (Value *V = foldIsPowerOf2(LHS, RHS, IsAnd, Builder, *this))
3392 return V;
3393
3394 if (Value *V = foldPowerOf2AndShiftedMask(LHS, RHS, IsAnd, Builder))
3395 return V;
3396
3397 // TODO: Verify whether this is safe for logical and/or.
3398 if (!IsLogical) {
3399 if (Value *X = foldUnsignedUnderflowCheck(LHS, RHS, IsAnd, Q, Builder))
3400 return X;
3401 if (Value *X = foldUnsignedUnderflowCheck(RHS, LHS, IsAnd, Q, Builder))
3402 return X;
3403 }
3404
3405 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
3406 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
3407 // TODO: Remove this and below when foldLogOpOfMaskedICmps can handle undefs.
3408 if (PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
3409 PredL == PredR && match(LHS1, m_ZeroInt()) && match(RHS1, m_ZeroInt()) &&
3410 LHS0->getType() == RHS0->getType() &&
3411 (!IsLogical || isGuaranteedNotToBePoison(RHS0))) {
3412 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
3413 return Builder.CreateICmp(PredL, NewOr,
3415 }
3416
3417 // (icmp ne A, -1) | (icmp ne B, -1) --> (icmp ne (A&B), -1)
3418 // (icmp eq A, -1) & (icmp eq B, -1) --> (icmp eq (A&B), -1)
3419 if (PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
3420 PredL == PredR && match(LHS1, m_AllOnes()) && match(RHS1, m_AllOnes()) &&
3421 LHS0->getType() == RHS0->getType() &&
3422 (!IsLogical || isGuaranteedNotToBePoison(RHS0))) {
3423 Value *NewAnd = Builder.CreateAnd(LHS0, RHS0);
3424 return Builder.CreateICmp(PredL, NewAnd,
3426 }
3427
3428 if (!IsLogical)
3429 if (Value *V =
3430 foldAndOrOfICmpsWithPow2AndWithZero(Builder, LHS, RHS, IsAnd, Q))
3431 return V;
3432
3433 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
3434 if (!LHSC || !RHSC)
3435 return nullptr;
3436
3437 // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
3438 // (trunc x) != C1 | (and x, CA) != C2 -> (and x, CA|CMAX) != C1|C2
3439 // where CMAX is the all ones value for the truncated type,
3440 // iff the lower bits of C2 and CA are zero.
3441 if (PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
3442 PredL == PredR && LHS->hasOneUse() && RHS->hasOneUse()) {
3443 Value *V;
3444 const APInt *AndC, *SmallC = nullptr, *BigC = nullptr;
3445
3446 // (trunc x) == C1 & (and x, CA) == C2
3447 // (and x, CA) == C2 & (trunc x) == C1
3448 if (match(RHS0, m_Trunc(m_Value(V))) &&
3449 match(LHS0, m_And(m_Specific(V), m_APInt(AndC)))) {
3450 SmallC = RHSC;
3451 BigC = LHSC;
3452 } else if (match(LHS0, m_Trunc(m_Value(V))) &&
3453 match(RHS0, m_And(m_Specific(V), m_APInt(AndC)))) {
3454 SmallC = LHSC;
3455 BigC = RHSC;
3456 }
3457
3458 if (SmallC && BigC) {
3459 unsigned BigBitSize = BigC->getBitWidth();
3460 unsigned SmallBitSize = SmallC->getBitWidth();
3461
3462 // Check that the low bits are zero.
3463 APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
3464 if ((Low & *AndC).isZero() && (Low & *BigC).isZero()) {
3465 Value *NewAnd = Builder.CreateAnd(V, Low | *AndC);
3466 APInt N = SmallC->zext(BigBitSize) | *BigC;
3467 Value *NewVal = ConstantInt::get(NewAnd->getType(), N);
3468 return Builder.CreateICmp(PredL, NewAnd, NewVal);
3469 }
3470 }
3471 }
3472
3473 // Match naive pattern (and its inverted form) for checking if two values
3474 // share same sign. An example of the pattern:
3475 // (icmp slt (X & Y), 0) | (icmp sgt (X | Y), -1) -> (icmp sgt (X ^ Y), -1)
3476 // Inverted form (example):
3477 // (icmp slt (X | Y), 0) & (icmp sgt (X & Y), -1) -> (icmp slt (X ^ Y), 0)
3478 bool TrueIfSignedL, TrueIfSignedR;
3479 if (isSignBitCheck(PredL, *LHSC, TrueIfSignedL) &&
3480 isSignBitCheck(PredR, *RHSC, TrueIfSignedR) &&
3481 (RHS->hasOneUse() || LHS->hasOneUse())) {
3482 Value *X, *Y;
3483 if (IsAnd) {
3484 if ((TrueIfSignedL && !TrueIfSignedR &&
3485 match(LHS0, m_Or(m_Value(X), m_Value(Y))) &&
3486 match(RHS0, m_c_And(m_Specific(X), m_Specific(Y)))) ||
3487 (!TrueIfSignedL && TrueIfSignedR &&
3488 match(LHS0, m_And(m_Value(X), m_Value(Y))) &&
3489 match(RHS0, m_c_Or(m_Specific(X), m_Specific(Y))))) {
3490 Value *NewXor = Builder.CreateXor(X, Y);
3491 return Builder.CreateIsNeg(NewXor);
3492 }
3493 } else {
3494 if ((TrueIfSignedL && !TrueIfSignedR &&
3495 match(LHS0, m_And(m_Value(X), m_Value(Y))) &&
3496 match(RHS0, m_c_Or(m_Specific(X), m_Specific(Y)))) ||
3497 (!TrueIfSignedL && TrueIfSignedR &&
3498 match(LHS0, m_Or(m_Value(X), m_Value(Y))) &&
3499 match(RHS0, m_c_And(m_Specific(X), m_Specific(Y))))) {
3500 Value *NewXor = Builder.CreateXor(X, Y);
3501 return Builder.CreateIsNotNeg(NewXor);
3502 }
3503 }
3504 }
3505
3506 return foldAndOrOfICmpsUsingRanges(LHS, RHS, IsAnd);
3507}
3508
3509/// If IsLogical is true, then the and/or is in select form and the transform
3510/// must be poison-safe.
3511Value *InstCombinerImpl::foldBooleanAndOr(Value *LHS, Value *RHS,
3512 Instruction &I, bool IsAnd,
3513 bool IsLogical) {
3514 if (!LHS->getType()->isIntOrIntVectorTy(1))
3515 return nullptr;
3516
3517 if (auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
3518 if (auto *RHSCmp = dyn_cast<ICmpInst>(RHS))
3519 if (Value *Res = foldAndOrOfICmps(LHSCmp, RHSCmp, I, IsAnd, IsLogical))
3520 return Res;
3521
3522 if (auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
3523 if (auto *RHSCmp = dyn_cast<FCmpInst>(RHS))
3524 if (Value *Res = foldLogicOfFCmps(LHSCmp, RHSCmp, IsAnd, IsLogical))
3525 return Res;
3526
3527 if (Value *Res = foldEqOfParts(LHS, RHS, IsAnd))
3528 return Res;
3529
3530 return nullptr;
3531}
3532
3534 InstCombiner::BuilderTy &Builder) {
3535 assert(I.getOpcode() == Instruction::Or &&
3536 "Simplification only supports or at the moment.");
3537
3538 Value *Cmp1, *Cmp2, *Cmp3, *Cmp4;
3539 if (!match(I.getOperand(0), m_And(m_Value(Cmp1), m_Value(Cmp2))) ||
3540 !match(I.getOperand(1), m_And(m_Value(Cmp3), m_Value(Cmp4))))
3541 return nullptr;
3542
3543 // Check if any two pairs of the and operations are inversions of each other.
3544 if (isKnownInversion(Cmp1, Cmp3) && isKnownInversion(Cmp2, Cmp4))
3545 return Builder.CreateXor(Cmp1, Cmp4);
3546 if (isKnownInversion(Cmp1, Cmp4) && isKnownInversion(Cmp2, Cmp3))
3547 return Builder.CreateXor(Cmp1, Cmp3);
3548
3549 return nullptr;
3550}
3551
3552// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
3553// here. We should standardize that construct where it is needed or choose some
3554// other way to ensure that commutated variants of patterns are not missed.
3556 if (Value *V = simplifyOrInst(I.getOperand(0), I.getOperand(1),
3558 return replaceInstUsesWith(I, V);
3559
3561 return &I;
3562
3564 return X;
3565
3567 return Phi;
3568
3569 // See if we can simplify any instructions used by the instruction whose sole
3570 // purpose is to compute bits we don't care about.
3572 return &I;
3573
3574 // Do this before using distributive laws to catch simple and/or/not patterns.
3576 return Xor;
3577
3579 return X;
3580
3581 // (A & B) | (C & D) -> A ^ D where A == ~C && B == ~D
3582 // (A & B) | (C & D) -> A ^ C where A == ~D && B == ~C
3583 if (Value *V = foldOrOfInversions(I, Builder))
3584 return replaceInstUsesWith(I, V);
3585
3586 // (A&B)|(A&C) -> A&(B|C) etc
3588 return replaceInstUsesWith(I, V);
3589
3590 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3591 Type *Ty = I.getType();
3592 if (Ty->isIntOrIntVectorTy(1)) {
3593 if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
3594 if (auto *R =
3595 foldAndOrOfSelectUsingImpliedCond(Op1, *SI0, /* IsAnd */ false))
3596 return R;
3597 }
3598 if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
3599 if (auto *R =
3600 foldAndOrOfSelectUsingImpliedCond(Op0, *SI1, /* IsAnd */ false))
3601 return R;
3602 }
3603 }
3604
3605 if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
3606 return FoldedLogic;
3607
3608 if (Instruction *BitOp = matchBSwapOrBitReverse(I, /*MatchBSwaps*/ true,
3609 /*MatchBitReversals*/ true))
3610 return BitOp;
3611
3612 if (Instruction *Funnel = matchFunnelShift(I, *this))
3613 return Funnel;
3614
3616 return replaceInstUsesWith(I, Concat);
3617
3619 return R;
3620
3622 return R;
3623
3624 if (cast<PossiblyDisjointInst>(I).isDisjoint()) {
3625 if (Instruction *R =
3626 foldAddLikeCommutative(I.getOperand(0), I.getOperand(1),
3627 /*NSW=*/true, /*NUW=*/true))
3628 return R;
3629 if (Instruction *R =
3630 foldAddLikeCommutative(I.getOperand(1), I.getOperand(0),
3631 /*NSW=*/true, /*NUW=*/true))
3632 return R;
3633 }
3634
3635 Value *X, *Y;
3636 const APInt *CV;
3637 if (match(&I, m_c_Or(m_OneUse(m_Xor(m_Value(X), m_APInt(CV))), m_Value(Y))) &&
3638 !CV->isAllOnes() && MaskedValueIsZero(Y, *CV, 0, &I)) {
3639 // (X ^ C) | Y -> (X | Y) ^ C iff Y & C == 0
3640 // The check for a 'not' op is for efficiency (if Y is known zero --> ~X).
3641 Value *Or = Builder.CreateOr(X, Y);
3642 return BinaryOperator::CreateXor(Or, ConstantInt::get(Ty, *CV));
3643 }
3644
3645 // If the operands have no common bits set:
3646 // or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
3648 m_Deferred(X)))) {
3649 Value *IncrementY = Builder.CreateAdd(Y, ConstantInt::get(Ty, 1));
3650 return BinaryOperator::CreateMul(X, IncrementY);
3651 }
3652
3653 // (A & C) | (B & D)
3654 Value *A, *B, *C, *D;
3655 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
3656 match(Op1, m_And(m_Value(B), m_Value(D)))) {
3657
3658 // (A & C0) | (B & C1)
3659 const APInt *C0, *C1;
3660 if (match(C, m_APInt(C0)) && match(D, m_APInt(C1))) {
3661 Value *X;
3662 if (*C0 == ~*C1) {
3663 // ((X | B) & MaskC) | (B & ~MaskC) -> (X & MaskC) | B
3664 if (match(A, m_c_Or(m_Value(X), m_Specific(B))))
3665 return BinaryOperator::CreateOr(Builder.CreateAnd(X, *C0), B);
3666 // (A & MaskC) | ((X | A) & ~MaskC) -> (X & ~MaskC) | A
3667 if (match(B, m_c_Or(m_Specific(A), m_Value(X))))
3668 return BinaryOperator::CreateOr(Builder.CreateAnd(X, *C1), A);
3669
3670 // ((X ^ B) & MaskC) | (B & ~MaskC) -> (X & MaskC) ^ B
3671 if (match(A, m_c_Xor(m_Value(X), m_Specific(B))))
3672 return BinaryOperator::CreateXor(Builder.CreateAnd(X, *C0), B);
3673 // (A & MaskC) | ((X ^ A) & ~MaskC) -> (X & ~MaskC) ^ A
3674 if (match(B, m_c_Xor(m_Specific(A), m_Value(X))))
3675 return BinaryOperator::CreateXor(Builder.CreateAnd(X, *C1), A);
3676 }
3677
3678 if ((*C0 & *C1).isZero()) {
3679 // ((X | B) & C0) | (B & C1) --> (X | B) & (C0 | C1)
3680 // iff (C0 & C1) == 0 and (X & ~C0) == 0
3681 if (match(A, m_c_Or(m_Value(X), m_Specific(B))) &&
3682 MaskedValueIsZero(X, ~*C0, 0, &I)) {
3683 Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
3684 return BinaryOperator::CreateAnd(A, C01);
3685 }
3686 // (A & C0) | ((X | A) & C1) --> (X | A) & (C0 | C1)
3687 // iff (C0 & C1) == 0 and (X & ~C1) == 0
3688 if (match(B, m_c_Or(m_Value(X), m_Specific(A))) &&
3689 MaskedValueIsZero(X, ~*C1, 0, &I)) {
3690 Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
3691 return BinaryOperator::CreateAnd(B, C01);
3692 }
3693 // ((X | C2) & C0) | ((X | C3) & C1) --> (X | C2 | C3) & (C0 | C1)
3694 // iff (C0 & C1) == 0 and (C2 & ~C0) == 0 and (C3 & ~C1) == 0.
3695 const APInt *C2, *C3;
3696 if (match(A, m_Or(m_Value(X), m_APInt(C2))) &&
3697 match(B, m_Or(m_Specific(X), m_APInt(C3))) &&
3698 (*C2 & ~*C0).isZero() && (*C3 & ~*C1).isZero()) {
3699 Value *Or = Builder.CreateOr(X, *C2 | *C3, "bitfield");
3700 Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
3701 return BinaryOperator::CreateAnd(Or, C01);
3702 }
3703 }
3704 }
3705
3706 // Don't try to form a select if it's unlikely that we'll get rid of at
3707 // least one of the operands. A select is generally more expensive than the
3708 // 'or' that it is replacing.
3709 if (Op0->hasOneUse() || Op1->hasOneUse()) {
3710 // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants.
3711 if (Value *V = matchSelectFromAndOr(A, C, B, D))
3712 return replaceInstUsesWith(I, V);
3713 if (Value *V = matchSelectFromAndOr(A, C, D, B))
3714 return replaceInstUsesWith(I, V);
3715 if (Value *V = matchSelectFromAndOr(C, A, B, D))
3716 return replaceInstUsesWith(I, V);
3717 if (Value *V = matchSelectFromAndOr(C, A, D, B))
3718 return replaceInstUsesWith(I, V);
3719 if (Value *V = matchSelectFromAndOr(B, D, A, C))
3720 return replaceInstUsesWith(I, V);
3721 if (Value *V = matchSelectFromAndOr(B, D, C, A))
3722 return replaceInstUsesWith(I, V);
3723 if (Value *V = matchSelectFromAndOr(D, B, A, C))
3724 return replaceInstUsesWith(I, V);
3725 if (Value *V = matchSelectFromAndOr(D, B, C, A))
3726 return replaceInstUsesWith(I, V);
3727 }
3728 }
3729
3730 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
3731 match(Op1, m_Not(m_Or(m_Value(B), m_Value(D)))) &&
3732 (Op0->hasOneUse() || Op1->hasOneUse())) {
3733 // (Cond & C) | ~(Cond | D) -> Cond ? C : ~D
3734 if (Value *V = matchSelectFromAndOr(A, C, B, D, true))
3735 return replaceInstUsesWith(I, V);
3736 if (Value *V = matchSelectFromAndOr(A, C, D, B, true))
3737 return replaceInstUsesWith(I, V);
3738 if (Value *V = matchSelectFromAndOr(C, A, B, D, true))
3739 return replaceInstUsesWith(I, V);
3740 if (Value *V = matchSelectFromAndOr(C, A, D, B, true))
3741 return replaceInstUsesWith(I, V);
3742 }
3743
3744 // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
3745 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
3746 if (match(Op1,
3749 return BinaryOperator::CreateOr(Op0, C);
3750
3751 // ((B ^ C) ^ A) | (A ^ B) -> (A ^ B) | C
3752 if (match(Op1, m_Xor(m_Value(A), m_Value(B))))
3753 if (match(Op0,
3756 return BinaryOperator::CreateOr(Op1, C);
3757
3758 if (Instruction *DeMorgan = matchDeMorgansLaws(I, *this))
3759 return DeMorgan;
3760
3761 // Canonicalize xor to the RHS.
3762 bool SwappedForXor = false;
3763 if (match(Op0, m_Xor(m_Value(), m_Value()))) {
3764 std::swap(Op0, Op1);
3765 SwappedForXor = true;
3766 }
3767
3768 if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
3769 // (A | ?) | (A ^ B) --> (A | ?) | B
3770 // (B | ?) | (A ^ B) --> (B | ?) | A
3771 if (match(Op0, m_c_Or(m_Specific(A), m_Value())))
3772 return BinaryOperator::CreateOr(Op0, B);
3773 if (match(Op0, m_c_Or(m_Specific(B), m_Value())))
3774 return BinaryOperator::CreateOr(Op0, A);
3775
3776 // (A & B) | (A ^ B) --> A | B
3777 // (B & A) | (A ^ B) --> A | B
3778 if (match(Op0, m_c_And(m_Specific(A), m_Specific(B))))
3779 return BinaryOperator::CreateOr(A, B);
3780
3781 // ~A | (A ^ B) --> ~(A & B)
3782 // ~B | (A ^ B) --> ~(A & B)
3783 // The swap above should always make Op0 the 'not'.
3784 if ((Op0->hasOneUse() || Op1->hasOneUse()) &&
3785 (match(Op0, m_Not(m_Specific(A))) || match(Op0, m_Not(m_Specific(B)))))
3787
3788 // Same as above, but peek through an 'and' to the common operand:
3789 // ~(A & ?) | (A ^ B) --> ~((A & ?) & B)
3790 // ~(B & ?) | (A ^ B) --> ~((B & ?) & A)
3792 if ((Op0->hasOneUse() || Op1->hasOneUse()) &&
3794 m_c_And(m_Specific(A), m_Value())))))
3796 if ((Op0->hasOneUse() || Op1->hasOneUse()) &&
3798 m_c_And(m_Specific(B), m_Value())))))
3800
3801 // (~A | C) | (A ^ B) --> ~(A & B) | C
3802 // (~B | C) | (A ^ B) --> ~(A & B) | C
3803 if (Op0->hasOneUse() && Op1->hasOneUse() &&
3804 (match(Op0, m_c_Or(m_Not(m_Specific(A)), m_Value(C))) ||
3805 match(Op0, m_c_Or(m_Not(m_Specific(B)), m_Value(C))))) {
3806 Value *Nand = Builder.CreateNot(Builder.CreateAnd(A, B), "nand");
3807 return BinaryOperator::CreateOr(Nand, C);
3808 }
3809 }
3810
3811 if (SwappedForXor)
3812 std::swap(Op0, Op1);
3813
3814 if (Value *Res =
3815 foldBooleanAndOr(Op0, Op1, I, /*IsAnd=*/false, /*IsLogical=*/false))
3816 return replaceInstUsesWith(I, Res);
3817
3818 if (match(Op1, m_OneUse(m_LogicalOr(m_Value(X), m_Value(Y))))) {
3819 bool IsLogical = isa<SelectInst>(Op1);
3820 if (auto *V = reassociateBooleanAndOr(Op0, X, Y, I, /*IsAnd=*/false,
3821 /*RHSIsLogical=*/IsLogical))
3822 return replaceInstUsesWith(I, V);
3823 }
3824 if (match(Op0, m_OneUse(m_LogicalOr(m_Value(X), m_Value(Y))))) {
3825 bool IsLogical = isa<SelectInst>(Op0);
3826 if (auto *V = reassociateBooleanAndOr(Op1, X, Y, I, /*IsAnd=*/false,
3827 /*RHSIsLogical=*/IsLogical))
3828 return replaceInstUsesWith(I, V);
3829 }
3830
3831 if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder))
3832 return FoldedFCmps;
3833
3834 if (Instruction *CastedOr = foldCastedBitwiseLogic(I))
3835 return CastedOr;
3836
3837 if (Instruction *Sel = foldBinopOfSextBoolToSelect(I))
3838 return Sel;
3839
3840 // or(sext(A), B) / or(B, sext(A)) --> A ? -1 : B, where A is i1 or <N x i1>.
3841 // TODO: Move this into foldBinopOfSextBoolToSelect as a more generalized fold
3842 // with binop identity constant. But creating a select with non-constant
3843 // arm may not be reversible due to poison semantics. Is that a good
3844 // canonicalization?
3845 if (match(&I, m_c_Or(m_OneUse(m_SExt(m_Value(A))), m_Value(B))) &&
3846 A->getType()->isIntOrIntVectorTy(1))
3848
3849 // Note: If we've gotten to the point of visiting the outer OR, then the
3850 // inner one couldn't be simplified. If it was a constant, then it won't
3851 // be simplified by a later pass either, so we try swapping the inner/outer
3852 // ORs in the hopes that we'll be able to simplify it this way.
3853 // (X|C) | V --> (X|V) | C
3854 ConstantInt *CI;
3855 if (Op0->hasOneUse() && !match(Op1, m_ConstantInt()) &&
3856 match(Op0, m_Or(m_Value(A), m_ConstantInt(CI)))) {
3857 Value *Inner = Builder.CreateOr(A, Op1);
3858 Inner->takeName(Op0);
3859 return BinaryOperator::CreateOr(Inner, CI);
3860 }
3861
3862 // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
3863 // Since this OR statement hasn't been optimized further yet, we hope
3864 // that this transformation will allow the new ORs to be optimized.
3865 {
3866 Value *X = nullptr, *Y = nullptr;
3867 if (Op0->hasOneUse() && Op1->hasOneUse() &&
3868 match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
3869 match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
3870 Value *orTrue = Builder.CreateOr(A, C);
3871 Value *orFalse = Builder.CreateOr(B, D);
3872 return SelectInst::Create(X, orTrue, orFalse);
3873 }
3874 }
3875
3876 // or(ashr(subNSW(Y, X), ScalarSizeInBits(Y) - 1), X) --> X s> Y ? -1 : X.
3877 {
3878 Value *X, *Y;
3882 m_Deferred(X)))) {
3883 Value *NewICmpInst = Builder.CreateICmpSGT(X, Y);
3885 return SelectInst::Create(NewICmpInst, AllOnes, X);
3886 }
3887 }
3888
3889 {
3890 // ((A & B) ^ A) | ((A & B) ^ B) -> A ^ B
3891 // (A ^ (A & B)) | (B ^ (A & B)) -> A ^ B
3892 // ((A & B) ^ B) | ((A & B) ^ A) -> A ^ B
3893 // (B ^ (A & B)) | (A ^ (A & B)) -> A ^ B
3894 const auto TryXorOpt = [&](Value *Lhs, Value *Rhs) -> Instruction * {
3895 if (match(Lhs, m_c_Xor(m_And(m_Value(A), m_Value(B)), m_Deferred(A))) &&
3896 match(Rhs,
3898 return BinaryOperator::CreateXor(A, B);
3899 }
3900 return nullptr;
3901 };
3902
3903 if (Instruction *Result = TryXorOpt(Op0, Op1))
3904 return Result;
3905 if (Instruction *Result = TryXorOpt(Op1, Op0))
3906 return Result;
3907 }
3908
3909 if (Instruction *V =
3911 return V;
3912
3913 CmpPredicate Pred;
3914 Value *Mul, *Ov, *MulIsNotZero, *UMulWithOv;
3915 // Check if the OR weakens the overflow condition for umul.with.overflow by
3916 // treating any non-zero result as overflow. In that case, we overflow if both
3917 // umul.with.overflow operands are != 0, as in that case the result can only
3918 // be 0, iff the multiplication overflows.
3919 if (match(&I,
3920 m_c_Or(m_CombineAnd(m_ExtractValue<1>(m_Value(UMulWithOv)),
3921 m_Value(Ov)),
3924 m_CombineAnd(m_ExtractValue<0>(
3925 m_Deferred(UMulWithOv)),
3926 m_Value(Mul)),
3927 m_ZeroInt()),
3928 m_Value(MulIsNotZero)))) &&
3929 (Ov->hasOneUse() || (MulIsNotZero->hasOneUse() && Mul->hasOneUse()))) {
3930 Value *A, *B;
3931 if (match(UMulWithOv, m_Intrinsic<Intrinsic::umul_with_overflow>(
3932 m_Value(A), m_Value(B)))) {
3933 Value *NotNullA = Builder.CreateIsNotNull(A);
3934 Value *NotNullB = Builder.CreateIsNotNull(B);
3935 return BinaryOperator::CreateAnd(NotNullA, NotNullB);
3936 }
3937 }
3938
3939 /// Res, Overflow = xxx_with_overflow X, C1
3940 /// Try to canonicalize the pattern "Overflow | icmp pred Res, C2" into
3941 /// "Overflow | icmp pred X, C2 +/- C1".
3942 const WithOverflowInst *WO;
3943 const Value *WOV;
3944 const APInt *C1, *C2;
3945 if (match(&I, m_c_Or(m_CombineAnd(m_ExtractValue<1>(m_CombineAnd(
3946 m_WithOverflowInst(WO), m_Value(WOV))),
3947 m_Value(Ov)),
3948 m_OneUse(m_ICmp(Pred, m_ExtractValue<0>(m_Deferred(WOV)),
3949 m_APInt(C2))))) &&
3950 (WO->getBinaryOp() == Instruction::Add ||
3951 WO->getBinaryOp() == Instruction::Sub) &&
3952 (ICmpInst::isEquality(Pred) ||
3953 WO->isSigned() == ICmpInst::isSigned(Pred)) &&
3954 match(WO->getRHS(), m_APInt(C1))) {
3955 bool Overflow;
3956 APInt NewC = WO->getBinaryOp() == Instruction::Add
3957 ? (ICmpInst::isSigned(Pred) ? C2->ssub_ov(*C1, Overflow)
3958 : C2->usub_ov(*C1, Overflow))
3959 : (ICmpInst::isSigned(Pred) ? C2->sadd_ov(*C1, Overflow)
3960 : C2->uadd_ov(*C1, Overflow));
3961 if (!Overflow || ICmpInst::isEquality(Pred)) {
3962 Value *NewCmp = Builder.CreateICmp(
3963 Pred, WO->getLHS(), ConstantInt::get(WO->getLHS()->getType(), NewC));
3964 return BinaryOperator::CreateOr(Ov, NewCmp);
3965 }
3966 }
3967
3968 // (~x) | y --> ~(x & (~y)) iff that gets rid of inversions
3970 return &I;
3971
3972 // Improve "get low bit mask up to and including bit X" pattern:
3973 // (1 << X) | ((1 << X) + -1) --> -1 l>> (bitwidth(x) - 1 - X)
3974 if (match(&I, m_c_Or(m_Add(m_Shl(m_One(), m_Value(X)), m_AllOnes()),
3975 m_Shl(m_One(), m_Deferred(X)))) &&
3976 match(&I, m_c_Or(m_OneUse(m_Value()), m_Value()))) {
3977 Value *Sub = Builder.CreateSub(
3978 ConstantInt::get(Ty, Ty->getScalarSizeInBits() - 1), X);
3979 return BinaryOperator::CreateLShr(Constant::getAllOnesValue(Ty), Sub);
3980 }
3981
3982 // An or recurrence w/loop invariant step is equivelent to (or start, step)
3983 PHINode *PN = nullptr;
3984 Value *Start = nullptr, *Step = nullptr;
3985 if (matchSimpleRecurrence(&I, PN, Start, Step) && DT.dominates(Step, PN))
3986 return replaceInstUsesWith(I, Builder.CreateOr(Start, Step));
3987
3988 // (A & B) | (C | D) or (C | D) | (A & B)
3989 // Can be combined if C or D is of type (A/B & X)
3991 m_OneUse(m_Or(m_Value(C), m_Value(D)))))) {
3992 // (A & B) | (C | ?) -> C | (? | (A & B))
3993 // (A & B) | (C | ?) -> C | (? | (A & B))
3994 // (A & B) | (C | ?) -> C | (? | (A & B))
3995 // (A & B) | (C | ?) -> C | (? | (A & B))
3996 // (C | ?) | (A & B) -> C | (? | (A & B))
3997 // (C | ?) | (A & B) -> C | (? | (A & B))
3998 // (C | ?) | (A & B) -> C | (? | (A & B))
3999 // (C | ?) | (A & B) -> C | (? | (A & B))
4000 if (match(D, m_OneUse(m_c_And(m_Specific(A), m_Value()))) ||
4002 return BinaryOperator::CreateOr(
4004 // (A & B) | (? | D) -> (? | (A & B)) | D
4005 // (A & B) | (? | D) -> (? | (A & B)) | D
4006 // (A & B) | (? | D) -> (? | (A & B)) | D
4007 // (A & B) | (? | D) -> (? | (A & B)) | D
4008 // (? | D) | (A & B) -> (? | (A & B)) | D
4009 // (? | D) | (A & B) -> (? | (A & B)) | D
4010 // (? | D) | (A & B) -> (? | (A & B)) | D
4011 // (? | D) | (A & B) -> (? | (A & B)) | D
4012 if (match(C, m_OneUse(m_c_And(m_Specific(A), m_Value()))) ||
4014 return BinaryOperator::CreateOr(
4016 }
4017
4019 return R;
4020
4021 if (Instruction *Canonicalized = canonicalizeLogicFirst(I, Builder))
4022 return Canonicalized;
4023
4024 if (Instruction *Folded = foldLogicOfIsFPClass(I, Op0, Op1))
4025 return Folded;
4026
4027 if (Instruction *Res = foldBinOpOfDisplacedShifts(I))
4028 return Res;
4029
4030 // If we are setting the sign bit of a floating-point value, convert
4031 // this to fneg(fabs), then cast back to integer.
4032 //
4033 // If the result isn't immediately cast back to a float, this will increase
4034 // the number of instructions. This is still probably a better canonical form
4035 // as it enables FP value tracking.
4036 //
4037 // Assumes any IEEE-represented type has the sign bit in the high bit.
4038 //
4039 // This is generous interpretation of noimplicitfloat, this is not a true
4040 // floating-point operation.
4041 Value *CastOp;
4042 if (match(Op0, m_ElementWiseBitCast(m_Value(CastOp))) &&
4043 match(Op1, m_SignMask()) &&
4045 Attribute::NoImplicitFloat)) {
4046 Type *EltTy = CastOp->getType()->getScalarType();
4047 if (EltTy->isFloatingPointTy() && EltTy->isIEEE()) {
4048 Value *FAbs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, CastOp);
4049 Value *FNegFAbs = Builder.CreateFNeg(FAbs);
4050 return new BitCastInst(FNegFAbs, I.getType());
4051 }
4052 }
4053
4054 // (X & C1) | C2 -> X & (C1 | C2) iff (X & C2) == C2
4055 if (match(Op0, m_OneUse(m_And(m_Value(X), m_APInt(C1)))) &&
4056 match(Op1, m_APInt(C2))) {
4057 KnownBits KnownX = computeKnownBits(X, /*Depth*/ 0, &I);
4058 if ((KnownX.One & *C2) == *C2)
4059 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *C1 | *C2));
4060 }
4061
4063 return Res;
4064
4065 if (Value *V =
4067 /*SimplifyOnly*/ false, *this))
4068 return BinaryOperator::CreateOr(V, Op1);
4069 if (Value *V =
4071 /*SimplifyOnly*/ false, *this))
4072 return BinaryOperator::CreateOr(Op0, V);
4073
4074 if (cast<PossiblyDisjointInst>(I).isDisjoint())
4076 return replaceInstUsesWith(I, V);
4077
4078 return nullptr;
4079}
4080
4081/// A ^ B can be specified using other logic ops in a variety of patterns. We
4082/// can fold these early and efficiently by morphing an existing instruction.
4084 InstCombiner::BuilderTy &Builder) {
4085 assert(I.getOpcode() == Instruction::Xor);
4086 Value *Op0 = I.getOperand(0);
4087 Value *Op1 = I.getOperand(1);
4088 Value *A, *B;
4089
4090 // There are 4 commuted variants for each of the basic patterns.
4091
4092 // (A & B) ^ (A | B) -> A ^ B
4093 // (A & B) ^ (B | A) -> A ^ B
4094 // (A | B) ^ (A & B) -> A ^ B
4095 // (A | B) ^ (B & A) -> A ^ B
4096 if (match(&I, m_c_Xor(m_And(m_Value(A), m_Value(B)),
4098 return BinaryOperator::CreateXor(A, B);
4099
4100 // (A | ~B) ^ (~A | B) -> A ^ B
4101 // (~B | A) ^ (~A | B) -> A ^ B
4102 // (~A | B) ^ (A | ~B) -> A ^ B
4103 // (B | ~A) ^ (A | ~B) -> A ^ B
4104 if (match(&I, m_Xor(m_c_Or(m_Value(A), m_Not(m_Value(B))),
4106 return BinaryOperator::CreateXor(A, B);
4107
4108 // (A & ~B) ^ (~A & B) -> A ^ B
4109 // (~B & A) ^ (~A & B) -> A ^ B
4110 // (~A & B) ^ (A & ~B) -> A ^ B
4111 // (B & ~A) ^ (A & ~B) -> A ^ B
4112 if (match(&I, m_Xor(m_c_And(m_Value(A), m_Not(m_Value(B))),
4114 return BinaryOperator::CreateXor(A, B);
4115
4116 // For the remaining cases we need to get rid of one of the operands.
4117 if (!Op0->hasOneUse() && !Op1->hasOneUse())
4118 return nullptr;
4119
4120 // (A | B) ^ ~(A & B) -> ~(A ^ B)
4121 // (A | B) ^ ~(B & A) -> ~(A ^ B)
4122 // (A & B) ^ ~(A | B) -> ~(A ^ B)
4123 // (A & B) ^ ~(B | A) -> ~(A ^ B)
4124 // Complexity sorting ensures the not will be on the right side.
4125 if ((match(Op0, m_Or(m_Value(A), m_Value(B))) &&
4126 match(Op1, m_Not(m_c_And(m_Specific(A), m_Specific(B))))) ||
4127 (match(Op0, m_And(m_Value(A), m_Value(B))) &&
4129 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
4130
4131 return nullptr;
4132}
4133
4134Value *InstCombinerImpl::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS,
4135 BinaryOperator &I) {
4136 assert(I.getOpcode() == Instruction::Xor && I.getOperand(0) == LHS &&
4137 I.getOperand(1) == RHS && "Should be 'xor' with these operands");
4138
4139 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
4140 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
4141 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
4142
4143 if (predicatesFoldable(PredL, PredR)) {
4144 if (LHS0 == RHS1 && LHS1 == RHS0) {
4145 std::swap(LHS0, LHS1);
4146 PredL = ICmpInst::getSwappedPredicate(PredL);
4147 }
4148 if (LHS0 == RHS0 && LHS1 == RHS1) {
4149 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
4150 unsigned Code = getICmpCode(PredL) ^ getICmpCode(PredR);
4151 bool IsSigned = LHS->isSigned() || RHS->isSigned();
4152 return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder);
4153 }
4154 }
4155
4156 // TODO: This can be generalized to compares of non-signbits using
4157 // decomposeBitTestICmp(). It could be enhanced more by using (something like)
4158 // foldLogOpOfMaskedICmps().
4159 const APInt *LC, *RC;
4160 if (match(LHS1, m_APInt(LC)) && match(RHS1, m_APInt(RC)) &&
4161 LHS0->getType() == RHS0->getType() &&
4162 LHS0->getType()->isIntOrIntVectorTy()) {
4163 // Convert xor of signbit tests to signbit test of xor'd values:
4164 // (X > -1) ^ (Y > -1) --> (X ^ Y) < 0
4165 // (X < 0) ^ (Y < 0) --> (X ^ Y) < 0
4166 // (X > -1) ^ (Y < 0) --> (X ^ Y) > -1
4167 // (X < 0) ^ (Y > -1) --> (X ^ Y) > -1
4168 bool TrueIfSignedL, TrueIfSignedR;
4169 if ((LHS->hasOneUse() || RHS->hasOneUse()) &&
4170 isSignBitCheck(PredL, *LC, TrueIfSignedL) &&
4171 isSignBitCheck(PredR, *RC, TrueIfSignedR)) {
4172 Value *XorLR = Builder.CreateXor(LHS0, RHS0);
4173 return TrueIfSignedL == TrueIfSignedR ? Builder.CreateIsNeg(XorLR) :
4174 Builder.CreateIsNotNeg(XorLR);
4175 }
4176
4177 // Fold (icmp pred1 X, C1) ^ (icmp pred2 X, C2)
4178 // into a single comparison using range-based reasoning.
4179 if (LHS0 == RHS0) {
4182 auto CRUnion = CR1.exactUnionWith(CR2);
4183 auto CRIntersect = CR1.exactIntersectWith(CR2);
4184 if (CRUnion && CRIntersect)
4185 if (auto CR = CRUnion->exactIntersectWith(CRIntersect->inverse())) {
4186 if (CR->isFullSet())
4187 return ConstantInt::getTrue(I.getType());
4188 if (CR->isEmptySet())
4189 return ConstantInt::getFalse(I.getType());
4190
4191 CmpInst::Predicate NewPred;
4192 APInt NewC, Offset;
4193 CR->getEquivalentICmp(NewPred, NewC, Offset);
4194
4195 if ((Offset.isZero() && (LHS->hasOneUse() || RHS->hasOneUse())) ||
4196 (LHS->hasOneUse() && RHS->hasOneUse())) {
4197 Value *NewV = LHS0;
4198 Type *Ty = LHS0->getType();
4199 if (!Offset.isZero())
4200 NewV = Builder.CreateAdd(NewV, ConstantInt::get(Ty, Offset));
4201 return Builder.CreateICmp(NewPred, NewV,
4202 ConstantInt::get(Ty, NewC));
4203 }
4204 }
4205 }
4206 }
4207
4208 // Instead of trying to imitate the folds for and/or, decompose this 'xor'
4209 // into those logic ops. That is, try to turn this into an and-of-icmps
4210 // because we have many folds for that pattern.
4211 //
4212 // This is based on a truth table definition of xor:
4213 // X ^ Y --> (X | Y) & !(X & Y)
4214 if (Value *OrICmp = simplifyBinOp(Instruction::Or, LHS, RHS, SQ)) {
4215 // TODO: If OrICmp is true, then the definition of xor simplifies to !(X&Y).
4216 // TODO: If OrICmp is false, the whole thing is false (InstSimplify?).
4217 if (Value *AndICmp = simplifyBinOp(Instruction::And, LHS, RHS, SQ)) {
4218 // TODO: Independently handle cases where the 'and' side is a constant.
4219 ICmpInst *X = nullptr, *Y = nullptr;
4220 if (OrICmp == LHS && AndICmp == RHS) {
4221 // (LHS | RHS) & !(LHS & RHS) --> LHS & !RHS --> X & !Y
4222 X = LHS;
4223 Y = RHS;
4224 }
4225 if (OrICmp == RHS && AndICmp == LHS) {
4226 // !(LHS & RHS) & (LHS | RHS) --> !LHS & RHS --> !Y & X
4227 X = RHS;
4228 Y = LHS;
4229 }
4230 if (X && Y && (Y->hasOneUse() || canFreelyInvertAllUsersOf(Y, &I))) {
4231 // Invert the predicate of 'Y', thus inverting its output.
4232 Y->setPredicate(Y->getInversePredicate());
4233 // So, are there other uses of Y?
4234 if (!Y->hasOneUse()) {
4235 // We need to adapt other uses of Y though. Get a value that matches
4236 // the original value of Y before inversion. While this increases
4237 // immediate instruction count, we have just ensured that all the
4238 // users are freely-invertible, so that 'not' *will* get folded away.
4240 // Set insertion point to right after the Y.
4241 Builder.SetInsertPoint(Y->getParent(), ++(Y->getIterator()));
4242 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
4243 // Replace all uses of Y (excluding the one in NotY!) with NotY.
4245 Y->replaceUsesWithIf(NotY,
4246 [NotY](Use &U) { return U.getUser() != NotY; });
4247 }
4248 // All done.
4249 return Builder.CreateAnd(LHS, RHS);
4250 }
4251 }
4252 }
4253
4254 return nullptr;
4255}
4256
4257/// If we have a masked merge, in the canonical form of:
4258/// (assuming that A only has one use.)
4259/// | A | |B|
4260/// ((x ^ y) & M) ^ y
4261/// | D |
4262/// * If M is inverted:
4263/// | D |
4264/// ((x ^ y) & ~M) ^ y
4265/// We can canonicalize by swapping the final xor operand
4266/// to eliminate the 'not' of the mask.
4267/// ((x ^ y) & M) ^ x
4268/// * If M is a constant, and D has one use, we transform to 'and' / 'or' ops
4269/// because that shortens the dependency chain and improves analysis:
4270/// (x & M) | (y & ~M)
4272 InstCombiner::BuilderTy &Builder) {
4273 Value *B, *X, *D;
4274 Value *M;
4275 if (!match(&I, m_c_Xor(m_Value(B),
4278 m_Value(D)),
4279 m_Value(M))))))
4280 return nullptr;
4281
4282 Value *NotM;
4283 if (match(M, m_Not(m_Value(NotM)))) {
4284 // De-invert the mask and swap the value in B part.
4285 Value *NewA = Builder.CreateAnd(D, NotM);
4286 return BinaryOperator::CreateXor(NewA, X);
4287 }
4288
4289 Constant *C;
4290 if (D->hasOneUse() && match(M, m_Constant(C))) {
4291 // Propagating undef is unsafe. Clamp undef elements to -1.
4292 Type *EltTy = C->getType()->getScalarType();
4294 // Unfold.
4295 Value *LHS = Builder.CreateAnd(X, C);
4296 Value *NotC = Builder.CreateNot(C);
4297 Value *RHS = Builder.CreateAnd(B, NotC);
4298 return BinaryOperator::CreateOr(LHS, RHS);
4299 }
4300
4301 return nullptr;
4302}
4303
4305 InstCombiner::BuilderTy &Builder) {
4306 Value *X, *Y;
4307 // FIXME: one-use check is not needed in general, but currently we are unable
4308 // to fold 'not' into 'icmp', if that 'icmp' has multiple uses. (D35182)
4309 if (!match(&I, m_Not(m_OneUse(m_Xor(m_Value(X), m_Value(Y))))))
4310 return nullptr;
4311
4312 auto hasCommonOperand = [](Value *A, Value *B, Value *C, Value *D) {
4313 return A == C || A == D || B == C || B == D;
4314 };
4315
4316 Value *A, *B, *C, *D;
4317 // Canonicalize ~((A & B) ^ (A | ?)) -> (A & B) | ~(A | ?)
4318 // 4 commuted variants
4319 if (match(X, m_And(m_Value(A), m_Value(B))) &&
4320 match(Y, m_Or(m_Value(C), m_Value(D))) && hasCommonOperand(A, B, C, D)) {
4321 Value *NotY = Builder.CreateNot(Y);
4322 return BinaryOperator::CreateOr(X, NotY);
4323 };
4324
4325 // Canonicalize ~((A | ?) ^ (A & B)) -> (A & B) | ~(A | ?)
4326 // 4 commuted variants
4327 if (match(Y, m_And(m_Value(A), m_Value(B))) &&
4328 match(X, m_Or(m_Value(C), m_Value(D))) && hasCommonOperand(A, B, C, D)) {
4329 Value *NotX = Builder.CreateNot(X);
4330 return BinaryOperator::CreateOr(Y, NotX);
4331 };
4332
4333 return nullptr;
4334}
4335
4336/// Canonicalize a shifty way to code absolute value to the more common pattern
4337/// that uses negation and select.
4339 InstCombiner::BuilderTy &Builder) {
4340 assert(Xor.getOpcode() == Instruction::Xor && "Expected an xor instruction.");
4341
4342 // There are 4 potential commuted variants. Move the 'ashr' candidate to Op1.
4343 // We're relying on the fact that we only do this transform when the shift has
4344 // exactly 2 uses and the add has exactly 1 use (otherwise, we might increase
4345 // instructions).
4346 Value *Op0 = Xor.getOperand(0), *Op1 = Xor.getOperand(1);
4347 if (Op0->hasNUses(2))
4348 std::swap(Op0, Op1);
4349
4350 Type *Ty = Xor.getType();
4351 Value *A;
4352 const APInt *ShAmt;
4353 if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) &&
4354 Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 &&
4355 match(Op0, m_OneUse(m_c_Add(m_Specific(A), m_Specific(Op1))))) {
4356 // Op1 = ashr i32 A, 31 ; smear the sign bit
4357 // xor (add A, Op1), Op1 ; add -1 and flip bits if negative
4358 // --> (A < 0) ? -A : A
4359 Value *IsNeg = Builder.CreateIsNeg(A);
4360 // Copy the nsw flags from the add to the negate.
4361 auto *Add = cast<BinaryOperator>(Op0);
4362 Value *NegA = Add->hasNoUnsignedWrap()
4363 ? Constant::getNullValue(A->getType())
4364 : Builder.CreateNeg(A, "", Add->hasNoSignedWrap());
4365 return SelectInst::Create(IsNeg, NegA, A);
4366 }
4367 return nullptr;
4368}
4369
4371 Instruction *IgnoredUser) {
4372 auto *I = dyn_cast<Instruction>(Op);
4373 return I && IC.isFreeToInvert(I, /*WillInvertAllUses=*/true) &&
4374 IC.canFreelyInvertAllUsersOf(I, IgnoredUser);
4375}
4376
4378 Instruction *IgnoredUser) {
4379 auto *I = cast<Instruction>(Op);
4380 IC.Builder.SetInsertPoint(*I->getInsertionPointAfterDef());
4381 Value *NotOp = IC.Builder.CreateNot(Op, Op->getName() + ".not");
4382 Op->replaceUsesWithIf(NotOp,
4383 [NotOp](Use &U) { return U.getUser() != NotOp; });
4384 IC.freelyInvertAllUsersOf(NotOp, IgnoredUser);
4385 return NotOp;
4386}
4387
4388// Transform
4389// z = ~(x &/| y)
4390// into:
4391// z = ((~x) |/& (~y))
4392// iff both x and y are free to invert and all uses of z can be freely updated.
4394 Value *Op0, *Op1;
4395 if (!match(&I, m_LogicalOp(m_Value(Op0), m_Value(Op1))))
4396 return false;
4397
4398 // If this logic op has not been simplified yet, just bail out and let that
4399 // happen first. Otherwise, the code below may wrongly invert.
4400 if (Op0 == Op1)
4401 return false;
4402
4403 Instruction::BinaryOps NewOpc =
4404 match(&I, m_LogicalAnd()) ? Instruction::Or : Instruction::And;
4405 bool IsBinaryOp = isa<BinaryOperator>(I);
4406
4407 // Can our users be adapted?
4408 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
4409 return false;
4410
4411 // And can the operands be adapted?
4412 if (!canFreelyInvert(*this, Op0, &I) || !canFreelyInvert(*this, Op1, &I))
4413 return false;
4414
4415 Op0 = freelyInvert(*this, Op0, &I);
4416 Op1 = freelyInvert(*this, Op1, &I);
4417
4418 Builder.SetInsertPoint(*I.getInsertionPointAfterDef());
4419 Value *NewLogicOp;
4420 if (IsBinaryOp)
4421 NewLogicOp = Builder.CreateBinOp(NewOpc, Op0, Op1, I.getName() + ".not");
4422 else
4423 NewLogicOp =
4424 Builder.CreateLogicalOp(NewOpc, Op0, Op1, I.getName() + ".not");
4425
4426 replaceInstUsesWith(I, NewLogicOp);
4427 // We can not just create an outer `not`, it will most likely be immediately
4428 // folded back, reconstructing our initial pattern, and causing an
4429 // infinite combine loop, so immediately manually fold it away.
4430 freelyInvertAllUsersOf(NewLogicOp);
4431 return true;
4432}
4433
4434// Transform
4435// z = (~x) &/| y
4436// into:
4437// z = ~(x |/& (~y))
4438// iff y is free to invert and all uses of z can be freely updated.
4440 Value *Op0, *Op1;
4441 if (!match(&I, m_LogicalOp(m_Value(Op0), m_Value(Op1))))
4442 return false;
4443 Instruction::BinaryOps NewOpc =
4444 match(&I, m_LogicalAnd()) ? Instruction::Or : Instruction::And;
4445 bool IsBinaryOp = isa<BinaryOperator>(I);
4446
4447 Value *NotOp0 = nullptr;
4448 Value *NotOp1 = nullptr;
4449 Value **OpToInvert = nullptr;
4450 if (match(Op0, m_Not(m_Value(NotOp0))) && canFreelyInvert(*this, Op1, &I)) {
4451 Op0 = NotOp0;
4452 OpToInvert = &Op1;
4453 } else if (match(Op1, m_Not(m_Value(NotOp1))) &&
4454 canFreelyInvert(*this, Op0, &I)) {
4455 Op1 = NotOp1;
4456 OpToInvert = &Op0;
4457 } else
4458 return false;
4459
4460 // And can our users be adapted?
4461 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
4462 return false;
4463
4464 *OpToInvert = freelyInvert(*this, *OpToInvert, &I);
4465
4466 Builder.SetInsertPoint(*I.getInsertionPointAfterDef());
4467 Value *NewBinOp;
4468 if (IsBinaryOp)
4469 NewBinOp = Builder.CreateBinOp(NewOpc, Op0, Op1, I.getName() + ".not");
4470 else
4471 NewBinOp = Builder.CreateLogicalOp(NewOpc, Op0, Op1, I.getName() + ".not");
4472 replaceInstUsesWith(I, NewBinOp);
4473 // We can not just create an outer `not`, it will most likely be immediately
4474 // folded back, reconstructing our initial pattern, and causing an
4475 // infinite combine loop, so immediately manually fold it away.
4476 freelyInvertAllUsersOf(NewBinOp);
4477 return true;
4478}
4479
4480Instruction *InstCombinerImpl::foldNot(BinaryOperator &I) {
4481 Value *NotOp;
4482 if (!match(&I, m_Not(m_Value(NotOp))))
4483 return nullptr;
4484
4485 // Apply DeMorgan's Law for 'nand' / 'nor' logic with an inverted operand.
4486 // We must eliminate the and/or (one-use) for these transforms to not increase
4487 // the instruction count.
4488 //
4489 // ~(~X & Y) --> (X | ~Y)
4490 // ~(Y & ~X) --> (X | ~Y)
4491 //
4492 // Note: The logical matches do not check for the commuted patterns because
4493 // those are handled via SimplifySelectsFeedingBinaryOp().
4494 Type *Ty = I.getType();
4495 Value *X, *Y;
4496 if (match(NotOp, m_OneUse(m_c_And(m_Not(m_Value(X)), m_Value(Y))))) {
4497 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
4498 return BinaryOperator::CreateOr(X, NotY);
4499 }
4500 if (match(NotOp, m_OneUse(m_LogicalAnd(m_Not(m_Value(X)), m_Value(Y))))) {
4501 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
4502 return SelectInst::Create(X, ConstantInt::getTrue(Ty), NotY);
4503 }
4504
4505 // ~(~X | Y) --> (X & ~Y)
4506 // ~(Y | ~X) --> (X & ~Y)
4507 if (match(NotOp, m_OneUse(m_c_Or(m_Not(m_Value(X)), m_Value(Y))))) {
4508 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
4509 return BinaryOperator::CreateAnd(X, NotY);
4510 }
4511 if (match(NotOp, m_OneUse(m_LogicalOr(m_Not(m_Value(X)), m_Value(Y))))) {
4512 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
4513 return SelectInst::Create(X, NotY, ConstantInt::getFalse(Ty));
4514 }
4515
4516 // Is this a 'not' (~) fed by a binary operator?
4517 BinaryOperator *NotVal;
4518 if (match(NotOp, m_BinOp(NotVal))) {
4519 // ~((-X) | Y) --> (X - 1) & (~Y)
4520 if (match(NotVal,
4523 Value *NotY = Builder.CreateNot(Y);
4524 return BinaryOperator::CreateAnd(DecX, NotY);
4525 }
4526
4527 // ~(~X >>s Y) --> (X >>s Y)
4528 if (match(NotVal, m_AShr(m_Not(m_Value(X)), m_Value(Y))))
4529 return BinaryOperator::CreateAShr(X, Y);
4530
4531 // Treat lshr with non-negative operand as ashr.
4532 // ~(~X >>u Y) --> (X >>s Y) iff X is known negative
4533 if (match(NotVal, m_LShr(m_Not(m_Value(X)), m_Value(Y))) &&
4535 return BinaryOperator::CreateAShr(X, Y);
4536
4537 // Bit-hack form of a signbit test for iN type:
4538 // ~(X >>s (N - 1)) --> sext i1 (X > -1) to iN
4539 unsigned FullShift = Ty->getScalarSizeInBits() - 1;
4540 if (match(NotVal, m_OneUse(m_AShr(m_Value(X), m_SpecificInt(FullShift))))) {
4541 Value *IsNotNeg = Builder.CreateIsNotNeg(X, "isnotneg");
4542 return new SExtInst(IsNotNeg, Ty);
4543 }
4544
4545 // If we are inverting a right-shifted constant, we may be able to eliminate
4546 // the 'not' by inverting the constant and using the opposite shift type.
4547 // Canonicalization rules ensure that only a negative constant uses 'ashr',
4548 // but we must check that in case that transform has not fired yet.
4549
4550 // ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits)
4551 Constant *C;
4552 if (match(NotVal, m_AShr(m_Constant(C), m_Value(Y))) &&
4553 match(C, m_Negative()))
4554 return BinaryOperator::CreateLShr(ConstantExpr::getNot(C), Y);
4555
4556 // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits)
4557 if (match(NotVal, m_LShr(m_Constant(C), m_Value(Y))) &&
4558 match(C, m_NonNegative()))
4559 return BinaryOperator::CreateAShr(ConstantExpr::getNot(C), Y);
4560
4561 // ~(X + C) --> ~C - X
4562 if (match(NotVal, m_Add(m_Value(X), m_ImmConstant(C))))
4563 return BinaryOperator::CreateSub(ConstantExpr::getNot(C), X);
4564
4565 // ~(X - Y) --> ~X + Y
4566 // FIXME: is it really beneficial to sink the `not` here?
4567 if (match(NotVal, m_Sub(m_Value(X), m_Value(Y))))
4568 if (isa<Constant>(X) || NotVal->hasOneUse())
4569 return BinaryOperator::CreateAdd(Builder.CreateNot(X), Y);
4570
4571 // ~(~X + Y) --> X - Y
4572 if (match(NotVal, m_c_Add(m_Not(m_Value(X)), m_Value(Y))))
4573 return BinaryOperator::CreateWithCopiedFlags(Instruction::Sub, X, Y,
4574 NotVal);
4575 }
4576
4577 // not (cmp A, B) = !cmp A, B
4578 CmpPredicate Pred;
4579 if (match(NotOp, m_Cmp(Pred, m_Value(), m_Value())) &&
4580 (NotOp->hasOneUse() ||
4581 InstCombiner::canFreelyInvertAllUsersOf(cast<Instruction>(NotOp),
4582 /*IgnoredUser=*/nullptr))) {
4583 cast<CmpInst>(NotOp)->setPredicate(CmpInst::getInversePredicate(Pred));
4585 return &I;
4586 }
4587
4588 // Move a 'not' ahead of casts of a bool to enable logic reduction:
4589 // not (bitcast (sext i1 X)) --> bitcast (sext (not i1 X))
4590 if (match(NotOp, m_OneUse(m_BitCast(m_OneUse(m_SExt(m_Value(X)))))) && X->getType()->isIntOrIntVectorTy(1)) {
4591 Type *SextTy = cast<BitCastOperator>(NotOp)->getSrcTy();
4592 Value *NotX = Builder.CreateNot(X);
4593 Value *Sext = Builder.CreateSExt(NotX, SextTy);
4594 return new BitCastInst(Sext, Ty);
4595 }
4596
4597 if (auto *NotOpI = dyn_cast<Instruction>(NotOp))
4598 if (sinkNotIntoLogicalOp(*NotOpI))
4599 return &I;
4600
4601 // Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max:
4602 // ~min(~X, ~Y) --> max(X, Y)
4603 // ~max(~X, Y) --> min(X, ~Y)
4604 auto *II = dyn_cast<IntrinsicInst>(NotOp);
4605 if (II && II->hasOneUse()) {
4606 if (match(NotOp, m_c_MaxOrMin(m_Not(m_Value(X)), m_Value(Y)))) {
4607 Intrinsic::ID InvID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
4608 Value *NotY = Builder.CreateNot(Y);
4609 Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, NotY);
4610 return replaceInstUsesWith(I, InvMaxMin);
4611 }
4612
4613 if (II->getIntrinsicID() == Intrinsic::is_fpclass) {
4614 ConstantInt *ClassMask = cast<ConstantInt>(II->getArgOperand(1));
4615 II->setArgOperand(
4616 1, ConstantInt::get(ClassMask->getType(),
4617 ~ClassMask->getZExtValue() & fcAllFlags));
4618 return replaceInstUsesWith(I, II);
4619 }
4620 }
4621
4622 if (NotOp->hasOneUse()) {
4623 // Pull 'not' into operands of select if both operands are one-use compares
4624 // or one is one-use compare and the other one is a constant.
4625 // Inverting the predicates eliminates the 'not' operation.
4626 // Example:
4627 // not (select ?, (cmp TPred, ?, ?), (cmp FPred, ?, ?) -->
4628 // select ?, (cmp InvTPred, ?, ?), (cmp InvFPred, ?, ?)
4629 // not (select ?, (cmp TPred, ?, ?), true -->
4630 // select ?, (cmp InvTPred, ?, ?), false
4631 if (auto *Sel = dyn_cast<SelectInst>(NotOp)) {
4632 Value *TV = Sel->getTrueValue();
4633 Value *FV = Sel->getFalseValue();
4634 auto *CmpT = dyn_cast<CmpInst>(TV);
4635 auto *CmpF = dyn_cast<CmpInst>(FV);
4636 bool InvertibleT = (CmpT && CmpT->hasOneUse()) || isa<Constant>(TV);
4637 bool InvertibleF = (CmpF && CmpF->hasOneUse()) || isa<Constant>(FV);
4638 if (InvertibleT && InvertibleF) {
4639 if (CmpT)
4640 CmpT->setPredicate(CmpT->getInversePredicate());
4641 else
4642 Sel->setTrueValue(ConstantExpr::getNot(cast<Constant>(TV)));
4643 if (CmpF)
4644 CmpF->setPredicate(CmpF->getInversePredicate());
4645 else
4646 Sel->setFalseValue(ConstantExpr::getNot(cast<Constant>(FV)));
4647 return replaceInstUsesWith(I, Sel);
4648 }
4649 }
4650 }
4651
4652 if (Instruction *NewXor = foldNotXor(I, Builder))
4653 return NewXor;
4654
4655 // TODO: Could handle multi-use better by checking if all uses of NotOp (other
4656 // than I) can be inverted.
4657 if (Value *R = getFreelyInverted(NotOp, NotOp->hasOneUse(), &Builder))
4658 return replaceInstUsesWith(I, R);
4659
4660 return nullptr;
4661}
4662
4663// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
4664// here. We should standardize that construct where it is needed or choose some
4665// other way to ensure that commutated variants of patterns are not missed.
4667 if (Value *V = simplifyXorInst(I.getOperand(0), I.getOperand(1),
4669 return replaceInstUsesWith(I, V);
4670
4672 return &I;
4673
4675 return X;
4676
4678 return Phi;
4679
4680 if (Instruction *NewXor = foldXorToXor(I, Builder))
4681 return NewXor;
4682
4683 // (A&B)^(A&C) -> A&(B^C) etc
4685 return replaceInstUsesWith(I, V);
4686
4687 // See if we can simplify any instructions used by the instruction whose sole
4688 // purpose is to compute bits we don't care about.
4690 return &I;
4691
4692 if (Instruction *R = foldNot(I))
4693 return R;
4694
4696 return R;
4697
4698 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
4699 Value *X, *Y, *M;
4700
4701 // (X | Y) ^ M -> (X ^ M) ^ Y
4702 // (X | Y) ^ M -> (Y ^ M) ^ X
4704 m_Value(M)))) {
4705 if (Value *XorAC = simplifyXorInst(X, M, SQ.getWithInstruction(&I)))
4706 return BinaryOperator::CreateXor(XorAC, Y);
4707
4708 if (Value *XorBC = simplifyXorInst(Y, M, SQ.getWithInstruction(&I)))
4709 return BinaryOperator::CreateXor(XorBC, X);
4710 }
4711
4712 // Fold (X & M) ^ (Y & ~M) -> (X & M) | (Y & ~M)
4713 // This it a special case in haveNoCommonBitsSet, but the computeKnownBits
4714 // calls in there are unnecessary as SimplifyDemandedInstructionBits should
4715 // have already taken care of those cases.
4716 if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(M)), m_Value()),
4717 m_c_And(m_Deferred(M), m_Value())))) {
4719 return BinaryOperator::CreateDisjointOr(Op0, Op1);
4720 else
4721 return BinaryOperator::CreateOr(Op0, Op1);
4722 }
4723
4725 return Xor;
4726
4727 Constant *C1;
4728 if (match(Op1, m_Constant(C1))) {
4729 Constant *C2;
4730
4731 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_ImmConstant(C2)))) &&
4732 match(C1, m_ImmConstant())) {
4733 // (X | C2) ^ C1 --> (X & ~C2) ^ (C1^C2)
4738 return BinaryOperator::CreateXor(
4740 }
4741
4742 // Use DeMorgan and reassociation to eliminate a 'not' op.
4743 if (match(Op0, m_OneUse(m_Or(m_Not(m_Value(X)), m_Constant(C2))))) {
4744 // (~X | C2) ^ C1 --> ((X & ~C2) ^ -1) ^ C1 --> (X & ~C2) ^ ~C1
4746 return BinaryOperator::CreateXor(And, ConstantExpr::getNot(C1));
4747 }
4748 if (match(Op0, m_OneUse(m_And(m_Not(m_Value(X)), m_Constant(C2))))) {
4749 // (~X & C2) ^ C1 --> ((X | ~C2) ^ -1) ^ C1 --> (X | ~C2) ^ ~C1
4751 return BinaryOperator::CreateXor(Or, ConstantExpr::getNot(C1));
4752 }
4753
4754 // Convert xor ([trunc] (ashr X, BW-1)), C =>
4755 // select(X >s -1, C, ~C)
4756 // The ashr creates "AllZeroOrAllOne's", which then optionally inverses the
4757 // constant depending on whether this input is less than 0.
4758 const APInt *CA;
4759 if (match(Op0, m_OneUse(m_TruncOrSelf(
4760 m_AShr(m_Value(X), m_APIntAllowPoison(CA))))) &&
4761 *CA == X->getType()->getScalarSizeInBits() - 1 &&
4762 !match(C1, m_AllOnes())) {
4763 assert(!C1->isZeroValue() && "Unexpected xor with 0");
4764 Value *IsNotNeg = Builder.CreateIsNotNeg(X);
4765 return SelectInst::Create(IsNotNeg, Op1, Builder.CreateNot(Op1));
4766 }
4767 }
4768
4769 Type *Ty = I.getType();
4770 {
4771 const APInt *RHSC;
4772 if (match(Op1, m_APInt(RHSC))) {
4773 Value *X;
4774 const APInt *C;
4775 // (C - X) ^ signmaskC --> (C + signmaskC) - X
4776 if (RHSC->isSignMask() && match(Op0, m_Sub(m_APInt(C), m_Value(X))))
4777 return BinaryOperator::CreateSub(ConstantInt::get(Ty, *C + *RHSC), X);
4778
4779 // (X + C) ^ signmaskC --> X + (C + signmaskC)
4780 if (RHSC->isSignMask() && match(Op0, m_Add(m_Value(X), m_APInt(C))))
4781 return BinaryOperator::CreateAdd(X, ConstantInt::get(Ty, *C + *RHSC));
4782
4783 // (X | C) ^ RHSC --> X ^ (C ^ RHSC) iff X & C == 0
4784 if (match(Op0, m_Or(m_Value(X), m_APInt(C))) &&
4785 MaskedValueIsZero(X, *C, 0, &I))
4786 return BinaryOperator::CreateXor(X, ConstantInt::get(Ty, *C ^ *RHSC));
4787
4788 // When X is a power-of-two or zero and zero input is poison:
4789 // ctlz(i32 X) ^ 31 --> cttz(X)
4790 // cttz(i32 X) ^ 31 --> ctlz(X)
4791 auto *II = dyn_cast<IntrinsicInst>(Op0);
4792 if (II && II->hasOneUse() && *RHSC == Ty->getScalarSizeInBits() - 1) {
4793 Intrinsic::ID IID = II->getIntrinsicID();
4794 if ((IID == Intrinsic::ctlz || IID == Intrinsic::cttz) &&
4795 match(II->getArgOperand(1), m_One()) &&
4796 isKnownToBeAPowerOfTwo(II->getArgOperand(0), /*OrZero */ true)) {
4797 IID = (IID == Intrinsic::ctlz) ? Intrinsic::cttz : Intrinsic::ctlz;
4798 Function *F =
4799 Intrinsic::getOrInsertDeclaration(II->getModule(), IID, Ty);
4800 return CallInst::Create(F, {II->getArgOperand(0), Builder.getTrue()});
4801 }
4802 }
4803
4804 // If RHSC is inverting the remaining bits of shifted X,
4805 // canonicalize to a 'not' before the shift to help SCEV and codegen:
4806 // (X << C) ^ RHSC --> ~X << C
4807 if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_APInt(C)))) &&
4808 *RHSC == APInt::getAllOnes(Ty->getScalarSizeInBits()).shl(*C)) {
4809 Value *NotX = Builder.CreateNot(X);
4810 return BinaryOperator::CreateShl(NotX, ConstantInt::get(Ty, *C));
4811 }
4812 // (X >>u C) ^ RHSC --> ~X >>u C
4813 if (match(Op0, m_OneUse(m_LShr(m_Value(X), m_APInt(C)))) &&
4814 *RHSC == APInt::getAllOnes(Ty->getScalarSizeInBits()).lshr(*C)) {
4815 Value *NotX = Builder.CreateNot(X);
4816 return BinaryOperator::CreateLShr(NotX, ConstantInt::get(Ty, *C));
4817 }
4818 // TODO: We could handle 'ashr' here as well. That would be matching
4819 // a 'not' op and moving it before the shift. Doing that requires
4820 // preventing the inverse fold in canShiftBinOpWithConstantRHS().
4821 }
4822
4823 // If we are XORing the sign bit of a floating-point value, convert
4824 // this to fneg, then cast back to integer.
4825 //
4826 // This is generous interpretation of noimplicitfloat, this is not a true
4827 // floating-point operation.
4828 //
4829 // Assumes any IEEE-represented type has the sign bit in the high bit.
4830 // TODO: Unify with APInt matcher. This version allows undef unlike m_APInt
4831 Value *CastOp;
4832 if (match(Op0, m_ElementWiseBitCast(m_Value(CastOp))) &&
4833 match(Op1, m_SignMask()) &&
4835 Attribute::NoImplicitFloat)) {
4836 Type *EltTy = CastOp->getType()->getScalarType();
4837 if (EltTy->isFloatingPointTy() && EltTy->isIEEE()) {
4838 Value *FNeg = Builder.CreateFNeg(CastOp);
4839 return new BitCastInst(FNeg, I.getType());
4840 }
4841 }
4842 }
4843
4844 // FIXME: This should not be limited to scalar (pull into APInt match above).
4845 {
4846 Value *X;
4847 ConstantInt *C1, *C2, *C3;
4848 // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
4849 if (match(Op1, m_ConstantInt(C3)) &&
4851 m_ConstantInt(C2))) &&
4852 Op0->hasOneUse()) {
4853 // fold (C1 >> C2) ^ C3
4854 APInt FoldConst = C1->getValue().lshr(C2->getValue());
4855 FoldConst ^= C3->getValue();
4856 // Prepare the two operands.
4857 auto *Opnd0 = Builder.CreateLShr(X, C2);
4858 Opnd0->takeName(Op0);
4859 return BinaryOperator::CreateXor(Opnd0, ConstantInt::get(Ty, FoldConst));
4860 }
4861 }
4862
4863 if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
4864 return FoldedLogic;
4865
4866 // Y ^ (X | Y) --> X & ~Y
4867 // Y ^ (Y | X) --> X & ~Y
4868 if (match(Op1, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op0)))))
4869 return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op0));
4870 // (X | Y) ^ Y --> X & ~Y
4871 // (Y | X) ^ Y --> X & ~Y
4872 if (match(Op0, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op1)))))
4873 return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op1));
4874
4875 // Y ^ (X & Y) --> ~X & Y
4876 // Y ^ (Y & X) --> ~X & Y
4877 if (match(Op1, m_OneUse(m_c_And(m_Value(X), m_Specific(Op0)))))
4878 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(X));
4879 // (X & Y) ^ Y --> ~X & Y
4880 // (Y & X) ^ Y --> ~X & Y
4881 // Canonical form is (X & C) ^ C; don't touch that.
4882 // TODO: A 'not' op is better for analysis and codegen, but demanded bits must
4883 // be fixed to prefer that (otherwise we get infinite looping).
4884 if (!match(Op1, m_Constant()) &&
4885 match(Op0, m_OneUse(m_c_And(m_Value(X), m_Specific(Op1)))))
4886 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(X));
4887
4888 Value *A, *B, *C;
4889 // (A ^ B) ^ (A | C) --> (~A & C) ^ B -- There are 4 commuted variants.
4892 return BinaryOperator::CreateXor(
4894
4895 // (A ^ B) ^ (B | C) --> (~B & C) ^ A -- There are 4 commuted variants.
4898 return BinaryOperator::CreateXor(
4900
4901 // (A & B) ^ (A ^ B) -> (A | B)
4902 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
4904 return BinaryOperator::CreateOr(A, B);
4905 // (A ^ B) ^ (A & B) -> (A | B)
4906 if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
4908 return BinaryOperator::CreateOr(A, B);
4909
4910 // (A & ~B) ^ ~A -> ~(A & B)
4911 // (~B & A) ^ ~A -> ~(A & B)
4912 if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
4913 match(Op1, m_Not(m_Specific(A))))
4915
4916 // (~A & B) ^ A --> A | B -- There are 4 commuted variants.
4918 return BinaryOperator::CreateOr(A, B);
4919
4920 // (~A | B) ^ A --> ~(A & B)
4921 if (match(Op0, m_OneUse(m_c_Or(m_Not(m_Specific(Op1)), m_Value(B)))))
4923
4924 // A ^ (~A | B) --> ~(A & B)
4925 if (match(Op1, m_OneUse(m_c_Or(m_Not(m_Specific(Op0)), m_Value(B)))))
4927
4928 // (A | B) ^ (A | C) --> (B ^ C) & ~A -- There are 4 commuted variants.
4929 // TODO: Loosen one-use restriction if common operand is a constant.
4930 Value *D;
4931 if (match(Op0, m_OneUse(m_Or(m_Value(A), m_Value(B)))) &&
4932 match(Op1, m_OneUse(m_Or(m_Value(C), m_Value(D))))) {
4933 if (B == C || B == D)
4934 std::swap(A, B);
4935 if (A == C)
4936 std::swap(C, D);
4937 if (A == D) {
4938 Value *NotA = Builder.CreateNot(A);
4939 return BinaryOperator::CreateAnd(Builder.CreateXor(B, C), NotA);
4940 }
4941 }
4942
4943 // (A & B) ^ (A | C) --> A ? ~B : C -- There are 4 commuted variants.
4944 if (I.getType()->isIntOrIntVectorTy(1) &&
4947 bool NeedFreeze = isa<SelectInst>(Op0) && isa<SelectInst>(Op1) && B == D;
4948 if (B == C || B == D)
4949 std::swap(A, B);
4950 if (A == C)
4951 std::swap(C, D);
4952 if (A == D) {
4953 if (NeedFreeze)
4955 Value *NotB = Builder.CreateNot(B);
4956 return SelectInst::Create(A, NotB, C);
4957 }
4958 }
4959
4960 if (auto *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
4961 if (auto *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
4962 if (Value *V = foldXorOfICmps(LHS, RHS, I))
4963 return replaceInstUsesWith(I, V);
4964
4965 if (Instruction *CastedXor = foldCastedBitwiseLogic(I))
4966 return CastedXor;
4967
4968 if (Instruction *Abs = canonicalizeAbs(I, Builder))
4969 return Abs;
4970
4971 // Otherwise, if all else failed, try to hoist the xor-by-constant:
4972 // (X ^ C) ^ Y --> (X ^ Y) ^ C
4973 // Just like we do in other places, we completely avoid the fold
4974 // for constantexprs, at least to avoid endless combine loop.
4977 m_ImmConstant(C1))),
4978 m_Value(Y))))
4979 return BinaryOperator::CreateXor(Builder.CreateXor(X, Y), C1);
4980
4982 return R;
4983
4984 if (Instruction *Canonicalized = canonicalizeLogicFirst(I, Builder))
4985 return Canonicalized;
4986
4987 if (Instruction *Folded = foldLogicOfIsFPClass(I, Op0, Op1))
4988 return Folded;
4989
4990 if (Instruction *Folded = canonicalizeConditionalNegationViaMathToSelect(I))
4991 return Folded;
4992
4993 if (Instruction *Res = foldBinOpOfDisplacedShifts(I))
4994 return Res;
4995
4997 return Res;
4998
4999 return nullptr;
5000}
AMDGPU Register Bank Select
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isSigned(unsigned int Opcode)
static Value * foldIsPowerOf2OrZero(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd, InstCombiner::BuilderTy &Builder, InstCombinerImpl &IC)
Fold (icmp eq ctpop(X) 1) | (icmp eq X 0) into (icmp ult ctpop(X) 2) and fold (icmp ne ctpop(X) 1) & ...
static unsigned conjugateICmpMask(unsigned Mask)
Convert an analysis of a masked ICmp into its equivalent if all boolean operations had the opposite s...
static Instruction * foldNotXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static Value * foldLogOpOfMaskedICmps(Value *LHS, Value *RHS, bool IsAnd, bool IsLogical, InstCombiner::BuilderTy &Builder, const SimplifyQuery &Q)
Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single (icmp(A & X) ==/!...
static Value * getFCmpValue(unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy &Builder, FMFSource FMF)
This is the complement of getFCmpCode, which turns an opcode and two operands into either a FCmp inst...
static bool matchIsFPClassLikeFCmp(Value *Op, Value *&ClassVal, uint64_t &ClassMask)
Match an fcmp against a special value that performs a test possible by llvm.is.fpclass.
static Value * foldSignedTruncationCheck(ICmpInst *ICmp0, ICmpInst *ICmp1, Instruction &CxtI, InstCombiner::BuilderTy &Builder)
General pattern: X & Y.
static Instruction * visitMaskedMerge(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
If we have a masked merge, in the canonical form of: (assuming that A only has one use....
static Instruction * canonicalizeAbs(BinaryOperator &Xor, InstCombiner::BuilderTy &Builder)
Canonicalize a shifty way to code absolute value to the more common pattern that uses negation and se...
static Value * foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd, InstCombiner::BuilderTy &Builder, InstCombinerImpl &IC)
Reduce a pair of compares that check if a value has exactly 1 bit set.
static Value * foldUnsignedUnderflowCheck(ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd, const SimplifyQuery &Q, InstCombiner::BuilderTy &Builder)
Commuted variants are assumed to be handled by calling this function again with the parameters swappe...
static Instruction * foldOrToXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static Value * simplifyAndOrWithOpReplaced(Value *V, Value *Op, Value *RepOp, bool SimplifyOnly, InstCombinerImpl &IC, unsigned Depth=0)
static Instruction * matchDeMorgansLaws(BinaryOperator &I, InstCombiner &IC)
Match variations of De Morgan's Laws: (~A & ~B) == (~(A | B)) (~A | ~B) == (~(A & B))
static Value * foldLogOpOfMaskedICmpsAsymmetric(Value *LHS, Value *RHS, bool IsAnd, Value *A, Value *B, Value *C, Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR, unsigned LHSMask, unsigned RHSMask, InstCombiner::BuilderTy &Builder)
Try to fold (icmp(A & B) ==/!= 0) &/| (icmp(A & D) ==/!= E) into a single (icmp(A & X) ==/!...
static Instruction * foldAndToXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static unsigned getMaskedICmpType(Value *A, Value *B, Value *C, ICmpInst::Predicate Pred)
Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C) satisfies.
static Instruction * foldXorToXor(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
A ^ B can be specified using other logic ops in a variety of patterns.
static bool canNarrowShiftAmt(Constant *C, unsigned BitWidth)
Return true if a constant shift amount is always less than the specified bit-width.
static Instruction * foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast, InstCombinerImpl &IC)
Fold {and,or,xor} (cast X), C.
static Value * foldAndOrOfICmpEqConstantAndICmp(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, bool IsLogical, IRBuilderBase &Builder)
static bool canFreelyInvert(InstCombiner &IC, Value *Op, Instruction *IgnoredUser)
static Value * foldNegativePower2AndShiftedMask(Value *A, Value *B, Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR, InstCombiner::BuilderTy &Builder)
Try to fold (icmp(A & B) == 0) & (icmp(A & D) != E) into (icmp A u< D) iff B is a contiguous set of o...
static Value * matchIsFiniteTest(InstCombiner::BuilderTy &Builder, FCmpInst *LHS, FCmpInst *RHS)
and (fcmp ord x, 0), (fcmp u* x, inf) -> fcmp o* x, inf
static Value * foldPowerOf2AndShiftedMask(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd, InstCombiner::BuilderTy &Builder)
Try to fold ((icmp X u< P) & (icmp(X & M) != M)) or ((icmp X s> -1) & (icmp(X & M) !...
static Value * stripSignOnlyFPOps(Value *Val)
Ignore all operations which only change the sign of a value, returning the underlying magnitude value...
static Value * freelyInvert(InstCombinerImpl &IC, Value *Op, Instruction *IgnoredUser)
static Value * foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(Value *LHS, Value *RHS, bool IsAnd, Value *A, Value *B, Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR, InstCombiner::BuilderTy &Builder)
Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single (icmp(A & X) ==/!...
static std::optional< IntPart > matchIntPart(Value *V)
Match an extraction of bits from an integer.
static Instruction * canonicalizeLogicFirst(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static Instruction * reassociateFCmps(BinaryOperator &BO, InstCombiner::BuilderTy &Builder)
This a limited reassociation for a special case (see above) where we are checking if two values are e...
static Value * getNewICmpValue(unsigned Code, bool Sign, Value *LHS, Value *RHS, InstCombiner::BuilderTy &Builder)
This is the complement of getICmpCode, which turns an opcode and two operands into either a constant ...
static Value * extractIntPart(const IntPart &P, IRBuilderBase &Builder)
Materialize an extraction of bits from an integer in IR.
static bool matchUnorderedInfCompare(FCmpInst::Predicate P, Value *LHS, Value *RHS)
Matches fcmp u__ x, +/-inf.
static Instruction * matchOrConcat(Instruction &Or, InstCombiner::BuilderTy &Builder)
Attempt to combine or(zext(x),shl(zext(y),bw/2) concat packing patterns.
static bool matchIsNotNaN(FCmpInst::Predicate P, Value *LHS, Value *RHS)
Matches canonical form of isnan, fcmp ord x, 0.
static bool areInverseVectorBitmasks(Constant *C1, Constant *C2)
If all elements of two constant vectors are 0/-1 and inverses, return true.
MaskedICmpType
Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns that can be simplified.
@ BMask_NotMixed
@ AMask_NotMixed
@ BMask_NotAllOnes
@ Mask_AllZeros
@ BMask_AllOnes
@ AMask_NotAllOnes
@ AMask_AllOnes
@ Mask_NotAllZeros
static Instruction * foldComplexAndOrPatterns(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
Try folding relatively complex patterns for both And and Or operations with all And and Or swapped.
static Value * foldOrOfInversions(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static Instruction * matchFunnelShift(Instruction &Or, InstCombinerImpl &IC)
Match UB-safe variants of the funnel shift intrinsic.
static Instruction * reassociateForUses(BinaryOperator &BO, InstCombinerImpl::BuilderTy &Builder)
Try to reassociate a pair of binops so that values with one use only are part of the same instruction...
static Value * foldAndOrOfICmpsWithPow2AndWithZero(InstCombiner::BuilderTy &Builder, ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, const SimplifyQuery &Q)
static Instruction * foldBitwiseLogicWithIntrinsics(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
static std::optional< std::pair< unsigned, unsigned > > getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C, Value *&D, Value *&E, Value *LHS, Value *RHS, ICmpInst::Predicate &PredL, ICmpInst::Predicate &PredR)
Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E).
static Value * foldAndOrOfICmpsWithConstEq(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd, bool IsLogical, InstCombiner::BuilderTy &Builder, const SimplifyQuery &Q)
Reduce logic-of-compares with equality to a constant by substituting a common operand with the consta...
This file provides internal interfaces used to implement the InstCombine.
This file provides the interface for the instcombine pass implementation.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:557
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define R2(n)
uint64_t High
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static unsigned getScalarSizeInBits(Type *Ty)
static constexpr int Concat[]
Value * RHS
Value * LHS
support::ulittle16_t & Lo
Definition: aarch32.cpp:204
support::ulittle16_t & Hi
Definition: aarch32.cpp:203
bool bitwiseIsEqual(const APFloat &RHS) const
Definition: APFloat.h:1410
APInt bitcastToAPInt() const
Definition: APFloat.h:1351
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1100
Class for arbitrary precision integers.
Definition: APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:986
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1520
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:910
unsigned countLeadingOnes() const
Definition: APInt.h:1603
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1922
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition: APInt.h:466
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1468
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1902
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition: APInt.h:1249
int32_t exactLogBase2() const
Definition: APInt.h:1761
APInt reverseBits() const
Definition: APInt.cpp:741
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1909
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1618
unsigned countLeadingZeros() const
Definition: APInt.h:1585
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:873
APInt byteSwap() const
Definition: APInt.cpp:719
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1257
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:440
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:306
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1915
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1431
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:219
Value * getRHS() const
bool isSigned() const
Whether the intrinsic is signed or unsigned.
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
Value * getLHS() const
BinaryOps getOpcode() const
Definition: InstrTypes.h:370
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static BinaryOperator * CreateWithCopiedFlags(BinaryOps Opc, Value *V1, Value *V2, Value *CopyO, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Definition: InstrTypes.h:218
This class represents a no-op cast from one type to another.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:444
Type * getSrcTy() const
Return the source type, as a convenience.
Definition: InstrTypes.h:613
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition: InstrTypes.h:608
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Type * getDestTy() const
Return the destination type, as a convenience.
Definition: InstrTypes.h:615
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:980
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:673
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:702
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:703
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:679
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:688
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:697
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:696
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:700
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:687
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:698
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:680
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:682
@ ICMP_EQ
equal
Definition: InstrTypes.h:694
@ ICMP_NE
not equal
Definition: InstrTypes.h:695
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:701
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:699
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:683
bool isSigned() const
Definition: InstrTypes.h:928
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:825
Predicate getOrderedPredicate() const
Definition: InstrTypes.h:798
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:787
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:763
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:22
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2644
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2631
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2658
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2637
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2279
static Constant * getExactLogBase2(Constant *C)
If C is a scalar/fixed width vector of known powers of 2, then this function returns a new scalar/fix...
Definition: Constants.cpp:2662
static Constant * getZero(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1057
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Definition: Constants.h:220
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:866
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:208
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:873
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:157
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:148
This class represents a range of values.
Definition: ConstantRange.h:47
std::optional< ConstantRange > exactUnionWith(const ConstantRange &CR) const
Union the two ranges and return the result if it can be represented exactly, otherwise return std::nu...
ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
const APInt & getLower() const
Return the lower value for this range.
bool isWrappedSet() const
Return true if this set wraps around the unsigned domain.
const APInt & getUpper() const
Return the upper value for this range.
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
std::optional< ConstantRange > exactIntersectWith(const ConstantRange &CR) const
Intersect the two ranges and return the result if it can be represented exactly, otherwise return std...
This is an important base class in LLVM.
Definition: Constant.h:42
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:784
static Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
Definition: Constants.cpp:808
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:76
This class represents an Operation in the Expression.
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
This instruction compares its operands according to the predicate given to the constructor.
This provides a helper for copying FMF from an instruction or setting specified flags.
Definition: IRBuilder.h:92
static FMFSource intersect(Value *A, Value *B)
Intersect the FMF from two instructions.
Definition: IRBuilder.h:106
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:731
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getInverseCmpPredicate() const
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
bool isEquality() const
Return true if this predicate is either EQ or NE.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:113
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2286
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2390
Value * CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1700
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:558
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2294
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:2051
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:485
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1053
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2045
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition: IRBuilder.h:2574
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1480
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Definition: IRBuilder.h:2186
Value * CreateIsNotNeg(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg > -1.
Definition: IRBuilder.h:2598
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:193
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2274
Value * CreateFCmpFMF(CmpInst::Predicate P, Value *LHS, Value *RHS, FMFSource FMFSource, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2398
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1733
Value * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type.
Definition: IRBuilder.cpp:889
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:900
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:505
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1757
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2270
Value * CreateIsNeg(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg < 0.
Definition: IRBuilder.h:2593
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1387
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2152
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2278
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition: IRBuilder.cpp:881
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1459
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2033
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1518
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1370
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition: IRBuilder.h:2588
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2019
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1540
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1671
Value * CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1688
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2302
Value * CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2282
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg == 0.
Definition: IRBuilder.h:2583
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:199
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1562
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2380
Value * CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1694
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1742
Instruction * canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(BinaryOperator &I)
Instruction * foldBinOpIntoSelectOrPhi(BinaryOperator &I)
This is a convenience wrapper function for the above two functions.
Instruction * visitOr(BinaryOperator &I)
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Performs a few simplifications for operators which are associative or commutative.
Value * foldUsingDistributiveLaws(BinaryOperator &I)
Tries to simplify binary operations which some other binary operation distributes over.
Instruction * foldBinOpShiftWithShift(BinaryOperator &I)
Value * insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi, bool isSigned, bool Inside)
Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise (V < Lo || V >= Hi).
bool sinkNotIntoLogicalOp(Instruction &I)
std::optional< std::pair< Intrinsic::ID, SmallVector< Value *, 3 > > > convertOrOfShiftsToFunnelShift(Instruction &Or)
Constant * getLosslessUnsignedTrunc(Constant *C, Type *TruncTy)
Instruction * visitAnd(BinaryOperator &I)
bool sinkNotIntoOtherHandOfLogicalOp(Instruction &I)
Instruction * foldBinopWithPhiOperands(BinaryOperator &BO)
For a binary operator with 2 phi operands, try to hoist the binary operation before the phi.
Instruction * foldAddLikeCommutative(Value *LHS, Value *RHS, bool NSW, bool NUW)
Common transforms for add / disjoint or.
Value * simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted)
Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
Instruction * tryFoldInstWithCtpopWithNot(Instruction *I)
Value * SimplifyAddWithRemainder(BinaryOperator &I)
Tries to simplify add operations using the definition of remainder.
Constant * getLosslessSignedTrunc(Constant *C, Type *TruncTy)
Instruction * visitXor(BinaryOperator &I)
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Tries to simplify operands to an integer instruction based on its demanded bits.
Instruction * foldVectorBinop(BinaryOperator &Inst)
Canonicalize the position of binops relative to shufflevector.
Instruction * matchBSwapOrBitReverse(Instruction &I, bool MatchBSwaps, bool MatchBitReversals)
Given an initial instruction, check to see if it is the root of a bswap/bitreverse idiom.
void freelyInvertAllUsersOf(Value *V, Value *IgnoredUser=nullptr)
Freely adapt every user of V as-if V was changed to !V.
The core instruction combiner logic.
Definition: InstCombiner.h:48
SimplifyQuery SQ
Definition: InstCombiner.h:77
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
Definition: InstCombiner.h:228
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
Definition: InstCombiner.h:443
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:388
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Definition: InstCombiner.h:65
const DataLayout & DL
Definition: InstCombiner.h:76
unsigned ComputeNumSignBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:455
static Value * peekThroughBitcast(Value *V, bool OneUseOnly=false)
Return the source operand of a potentially bitcasted value while optionally checking if it has one us...
Definition: InstCombiner.h:119
bool canFreelyInvertAllUsersOf(Instruction *V, Value *IgnoredUser)
Given i1 V, can every user of V be freely adapted if V is changed to !V ? InstCombine's freelyInvertA...
Definition: InstCombiner.h:244
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:332
DominatorTree & DT
Definition: InstCombiner.h:75
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombiner.h:433
BuilderTy & Builder
Definition: InstCombiner.h:61
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth=0, const Instruction *CxtI=nullptr) const
Definition: InstCombiner.h:450
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:209
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:338
void pushUsersToWorkList(Instruction &I)
When an instruction is simplified, add all users of the instruction to the work lists because they mi...
void push(Instruction *I)
Push the instruction onto the worklist stack.
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
Definition: Instruction.cpp:80
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:274
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
This class represents a sign extension of integer types.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
bool empty() const
Definition: SmallVector.h:81
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:270
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:243
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIEEE() const
Return whether the type is IEEE compatible, as defined by the eponymous method in APFloat.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:184
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isIEEELikeFPTy() const
Return true if this is a well-behaved IEEE-like type, which has a IEEE compatible layout as defined b...
Definition: Type.h:170
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
Value * getOperand(unsigned i) const
Definition: User.h:228
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
iterator_range< user_iterator > users()
Definition: Value.h:421
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Definition: Value.cpp:153
bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
Definition: Value.cpp:149
bool use_empty() const
Definition: Value.h:344
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Represents an op.with.overflow intrinsic.
This class represents zero extension of integer types.
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition: APInt.h:2227
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:731
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
Definition: PatternMatch.h:550
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:664
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
Definition: PatternMatch.h:726
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:619
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:982
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
cst_pred_ty< is_shifted_mask > m_ShiftedMask()
Definition: PatternMatch.h:515
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:826
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:764
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:885
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
Definition: PatternMatch.h:186
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
Definition: PatternMatch.h:990
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:560
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_or< CastInst_match< OpTy, SExtInst >, OpTy > m_SExtOrSelf(const OpTy &Op)
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition: PatternMatch.h:832
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:903
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:864
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:105
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
Definition: PatternMatch.h:627
DisjointOr_match< LHS, RHS, true > m_c_DisjointOr(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, FCmpInst > m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition: PatternMatch.h:322
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
cst_pred_ty< is_maxsignedvalue > m_MaxSignedValue()
Match an integer or vector with values having all bits except for the high bit set (0x7f....
Definition: PatternMatch.h:538
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
Definition: PatternMatch.h:773
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
m_Intrinsic_Ty< Opnd0, Opnd1 >::Ty m_CopySign(const Opnd0 &Op0, const Opnd1 &Op1)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_unless< Ty > m_Unless(const Ty &M)
Match if the inner matcher does NOT match.
Definition: PatternMatch.h:203
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
Definition: PatternMatch.h:698
NodeAddr< CodeNode * > Code
Definition: RDFGraph.h:388
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
@ Offset
Definition: DWP.cpp:480
Constant * getPredForFCmpCode(unsigned Code, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getFCmpCode.
bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
bool predicatesFoldable(CmpInst::Predicate P1, CmpInst::Predicate P2)
Return true if both predicates match sign or if at least one of them is an equality comparison (which...
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
Value * simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Or, fold the result or return null.
Value * simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Xor, fold the result or return null.
bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
std::optional< DecomposedBitTest > decomposeBitTest(Value *Cond, bool LookThroughTrunc=true, bool AllowNonZeroC=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
bool recognizeBSwapOrBitReverseIdiom(Instruction *I, bool MatchBSwaps, bool MatchBitReversals, SmallVectorImpl< Instruction * > &InsertedInsts)
Try to match a bswap or bitreverse idiom.
Definition: Local.cpp:4096
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:291
Value * simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
Value * simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
@ Other
Any other memory.
Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ And
Bitwise or logical AND of integers.
@ Add
Sum of integers.
std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
DWARFExpression::Operation Op
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
std::pair< Value *, FPClassTest > fcmpToClassTest(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Returns a pair of values, which if passed to llvm.is.fpclass, returns the same result as an fcmp with...
APFloat neg(APFloat X)
Returns the negated value of the argument.
Definition: APFloat.h:1540
unsigned getICmpCode(CmpInst::Predicate Pred)
Encode a icmp predicate into a three bit mask.
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
unsigned getFCmpCode(CmpInst::Predicate CC)
Similar to getICmpCode but for FCmpInst.
Constant * getPredForICmpCode(unsigned Code, bool Sign, Type *OpTy, CmpInst::Predicate &Pred)
This is the complement of getICmpCode.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:100
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:137
const DataLayout & DL
Definition: SimplifyQuery.h:71
const Instruction * CxtI
Definition: SimplifyQuery.h:75
const DominatorTree * DT
Definition: SimplifyQuery.h:73
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
Definition: SimplifyQuery.h:74