LLVM 20.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
50#include "llvm/IR/Constant.h"
51#include "llvm/IR/Constants.h"
52#include "llvm/IR/DataLayout.h"
54#include "llvm/IR/DebugLoc.h"
56#include "llvm/IR/Function.h"
57#include "llvm/IR/GlobalValue.h"
58#include "llvm/IR/Metadata.h"
59#include "llvm/IR/Type.h"
63#include "llvm/Support/Debug.h"
67#include "llvm/Support/Mutex.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <set>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85using namespace llvm::SDPatternMatch;
86
87/// makeVTList - Return an instance of the SDVTList struct initialized with the
88/// specified members.
89static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
90 SDVTList Res = {VTs, NumVTs};
91 return Res;
92}
93
94// Default null implementations of the callbacks.
98
99void SelectionDAG::DAGNodeDeletedListener::anchor() {}
100void SelectionDAG::DAGNodeInsertedListener::anchor() {}
101
102#define DEBUG_TYPE "selectiondag"
103
104static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
105 cl::Hidden, cl::init(true),
106 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
107
108static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
109 cl::desc("Number limit for gluing ld/st of memcpy."),
110 cl::Hidden, cl::init(0));
111
113 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
114}
115
116//===----------------------------------------------------------------------===//
117// ConstantFPSDNode Class
118//===----------------------------------------------------------------------===//
119
120/// isExactlyValue - We don't rely on operator== working on double values, as
121/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
122/// As such, this method can be used to do an exact bit-for-bit comparison of
123/// two floating point values.
125 return getValueAPF().bitwiseIsEqual(V);
126}
127
129 const APFloat& Val) {
130 assert(VT.isFloatingPoint() && "Can only convert between FP types");
131
132 // convert modifies in place, so make a copy.
133 APFloat Val2 = APFloat(Val);
134 bool losesInfo;
136 &losesInfo);
137 return !losesInfo;
138}
139
140//===----------------------------------------------------------------------===//
141// ISD Namespace
142//===----------------------------------------------------------------------===//
143
144bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
145 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
146 unsigned EltSize =
147 N->getValueType(0).getVectorElementType().getSizeInBits();
148 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
149 SplatVal = Op0->getAPIntValue().trunc(EltSize);
150 return true;
151 }
152 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
153 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
154 return true;
155 }
156 }
157
158 auto *BV = dyn_cast<BuildVectorSDNode>(N);
159 if (!BV)
160 return false;
161
162 APInt SplatUndef;
163 unsigned SplatBitSize;
164 bool HasUndefs;
165 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
166 // Endianness does not matter here. We are checking for a splat given the
167 // element size of the vector, and if we find such a splat for little endian
168 // layout, then that should be valid also for big endian (as the full vector
169 // size is known to be a multiple of the element size).
170 const bool IsBigEndian = false;
171 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
172 EltSize, IsBigEndian) &&
173 EltSize == SplatBitSize;
174}
175
176// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
177// specializations of the more general isConstantSplatVector()?
178
179bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
180 // Look through a bit convert.
181 while (N->getOpcode() == ISD::BITCAST)
182 N = N->getOperand(0).getNode();
183
184 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
185 APInt SplatVal;
186 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
187 }
188
189 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
190
191 unsigned i = 0, e = N->getNumOperands();
192
193 // Skip over all of the undef values.
194 while (i != e && N->getOperand(i).isUndef())
195 ++i;
196
197 // Do not accept an all-undef vector.
198 if (i == e) return false;
199
200 // Do not accept build_vectors that aren't all constants or which have non-~0
201 // elements. We have to be a bit careful here, as the type of the constant
202 // may not be the same as the type of the vector elements due to type
203 // legalization (the elements are promoted to a legal type for the target and
204 // a vector of a type may be legal when the base element type is not).
205 // We only want to check enough bits to cover the vector elements, because
206 // we care if the resultant vector is all ones, not whether the individual
207 // constants are.
208 SDValue NotZero = N->getOperand(i);
209 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
210 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
211 if (CN->getAPIntValue().countr_one() < EltSize)
212 return false;
213 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
214 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
215 return false;
216 } else
217 return false;
218
219 // Okay, we have at least one ~0 value, check to see if the rest match or are
220 // undefs. Even with the above element type twiddling, this should be OK, as
221 // the same type legalization should have applied to all the elements.
222 for (++i; i != e; ++i)
223 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
224 return false;
225 return true;
226}
227
228bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
229 // Look through a bit convert.
230 while (N->getOpcode() == ISD::BITCAST)
231 N = N->getOperand(0).getNode();
232
233 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
234 APInt SplatVal;
235 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
236 }
237
238 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
239
240 bool IsAllUndef = true;
241 for (const SDValue &Op : N->op_values()) {
242 if (Op.isUndef())
243 continue;
244 IsAllUndef = false;
245 // Do not accept build_vectors that aren't all constants or which have non-0
246 // elements. We have to be a bit careful here, as the type of the constant
247 // may not be the same as the type of the vector elements due to type
248 // legalization (the elements are promoted to a legal type for the target
249 // and a vector of a type may be legal when the base element type is not).
250 // We only want to check enough bits to cover the vector elements, because
251 // we care if the resultant vector is all zeros, not whether the individual
252 // constants are.
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
255 if (CN->getAPIntValue().countr_zero() < EltSize)
256 return false;
257 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
258 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
259 return false;
260 } else
261 return false;
262 }
263
264 // Do not accept an all-undef vector.
265 if (IsAllUndef)
266 return false;
267 return true;
268}
269
271 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
272}
273
275 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
276}
277
279 if (N->getOpcode() != ISD::BUILD_VECTOR)
280 return false;
281
282 for (const SDValue &Op : N->op_values()) {
283 if (Op.isUndef())
284 continue;
285 if (!isa<ConstantSDNode>(Op))
286 return false;
287 }
288 return true;
289}
290
292 if (N->getOpcode() != ISD::BUILD_VECTOR)
293 return false;
294
295 for (const SDValue &Op : N->op_values()) {
296 if (Op.isUndef())
297 continue;
298 if (!isa<ConstantFPSDNode>(Op))
299 return false;
300 }
301 return true;
302}
303
304bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
305 bool Signed) {
306 assert(N->getValueType(0).isVector() && "Expected a vector!");
307
308 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
309 if (EltSize <= NewEltSize)
310 return false;
311
312 if (N->getOpcode() == ISD::ZERO_EXTEND) {
313 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
314 NewEltSize) &&
315 !Signed;
316 }
317 if (N->getOpcode() == ISD::SIGN_EXTEND) {
318 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
319 NewEltSize) &&
320 Signed;
321 }
322 if (N->getOpcode() != ISD::BUILD_VECTOR)
323 return false;
324
325 for (const SDValue &Op : N->op_values()) {
326 if (Op.isUndef())
327 continue;
328 if (!isa<ConstantSDNode>(Op))
329 return false;
330
331 APInt C = Op->getAsAPIntVal().trunc(EltSize);
332 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
333 return false;
334 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
335 return false;
336 }
337
338 return true;
339}
340
342 // Return false if the node has no operands.
343 // This is "logically inconsistent" with the definition of "all" but
344 // is probably the desired behavior.
345 if (N->getNumOperands() == 0)
346 return false;
347 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
348}
349
351 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
352}
353
354template <typename ConstNodeType>
356 std::function<bool(ConstNodeType *)> Match,
357 bool AllowUndefs) {
358 // FIXME: Add support for scalar UNDEF cases?
359 if (auto *C = dyn_cast<ConstNodeType>(Op))
360 return Match(C);
361
362 // FIXME: Add support for vector UNDEF cases?
363 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
364 ISD::SPLAT_VECTOR != Op.getOpcode())
365 return false;
366
367 EVT SVT = Op.getValueType().getScalarType();
368 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
369 if (AllowUndefs && Op.getOperand(i).isUndef()) {
370 if (!Match(nullptr))
371 return false;
372 continue;
373 }
374
375 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
376 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
377 return false;
378 }
379 return true;
380}
381// Build used template types.
382template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
383 SDValue, std::function<bool(ConstantSDNode *)>, bool);
384template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
385 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
386
388 SDValue LHS, SDValue RHS,
389 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
390 bool AllowUndefs, bool AllowTypeMismatch) {
391 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
392 return false;
393
394 // TODO: Add support for scalar UNDEF cases?
395 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
396 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
397 return Match(LHSCst, RHSCst);
398
399 // TODO: Add support for vector UNDEF cases?
400 if (LHS.getOpcode() != RHS.getOpcode() ||
401 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
402 LHS.getOpcode() != ISD::SPLAT_VECTOR))
403 return false;
404
405 EVT SVT = LHS.getValueType().getScalarType();
406 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
407 SDValue LHSOp = LHS.getOperand(i);
408 SDValue RHSOp = RHS.getOperand(i);
409 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
410 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
411 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
412 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
413 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
414 return false;
415 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
416 LHSOp.getValueType() != RHSOp.getValueType()))
417 return false;
418 if (!Match(LHSCst, RHSCst))
419 return false;
420 }
421 return true;
422}
423
425 switch (VecReduceOpcode) {
426 default:
427 llvm_unreachable("Expected VECREDUCE opcode");
430 case ISD::VP_REDUCE_FADD:
431 case ISD::VP_REDUCE_SEQ_FADD:
432 return ISD::FADD;
435 case ISD::VP_REDUCE_FMUL:
436 case ISD::VP_REDUCE_SEQ_FMUL:
437 return ISD::FMUL;
439 case ISD::VP_REDUCE_ADD:
440 return ISD::ADD;
442 case ISD::VP_REDUCE_MUL:
443 return ISD::MUL;
445 case ISD::VP_REDUCE_AND:
446 return ISD::AND;
448 case ISD::VP_REDUCE_OR:
449 return ISD::OR;
451 case ISD::VP_REDUCE_XOR:
452 return ISD::XOR;
454 case ISD::VP_REDUCE_SMAX:
455 return ISD::SMAX;
457 case ISD::VP_REDUCE_SMIN:
458 return ISD::SMIN;
460 case ISD::VP_REDUCE_UMAX:
461 return ISD::UMAX;
463 case ISD::VP_REDUCE_UMIN:
464 return ISD::UMIN;
466 case ISD::VP_REDUCE_FMAX:
467 return ISD::FMAXNUM;
469 case ISD::VP_REDUCE_FMIN:
470 return ISD::FMINNUM;
472 case ISD::VP_REDUCE_FMAXIMUM:
473 return ISD::FMAXIMUM;
475 case ISD::VP_REDUCE_FMINIMUM:
476 return ISD::FMINIMUM;
477 }
478}
479
480bool ISD::isVPOpcode(unsigned Opcode) {
481 switch (Opcode) {
482 default:
483 return false;
484#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
485 case ISD::VPSD: \
486 return true;
487#include "llvm/IR/VPIntrinsics.def"
488 }
489}
490
491bool ISD::isVPBinaryOp(unsigned Opcode) {
492 switch (Opcode) {
493 default:
494 break;
495#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
496#define VP_PROPERTY_BINARYOP return true;
497#define END_REGISTER_VP_SDNODE(VPSD) break;
498#include "llvm/IR/VPIntrinsics.def"
499 }
500 return false;
501}
502
503bool ISD::isVPReduction(unsigned Opcode) {
504 switch (Opcode) {
505 default:
506 break;
507#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
508#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
509#define END_REGISTER_VP_SDNODE(VPSD) break;
510#include "llvm/IR/VPIntrinsics.def"
511 }
512 return false;
513}
514
515/// The operand position of the vector mask.
516std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
517 switch (Opcode) {
518 default:
519 return std::nullopt;
520#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
521 case ISD::VPSD: \
522 return MASKPOS;
523#include "llvm/IR/VPIntrinsics.def"
524 }
525}
526
527/// The operand position of the explicit vector length parameter.
528std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
529 switch (Opcode) {
530 default:
531 return std::nullopt;
532#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
533 case ISD::VPSD: \
534 return EVLPOS;
535#include "llvm/IR/VPIntrinsics.def"
536 }
537}
538
539std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
540 bool hasFPExcept) {
541 // FIXME: Return strict opcodes in case of fp exceptions.
542 switch (VPOpcode) {
543 default:
544 return std::nullopt;
545#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
546#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
547#define END_REGISTER_VP_SDNODE(VPOPC) break;
548#include "llvm/IR/VPIntrinsics.def"
549 }
550 return std::nullopt;
551}
552
553std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
554 switch (Opcode) {
555 default:
556 return std::nullopt;
557#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
558#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
559#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
560#include "llvm/IR/VPIntrinsics.def"
561 }
562}
563
565 switch (ExtType) {
566 case ISD::EXTLOAD:
567 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
568 case ISD::SEXTLOAD:
569 return ISD::SIGN_EXTEND;
570 case ISD::ZEXTLOAD:
571 return ISD::ZERO_EXTEND;
572 default:
573 break;
574 }
575
576 llvm_unreachable("Invalid LoadExtType");
577}
578
580 // To perform this operation, we just need to swap the L and G bits of the
581 // operation.
582 unsigned OldL = (Operation >> 2) & 1;
583 unsigned OldG = (Operation >> 1) & 1;
584 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
585 (OldL << 1) | // New G bit
586 (OldG << 2)); // New L bit.
587}
588
590 unsigned Operation = Op;
591 if (isIntegerLike)
592 Operation ^= 7; // Flip L, G, E bits, but not U.
593 else
594 Operation ^= 15; // Flip all of the condition bits.
595
597 Operation &= ~8; // Don't let N and U bits get set.
598
599 return ISD::CondCode(Operation);
600}
601
603 return getSetCCInverseImpl(Op, Type.isInteger());
604}
605
607 bool isIntegerLike) {
608 return getSetCCInverseImpl(Op, isIntegerLike);
609}
610
611/// For an integer comparison, return 1 if the comparison is a signed operation
612/// and 2 if the result is an unsigned comparison. Return zero if the operation
613/// does not depend on the sign of the input (setne and seteq).
614static int isSignedOp(ISD::CondCode Opcode) {
615 switch (Opcode) {
616 default: llvm_unreachable("Illegal integer setcc operation!");
617 case ISD::SETEQ:
618 case ISD::SETNE: return 0;
619 case ISD::SETLT:
620 case ISD::SETLE:
621 case ISD::SETGT:
622 case ISD::SETGE: return 1;
623 case ISD::SETULT:
624 case ISD::SETULE:
625 case ISD::SETUGT:
626 case ISD::SETUGE: return 2;
627 }
628}
629
631 EVT Type) {
632 bool IsInteger = Type.isInteger();
633 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
634 // Cannot fold a signed integer setcc with an unsigned integer setcc.
635 return ISD::SETCC_INVALID;
636
637 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
638
639 // If the N and U bits get set, then the resultant comparison DOES suddenly
640 // care about orderedness, and it is true when ordered.
641 if (Op > ISD::SETTRUE2)
642 Op &= ~16; // Clear the U bit if the N bit is set.
643
644 // Canonicalize illegal integer setcc's.
645 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
646 Op = ISD::SETNE;
647
648 return ISD::CondCode(Op);
649}
650
652 EVT Type) {
653 bool IsInteger = Type.isInteger();
654 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
655 // Cannot fold a signed setcc with an unsigned setcc.
656 return ISD::SETCC_INVALID;
657
658 // Combine all of the condition bits.
659 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
660
661 // Canonicalize illegal integer setcc's.
662 if (IsInteger) {
663 switch (Result) {
664 default: break;
665 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
666 case ISD::SETOEQ: // SETEQ & SETU[LG]E
667 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
668 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
669 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
670 }
671 }
672
673 return Result;
674}
675
676//===----------------------------------------------------------------------===//
677// SDNode Profile Support
678//===----------------------------------------------------------------------===//
679
680/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
681static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
682 ID.AddInteger(OpC);
683}
684
685/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
686/// solely with their pointer.
688 ID.AddPointer(VTList.VTs);
689}
690
691/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
693 ArrayRef<SDValue> Ops) {
694 for (const auto &Op : Ops) {
695 ID.AddPointer(Op.getNode());
696 ID.AddInteger(Op.getResNo());
697 }
698}
699
700/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
702 ArrayRef<SDUse> Ops) {
703 for (const auto &Op : Ops) {
704 ID.AddPointer(Op.getNode());
705 ID.AddInteger(Op.getResNo());
706 }
707}
708
709static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
710 SDVTList VTList, ArrayRef<SDValue> OpList) {
711 AddNodeIDOpcode(ID, OpC);
712 AddNodeIDValueTypes(ID, VTList);
713 AddNodeIDOperands(ID, OpList);
714}
715
716/// If this is an SDNode with special info, add this info to the NodeID data.
718 switch (N->getOpcode()) {
721 case ISD::MCSymbol:
722 llvm_unreachable("Should only be used on nodes with operands");
723 default: break; // Normal nodes don't need extra info.
725 case ISD::Constant: {
726 const ConstantSDNode *C = cast<ConstantSDNode>(N);
727 ID.AddPointer(C->getConstantIntValue());
728 ID.AddBoolean(C->isOpaque());
729 break;
730 }
732 case ISD::ConstantFP:
733 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
734 break;
739 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
740 ID.AddPointer(GA->getGlobal());
741 ID.AddInteger(GA->getOffset());
742 ID.AddInteger(GA->getTargetFlags());
743 break;
744 }
745 case ISD::BasicBlock:
746 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
747 break;
748 case ISD::Register:
749 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
750 break;
752 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
753 break;
754 case ISD::SRCVALUE:
755 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
756 break;
757 case ISD::FrameIndex:
759 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
760 break;
763 if (cast<LifetimeSDNode>(N)->hasOffset()) {
764 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
765 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
766 }
767 break;
769 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
770 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
771 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
772 break;
773 case ISD::JumpTable:
775 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
776 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
777 break;
780 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
781 ID.AddInteger(CP->getAlign().value());
782 ID.AddInteger(CP->getOffset());
783 if (CP->isMachineConstantPoolEntry())
784 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
785 else
786 ID.AddPointer(CP->getConstVal());
787 ID.AddInteger(CP->getTargetFlags());
788 break;
789 }
790 case ISD::TargetIndex: {
791 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
792 ID.AddInteger(TI->getIndex());
793 ID.AddInteger(TI->getOffset());
794 ID.AddInteger(TI->getTargetFlags());
795 break;
796 }
797 case ISD::LOAD: {
798 const LoadSDNode *LD = cast<LoadSDNode>(N);
799 ID.AddInteger(LD->getMemoryVT().getRawBits());
800 ID.AddInteger(LD->getRawSubclassData());
801 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
802 ID.AddInteger(LD->getMemOperand()->getFlags());
803 break;
804 }
805 case ISD::STORE: {
806 const StoreSDNode *ST = cast<StoreSDNode>(N);
807 ID.AddInteger(ST->getMemoryVT().getRawBits());
808 ID.AddInteger(ST->getRawSubclassData());
809 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
810 ID.AddInteger(ST->getMemOperand()->getFlags());
811 break;
812 }
813 case ISD::VP_LOAD: {
814 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
815 ID.AddInteger(ELD->getMemoryVT().getRawBits());
816 ID.AddInteger(ELD->getRawSubclassData());
817 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
818 ID.AddInteger(ELD->getMemOperand()->getFlags());
819 break;
820 }
821 case ISD::VP_STORE: {
822 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
823 ID.AddInteger(EST->getMemoryVT().getRawBits());
824 ID.AddInteger(EST->getRawSubclassData());
825 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
826 ID.AddInteger(EST->getMemOperand()->getFlags());
827 break;
828 }
829 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
830 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
831 ID.AddInteger(SLD->getMemoryVT().getRawBits());
832 ID.AddInteger(SLD->getRawSubclassData());
833 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
834 break;
835 }
836 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
837 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
838 ID.AddInteger(SST->getMemoryVT().getRawBits());
839 ID.AddInteger(SST->getRawSubclassData());
840 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
841 break;
842 }
843 case ISD::VP_GATHER: {
844 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
845 ID.AddInteger(EG->getMemoryVT().getRawBits());
846 ID.AddInteger(EG->getRawSubclassData());
847 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
848 ID.AddInteger(EG->getMemOperand()->getFlags());
849 break;
850 }
851 case ISD::VP_SCATTER: {
852 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
853 ID.AddInteger(ES->getMemoryVT().getRawBits());
854 ID.AddInteger(ES->getRawSubclassData());
855 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
856 ID.AddInteger(ES->getMemOperand()->getFlags());
857 break;
858 }
859 case ISD::MLOAD: {
860 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
861 ID.AddInteger(MLD->getMemoryVT().getRawBits());
862 ID.AddInteger(MLD->getRawSubclassData());
863 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
864 ID.AddInteger(MLD->getMemOperand()->getFlags());
865 break;
866 }
867 case ISD::MSTORE: {
868 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
869 ID.AddInteger(MST->getMemoryVT().getRawBits());
870 ID.AddInteger(MST->getRawSubclassData());
871 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
872 ID.AddInteger(MST->getMemOperand()->getFlags());
873 break;
874 }
875 case ISD::MGATHER: {
876 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
877 ID.AddInteger(MG->getMemoryVT().getRawBits());
878 ID.AddInteger(MG->getRawSubclassData());
879 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
880 ID.AddInteger(MG->getMemOperand()->getFlags());
881 break;
882 }
883 case ISD::MSCATTER: {
884 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
885 ID.AddInteger(MS->getMemoryVT().getRawBits());
886 ID.AddInteger(MS->getRawSubclassData());
887 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
888 ID.AddInteger(MS->getMemOperand()->getFlags());
889 break;
890 }
893 case ISD::ATOMIC_SWAP:
905 case ISD::ATOMIC_LOAD:
906 case ISD::ATOMIC_STORE: {
907 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
908 ID.AddInteger(AT->getMemoryVT().getRawBits());
909 ID.AddInteger(AT->getRawSubclassData());
910 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
911 ID.AddInteger(AT->getMemOperand()->getFlags());
912 break;
913 }
914 case ISD::VECTOR_SHUFFLE: {
915 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
916 for (int M : Mask)
917 ID.AddInteger(M);
918 break;
919 }
921 case ISD::BlockAddress: {
922 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
923 ID.AddPointer(BA->getBlockAddress());
924 ID.AddInteger(BA->getOffset());
925 ID.AddInteger(BA->getTargetFlags());
926 break;
927 }
928 case ISD::AssertAlign:
929 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
930 break;
931 case ISD::PREFETCH:
934 // Handled by MemIntrinsicSDNode check after the switch.
935 break;
936 } // end switch (N->getOpcode())
937
938 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
939 // to check.
940 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
941 ID.AddInteger(MN->getRawSubclassData());
942 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
943 ID.AddInteger(MN->getMemOperand()->getFlags());
944 ID.AddInteger(MN->getMemoryVT().getRawBits());
945 }
946}
947
948/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
949/// data.
950static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
951 AddNodeIDOpcode(ID, N->getOpcode());
952 // Add the return value info.
953 AddNodeIDValueTypes(ID, N->getVTList());
954 // Add the operand info.
955 AddNodeIDOperands(ID, N->ops());
956
957 // Handle SDNode leafs with special info.
959}
960
961//===----------------------------------------------------------------------===//
962// SelectionDAG Class
963//===----------------------------------------------------------------------===//
964
965/// doNotCSE - Return true if CSE should not be performed for this node.
966static bool doNotCSE(SDNode *N) {
967 if (N->getValueType(0) == MVT::Glue)
968 return true; // Never CSE anything that produces a glue result.
969
970 switch (N->getOpcode()) {
971 default: break;
972 case ISD::HANDLENODE:
973 case ISD::EH_LABEL:
974 return true; // Never CSE these nodes.
975 }
976
977 // Check that remaining values produced are not flags.
978 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
979 if (N->getValueType(i) == MVT::Glue)
980 return true; // Never CSE anything that produces a glue result.
981
982 return false;
983}
984
985/// RemoveDeadNodes - This method deletes all unreachable nodes in the
986/// SelectionDAG.
988 // Create a dummy node (which is not added to allnodes), that adds a reference
989 // to the root node, preventing it from being deleted.
990 HandleSDNode Dummy(getRoot());
991
993
994 // Add all obviously-dead nodes to the DeadNodes worklist.
995 for (SDNode &Node : allnodes())
996 if (Node.use_empty())
997 DeadNodes.push_back(&Node);
998
999 RemoveDeadNodes(DeadNodes);
1000
1001 // If the root changed (e.g. it was a dead load, update the root).
1002 setRoot(Dummy.getValue());
1003}
1004
1005/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1006/// given list, and any nodes that become unreachable as a result.
1008
1009 // Process the worklist, deleting the nodes and adding their uses to the
1010 // worklist.
1011 while (!DeadNodes.empty()) {
1012 SDNode *N = DeadNodes.pop_back_val();
1013 // Skip to next node if we've already managed to delete the node. This could
1014 // happen if replacing a node causes a node previously added to the node to
1015 // be deleted.
1016 if (N->getOpcode() == ISD::DELETED_NODE)
1017 continue;
1018
1019 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1020 DUL->NodeDeleted(N, nullptr);
1021
1022 // Take the node out of the appropriate CSE map.
1023 RemoveNodeFromCSEMaps(N);
1024
1025 // Next, brutally remove the operand list. This is safe to do, as there are
1026 // no cycles in the graph.
1027 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1028 SDUse &Use = *I++;
1029 SDNode *Operand = Use.getNode();
1030 Use.set(SDValue());
1031
1032 // Now that we removed this operand, see if there are no uses of it left.
1033 if (Operand->use_empty())
1034 DeadNodes.push_back(Operand);
1035 }
1036
1037 DeallocateNode(N);
1038 }
1039}
1040
1042 SmallVector<SDNode*, 16> DeadNodes(1, N);
1043
1044 // Create a dummy node that adds a reference to the root node, preventing
1045 // it from being deleted. (This matters if the root is an operand of the
1046 // dead node.)
1047 HandleSDNode Dummy(getRoot());
1048
1049 RemoveDeadNodes(DeadNodes);
1050}
1051
1053 // First take this out of the appropriate CSE map.
1054 RemoveNodeFromCSEMaps(N);
1055
1056 // Finally, remove uses due to operands of this node, remove from the
1057 // AllNodes list, and delete the node.
1058 DeleteNodeNotInCSEMaps(N);
1059}
1060
1061void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1062 assert(N->getIterator() != AllNodes.begin() &&
1063 "Cannot delete the entry node!");
1064 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1065
1066 // Drop all of the operands and decrement used node's use counts.
1067 N->DropOperands();
1068
1069 DeallocateNode(N);
1070}
1071
1072void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1073 assert(!(V->isVariadic() && isParameter));
1074 if (isParameter)
1075 ByvalParmDbgValues.push_back(V);
1076 else
1077 DbgValues.push_back(V);
1078 for (const SDNode *Node : V->getSDNodes())
1079 if (Node)
1080 DbgValMap[Node].push_back(V);
1081}
1082
1083void SDDbgInfo::erase(const SDNode *Node) {
1084 DbgValMapType::iterator I = DbgValMap.find(Node);
1085 if (I == DbgValMap.end())
1086 return;
1087 for (auto &Val: I->second)
1088 Val->setIsInvalidated();
1089 DbgValMap.erase(I);
1090}
1091
1092void SelectionDAG::DeallocateNode(SDNode *N) {
1093 // If we have operands, deallocate them.
1094 removeOperands(N);
1095
1096 NodeAllocator.Deallocate(AllNodes.remove(N));
1097
1098 // Set the opcode to DELETED_NODE to help catch bugs when node
1099 // memory is reallocated.
1100 // FIXME: There are places in SDag that have grown a dependency on the opcode
1101 // value in the released node.
1102 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1103 N->NodeType = ISD::DELETED_NODE;
1104
1105 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1106 // them and forget about that node.
1107 DbgInfo->erase(N);
1108
1109 // Invalidate extra info.
1110 SDEI.erase(N);
1111}
1112
1113#ifndef NDEBUG
1114/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1115static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1116 switch (N->getOpcode()) {
1117 default:
1118 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1119 TLI->verifyTargetSDNode(N);
1120 break;
1121 case ISD::BUILD_PAIR: {
1122 EVT VT = N->getValueType(0);
1123 assert(N->getNumValues() == 1 && "Too many results!");
1124 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1125 "Wrong return type!");
1126 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1127 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1128 "Mismatched operand types!");
1129 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1130 "Wrong operand type!");
1131 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1132 "Wrong return type size");
1133 break;
1134 }
1135 case ISD::BUILD_VECTOR: {
1136 assert(N->getNumValues() == 1 && "Too many results!");
1137 assert(N->getValueType(0).isVector() && "Wrong return type!");
1138 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1139 "Wrong number of operands!");
1140 EVT EltVT = N->getValueType(0).getVectorElementType();
1141 for (const SDUse &Op : N->ops()) {
1142 assert((Op.getValueType() == EltVT ||
1143 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1144 EltVT.bitsLE(Op.getValueType()))) &&
1145 "Wrong operand type!");
1146 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1147 "Operands must all have the same type");
1148 }
1149 break;
1150 }
1151 }
1152}
1153#endif // NDEBUG
1154
1155/// Insert a newly allocated node into the DAG.
1156///
1157/// Handles insertion into the all nodes list and CSE map, as well as
1158/// verification and other common operations when a new node is allocated.
1159void SelectionDAG::InsertNode(SDNode *N) {
1160 AllNodes.push_back(N);
1161#ifndef NDEBUG
1162 N->PersistentId = NextPersistentId++;
1163 VerifySDNode(N, TLI);
1164#endif
1165 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1166 DUL->NodeInserted(N);
1167}
1168
1169/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1170/// correspond to it. This is useful when we're about to delete or repurpose
1171/// the node. We don't want future request for structurally identical nodes
1172/// to return N anymore.
1173bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1174 bool Erased = false;
1175 switch (N->getOpcode()) {
1176 case ISD::HANDLENODE: return false; // noop.
1177 case ISD::CONDCODE:
1178 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1179 "Cond code doesn't exist!");
1180 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1181 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1182 break;
1184 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1185 break;
1187 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1188 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1189 ESN->getSymbol(), ESN->getTargetFlags()));
1190 break;
1191 }
1192 case ISD::MCSymbol: {
1193 auto *MCSN = cast<MCSymbolSDNode>(N);
1194 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1195 break;
1196 }
1197 case ISD::VALUETYPE: {
1198 EVT VT = cast<VTSDNode>(N)->getVT();
1199 if (VT.isExtended()) {
1200 Erased = ExtendedValueTypeNodes.erase(VT);
1201 } else {
1202 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1203 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1204 }
1205 break;
1206 }
1207 default:
1208 // Remove it from the CSE Map.
1209 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1210 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1211 Erased = CSEMap.RemoveNode(N);
1212 break;
1213 }
1214#ifndef NDEBUG
1215 // Verify that the node was actually in one of the CSE maps, unless it has a
1216 // glue result (which cannot be CSE'd) or is one of the special cases that are
1217 // not subject to CSE.
1218 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1219 !N->isMachineOpcode() && !doNotCSE(N)) {
1220 N->dump(this);
1221 dbgs() << "\n";
1222 llvm_unreachable("Node is not in map!");
1223 }
1224#endif
1225 return Erased;
1226}
1227
1228/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1229/// maps and modified in place. Add it back to the CSE maps, unless an identical
1230/// node already exists, in which case transfer all its users to the existing
1231/// node. This transfer can potentially trigger recursive merging.
1232void
1233SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1234 // For node types that aren't CSE'd, just act as if no identical node
1235 // already exists.
1236 if (!doNotCSE(N)) {
1237 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1238 if (Existing != N) {
1239 // If there was already an existing matching node, use ReplaceAllUsesWith
1240 // to replace the dead one with the existing one. This can cause
1241 // recursive merging of other unrelated nodes down the line.
1242 Existing->intersectFlagsWith(N->getFlags());
1243 ReplaceAllUsesWith(N, Existing);
1244
1245 // N is now dead. Inform the listeners and delete it.
1246 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1247 DUL->NodeDeleted(N, Existing);
1248 DeleteNodeNotInCSEMaps(N);
1249 return;
1250 }
1251 }
1252
1253 // If the node doesn't already exist, we updated it. Inform listeners.
1254 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1255 DUL->NodeUpdated(N);
1256}
1257
1258/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1259/// were replaced with those specified. If this node is never memoized,
1260/// return null, otherwise return a pointer to the slot it would take. If a
1261/// node already exists with these operands, the slot will be non-null.
1262SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1263 void *&InsertPos) {
1264 if (doNotCSE(N))
1265 return nullptr;
1266
1267 SDValue Ops[] = { Op };
1269 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1271 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1272 if (Node)
1273 Node->intersectFlagsWith(N->getFlags());
1274 return Node;
1275}
1276
1277/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1278/// were replaced with those specified. If this node is never memoized,
1279/// return null, otherwise return a pointer to the slot it would take. If a
1280/// node already exists with these operands, the slot will be non-null.
1281SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1282 SDValue Op1, SDValue Op2,
1283 void *&InsertPos) {
1284 if (doNotCSE(N))
1285 return nullptr;
1286
1287 SDValue Ops[] = { Op1, Op2 };
1289 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1291 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1292 if (Node)
1293 Node->intersectFlagsWith(N->getFlags());
1294 return Node;
1295}
1296
1297/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1298/// were replaced with those specified. If this node is never memoized,
1299/// return null, otherwise return a pointer to the slot it would take. If a
1300/// node already exists with these operands, the slot will be non-null.
1301SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1302 void *&InsertPos) {
1303 if (doNotCSE(N))
1304 return nullptr;
1305
1307 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1309 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1310 if (Node)
1311 Node->intersectFlagsWith(N->getFlags());
1312 return Node;
1313}
1314
1316 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1317 : VT.getTypeForEVT(*getContext());
1318
1319 return getDataLayout().getABITypeAlign(Ty);
1320}
1321
1322// EntryNode could meaningfully have debug info if we can find it...
1324 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1325 getVTList(MVT::Other, MVT::Glue)),
1326 Root(getEntryNode()) {
1327 InsertNode(&EntryNode);
1328 DbgInfo = new SDDbgInfo();
1329}
1330
1332 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1333 const TargetLibraryInfo *LibraryInfo,
1334 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1336 FunctionVarLocs const *VarLocs) {
1337 MF = &NewMF;
1338 SDAGISelPass = PassPtr;
1339 ORE = &NewORE;
1342 LibInfo = LibraryInfo;
1343 Context = &MF->getFunction().getContext();
1344 UA = NewUA;
1345 PSI = PSIin;
1346 BFI = BFIin;
1347 MMI = &MMIin;
1348 FnVarLocs = VarLocs;
1349}
1350
1352 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1353 allnodes_clear();
1354 OperandRecycler.clear(OperandAllocator);
1355 delete DbgInfo;
1356}
1357
1359 return MF->getFunction().hasOptSize() ||
1361}
1362
1363void SelectionDAG::allnodes_clear() {
1364 assert(&*AllNodes.begin() == &EntryNode);
1365 AllNodes.remove(AllNodes.begin());
1366 while (!AllNodes.empty())
1367 DeallocateNode(&AllNodes.front());
1368#ifndef NDEBUG
1369 NextPersistentId = 0;
1370#endif
1371}
1372
1373SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1374 void *&InsertPos) {
1375 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1376 if (N) {
1377 switch (N->getOpcode()) {
1378 default: break;
1379 case ISD::Constant:
1380 case ISD::ConstantFP:
1381 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1382 "debug location. Use another overload.");
1383 }
1384 }
1385 return N;
1386}
1387
1388SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1389 const SDLoc &DL, void *&InsertPos) {
1390 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1391 if (N) {
1392 switch (N->getOpcode()) {
1393 case ISD::Constant:
1394 case ISD::ConstantFP:
1395 // Erase debug location from the node if the node is used at several
1396 // different places. Do not propagate one location to all uses as it
1397 // will cause a worse single stepping debugging experience.
1398 if (N->getDebugLoc() != DL.getDebugLoc())
1399 N->setDebugLoc(DebugLoc());
1400 break;
1401 default:
1402 // When the node's point of use is located earlier in the instruction
1403 // sequence than its prior point of use, update its debug info to the
1404 // earlier location.
1405 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1406 N->setDebugLoc(DL.getDebugLoc());
1407 break;
1408 }
1409 }
1410 return N;
1411}
1412
1414 allnodes_clear();
1415 OperandRecycler.clear(OperandAllocator);
1416 OperandAllocator.Reset();
1417 CSEMap.clear();
1418
1419 ExtendedValueTypeNodes.clear();
1420 ExternalSymbols.clear();
1421 TargetExternalSymbols.clear();
1422 MCSymbols.clear();
1423 SDEI.clear();
1424 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1425 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1426
1427 EntryNode.UseList = nullptr;
1428 InsertNode(&EntryNode);
1429 Root = getEntryNode();
1430 DbgInfo->clear();
1431}
1432
1434 return VT.bitsGT(Op.getValueType())
1435 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1436 : getNode(ISD::FP_ROUND, DL, VT, Op,
1437 getIntPtrConstant(0, DL, /*isTarget=*/true));
1438}
1439
1440std::pair<SDValue, SDValue>
1442 const SDLoc &DL, EVT VT) {
1443 assert(!VT.bitsEq(Op.getValueType()) &&
1444 "Strict no-op FP extend/round not allowed.");
1445 SDValue Res =
1446 VT.bitsGT(Op.getValueType())
1447 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1448 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1449 {Chain, Op, getIntPtrConstant(0, DL)});
1450
1451 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1452}
1453
1455 return VT.bitsGT(Op.getValueType()) ?
1456 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1457 getNode(ISD::TRUNCATE, DL, VT, Op);
1458}
1459
1461 return VT.bitsGT(Op.getValueType()) ?
1462 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1463 getNode(ISD::TRUNCATE, DL, VT, Op);
1464}
1465
1467 return VT.bitsGT(Op.getValueType()) ?
1468 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1469 getNode(ISD::TRUNCATE, DL, VT, Op);
1470}
1471
1473 EVT VT) {
1474 assert(!VT.isVector());
1475 auto Type = Op.getValueType();
1476 SDValue DestOp;
1477 if (Type == VT)
1478 return Op;
1479 auto Size = Op.getValueSizeInBits();
1480 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1481 if (DestOp.getValueType() == VT)
1482 return DestOp;
1483
1484 return getAnyExtOrTrunc(DestOp, DL, VT);
1485}
1486
1488 EVT VT) {
1489 assert(!VT.isVector());
1490 auto Type = Op.getValueType();
1491 SDValue DestOp;
1492 if (Type == VT)
1493 return Op;
1494 auto Size = Op.getValueSizeInBits();
1495 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1496 if (DestOp.getValueType() == VT)
1497 return DestOp;
1498
1499 return getSExtOrTrunc(DestOp, DL, VT);
1500}
1501
1503 EVT VT) {
1504 assert(!VT.isVector());
1505 auto Type = Op.getValueType();
1506 SDValue DestOp;
1507 if (Type == VT)
1508 return Op;
1509 auto Size = Op.getValueSizeInBits();
1510 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1511 if (DestOp.getValueType() == VT)
1512 return DestOp;
1513
1514 return getZExtOrTrunc(DestOp, DL, VT);
1515}
1516
1518 EVT OpVT) {
1519 if (VT.bitsLE(Op.getValueType()))
1520 return getNode(ISD::TRUNCATE, SL, VT, Op);
1521
1523 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1524}
1525
1527 EVT OpVT = Op.getValueType();
1528 assert(VT.isInteger() && OpVT.isInteger() &&
1529 "Cannot getZeroExtendInReg FP types");
1530 assert(VT.isVector() == OpVT.isVector() &&
1531 "getZeroExtendInReg type should be vector iff the operand "
1532 "type is vector!");
1533 assert((!VT.isVector() ||
1535 "Vector element counts must match in getZeroExtendInReg");
1536 assert(VT.bitsLE(OpVT) && "Not extending!");
1537 if (OpVT == VT)
1538 return Op;
1540 VT.getScalarSizeInBits());
1541 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1542}
1543
1545 SDValue EVL, const SDLoc &DL,
1546 EVT VT) {
1547 EVT OpVT = Op.getValueType();
1548 assert(VT.isInteger() && OpVT.isInteger() &&
1549 "Cannot getVPZeroExtendInReg FP types");
1550 assert(VT.isVector() && OpVT.isVector() &&
1551 "getVPZeroExtendInReg type and operand type should be vector!");
1553 "Vector element counts must match in getZeroExtendInReg");
1554 assert(VT.bitsLE(OpVT) && "Not extending!");
1555 if (OpVT == VT)
1556 return Op;
1558 VT.getScalarSizeInBits());
1559 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1560 EVL);
1561}
1562
1564 // Only unsigned pointer semantics are supported right now. In the future this
1565 // might delegate to TLI to check pointer signedness.
1566 return getZExtOrTrunc(Op, DL, VT);
1567}
1568
1570 // Only unsigned pointer semantics are supported right now. In the future this
1571 // might delegate to TLI to check pointer signedness.
1572 return getZeroExtendInReg(Op, DL, VT);
1573}
1574
1576 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1577}
1578
1579/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1581 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1582}
1583
1585 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1586 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1587}
1588
1590 SDValue Mask, SDValue EVL, EVT VT) {
1591 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1592 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1593}
1594
1596 SDValue Mask, SDValue EVL) {
1597 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1598}
1599
1601 SDValue Mask, SDValue EVL) {
1602 if (VT.bitsGT(Op.getValueType()))
1603 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1604 if (VT.bitsLT(Op.getValueType()))
1605 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1606 return Op;
1607}
1608
1610 EVT OpVT) {
1611 if (!V)
1612 return getConstant(0, DL, VT);
1613
1614 switch (TLI->getBooleanContents(OpVT)) {
1617 return getConstant(1, DL, VT);
1619 return getAllOnesConstant(DL, VT);
1620 }
1621 llvm_unreachable("Unexpected boolean content enum!");
1622}
1623
1625 bool isT, bool isO) {
1626 EVT EltVT = VT.getScalarType();
1627 assert((EltVT.getSizeInBits() >= 64 ||
1628 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1629 "getConstant with a uint64_t value that doesn't fit in the type!");
1630 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1631}
1632
1634 bool isT, bool isO) {
1635 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1636}
1637
1639 EVT VT, bool isT, bool isO) {
1640 assert(VT.isInteger() && "Cannot create FP integer constant!");
1641
1642 EVT EltVT = VT.getScalarType();
1643 const ConstantInt *Elt = &Val;
1644
1645 // In some cases the vector type is legal but the element type is illegal and
1646 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1647 // inserted value (the type does not need to match the vector element type).
1648 // Any extra bits introduced will be truncated away.
1649 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1651 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1652 APInt NewVal;
1653 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1654 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1655 else
1656 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1657 Elt = ConstantInt::get(*getContext(), NewVal);
1658 }
1659 // In other cases the element type is illegal and needs to be expanded, for
1660 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1661 // the value into n parts and use a vector type with n-times the elements.
1662 // Then bitcast to the type requested.
1663 // Legalizing constants too early makes the DAGCombiner's job harder so we
1664 // only legalize if the DAG tells us we must produce legal types.
1665 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1666 TLI->getTypeAction(*getContext(), EltVT) ==
1668 const APInt &NewVal = Elt->getValue();
1669 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1670 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1671
1672 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1673 if (VT.isScalableVector() ||
1675 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1676 "Can only handle an even split!");
1677 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1678
1679 SmallVector<SDValue, 2> ScalarParts;
1680 for (unsigned i = 0; i != Parts; ++i)
1681 ScalarParts.push_back(getConstant(
1682 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1683 ViaEltVT, isT, isO));
1684
1685 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1686 }
1687
1688 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1689 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1690
1691 // Check the temporary vector is the correct size. If this fails then
1692 // getTypeToTransformTo() probably returned a type whose size (in bits)
1693 // isn't a power-of-2 factor of the requested type size.
1694 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1695
1696 SmallVector<SDValue, 2> EltParts;
1697 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1698 EltParts.push_back(getConstant(
1699 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1700 ViaEltVT, isT, isO));
1701
1702 // EltParts is currently in little endian order. If we actually want
1703 // big-endian order then reverse it now.
1704 if (getDataLayout().isBigEndian())
1705 std::reverse(EltParts.begin(), EltParts.end());
1706
1707 // The elements must be reversed when the element order is different
1708 // to the endianness of the elements (because the BITCAST is itself a
1709 // vector shuffle in this situation). However, we do not need any code to
1710 // perform this reversal because getConstant() is producing a vector
1711 // splat.
1712 // This situation occurs in MIPS MSA.
1713
1715 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1716 llvm::append_range(Ops, EltParts);
1717
1718 SDValue V =
1719 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1720 return V;
1721 }
1722
1723 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1724 "APInt size does not match type size!");
1725 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1726 SDVTList VTs = getVTList(EltVT);
1728 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1729 ID.AddPointer(Elt);
1730 ID.AddBoolean(isO);
1731 void *IP = nullptr;
1732 SDNode *N = nullptr;
1733 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1734 if (!VT.isVector())
1735 return SDValue(N, 0);
1736
1737 if (!N) {
1738 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1739 CSEMap.InsertNode(N, IP);
1740 InsertNode(N);
1741 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1742 }
1743
1744 SDValue Result(N, 0);
1745 if (VT.isVector())
1746 Result = getSplat(VT, DL, Result);
1747 return Result;
1748}
1749
1751 bool isT, bool isO) {
1752 unsigned Size = VT.getScalarSizeInBits();
1753 assert(
1754 isIntN(Size, Val) &&
1755 "getSignedConstant with a int64_t value that doesn't fit in the type!");
1756 return getConstant(APInt(Size, Val, true), DL, VT, isT, isO);
1757}
1758
1760 bool IsOpaque) {
1762 IsTarget, IsOpaque);
1763}
1764
1766 bool isTarget) {
1767 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1768}
1769
1771 const SDLoc &DL) {
1772 assert(VT.isInteger() && "Shift amount is not an integer type!");
1773 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1774 return getConstant(Val, DL, ShiftVT);
1775}
1776
1778 const SDLoc &DL) {
1779 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1780 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1781}
1782
1784 bool isTarget) {
1785 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1786}
1787
1789 bool isTarget) {
1790 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1791}
1792
1794 EVT VT, bool isTarget) {
1795 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1796
1797 EVT EltVT = VT.getScalarType();
1798
1799 // Do the map lookup using the actual bit pattern for the floating point
1800 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1801 // we don't have issues with SNANs.
1802 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1803 SDVTList VTs = getVTList(EltVT);
1805 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1806 ID.AddPointer(&V);
1807 void *IP = nullptr;
1808 SDNode *N = nullptr;
1809 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1810 if (!VT.isVector())
1811 return SDValue(N, 0);
1812
1813 if (!N) {
1814 N = newSDNode<ConstantFPSDNode>(isTarget, &V, VTs);
1815 CSEMap.InsertNode(N, IP);
1816 InsertNode(N);
1817 }
1818
1819 SDValue Result(N, 0);
1820 if (VT.isVector())
1821 Result = getSplat(VT, DL, Result);
1822 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1823 return Result;
1824}
1825
1827 bool isTarget) {
1828 EVT EltVT = VT.getScalarType();
1829 if (EltVT == MVT::f32)
1830 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1831 if (EltVT == MVT::f64)
1832 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1833 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1834 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1835 bool Ignored;
1836 APFloat APF = APFloat(Val);
1838 &Ignored);
1839 return getConstantFP(APF, DL, VT, isTarget);
1840 }
1841 llvm_unreachable("Unsupported type in getConstantFP");
1842}
1843
1845 EVT VT, int64_t Offset, bool isTargetGA,
1846 unsigned TargetFlags) {
1847 assert((TargetFlags == 0 || isTargetGA) &&
1848 "Cannot set target flags on target-independent globals");
1849
1850 // Truncate (with sign-extension) the offset value to the pointer size.
1852 if (BitWidth < 64)
1854
1855 unsigned Opc;
1856 if (GV->isThreadLocal())
1858 else
1859 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1860
1861 SDVTList VTs = getVTList(VT);
1863 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1864 ID.AddPointer(GV);
1865 ID.AddInteger(Offset);
1866 ID.AddInteger(TargetFlags);
1867 void *IP = nullptr;
1868 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1869 return SDValue(E, 0);
1870
1871 auto *N = newSDNode<GlobalAddressSDNode>(
1872 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1873 CSEMap.InsertNode(N, IP);
1874 InsertNode(N);
1875 return SDValue(N, 0);
1876}
1877
1878SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1879 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1880 SDVTList VTs = getVTList(VT);
1882 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1883 ID.AddInteger(FI);
1884 void *IP = nullptr;
1885 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1886 return SDValue(E, 0);
1887
1888 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1889 CSEMap.InsertNode(N, IP);
1890 InsertNode(N);
1891 return SDValue(N, 0);
1892}
1893
1894SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1895 unsigned TargetFlags) {
1896 assert((TargetFlags == 0 || isTarget) &&
1897 "Cannot set target flags on target-independent jump tables");
1898 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1899 SDVTList VTs = getVTList(VT);
1901 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1902 ID.AddInteger(JTI);
1903 ID.AddInteger(TargetFlags);
1904 void *IP = nullptr;
1905 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1906 return SDValue(E, 0);
1907
1908 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1909 CSEMap.InsertNode(N, IP);
1910 InsertNode(N);
1911 return SDValue(N, 0);
1912}
1913
1915 const SDLoc &DL) {
1917 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1918 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1919}
1920
1922 MaybeAlign Alignment, int Offset,
1923 bool isTarget, unsigned TargetFlags) {
1924 assert((TargetFlags == 0 || isTarget) &&
1925 "Cannot set target flags on target-independent globals");
1926 if (!Alignment)
1927 Alignment = shouldOptForSize()
1928 ? getDataLayout().getABITypeAlign(C->getType())
1929 : getDataLayout().getPrefTypeAlign(C->getType());
1930 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1931 SDVTList VTs = getVTList(VT);
1933 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1934 ID.AddInteger(Alignment->value());
1935 ID.AddInteger(Offset);
1936 ID.AddPointer(C);
1937 ID.AddInteger(TargetFlags);
1938 void *IP = nullptr;
1939 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1940 return SDValue(E, 0);
1941
1942 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1943 TargetFlags);
1944 CSEMap.InsertNode(N, IP);
1945 InsertNode(N);
1946 SDValue V = SDValue(N, 0);
1947 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1948 return V;
1949}
1950
1952 MaybeAlign Alignment, int Offset,
1953 bool isTarget, unsigned TargetFlags) {
1954 assert((TargetFlags == 0 || isTarget) &&
1955 "Cannot set target flags on target-independent globals");
1956 if (!Alignment)
1957 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1958 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1959 SDVTList VTs = getVTList(VT);
1961 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1962 ID.AddInteger(Alignment->value());
1963 ID.AddInteger(Offset);
1964 C->addSelectionDAGCSEId(ID);
1965 ID.AddInteger(TargetFlags);
1966 void *IP = nullptr;
1967 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1968 return SDValue(E, 0);
1969
1970 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1971 TargetFlags);
1972 CSEMap.InsertNode(N, IP);
1973 InsertNode(N);
1974 return SDValue(N, 0);
1975}
1976
1979 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), std::nullopt);
1980 ID.AddPointer(MBB);
1981 void *IP = nullptr;
1982 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1983 return SDValue(E, 0);
1984
1985 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1986 CSEMap.InsertNode(N, IP);
1987 InsertNode(N);
1988 return SDValue(N, 0);
1989}
1990
1992 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1993 ValueTypeNodes.size())
1994 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1995
1996 SDNode *&N = VT.isExtended() ?
1997 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1998
1999 if (N) return SDValue(N, 0);
2000 N = newSDNode<VTSDNode>(VT);
2001 InsertNode(N);
2002 return SDValue(N, 0);
2003}
2004
2006 SDNode *&N = ExternalSymbols[Sym];
2007 if (N) return SDValue(N, 0);
2008 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2009 InsertNode(N);
2010 return SDValue(N, 0);
2011}
2012
2014 SDNode *&N = MCSymbols[Sym];
2015 if (N)
2016 return SDValue(N, 0);
2017 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2018 InsertNode(N);
2019 return SDValue(N, 0);
2020}
2021
2023 unsigned TargetFlags) {
2024 SDNode *&N =
2025 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2026 if (N) return SDValue(N, 0);
2027 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2028 InsertNode(N);
2029 return SDValue(N, 0);
2030}
2031
2033 if ((unsigned)Cond >= CondCodeNodes.size())
2034 CondCodeNodes.resize(Cond+1);
2035
2036 if (!CondCodeNodes[Cond]) {
2037 auto *N = newSDNode<CondCodeSDNode>(Cond);
2038 CondCodeNodes[Cond] = N;
2039 InsertNode(N);
2040 }
2041
2042 return SDValue(CondCodeNodes[Cond], 0);
2043}
2044
2046 bool ConstantFold) {
2047 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2048 "APInt size does not match type size!");
2049
2050 if (MulImm == 0)
2051 return getConstant(0, DL, VT);
2052
2053 if (ConstantFold) {
2054 const MachineFunction &MF = getMachineFunction();
2055 const Function &F = MF.getFunction();
2056 ConstantRange CR = getVScaleRange(&F, 64);
2057 if (const APInt *C = CR.getSingleElement())
2058 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2059 }
2060
2061 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2062}
2063
2065 bool ConstantFold) {
2066 if (EC.isScalable())
2067 return getVScale(DL, VT,
2068 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2069
2070 return getConstant(EC.getKnownMinValue(), DL, VT);
2071}
2072
2074 APInt One(ResVT.getScalarSizeInBits(), 1);
2075 return getStepVector(DL, ResVT, One);
2076}
2077
2079 const APInt &StepVal) {
2080 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2081 if (ResVT.isScalableVector())
2082 return getNode(
2083 ISD::STEP_VECTOR, DL, ResVT,
2084 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2085
2086 SmallVector<SDValue, 16> OpsStepConstants;
2087 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2088 OpsStepConstants.push_back(
2089 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2090 return getBuildVector(ResVT, DL, OpsStepConstants);
2091}
2092
2093/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2094/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2096 std::swap(N1, N2);
2098}
2099
2101 SDValue N2, ArrayRef<int> Mask) {
2102 assert(VT.getVectorNumElements() == Mask.size() &&
2103 "Must have the same number of vector elements as mask elements!");
2104 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2105 "Invalid VECTOR_SHUFFLE");
2106
2107 // Canonicalize shuffle undef, undef -> undef
2108 if (N1.isUndef() && N2.isUndef())
2109 return getUNDEF(VT);
2110
2111 // Validate that all indices in Mask are within the range of the elements
2112 // input to the shuffle.
2113 int NElts = Mask.size();
2114 assert(llvm::all_of(Mask,
2115 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2116 "Index out of range");
2117
2118 // Copy the mask so we can do any needed cleanup.
2119 SmallVector<int, 8> MaskVec(Mask);
2120
2121 // Canonicalize shuffle v, v -> v, undef
2122 if (N1 == N2) {
2123 N2 = getUNDEF(VT);
2124 for (int i = 0; i != NElts; ++i)
2125 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2126 }
2127
2128 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2129 if (N1.isUndef())
2130 commuteShuffle(N1, N2, MaskVec);
2131
2132 if (TLI->hasVectorBlend()) {
2133 // If shuffling a splat, try to blend the splat instead. We do this here so
2134 // that even when this arises during lowering we don't have to re-handle it.
2135 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2136 BitVector UndefElements;
2137 SDValue Splat = BV->getSplatValue(&UndefElements);
2138 if (!Splat)
2139 return;
2140
2141 for (int i = 0; i < NElts; ++i) {
2142 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2143 continue;
2144
2145 // If this input comes from undef, mark it as such.
2146 if (UndefElements[MaskVec[i] - Offset]) {
2147 MaskVec[i] = -1;
2148 continue;
2149 }
2150
2151 // If we can blend a non-undef lane, use that instead.
2152 if (!UndefElements[i])
2153 MaskVec[i] = i + Offset;
2154 }
2155 };
2156 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2157 BlendSplat(N1BV, 0);
2158 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2159 BlendSplat(N2BV, NElts);
2160 }
2161
2162 // Canonicalize all index into lhs, -> shuffle lhs, undef
2163 // Canonicalize all index into rhs, -> shuffle rhs, undef
2164 bool AllLHS = true, AllRHS = true;
2165 bool N2Undef = N2.isUndef();
2166 for (int i = 0; i != NElts; ++i) {
2167 if (MaskVec[i] >= NElts) {
2168 if (N2Undef)
2169 MaskVec[i] = -1;
2170 else
2171 AllLHS = false;
2172 } else if (MaskVec[i] >= 0) {
2173 AllRHS = false;
2174 }
2175 }
2176 if (AllLHS && AllRHS)
2177 return getUNDEF(VT);
2178 if (AllLHS && !N2Undef)
2179 N2 = getUNDEF(VT);
2180 if (AllRHS) {
2181 N1 = getUNDEF(VT);
2182 commuteShuffle(N1, N2, MaskVec);
2183 }
2184 // Reset our undef status after accounting for the mask.
2185 N2Undef = N2.isUndef();
2186 // Re-check whether both sides ended up undef.
2187 if (N1.isUndef() && N2Undef)
2188 return getUNDEF(VT);
2189
2190 // If Identity shuffle return that node.
2191 bool Identity = true, AllSame = true;
2192 for (int i = 0; i != NElts; ++i) {
2193 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2194 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2195 }
2196 if (Identity && NElts)
2197 return N1;
2198
2199 // Shuffling a constant splat doesn't change the result.
2200 if (N2Undef) {
2201 SDValue V = N1;
2202
2203 // Look through any bitcasts. We check that these don't change the number
2204 // (and size) of elements and just changes their types.
2205 while (V.getOpcode() == ISD::BITCAST)
2206 V = V->getOperand(0);
2207
2208 // A splat should always show up as a build vector node.
2209 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2210 BitVector UndefElements;
2211 SDValue Splat = BV->getSplatValue(&UndefElements);
2212 // If this is a splat of an undef, shuffling it is also undef.
2213 if (Splat && Splat.isUndef())
2214 return getUNDEF(VT);
2215
2216 bool SameNumElts =
2217 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2218
2219 // We only have a splat which can skip shuffles if there is a splatted
2220 // value and no undef lanes rearranged by the shuffle.
2221 if (Splat && UndefElements.none()) {
2222 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2223 // number of elements match or the value splatted is a zero constant.
2224 if (SameNumElts || isNullConstant(Splat))
2225 return N1;
2226 }
2227
2228 // If the shuffle itself creates a splat, build the vector directly.
2229 if (AllSame && SameNumElts) {
2230 EVT BuildVT = BV->getValueType(0);
2231 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2232 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2233
2234 // We may have jumped through bitcasts, so the type of the
2235 // BUILD_VECTOR may not match the type of the shuffle.
2236 if (BuildVT != VT)
2237 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2238 return NewBV;
2239 }
2240 }
2241 }
2242
2243 SDVTList VTs = getVTList(VT);
2245 SDValue Ops[2] = { N1, N2 };
2247 for (int i = 0; i != NElts; ++i)
2248 ID.AddInteger(MaskVec[i]);
2249
2250 void* IP = nullptr;
2251 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2252 return SDValue(E, 0);
2253
2254 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2255 // SDNode doesn't have access to it. This memory will be "leaked" when
2256 // the node is deallocated, but recovered when the NodeAllocator is released.
2257 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2258 llvm::copy(MaskVec, MaskAlloc);
2259
2260 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2261 dl.getDebugLoc(), MaskAlloc);
2262 createOperands(N, Ops);
2263
2264 CSEMap.InsertNode(N, IP);
2265 InsertNode(N);
2266 SDValue V = SDValue(N, 0);
2267 NewSDValueDbgMsg(V, "Creating new node: ", this);
2268 return V;
2269}
2270
2272 EVT VT = SV.getValueType(0);
2273 SmallVector<int, 8> MaskVec(SV.getMask());
2275
2276 SDValue Op0 = SV.getOperand(0);
2277 SDValue Op1 = SV.getOperand(1);
2278 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2279}
2280
2282 SDVTList VTs = getVTList(VT);
2284 AddNodeIDNode(ID, ISD::Register, VTs, std::nullopt);
2285 ID.AddInteger(RegNo);
2286 void *IP = nullptr;
2287 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2288 return SDValue(E, 0);
2289
2290 auto *N = newSDNode<RegisterSDNode>(RegNo, VTs);
2291 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2292 CSEMap.InsertNode(N, IP);
2293 InsertNode(N);
2294 return SDValue(N, 0);
2295}
2296
2299 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), std::nullopt);
2300 ID.AddPointer(RegMask);
2301 void *IP = nullptr;
2302 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2303 return SDValue(E, 0);
2304
2305 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2306 CSEMap.InsertNode(N, IP);
2307 InsertNode(N);
2308 return SDValue(N, 0);
2309}
2310
2312 MCSymbol *Label) {
2313 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2314}
2315
2316SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2317 SDValue Root, MCSymbol *Label) {
2319 SDValue Ops[] = { Root };
2320 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2321 ID.AddPointer(Label);
2322 void *IP = nullptr;
2323 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2324 return SDValue(E, 0);
2325
2326 auto *N =
2327 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2328 createOperands(N, Ops);
2329
2330 CSEMap.InsertNode(N, IP);
2331 InsertNode(N);
2332 return SDValue(N, 0);
2333}
2334
2336 int64_t Offset, bool isTarget,
2337 unsigned TargetFlags) {
2338 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2339 SDVTList VTs = getVTList(VT);
2340
2342 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
2343 ID.AddPointer(BA);
2344 ID.AddInteger(Offset);
2345 ID.AddInteger(TargetFlags);
2346 void *IP = nullptr;
2347 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2348 return SDValue(E, 0);
2349
2350 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2351 CSEMap.InsertNode(N, IP);
2352 InsertNode(N);
2353 return SDValue(N, 0);
2354}
2355
2358 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), std::nullopt);
2359 ID.AddPointer(V);
2360
2361 void *IP = nullptr;
2362 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2363 return SDValue(E, 0);
2364
2365 auto *N = newSDNode<SrcValueSDNode>(V);
2366 CSEMap.InsertNode(N, IP);
2367 InsertNode(N);
2368 return SDValue(N, 0);
2369}
2370
2373 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), std::nullopt);
2374 ID.AddPointer(MD);
2375
2376 void *IP = nullptr;
2377 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2378 return SDValue(E, 0);
2379
2380 auto *N = newSDNode<MDNodeSDNode>(MD);
2381 CSEMap.InsertNode(N, IP);
2382 InsertNode(N);
2383 return SDValue(N, 0);
2384}
2385
2387 if (VT == V.getValueType())
2388 return V;
2389
2390 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2391}
2392
2394 unsigned SrcAS, unsigned DestAS) {
2395 SDVTList VTs = getVTList(VT);
2396 SDValue Ops[] = {Ptr};
2399 ID.AddInteger(SrcAS);
2400 ID.AddInteger(DestAS);
2401
2402 void *IP = nullptr;
2403 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2404 return SDValue(E, 0);
2405
2406 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2407 VTs, SrcAS, DestAS);
2408 createOperands(N, Ops);
2409
2410 CSEMap.InsertNode(N, IP);
2411 InsertNode(N);
2412 return SDValue(N, 0);
2413}
2414
2416 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2417}
2418
2419/// getShiftAmountOperand - Return the specified value casted to
2420/// the target's desired shift amount type.
2422 EVT OpTy = Op.getValueType();
2423 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2424 if (OpTy == ShTy || OpTy.isVector()) return Op;
2425
2426 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2427}
2428
2430 SDLoc dl(Node);
2432 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2433 EVT VT = Node->getValueType(0);
2434 SDValue Tmp1 = Node->getOperand(0);
2435 SDValue Tmp2 = Node->getOperand(1);
2436 const MaybeAlign MA(Node->getConstantOperandVal(3));
2437
2438 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2439 Tmp2, MachinePointerInfo(V));
2440 SDValue VAList = VAListLoad;
2441
2442 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2443 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2444 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2445
2446 VAList = getNode(
2447 ISD::AND, dl, VAList.getValueType(), VAList,
2448 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2449 }
2450
2451 // Increment the pointer, VAList, to the next vaarg
2452 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2453 getConstant(getDataLayout().getTypeAllocSize(
2454 VT.getTypeForEVT(*getContext())),
2455 dl, VAList.getValueType()));
2456 // Store the incremented VAList to the legalized pointer
2457 Tmp1 =
2458 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2459 // Load the actual argument out of the pointer VAList
2460 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2461}
2462
2464 SDLoc dl(Node);
2466 // This defaults to loading a pointer from the input and storing it to the
2467 // output, returning the chain.
2468 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2469 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2470 SDValue Tmp1 =
2471 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2472 Node->getOperand(2), MachinePointerInfo(VS));
2473 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2474 MachinePointerInfo(VD));
2475}
2476
2478 const DataLayout &DL = getDataLayout();
2479 Type *Ty = VT.getTypeForEVT(*getContext());
2480 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2481
2482 if (TLI->isTypeLegal(VT) || !VT.isVector())
2483 return RedAlign;
2484
2486 const Align StackAlign = TFI->getStackAlign();
2487
2488 // See if we can choose a smaller ABI alignment in cases where it's an
2489 // illegal vector type that will get broken down.
2490 if (RedAlign > StackAlign) {
2491 EVT IntermediateVT;
2492 MVT RegisterVT;
2493 unsigned NumIntermediates;
2494 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2495 NumIntermediates, RegisterVT);
2496 Ty = IntermediateVT.getTypeForEVT(*getContext());
2497 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2498 if (RedAlign2 < RedAlign)
2499 RedAlign = RedAlign2;
2500
2501 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2502 // If the stack is not realignable, the alignment should be limited to the
2503 // StackAlignment
2504 RedAlign = std::min(RedAlign, StackAlign);
2505 }
2506
2507 return RedAlign;
2508}
2509
2511 MachineFrameInfo &MFI = MF->getFrameInfo();
2513 int StackID = 0;
2514 if (Bytes.isScalable())
2515 StackID = TFI->getStackIDForScalableVectors();
2516 // The stack id gives an indication of whether the object is scalable or
2517 // not, so it's safe to pass in the minimum size here.
2518 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2519 false, nullptr, StackID);
2520 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2521}
2522
2524 Type *Ty = VT.getTypeForEVT(*getContext());
2525 Align StackAlign =
2526 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2527 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2528}
2529
2531 TypeSize VT1Size = VT1.getStoreSize();
2532 TypeSize VT2Size = VT2.getStoreSize();
2533 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2534 "Don't know how to choose the maximum size when creating a stack "
2535 "temporary");
2536 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2537 ? VT1Size
2538 : VT2Size;
2539
2540 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2541 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2542 const DataLayout &DL = getDataLayout();
2543 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2544 return CreateStackTemporary(Bytes, Align);
2545}
2546
2548 ISD::CondCode Cond, const SDLoc &dl) {
2549 EVT OpVT = N1.getValueType();
2550
2551 auto GetUndefBooleanConstant = [&]() {
2552 if (VT.getScalarType() == MVT::i1 ||
2553 TLI->getBooleanContents(OpVT) ==
2555 return getUNDEF(VT);
2556 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2557 // so we cannot use getUNDEF(). Return zero instead.
2558 return getConstant(0, dl, VT);
2559 };
2560
2561 // These setcc operations always fold.
2562 switch (Cond) {
2563 default: break;
2564 case ISD::SETFALSE:
2565 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2566 case ISD::SETTRUE:
2567 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2568
2569 case ISD::SETOEQ:
2570 case ISD::SETOGT:
2571 case ISD::SETOGE:
2572 case ISD::SETOLT:
2573 case ISD::SETOLE:
2574 case ISD::SETONE:
2575 case ISD::SETO:
2576 case ISD::SETUO:
2577 case ISD::SETUEQ:
2578 case ISD::SETUNE:
2579 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2580 break;
2581 }
2582
2583 if (OpVT.isInteger()) {
2584 // For EQ and NE, we can always pick a value for the undef to make the
2585 // predicate pass or fail, so we can return undef.
2586 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2587 // icmp eq/ne X, undef -> undef.
2588 if ((N1.isUndef() || N2.isUndef()) &&
2589 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2590 return GetUndefBooleanConstant();
2591
2592 // If both operands are undef, we can return undef for int comparison.
2593 // icmp undef, undef -> undef.
2594 if (N1.isUndef() && N2.isUndef())
2595 return GetUndefBooleanConstant();
2596
2597 // icmp X, X -> true/false
2598 // icmp X, undef -> true/false because undef could be X.
2599 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2600 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2601 }
2602
2603 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2604 const APInt &C2 = N2C->getAPIntValue();
2605 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2606 const APInt &C1 = N1C->getAPIntValue();
2607
2609 dl, VT, OpVT);
2610 }
2611 }
2612
2613 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2614 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2615
2616 if (N1CFP && N2CFP) {
2617 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2618 switch (Cond) {
2619 default: break;
2620 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2621 return GetUndefBooleanConstant();
2622 [[fallthrough]];
2623 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2624 OpVT);
2625 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2626 return GetUndefBooleanConstant();
2627 [[fallthrough]];
2629 R==APFloat::cmpLessThan, dl, VT,
2630 OpVT);
2631 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2632 return GetUndefBooleanConstant();
2633 [[fallthrough]];
2634 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2635 OpVT);
2636 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2637 return GetUndefBooleanConstant();
2638 [[fallthrough]];
2640 VT, OpVT);
2641 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2642 return GetUndefBooleanConstant();
2643 [[fallthrough]];
2645 R==APFloat::cmpEqual, dl, VT,
2646 OpVT);
2647 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2648 return GetUndefBooleanConstant();
2649 [[fallthrough]];
2651 R==APFloat::cmpEqual, dl, VT, OpVT);
2652 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2653 OpVT);
2654 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2655 OpVT);
2657 R==APFloat::cmpEqual, dl, VT,
2658 OpVT);
2659 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2660 OpVT);
2662 R==APFloat::cmpLessThan, dl, VT,
2663 OpVT);
2665 R==APFloat::cmpUnordered, dl, VT,
2666 OpVT);
2668 VT, OpVT);
2669 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2670 OpVT);
2671 }
2672 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2673 // Ensure that the constant occurs on the RHS.
2675 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2676 return SDValue();
2677 return getSetCC(dl, VT, N2, N1, SwappedCond);
2678 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2679 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2680 // If an operand is known to be a nan (or undef that could be a nan), we can
2681 // fold it.
2682 // Choosing NaN for the undef will always make unordered comparison succeed
2683 // and ordered comparison fails.
2684 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2685 switch (ISD::getUnorderedFlavor(Cond)) {
2686 default:
2687 llvm_unreachable("Unknown flavor!");
2688 case 0: // Known false.
2689 return getBoolConstant(false, dl, VT, OpVT);
2690 case 1: // Known true.
2691 return getBoolConstant(true, dl, VT, OpVT);
2692 case 2: // Undefined.
2693 return GetUndefBooleanConstant();
2694 }
2695 }
2696
2697 // Could not fold it.
2698 return SDValue();
2699}
2700
2701/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2702/// use this predicate to simplify operations downstream.
2704 unsigned BitWidth = Op.getScalarValueSizeInBits();
2706}
2707
2708/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2709/// this predicate to simplify operations downstream. Mask is known to be zero
2710/// for bits that V cannot have.
2712 unsigned Depth) const {
2713 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2714}
2715
2716/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2717/// DemandedElts. We use this predicate to simplify operations downstream.
2718/// Mask is known to be zero for bits that V cannot have.
2720 const APInt &DemandedElts,
2721 unsigned Depth) const {
2722 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2723}
2724
2725/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2726/// DemandedElts. We use this predicate to simplify operations downstream.
2728 unsigned Depth /* = 0 */) const {
2729 return computeKnownBits(V, DemandedElts, Depth).isZero();
2730}
2731
2732/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2734 unsigned Depth) const {
2735 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2736}
2737
2739 const APInt &DemandedElts,
2740 unsigned Depth) const {
2741 EVT VT = Op.getValueType();
2742 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2743
2744 unsigned NumElts = VT.getVectorNumElements();
2745 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2746
2747 APInt KnownZeroElements = APInt::getZero(NumElts);
2748 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2749 if (!DemandedElts[EltIdx])
2750 continue; // Don't query elements that are not demanded.
2751 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2752 if (MaskedVectorIsZero(Op, Mask, Depth))
2753 KnownZeroElements.setBit(EltIdx);
2754 }
2755 return KnownZeroElements;
2756}
2757
2758/// isSplatValue - Return true if the vector V has the same value
2759/// across all DemandedElts. For scalable vectors, we don't know the
2760/// number of lanes at compile time. Instead, we use a 1 bit APInt
2761/// to represent a conservative value for all lanes; that is, that
2762/// one bit value is implicitly splatted across all lanes.
2763bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2764 APInt &UndefElts, unsigned Depth) const {
2765 unsigned Opcode = V.getOpcode();
2766 EVT VT = V.getValueType();
2767 assert(VT.isVector() && "Vector type expected");
2768 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2769 "scalable demanded bits are ignored");
2770
2771 if (!DemandedElts)
2772 return false; // No demanded elts, better to assume we don't know anything.
2773
2774 if (Depth >= MaxRecursionDepth)
2775 return false; // Limit search depth.
2776
2777 // Deal with some common cases here that work for both fixed and scalable
2778 // vector types.
2779 switch (Opcode) {
2780 case ISD::SPLAT_VECTOR:
2781 UndefElts = V.getOperand(0).isUndef()
2782 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2783 : APInt(DemandedElts.getBitWidth(), 0);
2784 return true;
2785 case ISD::ADD:
2786 case ISD::SUB:
2787 case ISD::AND:
2788 case ISD::XOR:
2789 case ISD::OR: {
2790 APInt UndefLHS, UndefRHS;
2791 SDValue LHS = V.getOperand(0);
2792 SDValue RHS = V.getOperand(1);
2793 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2794 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2795 UndefElts = UndefLHS | UndefRHS;
2796 return true;
2797 }
2798 return false;
2799 }
2800 case ISD::ABS:
2801 case ISD::TRUNCATE:
2802 case ISD::SIGN_EXTEND:
2803 case ISD::ZERO_EXTEND:
2804 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2805 default:
2806 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2807 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2808 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2809 Depth);
2810 break;
2811}
2812
2813 // We don't support other cases than those above for scalable vectors at
2814 // the moment.
2815 if (VT.isScalableVector())
2816 return false;
2817
2818 unsigned NumElts = VT.getVectorNumElements();
2819 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2820 UndefElts = APInt::getZero(NumElts);
2821
2822 switch (Opcode) {
2823 case ISD::BUILD_VECTOR: {
2824 SDValue Scl;
2825 for (unsigned i = 0; i != NumElts; ++i) {
2826 SDValue Op = V.getOperand(i);
2827 if (Op.isUndef()) {
2828 UndefElts.setBit(i);
2829 continue;
2830 }
2831 if (!DemandedElts[i])
2832 continue;
2833 if (Scl && Scl != Op)
2834 return false;
2835 Scl = Op;
2836 }
2837 return true;
2838 }
2839 case ISD::VECTOR_SHUFFLE: {
2840 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2841 APInt DemandedLHS = APInt::getZero(NumElts);
2842 APInt DemandedRHS = APInt::getZero(NumElts);
2843 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2844 for (int i = 0; i != (int)NumElts; ++i) {
2845 int M = Mask[i];
2846 if (M < 0) {
2847 UndefElts.setBit(i);
2848 continue;
2849 }
2850 if (!DemandedElts[i])
2851 continue;
2852 if (M < (int)NumElts)
2853 DemandedLHS.setBit(M);
2854 else
2855 DemandedRHS.setBit(M - NumElts);
2856 }
2857
2858 // If we aren't demanding either op, assume there's no splat.
2859 // If we are demanding both ops, assume there's no splat.
2860 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2861 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2862 return false;
2863
2864 // See if the demanded elts of the source op is a splat or we only demand
2865 // one element, which should always be a splat.
2866 // TODO: Handle source ops splats with undefs.
2867 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2868 APInt SrcUndefs;
2869 return (SrcElts.popcount() == 1) ||
2870 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2871 (SrcElts & SrcUndefs).isZero());
2872 };
2873 if (!DemandedLHS.isZero())
2874 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2875 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2876 }
2878 // Offset the demanded elts by the subvector index.
2879 SDValue Src = V.getOperand(0);
2880 // We don't support scalable vectors at the moment.
2881 if (Src.getValueType().isScalableVector())
2882 return false;
2883 uint64_t Idx = V.getConstantOperandVal(1);
2884 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2885 APInt UndefSrcElts;
2886 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2887 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2888 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2889 return true;
2890 }
2891 break;
2892 }
2896 // Widen the demanded elts by the src element count.
2897 SDValue Src = V.getOperand(0);
2898 // We don't support scalable vectors at the moment.
2899 if (Src.getValueType().isScalableVector())
2900 return false;
2901 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2902 APInt UndefSrcElts;
2903 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2904 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2905 UndefElts = UndefSrcElts.trunc(NumElts);
2906 return true;
2907 }
2908 break;
2909 }
2910 case ISD::BITCAST: {
2911 SDValue Src = V.getOperand(0);
2912 EVT SrcVT = Src.getValueType();
2913 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2914 unsigned BitWidth = VT.getScalarSizeInBits();
2915
2916 // Ignore bitcasts from unsupported types.
2917 // TODO: Add fp support?
2918 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2919 break;
2920
2921 // Bitcast 'small element' vector to 'large element' vector.
2922 if ((BitWidth % SrcBitWidth) == 0) {
2923 // See if each sub element is a splat.
2924 unsigned Scale = BitWidth / SrcBitWidth;
2925 unsigned NumSrcElts = SrcVT.getVectorNumElements();
2926 APInt ScaledDemandedElts =
2927 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
2928 for (unsigned I = 0; I != Scale; ++I) {
2929 APInt SubUndefElts;
2930 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
2931 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
2932 SubDemandedElts &= ScaledDemandedElts;
2933 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
2934 return false;
2935 // TODO: Add support for merging sub undef elements.
2936 if (!SubUndefElts.isZero())
2937 return false;
2938 }
2939 return true;
2940 }
2941 break;
2942 }
2943 }
2944
2945 return false;
2946}
2947
2948/// Helper wrapper to main isSplatValue function.
2949bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
2950 EVT VT = V.getValueType();
2951 assert(VT.isVector() && "Vector type expected");
2952
2953 APInt UndefElts;
2954 // Since the number of lanes in a scalable vector is unknown at compile time,
2955 // we track one bit which is implicitly broadcast to all lanes. This means
2956 // that all lanes in a scalable vector are considered demanded.
2957 APInt DemandedElts
2959 return isSplatValue(V, DemandedElts, UndefElts) &&
2960 (AllowUndefs || !UndefElts);
2961}
2962
2965
2966 EVT VT = V.getValueType();
2967 unsigned Opcode = V.getOpcode();
2968 switch (Opcode) {
2969 default: {
2970 APInt UndefElts;
2971 // Since the number of lanes in a scalable vector is unknown at compile time,
2972 // we track one bit which is implicitly broadcast to all lanes. This means
2973 // that all lanes in a scalable vector are considered demanded.
2974 APInt DemandedElts
2976
2977 if (isSplatValue(V, DemandedElts, UndefElts)) {
2978 if (VT.isScalableVector()) {
2979 // DemandedElts and UndefElts are ignored for scalable vectors, since
2980 // the only supported cases are SPLAT_VECTOR nodes.
2981 SplatIdx = 0;
2982 } else {
2983 // Handle case where all demanded elements are UNDEF.
2984 if (DemandedElts.isSubsetOf(UndefElts)) {
2985 SplatIdx = 0;
2986 return getUNDEF(VT);
2987 }
2988 SplatIdx = (UndefElts & DemandedElts).countr_one();
2989 }
2990 return V;
2991 }
2992 break;
2993 }
2994 case ISD::SPLAT_VECTOR:
2995 SplatIdx = 0;
2996 return V;
2997 case ISD::VECTOR_SHUFFLE: {
2998 assert(!VT.isScalableVector());
2999 // Check if this is a shuffle node doing a splat.
3000 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3001 // getTargetVShiftNode currently struggles without the splat source.
3002 auto *SVN = cast<ShuffleVectorSDNode>(V);
3003 if (!SVN->isSplat())
3004 break;
3005 int Idx = SVN->getSplatIndex();
3006 int NumElts = V.getValueType().getVectorNumElements();
3007 SplatIdx = Idx % NumElts;
3008 return V.getOperand(Idx / NumElts);
3009 }
3010 }
3011
3012 return SDValue();
3013}
3014
3016 int SplatIdx;
3017 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3018 EVT SVT = SrcVector.getValueType().getScalarType();
3019 EVT LegalSVT = SVT;
3020 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3021 if (!SVT.isInteger())
3022 return SDValue();
3023 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3024 if (LegalSVT.bitsLT(SVT))
3025 return SDValue();
3026 }
3027 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
3028 getVectorIdxConstant(SplatIdx, SDLoc(V)));
3029 }
3030 return SDValue();
3031}
3032
3033std::optional<ConstantRange>
3035 unsigned Depth) const {
3036 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3037 V.getOpcode() == ISD::SRA) &&
3038 "Unknown shift node");
3039 // Shifting more than the bitwidth is not valid.
3040 unsigned BitWidth = V.getScalarValueSizeInBits();
3041
3042 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3043 const APInt &ShAmt = Cst->getAPIntValue();
3044 if (ShAmt.uge(BitWidth))
3045 return std::nullopt;
3046 return ConstantRange(ShAmt);
3047 }
3048
3049 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3050 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3051 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3052 if (!DemandedElts[i])
3053 continue;
3054 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3055 if (!SA) {
3056 MinAmt = MaxAmt = nullptr;
3057 break;
3058 }
3059 const APInt &ShAmt = SA->getAPIntValue();
3060 if (ShAmt.uge(BitWidth))
3061 return std::nullopt;
3062 if (!MinAmt || MinAmt->ugt(ShAmt))
3063 MinAmt = &ShAmt;
3064 if (!MaxAmt || MaxAmt->ult(ShAmt))
3065 MaxAmt = &ShAmt;
3066 }
3067 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3068 "Failed to find matching min/max shift amounts");
3069 if (MinAmt && MaxAmt)
3070 return ConstantRange(*MinAmt, *MaxAmt + 1);
3071 }
3072
3073 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3074 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3075 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3076 if (KnownAmt.getMaxValue().ult(BitWidth))
3077 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3078
3079 return std::nullopt;
3080}
3081
3082std::optional<uint64_t>
3084 unsigned Depth) const {
3085 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3086 V.getOpcode() == ISD::SRA) &&
3087 "Unknown shift node");
3088 if (std::optional<ConstantRange> AmtRange =
3089 getValidShiftAmountRange(V, DemandedElts, Depth))
3090 if (const APInt *ShAmt = AmtRange->getSingleElement())
3091 return ShAmt->getZExtValue();
3092 return std::nullopt;
3093}
3094
3095std::optional<uint64_t>
3097 EVT VT = V.getValueType();
3098 APInt DemandedElts = VT.isFixedLengthVector()
3100 : APInt(1, 1);
3101 return getValidShiftAmount(V, DemandedElts, Depth);
3102}
3103
3104std::optional<uint64_t>
3106 unsigned Depth) const {
3107 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3108 V.getOpcode() == ISD::SRA) &&
3109 "Unknown shift node");
3110 if (std::optional<ConstantRange> AmtRange =
3111 getValidShiftAmountRange(V, DemandedElts, Depth))
3112 return AmtRange->getUnsignedMin().getZExtValue();
3113 return std::nullopt;
3114}
3115
3116std::optional<uint64_t>
3118 EVT VT = V.getValueType();
3119 APInt DemandedElts = VT.isFixedLengthVector()
3121 : APInt(1, 1);
3122 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3123}
3124
3125std::optional<uint64_t>
3127 unsigned Depth) const {
3128 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3129 V.getOpcode() == ISD::SRA) &&
3130 "Unknown shift node");
3131 if (std::optional<ConstantRange> AmtRange =
3132 getValidShiftAmountRange(V, DemandedElts, Depth))
3133 return AmtRange->getUnsignedMax().getZExtValue();
3134 return std::nullopt;
3135}
3136
3137std::optional<uint64_t>
3139 EVT VT = V.getValueType();
3140 APInt DemandedElts = VT.isFixedLengthVector()
3142 : APInt(1, 1);
3143 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3144}
3145
3146/// Determine which bits of Op are known to be either zero or one and return
3147/// them in Known. For vectors, the known bits are those that are shared by
3148/// every vector element.
3150 EVT VT = Op.getValueType();
3151
3152 // Since the number of lanes in a scalable vector is unknown at compile time,
3153 // we track one bit which is implicitly broadcast to all lanes. This means
3154 // that all lanes in a scalable vector are considered demanded.
3155 APInt DemandedElts = VT.isFixedLengthVector()
3157 : APInt(1, 1);
3158 return computeKnownBits(Op, DemandedElts, Depth);
3159}
3160
3161/// Determine which bits of Op are known to be either zero or one and return
3162/// them in Known. The DemandedElts argument allows us to only collect the known
3163/// bits that are shared by the requested vector elements.
3165 unsigned Depth) const {
3166 unsigned BitWidth = Op.getScalarValueSizeInBits();
3167
3168 KnownBits Known(BitWidth); // Don't know anything.
3169
3170 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3171 // We know all of the bits for a constant!
3172 return KnownBits::makeConstant(C->getAPIntValue());
3173 }
3174 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3175 // We know all of the bits for a constant fp!
3176 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3177 }
3178
3179 if (Depth >= MaxRecursionDepth)
3180 return Known; // Limit search depth.
3181
3182 KnownBits Known2;
3183 unsigned NumElts = DemandedElts.getBitWidth();
3184 assert((!Op.getValueType().isFixedLengthVector() ||
3185 NumElts == Op.getValueType().getVectorNumElements()) &&
3186 "Unexpected vector size");
3187
3188 if (!DemandedElts)
3189 return Known; // No demanded elts, better to assume we don't know anything.
3190
3191 unsigned Opcode = Op.getOpcode();
3192 switch (Opcode) {
3193 case ISD::MERGE_VALUES:
3194 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3195 Depth + 1);
3196 case ISD::SPLAT_VECTOR: {
3197 SDValue SrcOp = Op.getOperand(0);
3198 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3199 "Expected SPLAT_VECTOR implicit truncation");
3200 // Implicitly truncate the bits to match the official semantics of
3201 // SPLAT_VECTOR.
3202 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3203 break;
3204 }
3206 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3207 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3208 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3209 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3210 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3211 }
3212 break;
3213 }
3214 case ISD::STEP_VECTOR: {
3215 const APInt &Step = Op.getConstantOperandAPInt(0);
3216
3217 if (Step.isPowerOf2())
3218 Known.Zero.setLowBits(Step.logBase2());
3219
3221
3222 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3223 break;
3224 const APInt MinNumElts =
3225 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3226
3227 bool Overflow;
3228 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3230 .umul_ov(MinNumElts, Overflow);
3231 if (Overflow)
3232 break;
3233
3234 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3235 if (Overflow)
3236 break;
3237
3238 Known.Zero.setHighBits(MaxValue.countl_zero());
3239 break;
3240 }
3241 case ISD::BUILD_VECTOR:
3242 assert(!Op.getValueType().isScalableVector());
3243 // Collect the known bits that are shared by every demanded vector element.
3244 Known.Zero.setAllBits(); Known.One.setAllBits();
3245 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3246 if (!DemandedElts[i])
3247 continue;
3248
3249 SDValue SrcOp = Op.getOperand(i);
3250 Known2 = computeKnownBits(SrcOp, Depth + 1);
3251
3252 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3253 if (SrcOp.getValueSizeInBits() != BitWidth) {
3254 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3255 "Expected BUILD_VECTOR implicit truncation");
3256 Known2 = Known2.trunc(BitWidth);
3257 }
3258
3259 // Known bits are the values that are shared by every demanded element.
3260 Known = Known.intersectWith(Known2);
3261
3262 // If we don't know any bits, early out.
3263 if (Known.isUnknown())
3264 break;
3265 }
3266 break;
3267 case ISD::VECTOR_SHUFFLE: {
3268 assert(!Op.getValueType().isScalableVector());
3269 // Collect the known bits that are shared by every vector element referenced
3270 // by the shuffle.
3271 APInt DemandedLHS, DemandedRHS;
3272 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3273 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3274 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3275 DemandedLHS, DemandedRHS))
3276 break;
3277
3278 // Known bits are the values that are shared by every demanded element.
3279 Known.Zero.setAllBits(); Known.One.setAllBits();
3280 if (!!DemandedLHS) {
3281 SDValue LHS = Op.getOperand(0);
3282 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3283 Known = Known.intersectWith(Known2);
3284 }
3285 // If we don't know any bits, early out.
3286 if (Known.isUnknown())
3287 break;
3288 if (!!DemandedRHS) {
3289 SDValue RHS = Op.getOperand(1);
3290 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3291 Known = Known.intersectWith(Known2);
3292 }
3293 break;
3294 }
3295 case ISD::VSCALE: {
3297 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3298 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3299 break;
3300 }
3301 case ISD::CONCAT_VECTORS: {
3302 if (Op.getValueType().isScalableVector())
3303 break;
3304 // Split DemandedElts and test each of the demanded subvectors.
3305 Known.Zero.setAllBits(); Known.One.setAllBits();
3306 EVT SubVectorVT = Op.getOperand(0).getValueType();
3307 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3308 unsigned NumSubVectors = Op.getNumOperands();
3309 for (unsigned i = 0; i != NumSubVectors; ++i) {
3310 APInt DemandedSub =
3311 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3312 if (!!DemandedSub) {
3313 SDValue Sub = Op.getOperand(i);
3314 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3315 Known = Known.intersectWith(Known2);
3316 }
3317 // If we don't know any bits, early out.
3318 if (Known.isUnknown())
3319 break;
3320 }
3321 break;
3322 }
3323 case ISD::INSERT_SUBVECTOR: {
3324 if (Op.getValueType().isScalableVector())
3325 break;
3326 // Demand any elements from the subvector and the remainder from the src its
3327 // inserted into.
3328 SDValue Src = Op.getOperand(0);
3329 SDValue Sub = Op.getOperand(1);
3330 uint64_t Idx = Op.getConstantOperandVal(2);
3331 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3332 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3333 APInt DemandedSrcElts = DemandedElts;
3334 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3335
3336 Known.One.setAllBits();
3337 Known.Zero.setAllBits();
3338 if (!!DemandedSubElts) {
3339 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3340 if (Known.isUnknown())
3341 break; // early-out.
3342 }
3343 if (!!DemandedSrcElts) {
3344 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3345 Known = Known.intersectWith(Known2);
3346 }
3347 break;
3348 }
3350 // Offset the demanded elts by the subvector index.
3351 SDValue Src = Op.getOperand(0);
3352 // Bail until we can represent demanded elements for scalable vectors.
3353 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3354 break;
3355 uint64_t Idx = Op.getConstantOperandVal(1);
3356 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3357 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3358 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3359 break;
3360 }
3361 case ISD::SCALAR_TO_VECTOR: {
3362 if (Op.getValueType().isScalableVector())
3363 break;
3364 // We know about scalar_to_vector as much as we know about it source,
3365 // which becomes the first element of otherwise unknown vector.
3366 if (DemandedElts != 1)
3367 break;
3368
3369 SDValue N0 = Op.getOperand(0);
3370 Known = computeKnownBits(N0, Depth + 1);
3371 if (N0.getValueSizeInBits() != BitWidth)
3372 Known = Known.trunc(BitWidth);
3373
3374 break;
3375 }
3376 case ISD::BITCAST: {
3377 if (Op.getValueType().isScalableVector())
3378 break;
3379
3380 SDValue N0 = Op.getOperand(0);
3381 EVT SubVT = N0.getValueType();
3382 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3383
3384 // Ignore bitcasts from unsupported types.
3385 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3386 break;
3387
3388 // Fast handling of 'identity' bitcasts.
3389 if (BitWidth == SubBitWidth) {
3390 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3391 break;
3392 }
3393
3394 bool IsLE = getDataLayout().isLittleEndian();
3395
3396 // Bitcast 'small element' vector to 'large element' scalar/vector.
3397 if ((BitWidth % SubBitWidth) == 0) {
3398 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3399
3400 // Collect known bits for the (larger) output by collecting the known
3401 // bits from each set of sub elements and shift these into place.
3402 // We need to separately call computeKnownBits for each set of
3403 // sub elements as the knownbits for each is likely to be different.
3404 unsigned SubScale = BitWidth / SubBitWidth;
3405 APInt SubDemandedElts(NumElts * SubScale, 0);
3406 for (unsigned i = 0; i != NumElts; ++i)
3407 if (DemandedElts[i])
3408 SubDemandedElts.setBit(i * SubScale);
3409
3410 for (unsigned i = 0; i != SubScale; ++i) {
3411 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3412 Depth + 1);
3413 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3414 Known.insertBits(Known2, SubBitWidth * Shifts);
3415 }
3416 }
3417
3418 // Bitcast 'large element' scalar/vector to 'small element' vector.
3419 if ((SubBitWidth % BitWidth) == 0) {
3420 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3421
3422 // Collect known bits for the (smaller) output by collecting the known
3423 // bits from the overlapping larger input elements and extracting the
3424 // sub sections we actually care about.
3425 unsigned SubScale = SubBitWidth / BitWidth;
3426 APInt SubDemandedElts =
3427 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3428 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3429
3430 Known.Zero.setAllBits(); Known.One.setAllBits();
3431 for (unsigned i = 0; i != NumElts; ++i)
3432 if (DemandedElts[i]) {
3433 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3434 unsigned Offset = (Shifts % SubScale) * BitWidth;
3435 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3436 // If we don't know any bits, early out.
3437 if (Known.isUnknown())
3438 break;
3439 }
3440 }
3441 break;
3442 }
3443 case ISD::AND:
3444 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3445 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3446
3447 Known &= Known2;
3448 break;
3449 case ISD::OR:
3450 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3451 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3452
3453 Known |= Known2;
3454 break;
3455 case ISD::XOR:
3456 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3457 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3458
3459 Known ^= Known2;
3460 break;
3461 case ISD::MUL: {
3462 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3463 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3464 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3465 // TODO: SelfMultiply can be poison, but not undef.
3466 if (SelfMultiply)
3467 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3468 Op.getOperand(0), DemandedElts, false, Depth + 1);
3469 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3470
3471 // If the multiplication is known not to overflow, the product of a number
3472 // with itself is non-negative. Only do this if we didn't already computed
3473 // the opposite value for the sign bit.
3474 if (Op->getFlags().hasNoSignedWrap() &&
3475 Op.getOperand(0) == Op.getOperand(1) &&
3476 !Known.isNegative())
3477 Known.makeNonNegative();
3478 break;
3479 }
3480 case ISD::MULHU: {
3481 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3482 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3483 Known = KnownBits::mulhu(Known, Known2);
3484 break;
3485 }
3486 case ISD::MULHS: {
3487 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3488 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3489 Known = KnownBits::mulhs(Known, Known2);
3490 break;
3491 }
3492 case ISD::ABDU: {
3493 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3494 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3495 Known = KnownBits::abdu(Known, Known2);
3496 break;
3497 }
3498 case ISD::ABDS: {
3499 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3500 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3501 Known = KnownBits::abds(Known, Known2);
3502 unsigned SignBits1 =
3503 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3504 if (SignBits1 == 1)
3505 break;
3506 unsigned SignBits0 =
3507 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3508 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3509 break;
3510 }
3511 case ISD::UMUL_LOHI: {
3512 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3513 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3514 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3515 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3516 if (Op.getResNo() == 0)
3517 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3518 else
3519 Known = KnownBits::mulhu(Known, Known2);
3520 break;
3521 }
3522 case ISD::SMUL_LOHI: {
3523 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3524 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3525 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3526 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3527 if (Op.getResNo() == 0)
3528 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3529 else
3530 Known = KnownBits::mulhs(Known, Known2);
3531 break;
3532 }
3533 case ISD::AVGFLOORU: {
3534 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3535 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3536 Known = KnownBits::avgFloorU(Known, Known2);
3537 break;
3538 }
3539 case ISD::AVGCEILU: {
3540 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3541 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3542 Known = KnownBits::avgCeilU(Known, Known2);
3543 break;
3544 }
3545 case ISD::AVGFLOORS: {
3546 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3547 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3548 Known = KnownBits::avgFloorS(Known, Known2);
3549 break;
3550 }
3551 case ISD::AVGCEILS: {
3552 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3553 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3554 Known = KnownBits::avgCeilS(Known, Known2);
3555 break;
3556 }
3557 case ISD::SELECT:
3558 case ISD::VSELECT:
3559 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3560 // If we don't know any bits, early out.
3561 if (Known.isUnknown())
3562 break;
3563 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3564
3565 // Only known if known in both the LHS and RHS.
3566 Known = Known.intersectWith(Known2);
3567 break;
3568 case ISD::SELECT_CC:
3569 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3570 // If we don't know any bits, early out.
3571 if (Known.isUnknown())
3572 break;
3573 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3574
3575 // Only known if known in both the LHS and RHS.
3576 Known = Known.intersectWith(Known2);
3577 break;
3578 case ISD::SMULO:
3579 case ISD::UMULO:
3580 if (Op.getResNo() != 1)
3581 break;
3582 // The boolean result conforms to getBooleanContents.
3583 // If we know the result of a setcc has the top bits zero, use this info.
3584 // We know that we have an integer-based boolean since these operations
3585 // are only available for integer.
3586 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3588 BitWidth > 1)
3589 Known.Zero.setBitsFrom(1);
3590 break;
3591 case ISD::SETCC:
3592 case ISD::SETCCCARRY:
3593 case ISD::STRICT_FSETCC:
3594 case ISD::STRICT_FSETCCS: {
3595 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3596 // If we know the result of a setcc has the top bits zero, use this info.
3597 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3599 BitWidth > 1)
3600 Known.Zero.setBitsFrom(1);
3601 break;
3602 }
3603 case ISD::SHL: {
3604 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3605 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3606
3607 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3608 bool NSW = Op->getFlags().hasNoSignedWrap();
3609
3610 bool ShAmtNonZero = Known2.isNonZero();
3611
3612 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3613
3614 // Minimum shift low bits are known zero.
3615 if (std::optional<uint64_t> ShMinAmt =
3616 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3617 Known.Zero.setLowBits(*ShMinAmt);
3618 break;
3619 }
3620 case ISD::SRL:
3621 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3622 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3623 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3624 Op->getFlags().hasExact());
3625
3626 // Minimum shift high bits are known zero.
3627 if (std::optional<uint64_t> ShMinAmt =
3628 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3629 Known.Zero.setHighBits(*ShMinAmt);
3630 break;
3631 case ISD::SRA:
3632 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3633 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3634 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3635 Op->getFlags().hasExact());
3636 break;
3637 case ISD::FSHL:
3638 case ISD::FSHR:
3639 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3640 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3641
3642 // For fshl, 0-shift returns the 1st arg.
3643 // For fshr, 0-shift returns the 2nd arg.
3644 if (Amt == 0) {
3645 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3646 DemandedElts, Depth + 1);
3647 break;
3648 }
3649
3650 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3651 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3652 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3653 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3654 if (Opcode == ISD::FSHL) {
3655 Known.One <<= Amt;
3656 Known.Zero <<= Amt;
3657 Known2.One.lshrInPlace(BitWidth - Amt);
3658 Known2.Zero.lshrInPlace(BitWidth - Amt);
3659 } else {
3660 Known.One <<= BitWidth - Amt;
3661 Known.Zero <<= BitWidth - Amt;
3662 Known2.One.lshrInPlace(Amt);
3663 Known2.Zero.lshrInPlace(Amt);
3664 }
3665 Known = Known.unionWith(Known2);
3666 }
3667 break;
3668 case ISD::SHL_PARTS:
3669 case ISD::SRA_PARTS:
3670 case ISD::SRL_PARTS: {
3671 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3672
3673 // Collect lo/hi source values and concatenate.
3674 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3675 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3676 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3677 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3678 Known = Known2.concat(Known);
3679
3680 // Collect shift amount.
3681 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3682
3683 if (Opcode == ISD::SHL_PARTS)
3684 Known = KnownBits::shl(Known, Known2);
3685 else if (Opcode == ISD::SRA_PARTS)
3686 Known = KnownBits::ashr(Known, Known2);
3687 else // if (Opcode == ISD::SRL_PARTS)
3688 Known = KnownBits::lshr(Known, Known2);
3689
3690 // TODO: Minimum shift low/high bits are known zero.
3691
3692 if (Op.getResNo() == 0)
3693 Known = Known.extractBits(LoBits, 0);
3694 else
3695 Known = Known.extractBits(HiBits, LoBits);
3696 break;
3697 }
3699 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3700 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3701 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3702 break;
3703 }
3704 case ISD::CTTZ:
3705 case ISD::CTTZ_ZERO_UNDEF: {
3706 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3707 // If we have a known 1, its position is our upper bound.
3708 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3709 unsigned LowBits = llvm::bit_width(PossibleTZ);
3710 Known.Zero.setBitsFrom(LowBits);
3711 break;
3712 }
3713 case ISD::CTLZ:
3714 case ISD::CTLZ_ZERO_UNDEF: {
3715 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3716 // If we have a known 1, its position is our upper bound.
3717 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3718 unsigned LowBits = llvm::bit_width(PossibleLZ);
3719 Known.Zero.setBitsFrom(LowBits);
3720 break;
3721 }
3722 case ISD::CTPOP: {
3723 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3724 // If we know some of the bits are zero, they can't be one.
3725 unsigned PossibleOnes = Known2.countMaxPopulation();
3726 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3727 break;
3728 }
3729 case ISD::PARITY: {
3730 // Parity returns 0 everywhere but the LSB.
3731 Known.Zero.setBitsFrom(1);
3732 break;
3733 }
3734 case ISD::LOAD: {
3735 LoadSDNode *LD = cast<LoadSDNode>(Op);
3736 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3737 if (ISD::isNON_EXTLoad(LD) && Cst) {
3738 // Determine any common known bits from the loaded constant pool value.
3739 Type *CstTy = Cst->getType();
3740 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3741 !Op.getValueType().isScalableVector()) {
3742 // If its a vector splat, then we can (quickly) reuse the scalar path.
3743 // NOTE: We assume all elements match and none are UNDEF.
3744 if (CstTy->isVectorTy()) {
3745 if (const Constant *Splat = Cst->getSplatValue()) {
3746 Cst = Splat;
3747 CstTy = Cst->getType();
3748 }
3749 }
3750 // TODO - do we need to handle different bitwidths?
3751 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3752 // Iterate across all vector elements finding common known bits.
3753 Known.One.setAllBits();
3754 Known.Zero.setAllBits();
3755 for (unsigned i = 0; i != NumElts; ++i) {
3756 if (!DemandedElts[i])
3757 continue;
3758 if (Constant *Elt = Cst->getAggregateElement(i)) {
3759 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3760 const APInt &Value = CInt->getValue();
3761 Known.One &= Value;
3762 Known.Zero &= ~Value;
3763 continue;
3764 }
3765 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3766 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3767 Known.One &= Value;
3768 Known.Zero &= ~Value;
3769 continue;
3770 }
3771 }
3772 Known.One.clearAllBits();
3773 Known.Zero.clearAllBits();
3774 break;
3775 }
3776 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3777 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3778 Known = KnownBits::makeConstant(CInt->getValue());
3779 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3780 Known =
3781 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3782 }
3783 }
3784 }
3785 } else if (Op.getResNo() == 0) {
3786 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3787 ? LD->getMemoryVT().getFixedSizeInBits()
3788 : BitWidth);
3789 EVT VT = Op.getValueType();
3790 // Fill in any known bits from range information. There are 3 types being
3791 // used. The results VT (same vector elt size as BitWidth), the loaded
3792 // MemoryVT (which may or may not be vector) and the range VTs original
3793 // type. The range matadata needs the full range (i.e
3794 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3795 // if it is know. These are then extended to the original VT sizes below.
3796 if (const MDNode *MD = LD->getRanges()) {
3798 if (VT.isVector()) {
3799 // Handle truncation to the first demanded element.
3800 // TODO: Figure out which demanded elements are covered
3801 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
3802 break;
3803 Known0 = Known0.trunc(BitWidth);
3804 }
3805 }
3806
3807 if (LD->getMemoryVT().isVector())
3808 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
3809
3810 // Extend the Known bits from memory to the size of the result.
3811 if (ISD::isZEXTLoad(Op.getNode()))
3812 Known = Known0.zext(BitWidth);
3813 else if (ISD::isSEXTLoad(Op.getNode()))
3814 Known = Known0.sext(BitWidth);
3815 else if (ISD::isEXTLoad(Op.getNode()))
3816 Known = Known0.anyext(BitWidth);
3817 else
3818 Known = Known0;
3819 assert(Known.getBitWidth() == BitWidth);
3820 return Known;
3821 }
3822 break;
3823 }
3825 if (Op.getValueType().isScalableVector())
3826 break;
3827 EVT InVT = Op.getOperand(0).getValueType();
3828 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3829 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3830 Known = Known.zext(BitWidth);
3831 break;
3832 }
3833 case ISD::ZERO_EXTEND: {
3834 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3835 Known = Known.zext(BitWidth);
3836 break;
3837 }
3839 if (Op.getValueType().isScalableVector())
3840 break;
3841 EVT InVT = Op.getOperand(0).getValueType();
3842 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3843 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3844 // If the sign bit is known to be zero or one, then sext will extend
3845 // it to the top bits, else it will just zext.
3846 Known = Known.sext(BitWidth);
3847 break;
3848 }
3849 case ISD::SIGN_EXTEND: {
3850 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3851 // If the sign bit is known to be zero or one, then sext will extend
3852 // it to the top bits, else it will just zext.
3853 Known = Known.sext(BitWidth);
3854 break;
3855 }
3857 if (Op.getValueType().isScalableVector())
3858 break;
3859 EVT InVT = Op.getOperand(0).getValueType();
3860 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3861 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3862 Known = Known.anyext(BitWidth);
3863 break;
3864 }
3865 case ISD::ANY_EXTEND: {
3866 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3867 Known = Known.anyext(BitWidth);
3868 break;
3869 }
3870 case ISD::TRUNCATE: {
3871 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3872 Known = Known.trunc(BitWidth);
3873 break;
3874 }
3875 case ISD::AssertZext: {
3876 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3878 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3879 Known.Zero |= (~InMask);
3880 Known.One &= (~Known.Zero);
3881 break;
3882 }
3883 case ISD::AssertAlign: {
3884 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3885 assert(LogOfAlign != 0);
3886
3887 // TODO: Should use maximum with source
3888 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3889 // well as clearing one bits.
3890 Known.Zero.setLowBits(LogOfAlign);
3891 Known.One.clearLowBits(LogOfAlign);
3892 break;
3893 }
3894 case ISD::FGETSIGN:
3895 // All bits are zero except the low bit.
3896 Known.Zero.setBitsFrom(1);
3897 break;
3898 case ISD::ADD:
3899 case ISD::SUB: {
3900 SDNodeFlags Flags = Op.getNode()->getFlags();
3901 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3902 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3904 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3905 Flags.hasNoUnsignedWrap(), Known, Known2);
3906 break;
3907 }
3908 case ISD::USUBO:
3909 case ISD::SSUBO:
3910 case ISD::USUBO_CARRY:
3911 case ISD::SSUBO_CARRY:
3912 if (Op.getResNo() == 1) {
3913 // If we know the result of a setcc has the top bits zero, use this info.
3914 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3916 BitWidth > 1)
3917 Known.Zero.setBitsFrom(1);
3918 break;
3919 }
3920 [[fallthrough]];
3921 case ISD::SUBC: {
3922 assert(Op.getResNo() == 0 &&
3923 "We only compute knownbits for the difference here.");
3924
3925 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
3926 KnownBits Borrow(1);
3927 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3928 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3929 // Borrow has bit width 1
3930 Borrow = Borrow.trunc(1);
3931 } else {
3932 Borrow.setAllZero();
3933 }
3934
3935 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3936 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3937 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
3938 break;
3939 }
3940 case ISD::UADDO:
3941 case ISD::SADDO:
3942 case ISD::UADDO_CARRY:
3943 case ISD::SADDO_CARRY:
3944 if (Op.getResNo() == 1) {
3945 // If we know the result of a setcc has the top bits zero, use this info.
3946 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3948 BitWidth > 1)
3949 Known.Zero.setBitsFrom(1);
3950 break;
3951 }
3952 [[fallthrough]];
3953 case ISD::ADDC:
3954 case ISD::ADDE: {
3955 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3956
3957 // With ADDE and UADDO_CARRY, a carry bit may be added in.
3958 KnownBits Carry(1);
3959 if (Opcode == ISD::ADDE)
3960 // Can't track carry from glue, set carry to unknown.
3961 Carry.resetAll();
3962 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3963 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3964 // Carry has bit width 1
3965 Carry = Carry.trunc(1);
3966 } else {
3967 Carry.setAllZero();
3968 }
3969
3970 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3971 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3972 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3973 break;
3974 }
3975 case ISD::UDIV: {
3976 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3977 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3978 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
3979 break;
3980 }
3981 case ISD::SDIV: {
3982 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3983 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3984 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
3985 break;
3986 }
3987 case ISD::SREM: {
3988 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3989 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3990 Known = KnownBits::srem(Known, Known2);
3991 break;
3992 }
3993 case ISD::UREM: {
3994 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3995 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3996 Known = KnownBits::urem(Known, Known2);
3997 break;
3998 }
3999 case ISD::EXTRACT_ELEMENT: {
4000 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4001 const unsigned Index = Op.getConstantOperandVal(1);
4002 const unsigned EltBitWidth = Op.getValueSizeInBits();
4003
4004 // Remove low part of known bits mask
4005 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4006 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4007
4008 // Remove high part of known bit mask
4009 Known = Known.trunc(EltBitWidth);
4010 break;
4011 }
4013 SDValue InVec = Op.getOperand(0);
4014 SDValue EltNo = Op.getOperand(1);
4015 EVT VecVT = InVec.getValueType();
4016 // computeKnownBits not yet implemented for scalable vectors.
4017 if (VecVT.isScalableVector())
4018 break;
4019 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4020 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4021
4022 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4023 // anything about the extended bits.
4024 if (BitWidth > EltBitWidth)
4025 Known = Known.trunc(EltBitWidth);
4026
4027 // If we know the element index, just demand that vector element, else for
4028 // an unknown element index, ignore DemandedElts and demand them all.
4029 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4030 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4031 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4032 DemandedSrcElts =
4033 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4034
4035 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4036 if (BitWidth > EltBitWidth)
4037 Known = Known.anyext(BitWidth);
4038 break;
4039 }
4041 if (Op.getValueType().isScalableVector())
4042 break;
4043
4044 // If we know the element index, split the demand between the
4045 // source vector and the inserted element, otherwise assume we need
4046 // the original demanded vector elements and the value.
4047 SDValue InVec = Op.getOperand(0);
4048 SDValue InVal = Op.getOperand(1);
4049 SDValue EltNo = Op.getOperand(2);
4050 bool DemandedVal = true;
4051 APInt DemandedVecElts = DemandedElts;
4052 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4053 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4054 unsigned EltIdx = CEltNo->getZExtValue();
4055 DemandedVal = !!DemandedElts[EltIdx];
4056 DemandedVecElts.clearBit(EltIdx);
4057 }
4058 Known.One.setAllBits();
4059 Known.Zero.setAllBits();
4060 if (DemandedVal) {
4061 Known2 = computeKnownBits(InVal, Depth + 1);
4062 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4063 }
4064 if (!!DemandedVecElts) {
4065 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4066 Known = Known.intersectWith(Known2);
4067 }
4068 break;
4069 }
4070 case ISD::BITREVERSE: {
4071 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4072 Known = Known2.reverseBits();
4073 break;
4074 }
4075 case ISD::BSWAP: {
4076 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4077 Known = Known2.byteSwap();
4078 break;
4079 }
4080 case ISD::ABS: {
4081 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4082 Known = Known2.abs();
4083 Known.Zero.setHighBits(
4084 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4085 break;
4086 }
4087 case ISD::USUBSAT: {
4088 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4089 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4090 Known = KnownBits::usub_sat(Known, Known2);
4091 break;
4092 }
4093 case ISD::UMIN: {
4094 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4095 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4096 Known = KnownBits::umin(Known, Known2);
4097 break;
4098 }
4099 case ISD::UMAX: {
4100 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4101 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4102 Known = KnownBits::umax(Known, Known2);
4103 break;
4104 }
4105 case ISD::SMIN:
4106 case ISD::SMAX: {
4107 // If we have a clamp pattern, we know that the number of sign bits will be
4108 // the minimum of the clamp min/max range.
4109 bool IsMax = (Opcode == ISD::SMAX);
4110 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4111 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4112 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4113 CstHigh =
4114 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4115 if (CstLow && CstHigh) {
4116 if (!IsMax)
4117 std::swap(CstLow, CstHigh);
4118
4119 const APInt &ValueLow = CstLow->getAPIntValue();
4120 const APInt &ValueHigh = CstHigh->getAPIntValue();
4121 if (ValueLow.sle(ValueHigh)) {
4122 unsigned LowSignBits = ValueLow.getNumSignBits();
4123 unsigned HighSignBits = ValueHigh.getNumSignBits();
4124 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4125 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4126 Known.One.setHighBits(MinSignBits);
4127 break;
4128 }
4129 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4130 Known.Zero.setHighBits(MinSignBits);
4131 break;
4132 }
4133 }
4134 }
4135
4136 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4137 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4138 if (IsMax)
4139 Known = KnownBits::smax(Known, Known2);
4140 else
4141 Known = KnownBits::smin(Known, Known2);
4142
4143 // For SMAX, if CstLow is non-negative we know the result will be
4144 // non-negative and thus all sign bits are 0.
4145 // TODO: There's an equivalent of this for smin with negative constant for
4146 // known ones.
4147 if (IsMax && CstLow) {
4148 const APInt &ValueLow = CstLow->getAPIntValue();
4149 if (ValueLow.isNonNegative()) {
4150 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4151 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4152 }
4153 }
4154
4155 break;
4156 }
4157 case ISD::UINT_TO_FP: {
4158 Known.makeNonNegative();
4159 break;
4160 }
4161 case ISD::SINT_TO_FP: {
4162 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4163 if (Known2.isNonNegative())
4164 Known.makeNonNegative();
4165 else if (Known2.isNegative())
4166 Known.makeNegative();
4167 break;
4168 }
4169 case ISD::FP_TO_UINT_SAT: {
4170 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4171 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4173 break;
4174 }
4176 if (Op.getResNo() == 1) {
4177 // The boolean result conforms to getBooleanContents.
4178 // If we know the result of a setcc has the top bits zero, use this info.
4179 // We know that we have an integer-based boolean since these operations
4180 // are only available for integer.
4181 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4183 BitWidth > 1)
4184 Known.Zero.setBitsFrom(1);
4185 break;
4186 }
4187 [[fallthrough]];
4189 case ISD::ATOMIC_SWAP:
4201 case ISD::ATOMIC_LOAD: {
4202 unsigned MemBits =
4203 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4204 // If we are looking at the loaded value.
4205 if (Op.getResNo() == 0) {
4207 Known.Zero.setBitsFrom(MemBits);
4208 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4209 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4210 Known.Zero.setBitsFrom(MemBits);
4211 }
4212 break;
4213 }
4214 case ISD::FrameIndex:
4216 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4217 Known, getMachineFunction());
4218 break;
4219
4220 default:
4221 if (Opcode < ISD::BUILTIN_OP_END)
4222 break;
4223 [[fallthrough]];
4227 // TODO: Probably okay to remove after audit; here to reduce change size
4228 // in initial enablement patch for scalable vectors
4229 if (Op.getValueType().isScalableVector())
4230 break;
4231
4232 // Allow the target to implement this method for its nodes.
4233 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4234 break;
4235 }
4236
4237 return Known;
4238}
4239
4240/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4242 switch (OR) {
4250 }
4251 llvm_unreachable("Unknown OverflowResult");
4252}
4253
4256 // X + 0 never overflow
4257 if (isNullConstant(N1))
4258 return OFK_Never;
4259
4260 // If both operands each have at least two sign bits, the addition
4261 // cannot overflow.
4262 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4263 return OFK_Never;
4264
4265 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4266 return OFK_Sometime;
4267}
4268
4271 // X + 0 never overflow
4272 if (isNullConstant(N1))
4273 return OFK_Never;
4274
4275 // mulhi + 1 never overflow
4276 KnownBits N1Known = computeKnownBits(N1);
4277 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4278 N1Known.getMaxValue().ult(2))
4279 return OFK_Never;
4280
4281 KnownBits N0Known = computeKnownBits(N0);
4282 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4283 N0Known.getMaxValue().ult(2))
4284 return OFK_Never;
4285
4286 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4287 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4288 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4289 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4290}
4291
4294 // X - 0 never overflow
4295 if (isNullConstant(N1))
4296 return OFK_Never;
4297
4298 // If both operands each have at least two sign bits, the subtraction
4299 // cannot overflow.
4300 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4301 return OFK_Never;
4302
4303 KnownBits N0Known = computeKnownBits(N0);
4304 KnownBits N1Known = computeKnownBits(N1);
4305 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4306 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4307 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4308}
4309
4312 // X - 0 never overflow
4313 if (isNullConstant(N1))
4314 return OFK_Never;
4315
4316 KnownBits N0Known = computeKnownBits(N0);
4317 KnownBits N1Known = computeKnownBits(N1);
4318 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4319 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4320 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4321}
4322
4325 // X * 0 and X * 1 never overflow.
4326 if (isNullConstant(N1) || isOneConstant(N1))
4327 return OFK_Never;
4328
4329 KnownBits N0Known = computeKnownBits(N0);
4330 KnownBits N1Known = computeKnownBits(N1);
4331 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4332 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4333 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4334}
4335
4338 // X * 0 and X * 1 never overflow.
4339 if (isNullConstant(N1) || isOneConstant(N1))
4340 return OFK_Never;
4341
4342 // Get the size of the result.
4343 unsigned BitWidth = N0.getScalarValueSizeInBits();
4344
4345 // Sum of the sign bits.
4346 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4347
4348 // If we have enough sign bits, then there's no overflow.
4349 if (SignBits > BitWidth + 1)
4350 return OFK_Never;
4351
4352 if (SignBits == BitWidth + 1) {
4353 // The overflow occurs when the true multiplication of the
4354 // the operands is the minimum negative number.
4355 KnownBits N0Known = computeKnownBits(N0);
4356 KnownBits N1Known = computeKnownBits(N1);
4357 // If one of the operands is non-negative, then there's no
4358 // overflow.
4359 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4360 return OFK_Never;
4361 }
4362
4363 return OFK_Sometime;
4364}
4365
4367 if (Depth >= MaxRecursionDepth)
4368 return false; // Limit search depth.
4369
4370 EVT OpVT = Val.getValueType();
4371 unsigned BitWidth = OpVT.getScalarSizeInBits();
4372
4373 // Is the constant a known power of 2?
4375 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4376 }))
4377 return true;
4378
4379 // A left-shift of a constant one will have exactly one bit set because
4380 // shifting the bit off the end is undefined.
4381 if (Val.getOpcode() == ISD::SHL) {
4382 auto *C = isConstOrConstSplat(Val.getOperand(0));
4383 if (C && C->getAPIntValue() == 1)
4384 return true;
4385 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4386 isKnownNeverZero(Val, Depth);
4387 }
4388
4389 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4390 // one bit set.
4391 if (Val.getOpcode() == ISD::SRL) {
4392 auto *C = isConstOrConstSplat(Val.getOperand(0));
4393 if (C && C->getAPIntValue().isSignMask())
4394 return true;
4395 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4396 isKnownNeverZero(Val, Depth);
4397 }
4398
4399 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4400 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4401
4402 // Are all operands of a build vector constant powers of two?
4403 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4404 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4405 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4406 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4407 return false;
4408 }))
4409 return true;
4410
4411 // Is the operand of a splat vector a constant power of two?
4412 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4413 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4414 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4415 return true;
4416
4417 // vscale(power-of-two) is a power-of-two for some targets
4418 if (Val.getOpcode() == ISD::VSCALE &&
4419 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4421 return true;
4422
4423 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4424 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4425 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4427
4428 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4429 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4431
4432 // Looking for `x & -x` pattern:
4433 // If x == 0:
4434 // x & -x -> 0
4435 // If x != 0:
4436 // x & -x -> non-zero pow2
4437 // so if we find the pattern return whether we know `x` is non-zero.
4438 SDValue X;
4439 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4440 return isKnownNeverZero(X, Depth);
4441
4442 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4443 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4444
4445 // More could be done here, though the above checks are enough
4446 // to handle some common cases.
4447 return false;
4448}
4449
4451 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4452 return C1->getValueAPF().getExactLog2Abs() >= 0;
4453
4454 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4455 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4456
4457 return false;
4458}
4459
4461 EVT VT = Op.getValueType();
4462
4463 // Since the number of lanes in a scalable vector is unknown at compile time,
4464 // we track one bit which is implicitly broadcast to all lanes. This means
4465 // that all lanes in a scalable vector are considered demanded.
4466 APInt DemandedElts = VT.isFixedLengthVector()
4468 : APInt(1, 1);
4469 return ComputeNumSignBits(Op, DemandedElts, Depth);
4470}
4471
4472unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4473 unsigned Depth) const {
4474 EVT VT = Op.getValueType();
4475 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4476 unsigned VTBits = VT.getScalarSizeInBits();
4477 unsigned NumElts = DemandedElts.getBitWidth();
4478 unsigned Tmp, Tmp2;
4479 unsigned FirstAnswer = 1;
4480
4481 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4482 const APInt &Val = C->getAPIntValue();
4483 return Val.getNumSignBits();
4484 }
4485
4486 if (Depth >= MaxRecursionDepth)
4487 return 1; // Limit search depth.
4488
4489 if (!DemandedElts)
4490 return 1; // No demanded elts, better to assume we don't know anything.
4491
4492 unsigned Opcode = Op.getOpcode();
4493 switch (Opcode) {
4494 default: break;
4495 case ISD::AssertSext:
4496 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4497 return VTBits-Tmp+1;
4498 case ISD::AssertZext:
4499 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4500 return VTBits-Tmp;
4501 case ISD::MERGE_VALUES:
4502 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4503 Depth + 1);
4504 case ISD::SPLAT_VECTOR: {
4505 // Check if the sign bits of source go down as far as the truncated value.
4506 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4507 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4508 if (NumSrcSignBits > (NumSrcBits - VTBits))
4509 return NumSrcSignBits - (NumSrcBits - VTBits);
4510 break;
4511 }
4512 case ISD::BUILD_VECTOR:
4513 assert(!VT.isScalableVector());
4514 Tmp = VTBits;
4515 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4516 if (!DemandedElts[i])
4517 continue;
4518
4519 SDValue SrcOp = Op.getOperand(i);
4520 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4521 // for constant nodes to ensure we only look at the sign bits.
4522 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4523 APInt T = C->getAPIntValue().trunc(VTBits);
4524 Tmp2 = T.getNumSignBits();
4525 } else {
4526 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4527
4528 if (SrcOp.getValueSizeInBits() != VTBits) {
4529 assert(SrcOp.getValueSizeInBits() > VTBits &&
4530 "Expected BUILD_VECTOR implicit truncation");
4531 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4532 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4533 }
4534 }
4535 Tmp = std::min(Tmp, Tmp2);
4536 }
4537 return Tmp;
4538
4539 case ISD::VECTOR_SHUFFLE: {
4540 // Collect the minimum number of sign bits that are shared by every vector
4541 // element referenced by the shuffle.
4542 APInt DemandedLHS, DemandedRHS;
4543 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4544 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4545 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4546 DemandedLHS, DemandedRHS))
4547 return 1;
4548
4549 Tmp = std::numeric_limits<unsigned>::max();
4550 if (!!DemandedLHS)
4551 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4552 if (!!DemandedRHS) {
4553 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4554 Tmp = std::min(Tmp, Tmp2);
4555 }
4556 // If we don't know anything, early out and try computeKnownBits fall-back.
4557 if (Tmp == 1)
4558 break;
4559 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4560 return Tmp;
4561 }
4562
4563 case ISD::BITCAST: {
4564 if (VT.isScalableVector())
4565 break;
4566 SDValue N0 = Op.getOperand(0);
4567 EVT SrcVT = N0.getValueType();
4568 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4569
4570 // Ignore bitcasts from unsupported types..
4571 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4572 break;
4573
4574 // Fast handling of 'identity' bitcasts.
4575 if (VTBits == SrcBits)
4576 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4577
4578 bool IsLE = getDataLayout().isLittleEndian();
4579
4580 // Bitcast 'large element' scalar/vector to 'small element' vector.
4581 if ((SrcBits % VTBits) == 0) {
4582 assert(VT.isVector() && "Expected bitcast to vector");
4583
4584 unsigned Scale = SrcBits / VTBits;
4585 APInt SrcDemandedElts =
4586 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4587
4588 // Fast case - sign splat can be simply split across the small elements.
4589 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4590 if (Tmp == SrcBits)
4591 return VTBits;
4592
4593 // Slow case - determine how far the sign extends into each sub-element.
4594 Tmp2 = VTBits;
4595 for (unsigned i = 0; i != NumElts; ++i)
4596 if (DemandedElts[i]) {
4597 unsigned SubOffset = i % Scale;
4598 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4599 SubOffset = SubOffset * VTBits;
4600 if (Tmp <= SubOffset)
4601 return 1;
4602 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4603 }
4604 return Tmp2;
4605 }
4606 break;
4607 }
4608
4610 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4611 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4612 return VTBits - Tmp + 1;
4613 case ISD::SIGN_EXTEND:
4614 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4615 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4617 // Max of the input and what this extends.
4618 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4619 Tmp = VTBits-Tmp+1;
4620 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4621 return std::max(Tmp, Tmp2);
4623 if (VT.isScalableVector())
4624 break;
4625 SDValue Src = Op.getOperand(0);
4626 EVT SrcVT = Src.getValueType();
4627 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4628 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4629 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4630 }
4631 case ISD::SRA:
4632 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4633 // SRA X, C -> adds C sign bits.
4634 if (std::optional<uint64_t> ShAmt =
4635 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4636 Tmp = std::min<uint64_t>(Tmp + *ShAmt, VTBits);
4637 return Tmp;
4638 case ISD::SHL:
4639 if (std::optional<ConstantRange> ShAmtRange =
4640 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4641 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4642 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4643 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4644 // shifted out, then we can compute the number of sign bits for the
4645 // operand being extended. A future improvement could be to pass along the
4646 // "shifted left by" information in the recursive calls to
4647 // ComputeKnownSignBits. Allowing us to handle this more generically.
4648 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4649 SDValue Ext = Op.getOperand(0);
4650 EVT ExtVT = Ext.getValueType();
4651 SDValue Extendee = Ext.getOperand(0);
4652 EVT ExtendeeVT = Extendee.getValueType();
4653 uint64_t SizeDifference =
4654 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4655 if (SizeDifference <= MinShAmt) {
4656 Tmp = SizeDifference +
4657 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4658 if (MaxShAmt < Tmp)
4659 return Tmp - MaxShAmt;
4660 }
4661 }
4662 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4663 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4664 if (MaxShAmt < Tmp)
4665 return Tmp - MaxShAmt;
4666 }
4667 break;
4668 case ISD::AND:
4669 case ISD::OR:
4670 case ISD::XOR: // NOT is handled here.
4671 // Logical binary ops preserve the number of sign bits at the worst.
4672 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4673 if (Tmp != 1) {
4674 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4675 FirstAnswer = std::min(Tmp, Tmp2);
4676 // We computed what we know about the sign bits as our first
4677 // answer. Now proceed to the generic code that uses
4678 // computeKnownBits, and pick whichever answer is better.
4679 }
4680 break;
4681
4682 case ISD::SELECT:
4683 case ISD::VSELECT:
4684 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4685 if (Tmp == 1) return 1; // Early out.
4686 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4687 return std::min(Tmp, Tmp2);
4688 case ISD::SELECT_CC:
4689 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4690 if (Tmp == 1) return 1; // Early out.
4691 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4692 return std::min(Tmp, Tmp2);
4693
4694 case ISD::SMIN:
4695 case ISD::SMAX: {
4696 // If we have a clamp pattern, we know that the number of sign bits will be
4697 // the minimum of the clamp min/max range.
4698 bool IsMax = (Opcode == ISD::SMAX);
4699 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4700 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4701 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4702 CstHigh =
4703 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4704 if (CstLow && CstHigh) {
4705 if (!IsMax)
4706 std::swap(CstLow, CstHigh);
4707 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4708 Tmp = CstLow->getAPIntValue().getNumSignBits();
4709 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4710 return std::min(Tmp, Tmp2);
4711 }
4712 }
4713
4714 // Fallback - just get the minimum number of sign bits of the operands.
4715 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4716 if (Tmp == 1)
4717 return 1; // Early out.
4718 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4719 return std::min(Tmp, Tmp2);
4720 }
4721 case ISD::UMIN:
4722 case ISD::UMAX:
4723 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4724 if (Tmp == 1)
4725 return 1; // Early out.
4726 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4727 return std::min(Tmp, Tmp2);
4728 case ISD::SSUBO_CARRY:
4729 case ISD::USUBO_CARRY:
4730 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4731 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4732 return VTBits;
4733 [[fallthrough]];
4734 case ISD::SADDO:
4735 case ISD::UADDO:
4736 case ISD::SADDO_CARRY:
4737 case ISD::UADDO_CARRY:
4738 case ISD::SSUBO:
4739 case ISD::USUBO:
4740 case ISD::SMULO:
4741 case ISD::UMULO:
4742 if (Op.getResNo() != 1)
4743 break;
4744 // The boolean result conforms to getBooleanContents. Fall through.
4745 // If setcc returns 0/-1, all bits are sign bits.
4746 // We know that we have an integer-based boolean since these operations
4747 // are only available for integer.
4748 if (TLI->getBooleanContents(VT.isVector(), false) ==
4750 return VTBits;
4751 break;
4752 case ISD::SETCC:
4753 case ISD::SETCCCARRY:
4754 case ISD::STRICT_FSETCC:
4755 case ISD::STRICT_FSETCCS: {
4756 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4757 // If setcc returns 0/-1, all bits are sign bits.
4758 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4760 return VTBits;
4761 break;
4762 }
4763 case ISD::ROTL:
4764 case ISD::ROTR:
4765 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4766
4767 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4768 if (Tmp == VTBits)
4769 return VTBits;
4770
4771 if (ConstantSDNode *C =
4772 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4773 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4774
4775 // Handle rotate right by N like a rotate left by 32-N.
4776 if (Opcode == ISD::ROTR)
4777 RotAmt = (VTBits - RotAmt) % VTBits;
4778
4779 // If we aren't rotating out all of the known-in sign bits, return the
4780 // number that are left. This handles rotl(sext(x), 1) for example.
4781 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4782 }
4783 break;
4784 case ISD::ADD:
4785 case ISD::ADDC:
4786 // Add can have at most one carry bit. Thus we know that the output
4787 // is, at worst, one more bit than the inputs.
4788 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4789 if (Tmp == 1) return 1; // Early out.
4790
4791 // Special case decrementing a value (ADD X, -1):
4792 if (ConstantSDNode *CRHS =
4793 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4794 if (CRHS->isAllOnes()) {
4795 KnownBits Known =
4796 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4797
4798 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4799 // sign bits set.
4800 if ((Known.Zero | 1).isAllOnes())
4801 return VTBits;
4802
4803 // If we are subtracting one from a positive number, there is no carry
4804 // out of the result.
4805 if (Known.isNonNegative())
4806 return Tmp;
4807 }
4808
4809 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4810 if (Tmp2 == 1) return 1; // Early out.
4811 return std::min(Tmp, Tmp2) - 1;
4812 case ISD::SUB:
4813 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4814 if (Tmp2 == 1) return 1; // Early out.
4815
4816 // Handle NEG.
4817 if (ConstantSDNode *CLHS =
4818 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4819 if (CLHS->isZero()) {
4820 KnownBits Known =
4821 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4822 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4823 // sign bits set.
4824 if ((Known.Zero | 1).isAllOnes())
4825 return VTBits;
4826
4827 // If the input is known to be positive (the sign bit is known clear),
4828 // the output of the NEG has the same number of sign bits as the input.
4829 if (Known.isNonNegative())
4830 return Tmp2;
4831
4832 // Otherwise, we treat this like a SUB.
4833 }
4834
4835 // Sub can have at most one carry bit. Thus we know that the output
4836 // is, at worst, one more bit than the inputs.
4837 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4838 if (Tmp == 1) return 1; // Early out.
4839 return std::min(Tmp, Tmp2) - 1;
4840 case ISD::MUL: {
4841 // The output of the Mul can be at most twice the valid bits in the inputs.
4842 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4843 if (SignBitsOp0 == 1)
4844 break;
4845 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
4846 if (SignBitsOp1 == 1)
4847 break;
4848 unsigned OutValidBits =
4849 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
4850 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
4851 }
4852 case ISD::AVGCEILS:
4853 case ISD::AVGFLOORS:
4854 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4855 if (Tmp == 1)
4856 return 1; // Early out.
4857 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4858 return std::min(Tmp, Tmp2);
4859 case ISD::SREM:
4860 // The sign bit is the LHS's sign bit, except when the result of the
4861 // remainder is zero. The magnitude of the result should be less than or
4862 // equal to the magnitude of the LHS. Therefore, the result should have
4863 // at least as many sign bits as the left hand side.
4864 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4865 case ISD::TRUNCATE: {
4866 // Check if the sign bits of source go down as far as the truncated value.
4867 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
4868 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4869 if (NumSrcSignBits > (NumSrcBits - VTBits))
4870 return NumSrcSignBits - (NumSrcBits - VTBits);
4871 break;
4872 }
4873 case ISD::EXTRACT_ELEMENT: {
4874 if (VT.isScalableVector())
4875 break;
4876 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
4877 const int BitWidth = Op.getValueSizeInBits();
4878 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
4879
4880 // Get reverse index (starting from 1), Op1 value indexes elements from
4881 // little end. Sign starts at big end.
4882 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
4883
4884 // If the sign portion ends in our element the subtraction gives correct
4885 // result. Otherwise it gives either negative or > bitwidth result
4886 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
4887 }
4889 if (VT.isScalableVector())
4890 break;
4891 // If we know the element index, split the demand between the
4892 // source vector and the inserted element, otherwise assume we need
4893 // the original demanded vector elements and the value.
4894 SDValue InVec = Op.getOperand(0);
4895 SDValue InVal = Op.getOperand(1);
4896 SDValue EltNo = Op.getOperand(2);
4897 bool DemandedVal = true;
4898 APInt DemandedVecElts = DemandedElts;
4899 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4900 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4901 unsigned EltIdx = CEltNo->getZExtValue();
4902 DemandedVal = !!DemandedElts[EltIdx];
4903 DemandedVecElts.clearBit(EltIdx);
4904 }
4905 Tmp = std::numeric_limits<unsigned>::max();
4906 if (DemandedVal) {
4907 // TODO - handle implicit truncation of inserted elements.
4908 if (InVal.getScalarValueSizeInBits() != VTBits)
4909 break;
4910 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
4911 Tmp = std::min(Tmp, Tmp2);
4912 }
4913 if (!!DemandedVecElts) {
4914 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
4915 Tmp = std::min(Tmp, Tmp2);
4916 }
4917 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4918 return Tmp;
4919 }
4921 assert(!VT.isScalableVector());
4922 SDValue InVec = Op.getOperand(0);
4923 SDValue EltNo = Op.getOperand(1);
4924 EVT VecVT = InVec.getValueType();
4925 // ComputeNumSignBits not yet implemented for scalable vectors.
4926 if (VecVT.isScalableVector())
4927 break;
4928 const unsigned BitWidth = Op.getValueSizeInBits();
4929 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
4930 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4931
4932 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
4933 // anything about sign bits. But if the sizes match we can derive knowledge
4934 // about sign bits from the vector operand.
4935 if (BitWidth != EltBitWidth)
4936 break;
4937
4938 // If we know the element index, just demand that vector element, else for
4939 // an unknown element index, ignore DemandedElts and demand them all.
4940 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4941 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4942 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4943 DemandedSrcElts =
4944 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4945
4946 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
4947 }
4949 // Offset the demanded elts by the subvector index.
4950 SDValue Src = Op.getOperand(0);
4951 // Bail until we can represent demanded elements for scalable vectors.
4952 if (Src.getValueType().isScalableVector())
4953 break;
4954 uint64_t Idx = Op.getConstantOperandVal(1);
4955 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
4956 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
4957 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4958 }
4959 case ISD::CONCAT_VECTORS: {
4960 if (VT.isScalableVector())
4961 break;
4962 // Determine the minimum number of sign bits across all demanded
4963 // elts of the input vectors. Early out if the result is already 1.
4964 Tmp = std::numeric_limits<unsigned>::max();
4965 EVT SubVectorVT = Op.getOperand(0).getValueType();
4966 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
4967 unsigned NumSubVectors = Op.getNumOperands();
4968 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
4969 APInt DemandedSub =
4970 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
4971 if (!DemandedSub)
4972 continue;
4973 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
4974 Tmp = std::min(Tmp, Tmp2);
4975 }
4976 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4977 return Tmp;
4978 }
4979 case ISD::INSERT_SUBVECTOR: {
4980 if (VT.isScalableVector())
4981 break;
4982 // Demand any elements from the subvector and the remainder from the src its
4983 // inserted into.
4984 SDValue Src = Op.getOperand(0);
4985 SDValue Sub = Op.getOperand(1);
4986 uint64_t Idx = Op.getConstantOperandVal(2);
4987 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
4988 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
4989 APInt DemandedSrcElts = DemandedElts;
4990 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
4991
4992 Tmp = std::numeric_limits<unsigned>::max();
4993 if (!!DemandedSubElts) {
4994 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
4995 if (Tmp == 1)
4996 return 1; // early-out
4997 }
4998 if (!!DemandedSrcElts) {
4999 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5000 Tmp = std::min(Tmp, Tmp2);
5001 }
5002 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5003 return Tmp;
5004 }
5005 case ISD::LOAD: {
5006 LoadSDNode *LD = cast<LoadSDNode>(Op);
5007 if (const MDNode *Ranges = LD->getRanges()) {
5008 if (DemandedElts != 1)
5009 break;
5010
5012 if (VTBits > CR.getBitWidth()) {
5013 switch (LD->getExtensionType()) {
5014 case ISD::SEXTLOAD:
5015 CR = CR.signExtend(VTBits);
5016 break;
5017 case ISD::ZEXTLOAD:
5018 CR = CR.zeroExtend(VTBits);
5019 break;
5020 default:
5021 break;
5022 }
5023 }
5024
5025 if (VTBits != CR.getBitWidth())
5026 break;
5027 return std::min(CR.getSignedMin().getNumSignBits(),
5029 }
5030
5031 break;
5032 }
5035 case ISD::ATOMIC_SWAP:
5047 case ISD::ATOMIC_LOAD: {
5048 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
5049 // If we are looking at the loaded value.
5050 if (Op.getResNo() == 0) {
5051 if (Tmp == VTBits)
5052 return 1; // early-out
5054 return VTBits - Tmp + 1;
5056 return VTBits - Tmp;
5057 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5058 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
5059 if (ETy == ISD::SEXTLOAD)
5060 return VTBits - Tmp + 1;
5061 if (ETy == ISD::ZEXTLOAD)
5062 return VTBits - Tmp;
5063 }
5064 }
5065 break;
5066 }
5067 }
5068
5069 // If we are looking at the loaded value of the SDNode.
5070 if (Op.getResNo() == 0) {
5071 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5072 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5073 unsigned ExtType = LD->getExtensionType();
5074 switch (ExtType) {
5075 default: break;
5076 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5077 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5078 return VTBits - Tmp + 1;
5079 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5080 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5081 return VTBits - Tmp;
5082 case ISD::NON_EXTLOAD:
5083 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5084 // We only need to handle vectors - computeKnownBits should handle
5085 // scalar cases.
5086 Type *CstTy = Cst->getType();
5087 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5088 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5089 VTBits == CstTy->getScalarSizeInBits()) {
5090 Tmp = VTBits;
5091 for (unsigned i = 0; i != NumElts; ++i) {
5092 if (!DemandedElts[i])
5093 continue;
5094 if (Constant *Elt = Cst->getAggregateElement(i)) {
5095 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5096 const APInt &Value = CInt->getValue();
5097 Tmp = std::min(Tmp, Value.getNumSignBits());
5098 continue;
5099 }
5100 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5101 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5102 Tmp = std::min(Tmp, Value.getNumSignBits());
5103 continue;
5104 }
5105 }
5106 // Unknown type. Conservatively assume no bits match sign bit.
5107 return 1;
5108 }
5109 return Tmp;
5110 }
5111 }
5112 break;
5113 }
5114 }
5115 }
5116
5117 // Allow the target to implement this method for its nodes.
5118 if (Opcode >= ISD::BUILTIN_OP_END ||
5119 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5120 Opcode == ISD::INTRINSIC_W_CHAIN ||
5121 Opcode == ISD::INTRINSIC_VOID) {
5122 // TODO: This can probably be removed once target code is audited. This
5123 // is here purely to reduce patch size and review complexity.
5124 if (!VT.isScalableVector()) {
5125 unsigned NumBits =
5126 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5127 if (NumBits > 1)
5128 FirstAnswer = std::max(FirstAnswer, NumBits);
5129 }
5130 }
5131
5132 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5133 // use this information.
5134 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5135 return std::max(FirstAnswer, Known.countMinSignBits());
5136}
5137
5139 unsigned Depth) const {
5140 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5141 return Op.getScalarValueSizeInBits() - SignBits + 1;
5142}
5143
5145 const APInt &DemandedElts,
5146 unsigned Depth) const {
5147 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5148 return Op.getScalarValueSizeInBits() - SignBits + 1;
5149}
5150
5152 unsigned Depth) const {
5153 // Early out for FREEZE.
5154 if (Op.getOpcode() == ISD::FREEZE)
5155 return true;
5156
5157 EVT VT = Op.getValueType();
5158 APInt DemandedElts = VT.isFixedLengthVector()
5160 : APInt(1, 1);
5161 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5162}
5163
5165 const APInt &DemandedElts,
5166 bool PoisonOnly,
5167 unsigned Depth) const {
5168 unsigned Opcode = Op.getOpcode();
5169
5170 // Early out for FREEZE.
5171 if (Opcode == ISD::FREEZE)
5172 return true;
5173
5174 if (Depth >= MaxRecursionDepth)
5175 return false; // Limit search depth.
5176
5177 if (isIntOrFPConstant(Op))
5178 return true;
5179
5180 switch (Opcode) {
5181 case ISD::CONDCODE:
5182 case ISD::VALUETYPE:
5183 case ISD::FrameIndex:
5185 case ISD::CopyFromReg:
5186 return true;
5187
5188 case ISD::UNDEF:
5189 return PoisonOnly;
5190
5191 case ISD::BUILD_VECTOR:
5192 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5193 // this shouldn't affect the result.
5194 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5195 if (!DemandedElts[i])
5196 continue;
5198 Depth + 1))
5199 return false;
5200 }
5201 return true;
5202
5203 case ISD::SPLAT_VECTOR:
5204 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5205 Depth + 1);
5206
5207 case ISD::VECTOR_SHUFFLE: {
5208 APInt DemandedLHS, DemandedRHS;
5209 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5210 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5211 DemandedElts, DemandedLHS, DemandedRHS,
5212 /*AllowUndefElts=*/false))
5213 return false;
5214 if (!DemandedLHS.isZero() &&
5215 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5216 PoisonOnly, Depth + 1))
5217 return false;
5218 if (!DemandedRHS.isZero() &&
5219 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5220 PoisonOnly, Depth + 1))
5221 return false;
5222 return true;
5223 }
5224
5225 // TODO: Search for noundef attributes from library functions.
5226
5227 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5228
5229 default:
5230 // Allow the target to implement this method for its nodes.
5231 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5232 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5234 Op, DemandedElts, *this, PoisonOnly, Depth);
5235 break;
5236 }
5237
5238 // If Op can't create undef/poison and none of its operands are undef/poison
5239 // then Op is never undef/poison.
5240 // NOTE: TargetNodes can handle this in themselves in
5241 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5242 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5243 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5244 Depth) &&
5245 all_of(Op->ops(), [&](SDValue V) {
5246 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5247 });
5248}
5249
5251 bool ConsiderFlags,
5252 unsigned Depth) const {
5253 EVT VT = Op.getValueType();
5254 APInt DemandedElts = VT.isFixedLengthVector()
5256 : APInt(1, 1);
5257 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5258 Depth);
5259}
5260
5262 bool PoisonOnly, bool ConsiderFlags,
5263 unsigned Depth) const {
5264 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5265 return true;
5266
5267 unsigned Opcode = Op.getOpcode();
5268 switch (Opcode) {
5269 case ISD::FREEZE:
5272 case ISD::SADDSAT:
5273 case ISD::UADDSAT:
5274 case ISD::SSUBSAT:
5275 case ISD::USUBSAT:
5276 case ISD::MULHU:
5277 case ISD::MULHS:
5278 case ISD::SMIN:
5279 case ISD::SMAX:
5280 case ISD::UMIN:
5281 case ISD::UMAX:
5282 case ISD::AND:
5283 case ISD::XOR:
5284 case ISD::ROTL:
5285 case ISD::ROTR:
5286 case ISD::FSHL:
5287 case ISD::FSHR:
5288 case ISD::BSWAP:
5289 case ISD::CTPOP:
5290 case ISD::BITREVERSE:
5291 case ISD::PARITY:
5292 case ISD::SIGN_EXTEND:
5293 case ISD::TRUNCATE:
5297 case ISD::BITCAST:
5298 case ISD::BUILD_VECTOR:
5299 case ISD::BUILD_PAIR:
5300 case ISD::SPLAT_VECTOR:
5301 return false;
5302
5303 case ISD::SELECT_CC:
5304 case ISD::SETCC: {
5305 // Integer setcc cannot create undef or poison.
5306 if (Op.getOperand(0).getValueType().isInteger())
5307 return false;
5308
5309 // FP compares are more complicated. They can create poison for nan/infinity
5310 // based on options and flags. The options and flags also cause special
5311 // nonan condition codes to be used. Those condition codes may be preserved
5312 // even if the nonan flag is dropped somewhere.
5313 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5314 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5315 if (((unsigned)CCCode & 0x10U))
5316 return true;
5317
5319 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5320 }
5321
5322 case ISD::OR:
5323 case ISD::ZERO_EXTEND:
5324 case ISD::ADD:
5325 case ISD::SUB:
5326 case ISD::MUL:
5327 // No poison except from flags (which is handled above)
5328 return false;
5329
5330 case ISD::SHL:
5331 case ISD::SRL:
5332 case ISD::SRA:
5333 // If the max shift amount isn't in range, then the shift can
5334 // create poison.
5335 return !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedElts,
5336 PoisonOnly, Depth + 1) ||
5337 !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5338
5340 // Check if we demand any upper (undef) elements.
5341 return !PoisonOnly && DemandedElts.ugt(1);
5342
5345 // Ensure that the element index is in bounds.
5346 EVT VecVT = Op.getOperand(0).getValueType();
5347 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5349 Depth + 1)) {
5350 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5351 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5352 }
5353 return true;
5354 }
5355
5356 case ISD::VECTOR_SHUFFLE: {
5357 // Check for any demanded shuffle element that is undef.
5358 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5359 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5360 if (Elt < 0 && DemandedElts[Idx])
5361 return true;
5362 return false;
5363 }
5364
5365 default:
5366 // Allow the target to implement this method for its nodes.
5367 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5368 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5370 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5371 break;
5372 }
5373
5374 // Be conservative and return true.
5375 return true;
5376}
5377
5378bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5379 unsigned Opcode = Op.getOpcode();
5380 if (Opcode == ISD::OR)
5381 return Op->getFlags().hasDisjoint() ||
5382 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5383 if (Opcode == ISD::XOR)
5384 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5385 return false;
5386}
5387
5389 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5390 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5391}
5392
5393bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5394 // If we're told that NaNs won't happen, assume they won't.
5395 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5396 return true;
5397
5398 if (Depth >= MaxRecursionDepth)
5399 return false; // Limit search depth.
5400
5401 // If the value is a constant, we can obviously see if it is a NaN or not.
5402 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5403 return !C->getValueAPF().isNaN() ||
5404 (SNaN && !C->getValueAPF().isSignaling());
5405 }
5406
5407 unsigned Opcode = Op.getOpcode();
5408 switch (Opcode) {
5409 case ISD::FADD:
5410 case ISD::FSUB:
5411 case ISD::FMUL:
5412 case ISD::FDIV:
5413 case ISD::FREM:
5414 case ISD::FSIN:
5415 case ISD::FCOS:
5416 case ISD::FTAN:
5417 case ISD::FASIN:
5418 case ISD::FACOS:
5419 case ISD::FATAN:
5420 case ISD::FSINH:
5421 case ISD::FCOSH:
5422 case ISD::FTANH:
5423 case ISD::FMA:
5424 case ISD::FMAD: {
5425 if (SNaN)
5426 return true;
5427 // TODO: Need isKnownNeverInfinity
5428 return false;
5429 }
5430 case ISD::FCANONICALIZE:
5431 case ISD::FEXP:
5432 case ISD::FEXP2:
5433 case ISD::FEXP10:
5434 case ISD::FTRUNC:
5435 case ISD::FFLOOR:
5436 case ISD::FCEIL:
5437 case ISD::FROUND:
5438 case ISD::FROUNDEVEN:
5439 case ISD::FRINT:
5440 case ISD::LRINT:
5441 case ISD::LLRINT:
5442 case ISD::FNEARBYINT:
5443 case ISD::FLDEXP: {
5444 if (SNaN)
5445 return true;
5446 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5447 }
5448 case ISD::FABS:
5449 case ISD::FNEG:
5450 case ISD::FCOPYSIGN: {
5451 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5452 }
5453 case ISD::SELECT:
5454 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5455 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5456 case ISD::FP_EXTEND:
5457 case ISD::FP_ROUND: {
5458 if (SNaN)
5459 return true;
5460 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5461 }
5462 case ISD::SINT_TO_FP:
5463 case ISD::UINT_TO_FP:
5464 return true;
5465 case ISD::FSQRT: // Need is known positive
5466 case ISD::FLOG:
5467 case ISD::FLOG2:
5468 case ISD::FLOG10:
5469 case ISD::FPOWI:
5470 case ISD::FPOW: {
5471 if (SNaN)
5472 return true;
5473 // TODO: Refine on operand
5474 return false;
5475 }
5476 case ISD::FMINNUM:
5477 case ISD::FMAXNUM:
5478 case ISD::FMINIMUMNUM:
5479 case ISD::FMAXIMUMNUM: {
5480 // Only one needs to be known not-nan, since it will be returned if the
5481 // other ends up being one.
5482 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5483 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5484 }
5485 case ISD::FMINNUM_IEEE:
5486 case ISD::FMAXNUM_IEEE: {
5487 if (SNaN)
5488 return true;
5489 // This can return a NaN if either operand is an sNaN, or if both operands
5490 // are NaN.
5491 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5492 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5493 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5494 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5495 }
5496 case ISD::FMINIMUM:
5497 case ISD::FMAXIMUM: {
5498 // TODO: Does this quiet or return the origina NaN as-is?
5499 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5500 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5501 }
5503 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5504 }
5505 case ISD::BUILD_VECTOR: {
5506 for (const SDValue &Opnd : Op->ops())
5507 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5508 return false;
5509 return true;
5510 }
5511 default:
5512 if (Opcode >= ISD::BUILTIN_OP_END ||
5513 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5514 Opcode == ISD::INTRINSIC_W_CHAIN ||
5515 Opcode == ISD::INTRINSIC_VOID) {
5516 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5517 }
5518
5519 return false;
5520 }
5521}
5522
5524 assert(Op.getValueType().isFloatingPoint() &&
5525 "Floating point type expected");
5526
5527 // If the value is a constant, we can obviously see if it is a zero or not.
5529 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5530}
5531
5533 if (Depth >= MaxRecursionDepth)
5534 return false; // Limit search depth.
5535
5536 assert(!Op.getValueType().isFloatingPoint() &&
5537 "Floating point types unsupported - use isKnownNeverZeroFloat");
5538
5539 // If the value is a constant, we can obviously see if it is a zero or not.
5541 [](ConstantSDNode *C) { return !C->isZero(); }))
5542 return true;
5543
5544 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5545 // some degree.
5546 switch (Op.getOpcode()) {
5547 default:
5548 break;
5549
5550 case ISD::OR:
5551 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5552 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5553
5554 case ISD::VSELECT:
5555 case ISD::SELECT:
5556 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5557 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5558
5559 case ISD::SHL: {
5560 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5561 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5562 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5563 // 1 << X is never zero.
5564 if (ValKnown.One[0])
5565 return true;
5566 // If max shift cnt of known ones is non-zero, result is non-zero.
5567 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5568 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5569 !ValKnown.One.shl(MaxCnt).isZero())
5570 return true;
5571 break;
5572 }
5573 case ISD::UADDSAT:
5574 case ISD::UMAX:
5575 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5576 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5577
5578 // For smin/smax: If either operand is known negative/positive
5579 // respectively we don't need the other to be known at all.
5580 case ISD::SMAX: {
5581 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5582 if (Op1.isStrictlyPositive())
5583 return true;
5584
5585 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5586 if (Op0.isStrictlyPositive())
5587 return true;
5588
5589 if (Op1.isNonZero() && Op0.isNonZero())
5590 return true;
5591
5592 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5593 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5594 }
5595 case ISD::SMIN: {
5596 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5597 if (Op1.isNegative())
5598 return true;
5599
5600 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5601 if (Op0.isNegative())
5602 return true;
5603
5604 if (Op1.isNonZero() && Op0.isNonZero())
5605 return true;
5606
5607 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5608 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5609 }
5610 case ISD::UMIN:
5611 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5612 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5613
5614 case ISD::ROTL:
5615 case ISD::ROTR:
5616 case ISD::BITREVERSE:
5617 case ISD::BSWAP:
5618 case ISD::CTPOP:
5619 case ISD::ABS:
5620 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5621
5622 case ISD::SRA:
5623 case ISD::SRL: {
5624 if (Op->getFlags().hasExact())
5625 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5626 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5627 if (ValKnown.isNegative())
5628 return true;
5629 // If max shift cnt of known ones is non-zero, result is non-zero.
5630 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5631 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5632 !ValKnown.One.lshr(MaxCnt).isZero())
5633 return true;
5634 break;
5635 }
5636 case ISD::UDIV:
5637 case ISD::SDIV:
5638 // div exact can only produce a zero if the dividend is zero.
5639 // TODO: For udiv this is also true if Op1 u<= Op0
5640 if (Op->getFlags().hasExact())
5641 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5642 break;
5643
5644 case ISD::ADD:
5645 if (Op->getFlags().hasNoUnsignedWrap())
5646 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5647 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5648 return true;
5649 // TODO: There are a lot more cases we can prove for add.
5650 break;
5651
5652 case ISD::SUB: {
5653 if (isNullConstant(Op.getOperand(0)))
5654 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5655
5656 std::optional<bool> ne =
5657 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5658 computeKnownBits(Op.getOperand(1), Depth + 1));
5659 return ne && *ne;
5660 }
5661
5662 case ISD::MUL:
5663 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5664 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5665 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5666 return true;
5667 break;
5668
5669 case ISD::ZERO_EXTEND:
5670 case ISD::SIGN_EXTEND:
5671 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5672 case ISD::VSCALE: {
5674 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
5675 ConstantRange CR =
5676 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
5677 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
5678 return true;
5679 break;
5680 }
5681 }
5682
5684}
5685
5687 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
5688 return !C1->isNegative();
5689
5690 return Op.getOpcode() == ISD::FABS;
5691}
5692
5694 // Check the obvious case.
5695 if (A == B) return true;
5696
5697 // For negative and positive zero.
5698 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5699 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5700 if (CA->isZero() && CB->isZero()) return true;
5701
5702 // Otherwise they may not be equal.
5703 return false;
5704}
5705
5706// Only bits set in Mask must be negated, other bits may be arbitrary.
5708 if (isBitwiseNot(V, AllowUndefs))
5709 return V.getOperand(0);
5710
5711 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5712 // bits in the non-extended part.
5713 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5714 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5715 return SDValue();
5716 SDValue ExtArg = V.getOperand(0);
5717 if (ExtArg.getScalarValueSizeInBits() >=
5718 MaskC->getAPIntValue().getActiveBits() &&
5719 isBitwiseNot(ExtArg, AllowUndefs) &&
5720 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
5721 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
5722 return ExtArg.getOperand(0).getOperand(0);
5723 return SDValue();
5724}
5725
5727 // Match masked merge pattern (X & ~M) op (Y & M)
5728 // Including degenerate case (X & ~M) op M
5729 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
5730 SDValue Other) {
5731 if (SDValue NotOperand =
5732 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
5733 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
5734 NotOperand->getOpcode() == ISD::TRUNCATE)
5735 NotOperand = NotOperand->getOperand(0);
5736
5737 if (Other == NotOperand)
5738 return true;
5739 if (Other->getOpcode() == ISD::AND)
5740 return NotOperand == Other->getOperand(0) ||
5741 NotOperand == Other->getOperand(1);
5742 }
5743 return false;
5744 };
5745
5746 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
5747 A = A->getOperand(0);
5748
5749 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
5750 B = B->getOperand(0);
5751
5752 if (A->getOpcode() == ISD::AND)
5753 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
5754 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
5755 return false;
5756}
5757
5758// FIXME: unify with llvm::haveNoCommonBitsSet.
5760 assert(A.getValueType() == B.getValueType() &&
5761 "Values must have the same type");
5764 return true;
5767}
5768
5769static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
5770 SelectionDAG &DAG) {
5771 if (cast<ConstantSDNode>(Step)->isZero())
5772 return DAG.getConstant(0, DL, VT);
5773
5774 return SDValue();
5775}
5776
5779 SelectionDAG &DAG) {
5780 int NumOps = Ops.size();
5781 assert(NumOps != 0 && "Can't build an empty vector!");
5782 assert(!VT.isScalableVector() &&
5783 "BUILD_VECTOR cannot be used with scalable types");
5784 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
5785 "Incorrect element count in BUILD_VECTOR!");
5786
5787 // BUILD_VECTOR of UNDEFs is UNDEF.
5788 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5789 return DAG.getUNDEF(VT);
5790
5791 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
5792 SDValue IdentitySrc;
5793 bool IsIdentity = true;
5794 for (int i = 0; i != NumOps; ++i) {
5795 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5796 Ops[i].getOperand(0).getValueType() != VT ||
5797 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
5798 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
5799 Ops[i].getConstantOperandAPInt(1) != i) {
5800 IsIdentity = false;
5801 break;
5802 }
5803 IdentitySrc = Ops[i].getOperand(0);
5804 }
5805 if (IsIdentity)
5806 return IdentitySrc;
5807
5808 return SDValue();
5809}
5810
5811/// Try to simplify vector concatenation to an input value, undef, or build
5812/// vector.
5815 SelectionDAG &DAG) {
5816 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
5817 assert(llvm::all_of(Ops,
5818 [Ops](SDValue Op) {
5819 return Ops[0].getValueType() == Op.getValueType();
5820 }) &&
5821 "Concatenation of vectors with inconsistent value types!");
5822 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
5823 VT.getVectorElementCount() &&
5824 "Incorrect element count in vector concatenation!");
5825
5826 if (Ops.size() == 1)
5827 return Ops[0];
5828
5829 // Concat of UNDEFs is UNDEF.
5830 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5831 return DAG.getUNDEF(VT);
5832
5833 // Scan the operands and look for extract operations from a single source
5834 // that correspond to insertion at the same location via this concatenation:
5835 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
5836 SDValue IdentitySrc;
5837 bool IsIdentity = true;
5838 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
5839 SDValue Op = Ops[i];
5840 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
5841 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
5842 Op.getOperand(0).getValueType() != VT ||
5843 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
5844 Op.getConstantOperandVal(1) != IdentityIndex) {
5845 IsIdentity = false;
5846 break;
5847 }
5848 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
5849 "Unexpected identity source vector for concat of extracts");
5850 IdentitySrc = Op.getOperand(0);
5851 }
5852 if (IsIdentity) {
5853 assert(IdentitySrc && "Failed to set source vector of extracts");
5854 return IdentitySrc;
5855 }
5856
5857 // The code below this point is only designed to work for fixed width
5858 // vectors, so we bail out for now.
5859 if (VT.isScalableVector())
5860 return SDValue();
5861
5862 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
5863 // simplified to one big BUILD_VECTOR.
5864 // FIXME: Add support for SCALAR_TO_VECTOR as well.
5865 EVT SVT = VT.getScalarType();
5867 for (SDValue Op : Ops) {
5868 EVT OpVT = Op.getValueType();
5869 if (Op.isUndef())
5870 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
5871 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
5872 Elts.append(Op->op_begin(), Op->op_end());
5873 else
5874 return SDValue();
5875 }
5876
5877 // BUILD_VECTOR requires all inputs to be of the same type, find the
5878 // maximum type and extend them all.
5879 for (SDValue Op : Elts)
5880 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
5881
5882 if (SVT.bitsGT(VT.getScalarType())) {
5883 for (SDValue &Op : Elts) {
5884 if (Op.isUndef())
5885 Op = DAG.getUNDEF(SVT);
5886 else
5887 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
5888 ? DAG.getZExtOrTrunc(Op, DL, SVT)
5889 : DAG.getSExtOrTrunc(Op, DL, SVT);
5890 }
5891 }
5892
5893 SDValue V = DAG.getBuildVector(VT, DL, Elts);
5894 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
5895 return V;
5896}
5897
5898/// Gets or creates the specified node.
5899SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
5900 SDVTList VTs = getVTList(VT);
5902 AddNodeIDNode(ID, Opcode, VTs, std::nullopt);
5903 void *IP = nullptr;
5904 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5905 return SDValue(E, 0);
5906
5907 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5908 CSEMap.InsertNode(N, IP);
5909
5910 InsertNode(N);
5911 SDValue V = SDValue(N, 0);
5912 NewSDValueDbgMsg(V, "Creating new node: ", this);
5913 return V;
5914}
5915
5916SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5917 SDValue N1) {
5918 SDNodeFlags Flags;
5919 if (Inserter)
5920 Flags = Inserter->getFlags();
5921 return getNode(Opcode, DL, VT, N1, Flags);
5922}
5923
5924SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5925 SDValue N1, const SDNodeFlags Flags) {
5926 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
5927
5928 // Constant fold unary operations with a vector integer or float operand.
5929 switch (Opcode) {
5930 default:
5931 // FIXME: Entirely reasonable to perform folding of other unary
5932 // operations here as the need arises.
5933 break;
5934 case ISD::FNEG:
5935 case ISD::FABS:
5936 case ISD::FCEIL:
5937 case ISD::FTRUNC:
5938 case ISD::FFLOOR:
5939 case ISD::FP_EXTEND:
5940 case ISD::FP_TO_SINT:
5941 case ISD::FP_TO_UINT:
5942 case ISD::FP_TO_FP16:
5943 case ISD::FP_TO_BF16:
5944 case ISD::TRUNCATE:
5945 case ISD::ANY_EXTEND:
5946 case ISD::ZERO_EXTEND:
5947 case ISD::SIGN_EXTEND:
5948 case ISD::UINT_TO_FP:
5949 case ISD::SINT_TO_FP:
5950 case ISD::FP16_TO_FP:
5951 case ISD::BF16_TO_FP:
5952 case ISD::BITCAST:
5953 case ISD::ABS:
5954 case ISD::BITREVERSE:
5955 case ISD::BSWAP:
5956 case ISD::CTLZ:
5958 case ISD::CTTZ:
5960 case ISD::CTPOP:
5961 case ISD::STEP_VECTOR: {
5962 SDValue Ops = {N1};
5963 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
5964 return Fold;
5965 }
5966 }
5967
5968 unsigned OpOpcode = N1.getNode()->getOpcode();
5969 switch (Opcode) {
5970 case ISD::STEP_VECTOR:
5971 assert(VT.isScalableVector() &&
5972 "STEP_VECTOR can only be used with scalable types");
5973 assert(OpOpcode == ISD::TargetConstant &&
5974 VT.getVectorElementType() == N1.getValueType() &&
5975 "Unexpected step operand");
5976 break;
5977 case ISD::FREEZE:
5978 assert(VT == N1.getValueType() && "Unexpected VT!");
5979 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
5980 /*Depth*/ 1))
5981 return N1;
5982 break;
5983 case ISD::TokenFactor:
5984 case ISD::MERGE_VALUES:
5986 return N1; // Factor, merge or concat of one node? No need.
5987 case ISD::BUILD_VECTOR: {
5988 // Attempt to simplify BUILD_VECTOR.
5989 SDValue Ops[] = {N1};
5990 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5991 return V;
5992 break;
5993 }
5994 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
5995 case ISD::FP_EXTEND:
5997 "Invalid FP cast!");
5998 if (N1.getValueType() == VT) return N1; // noop conversion.
5999 assert((!VT.isVector() || VT.getVectorElementCount() ==
6001 "Vector element count mismatch!");
6002 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6003 if (N1.isUndef())
6004 return getUNDEF(VT);
6005 break;
6006 case ISD::FP_TO_SINT:
6007 case ISD::FP_TO_UINT:
6008 if (N1.isUndef())
6009 return getUNDEF(VT);
6010 break;
6011 case ISD::SINT_TO_FP:
6012 case ISD::UINT_TO_FP:
6013 // [us]itofp(undef) = 0, because the result value is bounded.
6014 if (N1.isUndef())
6015 return getConstantFP(0.0, DL, VT);
6016 break;
6017 case ISD::SIGN_EXTEND:
6018 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6019 "Invalid SIGN_EXTEND!");
6020 assert(VT.isVector() == N1.getValueType().isVector() &&
6021 "SIGN_EXTEND result type type should be vector iff the operand "
6022 "type is vector!");
6023 if (N1.getValueType() == VT) return N1; // noop extension
6024 assert((!VT.isVector() || VT.getVectorElementCount() ==
6026 "Vector element count mismatch!");
6027 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6028 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6029 SDNodeFlags Flags;
6030 if (OpOpcode == ISD::ZERO_EXTEND)
6031 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6032 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6033 }
6034 if (OpOpcode == ISD::UNDEF)
6035 // sext(undef) = 0, because the top bits will all be the same.
6036 return getConstant(0, DL, VT);
6037 break;
6038 case ISD::ZERO_EXTEND:
6039 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6040 "Invalid ZERO_EXTEND!");
6041 assert(VT.isVector() == N1.getValueType().isVector() &&
6042 "ZERO_EXTEND result type type should be vector iff the operand "
6043 "type is vector!");
6044 if (N1.getValueType() == VT) return N1; // noop extension
6045 assert((!VT.isVector() || VT.getVectorElementCount() ==
6047 "Vector element count mismatch!");
6048 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6049 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6050 SDNodeFlags Flags;
6051 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6052 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6053 }
6054 if (OpOpcode == ISD::UNDEF)
6055 // zext(undef) = 0, because the top bits will be zero.
6056 return getConstant(0, DL, VT);
6057
6058 // Skip unnecessary zext_inreg pattern:
6059 // (zext (trunc x)) -> x iff the upper bits are known zero.
6060 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6061 // use to recognise zext_inreg patterns.
6062 if (OpOpcode == ISD::TRUNCATE) {
6063 SDValue OpOp = N1.getOperand(0);
6064 if (OpOp.getValueType() == VT) {
6065 if (OpOp.getOpcode() != ISD::AND) {
6068 if (MaskedValueIsZero(OpOp, HiBits)) {
6069 transferDbgValues(N1, OpOp);
6070 return OpOp;
6071 }
6072 }
6073 }
6074 }
6075 break;
6076 case ISD::ANY_EXTEND:
6077 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6078 "Invalid ANY_EXTEND!");
6079 assert(VT.isVector() == N1.getValueType().isVector() &&
6080 "ANY_EXTEND result type type should be vector iff the operand "
6081 "type is vector!");
6082 if (N1.getValueType() == VT) return N1; // noop extension
6083 assert((!VT.isVector() || VT.getVectorElementCount() ==
6085 "Vector element count mismatch!");
6086 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6087
6088 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6089 OpOpcode == ISD::ANY_EXTEND) {
6090 SDNodeFlags Flags;
6091 if (OpOpcode == ISD::ZERO_EXTEND)
6092 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6093 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6094 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6095 }
6096 if (OpOpcode == ISD::UNDEF)
6097 return getUNDEF(VT);
6098
6099 // (ext (trunc x)) -> x
6100 if (OpOpcode == ISD::TRUNCATE) {
6101 SDValue OpOp = N1.getOperand(0);
6102 if (OpOp.getValueType() == VT) {
6103 transferDbgValues(N1, OpOp);
6104 return OpOp;
6105 }
6106 }
6107 break;
6108 case ISD::TRUNCATE:
6109 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6110 "Invalid TRUNCATE!");
6111 assert(VT.isVector() == N1.getValueType().isVector() &&
6112 "TRUNCATE result type type should be vector iff the operand "
6113 "type is vector!");
6114 if (N1.getValueType() == VT) return N1; // noop truncate
6115 assert((!VT.isVector() || VT.getVectorElementCount() ==
6117 "Vector element count mismatch!");
6118 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6119 if (OpOpcode == ISD::TRUNCATE)
6120 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6121 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6122 OpOpcode == ISD::ANY_EXTEND) {
6123 // If the source is smaller than the dest, we still need an extend.
6125 VT.getScalarType()))
6126 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
6127 if (N1.getOperand(0).getValueType().bitsGT(VT))
6128 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6129 return N1.getOperand(0);
6130 }
6131 if (OpOpcode == ISD::UNDEF)
6132 return getUNDEF(VT);
6133 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6134 return getVScale(DL, VT,
6136 break;
6140 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6141 assert(N1.getValueType().bitsLE(VT) &&
6142 "The input must be the same size or smaller than the result.");
6145 "The destination vector type must have fewer lanes than the input.");
6146 break;
6147 case ISD::ABS:
6148 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6149 if (OpOpcode == ISD::UNDEF)
6150 return getConstant(0, DL, VT);
6151 break;
6152 case ISD::BSWAP:
6153 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6154 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6155 "BSWAP types must be a multiple of 16 bits!");
6156 if (OpOpcode == ISD::UNDEF)
6157 return getUNDEF(VT);
6158 // bswap(bswap(X)) -> X.
6159 if (OpOpcode == ISD::BSWAP)
6160 return N1.getOperand(0);
6161 break;
6162 case ISD::BITREVERSE:
6163 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6164 if (OpOpcode == ISD::UNDEF)
6165 return getUNDEF(VT);
6166 break;
6167 case ISD::BITCAST:
6169 "Cannot BITCAST between types of different sizes!");
6170 if (VT == N1.getValueType()) return N1; // noop conversion.
6171 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6172 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6173 if (OpOpcode == ISD::UNDEF)
6174 return getUNDEF(VT);
6175 break;
6177 assert(VT.isVector() && !N1.getValueType().isVector() &&
6178 (VT.getVectorElementType() == N1.getValueType() ||
6180 N1.getValueType().isInteger() &&
6182 "Illegal SCALAR_TO_VECTOR node!");
6183 if (OpOpcode == ISD::UNDEF)
6184 return getUNDEF(VT);
6185 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6186 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6187 isa<ConstantSDNode>(N1.getOperand(1)) &&
6188 N1.getConstantOperandVal(1) == 0 &&
6189 N1.getOperand(0).getValueType() == VT)
6190 return N1.getOperand(0);
6191 break;
6192 case ISD::FNEG:
6193 // Negation of an unknown bag of bits is still completely undefined.
6194 if (OpOpcode == ISD::UNDEF)
6195 return getUNDEF(VT);
6196
6197 if (OpOpcode == ISD::FNEG) // --X -> X
6198 return N1.getOperand(0);
6199 break;
6200 case ISD::FABS:
6201 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6202 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6203 break;
6204 case ISD::VSCALE:
6205 assert(VT == N1.getValueType() && "Unexpected VT!");
6206 break;
6207 case ISD::CTPOP:
6208 if (N1.getValueType().getScalarType() == MVT::i1)
6209 return N1;
6210 break;
6211 case ISD::CTLZ:
6212 case ISD::CTTZ:
6213 if (N1.getValueType().getScalarType() == MVT::i1)
6214 return getNOT(DL, N1, N1.getValueType());
6215 break;
6216 case ISD::VECREDUCE_ADD:
6217 if (N1.getValueType().getScalarType() == MVT::i1)
6218 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6219 break;
6222 if (N1.getValueType().getScalarType() == MVT::i1)
6223 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6224 break;
6227 if (N1.getValueType().getScalarType() == MVT::i1)
6228 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6229 break;
6230 case ISD::SPLAT_VECTOR:
6231 assert(VT.isVector() && "Wrong return type!");
6232 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6233 // that for now.
6235 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6237 N1.getValueType().isInteger() &&
6239 "Wrong operand type!");
6240 break;
6241 }
6242
6243 SDNode *N;
6244 SDVTList VTs = getVTList(VT);
6245 SDValue Ops[] = {N1};
6246 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6248 AddNodeIDNode(ID, Opcode, VTs, Ops);
6249 void *IP = nullptr;
6250 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6251 E->intersectFlagsWith(Flags);
6252 return SDValue(E, 0);
6253 }
6254
6255 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6256 N->setFlags(Flags);
6257 createOperands(N, Ops);
6258 CSEMap.InsertNode(N, IP);
6259 } else {
6260 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6261 createOperands(N, Ops);
6262 }
6263
6264 InsertNode(N);
6265 SDValue V = SDValue(N, 0);
6266 NewSDValueDbgMsg(V, "Creating new node: ", this);
6267 return V;
6268}
6269
6270static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6271 const APInt &C2) {
6272 switch (Opcode) {
6273 case ISD::ADD: return C1 + C2;
6274 case ISD::SUB: return C1 - C2;
6275 case ISD::MUL: return C1 * C2;
6276 case ISD::AND: return C1 & C2;
6277 case ISD::OR: return C1 | C2;
6278 case ISD::XOR: return C1 ^ C2;
6279 case ISD::SHL: return C1 << C2;
6280 case ISD::SRL: return C1.lshr(C2);
6281 case ISD::SRA: return C1.ashr(C2);
6282 case ISD::ROTL: return C1.rotl(C2);
6283 case ISD::ROTR: return C1.rotr(C2);
6284 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6285 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6286 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6287 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6288 case ISD::SADDSAT: return C1.sadd_sat(C2);
6289 case ISD::UADDSAT: return C1.uadd_sat(C2);
6290 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6291 case ISD::USUBSAT: return C1.usub_sat(C2);
6292 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6293 case ISD::USHLSAT: return C1.ushl_sat(C2);
6294 case ISD::UDIV:
6295 if (!C2.getBoolValue())
6296 break;
6297 return C1.udiv(C2);
6298 case ISD::UREM:
6299 if (!C2.getBoolValue())
6300 break;
6301 return C1.urem(C2);
6302 case ISD::SDIV:
6303 if (!C2.getBoolValue())
6304 break;
6305 return C1.sdiv(C2);
6306 case ISD::SREM:
6307 if (!C2.getBoolValue())
6308 break;
6309 return C1.srem(C2);
6310 case ISD::AVGFLOORS:
6311 return APIntOps::avgFloorS(C1, C2);
6312 case ISD::AVGFLOORU:
6313 return APIntOps::avgFloorU(C1, C2);
6314 case ISD::AVGCEILS:
6315 return APIntOps::avgCeilS(C1, C2);
6316 case ISD::AVGCEILU:
6317 return APIntOps::avgCeilU(C1, C2);
6318 case ISD::ABDS:
6319 return APIntOps::abds(C1, C2);
6320 case ISD::ABDU:
6321 return APIntOps::abdu(C1, C2);
6322 case ISD::MULHS:
6323 return APIntOps::mulhs(C1, C2);
6324 case ISD::MULHU:
6325 return APIntOps::mulhu(C1, C2);
6326 }
6327 return std::nullopt;
6328}
6329// Handle constant folding with UNDEF.
6330// TODO: Handle more cases.
6331static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6332 bool IsUndef1, const APInt &C2,
6333 bool IsUndef2) {
6334 if (!(IsUndef1 || IsUndef2))
6335 return FoldValue(Opcode, C1, C2);
6336
6337 // Fold and(x, undef) -> 0
6338 // Fold mul(x, undef) -> 0
6339 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6340 return APInt::getZero(C1.getBitWidth());
6341
6342 return std::nullopt;
6343}
6344
6346 const GlobalAddressSDNode *GA,
6347 const SDNode *N2) {
6348 if (GA->getOpcode() != ISD::GlobalAddress)
6349 return SDValue();
6350 if (!TLI->isOffsetFoldingLegal(GA))
6351 return SDValue();
6352 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6353 if (!C2)
6354 return SDValue();
6355 int64_t Offset = C2->getSExtValue();
6356 switch (Opcode) {
6357 case ISD::ADD: break;
6358 case ISD::SUB: Offset = -uint64_t(Offset); break;
6359 default: return SDValue();
6360 }
6361 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6362 GA->getOffset() + uint64_t(Offset));
6363}
6364
6365bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6366 switch (Opcode) {
6367 case ISD::SDIV:
6368 case ISD::UDIV:
6369 case ISD::SREM:
6370 case ISD::UREM: {
6371 // If a divisor is zero/undef or any element of a divisor vector is
6372 // zero/undef, the whole op is undef.
6373 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6374 SDValue Divisor = Ops[1];
6375 if (Divisor.isUndef() || isNullConstant(Divisor))
6376 return true;
6377
6378 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6379 llvm::any_of(Divisor->op_values(),
6380 [](SDValue V) { return V.isUndef() ||
6381 isNullConstant(V); });
6382 // TODO: Handle signed overflow.
6383 }
6384 // TODO: Handle oversized shifts.
6385 default:
6386 return false;
6387 }
6388}
6389
6391 EVT VT, ArrayRef<SDValue> Ops,
6392 SDNodeFlags Flags) {
6393 // If the opcode is a target-specific ISD node, there's nothing we can
6394 // do here and the operand rules may not line up with the below, so
6395 // bail early.
6396 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6397 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6398 // foldCONCAT_VECTORS in getNode before this is called.
6399 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6400 return SDValue();
6401
6402 unsigned NumOps = Ops.size();
6403 if (NumOps == 0)
6404 return SDValue();
6405
6406 if (isUndef(Opcode, Ops))
6407 return getUNDEF(VT);
6408
6409 // Handle unary special cases.
6410 if (NumOps == 1) {
6411 SDValue N1 = Ops[0];
6412
6413 // Constant fold unary operations with an integer constant operand. Even
6414 // opaque constant will be folded, because the folding of unary operations
6415 // doesn't create new constants with different values. Nevertheless, the
6416 // opaque flag is preserved during folding to prevent future folding with
6417 // other constants.
6418 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6419 const APInt &Val = C->getAPIntValue();
6420 switch (Opcode) {
6421 case ISD::SIGN_EXTEND:
6422 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6423 C->isTargetOpcode(), C->isOpaque());
6424 case ISD::TRUNCATE:
6425 if (C->isOpaque())
6426 break;
6427 [[fallthrough]];
6428 case ISD::ZERO_EXTEND:
6429 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6430 C->isTargetOpcode(), C->isOpaque());
6431 case ISD::ANY_EXTEND:
6432 // Some targets like RISCV prefer to sign extend some types.
6433 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6434 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6435 C->isTargetOpcode(), C->isOpaque());
6436 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6437 C->isTargetOpcode(), C->isOpaque());
6438 case ISD::ABS:
6439 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6440 C->isOpaque());
6441 case ISD::BITREVERSE:
6442 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6443 C->isOpaque());
6444 case ISD::BSWAP:
6445 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6446 C->isOpaque());
6447 case ISD::CTPOP:
6448 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6449 C->isOpaque());
6450 case ISD::CTLZ:
6452 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6453 C->isOpaque());
6454 case ISD::CTTZ:
6456 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6457 C->isOpaque());
6458 case ISD::UINT_TO_FP:
6459 case ISD::SINT_TO_FP: {
6461 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6463 return getConstantFP(FPV, DL, VT);
6464 }
6465 case ISD::FP16_TO_FP:
6466 case ISD::BF16_TO_FP: {
6467 bool Ignored;
6468 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6469 : APFloat::BFloat(),
6470 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6471
6472 // This can return overflow, underflow, or inexact; we don't care.
6473 // FIXME need to be more flexible about rounding mode.
6475 &Ignored);
6476 return getConstantFP(FPV, DL, VT);
6477 }
6478 case ISD::STEP_VECTOR:
6479 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6480 return V;
6481 break;
6482 case ISD::BITCAST:
6483 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6484 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6485 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6486 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6487 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6488 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6489 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6490 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6491 break;
6492 }
6493 }
6494
6495 // Constant fold unary operations with a floating point constant operand.
6496 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6497 APFloat V = C->getValueAPF(); // make copy
6498 switch (Opcode) {
6499 case ISD::FNEG:
6500 V.changeSign();
6501 return getConstantFP(V, DL, VT);
6502 case ISD::FABS:
6503 V.clearSign();
6504 return getConstantFP(V, DL, VT);
6505 case ISD::FCEIL: {
6506 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6507 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6508 return getConstantFP(V, DL, VT);
6509 return SDValue();
6510 }
6511 case ISD::FTRUNC: {
6512 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6513 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6514 return getConstantFP(V, DL, VT);
6515 return SDValue();
6516 }
6517 case ISD::FFLOOR: {
6518 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6519 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6520 return getConstantFP(V, DL, VT);
6521 return SDValue();
6522 }
6523 case ISD::FP_EXTEND: {
6524 bool ignored;
6525 // This can return overflow, underflow, or inexact; we don't care.
6526 // FIXME need to be more flexible about rounding mode.
6527 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
6528 &ignored);
6529 return getConstantFP(V, DL, VT);
6530 }
6531 case ISD::FP_TO_SINT:
6532 case ISD::FP_TO_UINT: {
6533 bool ignored;
6534 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6535 // FIXME need to be more flexible about rounding mode.
6537 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6538 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6539 break;
6540 return getConstant(IntVal, DL, VT);
6541 }
6542 case ISD::FP_TO_FP16:
6543 case ISD::FP_TO_BF16: {
6544 bool Ignored;
6545 // This can return overflow, underflow, or inexact; we don't care.
6546 // FIXME need to be more flexible about rounding mode.
6547 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6548 : APFloat::BFloat(),
6550 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6551 }
6552 case ISD::BITCAST:
6553 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6554 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6555 VT);
6556 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6557 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6558 VT);
6559 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6560 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6561 VT);
6562 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6563 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6564 break;
6565 }
6566 }
6567
6568 // Early-out if we failed to constant fold a bitcast.
6569 if (Opcode == ISD::BITCAST)
6570 return SDValue();
6571 }
6572
6573 // Handle binops special cases.
6574 if (NumOps == 2) {
6575 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6576 return CFP;
6577
6578 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6579 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6580 if (C1->isOpaque() || C2->isOpaque())
6581 return SDValue();
6582
6583 std::optional<APInt> FoldAttempt =
6584 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6585 if (!FoldAttempt)
6586 return SDValue();
6587
6588 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6589 assert((!Folded || !VT.isVector()) &&
6590 "Can't fold vectors ops with scalar operands");
6591 return Folded;
6592 }
6593 }
6594
6595 // fold (add Sym, c) -> Sym+c
6596 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6597 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6598 if (TLI->isCommutativeBinOp(Opcode))
6599 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6600 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6601 }
6602
6603 // This is for vector folding only from here on.
6604 if (!VT.isVector())
6605 return SDValue();
6606
6607 ElementCount NumElts = VT.getVectorElementCount();
6608
6609 // See if we can fold through any bitcasted integer ops.
6610 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6611 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6612 (Ops[0].getOpcode() == ISD::BITCAST ||
6613 Ops[1].getOpcode() == ISD::BITCAST)) {
6614 SDValue N1 = peekThroughBitcasts(Ops[0]);
6615 SDValue N2 = peekThroughBitcasts(Ops[1]);
6616 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6617 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6618 if (BV1 && BV2 && N1.getValueType().isInteger() &&
6619 N2.getValueType().isInteger()) {
6620 bool IsLE = getDataLayout().isLittleEndian();
6621 unsigned EltBits = VT.getScalarSizeInBits();
6622 SmallVector<APInt> RawBits1, RawBits2;
6623 BitVector UndefElts1, UndefElts2;
6624 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6625 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6626 SmallVector<APInt> RawBits;
6627 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6628 std::optional<APInt> Fold = FoldValueWithUndef(
6629 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6630 if (!Fold)
6631 break;
6632 RawBits.push_back(*Fold);
6633 }
6634 if (RawBits.size() == NumElts.getFixedValue()) {
6635 // We have constant folded, but we might need to cast this again back
6636 // to the original (possibly legalized) type.
6637 EVT BVVT, BVEltVT;
6638 if (N1.getValueType() == VT) {
6639 BVVT = N1.getValueType();
6640 BVEltVT = BV1->getOperand(0).getValueType();
6641 } else {
6642 BVVT = N2.getValueType();
6643 BVEltVT = BV2->getOperand(0).getValueType();
6644 }
6645 unsigned BVEltBits = BVEltVT.getSizeInBits();
6646 SmallVector<APInt> DstBits;
6647 BitVector DstUndefs;
6649 DstBits, RawBits, DstUndefs,
6650 BitVector(RawBits.size(), false));
6651 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6652 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6653 if (DstUndefs[I])
6654 continue;
6655 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6656 }
6657 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6658 }
6659 }
6660 }
6661 }
6662
6663 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6664 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6665 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6666 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6667 APInt RHSVal;
6668 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6669 APInt NewStep = Opcode == ISD::MUL
6670 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6671 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6672 return getStepVector(DL, VT, NewStep);
6673 }
6674 }
6675
6676 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6677 return !Op.getValueType().isVector() ||
6678 Op.getValueType().getVectorElementCount() == NumElts;
6679 };
6680
6681 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
6682 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
6683 Op.getOpcode() == ISD::BUILD_VECTOR ||
6684 Op.getOpcode() == ISD::SPLAT_VECTOR;
6685 };
6686
6687 // All operands must be vector types with the same number of elements as
6688 // the result type and must be either UNDEF or a build/splat vector
6689 // or UNDEF scalars.
6690 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
6691 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
6692 return SDValue();
6693
6694 // If we are comparing vectors, then the result needs to be a i1 boolean that
6695 // is then extended back to the legal result type depending on how booleans
6696 // are represented.
6697 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
6698 ISD::NodeType ExtendCode =
6699 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
6702
6703 // Find legal integer scalar type for constant promotion and
6704 // ensure that its scalar size is at least as large as source.
6705 EVT LegalSVT = VT.getScalarType();
6706 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
6707 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
6708 if (LegalSVT.bitsLT(VT.getScalarType()))
6709 return SDValue();
6710 }
6711
6712 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
6713 // only have one operand to check. For fixed-length vector types we may have
6714 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
6715 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
6716
6717 // Constant fold each scalar lane separately.
6718 SmallVector<SDValue, 4> ScalarResults;
6719 for (unsigned I = 0; I != NumVectorElts; I++) {
6720 SmallVector<SDValue, 4> ScalarOps;
6721 for (SDValue Op : Ops) {
6722 EVT InSVT = Op.getValueType().getScalarType();
6723 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
6724 Op.getOpcode() != ISD::SPLAT_VECTOR) {
6725 if (Op.isUndef())
6726 ScalarOps.push_back(getUNDEF(InSVT));
6727 else
6728 ScalarOps.push_back(Op);
6729 continue;
6730 }
6731
6732 SDValue ScalarOp =
6733 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
6734 EVT ScalarVT = ScalarOp.getValueType();
6735
6736 // Build vector (integer) scalar operands may need implicit
6737 // truncation - do this before constant folding.
6738 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
6739 // Don't create illegally-typed nodes unless they're constants or undef
6740 // - if we fail to constant fold we can't guarantee the (dead) nodes
6741 // we're creating will be cleaned up before being visited for
6742 // legalization.
6743 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
6744 !isa<ConstantSDNode>(ScalarOp) &&
6745 TLI->getTypeAction(*getContext(), InSVT) !=
6747 return SDValue();
6748 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
6749 }
6750
6751 ScalarOps.push_back(ScalarOp);
6752 }
6753
6754 // Constant fold the scalar operands.
6755 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
6756
6757 // Legalize the (integer) scalar constant if necessary.
6758 if (LegalSVT != SVT)
6759 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
6760
6761 // Scalar folding only succeeded if the result is a constant or UNDEF.
6762 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
6763 ScalarResult.getOpcode() != ISD::ConstantFP)
6764 return SDValue();
6765 ScalarResults.push_back(ScalarResult);
6766 }
6767
6768 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
6769 : getBuildVector(VT, DL, ScalarResults);
6770 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
6771 return V;
6772}
6773
6775 EVT VT, ArrayRef<SDValue> Ops) {
6776 // TODO: Add support for unary/ternary fp opcodes.
6777 if (Ops.size() != 2)
6778 return SDValue();
6779
6780 // TODO: We don't do any constant folding for strict FP opcodes here, but we
6781 // should. That will require dealing with a potentially non-default
6782 // rounding mode, checking the "opStatus" return value from the APFloat
6783 // math calculations, and possibly other variations.
6784 SDValue N1 = Ops[0];
6785 SDValue N2 = Ops[1];
6786 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
6787 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
6788 if (N1CFP && N2CFP) {
6789 APFloat C1 = N1CFP->getValueAPF(); // make copy
6790 const APFloat &C2 = N2CFP->getValueAPF();
6791 switch (Opcode) {
6792 case ISD::FADD:
6794 return getConstantFP(C1, DL, VT);
6795 case ISD::FSUB:
6797 return getConstantFP(C1, DL, VT);
6798 case ISD::FMUL:
6800 return getConstantFP(C1, DL, VT);
6801 case ISD::FDIV:
6803 return getConstantFP(C1, DL, VT);
6804 case ISD::FREM:
6805 C1.mod(C2);
6806 return getConstantFP(C1, DL, VT);
6807 case ISD::FCOPYSIGN:
6808 C1.copySign(C2);
6809 return getConstantFP(C1, DL, VT);
6810 case ISD::FMINNUM:
6811 return getConstantFP(minnum(C1, C2), DL, VT);
6812 case ISD::FMAXNUM:
6813 return getConstantFP(maxnum(C1, C2), DL, VT);
6814 case ISD::FMINIMUM:
6815 return getConstantFP(minimum(C1, C2), DL, VT);
6816 case ISD::FMAXIMUM:
6817 return getConstantFP(maximum(C1, C2), DL, VT);
6818 case ISD::FMINIMUMNUM:
6819 return getConstantFP(minimumnum(C1, C2), DL, VT);
6820 case ISD::FMAXIMUMNUM:
6821 return getConstantFP(maximumnum(C1, C2), DL, VT);
6822 default: break;
6823 }
6824 }
6825 if (N1CFP && Opcode == ISD::FP_ROUND) {
6826 APFloat C1 = N1CFP->getValueAPF(); // make copy
6827 bool Unused;
6828 // This can return overflow, underflow, or inexact; we don't care.
6829 // FIXME need to be more flexible about rounding mode.
6831 &Unused);
6832 return getConstantFP(C1, DL, VT);
6833 }
6834
6835 switch (Opcode) {
6836 case ISD::FSUB:
6837 // -0.0 - undef --> undef (consistent with "fneg undef")
6838 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
6839 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
6840 return getUNDEF(VT);
6841 [[fallthrough]];
6842
6843 case ISD::FADD:
6844 case ISD::FMUL:
6845 case ISD::FDIV:
6846 case ISD::FREM:
6847 // If both operands are undef, the result is undef. If 1 operand is undef,
6848 // the result is NaN. This should match the behavior of the IR optimizer.
6849 if (N1.isUndef() && N2.isUndef())
6850 return getUNDEF(VT);
6851 if (N1.isUndef() || N2.isUndef())
6853 }
6854 return SDValue();
6855}
6856
6858 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
6859
6860 // There's no need to assert on a byte-aligned pointer. All pointers are at
6861 // least byte aligned.
6862 if (A == Align(1))
6863 return Val;
6864
6865 SDVTList VTs = getVTList(Val.getValueType());
6867 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
6868 ID.AddInteger(A.value());
6869
6870 void *IP = nullptr;
6871 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6872 return SDValue(E, 0);
6873
6874 auto *N =
6875 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
6876 createOperands(N, {Val});
6877
6878 CSEMap.InsertNode(N, IP);
6879 InsertNode(N);
6880
6881 SDValue V(N, 0);
6882 NewSDValueDbgMsg(V, "Creating new node: ", this);
6883 return V;
6884}
6885
6886SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6887 SDValue N1, SDValue N2) {
6888 SDNodeFlags Flags;
6889 if (Inserter)
6890 Flags = Inserter->getFlags();
6891 return getNode(Opcode, DL, VT, N1, N2, Flags);
6892}
6893
6895 SDValue &N2) const {
6896 if (!TLI->isCommutativeBinOp(Opcode))
6897 return;
6898
6899 // Canonicalize:
6900 // binop(const, nonconst) -> binop(nonconst, const)
6905 if ((N1C && !N2C) || (N1CFP && !N2CFP))
6906 std::swap(N1, N2);
6907
6908 // Canonicalize:
6909 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
6910 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
6912 std::swap(N1, N2);
6913}
6914
6915SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6916 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
6918 N2.getOpcode() != ISD::DELETED_NODE &&
6919 "Operand is DELETED_NODE!");
6920
6921 canonicalizeCommutativeBinop(Opcode, N1, N2);
6922
6923 auto *N1C = dyn_cast<ConstantSDNode>(N1);
6924 auto *N2C = dyn_cast<ConstantSDNode>(N2);
6925
6926 // Don't allow undefs in vector splats - we might be returning N2 when folding
6927 // to zero etc.
6928 ConstantSDNode *N2CV =
6929 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
6930
6931 switch (Opcode) {
6932 default: break;
6933 case ISD::TokenFactor:
6934 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
6935 N2.getValueType() == MVT::Other && "Invalid token factor!");
6936 // Fold trivial token factors.
6937 if (N1.getOpcode() == ISD::EntryToken) return N2;
6938 if (N2.getOpcode() == ISD::EntryToken) return N1;
6939 if (N1 == N2) return N1;
6940 break;
6941 case ISD::BUILD_VECTOR: {
6942 // Attempt to simplify BUILD_VECTOR.
6943 SDValue Ops[] = {N1, N2};
6944 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6945 return V;
6946 break;
6947 }
6948 case ISD::CONCAT_VECTORS: {
6949 SDValue Ops[] = {N1, N2};
6950 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
6951 return V;
6952 break;
6953 }
6954 case ISD::AND:
6955 assert(VT.isInteger() && "This operator does not apply to FP types!");
6956 assert(N1.getValueType() == N2.getValueType() &&
6957 N1.getValueType() == VT && "Binary operator types must match!");
6958 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
6959 // worth handling here.
6960 if (N2CV && N2CV->isZero())
6961 return N2;
6962 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
6963 return N1;
6964 break;
6965 case ISD::OR:
6966 case ISD::XOR:
6967 case ISD::ADD:
6968 case ISD::SUB:
6969 assert(VT.isInteger() && "This operator does not apply to FP types!");
6970 assert(N1.getValueType() == N2.getValueType() &&
6971 N1.getValueType() == VT && "Binary operator types must match!");
6972 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
6973 // it's worth handling here.
6974 if (N2CV && N2CV->isZero())
6975 return N1;
6976 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
6977 VT.getVectorElementType() == MVT::i1)
6978 return getNode(ISD::XOR, DL, VT, N1, N2);
6979 break;
6980 case ISD::MUL:
6981 assert(VT.isInteger() && "This operator does not apply to FP types!");
6982 assert(N1.getValueType() == N2.getValueType() &&
6983 N1.getValueType() == VT && "Binary operator types must match!");
6984 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6985 return getNode(ISD::AND, DL, VT, N1, N2);
6986 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6987 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6988 const APInt &N2CImm = N2C->getAPIntValue();
6989 return getVScale(DL, VT, MulImm * N2CImm);
6990 }
6991 break;
6992 case ISD::UDIV:
6993 case ISD::UREM:
6994 case ISD::MULHU:
6995 case ISD::MULHS:
6996 case ISD::SDIV:
6997 case ISD::SREM:
6998 case ISD::SADDSAT:
6999 case ISD::SSUBSAT:
7000 case ISD::UADDSAT:
7001 case ISD::USUBSAT:
7002 assert(VT.isInteger() && "This operator does not apply to FP types!");
7003 assert(N1.getValueType() == N2.getValueType() &&
7004 N1.getValueType() == VT && "Binary operator types must match!");
7005 if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
7006 // fold (add_sat x, y) -> (or x, y) for bool types.
7007 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7008 return getNode(ISD::OR, DL, VT, N1, N2);
7009 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7010 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7011 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7012 }
7013 break;
7014 case ISD::SCMP:
7015 case ISD::UCMP:
7016 assert(N1.getValueType() == N2.getValueType() &&
7017 "Types of operands of UCMP/SCMP must match");
7018 assert(N1.getValueType().isVector() == VT.isVector() &&
7019 "Operands and return type of must both be scalars or vectors");
7020 if (VT.isVector())
7023 "Result and operands must have the same number of elements");
7024 break;
7025 case ISD::AVGFLOORS:
7026 case ISD::AVGFLOORU:
7027 case ISD::AVGCEILS:
7028 case ISD::AVGCEILU:
7029 assert(VT.isInteger() && "This operator does not apply to FP types!");
7030 assert(N1.getValueType() == N2.getValueType() &&
7031 N1.getValueType() == VT && "Binary operator types must match!");
7032 break;
7033 case ISD::ABDS:
7034 case ISD::ABDU:
7035 assert(VT.isInteger() && "This operator does not apply to FP types!");
7036 assert(N1.getValueType() == N2.getValueType() &&
7037 N1.getValueType() == VT && "Binary operator types must match!");
7038 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7039 return getNode(ISD::XOR, DL, VT, N1, N2);
7040 break;
7041 case ISD::SMIN:
7042 case ISD::UMAX:
7043 assert(VT.isInteger() && "This operator does not apply to FP types!");
7044 assert(N1.getValueType() == N2.getValueType() &&
7045 N1.getValueType() == VT && "Binary operator types must match!");
7046 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7047 return getNode(ISD::OR, DL, VT, N1, N2);
7048 break;
7049 case ISD::SMAX:
7050 case ISD::UMIN:
7051 assert(VT.isInteger() && "This operator does not apply to FP types!");
7052 assert(N1.getValueType() == N2.getValueType() &&
7053 N1.getValueType() == VT && "Binary operator types must match!");
7054 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7055 return getNode(ISD::AND, DL, VT, N1, N2);
7056 break;
7057 case ISD::FADD:
7058 case ISD::FSUB:
7059 case ISD::FMUL:
7060 case ISD::FDIV:
7061 case ISD::FREM:
7062 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7063 assert(N1.getValueType() == N2.getValueType() &&
7064 N1.getValueType() == VT && "Binary operator types must match!");
7065 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7066 return V;
7067 break;
7068 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7069 assert(N1.getValueType() == VT &&
7072 "Invalid FCOPYSIGN!");
7073 break;
7074 case ISD::SHL:
7075 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7076 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7077 const APInt &ShiftImm = N2C->getAPIntValue();
7078 return getVScale(DL, VT, MulImm << ShiftImm);
7079 }
7080 [[fallthrough]];
7081 case ISD::SRA:
7082 case ISD::SRL:
7083 if (SDValue V = simplifyShift(N1, N2))
7084 return V;
7085 [[fallthrough]];
7086 case ISD::ROTL:
7087 case ISD::ROTR:
7088 assert(VT == N1.getValueType() &&
7089 "Shift operators return type must be the same as their first arg");
7090 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7091 "Shifts only work on integers");
7092 assert((!VT.isVector() || VT == N2.getValueType()) &&
7093 "Vector shift amounts must be in the same as their first arg");
7094 // Verify that the shift amount VT is big enough to hold valid shift
7095 // amounts. This catches things like trying to shift an i1024 value by an
7096 // i8, which is easy to fall into in generic code that uses
7097 // TLI.getShiftAmount().
7100 "Invalid use of small shift amount with oversized value!");
7101
7102 // Always fold shifts of i1 values so the code generator doesn't need to
7103 // handle them. Since we know the size of the shift has to be less than the
7104 // size of the value, the shift/rotate count is guaranteed to be zero.
7105 if (VT == MVT::i1)
7106 return N1;
7107 if (N2CV && N2CV->isZero())
7108 return N1;
7109 break;
7110 case ISD::FP_ROUND:
7111 assert(VT.isFloatingPoint() &&
7113 VT.bitsLE(N1.getValueType()) &&
7114 N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7115 "Invalid FP_ROUND!");
7116 if (N1.getValueType() == VT) return N1; // noop conversion.
7117 break;
7118 case ISD::AssertSext:
7119 case ISD::AssertZext: {
7120 EVT EVT = cast<VTSDNode>(N2)->getVT();
7121 assert(VT == N1.getValueType() && "Not an inreg extend!");
7122 assert(VT.isInteger() && EVT.isInteger() &&
7123 "Cannot *_EXTEND_INREG FP types");
7124 assert(!EVT.isVector() &&
7125 "AssertSExt/AssertZExt type should be the vector element type "
7126 "rather than the vector type!");
7127 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7128 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7129 break;
7130 }
7132 EVT EVT = cast<VTSDNode>(N2)->getVT();
7133 assert(VT == N1.getValueType() && "Not an inreg extend!");
7134 assert(VT.isInteger() && EVT.isInteger() &&
7135 "Cannot *_EXTEND_INREG FP types");
7136 assert(EVT.isVector() == VT.isVector() &&
7137 "SIGN_EXTEND_INREG type should be vector iff the operand "
7138 "type is vector!");
7139 assert((!EVT.isVector() ||
7141 "Vector element counts must match in SIGN_EXTEND_INREG");
7142 assert(EVT.bitsLE(VT) && "Not extending!");
7143 if (EVT == VT) return N1; // Not actually extending
7144
7145 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7146 unsigned FromBits = EVT.getScalarSizeInBits();
7147 Val <<= Val.getBitWidth() - FromBits;
7148 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7149 return getConstant(Val, DL, ConstantVT);
7150 };
7151
7152 if (N1C) {
7153 const APInt &Val = N1C->getAPIntValue();
7154 return SignExtendInReg(Val, VT);
7155 }
7156
7159 llvm::EVT OpVT = N1.getOperand(0).getValueType();
7160 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7161 SDValue Op = N1.getOperand(i);
7162 if (Op.isUndef()) {
7163 Ops.push_back(getUNDEF(OpVT));
7164 continue;
7165 }
7166 ConstantSDNode *C = cast<ConstantSDNode>(Op);
7167 APInt Val = C->getAPIntValue();
7168 Ops.push_back(SignExtendInReg(Val, OpVT));
7169 }
7170 return getBuildVector(VT, DL, Ops);
7171 }
7172
7173 if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7174 isa<ConstantSDNode>(N1.getOperand(0)))
7175 return getNode(
7176 ISD::SPLAT_VECTOR, DL, VT,
7177 SignExtendInReg(N1.getConstantOperandAPInt(0),
7178 N1.getOperand(0).getValueType()));
7179 break;
7180 }
7182 case ISD::FP_TO_UINT_SAT: {
7183 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7184 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7185 assert(N1.getValueType().isVector() == VT.isVector() &&
7186 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7187 "vector!");
7188 assert((!VT.isVector() || VT.getVectorElementCount() ==
7190 "Vector element counts must match in FP_TO_*INT_SAT");
7191 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7192 "Type to saturate to must be a scalar.");
7193 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7194 "Not extending!");
7195 break;
7196 }
7199 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7200 element type of the vector.");
7201
7202 // Extract from an undefined value or using an undefined index is undefined.
7203 if (N1.isUndef() || N2.isUndef())
7204 return getUNDEF(VT);
7205
7206 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7207 // vectors. For scalable vectors we will provide appropriate support for
7208 // dealing with arbitrary indices.
7209 if (N2C && N1.getValueType().isFixedLengthVector() &&
7210 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7211 return getUNDEF(VT);
7212
7213 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7214 // expanding copies of large vectors from registers. This only works for
7215 // fixed length vectors, since we need to know the exact number of
7216 // elements.
7217 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7219 unsigned Factor =
7222 N1.getOperand(N2C->getZExtValue() / Factor),
7223 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
7224 }
7225
7226 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7227 // lowering is expanding large vector constants.
7228 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7229 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7232 "BUILD_VECTOR used for scalable vectors");
7233 unsigned Index =
7234 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7235 SDValue Elt = N1.getOperand(Index);
7236
7237 if (VT != Elt.getValueType())
7238 // If the vector element type is not legal, the BUILD_VECTOR operands
7239 // are promoted and implicitly truncated, and the result implicitly
7240 // extended. Make that explicit here.
7241 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7242
7243 return Elt;
7244 }
7245
7246 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7247 // operations are lowered to scalars.
7248 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7249 // If the indices are the same, return the inserted element else
7250 // if the indices are known different, extract the element from
7251 // the original vector.
7252 SDValue N1Op2 = N1.getOperand(2);
7253 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7254
7255 if (N1Op2C && N2C) {
7256 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7257 if (VT == N1.getOperand(1).getValueType())
7258 return N1.getOperand(1);
7259 if (VT.isFloatingPoint()) {
7261 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7262 }
7263 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7264 }
7265 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7266 }
7267 }
7268
7269 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7270 // when vector types are scalarized and v1iX is legal.
7271 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7272 // Here we are completely ignoring the extract element index (N2),
7273 // which is fine for fixed width vectors, since any index other than 0
7274 // is undefined anyway. However, this cannot be ignored for scalable
7275 // vectors - in theory we could support this, but we don't want to do this
7276 // without a profitability check.
7277 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7279 N1.getValueType().getVectorNumElements() == 1) {
7280 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7281 N1.getOperand(1));
7282 }
7283 break;
7285 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7286 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7287 (N1.getValueType().isInteger() == VT.isInteger()) &&
7288 N1.getValueType() != VT &&
7289 "Wrong types for EXTRACT_ELEMENT!");
7290
7291 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7292 // 64-bit integers into 32-bit parts. Instead of building the extract of
7293 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7294 if (N1.getOpcode() == ISD::BUILD_PAIR)
7295 return N1.getOperand(N2C->getZExtValue());
7296
7297 // EXTRACT_ELEMENT of a constant int is also very common.
7298 if (N1C) {
7299 unsigned ElementSize = VT.getSizeInBits();
7300 unsigned Shift = ElementSize * N2C->getZExtValue();
7301 const APInt &Val = N1C->getAPIntValue();
7302 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7303 }
7304 break;
7306 EVT N1VT = N1.getValueType();
7307 assert(VT.isVector() && N1VT.isVector() &&
7308 "Extract subvector VTs must be vectors!");
7310 "Extract subvector VTs must have the same element type!");
7311 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7312 "Cannot extract a scalable vector from a fixed length vector!");
7313 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7315 "Extract subvector must be from larger vector to smaller vector!");
7316 assert(N2C && "Extract subvector index must be a constant");
7317 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7318 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7319 N1VT.getVectorMinNumElements()) &&
7320 "Extract subvector overflow!");
7321 assert(N2C->getAPIntValue().getBitWidth() ==
7322 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7323 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7324
7325 // Trivial extraction.
7326 if (VT == N1VT)
7327 return N1;
7328
7329 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7330 if (N1.isUndef())
7331 return getUNDEF(VT);
7332
7333 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7334 // the concat have the same type as the extract.
7335 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7336 VT == N1.getOperand(0).getValueType()) {
7337 unsigned Factor = VT.getVectorMinNumElements();
7338 return N1.getOperand(N2C->getZExtValue() / Factor);
7339 }
7340
7341 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7342 // during shuffle legalization.
7343 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7344 VT == N1.getOperand(1).getValueType())
7345 return N1.getOperand(1);
7346 break;
7347 }
7348 }
7349
7350 // Perform trivial constant folding.
7351 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
7352 return SV;
7353
7354 // Canonicalize an UNDEF to the RHS, even over a constant.
7355 if (N1.isUndef()) {
7356 if (TLI->isCommutativeBinOp(Opcode)) {
7357 std::swap(N1, N2);
7358 } else {
7359 switch (Opcode) {
7360 case ISD::SUB:
7361 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7363 case ISD::UDIV:
7364 case ISD::SDIV:
7365 case ISD::UREM:
7366 case ISD::SREM:
7367 case ISD::SSUBSAT:
7368 case ISD::USUBSAT:
7369 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7370 }
7371 }
7372 }
7373
7374 // Fold a bunch of operators when the RHS is undef.
7375 if (N2.isUndef()) {
7376 switch (Opcode) {
7377 case ISD::XOR:
7378 if (N1.isUndef())
7379 // Handle undef ^ undef -> 0 special case. This is a common
7380 // idiom (misuse).
7381 return getConstant(0, DL, VT);
7382 [[fallthrough]];
7383 case ISD::ADD:
7384 case ISD::SUB:
7385 case ISD::UDIV:
7386 case ISD::SDIV:
7387 case ISD::UREM:
7388 case ISD::SREM:
7389 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7390 case ISD::MUL:
7391 case ISD::AND:
7392 case ISD::SSUBSAT:
7393 case ISD::USUBSAT:
7394 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7395 case ISD::OR:
7396 case ISD::SADDSAT:
7397 case ISD::UADDSAT:
7398 return getAllOnesConstant(DL, VT);
7399 }
7400 }
7401
7402 // Memoize this node if possible.
7403 SDNode *N;
7404 SDVTList VTs = getVTList(VT);
7405 SDValue Ops[] = {N1, N2};
7406 if (VT != MVT::Glue) {
7408 AddNodeIDNode(ID, Opcode, VTs, Ops);
7409 void *IP = nullptr;
7410 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7411 E->intersectFlagsWith(Flags);
7412 return SDValue(E, 0);
7413 }
7414
7415 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7416 N->setFlags(Flags);
7417 createOperands(N, Ops);
7418 CSEMap.InsertNode(N, IP);
7419 } else {
7420 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7421 createOperands(N, Ops);
7422 }
7423
7424 InsertNode(N);
7425 SDValue V = SDValue(N, 0);
7426 NewSDValueDbgMsg(V, "Creating new node: ", this);
7427 return V;
7428}
7429
7430SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7431 SDValue N1, SDValue N2, SDValue N3) {
7432 SDNodeFlags Flags;
7433 if (Inserter)
7434 Flags = Inserter->getFlags();
7435 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7436}
7437
7438SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7439 SDValue N1, SDValue N2, SDValue N3,
7440 const SDNodeFlags Flags) {
7442 N2.getOpcode() != ISD::DELETED_NODE &&
7443 N3.getOpcode() != ISD::DELETED_NODE &&
7444 "Operand is DELETED_NODE!");
7445 // Perform various simplifications.
7446 switch (Opcode) {
7447 case ISD::FMA:
7448 case ISD::FMAD: {
7449 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7450 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7451 N3.getValueType() == VT && "FMA types must match!");
7452 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7453 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7454 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7455 if (N1CFP && N2CFP && N3CFP) {
7456 APFloat V1 = N1CFP->getValueAPF();
7457 const APFloat &V2 = N2CFP->getValueAPF();
7458 const APFloat &V3 = N3CFP->getValueAPF();
7459 if (Opcode == ISD::FMAD) {
7462 } else
7464 return getConstantFP(V1, DL, VT);
7465 }
7466 break;
7467 }
7468 case ISD::BUILD_VECTOR: {
7469 // Attempt to simplify BUILD_VECTOR.
7470 SDValue Ops[] = {N1, N2, N3};
7471 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7472 return V;
7473 break;
7474 }
7475 case ISD::CONCAT_VECTORS: {
7476 SDValue Ops[] = {N1, N2, N3};
7477 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7478 return V;
7479 break;
7480 }
7481 case ISD::SETCC: {
7482 assert(VT.isInteger() && "SETCC result type must be an integer!");
7483 assert(N1.getValueType() == N2.getValueType() &&
7484 "SETCC operands must have the same type!");
7485 assert(VT.isVector() == N1.getValueType().isVector() &&
7486 "SETCC type should be vector iff the operand type is vector!");
7487 assert((!VT.isVector() || VT.getVectorElementCount() ==
7489 "SETCC vector element counts must match!");
7490 // Use FoldSetCC to simplify SETCC's.
7491 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7492 return V;
7493 // Vector constant folding.
7494 SDValue Ops[] = {N1, N2, N3};
7495 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7496 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7497 return V;
7498 }
7499 break;
7500 }
7501 case ISD::SELECT:
7502 case ISD::VSELECT:
7503 if (SDValue V = simplifySelect(N1, N2, N3))
7504 return V;
7505 break;
7507 llvm_unreachable("should use getVectorShuffle constructor!");
7508 case ISD::VECTOR_SPLICE: {
7509 if (cast<ConstantSDNode>(N3)->isZero())
7510 return N1;
7511 break;
7512 }
7514 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7515 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7516 // for scalable vectors where we will generate appropriate code to
7517 // deal with out-of-bounds cases correctly.
7518 if (N3C && N1.getValueType().isFixedLengthVector() &&
7520 return getUNDEF(VT);
7521
7522 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7523 if (N3.isUndef())
7524 return getUNDEF(VT);
7525
7526 // If the inserted element is an UNDEF, just use the input vector.
7527 if (N2.isUndef())
7528 return N1;
7529
7530 break;
7531 }
7532 case ISD::INSERT_SUBVECTOR: {
7533 // Inserting undef into undef is still undef.
7534 if (N1.isUndef() && N2.isUndef())
7535 return getUNDEF(VT);
7536
7537 EVT N2VT = N2.getValueType();
7538 assert(VT == N1.getValueType() &&
7539 "Dest and insert subvector source types must match!");
7540 assert(VT.isVector() && N2VT.isVector() &&
7541 "Insert subvector VTs must be vectors!");
7543 "Insert subvector VTs must have the same element type!");
7544 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7545 "Cannot insert a scalable vector into a fixed length vector!");
7546 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7548 "Insert subvector must be from smaller vector to larger vector!");
7549 assert(isa<ConstantSDNode>(N3) &&
7550 "Insert subvector index must be constant");
7551 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7552 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7554 "Insert subvector overflow!");
7556 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7557 "Constant index for INSERT_SUBVECTOR has an invalid size");
7558
7559 // Trivial insertion.
7560 if (VT == N2VT)
7561 return N2;
7562
7563 // If this is an insert of an extracted vector into an undef vector, we
7564 // can just use the input to the extract.
7565 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7566 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7567 return N2.getOperand(0);
7568 break;
7569 }
7570 case ISD::BITCAST:
7571 // Fold bit_convert nodes from a type to themselves.
7572 if (N1.getValueType() == VT)
7573 return N1;
7574 break;
7575 case ISD::VP_TRUNCATE:
7576 case ISD::VP_SIGN_EXTEND:
7577 case ISD::VP_ZERO_EXTEND:
7578 // Don't create noop casts.
7579 if (N1.getValueType() == VT)
7580 return N1;
7581 break;
7582 case ISD::VECTOR_COMPRESS: {
7583 [[maybe_unused]] EVT VecVT = N1.getValueType();
7584 [[maybe_unused]] EVT MaskVT = N2.getValueType();
7585 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
7586 assert(VT == VecVT && "Vector and result type don't match.");
7587 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
7588 "All inputs must be vectors.");
7589 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
7591 "Vector and mask must have same number of elements.");
7592
7593 if (N1.isUndef() || N2.isUndef())
7594 return N3;
7595
7596 break;
7597 }
7598 }
7599
7600 // Memoize node if it doesn't produce a glue result.
7601 SDNode *N;
7602 SDVTList VTs = getVTList(VT);
7603 SDValue Ops[] = {N1, N2, N3};
7604 if (VT != MVT::Glue) {
7606 AddNodeIDNode(ID, Opcode, VTs, Ops);
7607 void *IP = nullptr;
7608 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7609 E->intersectFlagsWith(Flags);
7610 return SDValue(E, 0);
7611 }
7612
7613 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7614 N->setFlags(Flags);
7615 createOperands(N, Ops);
7616 CSEMap.InsertNode(N, IP);
7617 } else {
7618 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7619 createOperands(N, Ops);
7620 }
7621
7622 InsertNode(N);
7623 SDValue V = SDValue(N, 0);
7624 NewSDValueDbgMsg(V, "Creating new node: ", this);
7625 return V;
7626}
7627
7628SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7629 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7630 const SDNodeFlags Flags) {
7631 SDValue Ops[] = { N1, N2, N3, N4 };
7632 return getNode(Opcode, DL, VT, Ops, Flags);
7633}
7634
7635SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7636 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7637 SDNodeFlags Flags;
7638 if (Inserter)
7639 Flags = Inserter->getFlags();
7640 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
7641}
7642
7643SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7644 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7645 SDValue N5, const SDNodeFlags Flags) {
7646 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7647 return getNode(Opcode, DL, VT, Ops, Flags);
7648}
7649
7650SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7651 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7652 SDValue N5) {
7653 SDNodeFlags Flags;
7654 if (Inserter)
7655 Flags = Inserter->getFlags();
7656 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
7657}
7658
7659/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7660/// the incoming stack arguments to be loaded from the stack.
7662 SmallVector<SDValue, 8> ArgChains;
7663
7664 // Include the original chain at the beginning of the list. When this is
7665 // used by target LowerCall hooks, this helps legalize find the
7666 // CALLSEQ_BEGIN node.
7667 ArgChains.push_back(Chain);
7668
7669 // Add a chain value for each stack argument.
7670 for (SDNode *U : getEntryNode().getNode()->uses())
7671 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7672 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7673 if (FI->getIndex() < 0)
7674 ArgChains.push_back(SDValue(L, 1));
7675
7676 // Build a tokenfactor for all the chains.
7677 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7678}
7679
7680/// getMemsetValue - Vectorized representation of the memset value
7681/// operand.
7683 const SDLoc &dl) {
7684 assert(!Value.isUndef());
7685
7686 unsigned NumBits = VT.getScalarSizeInBits();
7687 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7688 assert(C->getAPIntValue().getBitWidth() == 8);
7689 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7690 if (VT.isInteger()) {
7691 bool IsOpaque = VT.getSizeInBits() > 64 ||
7692 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7693 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7694 }
7695 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
7696 }
7697
7698 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7699 EVT IntVT = VT.getScalarType();
7700 if (!IntVT.isInteger())
7701 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7702
7703 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7704 if (NumBits > 8) {
7705 // Use a multiplication with 0x010101... to extend the input to the
7706 // required length.
7707 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7708 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7709 DAG.getConstant(Magic, dl, IntVT));
7710 }
7711
7712 if (VT != Value.getValueType() && !VT.isInteger())
7713 Value = DAG.getBitcast(VT.getScalarType(), Value);
7714 if (VT != Value.getValueType())
7715 Value = DAG.getSplatBuildVector(VT, dl, Value);
7716
7717 return Value;
7718}
7719
7720/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
7721/// used when a memcpy is turned into a memset when the source is a constant
7722/// string ptr.
7724 const TargetLowering &TLI,
7725 const ConstantDataArraySlice &Slice) {
7726 // Handle vector with all elements zero.
7727 if (Slice.Array == nullptr) {
7728 if (VT.isInteger())
7729 return DAG.getConstant(0, dl, VT);
7730 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
7731 return DAG.getConstantFP(0.0, dl, VT);
7732 if (VT.isVector()) {
7733 unsigned NumElts = VT.getVectorNumElements();
7734 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
7735 return DAG.getNode(ISD::BITCAST, dl, VT,
7736 DAG.getConstant(0, dl,
7738 EltVT, NumElts)));
7739 }
7740 llvm_unreachable("Expected type!");
7741 }
7742
7743 assert(!VT.isVector() && "Can't handle vector type here!");
7744 unsigned NumVTBits = VT.getSizeInBits();
7745 unsigned NumVTBytes = NumVTBits / 8;
7746 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
7747
7748 APInt Val(NumVTBits, 0);
7749 if (DAG.getDataLayout().isLittleEndian()) {
7750 for (unsigned i = 0; i != NumBytes; ++i)
7751 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
7752 } else {
7753 for (unsigned i = 0; i != NumBytes; ++i)
7754 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
7755 }
7756
7757 // If the "cost" of materializing the integer immediate is less than the cost
7758 // of a load, then it is cost effective to turn the load into the immediate.
7759 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
7760 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
7761 return DAG.getConstant(Val, dl, VT);
7762 return SDValue();
7763}
7764
7766 const SDLoc &DL,
7767 const SDNodeFlags Flags) {
7768 EVT VT = Base.getValueType();
7769 SDValue Index;
7770
7771 if (Offset.isScalable())
7772 Index = getVScale(DL, Base.getValueType(),
7773 APInt(Base.getValueSizeInBits().getFixedValue(),
7774 Offset.getKnownMinValue()));
7775 else
7776 Index = getConstant(Offset.getFixedValue(), DL, VT);
7777
7778 return getMemBasePlusOffset(Base, Index, DL, Flags);
7779}
7780
7782 const SDLoc &DL,
7783 const SDNodeFlags Flags) {
7784 assert(Offset.getValueType().isInteger());
7785 EVT BasePtrVT = Ptr.getValueType();
7786 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
7787}
7788
7789/// Returns true if memcpy source is constant data.
7791 uint64_t SrcDelta = 0;
7792 GlobalAddressSDNode *G = nullptr;
7793 if (Src.getOpcode() == ISD::GlobalAddress)
7794 G = cast<GlobalAddressSDNode>(Src);
7795 else if (Src.getOpcode() == ISD::ADD &&
7796 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
7797 Src.getOperand(1).getOpcode() == ISD::Constant) {
7798 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
7799 SrcDelta = Src.getConstantOperandVal(1);
7800 }
7801 if (!G)
7802 return false;
7803
7804 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
7805 SrcDelta + G->getOffset());
7806}
7807
7809 SelectionDAG &DAG) {
7810 // On Darwin, -Os means optimize for size without hurting performance, so
7811 // only really optimize for size when -Oz (MinSize) is used.
7813 return MF.getFunction().hasMinSize();
7814 return DAG.shouldOptForSize();
7815}
7816
7818 SmallVector<SDValue, 32> &OutChains, unsigned From,
7819 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
7820 SmallVector<SDValue, 16> &OutStoreChains) {
7821 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
7822 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
7823 SmallVector<SDValue, 16> GluedLoadChains;
7824 for (unsigned i = From; i < To; ++i) {
7825 OutChains.push_back(OutLoadChains[i]);
7826 GluedLoadChains.push_back(OutLoadChains[i]);
7827 }
7828
7829 // Chain for all loads.
7830 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7831 GluedLoadChains);
7832
7833 for (unsigned i = From; i < To; ++i) {
7834 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
7835 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
7836 ST->getBasePtr(), ST->getMemoryVT(),
7837 ST->getMemOperand());
7838 OutChains.push_back(NewStore);
7839 }
7840}
7841
7843 SDValue Chain, SDValue Dst, SDValue Src,
7844 uint64_t Size, Align Alignment,
7845 bool isVol, bool AlwaysInline,
7846 MachinePointerInfo DstPtrInfo,
7847 MachinePointerInfo SrcPtrInfo,
7848 const AAMDNodes &AAInfo, AAResults *AA) {
7849 // Turn a memcpy of undef to nop.
7850 // FIXME: We need to honor volatile even is Src is undef.
7851 if (Src.isUndef())
7852 return Chain;
7853
7854 // Expand memcpy to a series of load and store ops if the size operand falls
7855 // below a certain threshold.
7856 // TODO: In the AlwaysInline case, if the size is big then generate a loop
7857 // rather than maybe a humongous number of loads and stores.
7858 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7859 const DataLayout &DL = DAG.getDataLayout();
7860 LLVMContext &C = *DAG.getContext();
7861 std::vector<EVT> MemOps;
7862 bool DstAlignCanChange = false;
7864 MachineFrameInfo &MFI = MF.getFrameInfo();
7865 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7866 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7867 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7868 DstAlignCanChange = true;
7869 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7870 if (!SrcAlign || Alignment > *SrcAlign)
7871 SrcAlign = Alignment;
7872 assert(SrcAlign && "SrcAlign must be set");
7874 // If marked as volatile, perform a copy even when marked as constant.
7875 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
7876 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
7877 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
7878 const MemOp Op = isZeroConstant
7879 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
7880 /*IsZeroMemset*/ true, isVol)
7881 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
7882 *SrcAlign, isVol, CopyFromConstant);
7883 if (!TLI.findOptimalMemOpLowering(
7884 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
7885 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
7886 return SDValue();
7887
7888 if (DstAlignCanChange) {
7889 Type *Ty = MemOps[0].getTypeForEVT(C);
7890 Align NewAlign = DL.getABITypeAlign(Ty);
7891
7892 // Don't promote to an alignment that would require dynamic stack
7893 // realignment which may conflict with optimizations such as tail call
7894 // optimization.
7896 if (!TRI->hasStackRealignment(MF))
7897 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7898 NewAlign = NewAlign.previous();
7899
7900 if (NewAlign > Alignment) {
7901 // Give the stack frame object a larger alignment if needed.
7902 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7903 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7904 Alignment = NewAlign;
7905 }
7906 }
7907
7908 // Prepare AAInfo for loads/stores after lowering this memcpy.
7909 AAMDNodes NewAAInfo = AAInfo;
7910 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7911
7912 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
7913 bool isConstant =
7914 AA && SrcVal &&
7915 AA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
7916
7917 MachineMemOperand::Flags MMOFlags =
7919 SmallVector<SDValue, 16> OutLoadChains;
7920 SmallVector<SDValue, 16> OutStoreChains;
7921 SmallVector<SDValue, 32> OutChains;
7922 unsigned NumMemOps = MemOps.size();
7923 uint64_t SrcOff = 0, DstOff = 0;
7924 for (unsigned i = 0; i != NumMemOps; ++i) {
7925 EVT VT = MemOps[i];
7926 unsigned VTSize = VT.getSizeInBits() / 8;
7927 SDValue Value, Store;
7928
7929 if (VTSize > Size) {
7930 // Issuing an unaligned load / store pair that overlaps with the previous
7931 // pair. Adjust the offset accordingly.
7932 assert(i == NumMemOps-1 && i != 0);
7933 SrcOff -= VTSize - Size;
7934 DstOff -= VTSize - Size;
7935 }
7936
7937 if (CopyFromConstant &&
7938 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
7939 // It's unlikely a store of a vector immediate can be done in a single
7940 // instruction. It would require a load from a constantpool first.
7941 // We only handle zero vectors here.
7942 // FIXME: Handle other cases where store of vector immediate is done in
7943 // a single instruction.
7944 ConstantDataArraySlice SubSlice;
7945 if (SrcOff < Slice.Length) {
7946 SubSlice = Slice;
7947 SubSlice.move(SrcOff);
7948 } else {
7949 // This is an out-of-bounds access and hence UB. Pretend we read zero.
7950 SubSlice.Array = nullptr;
7951 SubSlice.Offset = 0;
7952 SubSlice.Length = VTSize;
7953 }
7954 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
7955 if (Value.getNode()) {
7956 Store = DAG.getStore(
7957 Chain, dl, Value,
7958 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7959 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7960 OutChains.push_back(Store);
7961 }
7962 }
7963
7964 if (!Store.getNode()) {
7965 // The type might not be legal for the target. This should only happen
7966 // if the type is smaller than a legal type, as on PPC, so the right
7967 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
7968 // to Load/Store if NVT==VT.
7969 // FIXME does the case above also need this?
7970 EVT NVT = TLI.getTypeToTransformTo(C, VT);
7971 assert(NVT.bitsGE(VT));
7972
7973 bool isDereferenceable =
7974 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7975 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7976 if (isDereferenceable)
7978 if (isConstant)
7979 SrcMMOFlags |= MachineMemOperand::MOInvariant;
7980
7981 Value = DAG.getExtLoad(
7982 ISD::EXTLOAD, dl, NVT, Chain,
7983 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7984 SrcPtrInfo.getWithOffset(SrcOff), VT,
7985 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
7986 OutLoadChains.push_back(Value.getValue(1));
7987
7988 Store = DAG.getTruncStore(
7989 Chain, dl, Value,
7990 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7991 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
7992 OutStoreChains.push_back(Store);
7993 }
7994 SrcOff += VTSize;
7995 DstOff += VTSize;
7996 Size -= VTSize;
7997 }
7998
7999 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8001 unsigned NumLdStInMemcpy = OutStoreChains.size();
8002
8003 if (NumLdStInMemcpy) {
8004 // It may be that memcpy might be converted to memset if it's memcpy
8005 // of constants. In such a case, we won't have loads and stores, but
8006 // just stores. In the absence of loads, there is nothing to gang up.
8007 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8008 // If target does not care, just leave as it.
8009 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8010 OutChains.push_back(OutLoadChains[i]);
8011 OutChains.push_back(OutStoreChains[i]);
8012 }
8013 } else {
8014 // Ld/St less than/equal limit set by target.
8015 if (NumLdStInMemcpy <= GluedLdStLimit) {
8016 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8017 NumLdStInMemcpy, OutLoadChains,
8018 OutStoreChains);
8019 } else {
8020 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8021 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8022 unsigned GlueIter = 0;
8023
8024 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8025 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8026 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8027
8028 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8029 OutLoadChains, OutStoreChains);
8030 GlueIter += GluedLdStLimit;
8031 }
8032
8033 // Residual ld/st.
8034 if (RemainingLdStInMemcpy) {
8035 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8036 RemainingLdStInMemcpy, OutLoadChains,
8037 OutStoreChains);
8038 }
8039 }
8040 }
8041 }
8042 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8043}
8044
8046 SDValue Chain, SDValue Dst, SDValue Src,
8047 uint64_t Size, Align Alignment,
8048 bool isVol, bool AlwaysInline,
8049 MachinePointerInfo DstPtrInfo,
8050 MachinePointerInfo SrcPtrInfo,
8051 const AAMDNodes &AAInfo) {
8052 // Turn a memmove of undef to nop.
8053 // FIXME: We need to honor volatile even is Src is undef.
8054 if (Src.isUndef())
8055 return Chain;
8056
8057 // Expand memmove to a series of load and store ops if the size operand falls
8058 // below a certain threshold.
8059 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8060 const DataLayout &DL = DAG.getDataLayout();
8061 LLVMContext &C = *DAG.getContext();
8062 std::vector<EVT> MemOps;
8063 bool DstAlignCanChange = false;
8065 MachineFrameInfo &MFI = MF.getFrameInfo();
8066 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8067 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8068 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8069 DstAlignCanChange = true;
8070 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8071 if (!SrcAlign || Alignment > *SrcAlign)
8072 SrcAlign = Alignment;
8073 assert(SrcAlign && "SrcAlign must be set");
8074 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8075 if (!TLI.findOptimalMemOpLowering(
8076 MemOps, Limit,
8077 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8078 /*IsVolatile*/ true),
8079 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8080 MF.getFunction().getAttributes()))
8081 return SDValue();
8082
8083 if (DstAlignCanChange) {
8084 Type *Ty = MemOps[0].getTypeForEVT(C);
8085 Align NewAlign = DL.getABITypeAlign(Ty);
8086
8087 // Don't promote to an alignment that would require dynamic stack
8088 // realignment which may conflict with optimizations such as tail call
8089 // optimization.
8091 if (!TRI->hasStackRealignment(MF))
8092 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8093 NewAlign = NewAlign.previous();
8094
8095 if (NewAlign > Alignment) {
8096 // Give the stack frame object a larger alignment if needed.
8097 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8098 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8099 Alignment = NewAlign;
8100 }
8101 }
8102
8103 // Prepare AAInfo for loads/stores after lowering this memmove.
8104 AAMDNodes NewAAInfo = AAInfo;
8105 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8106
8107 MachineMemOperand::Flags MMOFlags =
8109 uint64_t SrcOff = 0, DstOff = 0;
8110 SmallVector<SDValue, 8> LoadValues;
8111 SmallVector<SDValue, 8> LoadChains;
8112 SmallVector<SDValue, 8> OutChains;
8113 unsigned NumMemOps = MemOps.size();
8114 for (unsigned i = 0; i < NumMemOps; i++) {
8115 EVT VT = MemOps[i];
8116 unsigned VTSize = VT.getSizeInBits() / 8;
8117 SDValue Value;
8118
8119 bool isDereferenceable =
8120 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8121 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8122 if (isDereferenceable)
8124
8125 Value = DAG.getLoad(
8126 VT, dl, Chain,
8127 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8128 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8129 LoadValues.push_back(Value);
8130 LoadChains.push_back(Value.getValue(1));
8131 SrcOff += VTSize;
8132 }
8133 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8134 OutChains.clear();
8135 for (unsigned i = 0; i < NumMemOps; i++) {
8136 EVT VT = MemOps[i];
8137 unsigned VTSize = VT.getSizeInBits() / 8;
8138 SDValue Store;
8139
8140 Store = DAG.getStore(
8141 Chain, dl, LoadValues[i],
8142 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8143 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8144 OutChains.push_back(Store);
8145 DstOff += VTSize;
8146 }
8147
8148 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8149}
8150
8151/// Lower the call to 'memset' intrinsic function into a series of store
8152/// operations.
8153///
8154/// \param DAG Selection DAG where lowered code is placed.
8155/// \param dl Link to corresponding IR location.
8156/// \param Chain Control flow dependency.
8157/// \param Dst Pointer to destination memory location.
8158/// \param Src Value of byte to write into the memory.
8159/// \param Size Number of bytes to write.
8160/// \param Alignment Alignment of the destination in bytes.
8161/// \param isVol True if destination is volatile.
8162/// \param AlwaysInline Makes sure no function call is generated.
8163/// \param DstPtrInfo IR information on the memory pointer.
8164/// \returns New head in the control flow, if lowering was successful, empty
8165/// SDValue otherwise.
8166///
8167/// The function tries to replace 'llvm.memset' intrinsic with several store
8168/// operations and value calculation code. This is usually profitable for small
8169/// memory size or when the semantic requires inlining.
8171 SDValue Chain, SDValue Dst, SDValue Src,
8172 uint64_t Size, Align Alignment, bool isVol,
8173 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8174 const AAMDNodes &AAInfo) {
8175 // Turn a memset of undef to nop.
8176 // FIXME: We need to honor volatile even is Src is undef.
8177 if (Src.isUndef())
8178 return Chain;
8179
8180 // Expand memset to a series of load/store ops if the size operand
8181 // falls below a certain threshold.
8182 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8183 std::vector<EVT> MemOps;
8184 bool DstAlignCanChange = false;
8186 MachineFrameInfo &MFI = MF.getFrameInfo();
8187 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8188 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8189 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8190 DstAlignCanChange = true;
8191 bool IsZeroVal = isNullConstant(Src);
8192 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8193
8194 if (!TLI.findOptimalMemOpLowering(
8195 MemOps, Limit,
8196 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8197 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8198 return SDValue();
8199
8200 if (DstAlignCanChange) {
8201 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8202 const DataLayout &DL = DAG.getDataLayout();
8203 Align NewAlign = DL.getABITypeAlign(Ty);
8204
8205 // Don't promote to an alignment that would require dynamic stack
8206 // realignment which may conflict with optimizations such as tail call
8207 // optimization.
8209 if (!TRI->hasStackRealignment(MF))
8210 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8211 NewAlign = NewAlign.previous();
8212
8213 if (NewAlign > Alignment) {
8214 // Give the stack frame object a larger alignment if needed.
8215 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8216 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8217 Alignment = NewAlign;
8218 }
8219 }
8220
8221 SmallVector<SDValue, 8> OutChains;
8222 uint64_t DstOff = 0;
8223 unsigned NumMemOps = MemOps.size();
8224
8225 // Find the largest store and generate the bit pattern for it.
8226 EVT LargestVT = MemOps[0];
8227 for (unsigned i = 1; i < NumMemOps; i++)
8228 if (MemOps[i].bitsGT(LargestVT))
8229 LargestVT = MemOps[i];
8230 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8231
8232 // Prepare AAInfo for loads/stores after lowering this memset.
8233 AAMDNodes NewAAInfo = AAInfo;
8234 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8235
8236 for (unsigned i = 0; i < NumMemOps; i++) {
8237 EVT VT = MemOps[i];
8238 unsigned VTSize = VT.getSizeInBits() / 8;
8239 if (VTSize > Size) {
8240 // Issuing an unaligned load / store pair that overlaps with the previous
8241 // pair. Adjust the offset accordingly.
8242 assert(i == NumMemOps-1 && i != 0);
8243 DstOff -= VTSize - Size;
8244 }
8245
8246 // If this store is smaller than the largest store see whether we can get
8247 // the smaller value for free with a truncate or extract vector element and
8248 // then store.
8249 SDValue Value = MemSetValue;
8250 if (VT.bitsLT(LargestVT)) {
8251 unsigned Index;
8252 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8253 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8254 if (!LargestVT.isVector() && !VT.isVector() &&
8255 TLI.isTruncateFree(LargestVT, VT))
8256 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8257 else if (LargestVT.isVector() && !VT.isVector() &&
8259 LargestVT.getTypeForEVT(*DAG.getContext()),
8260 VT.getSizeInBits(), Index) &&
8261 TLI.isTypeLegal(SVT) &&
8262 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8263 // Target which can combine store(extractelement VectorTy, Idx) can get
8264 // the smaller value for free.
8265 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8266 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
8267 DAG.getVectorIdxConstant(Index, dl));
8268 } else
8269 Value = getMemsetValue(Src, VT, DAG, dl);
8270 }
8271 assert(Value.getValueType() == VT && "Value with wrong type.");
8272 SDValue Store = DAG.getStore(
8273 Chain, dl, Value,
8274 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8275 DstPtrInfo.getWithOffset(DstOff), Alignment,
8277 NewAAInfo);
8278 OutChains.push_back(Store);
8279 DstOff += VT.getSizeInBits() / 8;
8280 Size -= VTSize;
8281 }
8282
8283 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8284}
8285
8287 unsigned AS) {
8288 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8289 // pointer operands can be losslessly bitcasted to pointers of address space 0
8290 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8291 report_fatal_error("cannot lower memory intrinsic in address space " +
8292 Twine(AS));
8293 }
8294}
8295
8297 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
8298 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
8299 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
8300 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, AAResults *AA) {
8301 // Check to see if we should lower the memcpy to loads and stores first.
8302 // For cases within the target-specified limits, this is the best choice.
8303 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8304 if (ConstantSize) {
8305 // Memcpy with size zero? Just return the original chain.
8306 if (ConstantSize->isZero())
8307 return Chain;
8308
8310 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8311 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8312 if (Result.getNode())
8313 return Result;
8314 }
8315
8316 // Then check to see if we should lower the memcpy with target-specific
8317 // code. If the target chooses to do this, this is the next best.
8318 if (TSI) {
8319 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8320 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8321 DstPtrInfo, SrcPtrInfo);
8322 if (Result.getNode())
8323 return Result;
8324 }
8325
8326 // If we really need inline code and the target declined to provide it,
8327 // use a (potentially long) sequence of loads and stores.
8328 if (AlwaysInline) {
8329 assert(ConstantSize && "AlwaysInline requires a constant size!");
8331 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8332 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8333 }
8334
8337
8338 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8339 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8340 // respect volatile, so they may do things like read or write memory
8341 // beyond the given memory regions. But fixing this isn't easy, and most
8342 // people don't care.
8343
8344 // Emit a library call.
8347 Entry.Ty = PointerType::getUnqual(*getContext());
8348 Entry.Node = Dst; Args.push_back(Entry);
8349 Entry.Node = Src; Args.push_back(Entry);
8350
8351 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8352 Entry.Node = Size; Args.push_back(Entry);
8353 // FIXME: pass in SDLoc
8355 bool IsTailCall = false;
8356 if (OverrideTailCall.has_value()) {
8357 IsTailCall = *OverrideTailCall;
8358 } else {
8359 bool LowersToMemcpy =
8360 TLI->getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy");
8361 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8362 IsTailCall = CI && CI->isTailCall() &&
8364 ReturnsFirstArg && LowersToMemcpy);
8365 }
8366
8367 CLI.setDebugLoc(dl)
8368 .setChain(Chain)
8369 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8370 Dst.getValueType().getTypeForEVT(*getContext()),
8371 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8372 TLI->getPointerTy(getDataLayout())),
8373 std::move(Args))
8375 .setTailCall(IsTailCall);
8376
8377 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8378 return CallResult.second;
8379}
8380
8382 SDValue Dst, SDValue Src, SDValue Size,
8383 Type *SizeTy, unsigned ElemSz,
8384 bool isTailCall,
8385 MachinePointerInfo DstPtrInfo,
8386 MachinePointerInfo SrcPtrInfo) {
8387 // Emit a library call.
8390 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8391 Entry.Node = Dst;
8392 Args.push_back(Entry);
8393
8394 Entry.Node = Src;
8395 Args.push_back(Entry);
8396
8397 Entry.Ty = SizeTy;
8398 Entry.Node = Size;
8399 Args.push_back(Entry);
8400
8401 RTLIB::Libcall LibraryCall =
8403 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8404 report_fatal_error("Unsupported element size");
8405
8407 CLI.setDebugLoc(dl)
8408 .setChain(Chain)
8409 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8411 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8412 TLI->getPointerTy(getDataLayout())),
8413 std::move(Args))
8415 .setTailCall(isTailCall);
8416
8417 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8418 return CallResult.second;
8419}
8420
8422 SDValue Src, SDValue Size, Align Alignment,
8423 bool isVol, const CallInst *CI,
8424 std::optional<bool> OverrideTailCall,
8425 MachinePointerInfo DstPtrInfo,
8426 MachinePointerInfo SrcPtrInfo,
8427 const AAMDNodes &AAInfo, AAResults *AA) {
8428 // Check to see if we should lower the memmove to loads and stores first.
8429 // For cases within the target-specified limits, this is the best choice.
8430 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8431 if (ConstantSize) {
8432 // Memmove with size zero? Just return the original chain.
8433 if (ConstantSize->isZero())
8434 return Chain;
8435
8437 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8438 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8439 if (Result.getNode())
8440 return Result;
8441 }
8442
8443 // Then check to see if we should lower the memmove with target-specific
8444 // code. If the target chooses to do this, this is the next best.
8445 if (TSI) {
8446 SDValue Result =
8447 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8448 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8449 if (Result.getNode())
8450 return Result;
8451 }
8452
8455
8456 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8457 // not be safe. See memcpy above for more details.
8458
8459 // Emit a library call.
8462 Entry.Ty = PointerType::getUnqual(*getContext());
8463 Entry.Node = Dst; Args.push_back(Entry);
8464 Entry.Node = Src; Args.push_back(Entry);
8465
8466 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8467 Entry.Node = Size; Args.push_back(Entry);
8468 // FIXME: pass in SDLoc
8470
8471 bool IsTailCall = false;
8472 if (OverrideTailCall.has_value()) {
8473 IsTailCall = *OverrideTailCall;
8474 } else {
8475 bool LowersToMemmove =
8476 TLI->getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove");
8477 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8478 IsTailCall = CI && CI->isTailCall() &&
8480 ReturnsFirstArg && LowersToMemmove);
8481 }
8482
8483 CLI.setDebugLoc(dl)
8484 .setChain(Chain)
8485 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8486 Dst.getValueType().getTypeForEVT(*getContext()),
8487 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8488 TLI->getPointerTy(getDataLayout())),
8489 std::move(Args))
8491 .setTailCall(IsTailCall);
8492
8493 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8494 return CallResult.second;
8495}
8496
8498 SDValue Dst, SDValue Src, SDValue Size,
8499 Type *SizeTy, unsigned ElemSz,
8500 bool isTailCall,
8501 MachinePointerInfo DstPtrInfo,
8502 MachinePointerInfo SrcPtrInfo) {
8503 // Emit a library call.
8506 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8507 Entry.Node = Dst;
8508 Args.push_back(Entry);
8509
8510 Entry.Node = Src;
8511 Args.push_back(Entry);
8512
8513 Entry.Ty = SizeTy;
8514 Entry.Node = Size;
8515 Args.push_back(Entry);
8516
8517 RTLIB::Libcall LibraryCall =
8519 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8520 report_fatal_error("Unsupported element size");
8521
8523 CLI.setDebugLoc(dl)
8524 .setChain(Chain)
8525 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8527 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8528 TLI->getPointerTy(getDataLayout())),
8529 std::move(Args))
8531 .setTailCall(isTailCall);
8532
8533 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8534 return CallResult.second;
8535}
8536
8538 SDValue Src, SDValue Size, Align Alignment,
8539 bool isVol, bool AlwaysInline,
8540 const CallInst *CI,
8541 MachinePointerInfo DstPtrInfo,
8542 const AAMDNodes &AAInfo) {
8543 // Check to see if we should lower the memset to stores first.
8544 // For cases within the target-specified limits, this is the best choice.
8545 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8546 if (ConstantSize) {
8547 // Memset with size zero? Just return the original chain.
8548 if (ConstantSize->isZero())
8549 return Chain;
8550
8551 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8552 ConstantSize->getZExtValue(), Alignment,
8553 isVol, false, DstPtrInfo, AAInfo);
8554
8555 if (Result.getNode())
8556 return Result;
8557 }
8558
8559 // Then check to see if we should lower the memset with target-specific
8560 // code. If the target chooses to do this, this is the next best.
8561 if (TSI) {
8562 SDValue Result = TSI->EmitTargetCodeForMemset(
8563 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8564 if (Result.getNode())
8565 return Result;
8566 }
8567
8568 // If we really need inline code and the target declined to provide it,
8569 // use a (potentially long) sequence of loads and stores.
8570 if (AlwaysInline) {
8571 assert(ConstantSize && "AlwaysInline requires a constant size!");
8572 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8573 ConstantSize->getZExtValue(), Alignment,
8574 isVol, true, DstPtrInfo, AAInfo);
8575 assert(Result &&
8576 "getMemsetStores must return a valid sequence when AlwaysInline");
8577 return Result;
8578 }
8579
8581
8582 // Emit a library call.
8583 auto &Ctx = *getContext();
8584 const auto& DL = getDataLayout();
8585
8587 // FIXME: pass in SDLoc
8588 CLI.setDebugLoc(dl).setChain(Chain);
8589
8590 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8591
8592 // Helper function to create an Entry from Node and Type.
8593 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8595 Entry.Node = Node;
8596 Entry.Ty = Ty;
8597 return Entry;
8598 };
8599
8600 bool UseBZero = isNullConstant(Src) && BzeroName;
8601 // If zeroing out and bzero is present, use it.
8602 if (UseBZero) {
8604 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8605 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8606 CLI.setLibCallee(
8607 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8608 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8609 } else {
8611 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8612 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8613 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8614 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8615 Dst.getValueType().getTypeForEVT(Ctx),
8616 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8617 TLI->getPointerTy(DL)),
8618 std::move(Args));
8619 }
8620 bool LowersToMemset =
8621 TLI->getLibcallName(RTLIB::MEMSET) == StringRef("memset");
8622 // If we're going to use bzero, make sure not to tail call unless the
8623 // subsequent return doesn't need a value, as bzero doesn't return the first
8624 // arg unlike memset.
8625 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
8626 bool IsTailCall =
8627 CI && CI->isTailCall() &&
8628 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
8629 CLI.setDiscardResult().setTailCall(IsTailCall);
8630
8631 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8632 return CallResult.second;
8633}
8634
8637 Type *SizeTy, unsigned ElemSz,
8638 bool isTailCall,
8639 MachinePointerInfo DstPtrInfo) {
8640 // Emit a library call.
8643 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8644 Entry.Node = Dst;
8645 Args.push_back(Entry);
8646
8647 Entry.Ty = Type::getInt8Ty(*getContext());
8648 Entry.Node = Value;
8649 Args.push_back(Entry);
8650
8651 Entry.Ty = SizeTy;
8652 Entry.Node = Size;
8653 Args.push_back(Entry);
8654
8655 RTLIB::Libcall LibraryCall =
8657 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8658 report_fatal_error("Unsupported element size");
8659
8661 CLI.setDebugLoc(dl)
8662 .setChain(Chain)
8663 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8665 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8666 TLI->getPointerTy(getDataLayout())),
8667 std::move(Args))
8669 .setTailCall(isTailCall);
8670
8671 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8672 return CallResult.second;
8673}
8674
8675SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8676 SDVTList VTList, ArrayRef<SDValue> Ops,
8677 MachineMemOperand *MMO) {
8679 ID.AddInteger(MemVT.getRawBits());
8680 AddNodeIDNode(ID, Opcode, VTList, Ops);
8681 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8682 ID.AddInteger(MMO->getFlags());
8683 void* IP = nullptr;
8684 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8685 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8686 return SDValue(E, 0);
8687 }
8688
8689 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8690 VTList, MemVT, MMO);
8691 createOperands(N, Ops);
8692
8693 CSEMap.InsertNode(N, IP);
8694 InsertNode(N);
8695 return SDValue(N, 0);
8696}
8697
8699 EVT MemVT, SDVTList VTs, SDValue Chain,
8700 SDValue Ptr, SDValue Cmp, SDValue Swp,
8701 MachineMemOperand *MMO) {
8702 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8704 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8705
8706 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8707 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8708}
8709
8710SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8711 SDValue Chain, SDValue Ptr, SDValue Val,
8712 MachineMemOperand *MMO) {
8713 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
8714 Opcode == ISD::ATOMIC_LOAD_SUB ||
8715 Opcode == ISD::ATOMIC_LOAD_AND ||
8716 Opcode == ISD::ATOMIC_LOAD_CLR ||
8717 Opcode == ISD::ATOMIC_LOAD_OR ||
8718 Opcode == ISD::ATOMIC_LOAD_XOR ||
8719 Opcode == ISD::ATOMIC_LOAD_NAND ||
8720 Opcode == ISD::ATOMIC_LOAD_MIN ||
8721 Opcode == ISD::ATOMIC_LOAD_MAX ||
8722 Opcode == ISD::ATOMIC_LOAD_UMIN ||
8723 Opcode == ISD::ATOMIC_LOAD_UMAX ||
8724 Opcode == ISD::ATOMIC_LOAD_FADD ||
8725 Opcode == ISD::ATOMIC_LOAD_FSUB ||
8726 Opcode == ISD::ATOMIC_LOAD_FMAX ||
8727 Opcode == ISD::ATOMIC_LOAD_FMIN ||
8728 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
8729 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
8730 Opcode == ISD::ATOMIC_SWAP ||
8731 Opcode == ISD::ATOMIC_STORE) &&
8732 "Invalid Atomic Op");
8733
8734 EVT VT = Val.getValueType();
8735
8736 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
8737 getVTList(VT, MVT::Other);
8738 SDValue Ops[] = {Chain, Ptr, Val};
8739 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8740}
8741
8742SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8743 EVT VT, SDValue Chain, SDValue Ptr,
8744 MachineMemOperand *MMO) {
8745 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
8746
8747 SDVTList VTs = getVTList(VT, MVT::Other);
8748 SDValue Ops[] = {Chain, Ptr};
8749 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8750}
8751
8752/// getMergeValues - Create a MERGE_VALUES node from the given operands.
8754 if (Ops.size() == 1)
8755 return Ops[0];
8756
8758 VTs.reserve(Ops.size());
8759 for (const SDValue &Op : Ops)
8760 VTs.push_back(Op.getValueType());
8761 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
8762}
8763
8765 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
8766 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
8768 const AAMDNodes &AAInfo) {
8769 if (Size.hasValue() && !Size.getValue())
8771
8773 MachineMemOperand *MMO =
8774 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
8775
8776 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
8777}
8778
8780 SDVTList VTList,
8781 ArrayRef<SDValue> Ops, EVT MemVT,
8782 MachineMemOperand *MMO) {
8783 assert((Opcode == ISD::INTRINSIC_VOID ||
8784 Opcode == ISD::INTRINSIC_W_CHAIN ||
8785 Opcode == ISD::PREFETCH ||
8786 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
8787 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
8788 "Opcode is not a memory-accessing opcode!");
8789
8790 // Memoize the node unless it returns a glue result.
8792 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
8794 AddNodeIDNode(ID, Opcode, VTList, Ops);
8795 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
8796 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
8797 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8798 ID.AddInteger(MMO->getFlags());
8799 ID.AddInteger(MemVT.getRawBits());
8800 void *IP = nullptr;
8801 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8802 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
8803 return SDValue(E, 0);
8804 }
8805
8806 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8807 VTList, MemVT, MMO);
8808 createOperands(N, Ops);
8809
8810 CSEMap.InsertNode(N, IP);
8811 } else {
8812 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8813 VTList, MemVT, MMO);
8814 createOperands(N, Ops);
8815 }
8816 InsertNode(N);
8817 SDValue V(N, 0);
8818 NewSDValueDbgMsg(V, "Creating new node: ", this);
8819 return V;
8820}
8821
8823 SDValue Chain, int FrameIndex,
8824 int64_t Size, int64_t Offset) {
8825 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
8826 const auto VTs = getVTList(MVT::Other);
8827 SDValue Ops[2] = {
8828 Chain,
8829 getFrameIndex(FrameIndex,
8830 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
8831 true)};
8832
8834 AddNodeIDNode(ID, Opcode, VTs, Ops);
8835 ID.AddInteger(FrameIndex);
8836 ID.AddInteger(Size);
8837 ID.AddInteger(Offset);
8838 void *IP = nullptr;
8839 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8840 return SDValue(E, 0);
8841
8842 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
8843 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
8844 createOperands(N, Ops);
8845 CSEMap.InsertNode(N, IP);
8846 InsertNode(N);
8847 SDValue V(N, 0);
8848 NewSDValueDbgMsg(V, "Creating new node: ", this);
8849 return V;
8850}
8851
8854 uint32_t Attr) {
8855 const unsigned Opcode = ISD::PSEUDO_PROBE;
8856 const auto VTs = getVTList(MVT::Other);
8857 SDValue Ops[] = {Chain};
8859 AddNodeIDNode(ID, Opcode, VTs, Ops);
8860 ID.AddInteger(Guid);
8861 ID.AddInteger(Index);
8862 void *IP = nullptr;
8863 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
8864 return SDValue(E, 0);
8865
8866 auto *N = newSDNode<PseudoProbeSDNode>(
8867 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
8868 createOperands(N, Ops);
8869 CSEMap.InsertNode(N, IP);
8870 InsertNode(N);
8871 SDValue V(N, 0);
8872 NewSDValueDbgMsg(V, "Creating new node: ", this);
8873 return V;
8874}
8875
8876/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8877/// MachinePointerInfo record from it. This is particularly useful because the
8878/// code generator has many cases where it doesn't bother passing in a
8879/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8881 SelectionDAG &DAG, SDValue Ptr,
8882 int64_t Offset = 0) {
8883 // If this is FI+Offset, we can model it.
8884 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
8886 FI->getIndex(), Offset);
8887
8888 // If this is (FI+Offset1)+Offset2, we can model it.
8889 if (Ptr.getOpcode() != ISD::ADD ||
8890 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
8891 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
8892 return Info;
8893
8894 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
8896 DAG.getMachineFunction(), FI,
8897 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
8898}
8899
8900/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8901/// MachinePointerInfo record from it. This is particularly useful because the
8902/// code generator has many cases where it doesn't bother passing in a
8903/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8905 SelectionDAG &DAG, SDValue Ptr,
8906 SDValue OffsetOp) {
8907 // If the 'Offset' value isn't a constant, we can't handle this.
8908 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
8909 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
8910 if (OffsetOp.isUndef())
8911 return InferPointerInfo(Info, DAG, Ptr);
8912 return Info;
8913}
8914
8916 EVT VT, const SDLoc &dl, SDValue Chain,
8918 MachinePointerInfo PtrInfo, EVT MemVT,
8919 Align Alignment,
8920 MachineMemOperand::Flags MMOFlags,
8921 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8922 assert(Chain.getValueType() == MVT::Other &&
8923 "Invalid chain type");
8924
8925 MMOFlags |= MachineMemOperand::MOLoad;
8926 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8927 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8928 // clients.
8929 if (PtrInfo.V.isNull())
8930 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8931
8934 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8935 Alignment, AAInfo, Ranges);
8936 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
8937}
8938
8940 EVT VT, const SDLoc &dl, SDValue Chain,
8941 SDValue Ptr, SDValue Offset, EVT MemVT,
8942 MachineMemOperand *MMO) {
8943 if (VT == MemVT) {
8944 ExtType = ISD::NON_EXTLOAD;
8945 } else if (ExtType == ISD::NON_EXTLOAD) {
8946 assert(VT == MemVT && "Non-extending load from different memory type!");
8947 } else {
8948 // Extending load.
8949 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
8950 "Should only be an extending load, not truncating!");
8951 assert(VT.isInteger() == MemVT.isInteger() &&
8952 "Cannot convert from FP to Int or Int -> FP!");
8953 assert(VT.isVector() == MemVT.isVector() &&
8954 "Cannot use an ext load to convert to or from a vector!");
8955 assert((!VT.isVector() ||
8957 "Cannot use an ext load to change the number of vector elements!");
8958 }
8959
8960 bool Indexed = AM != ISD::UNINDEXED;
8961 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8962
8963 SDVTList VTs = Indexed ?
8964 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
8965 SDValue Ops[] = { Chain, Ptr, Offset };
8967 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
8968 ID.AddInteger(MemVT.getRawBits());
8969 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
8970 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
8971 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8972 ID.AddInteger(MMO->getFlags());
8973 void *IP = nullptr;
8974 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8975 cast<LoadSDNode>(E)->refineAlignment(MMO);
8976 return SDValue(E, 0);
8977 }
8978 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8979 ExtType, MemVT, MMO);
8980 createOperands(N, Ops);
8981
8982 CSEMap.InsertNode(N, IP);
8983 InsertNode(N);
8984 SDValue V(N, 0);
8985 NewSDValueDbgMsg(V, "Creating new node: ", this);
8986 return V;
8987}
8988
8991 MaybeAlign Alignment,
8992 MachineMemOperand::Flags MMOFlags,
8993 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8994 SDValue Undef = getUNDEF(Ptr.getValueType());
8995 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8996 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
8997}
8998
9001 SDValue Undef = getUNDEF(Ptr.getValueType());
9002 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9003 VT, MMO);
9004}
9005
9007 EVT VT, SDValue Chain, SDValue Ptr,
9008 MachinePointerInfo PtrInfo, EVT MemVT,
9009 MaybeAlign Alignment,
9010 MachineMemOperand::Flags MMOFlags,
9011 const AAMDNodes &AAInfo) {
9012 SDValue Undef = getUNDEF(Ptr.getValueType());
9013 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9014 MemVT, Alignment, MMOFlags, AAInfo);
9015}
9016
9018 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9019 MachineMemOperand *MMO) {
9020 SDValue Undef = getUNDEF(Ptr.getValueType());
9021 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9022 MemVT, MMO);
9023}
9024
9028 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9029 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9030 // Don't propagate the invariant or dereferenceable flags.
9031 auto MMOFlags =
9032 LD->getMemOperand()->getFlags() &
9034 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9035 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9036 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9037}
9038
9041 Align Alignment,
9042 MachineMemOperand::Flags MMOFlags,
9043 const AAMDNodes &AAInfo) {
9044 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9045
9046 MMOFlags |= MachineMemOperand::MOStore;
9047 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9048
9049 if (PtrInfo.V.isNull())
9050 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9051
9054 MachineMemOperand *MMO =
9055 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9056 return getStore(Chain, dl, Val, Ptr, MMO);
9057}
9058
9061 assert(Chain.getValueType() == MVT::Other &&
9062 "Invalid chain type");
9063 EVT VT = Val.getValueType();
9064 SDVTList VTs = getVTList(MVT::Other);
9065 SDValue Undef = getUNDEF(Ptr.getValueType());
9066 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9068 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9069 ID.AddInteger(VT.getRawBits());
9070 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9071 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
9072 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9073 ID.AddInteger(MMO->getFlags());
9074 void *IP = nullptr;
9075 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9076 cast<StoreSDNode>(E)->refineAlignment(MMO);
9077 return SDValue(E, 0);
9078 }
9079 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9080 ISD::UNINDEXED, false, VT, MMO);
9081 createOperands(N, Ops);
9082
9083 CSEMap.InsertNode(N, IP);
9084 InsertNode(N);
9085 SDValue V(N, 0);
9086 NewSDValueDbgMsg(V, "Creating new node: ", this);
9087 return V;
9088}
9089
9092 EVT SVT, Align Alignment,
9093 MachineMemOperand::Flags MMOFlags,
9094 const AAMDNodes &AAInfo) {
9095 assert(Chain.getValueType() == MVT::Other &&
9096 "Invalid chain type");
9097
9098 MMOFlags |= MachineMemOperand::MOStore;
9099 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9100
9101 if (PtrInfo.V.isNull())
9102 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9103
9106 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9107 AAInfo);
9108 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9109}
9110
9112 SDValue Ptr, EVT SVT,
9113 MachineMemOperand *MMO) {
9114 EVT VT = Val.getValueType();
9115
9116 assert(Chain.getValueType() == MVT::Other &&
9117 "Invalid chain type");
9118 if (VT == SVT)
9119 return getStore(Chain, dl, Val, Ptr, MMO);
9120
9122 "Should only be a truncating store, not extending!");
9123 assert(VT.isInteger() == SVT.isInteger() &&
9124 "Can't do FP-INT conversion!");
9125 assert(VT.isVector() == SVT.isVector() &&
9126 "Cannot use trunc store to convert to or from a vector!");
9127 assert((!VT.isVector() ||
9129 "Cannot use trunc store to change the number of vector elements!");
9130
9131 SDVTList VTs = getVTList(MVT::Other);
9132 SDValue Undef = getUNDEF(Ptr.getValueType());
9133 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9135 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9136 ID.AddInteger(SVT.getRawBits());
9137 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9138 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
9139 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9140 ID.AddInteger(MMO->getFlags());
9141 void *IP = nullptr;
9142 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9143 cast<StoreSDNode>(E)->refineAlignment(MMO);
9144 return SDValue(E, 0);
9145 }
9146 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9147 ISD::UNINDEXED, true, SVT, MMO);
9148 createOperands(N, Ops);
9149
9150 CSEMap.InsertNode(N, IP);
9151 InsertNode(N);
9152 SDValue V(N, 0);
9153 NewSDValueDbgMsg(V, "Creating new node: ", this);
9154 return V;
9155}
9156
9160 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9161 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9162 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9163 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
9165 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9166 ID.AddInteger(ST->getMemoryVT().getRawBits());
9167 ID.AddInteger(ST->getRawSubclassData());
9168 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9169 ID.AddInteger(ST->getMemOperand()->getFlags());
9170 void *IP = nullptr;
9171 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9172 return SDValue(E, 0);
9173
9174 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9175 ST->isTruncatingStore(), ST->getMemoryVT(),
9176 ST->getMemOperand());
9177 createOperands(N, Ops);
9178
9179 CSEMap.InsertNode(N, IP);
9180 InsertNode(N);
9181 SDValue V(N, 0);
9182 NewSDValueDbgMsg(V, "Creating new node: ", this);
9183 return V;
9184}
9185
9187 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9188 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9189 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9190 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9191 const MDNode *Ranges, bool IsExpanding) {
9192 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9193
9194 MMOFlags |= MachineMemOperand::MOLoad;
9195 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9196 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9197 // clients.
9198 if (PtrInfo.V.isNull())
9199 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9200
9203 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9204 Alignment, AAInfo, Ranges);
9205 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9206 MMO, IsExpanding);
9207}
9208
9210 ISD::LoadExtType ExtType, EVT VT,
9211 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9212 SDValue Offset, SDValue Mask, SDValue EVL,
9213 EVT MemVT, MachineMemOperand *MMO,
9214 bool IsExpanding) {
9215 bool Indexed = AM != ISD::UNINDEXED;
9216 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9217
9218 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9219 : getVTList(VT, MVT::Other);
9220 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9222 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9223 ID.AddInteger(MemVT.getRawBits());
9224 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9225 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9226 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9227 ID.AddInteger(MMO->getFlags());
9228 void *IP = nullptr;
9229 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9230 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
9231 return SDValue(E, 0);
9232 }
9233 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9234 ExtType, IsExpanding, MemVT, MMO);
9235 createOperands(N, Ops);
9236
9237 CSEMap.InsertNode(N, IP);
9238 InsertNode(N);
9239 SDValue V(N, 0);
9240 NewSDValueDbgMsg(V, "Creating new node: ", this);
9241 return V;
9242}
9243
9245 SDValue Ptr, SDValue Mask, SDValue EVL,
9246 MachinePointerInfo PtrInfo,
9247 MaybeAlign Alignment,
9248 MachineMemOperand::Flags MMOFlags,
9249 const AAMDNodes &AAInfo, const MDNode *Ranges,
9250 bool IsExpanding) {
9251 SDValue Undef = getUNDEF(Ptr.getValueType());
9252 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9253 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9254 IsExpanding);
9255}
9256
9258 SDValue Ptr, SDValue Mask, SDValue EVL,
9259 MachineMemOperand *MMO, bool IsExpanding) {
9260 SDValue Undef = getUNDEF(Ptr.getValueType());
9261 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9262 Mask, EVL, VT, MMO, IsExpanding);
9263}
9264
9266 EVT VT, SDValue Chain, SDValue Ptr,
9267 SDValue Mask, SDValue EVL,
9268 MachinePointerInfo PtrInfo, EVT MemVT,
9269 MaybeAlign Alignment,
9270 MachineMemOperand::Flags MMOFlags,
9271 const AAMDNodes &AAInfo, bool IsExpanding) {
9272 SDValue Undef = getUNDEF(Ptr.getValueType());
9273 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9274 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9275 IsExpanding);
9276}
9277
9279 EVT VT, SDValue Chain, SDValue Ptr,
9280 SDValue Mask, SDValue EVL, EVT MemVT,
9281 MachineMemOperand *MMO, bool IsExpanding) {
9282 SDValue Undef = getUNDEF(Ptr.getValueType());
9283 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9284 EVL, MemVT, MMO, IsExpanding);
9285}
9286
9290 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9291 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9292 // Don't propagate the invariant or dereferenceable flags.
9293 auto MMOFlags =
9294 LD->getMemOperand()->getFlags() &
9296 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9297 LD->getChain(), Base, Offset, LD->getMask(),
9298 LD->getVectorLength(), LD->getPointerInfo(),
9299 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
9300 nullptr, LD->isExpandingLoad());
9301}
9302
9305 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
9306 ISD::MemIndexedMode AM, bool IsTruncating,
9307 bool IsCompressing) {
9308 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9309 bool Indexed = AM != ISD::UNINDEXED;
9310 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9311 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9312 : getVTList(MVT::Other);
9313 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9315 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9316 ID.AddInteger(MemVT.getRawBits());
9317 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9318 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9319 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9320 ID.AddInteger(MMO->getFlags());
9321 void *IP = nullptr;
9322 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9323 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9324 return SDValue(E, 0);
9325 }
9326 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9327 IsTruncating, IsCompressing, MemVT, MMO);
9328 createOperands(N, Ops);
9329
9330 CSEMap.InsertNode(N, IP);
9331 InsertNode(N);
9332 SDValue V(N, 0);
9333 NewSDValueDbgMsg(V, "Creating new node: ", this);
9334 return V;
9335}
9336
9338 SDValue Val, SDValue Ptr, SDValue Mask,
9339 SDValue EVL, MachinePointerInfo PtrInfo,
9340 EVT SVT, Align Alignment,
9341 MachineMemOperand::Flags MMOFlags,
9342 const AAMDNodes &AAInfo,
9343 bool IsCompressing) {
9344 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9345
9346 MMOFlags |= MachineMemOperand::MOStore;
9347 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9348
9349 if (PtrInfo.V.isNull())
9350 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9351
9354 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9355 AAInfo);
9356 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9357 IsCompressing);
9358}
9359
9361 SDValue Val, SDValue Ptr, SDValue Mask,
9362 SDValue EVL, EVT SVT,
9363 MachineMemOperand *MMO,
9364 bool IsCompressing) {
9365 EVT VT = Val.getValueType();
9366
9367 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9368 if (VT == SVT)
9369 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9370 EVL, VT, MMO, ISD::UNINDEXED,
9371 /*IsTruncating*/ false, IsCompressing);
9372
9374 "Should only be a truncating store, not extending!");
9375 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9376 assert(VT.isVector() == SVT.isVector() &&
9377 "Cannot use trunc store to convert to or from a vector!");
9378 assert((!VT.isVector() ||
9380 "Cannot use trunc store to change the number of vector elements!");
9381
9382 SDVTList VTs = getVTList(MVT::Other);
9383 SDValue Undef = getUNDEF(Ptr.getValueType());
9384 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9386 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9387 ID.AddInteger(SVT.getRawBits());
9388 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9389 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9390 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9391 ID.AddInteger(MMO->getFlags());
9392 void *IP = nullptr;
9393 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9394 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9395 return SDValue(E, 0);
9396 }
9397 auto *N =
9398 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9399 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
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
9412 auto *ST = cast<VPStoreSDNode>(OrigStore);
9413 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9414 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9415 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9416 Offset, ST->getMask(), ST->getVectorLength()};
9418 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9419 ID.AddInteger(ST->getMemoryVT().getRawBits());
9420 ID.AddInteger(ST->getRawSubclassData());
9421 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9422 ID.AddInteger(ST->getMemOperand()->getFlags());
9423 void *IP = nullptr;
9424 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9425 return SDValue(E, 0);
9426
9427 auto *N = newSDNode<VPStoreSDNode>(
9428 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9429 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9430 createOperands(N, Ops);
9431
9432 CSEMap.InsertNode(N, IP);
9433 InsertNode(N);
9434 SDValue V(N, 0);
9435 NewSDValueDbgMsg(V, "Creating new node: ", this);
9436 return V;
9437}
9438
9440 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9441 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9442 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9443 bool Indexed = AM != ISD::UNINDEXED;
9444 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9445
9446 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9447 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9448 : getVTList(VT, MVT::Other);
9450 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9451 ID.AddInteger(VT.getRawBits());
9452 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9453 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9454 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9455
9456 void *IP = nullptr;
9457 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9458 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9459 return SDValue(E, 0);
9460 }
9461
9462 auto *N =
9463 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9464 ExtType, IsExpanding, MemVT, MMO);
9465 createOperands(N, Ops);
9466 CSEMap.InsertNode(N, IP);
9467 InsertNode(N);
9468 SDValue V(N, 0);
9469 NewSDValueDbgMsg(V, "Creating new node: ", this);
9470 return V;
9471}
9472
9474 SDValue Ptr, SDValue Stride,
9475 SDValue Mask, SDValue EVL,
9476 MachineMemOperand *MMO,
9477 bool IsExpanding) {
9478 SDValue Undef = getUNDEF(Ptr.getValueType());
9480 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9481}
9482
9484 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9485 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9486 MachineMemOperand *MMO, bool IsExpanding) {
9487 SDValue Undef = getUNDEF(Ptr.getValueType());
9488 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9489 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9490}
9491
9493 SDValue Val, SDValue Ptr,
9494 SDValue Offset, SDValue Stride,
9495 SDValue Mask, SDValue EVL, EVT MemVT,
9496 MachineMemOperand *MMO,
9498 bool IsTruncating, bool IsCompressing) {
9499 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9500 bool Indexed = AM != ISD::UNINDEXED;
9501 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9502 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9503 : getVTList(MVT::Other);
9504 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9506 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9507 ID.AddInteger(MemVT.getRawBits());
9508 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9509 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9510 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9511 void *IP = nullptr;
9512 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9513 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9514 return SDValue(E, 0);
9515 }
9516 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9517 VTs, AM, IsTruncating,
9518 IsCompressing, MemVT, MMO);
9519 createOperands(N, Ops);
9520
9521 CSEMap.InsertNode(N, IP);
9522 InsertNode(N);
9523 SDValue V(N, 0);
9524 NewSDValueDbgMsg(V, "Creating new node: ", this);
9525 return V;
9526}
9527
9529 SDValue Val, SDValue Ptr,
9530 SDValue Stride, SDValue Mask,
9531 SDValue EVL, EVT SVT,
9532 MachineMemOperand *MMO,
9533 bool IsCompressing) {
9534 EVT VT = Val.getValueType();
9535
9536 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9537 if (VT == SVT)
9538 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9539 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9540 /*IsTruncating*/ false, IsCompressing);
9541
9543 "Should only be a truncating store, not extending!");
9544 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9545 assert(VT.isVector() == SVT.isVector() &&
9546 "Cannot use trunc store to convert to or from a vector!");
9547 assert((!VT.isVector() ||
9549 "Cannot use trunc store to change the number of vector elements!");
9550
9551 SDVTList VTs = getVTList(MVT::Other);
9552 SDValue Undef = getUNDEF(Ptr.getValueType());
9553 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9555 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9556 ID.AddInteger(SVT.getRawBits());
9557 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9558 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9559 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9560 void *IP = nullptr;
9561 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9562 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9563 return SDValue(E, 0);
9564 }
9565 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9566 VTs, ISD::UNINDEXED, true,
9567 IsCompressing, SVT, MMO);
9568 createOperands(N, Ops);
9569
9570 CSEMap.InsertNode(N, IP);
9571 InsertNode(N);
9572 SDValue V(N, 0);
9573 NewSDValueDbgMsg(V, "Creating new node: ", this);
9574 return V;
9575}
9576
9579 ISD::MemIndexType IndexType) {
9580 assert(Ops.size() == 6 && "Incompatible number of operands");
9581
9583 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9584 ID.AddInteger(VT.getRawBits());
9585 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9586 dl.getIROrder(), VTs, VT, MMO, IndexType));
9587 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9588 ID.AddInteger(MMO->getFlags());
9589 void *IP = nullptr;
9590 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9591 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9592 return SDValue(E, 0);
9593 }
9594
9595 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9596 VT, MMO, IndexType);
9597 createOperands(N, Ops);
9598
9599 assert(N->getMask().getValueType().getVectorElementCount() ==
9600 N->getValueType(0).getVectorElementCount() &&
9601 "Vector width mismatch between mask and data");
9602 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9603 N->getValueType(0).getVectorElementCount().isScalable() &&
9604 "Scalable flags of index and data do not match");
9606 N->getIndex().getValueType().getVectorElementCount(),
9607 N->getValueType(0).getVectorElementCount()) &&
9608 "Vector width mismatch between index and data");
9609 assert(isa<ConstantSDNode>(N->getScale()) &&
9610 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9611 "Scale should be a constant power of 2");
9612
9613 CSEMap.InsertNode(N, IP);
9614 InsertNode(N);
9615 SDValue V(N, 0);
9616 NewSDValueDbgMsg(V, "Creating new node: ", this);
9617 return V;
9618}
9619
9622 MachineMemOperand *MMO,
9623 ISD::MemIndexType IndexType) {
9624 assert(Ops.size() == 7 && "Incompatible number of operands");
9625
9627 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9628 ID.AddInteger(VT.getRawBits());
9629 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9630 dl.getIROrder(), VTs, VT, MMO, IndexType));
9631 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9632 ID.AddInteger(MMO->getFlags());
9633 void *IP = nullptr;
9634 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9635 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9636 return SDValue(E, 0);
9637 }
9638 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9639 VT, MMO, IndexType);
9640 createOperands(N, Ops);
9641
9642 assert(N->getMask().getValueType().getVectorElementCount() ==
9643 N->getValue().getValueType().getVectorElementCount() &&
9644 "Vector width mismatch between mask and data");
9645 assert(
9646 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9647 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9648 "Scalable flags of index and data do not match");
9650 N->getIndex().getValueType().getVectorElementCount(),
9651 N->getValue().getValueType().getVectorElementCount()) &&
9652 "Vector width mismatch between index and data");
9653 assert(isa<ConstantSDNode>(N->getScale()) &&
9654 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9655 "Scale should be a constant power of 2");
9656
9657 CSEMap.InsertNode(N, IP);
9658 InsertNode(N);
9659 SDValue V(N, 0);
9660 NewSDValueDbgMsg(V, "Creating new node: ", this);
9661 return V;
9662}
9663
9666 SDValue PassThru, EVT MemVT,
9667 MachineMemOperand *MMO,
9669 ISD::LoadExtType ExtTy, bool isExpanding) {
9670 bool Indexed = AM != ISD::UNINDEXED;
9671 assert((Indexed || Offset.isUndef()) &&
9672 "Unindexed masked load with an offset!");
9673 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9674 : getVTList(VT, MVT::Other);
9675 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9677 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9678 ID.AddInteger(MemVT.getRawBits());
9679 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9680 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9681 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9682 ID.AddInteger(MMO->getFlags());
9683 void *IP = nullptr;
9684 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9685 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9686 return SDValue(E, 0);
9687 }
9688 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9689 AM, ExtTy, isExpanding, MemVT, MMO);
9690 createOperands(N, Ops);
9691
9692 CSEMap.InsertNode(N, IP);
9693 InsertNode(N);
9694 SDValue V(N, 0);
9695 NewSDValueDbgMsg(V, "Creating new node: ", this);
9696 return V;
9697}
9698
9702 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9703 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9704 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9705 Offset, LD->getMask(), LD->getPassThru(),
9706 LD->getMemoryVT(), LD->getMemOperand(), AM,
9707 LD->getExtensionType(), LD->isExpandingLoad());
9708}
9709
9712 SDValue Mask, EVT MemVT,
9713 MachineMemOperand *MMO,
9714 ISD::MemIndexedMode AM, bool IsTruncating,
9715 bool IsCompressing) {
9716 assert(Chain.getValueType() == MVT::Other &&
9717 "Invalid chain type");
9718 bool Indexed = AM != ISD::UNINDEXED;
9719 assert((Indexed || Offset.isUndef()) &&
9720 "Unindexed masked store with an offset!");
9721 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
9722 : getVTList(MVT::Other);
9723 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
9725 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
9726 ID.AddInteger(MemVT.getRawBits());
9727 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
9728 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9729 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9730 ID.AddInteger(MMO->getFlags());
9731 void *IP = nullptr;
9732 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9733 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
9734 return SDValue(E, 0);
9735 }
9736 auto *N =
9737 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9738 IsTruncating, IsCompressing, MemVT, MMO);
9739 createOperands(N, Ops);
9740
9741 CSEMap.InsertNode(N, IP);
9742 InsertNode(N);
9743 SDValue V(N, 0);
9744 NewSDValueDbgMsg(V, "Creating new node: ", this);
9745 return V;
9746}
9747
9751 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
9752 assert(ST->getOffset().isUndef() &&
9753 "Masked store is already a indexed store!");
9754 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9755 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
9756 AM, ST->isTruncatingStore(), ST->isCompressingStore());
9757}
9758
9761 MachineMemOperand *MMO,
9762 ISD::MemIndexType IndexType,
9763 ISD::LoadExtType ExtTy) {
9764 assert(Ops.size() == 6 && "Incompatible number of operands");
9765
9767 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
9768 ID.AddInteger(MemVT.getRawBits());
9769 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
9770 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
9771 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9772 ID.AddInteger(MMO->getFlags());
9773 void *IP = nullptr;
9774 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9775 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9776 return SDValue(E, 0);
9777 }
9778
9779 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9780 VTs, MemVT, MMO, IndexType, ExtTy);
9781 createOperands(N, Ops);
9782
9783 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
9784 "Incompatible type of the PassThru value in MaskedGatherSDNode");
9785 assert(N->getMask().getValueType().getVectorElementCount() ==
9786 N->getValueType(0).getVectorElementCount() &&
9787 "Vector width mismatch between mask and data");
9788 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9789 N->getValueType(0).getVectorElementCount().isScalable() &&
9790 "Scalable flags of index and data do not match");
9792 N->getIndex().getValueType().getVectorElementCount(),
9793 N->getValueType(0).getVectorElementCount()) &&
9794 "Vector width mismatch between index and data");
9795 assert(isa<ConstantSDNode>(N->getScale()) &&
9796 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9797 "Scale should be a constant power of 2");
9798
9799 CSEMap.InsertNode(N, IP);
9800 InsertNode(N);
9801 SDValue V(N, 0);
9802 NewSDValueDbgMsg(V, "Creating new node: ", this);
9803 return V;
9804}
9805
9808 MachineMemOperand *MMO,
9809 ISD::MemIndexType IndexType,
9810 bool IsTrunc) {
9811 assert(Ops.size() == 6 && "Incompatible number of operands");
9812
9814 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
9815 ID.AddInteger(MemVT.getRawBits());
9816 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
9817 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
9818 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9819 ID.AddInteger(MMO->getFlags());
9820 void *IP = nullptr;
9821 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9822 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
9823 return SDValue(E, 0);
9824 }
9825
9826 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9827 VTs, MemVT, MMO, IndexType, IsTrunc);
9828 createOperands(N, Ops);
9829
9830 assert(N->getMask().getValueType().getVectorElementCount() ==
9831 N->getValue().getValueType().getVectorElementCount() &&
9832 "Vector width mismatch between mask and data");
9833 assert(
9834 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9835 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9836 "Scalable flags of index and data do not match");
9838 N->getIndex().getValueType().getVectorElementCount(),
9839 N->getValue().getValueType().getVectorElementCount()) &&
9840 "Vector width mismatch between index and data");
9841 assert(isa<ConstantSDNode>(N->getScale()) &&
9842 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9843 "Scale should be a constant power of 2");
9844
9845 CSEMap.InsertNode(N, IP);
9846 InsertNode(N);
9847 SDValue V(N, 0);
9848 NewSDValueDbgMsg(V, "Creating new node: ", this);
9849 return V;
9850}
9851
9853 const SDLoc &dl, ArrayRef<SDValue> Ops,
9854 MachineMemOperand *MMO,
9855 ISD::MemIndexType IndexType) {
9856 assert(Ops.size() == 7 && "Incompatible number of operands");
9857
9860 ID.AddInteger(MemVT.getRawBits());
9861 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
9862 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
9863 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9864 ID.AddInteger(MMO->getFlags());
9865 void *IP = nullptr;
9866 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9867 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9868 return SDValue(E, 0);
9869 }
9870
9871 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9872 VTs, MemVT, MMO, IndexType);
9873 createOperands(N, Ops);
9874
9875 assert(N->getMask().getValueType().getVectorElementCount() ==
9876 N->getIndex().getValueType().getVectorElementCount() &&
9877 "Vector width mismatch between mask and data");
9878 assert(isa<ConstantSDNode>(N->getScale()) &&
9879 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9880 "Scale should be a constant power of 2");
9881 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
9882
9883 CSEMap.InsertNode(N, IP);
9884 InsertNode(N);
9885 SDValue V(N, 0);
9886 NewSDValueDbgMsg(V, "Creating new node: ", this);
9887 return V;
9888}
9889
9891 EVT MemVT, MachineMemOperand *MMO) {
9892 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9893 SDVTList VTs = getVTList(MVT::Other);
9894 SDValue Ops[] = {Chain, Ptr};
9897 ID.AddInteger(MemVT.getRawBits());
9898 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9899 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9900 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9901 ID.AddInteger(MMO->getFlags());
9902 void *IP = nullptr;
9903 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9904 return SDValue(E, 0);
9905
9906 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
9907 dl.getDebugLoc(), VTs, MemVT, MMO);
9908 createOperands(N, Ops);
9909
9910 CSEMap.InsertNode(N, IP);
9911 InsertNode(N);
9912 SDValue V(N, 0);
9913 NewSDValueDbgMsg(V, "Creating new node: ", this);
9914 return V;
9915}
9916
9918 EVT MemVT, MachineMemOperand *MMO) {
9919 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9920 SDVTList VTs = getVTList(MVT::Other);
9921 SDValue Ops[] = {Chain, Ptr};
9924 ID.AddInteger(MemVT.getRawBits());
9925 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9926 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9927 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9928 ID.AddInteger(MMO->getFlags());
9929 void *IP = nullptr;
9930 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9931 return SDValue(E, 0);
9932
9933 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
9934 dl.getDebugLoc(), VTs, MemVT, MMO);
9935 createOperands(N, Ops);
9936
9937 CSEMap.InsertNode(N, IP);
9938 InsertNode(N);
9939 SDValue V(N, 0);
9940 NewSDValueDbgMsg(V, "Creating new node: ", this);
9941 return V;
9942}
9943
9945 // select undef, T, F --> T (if T is a constant), otherwise F
9946 // select, ?, undef, F --> F
9947 // select, ?, T, undef --> T
9948 if (Cond.isUndef())
9949 return isConstantValueOfAnyType(T) ? T : F;
9950 if (T.isUndef())
9951 return F;
9952 if (F.isUndef())
9953 return T;
9954
9955 // select true, T, F --> T
9956 // select false, T, F --> F
9957 if (auto C = isBoolConstant(Cond, /*AllowTruncation=*/true))
9958 return *C ? T : F;
9959
9960 // select ?, T, T --> T
9961 if (T == F)
9962 return T;
9963
9964 return SDValue();
9965}
9966
9968 // shift undef, Y --> 0 (can always assume that the undef value is 0)
9969 if (X.isUndef())
9970 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
9971 // shift X, undef --> undef (because it may shift by the bitwidth)
9972 if (Y.isUndef())
9973 return getUNDEF(X.getValueType());
9974
9975 // shift 0, Y --> 0
9976 // shift X, 0 --> X
9978 return X;
9979
9980 // shift X, C >= bitwidth(X) --> undef
9981 // All vector elements must be too big (or undef) to avoid partial undefs.
9982 auto isShiftTooBig = [X](ConstantSDNode *Val) {
9983 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
9984 };
9985 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
9986 return getUNDEF(X.getValueType());
9987
9988 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
9989 if (X.getValueType().getScalarType() == MVT::i1)
9990 return X;
9991
9992 return SDValue();
9993}
9994
9996 SDNodeFlags Flags) {
9997 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
9998 // (an undef operand can be chosen to be Nan/Inf), then the result of this
9999 // operation is poison. That result can be relaxed to undef.
10000 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10001 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10002 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10003 (YC && YC->getValueAPF().isNaN());
10004 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10005 (YC && YC->getValueAPF().isInfinity());
10006
10007 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10008 return getUNDEF(X.getValueType());
10009
10010 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10011 return getUNDEF(X.getValueType());
10012
10013 if (!YC)
10014 return SDValue();
10015
10016 // X + -0.0 --> X
10017 if (Opcode == ISD::FADD)
10018 if (YC->getValueAPF().isNegZero())
10019 return X;
10020
10021 // X - +0.0 --> X
10022 if (Opcode == ISD::FSUB)
10023 if (YC->getValueAPF().isPosZero())
10024 return X;
10025
10026 // X * 1.0 --> X
10027 // X / 1.0 --> X
10028 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10029 if (YC->getValueAPF().isExactlyValue(1.0))
10030 return X;
10031
10032 // X * 0.0 --> 0.0
10033 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10034 if (YC->getValueAPF().isZero())
10035 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10036
10037 return SDValue();
10038}
10039
10041 SDValue Ptr, SDValue SV, unsigned Align) {
10042 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10043 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10044}
10045
10046SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10047 ArrayRef<SDUse> Ops) {
10048 switch (Ops.size()) {
10049 case 0: return getNode(Opcode, DL, VT);
10050 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
10051 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10052 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10053 default: break;
10054 }
10055
10056 // Copy from an SDUse array into an SDValue array for use with
10057 // the regular getNode logic.
10058 SmallVector<SDValue, 8> NewOps(Ops);
10059 return getNode(Opcode, DL, VT, NewOps);
10060}
10061
10062SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10063 ArrayRef<SDValue> Ops) {
10064 SDNodeFlags Flags;
10065 if (Inserter)
10066 Flags = Inserter->getFlags();
10067 return getNode(Opcode, DL, VT, Ops, Flags);
10068}
10069
10070SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10071 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10072 unsigned NumOps = Ops.size();
10073 switch (NumOps) {
10074 case 0: return getNode(Opcode, DL, VT);
10075 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10076 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10077 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10078 default: break;
10079 }
10080
10081#ifndef NDEBUG
10082 for (const auto &Op : Ops)
10083 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10084 "Operand is DELETED_NODE!");
10085#endif
10086
10087 switch (Opcode) {
10088 default: break;
10089 case ISD::BUILD_VECTOR:
10090 // Attempt to simplify BUILD_VECTOR.
10091 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10092 return V;
10093 break;
10095 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10096 return V;
10097 break;
10098 case ISD::SELECT_CC:
10099 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10100 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10101 "LHS and RHS of condition must have same type!");
10102 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10103 "True and False arms of SelectCC must have same type!");
10104 assert(Ops[2].getValueType() == VT &&
10105 "select_cc node must be of same type as true and false value!");
10106 assert((!Ops[0].getValueType().isVector() ||
10107 Ops[0].getValueType().getVectorElementCount() ==
10108 VT.getVectorElementCount()) &&
10109 "Expected select_cc with vector result to have the same sized "
10110 "comparison type!");
10111 break;
10112 case ISD::BR_CC:
10113 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10114 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10115 "LHS/RHS of comparison should match types!");
10116 break;
10117 case ISD::VP_ADD:
10118 case ISD::VP_SUB:
10119 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10120 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
10121 Opcode = ISD::VP_XOR;
10122 break;
10123 case ISD::VP_MUL:
10124 // If it is VP_MUL mask operation then turn it to VP_AND
10125 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
10126 Opcode = ISD::VP_AND;
10127 break;
10128 case ISD::VP_REDUCE_MUL:
10129 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10130 if (VT == MVT::i1)
10131 Opcode = ISD::VP_REDUCE_AND;
10132 break;
10133 case ISD::VP_REDUCE_ADD:
10134 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10135 if (VT == MVT::i1)
10136 Opcode = ISD::VP_REDUCE_XOR;
10137 break;
10138 case ISD::VP_REDUCE_SMAX:
10139 case ISD::VP_REDUCE_UMIN:
10140 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10141 // VP_REDUCE_AND.
10142 if (VT == MVT::i1)
10143 Opcode = ISD::VP_REDUCE_AND;
10144 break;
10145 case ISD::VP_REDUCE_SMIN:
10146 case ISD::VP_REDUCE_UMAX:
10147 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10148 // VP_REDUCE_OR.
10149 if (VT == MVT::i1)
10150 Opcode = ISD::VP_REDUCE_OR;
10151 break;
10152 }
10153
10154 // Memoize nodes.
10155 SDNode *N;
10156 SDVTList VTs = getVTList(VT);
10157
10158 if (VT != MVT::Glue) {
10160 AddNodeIDNode(ID, Opcode, VTs, Ops);
10161 void *IP = nullptr;
10162
10163 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10164 return SDValue(E, 0);
10165
10166 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10167 createOperands(N, Ops);
10168
10169 CSEMap.InsertNode(N, IP);
10170 } else {
10171 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10172 createOperands(N, Ops);
10173 }
10174
10175 N->setFlags(Flags);
10176 InsertNode(N);
10177 SDValue V(N, 0);
10178 NewSDValueDbgMsg(V, "Creating new node: ", this);
10179 return V;
10180}
10181
10182SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10183 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10184 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
10185}
10186
10187SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10188 ArrayRef<SDValue> Ops) {
10189 SDNodeFlags Flags;
10190 if (Inserter)
10191 Flags = Inserter->getFlags();
10192 return getNode(Opcode, DL, VTList, Ops, Flags);
10193}
10194
10195SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10196 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10197 if (VTList.NumVTs == 1)
10198 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10199
10200#ifndef NDEBUG
10201 for (const auto &Op : Ops)
10202 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10203 "Operand is DELETED_NODE!");
10204#endif
10205
10206 switch (Opcode) {
10207 case ISD::SADDO:
10208 case ISD::UADDO:
10209 case ISD::SSUBO:
10210 case ISD::USUBO: {
10211 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10212 "Invalid add/sub overflow op!");
10213 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10214 Ops[0].getValueType() == Ops[1].getValueType() &&
10215 Ops[0].getValueType() == VTList.VTs[0] &&
10216 "Binary operator types must match!");
10217 SDValue N1 = Ops[0], N2 = Ops[1];
10218 canonicalizeCommutativeBinop(Opcode, N1, N2);
10219
10220 // (X +- 0) -> X with zero-overflow.
10221 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10222 /*AllowTruncation*/ true);
10223 if (N2CV && N2CV->isZero()) {
10224 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10225 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10226 }
10227
10228 if (VTList.VTs[0].isVector() &&
10229 VTList.VTs[0].getVectorElementType() == MVT::i1 &&
10230 VTList.VTs[1].getVectorElementType() == MVT::i1) {
10231 SDValue F1 = getFreeze(N1);
10232 SDValue F2 = getFreeze(N2);
10233 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10234 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10235 return getNode(ISD::MERGE_VALUES, DL, VTList,
10236 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10237 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
10238 Flags);
10239 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
10240 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
10241 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
10242 return getNode(ISD::MERGE_VALUES, DL, VTList,
10243 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10244 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
10245 Flags);
10246 }
10247 }
10248 break;
10249 }
10250 case ISD::SADDO_CARRY:
10251 case ISD::UADDO_CARRY:
10252 case ISD::SSUBO_CARRY:
10253 case ISD::USUBO_CARRY:
10254 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
10255 "Invalid add/sub overflow op!");
10256 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10257 Ops[0].getValueType() == Ops[1].getValueType() &&
10258 Ops[0].getValueType() == VTList.VTs[0] &&
10259 Ops[2].getValueType() == VTList.VTs[1] &&
10260 "Binary operator types must match!");
10261 break;
10262 case ISD::SMUL_LOHI:
10263 case ISD::UMUL_LOHI: {
10264 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
10265 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
10266 VTList.VTs[0] == Ops[0].getValueType() &&
10267 VTList.VTs[0] == Ops[1].getValueType() &&
10268 "Binary operator types must match!");
10269 // Constant fold.
10270 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
10271 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
10272 if (LHS && RHS) {
10273 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
10274 unsigned OutWidth = Width * 2;
10275 APInt Val = LHS->getAPIntValue();
10276 APInt Mul = RHS->getAPIntValue();
10277 if (Opcode == ISD::SMUL_LOHI) {
10278 Val = Val.sext(OutWidth);
10279 Mul = Mul.sext(OutWidth);
10280 } else {
10281 Val = Val.zext(OutWidth);
10282 Mul = Mul.zext(OutWidth);
10283 }
10284 Val *= Mul;
10285
10286 SDValue Hi =
10287 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
10288 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
10289 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
10290 }
10291 break;
10292 }
10293 case ISD::FFREXP: {
10294 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
10295 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
10296 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
10297
10298 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
10299 int FrexpExp;
10300 APFloat FrexpMant =
10301 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
10302 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
10303 SDValue Result1 =
10304 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
10305 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
10306 }
10307
10308 break;
10309 }
10311 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10312 "Invalid STRICT_FP_EXTEND!");
10313 assert(VTList.VTs[0].isFloatingPoint() &&
10314 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
10315 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10316 "STRICT_FP_EXTEND result type should be vector iff the operand "
10317 "type is vector!");
10318 assert((!VTList.VTs[0].isVector() ||
10319 VTList.VTs[0].getVectorElementCount() ==
10320 Ops[1].getValueType().getVectorElementCount()) &&
10321 "Vector element count mismatch!");
10322 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
10323 "Invalid fpext node, dst <= src!");
10324 break;
10326 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
10327 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10328 "STRICT_FP_ROUND result type should be vector iff the operand "
10329 "type is vector!");
10330 assert((!VTList.VTs[0].isVector() ||
10331 VTList.VTs[0].getVectorElementCount() ==
10332 Ops[1].getValueType().getVectorElementCount()) &&
10333 "Vector element count mismatch!");
10334 assert(VTList.VTs[0].isFloatingPoint() &&
10335 Ops[1].getValueType().isFloatingPoint() &&
10336 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
10337 isa<ConstantSDNode>(Ops[2]) &&
10338 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10339 "Invalid STRICT_FP_ROUND!");
10340 break;
10341#if 0
10342 // FIXME: figure out how to safely handle things like
10343 // int foo(int x) { return 1 << (x & 255); }
10344 // int bar() { return foo(256); }
10345 case ISD::SRA_PARTS:
10346 case ISD::SRL_PARTS:
10347 case ISD::SHL_PARTS:
10348 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10349 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10350 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10351 else if (N3.getOpcode() == ISD::AND)
10352 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10353 // If the and is only masking out bits that cannot effect the shift,
10354 // eliminate the and.
10355 unsigned NumBits = VT.getScalarSizeInBits()*2;
10356 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10357 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10358 }
10359 break;
10360#endif
10361 }
10362
10363 // Memoize the node unless it returns a glue result.
10364 SDNode *N;
10365 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10367 AddNodeIDNode(ID, Opcode, VTList, Ops);
10368 void *IP = nullptr;
10369 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10370 return SDValue(E, 0);
10371
10372 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10373 createOperands(N, Ops);
10374 CSEMap.InsertNode(N, IP);
10375 } else {
10376 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10377 createOperands(N, Ops);
10378 }
10379
10380 N->setFlags(Flags);
10381 InsertNode(N);
10382 SDValue V(N, 0);
10383 NewSDValueDbgMsg(V, "Creating new node: ", this);
10384 return V;
10385}
10386
10387SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10388 SDVTList VTList) {
10389 return getNode(Opcode, DL, VTList, std::nullopt);
10390}
10391
10392SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10393 SDValue N1) {
10394 SDValue Ops[] = { N1 };
10395 return getNode(Opcode, DL, VTList, Ops);
10396}
10397
10398SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10399 SDValue N1, SDValue N2) {
10400 SDValue Ops[] = { N1, N2 };
10401 return getNode(Opcode, DL, VTList, Ops);
10402}
10403
10404SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10405 SDValue N1, SDValue N2, SDValue N3) {
10406 SDValue Ops[] = { N1, N2, N3 };
10407 return getNode(Opcode, DL, VTList, Ops);
10408}
10409
10410SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10411 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10412 SDValue Ops[] = { N1, N2, N3, N4 };
10413 return getNode(Opcode, DL, VTList, Ops);
10414}
10415
10416SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10417 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10418 SDValue N5) {
10419 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10420 return getNode(Opcode, DL, VTList, Ops);
10421}
10422
10424 return makeVTList(SDNode::getValueTypeList(VT), 1);
10425}
10426
10429 ID.AddInteger(2U);
10430 ID.AddInteger(VT1.getRawBits());
10431 ID.AddInteger(VT2.getRawBits());
10432
10433 void *IP = nullptr;
10434 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10435 if (!Result) {
10436 EVT *Array = Allocator.Allocate<EVT>(2);
10437 Array[0] = VT1;
10438 Array[1] = VT2;
10439 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10440 VTListMap.InsertNode(Result, IP);
10441 }
10442 return Result->getSDVTList();
10443}
10444
10447 ID.AddInteger(3U);
10448 ID.AddInteger(VT1.getRawBits());
10449 ID.AddInteger(VT2.getRawBits());
10450 ID.AddInteger(VT3.getRawBits());
10451
10452 void *IP = nullptr;
10453 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10454 if (!Result) {
10455 EVT *Array = Allocator.Allocate<EVT>(3);
10456 Array[0] = VT1;
10457 Array[1] = VT2;
10458 Array[2] = VT3;
10459 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10460 VTListMap.InsertNode(Result, IP);
10461 }
10462 return Result->getSDVTList();
10463}
10464
10467 ID.AddInteger(4U);
10468 ID.AddInteger(VT1.getRawBits());
10469 ID.AddInteger(VT2.getRawBits());
10470 ID.AddInteger(VT3.getRawBits());
10471 ID.AddInteger(VT4.getRawBits());
10472
10473 void *IP = nullptr;
10474 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10475 if (!Result) {
10476 EVT *Array = Allocator.Allocate<EVT>(4);
10477 Array[0] = VT1;
10478 Array[1] = VT2;
10479 Array[2] = VT3;
10480 Array[3] = VT4;
10481 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10482 VTListMap.InsertNode(Result, IP);
10483 }
10484 return Result->getSDVTList();
10485}
10486
10488 unsigned NumVTs = VTs.size();
10490 ID.AddInteger(NumVTs);
10491 for (unsigned index = 0; index < NumVTs; index++) {
10492 ID.AddInteger(VTs[index].getRawBits());
10493 }
10494
10495 void *IP = nullptr;
10496 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10497 if (!Result) {
10498 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10499 llvm::copy(VTs, Array);
10500 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10501 VTListMap.InsertNode(Result, IP);
10502 }
10503 return Result->getSDVTList();
10504}
10505
10506
10507/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10508/// specified operands. If the resultant node already exists in the DAG,
10509/// this does not modify the specified node, instead it returns the node that
10510/// already exists. If the resultant node does not exist in the DAG, the
10511/// input node is returned. As a degenerate case, if you specify the same
10512/// input operands as the node already has, the input node is returned.
10514 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10515
10516 // Check to see if there is no change.
10517 if (Op == N->getOperand(0)) return N;
10518
10519 // See if the modified node already exists.
10520 void *InsertPos = nullptr;
10521 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10522 return Existing;
10523
10524 // Nope it doesn't. Remove the node from its current place in the maps.
10525 if (InsertPos)
10526 if (!RemoveNodeFromCSEMaps(N))
10527 InsertPos = nullptr;
10528
10529 // Now we update the operands.
10530 N->OperandList[0].set(Op);
10531
10533 // If this gets put into a CSE map, add it.
10534 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10535 return N;
10536}
10537
10539 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10540
10541 // Check to see if there is no change.
10542 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10543 return N; // No operands changed, just return the input node.
10544
10545 // See if the modified node already exists.
10546 void *InsertPos = nullptr;
10547 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10548 return Existing;
10549
10550 // Nope it doesn't. Remove the node from its current place in the maps.
10551 if (InsertPos)
10552 if (!RemoveNodeFromCSEMaps(N))
10553 InsertPos = nullptr;
10554
10555 // Now we update the operands.
10556 if (N->OperandList[0] != Op1)
10557 N->OperandList[0].set(Op1);
10558 if (N->OperandList[1] != Op2)
10559 N->OperandList[1].set(Op2);
10560
10562 // If this gets put into a CSE map, add it.
10563 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10564 return N;
10565}
10566
10569 SDValue Ops[] = { Op1, Op2, Op3 };
10570 return UpdateNodeOperands(N, Ops);
10571}
10572
10575 SDValue Op3, SDValue Op4) {
10576 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10577 return UpdateNodeOperands(N, Ops);
10578}
10579
10582 SDValue Op3, SDValue Op4, SDValue Op5) {
10583 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10584 return UpdateNodeOperands(N, Ops);
10585}
10586
10589 unsigned NumOps = Ops.size();
10590 assert(N->getNumOperands() == NumOps &&
10591 "Update with wrong number of operands");
10592
10593 // If no operands changed just return the input node.
10594 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10595 return N;
10596
10597 // See if the modified node already exists.
10598 void *InsertPos = nullptr;
10599 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10600 return Existing;
10601
10602 // Nope it doesn't. Remove the node from its current place in the maps.
10603 if (InsertPos)
10604 if (!RemoveNodeFromCSEMaps(N))
10605 InsertPos = nullptr;
10606
10607 // Now we update the operands.
10608 for (unsigned i = 0; i != NumOps; ++i)
10609 if (N->OperandList[i] != Ops[i])
10610 N->OperandList[i].set(Ops[i]);
10611
10613 // If this gets put into a CSE map, add it.
10614 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10615 return N;
10616}
10617
10618/// DropOperands - Release the operands and set this node to have
10619/// zero operands.
10621 // Unlike the code in MorphNodeTo that does this, we don't need to
10622 // watch for dead nodes here.
10623 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10624 SDUse &Use = *I++;
10625 Use.set(SDValue());
10626 }
10627}
10628
10630 ArrayRef<MachineMemOperand *> NewMemRefs) {
10631 if (NewMemRefs.empty()) {
10632 N->clearMemRefs();
10633 return;
10634 }
10635
10636 // Check if we can avoid allocating by storing a single reference directly.
10637 if (NewMemRefs.size() == 1) {
10638 N->MemRefs = NewMemRefs[0];
10639 N->NumMemRefs = 1;
10640 return;
10641 }
10642
10643 MachineMemOperand **MemRefsBuffer =
10644 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10645 llvm::copy(NewMemRefs, MemRefsBuffer);
10646 N->MemRefs = MemRefsBuffer;
10647 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10648}
10649
10650/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10651/// machine opcode.
10652///
10654 EVT VT) {
10655 SDVTList VTs = getVTList(VT);
10656 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10657}
10658
10660 EVT VT, SDValue Op1) {
10661 SDVTList VTs = getVTList(VT);
10662 SDValue Ops[] = { Op1 };
10663 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10664}
10665
10667 EVT VT, SDValue Op1,
10668 SDValue Op2) {
10669 SDVTList VTs = getVTList(VT);
10670 SDValue Ops[] = { Op1, Op2 };
10671 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10672}
10673
10675 EVT VT, SDValue Op1,
10676 SDValue Op2, SDValue Op3) {
10677 SDVTList VTs = getVTList(VT);
10678 SDValue Ops[] = { Op1, Op2, Op3 };
10679 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10680}
10681
10683 EVT VT, ArrayRef<SDValue> Ops) {
10684 SDVTList VTs = getVTList(VT);
10685 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10686}
10687
10689 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10690 SDVTList VTs = getVTList(VT1, VT2);
10691 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10692}
10693
10695 EVT VT1, EVT VT2) {
10696 SDVTList VTs = getVTList(VT1, VT2);
10697 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10698}
10699
10701 EVT VT1, EVT VT2, EVT VT3,
10702 ArrayRef<SDValue> Ops) {
10703 SDVTList VTs = getVTList(VT1, VT2, VT3);
10704 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10705}
10706
10708 EVT VT1, EVT VT2,
10709 SDValue Op1, SDValue Op2) {
10710 SDVTList VTs = getVTList(VT1, VT2);
10711 SDValue Ops[] = { Op1, Op2 };
10712 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10713}
10714
10716 SDVTList VTs,ArrayRef<SDValue> Ops) {
10717 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
10718 // Reset the NodeID to -1.
10719 New->setNodeId(-1);
10720 if (New != N) {
10721 ReplaceAllUsesWith(N, New);
10723 }
10724 return New;
10725}
10726
10727/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
10728/// the line number information on the merged node since it is not possible to
10729/// preserve the information that operation is associated with multiple lines.
10730/// This will make the debugger working better at -O0, were there is a higher
10731/// probability having other instructions associated with that line.
10732///
10733/// For IROrder, we keep the smaller of the two
10734SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
10735 DebugLoc NLoc = N->getDebugLoc();
10736 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
10737 N->setDebugLoc(DebugLoc());
10738 }
10739 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
10740 N->setIROrder(Order);
10741 return N;
10742}
10743
10744/// MorphNodeTo - This *mutates* the specified node to have the specified
10745/// return type, opcode, and operands.
10746///
10747/// Note that MorphNodeTo returns the resultant node. If there is already a
10748/// node of the specified opcode and operands, it returns that node instead of
10749/// the current one. Note that the SDLoc need not be the same.
10750///
10751/// Using MorphNodeTo is faster than creating a new node and swapping it in
10752/// with ReplaceAllUsesWith both because it often avoids allocating a new
10753/// node, and because it doesn't require CSE recalculation for any of
10754/// the node's users.
10755///
10756/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
10757/// As a consequence it isn't appropriate to use from within the DAG combiner or
10758/// the legalizer which maintain worklists that would need to be updated when
10759/// deleting things.
10761 SDVTList VTs, ArrayRef<SDValue> Ops) {
10762 // If an identical node already exists, use it.
10763 void *IP = nullptr;
10764 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
10766 AddNodeIDNode(ID, Opc, VTs, Ops);
10767 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
10768 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
10769 }
10770
10771 if (!RemoveNodeFromCSEMaps(N))
10772 IP = nullptr;
10773
10774 // Start the morphing.
10775 N->NodeType = Opc;
10776 N->ValueList = VTs.VTs;
10777 N->NumValues = VTs.NumVTs;
10778
10779 // Clear the operands list, updating used nodes to remove this from their
10780 // use list. Keep track of any operands that become dead as a result.
10781 SmallPtrSet<SDNode*, 16> DeadNodeSet;
10782 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
10783 SDUse &Use = *I++;
10784 SDNode *Used = Use.getNode();
10785 Use.set(SDValue());
10786 if (Used->use_empty())
10787 DeadNodeSet.insert(Used);
10788 }
10789
10790 // For MachineNode, initialize the memory references information.
10791 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
10792 MN->clearMemRefs();
10793
10794 // Swap for an appropriately sized array from the recycler.
10795 removeOperands(N);
10796 createOperands(N, Ops);
10797
10798 // Delete any nodes that are still dead after adding the uses for the
10799 // new operands.
10800 if (!DeadNodeSet.empty()) {
10801 SmallVector<SDNode *, 16> DeadNodes;
10802 for (SDNode *N : DeadNodeSet)
10803 if (N->use_empty())
10804 DeadNodes.push_back(N);
10805 RemoveDeadNodes(DeadNodes);
10806 }
10807
10808 if (IP)
10809 CSEMap.InsertNode(N, IP); // Memoize the new node.
10810 return N;
10811}
10812
10814 unsigned OrigOpc = Node->getOpcode();
10815 unsigned NewOpc;
10816 switch (OrigOpc) {
10817 default:
10818 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
10819#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10820 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
10821#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10822 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
10823#include "llvm/IR/ConstrainedOps.def"
10824 }
10825
10826 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
10827
10828 // We're taking this node out of the chain, so we need to re-link things.
10829 SDValue InputChain = Node->getOperand(0);
10830 SDValue OutputChain = SDValue(Node, 1);
10831 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
10832
10834 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
10835 Ops.push_back(Node->getOperand(i));
10836
10837 SDVTList VTs = getVTList(Node->getValueType(0));
10838 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
10839
10840 // MorphNodeTo can operate in two ways: if an existing node with the
10841 // specified operands exists, it can just return it. Otherwise, it
10842 // updates the node in place to have the requested operands.
10843 if (Res == Node) {
10844 // If we updated the node in place, reset the node ID. To the isel,
10845 // this should be just like a newly allocated machine node.
10846 Res->setNodeId(-1);
10847 } else {
10848 ReplaceAllUsesWith(Node, Res);
10849 RemoveDeadNode(Node);
10850 }
10851
10852 return Res;
10853}
10854
10855/// getMachineNode - These are used for target selectors to create a new node
10856/// with specified return type(s), MachineInstr opcode, and operands.
10857///
10858/// Note that getMachineNode returns the resultant node. If there is already a
10859/// node of the specified opcode and operands, it returns that node instead of
10860/// the current one.
10862 EVT VT) {
10863 SDVTList VTs = getVTList(VT);
10864 return getMachineNode(Opcode, dl, VTs, std::nullopt);
10865}
10866
10868 EVT VT, SDValue Op1) {
10869 SDVTList VTs = getVTList(VT);
10870 SDValue Ops[] = { Op1 };
10871 return getMachineNode(Opcode, dl, VTs, Ops);
10872}
10873
10875 EVT VT, SDValue Op1, SDValue Op2) {
10876 SDVTList VTs = getVTList(VT);
10877 SDValue Ops[] = { Op1, Op2 };
10878 return getMachineNode(Opcode, dl, VTs, Ops);
10879}
10880
10882 EVT VT, SDValue Op1, SDValue Op2,
10883 SDValue Op3) {
10884 SDVTList VTs = getVTList(VT);
10885 SDValue Ops[] = { Op1, Op2, Op3 };
10886 return getMachineNode(Opcode, dl, VTs, Ops);
10887}
10888
10890 EVT VT, ArrayRef<SDValue> Ops) {
10891 SDVTList VTs = getVTList(VT);
10892 return getMachineNode(Opcode, dl, VTs, Ops);
10893}
10894
10896 EVT VT1, EVT VT2, SDValue Op1,
10897 SDValue Op2) {
10898 SDVTList VTs = getVTList(VT1, VT2);
10899 SDValue Ops[] = { Op1, Op2 };
10900 return getMachineNode(Opcode, dl, VTs, Ops);
10901}
10902
10904 EVT VT1, EVT VT2, SDValue Op1,
10905 SDValue Op2, SDValue Op3) {
10906 SDVTList VTs = getVTList(VT1, VT2);
10907 SDValue Ops[] = { Op1, Op2, Op3 };
10908 return getMachineNode(Opcode, dl, VTs, Ops);
10909}
10910
10912 EVT VT1, EVT VT2,
10913 ArrayRef<SDValue> Ops) {
10914 SDVTList VTs = getVTList(VT1, VT2);
10915 return getMachineNode(Opcode, dl, VTs, Ops);
10916}
10917
10919 EVT VT1, EVT VT2, EVT VT3,
10920 SDValue Op1, SDValue Op2) {
10921 SDVTList VTs = getVTList(VT1, VT2, VT3);
10922 SDValue Ops[] = { Op1, Op2 };
10923 return getMachineNode(Opcode, dl, VTs, Ops);
10924}
10925
10927 EVT VT1, EVT VT2, EVT VT3,
10928 SDValue Op1, SDValue Op2,
10929 SDValue Op3) {
10930 SDVTList VTs = getVTList(VT1, VT2, VT3);
10931 SDValue Ops[] = { Op1, Op2, Op3 };
10932 return getMachineNode(Opcode, dl, VTs, Ops);
10933}
10934
10936 EVT VT1, EVT VT2, EVT VT3,
10937 ArrayRef<SDValue> Ops) {
10938 SDVTList VTs = getVTList(VT1, VT2, VT3);
10939 return getMachineNode(Opcode, dl, VTs, Ops);
10940}
10941
10943 ArrayRef<EVT> ResultTys,
10944 ArrayRef<SDValue> Ops) {
10945 SDVTList VTs = getVTList(ResultTys);
10946 return getMachineNode(Opcode, dl, VTs, Ops);
10947}
10948
10950 SDVTList VTs,
10951 ArrayRef<SDValue> Ops) {
10952 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
10954 void *IP = nullptr;
10955
10956 if (DoCSE) {
10958 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
10959 IP = nullptr;
10960 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10961 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
10962 }
10963 }
10964
10965 // Allocate a new MachineSDNode.
10966 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10967 createOperands(N, Ops);
10968
10969 if (DoCSE)
10970 CSEMap.InsertNode(N, IP);
10971
10972 InsertNode(N);
10973 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
10974 return N;
10975}
10976
10977/// getTargetExtractSubreg - A convenience function for creating
10978/// TargetOpcode::EXTRACT_SUBREG nodes.
10980 SDValue Operand) {
10981 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10982 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
10983 VT, Operand, SRIdxVal);
10984 return SDValue(Subreg, 0);
10985}
10986
10987/// getTargetInsertSubreg - A convenience function for creating
10988/// TargetOpcode::INSERT_SUBREG nodes.
10990 SDValue Operand, SDValue Subreg) {
10991 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10992 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
10993 VT, Operand, Subreg, SRIdxVal);
10994 return SDValue(Result, 0);
10995}
10996
10997/// getNodeIfExists - Get the specified node if it's already available, or
10998/// else return NULL.
11000 ArrayRef<SDValue> Ops) {
11001 SDNodeFlags Flags;
11002 if (Inserter)
11003 Flags = Inserter->getFlags();
11004 return getNodeIfExists(Opcode, VTList, Ops, Flags);
11005}
11006
11009 const SDNodeFlags Flags) {
11010 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11012 AddNodeIDNode(ID, Opcode, VTList, Ops);
11013 void *IP = nullptr;
11014 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
11015 E->intersectFlagsWith(Flags);
11016 return E;
11017 }
11018 }
11019 return nullptr;
11020}
11021
11022/// doesNodeExist - Check if a node exists without modifying its flags.
11023bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11024 ArrayRef<SDValue> Ops) {
11025 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11027 AddNodeIDNode(ID, Opcode, VTList, Ops);
11028 void *IP = nullptr;
11029 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11030 return true;
11031 }
11032 return false;
11033}
11034
11035/// getDbgValue - Creates a SDDbgValue node.
11036///
11037/// SDNode
11039 SDNode *N, unsigned R, bool IsIndirect,
11040 const DebugLoc &DL, unsigned O) {
11041 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11042 "Expected inlined-at fields to agree");
11043 return new (DbgInfo->getAlloc())
11044 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11045 {}, IsIndirect, DL, O,
11046 /*IsVariadic=*/false);
11047}
11048
11049/// Constant
11051 DIExpression *Expr,
11052 const Value *C,
11053 const DebugLoc &DL, unsigned O) {
11054 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11055 "Expected inlined-at fields to agree");
11056 return new (DbgInfo->getAlloc())
11057 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11058 /*IsIndirect=*/false, DL, O,
11059 /*IsVariadic=*/false);
11060}
11061
11062/// FrameIndex
11064 DIExpression *Expr, unsigned FI,
11065 bool IsIndirect,
11066 const DebugLoc &DL,
11067 unsigned O) {
11068 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11069 "Expected inlined-at fields to agree");
11070 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11071}
11072
11073/// FrameIndex with dependencies
11075 DIExpression *Expr, unsigned FI,
11076 ArrayRef<SDNode *> Dependencies,
11077 bool IsIndirect,
11078 const DebugLoc &DL,
11079 unsigned O) {
11080 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11081 "Expected inlined-at fields to agree");
11082 return new (DbgInfo->getAlloc())
11083 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11084 Dependencies, IsIndirect, DL, O,
11085 /*IsVariadic=*/false);
11086}
11087
11088/// VReg
11090 unsigned VReg, bool IsIndirect,
11091 const DebugLoc &DL, unsigned O) {
11092 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11093 "Expected inlined-at fields to agree");
11094 return new (DbgInfo->getAlloc())
11095 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11096 {}, IsIndirect, DL, O,
11097 /*IsVariadic=*/false);
11098}
11099
11102 ArrayRef<SDNode *> Dependencies,
11103 bool IsIndirect, const DebugLoc &DL,
11104 unsigned O, bool IsVariadic) {
11105 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11106 "Expected inlined-at fields to agree");
11107 return new (DbgInfo->getAlloc())
11108 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11109 DL, O, IsVariadic);
11110}
11111
11113 unsigned OffsetInBits, unsigned SizeInBits,
11114 bool InvalidateDbg) {
11115 SDNode *FromNode = From.getNode();
11116 SDNode *ToNode = To.getNode();
11117 assert(FromNode && ToNode && "Can't modify dbg values");
11118
11119 // PR35338
11120 // TODO: assert(From != To && "Redundant dbg value transfer");
11121 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11122 if (From == To || FromNode == ToNode)
11123 return;
11124
11125 if (!FromNode->getHasDebugValue())
11126 return;
11127
11128 SDDbgOperand FromLocOp =
11129 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11131
11133 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11134 if (Dbg->isInvalidated())
11135 continue;
11136
11137 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11138
11139 // Create a new location ops vector that is equal to the old vector, but
11140 // with each instance of FromLocOp replaced with ToLocOp.
11141 bool Changed = false;
11142 auto NewLocOps = Dbg->copyLocationOps();
11143 std::replace_if(
11144 NewLocOps.begin(), NewLocOps.end(),
11145 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11146 bool Match = Op == FromLocOp;
11147 Changed |= Match;
11148 return Match;
11149 },
11150 ToLocOp);
11151 // Ignore this SDDbgValue if we didn't find a matching location.
11152 if (!Changed)
11153 continue;
11154
11155 DIVariable *Var = Dbg->getVariable();
11156 auto *Expr = Dbg->getExpression();
11157 // If a fragment is requested, update the expression.
11158 if (SizeInBits) {
11159 // When splitting a larger (e.g., sign-extended) value whose
11160 // lower bits are described with an SDDbgValue, do not attempt
11161 // to transfer the SDDbgValue to the upper bits.
11162 if (auto FI = Expr->getFragmentInfo())
11163 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11164 continue;
11165 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11166 SizeInBits);
11167 if (!Fragment)
11168 continue;
11169 Expr = *Fragment;
11170 }
11171
11172 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11173 // Clone the SDDbgValue and move it to To.
11174 SDDbgValue *Clone = getDbgValueList(
11175 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11176 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11177 Dbg->isVariadic());
11178 ClonedDVs.push_back(Clone);
11179
11180 if (InvalidateDbg) {
11181 // Invalidate value and indicate the SDDbgValue should not be emitted.
11182 Dbg->setIsInvalidated();
11183 Dbg->setIsEmitted();
11184 }
11185 }
11186
11187 for (SDDbgValue *Dbg : ClonedDVs) {
11188 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11189 "Transferred DbgValues should depend on the new SDNode");
11190 AddDbgValue(Dbg, false);
11191 }
11192}
11193
11195 if (!N.getHasDebugValue())
11196 return;
11197
11199 for (auto *DV : GetDbgValues(&N)) {
11200 if (DV->isInvalidated())
11201 continue;
11202 switch (N.getOpcode()) {
11203 default:
11204 break;
11205 case ISD::ADD: {
11206 SDValue N0 = N.getOperand(0);
11207 SDValue N1 = N.getOperand(1);
11208 if (!isa<ConstantSDNode>(N0)) {
11209 bool RHSConstant = isa<ConstantSDNode>(N1);
11211 if (RHSConstant)
11212 Offset = N.getConstantOperandVal(1);
11213 // We are not allowed to turn indirect debug values variadic, so
11214 // don't salvage those.
11215 if (!RHSConstant && DV->isIndirect())
11216 continue;
11217
11218 // Rewrite an ADD constant node into a DIExpression. Since we are
11219 // performing arithmetic to compute the variable's *value* in the
11220 // DIExpression, we need to mark the expression with a
11221 // DW_OP_stack_value.
11222 auto *DIExpr = DV->getExpression();
11223 auto NewLocOps = DV->copyLocationOps();
11224 bool Changed = false;
11225 size_t OrigLocOpsSize = NewLocOps.size();
11226 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11227 // We're not given a ResNo to compare against because the whole
11228 // node is going away. We know that any ISD::ADD only has one
11229 // result, so we can assume any node match is using the result.
11230 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11231 NewLocOps[i].getSDNode() != &N)
11232 continue;
11233 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11234 if (RHSConstant) {
11237 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
11238 } else {
11239 // Convert to a variadic expression (if not already).
11240 // convertToVariadicExpression() returns a const pointer, so we use
11241 // a temporary const variable here.
11242 const auto *TmpDIExpr =
11246 ExprOps.push_back(NewLocOps.size());
11247 ExprOps.push_back(dwarf::DW_OP_plus);
11250 NewLocOps.push_back(RHS);
11251 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
11252 }
11253 Changed = true;
11254 }
11255 (void)Changed;
11256 assert(Changed && "Salvage target doesn't use N");
11257
11258 bool IsVariadic =
11259 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
11260
11261 auto AdditionalDependencies = DV->getAdditionalDependencies();
11262 SDDbgValue *Clone = getDbgValueList(
11263 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
11264 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
11265 ClonedDVs.push_back(Clone);
11266 DV->setIsInvalidated();
11267 DV->setIsEmitted();
11268 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
11269 N0.getNode()->dumprFull(this);
11270 dbgs() << " into " << *DIExpr << '\n');
11271 }
11272 break;
11273 }
11274 case ISD::TRUNCATE: {
11275 SDValue N0 = N.getOperand(0);
11276 TypeSize FromSize = N0.getValueSizeInBits();
11277 TypeSize ToSize = N.getValueSizeInBits(0);
11278
11279 DIExpression *DbgExpression = DV->getExpression();
11280 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
11281 auto NewLocOps = DV->copyLocationOps();
11282 bool Changed = false;
11283 for (size_t i = 0; i < NewLocOps.size(); ++i) {
11284 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11285 NewLocOps[i].getSDNode() != &N)
11286 continue;
11287
11288 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11289 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
11290 Changed = true;
11291 }
11292 assert(Changed && "Salvage target doesn't use N");
11293 (void)Changed;
11294
11295 SDDbgValue *Clone =
11296 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
11297 DV->getAdditionalDependencies(), DV->isIndirect(),
11298 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
11299
11300 ClonedDVs.push_back(Clone);
11301 DV->setIsInvalidated();
11302 DV->setIsEmitted();
11303 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
11304 dbgs() << " into " << *DbgExpression << '\n');
11305 break;
11306 }
11307 }
11308 }
11309
11310 for (SDDbgValue *Dbg : ClonedDVs) {
11311 assert(!Dbg->getSDNodes().empty() &&
11312 "Salvaged DbgValue should depend on a new SDNode");
11313 AddDbgValue(Dbg, false);
11314 }
11315}
11316
11317/// Creates a SDDbgLabel node.
11319 const DebugLoc &DL, unsigned O) {
11320 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
11321 "Expected inlined-at fields to agree");
11322 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
11323}
11324
11325namespace {
11326
11327/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
11328/// pointed to by a use iterator is deleted, increment the use iterator
11329/// so that it doesn't dangle.
11330///
11331class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
11334
11335 void NodeDeleted(SDNode *N, SDNode *E) override {
11336 // Increment the iterator as needed.
11337 while (UI != UE && N == *UI)
11338 ++UI;
11339 }
11340
11341public:
11342 RAUWUpdateListener(SelectionDAG &d,
11345 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11346};
11347
11348} // end anonymous namespace
11349
11350/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11351/// This can cause recursive merging of nodes in the DAG.
11352///
11353/// This version assumes From has a single result value.
11354///
11356 SDNode *From = FromN.getNode();
11357 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11358 "Cannot replace with this method!");
11359 assert(From != To.getNode() && "Cannot replace uses of with self");
11360
11361 // Preserve Debug Values
11362 transferDbgValues(FromN, To);
11363 // Preserve extra info.
11364 copyExtraInfo(From, To.getNode());
11365
11366 // Iterate over all the existing uses of From. New uses will be added
11367 // to the beginning of the use list, which we avoid visiting.
11368 // This specifically avoids visiting uses of From that arise while the
11369 // replacement is happening, because any such uses would be the result
11370 // of CSE: If an existing node looks like From after one of its operands
11371 // is replaced by To, we don't want to replace of all its users with To
11372 // too. See PR3018 for more info.
11373 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11374 RAUWUpdateListener Listener(*this, UI, UE);
11375 while (UI != UE) {
11376 SDNode *User = *UI;
11377
11378 // This node is about to morph, remove its old self from the CSE maps.
11379 RemoveNodeFromCSEMaps(User);
11380
11381 // A user can appear in a use list multiple times, and when this
11382 // happens the uses are usually next to each other in the list.
11383 // To help reduce the number of CSE recomputations, process all
11384 // the uses of this user that we can find this way.
11385 do {
11386 SDUse &Use = UI.getUse();
11387 ++UI;
11388 Use.set(To);
11389 if (To->isDivergent() != From->isDivergent())
11391 } while (UI != UE && *UI == User);
11392 // Now that we have modified User, add it back to the CSE maps. If it
11393 // already exists there, recursively merge the results together.
11394 AddModifiedNodeToCSEMaps(User);
11395 }
11396
11397 // If we just RAUW'd the root, take note.
11398 if (FromN == getRoot())
11399 setRoot(To);
11400}
11401
11402/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11403/// This can cause recursive merging of nodes in the DAG.
11404///
11405/// This version assumes that for each value of From, there is a
11406/// corresponding value in To in the same position with the same type.
11407///
11409#ifndef NDEBUG
11410 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11411 assert((!From->hasAnyUseOfValue(i) ||
11412 From->getValueType(i) == To->getValueType(i)) &&
11413 "Cannot use this version of ReplaceAllUsesWith!");
11414#endif
11415
11416 // Handle the trivial case.
11417 if (From == To)
11418 return;
11419
11420 // Preserve Debug Info. Only do this if there's a use.
11421 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11422 if (From->hasAnyUseOfValue(i)) {
11423 assert((i < To->getNumValues()) && "Invalid To location");
11425 }
11426 // Preserve extra info.
11427 copyExtraInfo(From, To);
11428
11429 // Iterate over just the existing users of From. See the comments in
11430 // the ReplaceAllUsesWith above.
11431 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11432 RAUWUpdateListener Listener(*this, UI, UE);
11433 while (UI != UE) {
11434 SDNode *User = *UI;
11435
11436 // This node is about to morph, remove its old self from the CSE maps.
11437 RemoveNodeFromCSEMaps(User);
11438
11439 // A user can appear in a use list multiple times, and when this
11440 // happens the uses are usually next to each other in the list.
11441 // To help reduce the number of CSE recomputations, process all
11442 // the uses of this user that we can find this way.
11443 do {
11444 SDUse &Use = UI.getUse();
11445 ++UI;
11446 Use.setNode(To);
11447 if (To->isDivergent() != From->isDivergent())
11449 } while (UI != UE && *UI == User);
11450
11451 // Now that we have modified User, add it back to the CSE maps. If it
11452 // already exists there, recursively merge the results together.
11453 AddModifiedNodeToCSEMaps(User);
11454 }
11455
11456 // If we just RAUW'd the root, take note.
11457 if (From == getRoot().getNode())
11458 setRoot(SDValue(To, getRoot().getResNo()));
11459}
11460
11461/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11462/// This can cause recursive merging of nodes in the DAG.
11463///
11464/// This version can replace From with any result values. To must match the
11465/// number and types of values returned by From.
11467 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11468 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11469
11470 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11471 // Preserve Debug Info.
11472 transferDbgValues(SDValue(From, i), To[i]);
11473 // Preserve extra info.
11474 copyExtraInfo(From, To[i].getNode());
11475 }
11476
11477 // Iterate over just the existing users of From. See the comments in
11478 // the ReplaceAllUsesWith above.
11479 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11480 RAUWUpdateListener Listener(*this, UI, UE);
11481 while (UI != UE) {
11482 SDNode *User = *UI;
11483
11484 // This node is about to morph, remove its old self from the CSE maps.
11485 RemoveNodeFromCSEMaps(User);
11486
11487 // A user can appear in a use list multiple times, and when this happens the
11488 // uses are usually next to each other in the list. To help reduce the
11489 // number of CSE and divergence recomputations, process all the uses of this
11490 // user that we can find this way.
11491 bool To_IsDivergent = false;
11492 do {
11493 SDUse &Use = UI.getUse();
11494 const SDValue &ToOp = To[Use.getResNo()];
11495 ++UI;
11496 Use.set(ToOp);
11497 To_IsDivergent |= ToOp->isDivergent();
11498 } while (UI != UE && *UI == User);
11499
11500 if (To_IsDivergent != From->isDivergent())
11502
11503 // Now that we have modified User, add it back to the CSE maps. If it
11504 // already exists there, recursively merge the results together.
11505 AddModifiedNodeToCSEMaps(User);
11506 }
11507
11508 // If we just RAUW'd the root, take note.
11509 if (From == getRoot().getNode())
11510 setRoot(SDValue(To[getRoot().getResNo()]));
11511}
11512
11513/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11514/// uses of other values produced by From.getNode() alone. The Deleted
11515/// vector is handled the same way as for ReplaceAllUsesWith.
11517 // Handle the really simple, really trivial case efficiently.
11518 if (From == To) return;
11519
11520 // Handle the simple, trivial, case efficiently.
11521 if (From.getNode()->getNumValues() == 1) {
11523 return;
11524 }
11525
11526 // Preserve Debug Info.
11528 copyExtraInfo(From.getNode(), To.getNode());
11529
11530 // Iterate over just the existing users of From. See the comments in
11531 // the ReplaceAllUsesWith above.
11532 SDNode::use_iterator UI = From.getNode()->use_begin(),
11533 UE = From.getNode()->use_end();
11534 RAUWUpdateListener Listener(*this, UI, UE);
11535 while (UI != UE) {
11536 SDNode *User = *UI;
11537 bool UserRemovedFromCSEMaps = false;
11538
11539 // A user can appear in a use list multiple times, and when this
11540 // happens the uses are usually next to each other in the list.
11541 // To help reduce the number of CSE recomputations, process all
11542 // the uses of this user that we can find this way.
11543 do {
11544 SDUse &Use = UI.getUse();
11545
11546 // Skip uses of different values from the same node.
11547 if (Use.getResNo() != From.getResNo()) {
11548 ++UI;
11549 continue;
11550 }
11551
11552 // If this node hasn't been modified yet, it's still in the CSE maps,
11553 // so remove its old self from the CSE maps.
11554 if (!UserRemovedFromCSEMaps) {
11555 RemoveNodeFromCSEMaps(User);
11556 UserRemovedFromCSEMaps = true;
11557 }
11558
11559 ++UI;
11560 Use.set(To);
11561 if (To->isDivergent() != From->isDivergent())
11563 } while (UI != UE && *UI == User);
11564 // We are iterating over all uses of the From node, so if a use
11565 // doesn't use the specific value, no changes are made.
11566 if (!UserRemovedFromCSEMaps)
11567 continue;
11568
11569 // Now that we have modified User, add it back to the CSE maps. If it
11570 // already exists there, recursively merge the results together.
11571 AddModifiedNodeToCSEMaps(User);
11572 }
11573
11574 // If we just RAUW'd the root, take note.
11575 if (From == getRoot())
11576 setRoot(To);
11577}
11578
11579namespace {
11580
11581/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11582/// to record information about a use.
11583struct UseMemo {
11584 SDNode *User;
11585 unsigned Index;
11586 SDUse *Use;
11587};
11588
11589/// operator< - Sort Memos by User.
11590bool operator<(const UseMemo &L, const UseMemo &R) {
11591 return (intptr_t)L.User < (intptr_t)R.User;
11592}
11593
11594/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11595/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11596/// the node already has been taken care of recursively.
11597class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11599
11600 void NodeDeleted(SDNode *N, SDNode *E) override {
11601 for (UseMemo &Memo : Uses)
11602 if (Memo.User == N)
11603 Memo.User = nullptr;
11604 }
11605
11606public:
11607 RAUOVWUpdateListener(SelectionDAG &d, SmallVector<UseMemo, 4> &uses)
11608 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11609};
11610
11611} // end anonymous namespace
11612
11613/// Return true if a glue output should propagate divergence information.
11615 switch (Node->getOpcode()) {
11616 case ISD::CopyFromReg:
11617 case ISD::CopyToReg:
11618 return false;
11619 default:
11620 return true;
11621 }
11622
11623 llvm_unreachable("covered opcode switch");
11624}
11625
11627 if (TLI->isSDNodeAlwaysUniform(N)) {
11628 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11629 "Conflicting divergence information!");
11630 return false;
11631 }
11632 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11633 return true;
11634 for (const auto &Op : N->ops()) {
11635 EVT VT = Op.getValueType();
11636
11637 // Skip Chain. It does not carry divergence.
11638 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
11639 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
11640 return true;
11641 }
11642 return false;
11643}
11644
11646 SmallVector<SDNode *, 16> Worklist(1, N);
11647 do {
11648 N = Worklist.pop_back_val();
11649 bool IsDivergent = calculateDivergence(N);
11650 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11651 N->SDNodeBits.IsDivergent = IsDivergent;
11652 llvm::append_range(Worklist, N->uses());
11653 }
11654 } while (!Worklist.empty());
11655}
11656
11657void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11659 Order.reserve(AllNodes.size());
11660 for (auto &N : allnodes()) {
11661 unsigned NOps = N.getNumOperands();
11662 Degree[&N] = NOps;
11663 if (0 == NOps)
11664 Order.push_back(&N);
11665 }
11666 for (size_t I = 0; I != Order.size(); ++I) {
11667 SDNode *N = Order[I];
11668 for (auto *U : N->uses()) {
11669 unsigned &UnsortedOps = Degree[U];
11670 if (0 == --UnsortedOps)
11671 Order.push_back(U);
11672 }
11673 }
11674}
11675
11676#ifndef NDEBUG
11678 std::vector<SDNode *> TopoOrder;
11679 CreateTopologicalOrder(TopoOrder);
11680 for (auto *N : TopoOrder) {
11681 assert(calculateDivergence(N) == N->isDivergent() &&
11682 "Divergence bit inconsistency detected");
11683 }
11684}
11685#endif
11686
11687/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11688/// uses of other values produced by From.getNode() alone. The same value
11689/// may appear in both the From and To list. The Deleted vector is
11690/// handled the same way as for ReplaceAllUsesWith.
11692 const SDValue *To,
11693 unsigned Num){
11694 // Handle the simple, trivial case efficiently.
11695 if (Num == 1)
11696 return ReplaceAllUsesOfValueWith(*From, *To);
11697
11698 transferDbgValues(*From, *To);
11699 copyExtraInfo(From->getNode(), To->getNode());
11700
11701 // Read up all the uses and make records of them. This helps
11702 // processing new uses that are introduced during the
11703 // replacement process.
11705 for (unsigned i = 0; i != Num; ++i) {
11706 unsigned FromResNo = From[i].getResNo();
11707 SDNode *FromNode = From[i].getNode();
11708 for (SDNode::use_iterator UI = FromNode->use_begin(),
11709 E = FromNode->use_end(); UI != E; ++UI) {
11710 SDUse &Use = UI.getUse();
11711 if (Use.getResNo() == FromResNo) {
11712 UseMemo Memo = { *UI, i, &Use };
11713 Uses.push_back(Memo);
11714 }
11715 }
11716 }
11717
11718 // Sort the uses, so that all the uses from a given User are together.
11720 RAUOVWUpdateListener Listener(*this, Uses);
11721
11722 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
11723 UseIndex != UseIndexEnd; ) {
11724 // We know that this user uses some value of From. If it is the right
11725 // value, update it.
11726 SDNode *User = Uses[UseIndex].User;
11727 // If the node has been deleted by recursive CSE updates when updating
11728 // another node, then just skip this entry.
11729 if (User == nullptr) {
11730 ++UseIndex;
11731 continue;
11732 }
11733
11734 // This node is about to morph, remove its old self from the CSE maps.
11735 RemoveNodeFromCSEMaps(User);
11736
11737 // The Uses array is sorted, so all the uses for a given User
11738 // are next to each other in the list.
11739 // To help reduce the number of CSE recomputations, process all
11740 // the uses of this user that we can find this way.
11741 do {
11742 unsigned i = Uses[UseIndex].Index;
11743 SDUse &Use = *Uses[UseIndex].Use;
11744 ++UseIndex;
11745
11746 Use.set(To[i]);
11747 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
11748
11749 // Now that we have modified User, add it back to the CSE maps. If it
11750 // already exists there, recursively merge the results together.
11751 AddModifiedNodeToCSEMaps(User);
11752 }
11753}
11754
11755/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
11756/// based on their topological order. It returns the maximum id and a vector
11757/// of the SDNodes* in assigned order by reference.
11759 unsigned DAGSize = 0;
11760
11761 // SortedPos tracks the progress of the algorithm. Nodes before it are
11762 // sorted, nodes after it are unsorted. When the algorithm completes
11763 // it is at the end of the list.
11764 allnodes_iterator SortedPos = allnodes_begin();
11765
11766 // Visit all the nodes. Move nodes with no operands to the front of
11767 // the list immediately. Annotate nodes that do have operands with their
11768 // operand count. Before we do this, the Node Id fields of the nodes
11769 // may contain arbitrary values. After, the Node Id fields for nodes
11770 // before SortedPos will contain the topological sort index, and the
11771 // Node Id fields for nodes At SortedPos and after will contain the
11772 // count of outstanding operands.
11774 checkForCycles(&N, this);
11775 unsigned Degree = N.getNumOperands();
11776 if (Degree == 0) {
11777 // A node with no uses, add it to the result array immediately.
11778 N.setNodeId(DAGSize++);
11779 allnodes_iterator Q(&N);
11780 if (Q != SortedPos)
11781 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
11782 assert(SortedPos != AllNodes.end() && "Overran node list");
11783 ++SortedPos;
11784 } else {
11785 // Temporarily use the Node Id as scratch space for the degree count.
11786 N.setNodeId(Degree);
11787 }
11788 }
11789
11790 // Visit all the nodes. As we iterate, move nodes into sorted order,
11791 // such that by the time the end is reached all nodes will be sorted.
11792 for (SDNode &Node : allnodes()) {
11793 SDNode *N = &Node;
11794 checkForCycles(N, this);
11795 // N is in sorted position, so all its uses have one less operand
11796 // that needs to be sorted.
11797 for (SDNode *P : N->uses()) {
11798 unsigned Degree = P->getNodeId();
11799 assert(Degree != 0 && "Invalid node degree");
11800 --Degree;
11801 if (Degree == 0) {
11802 // All of P's operands are sorted, so P may sorted now.
11803 P->setNodeId(DAGSize++);
11804 if (P->getIterator() != SortedPos)
11805 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
11806 assert(SortedPos != AllNodes.end() && "Overran node list");
11807 ++SortedPos;
11808 } else {
11809 // Update P's outstanding operand count.
11810 P->setNodeId(Degree);
11811 }
11812 }
11813 if (Node.getIterator() == SortedPos) {
11814#ifndef NDEBUG
11816 SDNode *S = &*++I;
11817 dbgs() << "Overran sorted position:\n";
11818 S->dumprFull(this); dbgs() << "\n";
11819 dbgs() << "Checking if this is due to cycles\n";
11820 checkForCycles(this, true);
11821#endif
11822 llvm_unreachable(nullptr);
11823 }
11824 }
11825
11826 assert(SortedPos == AllNodes.end() &&
11827 "Topological sort incomplete!");
11828 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
11829 "First node in topological sort is not the entry token!");
11830 assert(AllNodes.front().getNodeId() == 0 &&
11831 "First node in topological sort has non-zero id!");
11832 assert(AllNodes.front().getNumOperands() == 0 &&
11833 "First node in topological sort has operands!");
11834 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
11835 "Last node in topologic sort has unexpected id!");
11836 assert(AllNodes.back().use_empty() &&
11837 "Last node in topologic sort has users!");
11838 assert(DAGSize == allnodes_size() && "Node count mismatch!");
11839 return DAGSize;
11840}
11841
11842/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
11843/// value is produced by SD.
11844void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
11845 for (SDNode *SD : DB->getSDNodes()) {
11846 if (!SD)
11847 continue;
11848 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
11849 SD->setHasDebugValue(true);
11850 }
11851 DbgInfo->add(DB, isParameter);
11852}
11853
11854void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
11855
11857 SDValue NewMemOpChain) {
11858 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
11859 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
11860 // The new memory operation must have the same position as the old load in
11861 // terms of memory dependency. Create a TokenFactor for the old load and new
11862 // memory operation and update uses of the old load's output chain to use that
11863 // TokenFactor.
11864 if (OldChain == NewMemOpChain || OldChain.use_empty())
11865 return NewMemOpChain;
11866
11867 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
11868 OldChain, NewMemOpChain);
11869 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
11870 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
11871 return TokenFactor;
11872}
11873
11875 SDValue NewMemOp) {
11876 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
11877 SDValue OldChain = SDValue(OldLoad, 1);
11878 SDValue NewMemOpChain = NewMemOp.getValue(1);
11879 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
11880}
11881
11883 Function **OutFunction) {
11884 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
11885
11886 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
11887 auto *Module = MF->getFunction().getParent();
11888 auto *Function = Module->getFunction(Symbol);
11889
11890 if (OutFunction != nullptr)
11891 *OutFunction = Function;
11892
11893 if (Function != nullptr) {
11894 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
11895 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
11896 }
11897
11898 std::string ErrorStr;
11899 raw_string_ostream ErrorFormatter(ErrorStr);
11900 ErrorFormatter << "Undefined external symbol ";
11901 ErrorFormatter << '"' << Symbol << '"';
11902 report_fatal_error(Twine(ErrorStr));
11903}
11904
11905//===----------------------------------------------------------------------===//
11906// SDNode Class
11907//===----------------------------------------------------------------------===//
11908
11910 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11911 return Const != nullptr && Const->isZero();
11912}
11913
11915 return V.isUndef() || isNullConstant(V);
11916}
11917
11919 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
11920 return Const != nullptr && Const->isZero() && !Const->isNegative();
11921}
11922
11924 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11925 return Const != nullptr && Const->isAllOnes();
11926}
11927
11929 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11930 return Const != nullptr && Const->isOne();
11931}
11932
11934 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11935 return Const != nullptr && Const->isMinSignedValue();
11936}
11937
11938bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
11939 unsigned OperandNo) {
11940 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
11941 // TODO: Target-specific opcodes could be added.
11942 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
11943 /*AllowTruncation*/ true)) {
11944 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
11945 switch (Opcode) {
11946 case ISD::ADD:
11947 case ISD::OR:
11948 case ISD::XOR:
11949 case ISD::UMAX:
11950 return Const.isZero();
11951 case ISD::MUL:
11952 return Const.isOne();
11953 case ISD::AND:
11954 case ISD::UMIN:
11955 return Const.isAllOnes();
11956 case ISD::SMAX:
11957 return Const.isMinSignedValue();
11958 case ISD::SMIN:
11959 return Const.isMaxSignedValue();
11960 case ISD::SUB:
11961 case ISD::SHL:
11962 case ISD::SRA:
11963 case ISD::SRL:
11964 return OperandNo == 1 && Const.isZero();
11965 case ISD::UDIV:
11966 case ISD::SDIV:
11967 return OperandNo == 1 && Const.isOne();
11968 }
11969 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
11970 switch (Opcode) {
11971 case ISD::FADD:
11972 return ConstFP->isZero() &&
11973 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
11974 case ISD::FSUB:
11975 return OperandNo == 1 && ConstFP->isZero() &&
11976 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
11977 case ISD::FMUL:
11978 return ConstFP->isExactlyValue(1.0);
11979 case ISD::FDIV:
11980 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
11981 case ISD::FMINNUM:
11982 case ISD::FMAXNUM: {
11983 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
11984 EVT VT = V.getValueType();
11985 const fltSemantics &Semantics = VT.getFltSemantics();
11986 APFloat NeutralAF = !Flags.hasNoNaNs()
11987 ? APFloat::getQNaN(Semantics)
11988 : !Flags.hasNoInfs()
11989 ? APFloat::getInf(Semantics)
11990 : APFloat::getLargest(Semantics);
11991 if (Opcode == ISD::FMAXNUM)
11992 NeutralAF.changeSign();
11993
11994 return ConstFP->isExactlyValue(NeutralAF);
11995 }
11996 }
11997 }
11998 return false;
11999}
12000
12002 while (V.getOpcode() == ISD::BITCAST)
12003 V = V.getOperand(0);
12004 return V;
12005}
12006
12008 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12009 V = V.getOperand(0);
12010 return V;
12011}
12012
12014 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12015 V = V.getOperand(0);
12016 return V;
12017}
12018
12020 while (V.getOpcode() == ISD::TRUNCATE)
12021 V = V.getOperand(0);
12022 return V;
12023}
12024
12025bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12026 if (V.getOpcode() != ISD::XOR)
12027 return false;
12028 V = peekThroughBitcasts(V.getOperand(1));
12029 unsigned NumBits = V.getScalarValueSizeInBits();
12030 ConstantSDNode *C =
12031 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12032 return C && (C->getAPIntValue().countr_one() >= NumBits);
12033}
12034
12036 bool AllowTruncation) {
12037 EVT VT = N.getValueType();
12038 APInt DemandedElts = VT.isFixedLengthVector()
12040 : APInt(1, 1);
12041 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12042}
12043
12045 bool AllowUndefs,
12046 bool AllowTruncation) {
12047 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
12048 return CN;
12049
12050 // SplatVectors can truncate their operands. Ignore that case here unless
12051 // AllowTruncation is set.
12052 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12053 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12054 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12055 EVT CVT = CN->getValueType(0);
12056 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12057 if (AllowTruncation || CVT == VecEltVT)
12058 return CN;
12059 }
12060 }
12061
12062 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12063 BitVector UndefElements;
12064 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12065
12066 // BuildVectors can truncate their operands. Ignore that case here unless
12067 // AllowTruncation is set.
12068 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12069 if (CN && (UndefElements.none() || AllowUndefs)) {
12070 EVT CVT = CN->getValueType(0);
12071 EVT NSVT = N.getValueType().getScalarType();
12072 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12073 if (AllowTruncation || (CVT == NSVT))
12074 return CN;
12075 }
12076 }
12077
12078 return nullptr;
12079}
12080
12082 EVT VT = N.getValueType();
12083 APInt DemandedElts = VT.isFixedLengthVector()
12085 : APInt(1, 1);
12086 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12087}
12088
12090 const APInt &DemandedElts,
12091 bool AllowUndefs) {
12092 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
12093 return CN;
12094
12095 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12096 BitVector UndefElements;
12097 ConstantFPSDNode *CN =
12098 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12099 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12100 if (CN && (UndefElements.none() || AllowUndefs))
12101 return CN;
12102 }
12103
12104 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12105 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12106 return CN;
12107
12108 return nullptr;
12109}
12110
12111bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12112 // TODO: may want to use peekThroughBitcast() here.
12113 ConstantSDNode *C =
12114 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12115 return C && C->isZero();
12116}
12117
12118bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12119 ConstantSDNode *C =
12120 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12121 return C && C->isOne();
12122}
12123
12124bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12126 unsigned BitWidth = N.getScalarValueSizeInBits();
12127 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12128 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12129}
12130
12132 DropOperands();
12133}
12134
12135MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12136 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12137 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12138 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12139 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12140 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12141 MemSDNodeBits.IsInvariant = MMO->isInvariant();
12142
12143 // We check here that the size of the memory operand fits within the size of
12144 // the MMO. This is because the MMO might indicate only a possible address
12145 // range instead of specifying the affected memory addresses precisely.
12146 assert(
12147 (!MMO->getType().isValid() ||
12149 "Size mismatch!");
12150}
12151
12152/// Profile - Gather unique data for the node.
12153///
12155 AddNodeIDNode(ID, this);
12156}
12157
12158namespace {
12159
12160 struct EVTArray {
12161 std::vector<EVT> VTs;
12162
12163 EVTArray() {
12164 VTs.reserve(MVT::VALUETYPE_SIZE);
12165 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
12166 VTs.push_back(MVT((MVT::SimpleValueType)i));
12167 }
12168 };
12169
12170} // end anonymous namespace
12171
12172/// getValueTypeList - Return a pointer to the specified value type.
12173///
12174const EVT *SDNode::getValueTypeList(EVT VT) {
12175 static std::set<EVT, EVT::compareRawBits> EVTs;
12176 static EVTArray SimpleVTArray;
12177 static sys::SmartMutex<true> VTMutex;
12178
12179 if (VT.isExtended()) {
12180 sys::SmartScopedLock<true> Lock(VTMutex);
12181 return &(*EVTs.insert(VT).first);
12182 }
12183 assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
12184 return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
12185}
12186
12187/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
12188/// indicated value. This method ignores uses of other values defined by this
12189/// operation.
12190bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
12191 assert(Value < getNumValues() && "Bad value!");
12192
12193 // TODO: Only iterate over uses of a given value of the node
12194 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
12195 if (UI.getUse().getResNo() == Value) {
12196 if (NUses == 0)
12197 return false;
12198 --NUses;
12199 }
12200 }
12201
12202 // Found exactly the right number of uses?
12203 return NUses == 0;
12204}
12205
12206/// hasAnyUseOfValue - Return true if there are any use of the indicated
12207/// value. This method ignores uses of other values defined by this operation.
12208bool SDNode::hasAnyUseOfValue(unsigned Value) const {
12209 assert(Value < getNumValues() && "Bad value!");
12210
12211 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
12212 if (UI.getUse().getResNo() == Value)
12213 return true;
12214
12215 return false;
12216}
12217
12218/// isOnlyUserOf - Return true if this node is the only use of N.
12219bool SDNode::isOnlyUserOf(const SDNode *N) const {
12220 bool Seen = false;
12221 for (const SDNode *User : N->uses()) {
12222 if (User == this)
12223 Seen = true;
12224 else
12225 return false;
12226 }
12227
12228 return Seen;
12229}
12230
12231/// Return true if the only users of N are contained in Nodes.
12233 bool Seen = false;
12234 for (const SDNode *User : N->uses()) {
12235 if (llvm::is_contained(Nodes, User))
12236 Seen = true;
12237 else
12238 return false;
12239 }
12240
12241 return Seen;
12242}
12243
12244/// isOperand - Return true if this node is an operand of N.
12245bool SDValue::isOperandOf(const SDNode *N) const {
12246 return is_contained(N->op_values(), *this);
12247}
12248
12249bool SDNode::isOperandOf(const SDNode *N) const {
12250 return any_of(N->op_values(),
12251 [this](SDValue Op) { return this == Op.getNode(); });
12252}
12253
12254/// reachesChainWithoutSideEffects - Return true if this operand (which must
12255/// be a chain) reaches the specified operand without crossing any
12256/// side-effecting instructions on any chain path. In practice, this looks
12257/// through token factors and non-volatile loads. In order to remain efficient,
12258/// this only looks a couple of nodes in, it does not do an exhaustive search.
12259///
12260/// Note that we only need to examine chains when we're searching for
12261/// side-effects; SelectionDAG requires that all side-effects are represented
12262/// by chains, even if another operand would force a specific ordering. This
12263/// constraint is necessary to allow transformations like splitting loads.
12265 unsigned Depth) const {
12266 if (*this == Dest) return true;
12267
12268 // Don't search too deeply, we just want to be able to see through
12269 // TokenFactor's etc.
12270 if (Depth == 0) return false;
12271
12272 // If this is a token factor, all inputs to the TF happen in parallel.
12273 if (getOpcode() == ISD::TokenFactor) {
12274 // First, try a shallow search.
12275 if (is_contained((*this)->ops(), Dest)) {
12276 // We found the chain we want as an operand of this TokenFactor.
12277 // Essentially, we reach the chain without side-effects if we could
12278 // serialize the TokenFactor into a simple chain of operations with
12279 // Dest as the last operation. This is automatically true if the
12280 // chain has one use: there are no other ordering constraints.
12281 // If the chain has more than one use, we give up: some other
12282 // use of Dest might force a side-effect between Dest and the current
12283 // node.
12284 if (Dest.hasOneUse())
12285 return true;
12286 }
12287 // Next, try a deep search: check whether every operand of the TokenFactor
12288 // reaches Dest.
12289 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
12290 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
12291 });
12292 }
12293
12294 // Loads don't have side effects, look through them.
12295 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
12296 if (Ld->isUnordered())
12297 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
12298 }
12299 return false;
12300}
12301
12302bool SDNode::hasPredecessor(const SDNode *N) const {
12305 Worklist.push_back(this);
12306 return hasPredecessorHelper(N, Visited, Worklist);
12307}
12308
12310 this->Flags.intersectWith(Flags);
12311}
12312
12313SDValue
12315 ArrayRef<ISD::NodeType> CandidateBinOps,
12316 bool AllowPartials) {
12317 // The pattern must end in an extract from index 0.
12318 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
12319 !isNullConstant(Extract->getOperand(1)))
12320 return SDValue();
12321
12322 // Match against one of the candidate binary ops.
12323 SDValue Op = Extract->getOperand(0);
12324 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
12325 return Op.getOpcode() == unsigned(BinOp);
12326 }))
12327 return SDValue();
12328
12329 // Floating-point reductions may require relaxed constraints on the final step
12330 // of the reduction because they may reorder intermediate operations.
12331 unsigned CandidateBinOp = Op.getOpcode();
12332 if (Op.getValueType().isFloatingPoint()) {
12333 SDNodeFlags Flags = Op->getFlags();
12334 switch (CandidateBinOp) {
12335 case ISD::FADD:
12336 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
12337 return SDValue();
12338 break;
12339 default:
12340 llvm_unreachable("Unhandled FP opcode for binop reduction");
12341 }
12342 }
12343
12344 // Matching failed - attempt to see if we did enough stages that a partial
12345 // reduction from a subvector is possible.
12346 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12347 if (!AllowPartials || !Op)
12348 return SDValue();
12349 EVT OpVT = Op.getValueType();
12350 EVT OpSVT = OpVT.getScalarType();
12351 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12352 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12353 return SDValue();
12354 BinOp = (ISD::NodeType)CandidateBinOp;
12355 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12357 };
12358
12359 // At each stage, we're looking for something that looks like:
12360 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12361 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12362 // i32 undef, i32 undef, i32 undef, i32 undef>
12363 // %a = binop <8 x i32> %op, %s
12364 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12365 // we expect something like:
12366 // <4,5,6,7,u,u,u,u>
12367 // <2,3,u,u,u,u,u,u>
12368 // <1,u,u,u,u,u,u,u>
12369 // While a partial reduction match would be:
12370 // <2,3,u,u,u,u,u,u>
12371 // <1,u,u,u,u,u,u,u>
12372 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12373 SDValue PrevOp;
12374 for (unsigned i = 0; i < Stages; ++i) {
12375 unsigned MaskEnd = (1 << i);
12376
12377 if (Op.getOpcode() != CandidateBinOp)
12378 return PartialReduction(PrevOp, MaskEnd);
12379
12380 SDValue Op0 = Op.getOperand(0);
12381 SDValue Op1 = Op.getOperand(1);
12382
12383 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12384 if (Shuffle) {
12385 Op = Op1;
12386 } else {
12387 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12388 Op = Op0;
12389 }
12390
12391 // The first operand of the shuffle should be the same as the other operand
12392 // of the binop.
12393 if (!Shuffle || Shuffle->getOperand(0) != Op)
12394 return PartialReduction(PrevOp, MaskEnd);
12395
12396 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12397 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12398 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12399 return PartialReduction(PrevOp, MaskEnd);
12400
12401 PrevOp = Op;
12402 }
12403
12404 // Handle subvector reductions, which tend to appear after the shuffle
12405 // reduction stages.
12406 while (Op.getOpcode() == CandidateBinOp) {
12407 unsigned NumElts = Op.getValueType().getVectorNumElements();
12408 SDValue Op0 = Op.getOperand(0);
12409 SDValue Op1 = Op.getOperand(1);
12410 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12412 Op0.getOperand(0) != Op1.getOperand(0))
12413 break;
12414 SDValue Src = Op0.getOperand(0);
12415 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12416 if (NumSrcElts != (2 * NumElts))
12417 break;
12418 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12419 Op1.getConstantOperandAPInt(1) == NumElts) &&
12420 !(Op1.getConstantOperandAPInt(1) == 0 &&
12421 Op0.getConstantOperandAPInt(1) == NumElts))
12422 break;
12423 Op = Src;
12424 }
12425
12426 BinOp = (ISD::NodeType)CandidateBinOp;
12427 return Op;
12428}
12429
12431 EVT VT = N->getValueType(0);
12432 EVT EltVT = VT.getVectorElementType();
12433 unsigned NE = VT.getVectorNumElements();
12434
12435 SDLoc dl(N);
12436
12437 // If ResNE is 0, fully unroll the vector op.
12438 if (ResNE == 0)
12439 ResNE = NE;
12440 else if (NE > ResNE)
12441 NE = ResNE;
12442
12443 if (N->getNumValues() == 2) {
12444 SmallVector<SDValue, 8> Scalars0, Scalars1;
12445 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12446 EVT VT1 = N->getValueType(1);
12447 EVT EltVT1 = VT1.getVectorElementType();
12448
12449 unsigned i;
12450 for (i = 0; i != NE; ++i) {
12451 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12452 SDValue Operand = N->getOperand(j);
12453 EVT OperandVT = Operand.getValueType();
12454
12455 // A vector operand; extract a single element.
12456 EVT OperandEltVT = OperandVT.getVectorElementType();
12457 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12458 Operand, getVectorIdxConstant(i, dl));
12459 }
12460
12461 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12462 Scalars0.push_back(EltOp);
12463 Scalars1.push_back(EltOp.getValue(1));
12464 }
12465
12466 SDValue Vec0 = getBuildVector(VT, dl, Scalars0);
12467 SDValue Vec1 = getBuildVector(VT1, dl, Scalars1);
12468 return getMergeValues({Vec0, Vec1}, dl);
12469 }
12470
12471 assert(N->getNumValues() == 1 &&
12472 "Can't unroll a vector with multiple results!");
12473
12475 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12476
12477 unsigned i;
12478 for (i= 0; i != NE; ++i) {
12479 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12480 SDValue Operand = N->getOperand(j);
12481 EVT OperandVT = Operand.getValueType();
12482 if (OperandVT.isVector()) {
12483 // A vector operand; extract a single element.
12484 EVT OperandEltVT = OperandVT.getVectorElementType();
12485 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12486 Operand, getVectorIdxConstant(i, dl));
12487 } else {
12488 // A scalar operand; just use it as is.
12489 Operands[j] = Operand;
12490 }
12491 }
12492
12493 switch (N->getOpcode()) {
12494 default: {
12495 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12496 N->getFlags()));
12497 break;
12498 }
12499 case ISD::VSELECT:
12500 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12501 break;
12502 case ISD::SHL:
12503 case ISD::SRA:
12504 case ISD::SRL:
12505 case ISD::ROTL:
12506 case ISD::ROTR:
12507 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12509 Operands[1])));
12510 break;
12512 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12513 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12514 Operands[0],
12515 getValueType(ExtVT)));
12516 }
12517 }
12518 }
12519
12520 for (; i < ResNE; ++i)
12521 Scalars.push_back(getUNDEF(EltVT));
12522
12523 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12524 return getBuildVector(VecVT, dl, Scalars);
12525}
12526
12527std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12528 SDNode *N, unsigned ResNE) {
12529 unsigned Opcode = N->getOpcode();
12530 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12531 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12532 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12533 "Expected an overflow opcode");
12534
12535 EVT ResVT = N->getValueType(0);
12536 EVT OvVT = N->getValueType(1);
12537 EVT ResEltVT = ResVT.getVectorElementType();
12538 EVT OvEltVT = OvVT.getVectorElementType();
12539 SDLoc dl(N);
12540
12541 // If ResNE is 0, fully unroll the vector op.
12542 unsigned NE = ResVT.getVectorNumElements();
12543 if (ResNE == 0)
12544 ResNE = NE;
12545 else if (NE > ResNE)
12546 NE = ResNE;
12547
12548 SmallVector<SDValue, 8> LHSScalars;
12549 SmallVector<SDValue, 8> RHSScalars;
12550 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12551 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12552
12553 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12554 SDVTList VTs = getVTList(ResEltVT, SVT);
12555 SmallVector<SDValue, 8> ResScalars;
12556 SmallVector<SDValue, 8> OvScalars;
12557 for (unsigned i = 0; i < NE; ++i) {
12558 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12559 SDValue Ov =
12560 getSelect(dl, OvEltVT, Res.getValue(1),
12561 getBoolConstant(true, dl, OvEltVT, ResVT),
12562 getConstant(0, dl, OvEltVT));
12563
12564 ResScalars.push_back(Res);
12565 OvScalars.push_back(Ov);
12566 }
12567
12568 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12569 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12570
12571 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12572 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12573 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12574 getBuildVector(NewOvVT, dl, OvScalars));
12575}
12576
12579 unsigned Bytes,
12580 int Dist) const {
12581 if (LD->isVolatile() || Base->isVolatile())
12582 return false;
12583 // TODO: probably too restrictive for atomics, revisit
12584 if (!LD->isSimple())
12585 return false;
12586 if (LD->isIndexed() || Base->isIndexed())
12587 return false;
12588 if (LD->getChain() != Base->getChain())
12589 return false;
12590 EVT VT = LD->getMemoryVT();
12591 if (VT.getSizeInBits() / 8 != Bytes)
12592 return false;
12593
12594 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12595 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12596
12597 int64_t Offset = 0;
12598 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12599 return (Dist * (int64_t)Bytes == Offset);
12600 return false;
12601}
12602
12603/// InferPtrAlignment - Infer alignment of a load / store address. Return
12604/// std::nullopt if it cannot be inferred.
12606 // If this is a GlobalAddress + cst, return the alignment.
12607 const GlobalValue *GV = nullptr;
12608 int64_t GVOffset = 0;
12609 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12610 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12611 KnownBits Known(PtrWidth);
12613 unsigned AlignBits = Known.countMinTrailingZeros();
12614 if (AlignBits)
12615 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12616 }
12617
12618 // If this is a direct reference to a stack slot, use information about the
12619 // stack slot's alignment.
12620 int FrameIdx = INT_MIN;
12621 int64_t FrameOffset = 0;
12622 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12623 FrameIdx = FI->getIndex();
12624 } else if (isBaseWithConstantOffset(Ptr) &&
12625 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12626 // Handle FI+Cst
12627 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12628 FrameOffset = Ptr.getConstantOperandVal(1);
12629 }
12630
12631 if (FrameIdx != INT_MIN) {
12633 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12634 }
12635
12636 return std::nullopt;
12637}
12638
12639/// Split the scalar node with EXTRACT_ELEMENT using the provided
12640/// VTs and return the low/high part.
12641std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12642 const SDLoc &DL,
12643 const EVT &LoVT,
12644 const EVT &HiVT) {
12645 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12646 "Split node must be a scalar type");
12647 SDValue Lo =
12649 SDValue Hi =
12651 return std::make_pair(Lo, Hi);
12652}
12653
12654/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12655/// which is split (or expanded) into two not necessarily identical pieces.
12656std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12657 // Currently all types are split in half.
12658 EVT LoVT, HiVT;
12659 if (!VT.isVector())
12660 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12661 else
12662 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12663
12664 return std::make_pair(LoVT, HiVT);
12665}
12666
12667/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12668/// type, dependent on an enveloping VT that has been split into two identical
12669/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12670std::pair<EVT, EVT>
12672 bool *HiIsEmpty) const {
12673 EVT EltTp = VT.getVectorElementType();
12674 // Examples:
12675 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12676 // custom VL=9 with enveloping VL=8/8 yields 8/1
12677 // custom VL=10 with enveloping VL=8/8 yields 8/2
12678 // etc.
12679 ElementCount VTNumElts = VT.getVectorElementCount();
12680 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12681 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12682 "Mixing fixed width and scalable vectors when enveloping a type");
12683 EVT LoVT, HiVT;
12684 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12685 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12686 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12687 *HiIsEmpty = false;
12688 } else {
12689 // Flag that hi type has zero storage size, but return split envelop type
12690 // (this would be easier if vector types with zero elements were allowed).
12691 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12692 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12693 *HiIsEmpty = true;
12694 }
12695 return std::make_pair(LoVT, HiVT);
12696}
12697
12698/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
12699/// low/high part.
12700std::pair<SDValue, SDValue>
12701SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
12702 const EVT &HiVT) {
12703 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
12704 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
12705 "Splitting vector with an invalid mixture of fixed and scalable "
12706 "vector types");
12708 N.getValueType().getVectorMinNumElements() &&
12709 "More vector elements requested than available!");
12710 SDValue Lo, Hi;
12711 Lo =
12713 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
12714 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
12715 // IDX with the runtime scaling factor of the result vector type. For
12716 // fixed-width result vectors, that runtime scaling factor is 1.
12719 return std::make_pair(Lo, Hi);
12720}
12721
12722std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
12723 const SDLoc &DL) {
12724 // Split the vector length parameter.
12725 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
12726 EVT VT = N.getValueType();
12728 "Expecting the mask to be an evenly-sized vector");
12729 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
12730 SDValue HalfNumElts =
12731 VecVT.isFixedLengthVector()
12732 ? getConstant(HalfMinNumElts, DL, VT)
12733 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
12734 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
12735 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
12736 return std::make_pair(Lo, Hi);
12737}
12738
12739/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
12741 EVT VT = N.getValueType();
12744 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
12746}
12747
12750 unsigned Start, unsigned Count,
12751 EVT EltVT) {
12752 EVT VT = Op.getValueType();
12753 if (Count == 0)
12754 Count = VT.getVectorNumElements();
12755 if (EltVT == EVT())
12756 EltVT = VT.getVectorElementType();
12757 SDLoc SL(Op);
12758 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
12759 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
12760 getVectorIdxConstant(i, SL)));
12761 }
12762}
12763
12764// getAddressSpace - Return the address space this GlobalAddress belongs to.
12766 return getGlobal()->getType()->getAddressSpace();
12767}
12768
12771 return Val.MachineCPVal->getType();
12772 return Val.ConstVal->getType();
12773}
12774
12775bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
12776 unsigned &SplatBitSize,
12777 bool &HasAnyUndefs,
12778 unsigned MinSplatBits,
12779 bool IsBigEndian) const {
12780 EVT VT = getValueType(0);
12781 assert(VT.isVector() && "Expected a vector type");
12782 unsigned VecWidth = VT.getSizeInBits();
12783 if (MinSplatBits > VecWidth)
12784 return false;
12785
12786 // FIXME: The widths are based on this node's type, but build vectors can
12787 // truncate their operands.
12788 SplatValue = APInt(VecWidth, 0);
12789 SplatUndef = APInt(VecWidth, 0);
12790
12791 // Get the bits. Bits with undefined values (when the corresponding element
12792 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
12793 // in SplatValue. If any of the values are not constant, give up and return
12794 // false.
12795 unsigned int NumOps = getNumOperands();
12796 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
12797 unsigned EltWidth = VT.getScalarSizeInBits();
12798
12799 for (unsigned j = 0; j < NumOps; ++j) {
12800 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
12801 SDValue OpVal = getOperand(i);
12802 unsigned BitPos = j * EltWidth;
12803
12804 if (OpVal.isUndef())
12805 SplatUndef.setBits(BitPos, BitPos + EltWidth);
12806 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
12807 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
12808 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
12809 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
12810 else
12811 return false;
12812 }
12813
12814 // The build_vector is all constants or undefs. Find the smallest element
12815 // size that splats the vector.
12816 HasAnyUndefs = (SplatUndef != 0);
12817
12818 // FIXME: This does not work for vectors with elements less than 8 bits.
12819 while (VecWidth > 8) {
12820 // If we can't split in half, stop here.
12821 if (VecWidth & 1)
12822 break;
12823
12824 unsigned HalfSize = VecWidth / 2;
12825 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
12826 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
12827 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
12828 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
12829
12830 // If the two halves do not match (ignoring undef bits), stop here.
12831 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
12832 MinSplatBits > HalfSize)
12833 break;
12834
12835 SplatValue = HighValue | LowValue;
12836 SplatUndef = HighUndef & LowUndef;
12837
12838 VecWidth = HalfSize;
12839 }
12840
12841 // FIXME: The loop above only tries to split in halves. But if the input
12842 // vector for example is <3 x i16> it wouldn't be able to detect a
12843 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
12844 // optimizations. I guess that back in the days when this helper was created
12845 // vectors normally was power-of-2 sized.
12846
12847 SplatBitSize = VecWidth;
12848 return true;
12849}
12850
12852 BitVector *UndefElements) const {
12853 unsigned NumOps = getNumOperands();
12854 if (UndefElements) {
12855 UndefElements->clear();
12856 UndefElements->resize(NumOps);
12857 }
12858 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12859 if (!DemandedElts)
12860 return SDValue();
12861 SDValue Splatted;
12862 for (unsigned i = 0; i != NumOps; ++i) {
12863 if (!DemandedElts[i])
12864 continue;
12865 SDValue Op = getOperand(i);
12866 if (Op.isUndef()) {
12867 if (UndefElements)
12868 (*UndefElements)[i] = true;
12869 } else if (!Splatted) {
12870 Splatted = Op;
12871 } else if (Splatted != Op) {
12872 return SDValue();
12873 }
12874 }
12875
12876 if (!Splatted) {
12877 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
12878 assert(getOperand(FirstDemandedIdx).isUndef() &&
12879 "Can only have a splat without a constant for all undefs.");
12880 return getOperand(FirstDemandedIdx);
12881 }
12882
12883 return Splatted;
12884}
12885
12887 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12888 return getSplatValue(DemandedElts, UndefElements);
12889}
12890
12892 SmallVectorImpl<SDValue> &Sequence,
12893 BitVector *UndefElements) const {
12894 unsigned NumOps = getNumOperands();
12895 Sequence.clear();
12896 if (UndefElements) {
12897 UndefElements->clear();
12898 UndefElements->resize(NumOps);
12899 }
12900 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12901 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
12902 return false;
12903
12904 // Set the undefs even if we don't find a sequence (like getSplatValue).
12905 if (UndefElements)
12906 for (unsigned I = 0; I != NumOps; ++I)
12907 if (DemandedElts[I] && getOperand(I).isUndef())
12908 (*UndefElements)[I] = true;
12909
12910 // Iteratively widen the sequence length looking for repetitions.
12911 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
12912 Sequence.append(SeqLen, SDValue());
12913 for (unsigned I = 0; I != NumOps; ++I) {
12914 if (!DemandedElts[I])
12915 continue;
12916 SDValue &SeqOp = Sequence[I % SeqLen];
12918 if (Op.isUndef()) {
12919 if (!SeqOp)
12920 SeqOp = Op;
12921 continue;
12922 }
12923 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
12924 Sequence.clear();
12925 break;
12926 }
12927 SeqOp = Op;
12928 }
12929 if (!Sequence.empty())
12930 return true;
12931 }
12932
12933 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
12934 return false;
12935}
12936
12938 BitVector *UndefElements) const {
12939 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12940 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
12941}
12942
12945 BitVector *UndefElements) const {
12946 return dyn_cast_or_null<ConstantSDNode>(
12947 getSplatValue(DemandedElts, UndefElements));
12948}
12949
12952 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
12953}
12954
12957 BitVector *UndefElements) const {
12958 return dyn_cast_or_null<ConstantFPSDNode>(
12959 getSplatValue(DemandedElts, UndefElements));
12960}
12961
12964 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
12965}
12966
12967int32_t
12969 uint32_t BitWidth) const {
12970 if (ConstantFPSDNode *CN =
12971 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
12972 bool IsExact;
12973 APSInt IntVal(BitWidth);
12974 const APFloat &APF = CN->getValueAPF();
12975 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
12976 APFloat::opOK ||
12977 !IsExact)
12978 return -1;
12979
12980 return IntVal.exactLogBase2();
12981 }
12982 return -1;
12983}
12984
12986 bool IsLittleEndian, unsigned DstEltSizeInBits,
12987 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
12988 // Early-out if this contains anything but Undef/Constant/ConstantFP.
12989 if (!isConstant())
12990 return false;
12991
12992 unsigned NumSrcOps = getNumOperands();
12993 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
12994 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12995 "Invalid bitcast scale");
12996
12997 // Extract raw src bits.
12998 SmallVector<APInt> SrcBitElements(NumSrcOps,
12999 APInt::getZero(SrcEltSizeInBits));
13000 BitVector SrcUndeElements(NumSrcOps, false);
13001
13002 for (unsigned I = 0; I != NumSrcOps; ++I) {
13004 if (Op.isUndef()) {
13005 SrcUndeElements.set(I);
13006 continue;
13007 }
13008 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13009 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13010 assert((CInt || CFP) && "Unknown constant");
13011 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13012 : CFP->getValueAPF().bitcastToAPInt();
13013 }
13014
13015 // Recast to dst width.
13016 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13017 SrcBitElements, UndefElements, SrcUndeElements);
13018 return true;
13019}
13020
13021void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13022 unsigned DstEltSizeInBits,
13023 SmallVectorImpl<APInt> &DstBitElements,
13024 ArrayRef<APInt> SrcBitElements,
13025 BitVector &DstUndefElements,
13026 const BitVector &SrcUndefElements) {
13027 unsigned NumSrcOps = SrcBitElements.size();
13028 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13029 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13030 "Invalid bitcast scale");
13031 assert(NumSrcOps == SrcUndefElements.size() &&
13032 "Vector size mismatch");
13033
13034 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13035 DstUndefElements.clear();
13036 DstUndefElements.resize(NumDstOps, false);
13037 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13038
13039 // Concatenate src elements constant bits together into dst element.
13040 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13041 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13042 for (unsigned I = 0; I != NumDstOps; ++I) {
13043 DstUndefElements.set(I);
13044 APInt &DstBits = DstBitElements[I];
13045 for (unsigned J = 0; J != Scale; ++J) {
13046 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13047 if (SrcUndefElements[Idx])
13048 continue;
13049 DstUndefElements.reset(I);
13050 const APInt &SrcBits = SrcBitElements[Idx];
13051 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13052 "Illegal constant bitwidths");
13053 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13054 }
13055 }
13056 return;
13057 }
13058
13059 // Split src element constant bits into dst elements.
13060 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13061 for (unsigned I = 0; I != NumSrcOps; ++I) {
13062 if (SrcUndefElements[I]) {
13063 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13064 continue;
13065 }
13066 const APInt &SrcBits = SrcBitElements[I];
13067 for (unsigned J = 0; J != Scale; ++J) {
13068 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13069 APInt &DstBits = DstBitElements[Idx];
13070 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13071 }
13072 }
13073}
13074
13076 for (const SDValue &Op : op_values()) {
13077 unsigned Opc = Op.getOpcode();
13078 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13079 return false;
13080 }
13081 return true;
13082}
13083
13084std::optional<std::pair<APInt, APInt>>
13086 unsigned NumOps = getNumOperands();
13087 if (NumOps < 2)
13088 return std::nullopt;
13089
13090 if (!isa<ConstantSDNode>(getOperand(0)) ||
13091 !isa<ConstantSDNode>(getOperand(1)))
13092 return std::nullopt;
13093
13094 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13095 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13096 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13097
13098 if (Stride.isZero())
13099 return std::nullopt;
13100
13101 for (unsigned i = 2; i < NumOps; ++i) {
13102 if (!isa<ConstantSDNode>(getOperand(i)))
13103 return std::nullopt;
13104
13105 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13106 if (Val != (Start + (Stride * i)))
13107 return std::nullopt;
13108 }
13109
13110 return std::make_pair(Start, Stride);
13111}
13112
13113bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
13114 // Find the first non-undef value in the shuffle mask.
13115 unsigned i, e;
13116 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
13117 /* search */;
13118
13119 // If all elements are undefined, this shuffle can be considered a splat
13120 // (although it should eventually get simplified away completely).
13121 if (i == e)
13122 return true;
13123
13124 // Make sure all remaining elements are either undef or the same as the first
13125 // non-undef value.
13126 for (int Idx = Mask[i]; i != e; ++i)
13127 if (Mask[i] >= 0 && Mask[i] != Idx)
13128 return false;
13129 return true;
13130}
13131
13132// Returns the SDNode if it is a constant integer BuildVector
13133// or constant integer.
13135 if (isa<ConstantSDNode>(N))
13136 return N.getNode();
13138 return N.getNode();
13139 // Treat a GlobalAddress supporting constant offset folding as a
13140 // constant integer.
13141 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
13142 if (GA->getOpcode() == ISD::GlobalAddress &&
13143 TLI->isOffsetFoldingLegal(GA))
13144 return GA;
13145 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13146 isa<ConstantSDNode>(N.getOperand(0)))
13147 return N.getNode();
13148 return nullptr;
13149}
13150
13151// Returns the SDNode if it is a constant float BuildVector
13152// or constant float.
13154 if (isa<ConstantFPSDNode>(N))
13155 return N.getNode();
13156
13158 return N.getNode();
13159
13160 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13161 isa<ConstantFPSDNode>(N.getOperand(0)))
13162 return N.getNode();
13163
13164 return nullptr;
13165}
13166
13168 bool AllowTruncation) const {
13169 ConstantSDNode *Const = isConstOrConstSplat(N, false, AllowTruncation);
13170 if (!Const)
13171 return std::nullopt;
13172
13173 const APInt &CVal = Const->getAPIntValue();
13174 switch (TLI->getBooleanContents(N.getValueType())) {
13176 if (CVal.isOne())
13177 return true;
13178 if (CVal.isZero())
13179 return false;
13180 return std::nullopt;
13182 if (CVal.isAllOnes())
13183 return true;
13184 if (CVal.isZero())
13185 return false;
13186 return std::nullopt;
13188 return CVal[0];
13189 }
13190 llvm_unreachable("Unknown BooleanContent enum");
13191}
13192
13193void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
13194 assert(!Node->OperandList && "Node already has operands");
13196 "too many operands to fit into SDNode");
13197 SDUse *Ops = OperandRecycler.allocate(
13198 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
13199
13200 bool IsDivergent = false;
13201 for (unsigned I = 0; I != Vals.size(); ++I) {
13202 Ops[I].setUser(Node);
13203 Ops[I].setInitial(Vals[I]);
13204 EVT VT = Ops[I].getValueType();
13205
13206 // Skip Chain. It does not carry divergence.
13207 if (VT != MVT::Other &&
13208 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
13209 Ops[I].getNode()->isDivergent()) {
13210 IsDivergent = true;
13211 }
13212 }
13213 Node->NumOperands = Vals.size();
13214 Node->OperandList = Ops;
13215 if (!TLI->isSDNodeAlwaysUniform(Node)) {
13216 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
13217 Node->SDNodeBits.IsDivergent = IsDivergent;
13218 }
13220}
13221
13224 size_t Limit = SDNode::getMaxNumOperands();
13225 while (Vals.size() > Limit) {
13226 unsigned SliceIdx = Vals.size() - Limit;
13227 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
13228 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
13229 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
13230 Vals.emplace_back(NewTF);
13231 }
13232 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
13233}
13234
13236 EVT VT, SDNodeFlags Flags) {
13237 switch (Opcode) {
13238 default:
13239 return SDValue();
13240 case ISD::ADD:
13241 case ISD::OR:
13242 case ISD::XOR:
13243 case ISD::UMAX:
13244 return getConstant(0, DL, VT);
13245 case ISD::MUL:
13246 return getConstant(1, DL, VT);
13247 case ISD::AND:
13248 case ISD::UMIN:
13249 return getAllOnesConstant(DL, VT);
13250 case ISD::SMAX:
13252 case ISD::SMIN:
13254 case ISD::FADD:
13255 return getConstantFP(-0.0, DL, VT);
13256 case ISD::FMUL:
13257 return getConstantFP(1.0, DL, VT);
13258 case ISD::FMINNUM:
13259 case ISD::FMAXNUM: {
13260 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13261 const fltSemantics &Semantics = VT.getFltSemantics();
13262 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
13263 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
13264 APFloat::getLargest(Semantics);
13265 if (Opcode == ISD::FMAXNUM)
13266 NeutralAF.changeSign();
13267
13268 return getConstantFP(NeutralAF, DL, VT);
13269 }
13270 case ISD::FMINIMUM:
13271 case ISD::FMAXIMUM: {
13272 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
13273 const fltSemantics &Semantics = VT.getFltSemantics();
13274 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
13275 : APFloat::getLargest(Semantics);
13276 if (Opcode == ISD::FMAXIMUM)
13277 NeutralAF.changeSign();
13278
13279 return getConstantFP(NeutralAF, DL, VT);
13280 }
13281
13282 }
13283}
13284
13285/// Helper used to make a call to a library function that has one argument of
13286/// pointer type.
13287///
13288/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
13289/// used to get or set floating-point state. They have one argument of pointer
13290/// type, which points to the memory region containing bits of the
13291/// floating-point state. The value returned by such function is ignored in the
13292/// created call.
13293///
13294/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
13295/// \param Ptr Pointer used to save/load state.
13296/// \param InChain Ingoing token chain.
13297/// \returns Outgoing chain token.
13299 SDValue InChain,
13300 const SDLoc &DLoc) {
13301 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
13304 Entry.Node = Ptr;
13305 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
13306 Args.push_back(Entry);
13307 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
13308 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
13309 TLI->getPointerTy(getDataLayout()));
13311 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
13312 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
13313 std::move(Args));
13314 return TLI->LowerCallTo(CLI).second;
13315}
13316
13318 assert(From && To && "Invalid SDNode; empty source SDValue?");
13319 auto I = SDEI.find(From);
13320 if (I == SDEI.end())
13321 return;
13322
13323 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
13324 // the iterator, hence the need to make a copy to prevent a use-after-free.
13325 NodeExtraInfo NEI = I->second;
13326 if (LLVM_LIKELY(!NEI.PCSections) && LLVM_LIKELY(!NEI.MMRA)) {
13327 // No deep copy required for the types of extra info set.
13328 //
13329 // FIXME: Investigate if other types of extra info also need deep copy. This
13330 // depends on the types of nodes they can be attached to: if some extra info
13331 // is only ever attached to nodes where a replacement To node is always the
13332 // node where later use and propagation of the extra info has the intended
13333 // semantics, no deep copy is required.
13334 SDEI[To] = std::move(NEI);
13335 return;
13336 }
13337
13338 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
13339 // through the replacement of From with To. Otherwise, replacements of a node
13340 // (From) with more complex nodes (To and its operands) may result in lost
13341 // extra info where the root node (To) is insignificant in further propagating
13342 // and using extra info when further lowering to MIR.
13343 //
13344 // In the first step pre-populate the visited set with the nodes reachable
13345 // from the old From node. This avoids copying NodeExtraInfo to parts of the
13346 // DAG that is not new and should be left untouched.
13347 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
13348 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
13349 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
13350 if (MaxDepth == 0) {
13351 // Remember this node in case we need to increase MaxDepth and continue
13352 // populating FromReach from this node.
13353 Leafs.emplace_back(N);
13354 return;
13355 }
13356 if (!FromReach.insert(N).second)
13357 return;
13358 for (const SDValue &Op : N->op_values())
13359 Self(Self, Op.getNode(), MaxDepth - 1);
13360 };
13361
13362 // Copy extra info to To and all its transitive operands (that are new).
13364 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
13365 if (FromReach.contains(N))
13366 return true;
13367 if (!Visited.insert(N).second)
13368 return true;
13369 if (getEntryNode().getNode() == N)
13370 return false;
13371 for (const SDValue &Op : N->op_values()) {
13372 if (!Self(Self, Op.getNode()))
13373 return false;
13374 }
13375 // Copy only if entry node was not reached.
13376 SDEI[N] = NEI;
13377 return true;
13378 };
13379
13380 // We first try with a lower MaxDepth, assuming that the path to common
13381 // operands between From and To is relatively short. This significantly
13382 // improves performance in the common case. The initial MaxDepth is big
13383 // enough to avoid retry in the common case; the last MaxDepth is large
13384 // enough to avoid having to use the fallback below (and protects from
13385 // potential stack exhaustion from recursion).
13386 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13387 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13388 // StartFrom is the previous (or initial) set of leafs reachable at the
13389 // previous maximum depth.
13391 std::swap(StartFrom, Leafs);
13392 for (const SDNode *N : StartFrom)
13393 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13394 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13395 return;
13396 // This should happen very rarely (reached the entry node).
13397 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13398 assert(!Leafs.empty());
13399 }
13400
13401 // This should not happen - but if it did, that means the subgraph reachable
13402 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13403 // could not visit all reachable common operands. Consequently, we were able
13404 // to reach the entry node.
13405 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13406 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13407 // Best-effort fallback if assertions disabled.
13408 SDEI[To] = std::move(NEI);
13409}
13410
13411#ifndef NDEBUG
13412static void checkForCyclesHelper(const SDNode *N,
13415 const llvm::SelectionDAG *DAG) {
13416 // If this node has already been checked, don't check it again.
13417 if (Checked.count(N))
13418 return;
13419
13420 // If a node has already been visited on this depth-first walk, reject it as
13421 // a cycle.
13422 if (!Visited.insert(N).second) {
13423 errs() << "Detected cycle in SelectionDAG\n";
13424 dbgs() << "Offending node:\n";
13425 N->dumprFull(DAG); dbgs() << "\n";
13426 abort();
13427 }
13428
13429 for (const SDValue &Op : N->op_values())
13430 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13431
13432 Checked.insert(N);
13433 Visited.erase(N);
13434}
13435#endif
13436
13438 const llvm::SelectionDAG *DAG,
13439 bool force) {
13440#ifndef NDEBUG
13441 bool check = force;
13442#ifdef EXPENSIVE_CHECKS
13443 check = true;
13444#endif // EXPENSIVE_CHECKS
13445 if (check) {
13446 assert(N && "Checking nonexistent SDNode");
13449 checkForCyclesHelper(N, visited, checked, DAG);
13450 }
13451#endif // !NDEBUG
13452}
13453
13454void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13455 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13456}
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:464
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:236
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
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:212
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1385
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:207
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1498
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1370
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1627
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1364
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:1470
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:1308
APInt abs() const
Get the absolute value.
Definition: APInt.h:1751
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:349
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
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:1446
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1089
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
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:1375
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:812
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1144
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1596
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1585
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1555
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:197
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:1395
unsigned logBase2() const
Definition: APInt.h:1717
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:805
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1297
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:312
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1128
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:1345
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
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:1235
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:418
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1367
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:1215
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:367
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:264
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:836
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1199
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:63
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:195
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:846
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:838
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:738
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:842
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:160
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:189
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:195
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:382
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:226
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:567
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:489
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.
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=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:493
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:451
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:390
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:547
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:842
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:487
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 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 getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
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:876
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)
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:488
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:559
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:555
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:690
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:482
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:859
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:500
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:576
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:570
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:892
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:550
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:346
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:384
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:435
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:367
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:502
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:717
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void reserve(size_type N)
Definition: SmallVector.h:676
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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:2220
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:2215
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:1239
@ 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:1344
@ 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:1407
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
Definition: ISDOpcodes.h:1355
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1440
@ 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:1337
@ 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:1128
@ 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:1339
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1309
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1340
@ 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:1099
@ 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:1322
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:840
@ 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:1425
@ 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:1429
@ 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:870
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1439
@ 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:963
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1335
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:953
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:236
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1336
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:996
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1480
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1342
@ 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:1235
@ 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:1170
@ 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:1422
@ 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:1289
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1426
@ 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:980
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1343
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1145
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:334
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1338
@ 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:1305
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:218
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1441
@ 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:1345
@ 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:1434
@ 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:1334
@ 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:1333
@ 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:1041
@ 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:906
@ 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:1397
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1316
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1341
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:1028
@ 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:859
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:848
@ 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:1372
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:938
@ 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:1367
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1259
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1442
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:972
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1347
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1331
@ 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:1047
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1332
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:886
@ 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:1075
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1392
@ 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:1423
@ 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:1330
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:1001
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:919
@ 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:881
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:957
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Definition: ISDOpcodes.h:1471
@ 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:905
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1430
@ 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:1214
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1408
@ 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:1346
@ 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:1080
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
Definition: ISDOpcodes.h:1052
@ 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
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:1492
bool isExtOpcode(unsigned Opcode)
Definition: ISDOpcodes.h:1674
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:1661
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:1666
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).
std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
Definition: ISDOpcodes.h:1565
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:1552
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:1603
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:1583
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:1553
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:2431
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:2098
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:1535
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
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition: APFloat.h:1501
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 isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:260
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:1886
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:581
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
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition: APFloat.h:1527
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:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:381
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:137
intptr_t getRawBits() const
Definition: ValueTypes.h:498
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:74
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:275
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:291
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:341
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:359
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:350
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:371
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:307
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:65
bool isFixedLengthVector() const
Definition: ValueTypes.h:178
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:314
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:283
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:247
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:204
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:319
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:142
const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
Definition: ValueTypes.cpp:306
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:327
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:299
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:439
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:152
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:468
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:472
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:310
DAGUpdateListener *const Next
Definition: SelectionDAG.h:311
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)