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