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