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;
137 &losesInfo);
138 return !losesInfo;
139}
140
141//===----------------------------------------------------------------------===//
142// ISD Namespace
143//===----------------------------------------------------------------------===//
144
145bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
146 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
147 unsigned EltSize =
148 N->getValueType(0).getVectorElementType().getSizeInBits();
149 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
150 SplatVal = Op0->getAPIntValue().trunc(EltSize);
151 return true;
152 }
153 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
154 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
155 return true;
156 }
157 }
158
159 auto *BV = dyn_cast<BuildVectorSDNode>(N);
160 if (!BV)
161 return false;
162
163 APInt SplatUndef;
164 unsigned SplatBitSize;
165 bool HasUndefs;
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 // Endianness does not matter here. We are checking for a splat given the
168 // element size of the vector, and if we find such a splat for little endian
169 // layout, then that should be valid also for big endian (as the full vector
170 // size is known to be a multiple of the element size).
171 const bool IsBigEndian = false;
172 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
173 EltSize, IsBigEndian) &&
174 EltSize == SplatBitSize;
175}
176
177// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
178// specializations of the more general isConstantSplatVector()?
179
180bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
181 // Look through a bit convert.
182 while (N->getOpcode() == ISD::BITCAST)
183 N = N->getOperand(0).getNode();
184
185 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
186 APInt SplatVal;
187 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
188 }
189
190 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
191
192 unsigned i = 0, e = N->getNumOperands();
193
194 // Skip over all of the undef values.
195 while (i != e && N->getOperand(i).isUndef())
196 ++i;
197
198 // Do not accept an all-undef vector.
199 if (i == e) return false;
200
201 // Do not accept build_vectors that aren't all constants or which have non-~0
202 // elements. We have to be a bit careful here, as the type of the constant
203 // may not be the same as the type of the vector elements due to type
204 // legalization (the elements are promoted to a legal type for the target and
205 // a vector of a type may be legal when the base element type is not).
206 // We only want to check enough bits to cover the vector elements, because
207 // we care if the resultant vector is all ones, not whether the individual
208 // constants are.
209 SDValue NotZero = N->getOperand(i);
210 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
211 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
212 if (CN->getAPIntValue().countr_one() < EltSize)
213 return false;
214 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
215 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
216 return false;
217 } else
218 return false;
219
220 // Okay, we have at least one ~0 value, check to see if the rest match or are
221 // undefs. Even with the above element type twiddling, this should be OK, as
222 // the same type legalization should have applied to all the elements.
223 for (++i; i != e; ++i)
224 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
225 return false;
226 return true;
227}
228
229bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
230 // Look through a bit convert.
231 while (N->getOpcode() == ISD::BITCAST)
232 N = N->getOperand(0).getNode();
233
234 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
235 APInt SplatVal;
236 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
237 }
238
239 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
240
241 bool IsAllUndef = true;
242 for (const SDValue &Op : N->op_values()) {
243 if (Op.isUndef())
244 continue;
245 IsAllUndef = false;
246 // Do not accept build_vectors that aren't all constants or which have non-0
247 // elements. We have to be a bit careful here, as the type of the constant
248 // may not be the same as the type of the vector elements due to type
249 // legalization (the elements are promoted to a legal type for the target
250 // and a vector of a type may be legal when the base element type is not).
251 // We only want to check enough bits to cover the vector elements, because
252 // we care if the resultant vector is all zeros, not whether the individual
253 // constants are.
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
256 if (CN->getAPIntValue().countr_zero() < EltSize)
257 return false;
258 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
259 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
260 return false;
261 } else
262 return false;
263 }
264
265 // Do not accept an all-undef vector.
266 if (IsAllUndef)
267 return false;
268 return true;
269}
270
272 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
273}
274
276 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
277}
278
280 if (N->getOpcode() != ISD::BUILD_VECTOR)
281 return false;
282
283 for (const SDValue &Op : N->op_values()) {
284 if (Op.isUndef())
285 continue;
286 if (!isa<ConstantSDNode>(Op))
287 return false;
288 }
289 return true;
290}
291
293 if (N->getOpcode() != ISD::BUILD_VECTOR)
294 return false;
295
296 for (const SDValue &Op : N->op_values()) {
297 if (Op.isUndef())
298 continue;
299 if (!isa<ConstantFPSDNode>(Op))
300 return false;
301 }
302 return true;
303}
304
305bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
306 bool Signed) {
307 assert(N->getValueType(0).isVector() && "Expected a vector!");
308
309 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
310 if (EltSize <= NewEltSize)
311 return false;
312
313 if (N->getOpcode() == ISD::ZERO_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 !Signed;
317 }
318 if (N->getOpcode() == ISD::SIGN_EXTEND) {
319 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
320 NewEltSize) &&
321 Signed;
322 }
323 if (N->getOpcode() != ISD::BUILD_VECTOR)
324 return false;
325
326 for (const SDValue &Op : N->op_values()) {
327 if (Op.isUndef())
328 continue;
329 if (!isa<ConstantSDNode>(Op))
330 return false;
331
332 APInt C = Op->getAsAPIntVal().trunc(EltSize);
333 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
334 return false;
335 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
336 return false;
337 }
338
339 return true;
340}
341
343 // Return false if the node has no operands.
344 // This is "logically inconsistent" with the definition of "all" but
345 // is probably the desired behavior.
346 if (N->getNumOperands() == 0)
347 return false;
348 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
349}
350
352 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
353}
354
355template <typename ConstNodeType>
357 std::function<bool(ConstNodeType *)> Match,
358 bool AllowUndefs) {
359 // FIXME: Add support for scalar UNDEF cases?
360 if (auto *C = dyn_cast<ConstNodeType>(Op))
361 return Match(C);
362
363 // FIXME: Add support for vector UNDEF cases?
364 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
365 ISD::SPLAT_VECTOR != Op.getOpcode())
366 return false;
367
368 EVT SVT = Op.getValueType().getScalarType();
369 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
370 if (AllowUndefs && Op.getOperand(i).isUndef()) {
371 if (!Match(nullptr))
372 return false;
373 continue;
374 }
375
376 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
377 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
378 return false;
379 }
380 return true;
381}
382// Build used template types.
383template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
384 SDValue, std::function<bool(ConstantSDNode *)>, bool);
385template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
386 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
387
389 SDValue LHS, SDValue RHS,
390 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
391 bool AllowUndefs, bool AllowTypeMismatch) {
392 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
393 return false;
394
395 // TODO: Add support for scalar UNDEF cases?
396 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
397 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
398 return Match(LHSCst, RHSCst);
399
400 // TODO: Add support for vector UNDEF cases?
401 if (LHS.getOpcode() != RHS.getOpcode() ||
402 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
403 LHS.getOpcode() != ISD::SPLAT_VECTOR))
404 return false;
405
406 EVT SVT = LHS.getValueType().getScalarType();
407 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
408 SDValue LHSOp = LHS.getOperand(i);
409 SDValue RHSOp = RHS.getOperand(i);
410 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
411 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
412 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
413 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
414 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
415 return false;
416 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
417 LHSOp.getValueType() != RHSOp.getValueType()))
418 return false;
419 if (!Match(LHSCst, RHSCst))
420 return false;
421 }
422 return true;
423}
424
426 switch (VecReduceOpcode) {
427 default:
428 llvm_unreachable("Expected VECREDUCE opcode");
431 case ISD::VP_REDUCE_FADD:
432 case ISD::VP_REDUCE_SEQ_FADD:
433 return ISD::FADD;
436 case ISD::VP_REDUCE_FMUL:
437 case ISD::VP_REDUCE_SEQ_FMUL:
438 return ISD::FMUL;
440 case ISD::VP_REDUCE_ADD:
441 return ISD::ADD;
443 case ISD::VP_REDUCE_MUL:
444 return ISD::MUL;
446 case ISD::VP_REDUCE_AND:
447 return ISD::AND;
449 case ISD::VP_REDUCE_OR:
450 return ISD::OR;
452 case ISD::VP_REDUCE_XOR:
453 return ISD::XOR;
455 case ISD::VP_REDUCE_SMAX:
456 return ISD::SMAX;
458 case ISD::VP_REDUCE_SMIN:
459 return ISD::SMIN;
461 case ISD::VP_REDUCE_UMAX:
462 return ISD::UMAX;
464 case ISD::VP_REDUCE_UMIN:
465 return ISD::UMIN;
467 case ISD::VP_REDUCE_FMAX:
468 return ISD::FMAXNUM;
470 case ISD::VP_REDUCE_FMIN:
471 return ISD::FMINNUM;
473 case ISD::VP_REDUCE_FMAXIMUM:
474 return ISD::FMAXIMUM;
476 case ISD::VP_REDUCE_FMINIMUM:
477 return ISD::FMINIMUM;
478 }
479}
480
481bool ISD::isVPOpcode(unsigned Opcode) {
482 switch (Opcode) {
483 default:
484 return false;
485#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
486 case ISD::VPSD: \
487 return true;
488#include "llvm/IR/VPIntrinsics.def"
489 }
490}
491
492bool ISD::isVPBinaryOp(unsigned Opcode) {
493 switch (Opcode) {
494 default:
495 break;
496#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
497#define VP_PROPERTY_BINARYOP return true;
498#define END_REGISTER_VP_SDNODE(VPSD) break;
499#include "llvm/IR/VPIntrinsics.def"
500 }
501 return false;
502}
503
504bool ISD::isVPReduction(unsigned Opcode) {
505 switch (Opcode) {
506 default:
507 break;
508#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
509#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
510#define END_REGISTER_VP_SDNODE(VPSD) break;
511#include "llvm/IR/VPIntrinsics.def"
512 }
513 return false;
514}
515
516/// The operand position of the vector mask.
517std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
518 switch (Opcode) {
519 default:
520 return std::nullopt;
521#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
522 case ISD::VPSD: \
523 return MASKPOS;
524#include "llvm/IR/VPIntrinsics.def"
525 }
526}
527
528/// The operand position of the explicit vector length parameter.
529std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
530 switch (Opcode) {
531 default:
532 return std::nullopt;
533#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
534 case ISD::VPSD: \
535 return EVLPOS;
536#include "llvm/IR/VPIntrinsics.def"
537 }
538}
539
540std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
541 bool hasFPExcept) {
542 // FIXME: Return strict opcodes in case of fp exceptions.
543 switch (VPOpcode) {
544 default:
545 return std::nullopt;
546#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
547#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
548#define END_REGISTER_VP_SDNODE(VPOPC) break;
549#include "llvm/IR/VPIntrinsics.def"
550 }
551 return std::nullopt;
552}
553
554unsigned ISD::getVPForBaseOpcode(unsigned Opcode) {
555 switch (Opcode) {
556 default:
557 llvm_unreachable("can not translate this Opcode to VP.");
558#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
559#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
560#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
561#include "llvm/IR/VPIntrinsics.def"
562 }
563}
564
566 switch (ExtType) {
567 case ISD::EXTLOAD:
568 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
569 case ISD::SEXTLOAD:
570 return ISD::SIGN_EXTEND;
571 case ISD::ZEXTLOAD:
572 return ISD::ZERO_EXTEND;
573 default:
574 break;
575 }
576
577 llvm_unreachable("Invalid LoadExtType");
578}
579
581 // To perform this operation, we just need to swap the L and G bits of the
582 // operation.
583 unsigned OldL = (Operation >> 2) & 1;
584 unsigned OldG = (Operation >> 1) & 1;
585 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
586 (OldL << 1) | // New G bit
587 (OldG << 2)); // New L bit.
588}
589
591 unsigned Operation = Op;
592 if (isIntegerLike)
593 Operation ^= 7; // Flip L, G, E bits, but not U.
594 else
595 Operation ^= 15; // Flip all of the condition bits.
596
598 Operation &= ~8; // Don't let N and U bits get set.
599
600 return ISD::CondCode(Operation);
601}
602
604 return getSetCCInverseImpl(Op, Type.isInteger());
605}
606
608 bool isIntegerLike) {
609 return getSetCCInverseImpl(Op, isIntegerLike);
610}
611
612/// For an integer comparison, return 1 if the comparison is a signed operation
613/// and 2 if the result is an unsigned comparison. Return zero if the operation
614/// does not depend on the sign of the input (setne and seteq).
615static int isSignedOp(ISD::CondCode Opcode) {
616 switch (Opcode) {
617 default: llvm_unreachable("Illegal integer setcc operation!");
618 case ISD::SETEQ:
619 case ISD::SETNE: return 0;
620 case ISD::SETLT:
621 case ISD::SETLE:
622 case ISD::SETGT:
623 case ISD::SETGE: return 1;
624 case ISD::SETULT:
625 case ISD::SETULE:
626 case ISD::SETUGT:
627 case ISD::SETUGE: return 2;
628 }
629}
630
632 EVT Type) {
633 bool IsInteger = Type.isInteger();
634 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
635 // Cannot fold a signed integer setcc with an unsigned integer setcc.
636 return ISD::SETCC_INVALID;
637
638 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
639
640 // If the N and U bits get set, then the resultant comparison DOES suddenly
641 // care about orderedness, and it is true when ordered.
642 if (Op > ISD::SETTRUE2)
643 Op &= ~16; // Clear the U bit if the N bit is set.
644
645 // Canonicalize illegal integer setcc's.
646 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
647 Op = ISD::SETNE;
648
649 return ISD::CondCode(Op);
650}
651
653 EVT Type) {
654 bool IsInteger = Type.isInteger();
655 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
656 // Cannot fold a signed setcc with an unsigned setcc.
657 return ISD::SETCC_INVALID;
658
659 // Combine all of the condition bits.
660 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
661
662 // Canonicalize illegal integer setcc's.
663 if (IsInteger) {
664 switch (Result) {
665 default: break;
666 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
667 case ISD::SETOEQ: // SETEQ & SETU[LG]E
668 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
669 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
670 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
671 }
672 }
673
674 return Result;
675}
676
677//===----------------------------------------------------------------------===//
678// SDNode Profile Support
679//===----------------------------------------------------------------------===//
680
681/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
682static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
683 ID.AddInteger(OpC);
684}
685
686/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
687/// solely with their pointer.
689 ID.AddPointer(VTList.VTs);
690}
691
692/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
694 ArrayRef<SDValue> Ops) {
695 for (const auto &Op : Ops) {
696 ID.AddPointer(Op.getNode());
697 ID.AddInteger(Op.getResNo());
698 }
699}
700
701/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
703 ArrayRef<SDUse> Ops) {
704 for (const auto &Op : Ops) {
705 ID.AddPointer(Op.getNode());
706 ID.AddInteger(Op.getResNo());
707 }
708}
709
710static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
711 SDVTList VTList, ArrayRef<SDValue> OpList) {
712 AddNodeIDOpcode(ID, OpC);
713 AddNodeIDValueTypes(ID, VTList);
714 AddNodeIDOperands(ID, OpList);
715}
716
717/// If this is an SDNode with special info, add this info to the NodeID data.
719 switch (N->getOpcode()) {
722 case ISD::MCSymbol:
723 llvm_unreachable("Should only be used on nodes with operands");
724 default: break; // Normal nodes don't need extra info.
726 case ISD::Constant: {
727 const ConstantSDNode *C = cast<ConstantSDNode>(N);
728 ID.AddPointer(C->getConstantIntValue());
729 ID.AddBoolean(C->isOpaque());
730 break;
731 }
733 case ISD::ConstantFP:
734 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
735 break;
740 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
741 ID.AddPointer(GA->getGlobal());
742 ID.AddInteger(GA->getOffset());
743 ID.AddInteger(GA->getTargetFlags());
744 break;
745 }
746 case ISD::BasicBlock:
747 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
748 break;
749 case ISD::Register:
750 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
751 break;
753 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
754 break;
755 case ISD::SRCVALUE:
756 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
757 break;
758 case ISD::FrameIndex:
760 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
761 break;
764 if (cast<LifetimeSDNode>(N)->hasOffset()) {
765 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
766 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
767 }
768 break;
770 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
771 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
772 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
773 break;
774 case ISD::JumpTable:
776 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
777 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
778 break;
781 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
782 ID.AddInteger(CP->getAlign().value());
783 ID.AddInteger(CP->getOffset());
784 if (CP->isMachineConstantPoolEntry())
785 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
786 else
787 ID.AddPointer(CP->getConstVal());
788 ID.AddInteger(CP->getTargetFlags());
789 break;
790 }
791 case ISD::TargetIndex: {
792 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
793 ID.AddInteger(TI->getIndex());
794 ID.AddInteger(TI->getOffset());
795 ID.AddInteger(TI->getTargetFlags());
796 break;
797 }
798 case ISD::LOAD: {
799 const LoadSDNode *LD = cast<LoadSDNode>(N);
800 ID.AddInteger(LD->getMemoryVT().getRawBits());
801 ID.AddInteger(LD->getRawSubclassData());
802 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
803 ID.AddInteger(LD->getMemOperand()->getFlags());
804 break;
805 }
806 case ISD::STORE: {
807 const StoreSDNode *ST = cast<StoreSDNode>(N);
808 ID.AddInteger(ST->getMemoryVT().getRawBits());
809 ID.AddInteger(ST->getRawSubclassData());
810 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
811 ID.AddInteger(ST->getMemOperand()->getFlags());
812 break;
813 }
814 case ISD::VP_LOAD: {
815 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
816 ID.AddInteger(ELD->getMemoryVT().getRawBits());
817 ID.AddInteger(ELD->getRawSubclassData());
818 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
819 ID.AddInteger(ELD->getMemOperand()->getFlags());
820 break;
821 }
822 case ISD::VP_STORE: {
823 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
824 ID.AddInteger(EST->getMemoryVT().getRawBits());
825 ID.AddInteger(EST->getRawSubclassData());
826 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
827 ID.AddInteger(EST->getMemOperand()->getFlags());
828 break;
829 }
830 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
831 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
832 ID.AddInteger(SLD->getMemoryVT().getRawBits());
833 ID.AddInteger(SLD->getRawSubclassData());
834 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
835 break;
836 }
837 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
838 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
839 ID.AddInteger(SST->getMemoryVT().getRawBits());
840 ID.AddInteger(SST->getRawSubclassData());
841 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
842 break;
843 }
844 case ISD::VP_GATHER: {
845 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
846 ID.AddInteger(EG->getMemoryVT().getRawBits());
847 ID.AddInteger(EG->getRawSubclassData());
848 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
849 ID.AddInteger(EG->getMemOperand()->getFlags());
850 break;
851 }
852 case ISD::VP_SCATTER: {
853 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
854 ID.AddInteger(ES->getMemoryVT().getRawBits());
855 ID.AddInteger(ES->getRawSubclassData());
856 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
857 ID.AddInteger(ES->getMemOperand()->getFlags());
858 break;
859 }
860 case ISD::MLOAD: {
861 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
862 ID.AddInteger(MLD->getMemoryVT().getRawBits());
863 ID.AddInteger(MLD->getRawSubclassData());
864 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
865 ID.AddInteger(MLD->getMemOperand()->getFlags());
866 break;
867 }
868 case ISD::MSTORE: {
869 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
870 ID.AddInteger(MST->getMemoryVT().getRawBits());
871 ID.AddInteger(MST->getRawSubclassData());
872 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
873 ID.AddInteger(MST->getMemOperand()->getFlags());
874 break;
875 }
876 case ISD::MGATHER: {
877 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
878 ID.AddInteger(MG->getMemoryVT().getRawBits());
879 ID.AddInteger(MG->getRawSubclassData());
880 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
881 ID.AddInteger(MG->getMemOperand()->getFlags());
882 break;
883 }
884 case ISD::MSCATTER: {
885 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
886 ID.AddInteger(MS->getMemoryVT().getRawBits());
887 ID.AddInteger(MS->getRawSubclassData());
888 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
889 ID.AddInteger(MS->getMemOperand()->getFlags());
890 break;
891 }
894 case ISD::ATOMIC_SWAP:
906 case ISD::ATOMIC_LOAD:
907 case ISD::ATOMIC_STORE: {
908 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
909 ID.AddInteger(AT->getMemoryVT().getRawBits());
910 ID.AddInteger(AT->getRawSubclassData());
911 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
912 ID.AddInteger(AT->getMemOperand()->getFlags());
913 break;
914 }
915 case ISD::VECTOR_SHUFFLE: {
916 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
917 for (int M : Mask)
918 ID.AddInteger(M);
919 break;
920 }
922 case ISD::BlockAddress: {
923 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
924 ID.AddPointer(BA->getBlockAddress());
925 ID.AddInteger(BA->getOffset());
926 ID.AddInteger(BA->getTargetFlags());
927 break;
928 }
929 case ISD::AssertAlign:
930 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
931 break;
932 case ISD::PREFETCH:
935 // Handled by MemIntrinsicSDNode check after the switch.
936 break;
937 } // end switch (N->getOpcode())
938
939 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
940 // to check.
941 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
942 ID.AddInteger(MN->getRawSubclassData());
943 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
944 ID.AddInteger(MN->getMemOperand()->getFlags());
945 ID.AddInteger(MN->getMemoryVT().getRawBits());
946 }
947}
948
949/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
950/// data.
951static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
952 AddNodeIDOpcode(ID, N->getOpcode());
953 // Add the return value info.
954 AddNodeIDValueTypes(ID, N->getVTList());
955 // Add the operand info.
956 AddNodeIDOperands(ID, N->ops());
957
958 // Handle SDNode leafs with special info.
960}
961
962//===----------------------------------------------------------------------===//
963// SelectionDAG Class
964//===----------------------------------------------------------------------===//
965
966/// doNotCSE - Return true if CSE should not be performed for this node.
967static bool doNotCSE(SDNode *N) {
968 if (N->getValueType(0) == MVT::Glue)
969 return true; // Never CSE anything that produces a glue result.
970
971 switch (N->getOpcode()) {
972 default: break;
973 case ISD::HANDLENODE:
974 case ISD::EH_LABEL:
975 return true; // Never CSE these nodes.
976 }
977
978 // Check that remaining values produced are not flags.
979 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
980 if (N->getValueType(i) == MVT::Glue)
981 return true; // Never CSE anything that produces a glue result.
982
983 return false;
984}
985
986/// RemoveDeadNodes - This method deletes all unreachable nodes in the
987/// SelectionDAG.
989 // Create a dummy node (which is not added to allnodes), that adds a reference
990 // to the root node, preventing it from being deleted.
991 HandleSDNode Dummy(getRoot());
992
994
995 // Add all obviously-dead nodes to the DeadNodes worklist.
996 for (SDNode &Node : allnodes())
997 if (Node.use_empty())
998 DeadNodes.push_back(&Node);
999
1000 RemoveDeadNodes(DeadNodes);
1001
1002 // If the root changed (e.g. it was a dead load, update the root).
1003 setRoot(Dummy.getValue());
1004}
1005
1006/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1007/// given list, and any nodes that become unreachable as a result.
1009
1010 // Process the worklist, deleting the nodes and adding their uses to the
1011 // worklist.
1012 while (!DeadNodes.empty()) {
1013 SDNode *N = DeadNodes.pop_back_val();
1014 // Skip to next node if we've already managed to delete the node. This could
1015 // happen if replacing a node causes a node previously added to the node to
1016 // be deleted.
1017 if (N->getOpcode() == ISD::DELETED_NODE)
1018 continue;
1019
1020 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1021 DUL->NodeDeleted(N, nullptr);
1022
1023 // Take the node out of the appropriate CSE map.
1024 RemoveNodeFromCSEMaps(N);
1025
1026 // Next, brutally remove the operand list. This is safe to do, as there are
1027 // no cycles in the graph.
1028 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1029 SDUse &Use = *I++;
1030 SDNode *Operand = Use.getNode();
1031 Use.set(SDValue());
1032
1033 // Now that we removed this operand, see if there are no uses of it left.
1034 if (Operand->use_empty())
1035 DeadNodes.push_back(Operand);
1036 }
1037
1038 DeallocateNode(N);
1039 }
1040}
1041
1043 SmallVector<SDNode*, 16> DeadNodes(1, N);
1044
1045 // Create a dummy node that adds a reference to the root node, preventing
1046 // it from being deleted. (This matters if the root is an operand of the
1047 // dead node.)
1048 HandleSDNode Dummy(getRoot());
1049
1050 RemoveDeadNodes(DeadNodes);
1051}
1052
1054 // First take this out of the appropriate CSE map.
1055 RemoveNodeFromCSEMaps(N);
1056
1057 // Finally, remove uses due to operands of this node, remove from the
1058 // AllNodes list, and delete the node.
1059 DeleteNodeNotInCSEMaps(N);
1060}
1061
1062void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1063 assert(N->getIterator() != AllNodes.begin() &&
1064 "Cannot delete the entry node!");
1065 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1066
1067 // Drop all of the operands and decrement used node's use counts.
1068 N->DropOperands();
1069
1070 DeallocateNode(N);
1071}
1072
1073void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1074 assert(!(V->isVariadic() && isParameter));
1075 if (isParameter)
1076 ByvalParmDbgValues.push_back(V);
1077 else
1078 DbgValues.push_back(V);
1079 for (const SDNode *Node : V->getSDNodes())
1080 if (Node)
1081 DbgValMap[Node].push_back(V);
1082}
1083
1084void SDDbgInfo::erase(const SDNode *Node) {
1085 DbgValMapType::iterator I = DbgValMap.find(Node);
1086 if (I == DbgValMap.end())
1087 return;
1088 for (auto &Val: I->second)
1089 Val->setIsInvalidated();
1090 DbgValMap.erase(I);
1091}
1092
1093void SelectionDAG::DeallocateNode(SDNode *N) {
1094 // If we have operands, deallocate them.
1095 removeOperands(N);
1096
1097 NodeAllocator.Deallocate(AllNodes.remove(N));
1098
1099 // Set the opcode to DELETED_NODE to help catch bugs when node
1100 // memory is reallocated.
1101 // FIXME: There are places in SDag that have grown a dependency on the opcode
1102 // value in the released node.
1103 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1104 N->NodeType = ISD::DELETED_NODE;
1105
1106 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1107 // them and forget about that node.
1108 DbgInfo->erase(N);
1109
1110 // Invalidate extra info.
1111 SDEI.erase(N);
1112}
1113
1114#ifndef NDEBUG
1115/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1116static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1117 switch (N->getOpcode()) {
1118 default:
1119 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1120 TLI->verifyTargetSDNode(N);
1121 break;
1122 case ISD::BUILD_PAIR: {
1123 EVT VT = N->getValueType(0);
1124 assert(N->getNumValues() == 1 && "Too many results!");
1125 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1126 "Wrong return type!");
1127 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1128 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1129 "Mismatched operand types!");
1130 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1131 "Wrong operand type!");
1132 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1133 "Wrong return type size");
1134 break;
1135 }
1136 case ISD::BUILD_VECTOR: {
1137 assert(N->getNumValues() == 1 && "Too many results!");
1138 assert(N->getValueType(0).isVector() && "Wrong return type!");
1139 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1140 "Wrong number of operands!");
1141 EVT EltVT = N->getValueType(0).getVectorElementType();
1142 for (const SDUse &Op : N->ops()) {
1143 assert((Op.getValueType() == EltVT ||
1144 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1145 EltVT.bitsLE(Op.getValueType()))) &&
1146 "Wrong operand type!");
1147 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1148 "Operands must all have the same type");
1149 }
1150 break;
1151 }
1152 }
1153}
1154#endif // NDEBUG
1155
1156/// Insert a newly allocated node into the DAG.
1157///
1158/// Handles insertion into the all nodes list and CSE map, as well as
1159/// verification and other common operations when a new node is allocated.
1160void SelectionDAG::InsertNode(SDNode *N) {
1161 AllNodes.push_back(N);
1162#ifndef NDEBUG
1163 N->PersistentId = NextPersistentId++;
1164 VerifySDNode(N, TLI);
1165#endif
1166 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1167 DUL->NodeInserted(N);
1168}
1169
1170/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1171/// correspond to it. This is useful when we're about to delete or repurpose
1172/// the node. We don't want future request for structurally identical nodes
1173/// to return N anymore.
1174bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1175 bool Erased = false;
1176 switch (N->getOpcode()) {
1177 case ISD::HANDLENODE: return false; // noop.
1178 case ISD::CONDCODE:
1179 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1180 "Cond code doesn't exist!");
1181 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1182 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1183 break;
1185 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1186 break;
1188 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1189 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1190 ESN->getSymbol(), ESN->getTargetFlags()));
1191 break;
1192 }
1193 case ISD::MCSymbol: {
1194 auto *MCSN = cast<MCSymbolSDNode>(N);
1195 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1196 break;
1197 }
1198 case ISD::VALUETYPE: {
1199 EVT VT = cast<VTSDNode>(N)->getVT();
1200 if (VT.isExtended()) {
1201 Erased = ExtendedValueTypeNodes.erase(VT);
1202 } else {
1203 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1204 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1205 }
1206 break;
1207 }
1208 default:
1209 // Remove it from the CSE Map.
1210 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1211 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1212 Erased = CSEMap.RemoveNode(N);
1213 break;
1214 }
1215#ifndef NDEBUG
1216 // Verify that the node was actually in one of the CSE maps, unless it has a
1217 // glue result (which cannot be CSE'd) or is one of the special cases that are
1218 // not subject to CSE.
1219 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1220 !N->isMachineOpcode() && !doNotCSE(N)) {
1221 N->dump(this);
1222 dbgs() << "\n";
1223 llvm_unreachable("Node is not in map!");
1224 }
1225#endif
1226 return Erased;
1227}
1228
1229/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1230/// maps and modified in place. Add it back to the CSE maps, unless an identical
1231/// node already exists, in which case transfer all its users to the existing
1232/// node. This transfer can potentially trigger recursive merging.
1233void
1234SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1235 // For node types that aren't CSE'd, just act as if no identical node
1236 // already exists.
1237 if (!doNotCSE(N)) {
1238 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1239 if (Existing != N) {
1240 // If there was already an existing matching node, use ReplaceAllUsesWith
1241 // to replace the dead one with the existing one. This can cause
1242 // recursive merging of other unrelated nodes down the line.
1243 Existing->intersectFlagsWith(N->getFlags());
1244 ReplaceAllUsesWith(N, Existing);
1245
1246 // N is now dead. Inform the listeners and delete it.
1247 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1248 DUL->NodeDeleted(N, Existing);
1249 DeleteNodeNotInCSEMaps(N);
1250 return;
1251 }
1252 }
1253
1254 // If the node doesn't already exist, we updated it. Inform listeners.
1255 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1256 DUL->NodeUpdated(N);
1257}
1258
1259/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1260/// were replaced with those specified. If this node is never memoized,
1261/// return null, otherwise return a pointer to the slot it would take. If a
1262/// node already exists with these operands, the slot will be non-null.
1263SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1264 void *&InsertPos) {
1265 if (doNotCSE(N))
1266 return nullptr;
1267
1268 SDValue Ops[] = { Op };
1270 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1272 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1273 if (Node)
1274 Node->intersectFlagsWith(N->getFlags());
1275 return Node;
1276}
1277
1278/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1279/// were replaced with those specified. If this node is never memoized,
1280/// return null, otherwise return a pointer to the slot it would take. If a
1281/// node already exists with these operands, the slot will be non-null.
1282SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1283 SDValue Op1, SDValue Op2,
1284 void *&InsertPos) {
1285 if (doNotCSE(N))
1286 return nullptr;
1287
1288 SDValue Ops[] = { Op1, Op2 };
1290 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1292 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1293 if (Node)
1294 Node->intersectFlagsWith(N->getFlags());
1295 return Node;
1296}
1297
1298/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1299/// were replaced with those specified. If this node is never memoized,
1300/// return null, otherwise return a pointer to the slot it would take. If a
1301/// node already exists with these operands, the slot will be non-null.
1302SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1303 void *&InsertPos) {
1304 if (doNotCSE(N))
1305 return nullptr;
1306
1308 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1310 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1311 if (Node)
1312 Node->intersectFlagsWith(N->getFlags());
1313 return Node;
1314}
1315
1317 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1318 : VT.getTypeForEVT(*getContext());
1319
1320 return getDataLayout().getABITypeAlign(Ty);
1321}
1322
1323// EntryNode could meaningfully have debug info if we can find it...
1325 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1326 getVTList(MVT::Other, MVT::Glue)),
1327 Root(getEntryNode()) {
1328 InsertNode(&EntryNode);
1329 DbgInfo = new SDDbgInfo();
1330}
1331
1333 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1334 const TargetLibraryInfo *LibraryInfo,
1335 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1337 FunctionVarLocs const *VarLocs) {
1338 MF = &NewMF;
1339 SDAGISelPass = PassPtr;
1340 ORE = &NewORE;
1343 LibInfo = LibraryInfo;
1344 Context = &MF->getFunction().getContext();
1345 UA = NewUA;
1346 PSI = PSIin;
1347 BFI = BFIin;
1348 MMI = &MMIin;
1349 FnVarLocs = VarLocs;
1350}
1351
1353 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1354 allnodes_clear();
1355 OperandRecycler.clear(OperandAllocator);
1356 delete DbgInfo;
1357}
1358
1360 return MF->getFunction().hasOptSize() ||
1362}
1363
1364void SelectionDAG::allnodes_clear() {
1365 assert(&*AllNodes.begin() == &EntryNode);
1366 AllNodes.remove(AllNodes.begin());
1367 while (!AllNodes.empty())
1368 DeallocateNode(&AllNodes.front());
1369#ifndef NDEBUG
1370 NextPersistentId = 0;
1371#endif
1372}
1373
1374SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1375 void *&InsertPos) {
1376 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1377 if (N) {
1378 switch (N->getOpcode()) {
1379 default: break;
1380 case ISD::Constant:
1381 case ISD::ConstantFP:
1382 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1383 "debug location. Use another overload.");
1384 }
1385 }
1386 return N;
1387}
1388
1389SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1390 const SDLoc &DL, void *&InsertPos) {
1391 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1392 if (N) {
1393 switch (N->getOpcode()) {
1394 case ISD::Constant:
1395 case ISD::ConstantFP:
1396 // Erase debug location from the node if the node is used at several
1397 // different places. Do not propagate one location to all uses as it
1398 // will cause a worse single stepping debugging experience.
1399 if (N->getDebugLoc() != DL.getDebugLoc())
1400 N->setDebugLoc(DebugLoc());
1401 break;
1402 default:
1403 // When the node's point of use is located earlier in the instruction
1404 // sequence than its prior point of use, update its debug info to the
1405 // earlier location.
1406 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1407 N->setDebugLoc(DL.getDebugLoc());
1408 break;
1409 }
1410 }
1411 return N;
1412}
1413
1415 allnodes_clear();
1416 OperandRecycler.clear(OperandAllocator);
1417 OperandAllocator.Reset();
1418 CSEMap.clear();
1419
1420 ExtendedValueTypeNodes.clear();
1421 ExternalSymbols.clear();
1422 TargetExternalSymbols.clear();
1423 MCSymbols.clear();
1424 SDEI.clear();
1425 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1426 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1427
1428 EntryNode.UseList = nullptr;
1429 InsertNode(&EntryNode);
1430 Root = getEntryNode();
1431 DbgInfo->clear();
1432}
1433
1435 return VT.bitsGT(Op.getValueType())
1436 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1437 : getNode(ISD::FP_ROUND, DL, VT, Op,
1438 getIntPtrConstant(0, DL, /*isTarget=*/true));
1439}
1440
1441std::pair<SDValue, SDValue>
1443 const SDLoc &DL, EVT VT) {
1444 assert(!VT.bitsEq(Op.getValueType()) &&
1445 "Strict no-op FP extend/round not allowed.");
1446 SDValue Res =
1447 VT.bitsGT(Op.getValueType())
1448 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1449 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1450 {Chain, Op, getIntPtrConstant(0, DL)});
1451
1452 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1453}
1454
1456 return VT.bitsGT(Op.getValueType()) ?
1457 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1458 getNode(ISD::TRUNCATE, DL, VT, Op);
1459}
1460
1462 return VT.bitsGT(Op.getValueType()) ?
1463 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1464 getNode(ISD::TRUNCATE, DL, VT, Op);
1465}
1466
1468 return VT.bitsGT(Op.getValueType()) ?
1469 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1470 getNode(ISD::TRUNCATE, DL, VT, Op);
1471}
1472
1474 EVT VT) {
1475 assert(!VT.isVector());
1476 auto Type = Op.getValueType();
1477 SDValue DestOp;
1478 if (Type == VT)
1479 return Op;
1480 auto Size = Op.getValueSizeInBits();
1481 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1482 if (DestOp.getValueType() == VT)
1483 return DestOp;
1484
1485 return getAnyExtOrTrunc(DestOp, DL, VT);
1486}
1487
1489 EVT VT) {
1490 assert(!VT.isVector());
1491 auto Type = Op.getValueType();
1492 SDValue DestOp;
1493 if (Type == VT)
1494 return Op;
1495 auto Size = Op.getValueSizeInBits();
1496 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1497 if (DestOp.getValueType() == VT)
1498 return DestOp;
1499
1500 return getSExtOrTrunc(DestOp, DL, VT);
1501}
1502
1504 EVT VT) {
1505 assert(!VT.isVector());
1506 auto Type = Op.getValueType();
1507 SDValue DestOp;
1508 if (Type == VT)
1509 return Op;
1510 auto Size = Op.getValueSizeInBits();
1511 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1512 if (DestOp.getValueType() == VT)
1513 return DestOp;
1514
1515 return getZExtOrTrunc(DestOp, DL, VT);
1516}
1517
1519 EVT OpVT) {
1520 if (VT.bitsLE(Op.getValueType()))
1521 return getNode(ISD::TRUNCATE, SL, VT, Op);
1522
1524 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1525}
1526
1528 EVT OpVT = Op.getValueType();
1529 assert(VT.isInteger() && OpVT.isInteger() &&
1530 "Cannot getZeroExtendInReg FP types");
1531 assert(VT.isVector() == OpVT.isVector() &&
1532 "getZeroExtendInReg type should be vector iff the operand "
1533 "type is vector!");
1534 assert((!VT.isVector() ||
1536 "Vector element counts must match in getZeroExtendInReg");
1537 assert(VT.bitsLE(OpVT) && "Not extending!");
1538 if (OpVT == VT)
1539 return Op;
1541 VT.getScalarSizeInBits());
1542 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1543}
1544
1546 SDValue EVL, const SDLoc &DL,
1547 EVT VT) {
1548 EVT OpVT = Op.getValueType();
1549 assert(VT.isInteger() && OpVT.isInteger() &&
1550 "Cannot getVPZeroExtendInReg FP types");
1551 assert(VT.isVector() && OpVT.isVector() &&
1552 "getVPZeroExtendInReg type and operand type should be vector!");
1554 "Vector element counts must match in getZeroExtendInReg");
1555 assert(VT.bitsLE(OpVT) && "Not extending!");
1556 if (OpVT == VT)
1557 return Op;
1559 VT.getScalarSizeInBits());
1560 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1561 EVL);
1562}
1563
1565 // Only unsigned pointer semantics are supported right now. In the future this
1566 // might delegate to TLI to check pointer signedness.
1567 return getZExtOrTrunc(Op, DL, VT);
1568}
1569
1571 // Only unsigned pointer semantics are supported right now. In the future this
1572 // might delegate to TLI to check pointer signedness.
1573 return getZeroExtendInReg(Op, DL, VT);
1574}
1575
1577 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1578}
1579
1580/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1582 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1583}
1584
1586 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1587 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1588}
1589
1591 SDValue Mask, SDValue EVL, EVT VT) {
1592 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1593 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1594}
1595
1597 SDValue Mask, SDValue EVL) {
1598 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1599}
1600
1602 SDValue Mask, SDValue EVL) {
1603 if (VT.bitsGT(Op.getValueType()))
1604 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1605 if (VT.bitsLT(Op.getValueType()))
1606 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1607 return Op;
1608}
1609
1611 EVT OpVT) {
1612 if (!V)
1613 return getConstant(0, DL, VT);
1614
1615 switch (TLI->getBooleanContents(OpVT)) {
1618 return getConstant(1, DL, VT);
1620 return getAllOnesConstant(DL, VT);
1621 }
1622 llvm_unreachable("Unexpected boolean content enum!");
1623}
1624
1626 bool isT, bool isO) {
1627 EVT EltVT = VT.getScalarType();
1628 assert((EltVT.getSizeInBits() >= 64 ||
1629 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1630 "getConstant with a uint64_t value that doesn't fit in the type!");
1631 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1632}
1633
1635 bool isT, bool isO) {
1636 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1637}
1638
1640 EVT VT, bool isT, bool isO) {
1641 assert(VT.isInteger() && "Cannot create FP integer constant!");
1642
1643 EVT EltVT = VT.getScalarType();
1644 const ConstantInt *Elt = &Val;
1645
1646 // In some cases the vector type is legal but the element type is illegal and
1647 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1648 // inserted value (the type does not need to match the vector element type).
1649 // Any extra bits introduced will be truncated away.
1650 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1652 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1653 APInt NewVal;
1654 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1655 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1656 else
1657 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1658 Elt = ConstantInt::get(*getContext(), NewVal);
1659 }
1660 // In other cases the element type is illegal and needs to be expanded, for
1661 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1662 // the value into n parts and use a vector type with n-times the elements.
1663 // Then bitcast to the type requested.
1664 // Legalizing constants too early makes the DAGCombiner's job harder so we
1665 // only legalize if the DAG tells us we must produce legal types.
1666 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1667 TLI->getTypeAction(*getContext(), EltVT) ==
1669 const APInt &NewVal = Elt->getValue();
1670 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1671 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1672
1673 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1674 if (VT.isScalableVector() ||
1676 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1677 "Can only handle an even split!");
1678 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1679
1680 SmallVector<SDValue, 2> ScalarParts;
1681 for (unsigned i = 0; i != Parts; ++i)
1682 ScalarParts.push_back(getConstant(
1683 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1684 ViaEltVT, isT, isO));
1685
1686 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1687 }
1688
1689 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1690 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1691
1692 // Check the temporary vector is the correct size. If this fails then
1693 // getTypeToTransformTo() probably returned a type whose size (in bits)
1694 // isn't a power-of-2 factor of the requested type size.
1695 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1696
1697 SmallVector<SDValue, 2> EltParts;
1698 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1699 EltParts.push_back(getConstant(
1700 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1701 ViaEltVT, isT, isO));
1702
1703 // EltParts is currently in little endian order. If we actually want
1704 // big-endian order then reverse it now.
1705 if (getDataLayout().isBigEndian())
1706 std::reverse(EltParts.begin(), EltParts.end());
1707
1708 // The elements must be reversed when the element order is different
1709 // to the endianness of the elements (because the BITCAST is itself a
1710 // vector shuffle in this situation). However, we do not need any code to
1711 // perform this reversal because getConstant() is producing a vector
1712 // splat.
1713 // This situation occurs in MIPS MSA.
1714
1716 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1717 llvm::append_range(Ops, EltParts);
1718
1719 SDValue V =
1720 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1721 return V;
1722 }
1723
1724 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1725 "APInt size does not match type size!");
1726 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1727 SDVTList VTs = getVTList(EltVT);
1729 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1730 ID.AddPointer(Elt);
1731 ID.AddBoolean(isO);
1732 void *IP = nullptr;
1733 SDNode *N = nullptr;
1734 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1735 if (!VT.isVector())
1736 return SDValue(N, 0);
1737
1738 if (!N) {
1739 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1740 CSEMap.InsertNode(N, IP);
1741 InsertNode(N);
1742 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1743 }
1744
1745 SDValue Result(N, 0);
1746 if (VT.isVector())
1747 Result = getSplat(VT, DL, Result);
1748 return Result;
1749}
1750
1752 bool isTarget) {
1753 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1754}
1755
1757 const SDLoc &DL) {
1758 assert(VT.isInteger() && "Shift amount is not an integer type!");
1759 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1760 return getConstant(Val, DL, ShiftVT);
1761}
1762
1764 const SDLoc &DL) {
1765 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1766 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1767}
1768
1770 bool isTarget) {
1771 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1772}
1773
1775 bool isTarget) {
1776 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1777}
1778
1780 EVT VT, bool isTarget) {
1781 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1782
1783 EVT EltVT = VT.getScalarType();
1784
1785 // Do the map lookup using the actual bit pattern for the floating point
1786 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1787 // we don't have issues with SNANs.
1788 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1789 SDVTList VTs = getVTList(EltVT);
1791 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1792 ID.AddPointer(&V);
1793 void *IP = nullptr;
1794 SDNode *N = nullptr;
1795 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1796 if (!VT.isVector())
1797 return SDValue(N, 0);
1798
1799 if (!N) {
1800 N = newSDNode<ConstantFPSDNode>(isTarget, &V, VTs);
1801 CSEMap.InsertNode(N, IP);
1802 InsertNode(N);
1803 }
1804
1805 SDValue Result(N, 0);
1806 if (VT.isVector())
1807 Result = getSplat(VT, DL, Result);
1808 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1809 return Result;
1810}
1811
1813 bool isTarget) {
1814 EVT EltVT = VT.getScalarType();
1815 if (EltVT == MVT::f32)
1816 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1817 if (EltVT == MVT::f64)
1818 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1819 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1820 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1821 bool Ignored;
1822 APFloat APF = APFloat(Val);
1824 &Ignored);
1825 return getConstantFP(APF, DL, VT, isTarget);
1826 }
1827 llvm_unreachable("Unsupported type in getConstantFP");
1828}
1829
1831 EVT VT, int64_t Offset, bool isTargetGA,
1832 unsigned TargetFlags) {
1833 assert((TargetFlags == 0 || isTargetGA) &&
1834 "Cannot set target flags on target-independent globals");
1835
1836 // Truncate (with sign-extension) the offset value to the pointer size.
1838 if (BitWidth < 64)
1840
1841 unsigned Opc;
1842 if (GV->isThreadLocal())
1844 else
1845 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1846
1847 SDVTList VTs = getVTList(VT);
1849 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1850 ID.AddPointer(GV);
1851 ID.AddInteger(Offset);
1852 ID.AddInteger(TargetFlags);
1853 void *IP = nullptr;
1854 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1855 return SDValue(E, 0);
1856
1857 auto *N = newSDNode<GlobalAddressSDNode>(
1858 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1859 CSEMap.InsertNode(N, IP);
1860 InsertNode(N);
1861 return SDValue(N, 0);
1862}
1863
1864SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1865 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1866 SDVTList VTs = getVTList(VT);
1868 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1869 ID.AddInteger(FI);
1870 void *IP = nullptr;
1871 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1872 return SDValue(E, 0);
1873
1874 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1875 CSEMap.InsertNode(N, IP);
1876 InsertNode(N);
1877 return SDValue(N, 0);
1878}
1879
1880SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1881 unsigned TargetFlags) {
1882 assert((TargetFlags == 0 || isTarget) &&
1883 "Cannot set target flags on target-independent jump tables");
1884 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1885 SDVTList VTs = getVTList(VT);
1887 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1888 ID.AddInteger(JTI);
1889 ID.AddInteger(TargetFlags);
1890 void *IP = nullptr;
1891 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1892 return SDValue(E, 0);
1893
1894 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1895 CSEMap.InsertNode(N, IP);
1896 InsertNode(N);
1897 return SDValue(N, 0);
1898}
1899
1901 const SDLoc &DL) {
1903 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1904 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1905}
1906
1908 MaybeAlign Alignment, int Offset,
1909 bool isTarget, unsigned TargetFlags) {
1910 assert((TargetFlags == 0 || isTarget) &&
1911 "Cannot set target flags on target-independent globals");
1912 if (!Alignment)
1913 Alignment = shouldOptForSize()
1914 ? getDataLayout().getABITypeAlign(C->getType())
1915 : getDataLayout().getPrefTypeAlign(C->getType());
1916 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1917 SDVTList VTs = getVTList(VT);
1919 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1920 ID.AddInteger(Alignment->value());
1921 ID.AddInteger(Offset);
1922 ID.AddPointer(C);
1923 ID.AddInteger(TargetFlags);
1924 void *IP = nullptr;
1925 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1926 return SDValue(E, 0);
1927
1928 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1929 TargetFlags);
1930 CSEMap.InsertNode(N, IP);
1931 InsertNode(N);
1932 SDValue V = SDValue(N, 0);
1933 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1934 return V;
1935}
1936
1938 MaybeAlign Alignment, int Offset,
1939 bool isTarget, unsigned TargetFlags) {
1940 assert((TargetFlags == 0 || isTarget) &&
1941 "Cannot set target flags on target-independent globals");
1942 if (!Alignment)
1943 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1944 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1945 SDVTList VTs = getVTList(VT);
1947 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1948 ID.AddInteger(Alignment->value());
1949 ID.AddInteger(Offset);
1950 C->addSelectionDAGCSEId(ID);
1951 ID.AddInteger(TargetFlags);
1952 void *IP = nullptr;
1953 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1954 return SDValue(E, 0);
1955
1956 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1957 TargetFlags);
1958 CSEMap.InsertNode(N, IP);
1959 InsertNode(N);
1960 return SDValue(N, 0);
1961}
1962
1965 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), std::nullopt);
1966 ID.AddPointer(MBB);
1967 void *IP = nullptr;
1968 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1969 return SDValue(E, 0);
1970
1971 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1972 CSEMap.InsertNode(N, IP);
1973 InsertNode(N);
1974 return SDValue(N, 0);
1975}
1976
1978 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1979 ValueTypeNodes.size())
1980 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1981
1982 SDNode *&N = VT.isExtended() ?
1983 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1984
1985 if (N) return SDValue(N, 0);
1986 N = newSDNode<VTSDNode>(VT);
1987 InsertNode(N);
1988 return SDValue(N, 0);
1989}
1990
1992 SDNode *&N = ExternalSymbols[Sym];
1993 if (N) return SDValue(N, 0);
1994 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
1995 InsertNode(N);
1996 return SDValue(N, 0);
1997}
1998
2000 SDNode *&N = MCSymbols[Sym];
2001 if (N)
2002 return SDValue(N, 0);
2003 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2004 InsertNode(N);
2005 return SDValue(N, 0);
2006}
2007
2009 unsigned TargetFlags) {
2010 SDNode *&N =
2011 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2012 if (N) return SDValue(N, 0);
2013 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2014 InsertNode(N);
2015 return SDValue(N, 0);
2016}
2017
2019 if ((unsigned)Cond >= CondCodeNodes.size())
2020 CondCodeNodes.resize(Cond+1);
2021
2022 if (!CondCodeNodes[Cond]) {
2023 auto *N = newSDNode<CondCodeSDNode>(Cond);
2024 CondCodeNodes[Cond] = N;
2025 InsertNode(N);
2026 }
2027
2028 return SDValue(CondCodeNodes[Cond], 0);
2029}
2030
2032 bool ConstantFold) {
2033 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2034 "APInt size does not match type size!");
2035
2036 if (MulImm == 0)
2037 return getConstant(0, DL, VT);
2038
2039 if (ConstantFold) {
2040 const MachineFunction &MF = getMachineFunction();
2041 const Function &F = MF.getFunction();
2042 ConstantRange CR = getVScaleRange(&F, 64);
2043 if (const APInt *C = CR.getSingleElement())
2044 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2045 }
2046
2047 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2048}
2049
2051 bool ConstantFold) {
2052 if (EC.isScalable())
2053 return getVScale(DL, VT,
2054 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2055
2056 return getConstant(EC.getKnownMinValue(), DL, VT);
2057}
2058
2060 APInt One(ResVT.getScalarSizeInBits(), 1);
2061 return getStepVector(DL, ResVT, One);
2062}
2063
2065 const APInt &StepVal) {
2066 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2067 if (ResVT.isScalableVector())
2068 return getNode(
2069 ISD::STEP_VECTOR, DL, ResVT,
2070 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2071
2072 SmallVector<SDValue, 16> OpsStepConstants;
2073 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2074 OpsStepConstants.push_back(
2075 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2076 return getBuildVector(ResVT, DL, OpsStepConstants);
2077}
2078
2079/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2080/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2082 std::swap(N1, N2);
2084}
2085
2087 SDValue N2, ArrayRef<int> Mask) {
2088 assert(VT.getVectorNumElements() == Mask.size() &&
2089 "Must have the same number of vector elements as mask elements!");
2090 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2091 "Invalid VECTOR_SHUFFLE");
2092
2093 // Canonicalize shuffle undef, undef -> undef
2094 if (N1.isUndef() && N2.isUndef())
2095 return getUNDEF(VT);
2096
2097 // Validate that all indices in Mask are within the range of the elements
2098 // input to the shuffle.
2099 int NElts = Mask.size();
2100 assert(llvm::all_of(Mask,
2101 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2102 "Index out of range");
2103
2104 // Copy the mask so we can do any needed cleanup.
2105 SmallVector<int, 8> MaskVec(Mask);
2106
2107 // Canonicalize shuffle v, v -> v, undef
2108 if (N1 == N2) {
2109 N2 = getUNDEF(VT);
2110 for (int i = 0; i != NElts; ++i)
2111 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2112 }
2113
2114 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2115 if (N1.isUndef())
2116 commuteShuffle(N1, N2, MaskVec);
2117
2118 if (TLI->hasVectorBlend()) {
2119 // If shuffling a splat, try to blend the splat instead. We do this here so
2120 // that even when this arises during lowering we don't have to re-handle it.
2121 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2122 BitVector UndefElements;
2123 SDValue Splat = BV->getSplatValue(&UndefElements);
2124 if (!Splat)
2125 return;
2126
2127 for (int i = 0; i < NElts; ++i) {
2128 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2129 continue;
2130
2131 // If this input comes from undef, mark it as such.
2132 if (UndefElements[MaskVec[i] - Offset]) {
2133 MaskVec[i] = -1;
2134 continue;
2135 }
2136
2137 // If we can blend a non-undef lane, use that instead.
2138 if (!UndefElements[i])
2139 MaskVec[i] = i + Offset;
2140 }
2141 };
2142 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2143 BlendSplat(N1BV, 0);
2144 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2145 BlendSplat(N2BV, NElts);
2146 }
2147
2148 // Canonicalize all index into lhs, -> shuffle lhs, undef
2149 // Canonicalize all index into rhs, -> shuffle rhs, undef
2150 bool AllLHS = true, AllRHS = true;
2151 bool N2Undef = N2.isUndef();
2152 for (int i = 0; i != NElts; ++i) {
2153 if (MaskVec[i] >= NElts) {
2154 if (N2Undef)
2155 MaskVec[i] = -1;
2156 else
2157 AllLHS = false;
2158 } else if (MaskVec[i] >= 0) {
2159 AllRHS = false;
2160 }
2161 }
2162 if (AllLHS && AllRHS)
2163 return getUNDEF(VT);
2164 if (AllLHS && !N2Undef)
2165 N2 = getUNDEF(VT);
2166 if (AllRHS) {
2167 N1 = getUNDEF(VT);
2168 commuteShuffle(N1, N2, MaskVec);
2169 }
2170 // Reset our undef status after accounting for the mask.
2171 N2Undef = N2.isUndef();
2172 // Re-check whether both sides ended up undef.
2173 if (N1.isUndef() && N2Undef)
2174 return getUNDEF(VT);
2175
2176 // If Identity shuffle return that node.
2177 bool Identity = true, AllSame = true;
2178 for (int i = 0; i != NElts; ++i) {
2179 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2180 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2181 }
2182 if (Identity && NElts)
2183 return N1;
2184
2185 // Shuffling a constant splat doesn't change the result.
2186 if (N2Undef) {
2187 SDValue V = N1;
2188
2189 // Look through any bitcasts. We check that these don't change the number
2190 // (and size) of elements and just changes their types.
2191 while (V.getOpcode() == ISD::BITCAST)
2192 V = V->getOperand(0);
2193
2194 // A splat should always show up as a build vector node.
2195 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2196 BitVector UndefElements;
2197 SDValue Splat = BV->getSplatValue(&UndefElements);
2198 // If this is a splat of an undef, shuffling it is also undef.
2199 if (Splat && Splat.isUndef())
2200 return getUNDEF(VT);
2201
2202 bool SameNumElts =
2203 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2204
2205 // We only have a splat which can skip shuffles if there is a splatted
2206 // value and no undef lanes rearranged by the shuffle.
2207 if (Splat && UndefElements.none()) {
2208 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2209 // number of elements match or the value splatted is a zero constant.
2210 if (SameNumElts || isNullConstant(Splat))
2211 return N1;
2212 }
2213
2214 // If the shuffle itself creates a splat, build the vector directly.
2215 if (AllSame && SameNumElts) {
2216 EVT BuildVT = BV->getValueType(0);
2217 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2218 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2219
2220 // We may have jumped through bitcasts, so the type of the
2221 // BUILD_VECTOR may not match the type of the shuffle.
2222 if (BuildVT != VT)
2223 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2224 return NewBV;
2225 }
2226 }
2227 }
2228
2229 SDVTList VTs = getVTList(VT);
2231 SDValue Ops[2] = { N1, N2 };
2233 for (int i = 0; i != NElts; ++i)
2234 ID.AddInteger(MaskVec[i]);
2235
2236 void* IP = nullptr;
2237 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2238 return SDValue(E, 0);
2239
2240 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2241 // SDNode doesn't have access to it. This memory will be "leaked" when
2242 // the node is deallocated, but recovered when the NodeAllocator is released.
2243 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2244 llvm::copy(MaskVec, MaskAlloc);
2245
2246 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2247 dl.getDebugLoc(), MaskAlloc);
2248 createOperands(N, Ops);
2249
2250 CSEMap.InsertNode(N, IP);
2251 InsertNode(N);
2252 SDValue V = SDValue(N, 0);
2253 NewSDValueDbgMsg(V, "Creating new node: ", this);
2254 return V;
2255}
2256
2258 EVT VT = SV.getValueType(0);
2259 SmallVector<int, 8> MaskVec(SV.getMask());
2261
2262 SDValue Op0 = SV.getOperand(0);
2263 SDValue Op1 = SV.getOperand(1);
2264 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2265}
2266
2268 SDVTList VTs = getVTList(VT);
2270 AddNodeIDNode(ID, ISD::Register, VTs, std::nullopt);
2271 ID.AddInteger(RegNo);
2272 void *IP = nullptr;
2273 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2274 return SDValue(E, 0);
2275
2276 auto *N = newSDNode<RegisterSDNode>(RegNo, VTs);
2277 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2278 CSEMap.InsertNode(N, IP);
2279 InsertNode(N);
2280 return SDValue(N, 0);
2281}
2282
2285 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), std::nullopt);
2286 ID.AddPointer(RegMask);
2287 void *IP = nullptr;
2288 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2289 return SDValue(E, 0);
2290
2291 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2292 CSEMap.InsertNode(N, IP);
2293 InsertNode(N);
2294 return SDValue(N, 0);
2295}
2296
2298 MCSymbol *Label) {
2299 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2300}
2301
2302SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2303 SDValue Root, MCSymbol *Label) {
2305 SDValue Ops[] = { Root };
2306 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2307 ID.AddPointer(Label);
2308 void *IP = nullptr;
2309 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2310 return SDValue(E, 0);
2311
2312 auto *N =
2313 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2314 createOperands(N, Ops);
2315
2316 CSEMap.InsertNode(N, IP);
2317 InsertNode(N);
2318 return SDValue(N, 0);
2319}
2320
2322 int64_t Offset, bool isTarget,
2323 unsigned TargetFlags) {
2324 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2325 SDVTList VTs = getVTList(VT);
2326
2328 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
2329 ID.AddPointer(BA);
2330 ID.AddInteger(Offset);
2331 ID.AddInteger(TargetFlags);
2332 void *IP = nullptr;
2333 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2334 return SDValue(E, 0);
2335
2336 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2337 CSEMap.InsertNode(N, IP);
2338 InsertNode(N);
2339 return SDValue(N, 0);
2340}
2341
2344 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), std::nullopt);
2345 ID.AddPointer(V);
2346
2347 void *IP = nullptr;
2348 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2349 return SDValue(E, 0);
2350
2351 auto *N = newSDNode<SrcValueSDNode>(V);
2352 CSEMap.InsertNode(N, IP);
2353 InsertNode(N);
2354 return SDValue(N, 0);
2355}
2356
2359 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), std::nullopt);
2360 ID.AddPointer(MD);
2361
2362 void *IP = nullptr;
2363 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2364 return SDValue(E, 0);
2365
2366 auto *N = newSDNode<MDNodeSDNode>(MD);
2367 CSEMap.InsertNode(N, IP);
2368 InsertNode(N);
2369 return SDValue(N, 0);
2370}
2371
2373 if (VT == V.getValueType())
2374 return V;
2375
2376 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2377}
2378
2380 unsigned SrcAS, unsigned DestAS) {
2381 SDVTList VTs = getVTList(VT);
2382 SDValue Ops[] = {Ptr};
2385 ID.AddInteger(SrcAS);
2386 ID.AddInteger(DestAS);
2387
2388 void *IP = nullptr;
2389 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2390 return SDValue(E, 0);
2391
2392 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2393 VTs, SrcAS, DestAS);
2394 createOperands(N, Ops);
2395
2396 CSEMap.InsertNode(N, IP);
2397 InsertNode(N);
2398 return SDValue(N, 0);
2399}
2400
2402 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2403}
2404
2405/// getShiftAmountOperand - Return the specified value casted to
2406/// the target's desired shift amount type.
2408 EVT OpTy = Op.getValueType();
2409 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2410 if (OpTy == ShTy || OpTy.isVector()) return Op;
2411
2412 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2413}
2414
2416 SDLoc dl(Node);
2418 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2419 EVT VT = Node->getValueType(0);
2420 SDValue Tmp1 = Node->getOperand(0);
2421 SDValue Tmp2 = Node->getOperand(1);
2422 const MaybeAlign MA(Node->getConstantOperandVal(3));
2423
2424 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2425 Tmp2, MachinePointerInfo(V));
2426 SDValue VAList = VAListLoad;
2427
2428 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2429 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2430 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2431
2432 VAList =
2433 getNode(ISD::AND, dl, VAList.getValueType(), VAList,
2434 getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2435 }
2436
2437 // Increment the pointer, VAList, to the next vaarg
2438 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2439 getConstant(getDataLayout().getTypeAllocSize(
2440 VT.getTypeForEVT(*getContext())),
2441 dl, VAList.getValueType()));
2442 // Store the incremented VAList to the legalized pointer
2443 Tmp1 =
2444 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2445 // Load the actual argument out of the pointer VAList
2446 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2447}
2448
2450 SDLoc dl(Node);
2452 // This defaults to loading a pointer from the input and storing it to the
2453 // output, returning the chain.
2454 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2455 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2456 SDValue Tmp1 =
2457 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2458 Node->getOperand(2), MachinePointerInfo(VS));
2459 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2460 MachinePointerInfo(VD));
2461}
2462
2464 const DataLayout &DL = getDataLayout();
2465 Type *Ty = VT.getTypeForEVT(*getContext());
2466 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2467
2468 if (TLI->isTypeLegal(VT) || !VT.isVector())
2469 return RedAlign;
2470
2472 const Align StackAlign = TFI->getStackAlign();
2473
2474 // See if we can choose a smaller ABI alignment in cases where it's an
2475 // illegal vector type that will get broken down.
2476 if (RedAlign > StackAlign) {
2477 EVT IntermediateVT;
2478 MVT RegisterVT;
2479 unsigned NumIntermediates;
2480 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2481 NumIntermediates, RegisterVT);
2482 Ty = IntermediateVT.getTypeForEVT(*getContext());
2483 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2484 if (RedAlign2 < RedAlign)
2485 RedAlign = RedAlign2;
2486
2487 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2488 // If the stack is not realignable, the alignment should be limited to the
2489 // StackAlignment
2490 RedAlign = std::min(RedAlign, StackAlign);
2491 }
2492
2493 return RedAlign;
2494}
2495
2497 MachineFrameInfo &MFI = MF->getFrameInfo();
2499 int StackID = 0;
2500 if (Bytes.isScalable())
2501 StackID = TFI->getStackIDForScalableVectors();
2502 // The stack id gives an indication of whether the object is scalable or
2503 // not, so it's safe to pass in the minimum size here.
2504 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2505 false, nullptr, StackID);
2506 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2507}
2508
2510 Type *Ty = VT.getTypeForEVT(*getContext());
2511 Align StackAlign =
2512 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2513 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2514}
2515
2517 TypeSize VT1Size = VT1.getStoreSize();
2518 TypeSize VT2Size = VT2.getStoreSize();
2519 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2520 "Don't know how to choose the maximum size when creating a stack "
2521 "temporary");
2522 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2523 ? VT1Size
2524 : VT2Size;
2525
2526 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2527 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2528 const DataLayout &DL = getDataLayout();
2529 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2530 return CreateStackTemporary(Bytes, Align);
2531}
2532
2534 ISD::CondCode Cond, const SDLoc &dl) {
2535 EVT OpVT = N1.getValueType();
2536
2537 auto GetUndefBooleanConstant = [&]() {
2538 if (VT.getScalarType() == MVT::i1 ||
2539 TLI->getBooleanContents(OpVT) ==
2541 return getUNDEF(VT);
2542 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2543 // so we cannot use getUNDEF(). Return zero instead.
2544 return getConstant(0, dl, VT);
2545 };
2546
2547 // These setcc operations always fold.
2548 switch (Cond) {
2549 default: break;
2550 case ISD::SETFALSE:
2551 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2552 case ISD::SETTRUE:
2553 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2554
2555 case ISD::SETOEQ:
2556 case ISD::SETOGT:
2557 case ISD::SETOGE:
2558 case ISD::SETOLT:
2559 case ISD::SETOLE:
2560 case ISD::SETONE:
2561 case ISD::SETO:
2562 case ISD::SETUO:
2563 case ISD::SETUEQ:
2564 case ISD::SETUNE:
2565 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2566 break;
2567 }
2568
2569 if (OpVT.isInteger()) {
2570 // For EQ and NE, we can always pick a value for the undef to make the
2571 // predicate pass or fail, so we can return undef.
2572 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2573 // icmp eq/ne X, undef -> undef.
2574 if ((N1.isUndef() || N2.isUndef()) &&
2575 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2576 return GetUndefBooleanConstant();
2577
2578 // If both operands are undef, we can return undef for int comparison.
2579 // icmp undef, undef -> undef.
2580 if (N1.isUndef() && N2.isUndef())
2581 return GetUndefBooleanConstant();
2582
2583 // icmp X, X -> true/false
2584 // icmp X, undef -> true/false because undef could be X.
2585 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2586 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2587 }
2588
2589 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2590 const APInt &C2 = N2C->getAPIntValue();
2591 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2592 const APInt &C1 = N1C->getAPIntValue();
2593
2595 dl, VT, OpVT);
2596 }
2597 }
2598
2599 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2600 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2601
2602 if (N1CFP && N2CFP) {
2603 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2604 switch (Cond) {
2605 default: break;
2606 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2607 return GetUndefBooleanConstant();
2608 [[fallthrough]];
2609 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2610 OpVT);
2611 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2612 return GetUndefBooleanConstant();
2613 [[fallthrough]];
2615 R==APFloat::cmpLessThan, dl, VT,
2616 OpVT);
2617 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2618 return GetUndefBooleanConstant();
2619 [[fallthrough]];
2620 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2621 OpVT);
2622 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2623 return GetUndefBooleanConstant();
2624 [[fallthrough]];
2626 VT, OpVT);
2627 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2628 return GetUndefBooleanConstant();
2629 [[fallthrough]];
2631 R==APFloat::cmpEqual, dl, VT,
2632 OpVT);
2633 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2634 return GetUndefBooleanConstant();
2635 [[fallthrough]];
2637 R==APFloat::cmpEqual, dl, VT, OpVT);
2638 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2639 OpVT);
2640 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2641 OpVT);
2643 R==APFloat::cmpEqual, dl, VT,
2644 OpVT);
2645 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2646 OpVT);
2648 R==APFloat::cmpLessThan, dl, VT,
2649 OpVT);
2651 R==APFloat::cmpUnordered, dl, VT,
2652 OpVT);
2654 VT, OpVT);
2655 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2656 OpVT);
2657 }
2658 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2659 // Ensure that the constant occurs on the RHS.
2661 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2662 return SDValue();
2663 return getSetCC(dl, VT, N2, N1, SwappedCond);
2664 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2665 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2666 // If an operand is known to be a nan (or undef that could be a nan), we can
2667 // fold it.
2668 // Choosing NaN for the undef will always make unordered comparison succeed
2669 // and ordered comparison fails.
2670 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2671 switch (ISD::getUnorderedFlavor(Cond)) {
2672 default:
2673 llvm_unreachable("Unknown flavor!");
2674 case 0: // Known false.
2675 return getBoolConstant(false, dl, VT, OpVT);
2676 case 1: // Known true.
2677 return getBoolConstant(true, dl, VT, OpVT);
2678 case 2: // Undefined.
2679 return GetUndefBooleanConstant();
2680 }
2681 }
2682
2683 // Could not fold it.
2684 return SDValue();
2685}
2686
2687/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2688/// use this predicate to simplify operations downstream.
2690 unsigned BitWidth = Op.getScalarValueSizeInBits();
2692}
2693
2694/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2695/// this predicate to simplify operations downstream. Mask is known to be zero
2696/// for bits that V cannot have.
2698 unsigned Depth) const {
2699 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2700}
2701
2702/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2703/// DemandedElts. We use this predicate to simplify operations downstream.
2704/// Mask is known to be zero for bits that V cannot have.
2706 const APInt &DemandedElts,
2707 unsigned Depth) const {
2708 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2709}
2710
2711/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2712/// DemandedElts. We use this predicate to simplify operations downstream.
2714 unsigned Depth /* = 0 */) const {
2715 return computeKnownBits(V, DemandedElts, Depth).isZero();
2716}
2717
2718/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2720 unsigned Depth) const {
2721 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2722}
2723
2725 const APInt &DemandedElts,
2726 unsigned Depth) const {
2727 EVT VT = Op.getValueType();
2728 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2729
2730 unsigned NumElts = VT.getVectorNumElements();
2731 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2732
2733 APInt KnownZeroElements = APInt::getZero(NumElts);
2734 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2735 if (!DemandedElts[EltIdx])
2736 continue; // Don't query elements that are not demanded.
2737 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2738 if (MaskedVectorIsZero(Op, Mask, Depth))
2739 KnownZeroElements.setBit(EltIdx);
2740 }
2741 return KnownZeroElements;
2742}
2743
2744/// isSplatValue - Return true if the vector V has the same value
2745/// across all DemandedElts. For scalable vectors, we don't know the
2746/// number of lanes at compile time. Instead, we use a 1 bit APInt
2747/// to represent a conservative value for all lanes; that is, that
2748/// one bit value is implicitly splatted across all lanes.
2749bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2750 APInt &UndefElts, unsigned Depth) const {
2751 unsigned Opcode = V.getOpcode();
2752 EVT VT = V.getValueType();
2753 assert(VT.isVector() && "Vector type expected");
2754 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2755 "scalable demanded bits are ignored");
2756
2757 if (!DemandedElts)
2758 return false; // No demanded elts, better to assume we don't know anything.
2759
2760 if (Depth >= MaxRecursionDepth)
2761 return false; // Limit search depth.
2762
2763 // Deal with some common cases here that work for both fixed and scalable
2764 // vector types.
2765 switch (Opcode) {
2766 case ISD::SPLAT_VECTOR:
2767 UndefElts = V.getOperand(0).isUndef()
2768 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2769 : APInt(DemandedElts.getBitWidth(), 0);
2770 return true;
2771 case ISD::ADD:
2772 case ISD::SUB:
2773 case ISD::AND:
2774 case ISD::XOR:
2775 case ISD::OR: {
2776 APInt UndefLHS, UndefRHS;
2777 SDValue LHS = V.getOperand(0);
2778 SDValue RHS = V.getOperand(1);
2779 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2780 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2781 UndefElts = UndefLHS | UndefRHS;
2782 return true;
2783 }
2784 return false;
2785 }
2786 case ISD::ABS:
2787 case ISD::TRUNCATE:
2788 case ISD::SIGN_EXTEND:
2789 case ISD::ZERO_EXTEND:
2790 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2791 default:
2792 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2793 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2794 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2795 Depth);
2796 break;
2797}
2798
2799 // We don't support other cases than those above for scalable vectors at
2800 // the moment.
2801 if (VT.isScalableVector())
2802 return false;
2803
2804 unsigned NumElts = VT.getVectorNumElements();
2805 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2806 UndefElts = APInt::getZero(NumElts);
2807
2808 switch (Opcode) {
2809 case ISD::BUILD_VECTOR: {
2810 SDValue Scl;
2811 for (unsigned i = 0; i != NumElts; ++i) {
2812 SDValue Op = V.getOperand(i);
2813 if (Op.isUndef()) {
2814 UndefElts.setBit(i);
2815 continue;
2816 }
2817 if (!DemandedElts[i])
2818 continue;
2819 if (Scl && Scl != Op)
2820 return false;
2821 Scl = Op;
2822 }
2823 return true;
2824 }
2825 case ISD::VECTOR_SHUFFLE: {
2826 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2827 APInt DemandedLHS = APInt::getZero(NumElts);
2828 APInt DemandedRHS = APInt::getZero(NumElts);
2829 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2830 for (int i = 0; i != (int)NumElts; ++i) {
2831 int M = Mask[i];
2832 if (M < 0) {
2833 UndefElts.setBit(i);
2834 continue;
2835 }
2836 if (!DemandedElts[i])
2837 continue;
2838 if (M < (int)NumElts)
2839 DemandedLHS.setBit(M);
2840 else
2841 DemandedRHS.setBit(M - NumElts);
2842 }
2843
2844 // If we aren't demanding either op, assume there's no splat.
2845 // If we are demanding both ops, assume there's no splat.
2846 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2847 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2848 return false;
2849
2850 // See if the demanded elts of the source op is a splat or we only demand
2851 // one element, which should always be a splat.
2852 // TODO: Handle source ops splats with undefs.
2853 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2854 APInt SrcUndefs;
2855 return (SrcElts.popcount() == 1) ||
2856 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2857 (SrcElts & SrcUndefs).isZero());
2858 };
2859 if (!DemandedLHS.isZero())
2860 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2861 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2862 }
2864 // Offset the demanded elts by the subvector index.
2865 SDValue Src = V.getOperand(0);
2866 // We don't support scalable vectors at the moment.
2867 if (Src.getValueType().isScalableVector())
2868 return false;
2869 uint64_t Idx = V.getConstantOperandVal(1);
2870 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2871 APInt UndefSrcElts;
2872 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2873 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2874 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2875 return true;
2876 }
2877 break;
2878 }
2882 // Widen the demanded elts by the src element count.
2883 SDValue Src = V.getOperand(0);
2884 // We don't support scalable vectors at the moment.
2885 if (Src.getValueType().isScalableVector())
2886 return false;
2887 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2888 APInt UndefSrcElts;
2889 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2890 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2891 UndefElts = UndefSrcElts.trunc(NumElts);
2892 return true;
2893 }
2894 break;
2895 }
2896 case ISD::BITCAST: {
2897 SDValue Src = V.getOperand(0);
2898 EVT SrcVT = Src.getValueType();
2899 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2900 unsigned BitWidth = VT.getScalarSizeInBits();
2901
2902 // Ignore bitcasts from unsupported types.
2903 // TODO: Add fp support?
2904 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2905 break;
2906
2907 // Bitcast 'small element' vector to 'large element' vector.
2908 if ((BitWidth % SrcBitWidth) == 0) {
2909 // See if each sub element is a splat.
2910 unsigned Scale = BitWidth / SrcBitWidth;
2911 unsigned NumSrcElts = SrcVT.getVectorNumElements();
2912 APInt ScaledDemandedElts =
2913 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
2914 for (unsigned I = 0; I != Scale; ++I) {
2915 APInt SubUndefElts;
2916 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
2917 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
2918 SubDemandedElts &= ScaledDemandedElts;
2919 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
2920 return false;
2921 // TODO: Add support for merging sub undef elements.
2922 if (!SubUndefElts.isZero())
2923 return false;
2924 }
2925 return true;
2926 }
2927 break;
2928 }
2929 }
2930
2931 return false;
2932}
2933
2934/// Helper wrapper to main isSplatValue function.
2935bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
2936 EVT VT = V.getValueType();
2937 assert(VT.isVector() && "Vector type expected");
2938
2939 APInt UndefElts;
2940 // Since the number of lanes in a scalable vector is unknown at compile time,
2941 // we track one bit which is implicitly broadcast to all lanes. This means
2942 // that all lanes in a scalable vector are considered demanded.
2943 APInt DemandedElts
2945 return isSplatValue(V, DemandedElts, UndefElts) &&
2946 (AllowUndefs || !UndefElts);
2947}
2948
2951
2952 EVT VT = V.getValueType();
2953 unsigned Opcode = V.getOpcode();
2954 switch (Opcode) {
2955 default: {
2956 APInt UndefElts;
2957 // Since the number of lanes in a scalable vector is unknown at compile time,
2958 // we track one bit which is implicitly broadcast to all lanes. This means
2959 // that all lanes in a scalable vector are considered demanded.
2960 APInt DemandedElts
2962
2963 if (isSplatValue(V, DemandedElts, UndefElts)) {
2964 if (VT.isScalableVector()) {
2965 // DemandedElts and UndefElts are ignored for scalable vectors, since
2966 // the only supported cases are SPLAT_VECTOR nodes.
2967 SplatIdx = 0;
2968 } else {
2969 // Handle case where all demanded elements are UNDEF.
2970 if (DemandedElts.isSubsetOf(UndefElts)) {
2971 SplatIdx = 0;
2972 return getUNDEF(VT);
2973 }
2974 SplatIdx = (UndefElts & DemandedElts).countr_one();
2975 }
2976 return V;
2977 }
2978 break;
2979 }
2980 case ISD::SPLAT_VECTOR:
2981 SplatIdx = 0;
2982 return V;
2983 case ISD::VECTOR_SHUFFLE: {
2984 assert(!VT.isScalableVector());
2985 // Check if this is a shuffle node doing a splat.
2986 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
2987 // getTargetVShiftNode currently struggles without the splat source.
2988 auto *SVN = cast<ShuffleVectorSDNode>(V);
2989 if (!SVN->isSplat())
2990 break;
2991 int Idx = SVN->getSplatIndex();
2992 int NumElts = V.getValueType().getVectorNumElements();
2993 SplatIdx = Idx % NumElts;
2994 return V.getOperand(Idx / NumElts);
2995 }
2996 }
2997
2998 return SDValue();
2999}
3000
3002 int SplatIdx;
3003 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3004 EVT SVT = SrcVector.getValueType().getScalarType();
3005 EVT LegalSVT = SVT;
3006 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3007 if (!SVT.isInteger())
3008 return SDValue();
3009 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3010 if (LegalSVT.bitsLT(SVT))
3011 return SDValue();
3012 }
3013 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
3014 getVectorIdxConstant(SplatIdx, SDLoc(V)));
3015 }
3016 return SDValue();
3017}
3018
3019std::optional<ConstantRange>
3021 unsigned Depth) const {
3022 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3023 V.getOpcode() == ISD::SRA) &&
3024 "Unknown shift node");
3025 // Shifting more than the bitwidth is not valid.
3026 unsigned BitWidth = V.getScalarValueSizeInBits();
3027
3028 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3029 const APInt &ShAmt = Cst->getAPIntValue();
3030 if (ShAmt.uge(BitWidth))
3031 return std::nullopt;
3032 return ConstantRange(ShAmt);
3033 }
3034
3035 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3036 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3037 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3038 if (!DemandedElts[i])
3039 continue;
3040 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3041 if (!SA) {
3042 MinAmt = MaxAmt = nullptr;
3043 break;
3044 }
3045 const APInt &ShAmt = SA->getAPIntValue();
3046 if (ShAmt.uge(BitWidth))
3047 return std::nullopt;
3048 if (!MinAmt || MinAmt->ugt(ShAmt))
3049 MinAmt = &ShAmt;
3050 if (!MaxAmt || MaxAmt->ult(ShAmt))
3051 MaxAmt = &ShAmt;
3052 }
3053 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3054 "Failed to find matching min/max shift amounts");
3055 if (MinAmt && MaxAmt)
3056 return ConstantRange(*MinAmt, *MaxAmt + 1);
3057 }
3058
3059 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3060 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3061 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3062 if (KnownAmt.getMaxValue().ult(BitWidth))
3063 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3064
3065 return std::nullopt;
3066}
3067
3068std::optional<uint64_t>
3070 unsigned Depth) const {
3071 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3072 V.getOpcode() == ISD::SRA) &&
3073 "Unknown shift node");
3074 if (std::optional<ConstantRange> AmtRange =
3075 getValidShiftAmountRange(V, DemandedElts, Depth))
3076 if (const APInt *ShAmt = AmtRange->getSingleElement())
3077 return ShAmt->getZExtValue();
3078 return std::nullopt;
3079}
3080
3081std::optional<uint64_t>
3083 EVT VT = V.getValueType();
3084 APInt DemandedElts = VT.isFixedLengthVector()
3086 : APInt(1, 1);
3087 return getValidShiftAmount(V, DemandedElts, Depth);
3088}
3089
3090std::optional<uint64_t>
3092 unsigned Depth) const {
3093 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3094 V.getOpcode() == ISD::SRA) &&
3095 "Unknown shift node");
3096 if (std::optional<ConstantRange> AmtRange =
3097 getValidShiftAmountRange(V, DemandedElts, Depth))
3098 return AmtRange->getUnsignedMin().getZExtValue();
3099 return std::nullopt;
3100}
3101
3102std::optional<uint64_t>
3104 EVT VT = V.getValueType();
3105 APInt DemandedElts = VT.isFixedLengthVector()
3107 : APInt(1, 1);
3108 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3109}
3110
3111std::optional<uint64_t>
3113 unsigned Depth) const {
3114 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3115 V.getOpcode() == ISD::SRA) &&
3116 "Unknown shift node");
3117 if (std::optional<ConstantRange> AmtRange =
3118 getValidShiftAmountRange(V, DemandedElts, Depth))
3119 return AmtRange->getUnsignedMax().getZExtValue();
3120 return std::nullopt;
3121}
3122
3123std::optional<uint64_t>
3125 EVT VT = V.getValueType();
3126 APInt DemandedElts = VT.isFixedLengthVector()
3128 : APInt(1, 1);
3129 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3130}
3131
3132/// Determine which bits of Op are known to be either zero or one and return
3133/// them in Known. For vectors, the known bits are those that are shared by
3134/// every vector element.
3136 EVT VT = Op.getValueType();
3137
3138 // Since the number of lanes in a scalable vector is unknown at compile time,
3139 // we track one bit which is implicitly broadcast to all lanes. This means
3140 // that all lanes in a scalable vector are considered demanded.
3141 APInt DemandedElts = VT.isFixedLengthVector()
3143 : APInt(1, 1);
3144 return computeKnownBits(Op, DemandedElts, Depth);
3145}
3146
3147/// Determine which bits of Op are known to be either zero or one and return
3148/// them in Known. The DemandedElts argument allows us to only collect the known
3149/// bits that are shared by the requested vector elements.
3151 unsigned Depth) const {
3152 unsigned BitWidth = Op.getScalarValueSizeInBits();
3153
3154 KnownBits Known(BitWidth); // Don't know anything.
3155
3156 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3157 // We know all of the bits for a constant!
3158 return KnownBits::makeConstant(C->getAPIntValue());
3159 }
3160 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3161 // We know all of the bits for a constant fp!
3162 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3163 }
3164
3165 if (Depth >= MaxRecursionDepth)
3166 return Known; // Limit search depth.
3167
3168 KnownBits Known2;
3169 unsigned NumElts = DemandedElts.getBitWidth();
3170 assert((!Op.getValueType().isFixedLengthVector() ||
3171 NumElts == Op.getValueType().getVectorNumElements()) &&
3172 "Unexpected vector size");
3173
3174 if (!DemandedElts)
3175 return Known; // No demanded elts, better to assume we don't know anything.
3176
3177 unsigned Opcode = Op.getOpcode();
3178 switch (Opcode) {
3179 case ISD::MERGE_VALUES:
3180 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3181 Depth + 1);
3182 case ISD::SPLAT_VECTOR: {
3183 SDValue SrcOp = Op.getOperand(0);
3184 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3185 "Expected SPLAT_VECTOR implicit truncation");
3186 // Implicitly truncate the bits to match the official semantics of
3187 // SPLAT_VECTOR.
3188 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3189 break;
3190 }
3192 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3193 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3194 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3195 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3196 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3197 }
3198 break;
3199 }
3200 case ISD::STEP_VECTOR: {
3201 const APInt &Step = Op.getConstantOperandAPInt(0);
3202
3203 if (Step.isPowerOf2())
3204 Known.Zero.setLowBits(Step.logBase2());
3205
3207
3208 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3209 break;
3210 const APInt MinNumElts =
3211 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3212
3213 bool Overflow;
3214 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3216 .umul_ov(MinNumElts, Overflow);
3217 if (Overflow)
3218 break;
3219
3220 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3221 if (Overflow)
3222 break;
3223
3224 Known.Zero.setHighBits(MaxValue.countl_zero());
3225 break;
3226 }
3227 case ISD::BUILD_VECTOR:
3228 assert(!Op.getValueType().isScalableVector());
3229 // Collect the known bits that are shared by every demanded vector element.
3230 Known.Zero.setAllBits(); Known.One.setAllBits();
3231 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3232 if (!DemandedElts[i])
3233 continue;
3234
3235 SDValue SrcOp = Op.getOperand(i);
3236 Known2 = computeKnownBits(SrcOp, Depth + 1);
3237
3238 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3239 if (SrcOp.getValueSizeInBits() != BitWidth) {
3240 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3241 "Expected BUILD_VECTOR implicit truncation");
3242 Known2 = Known2.trunc(BitWidth);
3243 }
3244
3245 // Known bits are the values that are shared by every demanded element.
3246 Known = Known.intersectWith(Known2);
3247
3248 // If we don't know any bits, early out.
3249 if (Known.isUnknown())
3250 break;
3251 }
3252 break;
3253 case ISD::VECTOR_SHUFFLE: {
3254 assert(!Op.getValueType().isScalableVector());
3255 // Collect the known bits that are shared by every vector element referenced
3256 // by the shuffle.
3257 APInt DemandedLHS, DemandedRHS;
3258 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3259 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3260 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3261 DemandedLHS, DemandedRHS))
3262 break;
3263
3264 // Known bits are the values that are shared by every demanded element.
3265 Known.Zero.setAllBits(); Known.One.setAllBits();
3266 if (!!DemandedLHS) {
3267 SDValue LHS = Op.getOperand(0);
3268 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3269 Known = Known.intersectWith(Known2);
3270 }
3271 // If we don't know any bits, early out.
3272 if (Known.isUnknown())
3273 break;
3274 if (!!DemandedRHS) {
3275 SDValue RHS = Op.getOperand(1);
3276 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3277 Known = Known.intersectWith(Known2);
3278 }
3279 break;
3280 }
3281 case ISD::VSCALE: {
3283 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3284 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3285 break;
3286 }
3287 case ISD::CONCAT_VECTORS: {
3288 if (Op.getValueType().isScalableVector())
3289 break;
3290 // Split DemandedElts and test each of the demanded subvectors.
3291 Known.Zero.setAllBits(); Known.One.setAllBits();
3292 EVT SubVectorVT = Op.getOperand(0).getValueType();
3293 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3294 unsigned NumSubVectors = Op.getNumOperands();
3295 for (unsigned i = 0; i != NumSubVectors; ++i) {
3296 APInt DemandedSub =
3297 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3298 if (!!DemandedSub) {
3299 SDValue Sub = Op.getOperand(i);
3300 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3301 Known = Known.intersectWith(Known2);
3302 }
3303 // If we don't know any bits, early out.
3304 if (Known.isUnknown())
3305 break;
3306 }
3307 break;
3308 }
3309 case ISD::INSERT_SUBVECTOR: {
3310 if (Op.getValueType().isScalableVector())
3311 break;
3312 // Demand any elements from the subvector and the remainder from the src its
3313 // inserted into.
3314 SDValue Src = Op.getOperand(0);
3315 SDValue Sub = Op.getOperand(1);
3316 uint64_t Idx = Op.getConstantOperandVal(2);
3317 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3318 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3319 APInt DemandedSrcElts = DemandedElts;
3320 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3321
3322 Known.One.setAllBits();
3323 Known.Zero.setAllBits();
3324 if (!!DemandedSubElts) {
3325 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3326 if (Known.isUnknown())
3327 break; // early-out.
3328 }
3329 if (!!DemandedSrcElts) {
3330 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3331 Known = Known.intersectWith(Known2);
3332 }
3333 break;
3334 }
3336 // Offset the demanded elts by the subvector index.
3337 SDValue Src = Op.getOperand(0);
3338 // Bail until we can represent demanded elements for scalable vectors.
3339 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3340 break;
3341 uint64_t Idx = Op.getConstantOperandVal(1);
3342 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3343 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3344 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3345 break;
3346 }
3347 case ISD::SCALAR_TO_VECTOR: {
3348 if (Op.getValueType().isScalableVector())
3349 break;
3350 // We know about scalar_to_vector as much as we know about it source,
3351 // which becomes the first element of otherwise unknown vector.
3352 if (DemandedElts != 1)
3353 break;
3354
3355 SDValue N0 = Op.getOperand(0);
3356 Known = computeKnownBits(N0, Depth + 1);
3357 if (N0.getValueSizeInBits() != BitWidth)
3358 Known = Known.trunc(BitWidth);
3359
3360 break;
3361 }
3362 case ISD::BITCAST: {
3363 if (Op.getValueType().isScalableVector())
3364 break;
3365
3366 SDValue N0 = Op.getOperand(0);
3367 EVT SubVT = N0.getValueType();
3368 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3369
3370 // Ignore bitcasts from unsupported types.
3371 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3372 break;
3373
3374 // Fast handling of 'identity' bitcasts.
3375 if (BitWidth == SubBitWidth) {
3376 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3377 break;
3378 }
3379
3380 bool IsLE = getDataLayout().isLittleEndian();
3381
3382 // Bitcast 'small element' vector to 'large element' scalar/vector.
3383 if ((BitWidth % SubBitWidth) == 0) {
3384 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3385
3386 // Collect known bits for the (larger) output by collecting the known
3387 // bits from each set of sub elements and shift these into place.
3388 // We need to separately call computeKnownBits for each set of
3389 // sub elements as the knownbits for each is likely to be different.
3390 unsigned SubScale = BitWidth / SubBitWidth;
3391 APInt SubDemandedElts(NumElts * SubScale, 0);
3392 for (unsigned i = 0; i != NumElts; ++i)
3393 if (DemandedElts[i])
3394 SubDemandedElts.setBit(i * SubScale);
3395
3396 for (unsigned i = 0; i != SubScale; ++i) {
3397 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3398 Depth + 1);
3399 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3400 Known.insertBits(Known2, SubBitWidth * Shifts);
3401 }
3402 }
3403
3404 // Bitcast 'large element' scalar/vector to 'small element' vector.
3405 if ((SubBitWidth % BitWidth) == 0) {
3406 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3407
3408 // Collect known bits for the (smaller) output by collecting the known
3409 // bits from the overlapping larger input elements and extracting the
3410 // sub sections we actually care about.
3411 unsigned SubScale = SubBitWidth / BitWidth;
3412 APInt SubDemandedElts =
3413 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3414 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3415
3416 Known.Zero.setAllBits(); Known.One.setAllBits();
3417 for (unsigned i = 0; i != NumElts; ++i)
3418 if (DemandedElts[i]) {
3419 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3420 unsigned Offset = (Shifts % SubScale) * BitWidth;
3421 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3422 // If we don't know any bits, early out.
3423 if (Known.isUnknown())
3424 break;
3425 }
3426 }
3427 break;
3428 }
3429 case ISD::AND:
3430 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3431 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3432
3433 Known &= Known2;
3434 break;
3435 case ISD::OR:
3436 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3437 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3438
3439 Known |= Known2;
3440 break;
3441 case ISD::XOR:
3442 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3443 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3444
3445 Known ^= Known2;
3446 break;
3447 case ISD::MUL: {
3448 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3449 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3450 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3451 // TODO: SelfMultiply can be poison, but not undef.
3452 if (SelfMultiply)
3453 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3454 Op.getOperand(0), DemandedElts, false, Depth + 1);
3455 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3456
3457 // If the multiplication is known not to overflow, the product of a number
3458 // with itself is non-negative. Only do this if we didn't already computed
3459 // the opposite value for the sign bit.
3460 if (Op->getFlags().hasNoSignedWrap() &&
3461 Op.getOperand(0) == Op.getOperand(1) &&
3462 !Known.isNegative())
3463 Known.makeNonNegative();
3464 break;
3465 }
3466 case ISD::MULHU: {
3467 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3468 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3469 Known = KnownBits::mulhu(Known, Known2);
3470 break;
3471 }
3472 case ISD::MULHS: {
3473 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3474 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3475 Known = KnownBits::mulhs(Known, Known2);
3476 break;
3477 }
3478 case ISD::ABDU: {
3479 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3480 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3481 Known = KnownBits::abdu(Known, Known2);
3482 break;
3483 }
3484 case ISD::ABDS: {
3485 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3486 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3487 Known = KnownBits::abds(Known, Known2);
3488 unsigned SignBits1 =
3489 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3490 if (SignBits1 == 1)
3491 break;
3492 unsigned SignBits0 =
3493 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3494 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3495 break;
3496 }
3497 case ISD::UMUL_LOHI: {
3498 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3499 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3500 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3501 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3502 if (Op.getResNo() == 0)
3503 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3504 else
3505 Known = KnownBits::mulhu(Known, Known2);
3506 break;
3507 }
3508 case ISD::SMUL_LOHI: {
3509 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3510 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3511 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3512 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3513 if (Op.getResNo() == 0)
3514 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3515 else
3516 Known = KnownBits::mulhs(Known, Known2);
3517 break;
3518 }
3519 case ISD::AVGFLOORU: {
3520 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3521 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3522 Known = KnownBits::avgFloorU(Known, Known2);
3523 break;
3524 }
3525 case ISD::AVGCEILU: {
3526 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3527 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3528 Known = KnownBits::avgCeilU(Known, Known2);
3529 break;
3530 }
3531 case ISD::AVGFLOORS: {
3532 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3533 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3534 Known = KnownBits::avgFloorS(Known, Known2);
3535 break;
3536 }
3537 case ISD::AVGCEILS: {
3538 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3539 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3540 Known = KnownBits::avgCeilS(Known, Known2);
3541 break;
3542 }
3543 case ISD::SELECT:
3544 case ISD::VSELECT:
3545 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3546 // If we don't know any bits, early out.
3547 if (Known.isUnknown())
3548 break;
3549 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3550
3551 // Only known if known in both the LHS and RHS.
3552 Known = Known.intersectWith(Known2);
3553 break;
3554 case ISD::SELECT_CC:
3555 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3556 // If we don't know any bits, early out.
3557 if (Known.isUnknown())
3558 break;
3559 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3560
3561 // Only known if known in both the LHS and RHS.
3562 Known = Known.intersectWith(Known2);
3563 break;
3564 case ISD::SMULO:
3565 case ISD::UMULO:
3566 if (Op.getResNo() != 1)
3567 break;
3568 // The boolean result conforms to getBooleanContents.
3569 // If we know the result of a setcc has the top bits zero, use this info.
3570 // We know that we have an integer-based boolean since these operations
3571 // are only available for integer.
3572 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3574 BitWidth > 1)
3575 Known.Zero.setBitsFrom(1);
3576 break;
3577 case ISD::SETCC:
3578 case ISD::SETCCCARRY:
3579 case ISD::STRICT_FSETCC:
3580 case ISD::STRICT_FSETCCS: {
3581 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3582 // If we know the result of a setcc has the top bits zero, use this info.
3583 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3585 BitWidth > 1)
3586 Known.Zero.setBitsFrom(1);
3587 break;
3588 }
3589 case ISD::SHL: {
3590 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3591 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3592
3593 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3594 bool NSW = Op->getFlags().hasNoSignedWrap();
3595
3596 bool ShAmtNonZero = Known2.isNonZero();
3597
3598 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3599
3600 // Minimum shift low bits are known zero.
3601 if (std::optional<uint64_t> ShMinAmt =
3602 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3603 Known.Zero.setLowBits(*ShMinAmt);
3604 break;
3605 }
3606 case ISD::SRL:
3607 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3608 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3609 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3610 Op->getFlags().hasExact());
3611
3612 // Minimum shift high bits are known zero.
3613 if (std::optional<uint64_t> ShMinAmt =
3614 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3615 Known.Zero.setHighBits(*ShMinAmt);
3616 break;
3617 case ISD::SRA:
3618 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3619 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3620 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3621 Op->getFlags().hasExact());
3622 break;
3623 case ISD::FSHL:
3624 case ISD::FSHR:
3625 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3626 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3627
3628 // For fshl, 0-shift returns the 1st arg.
3629 // For fshr, 0-shift returns the 2nd arg.
3630 if (Amt == 0) {
3631 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3632 DemandedElts, Depth + 1);
3633 break;
3634 }
3635
3636 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3637 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3638 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3639 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3640 if (Opcode == ISD::FSHL) {
3641 Known.One <<= Amt;
3642 Known.Zero <<= Amt;
3643 Known2.One.lshrInPlace(BitWidth - Amt);
3644 Known2.Zero.lshrInPlace(BitWidth - Amt);
3645 } else {
3646 Known.One <<= BitWidth - Amt;
3647 Known.Zero <<= BitWidth - Amt;
3648 Known2.One.lshrInPlace(Amt);
3649 Known2.Zero.lshrInPlace(Amt);
3650 }
3651 Known = Known.unionWith(Known2);
3652 }
3653 break;
3654 case ISD::SHL_PARTS:
3655 case ISD::SRA_PARTS:
3656 case ISD::SRL_PARTS: {
3657 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3658
3659 // Collect lo/hi source values and concatenate.
3660 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3661 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3662 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3663 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3664 Known = Known2.concat(Known);
3665
3666 // Collect shift amount.
3667 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3668
3669 if (Opcode == ISD::SHL_PARTS)
3670 Known = KnownBits::shl(Known, Known2);
3671 else if (Opcode == ISD::SRA_PARTS)
3672 Known = KnownBits::ashr(Known, Known2);
3673 else // if (Opcode == ISD::SRL_PARTS)
3674 Known = KnownBits::lshr(Known, Known2);
3675
3676 // TODO: Minimum shift low/high bits are known zero.
3677
3678 if (Op.getResNo() == 0)
3679 Known = Known.extractBits(LoBits, 0);
3680 else
3681 Known = Known.extractBits(HiBits, LoBits);
3682 break;
3683 }
3685 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3686 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3687 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3688 break;
3689 }
3690 case ISD::CTTZ:
3691 case ISD::CTTZ_ZERO_UNDEF: {
3692 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3693 // If we have a known 1, its position is our upper bound.
3694 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3695 unsigned LowBits = llvm::bit_width(PossibleTZ);
3696 Known.Zero.setBitsFrom(LowBits);
3697 break;
3698 }
3699 case ISD::CTLZ:
3700 case ISD::CTLZ_ZERO_UNDEF: {
3701 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3702 // If we have a known 1, its position is our upper bound.
3703 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3704 unsigned LowBits = llvm::bit_width(PossibleLZ);
3705 Known.Zero.setBitsFrom(LowBits);
3706 break;
3707 }
3708 case ISD::CTPOP: {
3709 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3710 // If we know some of the bits are zero, they can't be one.
3711 unsigned PossibleOnes = Known2.countMaxPopulation();
3712 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3713 break;
3714 }
3715 case ISD::PARITY: {
3716 // Parity returns 0 everywhere but the LSB.
3717 Known.Zero.setBitsFrom(1);
3718 break;
3719 }
3720 case ISD::LOAD: {
3721 LoadSDNode *LD = cast<LoadSDNode>(Op);
3722 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3723 if (ISD::isNON_EXTLoad(LD) && Cst) {
3724 // Determine any common known bits from the loaded constant pool value.
3725 Type *CstTy = Cst->getType();
3726 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3727 !Op.getValueType().isScalableVector()) {
3728 // If its a vector splat, then we can (quickly) reuse the scalar path.
3729 // NOTE: We assume all elements match and none are UNDEF.
3730 if (CstTy->isVectorTy()) {
3731 if (const Constant *Splat = Cst->getSplatValue()) {
3732 Cst = Splat;
3733 CstTy = Cst->getType();
3734 }
3735 }
3736 // TODO - do we need to handle different bitwidths?
3737 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3738 // Iterate across all vector elements finding common known bits.
3739 Known.One.setAllBits();
3740 Known.Zero.setAllBits();
3741 for (unsigned i = 0; i != NumElts; ++i) {
3742 if (!DemandedElts[i])
3743 continue;
3744 if (Constant *Elt = Cst->getAggregateElement(i)) {
3745 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3746 const APInt &Value = CInt->getValue();
3747 Known.One &= Value;
3748 Known.Zero &= ~Value;
3749 continue;
3750 }
3751 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3752 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3753 Known.One &= Value;
3754 Known.Zero &= ~Value;
3755 continue;
3756 }
3757 }
3758 Known.One.clearAllBits();
3759 Known.Zero.clearAllBits();
3760 break;
3761 }
3762 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3763 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3764 Known = KnownBits::makeConstant(CInt->getValue());
3765 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3766 Known =
3767 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3768 }
3769 }
3770 }
3771 } else if (Op.getResNo() == 0) {
3772 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3773 ? LD->getMemoryVT().getFixedSizeInBits()
3774 : BitWidth);
3775 EVT VT = Op.getValueType();
3776 // Fill in any known bits from range information. There are 3 types being
3777 // used. The results VT (same vector elt size as BitWidth), the loaded
3778 // MemoryVT (which may or may not be vector) and the range VTs original
3779 // type. The range matadata needs the full range (i.e
3780 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3781 // if it is know. These are then extended to the original VT sizes below.
3782 if (const MDNode *MD = LD->getRanges()) {
3784 if (VT.isVector()) {
3785 // Handle truncation to the first demanded element.
3786 // TODO: Figure out which demanded elements are covered
3787 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
3788 break;
3789 Known0 = Known0.trunc(BitWidth);
3790 }
3791 }
3792
3793 if (LD->getMemoryVT().isVector())
3794 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
3795
3796 // Extend the Known bits from memory to the size of the result.
3797 if (ISD::isZEXTLoad(Op.getNode()))
3798 Known = Known0.zext(BitWidth);
3799 else if (ISD::isSEXTLoad(Op.getNode()))
3800 Known = Known0.sext(BitWidth);
3801 else if (ISD::isEXTLoad(Op.getNode()))
3802 Known = Known0.anyext(BitWidth);
3803 else
3804 Known = Known0;
3805 assert(Known.getBitWidth() == BitWidth);
3806 return Known;
3807 }
3808 break;
3809 }
3811 if (Op.getValueType().isScalableVector())
3812 break;
3813 EVT InVT = Op.getOperand(0).getValueType();
3814 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3815 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3816 Known = Known.zext(BitWidth);
3817 break;
3818 }
3819 case ISD::ZERO_EXTEND: {
3820 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3821 Known = Known.zext(BitWidth);
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 // If the sign bit is known to be zero or one, then sext will extend
3831 // it to the top bits, else it will just zext.
3832 Known = Known.sext(BitWidth);
3833 break;
3834 }
3835 case ISD::SIGN_EXTEND: {
3836 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3837 // If the sign bit is known to be zero or one, then sext will extend
3838 // it to the top bits, else it will just zext.
3839 Known = Known.sext(BitWidth);
3840 break;
3841 }
3843 if (Op.getValueType().isScalableVector())
3844 break;
3845 EVT InVT = Op.getOperand(0).getValueType();
3846 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3847 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3848 Known = Known.anyext(BitWidth);
3849 break;
3850 }
3851 case ISD::ANY_EXTEND: {
3852 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3853 Known = Known.anyext(BitWidth);
3854 break;
3855 }
3856 case ISD::TRUNCATE: {
3857 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3858 Known = Known.trunc(BitWidth);
3859 break;
3860 }
3861 case ISD::AssertZext: {
3862 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3864 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3865 Known.Zero |= (~InMask);
3866 Known.One &= (~Known.Zero);
3867 break;
3868 }
3869 case ISD::AssertAlign: {
3870 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3871 assert(LogOfAlign != 0);
3872
3873 // TODO: Should use maximum with source
3874 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3875 // well as clearing one bits.
3876 Known.Zero.setLowBits(LogOfAlign);
3877 Known.One.clearLowBits(LogOfAlign);
3878 break;
3879 }
3880 case ISD::FGETSIGN:
3881 // All bits are zero except the low bit.
3882 Known.Zero.setBitsFrom(1);
3883 break;
3884 case ISD::ADD:
3885 case ISD::SUB: {
3886 SDNodeFlags Flags = Op.getNode()->getFlags();
3887 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3888 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3890 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3891 Flags.hasNoUnsignedWrap(), Known, Known2);
3892 break;
3893 }
3894 case ISD::USUBO:
3895 case ISD::SSUBO:
3896 case ISD::USUBO_CARRY:
3897 case ISD::SSUBO_CARRY:
3898 if (Op.getResNo() == 1) {
3899 // If we know the result of a setcc has the top bits zero, use this info.
3900 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3902 BitWidth > 1)
3903 Known.Zero.setBitsFrom(1);
3904 break;
3905 }
3906 [[fallthrough]];
3907 case ISD::SUBC: {
3908 assert(Op.getResNo() == 0 &&
3909 "We only compute knownbits for the difference here.");
3910
3911 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
3912 KnownBits Borrow(1);
3913 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3914 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3915 // Borrow has bit width 1
3916 Borrow = Borrow.trunc(1);
3917 } else {
3918 Borrow.setAllZero();
3919 }
3920
3921 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3922 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3923 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
3924 break;
3925 }
3926 case ISD::UADDO:
3927 case ISD::SADDO:
3928 case ISD::UADDO_CARRY:
3929 case ISD::SADDO_CARRY:
3930 if (Op.getResNo() == 1) {
3931 // If we know the result of a setcc has the top bits zero, use this info.
3932 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3934 BitWidth > 1)
3935 Known.Zero.setBitsFrom(1);
3936 break;
3937 }
3938 [[fallthrough]];
3939 case ISD::ADDC:
3940 case ISD::ADDE: {
3941 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3942
3943 // With ADDE and UADDO_CARRY, a carry bit may be added in.
3944 KnownBits Carry(1);
3945 if (Opcode == ISD::ADDE)
3946 // Can't track carry from glue, set carry to unknown.
3947 Carry.resetAll();
3948 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3949 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3950 // Carry has bit width 1
3951 Carry = Carry.trunc(1);
3952 } else {
3953 Carry.setAllZero();
3954 }
3955
3956 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3957 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3958 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3959 break;
3960 }
3961 case ISD::UDIV: {
3962 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3963 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3964 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
3965 break;
3966 }
3967 case ISD::SDIV: {
3968 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3969 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3970 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
3971 break;
3972 }
3973 case ISD::SREM: {
3974 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3975 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3976 Known = KnownBits::srem(Known, Known2);
3977 break;
3978 }
3979 case ISD::UREM: {
3980 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3981 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3982 Known = KnownBits::urem(Known, Known2);
3983 break;
3984 }
3985 case ISD::EXTRACT_ELEMENT: {
3986 Known = computeKnownBits(Op.getOperand(0), Depth+1);
3987 const unsigned Index = Op.getConstantOperandVal(1);
3988 const unsigned EltBitWidth = Op.getValueSizeInBits();
3989
3990 // Remove low part of known bits mask
3991 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3992 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3993
3994 // Remove high part of known bit mask
3995 Known = Known.trunc(EltBitWidth);
3996 break;
3997 }
3999 SDValue InVec = Op.getOperand(0);
4000 SDValue EltNo = Op.getOperand(1);
4001 EVT VecVT = InVec.getValueType();
4002 // computeKnownBits not yet implemented for scalable vectors.
4003 if (VecVT.isScalableVector())
4004 break;
4005 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4006 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4007
4008 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4009 // anything about the extended bits.
4010 if (BitWidth > EltBitWidth)
4011 Known = Known.trunc(EltBitWidth);
4012
4013 // If we know the element index, just demand that vector element, else for
4014 // an unknown element index, ignore DemandedElts and demand them all.
4015 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4016 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4017 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4018 DemandedSrcElts =
4019 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4020
4021 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4022 if (BitWidth > EltBitWidth)
4023 Known = Known.anyext(BitWidth);
4024 break;
4025 }
4027 if (Op.getValueType().isScalableVector())
4028 break;
4029
4030 // If we know the element index, split the demand between the
4031 // source vector and the inserted element, otherwise assume we need
4032 // the original demanded vector elements and the value.
4033 SDValue InVec = Op.getOperand(0);
4034 SDValue InVal = Op.getOperand(1);
4035 SDValue EltNo = Op.getOperand(2);
4036 bool DemandedVal = true;
4037 APInt DemandedVecElts = DemandedElts;
4038 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4039 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4040 unsigned EltIdx = CEltNo->getZExtValue();
4041 DemandedVal = !!DemandedElts[EltIdx];
4042 DemandedVecElts.clearBit(EltIdx);
4043 }
4044 Known.One.setAllBits();
4045 Known.Zero.setAllBits();
4046 if (DemandedVal) {
4047 Known2 = computeKnownBits(InVal, Depth + 1);
4048 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4049 }
4050 if (!!DemandedVecElts) {
4051 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4052 Known = Known.intersectWith(Known2);
4053 }
4054 break;
4055 }
4056 case ISD::BITREVERSE: {
4057 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4058 Known = Known2.reverseBits();
4059 break;
4060 }
4061 case ISD::BSWAP: {
4062 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4063 Known = Known2.byteSwap();
4064 break;
4065 }
4066 case ISD::ABS: {
4067 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4068 Known = Known2.abs();
4069 Known.Zero.setHighBits(
4070 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4071 break;
4072 }
4073 case ISD::USUBSAT: {
4074 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4075 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4076 Known = KnownBits::usub_sat(Known, Known2);
4077 break;
4078 }
4079 case ISD::UMIN: {
4080 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4081 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4082 Known = KnownBits::umin(Known, Known2);
4083 break;
4084 }
4085 case ISD::UMAX: {
4086 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4087 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4088 Known = KnownBits::umax(Known, Known2);
4089 break;
4090 }
4091 case ISD::SMIN:
4092 case ISD::SMAX: {
4093 // If we have a clamp pattern, we know that the number of sign bits will be
4094 // the minimum of the clamp min/max range.
4095 bool IsMax = (Opcode == ISD::SMAX);
4096 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4097 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4098 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4099 CstHigh =
4100 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4101 if (CstLow && CstHigh) {
4102 if (!IsMax)
4103 std::swap(CstLow, CstHigh);
4104
4105 const APInt &ValueLow = CstLow->getAPIntValue();
4106 const APInt &ValueHigh = CstHigh->getAPIntValue();
4107 if (ValueLow.sle(ValueHigh)) {
4108 unsigned LowSignBits = ValueLow.getNumSignBits();
4109 unsigned HighSignBits = ValueHigh.getNumSignBits();
4110 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4111 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4112 Known.One.setHighBits(MinSignBits);
4113 break;
4114 }
4115 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4116 Known.Zero.setHighBits(MinSignBits);
4117 break;
4118 }
4119 }
4120 }
4121
4122 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4123 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4124 if (IsMax)
4125 Known = KnownBits::smax(Known, Known2);
4126 else
4127 Known = KnownBits::smin(Known, Known2);
4128
4129 // For SMAX, if CstLow is non-negative we know the result will be
4130 // non-negative and thus all sign bits are 0.
4131 // TODO: There's an equivalent of this for smin with negative constant for
4132 // known ones.
4133 if (IsMax && CstLow) {
4134 const APInt &ValueLow = CstLow->getAPIntValue();
4135 if (ValueLow.isNonNegative()) {
4136 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4137 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4138 }
4139 }
4140
4141 break;
4142 }
4143 case ISD::UINT_TO_FP: {
4144 Known.makeNonNegative();
4145 break;
4146 }
4147 case ISD::SINT_TO_FP: {
4148 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4149 if (Known2.isNonNegative())
4150 Known.makeNonNegative();
4151 else if (Known2.isNegative())
4152 Known.makeNegative();
4153 break;
4154 }
4155 case ISD::FP_TO_UINT_SAT: {
4156 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4157 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4159 break;
4160 }
4162 if (Op.getResNo() == 1) {
4163 // The boolean result conforms to getBooleanContents.
4164 // If we know the result of a setcc has the top bits zero, use this info.
4165 // We know that we have an integer-based boolean since these operations
4166 // are only available for integer.
4167 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4169 BitWidth > 1)
4170 Known.Zero.setBitsFrom(1);
4171 break;
4172 }
4173 [[fallthrough]];
4175 case ISD::ATOMIC_SWAP:
4187 case ISD::ATOMIC_LOAD: {
4188 unsigned MemBits =
4189 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4190 // If we are looking at the loaded value.
4191 if (Op.getResNo() == 0) {
4193 Known.Zero.setBitsFrom(MemBits);
4194 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4195 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4196 Known.Zero.setBitsFrom(MemBits);
4197 }
4198 break;
4199 }
4200 case ISD::FrameIndex:
4202 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4203 Known, getMachineFunction());
4204 break;
4205
4206 default:
4207 if (Opcode < ISD::BUILTIN_OP_END)
4208 break;
4209 [[fallthrough]];
4213 // TODO: Probably okay to remove after audit; here to reduce change size
4214 // in initial enablement patch for scalable vectors
4215 if (Op.getValueType().isScalableVector())
4216 break;
4217
4218 // Allow the target to implement this method for its nodes.
4219 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4220 break;
4221 }
4222
4223 return Known;
4224}
4225
4226/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4228 switch (OR) {
4236 }
4237 llvm_unreachable("Unknown OverflowResult");
4238}
4239
4242 // X + 0 never overflow
4243 if (isNullConstant(N1))
4244 return OFK_Never;
4245
4246 // If both operands each have at least two sign bits, the addition
4247 // cannot overflow.
4248 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4249 return OFK_Never;
4250
4251 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4252 return OFK_Sometime;
4253}
4254
4257 // X + 0 never overflow
4258 if (isNullConstant(N1))
4259 return OFK_Never;
4260
4261 // mulhi + 1 never overflow
4262 KnownBits N1Known = computeKnownBits(N1);
4263 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4264 N1Known.getMaxValue().ult(2))
4265 return OFK_Never;
4266
4267 KnownBits N0Known = computeKnownBits(N0);
4268 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4269 N0Known.getMaxValue().ult(2))
4270 return OFK_Never;
4271
4272 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4273 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4274 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4275 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4276}
4277
4280 // X - 0 never overflow
4281 if (isNullConstant(N1))
4282 return OFK_Never;
4283
4284 // If both operands each have at least two sign bits, the subtraction
4285 // cannot overflow.
4286 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4287 return OFK_Never;
4288
4289 KnownBits N0Known = computeKnownBits(N0);
4290 KnownBits N1Known = computeKnownBits(N1);
4291 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4292 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4293 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4294}
4295
4298 // X - 0 never overflow
4299 if (isNullConstant(N1))
4300 return OFK_Never;
4301
4302 KnownBits N0Known = computeKnownBits(N0);
4303 KnownBits N1Known = computeKnownBits(N1);
4304 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4305 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4306 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4307}
4308
4311 // X * 0 and X * 1 never overflow.
4312 if (isNullConstant(N1) || isOneConstant(N1))
4313 return OFK_Never;
4314
4315 KnownBits N0Known = computeKnownBits(N0);
4316 KnownBits N1Known = computeKnownBits(N1);
4317 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4318 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4319 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4320}
4321
4324 // X * 0 and X * 1 never overflow.
4325 if (isNullConstant(N1) || isOneConstant(N1))
4326 return OFK_Never;
4327
4328 // Get the size of the result.
4329 unsigned BitWidth = N0.getScalarValueSizeInBits();
4330
4331 // Sum of the sign bits.
4332 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4333
4334 // If we have enough sign bits, then there's no overflow.
4335 if (SignBits > BitWidth + 1)
4336 return OFK_Never;
4337
4338 if (SignBits == BitWidth + 1) {
4339 // The overflow occurs when the true multiplication of the
4340 // the operands is the minimum negative number.
4341 KnownBits N0Known = computeKnownBits(N0);
4342 KnownBits N1Known = computeKnownBits(N1);
4343 // If one of the operands is non-negative, then there's no
4344 // overflow.
4345 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4346 return OFK_Never;
4347 }
4348
4349 return OFK_Sometime;
4350}
4351
4353 if (Depth >= MaxRecursionDepth)
4354 return false; // Limit search depth.
4355
4356 EVT OpVT = Val.getValueType();
4357 unsigned BitWidth = OpVT.getScalarSizeInBits();
4358
4359 // Is the constant a known power of 2?
4361 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4362 }))
4363 return true;
4364
4365 // A left-shift of a constant one will have exactly one bit set because
4366 // shifting the bit off the end is undefined.
4367 if (Val.getOpcode() == ISD::SHL) {
4368 auto *C = isConstOrConstSplat(Val.getOperand(0));
4369 if (C && C->getAPIntValue() == 1)
4370 return true;
4371 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4372 isKnownNeverZero(Val, Depth);
4373 }
4374
4375 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4376 // one bit set.
4377 if (Val.getOpcode() == ISD::SRL) {
4378 auto *C = isConstOrConstSplat(Val.getOperand(0));
4379 if (C && C->getAPIntValue().isSignMask())
4380 return true;
4381 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4382 isKnownNeverZero(Val, Depth);
4383 }
4384
4385 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4386 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4387
4388 // Are all operands of a build vector constant powers of two?
4389 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4390 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4391 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4392 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4393 return false;
4394 }))
4395 return true;
4396
4397 // Is the operand of a splat vector a constant power of two?
4398 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4399 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4400 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4401 return true;
4402
4403 // vscale(power-of-two) is a power-of-two for some targets
4404 if (Val.getOpcode() == ISD::VSCALE &&
4405 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4407 return true;
4408
4409 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4410 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4411 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4413
4414 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4415 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4417
4418 // Looking for `x & -x` pattern:
4419 // If x == 0:
4420 // x & -x -> 0
4421 // If x != 0:
4422 // x & -x -> non-zero pow2
4423 // so if we find the pattern return whether we know `x` is non-zero.
4424 SDValue X;
4425 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4426 return isKnownNeverZero(X, Depth);
4427
4428 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4429 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4430
4431 // More could be done here, though the above checks are enough
4432 // to handle some common cases.
4433 return false;
4434}
4435
4437 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4438 return C1->getValueAPF().getExactLog2Abs() >= 0;
4439
4440 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4441 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4442
4443 return false;
4444}
4445
4447 EVT VT = Op.getValueType();
4448
4449 // Since the number of lanes in a scalable vector is unknown at compile time,
4450 // we track one bit which is implicitly broadcast to all lanes. This means
4451 // that all lanes in a scalable vector are considered demanded.
4452 APInt DemandedElts = VT.isFixedLengthVector()
4454 : APInt(1, 1);
4455 return ComputeNumSignBits(Op, DemandedElts, Depth);
4456}
4457
4458unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4459 unsigned Depth) const {
4460 EVT VT = Op.getValueType();
4461 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4462 unsigned VTBits = VT.getScalarSizeInBits();
4463 unsigned NumElts = DemandedElts.getBitWidth();
4464 unsigned Tmp, Tmp2;
4465 unsigned FirstAnswer = 1;
4466
4467 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4468 const APInt &Val = C->getAPIntValue();
4469 return Val.getNumSignBits();
4470 }
4471
4472 if (Depth >= MaxRecursionDepth)
4473 return 1; // Limit search depth.
4474
4475 if (!DemandedElts)
4476 return 1; // No demanded elts, better to assume we don't know anything.
4477
4478 unsigned Opcode = Op.getOpcode();
4479 switch (Opcode) {
4480 default: break;
4481 case ISD::AssertSext:
4482 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4483 return VTBits-Tmp+1;
4484 case ISD::AssertZext:
4485 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4486 return VTBits-Tmp;
4487 case ISD::MERGE_VALUES:
4488 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4489 Depth + 1);
4490 case ISD::SPLAT_VECTOR: {
4491 // Check if the sign bits of source go down as far as the truncated value.
4492 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4493 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4494 if (NumSrcSignBits > (NumSrcBits - VTBits))
4495 return NumSrcSignBits - (NumSrcBits - VTBits);
4496 break;
4497 }
4498 case ISD::BUILD_VECTOR:
4499 assert(!VT.isScalableVector());
4500 Tmp = VTBits;
4501 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4502 if (!DemandedElts[i])
4503 continue;
4504
4505 SDValue SrcOp = Op.getOperand(i);
4506 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4507 // for constant nodes to ensure we only look at the sign bits.
4508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4509 APInt T = C->getAPIntValue().trunc(VTBits);
4510 Tmp2 = T.getNumSignBits();
4511 } else {
4512 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4513
4514 if (SrcOp.getValueSizeInBits() != VTBits) {
4515 assert(SrcOp.getValueSizeInBits() > VTBits &&
4516 "Expected BUILD_VECTOR implicit truncation");
4517 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4518 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4519 }
4520 }
4521 Tmp = std::min(Tmp, Tmp2);
4522 }
4523 return Tmp;
4524
4525 case ISD::VECTOR_SHUFFLE: {
4526 // Collect the minimum number of sign bits that are shared by every vector
4527 // element referenced by the shuffle.
4528 APInt DemandedLHS, DemandedRHS;
4529 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4530 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4531 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4532 DemandedLHS, DemandedRHS))
4533 return 1;
4534
4535 Tmp = std::numeric_limits<unsigned>::max();
4536 if (!!DemandedLHS)
4537 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4538 if (!!DemandedRHS) {
4539 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4540 Tmp = std::min(Tmp, Tmp2);
4541 }
4542 // If we don't know anything, early out and try computeKnownBits fall-back.
4543 if (Tmp == 1)
4544 break;
4545 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4546 return Tmp;
4547 }
4548
4549 case ISD::BITCAST: {
4550 if (VT.isScalableVector())
4551 break;
4552 SDValue N0 = Op.getOperand(0);
4553 EVT SrcVT = N0.getValueType();
4554 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4555
4556 // Ignore bitcasts from unsupported types..
4557 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4558 break;
4559
4560 // Fast handling of 'identity' bitcasts.
4561 if (VTBits == SrcBits)
4562 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4563
4564 bool IsLE = getDataLayout().isLittleEndian();
4565
4566 // Bitcast 'large element' scalar/vector to 'small element' vector.
4567 if ((SrcBits % VTBits) == 0) {
4568 assert(VT.isVector() && "Expected bitcast to vector");
4569
4570 unsigned Scale = SrcBits / VTBits;
4571 APInt SrcDemandedElts =
4572 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4573
4574 // Fast case - sign splat can be simply split across the small elements.
4575 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4576 if (Tmp == SrcBits)
4577 return VTBits;
4578
4579 // Slow case - determine how far the sign extends into each sub-element.
4580 Tmp2 = VTBits;
4581 for (unsigned i = 0; i != NumElts; ++i)
4582 if (DemandedElts[i]) {
4583 unsigned SubOffset = i % Scale;
4584 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4585 SubOffset = SubOffset * VTBits;
4586 if (Tmp <= SubOffset)
4587 return 1;
4588 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4589 }
4590 return Tmp2;
4591 }
4592 break;
4593 }
4594
4596 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4597 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4598 return VTBits - Tmp + 1;
4599 case ISD::SIGN_EXTEND:
4600 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4601 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4603 // Max of the input and what this extends.
4604 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4605 Tmp = VTBits-Tmp+1;
4606 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4607 return std::max(Tmp, Tmp2);
4609 if (VT.isScalableVector())
4610 break;
4611 SDValue Src = Op.getOperand(0);
4612 EVT SrcVT = Src.getValueType();
4613 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4614 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4615 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4616 }
4617 case ISD::SRA:
4618 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4619 // SRA X, C -> adds C sign bits.
4620 if (std::optional<uint64_t> ShAmt =
4621 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4622 Tmp = std::min<uint64_t>(Tmp + *ShAmt, VTBits);
4623 return Tmp;
4624 case ISD::SHL:
4625 if (std::optional<ConstantRange> ShAmtRange =
4626 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4627 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4628 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4629 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4630 // shifted out, then we can compute the number of sign bits for the
4631 // operand being extended. A future improvement could be to pass along the
4632 // "shifted left by" information in the recursive calls to
4633 // ComputeKnownSignBits. Allowing us to handle this more generically.
4634 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4635 SDValue Ext = Op.getOperand(0);
4636 EVT ExtVT = Ext.getValueType();
4637 SDValue Extendee = Ext.getOperand(0);
4638 EVT ExtendeeVT = Extendee.getValueType();
4639 uint64_t SizeDifference =
4640 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4641 if (SizeDifference <= MinShAmt) {
4642 Tmp = SizeDifference +
4643 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4644 if (MaxShAmt < Tmp)
4645 return Tmp - MaxShAmt;
4646 }
4647 }
4648 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4649 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4650 if (MaxShAmt < Tmp)
4651 return Tmp - MaxShAmt;
4652 }
4653 break;
4654 case ISD::AND:
4655 case ISD::OR:
4656 case ISD::XOR: // NOT is handled here.
4657 // Logical binary ops preserve the number of sign bits at the worst.
4658 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4659 if (Tmp != 1) {
4660 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4661 FirstAnswer = std::min(Tmp, Tmp2);
4662 // We computed what we know about the sign bits as our first
4663 // answer. Now proceed to the generic code that uses
4664 // computeKnownBits, and pick whichever answer is better.
4665 }
4666 break;
4667
4668 case ISD::SELECT:
4669 case ISD::VSELECT:
4670 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4671 if (Tmp == 1) return 1; // Early out.
4672 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4673 return std::min(Tmp, Tmp2);
4674 case ISD::SELECT_CC:
4675 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4676 if (Tmp == 1) return 1; // Early out.
4677 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4678 return std::min(Tmp, Tmp2);
4679
4680 case ISD::SMIN:
4681 case ISD::SMAX: {
4682 // If we have a clamp pattern, we know that the number of sign bits will be
4683 // the minimum of the clamp min/max range.
4684 bool IsMax = (Opcode == ISD::SMAX);
4685 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4686 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4687 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4688 CstHigh =
4689 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4690 if (CstLow && CstHigh) {
4691 if (!IsMax)
4692 std::swap(CstLow, CstHigh);
4693 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4694 Tmp = CstLow->getAPIntValue().getNumSignBits();
4695 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4696 return std::min(Tmp, Tmp2);
4697 }
4698 }
4699
4700 // Fallback - just get the minimum number of sign bits of the operands.
4701 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4702 if (Tmp == 1)
4703 return 1; // Early out.
4704 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4705 return std::min(Tmp, Tmp2);
4706 }
4707 case ISD::UMIN:
4708 case ISD::UMAX:
4709 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4710 if (Tmp == 1)
4711 return 1; // Early out.
4712 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4713 return std::min(Tmp, Tmp2);
4714 case ISD::SSUBO_CARRY:
4715 case ISD::USUBO_CARRY:
4716 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4717 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4718 return VTBits;
4719 [[fallthrough]];
4720 case ISD::SADDO:
4721 case ISD::UADDO:
4722 case ISD::SADDO_CARRY:
4723 case ISD::UADDO_CARRY:
4724 case ISD::SSUBO:
4725 case ISD::USUBO:
4726 case ISD::SMULO:
4727 case ISD::UMULO:
4728 if (Op.getResNo() != 1)
4729 break;
4730 // The boolean result conforms to getBooleanContents. Fall through.
4731 // If setcc returns 0/-1, all bits are sign bits.
4732 // We know that we have an integer-based boolean since these operations
4733 // are only available for integer.
4734 if (TLI->getBooleanContents(VT.isVector(), false) ==
4736 return VTBits;
4737 break;
4738 case ISD::SETCC:
4739 case ISD::SETCCCARRY:
4740 case ISD::STRICT_FSETCC:
4741 case ISD::STRICT_FSETCCS: {
4742 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4743 // If setcc returns 0/-1, all bits are sign bits.
4744 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4746 return VTBits;
4747 break;
4748 }
4749 case ISD::ROTL:
4750 case ISD::ROTR:
4751 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4752
4753 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4754 if (Tmp == VTBits)
4755 return VTBits;
4756
4757 if (ConstantSDNode *C =
4758 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4759 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4760
4761 // Handle rotate right by N like a rotate left by 32-N.
4762 if (Opcode == ISD::ROTR)
4763 RotAmt = (VTBits - RotAmt) % VTBits;
4764
4765 // If we aren't rotating out all of the known-in sign bits, return the
4766 // number that are left. This handles rotl(sext(x), 1) for example.
4767 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4768 }
4769 break;
4770 case ISD::ADD:
4771 case ISD::ADDC:
4772 // Add can have at most one carry bit. Thus we know that the output
4773 // is, at worst, one more bit than the inputs.
4774 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4775 if (Tmp == 1) return 1; // Early out.
4776
4777 // Special case decrementing a value (ADD X, -1):
4778 if (ConstantSDNode *CRHS =
4779 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4780 if (CRHS->isAllOnes()) {
4781 KnownBits Known =
4782 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4783
4784 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4785 // sign bits set.
4786 if ((Known.Zero | 1).isAllOnes())
4787 return VTBits;
4788
4789 // If we are subtracting one from a positive number, there is no carry
4790 // out of the result.
4791 if (Known.isNonNegative())
4792 return Tmp;
4793 }
4794
4795 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4796 if (Tmp2 == 1) return 1; // Early out.
4797 return std::min(Tmp, Tmp2) - 1;
4798 case ISD::SUB:
4799 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4800 if (Tmp2 == 1) return 1; // Early out.
4801
4802 // Handle NEG.
4803 if (ConstantSDNode *CLHS =
4804 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4805 if (CLHS->isZero()) {
4806 KnownBits Known =
4807 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4808 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4809 // sign bits set.
4810 if ((Known.Zero | 1).isAllOnes())
4811 return VTBits;
4812
4813 // If the input is known to be positive (the sign bit is known clear),
4814 // the output of the NEG has the same number of sign bits as the input.
4815 if (Known.isNonNegative())
4816 return Tmp2;
4817
4818 // Otherwise, we treat this like a SUB.
4819 }
4820
4821 // Sub can have at most one carry bit. Thus we know that the output
4822 // is, at worst, one more bit than the inputs.
4823 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4824 if (Tmp == 1) return 1; // Early out.
4825 return std::min(Tmp, Tmp2) - 1;
4826 case ISD::MUL: {
4827 // The output of the Mul can be at most twice the valid bits in the inputs.
4828 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4829 if (SignBitsOp0 == 1)
4830 break;
4831 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
4832 if (SignBitsOp1 == 1)
4833 break;
4834 unsigned OutValidBits =
4835 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
4836 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
4837 }
4838 case ISD::AVGCEILS:
4839 case ISD::AVGFLOORS:
4840 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4841 if (Tmp == 1)
4842 return 1; // Early out.
4843 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4844 return std::min(Tmp, Tmp2);
4845 case ISD::SREM:
4846 // The sign bit is the LHS's sign bit, except when the result of the
4847 // remainder is zero. The magnitude of the result should be less than or
4848 // equal to the magnitude of the LHS. Therefore, the result should have
4849 // at least as many sign bits as the left hand side.
4850 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4851 case ISD::TRUNCATE: {
4852 // Check if the sign bits of source go down as far as the truncated value.
4853 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
4854 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4855 if (NumSrcSignBits > (NumSrcBits - VTBits))
4856 return NumSrcSignBits - (NumSrcBits - VTBits);
4857 break;
4858 }
4859 case ISD::EXTRACT_ELEMENT: {
4860 if (VT.isScalableVector())
4861 break;
4862 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
4863 const int BitWidth = Op.getValueSizeInBits();
4864 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
4865
4866 // Get reverse index (starting from 1), Op1 value indexes elements from
4867 // little end. Sign starts at big end.
4868 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
4869
4870 // If the sign portion ends in our element the subtraction gives correct
4871 // result. Otherwise it gives either negative or > bitwidth result
4872 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
4873 }
4875 if (VT.isScalableVector())
4876 break;
4877 // If we know the element index, split the demand between the
4878 // source vector and the inserted element, otherwise assume we need
4879 // the original demanded vector elements and the value.
4880 SDValue InVec = Op.getOperand(0);
4881 SDValue InVal = Op.getOperand(1);
4882 SDValue EltNo = Op.getOperand(2);
4883 bool DemandedVal = true;
4884 APInt DemandedVecElts = DemandedElts;
4885 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4886 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4887 unsigned EltIdx = CEltNo->getZExtValue();
4888 DemandedVal = !!DemandedElts[EltIdx];
4889 DemandedVecElts.clearBit(EltIdx);
4890 }
4891 Tmp = std::numeric_limits<unsigned>::max();
4892 if (DemandedVal) {
4893 // TODO - handle implicit truncation of inserted elements.
4894 if (InVal.getScalarValueSizeInBits() != VTBits)
4895 break;
4896 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
4897 Tmp = std::min(Tmp, Tmp2);
4898 }
4899 if (!!DemandedVecElts) {
4900 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
4901 Tmp = std::min(Tmp, Tmp2);
4902 }
4903 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4904 return Tmp;
4905 }
4907 assert(!VT.isScalableVector());
4908 SDValue InVec = Op.getOperand(0);
4909 SDValue EltNo = Op.getOperand(1);
4910 EVT VecVT = InVec.getValueType();
4911 // ComputeNumSignBits not yet implemented for scalable vectors.
4912 if (VecVT.isScalableVector())
4913 break;
4914 const unsigned BitWidth = Op.getValueSizeInBits();
4915 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
4916 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4917
4918 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
4919 // anything about sign bits. But if the sizes match we can derive knowledge
4920 // about sign bits from the vector operand.
4921 if (BitWidth != EltBitWidth)
4922 break;
4923
4924 // If we know the element index, just demand that vector element, else for
4925 // an unknown element index, ignore DemandedElts and demand them all.
4926 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4927 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4928 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4929 DemandedSrcElts =
4930 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4931
4932 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
4933 }
4935 // Offset the demanded elts by the subvector index.
4936 SDValue Src = Op.getOperand(0);
4937 // Bail until we can represent demanded elements for scalable vectors.
4938 if (Src.getValueType().isScalableVector())
4939 break;
4940 uint64_t Idx = Op.getConstantOperandVal(1);
4941 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
4942 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
4943 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4944 }
4945 case ISD::CONCAT_VECTORS: {
4946 if (VT.isScalableVector())
4947 break;
4948 // Determine the minimum number of sign bits across all demanded
4949 // elts of the input vectors. Early out if the result is already 1.
4950 Tmp = std::numeric_limits<unsigned>::max();
4951 EVT SubVectorVT = Op.getOperand(0).getValueType();
4952 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
4953 unsigned NumSubVectors = Op.getNumOperands();
4954 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
4955 APInt DemandedSub =
4956 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
4957 if (!DemandedSub)
4958 continue;
4959 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
4960 Tmp = std::min(Tmp, Tmp2);
4961 }
4962 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4963 return Tmp;
4964 }
4965 case ISD::INSERT_SUBVECTOR: {
4966 if (VT.isScalableVector())
4967 break;
4968 // Demand any elements from the subvector and the remainder from the src its
4969 // inserted into.
4970 SDValue Src = Op.getOperand(0);
4971 SDValue Sub = Op.getOperand(1);
4972 uint64_t Idx = Op.getConstantOperandVal(2);
4973 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
4974 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
4975 APInt DemandedSrcElts = DemandedElts;
4976 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
4977
4978 Tmp = std::numeric_limits<unsigned>::max();
4979 if (!!DemandedSubElts) {
4980 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
4981 if (Tmp == 1)
4982 return 1; // early-out
4983 }
4984 if (!!DemandedSrcElts) {
4985 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4986 Tmp = std::min(Tmp, Tmp2);
4987 }
4988 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4989 return Tmp;
4990 }
4991 case ISD::LOAD: {
4992 LoadSDNode *LD = cast<LoadSDNode>(Op);
4993 if (const MDNode *Ranges = LD->getRanges()) {
4994 if (DemandedElts != 1)
4995 break;
4996
4998 if (VTBits > CR.getBitWidth()) {
4999 switch (LD->getExtensionType()) {
5000 case ISD::SEXTLOAD:
5001 CR = CR.signExtend(VTBits);
5002 break;
5003 case ISD::ZEXTLOAD:
5004 CR = CR.zeroExtend(VTBits);
5005 break;
5006 default:
5007 break;
5008 }
5009 }
5010
5011 if (VTBits != CR.getBitWidth())
5012 break;
5013 return std::min(CR.getSignedMin().getNumSignBits(),
5015 }
5016
5017 break;
5018 }
5021 case ISD::ATOMIC_SWAP:
5033 case ISD::ATOMIC_LOAD: {
5034 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
5035 // If we are looking at the loaded value.
5036 if (Op.getResNo() == 0) {
5037 if (Tmp == VTBits)
5038 return 1; // early-out
5040 return VTBits - Tmp + 1;
5042 return VTBits - Tmp;
5043 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5044 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
5045 if (ETy == ISD::SEXTLOAD)
5046 return VTBits - Tmp + 1;
5047 if (ETy == ISD::ZEXTLOAD)
5048 return VTBits - Tmp;
5049 }
5050 }
5051 break;
5052 }
5053 }
5054
5055 // If we are looking at the loaded value of the SDNode.
5056 if (Op.getResNo() == 0) {
5057 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5058 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5059 unsigned ExtType = LD->getExtensionType();
5060 switch (ExtType) {
5061 default: break;
5062 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5063 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5064 return VTBits - Tmp + 1;
5065 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5066 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5067 return VTBits - Tmp;
5068 case ISD::NON_EXTLOAD:
5069 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5070 // We only need to handle vectors - computeKnownBits should handle
5071 // scalar cases.
5072 Type *CstTy = Cst->getType();
5073 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5074 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5075 VTBits == CstTy->getScalarSizeInBits()) {
5076 Tmp = VTBits;
5077 for (unsigned i = 0; i != NumElts; ++i) {
5078 if (!DemandedElts[i])
5079 continue;
5080 if (Constant *Elt = Cst->getAggregateElement(i)) {
5081 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5082 const APInt &Value = CInt->getValue();
5083 Tmp = std::min(Tmp, Value.getNumSignBits());
5084 continue;
5085 }
5086 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5087 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5088 Tmp = std::min(Tmp, Value.getNumSignBits());
5089 continue;
5090 }
5091 }
5092 // Unknown type. Conservatively assume no bits match sign bit.
5093 return 1;
5094 }
5095 return Tmp;
5096 }
5097 }
5098 break;
5099 }
5100 }
5101 }
5102
5103 // Allow the target to implement this method for its nodes.
5104 if (Opcode >= ISD::BUILTIN_OP_END ||
5105 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5106 Opcode == ISD::INTRINSIC_W_CHAIN ||
5107 Opcode == ISD::INTRINSIC_VOID) {
5108 // TODO: This can probably be removed once target code is audited. This
5109 // is here purely to reduce patch size and review complexity.
5110 if (!VT.isScalableVector()) {
5111 unsigned NumBits =
5112 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5113 if (NumBits > 1)
5114 FirstAnswer = std::max(FirstAnswer, NumBits);
5115 }
5116 }
5117
5118 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5119 // use this information.
5120 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5121 return std::max(FirstAnswer, Known.countMinSignBits());
5122}
5123
5125 unsigned Depth) const {
5126 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5127 return Op.getScalarValueSizeInBits() - SignBits + 1;
5128}
5129
5131 const APInt &DemandedElts,
5132 unsigned Depth) const {
5133 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5134 return Op.getScalarValueSizeInBits() - SignBits + 1;
5135}
5136
5138 unsigned Depth) const {
5139 // Early out for FREEZE.
5140 if (Op.getOpcode() == ISD::FREEZE)
5141 return true;
5142
5143 // TODO: Assume we don't know anything for now.
5144 EVT VT = Op.getValueType();
5145 if (VT.isScalableVector())
5146 return false;
5147
5148 APInt DemandedElts = VT.isVector()
5150 : APInt(1, 1);
5151 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5152}
5153
5155 const APInt &DemandedElts,
5156 bool PoisonOnly,
5157 unsigned Depth) const {
5158 unsigned Opcode = Op.getOpcode();
5159
5160 // Early out for FREEZE.
5161 if (Opcode == ISD::FREEZE)
5162 return true;
5163
5164 if (Depth >= MaxRecursionDepth)
5165 return false; // Limit search depth.
5166
5167 if (isIntOrFPConstant(Op))
5168 return true;
5169
5170 switch (Opcode) {
5171 case ISD::CONDCODE:
5172 case ISD::VALUETYPE:
5173 case ISD::FrameIndex:
5175 case ISD::CopyFromReg:
5176 return true;
5177
5178 case ISD::UNDEF:
5179 return PoisonOnly;
5180
5181 case ISD::BUILD_VECTOR:
5182 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5183 // this shouldn't affect the result.
5184 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5185 if (!DemandedElts[i])
5186 continue;
5188 Depth + 1))
5189 return false;
5190 }
5191 return true;
5192
5193 case ISD::VECTOR_SHUFFLE: {
5194 APInt DemandedLHS, DemandedRHS;
5195 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5196 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5197 DemandedElts, DemandedLHS, DemandedRHS,
5198 /*AllowUndefElts=*/false))
5199 return false;
5200 if (!DemandedLHS.isZero() &&
5201 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5202 PoisonOnly, Depth + 1))
5203 return false;
5204 if (!DemandedRHS.isZero() &&
5205 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5206 PoisonOnly, Depth + 1))
5207 return false;
5208 return true;
5209 }
5210
5211 // TODO: Search for noundef attributes from library functions.
5212
5213 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5214
5215 default:
5216 // Allow the target to implement this method for its nodes.
5217 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5218 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5220 Op, DemandedElts, *this, PoisonOnly, Depth);
5221 break;
5222 }
5223
5224 // If Op can't create undef/poison and none of its operands are undef/poison
5225 // then Op is never undef/poison.
5226 // NOTE: TargetNodes can handle this in themselves in
5227 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5228 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5229 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5230 Depth) &&
5231 all_of(Op->ops(), [&](SDValue V) {
5232 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5233 });
5234}
5235
5237 bool ConsiderFlags,
5238 unsigned Depth) const {
5239 // TODO: Assume we don't know anything for now.
5240 EVT VT = Op.getValueType();
5241 if (VT.isScalableVector())
5242 return true;
5243
5244 APInt DemandedElts = VT.isVector()
5246 : APInt(1, 1);
5247 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5248 Depth);
5249}
5250
5252 bool PoisonOnly, bool ConsiderFlags,
5253 unsigned Depth) const {
5254 // TODO: Assume we don't know anything for now.
5255 EVT VT = Op.getValueType();
5256 if (VT.isScalableVector())
5257 return true;
5258
5259 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5260 return true;
5261
5262 unsigned Opcode = Op.getOpcode();
5263 switch (Opcode) {
5264 case ISD::FREEZE:
5267 case ISD::SADDSAT:
5268 case ISD::UADDSAT:
5269 case ISD::SSUBSAT:
5270 case ISD::USUBSAT:
5271 case ISD::MULHU:
5272 case ISD::MULHS:
5273 case ISD::SMIN:
5274 case ISD::SMAX:
5275 case ISD::UMIN:
5276 case ISD::UMAX:
5277 case ISD::AND:
5278 case ISD::XOR:
5279 case ISD::ROTL:
5280 case ISD::ROTR:
5281 case ISD::FSHL:
5282 case ISD::FSHR:
5283 case ISD::BSWAP:
5284 case ISD::CTPOP:
5285 case ISD::BITREVERSE:
5286 case ISD::PARITY:
5287 case ISD::SIGN_EXTEND:
5288 case ISD::TRUNCATE:
5292 case ISD::BITCAST:
5293 case ISD::BUILD_VECTOR:
5294 case ISD::BUILD_PAIR:
5295 return false;
5296
5297 case ISD::SELECT_CC:
5298 case ISD::SETCC: {
5299 // Integer setcc cannot create undef or poison.
5300 if (Op.getOperand(0).getValueType().isInteger())
5301 return false;
5302
5303 // FP compares are more complicated. They can create poison for nan/infinity
5304 // based on options and flags. The options and flags also cause special
5305 // nonan condition codes to be used. Those condition codes may be preserved
5306 // even if the nonan flag is dropped somewhere.
5307 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5308 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5309 if (((unsigned)CCCode & 0x10U))
5310 return true;
5311
5313 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5314 }
5315
5316 case ISD::OR:
5317 case ISD::ZERO_EXTEND:
5318 case ISD::ADD:
5319 case ISD::SUB:
5320 case ISD::MUL:
5321 // No poison except from flags (which is handled above)
5322 return false;
5323
5324 case ISD::SHL:
5325 case ISD::SRL:
5326 case ISD::SRA:
5327 // If the max shift amount isn't in range, then the shift can
5328 // create poison.
5329 return !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedElts,
5330 PoisonOnly, Depth + 1) ||
5331 !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5332
5334 // Check if we demand any upper (undef) elements.
5335 return !PoisonOnly && DemandedElts.ugt(1);
5336
5339 // Ensure that the element index is in bounds.
5340 EVT VecVT = Op.getOperand(0).getValueType();
5341 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5343 Depth + 1)) {
5344 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5345 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5346 }
5347 return true;
5348 }
5349
5350 case ISD::VECTOR_SHUFFLE: {
5351 // Check for any demanded shuffle element that is undef.
5352 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5353 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5354 if (Elt < 0 && DemandedElts[Idx])
5355 return true;
5356 return false;
5357 }
5358
5359 default:
5360 // Allow the target to implement this method for its nodes.
5361 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5362 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5364 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5365 break;
5366 }
5367
5368 // Be conservative and return true.
5369 return true;
5370}
5371
5372bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5373 unsigned Opcode = Op.getOpcode();
5374 if (Opcode == ISD::OR)
5375 return Op->getFlags().hasDisjoint() ||
5376 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5377 if (Opcode == ISD::XOR)
5378 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5379 return false;
5380}
5381
5383 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5384 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5385}
5386
5387bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5388 // If we're told that NaNs won't happen, assume they won't.
5389 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5390 return true;
5391
5392 if (Depth >= MaxRecursionDepth)
5393 return false; // Limit search depth.
5394
5395 // If the value is a constant, we can obviously see if it is a NaN or not.
5396 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5397 return !C->getValueAPF().isNaN() ||
5398 (SNaN && !C->getValueAPF().isSignaling());
5399 }
5400
5401 unsigned Opcode = Op.getOpcode();
5402 switch (Opcode) {
5403 case ISD::FADD:
5404 case ISD::FSUB:
5405 case ISD::FMUL:
5406 case ISD::FDIV:
5407 case ISD::FREM:
5408 case ISD::FSIN:
5409 case ISD::FCOS:
5410 case ISD::FTAN:
5411 case ISD::FASIN:
5412 case ISD::FACOS:
5413 case ISD::FATAN:
5414 case ISD::FSINH:
5415 case ISD::FCOSH:
5416 case ISD::FTANH:
5417 case ISD::FMA:
5418 case ISD::FMAD: {
5419 if (SNaN)
5420 return true;
5421 // TODO: Need isKnownNeverInfinity
5422 return false;
5423 }
5424 case ISD::FCANONICALIZE:
5425 case ISD::FEXP:
5426 case ISD::FEXP2:
5427 case ISD::FEXP10:
5428 case ISD::FTRUNC:
5429 case ISD::FFLOOR:
5430 case ISD::FCEIL:
5431 case ISD::FROUND:
5432 case ISD::FROUNDEVEN:
5433 case ISD::FRINT:
5434 case ISD::LRINT:
5435 case ISD::LLRINT:
5436 case ISD::FNEARBYINT:
5437 case ISD::FLDEXP: {
5438 if (SNaN)
5439 return true;
5440 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5441 }
5442 case ISD::FABS:
5443 case ISD::FNEG:
5444 case ISD::FCOPYSIGN: {
5445 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5446 }
5447 case ISD::SELECT:
5448 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5449 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5450 case ISD::FP_EXTEND:
5451 case ISD::FP_ROUND: {
5452 if (SNaN)
5453 return true;
5454 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5455 }
5456 case ISD::SINT_TO_FP:
5457 case ISD::UINT_TO_FP:
5458 return true;
5459 case ISD::FSQRT: // Need is known positive
5460 case ISD::FLOG:
5461 case ISD::FLOG2:
5462 case ISD::FLOG10:
5463 case ISD::FPOWI:
5464 case ISD::FPOW: {
5465 if (SNaN)
5466 return true;
5467 // TODO: Refine on operand
5468 return false;
5469 }
5470 case ISD::FMINNUM:
5471 case ISD::FMAXNUM: {
5472 // Only one needs to be known not-nan, since it will be returned if the
5473 // other ends up being one.
5474 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5475 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5476 }
5477 case ISD::FMINNUM_IEEE:
5478 case ISD::FMAXNUM_IEEE: {
5479 if (SNaN)
5480 return true;
5481 // This can return a NaN if either operand is an sNaN, or if both operands
5482 // are NaN.
5483 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5484 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5485 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5486 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5487 }
5488 case ISD::FMINIMUM:
5489 case ISD::FMAXIMUM: {
5490 // TODO: Does this quiet or return the origina NaN as-is?
5491 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5492 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5493 }
5495 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5496 }
5497 case ISD::BUILD_VECTOR: {
5498 for (const SDValue &Opnd : Op->ops())
5499 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5500 return false;
5501 return true;
5502 }
5503 default:
5504 if (Opcode >= ISD::BUILTIN_OP_END ||
5505 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5506 Opcode == ISD::INTRINSIC_W_CHAIN ||
5507 Opcode == ISD::INTRINSIC_VOID) {
5508 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5509 }
5510
5511 return false;
5512 }
5513}
5514
5516 assert(Op.getValueType().isFloatingPoint() &&
5517 "Floating point type expected");
5518
5519 // If the value is a constant, we can obviously see if it is a zero or not.
5521 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5522}
5523
5525 if (Depth >= MaxRecursionDepth)
5526 return false; // Limit search depth.
5527
5528 assert(!Op.getValueType().isFloatingPoint() &&
5529 "Floating point types unsupported - use isKnownNeverZeroFloat");
5530
5531 // If the value is a constant, we can obviously see if it is a zero or not.
5533 [](ConstantSDNode *C) { return !C->isZero(); }))
5534 return true;
5535
5536 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5537 // some degree.
5538 switch (Op.getOpcode()) {
5539 default:
5540 break;
5541
5542 case ISD::OR:
5543 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5544 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5545
5546 case ISD::VSELECT:
5547 case ISD::SELECT:
5548 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5549 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5550
5551 case ISD::SHL: {
5552 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5553 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5554 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5555 // 1 << X is never zero.
5556 if (ValKnown.One[0])
5557 return true;
5558 // If max shift cnt of known ones is non-zero, result is non-zero.
5559 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5560 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5561 !ValKnown.One.shl(MaxCnt).isZero())
5562 return true;
5563 break;
5564 }
5565 case ISD::UADDSAT:
5566 case ISD::UMAX:
5567 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5568 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5569
5570 // For smin/smax: If either operand is known negative/positive
5571 // respectively we don't need the other to be known at all.
5572 case ISD::SMAX: {
5573 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5574 if (Op1.isStrictlyPositive())
5575 return true;
5576
5577 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5578 if (Op0.isStrictlyPositive())
5579 return true;
5580
5581 if (Op1.isNonZero() && Op0.isNonZero())
5582 return true;
5583
5584 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5585 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5586 }
5587 case ISD::SMIN: {
5588 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5589 if (Op1.isNegative())
5590 return true;
5591
5592 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5593 if (Op0.isNegative())
5594 return true;
5595
5596 if (Op1.isNonZero() && Op0.isNonZero())
5597 return true;
5598
5599 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5600 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5601 }
5602 case ISD::UMIN:
5603 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5604 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5605
5606 case ISD::ROTL:
5607 case ISD::ROTR:
5608 case ISD::BITREVERSE:
5609 case ISD::BSWAP:
5610 case ISD::CTPOP:
5611 case ISD::ABS:
5612 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5613
5614 case ISD::SRA:
5615 case ISD::SRL: {
5616 if (Op->getFlags().hasExact())
5617 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5618 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5619 if (ValKnown.isNegative())
5620 return true;
5621 // If max shift cnt of known ones is non-zero, result is non-zero.
5622 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5623 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5624 !ValKnown.One.lshr(MaxCnt).isZero())
5625 return true;
5626 break;
5627 }
5628 case ISD::UDIV:
5629 case ISD::SDIV:
5630 // div exact can only produce a zero if the dividend is zero.
5631 // TODO: For udiv this is also true if Op1 u<= Op0
5632 if (Op->getFlags().hasExact())
5633 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5634 break;
5635
5636 case ISD::ADD:
5637 if (Op->getFlags().hasNoUnsignedWrap())
5638 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5639 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5640 return true;
5641 // TODO: There are a lot more cases we can prove for add.
5642 break;
5643
5644 case ISD::SUB: {
5645 if (isNullConstant(Op.getOperand(0)))
5646 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5647
5648 std::optional<bool> ne =
5649 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5650 computeKnownBits(Op.getOperand(1), Depth + 1));
5651 return ne && *ne;
5652 }
5653
5654 case ISD::MUL:
5655 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5656 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5657 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5658 return true;
5659 break;
5660
5661 case ISD::ZERO_EXTEND:
5662 case ISD::SIGN_EXTEND:
5663 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5664 case ISD::VSCALE: {
5666 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
5667 ConstantRange CR =
5668 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
5669 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
5670 return true;
5671 break;
5672 }
5673 }
5674
5676}
5677
5679 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
5680 return !C1->isNegative();
5681
5682 return Op.getOpcode() == ISD::FABS;
5683}
5684
5686 // Check the obvious case.
5687 if (A == B) return true;
5688
5689 // For negative and positive zero.
5690 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5691 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5692 if (CA->isZero() && CB->isZero()) return true;
5693
5694 // Otherwise they may not be equal.
5695 return false;
5696}
5697
5698// Only bits set in Mask must be negated, other bits may be arbitrary.
5700 if (isBitwiseNot(V, AllowUndefs))
5701 return V.getOperand(0);
5702
5703 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5704 // bits in the non-extended part.
5705 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5706 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5707 return SDValue();
5708 SDValue ExtArg = V.getOperand(0);
5709 if (ExtArg.getScalarValueSizeInBits() >=
5710 MaskC->getAPIntValue().getActiveBits() &&
5711 isBitwiseNot(ExtArg, AllowUndefs) &&
5712 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
5713 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
5714 return ExtArg.getOperand(0).getOperand(0);
5715 return SDValue();
5716}
5717
5719 // Match masked merge pattern (X & ~M) op (Y & M)
5720 // Including degenerate case (X & ~M) op M
5721 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
5722 SDValue Other) {
5723 if (SDValue NotOperand =
5724 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
5725 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
5726 NotOperand->getOpcode() == ISD::TRUNCATE)
5727 NotOperand = NotOperand->getOperand(0);
5728
5729 if (Other == NotOperand)
5730 return true;
5731 if (Other->getOpcode() == ISD::AND)
5732 return NotOperand == Other->getOperand(0) ||
5733 NotOperand == Other->getOperand(1);
5734 }
5735 return false;
5736 };
5737
5738 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
5739 A = A->getOperand(0);
5740
5741 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
5742 B = B->getOperand(0);
5743
5744 if (A->getOpcode() == ISD::AND)
5745 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
5746 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
5747 return false;
5748}
5749
5750// FIXME: unify with llvm::haveNoCommonBitsSet.
5752 assert(A.getValueType() == B.getValueType() &&
5753 "Values must have the same type");
5756 return true;
5759}
5760
5761static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
5762 SelectionDAG &DAG) {
5763 if (cast<ConstantSDNode>(Step)->isZero())
5764 return DAG.getConstant(0, DL, VT);
5765
5766 return SDValue();
5767}
5768
5771 SelectionDAG &DAG) {
5772 int NumOps = Ops.size();
5773 assert(NumOps != 0 && "Can't build an empty vector!");
5774 assert(!VT.isScalableVector() &&
5775 "BUILD_VECTOR cannot be used with scalable types");
5776 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
5777 "Incorrect element count in BUILD_VECTOR!");
5778
5779 // BUILD_VECTOR of UNDEFs is UNDEF.
5780 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5781 return DAG.getUNDEF(VT);
5782
5783 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
5784 SDValue IdentitySrc;
5785 bool IsIdentity = true;
5786 for (int i = 0; i != NumOps; ++i) {
5787 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5788 Ops[i].getOperand(0).getValueType() != VT ||
5789 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
5790 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
5791 Ops[i].getConstantOperandAPInt(1) != i) {
5792 IsIdentity = false;
5793 break;
5794 }
5795 IdentitySrc = Ops[i].getOperand(0);
5796 }
5797 if (IsIdentity)
5798 return IdentitySrc;
5799
5800 return SDValue();
5801}
5802
5803/// Try to simplify vector concatenation to an input value, undef, or build
5804/// vector.
5807 SelectionDAG &DAG) {
5808 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
5809 assert(llvm::all_of(Ops,
5810 [Ops](SDValue Op) {
5811 return Ops[0].getValueType() == Op.getValueType();
5812 }) &&
5813 "Concatenation of vectors with inconsistent value types!");
5814 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
5815 VT.getVectorElementCount() &&
5816 "Incorrect element count in vector concatenation!");
5817
5818 if (Ops.size() == 1)
5819 return Ops[0];
5820
5821 // Concat of UNDEFs is UNDEF.
5822 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5823 return DAG.getUNDEF(VT);
5824
5825 // Scan the operands and look for extract operations from a single source
5826 // that correspond to insertion at the same location via this concatenation:
5827 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
5828 SDValue IdentitySrc;
5829 bool IsIdentity = true;
5830 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
5831 SDValue Op = Ops[i];
5832 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
5833 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
5834 Op.getOperand(0).getValueType() != VT ||
5835 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
5836 Op.getConstantOperandVal(1) != IdentityIndex) {
5837 IsIdentity = false;
5838 break;
5839 }
5840 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
5841 "Unexpected identity source vector for concat of extracts");
5842 IdentitySrc = Op.getOperand(0);
5843 }
5844 if (IsIdentity) {
5845 assert(IdentitySrc && "Failed to set source vector of extracts");
5846 return IdentitySrc;
5847 }
5848
5849 // The code below this point is only designed to work for fixed width
5850 // vectors, so we bail out for now.
5851 if (VT.isScalableVector())
5852 return SDValue();
5853
5854 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
5855 // simplified to one big BUILD_VECTOR.
5856 // FIXME: Add support for SCALAR_TO_VECTOR as well.
5857 EVT SVT = VT.getScalarType();
5859 for (SDValue Op : Ops) {
5860 EVT OpVT = Op.getValueType();
5861 if (Op.isUndef())
5862 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
5863 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
5864 Elts.append(Op->op_begin(), Op->op_end());
5865 else
5866 return SDValue();
5867 }
5868
5869 // BUILD_VECTOR requires all inputs to be of the same type, find the
5870 // maximum type and extend them all.
5871 for (SDValue Op : Elts)
5872 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
5873
5874 if (SVT.bitsGT(VT.getScalarType())) {
5875 for (SDValue &Op : Elts) {
5876 if (Op.isUndef())
5877 Op = DAG.getUNDEF(SVT);
5878 else
5879 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
5880 ? DAG.getZExtOrTrunc(Op, DL, SVT)
5881 : DAG.getSExtOrTrunc(Op, DL, SVT);
5882 }
5883 }
5884
5885 SDValue V = DAG.getBuildVector(VT, DL, Elts);
5886 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
5887 return V;
5888}
5889
5890/// Gets or creates the specified node.
5891SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
5892 SDVTList VTs = getVTList(VT);
5894 AddNodeIDNode(ID, Opcode, VTs, std::nullopt);
5895 void *IP = nullptr;
5896 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5897 return SDValue(E, 0);
5898
5899 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5900 CSEMap.InsertNode(N, IP);
5901
5902 InsertNode(N);
5903 SDValue V = SDValue(N, 0);
5904 NewSDValueDbgMsg(V, "Creating new node: ", this);
5905 return V;
5906}
5907
5908SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5909 SDValue N1) {
5910 SDNodeFlags Flags;
5911 if (Inserter)
5912 Flags = Inserter->getFlags();
5913 return getNode(Opcode, DL, VT, N1, Flags);
5914}
5915
5916SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5917 SDValue N1, const SDNodeFlags Flags) {
5918 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
5919
5920 // Constant fold unary operations with a vector integer or float operand.
5921 switch (Opcode) {
5922 default:
5923 // FIXME: Entirely reasonable to perform folding of other unary
5924 // operations here as the need arises.
5925 break;
5926 case ISD::FNEG:
5927 case ISD::FABS:
5928 case ISD::FCEIL:
5929 case ISD::FTRUNC:
5930 case ISD::FFLOOR:
5931 case ISD::FP_EXTEND:
5932 case ISD::FP_TO_SINT:
5933 case ISD::FP_TO_UINT:
5934 case ISD::FP_TO_FP16:
5935 case ISD::FP_TO_BF16:
5936 case ISD::TRUNCATE:
5937 case ISD::ANY_EXTEND:
5938 case ISD::ZERO_EXTEND:
5939 case ISD::SIGN_EXTEND:
5940 case ISD::UINT_TO_FP:
5941 case ISD::SINT_TO_FP:
5942 case ISD::FP16_TO_FP:
5943 case ISD::BF16_TO_FP:
5944 case ISD::BITCAST:
5945 case ISD::ABS:
5946 case ISD::BITREVERSE:
5947 case ISD::BSWAP:
5948 case ISD::CTLZ:
5950 case ISD::CTTZ:
5952 case ISD::CTPOP:
5953 case ISD::STEP_VECTOR: {
5954 SDValue Ops = {N1};
5955 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
5956 return Fold;
5957 }
5958 }
5959
5960 unsigned OpOpcode = N1.getNode()->getOpcode();
5961 switch (Opcode) {
5962 case ISD::STEP_VECTOR:
5963 assert(VT.isScalableVector() &&
5964 "STEP_VECTOR can only be used with scalable types");
5965 assert(OpOpcode == ISD::TargetConstant &&
5966 VT.getVectorElementType() == N1.getValueType() &&
5967 "Unexpected step operand");
5968 break;
5969 case ISD::FREEZE:
5970 assert(VT == N1.getValueType() && "Unexpected VT!");
5971 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
5972 /*Depth*/ 1))
5973 return N1;
5974 break;
5975 case ISD::TokenFactor:
5976 case ISD::MERGE_VALUES:
5978 return N1; // Factor, merge or concat of one node? No need.
5979 case ISD::BUILD_VECTOR: {
5980 // Attempt to simplify BUILD_VECTOR.
5981 SDValue Ops[] = {N1};
5982 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5983 return V;
5984 break;
5985 }
5986 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
5987 case ISD::FP_EXTEND:
5989 "Invalid FP cast!");
5990 if (N1.getValueType() == VT) return N1; // noop conversion.
5991 assert((!VT.isVector() || VT.getVectorElementCount() ==
5993 "Vector element count mismatch!");
5994 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
5995 if (N1.isUndef())
5996 return getUNDEF(VT);
5997 break;
5998 case ISD::FP_TO_SINT:
5999 case ISD::FP_TO_UINT:
6000 if (N1.isUndef())
6001 return getUNDEF(VT);
6002 break;
6003 case ISD::SINT_TO_FP:
6004 case ISD::UINT_TO_FP:
6005 // [us]itofp(undef) = 0, because the result value is bounded.
6006 if (N1.isUndef())
6007 return getConstantFP(0.0, DL, VT);
6008 break;
6009 case ISD::SIGN_EXTEND:
6010 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6011 "Invalid SIGN_EXTEND!");
6012 assert(VT.isVector() == N1.getValueType().isVector() &&
6013 "SIGN_EXTEND result type type should be vector iff the operand "
6014 "type is vector!");
6015 if (N1.getValueType() == VT) return N1; // noop extension
6016 assert((!VT.isVector() || VT.getVectorElementCount() ==
6018 "Vector element count mismatch!");
6019 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6020 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6021 SDNodeFlags Flags;
6022 if (OpOpcode == ISD::ZERO_EXTEND)
6023 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6024 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6025 }
6026 if (OpOpcode == ISD::UNDEF)
6027 // sext(undef) = 0, because the top bits will all be the same.
6028 return getConstant(0, DL, VT);
6029 break;
6030 case ISD::ZERO_EXTEND:
6031 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6032 "Invalid ZERO_EXTEND!");
6033 assert(VT.isVector() == N1.getValueType().isVector() &&
6034 "ZERO_EXTEND result type type should be vector iff the operand "
6035 "type is vector!");
6036 if (N1.getValueType() == VT) return N1; // noop extension
6037 assert((!VT.isVector() || VT.getVectorElementCount() ==
6039 "Vector element count mismatch!");
6040 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6041 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6042 SDNodeFlags Flags;
6043 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6044 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6045 }
6046 if (OpOpcode == ISD::UNDEF)
6047 // zext(undef) = 0, because the top bits will be zero.
6048 return getConstant(0, DL, VT);
6049
6050 // Skip unnecessary zext_inreg pattern:
6051 // (zext (trunc x)) -> x iff the upper bits are known zero.
6052 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6053 // use to recognise zext_inreg patterns.
6054 if (OpOpcode == ISD::TRUNCATE) {
6055 SDValue OpOp = N1.getOperand(0);
6056 if (OpOp.getValueType() == VT) {
6057 if (OpOp.getOpcode() != ISD::AND) {
6060 if (MaskedValueIsZero(OpOp, HiBits)) {
6061 transferDbgValues(N1, OpOp);
6062 return OpOp;
6063 }
6064 }
6065 }
6066 }
6067 break;
6068 case ISD::ANY_EXTEND:
6069 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6070 "Invalid ANY_EXTEND!");
6071 assert(VT.isVector() == N1.getValueType().isVector() &&
6072 "ANY_EXTEND result type type should be vector iff the operand "
6073 "type is vector!");
6074 if (N1.getValueType() == VT) return N1; // noop extension
6075 assert((!VT.isVector() || VT.getVectorElementCount() ==
6077 "Vector element count mismatch!");
6078 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6079
6080 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6081 OpOpcode == ISD::ANY_EXTEND) {
6082 SDNodeFlags Flags;
6083 if (OpOpcode == ISD::ZERO_EXTEND)
6084 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6085 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6086 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6087 }
6088 if (OpOpcode == ISD::UNDEF)
6089 return getUNDEF(VT);
6090
6091 // (ext (trunc x)) -> x
6092 if (OpOpcode == ISD::TRUNCATE) {
6093 SDValue OpOp = N1.getOperand(0);
6094 if (OpOp.getValueType() == VT) {
6095 transferDbgValues(N1, OpOp);
6096 return OpOp;
6097 }
6098 }
6099 break;
6100 case ISD::TRUNCATE:
6101 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6102 "Invalid TRUNCATE!");
6103 assert(VT.isVector() == N1.getValueType().isVector() &&
6104 "TRUNCATE result type type should be vector iff the operand "
6105 "type is vector!");
6106 if (N1.getValueType() == VT) return N1; // noop truncate
6107 assert((!VT.isVector() || VT.getVectorElementCount() ==
6109 "Vector element count mismatch!");
6110 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6111 if (OpOpcode == ISD::TRUNCATE)
6112 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6113 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6114 OpOpcode == ISD::ANY_EXTEND) {
6115 // If the source is smaller than the dest, we still need an extend.
6117 VT.getScalarType()))
6118 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
6119 if (N1.getOperand(0).getValueType().bitsGT(VT))
6120 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6121 return N1.getOperand(0);
6122 }
6123 if (OpOpcode == ISD::UNDEF)
6124 return getUNDEF(VT);
6125 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6126 return getVScale(DL, VT,
6128 break;
6132 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6133 assert(N1.getValueType().bitsLE(VT) &&
6134 "The input must be the same size or smaller than the result.");
6137 "The destination vector type must have fewer lanes than the input.");
6138 break;
6139 case ISD::ABS:
6140 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6141 if (OpOpcode == ISD::UNDEF)
6142 return getConstant(0, DL, VT);
6143 break;
6144 case ISD::BSWAP:
6145 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6146 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6147 "BSWAP types must be a multiple of 16 bits!");
6148 if (OpOpcode == ISD::UNDEF)
6149 return getUNDEF(VT);
6150 // bswap(bswap(X)) -> X.
6151 if (OpOpcode == ISD::BSWAP)
6152 return N1.getOperand(0);
6153 break;
6154 case ISD::BITREVERSE:
6155 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6156 if (OpOpcode == ISD::UNDEF)
6157 return getUNDEF(VT);
6158 break;
6159 case ISD::BITCAST:
6161 "Cannot BITCAST between types of different sizes!");
6162 if (VT == N1.getValueType()) return N1; // noop conversion.
6163 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6164 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6165 if (OpOpcode == ISD::UNDEF)
6166 return getUNDEF(VT);
6167 break;
6169 assert(VT.isVector() && !N1.getValueType().isVector() &&
6170 (VT.getVectorElementType() == N1.getValueType() ||
6172 N1.getValueType().isInteger() &&
6174 "Illegal SCALAR_TO_VECTOR node!");
6175 if (OpOpcode == ISD::UNDEF)
6176 return getUNDEF(VT);
6177 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6178 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6179 isa<ConstantSDNode>(N1.getOperand(1)) &&
6180 N1.getConstantOperandVal(1) == 0 &&
6181 N1.getOperand(0).getValueType() == VT)
6182 return N1.getOperand(0);
6183 break;
6184 case ISD::FNEG:
6185 // Negation of an unknown bag of bits is still completely undefined.
6186 if (OpOpcode == ISD::UNDEF)
6187 return getUNDEF(VT);
6188
6189 if (OpOpcode == ISD::FNEG) // --X -> X
6190 return N1.getOperand(0);
6191 break;
6192 case ISD::FABS:
6193 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6194 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6195 break;
6196 case ISD::VSCALE:
6197 assert(VT == N1.getValueType() && "Unexpected VT!");
6198 break;
6199 case ISD::CTPOP:
6200 if (N1.getValueType().getScalarType() == MVT::i1)
6201 return N1;
6202 break;
6203 case ISD::CTLZ:
6204 case ISD::CTTZ:
6205 if (N1.getValueType().getScalarType() == MVT::i1)
6206 return getNOT(DL, N1, N1.getValueType());
6207 break;
6208 case ISD::VECREDUCE_ADD:
6209 if (N1.getValueType().getScalarType() == MVT::i1)
6210 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6211 break;
6214 if (N1.getValueType().getScalarType() == MVT::i1)
6215 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6216 break;
6219 if (N1.getValueType().getScalarType() == MVT::i1)
6220 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6221 break;
6222 case ISD::SPLAT_VECTOR:
6223 assert(VT.isVector() && "Wrong return type!");
6224 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6225 // that for now.
6227 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6229 N1.getValueType().isInteger() &&
6231 "Wrong operand type!");
6232 break;
6233 }
6234
6235 SDNode *N;
6236 SDVTList VTs = getVTList(VT);
6237 SDValue Ops[] = {N1};
6238 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6240 AddNodeIDNode(ID, Opcode, VTs, Ops);
6241 void *IP = nullptr;
6242 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6243 E->intersectFlagsWith(Flags);
6244 return SDValue(E, 0);
6245 }
6246
6247 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6248 N->setFlags(Flags);
6249 createOperands(N, Ops);
6250 CSEMap.InsertNode(N, IP);
6251 } else {
6252 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6253 createOperands(N, Ops);
6254 }
6255
6256 InsertNode(N);
6257 SDValue V = SDValue(N, 0);
6258 NewSDValueDbgMsg(V, "Creating new node: ", this);
6259 return V;
6260}
6261
6262static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6263 const APInt &C2) {
6264 switch (Opcode) {
6265 case ISD::ADD: return C1 + C2;
6266 case ISD::SUB: return C1 - C2;
6267 case ISD::MUL: return C1 * C2;
6268 case ISD::AND: return C1 & C2;
6269 case ISD::OR: return C1 | C2;
6270 case ISD::XOR: return C1 ^ C2;
6271 case ISD::SHL: return C1 << C2;
6272 case ISD::SRL: return C1.lshr(C2);
6273 case ISD::SRA: return C1.ashr(C2);
6274 case ISD::ROTL: return C1.rotl(C2);
6275 case ISD::ROTR: return C1.rotr(C2);
6276 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6277 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6278 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6279 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6280 case ISD::SADDSAT: return C1.sadd_sat(C2);
6281 case ISD::UADDSAT: return C1.uadd_sat(C2);
6282 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6283 case ISD::USUBSAT: return C1.usub_sat(C2);
6284 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6285 case ISD::USHLSAT: return C1.ushl_sat(C2);
6286 case ISD::UDIV:
6287 if (!C2.getBoolValue())
6288 break;
6289 return C1.udiv(C2);
6290 case ISD::UREM:
6291 if (!C2.getBoolValue())
6292 break;
6293 return C1.urem(C2);
6294 case ISD::SDIV:
6295 if (!C2.getBoolValue())
6296 break;
6297 return C1.sdiv(C2);
6298 case ISD::SREM:
6299 if (!C2.getBoolValue())
6300 break;
6301 return C1.srem(C2);
6302 case ISD::AVGFLOORS:
6303 return APIntOps::avgFloorS(C1, C2);
6304 case ISD::AVGFLOORU:
6305 return APIntOps::avgFloorU(C1, C2);
6306 case ISD::AVGCEILS:
6307 return APIntOps::avgCeilS(C1, C2);
6308 case ISD::AVGCEILU:
6309 return APIntOps::avgCeilU(C1, C2);
6310 case ISD::ABDS:
6311 return APIntOps::abds(C1, C2);
6312 case ISD::ABDU:
6313 return APIntOps::abdu(C1, C2);
6314 case ISD::MULHS:
6315 return APIntOps::mulhs(C1, C2);
6316 case ISD::MULHU:
6317 return APIntOps::mulhu(C1, C2);
6318 }
6319 return std::nullopt;
6320}
6321// Handle constant folding with UNDEF.
6322// TODO: Handle more cases.
6323static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6324 bool IsUndef1, const APInt &C2,
6325 bool IsUndef2) {
6326 if (!(IsUndef1 || IsUndef2))
6327 return FoldValue(Opcode, C1, C2);
6328
6329 // Fold and(x, undef) -> 0
6330 // Fold mul(x, undef) -> 0
6331 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6332 return APInt::getZero(C1.getBitWidth());
6333
6334 return std::nullopt;
6335}
6336
6338 const GlobalAddressSDNode *GA,
6339 const SDNode *N2) {
6340 if (GA->getOpcode() != ISD::GlobalAddress)
6341 return SDValue();
6342 if (!TLI->isOffsetFoldingLegal(GA))
6343 return SDValue();
6344 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6345 if (!C2)
6346 return SDValue();
6347 int64_t Offset = C2->getSExtValue();
6348 switch (Opcode) {
6349 case ISD::ADD: break;
6350 case ISD::SUB: Offset = -uint64_t(Offset); break;
6351 default: return SDValue();
6352 }
6353 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6354 GA->getOffset() + uint64_t(Offset));
6355}
6356
6357bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6358 switch (Opcode) {
6359 case ISD::SDIV:
6360 case ISD::UDIV:
6361 case ISD::SREM:
6362 case ISD::UREM: {
6363 // If a divisor is zero/undef or any element of a divisor vector is
6364 // zero/undef, the whole op is undef.
6365 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6366 SDValue Divisor = Ops[1];
6367 if (Divisor.isUndef() || isNullConstant(Divisor))
6368 return true;
6369
6370 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6371 llvm::any_of(Divisor->op_values(),
6372 [](SDValue V) { return V.isUndef() ||
6373 isNullConstant(V); });
6374 // TODO: Handle signed overflow.
6375 }
6376 // TODO: Handle oversized shifts.
6377 default:
6378 return false;
6379 }
6380}
6381
6383 EVT VT, ArrayRef<SDValue> Ops,
6384 SDNodeFlags Flags) {
6385 // If the opcode is a target-specific ISD node, there's nothing we can
6386 // do here and the operand rules may not line up with the below, so
6387 // bail early.
6388 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6389 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6390 // foldCONCAT_VECTORS in getNode before this is called.
6391 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6392 return SDValue();
6393
6394 unsigned NumOps = Ops.size();
6395 if (NumOps == 0)
6396 return SDValue();
6397
6398 if (isUndef(Opcode, Ops))
6399 return getUNDEF(VT);
6400
6401 // Handle unary special cases.
6402 if (NumOps == 1) {
6403 SDValue N1 = Ops[0];
6404
6405 // Constant fold unary operations with an integer constant operand. Even
6406 // opaque constant will be folded, because the folding of unary operations
6407 // doesn't create new constants with different values. Nevertheless, the
6408 // opaque flag is preserved during folding to prevent future folding with
6409 // other constants.
6410 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6411 const APInt &Val = C->getAPIntValue();
6412 switch (Opcode) {
6413 case ISD::SIGN_EXTEND:
6414 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6415 C->isTargetOpcode(), C->isOpaque());
6416 case ISD::TRUNCATE:
6417 if (C->isOpaque())
6418 break;
6419 [[fallthrough]];
6420 case ISD::ZERO_EXTEND:
6421 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6422 C->isTargetOpcode(), C->isOpaque());
6423 case ISD::ANY_EXTEND:
6424 // Some targets like RISCV prefer to sign extend some types.
6425 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6426 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6427 C->isTargetOpcode(), C->isOpaque());
6428 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6429 C->isTargetOpcode(), C->isOpaque());
6430 case ISD::ABS:
6431 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6432 C->isOpaque());
6433 case ISD::BITREVERSE:
6434 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6435 C->isOpaque());
6436 case ISD::BSWAP:
6437 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6438 C->isOpaque());
6439 case ISD::CTPOP:
6440 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6441 C->isOpaque());
6442 case ISD::CTLZ:
6444 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6445 C->isOpaque());
6446 case ISD::CTTZ:
6448 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6449 C->isOpaque());
6450 case ISD::UINT_TO_FP:
6451 case ISD::SINT_TO_FP: {
6454 (void)apf.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6456 return getConstantFP(apf, DL, VT);
6457 }
6458 case ISD::FP16_TO_FP:
6459 case ISD::BF16_TO_FP: {
6460 bool Ignored;
6461 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6462 : APFloat::BFloat(),
6463 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6464
6465 // This can return overflow, underflow, or inexact; we don't care.
6466 // FIXME need to be more flexible about rounding mode.
6467 (void)FPV.convert(EVTToAPFloatSemantics(VT),
6469 return getConstantFP(FPV, DL, VT);
6470 }
6471 case ISD::STEP_VECTOR:
6472 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6473 return V;
6474 break;
6475 case ISD::BITCAST:
6476 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6477 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6478 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6479 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6480 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6481 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6482 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6483 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6484 break;
6485 }
6486 }
6487
6488 // Constant fold unary operations with a floating point constant operand.
6489 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6490 APFloat V = C->getValueAPF(); // make copy
6491 switch (Opcode) {
6492 case ISD::FNEG:
6493 V.changeSign();
6494 return getConstantFP(V, DL, VT);
6495 case ISD::FABS:
6496 V.clearSign();
6497 return getConstantFP(V, DL, VT);
6498 case ISD::FCEIL: {
6499 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6500 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6501 return getConstantFP(V, DL, VT);
6502 return SDValue();
6503 }
6504 case ISD::FTRUNC: {
6505 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6506 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6507 return getConstantFP(V, DL, VT);
6508 return SDValue();
6509 }
6510 case ISD::FFLOOR: {
6511 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6512 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6513 return getConstantFP(V, DL, VT);
6514 return SDValue();
6515 }
6516 case ISD::FP_EXTEND: {
6517 bool ignored;
6518 // This can return overflow, underflow, or inexact; we don't care.
6519 // FIXME need to be more flexible about rounding mode.
6521 &ignored);
6522 return getConstantFP(V, DL, VT);
6523 }
6524 case ISD::FP_TO_SINT:
6525 case ISD::FP_TO_UINT: {
6526 bool ignored;
6527 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6528 // FIXME need to be more flexible about rounding mode.
6530 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6531 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6532 break;
6533 return getConstant(IntVal, DL, VT);
6534 }
6535 case ISD::FP_TO_FP16:
6536 case ISD::FP_TO_BF16: {
6537 bool Ignored;
6538 // This can return overflow, underflow, or inexact; we don't care.
6539 // FIXME need to be more flexible about rounding mode.
6540 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6541 : APFloat::BFloat(),
6543 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6544 }
6545 case ISD::BITCAST:
6546 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6547 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6548 VT);
6549 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6550 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6551 VT);
6552 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6553 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6554 VT);
6555 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6556 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6557 break;
6558 }
6559 }
6560
6561 // Early-out if we failed to constant fold a bitcast.
6562 if (Opcode == ISD::BITCAST)
6563 return SDValue();
6564 }
6565
6566 // Handle binops special cases.
6567 if (NumOps == 2) {
6568 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6569 return CFP;
6570
6571 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6572 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6573 if (C1->isOpaque() || C2->isOpaque())
6574 return SDValue();
6575
6576 std::optional<APInt> FoldAttempt =
6577 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6578 if (!FoldAttempt)
6579 return SDValue();
6580
6581 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6582 assert((!Folded || !VT.isVector()) &&
6583 "Can't fold vectors ops with scalar operands");
6584 return Folded;
6585 }
6586 }
6587
6588 // fold (add Sym, c) -> Sym+c
6589 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6590 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6591 if (TLI->isCommutativeBinOp(Opcode))
6592 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6593 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6594 }
6595
6596 // This is for vector folding only from here on.
6597 if (!VT.isVector())
6598 return SDValue();
6599
6600 ElementCount NumElts = VT.getVectorElementCount();
6601
6602 // See if we can fold through any bitcasted integer ops.
6603 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6604 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6605 (Ops[0].getOpcode() == ISD::BITCAST ||
6606 Ops[1].getOpcode() == ISD::BITCAST)) {
6607 SDValue N1 = peekThroughBitcasts(Ops[0]);
6608 SDValue N2 = peekThroughBitcasts(Ops[1]);
6609 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6610 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6611 if (BV1 && BV2 && N1.getValueType().isInteger() &&
6612 N2.getValueType().isInteger()) {
6613 bool IsLE = getDataLayout().isLittleEndian();
6614 unsigned EltBits = VT.getScalarSizeInBits();
6615 SmallVector<APInt> RawBits1, RawBits2;
6616 BitVector UndefElts1, UndefElts2;
6617 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6618 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6619 SmallVector<APInt> RawBits;
6620 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6621 std::optional<APInt> Fold = FoldValueWithUndef(
6622 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6623 if (!Fold)
6624 break;
6625 RawBits.push_back(*Fold);
6626 }
6627 if (RawBits.size() == NumElts.getFixedValue()) {
6628 // We have constant folded, but we might need to cast this again back
6629 // to the original (possibly legalized) type.
6630 EVT BVVT, BVEltVT;
6631 if (N1.getValueType() == VT) {
6632 BVVT = N1.getValueType();
6633 BVEltVT = BV1->getOperand(0).getValueType();
6634 } else {
6635 BVVT = N2.getValueType();
6636 BVEltVT = BV2->getOperand(0).getValueType();
6637 }
6638 unsigned BVEltBits = BVEltVT.getSizeInBits();
6639 SmallVector<APInt> DstBits;
6640 BitVector DstUndefs;
6642 DstBits, RawBits, DstUndefs,
6643 BitVector(RawBits.size(), false));
6644 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6645 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6646 if (DstUndefs[I])
6647 continue;
6648 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6649 }
6650 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6651 }
6652 }
6653 }
6654 }
6655
6656 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6657 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6658 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6659 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6660 APInt RHSVal;
6661 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6662 APInt NewStep = Opcode == ISD::MUL
6663 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6664 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6665 return getStepVector(DL, VT, NewStep);
6666 }
6667 }
6668
6669 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6670 return !Op.getValueType().isVector() ||
6671 Op.getValueType().getVectorElementCount() == NumElts;
6672 };
6673
6674 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
6675 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
6676 Op.getOpcode() == ISD::BUILD_VECTOR ||
6677 Op.getOpcode() == ISD::SPLAT_VECTOR;
6678 };
6679
6680 // All operands must be vector types with the same number of elements as
6681 // the result type and must be either UNDEF or a build/splat vector
6682 // or UNDEF scalars.
6683 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
6684 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
6685 return SDValue();
6686
6687 // If we are comparing vectors, then the result needs to be a i1 boolean that
6688 // is then extended back to the legal result type depending on how booleans
6689 // are represented.
6690 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
6691 ISD::NodeType ExtendCode =
6692 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
6695
6696 // Find legal integer scalar type for constant promotion and
6697 // ensure that its scalar size is at least as large as source.
6698 EVT LegalSVT = VT.getScalarType();
6699 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
6700 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
6701 if (LegalSVT.bitsLT(VT.getScalarType()))
6702 return SDValue();
6703 }
6704
6705 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
6706 // only have one operand to check. For fixed-length vector types we may have
6707 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
6708 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
6709
6710 // Constant fold each scalar lane separately.
6711 SmallVector<SDValue, 4> ScalarResults;
6712 for (unsigned I = 0; I != NumVectorElts; I++) {
6713 SmallVector<SDValue, 4> ScalarOps;
6714 for (SDValue Op : Ops) {
6715 EVT InSVT = Op.getValueType().getScalarType();
6716 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
6717 Op.getOpcode() != ISD::SPLAT_VECTOR) {
6718 if (Op.isUndef())
6719 ScalarOps.push_back(getUNDEF(InSVT));
6720 else
6721 ScalarOps.push_back(Op);
6722 continue;
6723 }
6724
6725 SDValue ScalarOp =
6726 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
6727 EVT ScalarVT = ScalarOp.getValueType();
6728
6729 // Build vector (integer) scalar operands may need implicit
6730 // truncation - do this before constant folding.
6731 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
6732 // Don't create illegally-typed nodes unless they're constants or undef
6733 // - if we fail to constant fold we can't guarantee the (dead) nodes
6734 // we're creating will be cleaned up before being visited for
6735 // legalization.
6736 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
6737 !isa<ConstantSDNode>(ScalarOp) &&
6738 TLI->getTypeAction(*getContext(), InSVT) !=
6740 return SDValue();
6741 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
6742 }
6743
6744 ScalarOps.push_back(ScalarOp);
6745 }
6746
6747 // Constant fold the scalar operands.
6748 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
6749
6750 // Legalize the (integer) scalar constant if necessary.
6751 if (LegalSVT != SVT)
6752 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
6753
6754 // Scalar folding only succeeded if the result is a constant or UNDEF.
6755 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
6756 ScalarResult.getOpcode() != ISD::ConstantFP)
6757 return SDValue();
6758 ScalarResults.push_back(ScalarResult);
6759 }
6760
6761 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
6762 : getBuildVector(VT, DL, ScalarResults);
6763 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
6764 return V;
6765}
6766
6768 EVT VT, ArrayRef<SDValue> Ops) {
6769 // TODO: Add support for unary/ternary fp opcodes.
6770 if (Ops.size() != 2)
6771 return SDValue();
6772
6773 // TODO: We don't do any constant folding for strict FP opcodes here, but we
6774 // should. That will require dealing with a potentially non-default
6775 // rounding mode, checking the "opStatus" return value from the APFloat
6776 // math calculations, and possibly other variations.
6777 SDValue N1 = Ops[0];
6778 SDValue N2 = Ops[1];
6779 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
6780 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
6781 if (N1CFP && N2CFP) {
6782 APFloat C1 = N1CFP->getValueAPF(); // make copy
6783 const APFloat &C2 = N2CFP->getValueAPF();
6784 switch (Opcode) {
6785 case ISD::FADD:
6787 return getConstantFP(C1, DL, VT);
6788 case ISD::FSUB:
6790 return getConstantFP(C1, DL, VT);
6791 case ISD::FMUL:
6793 return getConstantFP(C1, DL, VT);
6794 case ISD::FDIV:
6796 return getConstantFP(C1, DL, VT);
6797 case ISD::FREM:
6798 C1.mod(C2);
6799 return getConstantFP(C1, DL, VT);
6800 case ISD::FCOPYSIGN:
6801 C1.copySign(C2);
6802 return getConstantFP(C1, DL, VT);
6803 case ISD::FMINNUM:
6804 return getConstantFP(minnum(C1, C2), DL, VT);
6805 case ISD::FMAXNUM:
6806 return getConstantFP(maxnum(C1, C2), DL, VT);
6807 case ISD::FMINIMUM:
6808 return getConstantFP(minimum(C1, C2), DL, VT);
6809 case ISD::FMAXIMUM:
6810 return getConstantFP(maximum(C1, C2), DL, VT);
6811 default: break;
6812 }
6813 }
6814 if (N1CFP && Opcode == ISD::FP_ROUND) {
6815 APFloat C1 = N1CFP->getValueAPF(); // make copy
6816 bool Unused;
6817 // This can return overflow, underflow, or inexact; we don't care.
6818 // FIXME need to be more flexible about rounding mode.
6820 &Unused);
6821 return getConstantFP(C1, DL, VT);
6822 }
6823
6824 switch (Opcode) {
6825 case ISD::FSUB:
6826 // -0.0 - undef --> undef (consistent with "fneg undef")
6827 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
6828 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
6829 return getUNDEF(VT);
6830 [[fallthrough]];
6831
6832 case ISD::FADD:
6833 case ISD::FMUL:
6834 case ISD::FDIV:
6835 case ISD::FREM:
6836 // If both operands are undef, the result is undef. If 1 operand is undef,
6837 // the result is NaN. This should match the behavior of the IR optimizer.
6838 if (N1.isUndef() && N2.isUndef())
6839 return getUNDEF(VT);
6840 if (N1.isUndef() || N2.isUndef())
6842 }
6843 return SDValue();
6844}
6845
6847 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
6848
6849 // There's no need to assert on a byte-aligned pointer. All pointers are at
6850 // least byte aligned.
6851 if (A == Align(1))
6852 return Val;
6853
6854 SDVTList VTs = getVTList(Val.getValueType());
6856 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
6857 ID.AddInteger(A.value());
6858
6859 void *IP = nullptr;
6860 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6861 return SDValue(E, 0);
6862
6863 auto *N =
6864 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
6865 createOperands(N, {Val});
6866
6867 CSEMap.InsertNode(N, IP);
6868 InsertNode(N);
6869
6870 SDValue V(N, 0);
6871 NewSDValueDbgMsg(V, "Creating new node: ", this);
6872 return V;
6873}
6874
6875SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6876 SDValue N1, SDValue N2) {
6877 SDNodeFlags Flags;
6878 if (Inserter)
6879 Flags = Inserter->getFlags();
6880 return getNode(Opcode, DL, VT, N1, N2, Flags);
6881}
6882
6884 SDValue &N2) const {
6885 if (!TLI->isCommutativeBinOp(Opcode))
6886 return;
6887
6888 // Canonicalize:
6889 // binop(const, nonconst) -> binop(nonconst, const)
6894 if ((N1C && !N2C) || (N1CFP && !N2CFP))
6895 std::swap(N1, N2);
6896
6897 // Canonicalize:
6898 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
6899 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
6901 std::swap(N1, N2);
6902}
6903
6904SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6905 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
6907 N2.getOpcode() != ISD::DELETED_NODE &&
6908 "Operand is DELETED_NODE!");
6909
6910 canonicalizeCommutativeBinop(Opcode, N1, N2);
6911
6912 auto *N1C = dyn_cast<ConstantSDNode>(N1);
6913 auto *N2C = dyn_cast<ConstantSDNode>(N2);
6914
6915 // Don't allow undefs in vector splats - we might be returning N2 when folding
6916 // to zero etc.
6917 ConstantSDNode *N2CV =
6918 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
6919
6920 switch (Opcode) {
6921 default: break;
6922 case ISD::TokenFactor:
6923 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
6924 N2.getValueType() == MVT::Other && "Invalid token factor!");
6925 // Fold trivial token factors.
6926 if (N1.getOpcode() == ISD::EntryToken) return N2;
6927 if (N2.getOpcode() == ISD::EntryToken) return N1;
6928 if (N1 == N2) return N1;
6929 break;
6930 case ISD::BUILD_VECTOR: {
6931 // Attempt to simplify BUILD_VECTOR.
6932 SDValue Ops[] = {N1, N2};
6933 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6934 return V;
6935 break;
6936 }
6937 case ISD::CONCAT_VECTORS: {
6938 SDValue Ops[] = {N1, N2};
6939 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
6940 return V;
6941 break;
6942 }
6943 case ISD::AND:
6944 assert(VT.isInteger() && "This operator does not apply to FP types!");
6945 assert(N1.getValueType() == N2.getValueType() &&
6946 N1.getValueType() == VT && "Binary operator types must match!");
6947 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
6948 // worth handling here.
6949 if (N2CV && N2CV->isZero())
6950 return N2;
6951 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
6952 return N1;
6953 break;
6954 case ISD::OR:
6955 case ISD::XOR:
6956 case ISD::ADD:
6957 case ISD::SUB:
6958 assert(VT.isInteger() && "This operator does not apply to FP types!");
6959 assert(N1.getValueType() == N2.getValueType() &&
6960 N1.getValueType() == VT && "Binary operator types must match!");
6961 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
6962 // it's worth handling here.
6963 if (N2CV && N2CV->isZero())
6964 return N1;
6965 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
6966 VT.getVectorElementType() == MVT::i1)
6967 return getNode(ISD::XOR, DL, VT, N1, N2);
6968 break;
6969 case ISD::MUL:
6970 assert(VT.isInteger() && "This operator does not apply to FP types!");
6971 assert(N1.getValueType() == N2.getValueType() &&
6972 N1.getValueType() == VT && "Binary operator types must match!");
6973 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6974 return getNode(ISD::AND, DL, VT, N1, N2);
6975 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6976 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6977 const APInt &N2CImm = N2C->getAPIntValue();
6978 return getVScale(DL, VT, MulImm * N2CImm);
6979 }
6980 break;
6981 case ISD::UDIV:
6982 case ISD::UREM:
6983 case ISD::MULHU:
6984 case ISD::MULHS:
6985 case ISD::SDIV:
6986 case ISD::SREM:
6987 case ISD::SADDSAT:
6988 case ISD::SSUBSAT:
6989 case ISD::UADDSAT:
6990 case ISD::USUBSAT:
6991 assert(VT.isInteger() && "This operator does not apply to FP types!");
6992 assert(N1.getValueType() == N2.getValueType() &&
6993 N1.getValueType() == VT && "Binary operator types must match!");
6994 if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
6995 // fold (add_sat x, y) -> (or x, y) for bool types.
6996 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
6997 return getNode(ISD::OR, DL, VT, N1, N2);
6998 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
6999 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7000 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7001 }
7002 break;
7003 case ISD::SCMP:
7004 case ISD::UCMP:
7005 assert(N1.getValueType() == N2.getValueType() &&
7006 "Types of operands of UCMP/SCMP must match");
7007 assert(N1.getValueType().isVector() == VT.isVector() &&
7008 "Operands and return type of must both be scalars or vectors");
7009 if (VT.isVector())
7012 "Result and operands must have the same number of elements");
7013 break;
7014 case ISD::AVGFLOORS:
7015 case ISD::AVGFLOORU:
7016 case ISD::AVGCEILS:
7017 case ISD::AVGCEILU:
7018 assert(VT.isInteger() && "This operator does not apply to FP types!");
7019 assert(N1.getValueType() == N2.getValueType() &&
7020 N1.getValueType() == VT && "Binary operator types must match!");
7021 break;
7022 case ISD::ABDS:
7023 case ISD::ABDU:
7024 assert(VT.isInteger() && "This operator does not apply to FP types!");
7025 assert(N1.getValueType() == N2.getValueType() &&
7026 N1.getValueType() == VT && "Binary operator types must match!");
7027 break;
7028 case ISD::SMIN:
7029 case ISD::UMAX:
7030 assert(VT.isInteger() && "This operator does not apply to FP types!");
7031 assert(N1.getValueType() == N2.getValueType() &&
7032 N1.getValueType() == VT && "Binary operator types must match!");
7033 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7034 return getNode(ISD::OR, DL, VT, N1, N2);
7035 break;
7036 case ISD::SMAX:
7037 case ISD::UMIN:
7038 assert(VT.isInteger() && "This operator does not apply to FP types!");
7039 assert(N1.getValueType() == N2.getValueType() &&
7040 N1.getValueType() == VT && "Binary operator types must match!");
7041 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7042 return getNode(ISD::AND, DL, VT, N1, N2);
7043 break;
7044 case ISD::FADD:
7045 case ISD::FSUB:
7046 case ISD::FMUL:
7047 case ISD::FDIV:
7048 case ISD::FREM:
7049 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7050 assert(N1.getValueType() == N2.getValueType() &&
7051 N1.getValueType() == VT && "Binary operator types must match!");
7052 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7053 return V;
7054 break;
7055 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7056 assert(N1.getValueType() == VT &&
7059 "Invalid FCOPYSIGN!");
7060 break;
7061 case ISD::SHL:
7062 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7063 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7064 const APInt &ShiftImm = N2C->getAPIntValue();
7065 return getVScale(DL, VT, MulImm << ShiftImm);
7066 }
7067 [[fallthrough]];
7068 case ISD::SRA:
7069 case ISD::SRL:
7070 if (SDValue V = simplifyShift(N1, N2))
7071 return V;
7072 [[fallthrough]];
7073 case ISD::ROTL:
7074 case ISD::ROTR:
7075 assert(VT == N1.getValueType() &&
7076 "Shift operators return type must be the same as their first arg");
7077 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7078 "Shifts only work on integers");
7079 assert((!VT.isVector() || VT == N2.getValueType()) &&
7080 "Vector shift amounts must be in the same as their first arg");
7081 // Verify that the shift amount VT is big enough to hold valid shift
7082 // amounts. This catches things like trying to shift an i1024 value by an
7083 // i8, which is easy to fall into in generic code that uses
7084 // TLI.getShiftAmount().
7087 "Invalid use of small shift amount with oversized value!");
7088
7089 // Always fold shifts of i1 values so the code generator doesn't need to
7090 // handle them. Since we know the size of the shift has to be less than the
7091 // size of the value, the shift/rotate count is guaranteed to be zero.
7092 if (VT == MVT::i1)
7093 return N1;
7094 if (N2CV && N2CV->isZero())
7095 return N1;
7096 break;
7097 case ISD::FP_ROUND:
7098 assert(VT.isFloatingPoint() &&
7100 VT.bitsLE(N1.getValueType()) &&
7101 N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7102 "Invalid FP_ROUND!");
7103 if (N1.getValueType() == VT) return N1; // noop conversion.
7104 break;
7105 case ISD::AssertSext:
7106 case ISD::AssertZext: {
7107 EVT EVT = cast<VTSDNode>(N2)->getVT();
7108 assert(VT == N1.getValueType() && "Not an inreg extend!");
7109 assert(VT.isInteger() && EVT.isInteger() &&
7110 "Cannot *_EXTEND_INREG FP types");
7111 assert(!EVT.isVector() &&
7112 "AssertSExt/AssertZExt type should be the vector element type "
7113 "rather than the vector type!");
7114 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7115 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7116 break;
7117 }
7119 EVT EVT = cast<VTSDNode>(N2)->getVT();
7120 assert(VT == N1.getValueType() && "Not an inreg extend!");
7121 assert(VT.isInteger() && EVT.isInteger() &&
7122 "Cannot *_EXTEND_INREG FP types");
7123 assert(EVT.isVector() == VT.isVector() &&
7124 "SIGN_EXTEND_INREG type should be vector iff the operand "
7125 "type is vector!");
7126 assert((!EVT.isVector() ||
7128 "Vector element counts must match in SIGN_EXTEND_INREG");
7129 assert(EVT.bitsLE(VT) && "Not extending!");
7130 if (EVT == VT) return N1; // Not actually extending
7131
7132 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7133 unsigned FromBits = EVT.getScalarSizeInBits();
7134 Val <<= Val.getBitWidth() - FromBits;
7135 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7136 return getConstant(Val, DL, ConstantVT);
7137 };
7138
7139 if (N1C) {
7140 const APInt &Val = N1C->getAPIntValue();
7141 return SignExtendInReg(Val, VT);
7142 }
7143
7146 llvm::EVT OpVT = N1.getOperand(0).getValueType();
7147 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7148 SDValue Op = N1.getOperand(i);
7149 if (Op.isUndef()) {
7150 Ops.push_back(getUNDEF(OpVT));
7151 continue;
7152 }
7153 ConstantSDNode *C = cast<ConstantSDNode>(Op);
7154 APInt Val = C->getAPIntValue();
7155 Ops.push_back(SignExtendInReg(Val, OpVT));
7156 }
7157 return getBuildVector(VT, DL, Ops);
7158 }
7159
7160 if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7161 isa<ConstantSDNode>(N1.getOperand(0)))
7162 return getNode(
7163 ISD::SPLAT_VECTOR, DL, VT,
7164 SignExtendInReg(N1.getConstantOperandAPInt(0),
7165 N1.getOperand(0).getValueType()));
7166 break;
7167 }
7169 case ISD::FP_TO_UINT_SAT: {
7170 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7171 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7172 assert(N1.getValueType().isVector() == VT.isVector() &&
7173 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7174 "vector!");
7175 assert((!VT.isVector() || VT.getVectorElementCount() ==
7177 "Vector element counts must match in FP_TO_*INT_SAT");
7178 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7179 "Type to saturate to must be a scalar.");
7180 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7181 "Not extending!");
7182 break;
7183 }
7186 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7187 element type of the vector.");
7188
7189 // Extract from an undefined value or using an undefined index is undefined.
7190 if (N1.isUndef() || N2.isUndef())
7191 return getUNDEF(VT);
7192
7193 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7194 // vectors. For scalable vectors we will provide appropriate support for
7195 // dealing with arbitrary indices.
7196 if (N2C && N1.getValueType().isFixedLengthVector() &&
7197 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7198 return getUNDEF(VT);
7199
7200 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7201 // expanding copies of large vectors from registers. This only works for
7202 // fixed length vectors, since we need to know the exact number of
7203 // elements.
7204 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7206 unsigned Factor =
7209 N1.getOperand(N2C->getZExtValue() / Factor),
7210 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
7211 }
7212
7213 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7214 // lowering is expanding large vector constants.
7215 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7216 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7219 "BUILD_VECTOR used for scalable vectors");
7220 unsigned Index =
7221 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7222 SDValue Elt = N1.getOperand(Index);
7223
7224 if (VT != Elt.getValueType())
7225 // If the vector element type is not legal, the BUILD_VECTOR operands
7226 // are promoted and implicitly truncated, and the result implicitly
7227 // extended. Make that explicit here.
7228 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7229
7230 return Elt;
7231 }
7232
7233 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7234 // operations are lowered to scalars.
7235 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7236 // If the indices are the same, return the inserted element else
7237 // if the indices are known different, extract the element from
7238 // the original vector.
7239 SDValue N1Op2 = N1.getOperand(2);
7240 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7241
7242 if (N1Op2C && N2C) {
7243 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7244 if (VT == N1.getOperand(1).getValueType())
7245 return N1.getOperand(1);
7246 if (VT.isFloatingPoint()) {
7248 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7249 }
7250 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7251 }
7252 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7253 }
7254 }
7255
7256 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7257 // when vector types are scalarized and v1iX is legal.
7258 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7259 // Here we are completely ignoring the extract element index (N2),
7260 // which is fine for fixed width vectors, since any index other than 0
7261 // is undefined anyway. However, this cannot be ignored for scalable
7262 // vectors - in theory we could support this, but we don't want to do this
7263 // without a profitability check.
7264 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7266 N1.getValueType().getVectorNumElements() == 1) {
7267 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7268 N1.getOperand(1));
7269 }
7270 break;
7272 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7273 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7274 (N1.getValueType().isInteger() == VT.isInteger()) &&
7275 N1.getValueType() != VT &&
7276 "Wrong types for EXTRACT_ELEMENT!");
7277
7278 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7279 // 64-bit integers into 32-bit parts. Instead of building the extract of
7280 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7281 if (N1.getOpcode() == ISD::BUILD_PAIR)
7282 return N1.getOperand(N2C->getZExtValue());
7283
7284 // EXTRACT_ELEMENT of a constant int is also very common.
7285 if (N1C) {
7286 unsigned ElementSize = VT.getSizeInBits();
7287 unsigned Shift = ElementSize * N2C->getZExtValue();
7288 const APInt &Val = N1C->getAPIntValue();
7289 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7290 }
7291 break;
7293 EVT N1VT = N1.getValueType();
7294 assert(VT.isVector() && N1VT.isVector() &&
7295 "Extract subvector VTs must be vectors!");
7297 "Extract subvector VTs must have the same element type!");
7298 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7299 "Cannot extract a scalable vector from a fixed length vector!");
7300 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7302 "Extract subvector must be from larger vector to smaller vector!");
7303 assert(N2C && "Extract subvector index must be a constant");
7304 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7305 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7306 N1VT.getVectorMinNumElements()) &&
7307 "Extract subvector overflow!");
7308 assert(N2C->getAPIntValue().getBitWidth() ==
7309 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7310 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7311
7312 // Trivial extraction.
7313 if (VT == N1VT)
7314 return N1;
7315
7316 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7317 if (N1.isUndef())
7318 return getUNDEF(VT);
7319
7320 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7321 // the concat have the same type as the extract.
7322 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7323 VT == N1.getOperand(0).getValueType()) {
7324 unsigned Factor = VT.getVectorMinNumElements();
7325 return N1.getOperand(N2C->getZExtValue() / Factor);
7326 }
7327
7328 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7329 // during shuffle legalization.
7330 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7331 VT == N1.getOperand(1).getValueType())
7332 return N1.getOperand(1);
7333 break;
7334 }
7335 }
7336
7337 // Perform trivial constant folding.
7338 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
7339 return SV;
7340
7341 // Canonicalize an UNDEF to the RHS, even over a constant.
7342 if (N1.isUndef()) {
7343 if (TLI->isCommutativeBinOp(Opcode)) {
7344 std::swap(N1, N2);
7345 } else {
7346 switch (Opcode) {
7347 case ISD::SUB:
7348 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7350 case ISD::UDIV:
7351 case ISD::SDIV:
7352 case ISD::UREM:
7353 case ISD::SREM:
7354 case ISD::SSUBSAT:
7355 case ISD::USUBSAT:
7356 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7357 }
7358 }
7359 }
7360
7361 // Fold a bunch of operators when the RHS is undef.
7362 if (N2.isUndef()) {
7363 switch (Opcode) {
7364 case ISD::XOR:
7365 if (N1.isUndef())
7366 // Handle undef ^ undef -> 0 special case. This is a common
7367 // idiom (misuse).
7368 return getConstant(0, DL, VT);
7369 [[fallthrough]];
7370 case ISD::ADD:
7371 case ISD::SUB:
7372 case ISD::UDIV:
7373 case ISD::SDIV:
7374 case ISD::UREM:
7375 case ISD::SREM:
7376 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7377 case ISD::MUL:
7378 case ISD::AND:
7379 case ISD::SSUBSAT:
7380 case ISD::USUBSAT:
7381 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7382 case ISD::OR:
7383 case ISD::SADDSAT:
7384 case ISD::UADDSAT:
7385 return getAllOnesConstant(DL, VT);
7386 }
7387 }
7388
7389 // Memoize this node if possible.
7390 SDNode *N;
7391 SDVTList VTs = getVTList(VT);
7392 SDValue Ops[] = {N1, N2};
7393 if (VT != MVT::Glue) {
7395 AddNodeIDNode(ID, Opcode, VTs, Ops);
7396 void *IP = nullptr;
7397 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7398 E->intersectFlagsWith(Flags);
7399 return SDValue(E, 0);
7400 }
7401
7402 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7403 N->setFlags(Flags);
7404 createOperands(N, Ops);
7405 CSEMap.InsertNode(N, IP);
7406 } else {
7407 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7408 createOperands(N, Ops);
7409 }
7410
7411 InsertNode(N);
7412 SDValue V = SDValue(N, 0);
7413 NewSDValueDbgMsg(V, "Creating new node: ", this);
7414 return V;
7415}
7416
7417SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7418 SDValue N1, SDValue N2, SDValue N3) {
7419 SDNodeFlags Flags;
7420 if (Inserter)
7421 Flags = Inserter->getFlags();
7422 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7423}
7424
7425SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7426 SDValue N1, SDValue N2, SDValue N3,
7427 const SDNodeFlags Flags) {
7429 N2.getOpcode() != ISD::DELETED_NODE &&
7430 N3.getOpcode() != ISD::DELETED_NODE &&
7431 "Operand is DELETED_NODE!");
7432 // Perform various simplifications.
7433 switch (Opcode) {
7434 case ISD::FMA:
7435 case ISD::FMAD: {
7436 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7437 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7438 N3.getValueType() == VT && "FMA types must match!");
7439 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7440 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7441 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7442 if (N1CFP && N2CFP && N3CFP) {
7443 APFloat V1 = N1CFP->getValueAPF();
7444 const APFloat &V2 = N2CFP->getValueAPF();
7445 const APFloat &V3 = N3CFP->getValueAPF();
7446 if (Opcode == ISD::FMAD) {
7449 } else
7451 return getConstantFP(V1, DL, VT);
7452 }
7453 break;
7454 }
7455 case ISD::BUILD_VECTOR: {
7456 // Attempt to simplify BUILD_VECTOR.
7457 SDValue Ops[] = {N1, N2, N3};
7458 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7459 return V;
7460 break;
7461 }
7462 case ISD::CONCAT_VECTORS: {
7463 SDValue Ops[] = {N1, N2, N3};
7464 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7465 return V;
7466 break;
7467 }
7468 case ISD::SETCC: {
7469 assert(VT.isInteger() && "SETCC result type must be an integer!");
7470 assert(N1.getValueType() == N2.getValueType() &&
7471 "SETCC operands must have the same type!");
7472 assert(VT.isVector() == N1.getValueType().isVector() &&
7473 "SETCC type should be vector iff the operand type is vector!");
7474 assert((!VT.isVector() || VT.getVectorElementCount() ==
7476 "SETCC vector element counts must match!");
7477 // Use FoldSetCC to simplify SETCC's.
7478 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7479 return V;
7480 // Vector constant folding.
7481 SDValue Ops[] = {N1, N2, N3};
7482 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7483 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7484 return V;
7485 }
7486 break;
7487 }
7488 case ISD::SELECT:
7489 case ISD::VSELECT:
7490 if (SDValue V = simplifySelect(N1, N2, N3))
7491 return V;
7492 break;
7494 llvm_unreachable("should use getVectorShuffle constructor!");
7495 case ISD::VECTOR_SPLICE: {
7496 if (cast<ConstantSDNode>(N3)->isZero())
7497 return N1;
7498 break;
7499 }
7501 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7502 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7503 // for scalable vectors where we will generate appropriate code to
7504 // deal with out-of-bounds cases correctly.
7505 if (N3C && N1.getValueType().isFixedLengthVector() &&
7507 return getUNDEF(VT);
7508
7509 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7510 if (N3.isUndef())
7511 return getUNDEF(VT);
7512
7513 // If the inserted element is an UNDEF, just use the input vector.
7514 if (N2.isUndef())
7515 return N1;
7516
7517 break;
7518 }
7519 case ISD::INSERT_SUBVECTOR: {
7520 // Inserting undef into undef is still undef.
7521 if (N1.isUndef() && N2.isUndef())
7522 return getUNDEF(VT);
7523
7524 EVT N2VT = N2.getValueType();
7525 assert(VT == N1.getValueType() &&
7526 "Dest and insert subvector source types must match!");
7527 assert(VT.isVector() && N2VT.isVector() &&
7528 "Insert subvector VTs must be vectors!");
7530 "Insert subvector VTs must have the same element type!");
7531 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7532 "Cannot insert a scalable vector into a fixed length vector!");
7533 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7535 "Insert subvector must be from smaller vector to larger vector!");
7536 assert(isa<ConstantSDNode>(N3) &&
7537 "Insert subvector index must be constant");
7538 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7539 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7541 "Insert subvector overflow!");
7543 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7544 "Constant index for INSERT_SUBVECTOR has an invalid size");
7545
7546 // Trivial insertion.
7547 if (VT == N2VT)
7548 return N2;
7549
7550 // If this is an insert of an extracted vector into an undef vector, we
7551 // can just use the input to the extract.
7552 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7553 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7554 return N2.getOperand(0);
7555 break;
7556 }
7557 case ISD::BITCAST:
7558 // Fold bit_convert nodes from a type to themselves.
7559 if (N1.getValueType() == VT)
7560 return N1;
7561 break;
7562 case ISD::VP_TRUNCATE:
7563 case ISD::VP_SIGN_EXTEND:
7564 case ISD::VP_ZERO_EXTEND:
7565 // Don't create noop casts.
7566 if (N1.getValueType() == VT)
7567 return N1;
7568 break;
7569 case ISD::VECTOR_COMPRESS: {
7570 [[maybe_unused]] EVT VecVT = N1.getValueType();
7571 [[maybe_unused]] EVT MaskVT = N2.getValueType();
7572 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
7573 assert(VT == VecVT && "Vector and result type don't match.");
7574 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
7575 "All inputs must be vectors.");
7576 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
7578 "Vector and mask must have same number of elements.");
7579
7580 if (N1.isUndef() || N2.isUndef())
7581 return N3;
7582
7583 break;
7584 }
7585 }
7586
7587 // Memoize node if it doesn't produce a glue result.
7588 SDNode *N;
7589 SDVTList VTs = getVTList(VT);
7590 SDValue Ops[] = {N1, N2, N3};
7591 if (VT != MVT::Glue) {
7593 AddNodeIDNode(ID, Opcode, VTs, Ops);
7594 void *IP = nullptr;
7595 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7596 E->intersectFlagsWith(Flags);
7597 return SDValue(E, 0);
7598 }
7599
7600 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7601 N->setFlags(Flags);
7602 createOperands(N, Ops);
7603 CSEMap.InsertNode(N, IP);
7604 } else {
7605 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7606 createOperands(N, Ops);
7607 }
7608
7609 InsertNode(N);
7610 SDValue V = SDValue(N, 0);
7611 NewSDValueDbgMsg(V, "Creating new node: ", this);
7612 return V;
7613}
7614
7615SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7616 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7617 SDValue Ops[] = { N1, N2, N3, N4 };
7618 return getNode(Opcode, DL, VT, Ops);
7619}
7620
7621SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7622 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7623 SDValue N5) {
7624 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7625 return getNode(Opcode, DL, VT, Ops);
7626}
7627
7628/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7629/// the incoming stack arguments to be loaded from the stack.
7631 SmallVector<SDValue, 8> ArgChains;
7632
7633 // Include the original chain at the beginning of the list. When this is
7634 // used by target LowerCall hooks, this helps legalize find the
7635 // CALLSEQ_BEGIN node.
7636 ArgChains.push_back(Chain);
7637
7638 // Add a chain value for each stack argument.
7639 for (SDNode *U : getEntryNode().getNode()->uses())
7640 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7641 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7642 if (FI->getIndex() < 0)
7643 ArgChains.push_back(SDValue(L, 1));
7644
7645 // Build a tokenfactor for all the chains.
7646 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7647}
7648
7649/// getMemsetValue - Vectorized representation of the memset value
7650/// operand.
7652 const SDLoc &dl) {
7653 assert(!Value.isUndef());
7654
7655 unsigned NumBits = VT.getScalarSizeInBits();
7656 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7657 assert(C->getAPIntValue().getBitWidth() == 8);
7658 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7659 if (VT.isInteger()) {
7660 bool IsOpaque = VT.getSizeInBits() > 64 ||
7661 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7662 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7663 }
7664 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
7665 VT);
7666 }
7667
7668 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7669 EVT IntVT = VT.getScalarType();
7670 if (!IntVT.isInteger())
7671 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7672
7673 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7674 if (NumBits > 8) {
7675 // Use a multiplication with 0x010101... to extend the input to the
7676 // required length.
7677 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7678 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7679 DAG.getConstant(Magic, dl, IntVT));
7680 }
7681
7682 if (VT != Value.getValueType() && !VT.isInteger())
7683 Value = DAG.getBitcast(VT.getScalarType(), Value);
7684 if (VT != Value.getValueType())
7685 Value = DAG.getSplatBuildVector(VT, dl, Value);
7686
7687 return Value;
7688}
7689
7690/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
7691/// used when a memcpy is turned into a memset when the source is a constant
7692/// string ptr.
7694 const TargetLowering &TLI,
7695 const ConstantDataArraySlice &Slice) {
7696 // Handle vector with all elements zero.
7697 if (Slice.Array == nullptr) {
7698 if (VT.isInteger())
7699 return DAG.getConstant(0, dl, VT);
7700 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
7701 return DAG.getConstantFP(0.0, dl, VT);
7702 if (VT.isVector()) {
7703 unsigned NumElts = VT.getVectorNumElements();
7704 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
7705 return DAG.getNode(ISD::BITCAST, dl, VT,
7706 DAG.getConstant(0, dl,
7708 EltVT, NumElts)));
7709 }
7710 llvm_unreachable("Expected type!");
7711 }
7712
7713 assert(!VT.isVector() && "Can't handle vector type here!");
7714 unsigned NumVTBits = VT.getSizeInBits();
7715 unsigned NumVTBytes = NumVTBits / 8;
7716 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
7717
7718 APInt Val(NumVTBits, 0);
7719 if (DAG.getDataLayout().isLittleEndian()) {
7720 for (unsigned i = 0; i != NumBytes; ++i)
7721 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
7722 } else {
7723 for (unsigned i = 0; i != NumBytes; ++i)
7724 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
7725 }
7726
7727 // If the "cost" of materializing the integer immediate is less than the cost
7728 // of a load, then it is cost effective to turn the load into the immediate.
7729 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
7730 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
7731 return DAG.getConstant(Val, dl, VT);
7732 return SDValue();
7733}
7734
7736 const SDLoc &DL,
7737 const SDNodeFlags Flags) {
7738 EVT VT = Base.getValueType();
7739 SDValue Index;
7740
7741 if (Offset.isScalable())
7742 Index = getVScale(DL, Base.getValueType(),
7743 APInt(Base.getValueSizeInBits().getFixedValue(),
7744 Offset.getKnownMinValue()));
7745 else
7746 Index = getConstant(Offset.getFixedValue(), DL, VT);
7747
7748 return getMemBasePlusOffset(Base, Index, DL, Flags);
7749}
7750
7752 const SDLoc &DL,
7753 const SDNodeFlags Flags) {
7754 assert(Offset.getValueType().isInteger());
7755 EVT BasePtrVT = Ptr.getValueType();
7756 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
7757}
7758
7759/// Returns true if memcpy source is constant data.
7761 uint64_t SrcDelta = 0;
7762 GlobalAddressSDNode *G = nullptr;
7763 if (Src.getOpcode() == ISD::GlobalAddress)
7764 G = cast<GlobalAddressSDNode>(Src);
7765 else if (Src.getOpcode() == ISD::ADD &&
7766 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
7767 Src.getOperand(1).getOpcode() == ISD::Constant) {
7768 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
7769 SrcDelta = Src.getConstantOperandVal(1);
7770 }
7771 if (!G)
7772 return false;
7773
7774 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
7775 SrcDelta + G->getOffset());
7776}
7777
7779 SelectionDAG &DAG) {
7780 // On Darwin, -Os means optimize for size without hurting performance, so
7781 // only really optimize for size when -Oz (MinSize) is used.
7783 return MF.getFunction().hasMinSize();
7784 return DAG.shouldOptForSize();
7785}
7786
7788 SmallVector<SDValue, 32> &OutChains, unsigned From,
7789 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
7790 SmallVector<SDValue, 16> &OutStoreChains) {
7791 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
7792 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
7793 SmallVector<SDValue, 16> GluedLoadChains;
7794 for (unsigned i = From; i < To; ++i) {
7795 OutChains.push_back(OutLoadChains[i]);
7796 GluedLoadChains.push_back(OutLoadChains[i]);
7797 }
7798
7799 // Chain for all loads.
7800 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7801 GluedLoadChains);
7802
7803 for (unsigned i = From; i < To; ++i) {
7804 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
7805 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
7806 ST->getBasePtr(), ST->getMemoryVT(),
7807 ST->getMemOperand());
7808 OutChains.push_back(NewStore);
7809 }
7810}
7811
7813 SDValue Chain, SDValue Dst, SDValue Src,
7814 uint64_t Size, Align Alignment,
7815 bool isVol, bool AlwaysInline,
7816 MachinePointerInfo DstPtrInfo,
7817 MachinePointerInfo SrcPtrInfo,
7818 const AAMDNodes &AAInfo, AAResults *AA) {
7819 // Turn a memcpy of undef to nop.
7820 // FIXME: We need to honor volatile even is Src is undef.
7821 if (Src.isUndef())
7822 return Chain;
7823
7824 // Expand memcpy to a series of load and store ops if the size operand falls
7825 // below a certain threshold.
7826 // TODO: In the AlwaysInline case, if the size is big then generate a loop
7827 // rather than maybe a humongous number of loads and stores.
7828 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7829 const DataLayout &DL = DAG.getDataLayout();
7830 LLVMContext &C = *DAG.getContext();
7831 std::vector<EVT> MemOps;
7832 bool DstAlignCanChange = false;
7834 MachineFrameInfo &MFI = MF.getFrameInfo();
7835 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7836 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7837 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7838 DstAlignCanChange = true;
7839 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7840 if (!SrcAlign || Alignment > *SrcAlign)
7841 SrcAlign = Alignment;
7842 assert(SrcAlign && "SrcAlign must be set");
7844 // If marked as volatile, perform a copy even when marked as constant.
7845 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
7846 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
7847 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
7848 const MemOp Op = isZeroConstant
7849 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
7850 /*IsZeroMemset*/ true, isVol)
7851 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
7852 *SrcAlign, isVol, CopyFromConstant);
7853 if (!TLI.findOptimalMemOpLowering(
7854 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
7855 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
7856 return SDValue();
7857
7858 if (DstAlignCanChange) {
7859 Type *Ty = MemOps[0].getTypeForEVT(C);
7860 Align NewAlign = DL.getABITypeAlign(Ty);
7861
7862 // Don't promote to an alignment that would require dynamic stack
7863 // realignment which may conflict with optimizations such as tail call
7864 // optimization.
7866 if (!TRI->hasStackRealignment(MF))
7867 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7868 NewAlign = NewAlign.previous();
7869
7870 if (NewAlign > Alignment) {
7871 // Give the stack frame object a larger alignment if needed.
7872 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7873 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7874 Alignment = NewAlign;
7875 }
7876 }
7877
7878 // Prepare AAInfo for loads/stores after lowering this memcpy.
7879 AAMDNodes NewAAInfo = AAInfo;
7880 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7881
7882 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
7883 bool isConstant =
7884 AA && SrcVal &&
7885 AA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
7886
7887 MachineMemOperand::Flags MMOFlags =
7889 SmallVector<SDValue, 16> OutLoadChains;
7890 SmallVector<SDValue, 16> OutStoreChains;
7891 SmallVector<SDValue, 32> OutChains;
7892 unsigned NumMemOps = MemOps.size();
7893 uint64_t SrcOff = 0, DstOff = 0;
7894 for (unsigned i = 0; i != NumMemOps; ++i) {
7895 EVT VT = MemOps[i];
7896 unsigned VTSize = VT.getSizeInBits() / 8;
7897 SDValue Value, Store;
7898
7899 if (VTSize > Size) {
7900 // Issuing an unaligned load / store pair that overlaps with the previous
7901 // pair. Adjust the offset accordingly.
7902 assert(i == NumMemOps-1 && i != 0);
7903 SrcOff -= VTSize - Size;
7904 DstOff -= VTSize - Size;
7905 }
7906
7907 if (CopyFromConstant &&
7908 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
7909 // It's unlikely a store of a vector immediate can be done in a single
7910 // instruction. It would require a load from a constantpool first.
7911 // We only handle zero vectors here.
7912 // FIXME: Handle other cases where store of vector immediate is done in
7913 // a single instruction.
7914 ConstantDataArraySlice SubSlice;
7915 if (SrcOff < Slice.Length) {
7916 SubSlice = Slice;
7917 SubSlice.move(SrcOff);
7918 } else {
7919 // This is an out-of-bounds access and hence UB. Pretend we read zero.
7920 SubSlice.Array = nullptr;
7921 SubSlice.Offset = 0;
7922 SubSlice.Length = VTSize;
7923 }
7924 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
7925 if (Value.getNode()) {
7926 Store = DAG.getStore(
7927 Chain, dl, Value,
7928 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7929 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7930 OutChains.push_back(Store);
7931 }
7932 }
7933
7934 if (!Store.getNode()) {
7935 // The type might not be legal for the target. This should only happen
7936 // if the type is smaller than a legal type, as on PPC, so the right
7937 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
7938 // to Load/Store if NVT==VT.
7939 // FIXME does the case above also need this?
7940 EVT NVT = TLI.getTypeToTransformTo(C, VT);
7941 assert(NVT.bitsGE(VT));
7942
7943 bool isDereferenceable =
7944 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7945 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7946 if (isDereferenceable)
7948 if (isConstant)
7949 SrcMMOFlags |= MachineMemOperand::MOInvariant;
7950
7951 Value = DAG.getExtLoad(
7952 ISD::EXTLOAD, dl, NVT, Chain,
7953 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7954 SrcPtrInfo.getWithOffset(SrcOff), VT,
7955 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
7956 OutLoadChains.push_back(Value.getValue(1));
7957
7958 Store = DAG.getTruncStore(
7959 Chain, dl, Value,
7960 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7961 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
7962 OutStoreChains.push_back(Store);
7963 }
7964 SrcOff += VTSize;
7965 DstOff += VTSize;
7966 Size -= VTSize;
7967 }
7968
7969 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
7971 unsigned NumLdStInMemcpy = OutStoreChains.size();
7972
7973 if (NumLdStInMemcpy) {
7974 // It may be that memcpy might be converted to memset if it's memcpy
7975 // of constants. In such a case, we won't have loads and stores, but
7976 // just stores. In the absence of loads, there is nothing to gang up.
7977 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
7978 // If target does not care, just leave as it.
7979 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
7980 OutChains.push_back(OutLoadChains[i]);
7981 OutChains.push_back(OutStoreChains[i]);
7982 }
7983 } else {
7984 // Ld/St less than/equal limit set by target.
7985 if (NumLdStInMemcpy <= GluedLdStLimit) {
7986 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7987 NumLdStInMemcpy, OutLoadChains,
7988 OutStoreChains);
7989 } else {
7990 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
7991 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
7992 unsigned GlueIter = 0;
7993
7994 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
7995 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
7996 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
7997
7998 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
7999 OutLoadChains, OutStoreChains);
8000 GlueIter += GluedLdStLimit;
8001 }
8002
8003 // Residual ld/st.
8004 if (RemainingLdStInMemcpy) {
8005 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8006 RemainingLdStInMemcpy, OutLoadChains,
8007 OutStoreChains);
8008 }
8009 }
8010 }
8011 }
8012 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8013}
8014
8016 SDValue Chain, SDValue Dst, SDValue Src,
8017 uint64_t Size, Align Alignment,
8018 bool isVol, bool AlwaysInline,
8019 MachinePointerInfo DstPtrInfo,
8020 MachinePointerInfo SrcPtrInfo,
8021 const AAMDNodes &AAInfo) {
8022 // Turn a memmove of undef to nop.
8023 // FIXME: We need to honor volatile even is Src is undef.
8024 if (Src.isUndef())
8025 return Chain;
8026
8027 // Expand memmove to a series of load and store ops if the size operand falls
8028 // below a certain threshold.
8029 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8030 const DataLayout &DL = DAG.getDataLayout();
8031 LLVMContext &C = *DAG.getContext();
8032 std::vector<EVT> MemOps;
8033 bool DstAlignCanChange = false;
8035 MachineFrameInfo &MFI = MF.getFrameInfo();
8036 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8037 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8038 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8039 DstAlignCanChange = true;
8040 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8041 if (!SrcAlign || Alignment > *SrcAlign)
8042 SrcAlign = Alignment;
8043 assert(SrcAlign && "SrcAlign must be set");
8044 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8045 if (!TLI.findOptimalMemOpLowering(
8046 MemOps, Limit,
8047 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8048 /*IsVolatile*/ true),
8049 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8050 MF.getFunction().getAttributes()))
8051 return SDValue();
8052
8053 if (DstAlignCanChange) {
8054 Type *Ty = MemOps[0].getTypeForEVT(C);
8055 Align NewAlign = DL.getABITypeAlign(Ty);
8056
8057 // Don't promote to an alignment that would require dynamic stack
8058 // realignment which may conflict with optimizations such as tail call
8059 // optimization.
8061 if (!TRI->hasStackRealignment(MF))
8062 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8063 NewAlign = NewAlign.previous();
8064
8065 if (NewAlign > Alignment) {
8066 // Give the stack frame object a larger alignment if needed.
8067 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8068 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8069 Alignment = NewAlign;
8070 }
8071 }
8072
8073 // Prepare AAInfo for loads/stores after lowering this memmove.
8074 AAMDNodes NewAAInfo = AAInfo;
8075 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8076
8077 MachineMemOperand::Flags MMOFlags =
8079 uint64_t SrcOff = 0, DstOff = 0;
8080 SmallVector<SDValue, 8> LoadValues;
8081 SmallVector<SDValue, 8> LoadChains;
8082 SmallVector<SDValue, 8> OutChains;
8083 unsigned NumMemOps = MemOps.size();
8084 for (unsigned i = 0; i < NumMemOps; i++) {
8085 EVT VT = MemOps[i];
8086 unsigned VTSize = VT.getSizeInBits() / 8;
8087 SDValue Value;
8088
8089 bool isDereferenceable =
8090 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8091 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8092 if (isDereferenceable)
8094
8095 Value = DAG.getLoad(
8096 VT, dl, Chain,
8097 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8098 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8099 LoadValues.push_back(Value);
8100 LoadChains.push_back(Value.getValue(1));
8101 SrcOff += VTSize;
8102 }
8103 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8104 OutChains.clear();
8105 for (unsigned i = 0; i < NumMemOps; i++) {
8106 EVT VT = MemOps[i];
8107 unsigned VTSize = VT.getSizeInBits() / 8;
8108 SDValue Store;
8109
8110 Store = DAG.getStore(
8111 Chain, dl, LoadValues[i],
8112 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8113 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8114 OutChains.push_back(Store);
8115 DstOff += VTSize;
8116 }
8117
8118 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8119}
8120
8121/// Lower the call to 'memset' intrinsic function into a series of store
8122/// operations.
8123///
8124/// \param DAG Selection DAG where lowered code is placed.
8125/// \param dl Link to corresponding IR location.
8126/// \param Chain Control flow dependency.
8127/// \param Dst Pointer to destination memory location.
8128/// \param Src Value of byte to write into the memory.
8129/// \param Size Number of bytes to write.
8130/// \param Alignment Alignment of the destination in bytes.
8131/// \param isVol True if destination is volatile.
8132/// \param AlwaysInline Makes sure no function call is generated.
8133/// \param DstPtrInfo IR information on the memory pointer.
8134/// \returns New head in the control flow, if lowering was successful, empty
8135/// SDValue otherwise.
8136///
8137/// The function tries to replace 'llvm.memset' intrinsic with several store
8138/// operations and value calculation code. This is usually profitable for small
8139/// memory size or when the semantic requires inlining.
8141 SDValue Chain, SDValue Dst, SDValue Src,
8142 uint64_t Size, Align Alignment, bool isVol,
8143 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8144 const AAMDNodes &AAInfo) {
8145 // Turn a memset of undef to nop.
8146 // FIXME: We need to honor volatile even is Src is undef.
8147 if (Src.isUndef())
8148 return Chain;
8149
8150 // Expand memset to a series of load/store ops if the size operand
8151 // falls below a certain threshold.
8152 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8153 std::vector<EVT> MemOps;
8154 bool DstAlignCanChange = false;
8156 MachineFrameInfo &MFI = MF.getFrameInfo();
8157 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8158 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8159 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8160 DstAlignCanChange = true;
8161 bool IsZeroVal = isNullConstant(Src);
8162 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8163
8164 if (!TLI.findOptimalMemOpLowering(
8165 MemOps, Limit,
8166 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8167 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8168 return SDValue();
8169
8170 if (DstAlignCanChange) {
8171 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8172 const DataLayout &DL = DAG.getDataLayout();
8173 Align NewAlign = DL.getABITypeAlign(Ty);
8174
8175 // Don't promote to an alignment that would require dynamic stack
8176 // realignment which may conflict with optimizations such as tail call
8177 // optimization.
8179 if (!TRI->hasStackRealignment(MF))
8180 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8181 NewAlign = NewAlign.previous();
8182
8183 if (NewAlign > Alignment) {
8184 // Give the stack frame object a larger alignment if needed.
8185 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8186 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8187 Alignment = NewAlign;
8188 }
8189 }
8190
8191 SmallVector<SDValue, 8> OutChains;
8192 uint64_t DstOff = 0;
8193 unsigned NumMemOps = MemOps.size();
8194
8195 // Find the largest store and generate the bit pattern for it.
8196 EVT LargestVT = MemOps[0];
8197 for (unsigned i = 1; i < NumMemOps; i++)
8198 if (MemOps[i].bitsGT(LargestVT))
8199 LargestVT = MemOps[i];
8200 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8201
8202 // Prepare AAInfo for loads/stores after lowering this memset.
8203 AAMDNodes NewAAInfo = AAInfo;
8204 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8205
8206 for (unsigned i = 0; i < NumMemOps; i++) {
8207 EVT VT = MemOps[i];
8208 unsigned VTSize = VT.getSizeInBits() / 8;
8209 if (VTSize > Size) {
8210 // Issuing an unaligned load / store pair that overlaps with the previous
8211 // pair. Adjust the offset accordingly.
8212 assert(i == NumMemOps-1 && i != 0);
8213 DstOff -= VTSize - Size;
8214 }
8215
8216 // If this store is smaller than the largest store see whether we can get
8217 // the smaller value for free with a truncate or extract vector element and
8218 // then store.
8219 SDValue Value = MemSetValue;
8220 if (VT.bitsLT(LargestVT)) {
8221 unsigned Index;
8222 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8223 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8224 if (!LargestVT.isVector() && !VT.isVector() &&
8225 TLI.isTruncateFree(LargestVT, VT))
8226 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8227 else if (LargestVT.isVector() && !VT.isVector() &&
8229 LargestVT.getTypeForEVT(*DAG.getContext()),
8230 VT.getSizeInBits(), Index) &&
8231 TLI.isTypeLegal(SVT) &&
8232 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8233 // Target which can combine store(extractelement VectorTy, Idx) can get
8234 // the smaller value for free.
8235 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8236 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
8237 DAG.getVectorIdxConstant(Index, dl));
8238 } else
8239 Value = getMemsetValue(Src, VT, DAG, dl);
8240 }
8241 assert(Value.getValueType() == VT && "Value with wrong type.");
8242 SDValue Store = DAG.getStore(
8243 Chain, dl, Value,
8244 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8245 DstPtrInfo.getWithOffset(DstOff), Alignment,
8247 NewAAInfo);
8248 OutChains.push_back(Store);
8249 DstOff += VT.getSizeInBits() / 8;
8250 Size -= VTSize;
8251 }
8252
8253 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8254}
8255
8257 unsigned AS) {
8258 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8259 // pointer operands can be losslessly bitcasted to pointers of address space 0
8260 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8261 report_fatal_error("cannot lower memory intrinsic in address space " +
8262 Twine(AS));
8263 }
8264}
8265
8267 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
8268 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
8269 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
8270 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, AAResults *AA) {
8271 // Check to see if we should lower the memcpy to loads and stores first.
8272 // For cases within the target-specified limits, this is the best choice.
8273 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8274 if (ConstantSize) {
8275 // Memcpy with size zero? Just return the original chain.
8276 if (ConstantSize->isZero())
8277 return Chain;
8278
8280 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8281 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8282 if (Result.getNode())
8283 return Result;
8284 }
8285
8286 // Then check to see if we should lower the memcpy with target-specific
8287 // code. If the target chooses to do this, this is the next best.
8288 if (TSI) {
8289 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8290 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8291 DstPtrInfo, SrcPtrInfo);
8292 if (Result.getNode())
8293 return Result;
8294 }
8295
8296 // If we really need inline code and the target declined to provide it,
8297 // use a (potentially long) sequence of loads and stores.
8298 if (AlwaysInline) {
8299 assert(ConstantSize && "AlwaysInline requires a constant size!");
8301 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8302 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8303 }
8304
8307
8308 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8309 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8310 // respect volatile, so they may do things like read or write memory
8311 // beyond the given memory regions. But fixing this isn't easy, and most
8312 // people don't care.
8313
8314 // Emit a library call.
8317 Entry.Ty = PointerType::getUnqual(*getContext());
8318 Entry.Node = Dst; Args.push_back(Entry);
8319 Entry.Node = Src; Args.push_back(Entry);
8320
8321 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8322 Entry.Node = Size; Args.push_back(Entry);
8323 // FIXME: pass in SDLoc
8325 bool IsTailCall = false;
8326 if (OverrideTailCall.has_value()) {
8327 IsTailCall = *OverrideTailCall;
8328 } else {
8329 bool LowersToMemcpy =
8330 TLI->getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy");
8331 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8332 IsTailCall = CI && CI->isTailCall() &&
8334 ReturnsFirstArg && LowersToMemcpy);
8335 }
8336
8337 CLI.setDebugLoc(dl)
8338 .setChain(Chain)
8339 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8340 Dst.getValueType().getTypeForEVT(*getContext()),
8341 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8342 TLI->getPointerTy(getDataLayout())),
8343 std::move(Args))
8345 .setTailCall(IsTailCall);
8346
8347 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8348 return CallResult.second;
8349}
8350
8352 SDValue Dst, SDValue Src, SDValue Size,
8353 Type *SizeTy, unsigned ElemSz,
8354 bool isTailCall,
8355 MachinePointerInfo DstPtrInfo,
8356 MachinePointerInfo SrcPtrInfo) {
8357 // Emit a library call.
8360 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8361 Entry.Node = Dst;
8362 Args.push_back(Entry);
8363
8364 Entry.Node = Src;
8365 Args.push_back(Entry);
8366
8367 Entry.Ty = SizeTy;
8368 Entry.Node = Size;
8369 Args.push_back(Entry);
8370
8371 RTLIB::Libcall LibraryCall =
8373 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8374 report_fatal_error("Unsupported element size");
8375
8377 CLI.setDebugLoc(dl)
8378 .setChain(Chain)
8379 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8381 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8382 TLI->getPointerTy(getDataLayout())),
8383 std::move(Args))
8385 .setTailCall(isTailCall);
8386
8387 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8388 return CallResult.second;
8389}
8390
8392 SDValue Src, SDValue Size, Align Alignment,
8393 bool isVol, const CallInst *CI,
8394 std::optional<bool> OverrideTailCall,
8395 MachinePointerInfo DstPtrInfo,
8396 MachinePointerInfo SrcPtrInfo,
8397 const AAMDNodes &AAInfo, AAResults *AA) {
8398 // Check to see if we should lower the memmove to loads and stores first.
8399 // For cases within the target-specified limits, this is the best choice.
8400 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8401 if (ConstantSize) {
8402 // Memmove with size zero? Just return the original chain.
8403 if (ConstantSize->isZero())
8404 return Chain;
8405
8407 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8408 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8409 if (Result.getNode())
8410 return Result;
8411 }
8412
8413 // Then check to see if we should lower the memmove with target-specific
8414 // code. If the target chooses to do this, this is the next best.
8415 if (TSI) {
8416 SDValue Result =
8417 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8418 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8419 if (Result.getNode())
8420 return Result;
8421 }
8422
8425
8426 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8427 // not be safe. See memcpy above for more details.
8428
8429 // Emit a library call.
8432 Entry.Ty = PointerType::getUnqual(*getContext());
8433 Entry.Node = Dst; Args.push_back(Entry);
8434 Entry.Node = Src; Args.push_back(Entry);
8435
8436 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8437 Entry.Node = Size; Args.push_back(Entry);
8438 // FIXME: pass in SDLoc
8440
8441 bool IsTailCall = false;
8442 if (OverrideTailCall.has_value()) {
8443 IsTailCall = *OverrideTailCall;
8444 } else {
8445 bool LowersToMemmove =
8446 TLI->getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove");
8447 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8448 IsTailCall = CI && CI->isTailCall() &&
8450 ReturnsFirstArg && LowersToMemmove);
8451 }
8452
8453 CLI.setDebugLoc(dl)
8454 .setChain(Chain)
8455 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8456 Dst.getValueType().getTypeForEVT(*getContext()),
8457 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8458 TLI->getPointerTy(getDataLayout())),
8459 std::move(Args))
8461 .setTailCall(IsTailCall);
8462
8463 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8464 return CallResult.second;
8465}
8466
8468 SDValue Dst, SDValue Src, SDValue Size,
8469 Type *SizeTy, unsigned ElemSz,
8470 bool isTailCall,
8471 MachinePointerInfo DstPtrInfo,
8472 MachinePointerInfo SrcPtrInfo) {
8473 // Emit a library call.
8476 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8477 Entry.Node = Dst;
8478 Args.push_back(Entry);
8479
8480 Entry.Node = Src;
8481 Args.push_back(Entry);
8482
8483 Entry.Ty = SizeTy;
8484 Entry.Node = Size;
8485 Args.push_back(Entry);
8486
8487 RTLIB::Libcall LibraryCall =
8489 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8490 report_fatal_error("Unsupported element size");
8491
8493 CLI.setDebugLoc(dl)
8494 .setChain(Chain)
8495 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8497 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8498 TLI->getPointerTy(getDataLayout())),
8499 std::move(Args))
8501 .setTailCall(isTailCall);
8502
8503 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8504 return CallResult.second;
8505}
8506
8508 SDValue Src, SDValue Size, Align Alignment,
8509 bool isVol, bool AlwaysInline,
8510 const CallInst *CI,
8511 MachinePointerInfo DstPtrInfo,
8512 const AAMDNodes &AAInfo) {
8513 // Check to see if we should lower the memset to stores first.
8514 // For cases within the target-specified limits, this is the best choice.
8515 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8516 if (ConstantSize) {
8517 // Memset with size zero? Just return the original chain.
8518 if (ConstantSize->isZero())
8519 return Chain;
8520
8521 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8522 ConstantSize->getZExtValue(), Alignment,
8523 isVol, false, DstPtrInfo, AAInfo);
8524
8525 if (Result.getNode())
8526 return Result;
8527 }
8528
8529 // Then check to see if we should lower the memset with target-specific
8530 // code. If the target chooses to do this, this is the next best.
8531 if (TSI) {
8532 SDValue Result = TSI->EmitTargetCodeForMemset(
8533 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8534 if (Result.getNode())
8535 return Result;
8536 }
8537
8538 // If we really need inline code and the target declined to provide it,
8539 // use a (potentially long) sequence of loads and stores.
8540 if (AlwaysInline) {
8541 assert(ConstantSize && "AlwaysInline requires a constant size!");
8542 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8543 ConstantSize->getZExtValue(), Alignment,
8544 isVol, true, DstPtrInfo, AAInfo);
8545 assert(Result &&
8546 "getMemsetStores must return a valid sequence when AlwaysInline");
8547 return Result;
8548 }
8549
8551
8552 // Emit a library call.
8553 auto &Ctx = *getContext();
8554 const auto& DL = getDataLayout();
8555
8557 // FIXME: pass in SDLoc
8558 CLI.setDebugLoc(dl).setChain(Chain);
8559
8560 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8561
8562 // Helper function to create an Entry from Node and Type.
8563 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8565 Entry.Node = Node;
8566 Entry.Ty = Ty;
8567 return Entry;
8568 };
8569
8570 bool UseBZero = isNullConstant(Src) && BzeroName;
8571 // If zeroing out and bzero is present, use it.
8572 if (UseBZero) {
8574 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8575 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8576 CLI.setLibCallee(
8577 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8578 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8579 } else {
8581 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8582 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8583 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8584 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8585 Dst.getValueType().getTypeForEVT(Ctx),
8586 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8587 TLI->getPointerTy(DL)),
8588 std::move(Args));
8589 }
8590 bool LowersToMemset =
8591 TLI->getLibcallName(RTLIB::MEMSET) == StringRef("memset");
8592 // If we're going to use bzero, make sure not to tail call unless the
8593 // subsequent return doesn't need a value, as bzero doesn't return the first
8594 // arg unlike memset.
8595 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
8596 bool IsTailCall =
8597 CI && CI->isTailCall() &&
8598 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
8599 CLI.setDiscardResult().setTailCall(IsTailCall);
8600
8601 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8602 return CallResult.second;
8603}
8604
8607 Type *SizeTy, unsigned ElemSz,
8608 bool isTailCall,
8609 MachinePointerInfo DstPtrInfo) {
8610 // Emit a library call.
8613 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8614 Entry.Node = Dst;
8615 Args.push_back(Entry);
8616
8617 Entry.Ty = Type::getInt8Ty(*getContext());
8618 Entry.Node = Value;
8619 Args.push_back(Entry);
8620
8621 Entry.Ty = SizeTy;
8622 Entry.Node = Size;
8623 Args.push_back(Entry);
8624
8625 RTLIB::Libcall LibraryCall =
8627 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8628 report_fatal_error("Unsupported element size");
8629
8631 CLI.setDebugLoc(dl)
8632 .setChain(Chain)
8633 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8635 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8636 TLI->getPointerTy(getDataLayout())),
8637 std::move(Args))
8639 .setTailCall(isTailCall);
8640
8641 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8642 return CallResult.second;
8643}
8644
8645SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8646 SDVTList VTList, ArrayRef<SDValue> Ops,
8647 MachineMemOperand *MMO) {
8649 ID.AddInteger(MemVT.getRawBits());
8650 AddNodeIDNode(ID, Opcode, VTList, Ops);
8651 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8652 ID.AddInteger(MMO->getFlags());
8653 void* IP = nullptr;
8654 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8655 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8656 return SDValue(E, 0);
8657 }
8658
8659 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8660 VTList, MemVT, MMO);
8661 createOperands(N, Ops);
8662
8663 CSEMap.InsertNode(N, IP);
8664 InsertNode(N);
8665 return SDValue(N, 0);
8666}
8667
8669 EVT MemVT, SDVTList VTs, SDValue Chain,
8670 SDValue Ptr, SDValue Cmp, SDValue Swp,
8671 MachineMemOperand *MMO) {
8672 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8674 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8675
8676 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8677 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8678}
8679
8680SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8681 SDValue Chain, SDValue Ptr, SDValue Val,
8682 MachineMemOperand *MMO) {
8683 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
8684 Opcode == ISD::ATOMIC_LOAD_SUB ||
8685 Opcode == ISD::ATOMIC_LOAD_AND ||
8686 Opcode == ISD::ATOMIC_LOAD_CLR ||
8687 Opcode == ISD::ATOMIC_LOAD_OR ||
8688 Opcode == ISD::ATOMIC_LOAD_XOR ||
8689 Opcode == ISD::ATOMIC_LOAD_NAND ||
8690 Opcode == ISD::ATOMIC_LOAD_MIN ||
8691 Opcode == ISD::ATOMIC_LOAD_MAX ||
8692 Opcode == ISD::ATOMIC_LOAD_UMIN ||
8693 Opcode == ISD::ATOMIC_LOAD_UMAX ||
8694 Opcode == ISD::ATOMIC_LOAD_FADD ||
8695 Opcode == ISD::ATOMIC_LOAD_FSUB ||
8696 Opcode == ISD::ATOMIC_LOAD_FMAX ||
8697 Opcode == ISD::ATOMIC_LOAD_FMIN ||
8698 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
8699 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
8700 Opcode == ISD::ATOMIC_SWAP ||
8701 Opcode == ISD::ATOMIC_STORE) &&
8702 "Invalid Atomic Op");
8703
8704 EVT VT = Val.getValueType();
8705
8706 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
8707 getVTList(VT, MVT::Other);
8708 SDValue Ops[] = {Chain, Ptr, Val};
8709 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8710}
8711
8712SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8713 EVT VT, SDValue Chain, SDValue Ptr,
8714 MachineMemOperand *MMO) {
8715 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
8716
8717 SDVTList VTs = getVTList(VT, MVT::Other);
8718 SDValue Ops[] = {Chain, Ptr};
8719 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8720}
8721
8722/// getMergeValues - Create a MERGE_VALUES node from the given operands.
8724 if (Ops.size() == 1)
8725 return Ops[0];
8726
8728 VTs.reserve(Ops.size());
8729 for (const SDValue &Op : Ops)
8730 VTs.push_back(Op.getValueType());
8731 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
8732}
8733
8735 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
8736 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
8738 const AAMDNodes &AAInfo) {
8739 if (Size.hasValue() && !Size.getValue())
8741
8743 MachineMemOperand *MMO =
8744 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
8745
8746 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
8747}
8748
8750 SDVTList VTList,
8751 ArrayRef<SDValue> Ops, EVT MemVT,
8752 MachineMemOperand *MMO) {
8753 assert((Opcode == ISD::INTRINSIC_VOID ||
8754 Opcode == ISD::INTRINSIC_W_CHAIN ||
8755 Opcode == ISD::PREFETCH ||
8756 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
8757 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
8758 "Opcode is not a memory-accessing opcode!");
8759
8760 // Memoize the node unless it returns a glue result.
8762 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
8764 AddNodeIDNode(ID, Opcode, VTList, Ops);
8765 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
8766 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
8767 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8768 ID.AddInteger(MMO->getFlags());
8769 ID.AddInteger(MemVT.getRawBits());
8770 void *IP = nullptr;
8771 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8772 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
8773 return SDValue(E, 0);
8774 }
8775
8776 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8777 VTList, MemVT, MMO);
8778 createOperands(N, Ops);
8779
8780 CSEMap.InsertNode(N, IP);
8781 } else {
8782 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8783 VTList, MemVT, MMO);
8784 createOperands(N, Ops);
8785 }
8786 InsertNode(N);
8787 SDValue V(N, 0);
8788 NewSDValueDbgMsg(V, "Creating new node: ", this);
8789 return V;
8790}
8791
8793 SDValue Chain, int FrameIndex,
8794 int64_t Size, int64_t Offset) {
8795 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
8796 const auto VTs = getVTList(MVT::Other);
8797 SDValue Ops[2] = {
8798 Chain,
8799 getFrameIndex(FrameIndex,
8800 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
8801 true)};
8802
8804 AddNodeIDNode(ID, Opcode, VTs, Ops);
8805 ID.AddInteger(FrameIndex);
8806 ID.AddInteger(Size);
8807 ID.AddInteger(Offset);
8808 void *IP = nullptr;
8809 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8810 return SDValue(E, 0);
8811
8812 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
8813 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
8814 createOperands(N, Ops);
8815 CSEMap.InsertNode(N, IP);
8816 InsertNode(N);
8817 SDValue V(N, 0);
8818 NewSDValueDbgMsg(V, "Creating new node: ", this);
8819 return V;
8820}
8821
8824 uint32_t Attr) {
8825 const unsigned Opcode = ISD::PSEUDO_PROBE;
8826 const auto VTs = getVTList(MVT::Other);
8827 SDValue Ops[] = {Chain};
8829 AddNodeIDNode(ID, Opcode, VTs, Ops);
8830 ID.AddInteger(Guid);
8831 ID.AddInteger(Index);
8832 void *IP = nullptr;
8833 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
8834 return SDValue(E, 0);
8835
8836 auto *N = newSDNode<PseudoProbeSDNode>(
8837 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
8838 createOperands(N, Ops);
8839 CSEMap.InsertNode(N, IP);
8840 InsertNode(N);
8841 SDValue V(N, 0);
8842 NewSDValueDbgMsg(V, "Creating new node: ", this);
8843 return V;
8844}
8845
8846/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8847/// MachinePointerInfo record from it. This is particularly useful because the
8848/// code generator has many cases where it doesn't bother passing in a
8849/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8851 SelectionDAG &DAG, SDValue Ptr,
8852 int64_t Offset = 0) {
8853 // If this is FI+Offset, we can model it.
8854 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
8856 FI->getIndex(), Offset);
8857
8858 // If this is (FI+Offset1)+Offset2, we can model it.
8859 if (Ptr.getOpcode() != ISD::ADD ||
8860 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
8861 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
8862 return Info;
8863
8864 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
8866 DAG.getMachineFunction(), FI,
8867 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
8868}
8869
8870/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8871/// MachinePointerInfo record from it. This is particularly useful because the
8872/// code generator has many cases where it doesn't bother passing in a
8873/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8875 SelectionDAG &DAG, SDValue Ptr,
8876 SDValue OffsetOp) {
8877 // If the 'Offset' value isn't a constant, we can't handle this.
8878 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
8879 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
8880 if (OffsetOp.isUndef())
8881 return InferPointerInfo(Info, DAG, Ptr);
8882 return Info;
8883}
8884
8886 EVT VT, const SDLoc &dl, SDValue Chain,
8888 MachinePointerInfo PtrInfo, EVT MemVT,
8889 Align Alignment,
8890 MachineMemOperand::Flags MMOFlags,
8891 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8892 assert(Chain.getValueType() == MVT::Other &&
8893 "Invalid chain type");
8894
8895 MMOFlags |= MachineMemOperand::MOLoad;
8896 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8897 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8898 // clients.
8899 if (PtrInfo.V.isNull())
8900 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8901
8904 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8905 Alignment, AAInfo, Ranges);
8906 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
8907}
8908
8910 EVT VT, const SDLoc &dl, SDValue Chain,
8911 SDValue Ptr, SDValue Offset, EVT MemVT,
8912 MachineMemOperand *MMO) {
8913 if (VT == MemVT) {
8914 ExtType = ISD::NON_EXTLOAD;
8915 } else if (ExtType == ISD::NON_EXTLOAD) {
8916 assert(VT == MemVT && "Non-extending load from different memory type!");
8917 } else {
8918 // Extending load.
8919 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
8920 "Should only be an extending load, not truncating!");
8921 assert(VT.isInteger() == MemVT.isInteger() &&
8922 "Cannot convert from FP to Int or Int -> FP!");
8923 assert(VT.isVector() == MemVT.isVector() &&
8924 "Cannot use an ext load to convert to or from a vector!");
8925 assert((!VT.isVector() ||
8927 "Cannot use an ext load to change the number of vector elements!");
8928 }
8929
8930 bool Indexed = AM != ISD::UNINDEXED;
8931 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8932
8933 SDVTList VTs = Indexed ?
8934 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
8935 SDValue Ops[] = { Chain, Ptr, Offset };
8937 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
8938 ID.AddInteger(MemVT.getRawBits());
8939 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
8940 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
8941 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8942 ID.AddInteger(MMO->getFlags());
8943 void *IP = nullptr;
8944 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8945 cast<LoadSDNode>(E)->refineAlignment(MMO);
8946 return SDValue(E, 0);
8947 }
8948 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8949 ExtType, MemVT, MMO);
8950 createOperands(N, Ops);
8951
8952 CSEMap.InsertNode(N, IP);
8953 InsertNode(N);
8954 SDValue V(N, 0);
8955 NewSDValueDbgMsg(V, "Creating new node: ", this);
8956 return V;
8957}
8958
8961 MaybeAlign Alignment,
8962 MachineMemOperand::Flags MMOFlags,
8963 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8964 SDValue Undef = getUNDEF(Ptr.getValueType());
8965 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8966 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
8967}
8968
8971 SDValue Undef = getUNDEF(Ptr.getValueType());
8972 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8973 VT, MMO);
8974}
8975
8977 EVT VT, SDValue Chain, SDValue Ptr,
8978 MachinePointerInfo PtrInfo, EVT MemVT,
8979 MaybeAlign Alignment,
8980 MachineMemOperand::Flags MMOFlags,
8981 const AAMDNodes &AAInfo) {
8982 SDValue Undef = getUNDEF(Ptr.getValueType());
8983 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
8984 MemVT, Alignment, MMOFlags, AAInfo);
8985}
8986
8988 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
8989 MachineMemOperand *MMO) {
8990 SDValue Undef = getUNDEF(Ptr.getValueType());
8991 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
8992 MemVT, MMO);
8993}
8994
8998 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
8999 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9000 // Don't propagate the invariant or dereferenceable flags.
9001 auto MMOFlags =
9002 LD->getMemOperand()->getFlags() &
9004 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9005 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9006 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9007}
9008
9011 Align Alignment,
9012 MachineMemOperand::Flags MMOFlags,
9013 const AAMDNodes &AAInfo) {
9014 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9015
9016 MMOFlags |= MachineMemOperand::MOStore;
9017 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9018
9019 if (PtrInfo.V.isNull())
9020 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9021
9024 MachineMemOperand *MMO =
9025 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9026 return getStore(Chain, dl, Val, Ptr, MMO);
9027}
9028
9031 assert(Chain.getValueType() == MVT::Other &&
9032 "Invalid chain type");
9033 EVT VT = Val.getValueType();
9034 SDVTList VTs = getVTList(MVT::Other);
9035 SDValue Undef = getUNDEF(Ptr.getValueType());
9036 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9038 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9039 ID.AddInteger(VT.getRawBits());
9040 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9041 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
9042 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9043 ID.AddInteger(MMO->getFlags());
9044 void *IP = nullptr;
9045 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9046 cast<StoreSDNode>(E)->refineAlignment(MMO);
9047 return SDValue(E, 0);
9048 }
9049 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9050 ISD::UNINDEXED, false, VT, MMO);
9051 createOperands(N, Ops);
9052
9053 CSEMap.InsertNode(N, IP);
9054 InsertNode(N);
9055 SDValue V(N, 0);
9056 NewSDValueDbgMsg(V, "Creating new node: ", this);
9057 return V;
9058}
9059
9062 EVT SVT, Align Alignment,
9063 MachineMemOperand::Flags MMOFlags,
9064 const AAMDNodes &AAInfo) {
9065 assert(Chain.getValueType() == MVT::Other &&
9066 "Invalid chain type");
9067
9068 MMOFlags |= MachineMemOperand::MOStore;
9069 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9070
9071 if (PtrInfo.V.isNull())
9072 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9073
9076 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9077 AAInfo);
9078 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9079}
9080
9082 SDValue Ptr, EVT SVT,
9083 MachineMemOperand *MMO) {
9084 EVT VT = Val.getValueType();
9085
9086 assert(Chain.getValueType() == MVT::Other &&
9087 "Invalid chain type");
9088 if (VT == SVT)
9089 return getStore(Chain, dl, Val, Ptr, MMO);
9090
9092 "Should only be a truncating store, not extending!");
9093 assert(VT.isInteger() == SVT.isInteger() &&
9094 "Can't do FP-INT conversion!");
9095 assert(VT.isVector() == SVT.isVector() &&
9096 "Cannot use trunc store to convert to or from a vector!");
9097 assert((!VT.isVector() ||
9099 "Cannot use trunc store to change the number of vector elements!");
9100
9101 SDVTList VTs = getVTList(MVT::Other);
9102 SDValue Undef = getUNDEF(Ptr.getValueType());
9103 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9105 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9106 ID.AddInteger(SVT.getRawBits());
9107 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9108 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
9109 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9110 ID.AddInteger(MMO->getFlags());
9111 void *IP = nullptr;
9112 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9113 cast<StoreSDNode>(E)->refineAlignment(MMO);
9114 return SDValue(E, 0);
9115 }
9116 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9117 ISD::UNINDEXED, true, SVT, MMO);
9118 createOperands(N, Ops);
9119
9120 CSEMap.InsertNode(N, IP);
9121 InsertNode(N);
9122 SDValue V(N, 0);
9123 NewSDValueDbgMsg(V, "Creating new node: ", this);
9124 return V;
9125}
9126
9130 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9131 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9132 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9133 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
9135 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9136 ID.AddInteger(ST->getMemoryVT().getRawBits());
9137 ID.AddInteger(ST->getRawSubclassData());
9138 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9139 ID.AddInteger(ST->getMemOperand()->getFlags());
9140 void *IP = nullptr;
9141 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9142 return SDValue(E, 0);
9143
9144 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9145 ST->isTruncatingStore(), ST->getMemoryVT(),
9146 ST->getMemOperand());
9147 createOperands(N, Ops);
9148
9149 CSEMap.InsertNode(N, IP);
9150 InsertNode(N);
9151 SDValue V(N, 0);
9152 NewSDValueDbgMsg(V, "Creating new node: ", this);
9153 return V;
9154}
9155
9157 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9158 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9159 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9160 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9161 const MDNode *Ranges, bool IsExpanding) {
9162 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9163
9164 MMOFlags |= MachineMemOperand::MOLoad;
9165 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9166 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9167 // clients.
9168 if (PtrInfo.V.isNull())
9169 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9170
9173 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9174 Alignment, AAInfo, Ranges);
9175 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9176 MMO, IsExpanding);
9177}
9178
9180 ISD::LoadExtType ExtType, EVT VT,
9181 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9182 SDValue Offset, SDValue Mask, SDValue EVL,
9183 EVT MemVT, MachineMemOperand *MMO,
9184 bool IsExpanding) {
9185 bool Indexed = AM != ISD::UNINDEXED;
9186 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9187
9188 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9189 : getVTList(VT, MVT::Other);
9190 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9192 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9193 ID.AddInteger(MemVT.getRawBits());
9194 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9195 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9196 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9197 ID.AddInteger(MMO->getFlags());
9198 void *IP = nullptr;
9199 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9200 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
9201 return SDValue(E, 0);
9202 }
9203 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9204 ExtType, IsExpanding, MemVT, MMO);
9205 createOperands(N, Ops);
9206
9207 CSEMap.InsertNode(N, IP);
9208 InsertNode(N);
9209 SDValue V(N, 0);
9210 NewSDValueDbgMsg(V, "Creating new node: ", this);
9211 return V;
9212}
9213
9215 SDValue Ptr, SDValue Mask, SDValue EVL,
9216 MachinePointerInfo PtrInfo,
9217 MaybeAlign Alignment,
9218 MachineMemOperand::Flags MMOFlags,
9219 const AAMDNodes &AAInfo, const MDNode *Ranges,
9220 bool IsExpanding) {
9221 SDValue Undef = getUNDEF(Ptr.getValueType());
9222 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9223 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9224 IsExpanding);
9225}
9226
9228 SDValue Ptr, SDValue Mask, SDValue EVL,
9229 MachineMemOperand *MMO, bool IsExpanding) {
9230 SDValue Undef = getUNDEF(Ptr.getValueType());
9231 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9232 Mask, EVL, VT, MMO, IsExpanding);
9233}
9234
9236 EVT VT, SDValue Chain, SDValue Ptr,
9237 SDValue Mask, SDValue EVL,
9238 MachinePointerInfo PtrInfo, EVT MemVT,
9239 MaybeAlign Alignment,
9240 MachineMemOperand::Flags MMOFlags,
9241 const AAMDNodes &AAInfo, bool IsExpanding) {
9242 SDValue Undef = getUNDEF(Ptr.getValueType());
9243 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9244 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9245 IsExpanding);
9246}
9247
9249 EVT VT, SDValue Chain, SDValue Ptr,
9250 SDValue Mask, SDValue EVL, EVT MemVT,
9251 MachineMemOperand *MMO, bool IsExpanding) {
9252 SDValue Undef = getUNDEF(Ptr.getValueType());
9253 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9254 EVL, MemVT, MMO, IsExpanding);
9255}
9256
9260 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9261 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9262 // Don't propagate the invariant or dereferenceable flags.
9263 auto MMOFlags =
9264 LD->getMemOperand()->getFlags() &
9266 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9267 LD->getChain(), Base, Offset, LD->getMask(),
9268 LD->getVectorLength(), LD->getPointerInfo(),
9269 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
9270 nullptr, LD->isExpandingLoad());
9271}
9272
9275 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
9276 ISD::MemIndexedMode AM, bool IsTruncating,
9277 bool IsCompressing) {
9278 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9279 bool Indexed = AM != ISD::UNINDEXED;
9280 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9281 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9282 : getVTList(MVT::Other);
9283 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9285 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9286 ID.AddInteger(MemVT.getRawBits());
9287 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9288 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9289 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9290 ID.AddInteger(MMO->getFlags());
9291 void *IP = nullptr;
9292 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9293 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9294 return SDValue(E, 0);
9295 }
9296 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9297 IsTruncating, IsCompressing, MemVT, MMO);
9298 createOperands(N, Ops);
9299
9300 CSEMap.InsertNode(N, IP);
9301 InsertNode(N);
9302 SDValue V(N, 0);
9303 NewSDValueDbgMsg(V, "Creating new node: ", this);
9304 return V;
9305}
9306
9308 SDValue Val, SDValue Ptr, SDValue Mask,
9309 SDValue EVL, MachinePointerInfo PtrInfo,
9310 EVT SVT, Align Alignment,
9311 MachineMemOperand::Flags MMOFlags,
9312 const AAMDNodes &AAInfo,
9313 bool IsCompressing) {
9314 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9315
9316 MMOFlags |= MachineMemOperand::MOStore;
9317 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9318
9319 if (PtrInfo.V.isNull())
9320 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9321
9324 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9325 AAInfo);
9326 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9327 IsCompressing);
9328}
9329
9331 SDValue Val, SDValue Ptr, SDValue Mask,
9332 SDValue EVL, EVT SVT,
9333 MachineMemOperand *MMO,
9334 bool IsCompressing) {
9335 EVT VT = Val.getValueType();
9336
9337 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9338 if (VT == SVT)
9339 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9340 EVL, VT, MMO, ISD::UNINDEXED,
9341 /*IsTruncating*/ false, IsCompressing);
9342
9344 "Should only be a truncating store, not extending!");
9345 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9346 assert(VT.isVector() == SVT.isVector() &&
9347 "Cannot use trunc store to convert to or from a vector!");
9348 assert((!VT.isVector() ||
9350 "Cannot use trunc store to change the number of vector elements!");
9351
9352 SDVTList VTs = getVTList(MVT::Other);
9353 SDValue Undef = getUNDEF(Ptr.getValueType());
9354 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9356 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9357 ID.AddInteger(SVT.getRawBits());
9358 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9359 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9360 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9361 ID.AddInteger(MMO->getFlags());
9362 void *IP = nullptr;
9363 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9364 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9365 return SDValue(E, 0);
9366 }
9367 auto *N =
9368 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9369 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
9370 createOperands(N, Ops);
9371
9372 CSEMap.InsertNode(N, IP);
9373 InsertNode(N);
9374 SDValue V(N, 0);
9375 NewSDValueDbgMsg(V, "Creating new node: ", this);
9376 return V;
9377}
9378
9382 auto *ST = cast<VPStoreSDNode>(OrigStore);
9383 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9384 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9385 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9386 Offset, ST->getMask(), ST->getVectorLength()};
9388 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9389 ID.AddInteger(ST->getMemoryVT().getRawBits());
9390 ID.AddInteger(ST->getRawSubclassData());
9391 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9392 ID.AddInteger(ST->getMemOperand()->getFlags());
9393 void *IP = nullptr;
9394 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9395 return SDValue(E, 0);
9396
9397 auto *N = newSDNode<VPStoreSDNode>(
9398 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9399 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9400 createOperands(N, Ops);
9401
9402 CSEMap.InsertNode(N, IP);
9403 InsertNode(N);
9404 SDValue V(N, 0);
9405 NewSDValueDbgMsg(V, "Creating new node: ", this);
9406 return V;
9407}
9408
9410 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9411 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9412 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9413 bool Indexed = AM != ISD::UNINDEXED;
9414 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9415
9416 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9417 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9418 : getVTList(VT, MVT::Other);
9420 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9421 ID.AddInteger(VT.getRawBits());
9422 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9423 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9424 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9425
9426 void *IP = nullptr;
9427 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9428 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9429 return SDValue(E, 0);
9430 }
9431
9432 auto *N =
9433 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9434 ExtType, IsExpanding, MemVT, MMO);
9435 createOperands(N, Ops);
9436 CSEMap.InsertNode(N, IP);
9437 InsertNode(N);
9438 SDValue V(N, 0);
9439 NewSDValueDbgMsg(V, "Creating new node: ", this);
9440 return V;
9441}
9442
9444 SDValue Ptr, SDValue Stride,
9445 SDValue Mask, SDValue EVL,
9446 MachineMemOperand *MMO,
9447 bool IsExpanding) {
9448 SDValue Undef = getUNDEF(Ptr.getValueType());
9450 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9451}
9452
9454 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9455 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9456 MachineMemOperand *MMO, bool IsExpanding) {
9457 SDValue Undef = getUNDEF(Ptr.getValueType());
9458 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9459 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9460}
9461
9463 SDValue Val, SDValue Ptr,
9464 SDValue Offset, SDValue Stride,
9465 SDValue Mask, SDValue EVL, EVT MemVT,
9466 MachineMemOperand *MMO,
9468 bool IsTruncating, bool IsCompressing) {
9469 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9470 bool Indexed = AM != ISD::UNINDEXED;
9471 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9472 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9473 : getVTList(MVT::Other);
9474 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9476 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9477 ID.AddInteger(MemVT.getRawBits());
9478 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9479 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9480 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9481 void *IP = nullptr;
9482 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9483 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9484 return SDValue(E, 0);
9485 }
9486 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9487 VTs, AM, IsTruncating,
9488 IsCompressing, MemVT, MMO);
9489 createOperands(N, Ops);
9490
9491 CSEMap.InsertNode(N, IP);
9492 InsertNode(N);
9493 SDValue V(N, 0);
9494 NewSDValueDbgMsg(V, "Creating new node: ", this);
9495 return V;
9496}
9497
9499 SDValue Val, SDValue Ptr,
9500 SDValue Stride, SDValue Mask,
9501 SDValue EVL, EVT SVT,
9502 MachineMemOperand *MMO,
9503 bool IsCompressing) {
9504 EVT VT = Val.getValueType();
9505
9506 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9507 if (VT == SVT)
9508 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9509 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9510 /*IsTruncating*/ false, IsCompressing);
9511
9513 "Should only be a truncating store, not extending!");
9514 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9515 assert(VT.isVector() == SVT.isVector() &&
9516 "Cannot use trunc store to convert to or from a vector!");
9517 assert((!VT.isVector() ||
9519 "Cannot use trunc store to change the number of vector elements!");
9520
9521 SDVTList VTs = getVTList(MVT::Other);
9522 SDValue Undef = getUNDEF(Ptr.getValueType());
9523 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9525 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9526 ID.AddInteger(SVT.getRawBits());
9527 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9528 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9529 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9530 void *IP = nullptr;
9531 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9532 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9533 return SDValue(E, 0);
9534 }
9535 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9536 VTs, ISD::UNINDEXED, true,
9537 IsCompressing, SVT, MMO);
9538 createOperands(N, Ops);
9539
9540 CSEMap.InsertNode(N, IP);
9541 InsertNode(N);
9542 SDValue V(N, 0);
9543 NewSDValueDbgMsg(V, "Creating new node: ", this);
9544 return V;
9545}
9546
9549 ISD::MemIndexType IndexType) {
9550 assert(Ops.size() == 6 && "Incompatible number of operands");
9551
9553 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9554 ID.AddInteger(VT.getRawBits());
9555 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9556 dl.getIROrder(), VTs, VT, MMO, IndexType));
9557 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9558 ID.AddInteger(MMO->getFlags());
9559 void *IP = nullptr;
9560 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9561 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9562 return SDValue(E, 0);
9563 }
9564
9565 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9566 VT, MMO, IndexType);
9567 createOperands(N, Ops);
9568
9569 assert(N->getMask().getValueType().getVectorElementCount() ==
9570 N->getValueType(0).getVectorElementCount() &&
9571 "Vector width mismatch between mask and data");
9572 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9573 N->getValueType(0).getVectorElementCount().isScalable() &&
9574 "Scalable flags of index and data do not match");
9576 N->getIndex().getValueType().getVectorElementCount(),
9577 N->getValueType(0).getVectorElementCount()) &&
9578 "Vector width mismatch between index and data");
9579 assert(isa<ConstantSDNode>(N->getScale()) &&
9580 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9581 "Scale should be a constant power of 2");
9582
9583 CSEMap.InsertNode(N, IP);
9584 InsertNode(N);
9585 SDValue V(N, 0);
9586 NewSDValueDbgMsg(V, "Creating new node: ", this);
9587 return V;
9588}
9589
9592 MachineMemOperand *MMO,
9593 ISD::MemIndexType IndexType) {
9594 assert(Ops.size() == 7 && "Incompatible number of operands");
9595
9597 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9598 ID.AddInteger(VT.getRawBits());
9599 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9600 dl.getIROrder(), VTs, VT, MMO, IndexType));
9601 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9602 ID.AddInteger(MMO->getFlags());
9603 void *IP = nullptr;
9604 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9605 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9606 return SDValue(E, 0);
9607 }
9608 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9609 VT, MMO, IndexType);
9610 createOperands(N, Ops);
9611
9612 assert(N->getMask().getValueType().getVectorElementCount() ==
9613 N->getValue().getValueType().getVectorElementCount() &&
9614 "Vector width mismatch between mask and data");
9615 assert(
9616 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9617 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9618 "Scalable flags of index and data do not match");
9620 N->getIndex().getValueType().getVectorElementCount(),
9621 N->getValue().getValueType().getVectorElementCount()) &&
9622 "Vector width mismatch between index and data");
9623 assert(isa<ConstantSDNode>(N->getScale()) &&
9624 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9625 "Scale should be a constant power of 2");
9626
9627 CSEMap.InsertNode(N, IP);
9628 InsertNode(N);
9629 SDValue V(N, 0);
9630 NewSDValueDbgMsg(V, "Creating new node: ", this);
9631 return V;
9632}
9633
9636 SDValue PassThru, EVT MemVT,
9637 MachineMemOperand *MMO,
9639 ISD::LoadExtType ExtTy, bool isExpanding) {
9640 bool Indexed = AM != ISD::UNINDEXED;
9641 assert((Indexed || Offset.isUndef()) &&
9642 "Unindexed masked load with an offset!");
9643 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9644 : getVTList(VT, MVT::Other);
9645 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9647 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9648 ID.AddInteger(MemVT.getRawBits());
9649 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9650 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9651 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9652 ID.AddInteger(MMO->getFlags());
9653 void *IP = nullptr;
9654 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9655 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9656 return SDValue(E, 0);
9657 }
9658 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9659 AM, ExtTy, isExpanding, MemVT, MMO);
9660 createOperands(N, Ops);
9661
9662 CSEMap.InsertNode(N, IP);
9663 InsertNode(N);
9664 SDValue V(N, 0);
9665 NewSDValueDbgMsg(V, "Creating new node: ", this);
9666 return V;
9667}
9668
9672 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9673 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9674 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9675 Offset, LD->getMask(), LD->getPassThru(),
9676 LD->getMemoryVT(), LD->getMemOperand(), AM,
9677 LD->getExtensionType(), LD->isExpandingLoad());
9678}
9679
9682 SDValue Mask, EVT MemVT,
9683 MachineMemOperand *MMO,
9684 ISD::MemIndexedMode AM, bool IsTruncating,
9685 bool IsCompressing) {
9686 assert(Chain.getValueType() == MVT::Other &&
9687 "Invalid chain type");
9688 bool Indexed = AM != ISD::UNINDEXED;
9689 assert((Indexed || Offset.isUndef()) &&
9690 "Unindexed masked store with an offset!");
9691 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
9692 : getVTList(MVT::Other);
9693 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
9695 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
9696 ID.AddInteger(MemVT.getRawBits());
9697 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
9698 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9699 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9700 ID.AddInteger(MMO->getFlags());
9701 void *IP = nullptr;
9702 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9703 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
9704 return SDValue(E, 0);
9705 }
9706 auto *N =
9707 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9708 IsTruncating, IsCompressing, MemVT, MMO);
9709 createOperands(N, Ops);
9710
9711 CSEMap.InsertNode(N, IP);
9712 InsertNode(N);
9713 SDValue V(N, 0);
9714 NewSDValueDbgMsg(V, "Creating new node: ", this);
9715 return V;
9716}
9717
9721 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
9722 assert(ST->getOffset().isUndef() &&
9723 "Masked store is already a indexed store!");
9724 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9725 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
9726 AM, ST->isTruncatingStore(), ST->isCompressingStore());
9727}
9728
9731 MachineMemOperand *MMO,
9732 ISD::MemIndexType IndexType,
9733 ISD::LoadExtType ExtTy) {
9734 assert(Ops.size() == 6 && "Incompatible number of operands");
9735
9737 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
9738 ID.AddInteger(MemVT.getRawBits());
9739 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
9740 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
9741 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9742 ID.AddInteger(MMO->getFlags());
9743 void *IP = nullptr;
9744 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9745 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9746 return SDValue(E, 0);
9747 }
9748
9749 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9750 VTs, MemVT, MMO, IndexType, ExtTy);
9751 createOperands(N, Ops);
9752
9753 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
9754 "Incompatible type of the PassThru value in MaskedGatherSDNode");
9755 assert(N->getMask().getValueType().getVectorElementCount() ==
9756 N->getValueType(0).getVectorElementCount() &&
9757 "Vector width mismatch between mask and data");
9758 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9759 N->getValueType(0).getVectorElementCount().isScalable() &&
9760 "Scalable flags of index and data do not match");
9762 N->getIndex().getValueType().getVectorElementCount(),
9763 N->getValueType(0).getVectorElementCount()) &&
9764 "Vector width mismatch between index and data");
9765 assert(isa<ConstantSDNode>(N->getScale()) &&
9766 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9767 "Scale should be a constant power of 2");
9768
9769 CSEMap.InsertNode(N, IP);
9770 InsertNode(N);
9771 SDValue V(N, 0);
9772 NewSDValueDbgMsg(V, "Creating new node: ", this);
9773 return V;
9774}
9775
9778 MachineMemOperand *MMO,
9779 ISD::MemIndexType IndexType,
9780 bool IsTrunc) {
9781 assert(Ops.size() == 6 && "Incompatible number of operands");
9782
9784 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
9785 ID.AddInteger(MemVT.getRawBits());
9786 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
9787 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
9788 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9789 ID.AddInteger(MMO->getFlags());
9790 void *IP = nullptr;
9791 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9792 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
9793 return SDValue(E, 0);
9794 }
9795
9796 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9797 VTs, MemVT, MMO, IndexType, IsTrunc);
9798 createOperands(N, Ops);
9799
9800 assert(N->getMask().getValueType().getVectorElementCount() ==
9801 N->getValue().getValueType().getVectorElementCount() &&
9802 "Vector width mismatch between mask and data");
9803 assert(
9804 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9805 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9806 "Scalable flags of index and data do not match");
9808 N->getIndex().getValueType().getVectorElementCount(),
9809 N->getValue().getValueType().getVectorElementCount()) &&
9810 "Vector width mismatch between index and data");
9811 assert(isa<ConstantSDNode>(N->getScale()) &&
9812 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9813 "Scale should be a constant power of 2");
9814
9815 CSEMap.InsertNode(N, IP);
9816 InsertNode(N);
9817 SDValue V(N, 0);
9818 NewSDValueDbgMsg(V, "Creating new node: ", this);
9819 return V;
9820}
9821
9823 const SDLoc &dl, ArrayRef<SDValue> Ops,
9824 MachineMemOperand *MMO,
9825 ISD::MemIndexType IndexType) {
9826 assert(Ops.size() == 7 && "Incompatible number of operands");
9827
9830 ID.AddInteger(MemVT.getRawBits());
9831 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
9832 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
9833 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9834 ID.AddInteger(MMO->getFlags());
9835 void *IP = nullptr;
9836 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9837 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9838 return SDValue(E, 0);
9839 }
9840
9841 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9842 VTs, MemVT, MMO, IndexType);
9843 createOperands(N, Ops);
9844
9845 assert(N->getMask().getValueType().getVectorElementCount() ==
9846 N->getIndex().getValueType().getVectorElementCount() &&
9847 "Vector width mismatch between mask and data");
9848 assert(isa<ConstantSDNode>(N->getScale()) &&
9849 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9850 "Scale should be a constant power of 2");
9851 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
9852
9853 CSEMap.InsertNode(N, IP);
9854 InsertNode(N);
9855 SDValue V(N, 0);
9856 NewSDValueDbgMsg(V, "Creating new node: ", this);
9857 return V;
9858}
9859
9861 EVT MemVT, MachineMemOperand *MMO) {
9862 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9863 SDVTList VTs = getVTList(MVT::Other);
9864 SDValue Ops[] = {Chain, Ptr};
9867 ID.AddInteger(MemVT.getRawBits());
9868 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9869 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9870 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9871 ID.AddInteger(MMO->getFlags());
9872 void *IP = nullptr;
9873 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9874 return SDValue(E, 0);
9875
9876 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
9877 dl.getDebugLoc(), VTs, MemVT, MMO);
9878 createOperands(N, Ops);
9879
9880 CSEMap.InsertNode(N, IP);
9881 InsertNode(N);
9882 SDValue V(N, 0);
9883 NewSDValueDbgMsg(V, "Creating new node: ", this);
9884 return V;
9885}
9886
9888 EVT MemVT, MachineMemOperand *MMO) {
9889 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9890 SDVTList VTs = getVTList(MVT::Other);
9891 SDValue Ops[] = {Chain, Ptr};
9894 ID.AddInteger(MemVT.getRawBits());
9895 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9896 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9897 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9898 ID.AddInteger(MMO->getFlags());
9899 void *IP = nullptr;
9900 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9901 return SDValue(E, 0);
9902
9903 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
9904 dl.getDebugLoc(), VTs, MemVT, MMO);
9905 createOperands(N, Ops);
9906
9907 CSEMap.InsertNode(N, IP);
9908 InsertNode(N);
9909 SDValue V(N, 0);
9910 NewSDValueDbgMsg(V, "Creating new node: ", this);
9911 return V;
9912}
9913
9915 // select undef, T, F --> T (if T is a constant), otherwise F
9916 // select, ?, undef, F --> F
9917 // select, ?, T, undef --> T
9918 if (Cond.isUndef())
9919 return isConstantValueOfAnyType(T) ? T : F;
9920 if (T.isUndef())
9921 return F;
9922 if (F.isUndef())
9923 return T;
9924
9925 // select true, T, F --> T
9926 // select false, T, F --> F
9927 if (auto C = isBoolConstant(Cond, /*AllowTruncation=*/true))
9928 return *C ? T : F;
9929
9930 // select ?, T, T --> T
9931 if (T == F)
9932 return T;
9933
9934 return SDValue();
9935}
9936
9938 // shift undef, Y --> 0 (can always assume that the undef value is 0)
9939 if (X.isUndef())
9940 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
9941 // shift X, undef --> undef (because it may shift by the bitwidth)
9942 if (Y.isUndef())
9943 return getUNDEF(X.getValueType());
9944
9945 // shift 0, Y --> 0
9946 // shift X, 0 --> X
9948 return X;
9949
9950 // shift X, C >= bitwidth(X) --> undef
9951 // All vector elements must be too big (or undef) to avoid partial undefs.
9952 auto isShiftTooBig = [X](ConstantSDNode *Val) {
9953 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
9954 };
9955 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
9956 return getUNDEF(X.getValueType());
9957
9958 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
9959 if (X.getValueType().getScalarType() == MVT::i1)
9960 return X;
9961
9962 return SDValue();
9963}
9964
9966 SDNodeFlags Flags) {
9967 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
9968 // (an undef operand can be chosen to be Nan/Inf), then the result of this
9969 // operation is poison. That result can be relaxed to undef.
9970 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
9971 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
9972 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
9973 (YC && YC->getValueAPF().isNaN());
9974 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
9975 (YC && YC->getValueAPF().isInfinity());
9976
9977 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
9978 return getUNDEF(X.getValueType());
9979
9980 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
9981 return getUNDEF(X.getValueType());
9982
9983 if (!YC)
9984 return SDValue();
9985
9986 // X + -0.0 --> X
9987 if (Opcode == ISD::FADD)
9988 if (YC->getValueAPF().isNegZero())
9989 return X;
9990
9991 // X - +0.0 --> X
9992 if (Opcode == ISD::FSUB)
9993 if (YC->getValueAPF().isPosZero())
9994 return X;
9995
9996 // X * 1.0 --> X
9997 // X / 1.0 --> X
9998 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
9999 if (YC->getValueAPF().isExactlyValue(1.0))
10000 return X;
10001
10002 // X * 0.0 --> 0.0
10003 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10004 if (YC->getValueAPF().isZero())
10005 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10006
10007 return SDValue();
10008}
10009
10011 SDValue Ptr, SDValue SV, unsigned Align) {
10012 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10013 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10014}
10015
10016SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10017 ArrayRef<SDUse> Ops) {
10018 switch (Ops.size()) {
10019 case 0: return getNode(Opcode, DL, VT);
10020 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
10021 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10022 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10023 default: break;
10024 }
10025
10026 // Copy from an SDUse array into an SDValue array for use with
10027 // the regular getNode logic.
10028 SmallVector<SDValue, 8> NewOps(Ops);
10029 return getNode(Opcode, DL, VT, NewOps);
10030}
10031
10032SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10033 ArrayRef<SDValue> Ops) {
10034 SDNodeFlags Flags;
10035 if (Inserter)
10036 Flags = Inserter->getFlags();
10037 return getNode(Opcode, DL, VT, Ops, Flags);
10038}
10039
10040SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10041 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10042 unsigned NumOps = Ops.size();
10043 switch (NumOps) {
10044 case 0: return getNode(Opcode, DL, VT);
10045 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10046 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10047 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10048 default: break;
10049 }
10050
10051#ifndef NDEBUG
10052 for (const auto &Op : Ops)
10053 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10054 "Operand is DELETED_NODE!");
10055#endif
10056
10057 switch (Opcode) {
10058 default: break;
10059 case ISD::BUILD_VECTOR:
10060 // Attempt to simplify BUILD_VECTOR.
10061 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10062 return V;
10063 break;
10065 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10066 return V;
10067 break;
10068 case ISD::SELECT_CC:
10069 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10070 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10071 "LHS and RHS of condition must have same type!");
10072 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10073 "True and False arms of SelectCC must have same type!");
10074 assert(Ops[2].getValueType() == VT &&
10075 "select_cc node must be of same type as true and false value!");
10076 assert((!Ops[0].getValueType().isVector() ||
10077 Ops[0].getValueType().getVectorElementCount() ==
10078 VT.getVectorElementCount()) &&
10079 "Expected select_cc with vector result to have the same sized "
10080 "comparison type!");
10081 break;
10082 case ISD::BR_CC:
10083 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10084 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10085 "LHS/RHS of comparison should match types!");
10086 break;
10087 case ISD::VP_ADD:
10088 case ISD::VP_SUB:
10089 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10090 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
10091 Opcode = ISD::VP_XOR;
10092 break;
10093 case ISD::VP_MUL:
10094 // If it is VP_MUL mask operation then turn it to VP_AND
10095 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
10096 Opcode = ISD::VP_AND;
10097 break;
10098 case ISD::VP_REDUCE_MUL:
10099 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10100 if (VT == MVT::i1)
10101 Opcode = ISD::VP_REDUCE_AND;
10102 break;
10103 case ISD::VP_REDUCE_ADD:
10104 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10105 if (VT == MVT::i1)
10106 Opcode = ISD::VP_REDUCE_XOR;
10107 break;
10108 case ISD::VP_REDUCE_SMAX:
10109 case ISD::VP_REDUCE_UMIN:
10110 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10111 // VP_REDUCE_AND.
10112 if (VT == MVT::i1)
10113 Opcode = ISD::VP_REDUCE_AND;
10114 break;
10115 case ISD::VP_REDUCE_SMIN:
10116 case ISD::VP_REDUCE_UMAX:
10117 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10118 // VP_REDUCE_OR.
10119 if (VT == MVT::i1)
10120 Opcode = ISD::VP_REDUCE_OR;
10121 break;
10122 }
10123
10124 // Memoize nodes.
10125 SDNode *N;
10126 SDVTList VTs = getVTList(VT);
10127
10128 if (VT != MVT::Glue) {
10130 AddNodeIDNode(ID, Opcode, VTs, Ops);
10131 void *IP = nullptr;
10132
10133 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10134 return SDValue(E, 0);
10135
10136 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10137 createOperands(N, Ops);
10138
10139 CSEMap.InsertNode(N, IP);
10140 } else {
10141 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10142 createOperands(N, Ops);
10143 }
10144
10145 N->setFlags(Flags);
10146 InsertNode(N);
10147 SDValue V(N, 0);
10148 NewSDValueDbgMsg(V, "Creating new node: ", this);
10149 return V;
10150}
10151
10152SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10153 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10154 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
10155}
10156
10157SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10158 ArrayRef<SDValue> Ops) {
10159 SDNodeFlags Flags;
10160 if (Inserter)
10161 Flags = Inserter->getFlags();
10162 return getNode(Opcode, DL, VTList, Ops, Flags);
10163}
10164
10165SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10166 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10167 if (VTList.NumVTs == 1)
10168 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10169
10170#ifndef NDEBUG
10171 for (const auto &Op : Ops)
10172 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10173 "Operand is DELETED_NODE!");
10174#endif
10175
10176 switch (Opcode) {
10177 case ISD::SADDO:
10178 case ISD::UADDO:
10179 case ISD::SSUBO:
10180 case ISD::USUBO: {
10181 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10182 "Invalid add/sub overflow op!");
10183 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10184 Ops[0].getValueType() == Ops[1].getValueType() &&
10185 Ops[0].getValueType() == VTList.VTs[0] &&
10186 "Binary operator types must match!");
10187 SDValue N1 = Ops[0], N2 = Ops[1];
10188 canonicalizeCommutativeBinop(Opcode, N1, N2);
10189
10190 // (X +- 0) -> X with zero-overflow.
10191 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10192 /*AllowTruncation*/ true);
10193 if (N2CV && N2CV->isZero()) {
10194 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10195 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10196 }
10197
10198 if (VTList.VTs[0].isVector() &&
10199 VTList.VTs[0].getVectorElementType() == MVT::i1 &&
10200 VTList.VTs[1].getVectorElementType() == MVT::i1) {
10201 SDValue F1 = getFreeze(N1);
10202 SDValue F2 = getFreeze(N2);
10203 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10204 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10205 return getNode(ISD::MERGE_VALUES, DL, VTList,
10206 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10207 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
10208 Flags);
10209 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
10210 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
10211 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
10212 return getNode(ISD::MERGE_VALUES, DL, VTList,
10213 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10214 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
10215 Flags);
10216 }
10217 }
10218 break;
10219 }
10220 case ISD::SADDO_CARRY:
10221 case ISD::UADDO_CARRY:
10222 case ISD::SSUBO_CARRY:
10223 case ISD::USUBO_CARRY:
10224 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
10225 "Invalid add/sub overflow op!");
10226 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10227 Ops[0].getValueType() == Ops[1].getValueType() &&
10228 Ops[0].getValueType() == VTList.VTs[0] &&
10229 Ops[2].getValueType() == VTList.VTs[1] &&
10230 "Binary operator types must match!");
10231 break;
10232 case ISD::SMUL_LOHI:
10233 case ISD::UMUL_LOHI: {
10234 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
10235 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
10236 VTList.VTs[0] == Ops[0].getValueType() &&
10237 VTList.VTs[0] == Ops[1].getValueType() &&
10238 "Binary operator types must match!");
10239 // Constant fold.
10240 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
10241 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
10242 if (LHS && RHS) {
10243 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
10244 unsigned OutWidth = Width * 2;
10245 APInt Val = LHS->getAPIntValue();
10246 APInt Mul = RHS->getAPIntValue();
10247 if (Opcode == ISD::SMUL_LOHI) {
10248 Val = Val.sext(OutWidth);
10249 Mul = Mul.sext(OutWidth);
10250 } else {
10251 Val = Val.zext(OutWidth);
10252 Mul = Mul.zext(OutWidth);
10253 }
10254 Val *= Mul;
10255
10256 SDValue Hi =
10257 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
10258 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
10259 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
10260 }
10261 break;
10262 }
10263 case ISD::FFREXP: {
10264 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
10265 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
10266 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
10267
10268 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
10269 int FrexpExp;
10270 APFloat FrexpMant =
10271 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
10272 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
10273 SDValue Result1 =
10274 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
10275 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
10276 }
10277
10278 break;
10279 }
10281 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10282 "Invalid STRICT_FP_EXTEND!");
10283 assert(VTList.VTs[0].isFloatingPoint() &&
10284 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
10285 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10286 "STRICT_FP_EXTEND result type should be vector iff the operand "
10287 "type is vector!");
10288 assert((!VTList.VTs[0].isVector() ||
10289 VTList.VTs[0].getVectorElementCount() ==
10290 Ops[1].getValueType().getVectorElementCount()) &&
10291 "Vector element count mismatch!");
10292 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
10293 "Invalid fpext node, dst <= src!");
10294 break;
10296 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
10297 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10298 "STRICT_FP_ROUND result type should be vector iff the operand "
10299 "type is vector!");
10300 assert((!VTList.VTs[0].isVector() ||
10301 VTList.VTs[0].getVectorElementCount() ==
10302 Ops[1].getValueType().getVectorElementCount()) &&
10303 "Vector element count mismatch!");
10304 assert(VTList.VTs[0].isFloatingPoint() &&
10305 Ops[1].getValueType().isFloatingPoint() &&
10306 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
10307 isa<ConstantSDNode>(Ops[2]) &&
10308 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10309 "Invalid STRICT_FP_ROUND!");
10310 break;
10311#if 0
10312 // FIXME: figure out how to safely handle things like
10313 // int foo(int x) { return 1 << (x & 255); }
10314 // int bar() { return foo(256); }
10315 case ISD::SRA_PARTS:
10316 case ISD::SRL_PARTS:
10317 case ISD::SHL_PARTS:
10318 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10319 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10320 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10321 else if (N3.getOpcode() == ISD::AND)
10322 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10323 // If the and is only masking out bits that cannot effect the shift,
10324 // eliminate the and.
10325 unsigned NumBits = VT.getScalarSizeInBits()*2;
10326 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10327 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10328 }
10329 break;
10330#endif
10331 }
10332
10333 // Memoize the node unless it returns a glue result.
10334 SDNode *N;
10335 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10337 AddNodeIDNode(ID, Opcode, VTList, Ops);
10338 void *IP = nullptr;
10339 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10340 return SDValue(E, 0);
10341
10342 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10343 createOperands(N, Ops);
10344 CSEMap.InsertNode(N, IP);
10345 } else {
10346 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10347 createOperands(N, Ops);
10348 }
10349
10350 N->setFlags(Flags);
10351 InsertNode(N);
10352 SDValue V(N, 0);
10353 NewSDValueDbgMsg(V, "Creating new node: ", this);
10354 return V;
10355}
10356
10357SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10358 SDVTList VTList) {
10359 return getNode(Opcode, DL, VTList, std::nullopt);
10360}
10361
10362SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10363 SDValue N1) {
10364 SDValue Ops[] = { N1 };
10365 return getNode(Opcode, DL, VTList, Ops);
10366}
10367
10368SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10369 SDValue N1, SDValue N2) {
10370 SDValue Ops[] = { N1, N2 };
10371 return getNode(Opcode, DL, VTList, Ops);
10372}
10373
10374SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10375 SDValue N1, SDValue N2, SDValue N3) {
10376 SDValue Ops[] = { N1, N2, N3 };
10377 return getNode(Opcode, DL, VTList, Ops);
10378}
10379
10380SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10381 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10382 SDValue Ops[] = { N1, N2, N3, N4 };
10383 return getNode(Opcode, DL, VTList, Ops);
10384}
10385
10386SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10387 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10388 SDValue N5) {
10389 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10390 return getNode(Opcode, DL, VTList, Ops);
10391}
10392
10394 return makeVTList(SDNode::getValueTypeList(VT), 1);
10395}
10396
10399 ID.AddInteger(2U);
10400 ID.AddInteger(VT1.getRawBits());
10401 ID.AddInteger(VT2.getRawBits());
10402
10403 void *IP = nullptr;
10404 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10405 if (!Result) {
10406 EVT *Array = Allocator.Allocate<EVT>(2);
10407 Array[0] = VT1;
10408 Array[1] = VT2;
10409 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10410 VTListMap.InsertNode(Result, IP);
10411 }
10412 return Result->getSDVTList();
10413}
10414
10417 ID.AddInteger(3U);
10418 ID.AddInteger(VT1.getRawBits());
10419 ID.AddInteger(VT2.getRawBits());
10420 ID.AddInteger(VT3.getRawBits());
10421
10422 void *IP = nullptr;
10423 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10424 if (!Result) {
10425 EVT *Array = Allocator.Allocate<EVT>(3);
10426 Array[0] = VT1;
10427 Array[1] = VT2;
10428 Array[2] = VT3;
10429 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10430 VTListMap.InsertNode(Result, IP);
10431 }
10432 return Result->getSDVTList();
10433}
10434
10437 ID.AddInteger(4U);
10438 ID.AddInteger(VT1.getRawBits());
10439 ID.AddInteger(VT2.getRawBits());
10440 ID.AddInteger(VT3.getRawBits());
10441 ID.AddInteger(VT4.getRawBits());
10442
10443 void *IP = nullptr;
10444 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10445 if (!Result) {
10446 EVT *Array = Allocator.Allocate<EVT>(4);
10447 Array[0] = VT1;
10448 Array[1] = VT2;
10449 Array[2] = VT3;
10450 Array[3] = VT4;
10451 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10452 VTListMap.InsertNode(Result, IP);
10453 }
10454 return Result->getSDVTList();
10455}
10456
10458 unsigned NumVTs = VTs.size();
10460 ID.AddInteger(NumVTs);
10461 for (unsigned index = 0; index < NumVTs; index++) {
10462 ID.AddInteger(VTs[index].getRawBits());
10463 }
10464
10465 void *IP = nullptr;
10466 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10467 if (!Result) {
10468 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10469 llvm::copy(VTs, Array);
10470 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10471 VTListMap.InsertNode(Result, IP);
10472 }
10473 return Result->getSDVTList();
10474}
10475
10476
10477/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10478/// specified operands. If the resultant node already exists in the DAG,
10479/// this does not modify the specified node, instead it returns the node that
10480/// already exists. If the resultant node does not exist in the DAG, the
10481/// input node is returned. As a degenerate case, if you specify the same
10482/// input operands as the node already has, the input node is returned.
10484 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10485
10486 // Check to see if there is no change.
10487 if (Op == N->getOperand(0)) return N;
10488
10489 // See if the modified node already exists.
10490 void *InsertPos = nullptr;
10491 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10492 return Existing;
10493
10494 // Nope it doesn't. Remove the node from its current place in the maps.
10495 if (InsertPos)
10496 if (!RemoveNodeFromCSEMaps(N))
10497 InsertPos = nullptr;
10498
10499 // Now we update the operands.
10500 N->OperandList[0].set(Op);
10501
10503 // If this gets put into a CSE map, add it.
10504 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10505 return N;
10506}
10507
10509 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10510
10511 // Check to see if there is no change.
10512 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10513 return N; // No operands changed, just return the input node.
10514
10515 // See if the modified node already exists.
10516 void *InsertPos = nullptr;
10517 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10518 return Existing;
10519
10520 // Nope it doesn't. Remove the node from its current place in the maps.
10521 if (InsertPos)
10522 if (!RemoveNodeFromCSEMaps(N))
10523 InsertPos = nullptr;
10524
10525 // Now we update the operands.
10526 if (N->OperandList[0] != Op1)
10527 N->OperandList[0].set(Op1);
10528 if (N->OperandList[1] != Op2)
10529 N->OperandList[1].set(Op2);
10530
10532 // If this gets put into a CSE map, add it.
10533 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10534 return N;
10535}
10536
10539 SDValue Ops[] = { Op1, Op2, Op3 };
10540 return UpdateNodeOperands(N, Ops);
10541}
10542
10545 SDValue Op3, SDValue Op4) {
10546 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10547 return UpdateNodeOperands(N, Ops);
10548}
10549
10552 SDValue Op3, SDValue Op4, SDValue Op5) {
10553 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10554 return UpdateNodeOperands(N, Ops);
10555}
10556
10559 unsigned NumOps = Ops.size();
10560 assert(N->getNumOperands() == NumOps &&
10561 "Update with wrong number of operands");
10562
10563 // If no operands changed just return the input node.
10564 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10565 return N;
10566
10567 // See if the modified node already exists.
10568 void *InsertPos = nullptr;
10569 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10570 return Existing;
10571
10572 // Nope it doesn't. Remove the node from its current place in the maps.
10573 if (InsertPos)
10574 if (!RemoveNodeFromCSEMaps(N))
10575 InsertPos = nullptr;
10576
10577 // Now we update the operands.
10578 for (unsigned i = 0; i != NumOps; ++i)
10579 if (N->OperandList[i] != Ops[i])
10580 N->OperandList[i].set(Ops[i]);
10581
10583 // If this gets put into a CSE map, add it.
10584 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10585 return N;
10586}
10587
10588/// DropOperands - Release the operands and set this node to have
10589/// zero operands.
10591 // Unlike the code in MorphNodeTo that does this, we don't need to
10592 // watch for dead nodes here.
10593 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10594 SDUse &Use = *I++;
10595 Use.set(SDValue());
10596 }
10597}
10598
10600 ArrayRef<MachineMemOperand *> NewMemRefs) {
10601 if (NewMemRefs.empty()) {
10602 N->clearMemRefs();
10603 return;
10604 }
10605
10606 // Check if we can avoid allocating by storing a single reference directly.
10607 if (NewMemRefs.size() == 1) {
10608 N->MemRefs = NewMemRefs[0];
10609 N->NumMemRefs = 1;
10610 return;
10611 }
10612
10613 MachineMemOperand **MemRefsBuffer =
10614 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10615 llvm::copy(NewMemRefs, MemRefsBuffer);
10616 N->MemRefs = MemRefsBuffer;
10617 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10618}
10619
10620/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10621/// machine opcode.
10622///
10624 EVT VT) {
10625 SDVTList VTs = getVTList(VT);
10626 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10627}
10628
10630 EVT VT, SDValue Op1) {
10631 SDVTList VTs = getVTList(VT);
10632 SDValue Ops[] = { Op1 };
10633 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10634}
10635
10637 EVT VT, SDValue Op1,
10638 SDValue Op2) {
10639 SDVTList VTs = getVTList(VT);
10640 SDValue Ops[] = { Op1, Op2 };
10641 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10642}
10643
10645 EVT VT, SDValue Op1,
10646 SDValue Op2, SDValue Op3) {
10647 SDVTList VTs = getVTList(VT);
10648 SDValue Ops[] = { Op1, Op2, Op3 };
10649 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10650}
10651
10653 EVT VT, ArrayRef<SDValue> Ops) {
10654 SDVTList VTs = getVTList(VT);
10655 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10656}
10657
10659 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10660 SDVTList VTs = getVTList(VT1, VT2);
10661 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10662}
10663
10665 EVT VT1, EVT VT2) {
10666 SDVTList VTs = getVTList(VT1, VT2);
10667 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10668}
10669
10671 EVT VT1, EVT VT2, EVT VT3,
10672 ArrayRef<SDValue> Ops) {
10673 SDVTList VTs = getVTList(VT1, VT2, VT3);
10674 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10675}
10676
10678 EVT VT1, EVT VT2,
10679 SDValue Op1, SDValue Op2) {
10680 SDVTList VTs = getVTList(VT1, VT2);
10681 SDValue Ops[] = { Op1, Op2 };
10682 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10683}
10684
10686 SDVTList VTs,ArrayRef<SDValue> Ops) {
10687 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
10688 // Reset the NodeID to -1.
10689 New->setNodeId(-1);
10690 if (New != N) {
10691 ReplaceAllUsesWith(N, New);
10693 }
10694 return New;
10695}
10696
10697/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
10698/// the line number information on the merged node since it is not possible to
10699/// preserve the information that operation is associated with multiple lines.
10700/// This will make the debugger working better at -O0, were there is a higher
10701/// probability having other instructions associated with that line.
10702///
10703/// For IROrder, we keep the smaller of the two
10704SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
10705 DebugLoc NLoc = N->getDebugLoc();
10706 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
10707 N->setDebugLoc(DebugLoc());
10708 }
10709 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
10710 N->setIROrder(Order);
10711 return N;
10712}
10713
10714/// MorphNodeTo - This *mutates* the specified node to have the specified
10715/// return type, opcode, and operands.
10716///
10717/// Note that MorphNodeTo returns the resultant node. If there is already a
10718/// node of the specified opcode and operands, it returns that node instead of
10719/// the current one. Note that the SDLoc need not be the same.
10720///
10721/// Using MorphNodeTo is faster than creating a new node and swapping it in
10722/// with ReplaceAllUsesWith both because it often avoids allocating a new
10723/// node, and because it doesn't require CSE recalculation for any of
10724/// the node's users.
10725///
10726/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
10727/// As a consequence it isn't appropriate to use from within the DAG combiner or
10728/// the legalizer which maintain worklists that would need to be updated when
10729/// deleting things.
10731 SDVTList VTs, ArrayRef<SDValue> Ops) {
10732 // If an identical node already exists, use it.
10733 void *IP = nullptr;
10734 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
10736 AddNodeIDNode(ID, Opc, VTs, Ops);
10737 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
10738 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
10739 }
10740
10741 if (!RemoveNodeFromCSEMaps(N))
10742 IP = nullptr;
10743
10744 // Start the morphing.
10745 N->NodeType = Opc;
10746 N->ValueList = VTs.VTs;
10747 N->NumValues = VTs.NumVTs;
10748
10749 // Clear the operands list, updating used nodes to remove this from their
10750 // use list. Keep track of any operands that become dead as a result.
10751 SmallPtrSet<SDNode*, 16> DeadNodeSet;
10752 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
10753 SDUse &Use = *I++;
10754 SDNode *Used = Use.getNode();
10755 Use.set(SDValue());
10756 if (Used->use_empty())
10757 DeadNodeSet.insert(Used);
10758 }
10759
10760 // For MachineNode, initialize the memory references information.
10761 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
10762 MN->clearMemRefs();
10763
10764 // Swap for an appropriately sized array from the recycler.
10765 removeOperands(N);
10766 createOperands(N, Ops);
10767
10768 // Delete any nodes that are still dead after adding the uses for the
10769 // new operands.
10770 if (!DeadNodeSet.empty()) {
10771 SmallVector<SDNode *, 16> DeadNodes;
10772 for (SDNode *N : DeadNodeSet)
10773 if (N->use_empty())
10774 DeadNodes.push_back(N);
10775 RemoveDeadNodes(DeadNodes);
10776 }
10777
10778 if (IP)
10779 CSEMap.InsertNode(N, IP); // Memoize the new node.
10780 return N;
10781}
10782
10784 unsigned OrigOpc = Node->getOpcode();
10785 unsigned NewOpc;
10786 switch (OrigOpc) {
10787 default:
10788 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
10789#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10790 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
10791#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10792 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
10793#include "llvm/IR/ConstrainedOps.def"
10794 }
10795
10796 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
10797
10798 // We're taking this node out of the chain, so we need to re-link things.
10799 SDValue InputChain = Node->getOperand(0);
10800 SDValue OutputChain = SDValue(Node, 1);
10801 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
10802
10804 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
10805 Ops.push_back(Node->getOperand(i));
10806
10807 SDVTList VTs = getVTList(Node->getValueType(0));
10808 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
10809
10810 // MorphNodeTo can operate in two ways: if an existing node with the
10811 // specified operands exists, it can just return it. Otherwise, it
10812 // updates the node in place to have the requested operands.
10813 if (Res == Node) {
10814 // If we updated the node in place, reset the node ID. To the isel,
10815 // this should be just like a newly allocated machine node.
10816 Res->setNodeId(-1);
10817 } else {
10818 ReplaceAllUsesWith(Node, Res);
10819 RemoveDeadNode(Node);
10820 }
10821
10822 return Res;
10823}
10824
10825/// getMachineNode - These are used for target selectors to create a new node
10826/// with specified return type(s), MachineInstr opcode, and operands.
10827///
10828/// Note that getMachineNode returns the resultant node. If there is already a
10829/// node of the specified opcode and operands, it returns that node instead of
10830/// the current one.
10832 EVT VT) {
10833 SDVTList VTs = getVTList(VT);
10834 return getMachineNode(Opcode, dl, VTs, std::nullopt);
10835}
10836
10838 EVT VT, SDValue Op1) {
10839 SDVTList VTs = getVTList(VT);
10840 SDValue Ops[] = { Op1 };
10841 return getMachineNode(Opcode, dl, VTs, Ops);
10842}
10843
10845 EVT VT, SDValue Op1, SDValue Op2) {
10846 SDVTList VTs = getVTList(VT);
10847 SDValue Ops[] = { Op1, Op2 };
10848 return getMachineNode(Opcode, dl, VTs, Ops);
10849}
10850
10852 EVT VT, SDValue Op1, SDValue Op2,
10853 SDValue Op3) {
10854 SDVTList VTs = getVTList(VT);
10855 SDValue Ops[] = { Op1, Op2, Op3 };
10856 return getMachineNode(Opcode, dl, VTs, Ops);
10857}
10858
10860 EVT VT, ArrayRef<SDValue> Ops) {
10861 SDVTList VTs = getVTList(VT);
10862 return getMachineNode(Opcode, dl, VTs, Ops);
10863}
10864
10866 EVT VT1, EVT VT2, SDValue Op1,
10867 SDValue Op2) {
10868 SDVTList VTs = getVTList(VT1, VT2);
10869 SDValue Ops[] = { Op1, Op2 };
10870 return getMachineNode(Opcode, dl, VTs, Ops);
10871}
10872
10874 EVT VT1, EVT VT2, SDValue Op1,
10875 SDValue Op2, SDValue Op3) {
10876 SDVTList VTs = getVTList(VT1, VT2);
10877 SDValue Ops[] = { Op1, Op2, Op3 };
10878 return getMachineNode(Opcode, dl, VTs, Ops);
10879}
10880
10882 EVT VT1, EVT VT2,
10883 ArrayRef<SDValue> Ops) {
10884 SDVTList VTs = getVTList(VT1, VT2);
10885 return getMachineNode(Opcode, dl, VTs, Ops);
10886}
10887
10889 EVT VT1, EVT VT2, EVT VT3,
10890 SDValue Op1, SDValue Op2) {
10891 SDVTList VTs = getVTList(VT1, VT2, VT3);
10892 SDValue Ops[] = { Op1, Op2 };
10893 return getMachineNode(Opcode, dl, VTs, Ops);
10894}
10895
10897 EVT VT1, EVT VT2, EVT VT3,
10898 SDValue Op1, SDValue Op2,
10899 SDValue Op3) {
10900 SDVTList VTs = getVTList(VT1, VT2, VT3);
10901 SDValue Ops[] = { Op1, Op2, Op3 };
10902 return getMachineNode(Opcode, dl, VTs, Ops);
10903}
10904
10906 EVT VT1, EVT VT2, EVT VT3,
10907 ArrayRef<SDValue> Ops) {
10908 SDVTList VTs = getVTList(VT1, VT2, VT3);
10909 return getMachineNode(Opcode, dl, VTs, Ops);
10910}
10911
10913 ArrayRef<EVT> ResultTys,
10914 ArrayRef<SDValue> Ops) {
10915 SDVTList VTs = getVTList(ResultTys);
10916 return getMachineNode(Opcode, dl, VTs, Ops);
10917}
10918
10920 SDVTList VTs,
10921 ArrayRef<SDValue> Ops) {
10922 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
10924 void *IP = nullptr;
10925
10926 if (DoCSE) {
10928 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
10929 IP = nullptr;
10930 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10931 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
10932 }
10933 }
10934
10935 // Allocate a new MachineSDNode.
10936 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10937 createOperands(N, Ops);
10938
10939 if (DoCSE)
10940 CSEMap.InsertNode(N, IP);
10941
10942 InsertNode(N);
10943 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
10944 return N;
10945}
10946
10947/// getTargetExtractSubreg - A convenience function for creating
10948/// TargetOpcode::EXTRACT_SUBREG nodes.
10950 SDValue Operand) {
10951 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10952 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
10953 VT, Operand, SRIdxVal);
10954 return SDValue(Subreg, 0);
10955}
10956
10957/// getTargetInsertSubreg - A convenience function for creating
10958/// TargetOpcode::INSERT_SUBREG nodes.
10960 SDValue Operand, SDValue Subreg) {
10961 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10962 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
10963 VT, Operand, Subreg, SRIdxVal);
10964 return SDValue(Result, 0);
10965}
10966
10967/// getNodeIfExists - Get the specified node if it's already available, or
10968/// else return NULL.
10970 ArrayRef<SDValue> Ops) {
10971 SDNodeFlags Flags;
10972 if (Inserter)
10973 Flags = Inserter->getFlags();
10974 return getNodeIfExists(Opcode, VTList, Ops, Flags);
10975}
10976
10979 const SDNodeFlags Flags) {
10980 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10982 AddNodeIDNode(ID, Opcode, VTList, Ops);
10983 void *IP = nullptr;
10984 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
10985 E->intersectFlagsWith(Flags);
10986 return E;
10987 }
10988 }
10989 return nullptr;
10990}
10991
10992/// doesNodeExist - Check if a node exists without modifying its flags.
10993bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
10994 ArrayRef<SDValue> Ops) {
10995 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10997 AddNodeIDNode(ID, Opcode, VTList, Ops);
10998 void *IP = nullptr;
10999 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11000 return true;
11001 }
11002 return false;
11003}
11004
11005/// getDbgValue - Creates a SDDbgValue node.
11006///
11007/// SDNode
11009 SDNode *N, unsigned R, bool IsIndirect,
11010 const DebugLoc &DL, unsigned O) {
11011 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11012 "Expected inlined-at fields to agree");
11013 return new (DbgInfo->getAlloc())
11014 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11015 {}, IsIndirect, DL, O,
11016 /*IsVariadic=*/false);
11017}
11018
11019/// Constant
11021 DIExpression *Expr,
11022 const Value *C,
11023 const DebugLoc &DL, unsigned O) {
11024 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11025 "Expected inlined-at fields to agree");
11026 return new (DbgInfo->getAlloc())
11027 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11028 /*IsIndirect=*/false, DL, O,
11029 /*IsVariadic=*/false);
11030}
11031
11032/// FrameIndex
11034 DIExpression *Expr, unsigned FI,
11035 bool IsIndirect,
11036 const DebugLoc &DL,
11037 unsigned O) {
11038 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11039 "Expected inlined-at fields to agree");
11040 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11041}
11042
11043/// FrameIndex with dependencies
11045 DIExpression *Expr, unsigned FI,
11046 ArrayRef<SDNode *> Dependencies,
11047 bool IsIndirect,
11048 const DebugLoc &DL,
11049 unsigned O) {
11050 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11051 "Expected inlined-at fields to agree");
11052 return new (DbgInfo->getAlloc())
11053 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11054 Dependencies, IsIndirect, DL, O,
11055 /*IsVariadic=*/false);
11056}
11057
11058/// VReg
11060 unsigned VReg, bool IsIndirect,
11061 const DebugLoc &DL, unsigned O) {
11062 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11063 "Expected inlined-at fields to agree");
11064 return new (DbgInfo->getAlloc())
11065 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11066 {}, IsIndirect, DL, O,
11067 /*IsVariadic=*/false);
11068}
11069
11072 ArrayRef<SDNode *> Dependencies,
11073 bool IsIndirect, const DebugLoc &DL,
11074 unsigned O, bool IsVariadic) {
11075 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11076 "Expected inlined-at fields to agree");
11077 return new (DbgInfo->getAlloc())
11078 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11079 DL, O, IsVariadic);
11080}
11081
11083 unsigned OffsetInBits, unsigned SizeInBits,
11084 bool InvalidateDbg) {
11085 SDNode *FromNode = From.getNode();
11086 SDNode *ToNode = To.getNode();
11087 assert(FromNode && ToNode && "Can't modify dbg values");
11088
11089 // PR35338
11090 // TODO: assert(From != To && "Redundant dbg value transfer");
11091 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11092 if (From == To || FromNode == ToNode)
11093 return;
11094
11095 if (!FromNode->getHasDebugValue())
11096 return;
11097
11098 SDDbgOperand FromLocOp =
11099 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11101
11103 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11104 if (Dbg->isInvalidated())
11105 continue;
11106
11107 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11108
11109 // Create a new location ops vector that is equal to the old vector, but
11110 // with each instance of FromLocOp replaced with ToLocOp.
11111 bool Changed = false;
11112 auto NewLocOps = Dbg->copyLocationOps();
11113 std::replace_if(
11114 NewLocOps.begin(), NewLocOps.end(),
11115 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11116 bool Match = Op == FromLocOp;
11117 Changed |= Match;
11118 return Match;
11119 },
11120 ToLocOp);
11121 // Ignore this SDDbgValue if we didn't find a matching location.
11122 if (!Changed)
11123 continue;
11124
11125 DIVariable *Var = Dbg->getVariable();
11126 auto *Expr = Dbg->getExpression();
11127 // If a fragment is requested, update the expression.
11128 if (SizeInBits) {
11129 // When splitting a larger (e.g., sign-extended) value whose
11130 // lower bits are described with an SDDbgValue, do not attempt
11131 // to transfer the SDDbgValue to the upper bits.
11132 if (auto FI = Expr->getFragmentInfo())
11133 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11134 continue;
11135 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11136 SizeInBits);
11137 if (!Fragment)
11138 continue;
11139 Expr = *Fragment;
11140 }
11141
11142 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11143 // Clone the SDDbgValue and move it to To.
11144 SDDbgValue *Clone = getDbgValueList(
11145 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11146 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11147 Dbg->isVariadic());
11148 ClonedDVs.push_back(Clone);
11149
11150 if (InvalidateDbg) {
11151 // Invalidate value and indicate the SDDbgValue should not be emitted.
11152 Dbg->setIsInvalidated();
11153 Dbg->setIsEmitted();
11154 }
11155 }
11156
11157 for (SDDbgValue *Dbg : ClonedDVs) {
11158 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11159 "Transferred DbgValues should depend on the new SDNode");
11160 AddDbgValue(Dbg, false);
11161 }
11162}
11163
11165 if (!N.getHasDebugValue())
11166 return;
11167
11169 for (auto *DV : GetDbgValues(&N)) {
11170 if (DV->isInvalidated())
11171 continue;
11172 switch (N.getOpcode()) {
11173 default:
11174 break;
11175 case ISD::ADD: {
11176 SDValue N0 = N.getOperand(0);
11177 SDValue N1 = N.getOperand(1);
11178 if (!isa<ConstantSDNode>(N0)) {
11179 bool RHSConstant = isa<ConstantSDNode>(N1);
11181 if (RHSConstant)
11182 Offset = N.getConstantOperandVal(1);
11183 // We are not allowed to turn indirect debug values variadic, so
11184 // don't salvage those.
11185 if (!RHSConstant && DV->isIndirect())
11186 continue;
11187
11188 // Rewrite an ADD constant node into a DIExpression. Since we are
11189 // performing arithmetic to compute the variable's *value* in the
11190 // DIExpression, we need to mark the expression with a
11191 // DW_OP_stack_value.
11192 auto *DIExpr = DV->getExpression();
11193 auto NewLocOps = DV->copyLocationOps();
11194 bool Changed = false;
11195 size_t OrigLocOpsSize = NewLocOps.size();
11196 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11197 // We're not given a ResNo to compare against because the whole
11198 // node is going away. We know that any ISD::ADD only has one
11199 // result, so we can assume any node match is using the result.
11200 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11201 NewLocOps[i].getSDNode() != &N)
11202 continue;
11203 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11204 if (RHSConstant) {
11207 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
11208 } else {
11209 // Convert to a variadic expression (if not already).
11210 // convertToVariadicExpression() returns a const pointer, so we use
11211 // a temporary const variable here.
11212 const auto *TmpDIExpr =
11216 ExprOps.push_back(NewLocOps.size());
11217 ExprOps.push_back(dwarf::DW_OP_plus);
11220 NewLocOps.push_back(RHS);
11221 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
11222 }
11223 Changed = true;
11224 }
11225 (void)Changed;
11226 assert(Changed && "Salvage target doesn't use N");
11227
11228 bool IsVariadic =
11229 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
11230
11231 auto AdditionalDependencies = DV->getAdditionalDependencies();
11232 SDDbgValue *Clone = getDbgValueList(
11233 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
11234 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
11235 ClonedDVs.push_back(Clone);
11236 DV->setIsInvalidated();
11237 DV->setIsEmitted();
11238 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
11239 N0.getNode()->dumprFull(this);
11240 dbgs() << " into " << *DIExpr << '\n');
11241 }
11242 break;
11243 }
11244 case ISD::TRUNCATE: {
11245 SDValue N0 = N.getOperand(0);
11246 TypeSize FromSize = N0.getValueSizeInBits();
11247 TypeSize ToSize = N.getValueSizeInBits(0);
11248
11249 DIExpression *DbgExpression = DV->getExpression();
11250 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
11251 auto NewLocOps = DV->copyLocationOps();
11252 bool Changed = false;
11253 for (size_t i = 0; i < NewLocOps.size(); ++i) {
11254 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11255 NewLocOps[i].getSDNode() != &N)
11256 continue;
11257
11258 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11259 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
11260 Changed = true;
11261 }
11262 assert(Changed && "Salvage target doesn't use N");
11263 (void)Changed;
11264
11265 SDDbgValue *Clone =
11266 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
11267 DV->getAdditionalDependencies(), DV->isIndirect(),
11268 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
11269
11270 ClonedDVs.push_back(Clone);
11271 DV->setIsInvalidated();
11272 DV->setIsEmitted();
11273 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
11274 dbgs() << " into " << *DbgExpression << '\n');
11275 break;
11276 }
11277 }
11278 }
11279
11280 for (SDDbgValue *Dbg : ClonedDVs) {
11281 assert(!Dbg->getSDNodes().empty() &&
11282 "Salvaged DbgValue should depend on a new SDNode");
11283 AddDbgValue(Dbg, false);
11284 }
11285}
11286
11287/// Creates a SDDbgLabel node.
11289 const DebugLoc &DL, unsigned O) {
11290 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
11291 "Expected inlined-at fields to agree");
11292 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
11293}
11294
11295namespace {
11296
11297/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
11298/// pointed to by a use iterator is deleted, increment the use iterator
11299/// so that it doesn't dangle.
11300///
11301class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
11304
11305 void NodeDeleted(SDNode *N, SDNode *E) override {
11306 // Increment the iterator as needed.
11307 while (UI != UE && N == *UI)
11308 ++UI;
11309 }
11310
11311public:
11312 RAUWUpdateListener(SelectionDAG &d,
11315 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11316};
11317
11318} // end anonymous namespace
11319
11320/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11321/// This can cause recursive merging of nodes in the DAG.
11322///
11323/// This version assumes From has a single result value.
11324///
11326 SDNode *From = FromN.getNode();
11327 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11328 "Cannot replace with this method!");
11329 assert(From != To.getNode() && "Cannot replace uses of with self");
11330
11331 // Preserve Debug Values
11332 transferDbgValues(FromN, To);
11333 // Preserve extra info.
11334 copyExtraInfo(From, To.getNode());
11335
11336 // Iterate over all the existing uses of From. New uses will be added
11337 // to the beginning of the use list, which we avoid visiting.
11338 // This specifically avoids visiting uses of From that arise while the
11339 // replacement is happening, because any such uses would be the result
11340 // of CSE: If an existing node looks like From after one of its operands
11341 // is replaced by To, we don't want to replace of all its users with To
11342 // too. See PR3018 for more info.
11343 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11344 RAUWUpdateListener Listener(*this, UI, UE);
11345 while (UI != UE) {
11346 SDNode *User = *UI;
11347
11348 // This node is about to morph, remove its old self from the CSE maps.
11349 RemoveNodeFromCSEMaps(User);
11350
11351 // A user can appear in a use list multiple times, and when this
11352 // happens the uses are usually next to each other in the list.
11353 // To help reduce the number of CSE recomputations, process all
11354 // the uses of this user that we can find this way.
11355 do {
11356 SDUse &Use = UI.getUse();
11357 ++UI;
11358 Use.set(To);
11359 if (To->isDivergent() != From->isDivergent())
11361 } while (UI != UE && *UI == User);
11362 // Now that we have modified User, add it back to the CSE maps. If it
11363 // already exists there, recursively merge the results together.
11364 AddModifiedNodeToCSEMaps(User);
11365 }
11366
11367 // If we just RAUW'd the root, take note.
11368 if (FromN == getRoot())
11369 setRoot(To);
11370}
11371
11372/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11373/// This can cause recursive merging of nodes in the DAG.
11374///
11375/// This version assumes that for each value of From, there is a
11376/// corresponding value in To in the same position with the same type.
11377///
11379#ifndef NDEBUG
11380 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11381 assert((!From->hasAnyUseOfValue(i) ||
11382 From->getValueType(i) == To->getValueType(i)) &&
11383 "Cannot use this version of ReplaceAllUsesWith!");
11384#endif
11385
11386 // Handle the trivial case.
11387 if (From == To)
11388 return;
11389
11390 // Preserve Debug Info. Only do this if there's a use.
11391 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11392 if (From->hasAnyUseOfValue(i)) {
11393 assert((i < To->getNumValues()) && "Invalid To location");
11395 }
11396 // Preserve extra info.
11397 copyExtraInfo(From, To);
11398
11399 // Iterate over just the existing users of From. See the comments in
11400 // the ReplaceAllUsesWith above.
11401 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11402 RAUWUpdateListener Listener(*this, UI, UE);
11403 while (UI != UE) {
11404 SDNode *User = *UI;
11405
11406 // This node is about to morph, remove its old self from the CSE maps.
11407 RemoveNodeFromCSEMaps(User);
11408
11409 // A user can appear in a use list multiple times, and when this
11410 // happens the uses are usually next to each other in the list.
11411 // To help reduce the number of CSE recomputations, process all
11412 // the uses of this user that we can find this way.
11413 do {
11414 SDUse &Use = UI.getUse();
11415 ++UI;
11416 Use.setNode(To);
11417 if (To->isDivergent() != From->isDivergent())
11419 } while (UI != UE && *UI == User);
11420
11421 // Now that we have modified User, add it back to the CSE maps. If it
11422 // already exists there, recursively merge the results together.
11423 AddModifiedNodeToCSEMaps(User);
11424 }
11425
11426 // If we just RAUW'd the root, take note.
11427 if (From == getRoot().getNode())
11428 setRoot(SDValue(To, getRoot().getResNo()));
11429}
11430
11431/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11432/// This can cause recursive merging of nodes in the DAG.
11433///
11434/// This version can replace From with any result values. To must match the
11435/// number and types of values returned by From.
11437 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11438 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11439
11440 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11441 // Preserve Debug Info.
11442 transferDbgValues(SDValue(From, i), To[i]);
11443 // Preserve extra info.
11444 copyExtraInfo(From, To[i].getNode());
11445 }
11446
11447 // Iterate over just the existing users of From. See the comments in
11448 // the ReplaceAllUsesWith above.
11449 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11450 RAUWUpdateListener Listener(*this, UI, UE);
11451 while (UI != UE) {
11452 SDNode *User = *UI;
11453
11454 // This node is about to morph, remove its old self from the CSE maps.
11455 RemoveNodeFromCSEMaps(User);
11456
11457 // A user can appear in a use list multiple times, and when this happens the
11458 // uses are usually next to each other in the list. To help reduce the
11459 // number of CSE and divergence recomputations, process all the uses of this
11460 // user that we can find this way.
11461 bool To_IsDivergent = false;
11462 do {
11463 SDUse &Use = UI.getUse();
11464 const SDValue &ToOp = To[Use.getResNo()];
11465 ++UI;
11466 Use.set(ToOp);
11467 To_IsDivergent |= ToOp->isDivergent();
11468 } while (UI != UE && *UI == User);
11469
11470 if (To_IsDivergent != From->isDivergent())
11472
11473 // Now that we have modified User, add it back to the CSE maps. If it
11474 // already exists there, recursively merge the results together.
11475 AddModifiedNodeToCSEMaps(User);
11476 }
11477
11478 // If we just RAUW'd the root, take note.
11479 if (From == getRoot().getNode())
11480 setRoot(SDValue(To[getRoot().getResNo()]));
11481}
11482
11483/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11484/// uses of other values produced by From.getNode() alone. The Deleted
11485/// vector is handled the same way as for ReplaceAllUsesWith.
11487 // Handle the really simple, really trivial case efficiently.
11488 if (From == To) return;
11489
11490 // Handle the simple, trivial, case efficiently.
11491 if (From.getNode()->getNumValues() == 1) {
11493 return;
11494 }
11495
11496 // Preserve Debug Info.
11498 copyExtraInfo(From.getNode(), To.getNode());
11499
11500 // Iterate over just the existing users of From. See the comments in
11501 // the ReplaceAllUsesWith above.
11502 SDNode::use_iterator UI = From.getNode()->use_begin(),
11503 UE = From.getNode()->use_end();
11504 RAUWUpdateListener Listener(*this, UI, UE);
11505 while (UI != UE) {
11506 SDNode *User = *UI;
11507 bool UserRemovedFromCSEMaps = false;
11508
11509 // A user can appear in a use list multiple times, and when this
11510 // happens the uses are usually next to each other in the list.
11511 // To help reduce the number of CSE recomputations, process all
11512 // the uses of this user that we can find this way.
11513 do {
11514 SDUse &Use = UI.getUse();
11515
11516 // Skip uses of different values from the same node.
11517 if (Use.getResNo() != From.getResNo()) {
11518 ++UI;
11519 continue;
11520 }
11521
11522 // If this node hasn't been modified yet, it's still in the CSE maps,
11523 // so remove its old self from the CSE maps.
11524 if (!UserRemovedFromCSEMaps) {
11525 RemoveNodeFromCSEMaps(User);
11526 UserRemovedFromCSEMaps = true;
11527 }
11528
11529 ++UI;
11530 Use.set(To);
11531 if (To->isDivergent() != From->isDivergent())
11533 } while (UI != UE && *UI == User);
11534 // We are iterating over all uses of the From node, so if a use
11535 // doesn't use the specific value, no changes are made.
11536 if (!UserRemovedFromCSEMaps)
11537 continue;
11538
11539 // Now that we have modified User, add it back to the CSE maps. If it
11540 // already exists there, recursively merge the results together.
11541 AddModifiedNodeToCSEMaps(User);
11542 }
11543
11544 // If we just RAUW'd the root, take note.
11545 if (From == getRoot())
11546 setRoot(To);
11547}
11548
11549namespace {
11550
11551/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11552/// to record information about a use.
11553struct UseMemo {
11554 SDNode *User;
11555 unsigned Index;
11556 SDUse *Use;
11557};
11558
11559/// operator< - Sort Memos by User.
11560bool operator<(const UseMemo &L, const UseMemo &R) {
11561 return (intptr_t)L.User < (intptr_t)R.User;
11562}
11563
11564/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11565/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11566/// the node already has been taken care of recursively.
11567class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11569
11570 void NodeDeleted(SDNode *N, SDNode *E) override {
11571 for (UseMemo &Memo : Uses)
11572 if (Memo.User == N)
11573 Memo.User = nullptr;
11574 }
11575
11576public:
11577 RAUOVWUpdateListener(SelectionDAG &d, SmallVector<UseMemo, 4> &uses)
11578 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11579};
11580
11581} // end anonymous namespace
11582
11583/// Return true if a glue output should propagate divergence information.
11585 switch (Node->getOpcode()) {
11586 case ISD::CopyFromReg:
11587 case ISD::CopyToReg:
11588 return false;
11589 default:
11590 return true;
11591 }
11592
11593 llvm_unreachable("covered opcode switch");
11594}
11595
11597 if (TLI->isSDNodeAlwaysUniform(N)) {
11598 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11599 "Conflicting divergence information!");
11600 return false;
11601 }
11602 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11603 return true;
11604 for (const auto &Op : N->ops()) {
11605 EVT VT = Op.getValueType();
11606
11607 // Skip Chain. It does not carry divergence.
11608 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
11609 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
11610 return true;
11611 }
11612 return false;
11613}
11614
11616 SmallVector<SDNode *, 16> Worklist(1, N);
11617 do {
11618 N = Worklist.pop_back_val();
11619 bool IsDivergent = calculateDivergence(N);
11620 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11621 N->SDNodeBits.IsDivergent = IsDivergent;
11622 llvm::append_range(Worklist, N->uses());
11623 }
11624 } while (!Worklist.empty());
11625}
11626
11627void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11629 Order.reserve(AllNodes.size());
11630 for (auto &N : allnodes()) {
11631 unsigned NOps = N.getNumOperands();
11632 Degree[&N] = NOps;
11633 if (0 == NOps)
11634 Order.push_back(&N);
11635 }
11636 for (size_t I = 0; I != Order.size(); ++I) {
11637 SDNode *N = Order[I];
11638 for (auto *U : N->uses()) {
11639 unsigned &UnsortedOps = Degree[U];
11640 if (0 == --UnsortedOps)
11641 Order.push_back(U);
11642 }
11643 }
11644}
11645
11646#ifndef NDEBUG
11648 std::vector<SDNode *> TopoOrder;
11649 CreateTopologicalOrder(TopoOrder);
11650 for (auto *N : TopoOrder) {
11651 assert(calculateDivergence(N) == N->isDivergent() &&
11652 "Divergence bit inconsistency detected");
11653 }
11654}
11655#endif
11656
11657/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11658/// uses of other values produced by From.getNode() alone. The same value
11659/// may appear in both the From and To list. The Deleted vector is
11660/// handled the same way as for ReplaceAllUsesWith.
11662 const SDValue *To,
11663 unsigned Num){
11664 // Handle the simple, trivial case efficiently.
11665 if (Num == 1)
11666 return ReplaceAllUsesOfValueWith(*From, *To);
11667
11668 transferDbgValues(*From, *To);
11669 copyExtraInfo(From->getNode(), To->getNode());
11670
11671 // Read up all the uses and make records of them. This helps
11672 // processing new uses that are introduced during the
11673 // replacement process.
11675 for (unsigned i = 0; i != Num; ++i) {
11676 unsigned FromResNo = From[i].getResNo();
11677 SDNode *FromNode = From[i].getNode();
11678 for (SDNode::use_iterator UI = FromNode->use_begin(),
11679 E = FromNode->use_end(); UI != E; ++UI) {
11680 SDUse &Use = UI.getUse();
11681 if (Use.getResNo() == FromResNo) {
11682 UseMemo Memo = { *UI, i, &Use };
11683 Uses.push_back(Memo);
11684 }
11685 }
11686 }
11687
11688 // Sort the uses, so that all the uses from a given User are together.
11690 RAUOVWUpdateListener Listener(*this, Uses);
11691
11692 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
11693 UseIndex != UseIndexEnd; ) {
11694 // We know that this user uses some value of From. If it is the right
11695 // value, update it.
11696 SDNode *User = Uses[UseIndex].User;
11697 // If the node has been deleted by recursive CSE updates when updating
11698 // another node, then just skip this entry.
11699 if (User == nullptr) {
11700 ++UseIndex;
11701 continue;
11702 }
11703
11704 // This node is about to morph, remove its old self from the CSE maps.
11705 RemoveNodeFromCSEMaps(User);
11706
11707 // The Uses array is sorted, so all the uses for a given User
11708 // are next to each other in the list.
11709 // To help reduce the number of CSE recomputations, process all
11710 // the uses of this user that we can find this way.
11711 do {
11712 unsigned i = Uses[UseIndex].Index;
11713 SDUse &Use = *Uses[UseIndex].Use;
11714 ++UseIndex;
11715
11716 Use.set(To[i]);
11717 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
11718
11719 // Now that we have modified User, add it back to the CSE maps. If it
11720 // already exists there, recursively merge the results together.
11721 AddModifiedNodeToCSEMaps(User);
11722 }
11723}
11724
11725/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
11726/// based on their topological order. It returns the maximum id and a vector
11727/// of the SDNodes* in assigned order by reference.
11729 unsigned DAGSize = 0;
11730
11731 // SortedPos tracks the progress of the algorithm. Nodes before it are
11732 // sorted, nodes after it are unsorted. When the algorithm completes
11733 // it is at the end of the list.
11734 allnodes_iterator SortedPos = allnodes_begin();
11735
11736 // Visit all the nodes. Move nodes with no operands to the front of
11737 // the list immediately. Annotate nodes that do have operands with their
11738 // operand count. Before we do this, the Node Id fields of the nodes
11739 // may contain arbitrary values. After, the Node Id fields for nodes
11740 // before SortedPos will contain the topological sort index, and the
11741 // Node Id fields for nodes At SortedPos and after will contain the
11742 // count of outstanding operands.
11744 checkForCycles(&N, this);
11745 unsigned Degree = N.getNumOperands();
11746 if (Degree == 0) {
11747 // A node with no uses, add it to the result array immediately.
11748 N.setNodeId(DAGSize++);
11749 allnodes_iterator Q(&N);
11750 if (Q != SortedPos)
11751 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
11752 assert(SortedPos != AllNodes.end() && "Overran node list");
11753 ++SortedPos;
11754 } else {
11755 // Temporarily use the Node Id as scratch space for the degree count.
11756 N.setNodeId(Degree);
11757 }
11758 }
11759
11760 // Visit all the nodes. As we iterate, move nodes into sorted order,
11761 // such that by the time the end is reached all nodes will be sorted.
11762 for (SDNode &Node : allnodes()) {
11763 SDNode *N = &Node;
11764 checkForCycles(N, this);
11765 // N is in sorted position, so all its uses have one less operand
11766 // that needs to be sorted.
11767 for (SDNode *P : N->uses()) {
11768 unsigned Degree = P->getNodeId();
11769 assert(Degree != 0 && "Invalid node degree");
11770 --Degree;
11771 if (Degree == 0) {
11772 // All of P's operands are sorted, so P may sorted now.
11773 P->setNodeId(DAGSize++);
11774 if (P->getIterator() != SortedPos)
11775 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
11776 assert(SortedPos != AllNodes.end() && "Overran node list");
11777 ++SortedPos;
11778 } else {
11779 // Update P's outstanding operand count.
11780 P->setNodeId(Degree);
11781 }
11782 }
11783 if (Node.getIterator() == SortedPos) {
11784#ifndef NDEBUG
11786 SDNode *S = &*++I;
11787 dbgs() << "Overran sorted position:\n";
11788 S->dumprFull(this); dbgs() << "\n";
11789 dbgs() << "Checking if this is due to cycles\n";
11790 checkForCycles(this, true);
11791#endif
11792 llvm_unreachable(nullptr);
11793 }
11794 }
11795
11796 assert(SortedPos == AllNodes.end() &&
11797 "Topological sort incomplete!");
11798 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
11799 "First node in topological sort is not the entry token!");
11800 assert(AllNodes.front().getNodeId() == 0 &&
11801 "First node in topological sort has non-zero id!");
11802 assert(AllNodes.front().getNumOperands() == 0 &&
11803 "First node in topological sort has operands!");
11804 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
11805 "Last node in topologic sort has unexpected id!");
11806 assert(AllNodes.back().use_empty() &&
11807 "Last node in topologic sort has users!");
11808 assert(DAGSize == allnodes_size() && "Node count mismatch!");
11809 return DAGSize;
11810}
11811
11812/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
11813/// value is produced by SD.
11814void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
11815 for (SDNode *SD : DB->getSDNodes()) {
11816 if (!SD)
11817 continue;
11818 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
11819 SD->setHasDebugValue(true);
11820 }
11821 DbgInfo->add(DB, isParameter);
11822}
11823
11824void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
11825
11827 SDValue NewMemOpChain) {
11828 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
11829 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
11830 // The new memory operation must have the same position as the old load in
11831 // terms of memory dependency. Create a TokenFactor for the old load and new
11832 // memory operation and update uses of the old load's output chain to use that
11833 // TokenFactor.
11834 if (OldChain == NewMemOpChain || OldChain.use_empty())
11835 return NewMemOpChain;
11836
11837 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
11838 OldChain, NewMemOpChain);
11839 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
11840 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
11841 return TokenFactor;
11842}
11843
11845 SDValue NewMemOp) {
11846 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
11847 SDValue OldChain = SDValue(OldLoad, 1);
11848 SDValue NewMemOpChain = NewMemOp.getValue(1);
11849 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
11850}
11851
11853 Function **OutFunction) {
11854 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
11855
11856 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
11857 auto *Module = MF->getFunction().getParent();
11858 auto *Function = Module->getFunction(Symbol);
11859
11860 if (OutFunction != nullptr)
11861 *OutFunction = Function;
11862
11863 if (Function != nullptr) {
11864 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
11865 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
11866 }
11867
11868 std::string ErrorStr;
11869 raw_string_ostream ErrorFormatter(ErrorStr);
11870 ErrorFormatter << "Undefined external symbol ";
11871 ErrorFormatter << '"' << Symbol << '"';
11872 report_fatal_error(Twine(ErrorStr));
11873}
11874
11875//===----------------------------------------------------------------------===//
11876// SDNode Class
11877//===----------------------------------------------------------------------===//
11878
11880 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11881 return Const != nullptr && Const->isZero();
11882}
11883
11885 return V.isUndef() || isNullConstant(V);
11886}
11887
11889 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
11890 return Const != nullptr && Const->isZero() && !Const->isNegative();
11891}
11892
11894 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11895 return Const != nullptr && Const->isAllOnes();
11896}
11897
11899 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11900 return Const != nullptr && Const->isOne();
11901}
11902
11904 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11905 return Const != nullptr && Const->isMinSignedValue();
11906}
11907
11908bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
11909 unsigned OperandNo) {
11910 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
11911 // TODO: Target-specific opcodes could be added.
11912 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
11913 /*AllowTruncation*/ true)) {
11914 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
11915 switch (Opcode) {
11916 case ISD::ADD:
11917 case ISD::OR:
11918 case ISD::XOR:
11919 case ISD::UMAX:
11920 return Const.isZero();
11921 case ISD::MUL:
11922 return Const.isOne();
11923 case ISD::AND:
11924 case ISD::UMIN:
11925 return Const.isAllOnes();
11926 case ISD::SMAX:
11927 return Const.isMinSignedValue();
11928 case ISD::SMIN:
11929 return Const.isMaxSignedValue();
11930 case ISD::SUB:
11931 case ISD::SHL:
11932 case ISD::SRA:
11933 case ISD::SRL:
11934 return OperandNo == 1 && Const.isZero();
11935 case ISD::UDIV:
11936 case ISD::SDIV:
11937 return OperandNo == 1 && Const.isOne();
11938 }
11939 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
11940 switch (Opcode) {
11941 case ISD::FADD:
11942 return ConstFP->isZero() &&
11943 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
11944 case ISD::FSUB:
11945 return OperandNo == 1 && ConstFP->isZero() &&
11946 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
11947 case ISD::FMUL:
11948 return ConstFP->isExactlyValue(1.0);
11949 case ISD::FDIV:
11950 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
11951 case ISD::FMINNUM:
11952 case ISD::FMAXNUM: {
11953 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
11954 EVT VT = V.getValueType();
11956 APFloat NeutralAF = !Flags.hasNoNaNs()
11957 ? APFloat::getQNaN(Semantics)
11958 : !Flags.hasNoInfs()
11959 ? APFloat::getInf(Semantics)
11960 : APFloat::getLargest(Semantics);
11961 if (Opcode == ISD::FMAXNUM)
11962 NeutralAF.changeSign();
11963
11964 return ConstFP->isExactlyValue(NeutralAF);
11965 }
11966 }
11967 }
11968 return false;
11969}
11970
11972 while (V.getOpcode() == ISD::BITCAST)
11973 V = V.getOperand(0);
11974 return V;
11975}
11976
11978 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
11979 V = V.getOperand(0);
11980 return V;
11981}
11982
11984 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11985 V = V.getOperand(0);
11986 return V;
11987}
11988
11990 while (V.getOpcode() == ISD::TRUNCATE)
11991 V = V.getOperand(0);
11992 return V;
11993}
11994
11995bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
11996 if (V.getOpcode() != ISD::XOR)
11997 return false;
11998 V = peekThroughBitcasts(V.getOperand(1));
11999 unsigned NumBits = V.getScalarValueSizeInBits();
12000 ConstantSDNode *C =
12001 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12002 return C && (C->getAPIntValue().countr_one() >= NumBits);
12003}
12004
12006 bool AllowTruncation) {
12007 EVT VT = N.getValueType();
12008 APInt DemandedElts = VT.isFixedLengthVector()
12010 : APInt(1, 1);
12011 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12012}
12013
12015 bool AllowUndefs,
12016 bool AllowTruncation) {
12017 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
12018 return CN;
12019
12020 // SplatVectors can truncate their operands. Ignore that case here unless
12021 // AllowTruncation is set.
12022 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12023 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12024 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12025 EVT CVT = CN->getValueType(0);
12026 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12027 if (AllowTruncation || CVT == VecEltVT)
12028 return CN;
12029 }
12030 }
12031
12032 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12033 BitVector UndefElements;
12034 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12035
12036 // BuildVectors can truncate their operands. Ignore that case here unless
12037 // AllowTruncation is set.
12038 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12039 if (CN && (UndefElements.none() || AllowUndefs)) {
12040 EVT CVT = CN->getValueType(0);
12041 EVT NSVT = N.getValueType().getScalarType();
12042 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12043 if (AllowTruncation || (CVT == NSVT))
12044 return CN;
12045 }
12046 }
12047
12048 return nullptr;
12049}
12050
12052 EVT VT = N.getValueType();
12053 APInt DemandedElts = VT.isFixedLengthVector()
12055 : APInt(1, 1);
12056 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12057}
12058
12060 const APInt &DemandedElts,
12061 bool AllowUndefs) {
12062 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
12063 return CN;
12064
12065 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12066 BitVector UndefElements;
12067 ConstantFPSDNode *CN =
12068 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12069 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12070 if (CN && (UndefElements.none() || AllowUndefs))
12071 return CN;
12072 }
12073
12074 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12075 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12076 return CN;
12077
12078 return nullptr;
12079}
12080
12081bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12082 // TODO: may want to use peekThroughBitcast() here.
12083 ConstantSDNode *C =
12084 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12085 return C && C->isZero();
12086}
12087
12088bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12089 ConstantSDNode *C =
12090 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12091 return C && C->isOne();
12092}
12093
12094bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12096 unsigned BitWidth = N.getScalarValueSizeInBits();
12097 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12098 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12099}
12100
12102 DropOperands();
12103}
12104
12105MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12106 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12107 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12108 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12109 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12110 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12111 MemSDNodeBits.IsInvariant = MMO->isInvariant();
12112
12113 // We check here that the size of the memory operand fits within the size of
12114 // the MMO. This is because the MMO might indicate only a possible address
12115 // range instead of specifying the affected memory addresses precisely.
12116 assert(
12117 (!MMO->getType().isValid() ||
12119 "Size mismatch!");
12120}
12121
12122/// Profile - Gather unique data for the node.
12123///
12125 AddNodeIDNode(ID, this);
12126}
12127
12128namespace {
12129
12130 struct EVTArray {
12131 std::vector<EVT> VTs;
12132
12133 EVTArray() {
12134 VTs.reserve(MVT::VALUETYPE_SIZE);
12135 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
12136 VTs.push_back(MVT((MVT::SimpleValueType)i));
12137 }
12138 };
12139
12140} // end anonymous namespace
12141
12142/// getValueTypeList - Return a pointer to the specified value type.
12143///
12144const EVT *SDNode::getValueTypeList(EVT VT) {
12145 static std::set<EVT, EVT::compareRawBits> EVTs;
12146 static EVTArray SimpleVTArray;
12147 static sys::SmartMutex<true> VTMutex;
12148
12149 if (VT.isExtended()) {
12150 sys::SmartScopedLock<true> Lock(VTMutex);
12151 return &(*EVTs.insert(VT).first);
12152 }
12153 assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
12154 return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
12155}
12156
12157/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
12158/// indicated value. This method ignores uses of other values defined by this
12159/// operation.
12160bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
12161 assert(Value < getNumValues() && "Bad value!");
12162
12163 // TODO: Only iterate over uses of a given value of the node
12164 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
12165 if (UI.getUse().getResNo() == Value) {
12166 if (NUses == 0)
12167 return false;
12168 --NUses;
12169 }
12170 }
12171
12172 // Found exactly the right number of uses?
12173 return NUses == 0;
12174}
12175
12176/// hasAnyUseOfValue - Return true if there are any use of the indicated
12177/// value. This method ignores uses of other values defined by this operation.
12178bool SDNode::hasAnyUseOfValue(unsigned Value) const {
12179 assert(Value < getNumValues() && "Bad value!");
12180
12181 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
12182 if (UI.getUse().getResNo() == Value)
12183 return true;
12184
12185 return false;
12186}
12187
12188/// isOnlyUserOf - Return true if this node is the only use of N.
12189bool SDNode::isOnlyUserOf(const SDNode *N) const {
12190 bool Seen = false;
12191 for (const SDNode *User : N->uses()) {
12192 if (User == this)
12193 Seen = true;
12194 else
12195 return false;
12196 }
12197
12198 return Seen;
12199}
12200
12201/// Return true if the only users of N are contained in Nodes.
12203 bool Seen = false;
12204 for (const SDNode *User : N->uses()) {
12205 if (llvm::is_contained(Nodes, User))
12206 Seen = true;
12207 else
12208 return false;
12209 }
12210
12211 return Seen;
12212}
12213
12214/// isOperand - Return true if this node is an operand of N.
12215bool SDValue::isOperandOf(const SDNode *N) const {
12216 return is_contained(N->op_values(), *this);
12217}
12218
12219bool SDNode::isOperandOf(const SDNode *N) const {
12220 return any_of(N->op_values(),
12221 [this](SDValue Op) { return this == Op.getNode(); });
12222}
12223
12224/// reachesChainWithoutSideEffects - Return true if this operand (which must
12225/// be a chain) reaches the specified operand without crossing any
12226/// side-effecting instructions on any chain path. In practice, this looks
12227/// through token factors and non-volatile loads. In order to remain efficient,
12228/// this only looks a couple of nodes in, it does not do an exhaustive search.
12229///
12230/// Note that we only need to examine chains when we're searching for
12231/// side-effects; SelectionDAG requires that all side-effects are represented
12232/// by chains, even if another operand would force a specific ordering. This
12233/// constraint is necessary to allow transformations like splitting loads.
12235 unsigned Depth) const {
12236 if (*this == Dest) return true;
12237
12238 // Don't search too deeply, we just want to be able to see through
12239 // TokenFactor's etc.
12240 if (Depth == 0) return false;
12241
12242 // If this is a token factor, all inputs to the TF happen in parallel.
12243 if (getOpcode() == ISD::TokenFactor) {
12244 // First, try a shallow search.
12245 if (is_contained((*this)->ops(), Dest)) {
12246 // We found the chain we want as an operand of this TokenFactor.
12247 // Essentially, we reach the chain without side-effects if we could
12248 // serialize the TokenFactor into a simple chain of operations with
12249 // Dest as the last operation. This is automatically true if the
12250 // chain has one use: there are no other ordering constraints.
12251 // If the chain has more than one use, we give up: some other
12252 // use of Dest might force a side-effect between Dest and the current
12253 // node.
12254 if (Dest.hasOneUse())
12255 return true;
12256 }
12257 // Next, try a deep search: check whether every operand of the TokenFactor
12258 // reaches Dest.
12259 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
12260 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
12261 });
12262 }
12263
12264 // Loads don't have side effects, look through them.
12265 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
12266 if (Ld->isUnordered())
12267 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
12268 }
12269 return false;
12270}
12271
12272bool SDNode::hasPredecessor(const SDNode *N) const {
12275 Worklist.push_back(this);
12276 return hasPredecessorHelper(N, Visited, Worklist);
12277}
12278
12280 this->Flags.intersectWith(Flags);
12281}
12282
12283SDValue
12285 ArrayRef<ISD::NodeType> CandidateBinOps,
12286 bool AllowPartials) {
12287 // The pattern must end in an extract from index 0.
12288 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
12289 !isNullConstant(Extract->getOperand(1)))
12290 return SDValue();
12291
12292 // Match against one of the candidate binary ops.
12293 SDValue Op = Extract->getOperand(0);
12294 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
12295 return Op.getOpcode() == unsigned(BinOp);
12296 }))
12297 return SDValue();
12298
12299 // Floating-point reductions may require relaxed constraints on the final step
12300 // of the reduction because they may reorder intermediate operations.
12301 unsigned CandidateBinOp = Op.getOpcode();
12302 if (Op.getValueType().isFloatingPoint()) {
12303 SDNodeFlags Flags = Op->getFlags();
12304 switch (CandidateBinOp) {
12305 case ISD::FADD:
12306 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
12307 return SDValue();
12308 break;
12309 default:
12310 llvm_unreachable("Unhandled FP opcode for binop reduction");
12311 }
12312 }
12313
12314 // Matching failed - attempt to see if we did enough stages that a partial
12315 // reduction from a subvector is possible.
12316 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12317 if (!AllowPartials || !Op)
12318 return SDValue();
12319 EVT OpVT = Op.getValueType();
12320 EVT OpSVT = OpVT.getScalarType();
12321 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12322 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12323 return SDValue();
12324 BinOp = (ISD::NodeType)CandidateBinOp;
12325 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12327 };
12328
12329 // At each stage, we're looking for something that looks like:
12330 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12331 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12332 // i32 undef, i32 undef, i32 undef, i32 undef>
12333 // %a = binop <8 x i32> %op, %s
12334 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12335 // we expect something like:
12336 // <4,5,6,7,u,u,u,u>
12337 // <2,3,u,u,u,u,u,u>
12338 // <1,u,u,u,u,u,u,u>
12339 // While a partial reduction match would be:
12340 // <2,3,u,u,u,u,u,u>
12341 // <1,u,u,u,u,u,u,u>
12342 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12343 SDValue PrevOp;
12344 for (unsigned i = 0; i < Stages; ++i) {
12345 unsigned MaskEnd = (1 << i);
12346
12347 if (Op.getOpcode() != CandidateBinOp)
12348 return PartialReduction(PrevOp, MaskEnd);
12349
12350 SDValue Op0 = Op.getOperand(0);
12351 SDValue Op1 = Op.getOperand(1);
12352
12353 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12354 if (Shuffle) {
12355 Op = Op1;
12356 } else {
12357 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12358 Op = Op0;
12359 }
12360
12361 // The first operand of the shuffle should be the same as the other operand
12362 // of the binop.
12363 if (!Shuffle || Shuffle->getOperand(0) != Op)
12364 return PartialReduction(PrevOp, MaskEnd);
12365
12366 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12367 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12368 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12369 return PartialReduction(PrevOp, MaskEnd);
12370
12371 PrevOp = Op;
12372 }
12373
12374 // Handle subvector reductions, which tend to appear after the shuffle
12375 // reduction stages.
12376 while (Op.getOpcode() == CandidateBinOp) {
12377 unsigned NumElts = Op.getValueType().getVectorNumElements();
12378 SDValue Op0 = Op.getOperand(0);
12379 SDValue Op1 = Op.getOperand(1);
12380 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12382 Op0.getOperand(0) != Op1.getOperand(0))
12383 break;
12384 SDValue Src = Op0.getOperand(0);
12385 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12386 if (NumSrcElts != (2 * NumElts))
12387 break;
12388 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12389 Op1.getConstantOperandAPInt(1) == NumElts) &&
12390 !(Op1.getConstantOperandAPInt(1) == 0 &&
12391 Op0.getConstantOperandAPInt(1) == NumElts))
12392 break;
12393 Op = Src;
12394 }
12395
12396 BinOp = (ISD::NodeType)CandidateBinOp;
12397 return Op;
12398}
12399
12401 EVT VT = N->getValueType(0);
12402 EVT EltVT = VT.getVectorElementType();
12403 unsigned NE = VT.getVectorNumElements();
12404
12405 SDLoc dl(N);
12406
12407 // If ResNE is 0, fully unroll the vector op.
12408 if (ResNE == 0)
12409 ResNE = NE;
12410 else if (NE > ResNE)
12411 NE = ResNE;
12412
12413 if (N->getNumValues() == 2) {
12414 SmallVector<SDValue, 8> Scalars0, Scalars1;
12415 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12416 EVT VT1 = N->getValueType(1);
12417 EVT EltVT1 = VT1.getVectorElementType();
12418
12419 unsigned i;
12420 for (i = 0; i != NE; ++i) {
12421 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12422 SDValue Operand = N->getOperand(j);
12423 EVT OperandVT = Operand.getValueType();
12424
12425 // A vector operand; extract a single element.
12426 EVT OperandEltVT = OperandVT.getVectorElementType();
12427 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12428 Operand, getVectorIdxConstant(i, dl));
12429 }
12430
12431 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12432 Scalars0.push_back(EltOp);
12433 Scalars1.push_back(EltOp.getValue(1));
12434 }
12435
12436 SDValue Vec0 = getBuildVector(VT, dl, Scalars0);
12437 SDValue Vec1 = getBuildVector(VT1, dl, Scalars1);
12438 return getMergeValues({Vec0, Vec1}, dl);
12439 }
12440
12441 assert(N->getNumValues() == 1 &&
12442 "Can't unroll a vector with multiple results!");
12443
12445 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12446
12447 unsigned i;
12448 for (i= 0; i != NE; ++i) {
12449 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12450 SDValue Operand = N->getOperand(j);
12451 EVT OperandVT = Operand.getValueType();
12452 if (OperandVT.isVector()) {
12453 // A vector operand; extract a single element.
12454 EVT OperandEltVT = OperandVT.getVectorElementType();
12455 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12456 Operand, getVectorIdxConstant(i, dl));
12457 } else {
12458 // A scalar operand; just use it as is.
12459 Operands[j] = Operand;
12460 }
12461 }
12462
12463 switch (N->getOpcode()) {
12464 default: {
12465 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12466 N->getFlags()));
12467 break;
12468 }
12469 case ISD::VSELECT:
12470 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12471 break;
12472 case ISD::SHL:
12473 case ISD::SRA:
12474 case ISD::SRL:
12475 case ISD::ROTL:
12476 case ISD::ROTR:
12477 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12479 Operands[1])));
12480 break;
12482 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12483 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12484 Operands[0],
12485 getValueType(ExtVT)));
12486 }
12487 }
12488 }
12489
12490 for (; i < ResNE; ++i)
12491 Scalars.push_back(getUNDEF(EltVT));
12492
12493 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12494 return getBuildVector(VecVT, dl, Scalars);
12495}
12496
12497std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12498 SDNode *N, unsigned ResNE) {
12499 unsigned Opcode = N->getOpcode();
12500 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12501 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12502 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12503 "Expected an overflow opcode");
12504
12505 EVT ResVT = N->getValueType(0);
12506 EVT OvVT = N->getValueType(1);
12507 EVT ResEltVT = ResVT.getVectorElementType();
12508 EVT OvEltVT = OvVT.getVectorElementType();
12509 SDLoc dl(N);
12510
12511 // If ResNE is 0, fully unroll the vector op.
12512 unsigned NE = ResVT.getVectorNumElements();
12513 if (ResNE == 0)
12514 ResNE = NE;
12515 else if (NE > ResNE)
12516 NE = ResNE;
12517
12518 SmallVector<SDValue, 8> LHSScalars;
12519 SmallVector<SDValue, 8> RHSScalars;
12520 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12521 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12522
12523 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12524 SDVTList VTs = getVTList(ResEltVT, SVT);
12525 SmallVector<SDValue, 8> ResScalars;
12526 SmallVector<SDValue, 8> OvScalars;
12527 for (unsigned i = 0; i < NE; ++i) {
12528 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12529 SDValue Ov =
12530 getSelect(dl, OvEltVT, Res.getValue(1),
12531 getBoolConstant(true, dl, OvEltVT, ResVT),
12532 getConstant(0, dl, OvEltVT));
12533
12534 ResScalars.push_back(Res);
12535 OvScalars.push_back(Ov);
12536 }
12537
12538 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12539 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12540
12541 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12542 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12543 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12544 getBuildVector(NewOvVT, dl, OvScalars));
12545}
12546
12549 unsigned Bytes,
12550 int Dist) const {
12551 if (LD->isVolatile() || Base->isVolatile())
12552 return false;
12553 // TODO: probably too restrictive for atomics, revisit
12554 if (!LD->isSimple())
12555 return false;
12556 if (LD->isIndexed() || Base->isIndexed())
12557 return false;
12558 if (LD->getChain() != Base->getChain())
12559 return false;
12560 EVT VT = LD->getMemoryVT();
12561 if (VT.getSizeInBits() / 8 != Bytes)
12562 return false;
12563
12564 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12565 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12566
12567 int64_t Offset = 0;
12568 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12569 return (Dist * (int64_t)Bytes == Offset);
12570 return false;
12571}
12572
12573/// InferPtrAlignment - Infer alignment of a load / store address. Return
12574/// std::nullopt if it cannot be inferred.
12576 // If this is a GlobalAddress + cst, return the alignment.
12577 const GlobalValue *GV = nullptr;
12578 int64_t GVOffset = 0;
12579 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12580 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12581 KnownBits Known(PtrWidth);
12583 unsigned AlignBits = Known.countMinTrailingZeros();
12584 if (AlignBits)
12585 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12586 }
12587
12588 // If this is a direct reference to a stack slot, use information about the
12589 // stack slot's alignment.
12590 int FrameIdx = INT_MIN;
12591 int64_t FrameOffset = 0;
12592 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12593 FrameIdx = FI->getIndex();
12594 } else if (isBaseWithConstantOffset(Ptr) &&
12595 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12596 // Handle FI+Cst
12597 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12598 FrameOffset = Ptr.getConstantOperandVal(1);
12599 }
12600
12601 if (FrameIdx != INT_MIN) {
12603 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12604 }
12605
12606 return std::nullopt;
12607}
12608
12609/// Split the scalar node with EXTRACT_ELEMENT using the provided
12610/// VTs and return the low/high part.
12611std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12612 const SDLoc &DL,
12613 const EVT &LoVT,
12614 const EVT &HiVT) {
12615 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12616 "Split node must be a scalar type");
12617 SDValue Lo =
12619 SDValue Hi =
12621 return std::make_pair(Lo, Hi);
12622}
12623
12624/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12625/// which is split (or expanded) into two not necessarily identical pieces.
12626std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12627 // Currently all types are split in half.
12628 EVT LoVT, HiVT;
12629 if (!VT.isVector())
12630 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12631 else
12632 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12633
12634 return std::make_pair(LoVT, HiVT);
12635}
12636
12637/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12638/// type, dependent on an enveloping VT that has been split into two identical
12639/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12640std::pair<EVT, EVT>
12642 bool *HiIsEmpty) const {
12643 EVT EltTp = VT.getVectorElementType();
12644 // Examples:
12645 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12646 // custom VL=9 with enveloping VL=8/8 yields 8/1
12647 // custom VL=10 with enveloping VL=8/8 yields 8/2
12648 // etc.
12649 ElementCount VTNumElts = VT.getVectorElementCount();
12650 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12651 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12652 "Mixing fixed width and scalable vectors when enveloping a type");
12653 EVT LoVT, HiVT;
12654 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12655 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12656 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12657 *HiIsEmpty = false;
12658 } else {
12659 // Flag that hi type has zero storage size, but return split envelop type
12660 // (this would be easier if vector types with zero elements were allowed).
12661 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12662 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12663 *HiIsEmpty = true;
12664 }
12665 return std::make_pair(LoVT, HiVT);
12666}
12667
12668/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
12669/// low/high part.
12670std::pair<SDValue, SDValue>
12671SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
12672 const EVT &HiVT) {
12673 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
12674 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
12675 "Splitting vector with an invalid mixture of fixed and scalable "
12676 "vector types");
12678 N.getValueType().getVectorMinNumElements() &&
12679 "More vector elements requested than available!");
12680 SDValue Lo, Hi;
12681 Lo =
12683 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
12684 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
12685 // IDX with the runtime scaling factor of the result vector type. For
12686 // fixed-width result vectors, that runtime scaling factor is 1.
12689 return std::make_pair(Lo, Hi);
12690}
12691
12692std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
12693 const SDLoc &DL) {
12694 // Split the vector length parameter.
12695 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
12696 EVT VT = N.getValueType();
12698 "Expecting the mask to be an evenly-sized vector");
12699 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
12700 SDValue HalfNumElts =
12701 VecVT.isFixedLengthVector()
12702 ? getConstant(HalfMinNumElts, DL, VT)
12703 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
12704 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
12705 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
12706 return std::make_pair(Lo, Hi);
12707}
12708
12709/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
12711 EVT VT = N.getValueType();
12714 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
12716}
12717
12720 unsigned Start, unsigned Count,
12721 EVT EltVT) {
12722 EVT VT = Op.getValueType();
12723 if (Count == 0)
12724 Count = VT.getVectorNumElements();
12725 if (EltVT == EVT())
12726 EltVT = VT.getVectorElementType();
12727 SDLoc SL(Op);
12728 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
12729 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
12730 getVectorIdxConstant(i, SL)));
12731 }
12732}
12733
12734// getAddressSpace - Return the address space this GlobalAddress belongs to.
12736 return getGlobal()->getType()->getAddressSpace();
12737}
12738
12741 return Val.MachineCPVal->getType();
12742 return Val.ConstVal->getType();
12743}
12744
12745bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
12746 unsigned &SplatBitSize,
12747 bool &HasAnyUndefs,
12748 unsigned MinSplatBits,
12749 bool IsBigEndian) const {
12750 EVT VT = getValueType(0);
12751 assert(VT.isVector() && "Expected a vector type");
12752 unsigned VecWidth = VT.getSizeInBits();
12753 if (MinSplatBits > VecWidth)
12754 return false;
12755
12756 // FIXME: The widths are based on this node's type, but build vectors can
12757 // truncate their operands.
12758 SplatValue = APInt(VecWidth, 0);
12759 SplatUndef = APInt(VecWidth, 0);
12760
12761 // Get the bits. Bits with undefined values (when the corresponding element
12762 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
12763 // in SplatValue. If any of the values are not constant, give up and return
12764 // false.
12765 unsigned int NumOps = getNumOperands();
12766 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
12767 unsigned EltWidth = VT.getScalarSizeInBits();
12768
12769 for (unsigned j = 0; j < NumOps; ++j) {
12770 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
12771 SDValue OpVal = getOperand(i);
12772 unsigned BitPos = j * EltWidth;
12773
12774 if (OpVal.isUndef())
12775 SplatUndef.setBits(BitPos, BitPos + EltWidth);
12776 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
12777 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
12778 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
12779 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
12780 else
12781 return false;
12782 }
12783
12784 // The build_vector is all constants or undefs. Find the smallest element
12785 // size that splats the vector.
12786 HasAnyUndefs = (SplatUndef != 0);
12787
12788 // FIXME: This does not work for vectors with elements less than 8 bits.
12789 while (VecWidth > 8) {
12790 // If we can't split in half, stop here.
12791 if (VecWidth & 1)
12792 break;
12793
12794 unsigned HalfSize = VecWidth / 2;
12795 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
12796 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
12797 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
12798 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
12799
12800 // If the two halves do not match (ignoring undef bits), stop here.
12801 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
12802 MinSplatBits > HalfSize)
12803 break;
12804
12805 SplatValue = HighValue | LowValue;
12806 SplatUndef = HighUndef & LowUndef;
12807
12808 VecWidth = HalfSize;
12809 }
12810
12811 // FIXME: The loop above only tries to split in halves. But if the input
12812 // vector for example is <3 x i16> it wouldn't be able to detect a
12813 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
12814 // optimizations. I guess that back in the days when this helper was created
12815 // vectors normally was power-of-2 sized.
12816
12817 SplatBitSize = VecWidth;
12818 return true;
12819}
12820
12822 BitVector *UndefElements) const {
12823 unsigned NumOps = getNumOperands();
12824 if (UndefElements) {
12825 UndefElements->clear();
12826 UndefElements->resize(NumOps);
12827 }
12828 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12829 if (!DemandedElts)
12830 return SDValue();
12831 SDValue Splatted;
12832 for (unsigned i = 0; i != NumOps; ++i) {
12833 if (!DemandedElts[i])
12834 continue;
12835 SDValue Op = getOperand(i);
12836 if (Op.isUndef()) {
12837 if (UndefElements)
12838 (*UndefElements)[i] = true;
12839 } else if (!Splatted) {
12840 Splatted = Op;
12841 } else if (Splatted != Op) {
12842 return SDValue();
12843 }
12844 }
12845
12846 if (!Splatted) {
12847 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
12848 assert(getOperand(FirstDemandedIdx).isUndef() &&
12849 "Can only have a splat without a constant for all undefs.");
12850 return getOperand(FirstDemandedIdx);
12851 }
12852
12853 return Splatted;
12854}
12855
12857 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12858 return getSplatValue(DemandedElts, UndefElements);
12859}
12860
12862 SmallVectorImpl<SDValue> &Sequence,
12863 BitVector *UndefElements) const {
12864 unsigned NumOps = getNumOperands();
12865 Sequence.clear();
12866 if (UndefElements) {
12867 UndefElements->clear();
12868 UndefElements->resize(NumOps);
12869 }
12870 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12871 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
12872 return false;
12873
12874 // Set the undefs even if we don't find a sequence (like getSplatValue).
12875 if (UndefElements)
12876 for (unsigned I = 0; I != NumOps; ++I)
12877 if (DemandedElts[I] && getOperand(I).isUndef())
12878 (*UndefElements)[I] = true;
12879
12880 // Iteratively widen the sequence length looking for repetitions.
12881 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
12882 Sequence.append(SeqLen, SDValue());
12883 for (unsigned I = 0; I != NumOps; ++I) {
12884 if (!DemandedElts[I])
12885 continue;
12886 SDValue &SeqOp = Sequence[I % SeqLen];
12888 if (Op.isUndef()) {
12889 if (!SeqOp)
12890 SeqOp = Op;
12891 continue;
12892 }
12893 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
12894 Sequence.clear();
12895 break;
12896 }
12897 SeqOp = Op;
12898 }
12899 if (!Sequence.empty())
12900 return true;
12901 }
12902
12903 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
12904 return false;
12905}
12906
12908 BitVector *UndefElements) const {
12909 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12910 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
12911}
12912
12915 BitVector *UndefElements) const {
12916 return dyn_cast_or_null<ConstantSDNode>(
12917 getSplatValue(DemandedElts, UndefElements));
12918}
12919
12922 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
12923}
12924
12927 BitVector *UndefElements) const {
12928 return dyn_cast_or_null<ConstantFPSDNode>(
12929 getSplatValue(DemandedElts, UndefElements));
12930}
12931
12934 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
12935}
12936
12937int32_t
12939 uint32_t BitWidth) const {
12940 if (ConstantFPSDNode *CN =
12941 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
12942 bool IsExact;
12943 APSInt IntVal(BitWidth);
12944 const APFloat &APF = CN->getValueAPF();
12945 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
12946 APFloat::opOK ||
12947 !IsExact)
12948 return -1;
12949
12950 return IntVal.exactLogBase2();
12951 }
12952 return -1;
12953}
12954
12956 bool IsLittleEndian, unsigned DstEltSizeInBits,
12957 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
12958 // Early-out if this contains anything but Undef/Constant/ConstantFP.
12959 if (!isConstant())
12960 return false;
12961
12962 unsigned NumSrcOps = getNumOperands();
12963 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
12964 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12965 "Invalid bitcast scale");
12966
12967 // Extract raw src bits.
12968 SmallVector<APInt> SrcBitElements(NumSrcOps,
12969 APInt::getZero(SrcEltSizeInBits));
12970 BitVector SrcUndeElements(NumSrcOps, false);
12971
12972 for (unsigned I = 0; I != NumSrcOps; ++I) {
12974 if (Op.isUndef()) {
12975 SrcUndeElements.set(I);
12976 continue;
12977 }
12978 auto *CInt = dyn_cast<ConstantSDNode>(Op);
12979 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
12980 assert((CInt || CFP) && "Unknown constant");
12981 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
12982 : CFP->getValueAPF().bitcastToAPInt();
12983 }
12984
12985 // Recast to dst width.
12986 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
12987 SrcBitElements, UndefElements, SrcUndeElements);
12988 return true;
12989}
12990
12991void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
12992 unsigned DstEltSizeInBits,
12993 SmallVectorImpl<APInt> &DstBitElements,
12994 ArrayRef<APInt> SrcBitElements,
12995 BitVector &DstUndefElements,
12996 const BitVector &SrcUndefElements) {
12997 unsigned NumSrcOps = SrcBitElements.size();
12998 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
12999 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13000 "Invalid bitcast scale");
13001 assert(NumSrcOps == SrcUndefElements.size() &&
13002 "Vector size mismatch");
13003
13004 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13005 DstUndefElements.clear();
13006 DstUndefElements.resize(NumDstOps, false);
13007 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13008
13009 // Concatenate src elements constant bits together into dst element.
13010 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13011 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13012 for (unsigned I = 0; I != NumDstOps; ++I) {
13013 DstUndefElements.set(I);
13014 APInt &DstBits = DstBitElements[I];
13015 for (unsigned J = 0; J != Scale; ++J) {
13016 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13017 if (SrcUndefElements[Idx])
13018 continue;
13019 DstUndefElements.reset(I);
13020 const APInt &SrcBits = SrcBitElements[Idx];
13021 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13022 "Illegal constant bitwidths");
13023 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13024 }
13025 }
13026 return;
13027 }
13028
13029 // Split src element constant bits into dst elements.
13030 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13031 for (unsigned I = 0; I != NumSrcOps; ++I) {
13032 if (SrcUndefElements[I]) {
13033 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13034 continue;
13035 }
13036 const APInt &SrcBits = SrcBitElements[I];
13037 for (unsigned J = 0; J != Scale; ++J) {
13038 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13039 APInt &DstBits = DstBitElements[Idx];
13040 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13041 }
13042 }
13043}
13044
13046 for (const SDValue &Op : op_values()) {
13047 unsigned Opc = Op.getOpcode();
13048 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13049 return false;
13050 }
13051 return true;
13052}
13053
13054std::optional<std::pair<APInt, APInt>>
13056 unsigned NumOps = getNumOperands();
13057 if (NumOps < 2)
13058 return std::nullopt;
13059
13060 if (!isa<ConstantSDNode>(getOperand(0)) ||
13061 !isa<ConstantSDNode>(getOperand(1)))
13062 return std::nullopt;
13063
13064 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13065 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13066 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13067
13068 if (Stride.isZero())
13069 return std::nullopt;
13070
13071 for (unsigned i = 2; i < NumOps; ++i) {
13072 if (!isa<ConstantSDNode>(getOperand(i)))
13073 return std::nullopt;
13074
13075 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13076 if (Val != (Start + (Stride * i)))
13077 return std::nullopt;
13078 }
13079
13080 return std::make_pair(Start, Stride);
13081}
13082
13083bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
13084 // Find the first non-undef value in the shuffle mask.
13085 unsigned i, e;
13086 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
13087 /* search */;
13088
13089 // If all elements are undefined, this shuffle can be considered a splat
13090 // (although it should eventually get simplified away completely).
13091 if (i == e)
13092 return true;
13093
13094 // Make sure all remaining elements are either undef or the same as the first
13095 // non-undef value.
13096 for (int Idx = Mask[i]; i != e; ++i)
13097 if (Mask[i] >= 0 && Mask[i] != Idx)
13098 return false;
13099 return true;
13100}
13101
13102// Returns the SDNode if it is a constant integer BuildVector
13103// or constant integer.
13105 if (isa<ConstantSDNode>(N))
13106 return N.getNode();
13108 return N.getNode();
13109 // Treat a GlobalAddress supporting constant offset folding as a
13110 // constant integer.
13111 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
13112 if (GA->getOpcode() == ISD::GlobalAddress &&
13113 TLI->isOffsetFoldingLegal(GA))
13114 return GA;
13115 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13116 isa<ConstantSDNode>(N.getOperand(0)))
13117 return N.getNode();
13118 return nullptr;
13119}
13120
13121// Returns the SDNode if it is a constant float BuildVector
13122// or constant float.
13124 if (isa<ConstantFPSDNode>(N))
13125 return N.getNode();
13126
13128 return N.getNode();
13129
13130 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13131 isa<ConstantFPSDNode>(N.getOperand(0)))
13132 return N.getNode();
13133
13134 return nullptr;
13135}
13136
13138 bool AllowTruncation) const {
13139 ConstantSDNode *Const = isConstOrConstSplat(N, false, AllowTruncation);
13140 if (!Const)
13141 return std::nullopt;
13142
13143 const APInt &CVal = Const->getAPIntValue();
13144 switch (TLI->getBooleanContents(N.getValueType())) {
13146 if (CVal.isOne())
13147 return true;
13148 if (CVal.isZero())
13149 return false;
13150 return std::nullopt;
13152 if (CVal.isAllOnes())
13153 return true;
13154 if (CVal.isZero())
13155 return false;
13156 return std::nullopt;
13158 return CVal[0];
13159 }
13160 llvm_unreachable("Unknown BooleanContent enum");
13161}
13162
13163void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
13164 assert(!Node->OperandList && "Node already has operands");
13166 "too many operands to fit into SDNode");
13167 SDUse *Ops = OperandRecycler.allocate(
13168 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
13169
13170 bool IsDivergent = false;
13171 for (unsigned I = 0; I != Vals.size(); ++I) {
13172 Ops[I].setUser(Node);
13173 Ops[I].setInitial(Vals[I]);
13174 EVT VT = Ops[I].getValueType();
13175
13176 // Skip Chain. It does not carry divergence.
13177 if (VT != MVT::Other &&
13178 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
13179 Ops[I].getNode()->isDivergent()) {
13180 IsDivergent = true;
13181 }
13182 }
13183 Node->NumOperands = Vals.size();
13184 Node->OperandList = Ops;
13185 if (!TLI->isSDNodeAlwaysUniform(Node)) {
13186 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
13187 Node->SDNodeBits.IsDivergent = IsDivergent;
13188 }
13190}
13191
13194 size_t Limit = SDNode::getMaxNumOperands();
13195 while (Vals.size() > Limit) {
13196 unsigned SliceIdx = Vals.size() - Limit;
13197 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
13198 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
13199 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
13200 Vals.emplace_back(NewTF);
13201 }
13202 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
13203}
13204
13206 EVT VT, SDNodeFlags Flags) {
13207 switch (Opcode) {
13208 default:
13209 return SDValue();
13210 case ISD::ADD:
13211 case ISD::OR:
13212 case ISD::XOR:
13213 case ISD::UMAX:
13214 return getConstant(0, DL, VT);
13215 case ISD::MUL:
13216 return getConstant(1, DL, VT);
13217 case ISD::AND:
13218 case ISD::UMIN:
13219 return getAllOnesConstant(DL, VT);
13220 case ISD::SMAX:
13222 case ISD::SMIN:
13224 case ISD::FADD:
13225 return getConstantFP(-0.0, DL, VT);
13226 case ISD::FMUL:
13227 return getConstantFP(1.0, DL, VT);
13228 case ISD::FMINNUM:
13229 case ISD::FMAXNUM: {
13230 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13231 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
13232 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
13233 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
13234 APFloat::getLargest(Semantics);
13235 if (Opcode == ISD::FMAXNUM)
13236 NeutralAF.changeSign();
13237
13238 return getConstantFP(NeutralAF, DL, VT);
13239 }
13240 case ISD::FMINIMUM:
13241 case ISD::FMAXIMUM: {
13242 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
13243 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
13244 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
13245 : APFloat::getLargest(Semantics);
13246 if (Opcode == ISD::FMAXIMUM)
13247 NeutralAF.changeSign();
13248
13249 return getConstantFP(NeutralAF, DL, VT);
13250 }
13251
13252 }
13253}
13254
13255/// Helper used to make a call to a library function that has one argument of
13256/// pointer type.
13257///
13258/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
13259/// used to get or set floating-point state. They have one argument of pointer
13260/// type, which points to the memory region containing bits of the
13261/// floating-point state. The value returned by such function is ignored in the
13262/// created call.
13263///
13264/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
13265/// \param Ptr Pointer used to save/load state.
13266/// \param InChain Ingoing token chain.
13267/// \returns Outgoing chain token.
13269 SDValue InChain,
13270 const SDLoc &DLoc) {
13271 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
13274 Entry.Node = Ptr;
13275 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
13276 Args.push_back(Entry);
13277 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
13278 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
13279 TLI->getPointerTy(getDataLayout()));
13281 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
13282 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
13283 std::move(Args));
13284 return TLI->LowerCallTo(CLI).second;
13285}
13286
13288 assert(From && To && "Invalid SDNode; empty source SDValue?");
13289 auto I = SDEI.find(From);
13290 if (I == SDEI.end())
13291 return;
13292
13293 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
13294 // the iterator, hence the need to make a copy to prevent a use-after-free.
13295 NodeExtraInfo NEI = I->second;
13296 if (LLVM_LIKELY(!NEI.PCSections) && LLVM_LIKELY(!NEI.MMRA)) {
13297 // No deep copy required for the types of extra info set.
13298 //
13299 // FIXME: Investigate if other types of extra info also need deep copy. This
13300 // depends on the types of nodes they can be attached to: if some extra info
13301 // is only ever attached to nodes where a replacement To node is always the
13302 // node where later use and propagation of the extra info has the intended
13303 // semantics, no deep copy is required.
13304 SDEI[To] = std::move(NEI);
13305 return;
13306 }
13307
13308 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
13309 // through the replacement of From with To. Otherwise, replacements of a node
13310 // (From) with more complex nodes (To and its operands) may result in lost
13311 // extra info where the root node (To) is insignificant in further propagating
13312 // and using extra info when further lowering to MIR.
13313 //
13314 // In the first step pre-populate the visited set with the nodes reachable
13315 // from the old From node. This avoids copying NodeExtraInfo to parts of the
13316 // DAG that is not new and should be left untouched.
13317 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
13318 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
13319 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
13320 if (MaxDepth == 0) {
13321 // Remember this node in case we need to increase MaxDepth and continue
13322 // populating FromReach from this node.
13323 Leafs.emplace_back(N);
13324 return;
13325 }
13326 if (!FromReach.insert(N).second)
13327 return;
13328 for (const SDValue &Op : N->op_values())
13329 Self(Self, Op.getNode(), MaxDepth - 1);
13330 };
13331
13332 // Copy extra info to To and all its transitive operands (that are new).
13334 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
13335 if (FromReach.contains(N))
13336 return true;
13337 if (!Visited.insert(N).second)
13338 return true;
13339 if (getEntryNode().getNode() == N)
13340 return false;
13341 for (const SDValue &Op : N->op_values()) {
13342 if (!Self(Self, Op.getNode()))
13343 return false;
13344 }
13345 // Copy only if entry node was not reached.
13346 SDEI[N] = NEI;
13347 return true;
13348 };
13349
13350 // We first try with a lower MaxDepth, assuming that the path to common
13351 // operands between From and To is relatively short. This significantly
13352 // improves performance in the common case. The initial MaxDepth is big
13353 // enough to avoid retry in the common case; the last MaxDepth is large
13354 // enough to avoid having to use the fallback below (and protects from
13355 // potential stack exhaustion from recursion).
13356 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13357 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13358 // StartFrom is the previous (or initial) set of leafs reachable at the
13359 // previous maximum depth.
13361 std::swap(StartFrom, Leafs);
13362 for (const SDNode *N : StartFrom)
13363 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13364 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13365 return;
13366 // This should happen very rarely (reached the entry node).
13367 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13368 assert(!Leafs.empty());
13369 }
13370
13371 // This should not happen - but if it did, that means the subgraph reachable
13372 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13373 // could not visit all reachable common operands. Consequently, we were able
13374 // to reach the entry node.
13375 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13376 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13377 // Best-effort fallback if assertions disabled.
13378 SDEI[To] = std::move(NEI);
13379}
13380
13381#ifndef NDEBUG
13382static void checkForCyclesHelper(const SDNode *N,
13385 const llvm::SelectionDAG *DAG) {
13386 // If this node has already been checked, don't check it again.
13387 if (Checked.count(N))
13388 return;
13389
13390 // If a node has already been visited on this depth-first walk, reject it as
13391 // a cycle.
13392 if (!Visited.insert(N).second) {
13393 errs() << "Detected cycle in SelectionDAG\n";
13394 dbgs() << "Offending node:\n";
13395 N->dumprFull(DAG); dbgs() << "\n";
13396 abort();
13397 }
13398
13399 for (const SDValue &Op : N->op_values())
13400 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13401
13402 Checked.insert(N);
13403 Visited.erase(N);
13404}
13405#endif
13406
13408 const llvm::SelectionDAG *DAG,
13409 bool force) {
13410#ifndef NDEBUG
13411 bool check = force;
13412#ifdef EXPENSIVE_CHECKS
13413 check = true;
13414#endif // EXPENSIVE_CHECKS
13415 if (check) {
13416 assert(N && "Checking nonexistent SDNode");
13419 checkForCyclesHelper(N, visited, checked, DAG);
13420 }
13421#endif // !NDEBUG
13422}
13423
13424void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13425 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13426}
static bool isConstant(const MachineInstr &MI)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition: Compiler.h:468
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:240
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Given that RA is a live propagate it s liveness to any other values it uses(according to Uses). void DeadArgumentEliminationPass
Given that RA is a live value
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file defines a hash set that can be used to remove duplication of nodes in a graph.
Rewrite Partial Register Uses
#define check(cond)
static const unsigned MaxDepth
static LVOptions Options
Definition: LVOptions.cpp:25
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:512
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
mir Rename Register Operands
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
unsigned const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Contains matchers for matching SelectionDAG nodes and values.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, AAResults *AA)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void VerifySDNode(SDNode *N, const TargetLowering *TLI)
VerifySDNode - Check the given SDNode. Aborts if it is invalid.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file describes how to lower LLVM code to machine code.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
Value * RHS
Value * LHS
static unsigned getSize(unsigned Kind)
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:1032
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1125
void copySign(const APFloat &RHS)
Definition: APFloat.h:1219
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5337
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1107
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition: APFloat.h:1343
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1098
bool isFinite() const
Definition: APFloat.h:1365
bool isNaN() const
Definition: APFloat.h:1358
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1249
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1116
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1152
bool isZero() const
Definition: APFloat.h:1356
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1050
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1241
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1010
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1143
bool isPosZero() const
Definition: APFloat.h:1371
bool isNegZero() const
Definition: APFloat.h:1372
void changeSign()
Definition: APFloat.h:1214
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:1021
bool isInfinity() const
Definition: APFloat.h:1357
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2025
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:214
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1387
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:209
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1500
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1372
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1629
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1366
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:608
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1002
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1472
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1310
APInt abs() const
Get the absolute value.
Definition: APInt.h:1753
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:1996
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:351
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1162
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:360
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1636
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1448
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1091
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:189
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:309
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1377
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition: APInt.cpp:1124
APInt reverseBits() const
Definition: APInt.cpp:737
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:814
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1146
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1598
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1587
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1557
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:620
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:199
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2056
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2070
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1010
APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition: APInt.cpp:1111
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition: APInt.cpp:368
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1397
unsigned logBase2() const
Definition: APInt.h:1719
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:807
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1299
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1706
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:314
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1130
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1347
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:853
APInt byteSwap() const
Definition: APInt.cpp:715
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1237
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:420
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:286
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:180
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1369
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:453
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1217
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:369
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:266
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:219
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:838
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:831
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1201
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2015
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
Recycle small arrays allocated from a BumpPtrAllocator.
Definition: ArrayRecycler.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:195
This is an SDNode representing atomic operations.
static BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
BitVector & reset()
Definition: BitVector.h:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition: BitVector.h:335
BitVector & set()
Definition: BitVector.h:351
bool none() const
none - Returns true if none of the bits are set.
Definition: BitVector.h:188
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:159
unsigned getTargetFlags() const
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition: Constants.h:890
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:148
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition: Allocator.h:123
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:269
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:149
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:146
bool isMachineConstantPoolEntry() const
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition: Constant.h:42
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1686
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
DWARF expression.
static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:238
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
Definition: DataLayout.cpp:872
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:864
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:763
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:868
A debug info location.
Definition: DebugLoc.h:33
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:327
MachineBasicBlock * MBB
MBB - The current block.
Data structure describing the variable locations in a function.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:705
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:702
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:357
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:380
unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:263
unsigned getAddressSpace() const
Definition: GlobalValue.h:205
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
This class is used to form a handle around another node that is persistent and is updated across invo...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
constexpr bool isValid() const
Definition: LowLevelType.h:145
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate the offet and size that ar...
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Metadata node.
Definition: Metadata.h:1069
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:193
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:679
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Definition: PointerUnion.h:142
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Keeps track of dbg_value information through SDISel.
Definition: SelectionDAG.h:162
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:191
void add(SDDbgValue *V, bool isParameter)
void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
ArrayRef< SDDbgValue * > getSDDbgValues(const SDNode *Node) const
Definition: SelectionDAG.h:197
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(unsigned VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
MemSDNodeBitfields MemSDNodeBits
void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Return true if the type of the node type undefined.
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
EVT getValueType() const
Convenience function for get().getValueType().
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual SDValue EmitTargetCodeForMemset(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo) const
Emit target-specific code that performs a memset.
virtual SDValue EmitTargetCodeForMemmove(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memmove.
virtual SDValue EmitTargetCodeForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memcpy.
SDNodeFlags getFlags() const
Definition: SelectionDAG.h:384
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:228
Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
Definition: SelectionDAG.h:569
SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
bool isKnownNeverSNaN(SDValue Op, unsigned Depth=0) const
SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
Definition: SelectionDAG.h:491
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
void updateDivergence(SDNode *N)
SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), AAResults *AA=nullptr)
SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
SDNode * isConstantIntBuildVectorOrConstantInt(SDValue N) const
Test whether the given value is a constant int or similar node.
SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, unsigned VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
bool calculateDivergence(SDNode *N)
SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
std::optional< uint64_t > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
bool shouldOptForSize() const
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:495
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:453
SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
Definition: SelectionDAG.h:392
std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
Definition: SelectionDAG.h:549
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Definition: SelectionDAG.h:844
SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
void DeleteNode(SDNode *N)
Remove the specified node from the system.
SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:489
SDNode * isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), AAResults *AA=nullptr)
SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
Definition: SelectionDAG.h:677
SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
Definition: SelectionDAG.h:878
SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
SDValue getRegister(unsigned Reg, EVT VT)
void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
SDValue getBasicBlock(MachineBasicBlock *MBB)
SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
static const fltSemantics & EVTToAPFloatSemantics(EVT VT)
Returns an APFloat semantics tag appropriate for the given type.
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:490
std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
Definition: SelectionDAG.h:561
SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getValueType(EVT)
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
Definition: SelectionDAG.h:557
SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
bool isKnownNeverNaN(SDValue Op, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN.
SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:692
unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:484
SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
std::optional< uint64_t > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
Definition: SelectionDAG.h:861
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
SDValue getRegisterMask(const uint32_t *RegMask)
SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
SDValue getCondCode(ISD::CondCode Cond)
SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex, int64_t Size, int64_t Offset=-1)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the por...
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
std::optional< uint64_t > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVMContext * getContext() const
Definition: SelectionDAG.h:502
SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
Definition: SelectionDAG.h:578
SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=0, const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags)
Get the specified node if it's already available, or else return NULL.
void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
std::optional< bool > isBoolConstant(SDValue N, bool AllowTruncation=false) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:572
SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
Definition: SelectionDAG.h:894
std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
Definition: SelectionDAG.h:552
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
static bool isSplatMask(const int *Mask, EVT VT)
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:323
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:361
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:412
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:344
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:479
bool empty() const
Definition: SmallVector.h:95
size_t size() const
Definition: SmallVector.h:92
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:587
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:718
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:951
void reserve(size_type N)
Definition: SmallVector.h:677
iterator erase(const_iterator CI)
Definition: SmallVector.h:751
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:697
void push_back(const T &Elt)
Definition: SmallVector.h:427
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1210
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
virtual MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
virtual bool isCommutativeBinOp(unsigned Opcode) const
Returns true if the opcode is a commutative binary operation.
virtual ISD::NodeType getExtendForAtomicOps() const
Returns how the platform's atomic operations are extended (ZERO_EXTEND, SIGN_EXTEND,...
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const
Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type from this source type with ...
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal on this target.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
virtual bool hasVectorBlend() const
Return true if the target has a vector blend instruction.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual void computeKnownBitsForFrameIndex(int FIOp, KnownBits &Known, const MachineFunction &MF) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
This method can be implemented by targets that want to expose additional information about sign bits ...
virtual void verifyTargetSDNode(const SDNode *N) const
Check the given SDNode. Aborts if it is invalid.
virtual bool findOptimalMemOpLowering(std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual bool isKnownNeverNaNForTargetNode(SDValue Op, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const
If SNaN is false,.
virtual void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
virtual bool isSDNodeSourceOfDivergence(const SDNode *N, FunctionLoweringInfo *FLI, UniformityInfo *UA) const
virtual bool isSDNodeAlwaysUniform(const SDNode *N) const
virtual bool isSplatValueForTargetNode(SDValue Op, const APInt &DemandedElts, APInt &UndefElts, const SelectionDAG &DAG, unsigned Depth=0) const
Return true if vector Op has the same value across all DemandedElts, indicating any elements which ma...
virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const
Return true if folding a constant offset with the given GlobalAddress is legal.
virtual const Constant * getTargetConstantFromLoad(LoadSDNode *LD) const
This method returns the constant pool value that will be loaded by LD.
virtual bool isGAPlusOffset(SDNode *N, const GlobalValue *&GA, int64_t &Offset) const
Returns true (and the GlobalValue and the offset) if the node is a GlobalAddress + offset.
virtual bool isGuaranteedNotToBeUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, unsigned Depth) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
virtual bool canCreateUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, bool ConsiderFlags, unsigned Depth) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetOptions Options
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, XROS, or DriverKit).
Definition: Triple.h:558
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:345
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:261
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void set(Value *Val)
Definition: Value.h:882
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:185
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:232
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition: TypeSize.h:179
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:239
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
Definition: Mutex.h:28
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition: APInt.cpp:3100
const APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition: APInt.h:2222
APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition: APInt.cpp:3087
APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition: APInt.cpp:3077
const APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition: APInt.h:2217
APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition: APInt.cpp:3092
APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition: APInt.cpp:2978
APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition: APInt.cpp:3072
APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition: APInt.cpp:3082
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition: ISDOpcodes.h:40
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:779
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:243
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:752
@ TargetConstantPool
Definition: ISDOpcodes.h:174
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
Definition: ISDOpcodes.h:1214
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:490
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1319
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition: ISDOpcodes.h:44
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1382
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
Definition: ISDOpcodes.h:1330
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1415
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:511
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:257
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1312
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:573
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1103
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:743
@ TargetBlockAddress
Definition: ISDOpcodes.h:176
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1314
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1284
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1315
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:276
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition: ISDOpcodes.h:501
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:246
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1074
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:813
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:497
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:205
@ GlobalAddress
Definition: ISDOpcodes.h:78
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:1297
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:820
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:557
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1400
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:397
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1404
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:716
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:850
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1414
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:491
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:943
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1310
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:933
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:236
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1311
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:976
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1455
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1317
@ GlobalTLSAddress
Definition: ISDOpcodes.h:79
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
Definition: ISDOpcodes.h:1210
@ FrameIndex
Definition: ISDOpcodes.h:80
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1145
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:804
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:684
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:634
@ TargetExternalSymbol
Definition: ISDOpcodes.h:175
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1397
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:751
@ TargetJumpTable
Definition: ISDOpcodes.h:173
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition: ISDOpcodes.h:183
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1264
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1401
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition: ISDOpcodes.h:787
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:960
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1318
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1120
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:334
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1313
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:660
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:514
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:356
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:756
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1280
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:218
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1416
@ RegisterMask
Definition: ISDOpcodes.h:75
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition: ISDOpcodes.h:229
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition: ISDOpcodes.h:641
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition: ISDOpcodes.h:68
@ ATOMIC_LOAD_FMIN
Definition: ISDOpcodes.h:1320
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition: ISDOpcodes.h:215
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:330
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition: ISDOpcodes.h:170
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1409
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:673
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:734
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1309
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:614
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1308
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:587
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:1021
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition: ISDOpcodes.h:47
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:549
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition: ISDOpcodes.h:209
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:810
@ TargetConstantFP
Definition: ISDOpcodes.h:165
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:886
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:771
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1372
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1291
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1316
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:1008
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:366
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:338
@ TargetFrameIndex
Definition: ISDOpcodes.h:172
@ ConstantPool
Definition: ISDOpcodes.h:82
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition: ISDOpcodes.h:839
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:828
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:696
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1347
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:918
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:765
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:310
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
Definition: ISDOpcodes.h:1342
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1234
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1417
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:952
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1322
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1306
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:479
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1027
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1307
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:866
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition: ISDOpcodes.h:164
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:484
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:708
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:190
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1050
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1367
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition: ISDOpcodes.h:704
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:679
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1398
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:286
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition: ISDOpcodes.h:650
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition: ISDOpcodes.h:223
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition: ISDOpcodes.h:538
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition: ISDOpcodes.h:52
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition: ISDOpcodes.h:626
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1305
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:981
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:899
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition: ISDOpcodes.h:668
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:861
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:937
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Definition: ISDOpcodes.h:1446
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition: ISDOpcodes.h:885
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1405
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:816
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1189
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1383
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:793
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:61
@ ATOMIC_LOAD_UINC_WRAP
Definition: ISDOpcodes.h:1321
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:507
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:347
@ AssertZext
Definition: ISDOpcodes.h:62
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1055
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition: ISDOpcodes.h:691
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:320
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:198
@ TargetGlobalTLSAddress
Definition: ISDOpcodes.h:171
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition: ISDOpcodes.h:529
unsigned getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantSDNode predicate.
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
static const int FIRST_TARGET_MEMORY_OPCODE
FIRST_TARGET_MEMORY_OPCODE - Target-specific pre-isel operations which do not reference a specific me...
Definition: ISDOpcodes.h:1467
bool isExtOpcode(unsigned Opcode)
Definition: ISDOpcodes.h:1649
bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
Definition: ISDOpcodes.h:1636
std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
Definition: ISDOpcodes.h:1641
std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
Definition: ISDOpcodes.h:1540
bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
Definition: ISDOpcodes.h:1527
bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
Definition: ISDOpcodes.h:1578
bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
Definition: ISDOpcodes.h:1558
bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:893
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit.
Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:147
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
Definition: Mutex.h:69
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:353
@ Offset
Definition: DWP.cpp:480
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition: Analysis.cpp:233
SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1722
bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:255
bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition: Utils.cpp:1546
SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2406
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:307
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:317
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1514
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2073
bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
MaybeAlign getAlign(const Function &F, unsigned Index)
bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition: Utils.cpp:1528
bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1438
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729
bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 maximumNumber semantics.
Definition: APFloat.h:1475
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:340
bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:291
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1736
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54
bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 minimumNumber semantics.
Definition: APFloat.h:1461
@ Mul
Product of integers.
bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition: Analysis.cpp:535
DWARFExpression::Operation Op
ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1824
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition: Analysis.cpp:714
bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:573
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition: APFloat.h:1488
bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:382
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:760
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition: Metadata.h:780
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:777
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:281
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:244
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:257
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:254
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:258
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:283
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:282
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:279
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:256
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:280
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:270
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Align previous() const
Definition: Alignment.h:88
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition: ValueTypes.h:34
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:380
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:136
intptr_t getRawBits() const
Definition: ValueTypes.h:497
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition: ValueTypes.h:73
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:274
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:290
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:146
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:340
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:358
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:349
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:370
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:306
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64
bool isFixedLengthVector() const
Definition: ValueTypes.h:177
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:167
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:313
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:282
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:246
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:203
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:173
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:318
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:141
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:326
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:298
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:438
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:151
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:290
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:149
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:902
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:244
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:202
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:97
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:76
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:113
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:762
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:231
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:428
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1042
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:62
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:263
static std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
Definition: KnownBits.cpp:496
void makeNegative()
Make this value negative.
Definition: KnownBits.h:108
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:150
KnownBits byteSwap() const
Definition: KnownBits.h:456
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:278
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:82
KnownBits reverseBits() const
Definition: KnownBits.h:460
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:222
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:178
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:161
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:70
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition: KnownBits.h:310
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:370
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:100
static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
Definition: KnownBits.cpp:228
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:214
static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
Definition: KnownBits.cpp:782
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:300
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:169
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
Definition: KnownBits.cpp:137
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:185
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:134
static KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:247
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:215
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:894
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1059
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1002
static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition: KnownBits.cpp:51
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:103
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:946
static KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
Definition: KnownBits.cpp:777
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:315
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:94
static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition: KnownBits.cpp:44
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:269
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:208
static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
Definition: KnownBits.cpp:792
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:797
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition: KnownBits.h:156
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:550
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:285
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:196
static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
Definition: KnownBits.cpp:787
This class contains a discriminated union of information about pointers in memory operands,...
bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
These are IR-level optimization flags that may be propagated to SDNodes.
void intersectWith(const SDNodeFlags Flags)
Clear any flags in this flag set that aren't also set in Flags.
bool hasNonNeg() const
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
Definition: SelectionDAG.h:312
DAGUpdateListener *const Next
Definition: SelectionDAG.h:313
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)