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