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