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