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