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