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