LLVM 20.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
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 implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
50#include "llvm/IR/Constant.h"
51#include "llvm/IR/Constants.h"
52#include "llvm/IR/DataLayout.h"
54#include "llvm/IR/DebugLoc.h"
56#include "llvm/IR/Function.h"
57#include "llvm/IR/GlobalValue.h"
58#include "llvm/IR/Metadata.h"
59#include "llvm/IR/Type.h"
63#include "llvm/Support/Debug.h"
67#include "llvm/Support/Mutex.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <set>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85using namespace llvm::SDPatternMatch;
86
87/// makeVTList - Return an instance of the SDVTList struct initialized with the
88/// specified members.
89static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
90 SDVTList Res = {VTs, NumVTs};
91 return Res;
92}
93
94// Default null implementations of the callbacks.
98
99void SelectionDAG::DAGNodeDeletedListener::anchor() {}
100void SelectionDAG::DAGNodeInsertedListener::anchor() {}
101
102#define DEBUG_TYPE "selectiondag"
103
104static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
105 cl::Hidden, cl::init(true),
106 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
107
108static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
109 cl::desc("Number limit for gluing ld/st of memcpy."),
110 cl::Hidden, cl::init(0));
111
113 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
114}
115
116//===----------------------------------------------------------------------===//
117// ConstantFPSDNode Class
118//===----------------------------------------------------------------------===//
119
120/// isExactlyValue - We don't rely on operator== working on double values, as
121/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
122/// As such, this method can be used to do an exact bit-for-bit comparison of
123/// two floating point values.
125 return getValueAPF().bitwiseIsEqual(V);
126}
127
129 const APFloat& Val) {
130 assert(VT.isFloatingPoint() && "Can only convert between FP types");
131
132 // convert modifies in place, so make a copy.
133 APFloat Val2 = APFloat(Val);
134 bool losesInfo;
136 &losesInfo);
137 return !losesInfo;
138}
139
140//===----------------------------------------------------------------------===//
141// ISD Namespace
142//===----------------------------------------------------------------------===//
143
144bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
145 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
146 unsigned EltSize =
147 N->getValueType(0).getVectorElementType().getSizeInBits();
148 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
149 SplatVal = Op0->getAPIntValue().trunc(EltSize);
150 return true;
151 }
152 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
153 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
154 return true;
155 }
156 }
157
158 auto *BV = dyn_cast<BuildVectorSDNode>(N);
159 if (!BV)
160 return false;
161
162 APInt SplatUndef;
163 unsigned SplatBitSize;
164 bool HasUndefs;
165 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
166 // Endianness does not matter here. We are checking for a splat given the
167 // element size of the vector, and if we find such a splat for little endian
168 // layout, then that should be valid also for big endian (as the full vector
169 // size is known to be a multiple of the element size).
170 const bool IsBigEndian = false;
171 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
172 EltSize, IsBigEndian) &&
173 EltSize == SplatBitSize;
174}
175
176// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
177// specializations of the more general isConstantSplatVector()?
178
179bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
180 // Look through a bit convert.
181 while (N->getOpcode() == ISD::BITCAST)
182 N = N->getOperand(0).getNode();
183
184 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
185 APInt SplatVal;
186 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
187 }
188
189 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
190
191 unsigned i = 0, e = N->getNumOperands();
192
193 // Skip over all of the undef values.
194 while (i != e && N->getOperand(i).isUndef())
195 ++i;
196
197 // Do not accept an all-undef vector.
198 if (i == e) return false;
199
200 // Do not accept build_vectors that aren't all constants or which have non-~0
201 // elements. We have to be a bit careful here, as the type of the constant
202 // may not be the same as the type of the vector elements due to type
203 // legalization (the elements are promoted to a legal type for the target and
204 // a vector of a type may be legal when the base element type is not).
205 // We only want to check enough bits to cover the vector elements, because
206 // we care if the resultant vector is all ones, not whether the individual
207 // constants are.
208 SDValue NotZero = N->getOperand(i);
209 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
210 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
211 if (CN->getAPIntValue().countr_one() < EltSize)
212 return false;
213 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
214 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
215 return false;
216 } else
217 return false;
218
219 // Okay, we have at least one ~0 value, check to see if the rest match or are
220 // undefs. Even with the above element type twiddling, this should be OK, as
221 // the same type legalization should have applied to all the elements.
222 for (++i; i != e; ++i)
223 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
224 return false;
225 return true;
226}
227
228bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
229 // Look through a bit convert.
230 while (N->getOpcode() == ISD::BITCAST)
231 N = N->getOperand(0).getNode();
232
233 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
234 APInt SplatVal;
235 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
236 }
237
238 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
239
240 bool IsAllUndef = true;
241 for (const SDValue &Op : N->op_values()) {
242 if (Op.isUndef())
243 continue;
244 IsAllUndef = false;
245 // Do not accept build_vectors that aren't all constants or which have non-0
246 // elements. We have to be a bit careful here, as the type of the constant
247 // may not be the same as the type of the vector elements due to type
248 // legalization (the elements are promoted to a legal type for the target
249 // and a vector of a type may be legal when the base element type is not).
250 // We only want to check enough bits to cover the vector elements, because
251 // we care if the resultant vector is all zeros, not whether the individual
252 // constants are.
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
255 if (CN->getAPIntValue().countr_zero() < EltSize)
256 return false;
257 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
258 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
259 return false;
260 } else
261 return false;
262 }
263
264 // Do not accept an all-undef vector.
265 if (IsAllUndef)
266 return false;
267 return true;
268}
269
271 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
272}
273
275 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
276}
277
279 if (N->getOpcode() != ISD::BUILD_VECTOR)
280 return false;
281
282 for (const SDValue &Op : N->op_values()) {
283 if (Op.isUndef())
284 continue;
285 if (!isa<ConstantSDNode>(Op))
286 return false;
287 }
288 return true;
289}
290
292 if (N->getOpcode() != ISD::BUILD_VECTOR)
293 return false;
294
295 for (const SDValue &Op : N->op_values()) {
296 if (Op.isUndef())
297 continue;
298 if (!isa<ConstantFPSDNode>(Op))
299 return false;
300 }
301 return true;
302}
303
304bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
305 bool Signed) {
306 assert(N->getValueType(0).isVector() && "Expected a vector!");
307
308 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
309 if (EltSize <= NewEltSize)
310 return false;
311
312 if (N->getOpcode() == ISD::ZERO_EXTEND) {
313 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
314 NewEltSize) &&
315 !Signed;
316 }
317 if (N->getOpcode() == ISD::SIGN_EXTEND) {
318 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
319 NewEltSize) &&
320 Signed;
321 }
322 if (N->getOpcode() != ISD::BUILD_VECTOR)
323 return false;
324
325 for (const SDValue &Op : N->op_values()) {
326 if (Op.isUndef())
327 continue;
328 if (!isa<ConstantSDNode>(Op))
329 return false;
330
331 APInt C = Op->getAsAPIntVal().trunc(EltSize);
332 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
333 return false;
334 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
335 return false;
336 }
337
338 return true;
339}
340
342 // Return false if the node has no operands.
343 // This is "logically inconsistent" with the definition of "all" but
344 // is probably the desired behavior.
345 if (N->getNumOperands() == 0)
346 return false;
347 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
348}
349
351 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
352}
353
354template <typename ConstNodeType>
356 std::function<bool(ConstNodeType *)> Match,
357 bool AllowUndefs) {
358 // FIXME: Add support for scalar UNDEF cases?
359 if (auto *C = dyn_cast<ConstNodeType>(Op))
360 return Match(C);
361
362 // FIXME: Add support for vector UNDEF cases?
363 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
364 ISD::SPLAT_VECTOR != Op.getOpcode())
365 return false;
366
367 EVT SVT = Op.getValueType().getScalarType();
368 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
369 if (AllowUndefs && Op.getOperand(i).isUndef()) {
370 if (!Match(nullptr))
371 return false;
372 continue;
373 }
374
375 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
376 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
377 return false;
378 }
379 return true;
380}
381// Build used template types.
382template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
383 SDValue, std::function<bool(ConstantSDNode *)>, bool);
384template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
385 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
386
388 SDValue LHS, SDValue RHS,
389 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
390 bool AllowUndefs, bool AllowTypeMismatch) {
391 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
392 return false;
393
394 // TODO: Add support for scalar UNDEF cases?
395 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
396 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
397 return Match(LHSCst, RHSCst);
398
399 // TODO: Add support for vector UNDEF cases?
400 if (LHS.getOpcode() != RHS.getOpcode() ||
401 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
402 LHS.getOpcode() != ISD::SPLAT_VECTOR))
403 return false;
404
405 EVT SVT = LHS.getValueType().getScalarType();
406 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
407 SDValue LHSOp = LHS.getOperand(i);
408 SDValue RHSOp = RHS.getOperand(i);
409 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
410 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
411 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
412 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
413 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
414 return false;
415 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
416 LHSOp.getValueType() != RHSOp.getValueType()))
417 return false;
418 if (!Match(LHSCst, RHSCst))
419 return false;
420 }
421 return true;
422}
423
425 switch (VecReduceOpcode) {
426 default:
427 llvm_unreachable("Expected VECREDUCE opcode");
430 case ISD::VP_REDUCE_FADD:
431 case ISD::VP_REDUCE_SEQ_FADD:
432 return ISD::FADD;
435 case ISD::VP_REDUCE_FMUL:
436 case ISD::VP_REDUCE_SEQ_FMUL:
437 return ISD::FMUL;
439 case ISD::VP_REDUCE_ADD:
440 return ISD::ADD;
442 case ISD::VP_REDUCE_MUL:
443 return ISD::MUL;
445 case ISD::VP_REDUCE_AND:
446 return ISD::AND;
448 case ISD::VP_REDUCE_OR:
449 return ISD::OR;
451 case ISD::VP_REDUCE_XOR:
452 return ISD::XOR;
454 case ISD::VP_REDUCE_SMAX:
455 return ISD::SMAX;
457 case ISD::VP_REDUCE_SMIN:
458 return ISD::SMIN;
460 case ISD::VP_REDUCE_UMAX:
461 return ISD::UMAX;
463 case ISD::VP_REDUCE_UMIN:
464 return ISD::UMIN;
466 case ISD::VP_REDUCE_FMAX:
467 return ISD::FMAXNUM;
469 case ISD::VP_REDUCE_FMIN:
470 return ISD::FMINNUM;
472 case ISD::VP_REDUCE_FMAXIMUM:
473 return ISD::FMAXIMUM;
475 case ISD::VP_REDUCE_FMINIMUM:
476 return ISD::FMINIMUM;
477 }
478}
479
480bool ISD::isVPOpcode(unsigned Opcode) {
481 switch (Opcode) {
482 default:
483 return false;
484#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
485 case ISD::VPSD: \
486 return true;
487#include "llvm/IR/VPIntrinsics.def"
488 }
489}
490
491bool ISD::isVPBinaryOp(unsigned Opcode) {
492 switch (Opcode) {
493 default:
494 break;
495#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
496#define VP_PROPERTY_BINARYOP return true;
497#define END_REGISTER_VP_SDNODE(VPSD) break;
498#include "llvm/IR/VPIntrinsics.def"
499 }
500 return false;
501}
502
503bool ISD::isVPReduction(unsigned Opcode) {
504 switch (Opcode) {
505 default:
506 break;
507#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
508#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
509#define END_REGISTER_VP_SDNODE(VPSD) break;
510#include "llvm/IR/VPIntrinsics.def"
511 }
512 return false;
513}
514
515/// The operand position of the vector mask.
516std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
517 switch (Opcode) {
518 default:
519 return std::nullopt;
520#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
521 case ISD::VPSD: \
522 return MASKPOS;
523#include "llvm/IR/VPIntrinsics.def"
524 }
525}
526
527/// The operand position of the explicit vector length parameter.
528std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
529 switch (Opcode) {
530 default:
531 return std::nullopt;
532#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
533 case ISD::VPSD: \
534 return EVLPOS;
535#include "llvm/IR/VPIntrinsics.def"
536 }
537}
538
539std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
540 bool hasFPExcept) {
541 // FIXME: Return strict opcodes in case of fp exceptions.
542 switch (VPOpcode) {
543 default:
544 return std::nullopt;
545#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
546#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
547#define END_REGISTER_VP_SDNODE(VPOPC) break;
548#include "llvm/IR/VPIntrinsics.def"
549 }
550 return std::nullopt;
551}
552
553std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
554 switch (Opcode) {
555 default:
556 return std::nullopt;
557#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
558#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
559#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
560#include "llvm/IR/VPIntrinsics.def"
561 }
562}
563
565 switch (ExtType) {
566 case ISD::EXTLOAD:
567 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
568 case ISD::SEXTLOAD:
569 return ISD::SIGN_EXTEND;
570 case ISD::ZEXTLOAD:
571 return ISD::ZERO_EXTEND;
572 default:
573 break;
574 }
575
576 llvm_unreachable("Invalid LoadExtType");
577}
578
580 // To perform this operation, we just need to swap the L and G bits of the
581 // operation.
582 unsigned OldL = (Operation >> 2) & 1;
583 unsigned OldG = (Operation >> 1) & 1;
584 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
585 (OldL << 1) | // New G bit
586 (OldG << 2)); // New L bit.
587}
588
590 unsigned Operation = Op;
591 if (isIntegerLike)
592 Operation ^= 7; // Flip L, G, E bits, but not U.
593 else
594 Operation ^= 15; // Flip all of the condition bits.
595
597 Operation &= ~8; // Don't let N and U bits get set.
598
599 return ISD::CondCode(Operation);
600}
601
603 return getSetCCInverseImpl(Op, Type.isInteger());
604}
605
607 bool isIntegerLike) {
608 return getSetCCInverseImpl(Op, isIntegerLike);
609}
610
611/// For an integer comparison, return 1 if the comparison is a signed operation
612/// and 2 if the result is an unsigned comparison. Return zero if the operation
613/// does not depend on the sign of the input (setne and seteq).
614static int isSignedOp(ISD::CondCode Opcode) {
615 switch (Opcode) {
616 default: llvm_unreachable("Illegal integer setcc operation!");
617 case ISD::SETEQ:
618 case ISD::SETNE: return 0;
619 case ISD::SETLT:
620 case ISD::SETLE:
621 case ISD::SETGT:
622 case ISD::SETGE: return 1;
623 case ISD::SETULT:
624 case ISD::SETULE:
625 case ISD::SETUGT:
626 case ISD::SETUGE: return 2;
627 }
628}
629
631 EVT Type) {
632 bool IsInteger = Type.isInteger();
633 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
634 // Cannot fold a signed integer setcc with an unsigned integer setcc.
635 return ISD::SETCC_INVALID;
636
637 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
638
639 // If the N and U bits get set, then the resultant comparison DOES suddenly
640 // care about orderedness, and it is true when ordered.
641 if (Op > ISD::SETTRUE2)
642 Op &= ~16; // Clear the U bit if the N bit is set.
643
644 // Canonicalize illegal integer setcc's.
645 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
646 Op = ISD::SETNE;
647
648 return ISD::CondCode(Op);
649}
650
652 EVT Type) {
653 bool IsInteger = Type.isInteger();
654 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
655 // Cannot fold a signed setcc with an unsigned setcc.
656 return ISD::SETCC_INVALID;
657
658 // Combine all of the condition bits.
659 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
660
661 // Canonicalize illegal integer setcc's.
662 if (IsInteger) {
663 switch (Result) {
664 default: break;
665 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
666 case ISD::SETOEQ: // SETEQ & SETU[LG]E
667 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
668 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
669 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
670 }
671 }
672
673 return Result;
674}
675
676//===----------------------------------------------------------------------===//
677// SDNode Profile Support
678//===----------------------------------------------------------------------===//
679
680/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
681static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
682 ID.AddInteger(OpC);
683}
684
685/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
686/// solely with their pointer.
688 ID.AddPointer(VTList.VTs);
689}
690
691/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
693 ArrayRef<SDValue> Ops) {
694 for (const auto &Op : Ops) {
695 ID.AddPointer(Op.getNode());
696 ID.AddInteger(Op.getResNo());
697 }
698}
699
700/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
702 ArrayRef<SDUse> Ops) {
703 for (const auto &Op : Ops) {
704 ID.AddPointer(Op.getNode());
705 ID.AddInteger(Op.getResNo());
706 }
707}
708
709static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
710 SDVTList VTList, ArrayRef<SDValue> OpList) {
711 AddNodeIDOpcode(ID, OpC);
712 AddNodeIDValueTypes(ID, VTList);
713 AddNodeIDOperands(ID, OpList);
714}
715
716/// If this is an SDNode with special info, add this info to the NodeID data.
718 switch (N->getOpcode()) {
721 case ISD::MCSymbol:
722 llvm_unreachable("Should only be used on nodes with operands");
723 default: break; // Normal nodes don't need extra info.
725 case ISD::Constant: {
726 const ConstantSDNode *C = cast<ConstantSDNode>(N);
727 ID.AddPointer(C->getConstantIntValue());
728 ID.AddBoolean(C->isOpaque());
729 break;
730 }
732 case ISD::ConstantFP:
733 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
734 break;
739 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
740 ID.AddPointer(GA->getGlobal());
741 ID.AddInteger(GA->getOffset());
742 ID.AddInteger(GA->getTargetFlags());
743 break;
744 }
745 case ISD::BasicBlock:
746 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
747 break;
748 case ISD::Register:
749 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
750 break;
752 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
753 break;
754 case ISD::SRCVALUE:
755 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
756 break;
757 case ISD::FrameIndex:
759 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
760 break;
763 if (cast<LifetimeSDNode>(N)->hasOffset()) {
764 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
765 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
766 }
767 break;
769 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
770 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
771 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
772 break;
773 case ISD::JumpTable:
775 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
776 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
777 break;
780 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
781 ID.AddInteger(CP->getAlign().value());
782 ID.AddInteger(CP->getOffset());
783 if (CP->isMachineConstantPoolEntry())
784 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
785 else
786 ID.AddPointer(CP->getConstVal());
787 ID.AddInteger(CP->getTargetFlags());
788 break;
789 }
790 case ISD::TargetIndex: {
791 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
792 ID.AddInteger(TI->getIndex());
793 ID.AddInteger(TI->getOffset());
794 ID.AddInteger(TI->getTargetFlags());
795 break;
796 }
797 case ISD::LOAD: {
798 const LoadSDNode *LD = cast<LoadSDNode>(N);
799 ID.AddInteger(LD->getMemoryVT().getRawBits());
800 ID.AddInteger(LD->getRawSubclassData());
801 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
802 ID.AddInteger(LD->getMemOperand()->getFlags());
803 break;
804 }
805 case ISD::STORE: {
806 const StoreSDNode *ST = cast<StoreSDNode>(N);
807 ID.AddInteger(ST->getMemoryVT().getRawBits());
808 ID.AddInteger(ST->getRawSubclassData());
809 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
810 ID.AddInteger(ST->getMemOperand()->getFlags());
811 break;
812 }
813 case ISD::VP_LOAD: {
814 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
815 ID.AddInteger(ELD->getMemoryVT().getRawBits());
816 ID.AddInteger(ELD->getRawSubclassData());
817 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
818 ID.AddInteger(ELD->getMemOperand()->getFlags());
819 break;
820 }
821 case ISD::VP_STORE: {
822 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
823 ID.AddInteger(EST->getMemoryVT().getRawBits());
824 ID.AddInteger(EST->getRawSubclassData());
825 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
826 ID.AddInteger(EST->getMemOperand()->getFlags());
827 break;
828 }
829 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
830 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
831 ID.AddInteger(SLD->getMemoryVT().getRawBits());
832 ID.AddInteger(SLD->getRawSubclassData());
833 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
834 break;
835 }
836 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
837 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
838 ID.AddInteger(SST->getMemoryVT().getRawBits());
839 ID.AddInteger(SST->getRawSubclassData());
840 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
841 break;
842 }
843 case ISD::VP_GATHER: {
844 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
845 ID.AddInteger(EG->getMemoryVT().getRawBits());
846 ID.AddInteger(EG->getRawSubclassData());
847 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
848 ID.AddInteger(EG->getMemOperand()->getFlags());
849 break;
850 }
851 case ISD::VP_SCATTER: {
852 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
853 ID.AddInteger(ES->getMemoryVT().getRawBits());
854 ID.AddInteger(ES->getRawSubclassData());
855 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
856 ID.AddInteger(ES->getMemOperand()->getFlags());
857 break;
858 }
859 case ISD::MLOAD: {
860 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
861 ID.AddInteger(MLD->getMemoryVT().getRawBits());
862 ID.AddInteger(MLD->getRawSubclassData());
863 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
864 ID.AddInteger(MLD->getMemOperand()->getFlags());
865 break;
866 }
867 case ISD::MSTORE: {
868 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
869 ID.AddInteger(MST->getMemoryVT().getRawBits());
870 ID.AddInteger(MST->getRawSubclassData());
871 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
872 ID.AddInteger(MST->getMemOperand()->getFlags());
873 break;
874 }
875 case ISD::MGATHER: {
876 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
877 ID.AddInteger(MG->getMemoryVT().getRawBits());
878 ID.AddInteger(MG->getRawSubclassData());
879 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
880 ID.AddInteger(MG->getMemOperand()->getFlags());
881 break;
882 }
883 case ISD::MSCATTER: {
884 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
885 ID.AddInteger(MS->getMemoryVT().getRawBits());
886 ID.AddInteger(MS->getRawSubclassData());
887 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
888 ID.AddInteger(MS->getMemOperand()->getFlags());
889 break;
890 }
893 case ISD::ATOMIC_SWAP:
905 case ISD::ATOMIC_LOAD:
906 case ISD::ATOMIC_STORE: {
907 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
908 ID.AddInteger(AT->getMemoryVT().getRawBits());
909 ID.AddInteger(AT->getRawSubclassData());
910 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
911 ID.AddInteger(AT->getMemOperand()->getFlags());
912 break;
913 }
914 case ISD::VECTOR_SHUFFLE: {
915 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
916 for (int M : Mask)
917 ID.AddInteger(M);
918 break;
919 }
921 case ISD::BlockAddress: {
922 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
923 ID.AddPointer(BA->getBlockAddress());
924 ID.AddInteger(BA->getOffset());
925 ID.AddInteger(BA->getTargetFlags());
926 break;
927 }
928 case ISD::AssertAlign:
929 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
930 break;
931 case ISD::PREFETCH:
934 // Handled by MemIntrinsicSDNode check after the switch.
935 break;
936 } // end switch (N->getOpcode())
937
938 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
939 // to check.
940 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
941 ID.AddInteger(MN->getRawSubclassData());
942 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
943 ID.AddInteger(MN->getMemOperand()->getFlags());
944 ID.AddInteger(MN->getMemoryVT().getRawBits());
945 }
946}
947
948/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
949/// data.
950static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
951 AddNodeIDOpcode(ID, N->getOpcode());
952 // Add the return value info.
953 AddNodeIDValueTypes(ID, N->getVTList());
954 // Add the operand info.
955 AddNodeIDOperands(ID, N->ops());
956
957 // Handle SDNode leafs with special info.
959}
960
961//===----------------------------------------------------------------------===//
962// SelectionDAG Class
963//===----------------------------------------------------------------------===//
964
965/// doNotCSE - Return true if CSE should not be performed for this node.
966static bool doNotCSE(SDNode *N) {
967 if (N->getValueType(0) == MVT::Glue)
968 return true; // Never CSE anything that produces a glue result.
969
970 switch (N->getOpcode()) {
971 default: break;
972 case ISD::HANDLENODE:
973 case ISD::EH_LABEL:
974 return true; // Never CSE these nodes.
975 }
976
977 // Check that remaining values produced are not flags.
978 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
979 if (N->getValueType(i) == MVT::Glue)
980 return true; // Never CSE anything that produces a glue result.
981
982 return false;
983}
984
985/// RemoveDeadNodes - This method deletes all unreachable nodes in the
986/// SelectionDAG.
988 // Create a dummy node (which is not added to allnodes), that adds a reference
989 // to the root node, preventing it from being deleted.
990 HandleSDNode Dummy(getRoot());
991
993
994 // Add all obviously-dead nodes to the DeadNodes worklist.
995 for (SDNode &Node : allnodes())
996 if (Node.use_empty())
997 DeadNodes.push_back(&Node);
998
999 RemoveDeadNodes(DeadNodes);
1000
1001 // If the root changed (e.g. it was a dead load, update the root).
1002 setRoot(Dummy.getValue());
1003}
1004
1005/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1006/// given list, and any nodes that become unreachable as a result.
1008
1009 // Process the worklist, deleting the nodes and adding their uses to the
1010 // worklist.
1011 while (!DeadNodes.empty()) {
1012 SDNode *N = DeadNodes.pop_back_val();
1013 // Skip to next node if we've already managed to delete the node. This could
1014 // happen if replacing a node causes a node previously added to the node to
1015 // be deleted.
1016 if (N->getOpcode() == ISD::DELETED_NODE)
1017 continue;
1018
1019 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1020 DUL->NodeDeleted(N, nullptr);
1021
1022 // Take the node out of the appropriate CSE map.
1023 RemoveNodeFromCSEMaps(N);
1024
1025 // Next, brutally remove the operand list. This is safe to do, as there are
1026 // no cycles in the graph.
1027 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1028 SDUse &Use = *I++;
1029 SDNode *Operand = Use.getNode();
1030 Use.set(SDValue());
1031
1032 // Now that we removed this operand, see if there are no uses of it left.
1033 if (Operand->use_empty())
1034 DeadNodes.push_back(Operand);
1035 }
1036
1037 DeallocateNode(N);
1038 }
1039}
1040
1042 SmallVector<SDNode*, 16> DeadNodes(1, N);
1043
1044 // Create a dummy node that adds a reference to the root node, preventing
1045 // it from being deleted. (This matters if the root is an operand of the
1046 // dead node.)
1047 HandleSDNode Dummy(getRoot());
1048
1049 RemoveDeadNodes(DeadNodes);
1050}
1051
1053 // First take this out of the appropriate CSE map.
1054 RemoveNodeFromCSEMaps(N);
1055
1056 // Finally, remove uses due to operands of this node, remove from the
1057 // AllNodes list, and delete the node.
1058 DeleteNodeNotInCSEMaps(N);
1059}
1060
1061void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1062 assert(N->getIterator() != AllNodes.begin() &&
1063 "Cannot delete the entry node!");
1064 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1065
1066 // Drop all of the operands and decrement used node's use counts.
1067 N->DropOperands();
1068
1069 DeallocateNode(N);
1070}
1071
1072void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1073 assert(!(V->isVariadic() && isParameter));
1074 if (isParameter)
1075 ByvalParmDbgValues.push_back(V);
1076 else
1077 DbgValues.push_back(V);
1078 for (const SDNode *Node : V->getSDNodes())
1079 if (Node)
1080 DbgValMap[Node].push_back(V);
1081}
1082
1083void SDDbgInfo::erase(const SDNode *Node) {
1084 DbgValMapType::iterator I = DbgValMap.find(Node);
1085 if (I == DbgValMap.end())
1086 return;
1087 for (auto &Val: I->second)
1088 Val->setIsInvalidated();
1089 DbgValMap.erase(I);
1090}
1091
1092void SelectionDAG::DeallocateNode(SDNode *N) {
1093 // If we have operands, deallocate them.
1094 removeOperands(N);
1095
1096 NodeAllocator.Deallocate(AllNodes.remove(N));
1097
1098 // Set the opcode to DELETED_NODE to help catch bugs when node
1099 // memory is reallocated.
1100 // FIXME: There are places in SDag that have grown a dependency on the opcode
1101 // value in the released node.
1102 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1103 N->NodeType = ISD::DELETED_NODE;
1104
1105 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1106 // them and forget about that node.
1107 DbgInfo->erase(N);
1108
1109 // Invalidate extra info.
1110 SDEI.erase(N);
1111}
1112
1113#ifndef NDEBUG
1114/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1115static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1116 switch (N->getOpcode()) {
1117 default:
1118 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1119 TLI->verifyTargetSDNode(N);
1120 break;
1121 case ISD::BUILD_PAIR: {
1122 EVT VT = N->getValueType(0);
1123 assert(N->getNumValues() == 1 && "Too many results!");
1124 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1125 "Wrong return type!");
1126 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1127 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1128 "Mismatched operand types!");
1129 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1130 "Wrong operand type!");
1131 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1132 "Wrong return type size");
1133 break;
1134 }
1135 case ISD::BUILD_VECTOR: {
1136 assert(N->getNumValues() == 1 && "Too many results!");
1137 assert(N->getValueType(0).isVector() && "Wrong return type!");
1138 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1139 "Wrong number of operands!");
1140 EVT EltVT = N->getValueType(0).getVectorElementType();
1141 for (const SDUse &Op : N->ops()) {
1142 assert((Op.getValueType() == EltVT ||
1143 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1144 EltVT.bitsLE(Op.getValueType()))) &&
1145 "Wrong operand type!");
1146 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1147 "Operands must all have the same type");
1148 }
1149 break;
1150 }
1151 }
1152}
1153#endif // NDEBUG
1154
1155/// Insert a newly allocated node into the DAG.
1156///
1157/// Handles insertion into the all nodes list and CSE map, as well as
1158/// verification and other common operations when a new node is allocated.
1159void SelectionDAG::InsertNode(SDNode *N) {
1160 AllNodes.push_back(N);
1161#ifndef NDEBUG
1162 N->PersistentId = NextPersistentId++;
1163 VerifySDNode(N, TLI);
1164#endif
1165 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1166 DUL->NodeInserted(N);
1167}
1168
1169/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1170/// correspond to it. This is useful when we're about to delete or repurpose
1171/// the node. We don't want future request for structurally identical nodes
1172/// to return N anymore.
1173bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1174 bool Erased = false;
1175 switch (N->getOpcode()) {
1176 case ISD::HANDLENODE: return false; // noop.
1177 case ISD::CONDCODE:
1178 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1179 "Cond code doesn't exist!");
1180 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1181 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1182 break;
1184 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1185 break;
1187 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1188 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1189 ESN->getSymbol(), ESN->getTargetFlags()));
1190 break;
1191 }
1192 case ISD::MCSymbol: {
1193 auto *MCSN = cast<MCSymbolSDNode>(N);
1194 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1195 break;
1196 }
1197 case ISD::VALUETYPE: {
1198 EVT VT = cast<VTSDNode>(N)->getVT();
1199 if (VT.isExtended()) {
1200 Erased = ExtendedValueTypeNodes.erase(VT);
1201 } else {
1202 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1203 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1204 }
1205 break;
1206 }
1207 default:
1208 // Remove it from the CSE Map.
1209 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1210 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1211 Erased = CSEMap.RemoveNode(N);
1212 break;
1213 }
1214#ifndef NDEBUG
1215 // Verify that the node was actually in one of the CSE maps, unless it has a
1216 // glue result (which cannot be CSE'd) or is one of the special cases that are
1217 // not subject to CSE.
1218 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1219 !N->isMachineOpcode() && !doNotCSE(N)) {
1220 N->dump(this);
1221 dbgs() << "\n";
1222 llvm_unreachable("Node is not in map!");
1223 }
1224#endif
1225 return Erased;
1226}
1227
1228/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1229/// maps and modified in place. Add it back to the CSE maps, unless an identical
1230/// node already exists, in which case transfer all its users to the existing
1231/// node. This transfer can potentially trigger recursive merging.
1232void
1233SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1234 // For node types that aren't CSE'd, just act as if no identical node
1235 // already exists.
1236 if (!doNotCSE(N)) {
1237 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1238 if (Existing != N) {
1239 // If there was already an existing matching node, use ReplaceAllUsesWith
1240 // to replace the dead one with the existing one. This can cause
1241 // recursive merging of other unrelated nodes down the line.
1242 Existing->intersectFlagsWith(N->getFlags());
1243 ReplaceAllUsesWith(N, Existing);
1244
1245 // N is now dead. Inform the listeners and delete it.
1246 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1247 DUL->NodeDeleted(N, Existing);
1248 DeleteNodeNotInCSEMaps(N);
1249 return;
1250 }
1251 }
1252
1253 // If the node doesn't already exist, we updated it. Inform listeners.
1254 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1255 DUL->NodeUpdated(N);
1256}
1257
1258/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1259/// were replaced with those specified. If this node is never memoized,
1260/// return null, otherwise return a pointer to the slot it would take. If a
1261/// node already exists with these operands, the slot will be non-null.
1262SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1263 void *&InsertPos) {
1264 if (doNotCSE(N))
1265 return nullptr;
1266
1267 SDValue Ops[] = { Op };
1269 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1271 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1272 if (Node)
1273 Node->intersectFlagsWith(N->getFlags());
1274 return Node;
1275}
1276
1277/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1278/// were replaced with those specified. If this node is never memoized,
1279/// return null, otherwise return a pointer to the slot it would take. If a
1280/// node already exists with these operands, the slot will be non-null.
1281SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1282 SDValue Op1, SDValue Op2,
1283 void *&InsertPos) {
1284 if (doNotCSE(N))
1285 return nullptr;
1286
1287 SDValue Ops[] = { Op1, Op2 };
1289 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1291 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1292 if (Node)
1293 Node->intersectFlagsWith(N->getFlags());
1294 return Node;
1295}
1296
1297/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1298/// were replaced with those specified. If this node is never memoized,
1299/// return null, otherwise return a pointer to the slot it would take. If a
1300/// node already exists with these operands, the slot will be non-null.
1301SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1302 void *&InsertPos) {
1303 if (doNotCSE(N))
1304 return nullptr;
1305
1307 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1309 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1310 if (Node)
1311 Node->intersectFlagsWith(N->getFlags());
1312 return Node;
1313}
1314
1316 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1317 : VT.getTypeForEVT(*getContext());
1318
1319 return getDataLayout().getABITypeAlign(Ty);
1320}
1321
1322// EntryNode could meaningfully have debug info if we can find it...
1324 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1325 getVTList(MVT::Other, MVT::Glue)),
1326 Root(getEntryNode()) {
1327 InsertNode(&EntryNode);
1328 DbgInfo = new SDDbgInfo();
1329}
1330
1332 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1333 const TargetLibraryInfo *LibraryInfo,
1334 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1336 FunctionVarLocs const *VarLocs) {
1337 MF = &NewMF;
1338 SDAGISelPass = PassPtr;
1339 ORE = &NewORE;
1342 LibInfo = LibraryInfo;
1343 Context = &MF->getFunction().getContext();
1344 UA = NewUA;
1345 PSI = PSIin;
1346 BFI = BFIin;
1347 MMI = &MMIin;
1348 FnVarLocs = VarLocs;
1349}
1350
1352 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1353 allnodes_clear();
1354 OperandRecycler.clear(OperandAllocator);
1355 delete DbgInfo;
1356}
1357
1359 return MF->getFunction().hasOptSize() ||
1361}
1362
1363void SelectionDAG::allnodes_clear() {
1364 assert(&*AllNodes.begin() == &EntryNode);
1365 AllNodes.remove(AllNodes.begin());
1366 while (!AllNodes.empty())
1367 DeallocateNode(&AllNodes.front());
1368#ifndef NDEBUG
1369 NextPersistentId = 0;
1370#endif
1371}
1372
1373SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1374 void *&InsertPos) {
1375 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1376 if (N) {
1377 switch (N->getOpcode()) {
1378 default: break;
1379 case ISD::Constant:
1380 case ISD::ConstantFP:
1381 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1382 "debug location. Use another overload.");
1383 }
1384 }
1385 return N;
1386}
1387
1388SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1389 const SDLoc &DL, void *&InsertPos) {
1390 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1391 if (N) {
1392 switch (N->getOpcode()) {
1393 case ISD::Constant:
1394 case ISD::ConstantFP:
1395 // Erase debug location from the node if the node is used at several
1396 // different places. Do not propagate one location to all uses as it
1397 // will cause a worse single stepping debugging experience.
1398 if (N->getDebugLoc() != DL.getDebugLoc())
1399 N->setDebugLoc(DebugLoc());
1400 break;
1401 default:
1402 // When the node's point of use is located earlier in the instruction
1403 // sequence than its prior point of use, update its debug info to the
1404 // earlier location.
1405 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1406 N->setDebugLoc(DL.getDebugLoc());
1407 break;
1408 }
1409 }
1410 return N;
1411}
1412
1414 allnodes_clear();
1415 OperandRecycler.clear(OperandAllocator);
1416 OperandAllocator.Reset();
1417 CSEMap.clear();
1418
1419 ExtendedValueTypeNodes.clear();
1420 ExternalSymbols.clear();
1421 TargetExternalSymbols.clear();
1422 MCSymbols.clear();
1423 SDEI.clear();
1424 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1425 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1426
1427 EntryNode.UseList = nullptr;
1428 InsertNode(&EntryNode);
1429 Root = getEntryNode();
1430 DbgInfo->clear();
1431}
1432
1434 return VT.bitsGT(Op.getValueType())
1435 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1436 : getNode(ISD::FP_ROUND, DL, VT, Op,
1437 getIntPtrConstant(0, DL, /*isTarget=*/true));
1438}
1439
1440std::pair<SDValue, SDValue>
1442 const SDLoc &DL, EVT VT) {
1443 assert(!VT.bitsEq(Op.getValueType()) &&
1444 "Strict no-op FP extend/round not allowed.");
1445 SDValue Res =
1446 VT.bitsGT(Op.getValueType())
1447 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1448 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1449 {Chain, Op, getIntPtrConstant(0, DL)});
1450
1451 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1452}
1453
1455 return VT.bitsGT(Op.getValueType()) ?
1456 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1457 getNode(ISD::TRUNCATE, DL, VT, Op);
1458}
1459
1461 return VT.bitsGT(Op.getValueType()) ?
1462 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1463 getNode(ISD::TRUNCATE, DL, VT, Op);
1464}
1465
1467 return VT.bitsGT(Op.getValueType()) ?
1468 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1469 getNode(ISD::TRUNCATE, DL, VT, Op);
1470}
1471
1473 EVT VT) {
1474 assert(!VT.isVector());
1475 auto Type = Op.getValueType();
1476 SDValue DestOp;
1477 if (Type == VT)
1478 return Op;
1479 auto Size = Op.getValueSizeInBits();
1480 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1481 if (DestOp.getValueType() == VT)
1482 return DestOp;
1483
1484 return getAnyExtOrTrunc(DestOp, DL, VT);
1485}
1486
1488 EVT VT) {
1489 assert(!VT.isVector());
1490 auto Type = Op.getValueType();
1491 SDValue DestOp;
1492 if (Type == VT)
1493 return Op;
1494 auto Size = Op.getValueSizeInBits();
1495 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1496 if (DestOp.getValueType() == VT)
1497 return DestOp;
1498
1499 return getSExtOrTrunc(DestOp, DL, VT);
1500}
1501
1503 EVT VT) {
1504 assert(!VT.isVector());
1505 auto Type = Op.getValueType();
1506 SDValue DestOp;
1507 if (Type == VT)
1508 return Op;
1509 auto Size = Op.getValueSizeInBits();
1510 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1511 if (DestOp.getValueType() == VT)
1512 return DestOp;
1513
1514 return getZExtOrTrunc(DestOp, DL, VT);
1515}
1516
1518 EVT OpVT) {
1519 if (VT.bitsLE(Op.getValueType()))
1520 return getNode(ISD::TRUNCATE, SL, VT, Op);
1521
1523 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1524}
1525
1527 EVT OpVT = Op.getValueType();
1528 assert(VT.isInteger() && OpVT.isInteger() &&
1529 "Cannot getZeroExtendInReg FP types");
1530 assert(VT.isVector() == OpVT.isVector() &&
1531 "getZeroExtendInReg type should be vector iff the operand "
1532 "type is vector!");
1533 assert((!VT.isVector() ||
1535 "Vector element counts must match in getZeroExtendInReg");
1536 assert(VT.bitsLE(OpVT) && "Not extending!");
1537 if (OpVT == VT)
1538 return Op;
1540 VT.getScalarSizeInBits());
1541 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1542}
1543
1545 SDValue EVL, const SDLoc &DL,
1546 EVT VT) {
1547 EVT OpVT = Op.getValueType();
1548 assert(VT.isInteger() && OpVT.isInteger() &&
1549 "Cannot getVPZeroExtendInReg FP types");
1550 assert(VT.isVector() && OpVT.isVector() &&
1551 "getVPZeroExtendInReg type and operand type should be vector!");
1553 "Vector element counts must match in getZeroExtendInReg");
1554 assert(VT.bitsLE(OpVT) && "Not extending!");
1555 if (OpVT == VT)
1556 return Op;
1558 VT.getScalarSizeInBits());
1559 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1560 EVL);
1561}
1562
1564 // Only unsigned pointer semantics are supported right now. In the future this
1565 // might delegate to TLI to check pointer signedness.
1566 return getZExtOrTrunc(Op, DL, VT);
1567}
1568
1570 // Only unsigned pointer semantics are supported right now. In the future this
1571 // might delegate to TLI to check pointer signedness.
1572 return getZeroExtendInReg(Op, DL, VT);
1573}
1574
1576 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1577}
1578
1579/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1581 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1582}
1583
1585 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1586 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1587}
1588
1590 SDValue Mask, SDValue EVL, EVT VT) {
1591 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1592 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1593}
1594
1596 SDValue Mask, SDValue EVL) {
1597 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1598}
1599
1601 SDValue Mask, SDValue EVL) {
1602 if (VT.bitsGT(Op.getValueType()))
1603 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1604 if (VT.bitsLT(Op.getValueType()))
1605 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1606 return Op;
1607}
1608
1610 EVT OpVT) {
1611 if (!V)
1612 return getConstant(0, DL, VT);
1613
1614 switch (TLI->getBooleanContents(OpVT)) {
1617 return getConstant(1, DL, VT);
1619 return getAllOnesConstant(DL, VT);
1620 }
1621 llvm_unreachable("Unexpected boolean content enum!");
1622}
1623
1625 bool isT, bool isO) {
1626 EVT EltVT = VT.getScalarType();
1627 assert((EltVT.getSizeInBits() >= 64 ||
1628 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1629 "getConstant with a uint64_t value that doesn't fit in the type!");
1630 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1631}
1632
1634 bool isT, bool isO) {
1635 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1636}
1637
1639 EVT VT, bool isT, bool isO) {
1640 assert(VT.isInteger() && "Cannot create FP integer constant!");
1641
1642 EVT EltVT = VT.getScalarType();
1643 const ConstantInt *Elt = &Val;
1644
1645 // In some cases the vector type is legal but the element type is illegal and
1646 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1647 // inserted value (the type does not need to match the vector element type).
1648 // Any extra bits introduced will be truncated away.
1649 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1651 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1652 APInt NewVal;
1653 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1654 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1655 else
1656 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1657 Elt = ConstantInt::get(*getContext(), NewVal);
1658 }
1659 // In other cases the element type is illegal and needs to be expanded, for
1660 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1661 // the value into n parts and use a vector type with n-times the elements.
1662 // Then bitcast to the type requested.
1663 // Legalizing constants too early makes the DAGCombiner's job harder so we
1664 // only legalize if the DAG tells us we must produce legal types.
1665 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1666 TLI->getTypeAction(*getContext(), EltVT) ==
1668 const APInt &NewVal = Elt->getValue();
1669 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1670 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1671
1672 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1673 if (VT.isScalableVector() ||
1675 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1676 "Can only handle an even split!");
1677 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1678
1679 SmallVector<SDValue, 2> ScalarParts;
1680 for (unsigned i = 0; i != Parts; ++i)
1681 ScalarParts.push_back(getConstant(
1682 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1683 ViaEltVT, isT, isO));
1684
1685 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1686 }
1687
1688 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1689 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1690
1691 // Check the temporary vector is the correct size. If this fails then
1692 // getTypeToTransformTo() probably returned a type whose size (in bits)
1693 // isn't a power-of-2 factor of the requested type size.
1694 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1695
1696 SmallVector<SDValue, 2> EltParts;
1697 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1698 EltParts.push_back(getConstant(
1699 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1700 ViaEltVT, isT, isO));
1701
1702 // EltParts is currently in little endian order. If we actually want
1703 // big-endian order then reverse it now.
1704 if (getDataLayout().isBigEndian())
1705 std::reverse(EltParts.begin(), EltParts.end());
1706
1707 // The elements must be reversed when the element order is different
1708 // to the endianness of the elements (because the BITCAST is itself a
1709 // vector shuffle in this situation). However, we do not need any code to
1710 // perform this reversal because getConstant() is producing a vector
1711 // splat.
1712 // This situation occurs in MIPS MSA.
1713
1715 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1716 llvm::append_range(Ops, EltParts);
1717
1718 SDValue V =
1719 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1720 return V;
1721 }
1722
1723 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1724 "APInt size does not match type size!");
1725 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1726 SDVTList VTs = getVTList(EltVT);
1728 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1729 ID.AddPointer(Elt);
1730 ID.AddBoolean(isO);
1731 void *IP = nullptr;
1732 SDNode *N = nullptr;
1733 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1734 if (!VT.isVector())
1735 return SDValue(N, 0);
1736
1737 if (!N) {
1738 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1739 CSEMap.InsertNode(N, IP);
1740 InsertNode(N);
1741 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1742 }
1743
1744 SDValue Result(N, 0);
1745 if (VT.isVector())
1746 Result = getSplat(VT, DL, Result);
1747 return Result;
1748}
1749
1751 bool isT, bool isO) {
1752 unsigned Size = VT.getScalarSizeInBits();
1753 assert(
1754 isIntN(Size, Val) &&
1755 "getSignedConstant with a int64_t value that doesn't fit in the type!");
1756 return getConstant(APInt(Size, Val, true), DL, VT, isT, isO);
1757}
1758
1760 bool IsOpaque) {
1762 IsTarget, IsOpaque);
1763}
1764
1766 bool isTarget) {
1767 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1768}
1769
1771 const SDLoc &DL) {
1772 assert(VT.isInteger() && "Shift amount is not an integer type!");
1773 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1774 return getConstant(Val, DL, ShiftVT);
1775}
1776
1778 const SDLoc &DL) {
1779 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1780 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1781}
1782
1784 bool isTarget) {
1785 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1786}
1787
1789 bool isTarget) {
1790 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1791}
1792
1794 EVT VT, bool isTarget) {
1795 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1796
1797 EVT EltVT = VT.getScalarType();
1798
1799 // Do the map lookup using the actual bit pattern for the floating point
1800 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1801 // we don't have issues with SNANs.
1802 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1803 SDVTList VTs = getVTList(EltVT);
1805 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1806 ID.AddPointer(&V);
1807 void *IP = nullptr;
1808 SDNode *N = nullptr;
1809 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1810 if (!VT.isVector())
1811 return SDValue(N, 0);
1812
1813 if (!N) {
1814 N = newSDNode<ConstantFPSDNode>(isTarget, &V, VTs);
1815 CSEMap.InsertNode(N, IP);
1816 InsertNode(N);
1817 }
1818
1819 SDValue Result(N, 0);
1820 if (VT.isVector())
1821 Result = getSplat(VT, DL, Result);
1822 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1823 return Result;
1824}
1825
1827 bool isTarget) {
1828 EVT EltVT = VT.getScalarType();
1829 if (EltVT == MVT::f32)
1830 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1831 if (EltVT == MVT::f64)
1832 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1833 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1834 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1835 bool Ignored;
1836 APFloat APF = APFloat(Val);
1838 &Ignored);
1839 return getConstantFP(APF, DL, VT, isTarget);
1840 }
1841 llvm_unreachable("Unsupported type in getConstantFP");
1842}
1843
1845 EVT VT, int64_t Offset, bool isTargetGA,
1846 unsigned TargetFlags) {
1847 assert((TargetFlags == 0 || isTargetGA) &&
1848 "Cannot set target flags on target-independent globals");
1849
1850 // Truncate (with sign-extension) the offset value to the pointer size.
1852 if (BitWidth < 64)
1854
1855 unsigned Opc;
1856 if (GV->isThreadLocal())
1858 else
1859 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1860
1861 SDVTList VTs = getVTList(VT);
1863 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1864 ID.AddPointer(GV);
1865 ID.AddInteger(Offset);
1866 ID.AddInteger(TargetFlags);
1867 void *IP = nullptr;
1868 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1869 return SDValue(E, 0);
1870
1871 auto *N = newSDNode<GlobalAddressSDNode>(
1872 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1873 CSEMap.InsertNode(N, IP);
1874 InsertNode(N);
1875 return SDValue(N, 0);
1876}
1877
1878SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1879 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1880 SDVTList VTs = getVTList(VT);
1882 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1883 ID.AddInteger(FI);
1884 void *IP = nullptr;
1885 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1886 return SDValue(E, 0);
1887
1888 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1889 CSEMap.InsertNode(N, IP);
1890 InsertNode(N);
1891 return SDValue(N, 0);
1892}
1893
1894SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1895 unsigned TargetFlags) {
1896 assert((TargetFlags == 0 || isTarget) &&
1897 "Cannot set target flags on target-independent jump tables");
1898 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1899 SDVTList VTs = getVTList(VT);
1901 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1902 ID.AddInteger(JTI);
1903 ID.AddInteger(TargetFlags);
1904 void *IP = nullptr;
1905 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1906 return SDValue(E, 0);
1907
1908 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1909 CSEMap.InsertNode(N, IP);
1910 InsertNode(N);
1911 return SDValue(N, 0);
1912}
1913
1915 const SDLoc &DL) {
1917 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1918 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1919}
1920
1922 MaybeAlign Alignment, int Offset,
1923 bool isTarget, unsigned TargetFlags) {
1924 assert((TargetFlags == 0 || isTarget) &&
1925 "Cannot set target flags on target-independent globals");
1926 if (!Alignment)
1927 Alignment = shouldOptForSize()
1928 ? getDataLayout().getABITypeAlign(C->getType())
1929 : getDataLayout().getPrefTypeAlign(C->getType());
1930 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1931 SDVTList VTs = getVTList(VT);
1933 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1934 ID.AddInteger(Alignment->value());
1935 ID.AddInteger(Offset);
1936 ID.AddPointer(C);
1937 ID.AddInteger(TargetFlags);
1938 void *IP = nullptr;
1939 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1940 return SDValue(E, 0);
1941
1942 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1943 TargetFlags);
1944 CSEMap.InsertNode(N, IP);
1945 InsertNode(N);
1946 SDValue V = SDValue(N, 0);
1947 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1948 return V;
1949}
1950
1952 MaybeAlign Alignment, int Offset,
1953 bool isTarget, unsigned TargetFlags) {
1954 assert((TargetFlags == 0 || isTarget) &&
1955 "Cannot set target flags on target-independent globals");
1956 if (!Alignment)
1957 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1958 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1959 SDVTList VTs = getVTList(VT);
1961 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1962 ID.AddInteger(Alignment->value());
1963 ID.AddInteger(Offset);
1964 C->addSelectionDAGCSEId(ID);
1965 ID.AddInteger(TargetFlags);
1966 void *IP = nullptr;
1967 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1968 return SDValue(E, 0);
1969
1970 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1971 TargetFlags);
1972 CSEMap.InsertNode(N, IP);
1973 InsertNode(N);
1974 return SDValue(N, 0);
1975}
1976
1979 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), std::nullopt);
1980 ID.AddPointer(MBB);
1981 void *IP = nullptr;
1982 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1983 return SDValue(E, 0);
1984
1985 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1986 CSEMap.InsertNode(N, IP);
1987 InsertNode(N);
1988 return SDValue(N, 0);
1989}
1990
1992 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1993 ValueTypeNodes.size())
1994 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1995
1996 SDNode *&N = VT.isExtended() ?
1997 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1998
1999 if (N) return SDValue(N, 0);
2000 N = newSDNode<VTSDNode>(VT);
2001 InsertNode(N);
2002 return SDValue(N, 0);
2003}
2004
2006 SDNode *&N = ExternalSymbols[Sym];
2007 if (N) return SDValue(N, 0);
2008 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2009 InsertNode(N);
2010 return SDValue(N, 0);
2011}
2012
2014 SDNode *&N = MCSymbols[Sym];
2015 if (N)
2016 return SDValue(N, 0);
2017 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2018 InsertNode(N);
2019 return SDValue(N, 0);
2020}
2021
2023 unsigned TargetFlags) {
2024 SDNode *&N =
2025 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2026 if (N) return SDValue(N, 0);
2027 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2028 InsertNode(N);
2029 return SDValue(N, 0);
2030}
2031
2033 if ((unsigned)Cond >= CondCodeNodes.size())
2034 CondCodeNodes.resize(Cond+1);
2035
2036 if (!CondCodeNodes[Cond]) {
2037 auto *N = newSDNode<CondCodeSDNode>(Cond);
2038 CondCodeNodes[Cond] = N;
2039 InsertNode(N);
2040 }
2041
2042 return SDValue(CondCodeNodes[Cond], 0);
2043}
2044
2046 bool ConstantFold) {
2047 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2048 "APInt size does not match type size!");
2049
2050 if (MulImm == 0)
2051 return getConstant(0, DL, VT);
2052
2053 if (ConstantFold) {
2054 const MachineFunction &MF = getMachineFunction();
2055 const Function &F = MF.getFunction();
2056 ConstantRange CR = getVScaleRange(&F, 64);
2057 if (const APInt *C = CR.getSingleElement())
2058 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2059 }
2060
2061 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2062}
2063
2065 bool ConstantFold) {
2066 if (EC.isScalable())
2067 return getVScale(DL, VT,
2068 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2069
2070 return getConstant(EC.getKnownMinValue(), DL, VT);
2071}
2072
2074 APInt One(ResVT.getScalarSizeInBits(), 1);
2075 return getStepVector(DL, ResVT, One);
2076}
2077
2079 const APInt &StepVal) {
2080 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2081 if (ResVT.isScalableVector())
2082 return getNode(
2083 ISD::STEP_VECTOR, DL, ResVT,
2084 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2085
2086 SmallVector<SDValue, 16> OpsStepConstants;
2087 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2088 OpsStepConstants.push_back(
2089 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2090 return getBuildVector(ResVT, DL, OpsStepConstants);
2091}
2092
2093/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2094/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2096 std::swap(N1, N2);
2098}
2099
2101 SDValue N2, ArrayRef<int> Mask) {
2102 assert(VT.getVectorNumElements() == Mask.size() &&
2103 "Must have the same number of vector elements as mask elements!");
2104 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2105 "Invalid VECTOR_SHUFFLE");
2106
2107 // Canonicalize shuffle undef, undef -> undef
2108 if (N1.isUndef() && N2.isUndef())
2109 return getUNDEF(VT);
2110
2111 // Validate that all indices in Mask are within the range of the elements
2112 // input to the shuffle.
2113 int NElts = Mask.size();
2114 assert(llvm::all_of(Mask,
2115 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2116 "Index out of range");
2117
2118 // Copy the mask so we can do any needed cleanup.
2119 SmallVector<int, 8> MaskVec(Mask);
2120
2121 // Canonicalize shuffle v, v -> v, undef
2122 if (N1 == N2) {
2123 N2 = getUNDEF(VT);
2124 for (int i = 0; i != NElts; ++i)
2125 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2126 }
2127
2128 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2129 if (N1.isUndef())
2130 commuteShuffle(N1, N2, MaskVec);
2131
2132 if (TLI->hasVectorBlend()) {
2133 // If shuffling a splat, try to blend the splat instead. We do this here so
2134 // that even when this arises during lowering we don't have to re-handle it.
2135 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2136 BitVector UndefElements;
2137 SDValue Splat = BV->getSplatValue(&UndefElements);
2138 if (!Splat)
2139 return;
2140
2141 for (int i = 0; i < NElts; ++i) {
2142 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2143 continue;
2144
2145 // If this input comes from undef, mark it as such.
2146 if (UndefElements[MaskVec[i] - Offset]) {
2147 MaskVec[i] = -1;
2148 continue;
2149 }
2150
2151 // If we can blend a non-undef lane, use that instead.
2152 if (!UndefElements[i])
2153 MaskVec[i] = i + Offset;
2154 }
2155 };
2156 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2157 BlendSplat(N1BV, 0);
2158 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2159 BlendSplat(N2BV, NElts);
2160 }
2161
2162 // Canonicalize all index into lhs, -> shuffle lhs, undef
2163 // Canonicalize all index into rhs, -> shuffle rhs, undef
2164 bool AllLHS = true, AllRHS = true;
2165 bool N2Undef = N2.isUndef();
2166 for (int i = 0; i != NElts; ++i) {
2167 if (MaskVec[i] >= NElts) {
2168 if (N2Undef)
2169 MaskVec[i] = -1;
2170 else
2171 AllLHS = false;
2172 } else if (MaskVec[i] >= 0) {
2173 AllRHS = false;
2174 }
2175 }
2176 if (AllLHS && AllRHS)
2177 return getUNDEF(VT);
2178 if (AllLHS && !N2Undef)
2179 N2 = getUNDEF(VT);
2180 if (AllRHS) {
2181 N1 = getUNDEF(VT);
2182 commuteShuffle(N1, N2, MaskVec);
2183 }
2184 // Reset our undef status after accounting for the mask.
2185 N2Undef = N2.isUndef();
2186 // Re-check whether both sides ended up undef.
2187 if (N1.isUndef() && N2Undef)
2188 return getUNDEF(VT);
2189
2190 // If Identity shuffle return that node.
2191 bool Identity = true, AllSame = true;
2192 for (int i = 0; i != NElts; ++i) {
2193 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2194 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2195 }
2196 if (Identity && NElts)
2197 return N1;
2198
2199 // Shuffling a constant splat doesn't change the result.
2200 if (N2Undef) {
2201 SDValue V = N1;
2202
2203 // Look through any bitcasts. We check that these don't change the number
2204 // (and size) of elements and just changes their types.
2205 while (V.getOpcode() == ISD::BITCAST)
2206 V = V->getOperand(0);
2207
2208 // A splat should always show up as a build vector node.
2209 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2210 BitVector UndefElements;
2211 SDValue Splat = BV->getSplatValue(&UndefElements);
2212 // If this is a splat of an undef, shuffling it is also undef.
2213 if (Splat && Splat.isUndef())
2214 return getUNDEF(VT);
2215
2216 bool SameNumElts =
2217 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2218
2219 // We only have a splat which can skip shuffles if there is a splatted
2220 // value and no undef lanes rearranged by the shuffle.
2221 if (Splat && UndefElements.none()) {
2222 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2223 // number of elements match or the value splatted is a zero constant.
2224 if (SameNumElts || isNullConstant(Splat))
2225 return N1;
2226 }
2227
2228 // If the shuffle itself creates a splat, build the vector directly.
2229 if (AllSame && SameNumElts) {
2230 EVT BuildVT = BV->getValueType(0);
2231 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2232 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2233
2234 // We may have jumped through bitcasts, so the type of the
2235 // BUILD_VECTOR may not match the type of the shuffle.
2236 if (BuildVT != VT)
2237 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2238 return NewBV;
2239 }
2240 }
2241 }
2242
2243 SDVTList VTs = getVTList(VT);
2245 SDValue Ops[2] = { N1, N2 };
2247 for (int i = 0; i != NElts; ++i)
2248 ID.AddInteger(MaskVec[i]);
2249
2250 void* IP = nullptr;
2251 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2252 return SDValue(E, 0);
2253
2254 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2255 // SDNode doesn't have access to it. This memory will be "leaked" when
2256 // the node is deallocated, but recovered when the NodeAllocator is released.
2257 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2258 llvm::copy(MaskVec, MaskAlloc);
2259
2260 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2261 dl.getDebugLoc(), MaskAlloc);
2262 createOperands(N, Ops);
2263
2264 CSEMap.InsertNode(N, IP);
2265 InsertNode(N);
2266 SDValue V = SDValue(N, 0);
2267 NewSDValueDbgMsg(V, "Creating new node: ", this);
2268 return V;
2269}
2270
2272 EVT VT = SV.getValueType(0);
2273 SmallVector<int, 8> MaskVec(SV.getMask());
2275
2276 SDValue Op0 = SV.getOperand(0);
2277 SDValue Op1 = SV.getOperand(1);
2278 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2279}
2280
2282 SDVTList VTs = getVTList(VT);
2284 AddNodeIDNode(ID, ISD::Register, VTs, std::nullopt);
2285 ID.AddInteger(RegNo);
2286 void *IP = nullptr;
2287 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2288 return SDValue(E, 0);
2289
2290 auto *N = newSDNode<RegisterSDNode>(RegNo, VTs);
2291 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2292 CSEMap.InsertNode(N, IP);
2293 InsertNode(N);
2294 return SDValue(N, 0);
2295}
2296
2299 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), std::nullopt);
2300 ID.AddPointer(RegMask);
2301 void *IP = nullptr;
2302 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2303 return SDValue(E, 0);
2304
2305 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2306 CSEMap.InsertNode(N, IP);
2307 InsertNode(N);
2308 return SDValue(N, 0);
2309}
2310
2312 MCSymbol *Label) {
2313 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2314}
2315
2316SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2317 SDValue Root, MCSymbol *Label) {
2319 SDValue Ops[] = { Root };
2320 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2321 ID.AddPointer(Label);
2322 void *IP = nullptr;
2323 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2324 return SDValue(E, 0);
2325
2326 auto *N =
2327 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2328 createOperands(N, Ops);
2329
2330 CSEMap.InsertNode(N, IP);
2331 InsertNode(N);
2332 return SDValue(N, 0);
2333}
2334
2336 int64_t Offset, bool isTarget,
2337 unsigned TargetFlags) {
2338 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2339 SDVTList VTs = getVTList(VT);
2340
2342 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
2343 ID.AddPointer(BA);
2344 ID.AddInteger(Offset);
2345 ID.AddInteger(TargetFlags);
2346 void *IP = nullptr;
2347 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2348 return SDValue(E, 0);
2349
2350 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2351 CSEMap.InsertNode(N, IP);
2352 InsertNode(N);
2353 return SDValue(N, 0);
2354}
2355
2358 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), std::nullopt);
2359 ID.AddPointer(V);
2360
2361 void *IP = nullptr;
2362 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2363 return SDValue(E, 0);
2364
2365 auto *N = newSDNode<SrcValueSDNode>(V);
2366 CSEMap.InsertNode(N, IP);
2367 InsertNode(N);
2368 return SDValue(N, 0);
2369}
2370
2373 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), std::nullopt);
2374 ID.AddPointer(MD);
2375
2376 void *IP = nullptr;
2377 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2378 return SDValue(E, 0);
2379
2380 auto *N = newSDNode<MDNodeSDNode>(MD);
2381 CSEMap.InsertNode(N, IP);
2382 InsertNode(N);
2383 return SDValue(N, 0);
2384}
2385
2387 if (VT == V.getValueType())
2388 return V;
2389
2390 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2391}
2392
2394 unsigned SrcAS, unsigned DestAS) {
2395 SDVTList VTs = getVTList(VT);
2396 SDValue Ops[] = {Ptr};
2399 ID.AddInteger(SrcAS);
2400 ID.AddInteger(DestAS);
2401
2402 void *IP = nullptr;
2403 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2404 return SDValue(E, 0);
2405
2406 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2407 VTs, SrcAS, DestAS);
2408 createOperands(N, Ops);
2409
2410 CSEMap.InsertNode(N, IP);
2411 InsertNode(N);
2412 return SDValue(N, 0);
2413}
2414
2416 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2417}
2418
2419/// getShiftAmountOperand - Return the specified value casted to
2420/// the target's desired shift amount type.
2422 EVT OpTy = Op.getValueType();
2423 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2424 if (OpTy == ShTy || OpTy.isVector()) return Op;
2425
2426 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2427}
2428
2430 SDLoc dl(Node);
2432 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2433 EVT VT = Node->getValueType(0);
2434 SDValue Tmp1 = Node->getOperand(0);
2435 SDValue Tmp2 = Node->getOperand(1);
2436 const MaybeAlign MA(Node->getConstantOperandVal(3));
2437
2438 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2439 Tmp2, MachinePointerInfo(V));
2440 SDValue VAList = VAListLoad;
2441
2442 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2443 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2444 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2445
2446 VAList = getNode(
2447 ISD::AND, dl, VAList.getValueType(), VAList,
2448 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2449 }
2450
2451 // Increment the pointer, VAList, to the next vaarg
2452 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2453 getConstant(getDataLayout().getTypeAllocSize(
2454 VT.getTypeForEVT(*getContext())),
2455 dl, VAList.getValueType()));
2456 // Store the incremented VAList to the legalized pointer
2457 Tmp1 =
2458 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2459 // Load the actual argument out of the pointer VAList
2460 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2461}
2462
2464 SDLoc dl(Node);
2466 // This defaults to loading a pointer from the input and storing it to the
2467 // output, returning the chain.
2468 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2469 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2470 SDValue Tmp1 =
2471 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2472 Node->getOperand(2), MachinePointerInfo(VS));
2473 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2474 MachinePointerInfo(VD));
2475}
2476
2478 const DataLayout &DL = getDataLayout();
2479 Type *Ty = VT.getTypeForEVT(*getContext());
2480 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2481
2482 if (TLI->isTypeLegal(VT) || !VT.isVector())
2483 return RedAlign;
2484
2486 const Align StackAlign = TFI->getStackAlign();
2487
2488 // See if we can choose a smaller ABI alignment in cases where it's an
2489 // illegal vector type that will get broken down.
2490 if (RedAlign > StackAlign) {
2491 EVT IntermediateVT;
2492 MVT RegisterVT;
2493 unsigned NumIntermediates;
2494 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2495 NumIntermediates, RegisterVT);
2496 Ty = IntermediateVT.getTypeForEVT(*getContext());
2497 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2498 if (RedAlign2 < RedAlign)
2499 RedAlign = RedAlign2;
2500
2501 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2502 // If the stack is not realignable, the alignment should be limited to the
2503 // StackAlignment
2504 RedAlign = std::min(RedAlign, StackAlign);
2505 }
2506
2507 return RedAlign;
2508}
2509
2511 MachineFrameInfo &MFI = MF->getFrameInfo();
2513 int StackID = 0;
2514 if (Bytes.isScalable())
2515 StackID = TFI->getStackIDForScalableVectors();
2516 // The stack id gives an indication of whether the object is scalable or
2517 // not, so it's safe to pass in the minimum size here.
2518 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2519 false, nullptr, StackID);
2520 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2521}
2522
2524 Type *Ty = VT.getTypeForEVT(*getContext());
2525 Align StackAlign =
2526 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2527 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2528}
2529
2531 TypeSize VT1Size = VT1.getStoreSize();
2532 TypeSize VT2Size = VT2.getStoreSize();
2533 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2534 "Don't know how to choose the maximum size when creating a stack "
2535 "temporary");
2536 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2537 ? VT1Size
2538 : VT2Size;
2539
2540 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2541 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2542 const DataLayout &DL = getDataLayout();
2543 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2544 return CreateStackTemporary(Bytes, Align);
2545}
2546
2548 ISD::CondCode Cond, const SDLoc &dl) {
2549 EVT OpVT = N1.getValueType();
2550
2551 auto GetUndefBooleanConstant = [&]() {
2552 if (VT.getScalarType() == MVT::i1 ||
2553 TLI->getBooleanContents(OpVT) ==
2555 return getUNDEF(VT);
2556 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2557 // so we cannot use getUNDEF(). Return zero instead.
2558 return getConstant(0, dl, VT);
2559 };
2560
2561 // These setcc operations always fold.
2562 switch (Cond) {
2563 default: break;
2564 case ISD::SETFALSE:
2565 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2566 case ISD::SETTRUE:
2567 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2568
2569 case ISD::SETOEQ:
2570 case ISD::SETOGT:
2571 case ISD::SETOGE:
2572 case ISD::SETOLT:
2573 case ISD::SETOLE:
2574 case ISD::SETONE:
2575 case ISD::SETO:
2576 case ISD::SETUO:
2577 case ISD::SETUEQ:
2578 case ISD::SETUNE:
2579 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2580 break;
2581 }
2582
2583 if (OpVT.isInteger()) {
2584 // For EQ and NE, we can always pick a value for the undef to make the
2585 // predicate pass or fail, so we can return undef.
2586 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2587 // icmp eq/ne X, undef -> undef.
2588 if ((N1.isUndef() || N2.isUndef()) &&
2589 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2590 return GetUndefBooleanConstant();
2591
2592 // If both operands are undef, we can return undef for int comparison.
2593 // icmp undef, undef -> undef.
2594 if (N1.isUndef() && N2.isUndef())
2595 return GetUndefBooleanConstant();
2596
2597 // icmp X, X -> true/false
2598 // icmp X, undef -> true/false because undef could be X.
2599 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2600 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2601 }
2602
2603 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2604 const APInt &C2 = N2C->getAPIntValue();
2605 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2606 const APInt &C1 = N1C->getAPIntValue();
2607
2609 dl, VT, OpVT);
2610 }
2611 }
2612
2613 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2614 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2615
2616 if (N1CFP && N2CFP) {
2617 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2618 switch (Cond) {
2619 default: break;
2620 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2621 return GetUndefBooleanConstant();
2622 [[fallthrough]];
2623 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2624 OpVT);
2625 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2626 return GetUndefBooleanConstant();
2627 [[fallthrough]];
2629 R==APFloat::cmpLessThan, dl, VT,
2630 OpVT);
2631 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2632 return GetUndefBooleanConstant();
2633 [[fallthrough]];
2634 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2635 OpVT);
2636 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2637 return GetUndefBooleanConstant();
2638 [[fallthrough]];
2640 VT, OpVT);
2641 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2642 return GetUndefBooleanConstant();
2643 [[fallthrough]];
2645 R==APFloat::cmpEqual, dl, VT,
2646 OpVT);
2647 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2648 return GetUndefBooleanConstant();
2649 [[fallthrough]];
2651 R==APFloat::cmpEqual, dl, VT, OpVT);
2652 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2653 OpVT);
2654 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2655 OpVT);
2657 R==APFloat::cmpEqual, dl, VT,
2658 OpVT);
2659 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2660 OpVT);
2662 R==APFloat::cmpLessThan, dl, VT,
2663 OpVT);
2665 R==APFloat::cmpUnordered, dl, VT,
2666 OpVT);
2668 VT, OpVT);
2669 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2670 OpVT);
2671 }
2672 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2673 // Ensure that the constant occurs on the RHS.
2675 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2676 return SDValue();
2677 return getSetCC(dl, VT, N2, N1, SwappedCond);
2678 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2679 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2680 // If an operand is known to be a nan (or undef that could be a nan), we can
2681 // fold it.
2682 // Choosing NaN for the undef will always make unordered comparison succeed
2683 // and ordered comparison fails.
2684 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2685 switch (ISD::getUnorderedFlavor(Cond)) {
2686 default:
2687 llvm_unreachable("Unknown flavor!");
2688 case 0: // Known false.
2689 return getBoolConstant(false, dl, VT, OpVT);
2690 case 1: // Known true.
2691 return getBoolConstant(true, dl, VT, OpVT);
2692 case 2: // Undefined.
2693 return GetUndefBooleanConstant();
2694 }
2695 }
2696
2697 // Could not fold it.
2698 return SDValue();
2699}
2700
2701/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2702/// use this predicate to simplify operations downstream.
2704 unsigned BitWidth = Op.getScalarValueSizeInBits();
2706}
2707
2708/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2709/// this predicate to simplify operations downstream. Mask is known to be zero
2710/// for bits that V cannot have.
2712 unsigned Depth) const {
2713 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2714}
2715
2716/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2717/// DemandedElts. We use this predicate to simplify operations downstream.
2718/// Mask is known to be zero for bits that V cannot have.
2720 const APInt &DemandedElts,
2721 unsigned Depth) const {
2722 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2723}
2724
2725/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2726/// DemandedElts. We use this predicate to simplify operations downstream.
2728 unsigned Depth /* = 0 */) const {
2729 return computeKnownBits(V, DemandedElts, Depth).isZero();
2730}
2731
2732/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2734 unsigned Depth) const {
2735 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2736}
2737
2739 const APInt &DemandedElts,
2740 unsigned Depth) const {
2741 EVT VT = Op.getValueType();
2742 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2743
2744 unsigned NumElts = VT.getVectorNumElements();
2745 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2746
2747 APInt KnownZeroElements = APInt::getZero(NumElts);
2748 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2749 if (!DemandedElts[EltIdx])
2750 continue; // Don't query elements that are not demanded.
2751 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2752 if (MaskedVectorIsZero(Op, Mask, Depth))
2753 KnownZeroElements.setBit(EltIdx);
2754 }
2755 return KnownZeroElements;
2756}
2757
2758/// isSplatValue - Return true if the vector V has the same value
2759/// across all DemandedElts. For scalable vectors, we don't know the
2760/// number of lanes at compile time. Instead, we use a 1 bit APInt
2761/// to represent a conservative value for all lanes; that is, that
2762/// one bit value is implicitly splatted across all lanes.
2763bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2764 APInt &UndefElts, unsigned Depth) const {
2765 unsigned Opcode = V.getOpcode();
2766 EVT VT = V.getValueType();
2767 assert(VT.isVector() && "Vector type expected");
2768 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2769 "scalable demanded bits are ignored");
2770
2771 if (!DemandedElts)
2772 return false; // No demanded elts, better to assume we don't know anything.
2773
2774 if (Depth >= MaxRecursionDepth)
2775 return false; // Limit search depth.
2776
2777 // Deal with some common cases here that work for both fixed and scalable
2778 // vector types.
2779 switch (Opcode) {
2780 case ISD::SPLAT_VECTOR:
2781 UndefElts = V.getOperand(0).isUndef()
2782 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2783 : APInt(DemandedElts.getBitWidth(), 0);
2784 return true;
2785 case ISD::ADD:
2786 case ISD::SUB:
2787 case ISD::AND:
2788 case ISD::XOR:
2789 case ISD::OR: {
2790 APInt UndefLHS, UndefRHS;
2791 SDValue LHS = V.getOperand(0);
2792 SDValue RHS = V.getOperand(1);
2793 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2794 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2795 UndefElts = UndefLHS | UndefRHS;
2796 return true;
2797 }
2798 return false;
2799 }
2800 case ISD::ABS:
2801 case ISD::TRUNCATE:
2802 case ISD::SIGN_EXTEND:
2803 case ISD::ZERO_EXTEND:
2804 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2805 default:
2806 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2807 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2808 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2809 Depth);
2810 break;
2811}
2812
2813 // We don't support other cases than those above for scalable vectors at
2814 // the moment.
2815 if (VT.isScalableVector())
2816 return false;
2817
2818 unsigned NumElts = VT.getVectorNumElements();
2819 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2820 UndefElts = APInt::getZero(NumElts);
2821
2822 switch (Opcode) {
2823 case ISD::BUILD_VECTOR: {
2824 SDValue Scl;
2825 for (unsigned i = 0; i != NumElts; ++i) {
2826 SDValue Op = V.getOperand(i);
2827 if (Op.isUndef()) {
2828 UndefElts.setBit(i);
2829 continue;
2830 }
2831 if (!DemandedElts[i])
2832 continue;
2833 if (Scl && Scl != Op)
2834 return false;
2835 Scl = Op;
2836 }
2837 return true;
2838 }
2839 case ISD::VECTOR_SHUFFLE: {
2840 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2841 APInt DemandedLHS = APInt::getZero(NumElts);
2842 APInt DemandedRHS = APInt::getZero(NumElts);
2843 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2844 for (int i = 0; i != (int)NumElts; ++i) {
2845 int M = Mask[i];
2846 if (M < 0) {
2847 UndefElts.setBit(i);
2848 continue;
2849 }
2850 if (!DemandedElts[i])
2851 continue;
2852 if (M < (int)NumElts)
2853 DemandedLHS.setBit(M);
2854 else
2855 DemandedRHS.setBit(M - NumElts);
2856 }
2857
2858 // If we aren't demanding either op, assume there's no splat.
2859 // If we are demanding both ops, assume there's no splat.
2860 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2861 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2862 return false;
2863
2864 // See if the demanded elts of the source op is a splat or we only demand
2865 // one element, which should always be a splat.
2866 // TODO: Handle source ops splats with undefs.
2867 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2868 APInt SrcUndefs;
2869 return (SrcElts.popcount() == 1) ||
2870 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2871 (SrcElts & SrcUndefs).isZero());
2872 };
2873 if (!DemandedLHS.isZero())
2874 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2875 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2876 }
2878 // Offset the demanded elts by the subvector index.
2879 SDValue Src = V.getOperand(0);
2880 // We don't support scalable vectors at the moment.
2881 if (Src.getValueType().isScalableVector())
2882 return false;
2883 uint64_t Idx = V.getConstantOperandVal(1);
2884 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2885 APInt UndefSrcElts;
2886 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2887 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2888 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2889 return true;
2890 }
2891 break;
2892 }
2896 // Widen the demanded elts by the src element count.
2897 SDValue Src = V.getOperand(0);
2898 // We don't support scalable vectors at the moment.
2899 if (Src.getValueType().isScalableVector())
2900 return false;
2901 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2902 APInt UndefSrcElts;
2903 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2904 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2905 UndefElts = UndefSrcElts.trunc(NumElts);
2906 return true;
2907 }
2908 break;
2909 }
2910 case ISD::BITCAST: {
2911 SDValue Src = V.getOperand(0);
2912 EVT SrcVT = Src.getValueType();
2913 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2914 unsigned BitWidth = VT.getScalarSizeInBits();
2915
2916 // Ignore bitcasts from unsupported types.
2917 // TODO: Add fp support?
2918 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2919 break;
2920
2921 // Bitcast 'small element' vector to 'large element' vector.
2922 if ((BitWidth % SrcBitWidth) == 0) {
2923 // See if each sub element is a splat.
2924 unsigned Scale = BitWidth / SrcBitWidth;
2925 unsigned NumSrcElts = SrcVT.getVectorNumElements();
2926 APInt ScaledDemandedElts =
2927 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
2928 for (unsigned I = 0; I != Scale; ++I) {
2929 APInt SubUndefElts;
2930 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
2931 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
2932 SubDemandedElts &= ScaledDemandedElts;
2933 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
2934 return false;
2935 // TODO: Add support for merging sub undef elements.
2936 if (!SubUndefElts.isZero())
2937 return false;
2938 }
2939 return true;
2940 }
2941 break;
2942 }
2943 }
2944
2945 return false;
2946}
2947
2948/// Helper wrapper to main isSplatValue function.
2949bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
2950 EVT VT = V.getValueType();
2951 assert(VT.isVector() && "Vector type expected");
2952
2953 APInt UndefElts;
2954 // Since the number of lanes in a scalable vector is unknown at compile time,
2955 // we track one bit which is implicitly broadcast to all lanes. This means
2956 // that all lanes in a scalable vector are considered demanded.
2957 APInt DemandedElts
2959 return isSplatValue(V, DemandedElts, UndefElts) &&
2960 (AllowUndefs || !UndefElts);
2961}
2962
2965
2966 EVT VT = V.getValueType();
2967 unsigned Opcode = V.getOpcode();
2968 switch (Opcode) {
2969 default: {
2970 APInt UndefElts;
2971 // Since the number of lanes in a scalable vector is unknown at compile time,
2972 // we track one bit which is implicitly broadcast to all lanes. This means
2973 // that all lanes in a scalable vector are considered demanded.
2974 APInt DemandedElts
2976
2977 if (isSplatValue(V, DemandedElts, UndefElts)) {
2978 if (VT.isScalableVector()) {
2979 // DemandedElts and UndefElts are ignored for scalable vectors, since
2980 // the only supported cases are SPLAT_VECTOR nodes.
2981 SplatIdx = 0;
2982 } else {
2983 // Handle case where all demanded elements are UNDEF.
2984 if (DemandedElts.isSubsetOf(UndefElts)) {
2985 SplatIdx = 0;
2986 return getUNDEF(VT);
2987 }
2988 SplatIdx = (UndefElts & DemandedElts).countr_one();
2989 }
2990 return V;
2991 }
2992 break;
2993 }
2994 case ISD::SPLAT_VECTOR:
2995 SplatIdx = 0;
2996 return V;
2997 case ISD::VECTOR_SHUFFLE: {
2998 assert(!VT.isScalableVector());
2999 // Check if this is a shuffle node doing a splat.
3000 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3001 // getTargetVShiftNode currently struggles without the splat source.
3002 auto *SVN = cast<ShuffleVectorSDNode>(V);
3003 if (!SVN->isSplat())
3004 break;
3005 int Idx = SVN->getSplatIndex();
3006 int NumElts = V.getValueType().getVectorNumElements();
3007 SplatIdx = Idx % NumElts;
3008 return V.getOperand(Idx / NumElts);
3009 }
3010 }
3011
3012 return SDValue();
3013}
3014
3016 int SplatIdx;
3017 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3018 EVT SVT = SrcVector.getValueType().getScalarType();
3019 EVT LegalSVT = SVT;
3020 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3021 if (!SVT.isInteger())
3022 return SDValue();
3023 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3024 if (LegalSVT.bitsLT(SVT))
3025 return SDValue();
3026 }
3027 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
3028 getVectorIdxConstant(SplatIdx, SDLoc(V)));
3029 }
3030 return SDValue();
3031}
3032
3033std::optional<ConstantRange>
3035 unsigned Depth) const {
3036 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3037 V.getOpcode() == ISD::SRA) &&
3038 "Unknown shift node");
3039 // Shifting more than the bitwidth is not valid.
3040 unsigned BitWidth = V.getScalarValueSizeInBits();
3041
3042 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3043 const APInt &ShAmt = Cst->getAPIntValue();
3044 if (ShAmt.uge(BitWidth))
3045 return std::nullopt;
3046 return ConstantRange(ShAmt);
3047 }
3048
3049 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3050 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3051 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3052 if (!DemandedElts[i])
3053 continue;
3054 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3055 if (!SA) {
3056 MinAmt = MaxAmt = nullptr;
3057 break;
3058 }
3059 const APInt &ShAmt = SA->getAPIntValue();
3060 if (ShAmt.uge(BitWidth))
3061 return std::nullopt;
3062 if (!MinAmt || MinAmt->ugt(ShAmt))
3063 MinAmt = &ShAmt;
3064 if (!MaxAmt || MaxAmt->ult(ShAmt))
3065 MaxAmt = &ShAmt;
3066 }
3067 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3068 "Failed to find matching min/max shift amounts");
3069 if (MinAmt && MaxAmt)
3070 return ConstantRange(*MinAmt, *MaxAmt + 1);
3071 }
3072
3073 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3074 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3075 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3076 if (KnownAmt.getMaxValue().ult(BitWidth))
3077 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3078
3079 return std::nullopt;
3080}
3081
3082std::optional<uint64_t>
3084 unsigned Depth) const {
3085 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3086 V.getOpcode() == ISD::SRA) &&
3087 "Unknown shift node");
3088 if (std::optional<ConstantRange> AmtRange =
3089 getValidShiftAmountRange(V, DemandedElts, Depth))
3090 if (const APInt *ShAmt = AmtRange->getSingleElement())
3091 return ShAmt->getZExtValue();
3092 return std::nullopt;
3093}
3094
3095std::optional<uint64_t>
3097 EVT VT = V.getValueType();
3098 APInt DemandedElts = VT.isFixedLengthVector()
3100 : APInt(1, 1);
3101 return getValidShiftAmount(V, DemandedElts, Depth);
3102}
3103
3104std::optional<uint64_t>
3106 unsigned Depth) const {
3107 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3108 V.getOpcode() == ISD::SRA) &&
3109 "Unknown shift node");
3110 if (std::optional<ConstantRange> AmtRange =
3111 getValidShiftAmountRange(V, DemandedElts, Depth))
3112 return AmtRange->getUnsignedMin().getZExtValue();
3113 return std::nullopt;
3114}
3115
3116std::optional<uint64_t>
3118 EVT VT = V.getValueType();
3119 APInt DemandedElts = VT.isFixedLengthVector()
3121 : APInt(1, 1);
3122 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3123}
3124
3125std::optional<uint64_t>
3127 unsigned Depth) const {
3128 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3129 V.getOpcode() == ISD::SRA) &&
3130 "Unknown shift node");
3131 if (std::optional<ConstantRange> AmtRange =
3132 getValidShiftAmountRange(V, DemandedElts, Depth))
3133 return AmtRange->getUnsignedMax().getZExtValue();
3134 return std::nullopt;
3135}
3136
3137std::optional<uint64_t>
3139 EVT VT = V.getValueType();
3140 APInt DemandedElts = VT.isFixedLengthVector()
3142 : APInt(1, 1);
3143 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3144}
3145
3146/// Determine which bits of Op are known to be either zero or one and return
3147/// them in Known. For vectors, the known bits are those that are shared by
3148/// every vector element.
3150 EVT VT = Op.getValueType();
3151
3152 // Since the number of lanes in a scalable vector is unknown at compile time,
3153 // we track one bit which is implicitly broadcast to all lanes. This means
3154 // that all lanes in a scalable vector are considered demanded.
3155 APInt DemandedElts = VT.isFixedLengthVector()
3157 : APInt(1, 1);
3158 return computeKnownBits(Op, DemandedElts, Depth);
3159}
3160
3161/// Determine which bits of Op are known to be either zero or one and return
3162/// them in Known. The DemandedElts argument allows us to only collect the known
3163/// bits that are shared by the requested vector elements.
3165 unsigned Depth) const {
3166 unsigned BitWidth = Op.getScalarValueSizeInBits();
3167
3168 KnownBits Known(BitWidth); // Don't know anything.
3169
3170 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3171 // We know all of the bits for a constant!
3172 return KnownBits::makeConstant(C->getAPIntValue());
3173 }
3174 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3175 // We know all of the bits for a constant fp!
3176 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3177 }
3178
3179 if (Depth >= MaxRecursionDepth)
3180 return Known; // Limit search depth.
3181
3182 KnownBits Known2;
3183 unsigned NumElts = DemandedElts.getBitWidth();
3184 assert((!Op.getValueType().isFixedLengthVector() ||
3185 NumElts == Op.getValueType().getVectorNumElements()) &&
3186 "Unexpected vector size");
3187
3188 if (!DemandedElts)
3189 return Known; // No demanded elts, better to assume we don't know anything.
3190
3191 unsigned Opcode = Op.getOpcode();
3192 switch (Opcode) {
3193 case ISD::MERGE_VALUES:
3194 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3195 Depth + 1);
3196 case ISD::SPLAT_VECTOR: {
3197 SDValue SrcOp = Op.getOperand(0);
3198 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3199 "Expected SPLAT_VECTOR implicit truncation");
3200 // Implicitly truncate the bits to match the official semantics of
3201 // SPLAT_VECTOR.
3202 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3203 break;
3204 }
3206 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3207 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3208 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3209 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3210 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3211 }
3212 break;
3213 }
3214 case ISD::STEP_VECTOR: {
3215 const APInt &Step = Op.getConstantOperandAPInt(0);
3216
3217 if (Step.isPowerOf2())
3218 Known.Zero.setLowBits(Step.logBase2());
3219
3221
3222 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3223 break;
3224 const APInt MinNumElts =
3225 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3226
3227 bool Overflow;
3228 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3230 .umul_ov(MinNumElts, Overflow);
3231 if (Overflow)
3232 break;
3233
3234 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3235 if (Overflow)
3236 break;
3237
3238 Known.Zero.setHighBits(MaxValue.countl_zero());
3239 break;
3240 }
3241 case ISD::BUILD_VECTOR:
3242 assert(!Op.getValueType().isScalableVector());
3243 // Collect the known bits that are shared by every demanded vector element.
3244 Known.Zero.setAllBits(); Known.One.setAllBits();
3245 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3246 if (!DemandedElts[i])
3247 continue;
3248
3249 SDValue SrcOp = Op.getOperand(i);
3250 Known2 = computeKnownBits(SrcOp, Depth + 1);
3251
3252 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3253 if (SrcOp.getValueSizeInBits() != BitWidth) {
3254 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3255 "Expected BUILD_VECTOR implicit truncation");
3256 Known2 = Known2.trunc(BitWidth);
3257 }
3258
3259 // Known bits are the values that are shared by every demanded element.
3260 Known = Known.intersectWith(Known2);
3261
3262 // If we don't know any bits, early out.
3263 if (Known.isUnknown())
3264 break;
3265 }
3266 break;
3267 case ISD::VECTOR_SHUFFLE: {
3268 assert(!Op.getValueType().isScalableVector());
3269 // Collect the known bits that are shared by every vector element referenced
3270 // by the shuffle.
3271 APInt DemandedLHS, DemandedRHS;
3272 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3273 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3274 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3275 DemandedLHS, DemandedRHS))
3276 break;
3277
3278 // Known bits are the values that are shared by every demanded element.
3279 Known.Zero.setAllBits(); Known.One.setAllBits();
3280 if (!!DemandedLHS) {
3281 SDValue LHS = Op.getOperand(0);
3282 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3283 Known = Known.intersectWith(Known2);
3284 }
3285 // If we don't know any bits, early out.
3286 if (Known.isUnknown())
3287 break;
3288 if (!!DemandedRHS) {
3289 SDValue RHS = Op.getOperand(1);
3290 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3291 Known = Known.intersectWith(Known2);
3292 }
3293 break;
3294 }
3295 case ISD::VSCALE: {
3297 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3298 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3299 break;
3300 }
3301 case ISD::CONCAT_VECTORS: {
3302 if (Op.getValueType().isScalableVector())
3303 break;
3304 // Split DemandedElts and test each of the demanded subvectors.
3305 Known.Zero.setAllBits(); Known.One.setAllBits();
3306 EVT SubVectorVT = Op.getOperand(0).getValueType();
3307 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3308 unsigned NumSubVectors = Op.getNumOperands();
3309 for (unsigned i = 0; i != NumSubVectors; ++i) {
3310 APInt DemandedSub =
3311 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3312 if (!!DemandedSub) {
3313 SDValue Sub = Op.getOperand(i);
3314 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3315 Known = Known.intersectWith(Known2);
3316 }
3317 // If we don't know any bits, early out.
3318 if (Known.isUnknown())
3319 break;
3320 }
3321 break;
3322 }
3323 case ISD::INSERT_SUBVECTOR: {
3324 if (Op.getValueType().isScalableVector())
3325 break;
3326 // Demand any elements from the subvector and the remainder from the src its
3327 // inserted into.
3328 SDValue Src = Op.getOperand(0);
3329 SDValue Sub = Op.getOperand(1);
3330 uint64_t Idx = Op.getConstantOperandVal(2);
3331 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3332 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3333 APInt DemandedSrcElts = DemandedElts;
3334 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3335
3336 Known.One.setAllBits();
3337 Known.Zero.setAllBits();
3338 if (!!DemandedSubElts) {
3339 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3340 if (Known.isUnknown())
3341 break; // early-out.
3342 }
3343 if (!!DemandedSrcElts) {
3344 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3345 Known = Known.intersectWith(Known2);
3346 }
3347 break;
3348 }
3350 // Offset the demanded elts by the subvector index.
3351 SDValue Src = Op.getOperand(0);
3352 // Bail until we can represent demanded elements for scalable vectors.
3353 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3354 break;
3355 uint64_t Idx = Op.getConstantOperandVal(1);
3356 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3357 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3358 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3359 break;
3360 }
3361 case ISD::SCALAR_TO_VECTOR: {
3362 if (Op.getValueType().isScalableVector())
3363 break;
3364 // We know about scalar_to_vector as much as we know about it source,
3365 // which becomes the first element of otherwise unknown vector.
3366 if (DemandedElts != 1)
3367 break;
3368
3369 SDValue N0 = Op.getOperand(0);
3370 Known = computeKnownBits(N0, Depth + 1);
3371 if (N0.getValueSizeInBits() != BitWidth)
3372 Known = Known.trunc(BitWidth);
3373
3374 break;
3375 }
3376 case ISD::BITCAST: {
3377 if (Op.getValueType().isScalableVector())
3378 break;
3379
3380 SDValue N0 = Op.getOperand(0);
3381 EVT SubVT = N0.getValueType();
3382 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3383
3384 // Ignore bitcasts from unsupported types.
3385 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3386 break;
3387
3388 // Fast handling of 'identity' bitcasts.
3389 if (BitWidth == SubBitWidth) {
3390 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3391 break;
3392 }
3393
3394 bool IsLE = getDataLayout().isLittleEndian();
3395
3396 // Bitcast 'small element' vector to 'large element' scalar/vector.
3397 if ((BitWidth % SubBitWidth) == 0) {
3398 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3399
3400 // Collect known bits for the (larger) output by collecting the known
3401 // bits from each set of sub elements and shift these into place.
3402 // We need to separately call computeKnownBits for each set of
3403 // sub elements as the knownbits for each is likely to be different.
3404 unsigned SubScale = BitWidth / SubBitWidth;
3405 APInt SubDemandedElts(NumElts * SubScale, 0);
3406 for (unsigned i = 0; i != NumElts; ++i)
3407 if (DemandedElts[i])
3408 SubDemandedElts.setBit(i * SubScale);
3409
3410 for (unsigned i = 0; i != SubScale; ++i) {
3411 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3412 Depth + 1);
3413 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3414 Known.insertBits(Known2, SubBitWidth * Shifts);
3415 }
3416 }
3417
3418 // Bitcast 'large element' scalar/vector to 'small element' vector.
3419 if ((SubBitWidth % BitWidth) == 0) {
3420 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3421
3422 // Collect known bits for the (smaller) output by collecting the known
3423 // bits from the overlapping larger input elements and extracting the
3424 // sub sections we actually care about.
3425 unsigned SubScale = SubBitWidth / BitWidth;
3426 APInt SubDemandedElts =
3427 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3428 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3429
3430 Known.Zero.setAllBits(); Known.One.setAllBits();
3431 for (unsigned i = 0; i != NumElts; ++i)
3432 if (DemandedElts[i]) {
3433 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3434 unsigned Offset = (Shifts % SubScale) * BitWidth;
3435 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3436 // If we don't know any bits, early out.
3437 if (Known.isUnknown())
3438 break;
3439 }
3440 }
3441 break;
3442 }
3443 case ISD::AND:
3444 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3445 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3446
3447 Known &= Known2;
3448 break;
3449 case ISD::OR:
3450 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3451 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3452
3453 Known |= Known2;
3454 break;
3455 case ISD::XOR:
3456 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3457 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3458
3459 Known ^= Known2;
3460 break;
3461 case ISD::MUL: {
3462 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3463 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3464 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3465 // TODO: SelfMultiply can be poison, but not undef.
3466 if (SelfMultiply)
3467 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3468 Op.getOperand(0), DemandedElts, false, Depth + 1);
3469 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3470
3471 // If the multiplication is known not to overflow, the product of a number
3472 // with itself is non-negative. Only do this if we didn't already computed
3473 // the opposite value for the sign bit.
3474 if (Op->getFlags().hasNoSignedWrap() &&
3475 Op.getOperand(0) == Op.getOperand(1) &&
3476 !Known.isNegative())
3477 Known.makeNonNegative();
3478 break;
3479 }
3480 case ISD::MULHU: {
3481 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3482 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3483 Known = KnownBits::mulhu(Known, Known2);
3484 break;
3485 }
3486 case ISD::MULHS: {
3487 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3488 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3489 Known = KnownBits::mulhs(Known, Known2);
3490 break;
3491 }
3492 case ISD::ABDU: {
3493 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3494 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3495 Known = KnownBits::abdu(Known, Known2);
3496 break;
3497 }
3498 case ISD::ABDS: {
3499 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3500 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3501 Known = KnownBits::abds(Known, Known2);
3502 unsigned SignBits1 =
3503 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3504 if (SignBits1 == 1)
3505 break;
3506 unsigned SignBits0 =
3507 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3508 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3509 break;
3510 }
3511 case ISD::UMUL_LOHI: {
3512 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3513 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3514 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3515 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3516 if (Op.getResNo() == 0)
3517 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3518 else
3519 Known = KnownBits::mulhu(Known, Known2);
3520 break;
3521 }
3522 case ISD::SMUL_LOHI: {
3523 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3524 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3525 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3526 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3527 if (Op.getResNo() == 0)
3528 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3529 else
3530 Known = KnownBits::mulhs(Known, Known2);
3531 break;
3532 }
3533 case ISD::AVGFLOORU: {
3534 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3535 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3536 Known = KnownBits::avgFloorU(Known, Known2);
3537 break;
3538 }
3539 case ISD::AVGCEILU: {
3540 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3541 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3542 Known = KnownBits::avgCeilU(Known, Known2);
3543 break;
3544 }
3545 case ISD::AVGFLOORS: {
3546 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3547 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3548 Known = KnownBits::avgFloorS(Known, Known2);
3549 break;
3550 }
3551 case ISD::AVGCEILS: {
3552 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3553 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3554 Known = KnownBits::avgCeilS(Known, Known2);
3555 break;
3556 }
3557 case ISD::SELECT:
3558 case ISD::VSELECT:
3559 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3560 // If we don't know any bits, early out.
3561 if (Known.isUnknown())
3562 break;
3563 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3564
3565 // Only known if known in both the LHS and RHS.
3566 Known = Known.intersectWith(Known2);
3567 break;
3568 case ISD::SELECT_CC:
3569 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3570 // If we don't know any bits, early out.
3571 if (Known.isUnknown())
3572 break;
3573 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3574
3575 // Only known if known in both the LHS and RHS.
3576 Known = Known.intersectWith(Known2);
3577 break;
3578 case ISD::SMULO:
3579 case ISD::UMULO:
3580 if (Op.getResNo() != 1)
3581 break;
3582 // The boolean result conforms to getBooleanContents.
3583 // If we know the result of a setcc has the top bits zero, use this info.
3584 // We know that we have an integer-based boolean since these operations
3585 // are only available for integer.
3586 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3588 BitWidth > 1)
3589 Known.Zero.setBitsFrom(1);
3590 break;
3591 case ISD::SETCC:
3592 case ISD::SETCCCARRY:
3593 case ISD::STRICT_FSETCC:
3594 case ISD::STRICT_FSETCCS: {
3595 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3596 // If we know the result of a setcc has the top bits zero, use this info.
3597 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3599 BitWidth > 1)
3600 Known.Zero.setBitsFrom(1);
3601 break;
3602 }
3603 case ISD::SHL: {
3604 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3605 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3606
3607 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3608 bool NSW = Op->getFlags().hasNoSignedWrap();
3609
3610 bool ShAmtNonZero = Known2.isNonZero();
3611
3612 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3613
3614 // Minimum shift low bits are known zero.
3615 if (std::optional<uint64_t> ShMinAmt =
3616 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3617 Known.Zero.setLowBits(*ShMinAmt);
3618 break;
3619 }
3620 case ISD::SRL:
3621 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3622 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3623 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3624 Op->getFlags().hasExact());
3625
3626 // Minimum shift high bits are known zero.
3627 if (std::optional<uint64_t> ShMinAmt =
3628 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3629 Known.Zero.setHighBits(*ShMinAmt);
3630 break;
3631 case ISD::SRA:
3632 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3633 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3634 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3635 Op->getFlags().hasExact());
3636 break;
3637 case ISD::FSHL:
3638 case ISD::FSHR:
3639 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3640 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3641
3642 // For fshl, 0-shift returns the 1st arg.
3643 // For fshr, 0-shift returns the 2nd arg.
3644 if (Amt == 0) {
3645 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3646 DemandedElts, Depth + 1);
3647 break;
3648 }
3649
3650 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3651 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3652 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3653 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3654 if (Opcode == ISD::FSHL) {
3655 Known.One <<= Amt;
3656 Known.Zero <<= Amt;
3657 Known2.One.lshrInPlace(BitWidth - Amt);
3658 Known2.Zero.lshrInPlace(BitWidth - Amt);
3659 } else {
3660 Known.One <<= BitWidth - Amt;
3661 Known.Zero <<= BitWidth - Amt;
3662 Known2.One.lshrInPlace(Amt);
3663 Known2.Zero.lshrInPlace(Amt);
3664 }
3665 Known = Known.unionWith(Known2);
3666 }
3667 break;
3668 case ISD::SHL_PARTS:
3669 case ISD::SRA_PARTS:
3670 case ISD::SRL_PARTS: {
3671 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3672
3673 // Collect lo/hi source values and concatenate.
3674 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3675 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3676 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3677 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3678 Known = Known2.concat(Known);
3679
3680 // Collect shift amount.
3681 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3682
3683 if (Opcode == ISD::SHL_PARTS)
3684 Known = KnownBits::shl(Known, Known2);
3685 else if (Opcode == ISD::SRA_PARTS)
3686 Known = KnownBits::ashr(Known, Known2);
3687 else // if (Opcode == ISD::SRL_PARTS)
3688 Known = KnownBits::lshr(Known, Known2);
3689
3690 // TODO: Minimum shift low/high bits are known zero.
3691
3692 if (Op.getResNo() == 0)
3693 Known = Known.extractBits(LoBits, 0);
3694 else
3695 Known = Known.extractBits(HiBits, LoBits);
3696 break;
3697 }
3699 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3700 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3701 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3702 break;
3703 }
3704 case ISD::CTTZ:
3705 case ISD::CTTZ_ZERO_UNDEF: {
3706 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3707 // If we have a known 1, its position is our upper bound.
3708 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3709 unsigned LowBits = llvm::bit_width(PossibleTZ);
3710 Known.Zero.setBitsFrom(LowBits);
3711 break;
3712 }
3713 case ISD::CTLZ:
3714 case ISD::CTLZ_ZERO_UNDEF: {
3715 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3716 // If we have a known 1, its position is our upper bound.
3717 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3718 unsigned LowBits = llvm::bit_width(PossibleLZ);
3719 Known.Zero.setBitsFrom(LowBits);
3720 break;
3721 }
3722 case ISD::CTPOP: {
3723 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3724 // If we know some of the bits are zero, they can't be one.
3725 unsigned PossibleOnes = Known2.countMaxPopulation();
3726 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3727 break;
3728 }
3729 case ISD::PARITY: {
3730 // Parity returns 0 everywhere but the LSB.
3731 Known.Zero.setBitsFrom(1);
3732 break;
3733 }
3734 case ISD::LOAD: {
3735 LoadSDNode *LD = cast<LoadSDNode>(Op);
3736 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3737 if (ISD::isNON_EXTLoad(LD) && Cst) {
3738 // Determine any common known bits from the loaded constant pool value.
3739 Type *CstTy = Cst->getType();
3740 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3741 !Op.getValueType().isScalableVector()) {
3742 // If its a vector splat, then we can (quickly) reuse the scalar path.
3743 // NOTE: We assume all elements match and none are UNDEF.
3744 if (CstTy->isVectorTy()) {
3745 if (const Constant *Splat = Cst->getSplatValue()) {
3746 Cst = Splat;
3747 CstTy = Cst->getType();
3748 }
3749 }
3750 // TODO - do we need to handle different bitwidths?
3751 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3752 // Iterate across all vector elements finding common known bits.
3753 Known.One.setAllBits();
3754 Known.Zero.setAllBits();
3755 for (unsigned i = 0; i != NumElts; ++i) {
3756 if (!DemandedElts[i])
3757 continue;
3758 if (Constant *Elt = Cst->getAggregateElement(i)) {
3759 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3760 const APInt &Value = CInt->getValue();
3761 Known.One &= Value;
3762 Known.Zero &= ~Value;
3763 continue;
3764 }
3765 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3766 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3767 Known.One &= Value;
3768 Known.Zero &= ~Value;
3769 continue;
3770 }
3771 }
3772 Known.One.clearAllBits();
3773 Known.Zero.clearAllBits();
3774 break;
3775 }
3776 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3777 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3778 Known = KnownBits::makeConstant(CInt->getValue());
3779 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3780 Known =
3781 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3782 }
3783 }
3784 }
3785 } else if (Op.getResNo() == 0) {
3786 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3787 ? LD->getMemoryVT().getFixedSizeInBits()
3788 : BitWidth);
3789 EVT VT = Op.getValueType();
3790 // Fill in any known bits from range information. There are 3 types being
3791 // used. The results VT (same vector elt size as BitWidth), the loaded
3792 // MemoryVT (which may or may not be vector) and the range VTs original
3793 // type. The range matadata needs the full range (i.e
3794 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3795 // if it is know. These are then extended to the original VT sizes below.
3796 if (const MDNode *MD = LD->getRanges()) {
3798 if (VT.isVector()) {
3799 // Handle truncation to the first demanded element.
3800 // TODO: Figure out which demanded elements are covered
3801 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
3802 break;
3803 Known0 = Known0.trunc(BitWidth);
3804 }
3805 }
3806
3807 if (LD->getMemoryVT().isVector())
3808 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
3809
3810 // Extend the Known bits from memory to the size of the result.
3811 if (ISD::isZEXTLoad(Op.getNode()))
3812 Known = Known0.zext(BitWidth);
3813 else if (ISD::isSEXTLoad(Op.getNode()))
3814 Known = Known0.sext(BitWidth);
3815 else if (ISD::isEXTLoad(Op.getNode()))
3816 Known = Known0.anyext(BitWidth);
3817 else
3818 Known = Known0;
3819 assert(Known.getBitWidth() == BitWidth);
3820 return Known;
3821 }
3822 break;
3823 }
3825 if (Op.getValueType().isScalableVector())
3826 break;
3827 EVT InVT = Op.getOperand(0).getValueType();
3828 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3829 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3830 Known = Known.zext(BitWidth);
3831 break;
3832 }
3833 case ISD::ZERO_EXTEND: {
3834 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3835 Known = Known.zext(BitWidth);
3836 break;
3837 }
3839 if (Op.getValueType().isScalableVector())
3840 break;
3841 EVT InVT = Op.getOperand(0).getValueType();
3842 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3843 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3844 // If the sign bit is known to be zero or one, then sext will extend
3845 // it to the top bits, else it will just zext.
3846 Known = Known.sext(BitWidth);
3847 break;
3848 }
3849 case ISD::SIGN_EXTEND: {
3850 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3851 // If the sign bit is known to be zero or one, then sext will extend
3852 // it to the top bits, else it will just zext.
3853 Known = Known.sext(BitWidth);
3854 break;
3855 }
3857 if (Op.getValueType().isScalableVector())
3858 break;
3859 EVT InVT = Op.getOperand(0).getValueType();
3860 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3861 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3862 Known = Known.anyext(BitWidth);
3863 break;
3864 }
3865 case ISD::ANY_EXTEND: {
3866 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3867 Known = Known.anyext(BitWidth);
3868 break;
3869 }
3870 case ISD::TRUNCATE: {
3871 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3872 Known = Known.trunc(BitWidth);
3873 break;
3874 }
3875 case ISD::AssertZext: {
3876 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3878 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3879 Known.Zero |= (~InMask);
3880 Known.One &= (~Known.Zero);
3881 break;
3882 }
3883 case ISD::AssertAlign: {
3884 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3885 assert(LogOfAlign != 0);
3886
3887 // TODO: Should use maximum with source
3888 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3889 // well as clearing one bits.
3890 Known.Zero.setLowBits(LogOfAlign);
3891 Known.One.clearLowBits(LogOfAlign);
3892 break;
3893 }
3894 case ISD::FGETSIGN:
3895 // All bits are zero except the low bit.
3896 Known.Zero.setBitsFrom(1);
3897 break;
3898 case ISD::ADD:
3899 case ISD::SUB: {
3900 SDNodeFlags Flags = Op.getNode()->getFlags();
3901 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3902 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3904 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3905 Flags.hasNoUnsignedWrap(), Known, Known2);
3906 break;
3907 }
3908 case ISD::USUBO:
3909 case ISD::SSUBO:
3910 case ISD::USUBO_CARRY:
3911 case ISD::SSUBO_CARRY:
3912 if (Op.getResNo() == 1) {
3913 // If we know the result of a setcc has the top bits zero, use this info.
3914 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3916 BitWidth > 1)
3917 Known.Zero.setBitsFrom(1);
3918 break;
3919 }
3920 [[fallthrough]];
3921 case ISD::SUBC: {
3922 assert(Op.getResNo() == 0 &&
3923 "We only compute knownbits for the difference here.");
3924
3925 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
3926 KnownBits Borrow(1);
3927 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3928 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3929 // Borrow has bit width 1
3930 Borrow = Borrow.trunc(1);
3931 } else {
3932 Borrow.setAllZero();
3933 }
3934
3935 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3936 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3937 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
3938 break;
3939 }
3940 case ISD::UADDO:
3941 case ISD::SADDO:
3942 case ISD::UADDO_CARRY:
3943 case ISD::SADDO_CARRY:
3944 if (Op.getResNo() == 1) {
3945 // If we know the result of a setcc has the top bits zero, use this info.
3946 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3948 BitWidth > 1)
3949 Known.Zero.setBitsFrom(1);
3950 break;
3951 }
3952 [[fallthrough]];
3953 case ISD::ADDC:
3954 case ISD::ADDE: {
3955 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3956
3957 // With ADDE and UADDO_CARRY, a carry bit may be added in.
3958 KnownBits Carry(1);
3959 if (Opcode == ISD::ADDE)
3960 // Can't track carry from glue, set carry to unknown.
3961 Carry.resetAll();
3962 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3963 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3964 // Carry has bit width 1
3965 Carry = Carry.trunc(1);
3966 } else {
3967 Carry.setAllZero();
3968 }
3969
3970 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3971 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3972 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3973 break;
3974 }
3975 case ISD::UDIV: {
3976 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3977 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3978 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
3979 break;
3980 }
3981 case ISD::SDIV: {
3982 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3983 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3984 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
3985 break;
3986 }
3987 case ISD::SREM: {
3988 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3989 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3990 Known = KnownBits::srem(Known, Known2);
3991 break;
3992 }
3993 case ISD::UREM: {
3994 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3995 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3996 Known = KnownBits::urem(Known, Known2);
3997 break;
3998 }
3999 case ISD::EXTRACT_ELEMENT: {
4000 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4001 const unsigned Index = Op.getConstantOperandVal(1);
4002 const unsigned EltBitWidth = Op.getValueSizeInBits();
4003
4004 // Remove low part of known bits mask
4005 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4006 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4007
4008 // Remove high part of known bit mask
4009 Known = Known.trunc(EltBitWidth);
4010 break;
4011 }
4013 SDValue InVec = Op.getOperand(0);
4014 SDValue EltNo = Op.getOperand(1);
4015 EVT VecVT = InVec.getValueType();
4016 // computeKnownBits not yet implemented for scalable vectors.
4017 if (VecVT.isScalableVector())
4018 break;
4019 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4020 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4021
4022 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4023 // anything about the extended bits.
4024 if (BitWidth > EltBitWidth)
4025 Known = Known.trunc(EltBitWidth);
4026
4027 // If we know the element index, just demand that vector element, else for
4028 // an unknown element index, ignore DemandedElts and demand them all.
4029 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4030 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4031 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4032 DemandedSrcElts =
4033 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4034
4035 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4036 if (BitWidth > EltBitWidth)
4037 Known = Known.anyext(BitWidth);
4038 break;
4039 }
4041 if (Op.getValueType().isScalableVector())
4042 break;
4043
4044 // If we know the element index, split the demand between the
4045 // source vector and the inserted element, otherwise assume we need
4046 // the original demanded vector elements and the value.
4047 SDValue InVec = Op.getOperand(0);
4048 SDValue InVal = Op.getOperand(1);
4049 SDValue EltNo = Op.getOperand(2);
4050 bool DemandedVal = true;
4051 APInt DemandedVecElts = DemandedElts;
4052 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4053 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4054 unsigned EltIdx = CEltNo->getZExtValue();
4055 DemandedVal = !!DemandedElts[EltIdx];
4056 DemandedVecElts.clearBit(EltIdx);
4057 }
4058 Known.One.setAllBits();
4059 Known.Zero.setAllBits();
4060 if (DemandedVal) {
4061 Known2 = computeKnownBits(InVal, Depth + 1);
4062 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4063 }
4064 if (!!DemandedVecElts) {
4065 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4066 Known = Known.intersectWith(Known2);
4067 }
4068 break;
4069 }
4070 case ISD::BITREVERSE: {
4071 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4072 Known = Known2.reverseBits();
4073 break;
4074 }
4075 case ISD::BSWAP: {
4076 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4077 Known = Known2.byteSwap();
4078 break;
4079 }
4080 case ISD::ABS: {
4081 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4082 Known = Known2.abs();
4083 Known.Zero.setHighBits(
4084 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4085 break;
4086 }
4087 case ISD::USUBSAT: {
4088 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4089 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4090 Known = KnownBits::usub_sat(Known, Known2);
4091 break;
4092 }
4093 case ISD::UMIN: {
4094 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4095 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4096 Known = KnownBits::umin(Known, Known2);
4097 break;
4098 }
4099 case ISD::UMAX: {
4100 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4101 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4102 Known = KnownBits::umax(Known, Known2);
4103 break;
4104 }
4105 case ISD::SMIN:
4106 case ISD::SMAX: {
4107 // If we have a clamp pattern, we know that the number of sign bits will be
4108 // the minimum of the clamp min/max range.
4109 bool IsMax = (Opcode == ISD::SMAX);
4110 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4111 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4112 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4113 CstHigh =
4114 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4115 if (CstLow && CstHigh) {
4116 if (!IsMax)
4117 std::swap(CstLow, CstHigh);
4118
4119 const APInt &ValueLow = CstLow->getAPIntValue();
4120 const APInt &ValueHigh = CstHigh->getAPIntValue();
4121 if (ValueLow.sle(ValueHigh)) {
4122 unsigned LowSignBits = ValueLow.getNumSignBits();
4123 unsigned HighSignBits = ValueHigh.getNumSignBits();
4124 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4125 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4126 Known.One.setHighBits(MinSignBits);
4127 break;
4128 }
4129 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4130 Known.Zero.setHighBits(MinSignBits);
4131 break;
4132 }
4133 }
4134 }
4135
4136 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4137 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4138 if (IsMax)
4139 Known = KnownBits::smax(Known, Known2);
4140 else
4141 Known = KnownBits::smin(Known, Known2);
4142
4143 // For SMAX, if CstLow is non-negative we know the result will be
4144 // non-negative and thus all sign bits are 0.
4145 // TODO: There's an equivalent of this for smin with negative constant for
4146 // known ones.
4147 if (IsMax && CstLow) {
4148 const APInt &ValueLow = CstLow->getAPIntValue();
4149 if (ValueLow.isNonNegative()) {
4150 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4151 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4152 }
4153 }
4154
4155 break;
4156 }
4157 case ISD::UINT_TO_FP: {
4158 Known.makeNonNegative();
4159 break;
4160 }
4161 case ISD::SINT_TO_FP: {
4162 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4163 if (Known2.isNonNegative())
4164 Known.makeNonNegative();
4165 else if (Known2.isNegative())
4166 Known.makeNegative();
4167 break;
4168 }
4169 case ISD::FP_TO_UINT_SAT: {
4170 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4171 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4173 break;
4174 }
4176 if (Op.getResNo() == 1) {
4177 // The boolean result conforms to getBooleanContents.
4178 // If we know the result of a setcc has the top bits zero, use this info.
4179 // We know that we have an integer-based boolean since these operations
4180 // are only available for integer.
4181 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4183 BitWidth > 1)
4184 Known.Zero.setBitsFrom(1);
4185 break;
4186 }
4187 [[fallthrough]];
4189 case ISD::ATOMIC_SWAP:
4201 case ISD::ATOMIC_LOAD: {
4202 unsigned MemBits =
4203 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4204 // If we are looking at the loaded value.
4205 if (Op.getResNo() == 0) {
4207 Known.Zero.setBitsFrom(MemBits);
4208 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4209 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4210 Known.Zero.setBitsFrom(MemBits);
4211 }
4212 break;
4213 }
4214 case ISD::FrameIndex:
4216 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4217 Known, getMachineFunction());
4218 break;
4219
4220 default:
4221 if (Opcode < ISD::BUILTIN_OP_END)
4222 break;
4223 [[fallthrough]];
4227 // TODO: Probably okay to remove after audit; here to reduce change size
4228 // in initial enablement patch for scalable vectors
4229 if (Op.getValueType().isScalableVector())
4230 break;
4231
4232 // Allow the target to implement this method for its nodes.
4233 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4234 break;
4235 }
4236
4237 return Known;
4238}
4239
4240/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4242 switch (OR) {
4250 }
4251 llvm_unreachable("Unknown OverflowResult");
4252}
4253
4256 // X + 0 never overflow
4257 if (isNullConstant(N1))
4258 return OFK_Never;
4259
4260 // If both operands each have at least two sign bits, the addition
4261 // cannot overflow.
4262 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4263 return OFK_Never;
4264
4265 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4266 return OFK_Sometime;
4267}
4268
4271 // X + 0 never overflow
4272 if (isNullConstant(N1))
4273 return OFK_Never;
4274
4275 // mulhi + 1 never overflow
4276 KnownBits N1Known = computeKnownBits(N1);
4277 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4278 N1Known.getMaxValue().ult(2))
4279 return OFK_Never;
4280
4281 KnownBits N0Known = computeKnownBits(N0);
4282 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4283 N0Known.getMaxValue().ult(2))
4284 return OFK_Never;
4285
4286 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4287 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4288 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4289 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4290}
4291
4294 // X - 0 never overflow
4295 if (isNullConstant(N1))
4296 return OFK_Never;
4297
4298 // If both operands each have at least two sign bits, the subtraction
4299 // cannot overflow.
4300 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4301 return OFK_Never;
4302
4303 KnownBits N0Known = computeKnownBits(N0);
4304 KnownBits N1Known = computeKnownBits(N1);
4305 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4306 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4307 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4308}
4309
4312 // X - 0 never overflow
4313 if (isNullConstant(N1))
4314 return OFK_Never;
4315
4316 KnownBits N0Known = computeKnownBits(N0);
4317 KnownBits N1Known = computeKnownBits(N1);
4318 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4319 ConstantRange N1Range =