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