LLVM 19.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"
23#include "llvm/IR/BasicBlock.h"
24#include "llvm/IR/Constant.h"
26#include "llvm/IR/Constants.h"
28#include "llvm/IR/IRBuilder.h"
29#include "llvm/IR/InstrTypes.h"
30#include "llvm/IR/Instruction.h"
33#include "llvm/IR/Intrinsics.h"
34#include "llvm/IR/Operator.h"
36#include "llvm/IR/Type.h"
37#include "llvm/IR/User.h"
38#include "llvm/IR/Value.h"
43#include <cassert>
44#include <utility>
45
46#define DEBUG_TYPE "instcombine"
48
49using namespace llvm;
50using namespace PatternMatch;
51
52
53/// Replace a select operand based on an equality comparison with the identity
54/// constant of a binop.
56 const TargetLibraryInfo &TLI,
57 InstCombinerImpl &IC) {
58 // The select condition must be an equality compare with a constant operand.
59 Value *X;
60 Constant *C;
62 if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C))))
63 return nullptr;
64
65 bool IsEq;
66 if (ICmpInst::isEquality(Pred))
67 IsEq = Pred == ICmpInst::ICMP_EQ;
68 else if (Pred == FCmpInst::FCMP_OEQ)
69 IsEq = true;
70 else if (Pred == FCmpInst::FCMP_UNE)
71 IsEq = false;
72 else
73 return nullptr;
74
75 // A select operand must be a binop.
77 if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO)))
78 return nullptr;
79
80 // The compare constant must be the identity constant for that binop.
81 // If this a floating-point compare with 0.0, any zero constant will do.
82 Type *Ty = BO->getType();
84 if (IdC != C) {
85 if (!IdC || !CmpInst::isFPPredicate(Pred))
86 return nullptr;
87 if (!match(IdC, m_AnyZeroFP()) || !match(C, m_AnyZeroFP()))
88 return nullptr;
89 }
90
91 // Last, match the compare variable operand with a binop operand.
92 Value *Y;
93 if (!BO->isCommutative() && !match(BO, m_BinOp(m_Value(Y), m_Specific(X))))
94 return nullptr;
95 if (!match(BO, m_c_BinOp(m_Value(Y), m_Specific(X))))
96 return nullptr;
97
98 // +0.0 compares equal to -0.0, and so it does not behave as required for this
99 // transform. Bail out if we can not exclude that possibility.
100 if (isa<FPMathOperator>(BO))
101 if (!BO->hasNoSignedZeros() &&
104 return nullptr;
105
106 // BO = binop Y, X
107 // S = { select (cmp eq X, C), BO, ? } or { select (cmp ne X, C), ?, BO }
108 // =>
109 // S = { select (cmp eq X, C), Y, ? } or { select (cmp ne X, C), ?, Y }
110 return IC.replaceOperand(Sel, IsEq ? 1 : 2, Y);
111}
112
113/// This folds:
114/// select (icmp eq (and X, C1)), TC, FC
115/// iff C1 is a power 2 and the difference between TC and FC is a power-of-2.
116/// To something like:
117/// (shr (and (X, C1)), (log2(C1) - log2(TC-FC))) + FC
118/// Or:
119/// (shl (and (X, C1)), (log2(TC-FC) - log2(C1))) + FC
120/// With some variations depending if FC is larger than TC, or the shift
121/// isn't needed, or the bit widths don't match.
123 InstCombiner::BuilderTy &Builder) {
124 const APInt *SelTC, *SelFC;
125 if (!match(Sel.getTrueValue(), m_APInt(SelTC)) ||
126 !match(Sel.getFalseValue(), m_APInt(SelFC)))
127 return nullptr;
128
129 // If this is a vector select, we need a vector compare.
130 Type *SelType = Sel.getType();
131 if (SelType->isVectorTy() != Cmp->getType()->isVectorTy())
132 return nullptr;
133
134 Value *V;
135 APInt AndMask;
136 bool CreateAnd = false;
137 ICmpInst::Predicate Pred = Cmp->getPredicate();
138 if (ICmpInst::isEquality(Pred)) {
139 if (!match(Cmp->getOperand(1), m_Zero()))
140 return nullptr;
141
142 V = Cmp->getOperand(0);
143 const APInt *AndRHS;
144 if (!match(V, m_And(m_Value(), m_Power2(AndRHS))))
145 return nullptr;
146
147 AndMask = *AndRHS;
148 } else if (decomposeBitTestICmp(Cmp->getOperand(0), Cmp->getOperand(1),
149 Pred, V, AndMask)) {
150 assert(ICmpInst::isEquality(Pred) && "Not equality test?");
151 if (!AndMask.isPowerOf2())
152 return nullptr;
153
154 CreateAnd = true;
155 } else {
156 return nullptr;
157 }
158
159 // In general, when both constants are non-zero, we would need an offset to
160 // replace the select. This would require more instructions than we started
161 // with. But there's one special-case that we handle here because it can
162 // simplify/reduce the instructions.
163 APInt TC = *SelTC;
164 APInt FC = *SelFC;
165 if (!TC.isZero() && !FC.isZero()) {
166 // If the select constants differ by exactly one bit and that's the same
167 // bit that is masked and checked by the select condition, the select can
168 // be replaced by bitwise logic to set/clear one bit of the constant result.
169 if (TC.getBitWidth() != AndMask.getBitWidth() || (TC ^ FC) != AndMask)
170 return nullptr;
171 if (CreateAnd) {
172 // If we have to create an 'and', then we must kill the cmp to not
173 // increase the instruction count.
174 if (!Cmp->hasOneUse())
175 return nullptr;
176 V = Builder.CreateAnd(V, ConstantInt::get(SelType, AndMask));
177 }
178 bool ExtraBitInTC = TC.ugt(FC);
179 if (Pred == ICmpInst::ICMP_EQ) {
180 // If the masked bit in V is clear, clear or set the bit in the result:
181 // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) ^ TC
182 // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) | TC
183 Constant *C = ConstantInt::get(SelType, TC);
184 return ExtraBitInTC ? Builder.CreateXor(V, C) : Builder.CreateOr(V, C);
185 }
186 if (Pred == ICmpInst::ICMP_NE) {
187 // If the masked bit in V is set, set or clear the bit in the result:
188 // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) | FC
189 // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) ^ FC
190 Constant *C = ConstantInt::get(SelType, FC);
191 return ExtraBitInTC ? Builder.CreateOr(V, C) : Builder.CreateXor(V, C);
192 }
193 llvm_unreachable("Only expecting equality predicates");
194 }
195
196 // Make sure one of the select arms is a power-of-2.
197 if (!TC.isPowerOf2() && !FC.isPowerOf2())
198 return nullptr;
199
200 // Determine which shift is needed to transform result of the 'and' into the
201 // desired result.
202 const APInt &ValC = !TC.isZero() ? TC : FC;
203 unsigned ValZeros = ValC.logBase2();
204 unsigned AndZeros = AndMask.logBase2();
205
206 // Insert the 'and' instruction on the input to the truncate.
207 if (CreateAnd)
208 V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask));
209
210 // If types don't match, we can still convert the select by introducing a zext
211 // or a trunc of the 'and'.
212 if (ValZeros > AndZeros) {
213 V = Builder.CreateZExtOrTrunc(V, SelType);
214 V = Builder.CreateShl(V, ValZeros - AndZeros);
215 } else if (ValZeros < AndZeros) {
216 V = Builder.CreateLShr(V, AndZeros - ValZeros);
217 V = Builder.CreateZExtOrTrunc(V, SelType);
218 } else {
219 V = Builder.CreateZExtOrTrunc(V, SelType);
220 }
221
222 // Okay, now we know that everything is set up, we just don't know whether we
223 // have a icmp_ne or icmp_eq and whether the true or false val is the zero.
224 bool ShouldNotVal = !TC.isZero();
225 ShouldNotVal ^= Pred == ICmpInst::ICMP_NE;
226 if (ShouldNotVal)
227 V = Builder.CreateXor(V, ValC);
228
229 return V;
230}
231
232/// We want to turn code that looks like this:
233/// %C = or %A, %B
234/// %D = select %cond, %C, %A
235/// into:
236/// %C = select %cond, %B, 0
237/// %D = or %A, %C
238///
239/// Assuming that the specified instruction is an operand to the select, return
240/// a bitmask indicating which operands of this instruction are foldable if they
241/// equal the other incoming value of the select.
243 switch (I->getOpcode()) {
244 case Instruction::Add:
245 case Instruction::FAdd:
246 case Instruction::Mul:
247 case Instruction::FMul:
248 case Instruction::And:
249 case Instruction::Or:
250 case Instruction::Xor:
251 return 3; // Can fold through either operand.
252 case Instruction::Sub: // Can only fold on the amount subtracted.
253 case Instruction::FSub:
254 case Instruction::FDiv: // Can only fold on the divisor amount.
255 case Instruction::Shl: // Can only fold on the shift amount.
256 case Instruction::LShr:
257 case Instruction::AShr:
258 return 1;
259 default:
260 return 0; // Cannot fold
261 }
262}
263
264/// We have (select c, TI, FI), and we know that TI and FI have the same opcode.
266 Instruction *FI) {
267 // Don't break up min/max patterns. The hasOneUse checks below prevent that
268 // for most cases, but vector min/max with bitcasts can be transformed. If the
269 // one-use restrictions are eased for other patterns, we still don't want to
270 // obfuscate min/max.
271 if ((match(&SI, m_SMin(m_Value(), m_Value())) ||
272 match(&SI, m_SMax(m_Value(), m_Value())) ||
273 match(&SI, m_UMin(m_Value(), m_Value())) ||
274 match(&SI, m_UMax(m_Value(), m_Value()))))
275 return nullptr;
276
277 // If this is a cast from the same type, merge.
278 Value *Cond = SI.getCondition();
279 Type *CondTy = Cond->getType();
280 if (TI->getNumOperands() == 1 && TI->isCast()) {
281 Type *FIOpndTy = FI->getOperand(0)->getType();
282 if (TI->getOperand(0)->getType() != FIOpndTy)
283 return nullptr;
284
285 // The select condition may be a vector. We may only change the operand
286 // type if the vector width remains the same (and matches the condition).
287 if (auto *CondVTy = dyn_cast<VectorType>(CondTy)) {
288 if (!FIOpndTy->isVectorTy() ||
289 CondVTy->getElementCount() !=
290 cast<VectorType>(FIOpndTy)->getElementCount())
291 return nullptr;
292
293 // TODO: If the backend knew how to deal with casts better, we could
294 // remove this limitation. For now, there's too much potential to create
295 // worse codegen by promoting the select ahead of size-altering casts
296 // (PR28160).
297 //
298 // Note that ValueTracking's matchSelectPattern() looks through casts
299 // without checking 'hasOneUse' when it matches min/max patterns, so this
300 // transform may end up happening anyway.
301 if (TI->getOpcode() != Instruction::BitCast &&
302 (!TI->hasOneUse() || !FI->hasOneUse()))
303 return nullptr;
304 } else if (!TI->hasOneUse() || !FI->hasOneUse()) {
305 // TODO: The one-use restrictions for a scalar select could be eased if
306 // the fold of a select in visitLoadInst() was enhanced to match a pattern
307 // that includes a cast.
308 return nullptr;
309 }
310
311 // Fold this by inserting a select from the input values.
312 Value *NewSI =
314 SI.getName() + ".v", &SI);
316 TI->getType());
317 }
318
319 Value *OtherOpT, *OtherOpF;
320 bool MatchIsOpZero;
321 auto getCommonOp = [&](Instruction *TI, Instruction *FI, bool Commute,
322 bool Swapped = false) -> Value * {
323 assert(!(Commute && Swapped) &&
324 "Commute and Swapped can't set at the same time");
325 if (!Swapped) {
326 if (TI->getOperand(0) == FI->getOperand(0)) {
327 OtherOpT = TI->getOperand(1);
328 OtherOpF = FI->getOperand(1);
329 MatchIsOpZero = true;
330 return TI->getOperand(0);
331 } else if (TI->getOperand(1) == FI->getOperand(1)) {
332 OtherOpT = TI->getOperand(0);
333 OtherOpF = FI->getOperand(0);
334 MatchIsOpZero = false;
335 return TI->getOperand(1);
336 }
337 }
338
339 if (!Commute && !Swapped)
340 return nullptr;
341
342 // If we are allowing commute or swap of operands, then
343 // allow a cross-operand match. In that case, MatchIsOpZero
344 // means that TI's operand 0 (FI's operand 1) is the common op.
345 if (TI->getOperand(0) == FI->getOperand(1)) {
346 OtherOpT = TI->getOperand(1);
347 OtherOpF = FI->getOperand(0);
348 MatchIsOpZero = true;
349 return TI->getOperand(0);
350 } else if (TI->getOperand(1) == FI->getOperand(0)) {
351 OtherOpT = TI->getOperand(0);
352 OtherOpF = FI->getOperand(1);
353 MatchIsOpZero = false;
354 return TI->getOperand(1);
355 }
356 return nullptr;
357 };
358
359 if (TI->hasOneUse() || FI->hasOneUse()) {
360 // Cond ? -X : -Y --> -(Cond ? X : Y)
361 Value *X, *Y;
362 if (match(TI, m_FNeg(m_Value(X))) && match(FI, m_FNeg(m_Value(Y)))) {
363 // Intersect FMF from the fneg instructions and union those with the
364 // select.
366 FMF &= FI->getFastMathFlags();
367 FMF |= SI.getFastMathFlags();
368 Value *NewSel =
369 Builder.CreateSelect(Cond, X, Y, SI.getName() + ".v", &SI);
370 if (auto *NewSelI = dyn_cast<Instruction>(NewSel))
371 NewSelI->setFastMathFlags(FMF);
372 Instruction *NewFNeg = UnaryOperator::CreateFNeg(NewSel);
373 NewFNeg->setFastMathFlags(FMF);
374 return NewFNeg;
375 }
376
377 // Min/max intrinsic with a common operand can have the common operand
378 // pulled after the select. This is the same transform as below for binops,
379 // but specialized for intrinsic matching and without the restrictive uses
380 // clause.
381 auto *TII = dyn_cast<IntrinsicInst>(TI);
382 auto *FII = dyn_cast<IntrinsicInst>(FI);
383 if (TII && FII && TII->getIntrinsicID() == FII->getIntrinsicID()) {
384 if (match(TII, m_MaxOrMin(m_Value(), m_Value()))) {
385 if (Value *MatchOp = getCommonOp(TI, FI, true)) {
386 Value *NewSel =
387 Builder.CreateSelect(Cond, OtherOpT, OtherOpF, "minmaxop", &SI);
388 return CallInst::Create(TII->getCalledFunction(), {NewSel, MatchOp});
389 }
390 }
391
392 // select c, (ldexp v, e0), (ldexp v, e1) -> ldexp v, (select c, e0, e1)
393 // select c, (ldexp v0, e), (ldexp v1, e) -> ldexp (select c, v0, v1), e
394 //
395 // select c, (ldexp v0, e0), (ldexp v1, e1) ->
396 // ldexp (select c, v0, v1), (select c, e0, e1)
397 if (TII->getIntrinsicID() == Intrinsic::ldexp) {
398 Value *LdexpVal0 = TII->getArgOperand(0);
399 Value *LdexpExp0 = TII->getArgOperand(1);
400 Value *LdexpVal1 = FII->getArgOperand(0);
401 Value *LdexpExp1 = FII->getArgOperand(1);
402 if (LdexpExp0->getType() == LdexpExp1->getType()) {
403 FPMathOperator *SelectFPOp = cast<FPMathOperator>(&SI);
404 FastMathFlags FMF = cast<FPMathOperator>(TII)->getFastMathFlags();
405 FMF &= cast<FPMathOperator>(FII)->getFastMathFlags();
406 FMF |= SelectFPOp->getFastMathFlags();
407
408 Value *SelectVal = Builder.CreateSelect(Cond, LdexpVal0, LdexpVal1);
409 Value *SelectExp = Builder.CreateSelect(Cond, LdexpExp0, LdexpExp1);
410
411 CallInst *NewLdexp = Builder.CreateIntrinsic(
412 TII->getType(), Intrinsic::ldexp, {SelectVal, SelectExp});
413 NewLdexp->setFastMathFlags(FMF);
414 return replaceInstUsesWith(SI, NewLdexp);
415 }
416 }
417 }
418
419 // icmp with a common operand also can have the common operand
420 // pulled after the select.
421 ICmpInst::Predicate TPred, FPred;
422 if (match(TI, m_ICmp(TPred, m_Value(), m_Value())) &&
423 match(FI, m_ICmp(FPred, m_Value(), m_Value()))) {
424 if (TPred == FPred || TPred == CmpInst::getSwappedPredicate(FPred)) {
425 bool Swapped = TPred != FPred;
426 if (Value *MatchOp =
427 getCommonOp(TI, FI, ICmpInst::isEquality(TPred), Swapped)) {
428 Value *NewSel = Builder.CreateSelect(Cond, OtherOpT, OtherOpF,
429 SI.getName() + ".v", &SI);
430 return new ICmpInst(
431 MatchIsOpZero ? TPred : CmpInst::getSwappedPredicate(TPred),
432 MatchOp, NewSel);
433 }
434 }
435 }
436 }
437
438 // Only handle binary operators (including two-operand getelementptr) with
439 // one-use here. As with the cast case above, it may be possible to relax the
440 // one-use constraint, but that needs be examined carefully since it may not
441 // reduce the total number of instructions.
442 if (TI->getNumOperands() != 2 || FI->getNumOperands() != 2 ||
443 !TI->isSameOperationAs(FI) ||
444 (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) ||
445 !TI->hasOneUse() || !FI->hasOneUse())
446 return nullptr;
447
448 // Figure out if the operations have any operands in common.
449 Value *MatchOp = getCommonOp(TI, FI, TI->isCommutative());
450 if (!MatchOp)
451 return nullptr;
452
453 // If the select condition is a vector, the operands of the original select's
454 // operands also must be vectors. This may not be the case for getelementptr
455 // for example.
456 if (CondTy->isVectorTy() && (!OtherOpT->getType()->isVectorTy() ||
457 !OtherOpF->getType()->isVectorTy()))
458 return nullptr;
459
460 // If we are sinking div/rem after a select, we may need to freeze the
461 // condition because div/rem may induce immediate UB with a poison operand.
462 // For example, the following transform is not safe if Cond can ever be poison
463 // because we can replace poison with zero and then we have div-by-zero that
464 // didn't exist in the original code:
465 // Cond ? x/y : x/z --> x / (Cond ? y : z)
466 auto *BO = dyn_cast<BinaryOperator>(TI);
467 if (BO && BO->isIntDivRem() && !isGuaranteedNotToBePoison(Cond)) {
468 // A udiv/urem with a common divisor is safe because UB can only occur with
469 // div-by-zero, and that would be present in the original code.
470 if (BO->getOpcode() == Instruction::SDiv ||
471 BO->getOpcode() == Instruction::SRem || MatchIsOpZero)
473 }
474
475 // If we reach here, they do have operations in common.
476 Value *NewSI = Builder.CreateSelect(Cond, OtherOpT, OtherOpF,
477 SI.getName() + ".v", &SI);
478 Value *Op0 = MatchIsOpZero ? MatchOp : NewSI;
479 Value *Op1 = MatchIsOpZero ? NewSI : MatchOp;
480 if (auto *BO = dyn_cast<BinaryOperator>(TI)) {
481 BinaryOperator *NewBO = BinaryOperator::Create(BO->getOpcode(), Op0, Op1);
482 NewBO->copyIRFlags(TI);
483 NewBO->andIRFlags(FI);
484 return NewBO;
485 }
486 if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
487 auto *FGEP = cast<GetElementPtrInst>(FI);
488 Type *ElementType = TGEP->getResultElementType();
489 return TGEP->isInBounds() && FGEP->isInBounds()
490 ? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1})
491 : GetElementPtrInst::Create(ElementType, Op0, {Op1});
492 }
493 llvm_unreachable("Expected BinaryOperator or GEP");
494 return nullptr;
495}
496
497static bool isSelect01(const APInt &C1I, const APInt &C2I) {
498 if (!C1I.isZero() && !C2I.isZero()) // One side must be zero.
499 return false;
500 return C1I.isOne() || C1I.isAllOnes() || C2I.isOne() || C2I.isAllOnes();
501}
502
503/// Try to fold the select into one of the operands to allow further
504/// optimization.
506 Value *FalseVal) {
507 // See the comment above getSelectFoldableOperands for a description of the
508 // transformation we are doing here.
509 auto TryFoldSelectIntoOp = [&](SelectInst &SI, Value *TrueVal,
510 Value *FalseVal,
511 bool Swapped) -> Instruction * {
512 auto *TVI = dyn_cast<BinaryOperator>(TrueVal);
513 if (!TVI || !TVI->hasOneUse() || isa<Constant>(FalseVal))
514 return nullptr;
515
516 unsigned SFO = getSelectFoldableOperands(TVI);
517 unsigned OpToFold = 0;
518 if ((SFO & 1) && FalseVal == TVI->getOperand(0))
519 OpToFold = 1;
520 else if ((SFO & 2) && FalseVal == TVI->getOperand(1))
521 OpToFold = 2;
522
523 if (!OpToFold)
524 return nullptr;
525
526 // TODO: We probably ought to revisit cases where the select and FP
527 // instructions have different flags and add tests to ensure the
528 // behaviour is correct.
529 FastMathFlags FMF;
530 if (isa<FPMathOperator>(&SI))
531 FMF = SI.getFastMathFlags();
533 TVI->getOpcode(), TVI->getType(), true, FMF.noSignedZeros());
534 Value *OOp = TVI->getOperand(2 - OpToFold);
535 // Avoid creating select between 2 constants unless it's selecting
536 // between 0, 1 and -1.
537 const APInt *OOpC;
538 bool OOpIsAPInt = match(OOp, m_APInt(OOpC));
539 if (isa<Constant>(OOp) &&
540 (!OOpIsAPInt || !isSelect01(C->getUniqueInteger(), *OOpC)))
541 return nullptr;
542
543 // If the false value is a NaN then we have that the floating point math
544 // operation in the transformed code may not preserve the exact NaN
545 // bit-pattern -- e.g. `fadd sNaN, 0.0 -> qNaN`.
546 // This makes the transformation incorrect since the original program would
547 // have preserved the exact NaN bit-pattern.
548 // Avoid the folding if the false value might be a NaN.
549 if (isa<FPMathOperator>(&SI) &&
550 !computeKnownFPClass(FalseVal, FMF, fcNan, &SI).isKnownNeverNaN())
551 return nullptr;
552
553 Value *NewSel = Builder.CreateSelect(SI.getCondition(), Swapped ? C : OOp,
554 Swapped ? OOp : C, "", &SI);
555 if (isa<FPMathOperator>(&SI))
556 cast<Instruction>(NewSel)->setFastMathFlags(FMF);
557 NewSel->takeName(TVI);
558 BinaryOperator *BO =
559 BinaryOperator::Create(TVI->getOpcode(), FalseVal, NewSel);
560 BO->copyIRFlags(TVI);
561 return BO;
562 };
563
564 if (Instruction *R = TryFoldSelectIntoOp(SI, TrueVal, FalseVal, false))
565 return R;
566
567 if (Instruction *R = TryFoldSelectIntoOp(SI, FalseVal, TrueVal, true))
568 return R;
569
570 return nullptr;
571}
572
573/// We want to turn:
574/// (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1)
575/// into:
576/// zext (icmp ne i32 (and X, (or Y, (shl 1, Z))), 0)
577/// Note:
578/// Z may be 0 if lshr is missing.
579/// Worst-case scenario is that we will replace 5 instructions with 5 different
580/// instructions, but we got rid of select.
581static Instruction *foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp,
582 Value *TVal, Value *FVal,
583 InstCombiner::BuilderTy &Builder) {
584 if (!(Cmp->hasOneUse() && Cmp->getOperand(0)->hasOneUse() &&
585 Cmp->getPredicate() == ICmpInst::ICMP_EQ &&
586 match(Cmp->getOperand(1), m_Zero()) && match(FVal, m_One())))
587 return nullptr;
588
589 // The TrueVal has general form of: and %B, 1
590 Value *B;
591 if (!match(TVal, m_OneUse(m_And(m_Value(B), m_One()))))
592 return nullptr;
593
594 // Where %B may be optionally shifted: lshr %X, %Z.
595 Value *X, *Z;
596 const bool HasShift = match(B, m_OneUse(m_LShr(m_Value(X), m_Value(Z))));
597
598 // The shift must be valid.
599 // TODO: This restricts the fold to constant shift amounts. Is there a way to
600 // handle variable shifts safely? PR47012
601 if (HasShift &&
603 APInt(SelType->getScalarSizeInBits(),
604 SelType->getScalarSizeInBits()))))
605 return nullptr;
606
607 if (!HasShift)
608 X = B;
609
610 Value *Y;
611 if (!match(Cmp->getOperand(0), m_c_And(m_Specific(X), m_Value(Y))))
612 return nullptr;
613
614 // ((X & Y) == 0) ? ((X >> Z) & 1) : 1 --> (X & (Y | (1 << Z))) != 0
615 // ((X & Y) == 0) ? (X & 1) : 1 --> (X & (Y | 1)) != 0
616 Constant *One = ConstantInt::get(SelType, 1);
617 Value *MaskB = HasShift ? Builder.CreateShl(One, Z) : One;
618 Value *FullMask = Builder.CreateOr(Y, MaskB);
619 Value *MaskedX = Builder.CreateAnd(X, FullMask);
620 Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX);
621 return new ZExtInst(ICmpNeZero, SelType);
622}
623
624/// We want to turn:
625/// (select (icmp eq (and X, C1), 0), 0, (shl [nsw/nuw] X, C2));
626/// iff C1 is a mask and the number of its leading zeros is equal to C2
627/// into:
628/// shl X, C2
630 Value *FVal,
631 InstCombiner::BuilderTy &Builder) {
633 Value *AndVal;
634 if (!match(Cmp, m_ICmp(Pred, m_Value(AndVal), m_Zero())))
635 return nullptr;
636
637 if (Pred == ICmpInst::ICMP_NE) {
638 Pred = ICmpInst::ICMP_EQ;
639 std::swap(TVal, FVal);
640 }
641
642 Value *X;
643 const APInt *C2, *C1;
644 if (Pred != ICmpInst::ICMP_EQ ||
645 !match(AndVal, m_And(m_Value(X), m_APInt(C1))) ||
646 !match(TVal, m_Zero()) || !match(FVal, m_Shl(m_Specific(X), m_APInt(C2))))
647 return nullptr;
648
649 if (!C1->isMask() ||
650 C1->countLeadingZeros() != static_cast<unsigned>(C2->getZExtValue()))
651 return nullptr;
652
653 auto *FI = dyn_cast<Instruction>(FVal);
654 if (!FI)
655 return nullptr;
656
657 FI->setHasNoSignedWrap(false);
658 FI->setHasNoUnsignedWrap(false);
659 return FVal;
660}
661
662/// We want to turn:
663/// (select (icmp sgt x, C), lshr (X, Y), ashr (X, Y)); iff C s>= -1
664/// (select (icmp slt x, C), ashr (X, Y), lshr (X, Y)); iff C s>= 0
665/// into:
666/// ashr (X, Y)
667static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal,
668 Value *FalseVal,
669 InstCombiner::BuilderTy &Builder) {
671 Value *CmpLHS = IC->getOperand(0);
672 Value *CmpRHS = IC->getOperand(1);
673 if (!CmpRHS->getType()->isIntOrIntVectorTy())
674 return nullptr;
675
676 Value *X, *Y;
677 unsigned Bitwidth = CmpRHS->getType()->getScalarSizeInBits();
678 if ((Pred != ICmpInst::ICMP_SGT ||
679 !match(CmpRHS,
680 m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, -1)))) &&
681 (Pred != ICmpInst::ICMP_SLT ||
682 !match(CmpRHS,
684 return nullptr;
685
686 // Canonicalize so that ashr is in FalseVal.
687 if (Pred == ICmpInst::ICMP_SLT)
688 std::swap(TrueVal, FalseVal);
689
690 if (match(TrueVal, m_LShr(m_Value(X), m_Value(Y))) &&
691 match(FalseVal, m_AShr(m_Specific(X), m_Specific(Y))) &&
692 match(CmpLHS, m_Specific(X))) {
693 const auto *Ashr = cast<Instruction>(FalseVal);
694 // if lshr is not exact and ashr is, this new ashr must not be exact.
695 bool IsExact = Ashr->isExact() && cast<Instruction>(TrueVal)->isExact();
696 return Builder.CreateAShr(X, Y, IC->getName(), IsExact);
697 }
698
699 return nullptr;
700}
701
702/// We want to turn:
703/// (select (icmp eq (and X, C1), 0), Y, (BinOp Y, C2))
704/// into:
705/// IF C2 u>= C1
706/// (BinOp Y, (shl (and X, C1), C3))
707/// ELSE
708/// (BinOp Y, (lshr (and X, C1), C3))
709/// iff:
710/// 0 on the RHS is the identity value (i.e add, xor, shl, etc...)
711/// C1 and C2 are both powers of 2
712/// where:
713/// IF C2 u>= C1
714/// C3 = Log(C2) - Log(C1)
715/// ELSE
716/// C3 = Log(C1) - Log(C2)
717///
718/// This transform handles cases where:
719/// 1. The icmp predicate is inverted
720/// 2. The select operands are reversed
721/// 3. The magnitude of C2 and C1 are flipped
722static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
723 Value *FalseVal,
724 InstCombiner::BuilderTy &Builder) {
725 // Only handle integer compares. Also, if this is a vector select, we need a
726 // vector compare.
727 if (!TrueVal->getType()->isIntOrIntVectorTy() ||
728 TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy())
729 return nullptr;
730
731 Value *CmpLHS = IC->getOperand(0);
732 Value *CmpRHS = IC->getOperand(1);
733
734 unsigned C1Log;
735 bool NeedAnd = false;
736 CmpInst::Predicate Pred = IC->getPredicate();
737 if (IC->isEquality()) {
738 if (!match(CmpRHS, m_Zero()))
739 return nullptr;
740
741 const APInt *C1;
742 if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1))))
743 return nullptr;
744
745 C1Log = C1->logBase2();
746 } else {
747 APInt C1;
748 if (!decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, CmpLHS, C1) ||
749 !C1.isPowerOf2())
750 return nullptr;
751
752 C1Log = C1.logBase2();
753 NeedAnd = true;
754 }
755
756 Value *Y, *V = CmpLHS;
757 BinaryOperator *BinOp;
758 const APInt *C2;
759 bool NeedXor;
760 if (match(FalseVal, m_BinOp(m_Specific(TrueVal), m_Power2(C2)))) {
761 Y = TrueVal;
762 BinOp = cast<BinaryOperator>(FalseVal);
763 NeedXor = Pred == ICmpInst::ICMP_NE;
764 } else if (match(TrueVal, m_BinOp(m_Specific(FalseVal), m_Power2(C2)))) {
765 Y = FalseVal;
766 BinOp = cast<BinaryOperator>(TrueVal);
767 NeedXor = Pred == ICmpInst::ICMP_EQ;
768 } else {
769 return nullptr;
770 }
771
772 // Check that 0 on RHS is identity value for this binop.
773 auto *IdentityC =
775 /*AllowRHSConstant*/ true);
776 if (IdentityC == nullptr || !IdentityC->isNullValue())
777 return nullptr;
778
779 unsigned C2Log = C2->logBase2();
780
781 bool NeedShift = C1Log != C2Log;
782 bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() !=
783 V->getType()->getScalarSizeInBits();
784
785 // Make sure we don't create more instructions than we save.
786 if ((NeedShift + NeedXor + NeedZExtTrunc + NeedAnd) >
787 (IC->hasOneUse() + BinOp->hasOneUse()))
788 return nullptr;
789
790 if (NeedAnd) {
791 // Insert the AND instruction on the input to the truncate.
792 APInt C1 = APInt::getOneBitSet(V->getType()->getScalarSizeInBits(), C1Log);
793 V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), C1));
794 }
795
796 if (C2Log > C1Log) {
797 V = Builder.CreateZExtOrTrunc(V, Y->getType());
798 V = Builder.CreateShl(V, C2Log - C1Log);
799 } else if (C1Log > C2Log) {
800 V = Builder.CreateLShr(V, C1Log - C2Log);
801 V = Builder.CreateZExtOrTrunc(V, Y->getType());
802 } else
803 V = Builder.CreateZExtOrTrunc(V, Y->getType());
804
805 if (NeedXor)
806 V = Builder.CreateXor(V, *C2);
807
808 return Builder.CreateBinOp(BinOp->getOpcode(), Y, V);
809}
810
811/// Canonicalize a set or clear of a masked set of constant bits to
812/// select-of-constants form.
814 InstCombiner::BuilderTy &Builder) {
815 Value *Cond = Sel.getCondition();
816 Value *T = Sel.getTrueValue();
817 Value *F = Sel.getFalseValue();
818 Type *Ty = Sel.getType();
819 Value *X;
820 const APInt *NotC, *C;
821
822 // Cond ? (X & ~C) : (X | C) --> (X & ~C) | (Cond ? 0 : C)
823 if (match(T, m_And(m_Value(X), m_APInt(NotC))) &&
824 match(F, m_OneUse(m_Or(m_Specific(X), m_APInt(C)))) && *NotC == ~(*C)) {
826 Constant *OrC = ConstantInt::get(Ty, *C);
827 Value *NewSel = Builder.CreateSelect(Cond, Zero, OrC, "masksel", &Sel);
828 return BinaryOperator::CreateOr(T, NewSel);
829 }
830
831 // Cond ? (X | C) : (X & ~C) --> (X & ~C) | (Cond ? C : 0)
832 if (match(F, m_And(m_Value(X), m_APInt(NotC))) &&
833 match(T, m_OneUse(m_Or(m_Specific(X), m_APInt(C)))) && *NotC == ~(*C)) {
835 Constant *OrC = ConstantInt::get(Ty, *C);
836 Value *NewSel = Builder.CreateSelect(Cond, OrC, Zero, "masksel", &Sel);
837 return BinaryOperator::CreateOr(F, NewSel);
838 }
839
840 return nullptr;
841}
842
843// select (x == 0), 0, x * y --> freeze(y) * x
844// select (y == 0), 0, x * y --> freeze(x) * y
845// select (x == 0), undef, x * y --> freeze(y) * x
846// select (x == undef), 0, x * y --> freeze(y) * x
847// Usage of mul instead of 0 will make the result more poisonous,
848// so the operand that was not checked in the condition should be frozen.
849// The latter folding is applied only when a constant compared with x is
850// is a vector consisting of 0 and undefs. If a constant compared with x
851// is a scalar undefined value or undefined vector then an expression
852// should be already folded into a constant.
854 auto *CondVal = SI.getCondition();
855 auto *TrueVal = SI.getTrueValue();
856 auto *FalseVal = SI.getFalseValue();
857 Value *X, *Y;
858 ICmpInst::Predicate Predicate;
859
860 // Assuming that constant compared with zero is not undef (but it may be
861 // a vector with some undef elements). Otherwise (when a constant is undef)
862 // the select expression should be already simplified.
863 if (!match(CondVal, m_ICmp(Predicate, m_Value(X), m_Zero())) ||
864 !ICmpInst::isEquality(Predicate))
865 return nullptr;
866
867 if (Predicate == ICmpInst::ICMP_NE)
868 std::swap(TrueVal, FalseVal);
869
870 // Check that TrueVal is a constant instead of matching it with m_Zero()
871 // to handle the case when it is a scalar undef value or a vector containing
872 // non-zero elements that are masked by undef elements in the compare
873 // constant.
874 auto *TrueValC = dyn_cast<Constant>(TrueVal);
875 if (TrueValC == nullptr ||
876 !match(FalseVal, m_c_Mul(m_Specific(X), m_Value(Y))) ||
877 !isa<Instruction>(FalseVal))
878 return nullptr;
879
880 auto *ZeroC = cast<Constant>(cast<Instruction>(CondVal)->getOperand(1));
881 auto *MergedC = Constant::mergeUndefsWith(TrueValC, ZeroC);
882 // If X is compared with 0 then TrueVal could be either zero or undef.
883 // m_Zero match vectors containing some undef elements, but for scalars
884 // m_Undef should be used explicitly.
885 if (!match(MergedC, m_Zero()) && !match(MergedC, m_Undef()))
886 return nullptr;
887
888 auto *FalseValI = cast<Instruction>(FalseVal);
889 auto *FrY = IC.InsertNewInstBefore(new FreezeInst(Y, Y->getName() + ".fr"),
890 FalseValI->getIterator());
891 IC.replaceOperand(*FalseValI, FalseValI->getOperand(0) == Y ? 0 : 1, FrY);
892 return IC.replaceInstUsesWith(SI, FalseValI);
893}
894
895/// Transform patterns such as (a > b) ? a - b : 0 into usub.sat(a, b).
896/// There are 8 commuted/swapped variants of this pattern.
897/// TODO: Also support a - UMIN(a,b) patterns.
899 const Value *TrueVal,
900 const Value *FalseVal,
901 InstCombiner::BuilderTy &Builder) {
902 ICmpInst::Predicate Pred = ICI->getPredicate();
903 Value *A = ICI->getOperand(0);
904 Value *B = ICI->getOperand(1);
905
906 // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0
907 // (a == 0) ? 0 : a - 1 -> (a != 0) ? a - 1 : 0
908 if (match(TrueVal, m_Zero())) {
910 std::swap(TrueVal, FalseVal);
911 }
912
913 if (!match(FalseVal, m_Zero()))
914 return nullptr;
915
916 // ugt 0 is canonicalized to ne 0 and requires special handling
917 // (a != 0) ? a + -1 : 0 -> usub.sat(a, 1)
918 if (Pred == ICmpInst::ICMP_NE) {
919 if (match(B, m_Zero()) && match(TrueVal, m_Add(m_Specific(A), m_AllOnes())))
920 return Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A,
921 ConstantInt::get(A->getType(), 1));
922 return nullptr;
923 }
924
925 if (!ICmpInst::isUnsigned(Pred))
926 return nullptr;
927
928 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_ULT) {
929 // (b < a) ? a - b : 0 -> (a > b) ? a - b : 0
930 std::swap(A, B);
932 }
933
934 assert((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&
935 "Unexpected isUnsigned predicate!");
936
937 // Ensure the sub is of the form:
938 // (a > b) ? a - b : 0 -> usub.sat(a, b)
939 // (a > b) ? b - a : 0 -> -usub.sat(a, b)
940 // Checking for both a-b and a+(-b) as a constant.
941 bool IsNegative = false;
942 const APInt *C;
943 if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A))) ||
944 (match(A, m_APInt(C)) &&
945 match(TrueVal, m_Add(m_Specific(B), m_SpecificInt(-*C)))))
946 IsNegative = true;
947 else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B))) &&
948 !(match(B, m_APInt(C)) &&
949 match(TrueVal, m_Add(m_Specific(A), m_SpecificInt(-*C)))))
950 return nullptr;
951
952 // If we are adding a negate and the sub and icmp are used anywhere else, we
953 // would end up with more instructions.
954 if (IsNegative && !TrueVal->hasOneUse() && !ICI->hasOneUse())
955 return nullptr;
956
957 // (a > b) ? a - b : 0 -> usub.sat(a, b)
958 // (a > b) ? b - a : 0 -> -usub.sat(a, b)
959 Value *Result = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A, B);
960 if (IsNegative)
961 Result = Builder.CreateNeg(Result);
962 return Result;
963}
964
966 InstCombiner::BuilderTy &Builder) {
967 if (!Cmp->hasOneUse())
968 return nullptr;
969
970 // Match unsigned saturated add with constant.
971 Value *Cmp0 = Cmp->getOperand(0);
972 Value *Cmp1 = Cmp->getOperand(1);
973 ICmpInst::Predicate Pred = Cmp->getPredicate();
974 Value *X;
975 const APInt *C, *CmpC;
976 if (Pred == ICmpInst::ICMP_ULT &&
977 match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
978 match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
979 // (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C)
980 return Builder.CreateBinaryIntrinsic(
981 Intrinsic::uadd_sat, X, ConstantInt::get(X->getType(), *C));
982 }
983
984 // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
985 // There are 8 commuted variants.
986 // Canonicalize -1 (saturated result) to true value of the select.
987 if (match(FVal, m_AllOnes())) {
988 std::swap(TVal, FVal);
989 Pred = CmpInst::getInversePredicate(Pred);
990 }
991 if (!match(TVal, m_AllOnes()))
992 return nullptr;
993
994 // Canonicalize predicate to less-than or less-or-equal-than.
995 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
996 std::swap(Cmp0, Cmp1);
997 Pred = CmpInst::getSwappedPredicate(Pred);
998 }
999 if (Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_ULE)
1000 return nullptr;
1001
1002 // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
1003 // Strictness of the comparison is irrelevant.
1004 Value *Y;
1005 if (match(Cmp0, m_Not(m_Value(X))) &&
1006 match(FVal, m_c_Add(m_Specific(X), m_Value(Y))) && Y == Cmp1) {
1007 // (~X u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
1008 // (~X u< Y) ? -1 : (Y + X) --> uadd.sat(X, Y)
1009 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X, Y);
1010 }
1011 // The 'not' op may be included in the sum but not the compare.
1012 // Strictness of the comparison is irrelevant.
1013 X = Cmp0;
1014 Y = Cmp1;
1015 if (match(FVal, m_c_Add(m_Not(m_Specific(X)), m_Specific(Y)))) {
1016 // (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y)
1017 // (X u< Y) ? -1 : (Y + ~X) --> uadd.sat(Y, ~X)
1018 BinaryOperator *BO = cast<BinaryOperator>(FVal);
1019 return Builder.CreateBinaryIntrinsic(
1020 Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1));
1021 }
1022 // The overflow may be detected via the add wrapping round.
1023 // This is only valid for strict comparison!
1024 if (Pred == ICmpInst::ICMP_ULT &&
1025 match(Cmp0, m_c_Add(m_Specific(Cmp1), m_Value(Y))) &&
1026 match(FVal, m_c_Add(m_Specific(Cmp1), m_Specific(Y)))) {
1027 // ((X + Y) u< X) ? -1 : (X + Y) --> uadd.sat(X, Y)
1028 // ((X + Y) u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
1029 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp1, Y);
1030 }
1031
1032 return nullptr;
1033}
1034
1035/// Try to match patterns with select and subtract as absolute difference.
1036static Value *foldAbsDiff(ICmpInst *Cmp, Value *TVal, Value *FVal,
1037 InstCombiner::BuilderTy &Builder) {
1038 auto *TI = dyn_cast<Instruction>(TVal);
1039 auto *FI = dyn_cast<Instruction>(FVal);
1040 if (!TI || !FI)
1041 return nullptr;
1042
1043 // Normalize predicate to gt/lt rather than ge/le.
1044 ICmpInst::Predicate Pred = Cmp->getStrictPredicate();
1045 Value *A = Cmp->getOperand(0);
1046 Value *B = Cmp->getOperand(1);
1047
1048 // Normalize "A - B" as the true value of the select.
1049 if (match(FI, m_Sub(m_Specific(A), m_Specific(B)))) {
1050 std::swap(FI, TI);
1051 Pred = ICmpInst::getSwappedPredicate(Pred);
1052 }
1053
1054 // With any pair of no-wrap subtracts:
1055 // (A > B) ? (A - B) : (B - A) --> abs(A - B)
1056 if (Pred == CmpInst::ICMP_SGT &&
1057 match(TI, m_Sub(m_Specific(A), m_Specific(B))) &&
1058 match(FI, m_Sub(m_Specific(B), m_Specific(A))) &&
1059 (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap()) &&
1060 (FI->hasNoSignedWrap() || FI->hasNoUnsignedWrap())) {
1061 // The remaining subtract is not "nuw" any more.
1062 // If there's one use of the subtract (no other use than the use we are
1063 // about to replace), then we know that the sub is "nsw" in this context
1064 // even if it was only "nuw" before. If there's another use, then we can't
1065 // add "nsw" to the existing instruction because it may not be safe in the
1066 // other user's context.
1067 TI->setHasNoUnsignedWrap(false);
1068 if (!TI->hasNoSignedWrap())
1069 TI->setHasNoSignedWrap(TI->hasOneUse());
1070 return Builder.CreateBinaryIntrinsic(Intrinsic::abs, TI, Builder.getTrue());
1071 }
1072
1073 return nullptr;
1074}
1075
1076/// Fold the following code sequence:
1077/// \code
1078/// int a = ctlz(x & -x);
1079// x ? 31 - a : a;
1080// // or
1081// x ? 31 - a : 32;
1082/// \code
1083///
1084/// into:
1085/// cttz(x)
1086static Instruction *foldSelectCtlzToCttz(ICmpInst *ICI, Value *TrueVal,
1087 Value *FalseVal,
1088 InstCombiner::BuilderTy &Builder) {
1089 unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits();
1090 if (!ICI->isEquality() || !match(ICI->getOperand(1), m_Zero()))
1091 return nullptr;
1092
1093 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
1094 std::swap(TrueVal, FalseVal);
1095
1096 Value *Ctlz;
1097 if (!match(FalseVal,
1098 m_Xor(m_Value(Ctlz), m_SpecificInt(BitWidth - 1))))
1099 return nullptr;
1100
1101 if (!match(Ctlz, m_Intrinsic<Intrinsic::ctlz>()))
1102 return nullptr;
1103
1104 if (TrueVal != Ctlz && !match(TrueVal, m_SpecificInt(BitWidth)))
1105 return nullptr;
1106
1107 Value *X = ICI->getOperand(0);
1108 auto *II = cast<IntrinsicInst>(Ctlz);
1109 if (!match(II->getOperand(0), m_c_And(m_Specific(X), m_Neg(m_Specific(X)))))
1110 return nullptr;
1111
1112 Function *F = Intrinsic::getDeclaration(II->getModule(), Intrinsic::cttz,
1113 II->getType());
1114 return CallInst::Create(F, {X, II->getArgOperand(1)});
1115}
1116
1117/// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single
1118/// call to cttz/ctlz with flag 'is_zero_poison' cleared.
1119///
1120/// For example, we can fold the following code sequence:
1121/// \code
1122/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true)
1123/// %1 = icmp ne i32 %x, 0
1124/// %2 = select i1 %1, i32 %0, i32 32
1125/// \code
1126///
1127/// into:
1128/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
1129static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
1130 InstCombiner::BuilderTy &Builder) {
1131 ICmpInst::Predicate Pred = ICI->getPredicate();
1132 Value *CmpLHS = ICI->getOperand(0);
1133 Value *CmpRHS = ICI->getOperand(1);
1134
1135 // Check if the select condition compares a value for equality.
1136 if (!ICI->isEquality())
1137 return nullptr;
1138
1139 Value *SelectArg = FalseVal;
1140 Value *ValueOnZero = TrueVal;
1141 if (Pred == ICmpInst::ICMP_NE)
1142 std::swap(SelectArg, ValueOnZero);
1143
1144 // Skip zero extend/truncate.
1145 Value *Count = nullptr;
1146 if (!match(SelectArg, m_ZExt(m_Value(Count))) &&
1147 !match(SelectArg, m_Trunc(m_Value(Count))))
1148 Count = SelectArg;
1149
1150 // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the
1151 // input to the cttz/ctlz is used as LHS for the compare instruction.
1152 Value *X;
1153 if (!match(Count, m_Intrinsic<Intrinsic::cttz>(m_Value(X))) &&
1154 !match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Value(X))))
1155 return nullptr;
1156
1157 // (X == 0) ? BitWidth : ctz(X)
1158 // (X == -1) ? BitWidth : ctz(~X)
1159 if ((X != CmpLHS || !match(CmpRHS, m_Zero())) &&
1160 (!match(X, m_Not(m_Specific(CmpLHS))) || !match(CmpRHS, m_AllOnes())))
1161 return nullptr;
1162
1163 IntrinsicInst *II = cast<IntrinsicInst>(Count);
1164
1165 // Check if the value propagated on zero is a constant number equal to the
1166 // sizeof in bits of 'Count'.
1167 unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits();
1168 if (match(ValueOnZero, m_SpecificInt(SizeOfInBits))) {
1169 // Explicitly clear the 'is_zero_poison' flag. It's always valid to go from
1170 // true to false on this flag, so we can replace it for all users.
1172 return SelectArg;
1173 }
1174
1175 // The ValueOnZero is not the bitwidth. But if the cttz/ctlz (and optional
1176 // zext/trunc) have one use (ending at the select), the cttz/ctlz result will
1177 // not be used if the input is zero. Relax to 'zero is poison' for that case.
1178 if (II->hasOneUse() && SelectArg->hasOneUse() &&
1179 !match(II->getArgOperand(1), m_One()))
1181
1182 return nullptr;
1183}
1184
1185static Value *canonicalizeSPF(ICmpInst &Cmp, Value *TrueVal, Value *FalseVal,
1186 InstCombinerImpl &IC) {
1187 Value *LHS, *RHS;
1188 // TODO: What to do with pointer min/max patterns?
1189 if (!TrueVal->getType()->isIntOrIntVectorTy())
1190 return nullptr;
1191
1193 matchDecomposedSelectPattern(&Cmp, TrueVal, FalseVal, LHS, RHS).Flavor;
1194 if (SPF == SelectPatternFlavor::SPF_ABS ||
1196 if (!Cmp.hasOneUse() && !RHS->hasOneUse())
1197 return nullptr; // TODO: Relax this restriction.
1198
1199 // Note that NSW flag can only be propagated for normal, non-negated abs!
1200 bool IntMinIsPoison = SPF == SelectPatternFlavor::SPF_ABS &&
1201 match(RHS, m_NSWNeg(m_Specific(LHS)));
1202 Constant *IntMinIsPoisonC =
1203 ConstantInt::get(Type::getInt1Ty(Cmp.getContext()), IntMinIsPoison);
1204 Value *Abs =
1205 IC.Builder.CreateBinaryIntrinsic(Intrinsic::abs, LHS, IntMinIsPoisonC);
1206
1208 return IC.Builder.CreateNeg(Abs); // Always without NSW flag!
1209 return Abs;
1210 }
1211
1213 Intrinsic::ID IntrinsicID;
1214 switch (SPF) {
1216 IntrinsicID = Intrinsic::umin;
1217 break;
1219 IntrinsicID = Intrinsic::umax;
1220 break;
1222 IntrinsicID = Intrinsic::smin;
1223 break;
1225 IntrinsicID = Intrinsic::smax;
1226 break;
1227 default:
1228 llvm_unreachable("Unexpected SPF");
1229 }
1230 return IC.Builder.CreateBinaryIntrinsic(IntrinsicID, LHS, RHS);
1231 }
1232
1233 return nullptr;
1234}
1235
1237 unsigned Depth) {
1238 // Conservatively limit replacement to two instructions upwards.
1239 if (Depth == 2)
1240 return false;
1241
1242 auto *I = dyn_cast<Instruction>(V);
1243 if (!I || !I->hasOneUse() || !isSafeToSpeculativelyExecute(I))
1244 return false;
1245
1246 bool Changed = false;
1247 for (Use &U : I->operands()) {
1248 if (U == Old) {
1249 replaceUse(U, New);
1250 Worklist.add(I);
1251 Changed = true;
1252 } else {
1253 Changed |= replaceInInstruction(U, Old, New, Depth + 1);
1254 }
1255 }
1256 return Changed;
1257}
1258
1259/// If we have a select with an equality comparison, then we know the value in
1260/// one of the arms of the select. See if substituting this value into an arm
1261/// and simplifying the result yields the same value as the other arm.
1262///
1263/// To make this transform safe, we must drop poison-generating flags
1264/// (nsw, etc) if we simplified to a binop because the select may be guarding
1265/// that poison from propagating. If the existing binop already had no
1266/// poison-generating flags, then this transform can be done by instsimplify.
1267///
1268/// Consider:
1269/// %cmp = icmp eq i32 %x, 2147483647
1270/// %add = add nsw i32 %x, 1
1271/// %sel = select i1 %cmp, i32 -2147483648, i32 %add
1272///
1273/// We can't replace %sel with %add unless we strip away the flags.
1274/// TODO: Wrapping flags could be preserved in some cases with better analysis.
1276 ICmpInst &Cmp) {
1277 if (!Cmp.isEquality())
1278 return nullptr;
1279
1280 // Canonicalize the pattern to ICMP_EQ by swapping the select operands.
1281 Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue();
1282 bool Swapped = false;
1283 if (Cmp.getPredicate() == ICmpInst::ICMP_NE) {
1284 std::swap(TrueVal, FalseVal);
1285 Swapped = true;
1286 }
1287
1288 // In X == Y ? f(X) : Z, try to evaluate f(Y) and replace the operand.
1289 // Make sure Y cannot be undef though, as we might pick different values for
1290 // undef in the icmp and in f(Y). Additionally, take care to avoid replacing
1291 // X == Y ? X : Z with X == Y ? Y : Z, as that would lead to an infinite
1292 // replacement cycle.
1293 Value *CmpLHS = Cmp.getOperand(0), *CmpRHS = Cmp.getOperand(1);
1294 if (TrueVal != CmpLHS &&
1295 isGuaranteedNotToBeUndefOrPoison(CmpRHS, SQ.AC, &Sel, &DT)) {
1296 if (Value *V = simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, SQ,
1297 /* AllowRefinement */ true))
1298 // Require either the replacement or the simplification result to be a
1299 // constant to avoid infinite loops.
1300 // FIXME: Make this check more precise.
1301 if (isa<Constant>(CmpRHS) || isa<Constant>(V))
1302 return replaceOperand(Sel, Swapped ? 2 : 1, V);
1303
1304 // Even if TrueVal does not simplify, we can directly replace a use of
1305 // CmpLHS with CmpRHS, as long as the instruction is not used anywhere
1306 // else and is safe to speculatively execute (we may end up executing it
1307 // with different operands, which should not cause side-effects or trigger
1308 // undefined behavior). Only do this if CmpRHS is a constant, as
1309 // profitability is not clear for other cases.
1310 // FIXME: Support vectors.
1311 if (match(CmpRHS, m_ImmConstant()) && !match(CmpLHS, m_ImmConstant()) &&
1312 !Cmp.getType()->isVectorTy())
1313 if (replaceInInstruction(TrueVal, CmpLHS, CmpRHS))
1314 return &Sel;
1315 }
1316 if (TrueVal != CmpRHS &&
1317 isGuaranteedNotToBeUndefOrPoison(CmpLHS, SQ.AC, &Sel, &DT))
1318 if (Value *V = simplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, SQ,
1319 /* AllowRefinement */ true))
1320 if (isa<Constant>(CmpLHS) || isa<Constant>(V))
1321 return replaceOperand(Sel, Swapped ? 2 : 1, V);
1322
1323 auto *FalseInst = dyn_cast<Instruction>(FalseVal);
1324 if (!FalseInst)
1325 return nullptr;
1326
1327 // InstSimplify already performed this fold if it was possible subject to
1328 // current poison-generating flags. Check whether dropping poison-generating
1329 // flags enables the transform.
1330
1331 // Try each equivalence substitution possibility.
1332 // We have an 'EQ' comparison, so the select's false value will propagate.
1333 // Example:
1334 // (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1
1336 if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, SQ,
1337 /* AllowRefinement */ false,
1338 &DropFlags) == TrueVal ||
1339 simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, SQ,
1340 /* AllowRefinement */ false,
1341 &DropFlags) == TrueVal) {
1342 for (Instruction *I : DropFlags) {
1343 I->dropPoisonGeneratingAnnotations();
1344 Worklist.add(I);
1345 }
1346
1347 return replaceInstUsesWith(Sel, FalseVal);
1348 }
1349
1350 return nullptr;
1351}
1352
1353// See if this is a pattern like:
1354// %old_cmp1 = icmp slt i32 %x, C2
1355// %old_replacement = select i1 %old_cmp1, i32 %target_low, i32 %target_high
1356// %old_x_offseted = add i32 %x, C1
1357// %old_cmp0 = icmp ult i32 %old_x_offseted, C0
1358// %r = select i1 %old_cmp0, i32 %x, i32 %old_replacement
1359// This can be rewritten as more canonical pattern:
1360// %new_cmp1 = icmp slt i32 %x, -C1
1361// %new_cmp2 = icmp sge i32 %x, C0-C1
1362// %new_clamped_low = select i1 %new_cmp1, i32 %target_low, i32 %x
1363// %r = select i1 %new_cmp2, i32 %target_high, i32 %new_clamped_low
1364// Iff -C1 s<= C2 s<= C0-C1
1365// Also ULT predicate can also be UGT iff C0 != -1 (+invert result)
1366// SLT predicate can also be SGT iff C2 != INT_MAX (+invert res.)
1367static Value *canonicalizeClampLike(SelectInst &Sel0, ICmpInst &Cmp0,
1368 InstCombiner::BuilderTy &Builder,
1369 InstCombiner &IC) {
1370 Value *X = Sel0.getTrueValue();
1371 Value *Sel1 = Sel0.getFalseValue();
1372
1373 // First match the condition of the outermost select.
1374 // Said condition must be one-use.
1375 if (!Cmp0.hasOneUse())
1376 return nullptr;
1377 ICmpInst::Predicate Pred0 = Cmp0.getPredicate();
1378 Value *Cmp00 = Cmp0.getOperand(0);
1379 Constant *C0;
1380 if (!match(Cmp0.getOperand(1),
1382 return nullptr;
1383
1384 if (!isa<SelectInst>(Sel1)) {
1385 Pred0 = ICmpInst::getInversePredicate(Pred0);
1386 std::swap(X, Sel1);
1387 }
1388
1389 // Canonicalize Cmp0 into ult or uge.
1390 // FIXME: we shouldn't care about lanes that are 'undef' in the end?
1391 switch (Pred0) {
1394 // Although icmp ult %x, 0 is an unusual thing to try and should generally
1395 // have been simplified, it does not verify with undef inputs so ensure we
1396 // are not in a strange state.
1397 if (!match(C0, m_SpecificInt_ICMP(
1400 return nullptr;
1401 break; // Great!
1404 // We want to canonicalize it to 'ult' or 'uge', so we'll need to increment
1405 // C0, which again means it must not have any all-ones elements.
1406 if (!match(C0,
1410 return nullptr; // Can't do, have all-ones element[s].
1412 C0 = InstCombiner::AddOne(C0);
1413 break;
1414 default:
1415 return nullptr; // Unknown predicate.
1416 }
1417
1418 // Now that we've canonicalized the ICmp, we know the X we expect;
1419 // the select in other hand should be one-use.
1420 if (!Sel1->hasOneUse())
1421 return nullptr;
1422
1423 // If the types do not match, look through any truncs to the underlying
1424 // instruction.
1425 if (Cmp00->getType() != X->getType() && X->hasOneUse())
1427
1428 // We now can finish matching the condition of the outermost select:
1429 // it should either be the X itself, or an addition of some constant to X.
1430 Constant *C1;
1431 if (Cmp00 == X)
1432 C1 = ConstantInt::getNullValue(X->getType());
1433 else if (!match(Cmp00,
1436 return nullptr;
1437
1438 Value *Cmp1;
1439 ICmpInst::Predicate Pred1;
1440 Constant *C2;
1441 Value *ReplacementLow, *ReplacementHigh;
1442 if (!match(Sel1, m_Select(m_Value(Cmp1), m_Value(ReplacementLow),
1443 m_Value(ReplacementHigh))) ||
1444 !match(Cmp1,
1445 m_ICmp(Pred1, m_Specific(X),
1447 return nullptr;
1448
1449 if (!Cmp1->hasOneUse() && (Cmp00 == X || !Cmp00->hasOneUse()))
1450 return nullptr; // Not enough one-use instructions for the fold.
1451 // FIXME: this restriction could be relaxed if Cmp1 can be reused as one of
1452 // two comparisons we'll need to build.
1453
1454 // Canonicalize Cmp1 into the form we expect.
1455 // FIXME: we shouldn't care about lanes that are 'undef' in the end?
1456 switch (Pred1) {
1458 break;
1460 // We'd have to increment C2 by one, and for that it must not have signed
1461 // max element, but then it would have been canonicalized to 'slt' before
1462 // we get here. So we can't do anything useful with 'sle'.
1463 return nullptr;
1465 // We want to canonicalize it to 'slt', so we'll need to increment C2,
1466 // which again means it must not have any signed max elements.
1467 if (!match(C2,
1470 C2->getType()->getScalarSizeInBits()))))
1471 return nullptr; // Can't do, have signed max element[s].
1472 C2 = InstCombiner::AddOne(C2);
1473 [[fallthrough]];
1475 // Also non-canonical, but here we don't need to change C2,
1476 // so we don't have any restrictions on C2, so we can just handle it.
1478 std::swap(ReplacementLow, ReplacementHigh);
1479 break;
1480 default:
1481 return nullptr; // Unknown predicate.
1482 }
1484 "Unexpected predicate type.");
1485
1486 // The thresholds of this clamp-like pattern.
1487 auto *ThresholdLowIncl = ConstantExpr::getNeg(C1);
1488 auto *ThresholdHighExcl = ConstantExpr::getSub(C0, C1);
1489
1492 "Unexpected predicate type.");
1493 if (Pred0 == ICmpInst::Predicate::ICMP_UGE)
1494 std::swap(ThresholdLowIncl, ThresholdHighExcl);
1495
1496 // The fold has a precondition 1: C2 s>= ThresholdLow
1497 auto *Precond1 = ConstantFoldCompareInstOperands(
1498 ICmpInst::Predicate::ICMP_SGE, C2, ThresholdLowIncl, IC.getDataLayout());
1499 if (!Precond1 || !match(Precond1, m_One()))
1500 return nullptr;
1501 // The fold has a precondition 2: C2 s<= ThresholdHigh
1502 auto *Precond2 = ConstantFoldCompareInstOperands(
1503 ICmpInst::Predicate::ICMP_SLE, C2, ThresholdHighExcl, IC.getDataLayout());
1504 if (!Precond2 || !match(Precond2, m_One()))
1505 return nullptr;
1506
1507 // If we are matching from a truncated input, we need to sext the
1508 // ReplacementLow and ReplacementHigh values. Only do the transform if they
1509 // are free to extend due to being constants.
1510 if (X->getType() != Sel0.getType()) {
1511 Constant *LowC, *HighC;
1512 if (!match(ReplacementLow, m_ImmConstant(LowC)) ||
1513 !match(ReplacementHigh, m_ImmConstant(HighC)))
1514 return nullptr;
1515 const DataLayout &DL = Sel0.getModule()->getDataLayout();
1516 ReplacementLow =
1517 ConstantFoldCastOperand(Instruction::SExt, LowC, X->getType(), DL);
1518 ReplacementHigh =
1519 ConstantFoldCastOperand(Instruction::SExt, HighC, X->getType(), DL);
1520 assert(ReplacementLow && ReplacementHigh &&
1521 "Constant folding of ImmConstant cannot fail");
1522 }
1523
1524 // All good, finally emit the new pattern.
1525 Value *ShouldReplaceLow = Builder.CreateICmpSLT(X, ThresholdLowIncl);
1526 Value *ShouldReplaceHigh = Builder.CreateICmpSGE(X, ThresholdHighExcl);
1527 Value *MaybeReplacedLow =
1528 Builder.CreateSelect(ShouldReplaceLow, ReplacementLow, X);
1529
1530 // Create the final select. If we looked through a truncate above, we will
1531 // need to retruncate the result.
1532 Value *MaybeReplacedHigh = Builder.CreateSelect(
1533 ShouldReplaceHigh, ReplacementHigh, MaybeReplacedLow);
1534 return Builder.CreateTrunc(MaybeReplacedHigh, Sel0.getType());
1535}
1536
1537// If we have
1538// %cmp = icmp [canonical predicate] i32 %x, C0
1539// %r = select i1 %cmp, i32 %y, i32 C1
1540// Where C0 != C1 and %x may be different from %y, see if the constant that we
1541// will have if we flip the strictness of the predicate (i.e. without changing
1542// the result) is identical to the C1 in select. If it matches we can change
1543// original comparison to one with swapped predicate, reuse the constant,
1544// and swap the hands of select.
1545static Instruction *
1546tryToReuseConstantFromSelectInComparison(SelectInst &Sel, ICmpInst &Cmp,
1547 InstCombinerImpl &IC) {
1549 Value *X;
1550 Constant *C0;
1551 if (!match(&Cmp, m_OneUse(m_ICmp(
1552 Pred, m_Value(X),
1554 return nullptr;
1555
1556 // If comparison predicate is non-relational, we won't be able to do anything.
1557 if (ICmpInst::isEquality(Pred))
1558 return nullptr;
1559
1560 // If comparison predicate is non-canonical, then we certainly won't be able
1561 // to make it canonical; canonicalizeCmpWithConstant() already tried.
1563 return nullptr;
1564
1565 // If the [input] type of comparison and select type are different, lets abort
1566 // for now. We could try to compare constants with trunc/[zs]ext though.
1567 if (C0->getType() != Sel.getType())
1568 return nullptr;
1569
1570 // ULT with 'add' of a constant is canonical. See foldICmpAddConstant().
1571 // FIXME: Are there more magic icmp predicate+constant pairs we must avoid?
1572 // Or should we just abandon this transform entirely?
1573 if (Pred == CmpInst::ICMP_ULT && match(X, m_Add(m_Value(), m_Constant())))
1574 return nullptr;
1575
1576
1577 Value *SelVal0, *SelVal1; // We do not care which one is from where.
1578 match(&Sel, m_Select(m_Value(), m_Value(SelVal0), m_Value(SelVal1)));
1579 // At least one of these values we are selecting between must be a constant
1580 // else we'll never succeed.
1581 if (!match(SelVal0, m_AnyIntegralConstant()) &&
1582 !match(SelVal1, m_AnyIntegralConstant()))
1583 return nullptr;
1584
1585 // Does this constant C match any of the `select` values?
1586 auto MatchesSelectValue = [SelVal0, SelVal1](Constant *C) {
1587 return C->isElementWiseEqual(SelVal0) || C->isElementWiseEqual(SelVal1);
1588 };
1589
1590 // If C0 *already* matches true/false value of select, we are done.
1591 if (MatchesSelectValue(C0))
1592 return nullptr;
1593
1594 // Check the constant we'd have with flipped-strictness predicate.
1595 auto FlippedStrictness =
1597 if (!FlippedStrictness)
1598 return nullptr;
1599
1600 // If said constant doesn't match either, then there is no hope,
1601 if (!MatchesSelectValue(FlippedStrictness->second))
1602 return nullptr;
1603
1604 // It matched! Lets insert the new comparison just before select.
1606 IC.Builder.SetInsertPoint(&Sel);
1607
1608 Pred = ICmpInst::getSwappedPredicate(Pred); // Yes, swapped.
1609 Value *NewCmp = IC.Builder.CreateICmp(Pred, X, FlippedStrictness->second,
1610 Cmp.getName() + ".inv");
1611 IC.replaceOperand(Sel, 0, NewCmp);
1612 Sel.swapValues();
1613 Sel.swapProfMetadata();
1614
1615 return &Sel;
1616}
1617
1618static Instruction *foldSelectZeroOrOnes(ICmpInst *Cmp, Value *TVal,
1619 Value *FVal,
1620 InstCombiner::BuilderTy &Builder) {
1621 if (!Cmp->hasOneUse())
1622 return nullptr;
1623
1624 const APInt *CmpC;
1625 if (!match(Cmp->getOperand(1), m_APIntAllowPoison(CmpC)))
1626 return nullptr;
1627
1628 // (X u< 2) ? -X : -1 --> sext (X != 0)
1629 Value *X = Cmp->getOperand(0);
1630 if (Cmp->getPredicate() == ICmpInst::ICMP_ULT && *CmpC == 2 &&
1631 match(TVal, m_Neg(m_Specific(X))) && match(FVal, m_AllOnes()))
1632 return new SExtInst(Builder.CreateIsNotNull(X), TVal->getType());
1633
1634 // (X u> 1) ? -1 : -X --> sext (X != 0)
1635 if (Cmp->getPredicate() == ICmpInst::ICMP_UGT && *CmpC == 1 &&
1636 match(FVal, m_Neg(m_Specific(X))) && match(TVal, m_AllOnes()))
1637 return new SExtInst(Builder.CreateIsNotNull(X), TVal->getType());
1638
1639 return nullptr;
1640}
1641
1642static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
1643 InstCombiner::BuilderTy &Builder) {
1644 const APInt *CmpC;
1645 Value *V;
1646 CmpInst::Predicate Pred;
1647 if (!match(ICI, m_ICmp(Pred, m_Value(V), m_APInt(CmpC))))
1648 return nullptr;
1649
1650 // Match clamp away from min/max value as a max/min operation.
1651 Value *TVal = SI.getTrueValue();
1652 Value *FVal = SI.getFalseValue();
1653 if (Pred == ICmpInst::ICMP_EQ && V == FVal) {
1654 // (V == UMIN) ? UMIN+1 : V --> umax(V, UMIN+1)
1655 if (CmpC->isMinValue() && match(TVal, m_SpecificInt(*CmpC + 1)))
1656 return Builder.CreateBinaryIntrinsic(Intrinsic::umax, V, TVal);
1657 // (V == UMAX) ? UMAX-1 : V --> umin(V, UMAX-1)
1658 if (CmpC->isMaxValue() && match(TVal, m_SpecificInt(*CmpC - 1)))
1659 return Builder.CreateBinaryIntrinsic(Intrinsic::umin, V, TVal);
1660 // (V == SMIN) ? SMIN+1 : V --> smax(V, SMIN+1)
1661 if (CmpC->isMinSignedValue() && match(TVal, m_SpecificInt(*CmpC + 1)))
1662 return Builder.CreateBinaryIntrinsic(Intrinsic::smax, V, TVal);
1663 // (V == SMAX) ? SMAX-1 : V --> smin(V, SMAX-1)
1664 if (CmpC->isMaxSignedValue() && match(TVal, m_SpecificInt(*CmpC - 1)))
1665 return Builder.CreateBinaryIntrinsic(Intrinsic::smin, V, TVal);
1666 }
1667
1668 BinaryOperator *BO;
1669 const APInt *C;
1670 CmpInst::Predicate CPred;
1671 if (match(&SI, m_Select(m_Specific(ICI), m_APInt(C), m_BinOp(BO))))
1672 CPred = ICI->getPredicate();
1673 else if (match(&SI, m_Select(m_Specific(ICI), m_BinOp(BO), m_APInt(C))))
1674 CPred = ICI->getInversePredicate();
1675 else
1676 return nullptr;
1677
1678 const APInt *BinOpC;
1679 if (!match(BO, m_BinOp(m_Specific(V), m_APInt(BinOpC))))
1680 return nullptr;
1681
1683 .binaryOp(BO->getOpcode(), *BinOpC);
1684 if (R == *C) {
1686 return BO;
1687 }
1688 return nullptr;
1689}
1690
1691static Instruction *foldSelectICmpEq(SelectInst &SI, ICmpInst *ICI,
1692 InstCombinerImpl &IC) {
1693 ICmpInst::Predicate Pred = ICI->getPredicate();
1694 if (!ICmpInst::isEquality(Pred))
1695 return nullptr;
1696
1697 Value *TrueVal = SI.getTrueValue();
1698 Value *FalseVal = SI.getFalseValue();
1699 Value *CmpLHS = ICI->getOperand(0);
1700 Value *CmpRHS = ICI->getOperand(1);
1701
1702 if (Pred == ICmpInst::ICMP_NE)
1703 std::swap(TrueVal, FalseVal);
1704
1705 // Transform (X == C) ? X : Y -> (X == C) ? C : Y
1706 // specific handling for Bitwise operation.
1707 // x&y -> (x|y) ^ (x^y) or (x|y) & ~(x^y)
1708 // x|y -> (x&y) | (x^y) or (x&y) ^ (x^y)
1709 // x^y -> (x|y) ^ (x&y) or (x|y) & ~(x&y)
1710 Value *X, *Y;
1711 if (!match(CmpLHS, m_BitwiseLogic(m_Value(X), m_Value(Y))) ||
1713 return nullptr;
1714
1715 const unsigned AndOps = Instruction::And, OrOps = Instruction::Or,
1716 XorOps = Instruction::Xor, NoOps = 0;
1717 enum NotMask { None = 0, NotInner, NotRHS };
1718
1719 auto matchFalseVal = [&](unsigned OuterOpc, unsigned InnerOpc,
1720 unsigned NotMask) {
1721 auto matchInner = m_c_BinOp(InnerOpc, m_Specific(X), m_Specific(Y));
1722 if (OuterOpc == NoOps)
1723 return match(CmpRHS, m_Zero()) && match(FalseVal, matchInner);
1724
1725 if (NotMask == NotInner) {
1726 return match(FalseVal, m_c_BinOp(OuterOpc, m_NotForbidPoison(matchInner),
1727 m_Specific(CmpRHS)));
1728 } else if (NotMask == NotRHS) {
1729 return match(FalseVal, m_c_BinOp(OuterOpc, matchInner,
1730 m_NotForbidPoison(m_Specific(CmpRHS))));
1731 } else {
1732 return match(FalseVal,
1733 m_c_BinOp(OuterOpc, matchInner, m_Specific(CmpRHS)));
1734 }
1735 };
1736
1737 // (X&Y)==C ? X|Y : X^Y -> (X^Y)|C : X^Y or (X^Y)^ C : X^Y
1738 // (X&Y)==C ? X^Y : X|Y -> (X|Y)^C : X|Y or (X|Y)&~C : X|Y
1739 if (match(CmpLHS, m_And(m_Value(X), m_Value(Y)))) {
1740 if (match(TrueVal, m_c_Or(m_Specific(X), m_Specific(Y)))) {
1741 // (X&Y)==C ? X|Y : (X^Y)|C -> (X^Y)|C : (X^Y)|C -> (X^Y)|C
1742 // (X&Y)==C ? X|Y : (X^Y)^C -> (X^Y)^C : (X^Y)^C -> (X^Y)^C
1743 if (matchFalseVal(OrOps, XorOps, None) ||
1744 matchFalseVal(XorOps, XorOps, None))
1745 return IC.replaceInstUsesWith(SI, FalseVal);
1746 } else if (match(TrueVal, m_c_Xor(m_Specific(X), m_Specific(Y)))) {
1747 // (X&Y)==C ? X^Y : (X|Y)^ C -> (X|Y)^ C : (X|Y)^ C -> (X|Y)^ C
1748 // (X&Y)==C ? X^Y : (X|Y)&~C -> (X|Y)&~C : (X|Y)&~C -> (X|Y)&~C
1749 if (matchFalseVal(XorOps, OrOps, None) ||
1750 matchFalseVal(AndOps, OrOps, NotRHS))
1751 return IC.replaceInstUsesWith(SI, FalseVal);
1752 }
1753 }
1754
1755 // (X|Y)==C ? X&Y : X^Y -> (X^Y)^C : X^Y or ~(X^Y)&C : X^Y
1756 // (X|Y)==C ? X^Y : X&Y -> (X&Y)^C : X&Y or ~(X&Y)&C : X&Y
1757 if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y)))) {
1758 if (match(TrueVal, m_c_And(m_Specific(X), m_Specific(Y)))) {
1759 // (X|Y)==C ? X&Y: (X^Y)^C -> (X^Y)^C: (X^Y)^C -> (X^Y)^C
1760 // (X|Y)==C ? X&Y:~(X^Y)&C ->~(X^Y)&C:~(X^Y)&C -> ~(X^Y)&C
1761 if (matchFalseVal(XorOps, XorOps, None) ||
1762 matchFalseVal(AndOps, XorOps, NotInner))
1763 return IC.replaceInstUsesWith(SI, FalseVal);
1764 } else if (match(TrueVal, m_c_Xor(m_Specific(X), m_Specific(Y)))) {
1765 // (X|Y)==C ? X^Y : (X&Y)^C -> (X&Y)^C : (X&Y)^C -> (X&Y)^C
1766 // (X|Y)==C ? X^Y :~(X&Y)&C -> ~(X&Y)&C :~(X&Y)&C -> ~(X&Y)&C
1767 if (matchFalseVal(XorOps, AndOps, None) ||
1768 matchFalseVal(AndOps, AndOps, NotInner))
1769 return IC.replaceInstUsesWith(SI, FalseVal);
1770 }
1771 }
1772
1773 // (X^Y)==C ? X&Y : X|Y -> (X|Y)^C : X|Y or (X|Y)&~C : X|Y
1774 // (X^Y)==C ? X|Y : X&Y -> (X&Y)|C : X&Y or (X&Y)^ C : X&Y
1775 if (match(CmpLHS, m_Xor(m_Value(X), m_Value(Y)))) {
1776 if ((match(TrueVal, m_c_And(m_Specific(X), m_Specific(Y))))) {
1777 // (X^Y)==C ? X&Y : (X|Y)^C -> (X|Y)^C
1778 // (X^Y)==C ? X&Y : (X|Y)&~C -> (X|Y)&~C
1779 if (matchFalseVal(XorOps, OrOps, None) ||
1780 matchFalseVal(AndOps, OrOps, NotRHS))
1781 return IC.replaceInstUsesWith(SI, FalseVal);
1782 } else if (match(TrueVal, m_c_Or(m_Specific(X), m_Specific(Y)))) {
1783 // (X^Y)==C ? (X|Y) : (X&Y)|C -> (X&Y)|C
1784 // (X^Y)==C ? (X|Y) : (X&Y)^C -> (X&Y)^C
1785 if (matchFalseVal(OrOps, AndOps, None) ||
1786 matchFalseVal(XorOps, AndOps, None))
1787 return IC.replaceInstUsesWith(SI, FalseVal);
1788 }
1789 }
1790
1791 return nullptr;
1792}
1793
1794/// Visit a SelectInst that has an ICmpInst as its first operand.
1796 ICmpInst *ICI) {
1797 if (Instruction *NewSel = foldSelectValueEquivalence(SI, *ICI))
1798 return NewSel;
1799
1800 if (Value *V =
1801 canonicalizeSPF(*ICI, SI.getTrueValue(), SI.getFalseValue(), *this))
1802 return replaceInstUsesWith(SI, V);
1803
1804 if (Value *V = foldSelectInstWithICmpConst(SI, ICI, Builder))
1805 return replaceInstUsesWith(SI, V);
1806
1807 if (Value *V = canonicalizeClampLike(SI, *ICI, Builder, *this))
1808 return replaceInstUsesWith(SI, V);
1809
1810 if (Instruction *NewSel =
1811 tryToReuseConstantFromSelectInComparison(SI, *ICI, *this))
1812 return NewSel;
1813
1814 if (Value *V = foldSelectICmpAnd(SI, ICI, Builder))
1815 return replaceInstUsesWith(SI, V);
1816
1817 // NOTE: if we wanted to, this is where to detect integer MIN/MAX
1818 bool Changed = false;
1819 Value *TrueVal = SI.getTrueValue();
1820 Value *FalseVal = SI.getFalseValue();
1821 ICmpInst::Predicate Pred = ICI->getPredicate();
1822 Value *CmpLHS = ICI->getOperand(0);
1823 Value *CmpRHS = ICI->getOperand(1);
1824 if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS) && !isa<Constant>(CmpLHS)) {
1825 if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
1826 // Transform (X == C) ? X : Y -> (X == C) ? C : Y
1827 replaceOperand(SI, 1, CmpRHS);
1828 Changed = true;
1829 } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
1830 // Transform (X != C) ? Y : X -> (X != C) ? Y : C
1831 replaceOperand(SI, 2, CmpRHS);
1832 Changed = true;
1833 }
1834 }
1835
1836 if (Instruction *NewSel = foldSelectICmpEq(SI, ICI, *this))
1837 return NewSel;
1838
1839 // Canonicalize a signbit condition to use zero constant by swapping:
1840 // (CmpLHS > -1) ? TV : FV --> (CmpLHS < 0) ? FV : TV
1841 // To avoid conflicts (infinite loops) with other canonicalizations, this is
1842 // not applied with any constant select arm.
1843 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes()) &&
1844 !match(TrueVal, m_Constant()) && !match(FalseVal, m_Constant()) &&
1845 ICI->hasOneUse()) {
1848 Value *IsNeg = Builder.CreateIsNeg(CmpLHS, ICI->getName());
1849 replaceOperand(SI, 0, IsNeg);
1850 SI.swapValues();
1851 SI.swapProfMetadata();
1852 return &SI;
1853 }
1854
1855 // FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring
1856 // decomposeBitTestICmp() might help.
1857 if (TrueVal->getType()->isIntOrIntVectorTy()) {
1858 unsigned BitWidth =
1859 DL.getTypeSizeInBits(TrueVal->getType()->getScalarType());
1860 APInt MinSignedValue = APInt::getSignedMinValue(BitWidth);
1861 Value *X;
1862 const APInt *Y, *C;
1863 bool TrueWhenUnset;
1864 bool IsBitTest = false;
1865 if (ICmpInst::isEquality(Pred) &&
1866 match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) &&
1867 match(CmpRHS, m_Zero())) {
1868 IsBitTest = true;
1869 TrueWhenUnset = Pred == ICmpInst::ICMP_EQ;
1870 } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) {
1871 X = CmpLHS;
1872 Y = &MinSignedValue;
1873 IsBitTest = true;
1874 TrueWhenUnset = false;
1875 } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) {
1876 X = CmpLHS;
1877 Y = &MinSignedValue;
1878 IsBitTest = true;
1879 TrueWhenUnset = true;
1880 }
1881 if (IsBitTest) {
1882 Value *V = nullptr;
1883 // (X & Y) == 0 ? X : X ^ Y --> X & ~Y
1884 if (TrueWhenUnset && TrueVal == X &&
1885 match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1886 V = Builder.CreateAnd(X, ~(*Y));
1887 // (X & Y) != 0 ? X ^ Y : X --> X & ~Y
1888 else if (!TrueWhenUnset && FalseVal == X &&
1889 match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1890 V = Builder.CreateAnd(X, ~(*Y));
1891 // (X & Y) == 0 ? X ^ Y : X --> X | Y
1892 else if (TrueWhenUnset && FalseVal == X &&
1893 match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1894 V = Builder.CreateOr(X, *Y);
1895 // (X & Y) != 0 ? X : X ^ Y --> X | Y
1896 else if (!TrueWhenUnset && TrueVal == X &&
1897 match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1898 V = Builder.CreateOr(X, *Y);
1899
1900 if (V)
1901 return replaceInstUsesWith(SI, V);
1902 }
1903 }
1904
1905 if (Instruction *V =
1906 foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder))
1907 return V;
1908
1909 if (Value *V = foldSelectICmpAndZeroShl(ICI, TrueVal, FalseVal, Builder))
1910 return replaceInstUsesWith(SI, V);
1911
1912 if (Instruction *V = foldSelectCtlzToCttz(ICI, TrueVal, FalseVal, Builder))
1913 return V;
1914
1915 if (Instruction *V = foldSelectZeroOrOnes(ICI, TrueVal, FalseVal, Builder))
1916 return V;
1917
1918 if (Value *V = foldSelectICmpAndBinOp(ICI, TrueVal, FalseVal, Builder))
1919 return replaceInstUsesWith(SI, V);
1920
1921 if (Value *V = foldSelectICmpLshrAshr(ICI, TrueVal, FalseVal, Builder))
1922 return replaceInstUsesWith(SI, V);
1923
1924 if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder))
1925 return replaceInstUsesWith(SI, V);
1926
1927 if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder))
1928 return replaceInstUsesWith(SI, V);
1929
1930 if (Value *V = canonicalizeSaturatedAdd(ICI, TrueVal, FalseVal, Builder))
1931 return replaceInstUsesWith(SI, V);
1932
1933 if (Value *V = foldAbsDiff(ICI, TrueVal, FalseVal, Builder))
1934 return replaceInstUsesWith(SI, V);
1935
1936 return Changed ? &SI : nullptr;
1937}
1938
1939/// SI is a select whose condition is a PHI node (but the two may be in
1940/// different blocks). See if the true/false values (V) are live in all of the
1941/// predecessor blocks of the PHI. For example, cases like this can't be mapped:
1942///
1943/// X = phi [ C1, BB1], [C2, BB2]
1944/// Y = add
1945/// Z = select X, Y, 0
1946///
1947/// because Y is not live in BB1/BB2.
1948static bool canSelectOperandBeMappingIntoPredBlock(const Value *V,
1949 const SelectInst &SI) {
1950 // If the value is a non-instruction value like a constant or argument, it
1951 // can always be mapped.
1952 const Instruction *I = dyn_cast<Instruction>(V);
1953 if (!I) return true;
1954
1955 // If V is a PHI node defined in the same block as the condition PHI, we can
1956 // map the arguments.
1957 const PHINode *CondPHI = cast<PHINode>(SI.getCondition());
1958
1959 if (const PHINode *VP = dyn_cast<PHINode>(I))
1960 if (VP->getParent() == CondPHI->getParent())
1961 return true;
1962
1963 // Otherwise, if the PHI and select are defined in the same block and if V is
1964 // defined in a different block, then we can transform it.
1965 if (SI.getParent() == CondPHI->getParent() &&
1966 I->getParent() != CondPHI->getParent())
1967 return true;
1968
1969 // Otherwise we have a 'hard' case and we can't tell without doing more
1970 // detailed dominator based analysis, punt.
1971 return false;
1972}
1973
1974/// We have an SPF (e.g. a min or max) of an SPF of the form:
1975/// SPF2(SPF1(A, B), C)
1978 Value *B, Instruction &Outer,
1980 Value *C) {
1981 if (Outer.getType() != Inner->getType())
1982 return nullptr;
1983
1984 if (C == A || C == B) {
1985 // MAX(MAX(A, B), B) -> MAX(A, B)
1986 // MIN(MIN(a, b), a) -> MIN(a, b)
1987 // TODO: This could be done in instsimplify.
1988 if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1))
1989 return replaceInstUsesWith(Outer, Inner);
1990 }
1991
1992 return nullptr;
1993}
1994
1995/// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))).
1996/// This is even legal for FP.
1997static Instruction *foldAddSubSelect(SelectInst &SI,
1998 InstCombiner::BuilderTy &Builder) {
1999 Value *CondVal = SI.getCondition();
2000 Value *TrueVal = SI.getTrueValue();
2001 Value *FalseVal = SI.getFalseValue();
2002 auto *TI = dyn_cast<Instruction>(TrueVal);
2003 auto *FI = dyn_cast<Instruction>(FalseVal);
2004 if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse())
2005 return nullptr;
2006
2007 Instruction *AddOp = nullptr, *SubOp = nullptr;
2008 if ((TI->getOpcode() == Instruction::Sub &&
2009 FI->getOpcode() == Instruction::Add) ||
2010 (TI->getOpcode() == Instruction::FSub &&
2011 FI->getOpcode() == Instruction::FAdd)) {
2012 AddOp = FI;
2013 SubOp = TI;
2014 } else if ((FI->getOpcode() == Instruction::Sub &&
2015 TI->getOpcode() == Instruction::Add) ||
2016 (FI->getOpcode() == Instruction::FSub &&
2017 TI->getOpcode() == Instruction::FAdd)) {
2018 AddOp = TI;
2019 SubOp = FI;
2020 }
2021
2022 if (AddOp) {
2023 Value *OtherAddOp = nullptr;
2024 if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
2025 OtherAddOp = AddOp->getOperand(1);
2026 } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
2027 OtherAddOp = AddOp->getOperand(0);
2028 }
2029
2030 if (OtherAddOp) {
2031 // So at this point we know we have (Y -> OtherAddOp):
2032 // select C, (add X, Y), (sub X, Z)
2033 Value *NegVal; // Compute -Z
2034 if (SI.getType()->isFPOrFPVectorTy()) {
2035 NegVal = Builder.CreateFNeg(SubOp->getOperand(1));
2036 if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
2038 Flags &= SubOp->getFastMathFlags();
2039 NegInst->setFastMathFlags(Flags);
2040 }
2041 } else {
2042 NegVal = Builder.CreateNeg(SubOp->getOperand(1));
2043 }
2044
2045 Value *NewTrueOp = OtherAddOp;
2046 Value *NewFalseOp = NegVal;
2047 if (AddOp != TI)
2048 std::swap(NewTrueOp, NewFalseOp);
2049 Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp,
2050 SI.getName() + ".p", &SI);
2051
2052 if (SI.getType()->isFPOrFPVectorTy()) {
2053 Instruction *RI =
2054 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
2055
2057 Flags &= SubOp->getFastMathFlags();
2058 RI->setFastMathFlags(Flags);
2059 return RI;
2060 } else
2061 return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
2062 }
2063 }
2064 return nullptr;
2065}
2066
2067/// Turn X + Y overflows ? -1 : X + Y -> uadd_sat X, Y
2068/// And X - Y overflows ? 0 : X - Y -> usub_sat X, Y
2069/// Along with a number of patterns similar to:
2070/// X + Y overflows ? (X < 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2071/// X - Y overflows ? (X > 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2072static Instruction *
2073foldOverflowingAddSubSelect(SelectInst &SI, InstCombiner::BuilderTy &Builder) {
2074 Value *CondVal = SI.getCondition();
2075 Value *TrueVal = SI.getTrueValue();
2076 Value *FalseVal = SI.getFalseValue();
2077
2078 WithOverflowInst *II;
2079 if (!match(CondVal, m_ExtractValue<1>(m_WithOverflowInst(II))) ||
2080 !match(FalseVal, m_ExtractValue<0>(m_Specific(II))))
2081 return nullptr;
2082
2083 Value *X = II->getLHS();
2084 Value *Y = II->getRHS();
2085
2086 auto IsSignedSaturateLimit = [&](Value *Limit, bool IsAdd) {
2087 Type *Ty = Limit->getType();
2088
2090 Value *TrueVal, *FalseVal, *Op;
2091 const APInt *C;
2092 if (!match(Limit, m_Select(m_ICmp(Pred, m_Value(Op), m_APInt(C)),
2093 m_Value(TrueVal), m_Value(FalseVal))))
2094 return false;
2095
2096 auto IsZeroOrOne = [](const APInt &C) { return C.isZero() || C.isOne(); };
2097 auto IsMinMax = [&](Value *Min, Value *Max) {
2100 return match(Min, m_SpecificInt(MinVal)) &&
2101 match(Max, m_SpecificInt(MaxVal));
2102 };
2103
2104 if (Op != X && Op != Y)
2105 return false;
2106
2107 if (IsAdd) {
2108 // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2109 // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2110 // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2111 // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2112 if (Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) &&
2113 IsMinMax(TrueVal, FalseVal))
2114 return true;
2115 // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2116 // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2117 // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2118 // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2119 if (Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) &&
2120 IsMinMax(FalseVal, TrueVal))
2121 return true;
2122 } else {
2123 // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2124 // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2125 if (Op == X && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C + 1) &&
2126 IsMinMax(TrueVal, FalseVal))
2127 return true;
2128 // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2129 // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2130 if (Op == X && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 2) &&
2131 IsMinMax(FalseVal, TrueVal))
2132 return true;
2133 // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2134 // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2135 if (Op == Y && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) &&
2136 IsMinMax(FalseVal, TrueVal))
2137 return true;
2138 // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2139 // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2140 if (Op == Y && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) &&
2141 IsMinMax(TrueVal, FalseVal))
2142 return true;
2143 }
2144
2145 return false;
2146 };
2147
2148 Intrinsic::ID NewIntrinsicID;
2149 if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow &&
2150 match(TrueVal, m_AllOnes()))
2151 // X + Y overflows ? -1 : X + Y -> uadd_sat X, Y
2152 NewIntrinsicID = Intrinsic::uadd_sat;
2153 else if (II->getIntrinsicID() == Intrinsic::usub_with_overflow &&
2154 match(TrueVal, m_Zero()))
2155 // X - Y overflows ? 0 : X - Y -> usub_sat X, Y
2156 NewIntrinsicID = Intrinsic::usub_sat;
2157 else if (II->getIntrinsicID() == Intrinsic::sadd_with_overflow &&
2158 IsSignedSaturateLimit(TrueVal, /*IsAdd=*/true))
2159 // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2160 // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2161 // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2162 // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2163 // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2164 // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
2165 // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2166 // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
2167 NewIntrinsicID = Intrinsic::sadd_sat;
2168 else if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow &&
2169 IsSignedSaturateLimit(TrueVal, /*IsAdd=*/false))
2170 // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2171 // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2172 // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2173 // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2174 // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2175 // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
2176 // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2177 // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
2178 NewIntrinsicID = Intrinsic::ssub_sat;
2179 else
2180 return nullptr;
2181
2182 Function *F =
2183 Intrinsic::getDeclaration(SI.getModule(), NewIntrinsicID, SI.getType());
2184 return CallInst::Create(F, {X, Y});
2185}
2186
2188 Constant *C;
2189 if (!match(Sel.getTrueValue(), m_Constant(C)) &&
2190 !match(Sel.getFalseValue(), m_Constant(C)))
2191 return nullptr;
2192
2193 Instruction *ExtInst;
2194 if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) &&
2195 !match(Sel.getFalseValue(), m_Instruction(ExtInst)))
2196 return nullptr;
2197
2198 auto ExtOpcode = ExtInst->getOpcode();
2199 if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt)
2200 return nullptr;
2201
2202 // If we are extending from a boolean type or if we can create a select that
2203 // has the same size operands as its condition, try to narrow the select.
2204 Value *X = ExtInst->getOperand(0);
2205 Type *SmallType = X->getType();
2206 Value *Cond = Sel.getCondition();
2207 auto *Cmp = dyn_cast<CmpInst>(Cond);
2208 if (!SmallType->isIntOrIntVectorTy(1) &&
2209 (!Cmp || Cmp->getOperand(0)->getType() != SmallType))
2210 return nullptr;
2211
2212 // If the constant is the same after truncation to the smaller type and
2213 // extension to the original type, we can narrow the select.
2214 Type *SelType = Sel.getType();
2215 Constant *TruncC = getLosslessTrunc(C, SmallType, ExtOpcode);
2216 if (TruncC && ExtInst->hasOneUse()) {
2217 Value *TruncCVal = cast<Value>(TruncC);
2218 if (ExtInst == Sel.getFalseValue())
2219 std::swap(X, TruncCVal);
2220
2221 // select Cond, (ext X), C --> ext(select Cond, X, C')
2222 // select Cond, C, (ext X) --> ext(select Cond, C', X)
2223 Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel);
2224 return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType);
2225 }
2226
2227 return nullptr;
2228}
2229
2230/// Try to transform a vector select with a constant condition vector into a
2231/// shuffle for easier combining with other shuffles and insert/extract.
2232static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) {
2233 Value *CondVal = SI.getCondition();
2234 Constant *CondC;
2235 auto *CondValTy = dyn_cast<FixedVectorType>(CondVal->getType());
2236 if (!CondValTy || !match(CondVal, m_Constant(CondC)))
2237 return nullptr;
2238
2239 unsigned NumElts = CondValTy->getNumElements();
2241 Mask.reserve(NumElts);
2242 for (unsigned i = 0; i != NumElts; ++i) {
2243 Constant *Elt = CondC->getAggregateElement(i);
2244 if (!Elt)
2245 return nullptr;
2246
2247 if (Elt->isOneValue()) {
2248 // If the select condition element is true, choose from the 1st vector.
2249 Mask.push_back(i);
2250 } else if (Elt->isNullValue()) {
2251 // If the select condition element is false, choose from the 2nd vector.
2252 Mask.push_back(i + NumElts);
2253 } else if (isa<UndefValue>(Elt)) {
2254 // Undef in a select condition (choose one of the operands) does not mean
2255 // the same thing as undef in a shuffle mask (any value is acceptable), so
2256 // give up.
2257 return nullptr;
2258 } else {
2259 // Bail out on a constant expression.
2260 return nullptr;
2261 }
2262 }
2263
2264 return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(), Mask);
2265}
2266
2267/// If we have a select of vectors with a scalar condition, try to convert that
2268/// to a vector select by splatting the condition. A splat may get folded with
2269/// other operations in IR and having all operands of a select be vector types
2270/// is likely better for vector codegen.
2271static Instruction *canonicalizeScalarSelectOfVecs(SelectInst &Sel,
2272 InstCombinerImpl &IC) {
2273 auto *Ty = dyn_cast<VectorType>(Sel.getType());
2274 if (!Ty)
2275 return nullptr;
2276
2277 // We can replace a single-use extract with constant index.
2278 Value *Cond = Sel.getCondition();
2280 return nullptr;
2281
2282 // select (extelt V, Index), T, F --> select (splat V, Index), T, F
2283 // Splatting the extracted condition reduces code (we could directly create a
2284 // splat shuffle of the source vector to eliminate the intermediate step).
2285 return IC.replaceOperand(
2286 Sel, 0, IC.Builder.CreateVectorSplat(Ty->getElementCount(), Cond));
2287}
2288
2289/// Reuse bitcasted operands between a compare and select:
2290/// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
2291/// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D))
2292static Instruction *foldSelectCmpBitcasts(SelectInst &Sel,
2293 InstCombiner::BuilderTy &Builder) {
2294 Value *Cond = Sel.getCondition();
2295 Value *TVal = Sel.getTrueValue();
2296 Value *FVal = Sel.getFalseValue();
2297
2298 CmpInst::Predicate Pred;
2299 Value *A, *B;
2300 if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B))))
2301 return nullptr;
2302
2303 // The select condition is a compare instruction. If the select's true/false
2304 // values are already the same as the compare operands, there's nothing to do.
2305 if (TVal == A || TVal == B || FVal == A || FVal == B)
2306 return nullptr;
2307
2308 Value *C, *D;
2309 if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D))))
2310 return nullptr;
2311
2312 // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc)
2313 Value *TSrc, *FSrc;
2314 if (!match(TVal, m_BitCast(m_Value(TSrc))) ||
2315 !match(FVal, m_BitCast(m_Value(FSrc))))
2316 return nullptr;
2317
2318 // If the select true/false values are *different bitcasts* of the same source
2319 // operands, make the select operands the same as the compare operands and
2320 // cast the result. This is the canonical select form for min/max.
2321 Value *NewSel;
2322 if (TSrc == C && FSrc == D) {
2323 // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
2324 // bitcast (select (cmp A, B), A, B)
2325 NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel);
2326 } else if (TSrc == D && FSrc == C) {
2327 // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) -->
2328 // bitcast (select (cmp A, B), B, A)
2329 NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel);
2330 } else {
2331 return nullptr;
2332 }
2333 return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType());
2334}
2335
2336/// Try to eliminate select instructions that test the returned flag of cmpxchg
2337/// instructions.
2338///
2339/// If a select instruction tests the returned flag of a cmpxchg instruction and
2340/// selects between the returned value of the cmpxchg instruction its compare
2341/// operand, the result of the select will always be equal to its false value.
2342/// For example:
2343///
2344/// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
2345/// %1 = extractvalue { i64, i1 } %0, 1
2346/// %2 = extractvalue { i64, i1 } %0, 0
2347/// %3 = select i1 %1, i64 %compare, i64 %2
2348/// ret i64 %3
2349///
2350/// The returned value of the cmpxchg instruction (%2) is the original value
2351/// located at %ptr prior to any update. If the cmpxchg operation succeeds, %2
2352/// must have been equal to %compare. Thus, the result of the select is always
2353/// equal to %2, and the code can be simplified to:
2354///
2355/// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
2356/// %1 = extractvalue { i64, i1 } %0, 0
2357/// ret i64 %1
2358///
2359static Value *foldSelectCmpXchg(SelectInst &SI) {
2360 // A helper that determines if V is an extractvalue instruction whose
2361 // aggregate operand is a cmpxchg instruction and whose single index is equal
2362 // to I. If such conditions are true, the helper returns the cmpxchg
2363 // instruction; otherwise, a nullptr is returned.
2364 auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * {
2365 auto *Extract = dyn_cast<ExtractValueInst>(V);
2366 if (!Extract)
2367 return nullptr;
2368 if (Extract->getIndices()[0] != I)
2369 return nullptr;
2370 return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand());
2371 };
2372
2373 // If the select has a single user, and this user is a select instruction that
2374 // we can simplify, skip the cmpxchg simplification for now.
2375 if (SI.hasOneUse())
2376 if (auto *Select = dyn_cast<SelectInst>(SI.user_back()))
2377 if (Select->getCondition() == SI.getCondition())
2378 if (Select->getFalseValue() == SI.getTrueValue() ||
2379 Select->getTrueValue() == SI.getFalseValue())
2380 return nullptr;
2381
2382 // Ensure the select condition is the returned flag of a cmpxchg instruction.
2383 auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1);
2384 if (!CmpXchg)
2385 return nullptr;
2386
2387 // Check the true value case: The true value of the select is the returned
2388 // value of the same cmpxchg used by the condition, and the false value is the
2389 // cmpxchg instruction's compare operand.
2390 if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0))
2391 if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue())
2392 return SI.getFalseValue();
2393
2394 // Check the false value case: The false value of the select is the returned
2395 // value of the same cmpxchg used by the condition, and the true value is the
2396 // cmpxchg instruction's compare operand.
2397 if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0))
2398 if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue())
2399 return SI.getFalseValue();
2400
2401 return nullptr;
2402}
2403
2404/// Try to reduce a funnel/rotate pattern that includes a compare and select
2405/// into a funnel shift intrinsic. Example:
2406/// rotl32(a, b) --> (b == 0 ? a : ((a >> (32 - b)) | (a << b)))
2407/// --> call llvm.fshl.i32(a, a, b)
2408/// fshl32(a, b, c) --> (c == 0 ? a : ((b >> (32 - c)) | (a << c)))
2409/// --> call llvm.fshl.i32(a, b, c)
2410/// fshr32(a, b, c) --> (c == 0 ? b : ((a >> (32 - c)) | (b << c)))
2411/// --> call llvm.fshr.i32(a, b, c)
2412static Instruction *foldSelectFunnelShift(SelectInst &Sel,
2413 InstCombiner::BuilderTy &Builder) {
2414 // This must be a power-of-2 type for a bitmasking transform to be valid.
2415 unsigned Width = Sel.getType()->getScalarSizeInBits();
2416 if (!isPowerOf2_32(Width))
2417 return nullptr;
2418
2419 BinaryOperator *Or0, *Or1;
2420 if (!match(Sel.getFalseValue(), m_OneUse(m_Or(m_BinOp(Or0), m_BinOp(Or1)))))
2421 return nullptr;
2422
2423 Value *SV0, *SV1, *SA0, *SA1;
2424 if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(SV0),
2425 m_ZExtOrSelf(m_Value(SA0))))) ||
2427 m_ZExtOrSelf(m_Value(SA1))))) ||
2428 Or0->getOpcode() == Or1->getOpcode())
2429 return nullptr;
2430
2431 // Canonicalize to or(shl(SV0, SA0), lshr(SV1, SA1)).
2432 if (Or0->getOpcode() == BinaryOperator::LShr) {
2433 std::swap(Or0, Or1);
2434 std::swap(SV0, SV1);
2435 std::swap(SA0, SA1);
2436 }
2437 assert(Or0->getOpcode() == BinaryOperator::Shl &&
2438 Or1->getOpcode() == BinaryOperator::LShr &&
2439 "Illegal or(shift,shift) pair");
2440
2441 // Check the shift amounts to see if they are an opposite pair.
2442 Value *ShAmt;
2443 if (match(SA1, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA0)))))
2444 ShAmt = SA0;
2445 else if (match(SA0, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA1)))))
2446 ShAmt = SA1;
2447 else
2448 return nullptr;
2449
2450 // We should now have this pattern:
2451 // select ?, TVal, (or (shl SV0, SA0), (lshr SV1, SA1))
2452 // The false value of the select must be a funnel-shift of the true value:
2453 // IsFShl -> TVal must be SV0 else TVal must be SV1.
2454 bool IsFshl = (ShAmt == SA0);
2455 Value *TVal = Sel.getTrueValue();
2456 if ((IsFshl && TVal != SV0) || (!IsFshl && TVal != SV1))
2457 return nullptr;
2458
2459 // Finally, see if the select is filtering out a shift-by-zero.
2460 Value *Cond = Sel.getCondition();
2462 if (!match(Cond, m_OneUse(m_ICmp(Pred, m_Specific(ShAmt), m_ZeroInt()))) ||
2463 Pred != ICmpInst::ICMP_EQ)
2464 return nullptr;
2465
2466 // If this is not a rotate then the select was blocking poison from the
2467 // 'shift-by-zero' non-TVal, but a funnel shift won't - so freeze it.
2468 if (SV0 != SV1) {
2469 if (IsFshl && !llvm::isGuaranteedNotToBePoison(SV1))
2470 SV1 = Builder.CreateFreeze(SV1);
2471 else if (!IsFshl && !llvm::isGuaranteedNotToBePoison(SV0))
2472 SV0 = Builder.CreateFreeze(SV0);
2473 }
2474
2475 // This is a funnel/rotate that avoids shift-by-bitwidth UB in a suboptimal way.
2476 // Convert to funnel shift intrinsic.
2477 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
2479 ShAmt = Builder.CreateZExt(ShAmt, Sel.getType());
2480 return CallInst::Create(F, { SV0, SV1, ShAmt });
2481}
2482
2483static Instruction *foldSelectToCopysign(SelectInst &Sel,
2484 InstCombiner::BuilderTy &Builder) {
2485 Value *Cond = Sel.getCondition();
2486 Value *TVal = Sel.getTrueValue();
2487 Value *FVal = Sel.getFalseValue();
2488 Type *SelType = Sel.getType();
2489
2490 // Match select ?, TC, FC where the constants are equal but negated.
2491 // TODO: Generalize to handle a negated variable operand?
2492 const APFloat *TC, *FC;
2493 if (!match(TVal, m_APFloatAllowPoison(TC)) ||
2494 !match(FVal, m_APFloatAllowPoison(FC)) ||
2495 !abs(*TC).bitwiseIsEqual(abs(*FC)))
2496 return nullptr;
2497
2498 assert(TC != FC && "Expected equal select arms to simplify");
2499
2500 Value *X;
2501 const APInt *C;
2502 bool IsTrueIfSignSet;
2505 m_APInt(C)))) ||
2506 !isSignBitCheck(Pred, *C, IsTrueIfSignSet) || X->getType() != SelType)
2507 return nullptr;
2508
2509 // If needed, negate the value that will be the sign argument of the copysign:
2510 // (bitcast X) < 0 ? -TC : TC --> copysign(TC, X)
2511 // (bitcast X) < 0 ? TC : -TC --> copysign(TC, -X)
2512 // (bitcast X) >= 0 ? -TC : TC --> copysign(TC, -X)
2513 // (bitcast X) >= 0 ? TC : -TC --> copysign(TC, X)
2514 // Note: FMF from the select can not be propagated to the new instructions.
2515 if (IsTrueIfSignSet ^ TC->isNegative())
2516 X = Builder.CreateFNeg(X);
2517
2518 // Canonicalize the magnitude argument as the positive constant since we do
2519 // not care about its sign.
2520 Value *MagArg = ConstantFP::get(SelType, abs(*TC));
2521 Function *F = Intrinsic::getDeclaration(Sel.getModule(), Intrinsic::copysign,
2522 Sel.getType());
2523 return CallInst::Create(F, { MagArg, X });
2524}
2525
2527 if (!isa<VectorType>(Sel.getType()))
2528 return nullptr;
2529
2530 Value *Cond = Sel.getCondition();
2531 Value *TVal = Sel.getTrueValue();
2532 Value *FVal = Sel.getFalseValue();
2533 Value *C, *X, *Y;
2534
2535 if (match(Cond, m_VecReverse(m_Value(C)))) {
2536 auto createSelReverse = [&](Value *C, Value *X, Value *Y) {
2537 Value *V = Builder.CreateSelect(C, X, Y, Sel.getName(), &Sel);
2538 if (auto *I = dyn_cast<Instruction>(V))
2539 I->copyIRFlags(&Sel);
2540 Module *M = Sel.getModule();
2541 Function *F =
2542 Intrinsic::getDeclaration(M, Intrinsic::vector_reverse, V->getType());
2543 return CallInst::Create(F, V);
2544 };
2545
2546 if (match(TVal, m_VecReverse(m_Value(X)))) {
2547 // select rev(C), rev(X), rev(Y) --> rev(select C, X, Y)
2548 if (match(FVal, m_VecReverse(m_Value(Y))) &&
2549 (Cond->hasOneUse() || TVal->hasOneUse() || FVal->hasOneUse()))
2550 return createSelReverse(C, X, Y);
2551
2552 // select rev(C), rev(X), FValSplat --> rev(select C, X, FValSplat)
2553 if ((Cond->hasOneUse() || TVal->hasOneUse()) && isSplatValue(FVal))
2554 return createSelReverse(C, X, FVal);
2555 }
2556 // select rev(C), TValSplat, rev(Y) --> rev(select C, TValSplat, Y)
2557 else if (isSplatValue(TVal) && match(FVal, m_VecReverse(m_Value(Y))) &&
2558 (Cond->hasOneUse() || FVal->hasOneUse()))
2559 return createSelReverse(C, TVal, Y);
2560 }
2561
2562 auto *VecTy = dyn_cast<FixedVectorType>(Sel.getType());
2563 if (!VecTy)
2564 return nullptr;
2565
2566 unsigned NumElts = VecTy->getNumElements();
2567 APInt PoisonElts(NumElts, 0);
2568 APInt AllOnesEltMask(APInt::getAllOnes(NumElts));
2569 if (Value *V = SimplifyDemandedVectorElts(&Sel, AllOnesEltMask, PoisonElts)) {
2570 if (V != &Sel)
2571 return replaceInstUsesWith(Sel, V);
2572 return &Sel;
2573 }
2574
2575 // A select of a "select shuffle" with a common operand can be rearranged
2576 // to select followed by "select shuffle". Because of poison, this only works
2577 // in the case of a shuffle with no undefined mask elements.
2579 if (match(TVal, m_OneUse(m_Shuffle(m_Value(X), m_Value(Y), m_Mask(Mask)))) &&
2580 !is_contained(Mask, PoisonMaskElem) &&
2581 cast<ShuffleVectorInst>(TVal)->isSelect()) {
2582 if (X == FVal) {
2583 // select Cond, (shuf_sel X, Y), X --> shuf_sel X, (select Cond, Y, X)
2584 Value *NewSel = Builder.CreateSelect(Cond, Y, X, "sel", &Sel);
2585 return new ShuffleVectorInst(X, NewSel, Mask);
2586 }
2587 if (Y == FVal) {
2588 // select Cond, (shuf_sel X, Y), Y --> shuf_sel (select Cond, X, Y), Y
2589 Value *NewSel = Builder.CreateSelect(Cond, X, Y, "sel", &Sel);
2590 return new ShuffleVectorInst(NewSel, Y, Mask);
2591 }
2592 }
2593 if (match(FVal, m_OneUse(m_Shuffle(m_Value(X), m_Value(Y), m_Mask(Mask)))) &&
2594 !is_contained(Mask, PoisonMaskElem) &&
2595 cast<ShuffleVectorInst>(FVal)->isSelect()) {
2596 if (X == TVal) {
2597 // select Cond, X, (shuf_sel X, Y) --> shuf_sel X, (select Cond, X, Y)
2598 Value *NewSel = Builder.CreateSelect(Cond, X, Y, "sel", &Sel);
2599 return new ShuffleVectorInst(X, NewSel, Mask);
2600 }
2601 if (Y == TVal) {
2602 // select Cond, Y, (shuf_sel X, Y) --> shuf_sel (select Cond, Y, X), Y
2603 Value *NewSel = Builder.CreateSelect(Cond, Y, X, "sel", &Sel);
2604 return new ShuffleVectorInst(NewSel, Y, Mask);
2605 }
2606 }
2607
2608 return nullptr;
2609}
2610
2611static Instruction *foldSelectToPhiImpl(SelectInst &Sel, BasicBlock *BB,
2612 const DominatorTree &DT,
2613 InstCombiner::BuilderTy &Builder) {
2614 // Find the block's immediate dominator that ends with a conditional branch
2615 // that matches select's condition (maybe inverted).
2616 auto *IDomNode = DT[BB]->getIDom();
2617 if (!IDomNode)
2618 return nullptr;
2619 BasicBlock *IDom = IDomNode->getBlock();
2620
2621 Value *Cond = Sel.getCondition();
2622 Value *IfTrue, *IfFalse;
2623 BasicBlock *TrueSucc, *FalseSucc;
2624 if (match(IDom->getTerminator(),
2625 m_Br(m_Specific(Cond), m_BasicBlock(TrueSucc),
2626 m_BasicBlock(FalseSucc)))) {
2627 IfTrue = Sel.getTrueValue();
2628 IfFalse = Sel.getFalseValue();
2629 } else if (match(IDom->getTerminator(),
2630 m_Br(m_Not(m_Specific(Cond)), m_BasicBlock(TrueSucc),
2631 m_BasicBlock(FalseSucc)))) {
2632 IfTrue = Sel.getFalseValue();
2633 IfFalse = Sel.getTrueValue();
2634 } else
2635 return nullptr;
2636
2637 // Make sure the branches are actually different.
2638 if (TrueSucc == FalseSucc)
2639 return nullptr;
2640
2641 // We want to replace select %cond, %a, %b with a phi that takes value %a
2642 // for all incoming edges that are dominated by condition `%cond == true`,
2643 // and value %b for edges dominated by condition `%cond == false`. If %a
2644 // or %b are also phis from the same basic block, we can go further and take
2645 // their incoming values from the corresponding blocks.
2646 BasicBlockEdge TrueEdge(IDom, TrueSucc);
2647 BasicBlockEdge FalseEdge(IDom, FalseSucc);
2649 for (auto *Pred : predecessors(BB)) {
2650 // Check implication.
2651 BasicBlockEdge Incoming(Pred, BB);
2652 if (DT.dominates(TrueEdge, Incoming))
2653 Inputs[Pred] = IfTrue->DoPHITranslation(BB, Pred);
2654 else if (DT.dominates(FalseEdge, Incoming))
2655 Inputs[Pred] = IfFalse->DoPHITranslation(BB, Pred);
2656 else
2657 return nullptr;
2658 // Check availability.
2659 if (auto *Insn = dyn_cast<Instruction>(Inputs[Pred]))
2660 if (!DT.dominates(Insn, Pred->getTerminator()))
2661 return nullptr;
2662 }
2663
2664 Builder.SetInsertPoint(BB, BB->begin());
2665 auto *PN = Builder.CreatePHI(Sel.getType(), Inputs.size());
2666 for (auto *Pred : predecessors(BB))
2667 PN->addIncoming(Inputs[Pred], Pred);
2668 PN->takeName(&Sel);
2669 return PN;
2670}
2671
2672static Instruction *foldSelectToPhi(SelectInst &Sel, const DominatorTree &DT,
2673 InstCombiner::BuilderTy &Builder) {
2674 // Try to replace this select with Phi in one of these blocks.
2675 SmallSetVector<BasicBlock *, 4> CandidateBlocks;
2676 CandidateBlocks.insert(Sel.getParent());
2677 for (Value *V : Sel.operands())
2678 if (auto *I = dyn_cast<Instruction>(V))
2679 CandidateBlocks.insert(I->getParent());
2680
2681 for (BasicBlock *BB : CandidateBlocks)
2682 if (auto *PN = foldSelectToPhiImpl(Sel, BB, DT, Builder))
2683 return PN;
2684 return nullptr;
2685}
2686
2687/// Tries to reduce a pattern that arises when calculating the remainder of the
2688/// Euclidean division. When the divisor is a power of two and is guaranteed not
2689/// to be negative, a signed remainder can be folded with a bitwise and.
2690///
2691/// (x % n) < 0 ? (x % n) + n : (x % n)
2692/// -> x & (n - 1)
2693static Instruction *foldSelectWithSRem(SelectInst &SI, InstCombinerImpl &IC,
2694 IRBuilderBase &Builder) {
2695 Value *CondVal = SI.getCondition();
2696 Value *TrueVal = SI.getTrueValue();
2697 Value *FalseVal = SI.getFalseValue();
2698
2700 Value *Op, *RemRes, *Remainder;
2701 const APInt *C;
2702 bool TrueIfSigned = false;
2703
2704 if (!(match(CondVal, m_ICmp(Pred, m_Value(RemRes), m_APInt(C))) &&
2705 isSignBitCheck(Pred, *C, TrueIfSigned)))
2706 return nullptr;
2707
2708 // If the sign bit is not set, we have a SGE/SGT comparison, and the operands
2709 // of the select are inverted.
2710 if (!TrueIfSigned)
2711 std::swap(TrueVal, FalseVal);
2712
2713 auto FoldToBitwiseAnd = [&](Value *Remainder) -> Instruction * {
2714 Value *Add = Builder.CreateAdd(
2715 Remainder, Constant::getAllOnesValue(RemRes->getType()));
2716 return BinaryOperator::CreateAnd(Op, Add);
2717 };
2718
2719 // Match the general case:
2720 // %rem = srem i32 %x, %n
2721 // %cnd = icmp slt i32 %rem, 0
2722 // %add = add i32 %rem, %n
2723 // %sel = select i1 %cnd, i32 %add, i32 %rem
2724 if (match(TrueVal, m_Add(m_Specific(RemRes), m_Value(Remainder))) &&
2725 match(RemRes, m_SRem(m_Value(Op), m_Specific(Remainder))) &&
2726 IC.isKnownToBeAPowerOfTwo(Remainder, /*OrZero*/ true) &&
2727 FalseVal == RemRes)
2728 return FoldToBitwiseAnd(Remainder);
2729
2730 // Match the case where the one arm has been replaced by constant 1:
2731 // %rem = srem i32 %n, 2
2732 // %cnd = icmp slt i32 %rem, 0
2733 // %sel = select i1 %cnd, i32 1, i32 %rem
2734 if (match(TrueVal, m_One()) &&
2735 match(RemRes, m_SRem(m_Value(Op), m_SpecificInt(2))) &&
2736 FalseVal == RemRes)
2737 return FoldToBitwiseAnd(ConstantInt::get(RemRes->getType(), 2));
2738
2739 return nullptr;
2740}
2741
2742static Value *foldSelectWithFrozenICmp(SelectInst &Sel, InstCombiner::BuilderTy &Builder) {
2743 FreezeInst *FI = dyn_cast<FreezeInst>(Sel.getCondition());
2744 if (!FI)
2745 return nullptr;
2746
2747 Value *Cond = FI->getOperand(0);
2748 Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue();
2749
2750 // select (freeze(x == y)), x, y --> y
2751 // select (freeze(x != y)), x, y --> x
2752 // The freeze should be only used by this select. Otherwise, remaining uses of
2753 // the freeze can observe a contradictory value.
2754 // c = freeze(x == y) ; Let's assume that y = poison & x = 42; c is 0 or 1
2755 // a = select c, x, y ;
2756 // f(a, c) ; f(poison, 1) cannot happen, but if a is folded
2757 // ; to y, this can happen.
2758 CmpInst::Predicate Pred;
2759 if (FI->hasOneUse() &&
2760 match(Cond, m_c_ICmp(Pred, m_Specific(TrueVal), m_Specific(FalseVal))) &&
2761 (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)) {
2762 return Pred == ICmpInst::ICMP_EQ ? FalseVal : TrueVal;
2763 }
2764
2765 return nullptr;
2766}
2767
2768/// Given that \p CondVal is known to be \p CondIsTrue, try to simplify \p SI.
2769static Value *simplifyNestedSelectsUsingImpliedCond(SelectInst &SI,
2770 Value *CondVal,
2771 bool CondIsTrue,
2772 const DataLayout &DL) {
2773 Value *InnerCondVal = SI.getCondition();
2774 Value *InnerTrueVal = SI.getTrueValue();
2775 Value *InnerFalseVal = SI.getFalseValue();
2776 assert(CondVal->getType() == InnerCondVal->getType() &&
2777 "The type of inner condition must match with the outer.");
2778 if (auto Implied = isImpliedCondition(CondVal, InnerCondVal, DL, CondIsTrue))
2779 return *Implied ? InnerTrueVal : InnerFalseVal;
2780 return nullptr;
2781}
2782
2783Instruction *InstCombinerImpl::foldAndOrOfSelectUsingImpliedCond(Value *Op,
2784 SelectInst &SI,
2785 bool IsAnd) {
2786 assert(Op->getType()->isIntOrIntVectorTy(1) &&
2787 "Op must be either i1 or vector of i1.");
2788 if (SI.getCondition()->getType() != Op->getType())
2789 return nullptr;
2790 if (Value *V = simplifyNestedSelectsUsingImpliedCond(SI, Op, IsAnd, DL))
2791 return SelectInst::Create(Op,
2792 IsAnd ? V : ConstantInt::getTrue(Op->getType()),
2793 IsAnd ? ConstantInt::getFalse(Op->getType()) : V);
2794 return nullptr;
2795}
2796
2797// Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need
2798// fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work.
2799static Instruction *foldSelectWithFCmpToFabs(SelectInst &SI,
2800 InstCombinerImpl &IC) {
2801 Value *CondVal = SI.getCondition();
2802
2803 bool ChangedFMF = false;
2804 for (bool Swap : {false, true}) {
2805 Value *TrueVal = SI.getTrueValue();
2806 Value *X = SI.getFalseValue();
2807 CmpInst::Predicate Pred;
2808
2809 if (Swap)
2810 std::swap(TrueVal, X);
2811
2812 if (!match(CondVal, m_FCmp(Pred, m_Specific(X), m_AnyZeroFP())))
2813 continue;
2814
2815 // fold (X <= +/-0.0) ? (0.0 - X) : X to fabs(X), when 'Swap' is false
2816 // fold (X > +/-0.0) ? X : (0.0 - X) to fabs(X), when 'Swap' is true
2817 if (match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X)))) {
2818 if (!Swap && (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
2819 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
2820 return IC.replaceInstUsesWith(SI, Fabs);
2821 }
2822 if (Swap && (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) {
2823 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
2824 return IC.replaceInstUsesWith(SI, Fabs);
2825 }
2826 }
2827
2828 if (!match(TrueVal, m_FNeg(m_Specific(X))))
2829 return nullptr;
2830
2831 // Forward-propagate nnan and ninf from the fneg to the select.
2832 // If all inputs are not those values, then the select is not either.
2833 // Note: nsz is defined differently, so it may not be correct to propagate.
2834 FastMathFlags FMF = cast<FPMathOperator>(TrueVal)->getFastMathFlags();
2835 if (FMF.noNaNs() && !SI.hasNoNaNs()) {
2836 SI.setHasNoNaNs(true);
2837 ChangedFMF = true;
2838 }
2839 if (FMF.noInfs() && !SI.hasNoInfs()) {
2840 SI.setHasNoInfs(true);
2841 ChangedFMF = true;
2842 }
2843
2844 // With nsz, when 'Swap' is false:
2845 // fold (X < +/-0.0) ? -X : X or (X <= +/-0.0) ? -X : X to fabs(X)
2846 // fold (X > +/-0.0) ? -X : X or (X >= +/-0.0) ? -X : X to -fabs(x)
2847 // when 'Swap' is true:
2848 // fold (X > +/-0.0) ? X : -X or (X >= +/-0.0) ? X : -X to fabs(X)
2849 // fold (X < +/-0.0) ? X : -X or (X <= +/-0.0) ? X : -X to -fabs(X)
2850 //
2851 // Note: We require "nnan" for this fold because fcmp ignores the signbit
2852 // of NAN, but IEEE-754 specifies the signbit of NAN values with
2853 // fneg/fabs operations.
2854 if (!SI.hasNoSignedZeros() || !SI.hasNoNaNs())
2855 return nullptr;
2856
2857 if (Swap)
2858 Pred = FCmpInst::getSwappedPredicate(Pred);
2859
2860 bool IsLTOrLE = Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
2861 Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE;
2862 bool IsGTOrGE = Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
2863 Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE;
2864
2865 if (IsLTOrLE) {
2866 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
2867 return IC.replaceInstUsesWith(SI, Fabs);
2868 }
2869 if (IsGTOrGE) {
2870 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
2871 Instruction *NewFNeg = UnaryOperator::CreateFNeg(Fabs);
2872 NewFNeg->setFastMathFlags(SI.getFastMathFlags());
2873 return NewFNeg;
2874 }
2875 }
2876
2877 // Match select with (icmp slt (bitcast X to int), 0)
2878 // or (icmp sgt (bitcast X to int), -1)
2879
2880 for (bool Swap : {false, true}) {
2881 Value *TrueVal = SI.getTrueValue();
2882 Value *X = SI.getFalseValue();
2883
2884 if (Swap)
2885 std::swap(TrueVal, X);
2886
2887 CmpInst::Predicate Pred;
2888 const APInt *C;
2889 bool TrueIfSigned;
2890 if (!match(CondVal,
2892 !isSignBitCheck(Pred, *C, TrueIfSigned))
2893 continue;
2894 if (!match(TrueVal, m_FNeg(m_Specific(X))))
2895 return nullptr;
2896 if (Swap == TrueIfSigned && !CondVal->hasOneUse() && !TrueVal->hasOneUse())
2897 return nullptr;
2898
2899 // Fold (IsNeg ? -X : X) or (!IsNeg ? X : -X) to fabs(X)
2900 // Fold (IsNeg ? X : -X) or (!IsNeg ? -X : X) to -fabs(X)
2901 Value *Fabs = IC.Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, &SI);
2902 if (Swap != TrueIfSigned)
2903 return IC.replaceInstUsesWith(SI, Fabs);
2904 return UnaryOperator::CreateFNegFMF(Fabs, &SI);
2905 }
2906
2907 return ChangedFMF ? &SI : nullptr;
2908}
2909
2910// Match the following IR pattern:
2911// %x.lowbits = and i8 %x, %lowbitmask
2912// %x.lowbits.are.zero = icmp eq i8 %x.lowbits, 0
2913// %x.biased = add i8 %x, %bias
2914// %x.biased.highbits = and i8 %x.biased, %highbitmask
2915// %x.roundedup = select i1 %x.lowbits.are.zero, i8 %x, i8 %x.biased.highbits
2916// Define:
2917// %alignment = add i8 %lowbitmask, 1
2918// Iff 1. an %alignment is a power-of-two (aka, %lowbitmask is a low bit mask)
2919// and 2. %bias is equal to either %lowbitmask or %alignment,
2920// and 3. %highbitmask is equal to ~%lowbitmask (aka, to -%alignment)
2921// then this pattern can be transformed into:
2922// %x.offset = add i8 %x, %lowbitmask
2923// %x.roundedup = and i8 %x.offset, %highbitmask
2924static Value *
2925foldRoundUpIntegerWithPow2Alignment(SelectInst &SI,
2926 InstCombiner::BuilderTy &Builder) {
2927 Value *Cond = SI.getCondition();
2928 Value *X = SI.getTrueValue();
2929 Value *XBiasedHighBits = SI.getFalseValue();
2930
2932 Value *XLowBits;
2933 if (!match(Cond, m_ICmp(Pred, m_Value(XLowBits), m_ZeroInt())) ||
2934 !ICmpInst::isEquality(Pred))
2935 return nullptr;
2936
2937 if (Pred == ICmpInst::Predicate::ICMP_NE)
2938 std::swap(X, XBiasedHighBits);
2939
2940 // FIXME: we could support non non-splats here.
2941
2942 const APInt *LowBitMaskCst;
2943 if (!match(XLowBits, m_And(m_Specific(X), m_APIntAllowPoison(LowBitMaskCst))))
2944 return nullptr;
2945
2946 // Match even if the AND and ADD are swapped.
2947 const APInt *BiasCst, *HighBitMaskCst;
2948 if (!match(XBiasedHighBits,
2950 m_APIntAllowPoison(HighBitMaskCst))) &&
2951 !match(XBiasedHighBits,
2952 m_Add(m_And(m_Specific(X), m_APIntAllowPoison(HighBitMaskCst)),
2953 m_APIntAllowPoison(BiasCst))))
2954 return nullptr;
2955
2956 if (!LowBitMaskCst->isMask())
2957 return nullptr;
2958
2959 APInt InvertedLowBitMaskCst = ~*LowBitMaskCst;
2960 if (InvertedLowBitMaskCst != *HighBitMaskCst)
2961 return nullptr;
2962
2963 APInt AlignmentCst = *LowBitMaskCst + 1;
2964
2965 if (*BiasCst != AlignmentCst && *BiasCst != *LowBitMaskCst)
2966 return nullptr;
2967
2968 if (!XBiasedHighBits->hasOneUse()) {
2969 // We can't directly return XBiasedHighBits if it is more poisonous.
2970 if (*BiasCst == *LowBitMaskCst && impliesPoison(XBiasedHighBits, X))
2971 return XBiasedHighBits;
2972 return nullptr;
2973 }
2974
2975 // FIXME: could we preserve undef's here?
2976 Type *Ty = X->getType();
2977 Value *XOffset = Builder.CreateAdd(X, ConstantInt::get(Ty, *LowBitMaskCst),
2978 X->getName() + ".biased");
2979 Value *R = Builder.CreateAnd(XOffset, ConstantInt::get(Ty, *HighBitMaskCst));
2980 R->takeName(&SI);
2981 return R;
2982}
2983
2984namespace {
2985struct DecomposedSelect {
2986 Value *Cond = nullptr;
2987 Value *TrueVal = nullptr;
2988 Value *FalseVal = nullptr;
2989};
2990} // namespace
2991
2992/// Look for patterns like
2993/// %outer.cond = select i1 %inner.cond, i1 %alt.cond, i1 false
2994/// %inner.sel = select i1 %inner.cond, i8 %inner.sel.t, i8 %inner.sel.f
2995/// %outer.sel = select i1 %outer.cond, i8 %outer.sel.t, i8 %inner.sel
2996/// and rewrite it as
2997/// %inner.sel = select i1 %cond.alternative, i8 %sel.outer.t, i8 %sel.inner.t
2998/// %sel.outer = select i1 %cond.inner, i8 %inner.sel, i8 %sel.inner.f
2999static Instruction *foldNestedSelects(SelectInst &OuterSelVal,
3000 InstCombiner::BuilderTy &Builder) {
3001 // We must start with a `select`.
3002 DecomposedSelect OuterSel;
3003 match(&OuterSelVal,
3004 m_Select(m_Value(OuterSel.Cond), m_Value(OuterSel.TrueVal),
3005 m_Value(OuterSel.FalseVal)));
3006
3007 // Canonicalize inversion of the outermost `select`'s condition.
3008 if (match(OuterSel.Cond, m_Not(m_Value(OuterSel.Cond))))
3009 std::swap(OuterSel.TrueVal, OuterSel.FalseVal);
3010
3011 // The condition of the outermost select must be an `and`/`or`.
3012 if (!match(OuterSel.Cond, m_c_LogicalOp(m_Value(), m_Value())))
3013 return nullptr;
3014
3015 // Depending on the logical op, inner select might be in different hand.
3016 bool IsAndVariant = match(OuterSel.Cond, m_LogicalAnd());
3017 Value *InnerSelVal = IsAndVariant ? OuterSel.FalseVal : OuterSel.TrueVal;
3018
3019 // Profitability check - avoid increasing instruction count.
3020 if (none_of(ArrayRef<Value *>({OuterSelVal.getCondition(), InnerSelVal}),
3021 [](Value *V) { return V->hasOneUse(); }))
3022 return nullptr;
3023
3024 // The appropriate hand of the outermost `select` must be a select itself.
3025 DecomposedSelect InnerSel;
3026 if (!match(InnerSelVal,
3027 m_Select(m_Value(InnerSel.Cond), m_Value(InnerSel.TrueVal),
3028 m_Value(InnerSel.FalseVal))))
3029 return nullptr;
3030
3031 // Canonicalize inversion of the innermost `select`'s condition.
3032 if (match(InnerSel.Cond, m_Not(m_Value(InnerSel.Cond))))
3033 std::swap(InnerSel.TrueVal, InnerSel.FalseVal);
3034
3035 Value *AltCond = nullptr;
3036 auto matchOuterCond = [OuterSel, IsAndVariant, &AltCond](auto m_InnerCond) {
3037 // An unsimplified select condition can match both LogicalAnd and LogicalOr
3038 // (select true, true, false). Since below we assume that LogicalAnd implies
3039 // InnerSel match the FVal and vice versa for LogicalOr, we can't match the
3040 // alternative pattern here.
3041 return IsAndVariant ? match(OuterSel.Cond,
3042 m_c_LogicalAnd(m_InnerCond, m_Value(AltCond)))
3043 : match(OuterSel.Cond,
3044 m_c_LogicalOr(m_InnerCond, m_Value(AltCond)));
3045 };
3046
3047 // Finally, match the condition that was driving the outermost `select`,
3048 // it should be a logical operation between the condition that was driving
3049 // the innermost `select` (after accounting for the possible inversions
3050 // of the condition), and some other condition.
3051 if (matchOuterCond(m_Specific(InnerSel.Cond))) {
3052 // Done!
3053 } else if (Value * NotInnerCond; matchOuterCond(m_CombineAnd(
3054 m_Not(m_Specific(InnerSel.Cond)), m_Value(NotInnerCond)))) {
3055 // Done!
3056 std::swap(InnerSel.TrueVal, InnerSel.FalseVal);
3057 InnerSel.Cond = NotInnerCond;
3058 } else // Not the pattern we were looking for.
3059 return nullptr;
3060
3061 Value *SelInner = Builder.CreateSelect(
3062 AltCond, IsAndVariant ? OuterSel.TrueVal : InnerSel.FalseVal,
3063 IsAndVariant ? InnerSel.TrueVal : OuterSel.FalseVal);
3064 SelInner->takeName(InnerSelVal);
3065 return SelectInst::Create(InnerSel.Cond,
3066 IsAndVariant ? SelInner : InnerSel.TrueVal,
3067 !IsAndVariant ? SelInner : InnerSel.FalseVal);
3068}
3069
3071 Value *CondVal = SI.getCondition();
3072 Value *TrueVal = SI.getTrueValue();
3073 Value *FalseVal = SI.getFalseValue();
3074 Type *SelType = SI.getType();
3075
3076 // Avoid potential infinite loops by checking for non-constant condition.
3077 // TODO: Can we assert instead by improving canonicalizeSelectToShuffle()?
3078 // Scalar select must have simplified?
3079 if (!SelType->isIntOrIntVectorTy(1) || isa<Constant>(CondVal) ||
3080 TrueVal->getType() != CondVal->getType())
3081 return nullptr;
3082
3083 auto *One = ConstantInt::getTrue(SelType);
3084 auto *Zero = ConstantInt::getFalse(SelType);
3085 Value *A, *B, *C, *D;
3086
3087 // Folding select to and/or i1 isn't poison safe in general. impliesPoison
3088 // checks whether folding it does not convert a well-defined value into
3089 // poison.
3090 if (match(TrueVal, m_One())) {
3091 if (impliesPoison(FalseVal, CondVal)) {
3092 // Change: A = select B, true, C --> A = or B, C
3093 return BinaryOperator::CreateOr(CondVal, FalseVal);
3094 }
3095
3096 if (match(CondVal, m_OneUse(m_Select(m_Value(A), m_One(), m_Value(B)))) &&
3097 impliesPoison(FalseVal, B)) {
3098 // (A || B) || C --> A || (B | C)
3099 return replaceInstUsesWith(
3100 SI, Builder.CreateLogicalOr(A, Builder.CreateOr(B, FalseVal)));
3101 }
3102
3103 if (auto *LHS = dyn_cast<FCmpInst>(CondVal))
3104 if (auto *RHS = dyn_cast<FCmpInst>(FalseVal))
3105 if (Value *V = foldLogicOfFCmps(LHS, RHS, /*IsAnd*/ false,
3106 /*IsSelectLogical*/ true))
3107 return replaceInstUsesWith(SI, V);
3108
3109 // (A && B) || (C && B) --> (A || C) && B
3110 if (match(CondVal, m_LogicalAnd(m_Value(A), m_Value(B))) &&
3111 match(FalseVal, m_LogicalAnd(m_Value(C), m_Value(D))) &&
3112 (CondVal->hasOneUse() || FalseVal->hasOneUse())) {
3113 bool CondLogicAnd = isa<SelectInst>(CondVal);
3114 bool FalseLogicAnd = isa<SelectInst>(FalseVal);
3115 auto AndFactorization = [&](Value *Common, Value *InnerCond,
3116 Value *InnerVal,
3117 bool SelFirst = false) -> Instruction * {
3118 Value *InnerSel = Builder.CreateSelect(InnerCond, One, InnerVal);
3119 if (SelFirst)
3120 std::swap(Common, InnerSel);
3121 if (FalseLogicAnd || (CondLogicAnd && Common == A))
3122 return SelectInst::Create(Common, InnerSel, Zero);
3123 else
3124 return BinaryOperator::CreateAnd(Common, InnerSel);
3125 };
3126
3127 if (A == C)
3128 return AndFactorization(A, B, D);
3129 if (A == D)
3130 return AndFactorization(A, B, C);
3131 if (B == C)
3132 return AndFactorization(B, A, D);
3133 if (B == D)
3134 return AndFactorization(B, A, C, CondLogicAnd && FalseLogicAnd);
3135 }
3136 }
3137
3138 if (match(FalseVal, m_Zero())) {
3139 if (impliesPoison(TrueVal, CondVal)) {
3140 // Change: A = select B, C, false --> A = and B, C
3141 return BinaryOperator::CreateAnd(CondVal, TrueVal);
3142 }
3143
3144 if (match(CondVal, m_OneUse(m_Select(m_Value(A), m_Value(B), m_Zero()))) &&
3145 impliesPoison(TrueVal, B)) {
3146 // (A && B) && C --> A && (B & C)
3147 return replaceInstUsesWith(
3148 SI, Builder.CreateLogicalAnd(A, Builder.CreateAnd(B, TrueVal)));
3149 }
3150
3151 if (auto *LHS = dyn_cast<FCmpInst>(CondVal))
3152 if (auto *RHS = dyn_cast<FCmpInst>(TrueVal))
3153 if (Value *V = foldLogicOfFCmps(LHS, RHS, /*IsAnd*/ true,
3154 /*IsSelectLogical*/ true))
3155 return replaceInstUsesWith(SI, V);
3156
3157 // (A || B) && (C || B) --> (A && C) || B
3158 if (match(CondVal, m_LogicalOr(m_Value(A), m_Value(B))) &&
3159 match(TrueVal, m_LogicalOr(m_Value(C), m_Value(D))) &&
3160 (CondVal->hasOneUse() || TrueVal->hasOneUse())) {
3161 bool CondLogicOr = isa<SelectInst>(CondVal);
3162 bool TrueLogicOr = isa<SelectInst>(TrueVal);
3163 auto OrFactorization = [&](Value *Common, Value *InnerCond,
3164 Value *InnerVal,
3165 bool SelFirst = false) -> Instruction * {
3166 Value *InnerSel = Builder.CreateSelect(InnerCond, InnerVal, Zero);
3167 if (SelFirst)
3168 std::swap(Common, InnerSel);
3169 if (TrueLogicOr || (CondLogicOr && Common == A))
3170 return SelectInst::Create(Common, One, InnerSel);
3171 else
3172 return BinaryOperator::CreateOr(Common, InnerSel);
3173 };
3174
3175 if (A == C)
3176 return OrFactorization(A, B, D);
3177 if (A == D)
3178 return OrFactorization(A, B, C);
3179 if (B == C)
3180 return OrFactorization(B, A, D);
3181 if (B == D)
3182 return OrFactorization(B, A, C, CondLogicOr && TrueLogicOr);
3183 }
3184 }
3185
3186 // We match the "full" 0 or 1 constant here to avoid a potential infinite
3187 // loop with vectors that may have undefined/poison elements.
3188 // select a, false, b -> select !a, b, false
3189 if (match(TrueVal, m_Specific(Zero))) {
3190 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
3191 return SelectInst::Create(NotCond, FalseVal, Zero);
3192 }
3193 // select a, b, true -> select !a, true, b
3194 if (match(FalseVal, m_Specific(One))) {
3195 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
3196 return SelectInst::Create(NotCond, One, TrueVal);
3197 }
3198
3199 // DeMorgan in select form: !a && !b --> !(a || b)
3200 // select !a, !b, false --> not (select a, true, b)
3201 if (match(&SI, m_LogicalAnd(m_Not(m_Value(A)), m_Not(m_Value(B)))) &&
3202 (CondVal->hasOneUse() || TrueVal->hasOneUse()) &&
3205
3206 // DeMorgan in select form: !a || !b --> !(a && b)
3207 // select !a, true, !b --> not (select a, b, false)
3208 if (match(&SI, m_LogicalOr(m_Not(m_Value(A)), m_Not(m_Value(B)))) &&
3209 (CondVal->hasOneUse() || FalseVal->hasOneUse()) &&
3212
3213 // select (select a, true, b), true, b -> select a, true, b
3214 if (match(CondVal, m_Select(m_Value(A), m_One(), m_Value(B))) &&
3215 match(TrueVal, m_One()) && match(FalseVal, m_Specific(B)))
3216 return replaceOperand(SI, 0, A);
3217 // select (select a, b, false), b, false -> select a, b, false
3218 if (match(CondVal, m_Select(m_Value(A), m_Value(B), m_Zero())) &&
3219 match(TrueVal, m_Specific(B)) && match(FalseVal, m_Zero()))
3220 return replaceOperand(SI, 0, A);
3221
3222 // ~(A & B) & (A | B) --> A ^ B
3225 return BinaryOperator::CreateXor(A, B);
3226
3227 // select (~a | c), a, b -> select a, (select c, true, b), false
3228 if (match(CondVal,
3229 m_OneUse(m_c_Or(m_Not(m_Specific(TrueVal)), m_Value(C))))) {
3230 Value *OrV = Builder.CreateSelect(C, One, FalseVal);
3231 return SelectInst::Create(TrueVal, OrV, Zero);
3232 }
3233 // select (c & b), a, b -> select b, (select ~c, true, a), false
3234 if (match(CondVal, m_OneUse(m_c_And(m_Value(C), m_Specific(FalseVal))))) {
3235 if (Value *NotC = getFreelyInverted(C, C->hasOneUse(), &Builder)) {
3236 Value *OrV = Builder.CreateSelect(NotC, One, TrueVal);
3237 return SelectInst::Create(FalseVal, OrV, Zero);
3238 }
3239 }
3240 // select (a | c), a, b -> select a, true, (select ~c, b, false)
3241 if (match(CondVal, m_OneUse(m_c_Or(m_Specific(TrueVal), m_Value(C))))) {
3242 if (Value *NotC = getFreelyInverted(C, C->hasOneUse(), &Builder)) {
3243 Value *AndV = Builder.CreateSelect(NotC, FalseVal, Zero);
3244 return SelectInst::Create(TrueVal, One, AndV);
3245 }
3246 }
3247 // select (c & ~b), a, b -> select b, true, (select c, a, false)
3248 if (match(CondVal,
3249 m_OneUse(m_c_And(m_Value(C), m_Not(m_Specific(FalseVal)))))) {
3250 Value *AndV = Builder.CreateSelect(C, TrueVal, Zero);
3251 return SelectInst::Create(FalseVal, One, AndV);
3252 }
3253
3254 if (match(FalseVal, m_Zero()) || match(TrueVal, m_One())) {
3255 Use *Y = nullptr;
3256 bool IsAnd = match(FalseVal, m_Zero()) ? true : false;
3257 Value *Op1 = IsAnd ? TrueVal : FalseVal;
3258 if (isCheckForZeroAndMulWithOverflow(CondVal, Op1, IsAnd, Y)) {
3259 auto *FI = new FreezeInst(*Y, (*Y)->getName() + ".fr");
3260 InsertNewInstBefore(FI, cast<Instruction>(Y->getUser())->getIterator());
3261 replaceUse(*Y, FI);
3262 return replaceInstUsesWith(SI, Op1);
3263 }
3264
3265 if (auto *ICmp0 = dyn_cast<ICmpInst>(CondVal))
3266 if (auto *ICmp1 = dyn_cast<ICmpInst>(Op1))
3267 if (auto *V = foldAndOrOfICmps(ICmp0, ICmp1, SI, IsAnd,
3268 /* IsLogical */ true))
3269 return replaceInstUsesWith(SI, V);
3270 }
3271
3272 // select (a || b), c, false -> select a, c, false
3273 // select c, (a || b), false -> select c, a, false
3274 // if c implies that b is false.
3275 if (match(CondVal, m_LogicalOr(m_Value(A), m_Value(B))) &&
3276 match(FalseVal, m_Zero())) {
3277 std::optional<bool> Res = isImpliedCondition(TrueVal, B, DL);
3278 if (Res && *Res == false)
3279 return replaceOperand(SI, 0, A);
3280 }
3281 if (match(TrueVal, m_LogicalOr(m_Value(A), m_Value(B))) &&
3282 match(FalseVal, m_Zero())) {
3283 std::optional<bool> Res = isImpliedCondition(CondVal, B, DL);
3284 if (Res && *Res == false)
3285 return replaceOperand(SI, 1, A);
3286 }
3287 // select c, true, (a && b) -> select c, true, a
3288 // select (a && b), true, c -> select a, true, c
3289 // if c = false implies that b = true
3290 if (match(TrueVal, m_One()) &&
3291 match(FalseVal, m_LogicalAnd(m_Value(A), m_Value(B)))) {
3292 std::optional<bool> Res = isImpliedCondition(CondVal, B, DL, false);
3293 if (Res && *Res == true)
3294 return replaceOperand(SI, 2, A);
3295 }
3296 if (match(CondVal, m_LogicalAnd(m_Value(A), m_Value(B))) &&
3297 match(TrueVal, m_One())) {
3298 std::optional<bool> Res = isImpliedCondition(FalseVal, B, DL, false);
3299 if (Res && *Res == true)
3300 return replaceOperand(SI, 0, A);
3301 }
3302
3303 if (match(TrueVal, m_One())) {
3304 Value *C;
3305
3306 // (C && A) || (!C && B) --> sel C, A, B
3307 // (A && C) || (!C && B) --> sel C, A, B
3308 // (C && A) || (B && !C) --> sel C, A, B
3309 // (A && C) || (B && !C) --> sel C, A, B (may require freeze)
3310 if (match(FalseVal, m_c_LogicalAnd(m_Not(m_Value(C)), m_Value(B))) &&
3311 match(CondVal, m_c_LogicalAnd(m_Specific(C), m_Value(A)))) {
3312 auto *SelCond = dyn_cast<SelectInst>(CondVal);
3313 auto *SelFVal = dyn_cast<SelectInst>(FalseVal);
3314 bool MayNeedFreeze = SelCond && SelFVal &&
3315 match(SelFVal->getTrueValue(),
3316 m_Not(m_Specific(SelCond->getTrueValue())));
3317 if (MayNeedFreeze)
3319 return SelectInst::Create(C, A, B);
3320 }
3321
3322 // (!C && A) || (C && B) --> sel C, B, A
3323 // (A && !C) || (C && B) --> sel C, B, A
3324 // (!C && A) || (B && C) --> sel C, B, A
3325 // (A && !C) || (B && C) --> sel C, B, A (may require freeze)
3326 if (match(CondVal, m_c_LogicalAnd(m_Not(m_Value(C)), m_Value(A))) &&
3327 match(FalseVal, m_c_LogicalAnd(m_Specific(C), m_Value(B)))) {
3328 auto *SelCond = dyn_cast<SelectInst>(CondVal);
3329 auto *SelFVal = dyn_cast<SelectInst>(FalseVal);
3330 bool MayNeedFreeze = SelCond && SelFVal &&
3331 match(SelCond->getTrueValue(),
3332 m_Not(m_Specific(SelFVal->getTrueValue())));
3333 if (MayNeedFreeze)
3335 return SelectInst::Create(C, B, A);
3336 }
3337 }
3338
3339 return nullptr;
3340}
3341
3342// Return true if we can safely remove the select instruction for std::bit_ceil
3343// pattern.
3344static bool isSafeToRemoveBitCeilSelect(ICmpInst::Predicate Pred, Value *Cond0,
3345 const APInt *Cond1, Value *CtlzOp,
3346 unsigned BitWidth,
3347 bool &ShouldDropNUW) {
3348 // The challenge in recognizing std::bit_ceil(X) is that the operand is used
3349 // for the CTLZ proper and select condition, each possibly with some
3350 // operation like add and sub.
3351 //
3352 // Our aim is to make sure that -ctlz & (BitWidth - 1) == 0 even when the
3353 // select instruction would select 1, which allows us to get rid of the select
3354 // instruction.
3355 //
3356 // To see if we can do so, we do some symbolic execution with ConstantRange.
3357 // Specifically, we compute the range of values that Cond0 could take when
3358 // Cond == false. Then we successively transform the range until we obtain
3359 // the range of values that CtlzOp could take.
3360 //
3361 // Conceptually, we follow the def-use chain backward from Cond0 while
3362 // transforming the range for Cond0 until we meet the common ancestor of Cond0
3363 // and CtlzOp. Then we follow the def-use chain forward until we obtain the
3364 // range for CtlzOp. That said, we only follow at most one ancestor from
3365 // Cond0. Likewise, we only follow at most one ancestor from CtrlOp.
3366
3368 CmpInst::getInversePredicate(Pred), *Cond1);
3369
3370 ShouldDropNUW = false;
3371
3372 // Match the operation that's used to compute CtlzOp from CommonAncestor. If
3373 // CtlzOp == CommonAncestor, return true as no operation is needed. If a
3374 // match is found, execute the operation on CR, update CR, and return true.
3375 // Otherwise, return false.
3376 auto MatchForward = [&](Value *CommonAncestor) {
3377 const APInt *C = nullptr;
3378 if (CtlzOp == CommonAncestor)
3379 return true;
3380 if (match(CtlzOp, m_Add(m_Specific(CommonAncestor), m_APInt(C)))) {
3381 CR = CR.add(*C);
3382 return true;
3383 }
3384 if (match(CtlzOp, m_Sub(m_APInt(C), m_Specific(CommonAncestor)))) {
3385 ShouldDropNUW = true;
3386 CR = ConstantRange(*C).sub(CR);
3387 return true;
3388 }
3389 if (match(CtlzOp, m_Not(m_Specific(CommonAncestor)))) {
3390 CR = CR.binaryNot();
3391 return true;
3392 }
3393 return false;
3394 };
3395
3396 const APInt *C = nullptr;
3397 Value *CommonAncestor;
3398 if (MatchForward(Cond0)) {
3399 // Cond0 is either CtlzOp or CtlzOp's parent. CR has been updated.
3400 } else if (match(Cond0, m_Add(m_Value(CommonAncestor), m_APInt(C)))) {
3401 CR = CR.sub(*C);
3402 if (!MatchForward(CommonAncestor))
3403 return false;
3404 // Cond0's parent is either CtlzOp or CtlzOp's parent. CR has been updated.
3405 } else {
3406 return false;
3407 }
3408
3409 // Return true if all the values in the range are either 0 or negative (if
3410 // treated as signed). We do so by evaluating:
3411 //
3412 // CR - 1 u>= (1 << BitWidth) - 1.
3413 APInt IntMax = APInt::getSignMask(BitWidth) - 1;
3414 CR = CR.sub(APInt(BitWidth, 1));
3415 return CR.icmp(ICmpInst::ICMP_UGE, IntMax);
3416}
3417
3418// Transform the std::bit_ceil(X) pattern like:
3419//
3420// %dec = add i32 %x, -1
3421// %ctlz = tail call i32 @llvm.ctlz.i32(i32 %dec, i1 false)
3422// %sub = sub i32 32, %ctlz
3423// %shl = shl i32 1, %sub
3424// %ugt = icmp ugt i32 %x, 1
3425// %sel = select i1 %ugt, i32 %shl, i32 1
3426//
3427// into:
3428//
3429// %dec = add i32 %x, -1
3430// %ctlz = tail call i32 @llvm.ctlz.i32(i32 %dec, i1 false)
3431// %neg = sub i32 0, %ctlz
3432// %masked = and i32 %ctlz, 31
3433// %shl = shl i32 1, %sub
3434//
3435// Note that the select is optimized away while the shift count is masked with
3436// 31. We handle some variations of the input operand like std::bit_ceil(X +
3437// 1).
3438static Instruction *foldBitCeil(SelectInst &SI, IRBuilderBase &Builder) {
3439 Type *SelType = SI.getType();
3440 unsigned BitWidth = SelType->getScalarSizeInBits();
3441
3442 Value *FalseVal = SI.getFalseValue();
3443 Value *TrueVal = SI.getTrueValue();
3445 const APInt *Cond1;
3446 Value *Cond0, *Ctlz, *CtlzOp;
3447 if (!match(SI.getCondition(), m_ICmp(Pred, m_Value(Cond0), m_APInt(Cond1))))
3448 return nullptr;
3449
3450 if (match(TrueVal, m_One())) {
3451 std::swap(FalseVal, TrueVal);
3452 Pred = CmpInst::getInversePredicate(Pred);
3453 }
3454
3455 bool ShouldDropNUW;
3456
3457 if (!match(FalseVal, m_One()) ||
3458 !match(TrueVal,
3460 m_Value(Ctlz)))))) ||
3461 !match(Ctlz, m_Intrinsic<Intrinsic::ctlz>(m_Value(CtlzOp), m_Zero())) ||
3462 !isSafeToRemoveBitCeilSelect(Pred, Cond0, Cond1, CtlzOp, BitWidth,
3463 ShouldDropNUW))
3464 return nullptr;
3465
3466 if (ShouldDropNUW)
3467 cast<Instruction>(CtlzOp)->setHasNoUnsignedWrap(false);
3468
3469 // Build 1 << (-CTLZ & (BitWidth-1)). The negation likely corresponds to a
3470 // single hardware instruction as opposed to BitWidth - CTLZ, where BitWidth
3471 // is an integer constant. Masking with BitWidth-1 comes free on some
3472 // hardware as part of the shift instruction.
3473 Value *Neg = Builder.CreateNeg(Ctlz);
3474 Value *Masked =
3475 Builder.CreateAnd(Neg, ConstantInt::get(SelType, BitWidth - 1));
3476 return BinaryOperator::Create(Instruction::Shl, ConstantInt::get(SelType, 1),
3477 Masked);
3478}
3479
3481 const Instruction *CtxI) const {
3482 KnownFPClass Known = computeKnownFPClass(MulVal, FMF, fcNegative, CtxI);
3483
3484 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity() &&
3485 (FMF.noSignedZeros() || Known.signBitIsZeroOrNaN());
3486}
3487
3488static bool matchFMulByZeroIfResultEqZero(InstCombinerImpl &IC, Value *Cmp0,
3489 Value *Cmp1, Value *TrueVal,
3490 Value *FalseVal, Instruction &CtxI,
3491 bool SelectIsNSZ) {
3492 Value *MulRHS;
3493 if (match(Cmp1, m_PosZeroFP()) &&
3494 match(TrueVal, m_c_FMul(m_Specific(Cmp0), m_Value(MulRHS)))) {
3495 FastMathFlags FMF = cast<FPMathOperator>(TrueVal)->getFastMathFlags();
3496 // nsz must be on the select, it must be ignored on the multiply. We
3497 // need nnan and ninf on the multiply for the other value.
3498 FMF.setNoSignedZeros(SelectIsNSZ);
3499 return IC.fmulByZeroIsZero(MulRHS, FMF, &CtxI);
3500 }
3501
3502 return false;
3503}
3504
3506 Value *CondVal = SI.getCondition();
3507 Value *TrueVal = SI.getTrueValue();
3508 Value *FalseVal = SI.getFalseValue();
3509 Type *SelType = SI.getType();
3510
3511 if (Value *V = simplifySelectInst(CondVal, TrueVal, FalseVal,
3512 SQ.getWithInstruction(&SI)))
3513 return replaceInstUsesWith(SI, V);
3514
3515 if (Instruction *I = canonicalizeSelectToShuffle(SI))
3516 return I;
3517
3518 if (Instruction *I = canonicalizeScalarSelectOfVecs(SI, *this))
3519 return I;
3520
3521 // If the type of select is not an integer type or if the condition and
3522 // the selection type are not both scalar nor both vector types, there is no
3523 // point in attempting to match these patterns.
3524 Type *CondType = CondVal->getType();
3525 if (!isa<Constant>(CondVal) && SelType->isIntOrIntVectorTy() &&
3526 CondType->isVectorTy() == SelType->isVectorTy()) {
3527 if (Value *S = simplifyWithOpReplaced(TrueVal, CondVal,
3528 ConstantInt::getTrue(CondType), SQ,
3529 /* AllowRefinement */ true))
3530 return replaceOperand(SI, 1, S);
3531
3532 if (Value *S = simplifyWithOpReplaced(FalseVal, CondVal,
3533 ConstantInt::getFalse(CondType), SQ,
3534 /* AllowRefinement */ true))
3535 return replaceOperand(SI, 2, S);
3536 }
3537
3538 if (Instruction *R = foldSelectOfBools(SI))
3539 return R;
3540
3541 // Selecting between two integer or vector splat integer constants?
3542 //
3543 // Note that we don't handle a scalar select of vectors:
3544 // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0>
3545 // because that may need 3 instructions to splat the condition value:
3546 // extend, insertelement, shufflevector.
3547 //
3548 // Do not handle i1 TrueVal and FalseVal otherwise would result in
3549 // zext/sext i1 to i1.
3550 if (SelType->isIntOrIntVectorTy() && !SelType->isIntOrIntVectorTy(1) &&
3551 CondVal->getType()->isVectorTy() == SelType->isVectorTy()) {
3552 // select C, 1, 0 -> zext C to int
3553 if (match(TrueVal, m_One()) && match(FalseVal, m_Zero()))
3554 return new ZExtInst(CondVal, SelType);
3555
3556 // select C, -1, 0 -> sext C to int
3557 if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero()))
3558 return new SExtInst(CondVal, SelType);
3559
3560 // select C, 0, 1 -> zext !C to int
3561 if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) {
3562 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
3563 return new ZExtInst(NotCond, SelType);
3564 }
3565
3566 // select C, 0, -1 -> sext !C to int
3567 if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) {
3568 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
3569 return new SExtInst(NotCond, SelType);
3570 }
3571 }
3572
3573 auto *SIFPOp = dyn_cast<FPMathOperator>(&SI);
3574
3575 if (auto *FCmp = dyn_cast<FCmpInst>(CondVal)) {
3576 FCmpInst::Predicate Pred = FCmp->getPredicate();
3577 Value *Cmp0 = FCmp->getOperand(0), *Cmp1 = FCmp->getOperand(1);
3578 // Are we selecting a value based on a comparison of the two values?
3579 if ((Cmp0 == TrueVal && Cmp1 == FalseVal) ||
3580 (Cmp0 == FalseVal && Cmp1 == TrueVal)) {
3581 // Canonicalize to use ordered comparisons by swapping the select
3582 // operands.
3583 //
3584 // e.g.
3585 // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X
3586 if (FCmp->hasOneUse() && FCmpInst::isUnordered(Pred)) {
3587 FCmpInst::Predicate InvPred = FCmp->getInversePredicate();
3589 // FIXME: The FMF should propagate from the select, not the fcmp.
3590 Builder.setFastMathFlags(FCmp->getFastMathFlags());
3591 Value *NewCond = Builder.CreateFCmp(InvPred, Cmp0, Cmp1,
3592 FCmp->getName() + ".inv");
3593 Value *NewSel = Builder.CreateSelect(NewCond, FalseVal, TrueVal);
3594 return replaceInstUsesWith(SI, NewSel);
3595 }
3596 }
3597
3598 if (SIFPOp) {
3599 // Fold out scale-if-equals-zero pattern.
3600 //
3601 // This pattern appears in code with denormal range checks after it's
3602 // assumed denormals are treated as zero. This drops a canonicalization.
3603
3604 // TODO: Could relax the signed zero logic. We just need to know the sign
3605 // of the result matches (fmul x, y has the same sign as x).
3606 //
3607 // TODO: Handle always-canonicalizing variant that selects some value or 1
3608 // scaling factor in the fmul visitor.
3609
3610 // TODO: Handle ldexp too
3611
3612 Value *MatchCmp0 = nullptr;
3613 Value *MatchCmp1 = nullptr;
3614
3615 // (select (fcmp [ou]eq x, 0.0), (fmul x, K), x => x
3616 // (select (fcmp [ou]ne x, 0.0), x, (fmul x, K) => x
3617 if (Pred == CmpInst::FCMP_OEQ || Pred == CmpInst::FCMP_UEQ) {
3618 MatchCmp0 = FalseVal;
3619 MatchCmp1 = TrueVal;
3620 } else if (Pred == CmpInst::FCMP_ONE || Pred == CmpInst::FCMP_UNE) {
3621 MatchCmp0 = TrueVal;
3622 MatchCmp1 = FalseVal;
3623 }
3624
3625 if (Cmp0 == MatchCmp0 &&
3626 matchFMulByZeroIfResultEqZero(*this, Cmp0, Cmp1, MatchCmp1, MatchCmp0,
3627 SI, SIFPOp->hasNoSignedZeros()))
3628 return replaceInstUsesWith(SI, Cmp0);
3629 }
3630 }
3631
3632 if (SIFPOp) {
3633 // TODO: Try to forward-propagate FMF from select arms to the select.
3634
3635 // Canonicalize select of FP values where NaN and -0.0 are not valid as
3636 // minnum/maxnum intrinsics.
3637 if (SIFPOp->hasNoNaNs() && SIFPOp->hasNoSignedZeros()) {
3638 Value *X, *Y;
3639 if (match(&SI, m_OrdFMax(m_Value(X), m_Value(Y))))
3640 return replaceInstUsesWith(
3641 SI, Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, X, Y, &SI));
3642
3643 if (match(&SI, m_OrdFMin(m_Value(X), m_Value(Y))))
3644 return replaceInstUsesWith(
3645 SI, Builder.CreateBinaryIntrinsic(Intrinsic::minnum, X, Y, &SI));
3646 }
3647 }
3648
3649 // Fold selecting to fabs.
3650 if (Instruction *Fabs = foldSelectWithFCmpToFabs(SI, *this))
3651 return Fabs;
3652
3653 // See if we are selecting two values based on a comparison of the two values.
3654 if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
3655 if (Instruction *Result = foldSelectInstWithICmp(SI, ICI))
3656 return Result;
3657
3658 if (Instruction *Add = foldAddSubSelect(SI, Builder))
3659 return Add;
3660 if (Instruction *Add = foldOverflowingAddSubSelect(SI, Builder))
3661 return Add;
3663 return Or;
3664 if (Instruction *Mul = foldSelectZeroOrMul(SI, *this))
3665 return Mul;
3666
3667 // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
3668 auto *TI = dyn_cast<Instruction>(TrueVal);
3669 auto *FI = dyn_cast<Instruction>(FalseVal);
3670 if (TI && FI && TI->getOpcode() == FI->getOpcode())
3671 if (Instruction *IV = foldSelectOpOp(SI, TI, FI))
3672 return IV;
3673
3674 if (Instruction *I = foldSelectExtConst(SI))
3675 return I;
3676
3677 if (Instruction *I = foldSelectWithSRem(SI, *this, Builder))
3678 return I;
3679
3680 // Fold (select C, (gep Ptr, Idx), Ptr) -> (gep Ptr, (select C, Idx, 0))
3681 // Fold (select C, Ptr, (gep Ptr, Idx)) -> (gep Ptr, (select C, 0, Idx))
3682 auto SelectGepWithBase = [&](GetElementPtrInst *Gep, Value *Base,
3683 bool Swap) -> GetElementPtrInst * {
3684 Value *Ptr = Gep->getPointerOperand();
3685 if (Gep->getNumOperands() != 2 || Gep->getPointerOperand() != Base ||
3686 !Gep->hasOneUse())
3687 return nullptr;
3688 Value *Idx = Gep->getOperand(1);
3689 if (isa<VectorType>(CondVal->getType()) && !isa<VectorType>(Idx->getType()))
3690 return nullptr;
3692 Value *NewT = Idx;
3693 Value *NewF = Constant::getNullValue(Idx->getType());
3694 if (Swap)
3695 std::swap(NewT, NewF);
3696 Value *NewSI =
3697 Builder.CreateSelect(CondVal, NewT, NewF, SI.getName() + ".idx", &SI);
3698 if (Gep->isInBounds())
3699 return GetElementPtrInst::CreateInBounds(ElementType, Ptr, {NewSI});
3700 return GetElementPtrInst::Create(ElementType, Ptr, {NewSI});
3701 };
3702 if (auto *TrueGep = dyn_cast<GetElementPtrInst>(TrueVal))
3703 if (auto *NewGep = SelectGepWithBase(TrueGep, FalseVal, false))
3704 return NewGep;
3705 if (auto *FalseGep = dyn_cast<GetElementPtrInst>(FalseVal))
3706 if (auto *NewGep = SelectGepWithBase(FalseGep, TrueVal, true))
3707 return NewGep;
3708
3709 // See if we can fold the select into one of our operands.
3710 if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) {
3711 if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))
3712 return FoldI;
3713
3714 Value *LHS, *RHS;
3715 Instruction::CastOps CastOp;
3716 SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp);
3717 auto SPF = SPR.Flavor;
3718 if (SPF) {
3719 Value *LHS2, *RHS2;
3720 if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor)
3721 if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS), SPF2, LHS2,
3722 RHS2, SI, SPF, RHS))
3723 return R;
3724 if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor)
3725 if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS), SPF2, LHS2,
3726 RHS2, SI, SPF, LHS))
3727 return R;
3728 }
3729
3731 // Canonicalize so that
3732 // - type casts are outside select patterns.
3733 // - float clamp is transformed to min/max pattern
3734
3735 bool IsCastNeeded = LHS->getType() != SelType;
3736 Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0);
3737 Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1);
3738 if (IsCastNeeded ||
3739 (LHS->getType()->isFPOrFPVectorTy() &&
3740 ((CmpLHS != LHS && CmpLHS != RHS) ||
3741 (CmpRHS != LHS && CmpRHS != RHS)))) {
3742 CmpInst::Predicate MinMaxPred = getMinMaxPred(SPF, SPR.Ordered);
3743
3744 Value *Cmp;
3745 if (CmpInst::isIntPredicate(MinMaxPred)) {
3746 Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS);
3747 } else {
3749 auto FMF =
3750 cast<FPMathOperator>(SI.getCondition())->getFastMathFlags();
3752 Cmp = Builder.CreateFCmp(MinMaxPred, LHS, RHS);
3753 }
3754
3755 Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
3756 if (!IsCastNeeded)
3757 return replaceInstUsesWith(SI, NewSI);
3758
3759 Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType);
3760 return replaceInstUsesWith(SI, NewCast);
3761 }
3762 }
3763 }
3764
3765 // See if we can fold the select into a phi node if the condition is a select.
3766 if (auto *PN = dyn_cast<PHINode>(SI.getCondition()))
3767 // The true/false values have to be live in the PHI predecessor's blocks.
3768 if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
3769 canSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
3770 if (Instruction *NV = foldOpIntoPhi(SI, PN))
3771 return NV;
3772
3773 if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
3774 if (TrueSI->getCondition()->getType() == CondVal->getType()) {
3775 // Fold nested selects if the inner condition can be implied by the outer
3776 // condition.
3777 if (Value *V = simplifyNestedSelectsUsingImpliedCond(
3778 *TrueSI, CondVal, /*CondIsTrue=*/true, DL))
3779 return replaceOperand(SI, 1, V);
3780
3781 // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b)
3782 // We choose this as normal form to enable folding on the And and
3783 // shortening paths for the values (this helps getUnderlyingObjects() for
3784 // example).
3785 if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) {
3786 Value *And = Builder.CreateLogicalAnd(CondVal, TrueSI->getCondition());
3787 replaceOperand(SI, 0, And);
3788 replaceOperand(SI, 1, TrueSI->getTrueValue());
3789 return &SI;
3790 }
3791 }
3792 }
3793 if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
3794 if (FalseSI->getCondition()->getType() == CondVal->getType()) {
3795 // Fold nested selects if the inner condition can be implied by the outer
3796 // condition.
3797 if (Value *V = simplifyNestedSelectsUsingImpliedCond(
3798 *FalseSI, CondVal, /*CondIsTrue=*/false, DL))
3799 return replaceOperand(SI, 2, V);
3800
3801 // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b)
3802 if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) {
3803 Value *Or = Builder.CreateLogicalOr(CondVal, FalseSI->getCondition());
3804 replaceOperand(SI, 0, Or);
3805 replaceOperand(SI, 2, FalseSI->getFalseValue());
3806 return &SI;
3807 }
3808 }
3809 }
3810
3811 // Try to simplify a binop sandwiched between 2 selects with the same
3812 // condition. This is not valid for div/rem because the select might be
3813 // preventing a division-by-zero.
3814 // TODO: A div/rem restriction is conservative; use something like
3815 // isSafeToSpeculativelyExecute().
3816 // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z)
3817 BinaryOperator *TrueBO;
3818 if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) && !TrueBO->isIntDivRem()) {
3819 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) {
3820 if (TrueBOSI->getCondition() == CondVal) {
3821 replaceOperand(*TrueBO, 0, TrueBOSI->getTrueValue());
3822 Worklist.push(TrueBO);
3823 return &SI;
3824 }
3825 }
3826 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) {
3827 if (TrueBOSI->getCondition() == CondVal) {
3828 replaceOperand(*TrueBO, 1, TrueBOSI->getTrueValue());
3829 Worklist.push(TrueBO);
3830 return &SI;
3831 }
3832 }
3833 }
3834
3835 // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W))
3836 BinaryOperator *FalseBO;
3837 if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) && !FalseBO->isIntDivRem()) {
3838 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) {
3839 if (FalseBOSI->getCondition() == CondVal) {
3840 replaceOperand(*FalseBO, 0, FalseBOSI->getFalseValue());
3841 Worklist.push(FalseBO);
3842 return &SI;
3843 }
3844 }
3845 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) {
3846 if (FalseBOSI->getCondition() == CondVal) {
3847 replaceOperand(*FalseBO, 1, FalseBOSI->getFalseValue());
3848 Worklist.push(FalseBO);
3849 return &SI;
3850 }
3851 }
3852 }
3853
3854 Value *NotCond;
3855 if (match(CondVal, m_Not(m_Value(NotCond))) &&
3857 replaceOperand(SI, 0, NotCond);
3858 SI.swapValues();
3859 SI.swapProfMetadata();
3860 return &SI;
3861 }
3862
3863 if (Instruction *I = foldVectorSelect(SI))
3864 return I;
3865
3866 // If we can compute the condition, there's no need for a select.
3867 // Like the above fold, we are attempting to reduce compile-time cost by
3868 // putting this fold here with limitations rather than in InstSimplify.
3869 // The motivation for this call into value tracking is to take advantage of
3870 // the assumption cache, so make sure that is populated.
3871 if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) {
3872 KnownBits Known(1);
3873 computeKnownBits(CondVal, Known, 0, &SI);
3874 if (Known.One.isOne())
3875 return replaceInstUsesWith(SI, TrueVal);
3876 if (Known.Zero.isOne())
3877 return replaceInstUsesWith(SI, FalseVal);
3878 }
3879
3880 if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder))
3881 return BitCastSel;
3882
3883 // Simplify selects that test the returned flag of cmpxchg instructions.
3884 if (Value *V = foldSelectCmpXchg(SI))
3885 return replaceInstUsesWith(SI, V);
3886
3887 if (Instruction *Select = foldSelectBinOpIdentity(SI, TLI, *this))
3888 return Select;
3889
3890 if (Instruction *Funnel = foldSelectFunnelShift(SI, Builder))
3891 return Funnel;
3892
3893 if (Instruction *Copysign = foldSelectToCopysign(SI, Builder))
3894 return Copysign;
3895
3896 if (Instruction *PN = foldSelectToPhi(SI, DT, Builder))
3897 return replaceInstUsesWith(SI, PN);
3898
3899 if (Value *Fr = foldSelectWithFrozenICmp(SI, Builder))
3900 return replaceInstUsesWith(SI, Fr);
3901
3902 if (Value *V = foldRoundUpIntegerWithPow2Alignment(SI, Builder))
3903 return replaceInstUsesWith(SI, V);
3904
3905 // select(mask, mload(,,mask,0), 0) -> mload(,,mask,0)
3906 // Load inst is intentionally not checked for hasOneUse()
3907 if (match(FalseVal, m_Zero()) &&
3908 (match(TrueVal, m_MaskedLoad(m_Value(), m_Value(), m_Specific(CondVal),
3909 m_CombineOr(m_Undef(), m_Zero()))) ||
3910 match(TrueVal, m_MaskedGather(m_Value(), m_Value(), m_Specific(CondVal),
3911 m_CombineOr(m_Undef(), m_Zero()))))) {
3912 auto *MaskedInst = cast<IntrinsicInst>(TrueVal);
3913 if (isa<UndefValue>(MaskedInst->getArgOperand(3)))
3914 MaskedInst->setArgOperand(3, FalseVal /* Zero */);
3915 return replaceInstUsesWith(SI, MaskedInst);
3916 }
3917
3918 Value *Mask;
3919 if (match(TrueVal, m_Zero()) &&
3920 (match(FalseVal, m_MaskedLoad(m_Value(), m_Value(), m_Value(Mask),
3921 m_CombineOr(m_Undef(), m_Zero()))) ||
3922 match(FalseVal, m_MaskedGather(m_Value(), m_Value(), m_Value(Mask),
3923 m_CombineOr(m_Undef(), m_Zero())))) &&
3924 (CondVal->getType() == Mask->getType())) {
3925 // We can remove the select by ensuring the load zeros all lanes the
3926 // select would have. We determine this by proving there is no overlap
3927 // between the load and select masks.
3928 // (i.e (load_mask & select_mask) == 0 == no overlap)
3929 bool CanMergeSelectIntoLoad = false;
3930 if (Value *V = simplifyAndInst(CondVal, Mask, SQ.getWithInstruction(&SI)))
3931 CanMergeSelectIntoLoad = match(V, m_Zero());
3932
3933 if (CanMergeSelectIntoLoad) {
3934 auto *MaskedInst = cast<IntrinsicInst>(FalseVal);
3935 if (isa<UndefValue>(MaskedInst->getArgOperand(3)))
3936 MaskedInst->setArgOperand(3, TrueVal /* Zero */);
3937 return replaceInstUsesWith(SI, MaskedInst);
3938 }
3939 }
3940
3941 if (Instruction *I = foldNestedSelects(SI, Builder))
3942 return I;
3943
3944 // Match logical variants of the pattern,
3945 // and transform them iff that gets rid of inversions.
3946 // (~x) | y --> ~(x & (~y))
3947 // (~x) & y --> ~(x | (~y))
3949 return &SI;
3950
3951 if (Instruction *I = foldBitCeil(SI, Builder))
3952 return I;
3953
3954 // Fold:
3955 // (select A && B, T, F) -> (select A, (select B, T, F), F)
3956 // (select A || B, T, F) -> (select A, T, (select B, T, F))
3957 // if (select B, T, F) is foldable.
3958 // TODO: preserve FMF flags
3959 auto FoldSelectWithAndOrCond = [&](bool IsAnd, Value *A,
3960 Value *B) -> Instruction * {
3961 if (Value *V = simplifySelectInst(B, TrueVal, FalseVal,
3962 SQ.getWithInstruction(&SI)))
3963 return SelectInst::Create(A, IsAnd ? V : TrueVal, IsAnd ? FalseVal : V);
3964
3965 // Is (select B, T, F) a SPF?
3966 if (CondVal->hasOneUse() && SelType->isIntOrIntVectorTy()) {
3967 if (ICmpInst *Cmp = dyn_cast<ICmpInst>(B))
3968 if (Value *V = canonicalizeSPF(*Cmp, TrueVal, FalseVal, *this))
3969 return SelectInst::Create(A, IsAnd ? V : TrueVal,
3970 IsAnd ? FalseVal : V);
3971 }
3972
3973 return nullptr;
3974 };
3975
3976 Value *LHS, *RHS;
3977 if (match(CondVal, m_And(m_Value(LHS), m_Value(RHS)))) {
3978 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ true, LHS, RHS))
3979 return I;
3980 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ true, RHS, LHS))
3981 return I;
3982 } else if (match(CondVal, m_Or(m_Value(LHS), m_Value(RHS)))) {
3983 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ false, LHS, RHS))
3984 return I;
3985 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ false, RHS, LHS))
3986 return I;
3987 } else {
3988 // We cannot swap the operands of logical and/or.
3989 // TODO: Can we swap the operands by inserting a freeze?
3990 if (match(CondVal, m_LogicalAnd(m_Value(LHS), m_Value(RHS)))) {
3991 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ true, LHS, RHS))
3992 return I;
3993 } else if (match(CondVal, m_LogicalOr(m_Value(LHS), m_Value(RHS)))) {
3994 if (Instruction *I = FoldSelectWithAndOrCond(/*IsAnd*/ false, LHS, RHS))
3995 return I;
3996 }
3997 }
3998
3999 return nullptr;
4000}
SmallVector< AArch64_IMM::ImmInsnModel, 4 > Insn
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu AMDGPU Register Bank Select
This file implements a class to represent arbitrary precision integral constant values and operations...
basic Basic Alias true
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const HexagonInstrInfo * TII
static cl::opt< bool > NeedAnd("extract-needand", cl::init(true), cl::Hidden, cl::desc("Require & in extract patterns"))
This file provides internal interfaces used to implement the InstCombine.
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 Instruction * foldSelectZeroOrMul(SelectInst &SI, InstCombinerImpl &IC)
static Value * canonicalizeSaturatedSubtract(const ICmpInst *ICI, const Value *TrueVal, const Value *FalseVal, InstCombiner::BuilderTy &Builder)
Transform patterns such as (a > b) ? a - b : 0 into usub.sat(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 * 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 * 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 * foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal, Value *FalseVal, 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,...
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 * foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp, 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...
This file provides the interface for the instcombine pass implementation.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition: blake3_impl.h:78
bool bitwiseIsEqual(const APFloat &RHS) const
Definition: APFloat.h:1260
bool isNegative() const
Definition: APFloat.h:1295
Class for arbitrary precision integers.
Definition: APInt.h:76
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:212
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:207
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:401
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1498
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:349
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1446
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:395
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:197
unsigned countLeadingZeros() const
Definition: APInt.h:1563
unsigned logBase2() const
Definition: APInt.h:1710
bool isMask(unsigned numBits) const
Definition: APInt.h:466
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:383
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:418
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:367
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
bool isMaxValue() const
Determine if this is the largest unsigned value.
Definition: APInt.h:377
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
MutableArrayRef< ResultElem > assumptions()
Access the list of assumption handles currently tracked for this function.
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:539
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:430
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:221
Value * getRHS() const
Value * getLHS() const
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore)
Construct a binary instruction, given the opcode and the two operands.
BinaryOps getOpcode() const
Definition: InstrTypes.h:513
static BinaryOperator * CreateNot(Value *Op, const Twine &Name, BasicBlock::iterator InsertBefore)
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1687
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1692
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr, BasicBlock::iterator InsertBefore)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name, BasicBlock::iterator InsertBefore)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name, BasicBlock::iterator InsertBefore)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:996
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:999
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:1008
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:997
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:998
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:1017
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:1016
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:1007
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:1001
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:1004
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:1005
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:1000
@ ICMP_EQ
equal
Definition: InstrTypes.h:1014
@ ICMP_NE
not equal
Definition: InstrTypes.h:1015
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:1009
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:1019
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:1006
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:1167
bool isFPPredicate() const
Definition: InstrTypes.h:1122
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:1129
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:1105
static 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:1233
bool isIntPredicate() const
Definition: InstrTypes.h:1123
bool isUnsigned() const
Definition: InstrTypes.h:1271
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2542
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
Definition: Constants.cpp:2596
static Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2523
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:849
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:856
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
static 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...
ConstantRange binaryNot() const
Return a new range representing the possible values resulting from a binary-xor of a value in this ra...
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...
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:41
static Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
Definition: Constants.cpp:791
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:417
bool isOneValue() const
Returns true if the value is one.
Definition: Constants.cpp:124
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:672
unsigned size() const
Definition: DenseMap.h:99
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:201
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags.
Definition: Operator.h:319
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
bool noSignedZeros() const
Definition: FMF.h:68
bool noInfs() const
Definition: FMF.h:67
void setNoSignedZeros(bool B=true)
Definition: FMF.h:85
bool noNaNs() const
Definition: FMF.h:66
This class represents a freeze function that returns random concrete value if an operand is either a ...
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:973
bool isInBounds() const
Determine whether the GEP has the inbounds flag.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr, BasicBlock::iterator InsertBefore)
Type * getResultElementType() const
static GetElementPtrInst * CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr, BasicBlock::iterator InsertBefore)
Create an "inbounds" getelementptr.
uint64_t getType(const MachineInstr &MI) const
This instruction compares its operands according to the predicate given to the constructor.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:94
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Definition: IRBuilder.cpp:913
Value * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type.
Definition: IRBuilder.cpp:921
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2361
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:2039
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1193
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:466
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:932
Value * CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2269
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1091
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition: IRBuilder.h:2535
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1437
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Definition: IRBuilder.h:311
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1721
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2397
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1749
Value * CreateIsNeg(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg < 0.
Definition: IRBuilder.h:2554
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1416
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2021
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1475
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1327
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition: IRBuilder.h:2549
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2007
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1497
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1666
Value * CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1676
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2273
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2161
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1456
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1519
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2351
Value * CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1682
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1730
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 * foldOpIntoPhi(Instruction &I, PHINode *PN)
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 * foldSelectValueEquivalence(SelectInst &SI, ICmpInst &ICI)
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)
Constant * getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp)
Instruction * foldSelectIntoOp(SelectInst &SI, Value *, Value *)
Try to fold the select into one of the operands to allow further optimization.
Instruction * visitSelectInst(SelectInst &SI)
Instruction * foldSelectOfBools(SelectInst &SI)
Instruction * foldSelectExtConst(SelectInst &Sel)
The core instruction combiner logic.
Definition: InstCombiner.h:47
SimplifyQuery SQ
Definition: InstCombiner.h:76
const DataLayout & getDataLayout() const
Definition: InstCombiner.h:341
static bool isCanonicalPredicate(CmpInst::Predicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
Definition: InstCombiner.h:157
TargetLibraryInfo & TLI
Definition: InstCombiner.h:73
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
Definition: InstCombiner.h:441
Instruction * InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old)
Inserts an instruction New before instruction Old.
Definition: InstCombiner.h:366
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:386
static bool shouldAvoidAbsorbingNotIntoSelect(const SelectInst &SI)
Definition: InstCombiner.h:191
static std::optional< std::pair< CmpInst::Predicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpInst::Predicate Pred, Constant *C)
void replaceUse(Use &U, Value *NewValue)
Replace use and add the previously used value to the worklist.
Definition: InstCombiner.h:418
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Definition: InstCombiner.h:64
const DataLayout & DL
Definition: InstCombiner.h:75
AssumptionCache & AC
Definition: InstCombiner.h:72
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:410
DominatorTree & DT
Definition: InstCombiner.h:74
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
Definition: InstCombiner.h:431
BuilderTy & Builder
Definition: InstCombiner.h:60
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:213
const SimplifyQuery & getSimplifyQuery() const
Definition: InstCombiner.h:342
static Constant * AddOne(Constant *C)
Add one to a Constant.
Definition: InstCombiner.h:175
void add(Instruction *I)
Add instruction to the worklist.
void push(Instruction *I)
Push the instruction onto the worklist stack.
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
Definition: Instruction.h:260
bool hasNoSignedZeros() const LLVM_READONLY
Determine whether the no-signed-zeros flag is set.
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:83
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction.
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
const BasicBlock * getParent() const
Definition: Instruction.h:152
void swapProfMetadata()
If the instruction has "branch_weights" MD_prof metadata and the MDNode has three operands (including...
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.
Definition: Instruction.h:252
void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs.
bool isIntDivRem() const
Definition: Instruction.h:258
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:54
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:293
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, BasicBlock::iterator InsertBefore, Instruction *MDFrom=nullptr)
const Value * getTrueValue() const
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
This instruction constructs a fixed permutation of two input vectors.
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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:265
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:234
static IntegerType * getInt1Ty(LLVMContext &C)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:216
static UnaryOperator * CreateFNegFMF(Value *Op, Instruction *FMFSource, const Twine &Name, BasicBlock::iterator InsertBefore)
Definition: InstrTypes.h:191
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
op_range operands()
Definition: User.h:242
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) const
Translate PHI node to its predecessor from the given basic block.
Definition: Value.cpp:1066
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
Represents an op.with.overflow intrinsic.
This class represents zero extension of integer types.
self_iterator getIterator()
Definition: ilist_node.h:109
#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.
Definition: BitmaskEnum.h:121
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1469
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
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.
Definition: PatternMatch.h:100
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.
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.
Definition: PatternMatch.h:619
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
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.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(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.
Definition: PatternMatch.h:972
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:816
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:764
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:875
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
Definition: PatternMatch.h:186
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
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.
Definition: PatternMatch.h:245
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.
Definition: PatternMatch.h:507
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
CastOperator_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition: PatternMatch.h:822
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.
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()...
Definition: PatternMatch.h:893
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:854
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedLoad Intrinsic.
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op, true > m_c_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations in either order.
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.
Definition: PatternMatch.h:105
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
auto m_c_LogicalOp(const LHS &L, const RHS &R)
Matches either L && R or L || R with LHS and RHS in either order.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
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.
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition: PatternMatch.h:322
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
match_combine_or< CastOperator_match< OpTy, Instruction::Trunc >, OpTy > m_TruncOrSelf(const OpTy &Op)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(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.
Definition: PatternMatch.h:773
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)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
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)
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition: PatternMatch.h:189
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
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.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
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.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2, Opnd3 >::Ty m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, const Opnd3 &Op3)
Matches MaskedGather Intrinsic.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
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.
Definition: PatternMatch.h:698
ElementType
The element type of an SRV or UAV resource.
Definition: DXILABI.h:68
DiagnosticInfoOptimizationBase::Argument NV
@ FalseVal
Definition: TGLexer.h:59
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1381
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.
CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
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...
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:275
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
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...
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:1736
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Value * simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
constexpr int PoisonMaskElem
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ None
Not a recurrence.
@ And
Bitwise or logical AND of integers.
@ Add
Sum of integers.
DWARFExpression::Operation Op
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, 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...
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.
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, Value *&X, APInt &Mask, bool LookThroughTrunc=true)
Decompose an icmp into the form ((X & Mask) pred 0) if possible.
bool cannotBeNegativeZero(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if we can prove that the specified FP value is never equal to -0.0.
bool isKnownNeverNaN(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
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,...
Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
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.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
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
Definition: SimplifyQuery.h:96
AssumptionCache * AC
Definition: SimplifyQuery.h:64