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