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