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