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