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