LLVM 22.0.0git
InstCombineSelect.cpp
Go to the documentation of this file.
1//===- InstCombineSelect.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 visitSelect function.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/STLExtras.h"
23#include "llvm/IR/BasicBlock.h"
24#include "llvm/IR/Constant.h"
26#include "llvm/IR/Constants.h"
28#include "llvm/IR/FMF.h"
29#include "llvm/IR/IRBuilder.h"
30#include "llvm/IR/InstrTypes.h"
31#include "llvm/IR/Instruction.h"
34#include "llvm/IR/Intrinsics.h"
35#include "llvm/IR/Operator.h"
37#include "llvm/IR/Type.h"
38#include "llvm/IR/User.h"
39#include "llvm/IR/Value.h"
44#include <cassert>
45#include <utility>
46
47#define DEBUG_TYPE "instcombine"
49
50using namespace llvm;
51using namespace PatternMatch;
52
53
54/// Replace a select operand based on an equality comparison with the identity
55/// constant of a binop.
57 const TargetLibraryInfo &TLI,
58 InstCombinerImpl &IC) {
59 // The select condition must be an equality compare with a constant operand.
60 Value *X;
61 Constant *C;
62 CmpPredicate Pred;
63 if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C))))
64 return nullptr;
65
66 bool IsEq;
67 if (ICmpInst::isEquality(Pred))
68 IsEq = Pred == ICmpInst::ICMP_EQ;
69 else if (Pred == FCmpInst::FCMP_OEQ)
70 IsEq = true;
71 else if (Pred == FCmpInst::FCMP_UNE)
72 IsEq = false;
73 else
74 return nullptr;
75
76 // A select operand must be a binop.
78 if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO)))
79 return nullptr;
80
81 // The compare constant must be the identity constant for that binop.
82 // If this a floating-point compare with 0.0, any zero constant will do.
83 Type *Ty = BO->getType();
85 if (IdC != C) {
86 if (!IdC || !CmpInst::isFPPredicate(Pred))
87 return nullptr;
88 if (!match(IdC, m_AnyZeroFP()) || !match(C, m_AnyZeroFP()))
89 return nullptr;
90 }
91
92 // Last, match the compare variable operand with a binop operand.
93 Value *Y;
94 if (!BO->isCommutative() && !match(BO, m_BinOp(m_Value(Y), m_Specific(X))))
95 return nullptr;
96 if (!match(BO, m_c_BinOp(m_Value(Y), m_Specific(X))))
97 return nullptr;
98
99 // +0.0 compares equal to -0.0, and so it does not behave as required for this
100 // transform. Bail out if we can not exclude that possibility.
101 if (isa<FPMathOperator>(BO))
102 if (!BO->hasNoSignedZeros() &&
105 return nullptr;
106
107 // BO = binop Y, X
108 // S = { select (cmp eq X, C), BO, ? } or { select (cmp ne X, C), ?, BO }
109 // =>
110 // S = { select (cmp eq X, C), Y, ? } or { select (cmp ne X, C), ?, Y }
111 return IC.replaceOperand(Sel, IsEq ? 1 : 2, Y);
112}
113
114/// This folds:
115/// select (icmp eq (and X, C1)), TC, FC
116/// iff C1 is a power 2 and the difference between TC and FC is a power-of-2.
117/// To something like:
118/// (shr (and (X, C1)), (log2(C1) - log2(TC-FC))) + FC
119/// Or:
120/// (shl (and (X, C1)), (log2(TC-FC) - log2(C1))) + FC
121/// With some variations depending if FC is larger than TC, or the shift
122/// isn't needed, or the bit widths don't match.
123static Value *foldSelectICmpAnd(SelectInst &Sel, Value *CondVal, Value *TrueVal,
124 Value *FalseVal, Value *V, const APInt &AndMask,
125 bool CreateAnd,
126 InstCombiner::BuilderTy &Builder) {
127 const APInt *SelTC, *SelFC;
128 if (!match(TrueVal, m_APInt(SelTC)) || !match(FalseVal, m_APInt(SelFC)))
129 return nullptr;
130
131 Type *SelType = Sel.getType();
132 // In general, when both constants are non-zero, we would need an offset to
133 // replace the select. This would require more instructions than we started
134 // with. But there's one special-case that we handle here because it can
135 // simplify/reduce the instructions.
136 const APInt &TC = *SelTC;
137 const APInt &FC = *SelFC;
138 if (!TC.isZero() && !FC.isZero()) {
139 if (TC.getBitWidth() != AndMask.getBitWidth())
140 return nullptr;
141 // If we have to create an 'and', then we must kill the cmp to not
142 // increase the instruction count.
143 if (CreateAnd && !CondVal->hasOneUse())
144 return nullptr;
145
146 // (V & AndMaskC) == 0 ? TC : FC --> TC | (V & AndMaskC)
147 // (V & AndMaskC) == 0 ? TC : FC --> TC ^ (V & AndMaskC)
148 // (V & AndMaskC) == 0 ? TC : FC --> TC + (V & AndMaskC)
149 // (V & AndMaskC) == 0 ? TC : FC --> TC - (V & AndMaskC)
150 Constant *TCC = ConstantInt::get(SelType, TC);
151 Constant *FCC = ConstantInt::get(SelType, FC);
152 Constant *MaskC = ConstantInt::get(SelType, AndMask);
153 for (auto Opc : {Instruction::Or, Instruction::Xor, Instruction::Add,
154 Instruction::Sub}) {
155 if (ConstantFoldBinaryOpOperands(Opc, TCC, MaskC, Sel.getDataLayout()) ==
156 FCC) {
157 if (CreateAnd)
158 V = Builder.CreateAnd(V, MaskC);
159 return Builder.CreateBinOp(Opc, TCC, V);
160 }
161 }
162
163 return nullptr;
164 }
165
166 // Make sure one of the select arms is a power-of-2.
167 if (!TC.isPowerOf2() && !FC.isPowerOf2())
168 return nullptr;
169
170 // Determine which shift is needed to transform result of the 'and' into the
171 // desired result.
172 const APInt &ValC = !TC.isZero() ? TC : FC;
173 unsigned ValZeros = ValC.logBase2();
174 unsigned AndZeros = AndMask.logBase2();
175 bool ShouldNotVal = !TC.isZero();
176 bool NeedShift = ValZeros != AndZeros;
177 bool NeedZExtTrunc =
178 SelType->getScalarSizeInBits() != V->getType()->getScalarSizeInBits();
179
180 // If we would need to create an 'and' + 'shift' + 'xor' + cast to replace
181 // a 'select' + 'icmp', then this transformation would result in more
182 // instructions and potentially interfere with other folding.
183 if (CreateAnd + ShouldNotVal + NeedShift + NeedZExtTrunc >
184 1 + CondVal->hasOneUse())
185 return nullptr;
186
187 // Insert the 'and' instruction on the input to the truncate.
188 if (CreateAnd)
189 V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask));
190
191 // If types don't match, we can still convert the select by introducing a zext
192 // or a trunc of the 'and'.
193 if (ValZeros > AndZeros) {
194 V = Builder.CreateZExtOrTrunc(V, SelType);
195 V = Builder.CreateShl(V, ValZeros - AndZeros);
196 } else if (ValZeros < AndZeros) {
197 V = Builder.CreateLShr(V, AndZeros - ValZeros);
198 V = Builder.CreateZExtOrTrunc(V, SelType);
199 } else {
200 V = Builder.CreateZExtOrTrunc(V, SelType);
201 }
202
203 // Okay, now we know that everything is set up, we just don't know whether we
204 // have a icmp_ne or icmp_eq and whether the true or false val is the zero.
205 if (ShouldNotVal)
206 V = Builder.CreateXor(V, ValC);
207
208 return V;
209}
210
211/// We want to turn code that looks like this:
212/// %C = or %A, %B
213/// %D = select %cond, %C, %A
214/// into:
215/// %C = select %cond, %B, 0
216/// %D = or %A, %C
217///
218/// Assuming that the specified instruction is an operand to the select, return
219/// a bitmask indicating which operands of this instruction are foldable if they
220/// equal the other incoming value of the select.
222 switch (I->getOpcode()) {
223 case Instruction::Add:
224 case Instruction::FAdd:
225 case Instruction::Mul:
226 case Instruction::FMul:
227 case Instruction::And:
228 case Instruction::Or:
229 case Instruction::Xor:
230 return 3; // Can fold through either operand.
231 case Instruction::Sub: // Can only fold on the amount subtracted.
232 case Instruction::FSub:
233 case Instruction::FDiv: // Can only fold on the divisor amount.
234 case Instruction::Shl: // Can only fold on the shift amount.
235 case Instruction::LShr:
236 case Instruction::AShr:
237 return 1;
238 default:
239 return 0; // Cannot fold
240 }
241}
242
243/// We have (select c, TI, FI), and we know that TI and FI have the same opcode.
245 Instruction *FI) {
246 // Don't break up min/max patterns. The hasOneUse checks below prevent that
247 // for most cases, but vector min/max with bitcasts can be transformed. If the
248 // one-use restrictions are eased for other patterns, we still don't want to
249 // obfuscate min/max.
250 if ((match(&SI, m_SMin(m_Value(), m_Value())) ||
251 match(&SI, m_SMax(m_Value(), m_Value())) ||
252 match(&SI, m_UMin(m_Value(), m_Value())) ||
253 match(&SI, m_UMax(m_Value(), m_Value()))))
254 return nullptr;
255
256 // If this is a cast from the same type, merge.
257 Value *Cond = SI.getCondition();
258 Type *CondTy = Cond->getType();
259 if (TI->getNumOperands() == 1 && TI->isCast()) {
260 Type *FIOpndTy = FI->getOperand(0)->getType();
261 if (TI->getOperand(0)->getType() != FIOpndTy)
262 return nullptr;
263
264 // The select condition may be a vector. We may only change the operand
265 // type if the vector width remains the same (and matches the condition).
266 if (auto *CondVTy = dyn_cast<VectorType>(CondTy)) {
267 if (!FIOpndTy->isVectorTy() ||
268 CondVTy->getElementCount() !=
269 cast<VectorType>(FIOpndTy)->getElementCount())
270 return nullptr;
271
272 // TODO: If the backend knew how to deal with casts better, we could
273 // remove this limitation. For now, there's too much potential to create
274 // worse codegen by promoting the select ahead of size-altering casts
275 // (PR28160).
276 //
277 // Note that ValueTracking's matchSelectPattern() looks through casts
278 // without checking 'hasOneUse' when it matches min/max patterns, so this
279 // transform may end up happening anyway.
280 if (TI->getOpcode() != Instruction::BitCast &&
281 (!TI->hasOneUse() || !FI->hasOneUse()))
282 return nullptr;
283 } else if (!TI->hasOneUse() || !FI->hasOneUse()) {
284 // TODO: The one-use restrictions for a scalar select could be eased if
285 // the fold of a select in visitLoadInst() was enhanced to match a pattern
286 // that includes a cast.
287 return nullptr;
288 }
289
290 // Fold this by inserting a select from the input values.
291 Value *NewSI =
293 SI.getName() + ".v", &SI);
295 TI->getType());
296 }
297
298 Value *OtherOpT, *OtherOpF;
299 bool MatchIsOpZero;
300 auto getCommonOp = [&](Instruction *TI, Instruction *FI, bool Commute,
301 bool Swapped = false) -> Value * {
302 assert(!(Commute && Swapped) &&
303 "Commute and Swapped can't set at the same time");
304 if (!Swapped) {
305 if (TI->getOperand(0) == FI->getOperand(0)) {
306 OtherOpT = TI->getOperand(1);
307 OtherOpF = FI->getOperand(1);
308 MatchIsOpZero = true;
309 return TI->getOperand(0);
310 } else if (TI->getOperand(1) == FI->getOperand(1)) {
311 OtherOpT = TI->getOperand(0);
312 OtherOpF = FI->getOperand(0);
313 MatchIsOpZero = false;
314 return TI->getOperand(1);
315 }
316 }
317
318 if (!Commute && !Swapped)
319 return nullptr;
320
321 // If we are allowing commute or swap of operands, then
322 // allow a cross-operand match. In that case, MatchIsOpZero
323 // means that TI's operand 0 (FI's operand 1) is the common op.
324 if (TI->getOperand(0) == FI->getOperand(1)) {
325 OtherOpT = TI->getOperand(1);
326 OtherOpF = FI->getOperand(0);
327 MatchIsOpZero = true;
328 return TI->getOperand(0);
329 } else if (TI->getOperand(1) == FI->getOperand(0)) {
330 OtherOpT = TI->getOperand(0);
331 OtherOpF = FI->getOperand(1);
332 MatchIsOpZero = false;
333 return TI->getOperand(1);
334 }
335 return nullptr;
336 };
337
338 if (TI->hasOneUse() || FI->hasOneUse()) {
339 // Cond ? -X : -Y --> -(Cond ? X : Y)
340 Value *X, *Y;
341 if (match(TI, m_FNeg(m_Value(X))) && match(FI, m_FNeg(m_Value(Y)))) {
342 // Intersect FMF from the fneg instructions and union those with the
343 // select.
345 FMF &= FI->getFastMathFlags();
346 FMF |= SI.getFastMathFlags();
347 Value *NewSel =
348 Builder.CreateSelect(Cond, X, Y, SI.getName() + ".v", &SI);
349 if (auto *NewSelI = dyn_cast<Instruction>(NewSel))
350 NewSelI->setFastMathFlags(FMF);
351 Instruction *NewFNeg = UnaryOperator::CreateFNeg(NewSel);
352 NewFNeg->setFastMathFlags(FMF);
353 return NewFNeg;
354 }
355
356 // Min/max intrinsic with a common operand can have the common operand
357 // pulled after the select. This is the same transform as below for binops,
358 // but specialized for intrinsic matching and without the restrictive uses
359 // clause.
360 auto *TII = dyn_cast<IntrinsicInst>(TI);
361 auto *FII = dyn_cast<IntrinsicInst>(FI);
362 if (TII && FII && TII->getIntrinsicID() == FII->getIntrinsicID()) {
363 if (match(TII, m_MaxOrMin(m_Value(), m_Value()))) {
364 if (Value *MatchOp = getCommonOp(TI, FI, true)) {
365 Value *NewSel =
366 Builder.CreateSelect(Cond, OtherOpT, OtherOpF, "minmaxop", &SI);
367 return CallInst::Create(TII->getCalledFunction(), {NewSel, MatchOp});
368 }
369 }
370
371 // select c, (ldexp v, e0), (ldexp v, e1) -> ldexp v, (select c, e0, e1)
372 // select c, (ldexp v0, e), (ldexp v1, e) -> ldexp (select c, v0, v1), e
373 //
374 // select c, (ldexp v0, e0), (ldexp v1, e1) ->
375 // ldexp (select c, v0, v1), (select c, e0, e1)
376 if (TII->getIntrinsicID() == Intrinsic::ldexp) {
377 Value *LdexpVal0 = TII->getArgOperand(0);
378 Value *LdexpExp0 = TII->getArgOperand(1);
379 Value *LdexpVal1 = FII->getArgOperand(0);
380 Value *LdexpExp1 = FII->getArgOperand(1);
381 if (LdexpExp0->getType() == LdexpExp1->getType()) {
382 FPMathOperator *SelectFPOp = cast<FPMathOperator>(&SI);
383 FastMathFlags FMF = cast<FPMathOperator>(TII)->getFastMathFlags();
384 FMF &= cast<FPMathOperator>(FII)->getFastMathFlags();
385 FMF |= SelectFPOp->getFastMathFlags();
386
387 Value *SelectVal = Builder.CreateSelect(Cond, LdexpVal0, LdexpVal1);
388 Value *SelectExp = Builder.CreateSelect(Cond, LdexpExp0, LdexpExp1);
389
390 CallInst *NewLdexp = Builder.CreateIntrinsic(
391 TII->getType(), Intrinsic::ldexp, {SelectVal, SelectExp});
392 NewLdexp->setFastMathFlags(FMF);
393 return replaceInstUsesWith(SI, NewLdexp);
394 }
395 }
396 }
397
398 auto CreateCmpSel = [&](std::optional<CmpPredicate> P,
399 bool Swapped) -> CmpInst * {
400 if (!P)
401 return nullptr;
402 auto *MatchOp = getCommonOp(TI, FI, ICmpInst::isEquality(*P),
403 ICmpInst::isRelational(*P) && Swapped);
404 if (!MatchOp)
405 return nullptr;
406 Value *NewSel = Builder.CreateSelect(Cond, OtherOpT, OtherOpF,
407 SI.getName() + ".v", &SI);
408 return new ICmpInst(MatchIsOpZero ? *P
410 MatchOp, NewSel);
411 };
412
413 // icmp with a common operand also can have the common operand
414 // pulled after the select.
415 CmpPredicate TPred, FPred;
416 if (match(TI, m_ICmp(TPred, m_Value(), m_Value())) &&
417 match(FI, m_ICmp(FPred, m_Value(), m_Value()))) {
418 if (auto *R =
419 CreateCmpSel(CmpPredicate::getMatching(TPred, FPred), false))
420 return R;
421 if (auto *R =
422 CreateCmpSel(CmpPredicate::getMatching(
424 true))
425 return R;
426 }
427 }
428
429 // Only handle binary operators (including two-operand getelementptr) with
430 // one-use here. As with the cast case above, it may be possible to relax the
431 // one-use constraint, but that needs be examined carefully since it may not
432 // reduce the total number of instructions.
433 if (TI->getNumOperands() != 2 || FI->getNumOperands() != 2 ||
434 !TI->isSameOperationAs(FI) ||
435 (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) ||
436 !TI->hasOneUse() || !FI->hasOneUse())
437 return nullptr;
438
439 // Figure out if the operations have any operands in common.
440 Value *MatchOp = getCommonOp(TI, FI, TI->isCommutative());
441 if (!MatchOp)
442 return nullptr;
443
444 // If the select condition is a vector, the operands of the original select's
445 // operands also must be vectors. This may not be the case for getelementptr
446 // for example.
447 if (CondTy->isVectorTy() && (!OtherOpT->getType()->isVectorTy() ||
448 !OtherOpF->getType()->isVectorTy()))
449 return nullptr;
450
451 // If we are sinking div/rem after a select, we may need to freeze the
452 // condition because div/rem may induce immediate UB with a poison operand.
453 // For example, the following transform is not safe if Cond can ever be poison
454 // because we can replace poison with zero and then we have div-by-zero that
455 // didn't exist in the original code:
456 // Cond ? x/y : x/z --> x / (Cond ? y : z)
457 auto *BO = dyn_cast<BinaryOperator>(TI);
458 if (BO && BO->isIntDivRem() && !isGuaranteedNotToBePoison(Cond)) {
459 // A udiv/urem with a common divisor is safe because UB can only occur with
460 // div-by-zero, and that would be present in the original code.
461 if (BO->getOpcode() == Instruction::SDiv ||
462 BO->getOpcode() == Instruction::SRem || MatchIsOpZero)
464 }
465
466 // If we reach here, they do have operations in common.
467 Value *NewSI = Builder.CreateSelect(Cond, OtherOpT, OtherOpF,
468 SI.getName() + ".v", &SI);
469 Value *Op0 = MatchIsOpZero ? MatchOp : NewSI;
470 Value *Op1 = MatchIsOpZero ? NewSI : MatchOp;
471 if (auto *BO = dyn_cast<BinaryOperator>(TI)) {
472 BinaryOperator *NewBO = BinaryOperator::Create(BO->getOpcode(), Op0, Op1);
473 NewBO->copyIRFlags(TI);
474 NewBO->andIRFlags(FI);
475 return NewBO;
476 }
477 if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
478 auto *FGEP = cast<GetElementPtrInst>(FI);
479 Type *ElementType = TGEP->getSourceElementType();
481 ElementType, Op0, Op1, TGEP->getNoWrapFlags() & FGEP->getNoWrapFlags());
482 }
483 llvm_unreachable("Expected BinaryOperator or GEP");
484 return nullptr;
485}
486
487static bool isSelect01(const APInt &C1I, const APInt &C2I) {
488 if (!C1I.isZero() && !C2I.isZero()) // One side must be zero.
489 return false;
490 return C1I.isOne() || C1I.isAllOnes() || C2I.isOne() || C2I.isAllOnes();
491}
492
493/// Try to fold the select into one of the operands to allow further
494/// optimization.
496 Value *FalseVal) {
497 // See the comment above getSelectFoldableOperands for a description of the
498 // transformation we are doing here.
499 auto TryFoldSelectIntoOp = [&](SelectInst &SI, Value *TrueVal,
500 Value *FalseVal,
501 bool Swapped) -> Instruction * {
502 auto *TVI = dyn_cast<BinaryOperator>(TrueVal);
503 if (!TVI || !TVI->hasOneUse() || isa<Constant>(FalseVal))
504 return nullptr;
505
506 unsigned SFO = getSelectFoldableOperands(TVI);
507 unsigned OpToFold = 0;
508 if ((SFO & 1) && FalseVal == TVI->getOperand(0))
509 OpToFold = 1;
510 else if ((SFO & 2) && FalseVal == TVI->getOperand(1))
511 OpToFold = 2;
512
513 if (!OpToFold)
514 return nullptr;
515
516 FastMathFlags FMF;
517 if (isa<FPMathOperator>(&SI))
518 FMF = SI.getFastMathFlags();
520 TVI->getOpcode(), TVI->getType(), true, FMF.noSignedZeros());
521 Value *OOp = TVI->getOperand(2 - OpToFold);
522 // Avoid creating select between 2 constants unless it's selecting
523 // between 0, 1 and -1.
524 const APInt *OOpC;
525 bool OOpIsAPInt = match(OOp, m_APInt(OOpC));
526 if (isa<Constant>(OOp) &&
527 (!OOpIsAPInt || !isSelect01(C->getUniqueInteger(), *OOpC)))
528 return nullptr;
529
530 // If the false value is a NaN then we have that the floating point math
531 // operation in the transformed code may not preserve the exact NaN
532 // bit-pattern -- e.g. `fadd sNaN, 0.0 -> qNaN`.
533 // This makes the transformation incorrect since the original program would
534 // have preserved the exact NaN bit-pattern.
535 // Avoid the folding if the false value might be a NaN.
536 if (isa<FPMathOperator>(&SI) &&
537 !computeKnownFPClass(FalseVal, FMF, fcNan, &SI).isKnownNeverNaN())
538 return nullptr;
539
540 Value *NewSel = Builder.CreateSelect(SI.getCondition(), Swapped ? C : OOp,
541 Swapped ? OOp : C, "", &SI);
542 if (isa<FPMathOperator>(&SI))
543 cast<Instruction>(NewSel)->setFastMathFlags(FMF);
544 NewSel->takeName(TVI);
545 BinaryOperator *BO =
546 BinaryOperator::Create(TVI->getOpcode(), FalseVal, NewSel);
547 BO->copyIRFlags(TVI);
548 if (isa<FPMathOperator>(&SI)) {
549 // Merge poison generating flags from the select.
550 BO->setHasNoNaNs(BO->hasNoNaNs() && FMF.noNaNs());
551 BO->setHasNoInfs(BO->hasNoInfs() && FMF.noInfs());
552 // Merge no-signed-zeros flag from the select.
553 // Otherwise we may produce zeros with different sign.
555 }
556 return BO;
557 };
558
559 if (Instruction *R = TryFoldSelectIntoOp(SI, TrueVal, FalseVal, false))
560 return R;
561
562 if (Instruction *R = TryFoldSelectIntoOp(SI, FalseVal, TrueVal, true))
563 return R;
564
565 return nullptr;
566}
567
568/// Try to fold a select to a min/max intrinsic. Many cases are already handled
569/// by matchDecomposedSelectPattern but here we handle the cases where more
570/// extensive modification of the IR is required.
571static Value *foldSelectICmpMinMax(const ICmpInst *Cmp, Value *TVal,
572 Value *FVal,
574 const SimplifyQuery &SQ) {
575 const Value *CmpLHS = Cmp->getOperand(0);
576 const Value *CmpRHS = Cmp->getOperand(1);
577 ICmpInst::Predicate Pred = Cmp->getPredicate();
578
579 // (X > Y) ? X : (Y - 1) ==> MIN(X, Y - 1)
580 // (X < Y) ? X : (Y + 1) ==> MAX(X, Y + 1)
581 // This transformation is valid when overflow corresponding to the sign of
582 // the comparison is poison and we must drop the non-matching overflow flag.
583 if (CmpRHS == TVal) {
584 std::swap(CmpLHS, CmpRHS);
585 Pred = CmpInst::getSwappedPredicate(Pred);
586 }
587
588 // TODO: consider handling 'or disjoint' as well, though these would need to
589 // be converted to 'add' instructions.
590 if (!(CmpLHS == TVal && isa<Instruction>(FVal)))
591 return nullptr;
592
593 if (Pred == CmpInst::ICMP_SGT &&
594 match(FVal, m_NSWAdd(m_Specific(CmpRHS), m_One()))) {
595 cast<Instruction>(FVal)->setHasNoUnsignedWrap(false);
596 return Builder.CreateBinaryIntrinsic(Intrinsic::smax, TVal, FVal);
597 }
598
599 if (Pred == CmpInst::ICMP_SLT &&
600 match(FVal, m_NSWAdd(m_Specific(CmpRHS), m_AllOnes()))) {
601 cast<Instruction>(FVal)->setHasNoUnsignedWrap(false);
602 return Builder.CreateBinaryIntrinsic(Intrinsic::smin, TVal, FVal);
603 }
604
605 if (Pred == CmpInst::ICMP_UGT &&
606 match(FVal, m_NUWAdd(m_Specific(CmpRHS), m_One()))) {
607 cast<Instruction>(FVal)->setHasNoSignedWrap(false);
608 return Builder.CreateBinaryIntrinsic(Intrinsic::umax, TVal, FVal);
609 }
610
611 // Note: We must use isKnownNonZero here because "sub nuw %x, 1" will be
612 // canonicalized to "add %x, -1" discarding the nuw flag.
613 if (Pred == CmpInst::ICMP_ULT &&
614 match(FVal, m_Add(m_Specific(CmpRHS), m_AllOnes())) &&
615 isKnownNonZero(CmpRHS, SQ)) {
616 cast<Instruction>(FVal)->setHasNoSignedWrap(false);
617 cast<Instruction>(FVal)->setHasNoUnsignedWrap(false);
618 return Builder.CreateBinaryIntrinsic(Intrinsic::umin, TVal, FVal);
619 }
620
621 return nullptr;
622}
623
624/// We want to turn:
625/// (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1)
626/// into:
627/// zext (icmp ne i32 (and X, (or Y, (shl 1, Z))), 0)
628/// Note:
629/// Z may be 0 if lshr is missing.
630/// Worst-case scenario is that we will replace 5 instructions with 5 different
631/// instructions, but we got rid of select.
632static Instruction *foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp,
633 Value *TVal, Value *FVal,
634 InstCombiner::BuilderTy &Builder) {
635 if (!(Cmp->hasOneUse() && Cmp->getOperand(0)->hasOneUse() &&
636 Cmp->getPredicate() == ICmpInst::ICMP_EQ &&
637 match(Cmp->getOperand(1), m_Zero()) && match(FVal, m_One())))
638 return nullptr;
639
640 // The TrueVal has general form of: and %B, 1
641 Value *B;
642 if (!match(TVal, m_OneUse(m_And(m_Value(B), m_One()))))
643 return nullptr;
644
645 // Where %B may be optionally shifted: lshr %X, %Z.
646 Value *X, *Z;
647 const bool HasShift = match(B, m_OneUse(m_LShr(m_Value(X), m_Value(Z))));
648
649 // The shift must be valid.
650 // TODO: This restricts the fold to constant shift amounts. Is there a way to
651 // handle variable shifts safely? PR47012
652 if (HasShift &&
654 APInt(SelType->getScalarSizeInBits(),
655 SelType->getScalarSizeInBits()))))
656 return nullptr;
657
658 if (!HasShift)
659 X = B;
660
661 Value *Y;
662 if (!match(Cmp->getOperand(0), m_c_And(m_Specific(X), m_Value(Y))))
663 return nullptr;
664
665 // ((X & Y) == 0) ? ((X >> Z) & 1) : 1 --> (X & (Y | (1 << Z))) != 0
666 // ((X & Y) == 0) ? (X & 1) : 1 --> (X & (Y | 1)) != 0
667 Constant *One = ConstantInt::get(SelType, 1);
668 Value *MaskB = HasShift ? Builder.CreateShl(One, Z) : One;
669 Value *FullMask = Builder.CreateOr(Y, MaskB);
670 Value *MaskedX = Builder.CreateAnd(X, FullMask);
671 Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX);
672 return new ZExtInst(ICmpNeZero, SelType);
673}
674
675/// We want to turn:
676/// (select (icmp eq (and X, C1), 0), 0, (shl [nsw/nuw] X, C2));
677/// iff C1 is a mask and the number of its leading zeros is equal to C2
678/// into:
679/// shl X, C2
681 Value *FVal,
682 InstCombiner::BuilderTy &Builder) {
683 CmpPredicate Pred;
684 Value *AndVal;
685 if (!match(Cmp, m_ICmp(Pred, m_Value(AndVal), m_Zero())))
686 return nullptr;
687
688 if (Pred == ICmpInst::ICMP_NE) {
689 Pred = ICmpInst::ICMP_EQ;
690 std::swap(TVal, FVal);
691 }
692
693 Value *X;
694 const APInt *C2, *C1;
695 if (Pred != ICmpInst::ICMP_EQ ||
696 !match(AndVal, m_And(m_Value(X), m_APInt(C1))) ||
697 !match(TVal, m_Zero()) || !match(FVal, m_Shl(m_Specific(X), m_APInt(C2))))
698 return nullptr;
699
700 if (!C1->isMask() ||
701 C1->countLeadingZeros() != static_cast<unsigned>(C2->getZExtValue()))
702 return nullptr;
703
704 auto *FI = dyn_cast<Instruction>(FVal);
705 if (!FI)
706 return nullptr;
707
708 FI->setHasNoSignedWrap(false);
709 FI->setHasNoUnsignedWrap(false);
710 return FVal;
711}
712
713/// We want to turn:
714/// (select (icmp sgt x, C), lshr (X, Y), ashr (X, Y)); iff C s>= -1
715/// (select (icmp slt x, C), ashr (X, Y), lshr (X, Y)); iff C s>= 0
716/// into:
717/// ashr (X, Y)
718static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal,
719 Value *FalseVal,
720 InstCombiner::BuilderTy &Builder) {
722 Value *CmpLHS = IC->getOperand(0);
723 Value *CmpRHS = IC->getOperand(1);
724 if (!CmpRHS->getType()->isIntOrIntVectorTy())
725 return nullptr;
726
727 Value *X, *Y;
728 unsigned Bitwidth = CmpRHS->getType()->getScalarSizeInBits();
729 if ((Pred != ICmpInst::ICMP_SGT ||
731 APInt::getAllOnes(Bitwidth)))) &&
732 (Pred != ICmpInst::ICMP_SLT ||
734 APInt::getZero(Bitwidth)))))
735 return nullptr;
736
737 // Canonicalize so that ashr is in FalseVal.
738 if (Pred == ICmpInst::ICMP_SLT)
739 std::swap(TrueVal, FalseVal);
740
741 if (match(TrueVal, m_LShr(m_Value(X), m_Value(Y))) &&
742 match(FalseVal, m_AShr(m_Specific(X), m_Specific(Y))) &&
743 match(CmpLHS, m_Specific(X))) {
744 const auto *Ashr = cast<Instruction>(FalseVal);
745 // if lshr is not exact and ashr is, this new ashr must not be exact.
746 bool IsExact = Ashr->isExact() && cast<Instruction>(TrueVal)->isExact();
747 return Builder.CreateAShr(X, Y, IC->getName(), IsExact);
748 }
749
750 return nullptr;
751}
752
753/// We want to turn:
754/// (select (icmp eq (and X, C1), 0), Y, (BinOp Y, C2))
755/// into:
756/// IF C2 u>= C1
757/// (BinOp Y, (shl (and X, C1), C3))
758/// ELSE
759/// (BinOp Y, (lshr (and X, C1), C3))
760/// iff:
761/// 0 on the RHS is the identity value (i.e add, xor, shl, etc...)
762/// C1 and C2 are both powers of 2
763/// where:
764/// IF C2 u>= C1
765/// C3 = Log(C2) - Log(C1)
766/// ELSE
767/// C3 = Log(C1) - Log(C2)
768///
769/// This transform handles cases where:
770/// 1. The icmp predicate is inverted
771/// 2. The select operands are reversed
772/// 3. The magnitude of C2 and C1 are flipped
773static Value *foldSelectICmpAndBinOp(Value *CondVal, Value *TrueVal,
774 Value *FalseVal, Value *V,
775 const APInt &AndMask, bool CreateAnd,
776 InstCombiner::BuilderTy &Builder) {
777 // Only handle integer compares.
778 if (!TrueVal->getType()->isIntOrIntVectorTy())
779 return nullptr;
780
781 unsigned C1Log = AndMask.logBase2();
782 Value *Y;
783 BinaryOperator *BinOp;
784 const APInt *C2;
785 bool NeedXor;
786 if (match(FalseVal, m_BinOp(m_Specific(TrueVal), m_Power2(C2)))) {
787 Y = TrueVal;
788 BinOp = cast<BinaryOperator>(FalseVal);
789 NeedXor = false;
790 } else if (match(TrueVal, m_BinOp(m_Specific(FalseVal), m_Power2(C2)))) {
791 Y = FalseVal;
792 BinOp = cast<BinaryOperator>(TrueVal);
793 NeedXor = true;
794 } else {
795 return nullptr;
796 }
797
798 // Check that 0 on RHS is identity value for this binop.
799 auto *IdentityC =
801 /*AllowRHSConstant*/ true);
802 if (IdentityC == nullptr || !IdentityC->isNullValue())
803 return nullptr;
804
805 unsigned C2Log = C2->logBase2();
806
807 bool NeedShift = C1Log != C2Log;
808 bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() !=
809 V->getType()->getScalarSizeInBits();
810
811 // Make sure we don't create more instructions than we save.
812 if ((NeedShift + NeedXor + NeedZExtTrunc + CreateAnd) >
813 (CondVal->hasOneUse() + BinOp->hasOneUse()))
814 return nullptr;
815
816 if (CreateAnd) {
817 // Insert the AND instruction on the input to the truncate.
818 V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask));
819 }
820
821 if (C2Log > C1Log) {
822 V = Builder.CreateZExtOrTrunc(V, Y->getType());
823 V = Builder.CreateShl(V, C2Log - C1Log);
824 } else if (C1Log > C2Log) {
825 V = Builder.CreateLShr(V, C1Log - C2Log);
826 V = Builder.CreateZExtOrTrunc(V, Y->getType());
827 } else
828 V = Builder.CreateZExtOrTrunc(V, Y->getType());
829
830 if (NeedXor)
831 V = Builder.CreateXor(V, *C2);
832
833 auto *Res = Builder.CreateBinOp(BinOp->getOpcode(), Y, V);
834 if (auto *BO = dyn_cast<BinaryOperator>(Res))
835 BO->copyIRFlags(BinOp);
836 return Res;
837}
838
839/// Canonicalize a set or clear of a masked set of constant bits to
840/// select-of-constants form.
842 InstCombiner::BuilderTy &Builder) {
843 Value *Cond = Sel.getCondition();
844 Value *T = Sel.getTrueValue();
845 Value *F = Sel.getFalseValue();
846 Type *Ty = Sel.getType();
847 Value *X;
848 const APInt *NotC, *C;
849
850 // Cond ? (X & ~C) : (X | C) --> (X & ~C) | (Cond ? 0 : C)
851 if (match(T, m_And(m_Value(X), m_APInt(NotC))) &&
852 match(F, m_OneUse(m_Or(m_Specific(X), m_APInt(C)))) && *NotC == ~(*C)) {
854 Constant *OrC = ConstantInt::get(Ty, *C);
855 Value *NewSel = Builder.CreateSelect(Cond, Zero, OrC, "masksel", &Sel);
856 return BinaryOperator::CreateOr(T, NewSel);
857 }
858
859 // Cond ? (X | C) : (X & ~C) --> (X & ~C) | (Cond ? C : 0)
860 if (match(F, m_And(m_Value(X), m_APInt(NotC))) &&
861 match(T, m_OneUse(m_Or(m_Specific(X), m_APInt(C)))) && *NotC == ~(*C)) {
863 Constant *OrC = ConstantInt::get(Ty, *C);
864 Value *NewSel = Builder.CreateSelect(Cond, OrC, Zero, "masksel", &Sel);
865 return BinaryOperator::CreateOr(F, NewSel);
866 }
867
868 return nullptr;
869}
870
871// select (x == 0), 0, x * y --> freeze(y) * x
872// select (y == 0), 0, x * y --> freeze(x) * y
873// select (x == 0), undef, x * y --> freeze(y) * x
874// select (x == undef), 0, x * y --> freeze(y) * x
875// Usage of mul instead of 0 will make the result more poisonous,
876// so the operand that was not checked in the condition should be frozen.
877// The latter folding is applied only when a constant compared with x is
878// is a vector consisting of 0 and undefs. If a constant compared with x
879// is a scalar undefined value or undefined vector then an expression
880// should be already folded into a constant.
881//
882// This also holds all operations such that Op(0) == 0
883// e.g. Shl, Umin, etc
885 InstCombinerImpl &IC) {
886 auto *CondVal = SI.getCondition();
887 auto *TrueVal = SI.getTrueValue();
888 auto *FalseVal = SI.getFalseValue();
889 Value *X, *Y;
891
892 // Assuming that constant compared with zero is not undef (but it may be
893 // a vector with some undef elements). Otherwise (when a constant is undef)
894 // the select expression should be already simplified.
895 if (!match(CondVal, m_ICmp(Predicate, m_Value(X), m_Zero())) ||
897 return nullptr;
898
900 std::swap(TrueVal, FalseVal);
901
902 // Check that TrueVal is a constant instead of matching it with m_Zero()
903 // to handle the case when it is a scalar undef value or a vector containing
904 // non-zero elements that are masked by undef elements in the compare
905 // constant.
906 auto *TrueValC = dyn_cast<Constant>(TrueVal);
907 if (TrueValC == nullptr || !isa<Instruction>(FalseVal))
908 return nullptr;
909
910 bool FreezeY;
911 if (match(FalseVal, m_c_Mul(m_Specific(X), m_Value(Y))) ||
912 match(FalseVal, m_c_And(m_Specific(X), m_Value(Y))) ||
913 match(FalseVal, m_FShl(m_Specific(X), m_Specific(X), m_Value(Y))) ||
914 match(FalseVal, m_FShr(m_Specific(X), m_Specific(X), m_Value(Y))) ||
915 match(FalseVal,
916 m_c_Intrinsic<Intrinsic::umin>(m_Specific(X), m_Value(Y)))) {
917 FreezeY = true;
918 } else if (match(FalseVal, m_IDiv(m_Specific(X), m_Value(Y))) ||
919 match(FalseVal, m_IRem(m_Specific(X), m_Value(Y)))) {
920 FreezeY = false;
921 } else {
922 return nullptr;
923 }
924
925 auto *ZeroC = cast<Constant>(cast<Instruction>(CondVal)->getOperand(1));
926 auto *MergedC = Constant::mergeUndefsWith(TrueValC, ZeroC);
927 // If X is compared with 0 then TrueVal could be either zero or undef.
928 // m_Zero match vectors containing some undef elements, but for scalars
929 // m_Undef should be used explicitly.
930 if (!match(MergedC, m_Zero()) && !match(MergedC, m_Undef()))
931 return nullptr;
932
933 auto *FalseValI = cast<Instruction>(FalseVal);
934 if (FreezeY) {
935 auto *FrY = IC.InsertNewInstBefore(new FreezeInst(Y, Y->getName() + ".fr"),
936 FalseValI->getIterator());
937 IC.replaceOperand(*FalseValI,
938 FalseValI->getOperand(0) == Y
939 ? 0
940 : (FalseValI->getOperand(1) == Y ? 1 : 2),
941 FrY);
942 }
943 return IC.replaceInstUsesWith(SI, FalseValI);
944}
945
946/// Transform patterns such as (a > b) ? a - b : 0 into usub.sat(a, b).
947/// There are 8 commuted/swapped variants of this pattern.
949 const Value *TrueVal,
950 const Value *FalseVal,
951 InstCombiner::BuilderTy &Builder) {
952 ICmpInst::Predicate Pred = ICI->getPredicate();
953 Value *A = ICI->getOperand(0);
954 Value *B = ICI->getOperand(1);
955
956 // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0
957 // (a == 0) ? 0 : a - 1 -> (a != 0) ? a - 1 : 0
958 if (match(TrueVal, m_Zero())) {
960 std::swap(TrueVal, FalseVal);
961 }
962
963 if (!match(FalseVal, m_Zero()))
964 return nullptr;
965
966 // ugt 0 is canonicalized to ne 0 and requires special handling
967 // (a != 0) ? a + -1 : 0 -> usub.sat(a, 1)
968 if (Pred == ICmpInst::ICMP_NE) {
969 if (match(B, m_Zero()) && match(TrueVal, m_Add(m_Specific(A), m_AllOnes())))
970 return Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A,
971 ConstantInt::get(A->getType(), 1));
972 return nullptr;
973 }
974
975 if (!ICmpInst::isUnsigned(Pred))
976 return nullptr;
977
978 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_ULT) {
979 // (b < a) ? a - b : 0 -> (a > b) ? a - b : 0
980 std::swap(A, B);
982 }
983
984 assert((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&
985 "Unexpected isUnsigned predicate!");
986
987 // Ensure the sub is of the form:
988 // (a > b) ? a - b : 0 -> usub.sat(a, b)
989 // (a > b) ? b - a : 0 -> -usub.sat(a, b)
990 // Checking for both a-b and a+(-b) as a constant.
991 bool IsNegative = false;
992 const APInt *C;
993 if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A))) ||
994 (match(A, m_APInt(C)) &&
995 match(TrueVal, m_Add(m_Specific(B), m_SpecificInt(-*C)))))
996 IsNegative = true;
997 else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B))) &&
998 !(match(B, m_APInt(C)) &&
999 match(TrueVal, m_Add(m_Specific(A), m_SpecificInt(-*C)))))
1000 return nullptr;
1001
1002 // If we are adding a negate and the sub and icmp are used anywhere else, we
1003 // would end up with more instructions.
1004 if (IsNegative && !TrueVal->hasOneUse() && !ICI->hasOneUse())
1005 return nullptr;
1006
1007 // (a > b) ? a - b : 0 -> usub.sat(a, b)
1008 // (a > b) ? b - a : 0 -> -usub.sat(a, b)
1009 Value *Result = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A, B);
1010 if (IsNegative)
1011 Result = Builder.CreateNeg(Result);
1012 return Result;
1013}
1014
1016 InstCombiner::BuilderTy &Builder) {
1017 if (!Cmp->hasOneUse())
1018 return nullptr;
1019
1020 // Match unsigned saturated add with constant.
1021 Value *Cmp0 = Cmp->getOperand(0);
1022 Value *Cmp1 = Cmp->getOperand(1);
1023 ICmpInst::Predicate Pred = Cmp->getPredicate();
1024 Value *X;
1025 const APInt *C;
1026
1027 // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
1028 // There are 8 commuted variants.
1029 // Canonicalize -1 (saturated result) to true value of the select.
1030 if (match(FVal, m_AllOnes())) {
1031 std::swap(TVal, FVal);
1032 Pred = CmpInst::getInversePredicate(Pred);
1033 }
1034 if (!match(TVal, m_AllOnes()))
1035 return nullptr;
1036
1037 // uge -1 is canonicalized to eq -1 and requires special handling
1038 // (a == -1) ? -1 : a + 1 -> uadd.sat(a, 1)
1039 if (Pred == ICmpInst::ICMP_EQ) {
1040 if (match(FVal, m_Add(m_Specific(Cmp0), m_One())) &&
1041 match(Cmp1, m_AllOnes())) {
1042 return Builder.CreateBinaryIntrinsic(
1043 Intrinsic::uadd_sat, Cmp0, ConstantInt::get(Cmp0->getType(), 1));
1044 }
1045 return nullptr;
1046 }
1047
1048 if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&
1049 match(FVal, m_Add(m_Specific(Cmp0), m_APIntAllowPoison(C))) &&
1050 match(Cmp1, m_SpecificIntAllowPoison(~*C))) {
1051 // (X u> ~C) ? -1 : (X + C) --> uadd.sat(X, C)
1052 // (X u>= ~C)? -1 : (X + C) --> uadd.sat(X, C)
1053 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp0,
1054 ConstantInt::get(Cmp0->getType(), *C));
1055 }
1056
1057 // Negative one does not work here because X u> -1 ? -1, X + -1 is not a
1058 // saturated add.
1059 if (Pred == ICmpInst::ICMP_UGT &&
1060 match(FVal, m_Add(m_Specific(Cmp0), m_APIntAllowPoison(C))) &&
1061 match(Cmp1, m_SpecificIntAllowPoison(~*C - 1)) && !C->isAllOnes()) {
1062 // (X u> ~C - 1) ? -1 : (X + C) --> uadd.sat(X, C)
1063 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp0,
1064 ConstantInt::get(Cmp0->getType(), *C));
1065 }
1066
1067 // Zero does not work here because X u>= 0 ? -1 : X -> is always -1, which is
1068 // not a saturated add.
1069 if (Pred == ICmpInst::ICMP_UGE &&
1070 match(FVal, m_Add(m_Specific(Cmp0), m_APIntAllowPoison(C))) &&
1071 match(Cmp1, m_SpecificIntAllowPoison(-*C)) && !C->isZero()) {
1072 // (X u >= -C) ? -1 : (X + C) --> uadd.sat(X, C)
1073 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp0,
1074 ConstantInt::get(Cmp0->getType(), *C));
1075 }
1076
1077 // Canonicalize predicate to less-than or less-or-equal-than.
1078 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
1079 std::swap(Cmp0, Cmp1);
1080 Pred = CmpInst::getSwappedPredicate(Pred);
1081 }
1082 if (Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_ULE)
1083 return nullptr;
1084
1085 // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
1086 // Strictness of the comparison is irrelevant.
1087 Value *Y;
1088 if (match(Cmp0, m_Not(m_Value(X))) &&
1089 match(FVal, m_c_Add(m_Specific(X), m_Value(Y))) && Y == Cmp1) {
1090 // (~X u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
1091 // (~X u< Y) ? -1 : (Y + X) --> uadd.sat(X, Y)
1092 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X, Y);
1093 }
1094 // The 'not' op may be included in the sum but not the compare.
1095 // Strictness of the comparison is irrelevant.
1096 X = Cmp0;
1097 Y = Cmp1;
1099 // (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y)
1100 // (X u< Y) ? -1 : (Y + ~X) --> uadd.sat(Y, ~X)
1101 BinaryOperator *BO = cast<BinaryOperator>(FVal);
1102 return Builder.CreateBinaryIntrinsic(
1103 Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1));
1104 }
1105 // The overflow may be detected via the add wrapping round.
1106 // This is only valid for strict comparison!
1107 if (Pred == ICmpInst::ICMP_ULT &&
1108 match(Cmp0, m_c_Add(m_Specific(Cmp1), m_Value(Y))) &&
1109 match(FVal, m_c_Add(m_Specific(Cmp1), m_Specific(Y)))) {
1110 // ((X + Y) u< X) ? -1 : (X + Y) --> uadd.sat(X, Y)
1111 // ((X + Y) u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
1112 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp1, Y);
1113 }
1114
1115 return nullptr;
1116}
1117
1118/// Try to match patterns with select and subtract as absolute difference.
1119static Value *foldAbsDiff(ICmpInst *Cmp, Value *TVal, Value *FVal,
1120 InstCombiner::BuilderTy &Builder) {
1121 auto *TI = dyn_cast<Instruction>(TVal);
1122 auto *FI = dyn_cast<Instruction>(FVal);
1123 if (!TI || !FI)
1124 return nullptr;
1125
1126 // Normalize predicate to gt/lt rather than ge/le.
1127 ICmpInst::Predicate Pred = Cmp->getStrictPredicate();
1128 Value *A = Cmp->getOperand(0);
1129 Value *B = Cmp->getOperand(1);
1130
1131 // Normalize "A - B" as the true value of the select.
1132 if (match(FI, m_Sub(m_Specific(A), m_Specific(B)))) {
1133 std::swap(FI, TI);
1134 Pred = ICmpInst::getSwappedPredicate(Pred);
1135 }
1136
1137 // With any pair of no-wrap subtracts:
1138 // (A > B) ? (A - B) : (B - A) --> abs(A - B)
1139 if (Pred == CmpInst::ICMP_SGT &&
1140 match(TI, m_Sub(m_Specific(A), m_Specific(B))) &&
1141 match(FI, m_Sub(m_Specific(B), m_Specific(A))) &&
1142 (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap()) &&
1143 (FI->hasNoSignedWrap() || FI->hasNoUnsignedWrap())) {
1144 // The remaining subtract is not "nuw" any more.
1145 // If there's one use of the subtract (no other use than the use we are
1146 // about to replace), then we know that the sub is "nsw" in this context
1147 // even if it was only "nuw" before. If there's another use, then we can't
1148 // add "nsw" to the existing instruction because it may not be safe in the
1149 // other user's context.
1150 TI->setHasNoUnsignedWrap(false);
1151 if (!TI->hasNoSignedWrap())
1152 TI->setHasNoSignedWrap(TI->hasOneUse());
1153 return Builder.CreateBinaryIntrinsic(Intrinsic::abs, TI, Builder.getTrue());
1154 }
1155
1156 return nullptr;
1157}
1158
1159/// Fold the following code sequence:
1160/// \code
1161/// int a = ctlz(x & -x);
1162// x ? 31 - a : a;
1163// // or
1164// x ? 31 - a : 32;
1165/// \code
1166///
1167/// into:
1168/// cttz(x)
1169static Instruction *foldSelectCtlzToCttz(ICmpInst *ICI, Value *TrueVal,
1170 Value *FalseVal,
1171 InstCombiner::BuilderTy &Builder) {
1172 unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits();
1173 if (!ICI->isEquality() || !match(ICI->getOperand(1), m_Zero()))
1174 return nullptr;
1175
1176 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
1177 std::swap(TrueVal, FalseVal);
1178
1179 Value *Ctlz;
1180 if (!match(FalseVal,
1181 m_Xor(m_Value(Ctlz), m_SpecificInt(BitWidth - 1))))
1182 return nullptr;
1183
1184 if (!match(Ctlz, m_Intrinsic<Intrinsic::ctlz>()))
1185 return nullptr;
1186
1187 if (TrueVal != Ctlz && !match(TrueVal, m_SpecificInt(BitWidth)))
1188 return nullptr;
1189
1190 Value *X = ICI->getOperand(0);
1191 auto *II = cast<IntrinsicInst>(Ctlz);
1192 if (!match(II->getOperand(0), m_c_And(m_Specific(X), m_Neg(m_Specific(X)))))
1193 return nullptr;
1194
1196 II->getModule(), Intrinsic::cttz, II->getType());
1197 return CallInst::Create(F, {X, II->getArgOperand(1)});
1198}
1199
1200/// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single
1201/// call to cttz/ctlz with flag 'is_zero_poison' cleared.
1202///
1203/// For example, we can fold the following code sequence:
1204/// \code
1205/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true)
1206/// %1 = icmp ne i32 %x, 0
1207/// %2 = select i1 %1, i32 %0, i32 32
1208/// \code
1209///
1210/// into:
1211/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
1212static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
1213 InstCombinerImpl &IC) {
1214 ICmpInst::Predicate Pred = ICI->getPredicate();
1215 Value *CmpLHS = ICI->getOperand(0);
1216 Value *CmpRHS = ICI->getOperand(1);
1217
1218 // Check if the select condition compares a value for equality.
1219 if (!ICI->isEquality())
1220 return nullptr;
1221
1222 Value *SelectArg = FalseVal;
1223 Value *ValueOnZero = TrueVal;
1224 if (Pred == ICmpInst::ICMP_NE)
1225 std::swap(SelectArg, ValueOnZero);
1226
1227 // Skip zero extend/truncate.
1228 Value *Count = nullptr;
1229 if (!match(SelectArg, m_ZExt(m_Value(Count))) &&
1230 !match(SelectArg, m_Trunc(m_Value(Count))))
1231 Count = SelectArg;
1232
1233 // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the
1234 // input to the cttz/ctlz is used as LHS for the compare instruction.
1235 Value *X;
1236 if (!match(Count, m_Intrinsic<Intrinsic::cttz>(m_Value(X))) &&
1237 !match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Value(X))))
1238 return nullptr;
1239
1240 // (X == 0) ? BitWidth : ctz(X)
1241 // (X == -1) ? BitWidth : ctz(~X)
1242 // (X == Y) ? BitWidth : ctz(X ^ Y)
1243 if ((X != CmpLHS || !match(CmpRHS, m_Zero())) &&
1244 (!match(X, m_Not(m_Specific(CmpLHS))) || !match(CmpRHS, m_AllOnes())) &&
1245 !match(X, m_c_Xor(m_Specific(CmpLHS), m_Specific(CmpRHS))))
1246 return nullptr;
1247
1248 IntrinsicInst *II = cast<IntrinsicInst>(Count);
1249
1250 // Check if the value propagated on zero is a constant number equal to the
1251 // sizeof in bits of 'Count'.
1252 unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits();
1253 if (match(ValueOnZero, m_SpecificInt(SizeOfInBits))) {
1254 // Explicitly clear the 'is_zero_poison' flag. It's always valid to go from
1255 // true to false on this flag, so we can replace it for all users.
1256 II->setArgOperand(1, ConstantInt::getFalse(II->getContext()));
1257 // A range annotation on the intrinsic may no longer be valid.
1258 II->dropPoisonGeneratingAnnotations();
1259 IC.addToWorklist(II);
1260 return SelectArg;
1261 }
1262
1263 // The ValueOnZero is not the bitwidth. But if the cttz/ctlz (and optional
1264 // zext/trunc) have one use (ending at the select), the cttz/ctlz result will
1265 // not be used if the input is zero. Relax to 'zero is poison' for that case.
1266 if (II->hasOneUse() && SelectArg->hasOneUse() &&
1267 !match(II->getArgOperand(1), m_One())) {
1268 II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));
1269 // noundef attribute on the intrinsic may no longer be valid.
1270 II->dropUBImplyingAttrsAndMetadata();
1271 IC.addToWorklist(II);
1272 }
1273
1274 return nullptr;
1275}
1276
1277static Value *canonicalizeSPF(ICmpInst &Cmp, Value *TrueVal, Value *FalseVal,
1278 InstCombinerImpl &IC) {
1279 Value *LHS, *RHS;
1280 // TODO: What to do with pointer min/max patterns?
1281 if (!TrueVal->getType()->isIntOrIntVectorTy())
1282 return nullptr;
1283
1285 matchDecomposedSelectPattern(&Cmp, TrueVal, FalseVal, LHS, RHS).Flavor;
1286 if (SPF == SelectPatternFlavor::SPF_ABS ||
1288 if (!Cmp.hasOneUse() && !RHS->hasOneUse())
1289 return nullptr; // TODO: Relax this restriction.
1290
1291 // Note that NSW flag can only be propagated for normal, non-negated abs!
1292 bool IntMinIsPoison = SPF == SelectPatternFlavor::SPF_ABS &&
1293 match(RHS, m_NSWNeg(m_Specific(LHS)));
1294 Constant *IntMinIsPoisonC =
1295 ConstantInt::get(Type::getInt1Ty(Cmp.getContext()), IntMinIsPoison);
1296 Value *Abs =
1297 IC.Builder.CreateBinaryIntrinsic(Intrinsic::abs, LHS, IntMinIsPoisonC);
1298
1300 return IC.Builder.CreateNeg(Abs); // Always without NSW flag!
1301 return Abs;
1302 }
1303
1305 Intrinsic::ID IntrinsicID = getMinMaxIntrinsic(SPF);
1306 return IC.Builder.CreateBinaryIntrinsic(IntrinsicID, LHS, RHS);
1307 }
1308
1309 return nullptr;
1310}
1311
1313 unsigned Depth) {
1314 // Conservatively limit replacement to two instructions upwards.
1315 if (Depth == 2)
1316 return false;
1317
1318 assert(!isa<Constant>(Old) && "Only replace non-constant values");
1319
1320 auto *I = dyn_cast<Instruction>(V);
1321 if (!I || !I->hasOneUse() ||
1323 return false;
1324
1325 // Forbid potentially lane-crossing instructions.
1326 if (Old->getType()->isVectorTy() && !isNotCrossLaneOperation(I))
1327 return false;
1328
1329 bool Changed = false;
1330 for (Use &U : I->operands()) {
1331 if (U == Old) {
1332 replaceUse(U, New);
1333 Worklist.add(I);
1334 Changed = true;
1335 } else {
1336 Changed |= replaceInInstruction(U, Old, New, Depth + 1);
1337 }
1338 }
1339 return Changed;
1340}
1341
1342/// If we have a select with an equality comparison, then we know the value in
1343/// one of the arms of the select. See if substituting this value into an arm
1344/// and simplifying the result yields the same value as the other arm.
1345///
1346/// To make this transform safe, we must drop poison-generating flags
1347/// (nsw, etc) if we simplified to a binop because the select may be guarding
1348/// that poison from propagating. If the existing binop already had no
1349/// poison-generating flags, then this transform can be done by instsimplify.
1350///
1351/// Consider:
1352/// %cmp = icmp eq i32 %x, 2147483647
1353/// %add = add nsw i32 %x, 1
1354/// %sel = select i1 %cmp, i32 -2147483648, i32 %add
1355///
1356/// We can't replace %sel with %add unless we strip away the flags.
1357/// TODO: Wrapping flags could be preserved in some cases with better analysis.
1359 CmpInst &Cmp) {
1360 // Canonicalize the pattern to an equivalence on the predicate by swapping the
1361 // select operands.
1362 Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue();
1363 bool Swapped = false;
1364 if (Cmp.isEquivalence(/*Invert=*/true)) {
1365 std::swap(TrueVal, FalseVal);
1366 Swapped = true;
1367 } else if (!Cmp.isEquivalence()) {
1368 return nullptr;
1369 }
1370
1371 Value *CmpLHS = Cmp.getOperand(0), *CmpRHS = Cmp.getOperand(1);
1372 auto ReplaceOldOpWithNewOp = [&](Value *OldOp,
1373 Value *NewOp) -> Instruction * {
1374 // In X == Y ? f(X) : Z, try to evaluate f(Y) and replace the operand.
1375 // Take care to avoid replacing X == Y ? X : Z with X == Y ? Y : Z, as that
1376 // would lead to an infinite replacement cycle.
1377 // If we will be able to evaluate f(Y) to a constant, we can allow undef,
1378 // otherwise Y cannot be undef as we might pick different values for undef
1379 // in the cmp and in f(Y).
1380 if (TrueVal == OldOp && (isa<Constant>(OldOp) || !isa<Constant>(NewOp)))
1381 return nullptr;
1382
1383 if (Value *V = simplifyWithOpReplaced(TrueVal, OldOp, NewOp, SQ,
1384 /* AllowRefinement=*/true)) {
1385 // Need some guarantees about the new simplified op to ensure we don't inf
1386 // loop.
1387 // If we simplify to a constant, replace if we aren't creating new undef.
1388 if (match(V, m_ImmConstant()) &&
1389 isGuaranteedNotToBeUndef(V, SQ.AC, &Sel, &DT))
1390 return replaceOperand(Sel, Swapped ? 2 : 1, V);
1391
1392 // If NewOp is a constant and OldOp is not replace iff NewOp doesn't
1393 // contain and undef elements.
1394 // Make sure that V is always simpler than TrueVal, otherwise we might
1395 // end up in an infinite loop.
1396 if (match(NewOp, m_ImmConstant()) ||
1397 (isa<Instruction>(TrueVal) &&
1398 is_contained(cast<Instruction>(TrueVal)->operands(), V))) {
1399 if (isGuaranteedNotToBeUndef(NewOp, SQ.AC, &Sel, &DT))
1400 return replaceOperand(Sel, Swapped ? 2 : 1, V);
1401 return nullptr;
1402 }
1403 }
1404
1405 // Even if TrueVal does not simplify, we can directly replace a use of
1406 // CmpLHS with CmpRHS, as long as the instruction is not used anywhere
1407 // else and is safe to speculatively execute (we may end up executing it
1408 // with different operands, which should not cause side-effects or trigger
1409 // undefined behavior). Only do this if CmpRHS is a constant, as
1410 // profitability is not clear for other cases.
1411 if (OldOp == CmpLHS && match(NewOp, m_ImmConstant()) &&
1412 !match(OldOp, m_Constant()) &&
1413 isGuaranteedNotToBeUndef(NewOp, SQ.AC, &Sel, &DT))
1414 if (replaceInInstruction(TrueVal, OldOp, NewOp))
1415 return &Sel;
1416 return nullptr;
1417 };
1418
1419 if (Instruction *R = ReplaceOldOpWithNewOp(CmpLHS, CmpRHS))
1420 return R;
1421 if (Instruction *R = ReplaceOldOpWithNewOp(CmpRHS, CmpLHS))
1422 return R;
1423
1424 auto *FalseInst = dyn_cast<Instruction>(FalseVal);
1425 if (!FalseInst)
1426 return nullptr;
1427
1428 // InstSimplify already performed this fold if it was possible subject to
1429 // current poison-generating flags. Check whether dropping poison-generating
1430 // flags enables the transform.
1431
1432 // Try each equivalence substitution possibility.
1433 // We have an 'EQ' comparison, so the select's false value will propagate.
1434 // Example:
1435 // (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1
1437 if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, SQ,
1438 /* AllowRefinement */ false,
1439 &DropFlags) == TrueVal ||
1440 simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, SQ,
1441 /* AllowRefinement */ false,
1442 &DropFlags) == TrueVal) {
1443 for (Instruction *I : DropFlags) {
1444 I->dropPoisonGeneratingAnnotations();
1445 Worklist.add(I);
1446 }
1447
1448 return replaceInstUsesWith(Sel, FalseVal);
1449 }
1450
1451 return nullptr;
1452}
1453
1454/// Fold the following code sequence:
1455/// \code
1456/// %XeqZ = icmp eq i64 %X, %Z
1457/// %YeqZ = icmp eq i64 %Y, %Z
1458/// %XeqY = icmp eq i64 %X, %Y
1459/// %not.YeqZ = xor i1 %YeqZ, true
1460/// %and = select i1 %not.YeqZ, i1 %XeqY, i1 false
1461/// %equal = select i1 %XeqZ, i1 %YeqZ, i1 %and
1462/// \code
1463///
1464/// into:
1465/// %equal = icmp eq i64 %X, %Y
1467 Value *X, *Y, *Z;
1468 Value *XeqY, *XeqZ = Sel.getCondition(), *YeqZ = Sel.getTrueValue();
1469
1471 return nullptr;
1472
1473 if (!match(YeqZ,
1475 std::swap(X, Z);
1476
1477 if (!match(YeqZ,
1479 return nullptr;
1480
1481 if (!match(Sel.getFalseValue(),
1482 m_c_LogicalAnd(m_Not(m_Specific(YeqZ)), m_Value(XeqY))))
1483 return nullptr;
1484
1485 if (!match(XeqY,
1487 return nullptr;
1488
1489 cast<ICmpInst>(XeqY)->setSameSign(false);
1490 return replaceInstUsesWith(Sel, XeqY);
1491}
1492
1493// See if this is a pattern like:
1494// %old_cmp1 = icmp slt i32 %x, C2
1495// %old_replacement = select i1 %old_cmp1, i32 %target_low, i32 %target_high
1496// %old_x_offseted = add i32 %x, C1
1497// %old_cmp0 = icmp ult i32 %old_x_offseted, C0
1498// %r = select i1 %old_cmp0, i32 %x, i32 %old_replacement
1499// This can be rewritten as more canonical pattern:
1500// %new_cmp1 = icmp slt i32 %x, -C1
1501// %new_cmp2 = icmp sge i32 %x, C0-C1
1502// %new_clamped_low = select i1 %new_cmp1, i32 %target_low, i32 %x
1503// %r = select i1 %new_cmp2, i32 %target_high, i32 %new_clamped_low
1504// Iff -C1 s<= C2 s<= C0-C1
1505// Also ULT predicate can also be UGT iff C0 != -1 (+invert result)
1506// SLT predicate can also be SGT iff C2 != INT_MAX (+invert res.)
1507static Value *canonicalizeClampLike(SelectInst &Sel0, ICmpInst &Cmp0,
1508 InstCombiner::BuilderTy &Builder,
1509 InstCombiner &IC) {
1510 Value *X = Sel0.getTrueValue();
1511 Value *Sel1 = Sel0.getFalseValue();
1512
1513 // First match the condition of the outermost select.
1514 // Said condition must be one-use.
1515 if (!Cmp0.hasOneUse())
1516 return nullptr;
1517 ICmpInst::Predicate Pred0 = Cmp0.getPredicate();
1518 Value *Cmp00 = Cmp0.getOperand(0);
1519 Constant *C0;
1520 if (!match(Cmp0.getOperand(1),
1522 return nullptr;
1523
1524 if (!isa<SelectInst>(Sel1)) {
1525 Pred0 = ICmpInst::getInversePredicate(Pred0);
1526 std::swap(X, Sel1);
1527 }
1528
1529 // Canonicalize Cmp0 into ult or uge.
1530 // FIXME: we shouldn't care about lanes that are 'undef' in the end?
1531 switch (Pred0) {
1534 // Although icmp ult %x, 0 is an unusual thing to try and should generally
1535 // have been simplified, it does not verify with undef inputs so ensure we
1536 // are not in a strange state.
1537 if (!match(C0, m_SpecificInt_ICMP(
1540 return nullptr;
1541 break; // Great!
1544 // We want to canonicalize it to 'ult' or 'uge', so we'll need to increment
1545 // C0, which again means it must not have any all-ones elements.
1546 if (!match(C0,
1550 return nullptr; // Can't do, have all-ones element[s].
1552 C0 = InstCombiner::AddOne(C0);
1553 break;
1554 default:
1555 return nullptr; // Unknown predicate.
1556 }
1557
1558 // Now that we've canonicalized the ICmp, we know the X we expect;
1559 // the select in other hand should be one-use.
1560 if (!Sel1->hasOneUse())
1561 return nullptr;
1562
1563 // If the types do not match, look through any truncs to the underlying
1564 // instruction.
1565 if (Cmp00->getType() != X->getType() && X->hasOneUse())
1567
1568 // We now can finish matching the condition of the outermost select:
1569 // it should either be the X itself, or an addition of some constant to X.
1570 Constant *C1;
1571 if (Cmp00 == X)
1572 C1 = ConstantInt::getNullValue(X->getType());
1573 else if (!match(Cmp00,
1576 return nullptr;
1577
1578 Value *Cmp1;
1579 CmpPredicate Pred1;
1580 Constant *C2;
1581 Value *ReplacementLow, *ReplacementHigh;
1582 if (!match(Sel1, m_Select(m_Value(Cmp1), m_Value(ReplacementLow),
1583 m_Value(ReplacementHigh))) ||
1584 !match(Cmp1,
1585 m_ICmp(Pred1, m_Specific(X),
1587 return nullptr;
1588
1589 if (!Cmp1->hasOneUse() && (Cmp00 == X || !Cmp00->hasOneUse()))
1590 return nullptr; // Not enough one-use instructions for the fold.
1591 // FIXME: this restriction could be relaxed if Cmp1 can be reused as one of
1592 // two comparisons we'll need to build.
1593
1594 // Canonicalize Cmp1 into the form we expect.
1595 // FIXME: we shouldn't care about lanes that are 'undef' in the end?
1596 switch (Pred1) {
1598 break;
1600 // We'd have to increment C2 by one, and for that it must not have signed
1601 // max element, but then it would have been canonicalized to 'slt' before
1602 // we get here. So we can't do anything useful with 'sle'.
1603 return nullptr;
1605 // We want to canonicalize it to 'slt', so we'll need to increment C2,
1606 // which again means it must not have any signed max elements.
1607 if (!match(C2,
1610 C2->getType()->getScalarSizeInBits()))))
1611 return nullptr; // Can't do, have signed max element[s].
1612 C2 = InstCombiner::AddOne(C2);
1613 [[fallthrough]];
1615 // Also non-canonical, but here we don't need to change C2,
1616 // so we don't have any restrictions on C2, so we can just handle it.
1618 std::swap(ReplacementLow, ReplacementHigh);
1619 break;
1620 default:
1621 return nullptr; // Unknown predicate.
1622 }
1624 "Unexpected predicate type.");
1625
1626 // The thresholds of this clamp-like pattern.
1627 auto *ThresholdLowIncl = ConstantExpr::getNeg(C1);
1628 auto *ThresholdHighExcl = ConstantExpr::getSub(C0, C1);
1629
1632 "Unexpected predicate type.");
1633 if (Pred0 == ICmpInst::Predicate::ICMP_UGE)
1634 std::swap(ThresholdLowIncl, ThresholdHighExcl);
1635
1636 // The fold has a precondition 1: C2 s>= ThresholdLow
1637 auto *Precond1 = ConstantFoldCompareInstOperands(
1638 ICmpInst::Predicate::ICMP_SGE, C2, ThresholdLowIncl, IC.getDataLayout());
1639 if (!Precond1 || !match(Precond1, m_One()))
1640 return nullptr;
1641 // The fold has a precondition 2: C2 s<= ThresholdHigh
1642 auto *Precond2 = ConstantFoldCompareInstOperands(
1643 ICmpInst::Predicate::ICMP_SLE, C2, ThresholdHighExcl, IC.getDataLayout());
1644 if (!Precond2 || !match(Precond2, m_One()))
1645 return nullptr;
1646
1647 // If we are matching from a truncated input, we need to sext the
1648 // ReplacementLow and ReplacementHigh values. Only do the transform if they
1649 // are free to extend due to being constants.
1650 if (X->getType() != Sel0.getType()) {
1651 Constant *LowC, *HighC;
1652 if (!match(ReplacementLow, m_ImmConstant(LowC)) ||
1653 !match(ReplacementHigh, m_ImmConstant(HighC)))
1654 return nullptr;
1655 const DataLayout &DL = Sel0.getDataLayout();
1656 ReplacementLow =
1657 ConstantFoldCastOperand(Instruction::SExt, LowC, X->getType(), DL);
1658 ReplacementHigh =
1659 ConstantFoldCastOperand(Instruction::SExt, HighC, X->getType(), DL);
1660 assert(ReplacementLow && ReplacementHigh &&
1661 "Constant folding of ImmConstant cannot fail");
1662 }
1663
1664 // All good, finally emit the new pattern.
1665 Value *ShouldReplaceLow = Builder.CreateICmpSLT(X, ThresholdLowIncl);
1666 Value *ShouldReplaceHigh = Builder.CreateICmpSGE(X, ThresholdHighExcl);
1667 Value *MaybeReplacedLow =
1668 Builder.CreateSelect(ShouldReplaceLow, ReplacementLow, X);
1669
1670 // Create the final select. If we looked through a truncate above, we will
1671 // need to retruncate the result.
1672 Value *MaybeReplacedHigh = Builder.CreateSelect(
1673 ShouldReplaceHigh, ReplacementHigh, MaybeReplacedLow);
1674 return Builder.CreateTrunc(MaybeReplacedHigh, Sel0.getType());
1675}
1676
1677// If we have
1678// %cmp = icmp [canonical predicate] i32 %x, C0
1679// %r = select i1 %cmp, i32 %y, i32 C1
1680// Where C0 != C1 and %x may be different from %y, see if the constant that we
1681// will have if we flip the strictness of the predicate (i.e. without changing
1682// the result) is identical to the C1 in select. If it matches we can change
1683// original comparison to one with swapped predicate, reuse the constant,
1684// and swap the hands of select.
1685static Instruction *
1686tryToReuseConstantFromSelectInComparison(SelectInst &Sel, ICmpInst &Cmp,
1687 InstCombinerImpl &IC) {
1688 CmpPredicate Pred;
1689 Value *X;
1690 Constant *C0;
1691 if (!match(&Cmp, m_OneUse(m_ICmp(
1692 Pred, m_Value(X),
1694 return nullptr;
1695
1696 // If comparison predicate is non-relational, we won't be able to do anything.
1697 if (ICmpInst::isEquality(Pred))
1698 return nullptr;
1699
1700 // If comparison predicate is non-canonical, then we certainly won't be able
1701 // to make it canonical; canonicalizeCmpWithConstant() already tried.
1703 return nullptr;
1704
1705 // If the [input] type of comparison and select type are different, lets abort
1706 // for now. We could try to compare constants with trunc/[zs]ext though.
1707 if (C0->getType() != Sel.getType())
1708 return nullptr;
1709
1710 // ULT with 'add' of a constant is canonical. See foldICmpAddConstant().
1711 // FIXME: Are there more magic icmp predicate+constant pairs we must avoid?
1712 // Or should we just abandon this transform entirely?
1713 if (Pred == CmpInst::ICMP_ULT && match(X, m_Add(m_Value(), m_Constant())))
1714 return nullptr;
1715
1716
1717 Value *SelVal0, *SelVal1; // We do not care which one is from where.
1718 match(&Sel, m_Select(m_Value(), m_Value(SelVal0), m_Value(SelVal1)));
1719 // At least one of these values we are selecting between must be a constant
1720 // else we'll never succeed.
1721 if (!match(SelVal0, m_AnyIntegralConstant()) &&
1722 !match(SelVal1, m_AnyIntegralConstant()))
1723 return nullptr;
1724
1725 // Does this constant C match any of the `select` values?
1726 auto MatchesSelectValue = [SelVal0, SelVal1](Constant *C) {
1727 return C->isElementWiseEqual(SelVal0) || C->isElementWiseEqual(SelVal1);
1728 };
1729
1730 // If C0 *already* matches true/false value of select, we are done.
1731 if (MatchesSelectValue(C0))
1732 return nullptr;
1733
1734 // Check the constant we'd have with flipped-strictness predicate.
1735 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, C0);
1736 if (!FlippedStrictness)
1737 return nullptr;
1738
1739 // If said constant doesn't match either, then there is no hope,
1740 if (!MatchesSelectValue(FlippedStrictness->second))
1741 return nullptr;
1742
1743 // It matched! Lets insert the new comparison just before select.
1745 IC.Builder.SetInsertPoint(&Sel);
1746
1747 Pred = ICmpInst::getSwappedPredicate(Pred); // Yes, swapped.
1748 Value *NewCmp = IC.Builder.CreateICmp(Pred, X, FlippedStrictness->second,
1749 Cmp.getName() + ".inv");
1750 IC.replaceOperand(Sel, 0, NewCmp);
1751 Sel.swapValues();
1752 Sel.swapProfMetadata();
1753
1754 return &Sel;
1755}
1756
1757static Instruction *foldSelectZeroOrOnes(ICmpInst *Cmp, Value *TVal,
1758 Value *FVal,
1759 InstCombiner::BuilderTy &Builder) {
1760 if (!Cmp->hasOneUse())
1761 return nullptr;
1762
1763 const APInt *CmpC;
1764 if (!match(Cmp->getOperand(1), m_APIntAllowPoison(CmpC)))
1765 return nullptr;
1766
1767 // (X u< 2) ? -X : -1 --> sext (X != 0)
1768 Value *X = Cmp->getOperand(0);
1769 if (Cmp->getPredicate() == ICmpInst::ICMP_ULT && *CmpC == 2 &&
1770 match(TVal, m_Neg(m_Specific(X))) && match(FVal, m_AllOnes()))
1771 return new SExtInst(Builder.CreateIsNotNull(X), TVal->getType());
1772
1773 // (X u> 1) ? -1 : -X --> sext (X != 0)
1774 if (Cmp->getPredicate() == ICmpInst::ICMP_UGT && *CmpC == 1 &&
1775 match(FVal, m_Neg(m_Specific(X))) && match(TVal, m_AllOnes()))
1776 return new SExtInst(Builder.CreateIsNotNull(X), TVal->getType());
1777
1778 return nullptr;
1779}
1780
1781static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
1782 InstCombiner::BuilderTy &Builder) {
1783 const APInt *CmpC;
1784 Value *V;
1785 CmpPredicate Pred;
1786 if (!match(ICI, m_ICmp(Pred, m_Value(V), m_APInt(CmpC))))
1787 return nullptr;
1788
1789 // Match clamp away from min/max value as a max/min operation.
1790 Value *TVal = SI.getTrueValue();
1791 Value *FVal = SI.getFalseValue();
1792 if (Pred == ICmpInst::ICMP_EQ && V == FVal) {
1793 // (V == UMIN) ? UMIN+1 : V --> umax(V, UMIN+1)
1794 if (CmpC->isMinValue() && match(TVal, m_SpecificInt(*CmpC + 1)))
1795 return Builder.CreateBinaryIntrinsic(Intrinsic::umax, V, TVal);
1796 // (V == UMAX) ? UMAX-1 : V --> umin(V, UMAX-1)
1797 if (CmpC->isMaxValue() && match(TVal, m_SpecificInt(*CmpC - 1)))
1798 return Builder.CreateBinaryIntrinsic(Intrinsic::umin, V, TVal);
1799 // (V == SMIN) ? SMIN+1 : V --> smax(V, SMIN+1)
1800 if (CmpC->isMinSignedValue() && match(TVal, m_SpecificInt(*CmpC + 1)))
1801 return Builder.CreateBinaryIntrinsic(Intrinsic::smax, V, TVal);
1802 // (V == SMAX) ? SMAX-1 : V --> smin(V, SMAX-1)
1803 if (CmpC->isMaxSignedValue() && match(TVal, m_SpecificInt(*CmpC - 1)))
1804 return Builder.CreateBinaryIntrinsic(Intrinsic::smin, V, TVal);
1805 }
1806
1807 // Fold icmp(X) ? f(X) : C to f(X) when f(X) is guaranteed to be equal to C
1808 // for all X in the exact range of the inverse predicate.
1809 Instruction *Op;
1810 const APInt *C;
1811 CmpInst::Predicate CPred;
1812 if (match(&SI, m_Select(m_Specific(ICI), m_APInt(C), m_Instruction(Op))))
1813 CPred = ICI->getPredicate();
1814 else if (match(&SI, m_Select(m_Specific(ICI), m_Instruction(Op), m_APInt(C))))
1815 CPred = ICI->getInversePredicate();
1816 else
1817 return nullptr;
1818
1819 ConstantRange InvDomCR = ConstantRange::makeExactICmpRegion(CPred, *CmpC);
1820 const APInt *OpC;
1821 if (match(Op, m_BinOp(m_Specific(V), m_APInt(OpC)))) {
1822 ConstantRange R = InvDomCR.binaryOp(
1823 static_cast<Instruction::BinaryOps>(Op->getOpcode()), *OpC);
1824 if (R == *C) {
1825 Op->dropPoisonGeneratingFlags();
1826 return Op;
1827 }
1828 }
1829 if (auto *MMI = dyn_cast<MinMaxIntrinsic>(Op);
1830 MMI && MMI->getLHS() == V && match(MMI->getRHS(), m_APInt(OpC))) {
1831 ConstantRange R = ConstantRange::intrinsic(MMI->getIntrinsicID(),
1832 {InvDomCR, ConstantRange(*OpC)});
1833 if (R == *C) {
1834 MMI->dropPoisonGeneratingAnnotations();
1835 return MMI;
1836 }
1837 }
1838
1839 return nullptr;
1840}
1841
1842/// `A == MIN_INT ? B != MIN_INT : A < B` --> `A < B`
1843/// `A == MAX_INT ? B != MAX_INT : A > B` --> `A > B`
1844static Instruction *foldSelectWithExtremeEqCond(Value *CmpLHS, Value *CmpRHS,
1845 Value *TrueVal,
1846 Value *FalseVal) {
1847 Type *Ty = CmpLHS->getType();
1848
1849 if (Ty->isPtrOrPtrVectorTy())
1850 return nullptr;
1851
1852 CmpPredicate Pred;
1853 Value *B;
1854
1855 if (!match(FalseVal, m_c_ICmp(Pred, m_Specific(CmpLHS), m_Value(B))))
1856 return nullptr;
1857
1858 Value *TValRHS;
1860 m_Value(TValRHS))))
1861 return nullptr;
1862
1863 APInt C;
1864 unsigned BitWidth = Ty->getScalarSizeInBits();
1865
1866 if (ICmpInst::isLT(Pred)) {
1869 } else if (ICmpInst::isGT(Pred)) {
1872 } else {
1873 return nullptr;
1874 }
1875
1876 if (!match(CmpRHS, m_SpecificInt(C)) || !match(TValRHS, m_SpecificInt(C)))
1877 return nullptr;
1878
1879 return new ICmpInst(Pred, CmpLHS, B);
1880}
1881
1882static Instruction *foldSelectICmpEq(SelectInst &SI, ICmpInst *ICI,
1883 InstCombinerImpl &IC) {
1884 ICmpInst::Predicate Pred = ICI->getPredicate();
1885 if (!ICmpInst::isEquality(Pred))
1886 return nullptr;
1887
1888 Value *TrueVal = SI.getTrueValue();
1889 Value *FalseVal = SI.getFalseValue();
1890 Value *CmpLHS = ICI->getOperand(0);
1891 Value *CmpRHS = ICI->getOperand(1);
1892
1893 if (Pred == ICmpInst::ICMP_NE)
1894 std::swap(TrueVal, FalseVal);
1895
1896 if (Instruction *Res =
1897 foldSelectWithExtremeEqCond(CmpLHS, CmpRHS, TrueVal, FalseVal))
1898 return Res;
1899
1900 return nullptr;
1901}
1902
1903/// Fold `X Pred C1 ? X BOp C2 : C1 BOp C2` to `min/max(X, C1) BOp C2`.
1904/// This allows for better canonicalization.
1906 Value *TrueVal,
1907 Value *FalseVal) {
1908 Constant *C1, *C2, *C3;
1909 Value *X;
1911
1912 if (!match(Cmp, m_ICmp(Predicate, m_Value(X), m_Constant(C1))))
1913 return nullptr;
1914
1916 return nullptr;
1917
1918 if (match(TrueVal, m_Constant())) {
1919 std::swap(FalseVal, TrueVal);
1921 }
1922
1923 if (!match(FalseVal, m_Constant(C3)) || !TrueVal->hasOneUse())
1924 return nullptr;
1925
1926 bool IsIntrinsic;
1927 unsigned Opcode;
1928 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(TrueVal)) {
1929 Opcode = BOp->getOpcode();
1930 IsIntrinsic = false;
1931
1932 // This fold causes some regressions and is primarily intended for
1933 // add and sub. So we early exit for div and rem to minimize the
1934 // regressions.
1935 if (Instruction::isIntDivRem(Opcode))
1936 return nullptr;
1937
1938 if (!match(BOp, m_BinOp(m_Specific(X), m_Constant(C2))))
1939 return nullptr;
1940
1941 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(TrueVal)) {
1942 if (!match(II, m_MaxOrMin(m_Specific(X), m_Constant(C2))))
1943 return nullptr;
1944 Opcode = II->getIntrinsicID();
1945 IsIntrinsic = true;
1946 } else {
1947 return nullptr;
1948 }
1949
1950 Value *RHS;
1952 const DataLayout &DL = Cmp->getDataLayout();
1954
1955 auto FoldBinaryOpOrIntrinsic = [&](Constant *LHS, Constant *RHS) {
1956 return IsIntrinsic ? ConstantFoldBinaryIntrinsic(Opcode, LHS, RHS,
1957 LHS->getType(), nullptr)
1959 };
1960
1961 if (C3 == FoldBinaryOpOrIntrinsic(C1, C2)) {
1963 RHS = C1;
1964 } else if (Flipped && C3 == FoldBinaryOpOrIntrinsic(Flipped->second, C2)) {
1965 SPF = getSelectPattern(Flipped->first).Flavor;
1966 RHS = Flipped->second;
1967 } else {
1968 return nullptr;
1969 }
1970
1971 Intrinsic::ID MinMaxID = getMinMaxIntrinsic(SPF);
1972 Value *MinMax = Builder.CreateBinaryIntrinsic(MinMaxID, X, RHS);
1973 if (IsIntrinsic)
1974 return Builder.CreateBinaryIntrinsic(Opcode, MinMax, C2);
1975
1976 const auto BinOpc = Instruction::BinaryOps(Opcode);
1977 Value *BinOp = Builder.CreateBinOp(BinOpc, MinMax, C2);
1978
1979 // If we can attach no-wrap flags to the new instruction, do so if the
1980 // old instruction had them and C1 BinOp C2 does not overflow.
1981 if (Instruction *BinOpInst = dyn_cast<Instruction>(BinOp)) {
1982 if (BinOpc == Instruction::Add || BinOpc == Instruction::Sub ||
1983 BinOpc == Instruction::Mul) {
1984 Instruction *OldBinOp = cast<BinaryOperator>(TrueVal);
1985 if (OldBinOp->hasNoSignedWrap() &&
1986 willNotOverflow(BinOpc, RHS, C2, *BinOpInst, /*IsSigned=*/true))
1987 BinOpInst->setHasNoSignedWrap();
1988 if (OldBinOp->hasNoUnsignedWrap() &&
1989 willNotOverflow(BinOpc, RHS, C2, *BinOpInst, /*IsSigned=*/false))
1990 BinOpInst->setHasNoUnsignedWrap();
1991 }
1992 }
1993 return BinOp;
1994}
1995
1996/// Folds:
1997/// %a_sub = call @llvm.usub.sat(x, IntConst1)
1998/// %b_sub = call @llvm.usub.sat(y, IntConst2)
1999/// %or = or %a_sub, %b_sub
2000/// %cmp = icmp eq %or, 0
2001/// %sel = select %cmp, 0, MostSignificantBit
2002/// into:
2003/// %a_sub' = usub.sat(x, IntConst1 - MostSignificantBit)
2004/// %b_sub' = usub.sat(y, IntConst2 - MostSignificantBit)
2005/// %or = or %a_sub', %b_sub'
2006/// %and = and %or, MostSignificantBit
2007/// Likewise, for vector arguments as well.
2008static Instruction *foldICmpUSubSatWithAndForMostSignificantBitCmp(
2009 SelectInst &SI, ICmpInst *ICI, InstCombiner::BuilderTy &Builder) {
2010 if (!SI.hasOneUse() || !ICI->hasOneUse())
2011 return nullptr;
2012 CmpPredicate Pred;
2013 Value *A, *B;
2014 const APInt *Constant1, *Constant2;
2015 if (!match(SI.getCondition(),
2016 m_ICmp(Pred,
2017 m_OneUse(m_Or(m_OneUse(m_Intrinsic<Intrinsic::usub_sat>(
2018 m_Value(A), m_APInt(Constant1))),
2019 m_OneUse(m_Intrinsic<Intrinsic::usub_sat>(
2020 m_Value(B), m_APInt(Constant2))))),
2021 m_Zero())))
2022 return nullptr;
2023
2024 Value *TrueVal = SI.getTrueValue();
2025 Value *FalseVal = SI.getFalseValue();
2026 if (!(Pred == ICmpInst::ICMP_EQ &&
2027 (match(TrueVal, m_Zero()) && match(FalseVal, m_SignMask()))) ||
2028 (Pred == ICmpInst::ICMP_NE &&
2029 (match(TrueVal, m_SignMask()) && match(FalseVal, m_Zero()))))
2030 return nullptr;
2031
2032 auto *Ty = A->getType();
2033 unsigned BW = Constant1->getBitWidth();
2034 APInt MostSignificantBit = APInt::getSignMask(BW);
2035
2036 // Anything over MSB is negative
2037 if (Constant1->isNonNegative() || Constant2->isNonNegative())
2038 return nullptr;
2039
2040 APInt AdjAP1 = *Constant1 - MostSignificantBit + 1;
2041 APInt AdjAP2 = *Constant2 - MostSignificantBit + 1;
2042
2043 auto *Adj1 = ConstantInt::get(Ty, AdjAP1);
2044 auto *Adj2 = ConstantInt::get(Ty, AdjAP2);
2045
2046 Value *NewA = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A, Adj1);
2047 Value *NewB = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, B, Adj2);
2048 Value *Or = Builder.CreateOr(NewA, NewB);
2049 Constant *MSBConst = ConstantInt::get(Ty, MostSignificantBit);
2050 return BinaryOperator::CreateAnd(Or, MSBConst);
2051}
2052
2053/// Visit a SelectInst that has an ICmpInst as its first operand.
2055 ICmpInst *ICI) {
2056 if (Value *V =
2057 canonicalizeSPF(*ICI, SI.getTrueValue(), SI.getFalseValue(), *this))
2058 return replaceInstUsesWith(SI, V);
2059
2060 if (Value *V = foldSelectInstWithICmpConst(SI, ICI, Builder))
2061 return replaceInstUsesWith(SI, V);
2062
2063 if (Value *V = canonicalizeClampLike(SI, *ICI, Builder, *this))
2064 return replaceInstUsesWith(SI, V);
2065
2066 if (Instruction *NewSel =
2067 tryToReuseConstantFromSelectInComparison(SI, *ICI, *this))
2068 return NewSel;
2069 if (Instruction *Folded =
2070 foldICmpUSubSatWithAndForMostSignificantBitCmp(SI, ICI, Builder))
2071 return Folded;
2072
2073 // NOTE: if we wanted to, this is where to detect integer MIN/MAX
2074 bool Changed = false;
2075 Value *TrueVal = SI.getTrueValue();
2076 Value *FalseVal = SI.getFalseValue();
2077 ICmpInst::Predicate Pred = ICI->getPredicate();
2078 Value *CmpLHS = ICI->getOperand(0);
2079 Value *CmpRHS = ICI->getOperand(1);
2080
2081 if (Instruction *NewSel = foldSelectICmpEq(SI, ICI, *this))
2082 return NewSel;
2083
2084 // Canonicalize a signbit condition to use zero constant by swapping:
2085 // (CmpLHS > -1) ? TV : FV --> (CmpLHS < 0) ? FV : TV
2086 // To avoid conflicts (infinite loops) with other canonicalizations, this is
2087 // not applied with any constant select arm.
2088 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes()) &&
2089 !match(TrueVal, m_Constant()) && !match(FalseVal, m_Constant()) &&
2090 ICI->hasOneUse()) {
2093 Value *IsNeg = Builder.CreateIsNeg(CmpLHS, ICI->getName());
2094 replaceOperand(SI, 0, IsNeg);
2095 SI.swapValues();
2096 SI.swapProfMetadata();
2097 return &SI;
2098 }
2099
2100 if (Value *V = foldSelectICmpMinMax(ICI, TrueVal, FalseVal, Builder, SQ))
2101 return replaceInstUsesWith(SI, V);
2102
2103 if (Instruction *V =
2104 foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder))
2105 return V;
2106
2107 if (Value *V = foldSelectICmpAndZeroShl(ICI, TrueVal, FalseVal, Builder))
2108 return replaceInstUsesWith(SI, V);
2109
2110 if (Instruction *V = foldSelectCtlzToCttz(ICI, TrueVal, FalseVal, Builder))
2111 return V;
2112
2113 if (Instruction *V = foldSelectZeroOrOnes(ICI, TrueVal, FalseVal, Builder))
2114 return V;
2115
2116 if (Value *V = foldSelectICmpLshrAshr(ICI, TrueVal, FalseVal, Builder))
2117 return replaceInstUsesWith(SI, V);
2118
2119 if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, *this))
2120 return replaceInstUsesWith(SI, V);
2121
2122 if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder))
2123 return replaceInstUsesWith(SI, V);
2124
2125 if (Value *V = canonicalizeSaturatedAdd(ICI, TrueVal, FalseVal, Builder))
2126 return replaceInstUsesWith(SI, V);
2127
2128 if (Value *V = foldAbsDiff(ICI, TrueVal, FalseVal, Builder))
2129 return replaceInstUsesWith(SI, V);
2130
2131 if (Value *V = foldSelectWithConstOpToBinOp(ICI, TrueVal, FalseVal))
2132 return replaceInstUsesWith(SI, V);
2133
2134 return Changed ? &SI : nullptr;
2135}
2136
2137/// We have an SPF (e.g. a min or max) of an SPF of the form:
2138/// SPF2(SPF1(A, B), C)
2141 Value *B, Instruction &Outer,
2143 Value *C) {
2144 if (Outer.getType() != Inner->getType())
2145 return nullptr;
2146
2147 if (C == A || C == B) {
2148 // MAX(MAX(A, B), B) -> MAX(A, B)
2149 // MIN(MIN(a, b), a) -> MIN(a, b)
2150 // TODO: This could be done in instsimplify.
2151 if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1))
2152 return replaceInstUsesWith(Outer, Inner);
2153 }
2154
2155 return nullptr;
2156}
2157
2158/// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))).
2159/// This is even legal for FP.
2160static Instruction *foldAddSubSelect(SelectInst &SI,
2161 InstCombiner::BuilderTy &Builder) {
2162 Value *CondVal = SI.getCondition();
2163 Value *TrueVal = SI.getTrueValue();
2164 Value *FalseVal = SI.getFalseValue();
2165 auto *TI = dyn_cast<Instruction>(TrueVal);
2166 auto *FI = dyn_cast<Instruction>(FalseVal);
2167 if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse())
2168 return nullptr;
2169
2170 Instruction *AddOp = nullptr, *SubOp = nullptr;
2171 if ((TI->getOpcode() == Instruction::Sub &&
2172 FI->getOpcode() == Instruction::Add) ||
2173 (TI->getOpcode() == Instruction::FSub &&
2174 FI->getOpcode() == Instruction::FAdd)) {
2175 AddOp = FI;
2176 SubOp = TI;
2177 } else if ((FI->getOpcode() == Instruction::Sub &&
2178 TI->getOpcode() == Instruction::Add) ||
2179 (FI->getOpcode() == Instruction::FSub &&
2180 TI->getOpcode() == Instruction::FAdd)) {
2181 AddOp = TI;
2182 SubOp = FI;
2183 }
2184
2185 if (AddOp) {
2186 Value *OtherAddOp = nullptr;
2187 if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
2188 OtherAddOp = AddOp->getOperand(1);
2189 } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
2190 OtherAddOp = AddOp->getOperand(0);
2191 }
2192
2193 if (OtherAddOp) {
2194 // So at this point we know we have (Y -> OtherAddOp):
2195 // select C, (add X, Y), (sub X, Z)
2196 Value *NegVal; // Compute -Z
2197 if (SI.getType()->isFPOrFPVectorTy()) {
2198 NegVal = Builder.CreateFNeg(SubOp->getOperand(1));
2199 if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
2201 Flags &= SubOp->getFastMathFlags();
2202 NegInst->setFastMathFlags(Flags);
2203 }
2204 } else {
2205 NegVal = Builder.CreateNeg(SubOp->getOperand(1));
2206 }
2207
2208 Value *NewTrueOp = OtherAddOp;
2209 Value *NewFalseOp = NegVal;
2210 if (AddOp != TI)
2211 std::swap(NewTrueOp, NewFalseOp);
2212 Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp,
2213 SI.getName() + ".p", &SI);
2214
2215 if (SI.getType()->isFPOrFPVectorTy()) {
2216 Instruction *RI =
2217 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
2218
2220 Flags &= SubOp->getFastMathFlags();
2221 RI->setFastMathFlags(Flags);
2222 return RI;
2223 } else
2224 return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
2225 }
2226 }
2227 return nullptr;
2228}
2229
2230/// Turn X + Y overflows ? -1 : X + Y -> uadd_sat X, Y
2231/// And X - Y overflows ? 0 : X - Y -> usub_sat X, Y
2232/// Along with a number of patterns similar to:
2233/// X + Y overflows ? (X < 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2234/// X - Y overflows ? (X > 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2235static Instruction *
2236foldOverflowingAddSubSelect(SelectInst &SI, InstCombiner::BuilderTy &Builder) {
2237 Value *CondVal = SI.getCondition();
2238 Value *TrueVal = SI.getTrueValue();
2239 Value *FalseVal = SI.getFalseValue();
2240
2242 if (!match(CondVal, m_ExtractValue<1>(m_WithOverflowInst(II))) ||
2243 !match(FalseVal, m_ExtractValue<0>(m_Specific(II))))
2244 return nullptr;
2245
2246 Value *X = II->getLHS();
2247 Value *Y = II->getRHS();
2248
2249 auto IsSignedSaturateLimit = [&](Value *Limit, bool IsAdd) {
2250 Type *Ty = Limit->getType();
2251
2252 CmpPredicate Pred;
2253 Value *TrueVal, *FalseVal, *Op;
2254 const APInt *C;
2255 if (!match(Limit, m_Select(m_ICmp(Pred, m_Value(Op), m_APInt(C)),
2256 m_Value(TrueVal), m_Value(FalseVal))))
2257 return false;
2258
2259 auto IsZeroOrOne = [](const APInt &C) { return C.isZero() || C.isOne(); };
2260 auto IsMinMax = [&](Value *Min, Value *Max) {
2263 return match(Min, m_SpecificInt(MinVal)) &&
2264 match(Max, m_SpecificInt(MaxVal));
2265 };
2266
2267 if (Op != X && Op != Y)
2268 return false;
2269
2270 if (IsAdd) {
2271 // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2272 // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2273 // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2274 // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2275 if (Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) &&
2276 IsMinMax(TrueVal, FalseVal))
2277 return true;
2278 // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2279 // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2280 // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2281 // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2282 if (Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) &&
2283 IsMinMax(FalseVal, TrueVal))
2284 return true;
2285 } else {
2286 // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2287 // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2288 if (Op == X && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C + 1) &&
2289 IsMinMax(TrueVal, FalseVal))
2290 return true;
2291 // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2292 // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2293 if (Op == X && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 2) &&
2294 IsMinMax(FalseVal, TrueVal))
2295 return true;
2296 // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2297 // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2298 if (Op == Y && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) &&
2299 IsMinMax(FalseVal, TrueVal))
2300 return true;
2301 // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2302 // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2303 if (Op == Y && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) &&
2304 IsMinMax(TrueVal, FalseVal))
2305 return true;
2306 }
2307
2308 return false;
2309 };
2310
2311 Intrinsic::ID NewIntrinsicID;
2312 if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow &&
2313 match(TrueVal, m_AllOnes()))
2314 // X + Y overflows ? -1 : X + Y -> uadd_sat X, Y
2315 NewIntrinsicID = Intrinsic::uadd_sat;
2316 else if (II->getIntrinsicID() == Intrinsic::usub_with_overflow &&
2317 match(TrueVal, m_Zero()))
2318 // X - Y overflows ? 0 : X - Y -> usub_sat X, Y
2319 NewIntrinsicID = Intrinsic::usub_sat;
2320 else if (II->getIntrinsicID() == Intrinsic::sadd_with_overflow &&
2321 IsSignedSaturateLimit(TrueVal, /*IsAdd=*/true))
2322 // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2323 // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2324 // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2325 // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2326 // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2327 // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2328 // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2329 // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2330 NewIntrinsicID = Intrinsic::sadd_sat;
2331 else if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow &&
2332 IsSignedSaturateLimit(TrueVal, /*IsAdd=*/false))
2333 // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2334 // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2335 // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2336 // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2337 // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2338 // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2339 // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2340 // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2341 NewIntrinsicID = Intrinsic::ssub_sat;
2342 else
2343 return nullptr;
2344
2346 NewIntrinsicID, SI.getType());
2347 return CallInst::Create(F, {X, Y});
2348}
2349
2351 Constant *C;
2352 if (!match(Sel.getTrueValue(), m_Constant(C)) &&
2353 !match(Sel.getFalseValue(), m_Constant(C)))
2354 return nullptr;
2355
2356 Instruction *ExtInst;
2357 if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) &&
2358 !match(Sel.getFalseValue(), m_Instruction(ExtInst)))
2359 return nullptr;
2360
2361 auto ExtOpcode = ExtInst->getOpcode();
2362 if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt)
2363 return nullptr;
2364
2365 // If we are extending from a boolean type or if we can create a select that
2366 // has the same size operands as its condition, try to narrow the select.
2367 Value *X = ExtInst->getOperand(0);
2368 Type *SmallType = X->getType();
2369 Value *Cond = Sel.getCondition();
2370 auto *Cmp = dyn_cast<CmpInst>(Cond);
2371 if (!SmallType->isIntOrIntVectorTy(1) &&
2372 (!Cmp || Cmp->getOperand(0)->getType() != SmallType))
2373 return nullptr;
2374
2375 // If the constant is the same after truncation to the smaller type and
2376 // extension to the original type, we can narrow the select.
2377 Type *SelType = Sel.getType();
2378 Constant *TruncC = getLosslessTrunc(C, SmallType, ExtOpcode);
2379 if (TruncC && ExtInst->hasOneUse()) {
2380 Value *TruncCVal = cast<Value>(TruncC);
2381 if (ExtInst == Sel.getFalseValue())
2382 std::swap(X, TruncCVal);
2383
2384 // select Cond, (ext X), C --> ext(select Cond, X, C')
2385 // select Cond, C, (ext X) --> ext(select Cond, C', X)
2386 Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel);
2387 return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType);
2388 }
2389
2390 return nullptr;
2391}
2392
2393/// Try to transform a vector select with a constant condition vector into a
2394/// shuffle for easier combining with other shuffles and insert/extract.
2395static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) {
2396 Value *CondVal = SI.getCondition();
2397 Constant *CondC;
2398 auto *CondValTy = dyn_cast<FixedVectorType>(CondVal->getType());
2399 if (!CondValTy || !match(CondVal, m_Constant(CondC)))
2400 return nullptr;
2401
2402 unsigned NumElts = CondValTy->getNumElements();
2404 Mask.reserve(NumElts);
2405 for (unsigned i = 0; i != NumElts; ++i) {
2406 Constant *Elt = CondC->getAggregateElement(i);
2407 if (!Elt)
2408 return nullptr;
2409
2410 if (Elt->isOneValue()) {
2411 // If the select condition element is true, choose from the 1st vector.
2412 Mask.push_back(i);
2413 } else if (Elt->isNullValue()) {
2414 // If the select condition element is false, choose from the 2nd vector.
2415 Mask.push_back(i + NumElts);
2416 } else if (isa<UndefValue>(Elt)) {
2417 // Undef in a select condition (choose one of the operands) does not mean
2418 // the same thing as undef in a shuffle mask (any value is acceptable), so
2419 // give up.
2420 return nullptr;
2421 } else {
2422 // Bail out on a constant expression.
2423 return nullptr;
2424 }
2425 }
2426
2427 return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(), Mask);
2428}
2429
2430/// If we have a select of vectors with a scalar condition, try to convert that
2431/// to a vector select by splatting the condition. A splat may get folded with
2432/// other operations in IR and having all operands of a select be vector types
2433/// is likely better for vector codegen.
2434static Instruction *canonicalizeScalarSelectOfVecs(SelectInst &Sel,
2435 InstCombinerImpl &IC) {
2436 auto *Ty = dyn_cast<VectorType>(Sel.getType());
2437 if (!Ty)
2438 return nullptr;
2439
2440 // We can replace a single-use extract with constant index.
2441 Value *Cond = Sel.getCondition();
2443 return nullptr;
2444
2445 // select (extelt V, Index), T, F --> select (splat V, Index), T, F
2446 // Splatting the extracted condition reduces code (we could directly create a
2447 // splat shuffle of the source vector to eliminate the intermediate step).
2448 return IC.replaceOperand(
2449 Sel, 0, IC.Builder.CreateVectorSplat(Ty->getElementCount(), Cond));
2450}
2451
2452/// Reuse bitcasted operands between a compare and select:
2453/// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
2454/// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D))
2455static Instruction *foldSelectCmpBitcasts(SelectInst &Sel,
2456 InstCombiner::BuilderTy &Builder) {
2457 Value *Cond = Sel.getCondition();
2458 Value *TVal = Sel.getTrueValue();
2459 Value *FVal = Sel.getFalseValue();
2460
2461 CmpPredicate Pred;
2462 Value *A, *B;
2463 if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B))))
2464 return nullptr;
2465
2466 // The select condition is a compare instruction. If the select's true/false
2467 // values are already the same as the compare operands, there's nothing to do.
2468 if (TVal == A || TVal == B || FVal == A || FVal == B)
2469 return nullptr;
2470
2471 Value *C, *D;
2472 if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D))))
2473 return nullptr;
2474
2475 // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc)
2476 Value *TSrc, *FSrc;
2477 if (!match(TVal, m_BitCast(m_Value(TSrc))) ||
2478 !match(FVal, m_BitCast(m_Value(FSrc))))
2479 return nullptr;
2480
2481 // If the select true/false values are *different bitcasts* of the same source
2482 // operands, make the select operands the same as the compare operands and
2483 // cast the result. This is the canonical select form for min/max.
2484 Value *NewSel;
2485 if (TSrc == C && FSrc == D) {
2486 // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
2487 // bitcast (select (cmp A, B), A, B)
2488 NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel);
2489 } else if (TSrc == D && FSrc == C) {
2490 // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) -->
2491 // bitcast (select (cmp A, B), B, A)
2492 NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel);
2493 } else {
2494 return nullptr;
2495 }
2496 return new BitCastInst(NewSel, Sel.getType());
2497}
2498
2499/// Try to eliminate select instructions that test the returned flag of cmpxchg
2500/// instructions.
2501///
2502/// If a select instruction tests the returned flag of a cmpxchg instruction and
2503/// selects between the returned value of the cmpxchg instruction its compare
2504/// operand, the result of the select will always be equal to its false value.
2505/// For example:
2506///
2507/// %cmpxchg = cmpxchg ptr %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
2508/// %val = extractvalue { i64, i1 } %cmpxchg, 0
2509/// %success = extractvalue { i64, i1 } %cmpxchg, 1
2510/// %sel = select i1 %success, i64 %compare, i64 %val
2511/// ret i64 %sel
2512///
2513/// The returned value of the cmpxchg instruction (%val) is the original value
2514/// located at %ptr prior to any update. If the cmpxchg operation succeeds, %val
2515/// must have been equal to %compare. Thus, the result of the select is always
2516/// equal to %val, and the code can be simplified to:
2517///
2518/// %cmpxchg = cmpxchg ptr %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
2519/// %val = extractvalue { i64, i1 } %cmpxchg, 0
2520/// ret i64 %val
2521///
2522static Value *foldSelectCmpXchg(SelectInst &SI) {
2523 // A helper that determines if V is an extractvalue instruction whose
2524 // aggregate operand is a cmpxchg instruction and whose single index is equal
2525 // to I. If such conditions are true, the helper returns the cmpxchg
2526 // instruction; otherwise, a nullptr is returned.
2527 auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * {
2528 auto *Extract = dyn_cast<ExtractValueInst>(V);
2529 if (!Extract)
2530 return nullptr;
2531 if (Extract->getIndices()[0] != I)
2532 return nullptr;
2533 return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand());
2534 };
2535
2536 // If the select has a single user, and this user is a select instruction that
2537 // we can simplify, skip the cmpxchg simplification for now.
2538 if (SI.hasOneUse())
2539 if (auto *Select = dyn_cast<SelectInst>(SI.user_back()))
2540 if (Select->getCondition() == SI.getCondition())
2541 if (Select->getFalseValue() == SI.getTrueValue() ||
2542 Select->getTrueValue() == SI.getFalseValue())
2543 return nullptr;
2544
2545 // Ensure the select condition is the returned flag of a cmpxchg instruction.
2546 auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1);
2547 if (!CmpXchg)
2548 return nullptr;
2549
2550 // Check the true value case: The true value of the select is the returned
2551 // value of the same cmpxchg used by the condition, and the false value is the
2552 // cmpxchg instruction's compare operand.
2553 if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0))
2554 if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue())
2555 return SI.getFalseValue();
2556
2557 // Check the false value case: The false value of the select is the returned
2558 // value of the same cmpxchg used by the condition, and the true value is the
2559 // cmpxchg instruction's compare operand.
2560 if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0))
2561 if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue())
2562 return SI.getFalseValue();
2563
2564 return nullptr;
2565}
2566
2567/// Try to reduce a funnel/rotate pattern that includes a compare and select
2568/// into a funnel shift intrinsic. Example:
2569/// rotl32(a, b) --> (b == 0 ? a : ((a >> (32 - b)) | (a << b)))
2570/// --> call llvm.fshl.i32(a, a, b)
2571/// fshl32(a, b, c) --> (c == 0 ? a : ((b >> (32 - c)) | (a << c)))
2572/// --> call llvm.fshl.i32(a, b, c)
2573/// fshr32(a, b, c) --> (c == 0 ? b : ((a >> (32 - c)) | (b << c)))
2574/// --> call llvm.fshr.i32(a, b, c)
2575static Instruction *foldSelectFunnelShift(SelectInst &Sel,
2576 InstCombiner::BuilderTy &Builder) {
2577 // This must be a power-of-2 type for a bitmasking transform to be valid.
2578 unsigned Width = Sel.getType()->getScalarSizeInBits();
2579 if (!isPowerOf2_32(Width))
2580 return nullptr;
2581
2582 BinaryOperator *Or0, *Or1;
2583 if (!match(Sel.getFalseValue(), m_OneUse(m_Or(m_BinOp(Or0), m_BinOp(Or1)))))
2584 return nullptr;
2585
2586 Value *SV0, *SV1, *SA0, *SA1;
2587 if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(SV0),
2588 m_ZExtOrSelf(m_Value(SA0))))) ||
2590 m_ZExtOrSelf(m_Value(SA1))))) ||
2591 Or0->getOpcode() == Or1->getOpcode())
2592 return nullptr;
2593
2594 // Canonicalize to or(shl(SV0, SA0), lshr(SV1, SA1)).
2595 if (Or0->getOpcode() == BinaryOperator::LShr) {
2596 std::swap(Or0, Or1);
2597 std::swap(SV0, SV1);
2598 std::swap(SA0, SA1);
2599 }
2600 assert(Or0->getOpcode() == BinaryOperator::Shl &&
2601 Or1->getOpcode() == BinaryOperator::LShr &&
2602 "Illegal or(shift,shift) pair");
2603
2604 // Check the shift amounts to see if they are an opposite pair.
2605 Value *ShAmt;
2606 if (match(SA1, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA0)))))
2607 ShAmt = SA0;
2608 else if (match(SA0, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA1)))))
2609 ShAmt = SA1;
2610 else
2611 return nullptr;
2612
2613 // We should now have this pattern:
2614 // select ?, TVal, (or (shl SV0, SA0), (lshr SV1, SA1))
2615 // The false value of the select must be a funnel-shift of the true value:
2616 // IsFShl -> TVal must be SV0 else TVal must be SV1.
2617 bool IsFshl = (ShAmt == SA0);
2618 Value *TVal = Sel.getTrueValue();
2619 if ((IsFshl && TVal != SV0) || (!IsFshl && TVal != SV1))
2620 return nullptr;
2621
2622 // Finally, see if the select is filtering out a shift-by-zero.
2623 Value *Cond = Sel.getCondition();
2625 m_ZeroInt()))))
2626 return nullptr;
2627
2628 // If this is not a rotate then the select was blocking poison from the
2629 // 'shift-by-zero' non-TVal, but a funnel shift won't - so freeze it.
2630 if (SV0 != SV1) {
2631 if (IsFshl && !llvm::isGuaranteedNotToBePoison(SV1))
2632 SV1 = Builder.CreateFreeze(SV1);
2633 else if (!IsFshl && !llvm::isGuaranteedNotToBePoison(SV0))
2634 SV0 = Builder.CreateFreeze(SV0);
2635 }
2636
2637 // This is a funnel/rotate that avoids shift-by-bitwidth UB in a suboptimal way.
2638 // Convert to funnel shift intrinsic.
2639 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
2640 Function *F =
2642 ShAmt = Builder.CreateZExt(ShAmt, Sel.getType());
2643 return CallInst::Create(F, { SV0, SV1, ShAmt });
2644}
2645
2646static Instruction *foldSelectToCopysign(SelectInst &Sel,
2647 InstCombiner::BuilderTy &Builder) {
2648 Value *Cond = Sel.getCondition();
2649 Value *TVal = Sel.getTrueValue();
2650 Value *FVal = Sel.getFalseValue();
2651 Type *SelType = Sel.getType();
2652
2653 // Match select ?, TC, FC where the constants are equal but negated.
2654 // TODO: Generalize to handle a negated variable operand?
2655 const APFloat *TC, *FC;
2656 if (!match(TVal, m_APFloatAllowPoison(TC)) ||
2657 !match(FVal, m_APFloatAllowPoison(FC)) ||
2658 !abs(*TC).bitwiseIsEqual(abs(*FC)))
2659 return nullptr;
2660
2661 assert(TC != FC && "Expected equal select arms to simplify");
2662
2663 Value *X;
2664 const APInt *C;
2665 bool IsTrueIfSignSet;
2666 CmpPredicate Pred;
2668 m_APInt(C)))) ||
2669 !isSignBitCheck(Pred, *C, IsTrueIfSignSet) || X->getType() != SelType)
2670 return nullptr;
2671
2672 // If needed, negate the value that will be the sign argument of the copysign:
2673 // (bitcast X) < 0 ? -TC : TC --> copysign(TC, X)
2674 // (bitcast X) < 0 ? TC : -TC --> copysign(TC, -X)
2675 // (bitcast X) >= 0 ? -TC : TC --> copysign(TC, -X)
2676 // (bitcast X) >= 0 ? TC : -TC --> copysign(TC, X)
2677 // Note: FMF from the select can not be propagated to the new instructions.
2678 if (IsTrueIfSignSet ^ TC->isNegative())
2679 X = Builder.CreateFNeg(X);
2680
2681 // Canonicalize the magnitude argument as the positive constant since we do
2682 // not care about its sign.
2683 Value *MagArg = ConstantFP::get(SelType, abs(*TC));
2685 Sel.getModule(), Intrinsic::copysign, Sel.getType());
2686 return CallInst::Create(F, { MagArg, X });
2687}
2688
2690 if (!isa<VectorType>(Sel.getType()))
2691 return nullptr;
2692
2693 Value *Cond = Sel.getCondition();
2694 Value *TVal = Sel.getTrueValue();
2695 Value *FVal = Sel.getFalseValue();
2696 Value *C, *X, *Y;
2697
2698 if (match(Cond, m_VecReverse(m_Value(C)))) {
2699 auto createSelReverse = [&](Value *C, Value *X, Value *Y) {
2700 Value *V = Builder.CreateSelect(C, X, Y, Sel.getName(), &Sel);
2701 if (auto *I = dyn_cast<Instruction>(V))
2702 I->copyIRFlags(&Sel);
2703 Module *M = Sel.getModule();
2705 M, Intrinsic::vector_reverse, V->getType());
2706 return CallInst::Create(F, V);
2707 };
2708
2709 if (match(TVal, m_VecReverse(m_Value(X)))) {
2710 // select rev(C), rev(X), rev(Y) --> rev(select C, X, Y)
2711 if (match(FVal, m_VecReverse(m_Value(Y))) &&
2712 (Cond->hasOneUse() || TVal->hasOneUse() || FVal->hasOneUse()))
2713 return createSelReverse(C, X, Y);
2714
2715 // select rev(C), rev(X), FValSplat --> rev(select C, X, FValSplat)
2716 if ((Cond->hasOneUse() || TVal->hasOneUse()) && isSplatValue(FVal))
2717 return createSelReverse(C, X, FVal);
2718 }
2719 // select rev(C), TValSplat, rev(Y) --> rev(select C, TValSplat, Y)
2720 else if (isSplatValue(TVal) && match(FVal, m_VecReverse(m_Value(Y))) &&
2721 (Cond->hasOneUse() || FVal->hasOneUse()))
2722 return createSelReverse(C, TVal, Y);
2723 }
2724
2725 auto *VecTy = dyn_cast<FixedVectorType>(Sel.getType());
2726 if (!VecTy)
2727 return nullptr;
2728
2729 unsigned NumElts = VecTy->getNumElements();
2730 APInt PoisonElts(NumElts, 0);
2731 APInt AllOnesEltMask(APInt::getAllOnes(NumElts));
2732 if (Value *V = SimplifyDemandedVectorElts(&Sel, AllOnesEltMask, PoisonElts)) {
2733 if (V != &Sel)
2734 return replaceInstUsesWith(Sel, V);
2735 return &Sel;
2736 }
2737
2738 // A select of a "select shuffle" with a common operand can be rearranged
2739 // to select followed by "select shuffle". Because of poison, this only works
2740 // in the case of a shuffle with no undefined mask elements.
2742 if (match(TVal, m_OneUse(m_Shuffle(m_Value(X), m_Value(Y), m_Mask(Mask)))) &&
2743 !is_contained(Mask, PoisonMaskElem) &&
2744 cast<ShuffleVectorInst>(TVal)->isSelect()) {
2745 if (X == FVal) {
2746 // select Cond, (shuf_sel X, Y), X --> shuf_sel X, (select Cond, Y, X)
2747 Value *NewSel = Builder.CreateSelect(Cond, Y, X, "sel", &Sel);
2748 return new ShuffleVectorInst(X, NewSel, Mask);
2749 }
2750 if (Y == FVal) {
2751 // select Cond, (shuf_sel X, Y), Y --> shuf_sel (select Cond, X, Y), Y
2752 Value *NewSel = Builder.CreateSelect(Cond, X, Y, "sel", &Sel);
2753 return new ShuffleVectorInst(NewSel, Y, Mask);
2754 }
2755 }
2756 if (match(FVal, m_OneUse(m_Shuffle(m_Value(X), m_Value(Y), m_Mask(Mask)))) &&
2757 !is_contained(Mask, PoisonMaskElem) &&
2758 cast<ShuffleVectorInst>(FVal)->isSelect()) {
2759 if (X == TVal) {
2760 // select Cond, X, (shuf_sel X, Y) --> shuf_sel X, (select Cond, X, Y)
2761 Value *NewSel = Builder.CreateSelect(Cond, X, Y, "sel", &Sel);
2762 return new ShuffleVectorInst(X, NewSel, Mask);
2763 }
2764 if (Y == TVal) {
2765 // select Cond, Y, (shuf_sel X, Y) --> shuf_sel (select Cond, Y, X), Y
2766 Value *NewSel = Builder.CreateSelect(Cond, Y, X, "sel", &Sel);
2767 return new ShuffleVectorInst(NewSel, Y, Mask);
2768 }
2769 }
2770
2771 return nullptr;
2772}
2773
2774static Instruction *foldSelectToPhiImpl(SelectInst &Sel, BasicBlock *BB,
2775 const DominatorTree &DT,
2776 InstCombiner::BuilderTy &Builder) {
2777 // Find the block's immediate dominator that ends with a conditional branch
2778 // that matches select's condition (maybe inverted).
2779 auto *IDomNode = DT[BB]->getIDom();
2780 if (!IDomNode)
2781 return nullptr;
2782 BasicBlock *IDom = IDomNode->getBlock();
2783
2784 Value *Cond = Sel.getCondition();
2785 Value *IfTrue, *IfFalse;
2786 BasicBlock *TrueSucc, *FalseSucc;
2787 if (match(IDom->getTerminator(),
2788 m_Br(m_Specific(Cond), m_BasicBlock(TrueSucc),
2789 m_BasicBlock(FalseSucc)))) {
2790 IfTrue = Sel.getTrueValue();
2791 IfFalse = Sel.getFalseValue();
2792 } else if (match(IDom->getTerminator(),
2793 m_Br(m_Not(m_Specific(Cond)), m_BasicBlock(TrueSucc),
2794 m_BasicBlock(FalseSucc)))) {
2795 IfTrue = Sel.getFalseValue();
2796 IfFalse = Sel.getTrueValue();
2797 } else
2798 return nullptr;
2799
2800 // Make sure the branches are actually different.
2801 if (TrueSucc == FalseSucc)
2802 return nullptr;
2803
2804 // We want to replace select %cond, %a, %b with a phi that takes value %a
2805 // for all incoming edges that are dominated by condition `%cond == true`,
2806 // and value %b for edges dominated by condition `%cond == false`. If %a
2807 // or %b are also phis from the same basic block, we can go further and take
2808 // their incoming values from the corresponding blocks.
2809 BasicBlockEdge TrueEdge(IDom, TrueSucc);
2810 BasicBlockEdge FalseEdge(IDom, FalseSucc);
2812 for (auto *Pred : predecessors(BB)) {
2813 // Check implication.
2814 BasicBlockEdge Incoming(Pred, BB);
2815 if (DT.dominates(TrueEdge, Incoming))
2816 Inputs[Pred] = IfTrue->DoPHITranslation(BB, Pred);
2817 else if (DT.dominates(FalseEdge, Incoming))
2818 Inputs[Pred] = IfFalse->DoPHITranslation(BB, Pred);
2819 else
2820 return nullptr;
2821 // Check availability.
2822 if (auto *Insn = dyn_cast<Instruction>(Inputs[Pred]))
2823 if (!DT.dominates(Insn, Pred->getTerminator()))
2824 return nullptr;
2825 }
2826
2827 Builder.SetInsertPoint(BB, BB->begin());
2828 auto *PN = Builder.CreatePHI(Sel.getType(), Inputs.size());
2829 for (auto *Pred : predecessors(BB))
2830 PN->addIncoming(Inputs[Pred], Pred);
2831 PN->takeName(&Sel);
2832 return PN;
2833}
2834
2835static Instruction *foldSelectToPhi(SelectInst &Sel, const DominatorTree &DT,
2836 InstCombiner::BuilderTy &Builder) {
2837 // Try to replace this select with Phi in one of these blocks.
2838 SmallSetVector<BasicBlock *, 4> CandidateBlocks;
2839 CandidateBlocks.insert(Sel.getParent());
2840 for (Value *V : Sel.operands())
2841 if (auto *I = dyn_cast<Instruction>(V))
2842 CandidateBlocks.insert(I->getParent());
2843
2844 for (BasicBlock *BB : CandidateBlocks)
2845 if (auto *PN = foldSelectToPhiImpl(Sel, BB, DT, Builder))
2846 return PN;
2847 return nullptr;
2848}
2849
2850/// Tries to reduce a pattern that arises when calculating the remainder of the
2851/// Euclidean division. When the divisor is a power of two and is guaranteed not
2852/// to be negative, a signed remainder can be folded with a bitwise and.
2853///
2854/// (x % n) < 0 ? (x % n) + n : (x % n)
2855/// -> x & (n - 1)
2856static Instruction *foldSelectWithSRem(SelectInst &SI, InstCombinerImpl &IC,
2857 IRBuilderBase &Builder) {
2858 Value *CondVal = SI.getCondition();
2859 Value *TrueVal = SI.getTrueValue();
2860 Value *FalseVal = SI.getFalseValue();
2861
2862 CmpPredicate Pred;
2863 Value *Op, *RemRes, *Remainder;
2864 const APInt *C;
2865 bool TrueIfSigned = false;
2866
2867 if (!(match(CondVal, m_ICmp(Pred, m_Value(RemRes), m_APInt(C))) &&
2868 isSignBitCheck(Pred, *C, TrueIfSigned)))
2869 return nullptr;
2870
2871 // If the sign bit is not set, we have a SGE/SGT comparison, and the operands
2872 // of the select are inverted.
2873 if (!TrueIfSigned)
2874 std::swap(TrueVal, FalseVal);
2875
2876 auto FoldToBitwiseAnd = [&](Value *Remainder) -> Instruction * {
2877 Value *Add = Builder.CreateAdd(
2878 Remainder, Constant::getAllOnesValue(RemRes->getType()));
2879 return BinaryOperator::CreateAnd(Op, Add);
2880 };
2881
2882 // Match the general case:
2883 // %rem = srem i32 %x, %n
2884 // %cnd = icmp slt i32 %rem, 0
2885 // %add = add i32 %rem, %n
2886 // %sel = select i1 %cnd, i32 %add, i32 %rem
2887 if (match(TrueVal, m_c_Add(m_Specific(RemRes), m_Value(Remainder))) &&
2888 match(RemRes, m_SRem(m_Value(Op), m_Specific(Remainder))) &&
2889 IC.isKnownToBeAPowerOfTwo(Remainder, /*OrZero=*/true) &&
2890 FalseVal == RemRes)
2891 return FoldToBitwiseAnd(Remainder);
2892
2893 // Match the case where the one arm has been replaced by constant 1:
2894 // %rem = srem i32 %n, 2
2895 // %cnd = icmp slt i32 %rem, 0
2896 // %sel = select i1 %cnd, i32 1, i32 %rem
2897 if (match(TrueVal, m_One()) &&
2898 match(RemRes, m_SRem(m_Value(Op), m_SpecificInt(2))) &&
2899 FalseVal == RemRes)
2900 return FoldToBitwiseAnd(ConstantInt::get(RemRes->getType(), 2));
2901
2902 return nullptr;
2903}
2904
2905static Value *foldSelectWithFrozenICmp(SelectInst &Sel, InstCombiner::BuilderTy &Builder) {
2906 FreezeInst *FI = dyn_cast<FreezeInst>(Sel.getCondition());
2907 if (!FI)
2908 return nullptr;
2909
2910 Value *Cond = FI->getOperand(0);
2911 Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue();
2912
2913 // select (freeze(x == y)), x, y --> y
2914 // select (freeze(x != y)), x, y --> x
2915 // The freeze should be only used by this select. Otherwise, remaining uses of
2916 // the freeze can observe a contradictory value.
2917 // c = freeze(x == y) ; Let's assume that y = poison & x = 42; c is 0 or 1
2918 // a = select c, x, y ;
2919 // f(a, c) ; f(poison, 1) cannot happen, but if a is folded
2920 // ; to y, this can happen.
2921 CmpPredicate Pred;
2922 if (FI->hasOneUse() &&
2923 match(Cond, m_c_ICmp(Pred, m_Specific(TrueVal), m_Specific(FalseVal))) &&
2924 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)) {
2925 return Pred == ICmpInst::ICMP_EQ ? FalseVal : TrueVal;
2926 }
2927
2928 return nullptr;
2929}
2930
2931/// Given that \p CondVal is known to be \p CondIsTrue, try to simplify \p SI.
2932static Value *simplifyNestedSelectsUsingImpliedCond(SelectInst &SI,
2933 Value *CondVal,
2934 bool CondIsTrue,
2935 const DataLayout &DL) {
2936 Value *InnerCondVal = SI.getCondition();
2937 Value *InnerTrueVal = SI.getTrueValue();
2938 Value *InnerFalseVal = SI.getFalseValue();
2939 assert(CondVal->getType() == InnerCondVal->getType() &&
2940 "The type of inner condition must match with the outer.");
2941 if (auto Implied = isImpliedCondition(CondVal, InnerCondVal, DL, CondIsTrue))
2942 return *Implied ? InnerTrueVal : InnerFalseVal;
2943 return nullptr;
2944}
2945
2946Instruction *InstCombinerImpl::foldAndOrOfSelectUsingImpliedCond(Value *Op,
2947 SelectInst &SI,
2948 bool IsAnd) {
2949 assert(Op->getType()->isIntOrIntVectorTy(1) &&
2950 "Op must be either i1 or vector of i1.");
2951 if (SI.getCondition()->getType() != Op->getType())
2952 return nullptr;
2953 if (Value *V = simplifyNestedSelectsUsingImpliedCond(SI, Op, IsAnd, DL))
2954 return SelectInst::Create(Op,
2955 IsAnd ? V : ConstantInt::getTrue(Op->getType()),
2956 IsAnd ? ConstantInt::getFalse(Op->getType()) : V);
2957 return nullptr;
2958}
2959
2960// Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need
2961// fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work.
2962static Instruction *foldSelectWithFCmpToFabs(SelectInst &SI,
2963 InstCombinerImpl &IC) {
2964 Value *CondVal = SI.getCondition();
2965
2966 bool ChangedFMF = false;
2967 for (bool Swap : {false, true}) {
2968 Value *TrueVal = SI.getTrueValue();
2969 Value *X = SI.getFalseValue();
2970 CmpPredicate Pred;
2971
2972 if (Swap)
2973 std::swap(TrueVal, X);
2974
2975 if (!match(CondVal, m_FCmp(Pred, m_Specific(X), m_AnyZeroFP())))
2976 continue;
2977
2978 // fold (X <= +/-0.0) ? (0.0 - X) : X to fabs(X), when 'Swap' is false
2979 // fold (X > +/-0.0) ? X : (0.0 - X) to fabs(X), when 'Swap' is true
2980 // Note: We require "nnan" for this fold because fcmp ignores the signbit
2981 // of NAN, but IEEE-754 specifies the signbit of NAN values with
2982 // fneg/fabs operations.
2983 if (match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X))) &&
2984 (cast<FPMathOperator>(CondVal)->hasNoNaNs() || SI.hasNoNaNs() ||
2985 (SI.hasOneUse() && canIgnoreSignBitOfNaN(*SI.use_begin())) ||
2987 cast<Instruction>(CondVal))))) {
2988 if (!Swap && (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
2989 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
2990 return IC.replaceInstUsesWith(SI, Fabs);
2991 }
2992 if (Swap && (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) {
2993 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
2994 return IC.replaceInstUsesWith(SI, Fabs);
2995 }
2996 }
2997
2998 if (!match(TrueVal, m_FNeg(m_Specific(X))))
2999 return nullptr;
3000
3001 // Forward-propagate nnan and ninf from the fcmp to the select.
3002 // If all inputs are not those values, then the select is not either.
3003 // Note: nsz is defined differently, so it may not be correct to propagate.
3004 FastMathFlags FMF = cast<FPMathOperator>(CondVal)->getFastMathFlags();
3005 if (FMF.noNaNs() && !SI.hasNoNaNs()) {
3006 SI.setHasNoNaNs(true);
3007 ChangedFMF = true;
3008 }
3009 if (FMF.noInfs() && !SI.hasNoInfs()) {
3010 SI.setHasNoInfs(true);
3011 ChangedFMF = true;
3012 }
3013 // Forward-propagate nnan from the fneg to the select.
3014 // The nnan flag can be propagated iff fneg is selected when X is NaN.
3015 if (!SI.hasNoNaNs() && cast<FPMathOperator>(TrueVal)->hasNoNaNs() &&
3016 (Swap ? FCmpInst::isOrdered(Pred) : FCmpInst::isUnordered(Pred))) {
3017 SI.setHasNoNaNs(true);
3018 ChangedFMF = true;
3019 }
3020
3021 // With nsz, when 'Swap' is false:
3022 // fold (X < +/-0.0) ? -X : X or (X <= +/-0.0) ? -X : X to fabs(X)
3023 // fold (X > +/-0.0) ? -X : X or (X >= +/-0.0) ? -X : X to -fabs(x)
3024 // when 'Swap' is true:
3025 // fold (X > +/-0.0) ? X : -X or (X >= +/-0.0) ? X : -X to fabs(X)
3026 // fold (X < +/-0.0) ? X : -X or (X <= +/-0.0) ? X : -X to -fabs(X)
3027 //
3028 // Note: We require "nnan" for this fold because fcmp ignores the signbit
3029 // of NAN, but IEEE-754 specifies the signbit of NAN values with
3030 // fneg/fabs operations.
3031 if (!SI.hasNoSignedZeros() &&
3032 (!SI.hasOneUse() || !canIgnoreSignBitOfZero(*SI.use_begin())))
3033 return nullptr;
3034 if (!SI.hasNoNaNs() &&
3035 (!SI.hasOneUse() || !canIgnoreSignBitOfNaN(*SI.use_begin())))
3036 return nullptr;
3037
3038 if (Swap)
3039 Pred = FCmpInst::getSwappedPredicate(Pred);
3040
3041 bool IsLTOrLE = Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
3042 Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE;
3043 bool IsGTOrGE = Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
3044 Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE;
3045
3046 if (IsLTOrLE) {
3047 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
3048 return IC.replaceInstUsesWith(SI, Fabs);
3049 }
3050 if (IsGTOrGE) {
3051 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
3052 Instruction *NewFNeg = UnaryOperator::CreateFNeg(Fabs);
3053 NewFNeg->setFastMathFlags(SI.getFastMathFlags());
3054 return NewFNeg;
3055 }
3056 }
3057
3058 // Match select with (icmp slt (bitcast X to int), 0)
3059 // or (icmp sgt (bitcast X to int), -1)
3060
3061 for (bool Swap : {false, true}) {
3062 Value *TrueVal = SI.getTrueValue();
3063 Value *X = SI.getFalseValue();
3064
3065 if (Swap)
3066 std::swap(TrueVal, X);
3067
3068 CmpPredicate Pred;
3069 const APInt *C;
3070 bool TrueIfSigned;
3071 if (!match(CondVal,
3073 !isSignBitCheck(Pred, *C, TrueIfSigned))
3074 continue;
3075 if (!match(TrueVal, m_FNeg(m_Specific(X))))
3076 return nullptr;
3077 if (Swap == TrueIfSigned && !CondVal->hasOneUse() && !TrueVal->hasOneUse())
3078 return nullptr;
3079
3080 // Fold (IsNeg ? -X : X) or (!IsNeg ? X : -X) to fabs(X)
3081 // Fold (IsNeg ? X : -X) or (!IsNeg ? -X : X) to -fabs(X)
3082 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
3083 if (Swap != TrueIfSigned)
3084 return IC.replaceInstUsesWith(SI, Fabs);
3085 return UnaryOperator::CreateFNegFMF(Fabs, &SI);
3086 }
3087
3088 return ChangedFMF ? &SI : nullptr;
3089}
3090
3091// Match the following IR pattern:
3092// %x.lowbits = and i8 %x, %lowbitmask
3093// %x.lowbits.are.zero = icmp eq i8 %x.lowbits, 0
3094// %x.biased = add i8 %x, %bias
3095// %x.biased.highbits = and i8 %x.biased, %highbitmask
3096// %x.roundedup = select i1 %x.lowbits.are.zero, i8 %x, i8 %x.biased.highbits
3097// Define:
3098// %alignment = add i8 %lowbitmask, 1
3099// Iff 1. an %alignment is a power-of-two (aka, %lowbitmask is a low bit mask)
3100// and 2. %bias is equal to either %lowbitmask or %alignment,
3101// and 3. %highbitmask is equal to ~%lowbitmask (aka, to -%alignment)
3102// then this pattern can be transformed into:
3103// %x.offset = add i8 %x, %lowbitmask
3104// %x.roundedup = and i8 %x.offset, %highbitmask
3105static Value *
3106foldRoundUpIntegerWithPow2Alignment(SelectInst &SI,
3107 InstCombiner::BuilderTy &Builder) {
3108 Value *Cond = SI.getCondition();
3109 Value *X = SI.getTrueValue();
3110 Value *XBiasedHighBits = SI.getFalseValue();
3111
3112 CmpPredicate Pred;
3113 Value *XLowBits;
3114 if (!match(Cond, m_ICmp(Pred, m_Value(XLowBits), m_ZeroInt())) ||
3115 !ICmpInst::isEquality(Pred))
3116 return nullptr;
3117
3118 if (Pred == ICmpInst::Predicate::ICMP_NE)
3119 std::swap(X, XBiasedHighBits);
3120
3121 // FIXME: we could support non non-splats here.
3122
3123 const APInt *LowBitMaskCst;
3124 if (!match(XLowBits, m_And(m_Specific(X), m_APIntAllowPoison(LowBitMaskCst))))
3125 return nullptr;
3126
3127 // Match even if the AND and ADD are swapped.
3128 const APInt *BiasCst, *HighBitMaskCst;
3129 if (!match(XBiasedHighBits,
3131 m_APIntAllowPoison(HighBitMaskCst))) &&
3132 !match(XBiasedHighBits,
3133 m_Add(m_And(m_Specific(X), m_APIntAllowPoison(HighBitMaskCst)),
3134 m_APIntAllowPoison(BiasCst))))
3135 return nullptr;
3136
3137 if (!LowBitMaskCst->isMask())
3138 return nullptr;
3139
3140 APInt InvertedLowBitMaskCst = ~*LowBitMaskCst;
3141 if (InvertedLowBitMaskCst != *HighBitMaskCst)
3142 return nullptr;
3143
3144 APInt AlignmentCst = *LowBitMaskCst + 1;
3145
3146 if (*BiasCst != AlignmentCst && *BiasCst != *LowBitMaskCst)
3147 return nullptr;
3148
3149 if (!XBiasedHighBits->hasOneUse()) {
3150 // We can't directly return XBiasedHighBits if it is more poisonous.
3151 if (*BiasCst == *LowBitMaskCst && impliesPoison(XBiasedHighBits, X))
3152 return XBiasedHighBits;
3153 return nullptr;
3154 }
3155
3156 // FIXME: could we preserve undef's here?
3157 Type *Ty = X->getType();
3158 Value *XOffset = Builder.CreateAdd(X, ConstantInt::get(Ty, *LowBitMaskCst),
3159 X->getName() + ".biased");
3160 Value *R = Builder.CreateAnd(XOffset, ConstantInt::get(Ty, *HighBitMaskCst));
3161 R->takeName(&SI);
3162 return R;
3163}
3164
3165namespace {
3166struct DecomposedSelect {
3167 Value *Cond = nullptr;
3168 Value *TrueVal = nullptr;
3169 Value *FalseVal = nullptr;
3170};
3171} // namespace
3172
3173/// Folds patterns like:
3174/// select c2 (select c1 a b) (select c1 b a)
3175/// into:
3176/// select (xor c1 c2) b a
3177static Instruction *
3178foldSelectOfSymmetricSelect(SelectInst &OuterSelVal,
3179 InstCombiner::BuilderTy &Builder) {
3180
3181 Value *OuterCond, *InnerCond, *InnerTrueVal, *InnerFalseVal;
3182 if (!match(
3183 &OuterSelVal,
3184 m_Select(m_Value(OuterCond),
3185 m_OneUse(m_Select(m_Value(InnerCond), m_Value(InnerTrueVal),
3186 m_Value(InnerFalseVal))),
3187 m_OneUse(m_Select(m_Deferred(InnerCond),
3188 m_Deferred(InnerFalseVal),
3189 m_Deferred(InnerTrueVal))))))
3190 return nullptr;
3191
3192 if (OuterCond->getType() != InnerCond->getType())
3193 return nullptr;
3194
3195 Value *Xor = Builder.CreateXor(InnerCond, OuterCond);
3196 return SelectInst::Create(Xor, InnerFalseVal, InnerTrueVal);
3197}
3198
3199/// Look for patterns like
3200/// %outer.cond = select i1 %inner.cond, i1 %alt.cond, i1 false
3201/// %inner.sel = select i1 %inner.cond, i8 %inner.sel.t, i8 %inner.sel.f
3202/// %outer.sel = select i1 %outer.cond, i8 %outer.sel.t, i8 %inner.sel
3203/// and rewrite it as
3204/// %inner.sel = select i1 %cond.alternative, i8 %sel.outer.t, i8 %sel.inner.t
3205/// %sel.outer = select i1 %cond.inner, i8 %inner.sel, i8 %sel.inner.f
3206static Instruction *foldNestedSelects(SelectInst &OuterSelVal,
3207 InstCombiner::BuilderTy &Builder) {
3208 // We must start with a `select`.
3209 DecomposedSelect OuterSel;
3210 match(&OuterSelVal,
3211 m_Select(m_Value(OuterSel.Cond), m_Value(OuterSel.TrueVal),
3212 m_Value(OuterSel.FalseVal)));
3213
3214 // Canonicalize inversion of the outermost `select`'s condition.
3215 if (match(OuterSel.Cond, m_Not(m_Value(OuterSel.Cond))))
3216 std::swap(OuterSel.TrueVal, OuterSel.FalseVal);
3217
3218 // The condition of the outermost select must be an `and`/`or`.
3219 if (!match(OuterSel.Cond, m_c_LogicalOp(m_Value(), m_Value())))
3220 return nullptr;
3221
3222 // Depending on the logical op, inner select might be in different hand.
3223 bool IsAndVariant = match(OuterSel.Cond, m_LogicalAnd());
3224 Value *InnerSelVal = IsAndVariant ? OuterSel.FalseVal : OuterSel.TrueVal;
3225
3226 // Profitability check - avoid increasing instruction count.
3227 if (none_of(ArrayRef<Value *>({OuterSelVal.getCondition(), InnerSelVal}),
3228 [](Value *V) { return V->hasOneUse(); }))
3229 return nullptr;
3230
3231 // The appropriate hand of the outermost `select` must be a select itself.
3232 DecomposedSelect InnerSel;
3233 if (!match(InnerSelVal,
3234 m_Select(m_Value(InnerSel.Cond), m_Value(InnerSel.TrueVal),
3235 m_Value(InnerSel.FalseVal))))
3236 return nullptr;
3237
3238 // Canonicalize inversion of the innermost `select`'s condition.
3239 if (match(InnerSel.Cond, m_Not(m_Value(InnerSel.Cond))))
3240 std::swap(InnerSel.TrueVal, InnerSel.FalseVal);
3241
3242 Value *AltCond = nullptr;
3243 auto matchOuterCond = [OuterSel, IsAndVariant, &AltCond](auto m_InnerCond) {
3244 // An unsimplified select condition can match both LogicalAnd and LogicalOr
3245 // (select true, true, false). Since below we assume that LogicalAnd implies
3246 // InnerSel match the FVal and vice versa for LogicalOr, we can't match the
3247 // alternative pattern here.
3248 return IsAndVariant ? match(OuterSel.Cond,
3249 m_c_LogicalAnd(m_InnerCond, m_Value(AltCond)))
3250 : match(OuterSel.Cond,
3251 m_c_LogicalOr(m_InnerCond, m_Value(AltCond)));
3252 };
3253
3254 // Finally, match the condition that was driving the outermost `select`,
3255 // it should be a logical operation between the condition that was driving
3256 // the innermost `select` (after accounting for the possible inversions
3257 // of the condition), and some other condition.
3258 if (matchOuterCond(m_Specific(InnerSel.Cond))) {
3259 // Done!
3260 } else if (Value * NotInnerCond; matchOuterCond(m_CombineAnd(
3261 m_Not(m_Specific(InnerSel.Cond)), m_Value(NotInnerCond)))) {
3262 // Done!
3263 std::swap(InnerSel.TrueVal, InnerSel.FalseVal);
3264 InnerSel.Cond = NotInnerCond;
3265 } else // Not the pattern we were looking for.
3266 return nullptr;
3267
3268 Value *SelInner = Builder.CreateSelect(
3269 AltCond, IsAndVariant ? OuterSel.TrueVal : InnerSel.FalseVal,
3270 IsAndVariant ? InnerSel.TrueVal : OuterSel.FalseVal);
3271 SelInner->takeName(InnerSelVal);
3272 return SelectInst::Create(InnerSel.Cond,
3273 IsAndVariant ? SelInner : InnerSel.TrueVal,
3274 !IsAndVariant ? SelInner : InnerSel.FalseVal);
3275}
3276
3277/// Return true if V is poison or \p Expected given that ValAssumedPoison is
3278/// already poison. For example, if ValAssumedPoison is `icmp samesign X, 10`
3279/// and V is `icmp ne X, 5`, impliesPoisonOrCond returns true.
3280static bool impliesPoisonOrCond(const Value *ValAssumedPoison, const Value *V,
3281 bool Expected) {
3282 if (impliesPoison(ValAssumedPoison, V))
3283 return true;
3284
3285 // Handle the case that ValAssumedPoison is `icmp samesign pred X, C1` and V
3286 // is `icmp pred X, C2`, where C1 is well-defined.
3287 if (auto *ICmp = dyn_cast<ICmpInst>(ValAssumedPoison)) {
3288 Value *LHS = ICmp->getOperand(0);
3289 const APInt *RHSC1;
3290 const APInt *RHSC2;
3291 CmpPredicate Pred;
3292 if (ICmp->hasSameSign() &&
3293 match(ICmp->getOperand(1), m_APIntForbidPoison(RHSC1)) &&
3294 match(V, m_ICmp(Pred, m_Specific(LHS), m_APIntAllowPoison(RHSC2)))) {
3295 unsigned BitWidth = RHSC1->getBitWidth();
3296 ConstantRange CRX =
3297 RHSC1->isNonNegative()
3300 : ConstantRange(APInt::getZero(BitWidth),
3301 APInt::getSignedMinValue(BitWidth));
3302 return CRX.icmp(Expected ? Pred : ICmpInst::getInverseCmpPredicate(Pred),
3303 *RHSC2);
3304 }
3305 }
3306
3307 return false;
3308}
3309
3311 Value *CondVal = SI.getCondition();
3312 Value *TrueVal = SI.getTrueValue();
3313 Value *FalseVal = SI.getFalseValue();
3314 Type *SelType = SI.getType();
3315
3316 // Avoid potential infinite loops by checking for non-constant condition.
3317 // TODO: Can we assert instead by improving canonicalizeSelectToShuffle()?
3318 // Scalar select must have simplified?
3319 if (!SelType->isIntOrIntVectorTy(1) || isa<Constant>(CondVal) ||
3320 TrueVal->getType() != CondVal->getType())
3321 return nullptr;
3322
3323 auto *One = ConstantInt::getTrue(SelType);
3324 auto *Zero = ConstantInt::getFalse(SelType);
3325 Value *A, *B, *C, *D;
3326
3327 // Folding select to and/or i1 isn't poison safe in general. impliesPoison
3328 // checks whether folding it does not convert a well-defined value into
3329 // poison.
3330 if (match(TrueVal, m_One())) {
3331 if (impliesPoisonOrCond(FalseVal, CondVal, /*Expected=*/false)) {
3332 // Change: A = select B, true, C --> A = or B, C
3333 return BinaryOperator::CreateOr(CondVal, FalseVal);
3334 }
3335
3336 if (match(CondVal, m_OneUse(m_Select(m_Value(A), m_One(), m_Value(B)))) &&
3337 impliesPoisonOrCond(FalseVal, B, /*Expected=*/false)) {
3338 // (A || B) || C --> A || (B | C)
3339 return replaceInstUsesWith(
3340 SI, Builder.CreateLogicalOr(A, Builder.CreateOr(B, FalseVal)));
3341 }
3342
3343 // (A && B) || (C && B) --> (A || C) && B
3344 if (match(CondVal, m_LogicalAnd(m_Value(A), m_Value(B))) &&
3345 match(FalseVal, m_LogicalAnd(m_Value(C), m_Value(D))) &&
3346 (CondVal->hasOneUse() || FalseVal->hasOneUse())) {
3347 bool CondLogicAnd = isa<SelectInst>(CondVal);
3348 bool FalseLogicAnd = isa<SelectInst>(FalseVal);
3349 auto AndFactorization = [&](Value *Common, Value *InnerCond,
3350 Value *InnerVal,
3351 bool SelFirst = false) -> Instruction * {
3352 Value *InnerSel = Builder.CreateSelect(InnerCond, One, InnerVal);
3353 if (SelFirst)
3354 std::swap(Common, InnerSel);
3355 if (FalseLogicAnd || (CondLogicAnd && Common == A))
3356 return SelectInst::Create(Common, InnerSel, Zero);
3357 else
3358 return BinaryOperator::CreateAnd(Common, InnerSel);
3359 };
3360
3361 if (A == C)
3362 return AndFactorization(A, B, D);
3363 if (A == D)
3364 return AndFactorization(A, B, C);
3365 if (B == C)
3366 return AndFactorization(B, A, D);
3367 if (B == D)
3368 return AndFactorization(B, A, C, CondLogicAnd && FalseLogicAnd);
3369 }
3370 }
3371
3372 if (match(FalseVal, m_Zero())) {
3373 if (impliesPoisonOrCond(TrueVal, CondVal, /*Expected=*/true)) {
3374 // Change: A = select B, C, false --> A = and B, C
3375 return BinaryOperator::CreateAnd(CondVal, TrueVal);
3376 }
3377
3378 if (match(CondVal, m_OneUse(m_Select(m_Value(A), m_Value(B), m_Zero()))) &&
3379 impliesPoisonOrCond(TrueVal, B, /*Expected=*/true)) {
3380 // (A && B) && C --> A && (B & C)
3381 return replaceInstUsesWith(
3382 SI, Builder.CreateLogicalAnd(A, Builder.CreateAnd(B, TrueVal)));
3383 }
3384
3385 // (A || B) && (C || B) --> (A && C) || B
3386 if (match(CondVal, m_LogicalOr(m_Value(A), m_Value(B))) &&
3387 match(TrueVal, m_LogicalOr(m_Value(C), m_Value(D))) &&
3388 (CondVal->hasOneUse() || TrueVal->hasOneUse())) {
3389 bool CondLogicOr = isa<SelectInst>(CondVal);
3390 bool TrueLogicOr = isa<SelectInst>(TrueVal);
3391 auto OrFactorization = [&](Value *Common, Value *InnerCond,
3392 Value *InnerVal,
3393 bool SelFirst = false) -> Instruction * {
3394 Value *InnerSel = Builder.CreateSelect(InnerCond, InnerVal, Zero);
3395 if (SelFirst)
3396 std::swap(Common, InnerSel);
3397 if (TrueLogicOr || (CondLogicOr && Common == A))
3398 return SelectInst::Create(Common, One, InnerSel);
3399 else
3400 return BinaryOperator::CreateOr(Common, InnerSel);
3401 };
3402
3403 if (A == C)
3404 return OrFactorization(A, B, D);
3405 if (A == D)
3406 return OrFactorization(A, B, C);
3407 if (B == C)
3408 return OrFactorization(B, A, D);
3409 if (B == D)
3410 return OrFactorization(B, A, C, CondLogicOr && TrueLogicOr);
3411 }
3412 }
3413
3414 // We match the "full" 0 or 1 constant here to avoid a potential infinite
3415 // loop with vectors that may have undefined/poison elements.
3416 // select a, false, b -> select !a, b, false
3417 if (match(TrueVal, m_Specific(Zero))) {
3418 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
3419 return SelectInst::Create(NotCond, FalseVal, Zero);
3420 }
3421 // select a, b, true -> select !a, true, b
3422 if (match(FalseVal, m_Specific(One))) {
3423 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
3424 return SelectInst::Create(NotCond, One, TrueVal);
3425 }
3426
3427 // DeMorgan in select form: !a && !b --> !(a || b)
3428 // select !a, !b, false --> not (select a, true, b)
3429 if (match(&SI, m_LogicalAnd(m_Not(m_Value(A)), m_Not(m_Value(B)))) &&
3430 (CondVal->hasOneUse() || TrueVal->hasOneUse()) &&
3433
3434 // DeMorgan in select form: !a || !b --> !(a && b)
3435 // select !a, true, !b --> not (select a, b, false)
3436 if (match(&SI, m_LogicalOr(m_Not(m_Value(A)), m_Not(m_Value(B)))) &&
3437 (CondVal->hasOneUse() || FalseVal->hasOneUse()) &&
3440
3441 // select (select a, true, b), true, b -> select a, true, b
3442 if (match(CondVal, m_Select(m_Value(A), m_One(), m_Value(B))) &&
3443 match(TrueVal, m_One()) && match(FalseVal, m_Specific(B)))
3444 return replaceOperand(SI, 0, A);
3445 // select (select a, b, false), b, false -> select a, b, false
3446 if (match(CondVal, m_Select(m_Value(A), m_Value(B), m_Zero())) &&
3447 match(TrueVal, m_Specific(B)) && match(FalseVal, m_Zero()))
3448 return replaceOperand(SI, 0, A);
3449
3450 // ~(A & B) & (A | B) --> A ^ B
3453 return BinaryOperator::CreateXor(A, B);
3454
3455 // select (~a | c), a, b -> select a, (select c, true, b), false
3456 if (match(CondVal,
3457 m_OneUse(m_c_Or(m_Not(m_Specific(TrueVal)), m_Value(C))))) {
3458 Value *OrV = Builder.CreateSelect(C, One, FalseVal);
3459 return SelectInst::Create(TrueVal, OrV, Zero);
3460 }
3461 // select (c & b), a, b -> select b, (select ~c, true, a), false
3462 if (match(CondVal, m_OneUse(m_c_And(m_Value(C), m_Specific(FalseVal))))) {
3463 if (Value *NotC = getFreelyInverted(C, C->hasOneUse(), &Builder)) {
3464 Value *OrV = Builder.CreateSelect(NotC, One, TrueVal);
3465 return SelectInst::Create(FalseVal, OrV, Zero);
3466 }
3467 }
3468 // select (a | c), a, b -> select a, true, (select ~c, b, false)
3469 if (match(CondVal, m_OneUse(m_c_Or(m_Specific(TrueVal), m_Value(C))))) {
3470 if (Value *NotC = getFreelyInverted(C, C->hasOneUse(), &Builder)) {
3471 Value *AndV = Builder.CreateSelect(NotC, FalseVal, Zero);
3472 return SelectInst::Create(TrueVal, One, AndV);
3473 }
3474 }
3475 // select (c & ~b), a, b -> select b, true, (select c, a, false)
3476 if (match(CondVal,
3477 m_OneUse(m_c_And(m_Value(C), m_Not(m_Specific(FalseVal)))))) {
3478 Value *AndV = Builder.CreateSelect(C, TrueVal, Zero);
3479 return SelectInst::Create(FalseVal, One, AndV);
3480 }
3481
3482 if (match(FalseVal, m_Zero()) || match(TrueVal, m_One())) {
3483 Use *Y = nullptr;
3484 bool IsAnd = match(FalseVal, m_Zero()) ? true : false;
3485 Value *Op1 = IsAnd ? TrueVal : FalseVal;
3486 if (isCheckForZeroAndMulWithOverflow(CondVal, Op1, IsAnd, Y)) {
3487 auto *FI = new FreezeInst(*Y, (*Y)->getName() + ".fr");
3488 InsertNewInstBefore(FI, cast<Instruction>(Y->getUser())->getIterator());
3489 replaceUse(*Y, FI);
3490 return replaceInstUsesWith(SI, Op1);
3491 }
3492
3493 if (auto *V = foldBooleanAndOr(CondVal, Op1, SI, IsAnd,
3494 /*IsLogical=*/true))
3495 return replaceInstUsesWith(SI, V);
3496 }
3497
3498 // select (a || b), c, false -> select a, c, false
3499 // select c, (a || b), false -> select c, a, false
3500 // if c implies that b is false.
3501 if (match(CondVal, m_LogicalOr(m_Value(A), m_Value(B))) &&
3502 match(FalseVal, m_Zero())) {
3503 std::optional<bool> Res = isImpliedCondition(TrueVal, B, DL);
3504 if (Res && *Res == false)
3505 return replaceOperand(SI, 0, A);
3506 }
3507 if (match(TrueVal, m_LogicalOr(m_Value(A), m_Value(B))) &&
3508 match(FalseVal, m_Zero())) {
3509 std::optional<bool> Res = isImpliedCondition(CondVal, B, DL);
3510 if (Res && *Res == false)
3511 return replaceOperand(SI, 1, A);
3512 }
3513 // select c, true, (a && b) -> select c, true, a
3514 // select (a && b), true, c -> select a, true, c
3515 // if c = false implies that b = true
3516 if (match(TrueVal, m_One()) &&
3517 match(FalseVal, m_LogicalAnd(m_Value(A), m_Value(B)))) {
3518 std::optional<bool> Res = isImpliedCondition(CondVal, B, DL, false);
3519 if (Res && *Res == true)
3520 return replaceOperand(SI, 2, A);
3521 }
3522 if (match(CondVal, m_LogicalAnd(m_Value(A), m_Value(B))) &&
3523 match(TrueVal, m_One())) {
3524 std::optional<bool> Res = isImpliedCondition(FalseVal, B, DL, false);
3525 if (Res && *Res == true)
3526 return replaceOperand(SI, 0, A);
3527 }
3528
3529 if (match(TrueVal, m_One())) {
3530 Value *C;
3531
3532 // (C && A) || (!C && B) --> sel C, A, B
3533 // (A && C) || (!C && B) --> sel C, A, B
3534 // (C && A) || (B && !C) --> sel C, A, B
3535 // (A && C) || (B && !C) --> sel C, A, B (may require freeze)
3536 if (match(FalseVal, m_c_LogicalAnd(m_Not(m_Value(C)), m_Value(B))) &&
3537 match(CondVal, m_c_LogicalAnd(m_Specific(C), m_Value(A)))) {
3538 auto *SelCond = dyn_cast<SelectInst>(CondVal);
3539 auto *SelFVal = dyn_cast<SelectInst>(FalseVal);
3540 bool MayNeedFreeze = SelCond && SelFVal &&
3541 match(SelFVal->getTrueValue(),
3542 m_Not(m_Specific(SelCond->getTrueValue())));
3543 if (MayNeedFreeze)
3545 return SelectInst::Create(C, A, B);
3546 }
3547
3548 // (!C && A) || (C && B) --> sel C, B, A
3549 // (A && !C) || (C && B) --> sel C, B, A
3550 // (!C && A) || (B && C) --> sel C, B, A
3551 // (A && !C) || (B && C) --> sel C, B, A (may require freeze)
3552 if (match(CondVal, m_c_LogicalAnd(m_Not(m_Value(C)), m_Value(A))) &&
3553 match(FalseVal, m_c_LogicalAnd(m_Specific(C), m_Value(B)))) {
3554 auto *SelCond = dyn_cast<SelectInst>(CondVal);
3555 auto *SelFVal = dyn_cast<SelectInst>(FalseVal);
3556 bool MayNeedFreeze = SelCond && SelFVal &&
3557 match(SelCond->getTrueValue(),
3558 m_Not(m_Specific(SelFVal->getTrueValue())));
3559 if (MayNeedFreeze)
3561 return SelectInst::Create(C, B, A);
3562 }
3563 }
3564
3565 return nullptr;
3566}
3567
3568// Return true if we can safely remove the select instruction for std::bit_ceil
3569// pattern.
3570static bool isSafeToRemoveBitCeilSelect(ICmpInst::Predicate Pred, Value *Cond0,
3571 const APInt *Cond1, Value *CtlzOp,
3572 unsigned BitWidth,
3573 bool &ShouldDropNoWrap) {
3574 // The challenge in recognizing std::bit_ceil(X) is that the operand is used
3575 // for the CTLZ proper and select condition, each possibly with some
3576 // operation like add and sub.
3577 //
3578 // Our aim is to make sure that -ctlz & (BitWidth - 1) == 0 even when the
3579 // select instruction would select 1, which allows us to get rid of the select
3580 // instruction.
3581 //
3582 // To see if we can do so, we do some symbolic execution with ConstantRange.
3583 // Specifically, we compute the range of values that Cond0 could take when
3584 // Cond == false. Then we successively transform the range until we obtain
3585 // the range of values that CtlzOp could take.
3586 //
3587 // Conceptually, we follow the def-use chain backward from Cond0 while
3588 // transforming the range for Cond0 until we meet the common ancestor of Cond0
3589 // and CtlzOp. Then we follow the def-use chain forward until we obtain the
3590 // range for CtlzOp. That said, we only follow at most one ancestor from
3591 // Cond0. Likewise, we only follow at most one ancestor from CtrlOp.
3592
3594 CmpInst::getInversePredicate(Pred), *Cond1);
3595
3596 ShouldDropNoWrap = false;
3597
3598 // Match the operation that's used to compute CtlzOp from CommonAncestor. If
3599 // CtlzOp == CommonAncestor, return true as no operation is needed. If a
3600 // match is found, execute the operation on CR, update CR, and return true.
3601 // Otherwise, return false.
3602 auto MatchForward = [&](Value *CommonAncestor) {
3603 const APInt *C = nullptr;
3604 if (CtlzOp == CommonAncestor)
3605 return true;
3606 if (match(CtlzOp, m_Add(m_Specific(CommonAncestor), m_APInt(C)))) {
3607 ShouldDropNoWrap = true;
3608 CR = CR.add(*C);
3609 return true;
3610 }
3611 if (match(CtlzOp, m_Sub(m_APInt(C), m_Specific(CommonAncestor)))) {
3612 ShouldDropNoWrap = true;
3613 CR = ConstantRange(*C).sub(CR);
3614 return true;
3615 }
3616 if (match(CtlzOp, m_Not(m_Specific(CommonAncestor)))) {
3617 CR = CR.binaryNot();
3618 return true;
3619 }
3620 return false;
3621 };
3622
3623 const APInt *C = nullptr;
3624 Value *CommonAncestor;
3625 if (MatchForward(Cond0)) {
3626 // Cond0 is either CtlzOp or CtlzOp's parent. CR has been updated.
3627 } else if (match(Cond0, m_Add(m_Value(CommonAncestor), m_APInt(C)))) {
3628 CR = CR.sub(*C);
3629 if (!MatchForward(CommonAncestor))
3630 return false;
3631 // Cond0's parent is either CtlzOp or CtlzOp's parent. CR has been updated.
3632 } else {
3633 return false;
3634 }
3635
3636 // Return true if all the values in the range are either 0 or negative (if
3637 // treated as signed). We do so by evaluating:
3638 //
3639 // CR - 1 u>= (1 << BitWidth) - 1.
3640 APInt IntMax = APInt::getSignMask(BitWidth) - 1;
3641 CR = CR.sub(APInt(BitWidth, 1));
3642 return CR.icmp(ICmpInst::ICMP_UGE, IntMax);
3643}
3644
3645// Transform the std::bit_ceil(X) pattern like:
3646//
3647// %dec = add i32 %x, -1
3648// %ctlz = tail call i32 @llvm.ctlz.i32(i32 %dec, i1 false)
3649// %sub = sub i32 32, %ctlz
3650// %shl = shl i32 1, %sub
3651// %ugt = icmp ugt i32 %x, 1
3652// %sel = select i1 %ugt, i32 %shl, i32 1
3653//
3654// into:
3655//
3656// %dec = add i32 %x, -1
3657// %ctlz = tail call i32 @llvm.ctlz.i32(i32 %dec, i1 false)
3658// %neg = sub i32 0, %ctlz
3659// %masked = and i32 %ctlz, 31
3660// %shl = shl i32 1, %sub
3661//
3662// Note that the select is optimized away while the shift count is masked with
3663// 31. We handle some variations of the input operand like std::bit_ceil(X +
3664// 1).
3665static Instruction *foldBitCeil(SelectInst &SI, IRBuilderBase &Builder,
3666 InstCombinerImpl &IC) {
3667 Type *SelType = SI.getType();
3668 unsigned BitWidth = SelType->getScalarSizeInBits();
3669
3670 Value *FalseVal = SI.getFalseValue();
3671 Value *TrueVal = SI.getTrueValue();
3672 CmpPredicate Pred;
3673 const APInt *Cond1;
3674 Value *Cond0, *Ctlz, *CtlzOp;
3675 if (!match(SI.getCondition(), m_ICmp(Pred, m_Value(Cond0), m_APInt(Cond1))))
3676 return nullptr;
3677
3678 if (match(TrueVal, m_One())) {
3679 std::swap(FalseVal, TrueVal);
3680 Pred = CmpInst::getInversePredicate(Pred);
3681 }
3682
3683 bool ShouldDropNoWrap;
3684
3685 if (!match(FalseVal, m_One()) ||
3686 !match(TrueVal,
3688 m_Value(Ctlz)))))) ||
3689 !match(Ctlz, m_Intrinsic<Intrinsic::ctlz>(m_Value(CtlzOp), m_Value())) ||
3690 !isSafeToRemoveBitCeilSelect(Pred, Cond0, Cond1, CtlzOp, BitWidth,
3691 ShouldDropNoWrap))
3692 return nullptr;
3693
3694 if (ShouldDropNoWrap) {
3695 cast<Instruction>(CtlzOp)->setHasNoUnsignedWrap(false);
3696 cast<Instruction>(CtlzOp)->setHasNoSignedWrap(false);
3697 }
3698
3699 // Build 1 << (-CTLZ & (BitWidth-1)). The negation likely corresponds to a
3700 // single hardware instruction as opposed to BitWidth - CTLZ, where BitWidth
3701 // is an integer constant. Masking with BitWidth-1 comes free on some
3702 // hardware as part of the shift instruction.
3703
3704 // Drop range attributes and re-infer them in the next iteration.
3705 cast<Instruction>(Ctlz)->dropPoisonGeneratingAnnotations();
3706 // Set is_zero_poison to false and re-infer them in the next iteration.
3707 cast<Instruction>(Ctlz)->setOperand(1, Builder.getFalse());
3708 IC.addToWorklist(cast<Instruction>(Ctlz));
3709 Value *Neg = Builder.CreateNeg(Ctlz);
3710 Value *Masked =
3711 Builder.CreateAnd(Neg, ConstantInt::get(SelType, BitWidth - 1));
3712 return BinaryOperator::Create(Instruction::Shl, ConstantInt::get(SelType, 1),
3713 Masked);
3714}
3715
3716// This function tries to fold the following operations:
3717// (x < y) ? -1 : zext(x != y)
3718// (x < y) ? -1 : zext(x > y)
3719// (x > y) ? 1 : sext(x != y)
3720// (x > y) ? 1 : sext(x < y)
3721// Into ucmp/scmp(x, y), where signedness is determined by the signedness
3722// of the comparison in the original sequence.
3724 Value *TV = SI.getTrueValue();
3725 Value *FV = SI.getFalseValue();
3726
3727 CmpPredicate Pred;
3728 Value *LHS, *RHS;
3729 if (!match(SI.getCondition(), m_ICmp(Pred, m_Value(LHS), m_Value(RHS))))
3730 return nullptr;
3731
3732 if (!LHS->getType()->isIntOrIntVectorTy())
3733 return nullptr;
3734
3735 // If there is no -1, 0 or 1 at TV, then invert the select statement and try
3736 // to canonicalize to one of the forms above
3737 if (!isa<Constant>(TV)) {
3738 if (!isa<Constant>(FV))
3739 return nullptr;
3741 std::swap(TV, FV);
3742 }
3743
3745 if (Constant *C = dyn_cast<Constant>(RHS)) {
3746 auto FlippedPredAndConst =
3748 if (!FlippedPredAndConst)
3749 return nullptr;
3750 Pred = FlippedPredAndConst->first;
3751 RHS = FlippedPredAndConst->second;
3752 } else {
3753 return nullptr;
3754 }
3755 }
3756
3757 // Try to swap operands and the predicate. We need to be careful when doing
3758 // so because two of the patterns have opposite predicates, so use the
3759 // constant inside select to determine if swapping operands would be
3760 // beneficial to us.
3761 if ((ICmpInst::isGT(Pred) && match(TV, m_AllOnes())) ||
3762 (ICmpInst::isLT(Pred) && match(TV, m_One()))) {
3763 Pred = ICmpInst::getSwappedPredicate(Pred);
3764 std::swap(LHS, RHS);
3765 }
3766 bool IsSigned = ICmpInst::isSigned(Pred);
3767
3768 bool Replace = false;
3769 CmpPredicate ExtendedCmpPredicate;
3770 // (x < y) ? -1 : zext(x != y)
3771 // (x < y) ? -1 : zext(x > y)
3772 if (ICmpInst::isLT(Pred) && match(TV, m_AllOnes()) &&
3773 match(FV, m_ZExt(m_c_ICmp(ExtendedCmpPredicate, m_Specific(LHS),
3774 m_Specific(RHS)))) &&
3775 (ExtendedCmpPredicate == ICmpInst::ICMP_NE ||
3776 ICmpInst::getSwappedPredicate(ExtendedCmpPredicate) == Pred))
3777 Replace = true;
3778
3779 // (x > y) ? 1 : sext(x != y)
3780 // (x > y) ? 1 : sext(x < y)
3781 if (ICmpInst::isGT(Pred) && match(TV, m_One()) &&
3782 match(FV, m_SExt(m_c_ICmp(ExtendedCmpPredicate, m_Specific(LHS),
3783 m_Specific(RHS)))) &&
3784 (ExtendedCmpPredicate == ICmpInst::ICMP_NE ||
3785 ICmpInst::getSwappedPredicate(ExtendedCmpPredicate) == Pred))
3786 Replace = true;
3787
3788 // (x == y) ? 0 : (x > y ? 1 : -1)
3789 CmpPredicate FalseBranchSelectPredicate;
3790 const APInt *InnerTV, *InnerFV;
3791 if (Pred == ICmpInst::ICMP_EQ && match(TV, m_Zero()) &&
3792 match(FV, m_Select(m_c_ICmp(FalseBranchSelectPredicate, m_Specific(LHS),
3793 m_Specific(RHS)),
3794 m_APInt(InnerTV), m_APInt(InnerFV)))) {
3795 if (!ICmpInst::isGT(FalseBranchSelectPredicate)) {
3796 FalseBranchSelectPredicate =
3797 ICmpInst::getSwappedPredicate(FalseBranchSelectPredicate);
3798 std::swap(LHS, RHS);
3799 }
3800
3801 if (!InnerTV->isOne()) {
3802 std::swap(InnerTV, InnerFV);
3803 std::swap(LHS, RHS);
3804 }
3805
3806 if (ICmpInst::isGT(FalseBranchSelectPredicate) && InnerTV->isOne() &&
3807 InnerFV->isAllOnes()) {
3808 IsSigned = ICmpInst::isSigned(FalseBranchSelectPredicate);
3809 Replace = true;
3810 }
3811 }
3812
3813 Intrinsic::ID IID = IsSigned ? Intrinsic::scmp : Intrinsic::ucmp;
3814 if (Replace)
3815 return replaceInstUsesWith(
3816 SI, Builder.CreateIntrinsic(SI.getType(), IID, {LHS, RHS}));
3817 return nullptr;
3818}
3819
3821 const Instruction *CtxI) const {
3822 KnownFPClass Known = computeKnownFPClass(MulVal, FMF, fcNegative, CtxI);
3823
3824 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity() &&
3825 (FMF.noSignedZeros() || Known.signBitIsZeroOrNaN());
3826}
3827
3828static bool matchFMulByZeroIfResultEqZero(InstCombinerImpl &IC, Value *Cmp0,
3829 Value *Cmp1, Value *TrueVal,
3830 Value *FalseVal, Instruction &CtxI,
3831 bool SelectIsNSZ) {
3832 Value *MulRHS;
3833 if (match(Cmp1, m_PosZeroFP()) &&
3834 match(TrueVal, m_c_FMul(m_Specific(Cmp0), m_Value(MulRHS)))) {
3835 FastMathFlags FMF = cast<FPMathOperator>(TrueVal)->getFastMathFlags();
3836 // nsz must be on the select, it must be ignored on the multiply. We
3837 // need nnan and ninf on the multiply for the other value.
3838 FMF.setNoSignedZeros(SelectIsNSZ);
3839 return IC.fmulByZeroIsZero(MulRHS, FMF, &CtxI);
3840 }
3841
3842 return false;
3843}
3844
3845/// Check whether the KnownBits of a select arm may be affected by the
3846/// select condition.
3847static bool hasAffectedValue(Value *V, SmallPtrSetImpl<Value *> &Affected,
3848 unsigned Depth) {
3850 return false;
3851
3852 // Ignore the case where the select arm itself is affected. These cases
3853 // are handled more efficiently by other optimizations.
3854 if (Depth != 0 && Affected.contains(V))
3855 return true;
3856
3857 if (auto *I = dyn_cast<Instruction>(V)) {
3858 if (isa<PHINode>(I)) {
3860 return false;
3862 }
3863 return any_of(I->operands(), [&](Value *Op) {
3864 return Op->getType()->isIntOrIntVectorTy() &&
3865 hasAffectedValue(Op, Affected, Depth + 1);
3866 });
3867 }
3868
3869 return false;
3870}
3871
3872// This transformation enables the possibility of transforming fcmp + sel into
3873// a fmaxnum/fminnum intrinsic.
3874static Value *foldSelectIntoAddConstant(SelectInst &SI,
3875 InstCombiner::BuilderTy &Builder) {
3876 // Do this transformation only when select instruction gives NaN and NSZ
3877 // guarantee.
3878 auto *SIFOp = dyn_cast<FPMathOperator>(&SI);
3879 if (!SIFOp || !SIFOp->hasNoSignedZeros() || !SIFOp->hasNoNaNs())
3880 return nullptr;
3881
3882 auto TryFoldIntoAddConstant =
3883 [&Builder, &SI](CmpInst::Predicate Pred, Value *X, Value *Z,
3884 Instruction *FAdd, Constant *C, bool Swapped) -> Value * {
3885 // Only these relational predicates can be transformed into maxnum/minnum
3886 // intrinsic.
3887 if (!CmpInst::isRelational(Pred) || !match(Z, m_AnyZeroFP()))
3888 return nullptr;
3889
3891 return nullptr;
3892
3893 Value *NewSelect = Builder.CreateSelect(SI.getCondition(), Swapped ? Z : X,
3894 Swapped ? X : Z, "", &SI);
3895 NewSelect->takeName(&SI);
3896
3897 Value *NewFAdd = Builder.CreateFAdd(NewSelect, C);
3898 NewFAdd->takeName(FAdd);
3899
3900 // Propagate FastMath flags
3901 FastMathFlags SelectFMF = SI.getFastMathFlags();
3902 FastMathFlags FAddFMF = FAdd->getFastMathFlags();
3903 FastMathFlags NewFMF = FastMathFlags::intersectRewrite(SelectFMF, FAddFMF) |
3904 FastMathFlags::unionValue(SelectFMF, FAddFMF);
3905 cast<Instruction>(NewFAdd)->setFastMathFlags(NewFMF);
3906 cast<Instruction>(NewSelect)->setFastMathFlags(NewFMF);
3907
3908 return NewFAdd;
3909 };
3910
3911 // select((fcmp Pred, X, 0), (fadd X, C), C)
3912 // => fadd((select (fcmp Pred, X, 0), X, 0), C)
3913 //
3914 // Pred := OGT, OGE, OLT, OLE, UGT, UGE, ULT, and ULE
3916 Constant *C;
3917 Value *X, *Z;
3918 CmpPredicate Pred;
3919
3920 // Note: OneUse check for `Cmp` is necessary because it makes sure that other
3921 // InstCombine folds don't undo this transformation and cause an infinite
3922 // loop. Furthermore, it could also increase the operation count.
3923 if (match(&SI, m_Select(m_OneUse(m_FCmp(Pred, m_Value(X), m_Value(Z))),
3925 return TryFoldIntoAddConstant(Pred, X, Z, FAdd, C, /*Swapped=*/false);
3926
3927 if (match(&SI, m_Select(m_OneUse(m_FCmp(Pred, m_Value(X), m_Value(Z))),
3929 return TryFoldIntoAddConstant(Pred, X, Z, FAdd, C, /*Swapped=*/true);
3930
3931 return nullptr;
3932}
3933
3934static Value *foldSelectBitTest(SelectInst &Sel, Value *CondVal, Value *TrueVal,
3935 Value *FalseVal,
3936 InstCombiner::BuilderTy &Builder,
3937 const SimplifyQuery &SQ) {
3938 // If this is a vector select, we need a vector compare.
3939 Type *SelType = Sel.getType();
3940 if (SelType->isVectorTy() != CondVal->getType()->isVectorTy())
3941 return nullptr;
3942
3943 Value *V;
3944 APInt AndMask;
3945 bool CreateAnd = false;
3946 CmpPredicate Pred;
3947 Value *CmpLHS, *CmpRHS;
3948
3949 if (match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
3950 if (ICmpInst::isEquality(Pred)) {
3951 if (!match(CmpRHS, m_Zero()))
3952 return nullptr;
3953
3954 V = CmpLHS;
3955 const APInt *AndRHS;
3956 if (!match(CmpLHS, m_And(m_Value(), m_Power2(AndRHS))))
3957 return nullptr;
3958
3959 AndMask = *AndRHS;
3960 } else if (auto Res = decomposeBitTestICmp(CmpLHS, CmpRHS, Pred)) {
3961 assert(ICmpInst::isEquality(Res->Pred) && "Not equality test?");
3962 AndMask = Res->Mask;
3963 V = Res->X;
3964 KnownBits Known = computeKnownBits(V, SQ.getWithInstruction(&Sel));
3965 AndMask &= Known.getMaxValue();
3966 if (!AndMask.isPowerOf2())
3967 return nullptr;
3968
3969 Pred = Res->Pred;
3970 CreateAnd = true;
3971 } else {
3972 return nullptr;
3973 }
3974 } else if (auto *Trunc = dyn_cast<TruncInst>(CondVal)) {
3975 V = Trunc->getOperand(0);
3976 AndMask = APInt(V->getType()->getScalarSizeInBits(), 1);
3977 Pred = ICmpInst::ICMP_NE;
3978 CreateAnd = !Trunc->hasNoUnsignedWrap();
3979 } else {
3980 return nullptr;
3981 }
3982
3983 if (Pred == ICmpInst::ICMP_NE)
3984 std::swap(TrueVal, FalseVal);
3985
3986 if (Value *X = foldSelectICmpAnd(Sel, CondVal, TrueVal, FalseVal, V, AndMask,
3987 CreateAnd, Builder))
3988 return X;
3989
3990 if (Value *X = foldSelectICmpAndBinOp(CondVal, TrueVal, FalseVal, V, AndMask,
3991 CreateAnd, Builder))
3992 return X;
3993
3994 return nullptr;
3995}
3996
3998 Value *CondVal = SI.getCondition();
3999 Value *TrueVal = SI.getTrueValue();
4000 Value *FalseVal = SI.getFalseValue();
4001 Type *SelType = SI.getType();
4002
4003 if (Value *V = simplifySelectInst(CondVal, TrueVal, FalseVal,
4004 SQ.getWithInstruction(&SI)))
4005 return replaceInstUsesWith(SI, V);
4006
4007 if (Instruction *I = canonicalizeSelectToShuffle(SI))
4008 return I;
4009
4010 if (Instruction *I = canonicalizeScalarSelectOfVecs(SI, *this))
4011 return I;
4012
4013 // If the type of select is not an integer type or if the condition and
4014 // the selection type are not both scalar nor both vector types, there is no
4015 // point in attempting to match these patterns.
4016 Type *CondType = CondVal->getType();
4017 if (!isa<Constant>(CondVal) && SelType->isIntOrIntVectorTy() &&
4018 CondType->isVectorTy() == SelType->isVectorTy()) {
4019 if (Value *S = simplifyWithOpReplaced(TrueVal, CondVal,
4020 ConstantInt::getTrue(CondType), SQ,
4021 /* AllowRefinement */ true))
4022 return replaceOperand(SI, 1, S);
4023
4024 if (Value *S = simplifyWithOpReplaced(FalseVal, CondVal,
4025 ConstantInt::getFalse(CondType), SQ,
4026 /* AllowRefinement */ true))
4027 return replaceOperand(SI, 2, S);
4028
4029 if (replaceInInstruction(TrueVal, CondVal,
4030 ConstantInt::getTrue(CondType)) ||
4031 replaceInInstruction(FalseVal, CondVal,
4032 ConstantInt::getFalse(CondType)))
4033 return &SI;
4034 }
4035
4036 if (Instruction *R = foldSelectOfBools(SI))
4037 return R;
4038
4039 // Selecting between two integer or vector splat integer constants?
4040 //
4041 // Note that we don't handle a scalar select of vectors:
4042 // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0>
4043 // because that may need 3 instructions to splat the condition value:
4044 // extend, insertelement, shufflevector.
4045 //
4046 // Do not handle i1 TrueVal and FalseVal otherwise would result in
4047 // zext/sext i1 to i1.
4048 if (SelType->isIntOrIntVectorTy() && !SelType->isIntOrIntVectorTy(1) &&
4049 CondVal->getType()->isVectorTy() == SelType->isVectorTy()) {
4050 // select C, 1, 0 -> zext C to int
4051 if (match(TrueVal, m_One()) && match(FalseVal, m_Zero()))
4052 return new ZExtInst(CondVal, SelType);
4053
4054 // select C, -1, 0 -> sext C to int
4055 if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero()))
4056 return new SExtInst(CondVal, SelType);
4057
4058 // select C, 0, 1 -> zext !C to int
4059 if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) {
4060 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
4061 return new ZExtInst(NotCond, SelType);
4062 }
4063
4064 // select C, 0, -1 -> sext !C to int
4065 if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) {
4066 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
4067 return new SExtInst(NotCond, SelType);
4068 }
4069 }
4070
4071 auto *SIFPOp = dyn_cast<FPMathOperator>(&SI);
4072
4073 if (auto *FCmp = dyn_cast<FCmpInst>(CondVal)) {
4074 FCmpInst::Predicate Pred = FCmp->getPredicate();
4075 Value *Cmp0 = FCmp->getOperand(0), *Cmp1 = FCmp->getOperand(1);
4076 // Are we selecting a value based on a comparison of the two values?
4077 if ((Cmp0 == TrueVal && Cmp1 == FalseVal) ||
4078 (Cmp0 == FalseVal && Cmp1 == TrueVal)) {
4079 // Canonicalize to use ordered comparisons by swapping the select
4080 // operands.
4081 //
4082 // e.g.
4083 // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X
4084 if (FCmp->hasOneUse() && FCmpInst::isUnordered(Pred)) {
4085 FCmpInst::Predicate InvPred = FCmp->getInversePredicate();
4086 Value *NewCond = Builder.CreateFCmpFMF(InvPred, Cmp0, Cmp1, FCmp,
4087 FCmp->getName() + ".inv");
4088 // Propagate ninf/nnan from fcmp to select.
4089 FastMathFlags FMF = SI.getFastMathFlags();
4090 if (FCmp->hasNoNaNs())
4091 FMF.setNoNaNs(true);
4092 if (FCmp->hasNoInfs())
4093 FMF.setNoInfs(true);
4094 Value *NewSel =
4095 Builder.CreateSelectFMF(NewCond, FalseVal, TrueVal, FMF);
4096 return replaceInstUsesWith(SI, NewSel);
4097 }
4098 }
4099
4100 if (SIFPOp) {
4101 // Fold out scale-if-equals-zero pattern.
4102 //
4103 // This pattern appears in code with denormal range checks after it's
4104 // assumed denormals are treated as zero. This drops a canonicalization.
4105
4106 // TODO: Could relax the signed zero logic. We just need to know the sign
4107 // of the result matches (fmul x, y has the same sign as x).
4108 //
4109 // TODO: Handle always-canonicalizing variant that selects some value or 1
4110 // scaling factor in the fmul visitor.
4111
4112 // TODO: Handle ldexp too
4113
4114 Value *MatchCmp0 = nullptr;
4115 Value *MatchCmp1 = nullptr;
4116
4117 // (select (fcmp [ou]eq x, 0.0), (fmul x, K), x => x
4118 // (select (fcmp [ou]ne x, 0.0), x, (fmul x, K) => x
4119 if (Pred == CmpInst::FCMP_OEQ || Pred == CmpInst::FCMP_UEQ) {
4120 MatchCmp0 = FalseVal;
4121 MatchCmp1 = TrueVal;
4122 } else if (Pred == CmpInst::FCMP_ONE || Pred == CmpInst::FCMP_UNE) {
4123 MatchCmp0 = TrueVal;
4124 MatchCmp1 = FalseVal;
4125 }
4126
4127 if (Cmp0 == MatchCmp0 &&
4128 matchFMulByZeroIfResultEqZero(*this, Cmp0, Cmp1, MatchCmp1, MatchCmp0,
4129 SI, SIFPOp->hasNoSignedZeros()))
4130 return replaceInstUsesWith(SI, Cmp0);
4131 }
4132 }
4133
4134 if (SIFPOp) {
4135 // TODO: Try to forward-propagate FMF from select arms to the select.
4136
4137 auto *FCmp = dyn_cast<FCmpInst>(CondVal);
4138
4139 // Canonicalize select of FP values where NaN and -0.0 are not valid as
4140 // minnum/maxnum intrinsics.
4141 if (SIFPOp->hasNoNaNs() &&
4142 (SIFPOp->hasNoSignedZeros() ||
4143 (SIFPOp->hasOneUse() &&
4144 canIgnoreSignBitOfZero(*SIFPOp->use_begin())))) {
4145 Value *X, *Y;
4146 if (match(&SI, m_OrdOrUnordFMax(m_Value(X), m_Value(Y)))) {
4147 Value *BinIntr =
4148 Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, X, Y, &SI);
4149 if (auto *BinIntrInst = dyn_cast<Instruction>(BinIntr)) {
4150 BinIntrInst->setHasNoNaNs(FCmp->hasNoNaNs());
4151 BinIntrInst->setHasNoInfs(FCmp->hasNoInfs());
4152 }
4153 return replaceInstUsesWith(SI, BinIntr);
4154 }
4155
4156 if (match(&SI, m_OrdOrUnordFMin(m_Value(X), m_Value(Y)))) {
4157 Value *BinIntr =
4158 Builder.CreateBinaryIntrinsic(Intrinsic::minnum, X, Y, &SI);
4159 if (auto *BinIntrInst = dyn_cast<Instruction>(BinIntr)) {
4160 BinIntrInst->setHasNoNaNs(FCmp->hasNoNaNs());
4161 BinIntrInst->setHasNoInfs(FCmp->hasNoInfs());
4162 }
4163 return replaceInstUsesWith(SI, BinIntr);
4164 }
4165 }
4166 }
4167
4168 // Fold selecting to fabs.
4169 if (Instruction *Fabs = foldSelectWithFCmpToFabs(SI, *this))
4170 return Fabs;
4171
4172 // See if we are selecting two values based on a comparison of the two values.
4173 if (CmpInst *CI = dyn_cast<CmpInst>(CondVal))
4174 if (Instruction *NewSel = foldSelectValueEquivalence(SI, *CI))
4175 return NewSel;
4176
4177 if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
4178 if (Instruction *Result = foldSelectInstWithICmp(SI, ICI))
4179 return Result;
4180
4181 if (Value *V = foldSelectBitTest(SI, CondVal, TrueVal, FalseVal, Builder, SQ))
4182 return replaceInstUsesWith(SI, V);
4183
4184 if (Instruction *Add = foldAddSubSelect(SI, Builder))
4185 return Add;
4186 if (Instruction *Add = foldOverflowingAddSubSelect(SI, Builder))
4187 return Add;
4189 return Or;
4190 if (Instruction *Mul = foldSelectZeroOrFixedOp(SI, *this))
4191 return Mul;
4192
4193 // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
4194 auto *TI = dyn_cast<Instruction>(TrueVal);
4195 auto *FI = dyn_cast<Instruction>(FalseVal);
4196 if (TI && FI && TI->getOpcode() == FI->getOpcode())
4197 if (Instruction *IV = foldSelectOpOp(SI, TI, FI))
4198 return IV;
4199
4200 if (Instruction *I = foldSelectExtConst(SI))
4201 return I;
4202
4203 if (Instruction *I = foldSelectWithSRem(SI, *this, Builder))
4204 return I;
4205
4206 // Fold (select C, (gep Ptr, Idx), Ptr) -> (gep Ptr, (select C, Idx, 0))
4207 // Fold (select C, Ptr, (gep Ptr, Idx)) -> (gep Ptr, (select C, 0, Idx))
4208 auto SelectGepWithBase = [&](GetElementPtrInst *Gep, Value *Base,
4209 bool Swap) -> GetElementPtrInst * {
4210 Value *Ptr = Gep->getPointerOperand();
4211 if (Gep->getNumOperands() != 2 || Gep->getPointerOperand() != Base ||
4212 !Gep->hasOneUse())
4213 return nullptr;
4214 Value *Idx = Gep->getOperand(1);
4215 if (isa<VectorType>(CondVal->getType()) && !isa<VectorType>(Idx->getType()))
4216 return nullptr;
4218 Value *NewT = Idx;
4219 Value *NewF = Constant::getNullValue(Idx->getType());
4220 if (Swap)
4221 std::swap(NewT, NewF);
4222 Value *NewSI =
4223 Builder.CreateSelect(CondVal, NewT, NewF, SI.getName() + ".idx", &SI);
4224 return GetElementPtrInst::Create(ElementType, Ptr, NewSI,
4225 Gep->getNoWrapFlags());
4226 };
4227 if (auto *TrueGep = dyn_cast<GetElementPtrInst>(TrueVal))
4228 if (auto *NewGep = SelectGepWithBase(TrueGep, FalseVal, false))
4229 return NewGep;
4230 if (auto *FalseGep = dyn_cast<GetElementPtrInst>(FalseVal))
4231 if (auto *NewGep = SelectGepWithBase(FalseGep, TrueVal, true))
4232 return NewGep;
4233
4234 // See if we can fold the select into one of our operands.
4235 if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) {
4236 if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))
4237 return FoldI;
4238
4239 Value *LHS, *RHS;
4240 Instruction::CastOps CastOp;
4241 SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp);
4242 auto SPF = SPR.Flavor;
4243 if (SPF) {
4244 Value *LHS2, *RHS2;
4245 if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor)
4246 if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS), SPF2, LHS2,
4247 RHS2, SI, SPF, RHS))
4248 return R;
4249 if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor)
4250 if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS), SPF2, LHS2,
4251 RHS2, SI, SPF, LHS))
4252 return R;
4253 }
4254
4256 // Canonicalize so that
4257 // - type casts are outside select patterns.
4258 // - float clamp is transformed to min/max pattern
4259
4260 bool IsCastNeeded = LHS->getType() != SelType;
4261 Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0);
4262 Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1);
4263 if (IsCastNeeded ||
4264 (LHS->getType()->isFPOrFPVectorTy() &&
4265 ((CmpLHS != LHS && CmpLHS != RHS) ||
4266 (CmpRHS != LHS && CmpRHS != RHS)))) {
4267 CmpInst::Predicate MinMaxPred = getMinMaxPred(SPF, SPR.Ordered);
4268
4269 Value *Cmp;
4270 if (CmpInst::isIntPredicate(MinMaxPred))
4271 Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS);
4272 else
4273 Cmp = Builder.CreateFCmpFMF(MinMaxPred, LHS, RHS,
4274 cast<Instruction>(SI.getCondition()));
4275
4276 Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
4277 if (!IsCastNeeded)
4278 return replaceInstUsesWith(SI, NewSI);
4279
4280 Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType);
4281 return replaceInstUsesWith(SI, NewCast);
4282 }
4283 }
4284 }
4285
4286 // See if we can fold the select into a phi node if the condition is a select.
4287 if (auto *PN = dyn_cast<PHINode>(SI.getCondition()))
4288 if (Instruction *NV = foldOpIntoPhi(SI, PN))
4289 return NV;
4290
4291 if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
4292 if (TrueSI->getCondition()->getType() == CondVal->getType()) {
4293 // Fold nested selects if the inner condition can be implied by the outer
4294 // condition.
4295 if (Value *V = simplifyNestedSelectsUsingImpliedCond(
4296 *TrueSI, CondVal, /*CondIsTrue=*/true, DL))
4297 return replaceOperand(SI, 1, V);
4298
4299 // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b)
4300 // We choose this as normal form to enable folding on the And and
4301 // shortening paths for the values (this helps getUnderlyingObjects() for
4302 // example).
4303 if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) {
4304 Value *And = Builder.CreateLogicalAnd(CondVal, TrueSI->getCondition());
4305 replaceOperand(SI, 0, And);
4306 replaceOperand(SI, 1, TrueSI->getTrueValue());
4307 return &SI;
4308 }
4309 }
4310 }
4311 if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
4312 if (FalseSI->getCondition()->getType() == CondVal->getType()) {
4313 // Fold nested selects if the inner condition can be implied by the outer
4314 // condition.
4315 if (Value *V = simplifyNestedSelectsUsingImpliedCond(
4316 *FalseSI, CondVal, /*CondIsTrue=*/false, DL))
4317 return replaceOperand(SI, 2, V);
4318
4319 // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b)
4320 if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) {
4321 Value *Or = Builder.CreateLogicalOr(CondVal, FalseSI->getCondition());
4322 replaceOperand(SI, 0, Or);
4323 replaceOperand(SI, 2, FalseSI->getFalseValue());
4324 return &SI;
4325 }
4326 }
4327 }
4328
4329 // Try to simplify a binop sandwiched between 2 selects with the same
4330 // condition. This is not valid for div/rem because the select might be
4331 // preventing a division-by-zero.
4332 // TODO: A div/rem restriction is conservative; use something like
4333 // isSafeToSpeculativelyExecute().
4334 // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z)
4335 BinaryOperator *TrueBO;
4336 if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) && !TrueBO->isIntDivRem()) {
4337 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) {
4338 if (TrueBOSI->getCondition() == CondVal) {
4339 replaceOperand(*TrueBO, 0, TrueBOSI->getTrueValue());
4340 Worklist.push(TrueBO);
4341 return &SI;
4342 }
4343 }
4344 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) {
4345 if (TrueBOSI->getCondition() == CondVal) {
4346 replaceOperand(*TrueBO, 1, TrueBOSI->getTrueValue());
4347 Worklist.push(TrueBO);
4348 return &SI;
4349 }
4350 }
4351 }
4352
4353 // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W))
4354 BinaryOperator *FalseBO;
4355 if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) && !FalseBO->isIntDivRem()) {
4356 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) {
4357 if (FalseBOSI->getCondition() == CondVal) {
4358 replaceOperand(*FalseBO, 0, FalseBOSI->getFalseValue());
4359 Worklist.push(FalseBO);
4360 return &SI;
4361 }
4362 }
4363 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) {
4364 if (FalseBOSI->getCondition() == CondVal) {
4365 replaceOperand(*FalseBO, 1, FalseBOSI->getFalseValue());
4366 Worklist.push(FalseBO);
4367 return &SI;
4368 }
4369 }
4370 }
4371
4372 Value *NotCond;
4373 if (match(CondVal, m_Not(m_Value(NotCond))) &&
4375 replaceOperand(SI, 0, NotCond);
4376 SI.swapValues();
4377 SI.swapProfMetadata();
4378 return &SI;
4379 }
4380
4381 if (Instruction *I = foldVectorSelect(SI))
4382 return I;
4383
4384 // If we can compute the condition, there's no need for a select.
4385 // Like the above fold, we are attempting to reduce compile-time cost by
4386 // putting this fold here with limitations rather than in InstSimplify.
4387 // The motivation for this call into value tracking is to take advantage of
4388 // the assumption cache, so make sure that is populated.
4389 if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) {
4390 KnownBits Known(1);
4391 computeKnownBits(CondVal, Known, &SI);
4392 if (Known.One.isOne())
4393 return replaceInstUsesWith(SI, TrueVal);
4394 if (Known.Zero.isOne())
4395 return replaceInstUsesWith(SI, FalseVal);
4396 }
4397
4398 if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder))
4399 return BitCastSel;
4400
4401 // Simplify selects that test the returned flag of cmpxchg instructions.
4402 if (Value *V = foldSelectCmpXchg(SI))
4403 return replaceInstUsesWith(SI, V);
4404
4405 if (Instruction *Select = foldSelectBinOpIdentity(SI, TLI, *this))
4406 return Select;
4407
4408 if (Instruction *Funnel = foldSelectFunnelShift(SI, Builder))
4409 return Funnel;
4410
4411 if (Instruction *Copysign = foldSelectToCopysign(SI, Builder))
4412 return Copysign;
4413
4414 if (Instruction *PN = foldSelectToPhi(SI, DT, Builder))
4415 return replaceInstUsesWith(SI, PN);
4416
4417 if (Value *Fr = foldSelectWithFrozenICmp(SI, Builder))
4418 return replaceInstUsesWith(SI, Fr);
4419
4420 if (Value *V = foldRoundUpIntegerWithPow2Alignment(SI, Builder))
4421 return replaceInstUsesWith(SI, V);
4422
4423 if (Value *V = foldSelectIntoAddConstant(SI, Builder))
4424 return replaceInstUsesWith(SI, V);
4425
4426 // select(mask, mload(,,mask,0), 0) -> mload(,,mask,0)
4427 // Load inst is intentionally not checked for hasOneUse()
4428 if (match(FalseVal, m_Zero()) &&
4429 (match(TrueVal, m_MaskedLoad(m_Value(), m_Value(), m_Specific(CondVal),
4430 m_CombineOr(m_Undef(), m_Zero()))) ||
4431 match(TrueVal, m_MaskedGather(m_Value(), m_Value(), m_Specific(CondVal),
4432 m_CombineOr(m_Undef(), m_Zero()))))) {
4433 auto *MaskedInst = cast<IntrinsicInst>(TrueVal);
4434 if (isa<UndefValue>(MaskedInst->getArgOperand(3)))
4435 MaskedInst->setArgOperand(3, FalseVal /* Zero */);
4436 return replaceInstUsesWith(SI, MaskedInst);
4437 }
4438
4439 Value *Mask;
4440 if (match(TrueVal, m_Zero()) &&
4441 (match(FalseVal, m_MaskedLoad(m_Value(), m_Value(), m_Value(Mask),
4442 m_CombineOr(m_Undef(), m_Zero()))) ||
4443 match(FalseVal, m_MaskedGather(m_Value(), m_Value(), m_Value(Mask),
4444 m_CombineOr(m_Undef(), m_Zero())))) &&
4445 (CondVal->getType() == Mask->getType())) {
4446 // We can remove the select by ensuring the load zeros all lanes the
4447 // select would have. We determine this by proving there is no overlap
4448 // between the load and select masks.
4449 // (i.e (load_mask & select_mask) == 0 == no overlap)
4450 bool CanMergeSelectIntoLoad = false;
4451 if (Value *V = simplifyAndInst(CondVal, Mask, SQ.getWithInstruction(&SI)))
4452 CanMergeSelectIntoLoad = match(V, m_Zero());
4453
4454 if (CanMergeSelectIntoLoad) {
4455 auto *MaskedInst = cast<IntrinsicInst>(FalseVal);
4456 if (isa<UndefValue>(MaskedInst->getArgOperand(3)))
4457 MaskedInst->setArgOperand(3, TrueVal /* Zero */);
4458 return replaceInstUsesWith(SI, MaskedInst);
4459 }
4460 }
4461
4462 if (Instruction *I = foldSelectOfSymmetricSelect(SI, Builder))
4463 return I;
4464
4465 if (Instruction *I = foldNestedSelects(SI, Builder))
4466 return I;
4467
4468 // Match logical variants of the pattern,
4469 // and transform them iff that gets rid of inversions.
4470 // (~x) | y --> ~(x & (~y))
4471 // (~x) & y --> ~(x | (~y))
4473 return &SI;
4474
4475 if (Instruction *I = foldBitCeil(SI, Builder, *this))
4476 return I;
4477
4478 if (Instruction *I = foldSelectToCmp(SI))
4479 return I;
4480
4482 return I;
4483
4484 // Fold:
4485 // (select A && B, T, F) -> (select A, (select B, T, F), F)
4486 // (select A || B, T, F) -> (select A, T, (select B, T, F))
4487 // if (select B, T, F) is foldable.
4488 // TODO: preserve FMF flags
4489 auto FoldSelectWithAndOrCond = [&](bool IsAnd, Value *A,
4490 Value *B) -> Instruction * {
4491 if (Value *V = simplifySelectInst(B, TrueVal, FalseVal,
4492 SQ.getWithInstruction(&SI)))
4493 return SelectInst::Create(A, IsAnd ? V : TrueVal, IsAnd ? FalseVal : V);
4494
4495 // Is (select B, T, F) a SPF?
4496 if (CondVal->hasOneUse() && SelType->isIntOrIntVectorTy()) {
4497 if (ICmpInst *Cmp = dyn_cast<ICmpInst>(B))
4498 if (Value *V = canonicalizeSPF(*Cmp, TrueVal, FalseVal, *this))
4499 return SelectInst::Create(A, IsAnd ? V : TrueVal,
4500 IsAnd ? FalseVal : V);
4501 }
4502
4503 return nullptr;
4504 };
4505
4506 Value *LHS, *RHS;
4507 if (match(CondVal, m_And(m_Value(LHS), m_Value(RHS)))) {
4508 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ true, LHS, RHS))
4509 return I;
4510 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ true, RHS, LHS))
4511 return I;
4512 } else if (match(CondVal, m_Or(m_Value(LHS), m_Value(RHS)))) {
4513 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ false, LHS, RHS))
4514 return I;
4515 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ false, RHS, LHS))
4516 return I;
4517 } else {
4518 // We cannot swap the operands of logical and/or.
4519 // TODO: Can we swap the operands by inserting a freeze?
4520 if (match(CondVal, m_LogicalAnd(m_Value(LHS), m_Value(RHS)))) {
4521 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ true, LHS, RHS))
4522 return I;
4523 } else if (match(CondVal, m_LogicalOr(m_Value(LHS), m_Value(RHS)))) {
4524 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ false, LHS, RHS))
4525 return I;
4526 }
4527 }
4528
4529 // select Cond, !X, X -> xor Cond, X
4530 if (CondVal->getType() == SI.getType() && isKnownInversion(FalseVal, TrueVal))
4531 return BinaryOperator::CreateXor(CondVal, FalseVal);
4532
4533 // For vectors, this transform is only safe if the simplification does not
4534 // look through any lane-crossing operations. For now, limit to scalars only.
4535 if (SelType->isIntegerTy() &&
4536 (!isa<Constant>(TrueVal) || !isa<Constant>(FalseVal))) {
4537 // Try to simplify select arms based on KnownBits implied by the condition.
4538 CondContext CC(CondVal);
4539 findValuesAffectedByCondition(CondVal, /*IsAssume=*/false, [&](Value *V) {
4540 CC.AffectedValues.insert(V);
4541 });
4543 if (!CC.AffectedValues.empty()) {
4544 if (!isa<Constant>(TrueVal) &&
4545 hasAffectedValue(TrueVal, CC.AffectedValues, /*Depth=*/0)) {
4546 KnownBits Known = llvm::computeKnownBits(TrueVal, Q);
4547 if (Known.isConstant())
4548 return replaceOperand(SI, 1,
4549 ConstantInt::get(SelType, Known.getConstant()));
4550 }
4551
4552 CC.Invert = true;
4553 if (!isa<Constant>(FalseVal) &&
4554 hasAffectedValue(FalseVal, CC.AffectedValues, /*Depth=*/0)) {
4555 KnownBits Known = llvm::computeKnownBits(FalseVal, Q);
4556 if (Known.isConstant())
4557 return replaceOperand(SI, 2,
4558 ConstantInt::get(SelType, Known.getConstant()));
4559 }
4560 }
4561 }
4562
4563 // select (trunc nuw X to i1), X, Y --> select (trunc nuw X to i1), 1, Y
4564 // select (trunc nuw X to i1), Y, X --> select (trunc nuw X to i1), Y, 0
4565 // select (trunc nsw X to i1), X, Y --> select (trunc nsw X to i1), -1, Y
4566 // select (trunc nsw X to i1), Y, X --> select (trunc nsw X to i1), Y, 0
4567 Value *Trunc;
4568 if (match(CondVal, m_NUWTrunc(m_Value(Trunc)))) {
4569 if (TrueVal == Trunc)
4570 return replaceOperand(SI, 1, ConstantInt::get(TrueVal->getType(), 1));
4571 if (FalseVal == Trunc)
4572 return replaceOperand(SI, 2, ConstantInt::get(FalseVal->getType(), 0));
4573 }
4574 if (match(CondVal, m_NSWTrunc(m_Value(Trunc)))) {
4575 if (TrueVal == Trunc)
4576 return replaceOperand(SI, 1,
4578 if (FalseVal == Trunc)
4579 return replaceOperand(SI, 2, ConstantInt::get(FalseVal->getType(), 0));
4580 }
4581
4582 return nullptr;
4583}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const HexagonInstrInfo * TII
This file provides internal interfaces used to implement the InstCombine.
static Value * foldSelectICmpMinMax(const ICmpInst *Cmp, Value *TVal, Value *FVal, InstCombiner::BuilderTy &Builder, const SimplifyQuery &SQ)
Try to fold a select to a min/max intrinsic.
static Value * canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal, InstCombiner::BuilderTy &Builder)
static Instruction * foldSetClearBits(SelectInst &Sel, InstCombiner::BuilderTy &Builder)
Canonicalize a set or clear of a masked set of constant bits to select-of-constants form.
static Instruction * foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp, Value *TVal, Value *FVal, InstCombiner::BuilderTy &Builder)
We want to turn: (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1) into: zext (icmp ne i32 (a...
static unsigned getSelectFoldableOperands(BinaryOperator *I)
We want to turn code that looks like this: C = or A, B D = select cond, C, A into: C = select cond,...
static Value * canonicalizeSaturatedSubtract(const ICmpInst *ICI, const Value *TrueVal, const Value *FalseVal, InstCombiner::BuilderTy &Builder)
Transform patterns such as (a > b) ? a - b : 0 into usub.sat(a, b).
static Value * foldAbsDiff(ICmpInst *Cmp, Value *TVal, Value *FVal, InstCombiner::BuilderTy &Builder)
Try to match patterns with select and subtract as absolute difference.
static Instruction * foldSelectZeroOrFixedOp(SelectInst &SI, InstCombinerImpl &IC)
static Instruction * foldSelectBinOpIdentity(SelectInst &Sel, const TargetLibraryInfo &TLI, InstCombinerImpl &IC)
Replace a select operand based on an equality comparison with the identity constant of a binop.
static Value * foldSelectICmpAnd(SelectInst &Sel, Value *CondVal, Value *TrueVal, Value *FalseVal, Value *V, const APInt &AndMask, bool CreateAnd, InstCombiner::BuilderTy &Builder)
This folds: select (icmp eq (and X, C1)), TC, FC iff C1 is a power 2 and the difference between TC an...
static Value * foldSelectICmpAndZeroShl(const ICmpInst *Cmp, Value *TVal, Value *FVal, InstCombiner::BuilderTy &Builder)
We want to turn: (select (icmp eq (and X, C1), 0), 0, (shl [nsw/nuw] X, C2)); iff C1 is a mask and th...
static Value * foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal, Value *FalseVal, InstCombiner::BuilderTy &Builder)
We want to turn: (select (icmp sgt x, C), lshr (X, Y), ashr (X, Y)); iff C s>= -1 (select (icmp slt x...
static bool isSelect01(const APInt &C1I, const APInt &C2I)
static Value * foldSelectICmpAndBinOp(Value *CondVal, Value *TrueVal, Value *FalseVal, Value *V, const APInt &AndMask, bool CreateAnd, InstCombiner::BuilderTy &Builder)
We want to turn: (select (icmp eq (and X, C1), 0), Y, (BinOp Y, C2)) into: IF C2 u>= C1 (BinOp Y,...
This file provides the interface for the instcombine pass implementation.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition: blake3_impl.h:83
bool bitwiseIsEqual(const APFloat &RHS) const
Definition: APFloat.h:1414
bool isNegative() const
Definition: APFloat.h:1449
Class for arbitrary precision integers.
Definition: APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:229
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:423
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1540
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:417
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
unsigned countLeadingZeros() const
Definition: APInt.h:1606
unsigned logBase2() const
Definition: APInt.h:1761
bool isMask(unsigned numBits) const
Definition: APInt.h:488
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:405
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:440
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:389
bool isMaxValue() const
Determine if this is the largest unsigned value.
Definition: APInt.h:399
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
MutableArrayRef< ResultElem > assumptions()
Access the list of assumption handles currently tracked for this function.
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:506
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:459
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:233
BinaryOps getOpcode() const
Definition: InstrTypes.h:374
static LLVM_ABI BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
This class represents a no-op cast from one type to another.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:681
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:692
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:686
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:689
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:685
@ ICMP_EQ
equal
Definition: InstrTypes.h:699
@ ICMP_NE
not equal
Definition: InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:706
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:694
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:691
bool isSigned() const
Definition: InstrTypes.h:932
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:829
bool isNonStrictPredicate() const
Definition: InstrTypes.h:854
bool isFPPredicate() const
Definition: InstrTypes.h:784
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:791
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:767
static LLVM_ABI bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:895
bool isIntPredicate() const
Definition: InstrTypes.h:785
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition: InstrTypes.h:938
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition: InstrTypes.h:928
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:23
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2654
static LLVM_ABI Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
Definition: Constants.cpp:2694
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2635
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:868
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:875
This class represents a range of values.
Definition: ConstantRange.h:47
LLVM_ABI ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
static LLVM_ABI ConstantRange intrinsic(Intrinsic::ID IntrinsicID, ArrayRef< ConstantRange > Ops)
Compute range of intrinsic result for the given operand ranges.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI ConstantRange binaryNot() const
Return a new range representing the possible values resulting from a binary-xor of a value in this ra...
LLVM_ABI ConstantRange binaryOp(Instruction::BinaryOps BinOp, const ConstantRange &Other) const
Return a new range representing the possible values resulting from an application of the specified bi...
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition: Constant.h:43
static LLVM_ABI Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
Definition: Constants.cpp:808
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
LLVM_ABI bool isOneValue() const
Returns true if the value is one.
Definition: Constants.cpp:124
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
unsigned size() const
Definition: DenseMap.h:108
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:135
Tagged union holding either a T or a Error.
Definition: Error.h:485
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:200
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags.
Definition: Operator.h:333
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:22
static FastMathFlags intersectRewrite(FastMathFlags LHS, FastMathFlags RHS)
Intersect rewrite-based flags.
Definition: FMF.h:112
bool noSignedZeros() const
Definition: FMF.h:67
bool noInfs() const
Definition: FMF.h:66
static FastMathFlags unionValue(FastMathFlags LHS, FastMathFlags RHS)
Union value flags.
Definition: FMF.h:120
void setNoSignedZeros(bool B=true)
Definition: FMF.h:84
void setNoNaNs(bool B=true)
Definition: FMF.h:78
bool noNaNs() const
Definition: FMF.h:65
void setNoInfs(bool B=true)
Definition: FMF.h:81
This class represents a freeze function that returns random concrete value if an operand is either a ...
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:949
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Definition: Instructions.h:973
Type * getSourceElementType() const
LLVM_ABI GEPNoWrapFlags getNoWrapFlags() const
Get the nowrap flags for the GEP instruction.
uint64_t getType(const MachineInstr &MI) const
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
static bool isLT(Predicate P)
Return true if the predicate is SLT or ULT.
CmpPredicate getInverseCmpPredicate() const
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:114
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:2100
Value * CreateFAdd(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1613
LLVM_ABI Value * CreateSelectFMF(Value *C, Value *True, Value *False, FMFSource FMFSource, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1010
LLVM_ABI Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1115
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:502
Value * CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2357
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1005
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition: IRBuilder.h:2637
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1513
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Definition: IRBuilder.h:2238
Value * CreateFCmpFMF(CmpInst::Predicate P, Value *LHS, Value *RHS, FMFSource FMFSource, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2457
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1781
LLVM_ABI Value * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type.
Definition: IRBuilder.cpp:823
LLVM_ABI CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:834
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2494
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1805
Value * CreateIsNeg(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg < 0.
Definition: IRBuilder.h:2656
LLVM_ABI CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition: IRBuilder.cpp:815
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1492
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2082
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1551
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1403
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:507
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition: IRBuilder.h:2651
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2068
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1708
Value * CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1725
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2361
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:207
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1532
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1599
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2439
Value * CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1731
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1790
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition: IRBuilder.h:1573
Instruction * foldSelectToCmp(SelectInst &SI)
bool fmulByZeroIsZero(Value *MulVal, FastMathFlags FMF, const Instruction *CtxI) const
Check if fmul MulVal, +0.0 will yield +0.0 (or signed zero is ignorable).
KnownFPClass computeKnownFPClass(Value *Val, FastMathFlags FMF, FPClassTest Interested=fcAllFlags, const Instruction *CtxI=nullptr, unsigned Depth=0) const
Instruction * foldSelectEqualityTest(SelectInst &SI)
Instruction * foldSelectValueEquivalence(SelectInst &SI, CmpInst &CI)
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN, bool AllowMultipleUses=false)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
Instruction * foldVectorSelect(SelectInst &Sel)
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &PoisonElts, unsigned Depth=0, bool AllowMultipleUsers=false) override
The specified value produces a vector with any number of elements.
Instruction * foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, Value *A, Value *B, Instruction &Outer, SelectPatternFlavor SPF2, Value *C)
Instruction * foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI)
We have (select c, TI, FI), and we know that TI and FI have the same opcode.
bool replaceInInstruction(Value *V, Value *Old, Value *New, unsigned Depth=0)
Instruction * foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI)
bool sinkNotIntoOtherHandOfLogicalOp(Instruction &I)
Constant * getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp)
Instruction * foldSelectIntoOp(SelectInst &SI, Value *, Value *)
Try to fold the select into one of the operands to allow further optimization.
Value * foldSelectWithConstOpToBinOp(ICmpInst *Cmp, Value *TrueVal, Value *FalseVal)
Instruction * visitSelectInst(SelectInst &SI)
Instruction * foldSelectOfBools(SelectInst &SI)
Instruction * foldSelectExtConst(SelectInst &Sel)
The core instruction combiner logic.
Definition: InstCombiner.h:48
SimplifyQuery SQ
Definition: InstCombiner.h:77
const DataLayout & getDataLayout() const
Definition: InstCombiner.h:337
TargetLibraryInfo & TLI
Definition: InstCombiner.h:74
Instruction * InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old)
Inserts an instruction New before instruction Old.
Definition: InstCombiner.h:368
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:388
static bool shouldAvoidAbsorbingNotIntoSelect(const SelectInst &SI)
Definition: InstCombiner.h:187
void replaceUse(Use &U, Value *NewValue)
Replace use and add the previously used value to the worklist.
Definition: InstCombiner.h:420
static bool isCanonicalPredicate(CmpPredicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
Definition: InstCombiner.h:160
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Definition: InstCombiner.h:65
const DataLayout & DL
Definition: InstCombiner.h:76
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CxtI, unsigned Depth=0) const
Definition: InstCombiner.h:433
AssumptionCache & AC
Definition: InstCombiner.h:73
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:332
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:412
DominatorTree & DT
Definition: InstCombiner.h:75
BuilderTy & Builder
Definition: InstCombiner.h:61
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:209
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:338
static Constant * AddOne(Constant *C)
Add one to a Constant.
Definition: InstCombiner.h:178
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, const Instruction *CxtI=nullptr, unsigned Depth=0)
Definition: InstCombiner.h:443
void add(Instruction *I)
Add instruction to the worklist.
void push(Instruction *I)
Push the instruction onto the worklist stack.
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoInfs() const LLVM_READONLY
Determine whether the no-infs flag is set.
LLVM_ABI bool isSameOperationAs(const Instruction *I, unsigned flags=0) const LLVM_READONLY
This function determines if the specified instruction executes the same operation as the current one.
bool isCast() const
Definition: Instruction.h:321
LLVM_ABI void setHasNoSignedZeros(bool B)
Set or clear the no-signed-zeros flag on this instruction, which must be an operator which supports t...
LLVM_ABI bool hasNoSignedZeros() const LLVM_READONLY
Determine whether the no-signed-zeros flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
LLVM_ABI void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:78
LLVM_ABI void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction.
LLVM_ABI void setHasNoNaNs(bool B)
Set or clear the no-nans flag on this instruction, which must be an operator which supports this flag...
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
LLVM_ABI void swapProfMetadata()
If the instruction has "branch_weights" MD_prof metadata and the MDNode has three operands (including...
LLVM_ABI void setHasNoInfs(bool B)
Set or clear the no-infs flag on this instruction, which must be an operator which supports this flag...
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:312
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition: Instruction.cpp:86
bool isIntDivRem() const
Definition: Instruction.h:318
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
const Value * getFalseValue() const
void swapValues()
Swap the true and false values of the select instruction.
const Value * getCondition() const
const Value * getTrueValue() const
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:168
This instruction constructs a fixed permutation of two input vectors.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:380
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:476
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:356
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:246
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:270
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:240
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:225
static UnaryOperator * CreateFNegFMF(Value *Op, Instruction *FMFSource, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Definition: InstrTypes.h:147
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
op_range operands()
Definition: User.h:292
Value * getOperand(unsigned i) const
Definition: User.h:232
unsigned getNumOperands() const
Definition: User.h:254
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
LLVM_ABI const Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) const
Translate PHI node to its predecessor from the given basic block.
Definition: Value.cpp:1093
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:439
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:396
Represents an op.with.overflow intrinsic.
This class represents zero extension of integer types.
const ParentTy * getParent() const
Definition: ilist_node.h:34
self_iterator getIterator()
Definition: ilist_node.h:134
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:126
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
int getMinValue(MCInstrInfo const &MCII, MCInst const &MCI)
Return the minimum value of an extendable operand.
int getMaxValue(MCInstrInfo const &MCII, MCInst const &MCI)
Return the maximum value of an extendable operand.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:751
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< cst_pred_ty< is_all_ones, false >, ValTy, Instruction::Xor, true > m_NotForbidPoison(const ValTy &V)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:664
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:619
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:862
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:766
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:962
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
Definition: PatternMatch.h:186
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
cst_pred_ty< is_any_apint > m_AnyIntegralConstant()
Match an integer or vector with any integral constant.
Definition: PatternMatch.h:507
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition: PatternMatch.h:876
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::FAdd > m_FAdd(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:980
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
NoWrapTrunc_match< OpTy, TruncInst::NoSignedWrap > m_NSWTrunc(const OpTy &Op)
Matches trunc nsw.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedLoad Intrinsic.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
apint_match m_APIntForbidPoison(const APInt *&Res)
Match APInt while forbidding poison in splat vector constants.
Definition: PatternMatch.h:310
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:105
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:931
auto m_c_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R with LHS and RHS in either order.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition: PatternMatch.h:322
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
Definition: PatternMatch.h:775
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition: PatternMatch.h:189
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, ICmpInst, true > m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedGather Intrinsic.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
Definition: PatternMatch.h:700
ElementType
The element type of an SRV or UAV resource.
Definition: DXILABI.h:59
DiagnosticInfoOptimizationBase::Argument NV
@ FalseVal
Definition: TGLexer.h:59
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI Constant * ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, Constant *RHS, Type *Ty, Instruction *FMFSource)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1563
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
LLVM_ABI bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:47
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:288
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1758
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI Value * simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
@ Mul
Product of integers.
@ Xor
Bitwise or logical XOR of integers.
@ Add
Sum of integers.
@ FAdd
Sum of floats.
DWARFExpression::Operation Op
bool isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I, bool IgnoreUBImplyingAttrs=true)
Don't use information from its non-constant operands.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:223
LLVM_ABI Value * simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, const SimplifyQuery &Q, bool AllowRefinement, SmallVectorImpl< Instruction * > *DropFlags=nullptr)
See if V simplifies when its operand Op is replaced with RepOp.
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
auto predecessors(const MachineBasicBlock *BB)
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
bool isCheckForZeroAndMulWithOverflow(Value *Op0, Value *Op1, bool IsAnd, Use *&Y)
Match one of the patterns up to the select/logic op: Op0 = icmp ne i4 X, 0 Agg = call { i4,...
LLVM_ABI Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false, bool DecomposeAnd=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:853
Evaluate query assuming this condition holds.
Definition: SimplifyQuery.h:63
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:54
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:138
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:60
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
Definition: KnownFPClass.h:51
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
Definition: KnownFPClass.h:45
bool signBitIsZeroOrNaN() const
Return true if the sign bit must be 0, ignoring the sign of nans.
Definition: KnownFPClass.h:159
Matching combinators.
SelectPatternFlavor Flavor
bool Ordered
Only applicable if Flavor is SPF_FMINNUM or SPF_FMAXNUM.
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
SimplifyQuery getWithCondContext(const CondContext &CC) const
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
Definition: SimplifyQuery.h:75