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