LLVM 22.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"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <string>
80#include <utility>
81#include <vector>
82
83using namespace llvm;
84using namespace llvm::SDPatternMatch;
85
86/// makeVTList - Return an instance of the SDVTList struct initialized with the
87/// specified members.
88static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
89 SDVTList Res = {VTs, NumVTs};
90 return Res;
91}
92
93// Default null implementations of the callbacks.
97
98void SelectionDAG::DAGNodeDeletedListener::anchor() {}
99void SelectionDAG::DAGNodeInsertedListener::anchor() {}
100
101#define DEBUG_TYPE "selectiondag"
102
103static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
104 cl::Hidden, cl::init(true),
105 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
106
107static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
108 cl::desc("Number limit for gluing ld/st of memcpy."),
109 cl::Hidden, cl::init(0));
110
112 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
113 cl::desc("DAG combiner limit number of steps when searching DAG "
114 "for predecessor nodes"));
115
117 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
118}
119
121
122//===----------------------------------------------------------------------===//
123// ConstantFPSDNode Class
124//===----------------------------------------------------------------------===//
125
126/// isExactlyValue - We don't rely on operator== working on double values, as
127/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
128/// As such, this method can be used to do an exact bit-for-bit comparison of
129/// two floating point values.
131 return getValueAPF().bitwiseIsEqual(V);
132}
133
135 const APFloat& Val) {
136 assert(VT.isFloatingPoint() && "Can only convert between FP types");
137
138 // convert modifies in place, so make a copy.
139 APFloat Val2 = APFloat(Val);
140 bool losesInfo;
142 &losesInfo);
143 return !losesInfo;
144}
145
146//===----------------------------------------------------------------------===//
147// ISD Namespace
148//===----------------------------------------------------------------------===//
149
150bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
151 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
152 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
153 unsigned EltSize =
154 N->getValueType(0).getVectorElementType().getSizeInBits();
155 SplatVal = OptAPInt->trunc(EltSize);
156 return true;
157 }
158 }
159
160 auto *BV = dyn_cast<BuildVectorSDNode>(N);
161 if (!BV)
162 return false;
163
164 APInt SplatUndef;
165 unsigned SplatBitSize;
166 bool HasUndefs;
167 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
168 // Endianness does not matter here. We are checking for a splat given the
169 // element size of the vector, and if we find such a splat for little endian
170 // layout, then that should be valid also for big endian (as the full vector
171 // size is known to be a multiple of the element size).
172 const bool IsBigEndian = false;
173 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
174 EltSize, IsBigEndian) &&
175 EltSize == SplatBitSize;
176}
177
178// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
179// specializations of the more general isConstantSplatVector()?
180
181bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
182 // Look through a bit convert.
183 while (N->getOpcode() == ISD::BITCAST)
184 N = N->getOperand(0).getNode();
185
186 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
187 APInt SplatVal;
188 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
189 }
190
191 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
192
193 unsigned i = 0, e = N->getNumOperands();
194
195 // Skip over all of the undef values.
196 while (i != e && N->getOperand(i).isUndef())
197 ++i;
198
199 // Do not accept an all-undef vector.
200 if (i == e) return false;
201
202 // Do not accept build_vectors that aren't all constants or which have non-~0
203 // elements. We have to be a bit careful here, as the type of the constant
204 // may not be the same as the type of the vector elements due to type
205 // legalization (the elements are promoted to a legal type for the target and
206 // a vector of a type may be legal when the base element type is not).
207 // We only want to check enough bits to cover the vector elements, because
208 // we care if the resultant vector is all ones, not whether the individual
209 // constants are.
210 SDValue NotZero = N->getOperand(i);
211 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
212 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
213 if (OptAPInt->countr_one() < EltSize)
214 return false;
215 } else
216 return false;
217
218 // Okay, we have at least one ~0 value, check to see if the rest match or are
219 // undefs. Even with the above element type twiddling, this should be OK, as
220 // the same type legalization should have applied to all the elements.
221 for (++i; i != e; ++i)
222 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
223 return false;
224 return true;
225}
226
227bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
228 // Look through a bit convert.
229 while (N->getOpcode() == ISD::BITCAST)
230 N = N->getOperand(0).getNode();
231
232 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
233 APInt SplatVal;
234 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
235 }
236
237 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
238
239 bool IsAllUndef = true;
240 for (const SDValue &Op : N->op_values()) {
241 if (Op.isUndef())
242 continue;
243 IsAllUndef = false;
244 // Do not accept build_vectors that aren't all constants or which have non-0
245 // elements. We have to be a bit careful here, as the type of the constant
246 // may not be the same as the type of the vector elements due to type
247 // legalization (the elements are promoted to a legal type for the target
248 // and a vector of a type may be legal when the base element type is not).
249 // We only want to check enough bits to cover the vector elements, because
250 // we care if the resultant vector is all zeros, not whether the individual
251 // constants are.
252 if (auto OptAPInt = Op->bitcastToAPInt()) {
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (OptAPInt->countr_zero() < EltSize)
255 return false;
256 } else
257 return false;
258 }
259
260 // Do not accept an all-undef vector.
261 if (IsAllUndef)
262 return false;
263 return true;
264}
265
267 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
268}
269
271 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
272}
273
275 if (N->getOpcode() != ISD::BUILD_VECTOR)
276 return false;
277
278 for (const SDValue &Op : N->op_values()) {
279 if (Op.isUndef())
280 continue;
282 return false;
283 }
284 return true;
285}
286
288 if (N->getOpcode() != ISD::BUILD_VECTOR)
289 return false;
290
291 for (const SDValue &Op : N->op_values()) {
292 if (Op.isUndef())
293 continue;
295 return false;
296 }
297 return true;
298}
299
300bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
301 bool Signed) {
302 assert(N->getValueType(0).isVector() && "Expected a vector!");
303
304 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
305 if (EltSize <= NewEltSize)
306 return false;
307
308 if (N->getOpcode() == ISD::ZERO_EXTEND) {
309 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
310 NewEltSize) &&
311 !Signed;
312 }
313 if (N->getOpcode() == ISD::SIGN_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 Signed;
317 }
318 if (N->getOpcode() != ISD::BUILD_VECTOR)
319 return false;
320
321 for (const SDValue &Op : N->op_values()) {
322 if (Op.isUndef())
323 continue;
325 return false;
326
327 APInt C = Op->getAsAPIntVal().trunc(EltSize);
328 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
329 return false;
330 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
331 return false;
332 }
333
334 return true;
335}
336
338 // Return false if the node has no operands.
339 // This is "logically inconsistent" with the definition of "all" but
340 // is probably the desired behavior.
341 if (N->getNumOperands() == 0)
342 return false;
343 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
344}
345
347 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
348}
349
350template <typename ConstNodeType>
352 std::function<bool(ConstNodeType *)> Match,
353 bool AllowUndefs, bool AllowTruncation) {
354 // FIXME: Add support for scalar UNDEF cases?
355 if (auto *C = dyn_cast<ConstNodeType>(Op))
356 return Match(C);
357
358 // FIXME: Add support for vector UNDEF cases?
359 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
360 ISD::SPLAT_VECTOR != Op.getOpcode())
361 return false;
362
363 EVT SVT = Op.getValueType().getScalarType();
364 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
365 if (AllowUndefs && Op.getOperand(i).isUndef()) {
366 if (!Match(nullptr))
367 return false;
368 continue;
369 }
370
371 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
372 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
373 !Match(Cst))
374 return false;
375 }
376 return true;
377}
378// Build used template types.
380 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
382 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
383
385 SDValue LHS, SDValue RHS,
386 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
387 bool AllowUndefs, bool AllowTypeMismatch) {
388 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
389 return false;
390
391 // TODO: Add support for scalar UNDEF cases?
392 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
393 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
394 return Match(LHSCst, RHSCst);
395
396 // TODO: Add support for vector UNDEF cases?
397 if (LHS.getOpcode() != RHS.getOpcode() ||
398 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
399 LHS.getOpcode() != ISD::SPLAT_VECTOR))
400 return false;
401
402 EVT SVT = LHS.getValueType().getScalarType();
403 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
404 SDValue LHSOp = LHS.getOperand(i);
405 SDValue RHSOp = RHS.getOperand(i);
406 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
407 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
408 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
409 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
410 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
411 return false;
412 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
413 LHSOp.getValueType() != RHSOp.getValueType()))
414 return false;
415 if (!Match(LHSCst, RHSCst))
416 return false;
417 }
418 return true;
419}
420
422 switch (MinMaxOpc) {
423 default:
424 llvm_unreachable("unrecognized opcode");
425 case ISD::UMIN:
426 return ISD::UMAX;
427 case ISD::UMAX:
428 return ISD::UMIN;
429 case ISD::SMIN:
430 return ISD::SMAX;
431 case ISD::SMAX:
432 return ISD::SMIN;
433 }
434}
435
437 switch (VecReduceOpcode) {
438 default:
439 llvm_unreachable("Expected VECREDUCE opcode");
442 case ISD::VP_REDUCE_FADD:
443 case ISD::VP_REDUCE_SEQ_FADD:
444 return ISD::FADD;
447 case ISD::VP_REDUCE_FMUL:
448 case ISD::VP_REDUCE_SEQ_FMUL:
449 return ISD::FMUL;
451 case ISD::VP_REDUCE_ADD:
452 return ISD::ADD;
454 case ISD::VP_REDUCE_MUL:
455 return ISD::MUL;
457 case ISD::VP_REDUCE_AND:
458 return ISD::AND;
460 case ISD::VP_REDUCE_OR:
461 return ISD::OR;
463 case ISD::VP_REDUCE_XOR:
464 return ISD::XOR;
466 case ISD::VP_REDUCE_SMAX:
467 return ISD::SMAX;
469 case ISD::VP_REDUCE_SMIN:
470 return ISD::SMIN;
472 case ISD::VP_REDUCE_UMAX:
473 return ISD::UMAX;
475 case ISD::VP_REDUCE_UMIN:
476 return ISD::UMIN;
478 case ISD::VP_REDUCE_FMAX:
479 return ISD::FMAXNUM;
481 case ISD::VP_REDUCE_FMIN:
482 return ISD::FMINNUM;
484 case ISD::VP_REDUCE_FMAXIMUM:
485 return ISD::FMAXIMUM;
487 case ISD::VP_REDUCE_FMINIMUM:
488 return ISD::FMINIMUM;
489 }
490}
491
492bool ISD::isVPOpcode(unsigned Opcode) {
493 switch (Opcode) {
494 default:
495 return false;
496#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
497 case ISD::VPSD: \
498 return true;
499#include "llvm/IR/VPIntrinsics.def"
500 }
501}
502
503bool ISD::isVPBinaryOp(unsigned Opcode) {
504 switch (Opcode) {
505 default:
506 break;
507#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
508#define VP_PROPERTY_BINARYOP return true;
509#define END_REGISTER_VP_SDNODE(VPSD) break;
510#include "llvm/IR/VPIntrinsics.def"
511 }
512 return false;
513}
514
515bool ISD::isVPReduction(unsigned Opcode) {
516 switch (Opcode) {
517 default:
518 return false;
519 case ISD::VP_REDUCE_ADD:
520 case ISD::VP_REDUCE_MUL:
521 case ISD::VP_REDUCE_AND:
522 case ISD::VP_REDUCE_OR:
523 case ISD::VP_REDUCE_XOR:
524 case ISD::VP_REDUCE_SMAX:
525 case ISD::VP_REDUCE_SMIN:
526 case ISD::VP_REDUCE_UMAX:
527 case ISD::VP_REDUCE_UMIN:
528 case ISD::VP_REDUCE_FMAX:
529 case ISD::VP_REDUCE_FMIN:
530 case ISD::VP_REDUCE_FMAXIMUM:
531 case ISD::VP_REDUCE_FMINIMUM:
532 case ISD::VP_REDUCE_FADD:
533 case ISD::VP_REDUCE_FMUL:
534 case ISD::VP_REDUCE_SEQ_FADD:
535 case ISD::VP_REDUCE_SEQ_FMUL:
536 return true;
537 }
538}
539
540/// The operand position of the vector mask.
541std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
542 switch (Opcode) {
543 default:
544 return std::nullopt;
545#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
546 case ISD::VPSD: \
547 return MASKPOS;
548#include "llvm/IR/VPIntrinsics.def"
549 }
550}
551
552/// The operand position of the explicit vector length parameter.
553std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
554 switch (Opcode) {
555 default:
556 return std::nullopt;
557#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
558 case ISD::VPSD: \
559 return EVLPOS;
560#include "llvm/IR/VPIntrinsics.def"
561 }
562}
563
564std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
565 bool hasFPExcept) {
566 // FIXME: Return strict opcodes in case of fp exceptions.
567 switch (VPOpcode) {
568 default:
569 return std::nullopt;
570#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
571#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
572#define END_REGISTER_VP_SDNODE(VPOPC) break;
573#include "llvm/IR/VPIntrinsics.def"
574 }
575 return std::nullopt;
576}
577
578std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
579 switch (Opcode) {
580 default:
581 return std::nullopt;
582#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
583#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
584#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
585#include "llvm/IR/VPIntrinsics.def"
586 }
587}
588
590 switch (ExtType) {
591 case ISD::EXTLOAD:
592 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
593 case ISD::SEXTLOAD:
594 return ISD::SIGN_EXTEND;
595 case ISD::ZEXTLOAD:
596 return ISD::ZERO_EXTEND;
597 default:
598 break;
599 }
600
601 llvm_unreachable("Invalid LoadExtType");
602}
603
605 // To perform this operation, we just need to swap the L and G bits of the
606 // operation.
607 unsigned OldL = (Operation >> 2) & 1;
608 unsigned OldG = (Operation >> 1) & 1;
609 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
610 (OldL << 1) | // New G bit
611 (OldG << 2)); // New L bit.
612}
613
615 unsigned Operation = Op;
616 if (isIntegerLike)
617 Operation ^= 7; // Flip L, G, E bits, but not U.
618 else
619 Operation ^= 15; // Flip all of the condition bits.
620
622 Operation &= ~8; // Don't let N and U bits get set.
623
624 return ISD::CondCode(Operation);
625}
626
630
632 bool isIntegerLike) {
633 return getSetCCInverseImpl(Op, isIntegerLike);
634}
635
636/// For an integer comparison, return 1 if the comparison is a signed operation
637/// and 2 if the result is an unsigned comparison. Return zero if the operation
638/// does not depend on the sign of the input (setne and seteq).
639static int isSignedOp(ISD::CondCode Opcode) {
640 switch (Opcode) {
641 default: llvm_unreachable("Illegal integer setcc operation!");
642 case ISD::SETEQ:
643 case ISD::SETNE: return 0;
644 case ISD::SETLT:
645 case ISD::SETLE:
646 case ISD::SETGT:
647 case ISD::SETGE: return 1;
648 case ISD::SETULT:
649 case ISD::SETULE:
650 case ISD::SETUGT:
651 case ISD::SETUGE: return 2;
652 }
653}
654
656 EVT Type) {
657 bool IsInteger = Type.isInteger();
658 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
659 // Cannot fold a signed integer setcc with an unsigned integer setcc.
660 return ISD::SETCC_INVALID;
661
662 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
663
664 // If the N and U bits get set, then the resultant comparison DOES suddenly
665 // care about orderedness, and it is true when ordered.
666 if (Op > ISD::SETTRUE2)
667 Op &= ~16; // Clear the U bit if the N bit is set.
668
669 // Canonicalize illegal integer setcc's.
670 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
671 Op = ISD::SETNE;
672
673 return ISD::CondCode(Op);
674}
675
677 EVT Type) {
678 bool IsInteger = Type.isInteger();
679 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
680 // Cannot fold a signed setcc with an unsigned setcc.
681 return ISD::SETCC_INVALID;
682
683 // Combine all of the condition bits.
684 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
685
686 // Canonicalize illegal integer setcc's.
687 if (IsInteger) {
688 switch (Result) {
689 default: break;
690 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
691 case ISD::SETOEQ: // SETEQ & SETU[LG]E
692 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
693 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
694 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
695 }
696 }
697
698 return Result;
699}
700
701//===----------------------------------------------------------------------===//
702// SDNode Profile Support
703//===----------------------------------------------------------------------===//
704
705/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
706static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
707 ID.AddInteger(OpC);
708}
709
710/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
711/// solely with their pointer.
713 ID.AddPointer(VTList.VTs);
714}
715
716/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
719 for (const auto &Op : Ops) {
720 ID.AddPointer(Op.getNode());
721 ID.AddInteger(Op.getResNo());
722 }
723}
724
725/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
728 for (const auto &Op : Ops) {
729 ID.AddPointer(Op.getNode());
730 ID.AddInteger(Op.getResNo());
731 }
732}
733
734static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
735 SDVTList VTList, ArrayRef<SDValue> OpList) {
736 AddNodeIDOpcode(ID, OpC);
737 AddNodeIDValueTypes(ID, VTList);
738 AddNodeIDOperands(ID, OpList);
739}
740
741/// If this is an SDNode with special info, add this info to the NodeID data.
743 switch (N->getOpcode()) {
746 case ISD::MCSymbol:
747 llvm_unreachable("Should only be used on nodes with operands");
748 default: break; // Normal nodes don't need extra info.
750 case ISD::Constant: {
752 ID.AddPointer(C->getConstantIntValue());
753 ID.AddBoolean(C->isOpaque());
754 break;
755 }
757 case ISD::ConstantFP:
758 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
759 break;
765 ID.AddPointer(GA->getGlobal());
766 ID.AddInteger(GA->getOffset());
767 ID.AddInteger(GA->getTargetFlags());
768 break;
769 }
770 case ISD::BasicBlock:
772 break;
773 case ISD::Register:
774 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
775 break;
777 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
778 break;
779 case ISD::SRCVALUE:
780 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
781 break;
782 case ISD::FrameIndex:
784 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
785 break;
787 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
788 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
789 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
790 break;
791 case ISD::JumpTable:
793 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
794 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
795 break;
799 ID.AddInteger(CP->getAlign().value());
800 ID.AddInteger(CP->getOffset());
803 else
804 ID.AddPointer(CP->getConstVal());
805 ID.AddInteger(CP->getTargetFlags());
806 break;
807 }
808 case ISD::TargetIndex: {
810 ID.AddInteger(TI->getIndex());
811 ID.AddInteger(TI->getOffset());
812 ID.AddInteger(TI->getTargetFlags());
813 break;
814 }
815 case ISD::LOAD: {
816 const LoadSDNode *LD = cast<LoadSDNode>(N);
817 ID.AddInteger(LD->getMemoryVT().getRawBits());
818 ID.AddInteger(LD->getRawSubclassData());
819 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
820 ID.AddInteger(LD->getMemOperand()->getFlags());
821 break;
822 }
823 case ISD::STORE: {
824 const StoreSDNode *ST = cast<StoreSDNode>(N);
825 ID.AddInteger(ST->getMemoryVT().getRawBits());
826 ID.AddInteger(ST->getRawSubclassData());
827 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
828 ID.AddInteger(ST->getMemOperand()->getFlags());
829 break;
830 }
831 case ISD::VP_LOAD: {
832 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
833 ID.AddInteger(ELD->getMemoryVT().getRawBits());
834 ID.AddInteger(ELD->getRawSubclassData());
835 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
836 ID.AddInteger(ELD->getMemOperand()->getFlags());
837 break;
838 }
839 case ISD::VP_LOAD_FF: {
840 const auto *LD = cast<VPLoadFFSDNode>(N);
841 ID.AddInteger(LD->getMemoryVT().getRawBits());
842 ID.AddInteger(LD->getRawSubclassData());
843 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
844 ID.AddInteger(LD->getMemOperand()->getFlags());
845 break;
846 }
847 case ISD::VP_STORE: {
848 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
849 ID.AddInteger(EST->getMemoryVT().getRawBits());
850 ID.AddInteger(EST->getRawSubclassData());
851 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
852 ID.AddInteger(EST->getMemOperand()->getFlags());
853 break;
854 }
855 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
857 ID.AddInteger(SLD->getMemoryVT().getRawBits());
858 ID.AddInteger(SLD->getRawSubclassData());
859 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
860 break;
861 }
862 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
864 ID.AddInteger(SST->getMemoryVT().getRawBits());
865 ID.AddInteger(SST->getRawSubclassData());
866 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
867 break;
868 }
869 case ISD::VP_GATHER: {
871 ID.AddInteger(EG->getMemoryVT().getRawBits());
872 ID.AddInteger(EG->getRawSubclassData());
873 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
874 ID.AddInteger(EG->getMemOperand()->getFlags());
875 break;
876 }
877 case ISD::VP_SCATTER: {
879 ID.AddInteger(ES->getMemoryVT().getRawBits());
880 ID.AddInteger(ES->getRawSubclassData());
881 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
882 ID.AddInteger(ES->getMemOperand()->getFlags());
883 break;
884 }
885 case ISD::MLOAD: {
887 ID.AddInteger(MLD->getMemoryVT().getRawBits());
888 ID.AddInteger(MLD->getRawSubclassData());
889 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
890 ID.AddInteger(MLD->getMemOperand()->getFlags());
891 break;
892 }
893 case ISD::MSTORE: {
895 ID.AddInteger(MST->getMemoryVT().getRawBits());
896 ID.AddInteger(MST->getRawSubclassData());
897 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
898 ID.AddInteger(MST->getMemOperand()->getFlags());
899 break;
900 }
901 case ISD::MGATHER: {
903 ID.AddInteger(MG->getMemoryVT().getRawBits());
904 ID.AddInteger(MG->getRawSubclassData());
905 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
906 ID.AddInteger(MG->getMemOperand()->getFlags());
907 break;
908 }
909 case ISD::MSCATTER: {
911 ID.AddInteger(MS->getMemoryVT().getRawBits());
912 ID.AddInteger(MS->getRawSubclassData());
913 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
914 ID.AddInteger(MS->getMemOperand()->getFlags());
915 break;
916 }
919 case ISD::ATOMIC_SWAP:
931 case ISD::ATOMIC_LOAD:
932 case ISD::ATOMIC_STORE: {
933 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
934 ID.AddInteger(AT->getMemoryVT().getRawBits());
935 ID.AddInteger(AT->getRawSubclassData());
936 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
937 ID.AddInteger(AT->getMemOperand()->getFlags());
938 break;
939 }
940 case ISD::VECTOR_SHUFFLE: {
941 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
942 for (int M : Mask)
943 ID.AddInteger(M);
944 break;
945 }
946 case ISD::ADDRSPACECAST: {
948 ID.AddInteger(ASC->getSrcAddressSpace());
949 ID.AddInteger(ASC->getDestAddressSpace());
950 break;
951 }
953 case ISD::BlockAddress: {
955 ID.AddPointer(BA->getBlockAddress());
956 ID.AddInteger(BA->getOffset());
957 ID.AddInteger(BA->getTargetFlags());
958 break;
959 }
960 case ISD::AssertAlign:
961 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
962 break;
963 case ISD::PREFETCH:
966 // Handled by MemIntrinsicSDNode check after the switch.
967 break;
969 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
970 break;
971 } // end switch (N->getOpcode())
972
973 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
974 // to check.
975 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
976 ID.AddInteger(MN->getRawSubclassData());
977 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
978 ID.AddInteger(MN->getMemOperand()->getFlags());
979 ID.AddInteger(MN->getMemoryVT().getRawBits());
980 }
981}
982
983/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
984/// data.
985static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
986 AddNodeIDOpcode(ID, N->getOpcode());
987 // Add the return value info.
988 AddNodeIDValueTypes(ID, N->getVTList());
989 // Add the operand info.
990 AddNodeIDOperands(ID, N->ops());
991
992 // Handle SDNode leafs with special info.
994}
995
996//===----------------------------------------------------------------------===//
997// SelectionDAG Class
998//===----------------------------------------------------------------------===//
999
1000/// doNotCSE - Return true if CSE should not be performed for this node.
1001static bool doNotCSE(SDNode *N) {
1002 if (N->getValueType(0) == MVT::Glue)
1003 return true; // Never CSE anything that produces a glue result.
1004
1005 switch (N->getOpcode()) {
1006 default: break;
1007 case ISD::HANDLENODE:
1008 case ISD::EH_LABEL:
1009 return true; // Never CSE these nodes.
1010 }
1011
1012 // Check that remaining values produced are not flags.
1013 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1014 if (N->getValueType(i) == MVT::Glue)
1015 return true; // Never CSE anything that produces a glue result.
1016
1017 return false;
1018}
1019
1020/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1021/// SelectionDAG.
1023 // Create a dummy node (which is not added to allnodes), that adds a reference
1024 // to the root node, preventing it from being deleted.
1025 HandleSDNode Dummy(getRoot());
1026
1027 SmallVector<SDNode*, 128> DeadNodes;
1028
1029 // Add all obviously-dead nodes to the DeadNodes worklist.
1030 for (SDNode &Node : allnodes())
1031 if (Node.use_empty())
1032 DeadNodes.push_back(&Node);
1033
1034 RemoveDeadNodes(DeadNodes);
1035
1036 // If the root changed (e.g. it was a dead load, update the root).
1037 setRoot(Dummy.getValue());
1038}
1039
1040/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1041/// given list, and any nodes that become unreachable as a result.
1043
1044 // Process the worklist, deleting the nodes and adding their uses to the
1045 // worklist.
1046 while (!DeadNodes.empty()) {
1047 SDNode *N = DeadNodes.pop_back_val();
1048 // Skip to next node if we've already managed to delete the node. This could
1049 // happen if replacing a node causes a node previously added to the node to
1050 // be deleted.
1051 if (N->getOpcode() == ISD::DELETED_NODE)
1052 continue;
1053
1054 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1055 DUL->NodeDeleted(N, nullptr);
1056
1057 // Take the node out of the appropriate CSE map.
1058 RemoveNodeFromCSEMaps(N);
1059
1060 // Next, brutally remove the operand list. This is safe to do, as there are
1061 // no cycles in the graph.
1062 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1063 SDUse &Use = *I++;
1064 SDNode *Operand = Use.getNode();
1065 Use.set(SDValue());
1066
1067 // Now that we removed this operand, see if there are no uses of it left.
1068 if (Operand->use_empty())
1069 DeadNodes.push_back(Operand);
1070 }
1071
1072 DeallocateNode(N);
1073 }
1074}
1075
1077 SmallVector<SDNode*, 16> DeadNodes(1, N);
1078
1079 // Create a dummy node that adds a reference to the root node, preventing
1080 // it from being deleted. (This matters if the root is an operand of the
1081 // dead node.)
1082 HandleSDNode Dummy(getRoot());
1083
1084 RemoveDeadNodes(DeadNodes);
1085}
1086
1088 // First take this out of the appropriate CSE map.
1089 RemoveNodeFromCSEMaps(N);
1090
1091 // Finally, remove uses due to operands of this node, remove from the
1092 // AllNodes list, and delete the node.
1093 DeleteNodeNotInCSEMaps(N);
1094}
1095
1096void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1097 assert(N->getIterator() != AllNodes.begin() &&
1098 "Cannot delete the entry node!");
1099 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1100
1101 // Drop all of the operands and decrement used node's use counts.
1102 N->DropOperands();
1103
1104 DeallocateNode(N);
1105}
1106
1107void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1108 assert(!(V->isVariadic() && isParameter));
1109 if (isParameter)
1110 ByvalParmDbgValues.push_back(V);
1111 else
1112 DbgValues.push_back(V);
1113 for (const SDNode *Node : V->getSDNodes())
1114 if (Node)
1115 DbgValMap[Node].push_back(V);
1116}
1117
1119 DbgValMapType::iterator I = DbgValMap.find(Node);
1120 if (I == DbgValMap.end())
1121 return;
1122 for (auto &Val: I->second)
1123 Val->setIsInvalidated();
1124 DbgValMap.erase(I);
1125}
1126
1127void SelectionDAG::DeallocateNode(SDNode *N) {
1128 // If we have operands, deallocate them.
1130
1131 NodeAllocator.Deallocate(AllNodes.remove(N));
1132
1133 // Set the opcode to DELETED_NODE to help catch bugs when node
1134 // memory is reallocated.
1135 // FIXME: There are places in SDag that have grown a dependency on the opcode
1136 // value in the released node.
1137 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1138 N->NodeType = ISD::DELETED_NODE;
1139
1140 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1141 // them and forget about that node.
1142 DbgInfo->erase(N);
1143
1144 // Invalidate extra info.
1145 SDEI.erase(N);
1146}
1147
1148#ifndef NDEBUG
1149/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1150void SelectionDAG::verifyNode(SDNode *N) const {
1151 switch (N->getOpcode()) {
1152 default:
1153 if (N->isTargetOpcode())
1155 break;
1156 case ISD::BUILD_PAIR: {
1157 EVT VT = N->getValueType(0);
1158 assert(N->getNumValues() == 1 && "Too many results!");
1159 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1160 "Wrong return type!");
1161 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1162 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1163 "Mismatched operand types!");
1164 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1165 "Wrong operand type!");
1166 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1167 "Wrong return type size");
1168 break;
1169 }
1170 case ISD::BUILD_VECTOR: {
1171 assert(N->getNumValues() == 1 && "Too many results!");
1172 assert(N->getValueType(0).isVector() && "Wrong return type!");
1173 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1174 "Wrong number of operands!");
1175 EVT EltVT = N->getValueType(0).getVectorElementType();
1176 for (const SDUse &Op : N->ops()) {
1177 assert((Op.getValueType() == EltVT ||
1178 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1179 EltVT.bitsLE(Op.getValueType()))) &&
1180 "Wrong operand type!");
1181 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1182 "Operands must all have the same type");
1183 }
1184 break;
1185 }
1186 }
1187}
1188#endif // NDEBUG
1189
1190/// Insert a newly allocated node into the DAG.
1191///
1192/// Handles insertion into the all nodes list and CSE map, as well as
1193/// verification and other common operations when a new node is allocated.
1194void SelectionDAG::InsertNode(SDNode *N) {
1195 AllNodes.push_back(N);
1196#ifndef NDEBUG
1197 N->PersistentId = NextPersistentId++;
1198 verifyNode(N);
1199#endif
1200 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1201 DUL->NodeInserted(N);
1202}
1203
1204/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1205/// correspond to it. This is useful when we're about to delete or repurpose
1206/// the node. We don't want future request for structurally identical nodes
1207/// to return N anymore.
1208bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1209 bool Erased = false;
1210 switch (N->getOpcode()) {
1211 case ISD::HANDLENODE: return false; // noop.
1212 case ISD::CONDCODE:
1213 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1214 "Cond code doesn't exist!");
1215 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1216 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1217 break;
1219 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1220 break;
1222 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1223 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1224 ESN->getSymbol(), ESN->getTargetFlags()));
1225 break;
1226 }
1227 case ISD::MCSymbol: {
1228 auto *MCSN = cast<MCSymbolSDNode>(N);
1229 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1230 break;
1231 }
1232 case ISD::VALUETYPE: {
1233 EVT VT = cast<VTSDNode>(N)->getVT();
1234 if (VT.isExtended()) {
1235 Erased = ExtendedValueTypeNodes.erase(VT);
1236 } else {
1237 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1238 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1239 }
1240 break;
1241 }
1242 default:
1243 // Remove it from the CSE Map.
1244 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1245 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1246 Erased = CSEMap.RemoveNode(N);
1247 break;
1248 }
1249#ifndef NDEBUG
1250 // Verify that the node was actually in one of the CSE maps, unless it has a
1251 // glue result (which cannot be CSE'd) or is one of the special cases that are
1252 // not subject to CSE.
1253 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1254 !N->isMachineOpcode() && !doNotCSE(N)) {
1255 N->dump(this);
1256 dbgs() << "\n";
1257 llvm_unreachable("Node is not in map!");
1258 }
1259#endif
1260 return Erased;
1261}
1262
1263/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1264/// maps and modified in place. Add it back to the CSE maps, unless an identical
1265/// node already exists, in which case transfer all its users to the existing
1266/// node. This transfer can potentially trigger recursive merging.
1267void
1268SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1269 // For node types that aren't CSE'd, just act as if no identical node
1270 // already exists.
1271 if (!doNotCSE(N)) {
1272 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1273 if (Existing != N) {
1274 // If there was already an existing matching node, use ReplaceAllUsesWith
1275 // to replace the dead one with the existing one. This can cause
1276 // recursive merging of other unrelated nodes down the line.
1277 Existing->intersectFlagsWith(N->getFlags());
1278 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1279 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1280 ReplaceAllUsesWith(N, Existing);
1281
1282 // N is now dead. Inform the listeners and delete it.
1283 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1284 DUL->NodeDeleted(N, Existing);
1285 DeleteNodeNotInCSEMaps(N);
1286 return;
1287 }
1288 }
1289
1290 // If the node doesn't already exist, we updated it. Inform listeners.
1291 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1292 DUL->NodeUpdated(N);
1293}
1294
1295/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1296/// were replaced with those specified. If this node is never memoized,
1297/// return null, otherwise return a pointer to the slot it would take. If a
1298/// node already exists with these operands, the slot will be non-null.
1299SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1300 void *&InsertPos) {
1301 if (doNotCSE(N))
1302 return nullptr;
1303
1304 SDValue Ops[] = { Op };
1305 FoldingSetNodeID ID;
1306 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1308 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1309 if (Node)
1310 Node->intersectFlagsWith(N->getFlags());
1311 return Node;
1312}
1313
1314/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1315/// were replaced with those specified. If this node is never memoized,
1316/// return null, otherwise return a pointer to the slot it would take. If a
1317/// node already exists with these operands, the slot will be non-null.
1318SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1319 SDValue Op1, SDValue Op2,
1320 void *&InsertPos) {
1321 if (doNotCSE(N))
1322 return nullptr;
1323
1324 SDValue Ops[] = { Op1, Op2 };
1325 FoldingSetNodeID ID;
1326 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1328 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1329 if (Node)
1330 Node->intersectFlagsWith(N->getFlags());
1331 return Node;
1332}
1333
1334/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1335/// were replaced with those specified. If this node is never memoized,
1336/// return null, otherwise return a pointer to the slot it would take. If a
1337/// node already exists with these operands, the slot will be non-null.
1338SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1339 void *&InsertPos) {
1340 if (doNotCSE(N))
1341 return nullptr;
1342
1343 FoldingSetNodeID ID;
1344 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1346 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1347 if (Node)
1348 Node->intersectFlagsWith(N->getFlags());
1349 return Node;
1350}
1351
1353 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1354 : VT.getTypeForEVT(*getContext());
1355
1356 return getDataLayout().getABITypeAlign(Ty);
1357}
1358
1359// EntryNode could meaningfully have debug info if we can find it...
1361 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1362 getVTList(MVT::Other, MVT::Glue)),
1363 Root(getEntryNode()) {
1364 InsertNode(&EntryNode);
1365 DbgInfo = new SDDbgInfo();
1366}
1367
1369 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1370 const TargetLibraryInfo *LibraryInfo,
1371 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1373 FunctionVarLocs const *VarLocs) {
1374 MF = &NewMF;
1375 SDAGISelPass = PassPtr;
1376 ORE = &NewORE;
1379 LibInfo = LibraryInfo;
1380 Context = &MF->getFunction().getContext();
1381 UA = NewUA;
1382 PSI = PSIin;
1383 BFI = BFIin;
1384 MMI = &MMIin;
1385 FnVarLocs = VarLocs;
1386}
1387
1389 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1390 allnodes_clear();
1391 OperandRecycler.clear(OperandAllocator);
1392 delete DbgInfo;
1393}
1394
1396 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1397}
1398
1399void SelectionDAG::allnodes_clear() {
1400 assert(&*AllNodes.begin() == &EntryNode);
1401 AllNodes.remove(AllNodes.begin());
1402 while (!AllNodes.empty())
1403 DeallocateNode(&AllNodes.front());
1404#ifndef NDEBUG
1405 NextPersistentId = 0;
1406#endif
1407}
1408
1409SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1410 void *&InsertPos) {
1411 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1412 if (N) {
1413 switch (N->getOpcode()) {
1414 default: break;
1415 case ISD::Constant:
1416 case ISD::ConstantFP:
1417 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1418 "debug location. Use another overload.");
1419 }
1420 }
1421 return N;
1422}
1423
1424SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1425 const SDLoc &DL, void *&InsertPos) {
1426 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1427 if (N) {
1428 switch (N->getOpcode()) {
1429 case ISD::Constant:
1430 case ISD::ConstantFP:
1431 // Erase debug location from the node if the node is used at several
1432 // different places. Do not propagate one location to all uses as it
1433 // will cause a worse single stepping debugging experience.
1434 if (N->getDebugLoc() != DL.getDebugLoc())
1435 N->setDebugLoc(DebugLoc());
1436 break;
1437 default:
1438 // When the node's point of use is located earlier in the instruction
1439 // sequence than its prior point of use, update its debug info to the
1440 // earlier location.
1441 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1442 N->setDebugLoc(DL.getDebugLoc());
1443 break;
1444 }
1445 }
1446 return N;
1447}
1448
1450 allnodes_clear();
1451 OperandRecycler.clear(OperandAllocator);
1452 OperandAllocator.Reset();
1453 CSEMap.clear();
1454
1455 ExtendedValueTypeNodes.clear();
1456 ExternalSymbols.clear();
1457 TargetExternalSymbols.clear();
1458 MCSymbols.clear();
1459 SDEI.clear();
1460 llvm::fill(CondCodeNodes, nullptr);
1461 llvm::fill(ValueTypeNodes, nullptr);
1462
1463 EntryNode.UseList = nullptr;
1464 InsertNode(&EntryNode);
1465 Root = getEntryNode();
1466 DbgInfo->clear();
1467}
1468
1470 return VT.bitsGT(Op.getValueType())
1471 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1472 : getNode(ISD::FP_ROUND, DL, VT, Op,
1473 getIntPtrConstant(0, DL, /*isTarget=*/true));
1474}
1475
1476std::pair<SDValue, SDValue>
1478 const SDLoc &DL, EVT VT) {
1479 assert(!VT.bitsEq(Op.getValueType()) &&
1480 "Strict no-op FP extend/round not allowed.");
1481 SDValue Res =
1482 VT.bitsGT(Op.getValueType())
1483 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1484 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1485 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1486
1487 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1488}
1489
1491 return VT.bitsGT(Op.getValueType()) ?
1492 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1493 getNode(ISD::TRUNCATE, DL, VT, Op);
1494}
1495
1497 return VT.bitsGT(Op.getValueType()) ?
1498 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1499 getNode(ISD::TRUNCATE, DL, VT, Op);
1500}
1501
1503 return VT.bitsGT(Op.getValueType()) ?
1504 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1505 getNode(ISD::TRUNCATE, DL, VT, Op);
1506}
1507
1509 EVT VT) {
1510 assert(!VT.isVector());
1511 auto Type = Op.getValueType();
1512 SDValue DestOp;
1513 if (Type == VT)
1514 return Op;
1515 auto Size = Op.getValueSizeInBits();
1516 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1517 if (DestOp.getValueType() == VT)
1518 return DestOp;
1519
1520 return getAnyExtOrTrunc(DestOp, DL, VT);
1521}
1522
1524 EVT VT) {
1525 assert(!VT.isVector());
1526 auto Type = Op.getValueType();
1527 SDValue DestOp;
1528 if (Type == VT)
1529 return Op;
1530 auto Size = Op.getValueSizeInBits();
1531 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1532 if (DestOp.getValueType() == VT)
1533 return DestOp;
1534
1535 return getSExtOrTrunc(DestOp, DL, VT);
1536}
1537
1539 EVT VT) {
1540 assert(!VT.isVector());
1541 auto Type = Op.getValueType();
1542 SDValue DestOp;
1543 if (Type == VT)
1544 return Op;
1545 auto Size = Op.getValueSizeInBits();
1546 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1547 if (DestOp.getValueType() == VT)
1548 return DestOp;
1549
1550 return getZExtOrTrunc(DestOp, DL, VT);
1551}
1552
1554 EVT OpVT) {
1555 if (VT.bitsLE(Op.getValueType()))
1556 return getNode(ISD::TRUNCATE, SL, VT, Op);
1557
1558 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1559 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1560}
1561
1563 EVT OpVT = Op.getValueType();
1564 assert(VT.isInteger() && OpVT.isInteger() &&
1565 "Cannot getZeroExtendInReg FP types");
1566 assert(VT.isVector() == OpVT.isVector() &&
1567 "getZeroExtendInReg type should be vector iff the operand "
1568 "type is vector!");
1569 assert((!VT.isVector() ||
1571 "Vector element counts must match in getZeroExtendInReg");
1572 assert(VT.bitsLE(OpVT) && "Not extending!");
1573 if (OpVT == VT)
1574 return Op;
1576 VT.getScalarSizeInBits());
1577 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1578}
1579
1581 SDValue EVL, const SDLoc &DL,
1582 EVT VT) {
1583 EVT OpVT = Op.getValueType();
1584 assert(VT.isInteger() && OpVT.isInteger() &&
1585 "Cannot getVPZeroExtendInReg FP types");
1586 assert(VT.isVector() && OpVT.isVector() &&
1587 "getVPZeroExtendInReg type and operand type should be vector!");
1589 "Vector element counts must match in getZeroExtendInReg");
1590 assert(VT.bitsLE(OpVT) && "Not extending!");
1591 if (OpVT == VT)
1592 return Op;
1594 VT.getScalarSizeInBits());
1595 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1596 EVL);
1597}
1598
1600 // Only unsigned pointer semantics are supported right now. In the future this
1601 // might delegate to TLI to check pointer signedness.
1602 return getZExtOrTrunc(Op, DL, VT);
1603}
1604
1606 // Only unsigned pointer semantics are supported right now. In the future this
1607 // might delegate to TLI to check pointer signedness.
1608 return getZeroExtendInReg(Op, DL, VT);
1609}
1610
1612 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1613}
1614
1615/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1617 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1618}
1619
1621 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1622 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1623}
1624
1626 SDValue Mask, SDValue EVL, EVT VT) {
1627 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1628 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1629}
1630
1632 SDValue Mask, SDValue EVL) {
1633 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1634}
1635
1637 SDValue Mask, SDValue EVL) {
1638 if (VT.bitsGT(Op.getValueType()))
1639 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1640 if (VT.bitsLT(Op.getValueType()))
1641 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1642 return Op;
1643}
1644
1646 EVT OpVT) {
1647 if (!V)
1648 return getConstant(0, DL, VT);
1649
1650 switch (TLI->getBooleanContents(OpVT)) {
1653 return getConstant(1, DL, VT);
1655 return getAllOnesConstant(DL, VT);
1656 }
1657 llvm_unreachable("Unexpected boolean content enum!");
1658}
1659
1661 bool isT, bool isO) {
1662 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1663 DL, VT, isT, isO);
1664}
1665
1667 bool isT, bool isO) {
1668 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1669}
1670
1672 EVT VT, bool isT, bool isO) {
1673 assert(VT.isInteger() && "Cannot create FP integer constant!");
1674
1675 EVT EltVT = VT.getScalarType();
1676 const ConstantInt *Elt = &Val;
1677
1678 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1679 // to-be-splatted scalar ConstantInt.
1680 if (isa<VectorType>(Elt->getType()))
1681 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1682
1683 // In some cases the vector type is legal but the element type is illegal and
1684 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1685 // inserted value (the type does not need to match the vector element type).
1686 // Any extra bits introduced will be truncated away.
1687 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1689 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1690 APInt NewVal;
1691 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1692 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1693 else
1694 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1695 Elt = ConstantInt::get(*getContext(), NewVal);
1696 }
1697 // In other cases the element type is illegal and needs to be expanded, for
1698 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1699 // the value into n parts and use a vector type with n-times the elements.
1700 // Then bitcast to the type requested.
1701 // Legalizing constants too early makes the DAGCombiner's job harder so we
1702 // only legalize if the DAG tells us we must produce legal types.
1703 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1704 TLI->getTypeAction(*getContext(), EltVT) ==
1706 const APInt &NewVal = Elt->getValue();
1707 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1708 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1709
1710 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1711 if (VT.isScalableVector() ||
1712 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1713 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1714 "Can only handle an even split!");
1715 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1716
1717 SmallVector<SDValue, 2> ScalarParts;
1718 for (unsigned i = 0; i != Parts; ++i)
1719 ScalarParts.push_back(getConstant(
1720 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1721 ViaEltVT, isT, isO));
1722
1723 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1724 }
1725
1726 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1727 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1728
1729 // Check the temporary vector is the correct size. If this fails then
1730 // getTypeToTransformTo() probably returned a type whose size (in bits)
1731 // isn't a power-of-2 factor of the requested type size.
1732 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1733
1734 SmallVector<SDValue, 2> EltParts;
1735 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1736 EltParts.push_back(getConstant(
1737 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1738 ViaEltVT, isT, isO));
1739
1740 // EltParts is currently in little endian order. If we actually want
1741 // big-endian order then reverse it now.
1742 if (getDataLayout().isBigEndian())
1743 std::reverse(EltParts.begin(), EltParts.end());
1744
1745 // The elements must be reversed when the element order is different
1746 // to the endianness of the elements (because the BITCAST is itself a
1747 // vector shuffle in this situation). However, we do not need any code to
1748 // perform this reversal because getConstant() is producing a vector
1749 // splat.
1750 // This situation occurs in MIPS MSA.
1751
1753 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1754 llvm::append_range(Ops, EltParts);
1755
1756 SDValue V =
1757 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1758 return V;
1759 }
1760
1761 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1762 "APInt size does not match type size!");
1763 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1764 SDVTList VTs = getVTList(EltVT);
1766 AddNodeIDNode(ID, Opc, VTs, {});
1767 ID.AddPointer(Elt);
1768 ID.AddBoolean(isO);
1769 void *IP = nullptr;
1770 SDNode *N = nullptr;
1771 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1772 if (!VT.isVector())
1773 return SDValue(N, 0);
1774
1775 if (!N) {
1776 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1777 CSEMap.InsertNode(N, IP);
1778 InsertNode(N);
1779 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1780 }
1781
1782 SDValue Result(N, 0);
1783 if (VT.isVector())
1784 Result = getSplat(VT, DL, Result);
1785 return Result;
1786}
1787
1789 bool isT, bool isO) {
1790 unsigned Size = VT.getScalarSizeInBits();
1791 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1792}
1793
1795 bool IsOpaque) {
1797 IsTarget, IsOpaque);
1798}
1799
1801 bool isTarget) {
1802 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1803}
1804
1806 const SDLoc &DL) {
1807 assert(VT.isInteger() && "Shift amount is not an integer type!");
1808 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1809 return getConstant(Val, DL, ShiftVT);
1810}
1811
1813 const SDLoc &DL) {
1814 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1815 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1816}
1817
1819 bool isTarget) {
1820 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1821}
1822
1824 bool isTarget) {
1825 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1826}
1827
1829 EVT VT, bool isTarget) {
1830 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1831
1832 EVT EltVT = VT.getScalarType();
1833 const ConstantFP *Elt = &V;
1834
1835 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1836 // the to-be-splatted scalar ConstantFP.
1837 if (isa<VectorType>(Elt->getType()))
1838 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1839
1840 // Do the map lookup using the actual bit pattern for the floating point
1841 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1842 // we don't have issues with SNANs.
1843 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1844 SDVTList VTs = getVTList(EltVT);
1846 AddNodeIDNode(ID, Opc, VTs, {});
1847 ID.AddPointer(Elt);
1848 void *IP = nullptr;
1849 SDNode *N = nullptr;
1850 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1851 if (!VT.isVector())
1852 return SDValue(N, 0);
1853
1854 if (!N) {
1855 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1856 CSEMap.InsertNode(N, IP);
1857 InsertNode(N);
1858 }
1859
1860 SDValue Result(N, 0);
1861 if (VT.isVector())
1862 Result = getSplat(VT, DL, Result);
1863 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1864 return Result;
1865}
1866
1868 bool isTarget) {
1869 EVT EltVT = VT.getScalarType();
1870 if (EltVT == MVT::f32)
1871 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1872 if (EltVT == MVT::f64)
1873 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1874 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1875 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1876 bool Ignored;
1877 APFloat APF = APFloat(Val);
1879 &Ignored);
1880 return getConstantFP(APF, DL, VT, isTarget);
1881 }
1882 llvm_unreachable("Unsupported type in getConstantFP");
1883}
1884
1886 EVT VT, int64_t Offset, bool isTargetGA,
1887 unsigned TargetFlags) {
1888 assert((TargetFlags == 0 || isTargetGA) &&
1889 "Cannot set target flags on target-independent globals");
1890
1891 // Truncate (with sign-extension) the offset value to the pointer size.
1893 if (BitWidth < 64)
1895
1896 unsigned Opc;
1897 if (GV->isThreadLocal())
1899 else
1901
1902 SDVTList VTs = getVTList(VT);
1904 AddNodeIDNode(ID, Opc, VTs, {});
1905 ID.AddPointer(GV);
1906 ID.AddInteger(Offset);
1907 ID.AddInteger(TargetFlags);
1908 void *IP = nullptr;
1909 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1910 return SDValue(E, 0);
1911
1912 auto *N = newSDNode<GlobalAddressSDNode>(
1913 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1914 CSEMap.InsertNode(N, IP);
1915 InsertNode(N);
1916 return SDValue(N, 0);
1917}
1918
1920 SDVTList VTs = getVTList(MVT::Untyped);
1923 ID.AddPointer(GV);
1924 void *IP = nullptr;
1925 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1926 return SDValue(E, 0);
1927
1928 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1929 CSEMap.InsertNode(N, IP);
1930 InsertNode(N);
1931 return SDValue(N, 0);
1932}
1933
1934SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1935 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1936 SDVTList VTs = getVTList(VT);
1938 AddNodeIDNode(ID, Opc, VTs, {});
1939 ID.AddInteger(FI);
1940 void *IP = nullptr;
1941 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1942 return SDValue(E, 0);
1943
1944 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1945 CSEMap.InsertNode(N, IP);
1946 InsertNode(N);
1947 return SDValue(N, 0);
1948}
1949
1950SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1951 unsigned TargetFlags) {
1952 assert((TargetFlags == 0 || isTarget) &&
1953 "Cannot set target flags on target-independent jump tables");
1954 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1955 SDVTList VTs = getVTList(VT);
1957 AddNodeIDNode(ID, Opc, VTs, {});
1958 ID.AddInteger(JTI);
1959 ID.AddInteger(TargetFlags);
1960 void *IP = nullptr;
1961 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1962 return SDValue(E, 0);
1963
1964 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1965 CSEMap.InsertNode(N, IP);
1966 InsertNode(N);
1967 return SDValue(N, 0);
1968}
1969
1971 const SDLoc &DL) {
1973 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
1974 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1975}
1976
1978 MaybeAlign Alignment, int Offset,
1979 bool isTarget, unsigned TargetFlags) {
1980 assert((TargetFlags == 0 || isTarget) &&
1981 "Cannot set target flags on target-independent globals");
1982 if (!Alignment)
1983 Alignment = shouldOptForSize()
1984 ? getDataLayout().getABITypeAlign(C->getType())
1985 : getDataLayout().getPrefTypeAlign(C->getType());
1986 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1987 SDVTList VTs = getVTList(VT);
1989 AddNodeIDNode(ID, Opc, VTs, {});
1990 ID.AddInteger(Alignment->value());
1991 ID.AddInteger(Offset);
1992 ID.AddPointer(C);
1993 ID.AddInteger(TargetFlags);
1994 void *IP = nullptr;
1995 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1996 return SDValue(E, 0);
1997
1998 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1999 TargetFlags);
2000 CSEMap.InsertNode(N, IP);
2001 InsertNode(N);
2002 SDValue V = SDValue(N, 0);
2003 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2004 return V;
2005}
2006
2008 MaybeAlign Alignment, int Offset,
2009 bool isTarget, unsigned TargetFlags) {
2010 assert((TargetFlags == 0 || isTarget) &&
2011 "Cannot set target flags on target-independent globals");
2012 if (!Alignment)
2013 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2014 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2015 SDVTList VTs = getVTList(VT);
2017 AddNodeIDNode(ID, Opc, VTs, {});
2018 ID.AddInteger(Alignment->value());
2019 ID.AddInteger(Offset);
2020 C->addSelectionDAGCSEId(ID);
2021 ID.AddInteger(TargetFlags);
2022 void *IP = nullptr;
2023 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2024 return SDValue(E, 0);
2025
2026 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2027 TargetFlags);
2028 CSEMap.InsertNode(N, IP);
2029 InsertNode(N);
2030 return SDValue(N, 0);
2031}
2032
2035 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2036 ID.AddPointer(MBB);
2037 void *IP = nullptr;
2038 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2039 return SDValue(E, 0);
2040
2041 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2042 CSEMap.InsertNode(N, IP);
2043 InsertNode(N);
2044 return SDValue(N, 0);
2045}
2046
2048 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2049 ValueTypeNodes.size())
2050 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2051
2052 SDNode *&N = VT.isExtended() ?
2053 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2054
2055 if (N) return SDValue(N, 0);
2056 N = newSDNode<VTSDNode>(VT);
2057 InsertNode(N);
2058 return SDValue(N, 0);
2059}
2060
2062 SDNode *&N = ExternalSymbols[Sym];
2063 if (N) return SDValue(N, 0);
2064 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2065 InsertNode(N);
2066 return SDValue(N, 0);
2067}
2068
2069SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2070 StringRef SymName = TLI->getLibcallImplName(Libcall);
2071 return getExternalSymbol(SymName.data(), VT);
2072}
2073
2075 SDNode *&N = MCSymbols[Sym];
2076 if (N)
2077 return SDValue(N, 0);
2078 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2079 InsertNode(N);
2080 return SDValue(N, 0);
2081}
2082
2084 unsigned TargetFlags) {
2085 SDNode *&N =
2086 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2087 if (N) return SDValue(N, 0);
2088 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2089 InsertNode(N);
2090 return SDValue(N, 0);
2091}
2092
2094 EVT VT, unsigned TargetFlags) {
2095 StringRef SymName = TLI->getLibcallImplName(Libcall);
2096 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2097}
2098
2100 if ((unsigned)Cond >= CondCodeNodes.size())
2101 CondCodeNodes.resize(Cond+1);
2102
2103 if (!CondCodeNodes[Cond]) {
2104 auto *N = newSDNode<CondCodeSDNode>(Cond);
2105 CondCodeNodes[Cond] = N;
2106 InsertNode(N);
2107 }
2108
2109 return SDValue(CondCodeNodes[Cond], 0);
2110}
2111
2113 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2114 "APInt size does not match type size!");
2115
2116 if (MulImm == 0)
2117 return getConstant(0, DL, VT);
2118
2119 const MachineFunction &MF = getMachineFunction();
2120 const Function &F = MF.getFunction();
2121 ConstantRange CR = getVScaleRange(&F, 64);
2122 if (const APInt *C = CR.getSingleElement())
2123 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2124
2125 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2126}
2127
2128/// \returns a value of type \p VT that represents the runtime value of \p
2129/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2130/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2131/// or TypeSize.
2132template <typename Ty>
2134 EVT VT, Ty Quantity) {
2135 if (Quantity.isScalable())
2136 return DAG.getVScale(
2137 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2138
2139 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2140}
2141
2143 ElementCount EC) {
2144 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2145}
2146
2148 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2149}
2150
2152 ElementCount EC) {
2153 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2154 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2155 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2156 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2157}
2158
2160 APInt One(ResVT.getScalarSizeInBits(), 1);
2161 return getStepVector(DL, ResVT, One);
2162}
2163
2165 const APInt &StepVal) {
2166 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2167 if (ResVT.isScalableVector())
2168 return getNode(
2169 ISD::STEP_VECTOR, DL, ResVT,
2170 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2171
2172 SmallVector<SDValue, 16> OpsStepConstants;
2173 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2174 OpsStepConstants.push_back(
2175 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2176 return getBuildVector(ResVT, DL, OpsStepConstants);
2177}
2178
2179/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2180/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2185
2187 SDValue N2, ArrayRef<int> Mask) {
2188 assert(VT.getVectorNumElements() == Mask.size() &&
2189 "Must have the same number of vector elements as mask elements!");
2190 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2191 "Invalid VECTOR_SHUFFLE");
2192
2193 // Canonicalize shuffle undef, undef -> undef
2194 if (N1.isUndef() && N2.isUndef())
2195 return getUNDEF(VT);
2196
2197 // Validate that all indices in Mask are within the range of the elements
2198 // input to the shuffle.
2199 int NElts = Mask.size();
2200 assert(llvm::all_of(Mask,
2201 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2202 "Index out of range");
2203
2204 // Copy the mask so we can do any needed cleanup.
2205 SmallVector<int, 8> MaskVec(Mask);
2206
2207 // Canonicalize shuffle v, v -> v, undef
2208 if (N1 == N2) {
2209 N2 = getUNDEF(VT);
2210 for (int i = 0; i != NElts; ++i)
2211 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2212 }
2213
2214 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2215 if (N1.isUndef())
2216 commuteShuffle(N1, N2, MaskVec);
2217
2218 if (TLI->hasVectorBlend()) {
2219 // If shuffling a splat, try to blend the splat instead. We do this here so
2220 // that even when this arises during lowering we don't have to re-handle it.
2221 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2222 BitVector UndefElements;
2223 SDValue Splat = BV->getSplatValue(&UndefElements);
2224 if (!Splat)
2225 return;
2226
2227 for (int i = 0; i < NElts; ++i) {
2228 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2229 continue;
2230
2231 // If this input comes from undef, mark it as such.
2232 if (UndefElements[MaskVec[i] - Offset]) {
2233 MaskVec[i] = -1;
2234 continue;
2235 }
2236
2237 // If we can blend a non-undef lane, use that instead.
2238 if (!UndefElements[i])
2239 MaskVec[i] = i + Offset;
2240 }
2241 };
2242 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2243 BlendSplat(N1BV, 0);
2244 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2245 BlendSplat(N2BV, NElts);
2246 }
2247
2248 // Canonicalize all index into lhs, -> shuffle lhs, undef
2249 // Canonicalize all index into rhs, -> shuffle rhs, undef
2250 bool AllLHS = true, AllRHS = true;
2251 bool N2Undef = N2.isUndef();
2252 for (int i = 0; i != NElts; ++i) {
2253 if (MaskVec[i] >= NElts) {
2254 if (N2Undef)
2255 MaskVec[i] = -1;
2256 else
2257 AllLHS = false;
2258 } else if (MaskVec[i] >= 0) {
2259 AllRHS = false;
2260 }
2261 }
2262 if (AllLHS && AllRHS)
2263 return getUNDEF(VT);
2264 if (AllLHS && !N2Undef)
2265 N2 = getUNDEF(VT);
2266 if (AllRHS) {
2267 N1 = getUNDEF(VT);
2268 commuteShuffle(N1, N2, MaskVec);
2269 }
2270 // Reset our undef status after accounting for the mask.
2271 N2Undef = N2.isUndef();
2272 // Re-check whether both sides ended up undef.
2273 if (N1.isUndef() && N2Undef)
2274 return getUNDEF(VT);
2275
2276 // If Identity shuffle return that node.
2277 bool Identity = true, AllSame = true;
2278 for (int i = 0; i != NElts; ++i) {
2279 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2280 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2281 }
2282 if (Identity && NElts)
2283 return N1;
2284
2285 // Shuffling a constant splat doesn't change the result.
2286 if (N2Undef) {
2287 SDValue V = N1;
2288
2289 // Look through any bitcasts. We check that these don't change the number
2290 // (and size) of elements and just changes their types.
2291 while (V.getOpcode() == ISD::BITCAST)
2292 V = V->getOperand(0);
2293
2294 // A splat should always show up as a build vector node.
2295 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2296 BitVector UndefElements;
2297 SDValue Splat = BV->getSplatValue(&UndefElements);
2298 // If this is a splat of an undef, shuffling it is also undef.
2299 if (Splat && Splat.isUndef())
2300 return getUNDEF(VT);
2301
2302 bool SameNumElts =
2303 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2304
2305 // We only have a splat which can skip shuffles if there is a splatted
2306 // value and no undef lanes rearranged by the shuffle.
2307 if (Splat && UndefElements.none()) {
2308 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2309 // number of elements match or the value splatted is a zero constant.
2310 if (SameNumElts || isNullConstant(Splat))
2311 return N1;
2312 }
2313
2314 // If the shuffle itself creates a splat, build the vector directly.
2315 if (AllSame && SameNumElts) {
2316 EVT BuildVT = BV->getValueType(0);
2317 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2318 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2319
2320 // We may have jumped through bitcasts, so the type of the
2321 // BUILD_VECTOR may not match the type of the shuffle.
2322 if (BuildVT != VT)
2323 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2324 return NewBV;
2325 }
2326 }
2327 }
2328
2329 SDVTList VTs = getVTList(VT);
2331 SDValue Ops[2] = { N1, N2 };
2333 for (int i = 0; i != NElts; ++i)
2334 ID.AddInteger(MaskVec[i]);
2335
2336 void* IP = nullptr;
2337 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2338 return SDValue(E, 0);
2339
2340 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2341 // SDNode doesn't have access to it. This memory will be "leaked" when
2342 // the node is deallocated, but recovered when the NodeAllocator is released.
2343 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2344 llvm::copy(MaskVec, MaskAlloc);
2345
2346 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2347 dl.getDebugLoc(), MaskAlloc);
2348 createOperands(N, Ops);
2349
2350 CSEMap.InsertNode(N, IP);
2351 InsertNode(N);
2352 SDValue V = SDValue(N, 0);
2353 NewSDValueDbgMsg(V, "Creating new node: ", this);
2354 return V;
2355}
2356
2358 EVT VT = SV.getValueType(0);
2359 SmallVector<int, 8> MaskVec(SV.getMask());
2361
2362 SDValue Op0 = SV.getOperand(0);
2363 SDValue Op1 = SV.getOperand(1);
2364 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2365}
2366
2368 SDVTList VTs = getVTList(VT);
2370 AddNodeIDNode(ID, ISD::Register, VTs, {});
2371 ID.AddInteger(Reg.id());
2372 void *IP = nullptr;
2373 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2374 return SDValue(E, 0);
2375
2376 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2377 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2378 CSEMap.InsertNode(N, IP);
2379 InsertNode(N);
2380 return SDValue(N, 0);
2381}
2382
2385 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2386 ID.AddPointer(RegMask);
2387 void *IP = nullptr;
2388 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2389 return SDValue(E, 0);
2390
2391 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2392 CSEMap.InsertNode(N, IP);
2393 InsertNode(N);
2394 return SDValue(N, 0);
2395}
2396
2398 MCSymbol *Label) {
2399 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2400}
2401
2402SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2403 SDValue Root, MCSymbol *Label) {
2405 SDValue Ops[] = { Root };
2406 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2407 ID.AddPointer(Label);
2408 void *IP = nullptr;
2409 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2410 return SDValue(E, 0);
2411
2412 auto *N =
2413 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2414 createOperands(N, Ops);
2415
2416 CSEMap.InsertNode(N, IP);
2417 InsertNode(N);
2418 return SDValue(N, 0);
2419}
2420
2422 int64_t Offset, bool isTarget,
2423 unsigned TargetFlags) {
2424 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2425 SDVTList VTs = getVTList(VT);
2426
2428 AddNodeIDNode(ID, Opc, VTs, {});
2429 ID.AddPointer(BA);
2430 ID.AddInteger(Offset);
2431 ID.AddInteger(TargetFlags);
2432 void *IP = nullptr;
2433 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2434 return SDValue(E, 0);
2435
2436 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2437 CSEMap.InsertNode(N, IP);
2438 InsertNode(N);
2439 return SDValue(N, 0);
2440}
2441
2444 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2445 ID.AddPointer(V);
2446
2447 void *IP = nullptr;
2448 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2449 return SDValue(E, 0);
2450
2451 auto *N = newSDNode<SrcValueSDNode>(V);
2452 CSEMap.InsertNode(N, IP);
2453 InsertNode(N);
2454 return SDValue(N, 0);
2455}
2456
2459 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2460 ID.AddPointer(MD);
2461
2462 void *IP = nullptr;
2463 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2464 return SDValue(E, 0);
2465
2466 auto *N = newSDNode<MDNodeSDNode>(MD);
2467 CSEMap.InsertNode(N, IP);
2468 InsertNode(N);
2469 return SDValue(N, 0);
2470}
2471
2473 if (VT == V.getValueType())
2474 return V;
2475
2476 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2477}
2478
2480 unsigned SrcAS, unsigned DestAS) {
2481 SDVTList VTs = getVTList(VT);
2482 SDValue Ops[] = {Ptr};
2485 ID.AddInteger(SrcAS);
2486 ID.AddInteger(DestAS);
2487
2488 void *IP = nullptr;
2489 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2490 return SDValue(E, 0);
2491
2492 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2493 VTs, SrcAS, DestAS);
2494 createOperands(N, Ops);
2495
2496 CSEMap.InsertNode(N, IP);
2497 InsertNode(N);
2498 return SDValue(N, 0);
2499}
2500
2502 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2503}
2504
2505/// getShiftAmountOperand - Return the specified value casted to
2506/// the target's desired shift amount type.
2508 EVT OpTy = Op.getValueType();
2509 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2510 if (OpTy == ShTy || OpTy.isVector()) return Op;
2511
2512 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2513}
2514
2516 SDLoc dl(Node);
2518 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2519 EVT VT = Node->getValueType(0);
2520 SDValue Tmp1 = Node->getOperand(0);
2521 SDValue Tmp2 = Node->getOperand(1);
2522 const MaybeAlign MA(Node->getConstantOperandVal(3));
2523
2524 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2525 Tmp2, MachinePointerInfo(V));
2526 SDValue VAList = VAListLoad;
2527
2528 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2529 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2530 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2531
2532 VAList = getNode(
2533 ISD::AND, dl, VAList.getValueType(), VAList,
2534 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2535 }
2536
2537 // Increment the pointer, VAList, to the next vaarg
2538 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2539 getConstant(getDataLayout().getTypeAllocSize(
2540 VT.getTypeForEVT(*getContext())),
2541 dl, VAList.getValueType()));
2542 // Store the incremented VAList to the legalized pointer
2543 Tmp1 =
2544 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2545 // Load the actual argument out of the pointer VAList
2546 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2547}
2548
2550 SDLoc dl(Node);
2552 // This defaults to loading a pointer from the input and storing it to the
2553 // output, returning the chain.
2554 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2555 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2556 SDValue Tmp1 =
2557 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2558 Node->getOperand(2), MachinePointerInfo(VS));
2559 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2560 MachinePointerInfo(VD));
2561}
2562
2564 const DataLayout &DL = getDataLayout();
2565 Type *Ty = VT.getTypeForEVT(*getContext());
2566 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2567
2568 if (TLI->isTypeLegal(VT) || !VT.isVector())
2569 return RedAlign;
2570
2571 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2572 const Align StackAlign = TFI->getStackAlign();
2573
2574 // See if we can choose a smaller ABI alignment in cases where it's an
2575 // illegal vector type that will get broken down.
2576 if (RedAlign > StackAlign) {
2577 EVT IntermediateVT;
2578 MVT RegisterVT;
2579 unsigned NumIntermediates;
2580 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2581 NumIntermediates, RegisterVT);
2582 Ty = IntermediateVT.getTypeForEVT(*getContext());
2583 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2584 if (RedAlign2 < RedAlign)
2585 RedAlign = RedAlign2;
2586
2587 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2588 // If the stack is not realignable, the alignment should be limited to the
2589 // StackAlignment
2590 RedAlign = std::min(RedAlign, StackAlign);
2591 }
2592
2593 return RedAlign;
2594}
2595
2597 MachineFrameInfo &MFI = MF->getFrameInfo();
2598 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2599 int StackID = 0;
2600 if (Bytes.isScalable())
2601 StackID = TFI->getStackIDForScalableVectors();
2602 // The stack id gives an indication of whether the object is scalable or
2603 // not, so it's safe to pass in the minimum size here.
2604 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2605 false, nullptr, StackID);
2606 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2607}
2608
2610 Type *Ty = VT.getTypeForEVT(*getContext());
2611 Align StackAlign =
2612 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2613 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2614}
2615
2617 TypeSize VT1Size = VT1.getStoreSize();
2618 TypeSize VT2Size = VT2.getStoreSize();
2619 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2620 "Don't know how to choose the maximum size when creating a stack "
2621 "temporary");
2622 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2623 ? VT1Size
2624 : VT2Size;
2625
2626 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2627 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2628 const DataLayout &DL = getDataLayout();
2629 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2630 return CreateStackTemporary(Bytes, Align);
2631}
2632
2634 ISD::CondCode Cond, const SDLoc &dl) {
2635 EVT OpVT = N1.getValueType();
2636
2637 auto GetUndefBooleanConstant = [&]() {
2638 if (VT.getScalarType() == MVT::i1 ||
2639 TLI->getBooleanContents(OpVT) ==
2641 return getUNDEF(VT);
2642 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2643 // so we cannot use getUNDEF(). Return zero instead.
2644 return getConstant(0, dl, VT);
2645 };
2646
2647 // These setcc operations always fold.
2648 switch (Cond) {
2649 default: break;
2650 case ISD::SETFALSE:
2651 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2652 case ISD::SETTRUE:
2653 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2654
2655 case ISD::SETOEQ:
2656 case ISD::SETOGT:
2657 case ISD::SETOGE:
2658 case ISD::SETOLT:
2659 case ISD::SETOLE:
2660 case ISD::SETONE:
2661 case ISD::SETO:
2662 case ISD::SETUO:
2663 case ISD::SETUEQ:
2664 case ISD::SETUNE:
2665 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2666 break;
2667 }
2668
2669 if (OpVT.isInteger()) {
2670 // For EQ and NE, we can always pick a value for the undef to make the
2671 // predicate pass or fail, so we can return undef.
2672 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2673 // icmp eq/ne X, undef -> undef.
2674 if ((N1.isUndef() || N2.isUndef()) &&
2675 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2676 return GetUndefBooleanConstant();
2677
2678 // If both operands are undef, we can return undef for int comparison.
2679 // icmp undef, undef -> undef.
2680 if (N1.isUndef() && N2.isUndef())
2681 return GetUndefBooleanConstant();
2682
2683 // icmp X, X -> true/false
2684 // icmp X, undef -> true/false because undef could be X.
2685 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2686 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2687 }
2688
2690 const APInt &C2 = N2C->getAPIntValue();
2692 const APInt &C1 = N1C->getAPIntValue();
2693
2695 dl, VT, OpVT);
2696 }
2697 }
2698
2699 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2700 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2701
2702 if (N1CFP && N2CFP) {
2703 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2704 switch (Cond) {
2705 default: break;
2706 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2707 return GetUndefBooleanConstant();
2708 [[fallthrough]];
2709 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2710 OpVT);
2711 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2712 return GetUndefBooleanConstant();
2713 [[fallthrough]];
2715 R==APFloat::cmpLessThan, dl, VT,
2716 OpVT);
2717 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2718 return GetUndefBooleanConstant();
2719 [[fallthrough]];
2720 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2721 OpVT);
2722 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2723 return GetUndefBooleanConstant();
2724 [[fallthrough]];
2726 VT, OpVT);
2727 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2728 return GetUndefBooleanConstant();
2729 [[fallthrough]];
2731 R==APFloat::cmpEqual, dl, VT,
2732 OpVT);
2733 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2734 return GetUndefBooleanConstant();
2735 [[fallthrough]];
2737 R==APFloat::cmpEqual, dl, VT, OpVT);
2738 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2739 OpVT);
2740 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2741 OpVT);
2743 R==APFloat::cmpEqual, dl, VT,
2744 OpVT);
2745 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2746 OpVT);
2748 R==APFloat::cmpLessThan, dl, VT,
2749 OpVT);
2751 R==APFloat::cmpUnordered, dl, VT,
2752 OpVT);
2754 VT, OpVT);
2755 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2756 OpVT);
2757 }
2758 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2759 // Ensure that the constant occurs on the RHS.
2761 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2762 return SDValue();
2763 return getSetCC(dl, VT, N2, N1, SwappedCond);
2764 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2765 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2766 // If an operand is known to be a nan (or undef that could be a nan), we can
2767 // fold it.
2768 // Choosing NaN for the undef will always make unordered comparison succeed
2769 // and ordered comparison fails.
2770 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2771 switch (ISD::getUnorderedFlavor(Cond)) {
2772 default:
2773 llvm_unreachable("Unknown flavor!");
2774 case 0: // Known false.
2775 return getBoolConstant(false, dl, VT, OpVT);
2776 case 1: // Known true.
2777 return getBoolConstant(true, dl, VT, OpVT);
2778 case 2: // Undefined.
2779 return GetUndefBooleanConstant();
2780 }
2781 }
2782
2783 // Could not fold it.
2784 return SDValue();
2785}
2786
2787/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2788/// use this predicate to simplify operations downstream.
2790 unsigned BitWidth = Op.getScalarValueSizeInBits();
2792}
2793
2794// TODO: Should have argument to specify if sign bit of nan is ignorable.
2796 if (Depth >= MaxRecursionDepth)
2797 return false; // Limit search depth.
2798
2799 unsigned Opc = Op.getOpcode();
2800 switch (Opc) {
2801 case ISD::FABS:
2802 return true;
2803 case ISD::AssertNoFPClass: {
2804 FPClassTest NoFPClass =
2805 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2806
2807 const FPClassTest TestMask = fcNan | fcNegative;
2808 return (NoFPClass & TestMask) == TestMask;
2809 }
2810 case ISD::ARITH_FENCE:
2811 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2812 case ISD::FEXP:
2813 case ISD::FEXP2:
2814 case ISD::FEXP10:
2815 return Op->getFlags().hasNoNaNs();
2816 case ISD::FMINNUM:
2817 case ISD::FMINNUM_IEEE:
2818 case ISD::FMINIMUM:
2819 case ISD::FMINIMUMNUM:
2820 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2821 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2822 case ISD::FMAXNUM:
2823 case ISD::FMAXNUM_IEEE:
2824 case ISD::FMAXIMUM:
2825 case ISD::FMAXIMUMNUM:
2826 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2827 // is sufficient.
2828 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2829 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2830 default:
2831 return false;
2832 }
2833
2834 llvm_unreachable("covered opcode switch");
2835}
2836
2837/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2838/// this predicate to simplify operations downstream. Mask is known to be zero
2839/// for bits that V cannot have.
2841 unsigned Depth) const {
2842 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2843}
2844
2845/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2846/// DemandedElts. We use this predicate to simplify operations downstream.
2847/// Mask is known to be zero for bits that V cannot have.
2849 const APInt &DemandedElts,
2850 unsigned Depth) const {
2851 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2852}
2853
2854/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2855/// DemandedElts. We use this predicate to simplify operations downstream.
2857 unsigned Depth /* = 0 */) const {
2858 return computeKnownBits(V, DemandedElts, Depth).isZero();
2859}
2860
2861/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2863 unsigned Depth) const {
2864 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2865}
2866
2868 const APInt &DemandedElts,
2869 unsigned Depth) const {
2870 EVT VT = Op.getValueType();
2871 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2872
2873 unsigned NumElts = VT.getVectorNumElements();
2874 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2875
2876 APInt KnownZeroElements = APInt::getZero(NumElts);
2877 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2878 if (!DemandedElts[EltIdx])
2879 continue; // Don't query elements that are not demanded.
2880 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2881 if (MaskedVectorIsZero(Op, Mask, Depth))
2882 KnownZeroElements.setBit(EltIdx);
2883 }
2884 return KnownZeroElements;
2885}
2886
2887/// isSplatValue - Return true if the vector V has the same value
2888/// across all DemandedElts. For scalable vectors, we don't know the
2889/// number of lanes at compile time. Instead, we use a 1 bit APInt
2890/// to represent a conservative value for all lanes; that is, that
2891/// one bit value is implicitly splatted across all lanes.
2892bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2893 APInt &UndefElts, unsigned Depth) const {
2894 unsigned Opcode = V.getOpcode();
2895 EVT VT = V.getValueType();
2896 assert(VT.isVector() && "Vector type expected");
2897 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2898 "scalable demanded bits are ignored");
2899
2900 if (!DemandedElts)
2901 return false; // No demanded elts, better to assume we don't know anything.
2902
2903 if (Depth >= MaxRecursionDepth)
2904 return false; // Limit search depth.
2905
2906 // Deal with some common cases here that work for both fixed and scalable
2907 // vector types.
2908 switch (Opcode) {
2909 case ISD::SPLAT_VECTOR:
2910 UndefElts = V.getOperand(0).isUndef()
2911 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2912 : APInt(DemandedElts.getBitWidth(), 0);
2913 return true;
2914 case ISD::ADD:
2915 case ISD::SUB:
2916 case ISD::AND:
2917 case ISD::XOR:
2918 case ISD::OR: {
2919 APInt UndefLHS, UndefRHS;
2920 SDValue LHS = V.getOperand(0);
2921 SDValue RHS = V.getOperand(1);
2922 // Only recognize splats with the same demanded undef elements for both
2923 // operands, otherwise we might fail to handle binop-specific undef
2924 // handling.
2925 // e.g. (and undef, 0) -> 0 etc.
2926 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2927 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2928 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2929 UndefElts = UndefLHS | UndefRHS;
2930 return true;
2931 }
2932 return false;
2933 }
2934 case ISD::ABS:
2935 case ISD::TRUNCATE:
2936 case ISD::SIGN_EXTEND:
2937 case ISD::ZERO_EXTEND:
2938 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2939 default:
2940 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2941 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2942 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2943 Depth);
2944 break;
2945 }
2946
2947 // We don't support other cases than those above for scalable vectors at
2948 // the moment.
2949 if (VT.isScalableVector())
2950 return false;
2951
2952 unsigned NumElts = VT.getVectorNumElements();
2953 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2954 UndefElts = APInt::getZero(NumElts);
2955
2956 switch (Opcode) {
2957 case ISD::BUILD_VECTOR: {
2958 SDValue Scl;
2959 for (unsigned i = 0; i != NumElts; ++i) {
2960 SDValue Op = V.getOperand(i);
2961 if (Op.isUndef()) {
2962 UndefElts.setBit(i);
2963 continue;
2964 }
2965 if (!DemandedElts[i])
2966 continue;
2967 if (Scl && Scl != Op)
2968 return false;
2969 Scl = Op;
2970 }
2971 return true;
2972 }
2973 case ISD::VECTOR_SHUFFLE: {
2974 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2975 APInt DemandedLHS = APInt::getZero(NumElts);
2976 APInt DemandedRHS = APInt::getZero(NumElts);
2977 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2978 for (int i = 0; i != (int)NumElts; ++i) {
2979 int M = Mask[i];
2980 if (M < 0) {
2981 UndefElts.setBit(i);
2982 continue;
2983 }
2984 if (!DemandedElts[i])
2985 continue;
2986 if (M < (int)NumElts)
2987 DemandedLHS.setBit(M);
2988 else
2989 DemandedRHS.setBit(M - NumElts);
2990 }
2991
2992 // If we aren't demanding either op, assume there's no splat.
2993 // If we are demanding both ops, assume there's no splat.
2994 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2995 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2996 return false;
2997
2998 // See if the demanded elts of the source op is a splat or we only demand
2999 // one element, which should always be a splat.
3000 // TODO: Handle source ops splats with undefs.
3001 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3002 APInt SrcUndefs;
3003 return (SrcElts.popcount() == 1) ||
3004 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3005 (SrcElts & SrcUndefs).isZero());
3006 };
3007 if (!DemandedLHS.isZero())
3008 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3009 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3010 }
3012 // Offset the demanded elts by the subvector index.
3013 SDValue Src = V.getOperand(0);
3014 // We don't support scalable vectors at the moment.
3015 if (Src.getValueType().isScalableVector())
3016 return false;
3017 uint64_t Idx = V.getConstantOperandVal(1);
3018 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3019 APInt UndefSrcElts;
3020 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3021 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3022 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3023 return true;
3024 }
3025 break;
3026 }
3030 // Widen the demanded elts by the src element count.
3031 SDValue Src = V.getOperand(0);
3032 // We don't support scalable vectors at the moment.
3033 if (Src.getValueType().isScalableVector())
3034 return false;
3035 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3036 APInt UndefSrcElts;
3037 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3038 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3039 UndefElts = UndefSrcElts.trunc(NumElts);
3040 return true;
3041 }
3042 break;
3043 }
3044 case ISD::BITCAST: {
3045 SDValue Src = V.getOperand(0);
3046 EVT SrcVT = Src.getValueType();
3047 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3048 unsigned BitWidth = VT.getScalarSizeInBits();
3049
3050 // Ignore bitcasts from unsupported types.
3051 // TODO: Add fp support?
3052 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3053 break;
3054
3055 // Bitcast 'small element' vector to 'large element' vector.
3056 if ((BitWidth % SrcBitWidth) == 0) {
3057 // See if each sub element is a splat.
3058 unsigned Scale = BitWidth / SrcBitWidth;
3059 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3060 APInt ScaledDemandedElts =
3061 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3062 for (unsigned I = 0; I != Scale; ++I) {
3063 APInt SubUndefElts;
3064 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3065 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3066 SubDemandedElts &= ScaledDemandedElts;
3067 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3068 return false;
3069 // TODO: Add support for merging sub undef elements.
3070 if (!SubUndefElts.isZero())
3071 return false;
3072 }
3073 return true;
3074 }
3075 break;
3076 }
3077 }
3078
3079 return false;
3080}
3081
3082/// Helper wrapper to main isSplatValue function.
3083bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3084 EVT VT = V.getValueType();
3085 assert(VT.isVector() && "Vector type expected");
3086
3087 APInt UndefElts;
3088 // Since the number of lanes in a scalable vector is unknown at compile time,
3089 // we track one bit which is implicitly broadcast to all lanes. This means
3090 // that all lanes in a scalable vector are considered demanded.
3091 APInt DemandedElts
3093 return isSplatValue(V, DemandedElts, UndefElts) &&
3094 (AllowUndefs || !UndefElts);
3095}
3096
3099
3100 EVT VT = V.getValueType();
3101 unsigned Opcode = V.getOpcode();
3102 switch (Opcode) {
3103 default: {
3104 APInt UndefElts;
3105 // Since the number of lanes in a scalable vector is unknown at compile time,
3106 // we track one bit which is implicitly broadcast to all lanes. This means
3107 // that all lanes in a scalable vector are considered demanded.
3108 APInt DemandedElts
3110
3111 if (isSplatValue(V, DemandedElts, UndefElts)) {
3112 if (VT.isScalableVector()) {
3113 // DemandedElts and UndefElts are ignored for scalable vectors, since
3114 // the only supported cases are SPLAT_VECTOR nodes.
3115 SplatIdx = 0;
3116 } else {
3117 // Handle case where all demanded elements are UNDEF.
3118 if (DemandedElts.isSubsetOf(UndefElts)) {
3119 SplatIdx = 0;
3120 return getUNDEF(VT);
3121 }
3122 SplatIdx = (UndefElts & DemandedElts).countr_one();
3123 }
3124 return V;
3125 }
3126 break;
3127 }
3128 case ISD::SPLAT_VECTOR:
3129 SplatIdx = 0;
3130 return V;
3131 case ISD::VECTOR_SHUFFLE: {
3132 assert(!VT.isScalableVector());
3133 // Check if this is a shuffle node doing a splat.
3134 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3135 // getTargetVShiftNode currently struggles without the splat source.
3136 auto *SVN = cast<ShuffleVectorSDNode>(V);
3137 if (!SVN->isSplat())
3138 break;
3139 int Idx = SVN->getSplatIndex();
3140 int NumElts = V.getValueType().getVectorNumElements();
3141 SplatIdx = Idx % NumElts;
3142 return V.getOperand(Idx / NumElts);
3143 }
3144 }
3145
3146 return SDValue();
3147}
3148
3150 int SplatIdx;
3151 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3152 EVT SVT = SrcVector.getValueType().getScalarType();
3153 EVT LegalSVT = SVT;
3154 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3155 if (!SVT.isInteger())
3156 return SDValue();
3157 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3158 if (LegalSVT.bitsLT(SVT))
3159 return SDValue();
3160 }
3161 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3162 }
3163 return SDValue();
3164}
3165
3166std::optional<ConstantRange>
3168 unsigned Depth) const {
3169 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3170 V.getOpcode() == ISD::SRA) &&
3171 "Unknown shift node");
3172 // Shifting more than the bitwidth is not valid.
3173 unsigned BitWidth = V.getScalarValueSizeInBits();
3174
3175 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3176 const APInt &ShAmt = Cst->getAPIntValue();
3177 if (ShAmt.uge(BitWidth))
3178 return std::nullopt;
3179 return ConstantRange(ShAmt);
3180 }
3181
3182 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3183 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3184 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3185 if (!DemandedElts[i])
3186 continue;
3187 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3188 if (!SA) {
3189 MinAmt = MaxAmt = nullptr;
3190 break;
3191 }
3192 const APInt &ShAmt = SA->getAPIntValue();
3193 if (ShAmt.uge(BitWidth))
3194 return std::nullopt;
3195 if (!MinAmt || MinAmt->ugt(ShAmt))
3196 MinAmt = &ShAmt;
3197 if (!MaxAmt || MaxAmt->ult(ShAmt))
3198 MaxAmt = &ShAmt;
3199 }
3200 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3201 "Failed to find matching min/max shift amounts");
3202 if (MinAmt && MaxAmt)
3203 return ConstantRange(*MinAmt, *MaxAmt + 1);
3204 }
3205
3206 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3207 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3208 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3209 if (KnownAmt.getMaxValue().ult(BitWidth))
3210 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3211
3212 return std::nullopt;
3213}
3214
3215std::optional<unsigned>
3217 unsigned Depth) const {
3218 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3219 V.getOpcode() == ISD::SRA) &&
3220 "Unknown shift node");
3221 if (std::optional<ConstantRange> AmtRange =
3222 getValidShiftAmountRange(V, DemandedElts, Depth))
3223 if (const APInt *ShAmt = AmtRange->getSingleElement())
3224 return ShAmt->getZExtValue();
3225 return std::nullopt;
3226}
3227
3228std::optional<unsigned>
3230 EVT VT = V.getValueType();
3231 APInt DemandedElts = VT.isFixedLengthVector()
3233 : APInt(1, 1);
3234 return getValidShiftAmount(V, DemandedElts, Depth);
3235}
3236
3237std::optional<unsigned>
3239 unsigned Depth) const {
3240 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3241 V.getOpcode() == ISD::SRA) &&
3242 "Unknown shift node");
3243 if (std::optional<ConstantRange> AmtRange =
3244 getValidShiftAmountRange(V, DemandedElts, Depth))
3245 return AmtRange->getUnsignedMin().getZExtValue();
3246 return std::nullopt;
3247}
3248
3249std::optional<unsigned>
3251 EVT VT = V.getValueType();
3252 APInt DemandedElts = VT.isFixedLengthVector()
3254 : APInt(1, 1);
3255 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3256}
3257
3258std::optional<unsigned>
3260 unsigned Depth) const {
3261 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3262 V.getOpcode() == ISD::SRA) &&
3263 "Unknown shift node");
3264 if (std::optional<ConstantRange> AmtRange =
3265 getValidShiftAmountRange(V, DemandedElts, Depth))
3266 return AmtRange->getUnsignedMax().getZExtValue();
3267 return std::nullopt;
3268}
3269
3270std::optional<unsigned>
3272 EVT VT = V.getValueType();
3273 APInt DemandedElts = VT.isFixedLengthVector()
3275 : APInt(1, 1);
3276 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3277}
3278
3279/// Determine which bits of Op are known to be either zero or one and return
3280/// them in Known. For vectors, the known bits are those that are shared by
3281/// every vector element.
3283 EVT VT = Op.getValueType();
3284
3285 // Since the number of lanes in a scalable vector is unknown at compile time,
3286 // we track one bit which is implicitly broadcast to all lanes. This means
3287 // that all lanes in a scalable vector are considered demanded.
3288 APInt DemandedElts = VT.isFixedLengthVector()
3290 : APInt(1, 1);
3291 return computeKnownBits(Op, DemandedElts, Depth);
3292}
3293
3294/// Determine which bits of Op are known to be either zero or one and return
3295/// them in Known. The DemandedElts argument allows us to only collect the known
3296/// bits that are shared by the requested vector elements.
3298 unsigned Depth) const {
3299 unsigned BitWidth = Op.getScalarValueSizeInBits();
3300
3301 KnownBits Known(BitWidth); // Don't know anything.
3302
3303 if (auto OptAPInt = Op->bitcastToAPInt()) {
3304 // We know all of the bits for a constant!
3305 return KnownBits::makeConstant(*std::move(OptAPInt));
3306 }
3307
3308 if (Depth >= MaxRecursionDepth)
3309 return Known; // Limit search depth.
3310
3311 KnownBits Known2;
3312 unsigned NumElts = DemandedElts.getBitWidth();
3313 assert((!Op.getValueType().isFixedLengthVector() ||
3314 NumElts == Op.getValueType().getVectorNumElements()) &&
3315 "Unexpected vector size");
3316
3317 if (!DemandedElts)
3318 return Known; // No demanded elts, better to assume we don't know anything.
3319
3320 unsigned Opcode = Op.getOpcode();
3321 switch (Opcode) {
3322 case ISD::MERGE_VALUES:
3323 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3324 Depth + 1);
3325 case ISD::SPLAT_VECTOR: {
3326 SDValue SrcOp = Op.getOperand(0);
3327 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3328 "Expected SPLAT_VECTOR implicit truncation");
3329 // Implicitly truncate the bits to match the official semantics of
3330 // SPLAT_VECTOR.
3331 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3332 break;
3333 }
3335 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3336 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3337 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3338 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3339 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3340 }
3341 break;
3342 }
3343 case ISD::STEP_VECTOR: {
3344 const APInt &Step = Op.getConstantOperandAPInt(0);
3345
3346 if (Step.isPowerOf2())
3347 Known.Zero.setLowBits(Step.logBase2());
3348
3350
3351 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3352 break;
3353 const APInt MinNumElts =
3354 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3355
3356 bool Overflow;
3357 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3359 .umul_ov(MinNumElts, Overflow);
3360 if (Overflow)
3361 break;
3362
3363 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3364 if (Overflow)
3365 break;
3366
3367 Known.Zero.setHighBits(MaxValue.countl_zero());
3368 break;
3369 }
3370 case ISD::BUILD_VECTOR:
3371 assert(!Op.getValueType().isScalableVector());
3372 // Collect the known bits that are shared by every demanded vector element.
3373 Known.setAllConflict();
3374 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3375 if (!DemandedElts[i])
3376 continue;
3377
3378 SDValue SrcOp = Op.getOperand(i);
3379 Known2 = computeKnownBits(SrcOp, Depth + 1);
3380
3381 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3382 if (SrcOp.getValueSizeInBits() != BitWidth) {
3383 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3384 "Expected BUILD_VECTOR implicit truncation");
3385 Known2 = Known2.trunc(BitWidth);
3386 }
3387
3388 // Known bits are the values that are shared by every demanded element.
3389 Known = Known.intersectWith(Known2);
3390
3391 // If we don't know any bits, early out.
3392 if (Known.isUnknown())
3393 break;
3394 }
3395 break;
3396 case ISD::VECTOR_COMPRESS: {
3397 SDValue Vec = Op.getOperand(0);
3398 SDValue PassThru = Op.getOperand(2);
3399 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3400 // If we don't know any bits, early out.
3401 if (Known.isUnknown())
3402 break;
3403 Known2 = computeKnownBits(Vec, Depth + 1);
3404 Known = Known.intersectWith(Known2);
3405 break;
3406 }
3407 case ISD::VECTOR_SHUFFLE: {
3408 assert(!Op.getValueType().isScalableVector());
3409 // Collect the known bits that are shared by every vector element referenced
3410 // by the shuffle.
3411 APInt DemandedLHS, DemandedRHS;
3413 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3414 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3415 DemandedLHS, DemandedRHS))
3416 break;
3417
3418 // Known bits are the values that are shared by every demanded element.
3419 Known.setAllConflict();
3420 if (!!DemandedLHS) {
3421 SDValue LHS = Op.getOperand(0);
3422 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3423 Known = Known.intersectWith(Known2);
3424 }
3425 // If we don't know any bits, early out.
3426 if (Known.isUnknown())
3427 break;
3428 if (!!DemandedRHS) {
3429 SDValue RHS = Op.getOperand(1);
3430 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3431 Known = Known.intersectWith(Known2);
3432 }
3433 break;
3434 }
3435 case ISD::VSCALE: {
3437 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3438 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3439 break;
3440 }
3441 case ISD::CONCAT_VECTORS: {
3442 if (Op.getValueType().isScalableVector())
3443 break;
3444 // Split DemandedElts and test each of the demanded subvectors.
3445 Known.setAllConflict();
3446 EVT SubVectorVT = Op.getOperand(0).getValueType();
3447 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3448 unsigned NumSubVectors = Op.getNumOperands();
3449 for (unsigned i = 0; i != NumSubVectors; ++i) {
3450 APInt DemandedSub =
3451 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3452 if (!!DemandedSub) {
3453 SDValue Sub = Op.getOperand(i);
3454 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3455 Known = Known.intersectWith(Known2);
3456 }
3457 // If we don't know any bits, early out.
3458 if (Known.isUnknown())
3459 break;
3460 }
3461 break;
3462 }
3463 case ISD::INSERT_SUBVECTOR: {
3464 if (Op.getValueType().isScalableVector())
3465 break;
3466 // Demand any elements from the subvector and the remainder from the src its
3467 // inserted into.
3468 SDValue Src = Op.getOperand(0);
3469 SDValue Sub = Op.getOperand(1);
3470 uint64_t Idx = Op.getConstantOperandVal(2);
3471 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3472 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3473 APInt DemandedSrcElts = DemandedElts;
3474 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3475
3476 Known.setAllConflict();
3477 if (!!DemandedSubElts) {
3478 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3479 if (Known.isUnknown())
3480 break; // early-out.
3481 }
3482 if (!!DemandedSrcElts) {
3483 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3484 Known = Known.intersectWith(Known2);
3485 }
3486 break;
3487 }
3489 // Offset the demanded elts by the subvector index.
3490 SDValue Src = Op.getOperand(0);
3491 // Bail until we can represent demanded elements for scalable vectors.
3492 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3493 break;
3494 uint64_t Idx = Op.getConstantOperandVal(1);
3495 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3496 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3497 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3498 break;
3499 }
3500 case ISD::SCALAR_TO_VECTOR: {
3501 if (Op.getValueType().isScalableVector())
3502 break;
3503 // We know about scalar_to_vector as much as we know about it source,
3504 // which becomes the first element of otherwise unknown vector.
3505 if (DemandedElts != 1)
3506 break;
3507
3508 SDValue N0 = Op.getOperand(0);
3509 Known = computeKnownBits(N0, Depth + 1);
3510 if (N0.getValueSizeInBits() != BitWidth)
3511 Known = Known.trunc(BitWidth);
3512
3513 break;
3514 }
3515 case ISD::BITCAST: {
3516 if (Op.getValueType().isScalableVector())
3517 break;
3518
3519 SDValue N0 = Op.getOperand(0);
3520 EVT SubVT = N0.getValueType();
3521 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3522
3523 // Ignore bitcasts from unsupported types.
3524 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3525 break;
3526
3527 // Fast handling of 'identity' bitcasts.
3528 if (BitWidth == SubBitWidth) {
3529 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3530 break;
3531 }
3532
3533 bool IsLE = getDataLayout().isLittleEndian();
3534
3535 // Bitcast 'small element' vector to 'large element' scalar/vector.
3536 if ((BitWidth % SubBitWidth) == 0) {
3537 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3538
3539 // Collect known bits for the (larger) output by collecting the known
3540 // bits from each set of sub elements and shift these into place.
3541 // We need to separately call computeKnownBits for each set of
3542 // sub elements as the knownbits for each is likely to be different.
3543 unsigned SubScale = BitWidth / SubBitWidth;
3544 APInt SubDemandedElts(NumElts * SubScale, 0);
3545 for (unsigned i = 0; i != NumElts; ++i)
3546 if (DemandedElts[i])
3547 SubDemandedElts.setBit(i * SubScale);
3548
3549 for (unsigned i = 0; i != SubScale; ++i) {
3550 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3551 Depth + 1);
3552 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3553 Known.insertBits(Known2, SubBitWidth * Shifts);
3554 }
3555 }
3556
3557 // Bitcast 'large element' scalar/vector to 'small element' vector.
3558 if ((SubBitWidth % BitWidth) == 0) {
3559 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3560
3561 // Collect known bits for the (smaller) output by collecting the known
3562 // bits from the overlapping larger input elements and extracting the
3563 // sub sections we actually care about.
3564 unsigned SubScale = SubBitWidth / BitWidth;
3565 APInt SubDemandedElts =
3566 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3567 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3568
3569 Known.setAllConflict();
3570 for (unsigned i = 0; i != NumElts; ++i)
3571 if (DemandedElts[i]) {
3572 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3573 unsigned Offset = (Shifts % SubScale) * BitWidth;
3574 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3575 // If we don't know any bits, early out.
3576 if (Known.isUnknown())
3577 break;
3578 }
3579 }
3580 break;
3581 }
3582 case ISD::AND:
3583 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3584 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3585
3586 Known &= Known2;
3587 break;
3588 case ISD::OR:
3589 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3590 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3591
3592 Known |= Known2;
3593 break;
3594 case ISD::XOR:
3595 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3596 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3597
3598 Known ^= Known2;
3599 break;
3600 case ISD::MUL: {
3601 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3602 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3603 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3604 // TODO: SelfMultiply can be poison, but not undef.
3605 if (SelfMultiply)
3606 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3607 Op.getOperand(0), DemandedElts, false, Depth + 1);
3608 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3609
3610 // If the multiplication is known not to overflow, the product of a number
3611 // with itself is non-negative. Only do this if we didn't already computed
3612 // the opposite value for the sign bit.
3613 if (Op->getFlags().hasNoSignedWrap() &&
3614 Op.getOperand(0) == Op.getOperand(1) &&
3615 !Known.isNegative())
3616 Known.makeNonNegative();
3617 break;
3618 }
3619 case ISD::MULHU: {
3620 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3621 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3622 Known = KnownBits::mulhu(Known, Known2);
3623 break;
3624 }
3625 case ISD::MULHS: {
3626 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3627 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3628 Known = KnownBits::mulhs(Known, Known2);
3629 break;
3630 }
3631 case ISD::ABDU: {
3632 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3633 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3634 Known = KnownBits::abdu(Known, Known2);
3635 break;
3636 }
3637 case ISD::ABDS: {
3638 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3639 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3640 Known = KnownBits::abds(Known, Known2);
3641 unsigned SignBits1 =
3642 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3643 if (SignBits1 == 1)
3644 break;
3645 unsigned SignBits0 =
3646 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3647 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3648 break;
3649 }
3650 case ISD::UMUL_LOHI: {
3651 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3652 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3653 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3654 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3655 if (Op.getResNo() == 0)
3656 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3657 else
3658 Known = KnownBits::mulhu(Known, Known2);
3659 break;
3660 }
3661 case ISD::SMUL_LOHI: {
3662 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3663 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3664 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3665 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3666 if (Op.getResNo() == 0)
3667 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3668 else
3669 Known = KnownBits::mulhs(Known, Known2);
3670 break;
3671 }
3672 case ISD::AVGFLOORU: {
3673 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3674 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3675 Known = KnownBits::avgFloorU(Known, Known2);
3676 break;
3677 }
3678 case ISD::AVGCEILU: {
3679 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3680 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3681 Known = KnownBits::avgCeilU(Known, Known2);
3682 break;
3683 }
3684 case ISD::AVGFLOORS: {
3685 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3686 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3687 Known = KnownBits::avgFloorS(Known, Known2);
3688 break;
3689 }
3690 case ISD::AVGCEILS: {
3691 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3692 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3693 Known = KnownBits::avgCeilS(Known, Known2);
3694 break;
3695 }
3696 case ISD::SELECT:
3697 case ISD::VSELECT:
3698 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3699 // If we don't know any bits, early out.
3700 if (Known.isUnknown())
3701 break;
3702 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3703
3704 // Only known if known in both the LHS and RHS.
3705 Known = Known.intersectWith(Known2);
3706 break;
3707 case ISD::SELECT_CC:
3708 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3709 // If we don't know any bits, early out.
3710 if (Known.isUnknown())
3711 break;
3712 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3713
3714 // Only known if known in both the LHS and RHS.
3715 Known = Known.intersectWith(Known2);
3716 break;
3717 case ISD::SMULO:
3718 case ISD::UMULO:
3719 if (Op.getResNo() != 1)
3720 break;
3721 // The boolean result conforms to getBooleanContents.
3722 // If we know the result of a setcc has the top bits zero, use this info.
3723 // We know that we have an integer-based boolean since these operations
3724 // are only available for integer.
3725 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3727 BitWidth > 1)
3728 Known.Zero.setBitsFrom(1);
3729 break;
3730 case ISD::SETCC:
3731 case ISD::SETCCCARRY:
3732 case ISD::STRICT_FSETCC:
3733 case ISD::STRICT_FSETCCS: {
3734 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3735 // If we know the result of a setcc has the top bits zero, use this info.
3736 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3738 BitWidth > 1)
3739 Known.Zero.setBitsFrom(1);
3740 break;
3741 }
3742 case ISD::SHL: {
3743 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3744 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3745
3746 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3747 bool NSW = Op->getFlags().hasNoSignedWrap();
3748
3749 bool ShAmtNonZero = Known2.isNonZero();
3750
3751 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3752
3753 // Minimum shift low bits are known zero.
3754 if (std::optional<unsigned> ShMinAmt =
3755 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3756 Known.Zero.setLowBits(*ShMinAmt);
3757 break;
3758 }
3759 case ISD::SRL:
3760 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3761 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3762 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3763 Op->getFlags().hasExact());
3764
3765 // Minimum shift high bits are known zero.
3766 if (std::optional<unsigned> ShMinAmt =
3767 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3768 Known.Zero.setHighBits(*ShMinAmt);
3769 break;
3770 case ISD::SRA:
3771 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3772 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3773 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3774 Op->getFlags().hasExact());
3775 break;
3776 case ISD::ROTL:
3777 case ISD::ROTR:
3778 if (ConstantSDNode *C =
3779 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3780 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3781
3782 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3783
3784 // Canonicalize to ROTR.
3785 if (Opcode == ISD::ROTL && Amt != 0)
3786 Amt = BitWidth - Amt;
3787
3788 Known.Zero = Known.Zero.rotr(Amt);
3789 Known.One = Known.One.rotr(Amt);
3790 }
3791 break;
3792 case ISD::FSHL:
3793 case ISD::FSHR:
3794 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3795 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3796
3797 // For fshl, 0-shift returns the 1st arg.
3798 // For fshr, 0-shift returns the 2nd arg.
3799 if (Amt == 0) {
3800 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3801 DemandedElts, Depth + 1);
3802 break;
3803 }
3804
3805 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3806 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3807 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3808 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3809 if (Opcode == ISD::FSHL) {
3810 Known <<= Amt;
3811 Known2 >>= BitWidth - Amt;
3812 } else {
3813 Known <<= BitWidth - Amt;
3814 Known2 >>= Amt;
3815 }
3816 Known = Known.unionWith(Known2);
3817 }
3818 break;
3819 case ISD::SHL_PARTS:
3820 case ISD::SRA_PARTS:
3821 case ISD::SRL_PARTS: {
3822 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3823
3824 // Collect lo/hi source values and concatenate.
3825 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3826 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3827 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3828 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3829 Known = Known2.concat(Known);
3830
3831 // Collect shift amount.
3832 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3833
3834 if (Opcode == ISD::SHL_PARTS)
3835 Known = KnownBits::shl(Known, Known2);
3836 else if (Opcode == ISD::SRA_PARTS)
3837 Known = KnownBits::ashr(Known, Known2);
3838 else // if (Opcode == ISD::SRL_PARTS)
3839 Known = KnownBits::lshr(Known, Known2);
3840
3841 // TODO: Minimum shift low/high bits are known zero.
3842
3843 if (Op.getResNo() == 0)
3844 Known = Known.extractBits(LoBits, 0);
3845 else
3846 Known = Known.extractBits(HiBits, LoBits);
3847 break;
3848 }
3850 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3851 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3852 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3853 break;
3854 }
3855 case ISD::CTTZ:
3856 case ISD::CTTZ_ZERO_UNDEF: {
3857 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3858 // If we have a known 1, its position is our upper bound.
3859 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3860 unsigned LowBits = llvm::bit_width(PossibleTZ);
3861 Known.Zero.setBitsFrom(LowBits);
3862 break;
3863 }
3864 case ISD::CTLZ:
3865 case ISD::CTLZ_ZERO_UNDEF: {
3866 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3867 // If we have a known 1, its position is our upper bound.
3868 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3869 unsigned LowBits = llvm::bit_width(PossibleLZ);
3870 Known.Zero.setBitsFrom(LowBits);
3871 break;
3872 }
3873 case ISD::CTPOP: {
3874 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3875 // If we know some of the bits are zero, they can't be one.
3876 unsigned PossibleOnes = Known2.countMaxPopulation();
3877 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3878 break;
3879 }
3880 case ISD::PARITY: {
3881 // Parity returns 0 everywhere but the LSB.
3882 Known.Zero.setBitsFrom(1);
3883 break;
3884 }
3885 case ISD::MGATHER:
3886 case ISD::MLOAD: {
3887 ISD::LoadExtType ETy =
3888 (Opcode == ISD::MGATHER)
3889 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3890 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3891 if (ETy == ISD::ZEXTLOAD) {
3892 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3893 KnownBits Known0(MemVT.getScalarSizeInBits());
3894 return Known0.zext(BitWidth);
3895 }
3896 break;
3897 }
3898 case ISD::LOAD: {
3900 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3901 if (ISD::isNON_EXTLoad(LD) && Cst) {
3902 // Determine any common known bits from the loaded constant pool value.
3903 Type *CstTy = Cst->getType();
3904 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3905 !Op.getValueType().isScalableVector()) {
3906 // If its a vector splat, then we can (quickly) reuse the scalar path.
3907 // NOTE: We assume all elements match and none are UNDEF.
3908 if (CstTy->isVectorTy()) {
3909 if (const Constant *Splat = Cst->getSplatValue()) {
3910 Cst = Splat;
3911 CstTy = Cst->getType();
3912 }
3913 }
3914 // TODO - do we need to handle different bitwidths?
3915 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3916 // Iterate across all vector elements finding common known bits.
3917 Known.setAllConflict();
3918 for (unsigned i = 0; i != NumElts; ++i) {
3919 if (!DemandedElts[i])
3920 continue;
3921 if (Constant *Elt = Cst->getAggregateElement(i)) {
3922 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3923 const APInt &Value = CInt->getValue();
3924 Known.One &= Value;
3925 Known.Zero &= ~Value;
3926 continue;
3927 }
3928 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3929 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3930 Known.One &= Value;
3931 Known.Zero &= ~Value;
3932 continue;
3933 }
3934 }
3935 Known.One.clearAllBits();
3936 Known.Zero.clearAllBits();
3937 break;
3938 }
3939 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3940 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3941 Known = KnownBits::makeConstant(CInt->getValue());
3942 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3943 Known =
3944 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3945 }
3946 }
3947 }
3948 } else if (Op.getResNo() == 0) {
3949 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3950 KnownBits KnownScalarMemory(ScalarMemorySize);
3951 if (const MDNode *MD = LD->getRanges())
3952 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3953
3954 // Extend the Known bits from memory to the size of the scalar result.
3955 if (ISD::isZEXTLoad(Op.getNode()))
3956 Known = KnownScalarMemory.zext(BitWidth);
3957 else if (ISD::isSEXTLoad(Op.getNode()))
3958 Known = KnownScalarMemory.sext(BitWidth);
3959 else if (ISD::isEXTLoad(Op.getNode()))
3960 Known = KnownScalarMemory.anyext(BitWidth);
3961 else
3962 Known = KnownScalarMemory;
3963 assert(Known.getBitWidth() == BitWidth);
3964 return Known;
3965 }
3966 break;
3967 }
3969 if (Op.getValueType().isScalableVector())
3970 break;
3971 EVT InVT = Op.getOperand(0).getValueType();
3972 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3973 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3974 Known = Known.zext(BitWidth);
3975 break;
3976 }
3977 case ISD::ZERO_EXTEND: {
3978 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3979 Known = Known.zext(BitWidth);
3980 break;
3981 }
3983 if (Op.getValueType().isScalableVector())
3984 break;
3985 EVT InVT = Op.getOperand(0).getValueType();
3986 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3987 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3988 // If the sign bit is known to be zero or one, then sext will extend
3989 // it to the top bits, else it will just zext.
3990 Known = Known.sext(BitWidth);
3991 break;
3992 }
3993 case ISD::SIGN_EXTEND: {
3994 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3995 // If the sign bit is known to be zero or one, then sext will extend
3996 // it to the top bits, else it will just zext.
3997 Known = Known.sext(BitWidth);
3998 break;
3999 }
4001 if (Op.getValueType().isScalableVector())
4002 break;
4003 EVT InVT = Op.getOperand(0).getValueType();
4004 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4005 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4006 Known = Known.anyext(BitWidth);
4007 break;
4008 }
4009 case ISD::ANY_EXTEND: {
4010 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4011 Known = Known.anyext(BitWidth);
4012 break;
4013 }
4014 case ISD::TRUNCATE: {
4015 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4016 Known = Known.trunc(BitWidth);
4017 break;
4018 }
4019 case ISD::AssertZext: {
4020 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4022 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4023 Known.Zero |= (~InMask);
4024 Known.One &= (~Known.Zero);
4025 break;
4026 }
4027 case ISD::AssertAlign: {
4028 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4029 assert(LogOfAlign != 0);
4030
4031 // TODO: Should use maximum with source
4032 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4033 // well as clearing one bits.
4034 Known.Zero.setLowBits(LogOfAlign);
4035 Known.One.clearLowBits(LogOfAlign);
4036 break;
4037 }
4038 case ISD::AssertNoFPClass: {
4039 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4040
4041 FPClassTest NoFPClass =
4042 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4043 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4044 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4045 // Cannot be negative.
4046 Known.makeNonNegative();
4047 }
4048
4049 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4050 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4051 // Cannot be positive.
4052 Known.makeNegative();
4053 }
4054
4055 break;
4056 }
4057 case ISD::FGETSIGN:
4058 // All bits are zero except the low bit.
4059 Known.Zero.setBitsFrom(1);
4060 break;
4061 case ISD::ADD:
4062 case ISD::SUB: {
4063 SDNodeFlags Flags = Op.getNode()->getFlags();
4064 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4065 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4067 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4068 Flags.hasNoUnsignedWrap(), Known, Known2);
4069 break;
4070 }
4071 case ISD::USUBO:
4072 case ISD::SSUBO:
4073 case ISD::USUBO_CARRY:
4074 case ISD::SSUBO_CARRY:
4075 if (Op.getResNo() == 1) {
4076 // If we know the result of a setcc has the top bits zero, use this info.
4077 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4079 BitWidth > 1)
4080 Known.Zero.setBitsFrom(1);
4081 break;
4082 }
4083 [[fallthrough]];
4084 case ISD::SUBC: {
4085 assert(Op.getResNo() == 0 &&
4086 "We only compute knownbits for the difference here.");
4087
4088 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4089 KnownBits Borrow(1);
4090 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4091 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4092 // Borrow has bit width 1
4093 Borrow = Borrow.trunc(1);
4094 } else {
4095 Borrow.setAllZero();
4096 }
4097
4098 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4099 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4100 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4101 break;
4102 }
4103 case ISD::UADDO:
4104 case ISD::SADDO:
4105 case ISD::UADDO_CARRY:
4106 case ISD::SADDO_CARRY:
4107 if (Op.getResNo() == 1) {
4108 // If we know the result of a setcc has the top bits zero, use this info.
4109 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4111 BitWidth > 1)
4112 Known.Zero.setBitsFrom(1);
4113 break;
4114 }
4115 [[fallthrough]];
4116 case ISD::ADDC:
4117 case ISD::ADDE: {
4118 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4119
4120 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4121 KnownBits Carry(1);
4122 if (Opcode == ISD::ADDE)
4123 // Can't track carry from glue, set carry to unknown.
4124 Carry.resetAll();
4125 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4126 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4127 // Carry has bit width 1
4128 Carry = Carry.trunc(1);
4129 } else {
4130 Carry.setAllZero();
4131 }
4132
4133 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4134 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4135 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4136 break;
4137 }
4138 case ISD::UDIV: {
4139 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4140 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4141 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4142 break;
4143 }
4144 case ISD::SDIV: {
4145 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4146 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4147 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4148 break;
4149 }
4150 case ISD::SREM: {
4151 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4152 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4153 Known = KnownBits::srem(Known, Known2);
4154 break;
4155 }
4156 case ISD::UREM: {
4157 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4158 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4159 Known = KnownBits::urem(Known, Known2);
4160 break;
4161 }
4162 case ISD::EXTRACT_ELEMENT: {
4163 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4164 const unsigned Index = Op.getConstantOperandVal(1);
4165 const unsigned EltBitWidth = Op.getValueSizeInBits();
4166
4167 // Remove low part of known bits mask
4168 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4169 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4170
4171 // Remove high part of known bit mask
4172 Known = Known.trunc(EltBitWidth);
4173 break;
4174 }
4176 SDValue InVec = Op.getOperand(0);
4177 SDValue EltNo = Op.getOperand(1);
4178 EVT VecVT = InVec.getValueType();
4179 // computeKnownBits not yet implemented for scalable vectors.
4180 if (VecVT.isScalableVector())
4181 break;
4182 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4183 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4184
4185 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4186 // anything about the extended bits.
4187 if (BitWidth > EltBitWidth)
4188 Known = Known.trunc(EltBitWidth);
4189
4190 // If we know the element index, just demand that vector element, else for
4191 // an unknown element index, ignore DemandedElts and demand them all.
4192 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4193 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4194 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4195 DemandedSrcElts =
4196 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4197
4198 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4199 if (BitWidth > EltBitWidth)
4200 Known = Known.anyext(BitWidth);
4201 break;
4202 }
4204 if (Op.getValueType().isScalableVector())
4205 break;
4206
4207 // If we know the element index, split the demand between the
4208 // source vector and the inserted element, otherwise assume we need
4209 // the original demanded vector elements and the value.
4210 SDValue InVec = Op.getOperand(0);
4211 SDValue InVal = Op.getOperand(1);
4212 SDValue EltNo = Op.getOperand(2);
4213 bool DemandedVal = true;
4214 APInt DemandedVecElts = DemandedElts;
4215 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4216 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4217 unsigned EltIdx = CEltNo->getZExtValue();
4218 DemandedVal = !!DemandedElts[EltIdx];
4219 DemandedVecElts.clearBit(EltIdx);
4220 }
4221 Known.setAllConflict();
4222 if (DemandedVal) {
4223 Known2 = computeKnownBits(InVal, Depth + 1);
4224 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4225 }
4226 if (!!DemandedVecElts) {
4227 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4228 Known = Known.intersectWith(Known2);
4229 }
4230 break;
4231 }
4232 case ISD::BITREVERSE: {
4233 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4234 Known = Known2.reverseBits();
4235 break;
4236 }
4237 case ISD::BSWAP: {
4238 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4239 Known = Known2.byteSwap();
4240 break;
4241 }
4242 case ISD::ABS: {
4243 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4244 Known = Known2.abs();
4245 Known.Zero.setHighBits(
4246 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4247 break;
4248 }
4249 case ISD::USUBSAT: {
4250 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4251 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4252 Known = KnownBits::usub_sat(Known, Known2);
4253 break;
4254 }
4255 case ISD::UMIN: {
4256 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4257 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4258 Known = KnownBits::umin(Known, Known2);
4259 break;
4260 }
4261 case ISD::UMAX: {
4262 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4263 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4264 Known = KnownBits::umax(Known, Known2);
4265 break;
4266 }
4267 case ISD::SMIN:
4268 case ISD::SMAX: {
4269 // If we have a clamp pattern, we know that the number of sign bits will be
4270 // the minimum of the clamp min/max range.
4271 bool IsMax = (Opcode == ISD::SMAX);
4272 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4273 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4274 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4275 CstHigh =
4276 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4277 if (CstLow && CstHigh) {
4278 if (!IsMax)
4279 std::swap(CstLow, CstHigh);
4280
4281 const APInt &ValueLow = CstLow->getAPIntValue();
4282 const APInt &ValueHigh = CstHigh->getAPIntValue();
4283 if (ValueLow.sle(ValueHigh)) {
4284 unsigned LowSignBits = ValueLow.getNumSignBits();
4285 unsigned HighSignBits = ValueHigh.getNumSignBits();
4286 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4287 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4288 Known.One.setHighBits(MinSignBits);
4289 break;
4290 }
4291 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4292 Known.Zero.setHighBits(MinSignBits);
4293 break;
4294 }
4295 }
4296 }
4297
4298 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4299 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4300 if (IsMax)
4301 Known = KnownBits::smax(Known, Known2);
4302 else
4303 Known = KnownBits::smin(Known, Known2);
4304
4305 // For SMAX, if CstLow is non-negative we know the result will be
4306 // non-negative and thus all sign bits are 0.
4307 // TODO: There's an equivalent of this for smin with negative constant for
4308 // known ones.
4309 if (IsMax && CstLow) {
4310 const APInt &ValueLow = CstLow->getAPIntValue();
4311 if (ValueLow.isNonNegative()) {
4312 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4313 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4314 }
4315 }
4316
4317 break;
4318 }
4319 case ISD::UINT_TO_FP: {
4320 Known.makeNonNegative();
4321 break;
4322 }
4323 case ISD::SINT_TO_FP: {
4324 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4325 if (Known2.isNonNegative())
4326 Known.makeNonNegative();
4327 else if (Known2.isNegative())
4328 Known.makeNegative();
4329 break;
4330 }
4331 case ISD::FP_TO_UINT_SAT: {
4332 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4333 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4335 break;
4336 }
4337 case ISD::ATOMIC_LOAD: {
4338 // If we are looking at the loaded value.
4339 if (Op.getResNo() == 0) {
4340 auto *AT = cast<AtomicSDNode>(Op);
4341 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4342 KnownBits KnownScalarMemory(ScalarMemorySize);
4343 if (const MDNode *MD = AT->getRanges())
4344 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4345
4346 switch (AT->getExtensionType()) {
4347 case ISD::ZEXTLOAD:
4348 Known = KnownScalarMemory.zext(BitWidth);
4349 break;
4350 case ISD::SEXTLOAD:
4351 Known = KnownScalarMemory.sext(BitWidth);
4352 break;
4353 case ISD::EXTLOAD:
4354 switch (TLI->getExtendForAtomicOps()) {
4355 case ISD::ZERO_EXTEND:
4356 Known = KnownScalarMemory.zext(BitWidth);
4357 break;
4358 case ISD::SIGN_EXTEND:
4359 Known = KnownScalarMemory.sext(BitWidth);
4360 break;
4361 default:
4362 Known = KnownScalarMemory.anyext(BitWidth);
4363 break;
4364 }
4365 break;
4366 case ISD::NON_EXTLOAD:
4367 Known = KnownScalarMemory;
4368 break;
4369 }
4370 assert(Known.getBitWidth() == BitWidth);
4371 }
4372 break;
4373 }
4375 if (Op.getResNo() == 1) {
4376 // The boolean result conforms to getBooleanContents.
4377 // If we know the result of a setcc has the top bits zero, use this info.
4378 // We know that we have an integer-based boolean since these operations
4379 // are only available for integer.
4380 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4382 BitWidth > 1)
4383 Known.Zero.setBitsFrom(1);
4384 break;
4385 }
4386 [[fallthrough]];
4388 case ISD::ATOMIC_SWAP:
4399 case ISD::ATOMIC_LOAD_UMAX: {
4400 // If we are looking at the loaded value.
4401 if (Op.getResNo() == 0) {
4402 auto *AT = cast<AtomicSDNode>(Op);
4403 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4404
4405 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4406 Known.Zero.setBitsFrom(MemBits);
4407 }
4408 break;
4409 }
4410 case ISD::FrameIndex:
4412 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4413 Known, getMachineFunction());
4414 break;
4415
4416 default:
4417 if (Opcode < ISD::BUILTIN_OP_END)
4418 break;
4419 [[fallthrough]];
4423 // TODO: Probably okay to remove after audit; here to reduce change size
4424 // in initial enablement patch for scalable vectors
4425 if (Op.getValueType().isScalableVector())
4426 break;
4427
4428 // Allow the target to implement this method for its nodes.
4429 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4430 break;
4431 }
4432
4433 return Known;
4434}
4435
4436/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4449
4452 // X + 0 never overflow
4453 if (isNullConstant(N1))
4454 return OFK_Never;
4455
4456 // If both operands each have at least two sign bits, the addition
4457 // cannot overflow.
4458 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4459 return OFK_Never;
4460
4461 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4462 return OFK_Sometime;
4463}
4464
4467 // X + 0 never overflow
4468 if (isNullConstant(N1))
4469 return OFK_Never;
4470
4471 // mulhi + 1 never overflow
4472 KnownBits N1Known = computeKnownBits(N1);
4473 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4474 N1Known.getMaxValue().ult(2))
4475 return OFK_Never;
4476
4477 KnownBits N0Known = computeKnownBits(N0);
4478 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4479 N0Known.getMaxValue().ult(2))
4480 return OFK_Never;
4481
4482 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4483 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4484 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4485 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4486}
4487
4490 // X - 0 never overflow
4491 if (isNullConstant(N1))
4492 return OFK_Never;
4493
4494 // If both operands each have at least two sign bits, the subtraction
4495 // cannot overflow.
4496 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4497 return OFK_Never;
4498
4499 KnownBits N0Known = computeKnownBits(N0);
4500 KnownBits N1Known = computeKnownBits(N1);
4501 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4502 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4503 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4504}
4505
4508 // X - 0 never overflow
4509 if (isNullConstant(N1))
4510 return OFK_Never;
4511
4512 KnownBits N0Known = computeKnownBits(N0);
4513 KnownBits N1Known = computeKnownBits(N1);
4514 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4515 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4516 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4517}
4518
4521 // X * 0 and X * 1 never overflow.
4522 if (isNullConstant(N1) || isOneConstant(N1))
4523 return OFK_Never;
4524
4525 KnownBits N0Known = computeKnownBits(N0);
4526 KnownBits N1Known = computeKnownBits(N1);
4527 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4528 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4529 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4530}
4531
4534 // X * 0 and X * 1 never overflow.
4535 if (isNullConstant(N1) || isOneConstant(N1))
4536 return OFK_Never;
4537
4538 // Get the size of the result.
4539 unsigned BitWidth = N0.getScalarValueSizeInBits();
4540
4541 // Sum of the sign bits.
4542 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4543
4544 // If we have enough sign bits, then there's no overflow.
4545 if (SignBits > BitWidth + 1)
4546 return OFK_Never;
4547
4548 if (SignBits == BitWidth + 1) {
4549 // The overflow occurs when the true multiplication of the
4550 // the operands is the minimum negative number.
4551 KnownBits N0Known = computeKnownBits(N0);
4552 KnownBits N1Known = computeKnownBits(N1);
4553 // If one of the operands is non-negative, then there's no
4554 // overflow.
4555 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4556 return OFK_Never;
4557 }
4558
4559 return OFK_Sometime;
4560}
4561
4563 if (Depth >= MaxRecursionDepth)
4564 return false; // Limit search depth.
4565
4566 EVT OpVT = Val.getValueType();
4567 unsigned BitWidth = OpVT.getScalarSizeInBits();
4568
4569 // Is the constant a known power of 2?
4571 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4572 }))
4573 return true;
4574
4575 // A left-shift of a constant one will have exactly one bit set because
4576 // shifting the bit off the end is undefined.
4577 if (Val.getOpcode() == ISD::SHL) {
4578 auto *C = isConstOrConstSplat(Val.getOperand(0));
4579 if (C && C->getAPIntValue() == 1)
4580 return true;
4581 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4582 isKnownNeverZero(Val, Depth);
4583 }
4584
4585 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4586 // one bit set.
4587 if (Val.getOpcode() == ISD::SRL) {
4588 auto *C = isConstOrConstSplat(Val.getOperand(0));
4589 if (C && C->getAPIntValue().isSignMask())
4590 return true;
4591 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4592 isKnownNeverZero(Val, Depth);
4593 }
4594
4595 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4596 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4597
4598 // Are all operands of a build vector constant powers of two?
4599 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4600 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4601 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4602 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4603 return false;
4604 }))
4605 return true;
4606
4607 // Is the operand of a splat vector a constant power of two?
4608 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4610 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4611 return true;
4612
4613 // vscale(power-of-two) is a power-of-two for some targets
4614 if (Val.getOpcode() == ISD::VSCALE &&
4615 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4617 return true;
4618
4619 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4620 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4621 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4623
4624 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4625 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4627
4628 // Looking for `x & -x` pattern:
4629 // If x == 0:
4630 // x & -x -> 0
4631 // If x != 0:
4632 // x & -x -> non-zero pow2
4633 // so if we find the pattern return whether we know `x` is non-zero.
4634 SDValue X;
4635 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4636 return isKnownNeverZero(X, Depth);
4637
4638 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4639 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4640
4641 // More could be done here, though the above checks are enough
4642 // to handle some common cases.
4643 return false;
4644}
4645
4647 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4648 return C1->getValueAPF().getExactLog2Abs() >= 0;
4649
4650 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4651 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4652
4653 return false;
4654}
4655
4657 EVT VT = Op.getValueType();
4658
4659 // Since the number of lanes in a scalable vector is unknown at compile time,
4660 // we track one bit which is implicitly broadcast to all lanes. This means
4661 // that all lanes in a scalable vector are considered demanded.
4662 APInt DemandedElts = VT.isFixedLengthVector()
4664 : APInt(1, 1);
4665 return ComputeNumSignBits(Op, DemandedElts, Depth);
4666}
4667
4668unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4669 unsigned Depth) const {
4670 EVT VT = Op.getValueType();
4671 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4672 unsigned VTBits = VT.getScalarSizeInBits();
4673 unsigned NumElts = DemandedElts.getBitWidth();
4674 unsigned Tmp, Tmp2;
4675 unsigned FirstAnswer = 1;
4676
4677 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4678 const APInt &Val = C->getAPIntValue();
4679 return Val.getNumSignBits();
4680 }
4681
4682 if (Depth >= MaxRecursionDepth)
4683 return 1; // Limit search depth.
4684
4685 if (!DemandedElts)
4686 return 1; // No demanded elts, better to assume we don't know anything.
4687
4688 unsigned Opcode = Op.getOpcode();
4689 switch (Opcode) {
4690 default: break;
4691 case ISD::AssertSext:
4692 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4693 return VTBits-Tmp+1;
4694 case ISD::AssertZext:
4695 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4696 return VTBits-Tmp;
4697 case ISD::FREEZE:
4698 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4699 /*PoisonOnly=*/false))
4700 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4701 break;
4702 case ISD::MERGE_VALUES:
4703 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4704 Depth + 1);
4705 case ISD::SPLAT_VECTOR: {
4706 // Check if the sign bits of source go down as far as the truncated value.
4707 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4708 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4709 if (NumSrcSignBits > (NumSrcBits - VTBits))
4710 return NumSrcSignBits - (NumSrcBits - VTBits);
4711 break;
4712 }
4713 case ISD::BUILD_VECTOR:
4714 assert(!VT.isScalableVector());
4715 Tmp = VTBits;
4716 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4717 if (!DemandedElts[i])
4718 continue;
4719
4720 SDValue SrcOp = Op.getOperand(i);
4721 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4722 // for constant nodes to ensure we only look at the sign bits.
4724 APInt T = C->getAPIntValue().trunc(VTBits);
4725 Tmp2 = T.getNumSignBits();
4726 } else {
4727 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4728
4729 if (SrcOp.getValueSizeInBits() != VTBits) {
4730 assert(SrcOp.getValueSizeInBits() > VTBits &&
4731 "Expected BUILD_VECTOR implicit truncation");
4732 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4733 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4734 }
4735 }
4736 Tmp = std::min(Tmp, Tmp2);
4737 }
4738 return Tmp;
4739
4740 case ISD::VECTOR_COMPRESS: {
4741 SDValue Vec = Op.getOperand(0);
4742 SDValue PassThru = Op.getOperand(2);
4743 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4744 if (Tmp == 1)
4745 return 1;
4746 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4747 Tmp = std::min(Tmp, Tmp2);
4748 return Tmp;
4749 }
4750
4751 case ISD::VECTOR_SHUFFLE: {
4752 // Collect the minimum number of sign bits that are shared by every vector
4753 // element referenced by the shuffle.
4754 APInt DemandedLHS, DemandedRHS;
4756 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4757 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4758 DemandedLHS, DemandedRHS))
4759 return 1;
4760
4761 Tmp = std::numeric_limits<unsigned>::max();
4762 if (!!DemandedLHS)
4763 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4764 if (!!DemandedRHS) {
4765 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4766 Tmp = std::min(Tmp, Tmp2);
4767 }
4768 // If we don't know anything, early out and try computeKnownBits fall-back.
4769 if (Tmp == 1)
4770 break;
4771 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4772 return Tmp;
4773 }
4774
4775 case ISD::BITCAST: {
4776 if (VT.isScalableVector())
4777 break;
4778 SDValue N0 = Op.getOperand(0);
4779 EVT SrcVT = N0.getValueType();
4780 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4781
4782 // Ignore bitcasts from unsupported types..
4783 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4784 break;
4785
4786 // Fast handling of 'identity' bitcasts.
4787 if (VTBits == SrcBits)
4788 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4789
4790 bool IsLE = getDataLayout().isLittleEndian();
4791
4792 // Bitcast 'large element' scalar/vector to 'small element' vector.
4793 if ((SrcBits % VTBits) == 0) {
4794 assert(VT.isVector() && "Expected bitcast to vector");
4795
4796 unsigned Scale = SrcBits / VTBits;
4797 APInt SrcDemandedElts =
4798 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4799
4800 // Fast case - sign splat can be simply split across the small elements.
4801 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4802 if (Tmp == SrcBits)
4803 return VTBits;
4804
4805 // Slow case - determine how far the sign extends into each sub-element.
4806 Tmp2 = VTBits;
4807 for (unsigned i = 0; i != NumElts; ++i)
4808 if (DemandedElts[i]) {
4809 unsigned SubOffset = i % Scale;
4810 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4811 SubOffset = SubOffset * VTBits;
4812 if (Tmp <= SubOffset)
4813 return 1;
4814 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4815 }
4816 return Tmp2;
4817 }
4818 break;
4819 }
4820
4822 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4823 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4824 return VTBits - Tmp + 1;
4825 case ISD::SIGN_EXTEND:
4826 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4827 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4829 // Max of the input and what this extends.
4830 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4831 Tmp = VTBits-Tmp+1;
4832 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4833 return std::max(Tmp, Tmp2);
4835 if (VT.isScalableVector())
4836 break;
4837 SDValue Src = Op.getOperand(0);
4838 EVT SrcVT = Src.getValueType();
4839 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4840 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4841 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4842 }
4843 case ISD::SRA:
4844 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4845 // SRA X, C -> adds C sign bits.
4846 if (std::optional<unsigned> ShAmt =
4847 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4848 Tmp = std::min(Tmp + *ShAmt, VTBits);
4849 return Tmp;
4850 case ISD::SHL:
4851 if (std::optional<ConstantRange> ShAmtRange =
4852 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4853 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4854 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4855 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4856 // shifted out, then we can compute the number of sign bits for the
4857 // operand being extended. A future improvement could be to pass along the
4858 // "shifted left by" information in the recursive calls to
4859 // ComputeKnownSignBits. Allowing us to handle this more generically.
4860 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4861 SDValue Ext = Op.getOperand(0);
4862 EVT ExtVT = Ext.getValueType();
4863 SDValue Extendee = Ext.getOperand(0);
4864 EVT ExtendeeVT = Extendee.getValueType();
4865 unsigned SizeDifference =
4866 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4867 if (SizeDifference <= MinShAmt) {
4868 Tmp = SizeDifference +
4869 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4870 if (MaxShAmt < Tmp)
4871 return Tmp - MaxShAmt;
4872 }
4873 }
4874 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4875 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4876 if (MaxShAmt < Tmp)
4877 return Tmp - MaxShAmt;
4878 }
4879 break;
4880 case ISD::AND:
4881 case ISD::OR:
4882 case ISD::XOR: // NOT is handled here.
4883 // Logical binary ops preserve the number of sign bits at the worst.
4884 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4885 if (Tmp != 1) {
4886 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4887 FirstAnswer = std::min(Tmp, Tmp2);
4888 // We computed what we know about the sign bits as our first
4889 // answer. Now proceed to the generic code that uses
4890 // computeKnownBits, and pick whichever answer is better.
4891 }
4892 break;
4893
4894 case ISD::SELECT:
4895 case ISD::VSELECT:
4896 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4897 if (Tmp == 1) return 1; // Early out.
4898 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4899 return std::min(Tmp, Tmp2);
4900 case ISD::SELECT_CC:
4901 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4902 if (Tmp == 1) return 1; // Early out.
4903 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4904 return std::min(Tmp, Tmp2);
4905
4906 case ISD::SMIN:
4907 case ISD::SMAX: {
4908 // If we have a clamp pattern, we know that the number of sign bits will be
4909 // the minimum of the clamp min/max range.
4910 bool IsMax = (Opcode == ISD::SMAX);
4911 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4912 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4913 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4914 CstHigh =
4915 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4916 if (CstLow && CstHigh) {
4917 if (!IsMax)
4918 std::swap(CstLow, CstHigh);
4919 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4920 Tmp = CstLow->getAPIntValue().getNumSignBits();
4921 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4922 return std::min(Tmp, Tmp2);
4923 }
4924 }
4925
4926 // Fallback - just get the minimum number of sign bits of the operands.
4927 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4928 if (Tmp == 1)
4929 return 1; // Early out.
4930 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4931 return std::min(Tmp, Tmp2);
4932 }
4933 case ISD::UMIN:
4934 case ISD::UMAX:
4935 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4936 if (Tmp == 1)
4937 return 1; // Early out.
4938 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4939 return std::min(Tmp, Tmp2);
4940 case ISD::SSUBO_CARRY:
4941 case ISD::USUBO_CARRY:
4942 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4943 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4944 return VTBits;
4945 [[fallthrough]];
4946 case ISD::SADDO:
4947 case ISD::UADDO:
4948 case ISD::SADDO_CARRY:
4949 case ISD::UADDO_CARRY:
4950 case ISD::SSUBO:
4951 case ISD::USUBO:
4952 case ISD::SMULO:
4953 case ISD::UMULO:
4954 if (Op.getResNo() != 1)
4955 break;
4956 // The boolean result conforms to getBooleanContents. Fall through.
4957 // If setcc returns 0/-1, all bits are sign bits.
4958 // We know that we have an integer-based boolean since these operations
4959 // are only available for integer.
4960 if (TLI->getBooleanContents(VT.isVector(), false) ==
4962 return VTBits;
4963 break;
4964 case ISD::SETCC:
4965 case ISD::SETCCCARRY:
4966 case ISD::STRICT_FSETCC:
4967 case ISD::STRICT_FSETCCS: {
4968 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4969 // If setcc returns 0/-1, all bits are sign bits.
4970 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4972 return VTBits;
4973 break;
4974 }
4975 case ISD::ROTL:
4976 case ISD::ROTR:
4977 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4978
4979 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4980 if (Tmp == VTBits)
4981 return VTBits;
4982
4983 if (ConstantSDNode *C =
4984 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4985 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4986
4987 // Handle rotate right by N like a rotate left by 32-N.
4988 if (Opcode == ISD::ROTR)
4989 RotAmt = (VTBits - RotAmt) % VTBits;
4990
4991 // If we aren't rotating out all of the known-in sign bits, return the
4992 // number that are left. This handles rotl(sext(x), 1) for example.
4993 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4994 }
4995 break;
4996 case ISD::ADD:
4997 case ISD::ADDC:
4998 // TODO: Move Operand 1 check before Operand 0 check
4999 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5000 if (Tmp == 1) return 1; // Early out.
5001
5002 // Special case decrementing a value (ADD X, -1):
5003 if (ConstantSDNode *CRHS =
5004 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5005 if (CRHS->isAllOnes()) {
5006 KnownBits Known =
5007 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5008
5009 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5010 // sign bits set.
5011 if ((Known.Zero | 1).isAllOnes())
5012 return VTBits;
5013
5014 // If we are subtracting one from a positive number, there is no carry
5015 // out of the result.
5016 if (Known.isNonNegative())
5017 return Tmp;
5018 }
5019
5020 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5021 if (Tmp2 == 1) return 1; // Early out.
5022
5023 // Add can have at most one carry bit. Thus we know that the output
5024 // is, at worst, one more bit than the inputs.
5025 return std::min(Tmp, Tmp2) - 1;
5026 case ISD::SUB:
5027 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5028 if (Tmp2 == 1) return 1; // Early out.
5029
5030 // Handle NEG.
5031 if (ConstantSDNode *CLHS =
5032 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5033 if (CLHS->isZero()) {
5034 KnownBits Known =
5035 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5036 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5037 // sign bits set.
5038 if ((Known.Zero | 1).isAllOnes())
5039 return VTBits;
5040
5041 // If the input is known to be positive (the sign bit is known clear),
5042 // the output of the NEG has the same number of sign bits as the input.
5043 if (Known.isNonNegative())
5044 return Tmp2;
5045
5046 // Otherwise, we treat this like a SUB.
5047 }
5048
5049 // Sub can have at most one carry bit. Thus we know that the output
5050 // is, at worst, one more bit than the inputs.
5051 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5052 if (Tmp == 1) return 1; // Early out.
5053 return std::min(Tmp, Tmp2) - 1;
5054 case ISD::MUL: {
5055 // The output of the Mul can be at most twice the valid bits in the inputs.
5056 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5057 if (SignBitsOp0 == 1)
5058 break;
5059 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5060 if (SignBitsOp1 == 1)
5061 break;
5062 unsigned OutValidBits =
5063 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5064 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5065 }
5066 case ISD::AVGCEILS:
5067 case ISD::AVGFLOORS:
5068 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5069 if (Tmp == 1)
5070 return 1; // Early out.
5071 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5072 return std::min(Tmp, Tmp2);
5073 case ISD::SREM:
5074 // The sign bit is the LHS's sign bit, except when the result of the
5075 // remainder is zero. The magnitude of the result should be less than or
5076 // equal to the magnitude of the LHS. Therefore, the result should have
5077 // at least as many sign bits as the left hand side.
5078 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5079 case ISD::TRUNCATE: {
5080 // Check if the sign bits of source go down as far as the truncated value.
5081 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5082 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5083 if (NumSrcSignBits > (NumSrcBits - VTBits))
5084 return NumSrcSignBits - (NumSrcBits - VTBits);
5085 break;
5086 }
5087 case ISD::EXTRACT_ELEMENT: {
5088 if (VT.isScalableVector())
5089 break;
5090 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5091 const int BitWidth = Op.getValueSizeInBits();
5092 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5093
5094 // Get reverse index (starting from 1), Op1 value indexes elements from
5095 // little end. Sign starts at big end.
5096 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5097
5098 // If the sign portion ends in our element the subtraction gives correct
5099 // result. Otherwise it gives either negative or > bitwidth result
5100 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5101 }
5103 if (VT.isScalableVector())
5104 break;
5105 // If we know the element index, split the demand between the
5106 // source vector and the inserted element, otherwise assume we need
5107 // the original demanded vector elements and the value.
5108 SDValue InVec = Op.getOperand(0);
5109 SDValue InVal = Op.getOperand(1);
5110 SDValue EltNo = Op.getOperand(2);
5111 bool DemandedVal = true;
5112 APInt DemandedVecElts = DemandedElts;
5113 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5114 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5115 unsigned EltIdx = CEltNo->getZExtValue();
5116 DemandedVal = !!DemandedElts[EltIdx];
5117 DemandedVecElts.clearBit(EltIdx);
5118 }
5119 Tmp = std::numeric_limits<unsigned>::max();
5120 if (DemandedVal) {
5121 // TODO - handle implicit truncation of inserted elements.
5122 if (InVal.getScalarValueSizeInBits() != VTBits)
5123 break;
5124 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5125 Tmp = std::min(Tmp, Tmp2);
5126 }
5127 if (!!DemandedVecElts) {
5128 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5129 Tmp = std::min(Tmp, Tmp2);
5130 }
5131 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5132 return Tmp;
5133 }
5135 assert(!VT.isScalableVector());
5136 SDValue InVec = Op.getOperand(0);
5137 SDValue EltNo = Op.getOperand(1);
5138 EVT VecVT = InVec.getValueType();
5139 // ComputeNumSignBits not yet implemented for scalable vectors.
5140 if (VecVT.isScalableVector())
5141 break;
5142 const unsigned BitWidth = Op.getValueSizeInBits();
5143 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5144 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5145
5146 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5147 // anything about sign bits. But if the sizes match we can derive knowledge
5148 // about sign bits from the vector operand.
5149 if (BitWidth != EltBitWidth)
5150 break;
5151
5152 // If we know the element index, just demand that vector element, else for
5153 // an unknown element index, ignore DemandedElts and demand them all.
5154 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5155 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5156 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5157 DemandedSrcElts =
5158 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5159
5160 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5161 }
5163 // Offset the demanded elts by the subvector index.
5164 SDValue Src = Op.getOperand(0);
5165 // Bail until we can represent demanded elements for scalable vectors.
5166 if (Src.getValueType().isScalableVector())
5167 break;
5168 uint64_t Idx = Op.getConstantOperandVal(1);
5169 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5170 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5171 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5172 }
5173 case ISD::CONCAT_VECTORS: {
5174 if (VT.isScalableVector())
5175 break;
5176 // Determine the minimum number of sign bits across all demanded
5177 // elts of the input vectors. Early out if the result is already 1.
5178 Tmp = std::numeric_limits<unsigned>::max();
5179 EVT SubVectorVT = Op.getOperand(0).getValueType();
5180 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5181 unsigned NumSubVectors = Op.getNumOperands();
5182 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5183 APInt DemandedSub =
5184 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5185 if (!DemandedSub)
5186 continue;
5187 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5188 Tmp = std::min(Tmp, Tmp2);
5189 }
5190 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5191 return Tmp;
5192 }
5193 case ISD::INSERT_SUBVECTOR: {
5194 if (VT.isScalableVector())
5195 break;
5196 // Demand any elements from the subvector and the remainder from the src its
5197 // inserted into.
5198 SDValue Src = Op.getOperand(0);
5199 SDValue Sub = Op.getOperand(1);
5200 uint64_t Idx = Op.getConstantOperandVal(2);
5201 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5202 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5203 APInt DemandedSrcElts = DemandedElts;
5204 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5205
5206 Tmp = std::numeric_limits<unsigned>::max();
5207 if (!!DemandedSubElts) {
5208 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5209 if (Tmp == 1)
5210 return 1; // early-out
5211 }
5212 if (!!DemandedSrcElts) {
5213 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5214 Tmp = std::min(Tmp, Tmp2);
5215 }
5216 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5217 return Tmp;
5218 }
5219 case ISD::LOAD: {
5220 // If we are looking at the loaded value of the SDNode.
5221 if (Op.getResNo() != 0)
5222 break;
5223
5225 if (const MDNode *Ranges = LD->getRanges()) {
5226 if (DemandedElts != 1)
5227 break;
5228
5230 if (VTBits > CR.getBitWidth()) {
5231 switch (LD->getExtensionType()) {
5232 case ISD::SEXTLOAD:
5233 CR = CR.signExtend(VTBits);
5234 break;
5235 case ISD::ZEXTLOAD:
5236 CR = CR.zeroExtend(VTBits);
5237 break;
5238 default:
5239 break;
5240 }
5241 }
5242
5243 if (VTBits != CR.getBitWidth())
5244 break;
5245 return std::min(CR.getSignedMin().getNumSignBits(),
5247 }
5248
5249 unsigned ExtType = LD->getExtensionType();
5250 switch (ExtType) {
5251 default:
5252 break;
5253 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5254 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5255 return VTBits - Tmp + 1;
5256 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5257 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5258 return VTBits - Tmp;
5259 case ISD::NON_EXTLOAD:
5260 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5261 // We only need to handle vectors - computeKnownBits should handle
5262 // scalar cases.
5263 Type *CstTy = Cst->getType();
5264 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5265 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5266 VTBits == CstTy->getScalarSizeInBits()) {
5267 Tmp = VTBits;
5268 for (unsigned i = 0; i != NumElts; ++i) {
5269 if (!DemandedElts[i])
5270 continue;
5271 if (Constant *Elt = Cst->getAggregateElement(i)) {
5272 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5273 const APInt &Value = CInt->getValue();
5274 Tmp = std::min(Tmp, Value.getNumSignBits());
5275 continue;
5276 }
5277 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5278 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5279 Tmp = std::min(Tmp, Value.getNumSignBits());
5280 continue;
5281 }
5282 }
5283 // Unknown type. Conservatively assume no bits match sign bit.
5284 return 1;
5285 }
5286 return Tmp;
5287 }
5288 }
5289 break;
5290 }
5291
5292 break;
5293 }
5296 case ISD::ATOMIC_SWAP:
5308 case ISD::ATOMIC_LOAD: {
5309 auto *AT = cast<AtomicSDNode>(Op);
5310 // If we are looking at the loaded value.
5311 if (Op.getResNo() == 0) {
5312 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5313 if (Tmp == VTBits)
5314 return 1; // early-out
5315
5316 // For atomic_load, prefer to use the extension type.
5317 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5318 switch (AT->getExtensionType()) {
5319 default:
5320 break;
5321 case ISD::SEXTLOAD:
5322 return VTBits - Tmp + 1;
5323 case ISD::ZEXTLOAD:
5324 return VTBits - Tmp;
5325 }
5326 }
5327
5328 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5329 return VTBits - Tmp + 1;
5330 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5331 return VTBits - Tmp;
5332 }
5333 break;
5334 }
5335 }
5336
5337 // Allow the target to implement this method for its nodes.
5338 if (Opcode >= ISD::BUILTIN_OP_END ||
5339 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5340 Opcode == ISD::INTRINSIC_W_CHAIN ||
5341 Opcode == ISD::INTRINSIC_VOID) {
5342 // TODO: This can probably be removed once target code is audited. This
5343 // is here purely to reduce patch size and review complexity.
5344 if (!VT.isScalableVector()) {
5345 unsigned NumBits =
5346 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5347 if (NumBits > 1)
5348 FirstAnswer = std::max(FirstAnswer, NumBits);
5349 }
5350 }
5351
5352 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5353 // use this information.
5354 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5355 return std::max(FirstAnswer, Known.countMinSignBits());
5356}
5357
5359 unsigned Depth) const {
5360 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5361 return Op.getScalarValueSizeInBits() - SignBits + 1;
5362}
5363
5365 const APInt &DemandedElts,
5366 unsigned Depth) const {
5367 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5368 return Op.getScalarValueSizeInBits() - SignBits + 1;
5369}
5370
5372 unsigned Depth) const {
5373 // Early out for FREEZE.
5374 if (Op.getOpcode() == ISD::FREEZE)
5375 return true;
5376
5377 EVT VT = Op.getValueType();
5378 APInt DemandedElts = VT.isFixedLengthVector()
5380 : APInt(1, 1);
5381 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5382}
5383
5385 const APInt &DemandedElts,
5386 bool PoisonOnly,
5387 unsigned Depth) const {
5388 unsigned Opcode = Op.getOpcode();
5389
5390 // Early out for FREEZE.
5391 if (Opcode == ISD::FREEZE)
5392 return true;
5393
5394 if (Depth >= MaxRecursionDepth)
5395 return false; // Limit search depth.
5396
5397 if (isIntOrFPConstant(Op))
5398 return true;
5399
5400 switch (Opcode) {
5401 case ISD::CONDCODE:
5402 case ISD::VALUETYPE:
5403 case ISD::FrameIndex:
5405 case ISD::CopyFromReg:
5406 return true;
5407
5408 case ISD::POISON:
5409 return false;
5410
5411 case ISD::UNDEF:
5412 return PoisonOnly;
5413
5414 case ISD::BUILD_VECTOR:
5415 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5416 // this shouldn't affect the result.
5417 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5418 if (!DemandedElts[i])
5419 continue;
5421 Depth + 1))
5422 return false;
5423 }
5424 return true;
5425
5427 SDValue Src = Op.getOperand(0);
5428 if (Src.getValueType().isScalableVector())
5429 break;
5430 uint64_t Idx = Op.getConstantOperandVal(1);
5431 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5432 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5433 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5434 Depth + 1);
5435 }
5436
5437 case ISD::INSERT_SUBVECTOR: {
5438 if (Op.getValueType().isScalableVector())
5439 break;
5440 SDValue Src = Op.getOperand(0);
5441 SDValue Sub = Op.getOperand(1);
5442 uint64_t Idx = Op.getConstantOperandVal(2);
5443 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5444 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5445 APInt DemandedSrcElts = DemandedElts;
5446 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5447
5448 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5449 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5450 return false;
5451 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5452 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5453 return false;
5454 return true;
5455 }
5456
5458 SDValue Src = Op.getOperand(0);
5459 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5460 EVT SrcVT = Src.getValueType();
5461 if (SrcVT.isFixedLengthVector() && IndexC &&
5462 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5463 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5464 IndexC->getZExtValue());
5465 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5466 Depth + 1);
5467 }
5468 break;
5469 }
5470
5472 SDValue InVec = Op.getOperand(0);
5473 SDValue InVal = Op.getOperand(1);
5474 SDValue EltNo = Op.getOperand(2);
5475 EVT VT = InVec.getValueType();
5476 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5477 if (IndexC && VT.isFixedLengthVector() &&
5478 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5479 if (DemandedElts[IndexC->getZExtValue()] &&
5481 return false;
5482 APInt InVecDemandedElts = DemandedElts;
5483 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5484 if (!!InVecDemandedElts &&
5486 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5487 InVecDemandedElts, PoisonOnly, Depth + 1))
5488 return false;
5489 return true;
5490 }
5491 break;
5492 }
5493
5495 // Check upper (known undef) elements.
5496 if (DemandedElts.ugt(1) && !PoisonOnly)
5497 return false;
5498 // Check element zero.
5499 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5500 Op.getOperand(0), PoisonOnly, Depth + 1))
5501 return false;
5502 return true;
5503
5504 case ISD::SPLAT_VECTOR:
5505 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5506 Depth + 1);
5507
5508 case ISD::VECTOR_SHUFFLE: {
5509 APInt DemandedLHS, DemandedRHS;
5510 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5511 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5512 DemandedElts, DemandedLHS, DemandedRHS,
5513 /*AllowUndefElts=*/false))
5514 return false;
5515 if (!DemandedLHS.isZero() &&
5516 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5517 PoisonOnly, Depth + 1))
5518 return false;
5519 if (!DemandedRHS.isZero() &&
5520 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5521 PoisonOnly, Depth + 1))
5522 return false;
5523 return true;
5524 }
5525
5526 case ISD::SHL:
5527 case ISD::SRL:
5528 case ISD::SRA:
5529 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5530 // enough to check operand 0 if Op can't create undef/poison.
5531 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5532 /*ConsiderFlags*/ true, Depth) &&
5533 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5534 PoisonOnly, Depth + 1);
5535
5536 case ISD::BSWAP:
5537 case ISD::CTPOP:
5538 case ISD::BITREVERSE:
5539 case ISD::AND:
5540 case ISD::OR:
5541 case ISD::XOR:
5542 case ISD::ADD:
5543 case ISD::SUB:
5544 case ISD::MUL:
5545 case ISD::SADDSAT:
5546 case ISD::UADDSAT:
5547 case ISD::SSUBSAT:
5548 case ISD::USUBSAT:
5549 case ISD::SSHLSAT:
5550 case ISD::USHLSAT:
5551 case ISD::SMIN:
5552 case ISD::SMAX:
5553 case ISD::UMIN:
5554 case ISD::UMAX:
5555 case ISD::ZERO_EXTEND:
5556 case ISD::SIGN_EXTEND:
5557 case ISD::ANY_EXTEND:
5558 case ISD::TRUNCATE:
5559 case ISD::VSELECT: {
5560 // If Op can't create undef/poison and none of its operands are undef/poison
5561 // then Op is never undef/poison. A difference from the more common check
5562 // below, outside the switch, is that we handle elementwise operations for
5563 // which the DemandedElts mask is valid for all operands here.
5564 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5565 /*ConsiderFlags*/ true, Depth) &&
5566 all_of(Op->ops(), [&](SDValue V) {
5567 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5568 PoisonOnly, Depth + 1);
5569 });
5570 }
5571
5572 // TODO: Search for noundef attributes from library functions.
5573
5574 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5575
5576 default:
5577 // Allow the target to implement this method for its nodes.
5578 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5579 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5580 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5581 Op, DemandedElts, *this, PoisonOnly, Depth);
5582 break;
5583 }
5584
5585 // If Op can't create undef/poison and none of its operands are undef/poison
5586 // then Op is never undef/poison.
5587 // NOTE: TargetNodes can handle this in themselves in
5588 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5589 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5590 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5591 Depth) &&
5592 all_of(Op->ops(), [&](SDValue V) {
5593 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5594 });
5595}
5596
5598 bool ConsiderFlags,
5599 unsigned Depth) const {
5600 EVT VT = Op.getValueType();
5601 APInt DemandedElts = VT.isFixedLengthVector()
5603 : APInt(1, 1);
5604 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5605 Depth);
5606}
5607
5609 bool PoisonOnly, bool ConsiderFlags,
5610 unsigned Depth) const {
5611 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5612 return true;
5613
5614 unsigned Opcode = Op.getOpcode();
5615 switch (Opcode) {
5616 case ISD::AssertSext:
5617 case ISD::AssertZext:
5618 case ISD::AssertAlign:
5620 // Assertion nodes can create poison if the assertion fails.
5621 return true;
5622
5623 case ISD::FREEZE:
5627 case ISD::SADDSAT:
5628 case ISD::UADDSAT:
5629 case ISD::SSUBSAT:
5630 case ISD::USUBSAT:
5631 case ISD::MULHU:
5632 case ISD::MULHS:
5633 case ISD::AVGFLOORS:
5634 case ISD::AVGFLOORU:
5635 case ISD::AVGCEILS:
5636 case ISD::AVGCEILU:
5637 case ISD::ABDU:
5638 case ISD::ABDS:
5639 case ISD::SMIN:
5640 case ISD::SMAX:
5641 case ISD::SCMP:
5642 case ISD::UMIN:
5643 case ISD::UMAX:
5644 case ISD::UCMP:
5645 case ISD::AND:
5646 case ISD::XOR:
5647 case ISD::ROTL:
5648 case ISD::ROTR:
5649 case ISD::FSHL:
5650 case ISD::FSHR:
5651 case ISD::BSWAP:
5652 case ISD::CTTZ:
5653 case ISD::CTLZ:
5654 case ISD::CTLS:
5655 case ISD::CTPOP:
5656 case ISD::BITREVERSE:
5657 case ISD::PARITY:
5658 case ISD::SIGN_EXTEND:
5659 case ISD::TRUNCATE:
5663 case ISD::BITCAST:
5664 case ISD::BUILD_VECTOR:
5665 case ISD::BUILD_PAIR:
5666 case ISD::SPLAT_VECTOR:
5667 case ISD::FABS:
5668 return false;
5669
5670 case ISD::ABS:
5671 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5672 // Different to Intrinsic::abs.
5673 return false;
5674
5675 case ISD::ADDC:
5676 case ISD::SUBC:
5677 case ISD::ADDE:
5678 case ISD::SUBE:
5679 case ISD::SADDO:
5680 case ISD::SSUBO:
5681 case ISD::SMULO:
5682 case ISD::SADDO_CARRY:
5683 case ISD::SSUBO_CARRY:
5684 case ISD::UADDO:
5685 case ISD::USUBO:
5686 case ISD::UMULO:
5687 case ISD::UADDO_CARRY:
5688 case ISD::USUBO_CARRY:
5689 // No poison on result or overflow flags.
5690 return false;
5691
5692 case ISD::SELECT_CC:
5693 case ISD::SETCC: {
5694 // Integer setcc cannot create undef or poison.
5695 if (Op.getOperand(0).getValueType().isInteger())
5696 return false;
5697
5698 // FP compares are more complicated. They can create poison for nan/infinity
5699 // based on options and flags. The options and flags also cause special
5700 // nonan condition codes to be used. Those condition codes may be preserved
5701 // even if the nonan flag is dropped somewhere.
5702 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5703 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5704 return (unsigned)CCCode & 0x10U;
5705 }
5706
5707 case ISD::OR:
5708 case ISD::ZERO_EXTEND:
5709 case ISD::SELECT:
5710 case ISD::VSELECT:
5711 case ISD::ADD:
5712 case ISD::SUB:
5713 case ISD::MUL:
5714 case ISD::FNEG:
5715 case ISD::FADD:
5716 case ISD::FSUB:
5717 case ISD::FMUL:
5718 case ISD::FDIV:
5719 case ISD::FREM:
5720 case ISD::FCOPYSIGN:
5721 case ISD::FMA:
5722 case ISD::FMAD:
5723 case ISD::FMULADD:
5724 case ISD::FP_EXTEND:
5727 // No poison except from flags (which is handled above)
5728 return false;
5729
5730 case ISD::SHL:
5731 case ISD::SRL:
5732 case ISD::SRA:
5733 // If the max shift amount isn't in range, then the shift can
5734 // create poison.
5735 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5736
5739 // If the amount is zero then the result will be poison.
5740 // TODO: Add isKnownNeverZero DemandedElts handling.
5741 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5742
5744 // Check if we demand any upper (undef) elements.
5745 return !PoisonOnly && DemandedElts.ugt(1);
5746
5749 // Ensure that the element index is in bounds.
5750 EVT VecVT = Op.getOperand(0).getValueType();
5751 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5752 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5753 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5754 }
5755
5756 case ISD::VECTOR_SHUFFLE: {
5757 // Check for any demanded shuffle element that is undef.
5758 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5759 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5760 if (Elt < 0 && DemandedElts[Idx])
5761 return true;
5762 return false;
5763 }
5764
5766 return false;
5767
5768 default:
5769 // Allow the target to implement this method for its nodes.
5770 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5771 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5772 return TLI->canCreateUndefOrPoisonForTargetNode(
5773 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5774 break;
5775 }
5776
5777 // Be conservative and return true.
5778 return true;
5779}
5780
5781bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5782 unsigned Opcode = Op.getOpcode();
5783 if (Opcode == ISD::OR)
5784 return Op->getFlags().hasDisjoint() ||
5785 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5786 if (Opcode == ISD::XOR)
5787 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5788 return false;
5789}
5790
5792 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5793 (Op.isAnyAdd() || isADDLike(Op));
5794}
5795
5797 unsigned Depth) const {
5798 EVT VT = Op.getValueType();
5799
5800 // Since the number of lanes in a scalable vector is unknown at compile time,
5801 // we track one bit which is implicitly broadcast to all lanes. This means
5802 // that all lanes in a scalable vector are considered demanded.
5803 APInt DemandedElts = VT.isFixedLengthVector()
5805 : APInt(1, 1);
5806
5807 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5808}
5809
5811 bool SNaN, unsigned Depth) const {
5812 assert(!DemandedElts.isZero() && "No demanded elements");
5813
5814 // If we're told that NaNs won't happen, assume they won't.
5815 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5816 return true;
5817
5818 if (Depth >= MaxRecursionDepth)
5819 return false; // Limit search depth.
5820
5821 // If the value is a constant, we can obviously see if it is a NaN or not.
5823 return !C->getValueAPF().isNaN() ||
5824 (SNaN && !C->getValueAPF().isSignaling());
5825 }
5826
5827 unsigned Opcode = Op.getOpcode();
5828 switch (Opcode) {
5829 case ISD::FADD:
5830 case ISD::FSUB:
5831 case ISD::FMUL:
5832 case ISD::FDIV:
5833 case ISD::FREM:
5834 case ISD::FSIN:
5835 case ISD::FCOS:
5836 case ISD::FTAN:
5837 case ISD::FASIN:
5838 case ISD::FACOS:
5839 case ISD::FATAN:
5840 case ISD::FATAN2:
5841 case ISD::FSINH:
5842 case ISD::FCOSH:
5843 case ISD::FTANH:
5844 case ISD::FMA:
5845 case ISD::FMULADD:
5846 case ISD::FMAD: {
5847 if (SNaN)
5848 return true;
5849 // TODO: Need isKnownNeverInfinity
5850 return false;
5851 }
5852 case ISD::FCANONICALIZE:
5853 case ISD::FEXP:
5854 case ISD::FEXP2:
5855 case ISD::FEXP10:
5856 case ISD::FTRUNC:
5857 case ISD::FFLOOR:
5858 case ISD::FCEIL:
5859 case ISD::FROUND:
5860 case ISD::FROUNDEVEN:
5861 case ISD::LROUND:
5862 case ISD::LLROUND:
5863 case ISD::FRINT:
5864 case ISD::LRINT:
5865 case ISD::LLRINT:
5866 case ISD::FNEARBYINT:
5867 case ISD::FLDEXP: {
5868 if (SNaN)
5869 return true;
5870 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5871 }
5872 case ISD::FABS:
5873 case ISD::FNEG:
5874 case ISD::FCOPYSIGN: {
5875 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5876 }
5877 case ISD::SELECT:
5878 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5879 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5880 case ISD::FP_EXTEND:
5881 case ISD::FP_ROUND: {
5882 if (SNaN)
5883 return true;
5884 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5885 }
5886 case ISD::SINT_TO_FP:
5887 case ISD::UINT_TO_FP:
5888 return true;
5889 case ISD::FSQRT: // Need is known positive
5890 case ISD::FLOG:
5891 case ISD::FLOG2:
5892 case ISD::FLOG10:
5893 case ISD::FPOWI:
5894 case ISD::FPOW: {
5895 if (SNaN)
5896 return true;
5897 // TODO: Refine on operand
5898 return false;
5899 }
5900 case ISD::FMINNUM:
5901 case ISD::FMAXNUM:
5902 case ISD::FMINIMUMNUM:
5903 case ISD::FMAXIMUMNUM: {
5904 // Only one needs to be known not-nan, since it will be returned if the
5905 // other ends up being one.
5906 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5907 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5908 }
5909 case ISD::FMINNUM_IEEE:
5910 case ISD::FMAXNUM_IEEE: {
5911 if (SNaN)
5912 return true;
5913 // This can return a NaN if either operand is an sNaN, or if both operands
5914 // are NaN.
5915 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5916 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5917 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5918 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5919 }
5920 case ISD::FMINIMUM:
5921 case ISD::FMAXIMUM: {
5922 // TODO: Does this quiet or return the origina NaN as-is?
5923 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5924 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5925 }
5927 SDValue Src = Op.getOperand(0);
5928 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5929 EVT SrcVT = Src.getValueType();
5930 if (SrcVT.isFixedLengthVector() && Idx &&
5931 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5932 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5933 Idx->getZExtValue());
5934 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5935 }
5936 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5937 }
5939 SDValue Src = Op.getOperand(0);
5940 if (Src.getValueType().isFixedLengthVector()) {
5941 unsigned Idx = Op.getConstantOperandVal(1);
5942 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5943 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5944 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5945 }
5946 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5947 }
5948 case ISD::INSERT_SUBVECTOR: {
5949 SDValue BaseVector = Op.getOperand(0);
5950 SDValue SubVector = Op.getOperand(1);
5951 EVT BaseVectorVT = BaseVector.getValueType();
5952 if (BaseVectorVT.isFixedLengthVector()) {
5953 unsigned Idx = Op.getConstantOperandVal(2);
5954 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5955 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5956
5957 // Clear/Extract the bits at the position where the subvector will be
5958 // inserted.
5959 APInt DemandedMask =
5960 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5961 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5962 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5963
5964 bool NeverNaN = true;
5965 if (!DemandedSrcElts.isZero())
5966 NeverNaN &=
5967 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5968 if (NeverNaN && !DemandedSubElts.isZero())
5969 NeverNaN &=
5970 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
5971 return NeverNaN;
5972 }
5973 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
5974 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
5975 }
5976 case ISD::BUILD_VECTOR: {
5977 unsigned NumElts = Op.getNumOperands();
5978 for (unsigned I = 0; I != NumElts; ++I)
5979 if (DemandedElts[I] &&
5980 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
5981 return false;
5982 return true;
5983 }
5984 case ISD::AssertNoFPClass: {
5985 FPClassTest NoFPClass =
5986 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
5987 if ((NoFPClass & fcNan) == fcNan)
5988 return true;
5989 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
5990 return true;
5991 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5992 }
5993 default:
5994 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5995 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
5996 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
5997 Depth);
5998 }
5999
6000 return false;
6001 }
6002}
6003
6005 assert(Op.getValueType().isFloatingPoint() &&
6006 "Floating point type expected");
6007
6008 // If the value is a constant, we can obviously see if it is a zero or not.
6010 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6011}
6012
6014 if (Depth >= MaxRecursionDepth)
6015 return false; // Limit search depth.
6016
6017 assert(!Op.getValueType().isFloatingPoint() &&
6018 "Floating point types unsupported - use isKnownNeverZeroFloat");
6019
6020 // If the value is a constant, we can obviously see if it is a zero or not.
6022 [](ConstantSDNode *C) { return !C->isZero(); }))
6023 return true;
6024
6025 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6026 // some degree.
6027 switch (Op.getOpcode()) {
6028 default:
6029 break;
6030
6031 case ISD::OR:
6032 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6033 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6034
6035 case ISD::VSELECT:
6036 case ISD::SELECT:
6037 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6038 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6039
6040 case ISD::SHL: {
6041 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6042 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6043 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6044 // 1 << X is never zero.
6045 if (ValKnown.One[0])
6046 return true;
6047 // If max shift cnt of known ones is non-zero, result is non-zero.
6048 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6049 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6050 !ValKnown.One.shl(MaxCnt).isZero())
6051 return true;
6052 break;
6053 }
6054 case ISD::UADDSAT:
6055 case ISD::UMAX:
6056 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6057 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6058
6059 // For smin/smax: If either operand is known negative/positive
6060 // respectively we don't need the other to be known at all.
6061 case ISD::SMAX: {
6062 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6063 if (Op1.isStrictlyPositive())
6064 return true;
6065
6066 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6067 if (Op0.isStrictlyPositive())
6068 return true;
6069
6070 if (Op1.isNonZero() && Op0.isNonZero())
6071 return true;
6072
6073 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6074 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6075 }
6076 case ISD::SMIN: {
6077 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6078 if (Op1.isNegative())
6079 return true;
6080
6081 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6082 if (Op0.isNegative())
6083 return true;
6084
6085 if (Op1.isNonZero() && Op0.isNonZero())
6086 return true;
6087
6088 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6089 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6090 }
6091 case ISD::UMIN:
6092 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6093 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6094
6095 case ISD::ROTL:
6096 case ISD::ROTR:
6097 case ISD::BITREVERSE:
6098 case ISD::BSWAP:
6099 case ISD::CTPOP:
6100 case ISD::ABS:
6101 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6102
6103 case ISD::SRA:
6104 case ISD::SRL: {
6105 if (Op->getFlags().hasExact())
6106 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6107 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6108 if (ValKnown.isNegative())
6109 return true;
6110 // If max shift cnt of known ones is non-zero, result is non-zero.
6111 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6112 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6113 !ValKnown.One.lshr(MaxCnt).isZero())
6114 return true;
6115 break;
6116 }
6117 case ISD::UDIV:
6118 case ISD::SDIV:
6119 // div exact can only produce a zero if the dividend is zero.
6120 // TODO: For udiv this is also true if Op1 u<= Op0
6121 if (Op->getFlags().hasExact())
6122 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6123 break;
6124
6125 case ISD::ADD:
6126 if (Op->getFlags().hasNoUnsignedWrap())
6127 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6128 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6129 return true;
6130 // TODO: There are a lot more cases we can prove for add.
6131 break;
6132
6133 case ISD::SUB: {
6134 if (isNullConstant(Op.getOperand(0)))
6135 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6136
6137 std::optional<bool> ne =
6138 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6139 computeKnownBits(Op.getOperand(1), Depth + 1));
6140 return ne && *ne;
6141 }
6142
6143 case ISD::MUL:
6144 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6145 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6146 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6147 return true;
6148 break;
6149
6150 case ISD::ZERO_EXTEND:
6151 case ISD::SIGN_EXTEND:
6152 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6153 case ISD::VSCALE: {
6155 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6156 ConstantRange CR =
6157 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6158 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6159 return true;
6160 break;
6161 }
6162 }
6163
6165}
6166
6168 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6169 return !C1->isNegative();
6170
6171 switch (Op.getOpcode()) {
6172 case ISD::FABS:
6173 case ISD::FEXP:
6174 case ISD::FEXP2:
6175 case ISD::FEXP10:
6176 return true;
6177 default:
6178 return false;
6179 }
6180
6181 llvm_unreachable("covered opcode switch");
6182}
6183
6185 assert(Use.getValueType().isFloatingPoint());
6186 const SDNode *User = Use.getUser();
6187 unsigned OperandNo = Use.getOperandNo();
6188 // Check if this use is insensitive to the sign of zero
6189 switch (User->getOpcode()) {
6190 case ISD::SETCC:
6191 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6192 case ISD::FABS:
6193 // fabs always produces +0.0.
6194 return true;
6195 case ISD::FCOPYSIGN:
6196 // copysign overwrites the sign bit of the first operand.
6197 return OperandNo == 0;
6198 case ISD::FADD:
6199 case ISD::FSUB: {
6200 // Arithmetic with non-zero constants fixes the uncertainty around the
6201 // sign bit.
6202 SDValue Other = User->getOperand(1 - OperandNo);
6204 }
6205 case ISD::FP_TO_SINT:
6206 case ISD::FP_TO_UINT:
6207 // fp-to-int conversions normalize signed zeros.
6208 return true;
6209 default:
6210 return false;
6211 }
6212}
6213
6215 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6216 // regression. Ideally, this should be implemented as a demanded-bits
6217 // optimization that stems from the users.
6218 if (Op->use_size() > 2)
6219 return false;
6220 return all_of(Op->uses(),
6221 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6222}
6223
6225 // Check the obvious case.
6226 if (A == B) return true;
6227
6228 // For negative and positive zero.
6231 if (CA->isZero() && CB->isZero()) return true;
6232
6233 // Otherwise they may not be equal.
6234 return false;
6235}
6236
6237// Only bits set in Mask must be negated, other bits may be arbitrary.
6239 if (isBitwiseNot(V, AllowUndefs))
6240 return V.getOperand(0);
6241
6242 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6243 // bits in the non-extended part.
6244 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6245 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6246 return SDValue();
6247 SDValue ExtArg = V.getOperand(0);
6248 if (ExtArg.getScalarValueSizeInBits() >=
6249 MaskC->getAPIntValue().getActiveBits() &&
6250 isBitwiseNot(ExtArg, AllowUndefs) &&
6251 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6252 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6253 return ExtArg.getOperand(0).getOperand(0);
6254 return SDValue();
6255}
6256
6258 // Match masked merge pattern (X & ~M) op (Y & M)
6259 // Including degenerate case (X & ~M) op M
6260 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6261 SDValue Other) {
6262 if (SDValue NotOperand =
6263 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6264 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6265 NotOperand->getOpcode() == ISD::TRUNCATE)
6266 NotOperand = NotOperand->getOperand(0);
6267
6268 if (Other == NotOperand)
6269 return true;
6270 if (Other->getOpcode() == ISD::AND)
6271 return NotOperand == Other->getOperand(0) ||
6272 NotOperand == Other->getOperand(1);
6273 }
6274 return false;
6275 };
6276
6277 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6278 A = A->getOperand(0);
6279
6280 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6281 B = B->getOperand(0);
6282
6283 if (A->getOpcode() == ISD::AND)
6284 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6285 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6286 return false;
6287}
6288
6289// FIXME: unify with llvm::haveNoCommonBitsSet.
6291 assert(A.getValueType() == B.getValueType() &&
6292 "Values must have the same type");
6295 return true;
6298}
6299
6300static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6301 SelectionDAG &DAG) {
6302 if (cast<ConstantSDNode>(Step)->isZero())
6303 return DAG.getConstant(0, DL, VT);
6304
6305 return SDValue();
6306}
6307
6310 SelectionDAG &DAG) {
6311 int NumOps = Ops.size();
6312 assert(NumOps != 0 && "Can't build an empty vector!");
6313 assert(!VT.isScalableVector() &&
6314 "BUILD_VECTOR cannot be used with scalable types");
6315 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6316 "Incorrect element count in BUILD_VECTOR!");
6317
6318 // BUILD_VECTOR of UNDEFs is UNDEF.
6319 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6320 return DAG.getUNDEF(VT);
6321
6322 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6323 SDValue IdentitySrc;
6324 bool IsIdentity = true;
6325 for (int i = 0; i != NumOps; ++i) {
6327 Ops[i].getOperand(0).getValueType() != VT ||
6328 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6329 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6330 Ops[i].getConstantOperandAPInt(1) != i) {
6331 IsIdentity = false;
6332 break;
6333 }
6334 IdentitySrc = Ops[i].getOperand(0);
6335 }
6336 if (IsIdentity)
6337 return IdentitySrc;
6338
6339 return SDValue();
6340}
6341
6342/// Try to simplify vector concatenation to an input value, undef, or build
6343/// vector.
6346 SelectionDAG &DAG) {
6347 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6349 [Ops](SDValue Op) {
6350 return Ops[0].getValueType() == Op.getValueType();
6351 }) &&
6352 "Concatenation of vectors with inconsistent value types!");
6353 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6354 VT.getVectorElementCount() &&
6355 "Incorrect element count in vector concatenation!");
6356
6357 if (Ops.size() == 1)
6358 return Ops[0];
6359
6360 // Concat of UNDEFs is UNDEF.
6361 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6362 return DAG.getUNDEF(VT);
6363
6364 // Scan the operands and look for extract operations from a single source
6365 // that correspond to insertion at the same location via this concatenation:
6366 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6367 SDValue IdentitySrc;
6368 bool IsIdentity = true;
6369 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6370 SDValue Op = Ops[i];
6371 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6372 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6373 Op.getOperand(0).getValueType() != VT ||
6374 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6375 Op.getConstantOperandVal(1) != IdentityIndex) {
6376 IsIdentity = false;
6377 break;
6378 }
6379 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6380 "Unexpected identity source vector for concat of extracts");
6381 IdentitySrc = Op.getOperand(0);
6382 }
6383 if (IsIdentity) {
6384 assert(IdentitySrc && "Failed to set source vector of extracts");
6385 return IdentitySrc;
6386 }
6387
6388 // The code below this point is only designed to work for fixed width
6389 // vectors, so we bail out for now.
6390 if (VT.isScalableVector())
6391 return SDValue();
6392
6393 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6394 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6395 // BUILD_VECTOR.
6396 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6397 EVT SVT = VT.getScalarType();
6399 for (SDValue Op : Ops) {
6400 EVT OpVT = Op.getValueType();
6401 if (Op.isUndef())
6402 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6403 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6404 Elts.append(Op->op_begin(), Op->op_end());
6405 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6406 OpVT.getVectorNumElements() == 1 &&
6407 isNullConstant(Op.getOperand(2)))
6408 Elts.push_back(Op.getOperand(1));
6409 else
6410 return SDValue();
6411 }
6412
6413 // BUILD_VECTOR requires all inputs to be of the same type, find the
6414 // maximum type and extend them all.
6415 for (SDValue Op : Elts)
6416 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6417
6418 if (SVT.bitsGT(VT.getScalarType())) {
6419 for (SDValue &Op : Elts) {
6420 if (Op.isUndef())
6421 Op = DAG.getUNDEF(SVT);
6422 else
6423 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6424 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6425 : DAG.getSExtOrTrunc(Op, DL, SVT);
6426 }
6427 }
6428
6429 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6430 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6431 return V;
6432}
6433
6434/// Gets or creates the specified node.
6435SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6436 SDVTList VTs = getVTList(VT);
6438 AddNodeIDNode(ID, Opcode, VTs, {});
6439 void *IP = nullptr;
6440 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6441 return SDValue(E, 0);
6442
6443 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6444 CSEMap.InsertNode(N, IP);
6445
6446 InsertNode(N);
6447 SDValue V = SDValue(N, 0);
6448 NewSDValueDbgMsg(V, "Creating new node: ", this);
6449 return V;
6450}
6451
6452SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6453 SDValue N1) {
6454 SDNodeFlags Flags;
6455 if (Inserter)
6456 Flags = Inserter->getFlags();
6457 return getNode(Opcode, DL, VT, N1, Flags);
6458}
6459
6460SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6461 SDValue N1, const SDNodeFlags Flags) {
6462 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6463
6464 // Constant fold unary operations with a vector integer or float operand.
6465 switch (Opcode) {
6466 default:
6467 // FIXME: Entirely reasonable to perform folding of other unary
6468 // operations here as the need arises.
6469 break;
6470 case ISD::FNEG:
6471 case ISD::FABS:
6472 case ISD::FCEIL:
6473 case ISD::FTRUNC:
6474 case ISD::FFLOOR:
6475 case ISD::FP_EXTEND:
6476 case ISD::FP_TO_SINT:
6477 case ISD::FP_TO_UINT:
6478 case ISD::FP_TO_FP16:
6479 case ISD::FP_TO_BF16:
6480 case ISD::TRUNCATE:
6481 case ISD::ANY_EXTEND:
6482 case ISD::ZERO_EXTEND:
6483 case ISD::SIGN_EXTEND:
6484 case ISD::UINT_TO_FP:
6485 case ISD::SINT_TO_FP:
6486 case ISD::FP16_TO_FP:
6487 case ISD::BF16_TO_FP:
6488 case ISD::BITCAST:
6489 case ISD::ABS:
6490 case ISD::BITREVERSE:
6491 case ISD::BSWAP:
6492 case ISD::CTLZ:
6494 case ISD::CTTZ:
6496 case ISD::CTPOP:
6497 case ISD::STEP_VECTOR: {
6498 SDValue Ops = {N1};
6499 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6500 return Fold;
6501 }
6502 }
6503
6504 unsigned OpOpcode = N1.getNode()->getOpcode();
6505 switch (Opcode) {
6506 case ISD::STEP_VECTOR:
6507 assert(VT.isScalableVector() &&
6508 "STEP_VECTOR can only be used with scalable types");
6509 assert(OpOpcode == ISD::TargetConstant &&
6510 VT.getVectorElementType() == N1.getValueType() &&
6511 "Unexpected step operand");
6512 break;
6513 case ISD::FREEZE:
6514 assert(VT == N1.getValueType() && "Unexpected VT!");
6515 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6516 return N1;
6517 break;
6518 case ISD::TokenFactor:
6519 case ISD::MERGE_VALUES:
6521 return N1; // Factor, merge or concat of one node? No need.
6522 case ISD::BUILD_VECTOR: {
6523 // Attempt to simplify BUILD_VECTOR.
6524 SDValue Ops[] = {N1};
6525 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6526 return V;
6527 break;
6528 }
6529 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6530 case ISD::FP_EXTEND:
6532 "Invalid FP cast!");
6533 if (N1.getValueType() == VT) return N1; // noop conversion.
6534 assert((!VT.isVector() || VT.getVectorElementCount() ==
6536 "Vector element count mismatch!");
6537 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6538 if (N1.isUndef())
6539 return getUNDEF(VT);
6540 break;
6541 case ISD::FP_TO_SINT:
6542 case ISD::FP_TO_UINT:
6543 if (N1.isUndef())
6544 return getUNDEF(VT);
6545 break;
6546 case ISD::SINT_TO_FP:
6547 case ISD::UINT_TO_FP:
6548 // [us]itofp(undef) = 0, because the result value is bounded.
6549 if (N1.isUndef())
6550 return getConstantFP(0.0, DL, VT);
6551 break;
6552 case ISD::SIGN_EXTEND:
6553 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6554 "Invalid SIGN_EXTEND!");
6555 assert(VT.isVector() == N1.getValueType().isVector() &&
6556 "SIGN_EXTEND result type type should be vector iff the operand "
6557 "type is vector!");
6558 if (N1.getValueType() == VT) return N1; // noop extension
6559 assert((!VT.isVector() || VT.getVectorElementCount() ==
6561 "Vector element count mismatch!");
6562 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6563 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6564 SDNodeFlags Flags;
6565 if (OpOpcode == ISD::ZERO_EXTEND)
6566 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6567 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6568 transferDbgValues(N1, NewVal);
6569 return NewVal;
6570 }
6571
6572 if (OpOpcode == ISD::POISON)
6573 return getPOISON(VT);
6574
6575 if (N1.isUndef())
6576 // sext(undef) = 0, because the top bits will all be the same.
6577 return getConstant(0, DL, VT);
6578
6579 // Skip unnecessary sext_inreg pattern:
6580 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6581 if (OpOpcode == ISD::TRUNCATE) {
6582 SDValue OpOp = N1.getOperand(0);
6583 if (OpOp.getValueType() == VT) {
6584 unsigned NumSignExtBits =
6586 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6587 transferDbgValues(N1, OpOp);
6588 return OpOp;
6589 }
6590 }
6591 }
6592 break;
6593 case ISD::ZERO_EXTEND:
6594 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6595 "Invalid ZERO_EXTEND!");
6596 assert(VT.isVector() == N1.getValueType().isVector() &&
6597 "ZERO_EXTEND result type type should be vector iff the operand "
6598 "type is vector!");
6599 if (N1.getValueType() == VT) return N1; // noop extension
6600 assert((!VT.isVector() || VT.getVectorElementCount() ==
6602 "Vector element count mismatch!");
6603 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6604 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6605 SDNodeFlags Flags;
6606 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6607 SDValue NewVal =
6608 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6609 transferDbgValues(N1, NewVal);
6610 return NewVal;
6611 }
6612
6613 if (OpOpcode == ISD::POISON)
6614 return getPOISON(VT);
6615
6616 if (N1.isUndef())
6617 // zext(undef) = 0, because the top bits will be zero.
6618 return getConstant(0, DL, VT);
6619
6620 // Skip unnecessary zext_inreg pattern:
6621 // (zext (trunc x)) -> x iff the upper bits are known zero.
6622 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6623 // use to recognise zext_inreg patterns.
6624 if (OpOpcode == ISD::TRUNCATE) {
6625 SDValue OpOp = N1.getOperand(0);
6626 if (OpOp.getValueType() == VT) {
6627 if (OpOp.getOpcode() != ISD::AND) {
6630 if (MaskedValueIsZero(OpOp, HiBits)) {
6631 transferDbgValues(N1, OpOp);
6632 return OpOp;
6633 }
6634 }
6635 }
6636 }
6637 break;
6638 case ISD::ANY_EXTEND:
6639 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6640 "Invalid ANY_EXTEND!");
6641 assert(VT.isVector() == N1.getValueType().isVector() &&
6642 "ANY_EXTEND result type type should be vector iff the operand "
6643 "type is vector!");
6644 if (N1.getValueType() == VT) return N1; // noop extension
6645 assert((!VT.isVector() || VT.getVectorElementCount() ==
6647 "Vector element count mismatch!");
6648 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6649
6650 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6651 OpOpcode == ISD::ANY_EXTEND) {
6652 SDNodeFlags Flags;
6653 if (OpOpcode == ISD::ZERO_EXTEND)
6654 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6655 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6656 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6657 }
6658 if (N1.isUndef())
6659 return getUNDEF(VT);
6660
6661 // (ext (trunc x)) -> x
6662 if (OpOpcode == ISD::TRUNCATE) {
6663 SDValue OpOp = N1.getOperand(0);
6664 if (OpOp.getValueType() == VT) {
6665 transferDbgValues(N1, OpOp);
6666 return OpOp;
6667 }
6668 }
6669 break;
6670 case ISD::TRUNCATE:
6671 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6672 "Invalid TRUNCATE!");
6673 assert(VT.isVector() == N1.getValueType().isVector() &&
6674 "TRUNCATE result type type should be vector iff the operand "
6675 "type is vector!");
6676 if (N1.getValueType() == VT) return N1; // noop truncate
6677 assert((!VT.isVector() || VT.getVectorElementCount() ==
6679 "Vector element count mismatch!");
6680 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6681 if (OpOpcode == ISD::TRUNCATE)
6682 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6683 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6684 OpOpcode == ISD::ANY_EXTEND) {
6685 // If the source is smaller than the dest, we still need an extend.
6687 VT.getScalarType())) {
6688 SDNodeFlags Flags;
6689 if (OpOpcode == ISD::ZERO_EXTEND)
6690 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6691 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6692 }
6693 if (N1.getOperand(0).getValueType().bitsGT(VT))
6694 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6695 return N1.getOperand(0);
6696 }
6697 if (N1.isUndef())
6698 return getUNDEF(VT);
6699 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6700 return getVScale(DL, VT,
6702 break;
6706 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6707 assert(N1.getValueType().bitsLE(VT) &&
6708 "The input must be the same size or smaller than the result.");
6711 "The destination vector type must have fewer lanes than the input.");
6712 break;
6713 case ISD::ABS:
6714 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6715 if (N1.isUndef())
6716 return getConstant(0, DL, VT);
6717 break;
6718 case ISD::BSWAP:
6719 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6720 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6721 "BSWAP types must be a multiple of 16 bits!");
6722 if (N1.isUndef())
6723 return getUNDEF(VT);
6724 // bswap(bswap(X)) -> X.
6725 if (OpOpcode == ISD::BSWAP)
6726 return N1.getOperand(0);
6727 break;
6728 case ISD::BITREVERSE:
6729 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6730 if (N1.isUndef())
6731 return getUNDEF(VT);
6732 break;
6733 case ISD::BITCAST:
6735 "Cannot BITCAST between types of different sizes!");
6736 if (VT == N1.getValueType()) return N1; // noop conversion.
6737 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6738 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6739 if (N1.isUndef())
6740 return getUNDEF(VT);
6741 break;
6743 assert(VT.isVector() && !N1.getValueType().isVector() &&
6744 (VT.getVectorElementType() == N1.getValueType() ||
6746 N1.getValueType().isInteger() &&
6748 "Illegal SCALAR_TO_VECTOR node!");
6749 if (N1.isUndef())
6750 return getUNDEF(VT);
6751 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6752 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6754 N1.getConstantOperandVal(1) == 0 &&
6755 N1.getOperand(0).getValueType() == VT)
6756 return N1.getOperand(0);
6757 break;
6758 case ISD::FNEG:
6759 // Negation of an unknown bag of bits is still completely undefined.
6760 if (N1.isUndef())
6761 return getUNDEF(VT);
6762
6763 if (OpOpcode == ISD::FNEG) // --X -> X
6764 return N1.getOperand(0);
6765 break;
6766 case ISD::FABS:
6767 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6768 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6769 break;
6770 case ISD::VSCALE:
6771 assert(VT == N1.getValueType() && "Unexpected VT!");
6772 break;
6773 case ISD::CTPOP:
6774 if (N1.getValueType().getScalarType() == MVT::i1)
6775 return N1;
6776 break;
6777 case ISD::CTLZ:
6778 case ISD::CTTZ:
6779 if (N1.getValueType().getScalarType() == MVT::i1)
6780 return getNOT(DL, N1, N1.getValueType());
6781 break;
6782 case ISD::VECREDUCE_ADD:
6783 if (N1.getValueType().getScalarType() == MVT::i1)
6784 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6785 break;
6788 if (N1.getValueType().getScalarType() == MVT::i1)
6789 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6790 break;
6793 if (N1.getValueType().getScalarType() == MVT::i1)
6794 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6795 break;
6796 case ISD::SPLAT_VECTOR:
6797 assert(VT.isVector() && "Wrong return type!");
6798 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6799 // that for now.
6801 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6803 N1.getValueType().isInteger() &&
6805 "Wrong operand type!");
6806 break;
6807 }
6808
6809 SDNode *N;
6810 SDVTList VTs = getVTList(VT);
6811 SDValue Ops[] = {N1};
6812 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6814 AddNodeIDNode(ID, Opcode, VTs, Ops);
6815 void *IP = nullptr;
6816 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6817 E->intersectFlagsWith(Flags);
6818 return SDValue(E, 0);
6819 }
6820
6821 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6822 N->setFlags(Flags);
6823 createOperands(N, Ops);
6824 CSEMap.InsertNode(N, IP);
6825 } else {
6826 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6827 createOperands(N, Ops);
6828 }
6829
6830 InsertNode(N);
6831 SDValue V = SDValue(N, 0);
6832 NewSDValueDbgMsg(V, "Creating new node: ", this);
6833 return V;
6834}
6835
6836static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6837 const APInt &C2) {
6838 switch (Opcode) {
6839 case ISD::ADD: return C1 + C2;
6840 case ISD::SUB: return C1 - C2;
6841 case ISD::MUL: return C1 * C2;
6842 case ISD::AND: return C1 & C2;
6843 case ISD::OR: return C1 | C2;
6844 case ISD::XOR: return C1 ^ C2;
6845 case ISD::SHL: return C1 << C2;
6846 case ISD::SRL: return C1.lshr(C2);
6847 case ISD::SRA: return C1.ashr(C2);
6848 case ISD::ROTL: return C1.rotl(C2);
6849 case ISD::ROTR: return C1.rotr(C2);
6850 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6851 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6852 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6853 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6854 case ISD::SADDSAT: return C1.sadd_sat(C2);
6855 case ISD::UADDSAT: return C1.uadd_sat(C2);
6856 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6857 case ISD::USUBSAT: return C1.usub_sat(C2);
6858 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6859 case ISD::USHLSAT: return C1.ushl_sat(C2);
6860 case ISD::UDIV:
6861 if (!C2.getBoolValue())
6862 break;
6863 return C1.udiv(C2);
6864 case ISD::UREM:
6865 if (!C2.getBoolValue())
6866 break;
6867 return C1.urem(C2);
6868 case ISD::SDIV:
6869 if (!C2.getBoolValue())
6870 break;
6871 return C1.sdiv(C2);
6872 case ISD::SREM:
6873 if (!C2.getBoolValue())
6874 break;
6875 return C1.srem(C2);
6876 case ISD::AVGFLOORS:
6877 return APIntOps::avgFloorS(C1, C2);
6878 case ISD::AVGFLOORU:
6879 return APIntOps::avgFloorU(C1, C2);
6880 case ISD::AVGCEILS:
6881 return APIntOps::avgCeilS(C1, C2);
6882 case ISD::AVGCEILU:
6883 return APIntOps::avgCeilU(C1, C2);
6884 case ISD::ABDS:
6885 return APIntOps::abds(C1, C2);
6886 case ISD::ABDU:
6887 return APIntOps::abdu(C1, C2);
6888 case ISD::MULHS:
6889 return APIntOps::mulhs(C1, C2);
6890 case ISD::MULHU:
6891 return APIntOps::mulhu(C1, C2);
6892 case ISD::CLMUL:
6893 return APIntOps::clmul(C1, C2);
6894 case ISD::CLMULR:
6895 return APIntOps::clmulr(C1, C2);
6896 case ISD::CLMULH:
6897 return APIntOps::clmulh(C1, C2);
6898 }
6899 return std::nullopt;
6900}
6901// Handle constant folding with UNDEF.
6902// TODO: Handle more cases.
6903static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6904 bool IsUndef1, const APInt &C2,
6905 bool IsUndef2) {
6906 if (!(IsUndef1 || IsUndef2))
6907 return FoldValue(Opcode, C1, C2);
6908
6909 // Fold and(x, undef) -> 0
6910 // Fold mul(x, undef) -> 0
6911 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6912 return APInt::getZero(C1.getBitWidth());
6913
6914 return std::nullopt;
6915}
6916
6918 const GlobalAddressSDNode *GA,
6919 const SDNode *N2) {
6920 if (GA->getOpcode() != ISD::GlobalAddress)
6921 return SDValue();
6922 if (!TLI->isOffsetFoldingLegal(GA))
6923 return SDValue();
6924 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6925 if (!C2)
6926 return SDValue();
6927 int64_t Offset = C2->getSExtValue();
6928 switch (Opcode) {
6929 case ISD::ADD:
6930 case ISD::PTRADD:
6931 break;
6932 case ISD::SUB: Offset = -uint64_t(Offset); break;
6933 default: return SDValue();
6934 }
6935 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6936 GA->getOffset() + uint64_t(Offset));
6937}
6938
6940 switch (Opcode) {
6941 case ISD::SDIV:
6942 case ISD::UDIV:
6943 case ISD::SREM:
6944 case ISD::UREM: {
6945 // If a divisor is zero/undef or any element of a divisor vector is
6946 // zero/undef, the whole op is undef.
6947 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6948 SDValue Divisor = Ops[1];
6949 if (Divisor.isUndef() || isNullConstant(Divisor))
6950 return true;
6951
6952 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6953 llvm::any_of(Divisor->op_values(),
6954 [](SDValue V) { return V.isUndef() ||
6955 isNullConstant(V); });
6956 // TODO: Handle signed overflow.
6957 }
6958 // TODO: Handle oversized shifts.
6959 default:
6960 return false;
6961 }
6962}
6963
6966 SDNodeFlags Flags) {
6967 // If the opcode is a target-specific ISD node, there's nothing we can
6968 // do here and the operand rules may not line up with the below, so
6969 // bail early.
6970 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6971 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6972 // foldCONCAT_VECTORS in getNode before this is called.
6973 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6974 return SDValue();
6975
6976 unsigned NumOps = Ops.size();
6977 if (NumOps == 0)
6978 return SDValue();
6979
6980 if (isUndef(Opcode, Ops))
6981 return getUNDEF(VT);
6982
6983 // Handle unary special cases.
6984 if (NumOps == 1) {
6985 SDValue N1 = Ops[0];
6986
6987 // Constant fold unary operations with an integer constant operand. Even
6988 // opaque constant will be folded, because the folding of unary operations
6989 // doesn't create new constants with different values. Nevertheless, the
6990 // opaque flag is preserved during folding to prevent future folding with
6991 // other constants.
6992 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6993 const APInt &Val = C->getAPIntValue();
6994 switch (Opcode) {
6995 case ISD::SIGN_EXTEND:
6996 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6997 C->isTargetOpcode(), C->isOpaque());
6998 case ISD::TRUNCATE:
6999 if (C->isOpaque())
7000 break;
7001 [[fallthrough]];
7002 case ISD::ZERO_EXTEND:
7003 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7004 C->isTargetOpcode(), C->isOpaque());
7005 case ISD::ANY_EXTEND:
7006 // Some targets like RISCV prefer to sign extend some types.
7007 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7008 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7009 C->isTargetOpcode(), C->isOpaque());
7010 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7011 C->isTargetOpcode(), C->isOpaque());
7012 case ISD::ABS:
7013 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7014 C->isOpaque());
7015 case ISD::BITREVERSE:
7016 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7017 C->isOpaque());
7018 case ISD::BSWAP:
7019 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7020 C->isOpaque());
7021 case ISD::CTPOP:
7022 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7023 C->isOpaque());
7024 case ISD::CTLZ:
7026 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7027 C->isOpaque());
7028 case ISD::CTTZ:
7030 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7031 C->isOpaque());
7032 case ISD::UINT_TO_FP:
7033 case ISD::SINT_TO_FP: {
7035 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7037 return getConstantFP(FPV, DL, VT);
7038 }
7039 case ISD::FP16_TO_FP:
7040 case ISD::BF16_TO_FP: {
7041 bool Ignored;
7042 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7043 : APFloat::BFloat(),
7044 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7045
7046 // This can return overflow, underflow, or inexact; we don't care.
7047 // FIXME need to be more flexible about rounding mode.
7049 &Ignored);
7050 return getConstantFP(FPV, DL, VT);
7051 }
7052 case ISD::STEP_VECTOR:
7053 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7054 return V;
7055 break;
7056 case ISD::BITCAST:
7057 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7058 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7059 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7060 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7061 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7062 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7063 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7064 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7065 break;
7066 }
7067 }
7068
7069 // Constant fold unary operations with a floating point constant operand.
7070 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7071 APFloat V = C->getValueAPF(); // make copy
7072 switch (Opcode) {
7073 case ISD::FNEG:
7074 V.changeSign();
7075 return getConstantFP(V, DL, VT);
7076 case ISD::FABS:
7077 V.clearSign();
7078 return getConstantFP(V, DL, VT);
7079 case ISD::FCEIL: {
7080 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7082 return getConstantFP(V, DL, VT);
7083 return SDValue();
7084 }
7085 case ISD::FTRUNC: {
7086 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7088 return getConstantFP(V, DL, VT);
7089 return SDValue();
7090 }
7091 case ISD::FFLOOR: {
7092 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7094 return getConstantFP(V, DL, VT);
7095 return SDValue();
7096 }
7097 case ISD::FP_EXTEND: {
7098 bool ignored;
7099 // This can return overflow, underflow, or inexact; we don't care.
7100 // FIXME need to be more flexible about rounding mode.
7101 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7102 &ignored);
7103 return getConstantFP(V, DL, VT);
7104 }
7105 case ISD::FP_TO_SINT:
7106 case ISD::FP_TO_UINT: {
7107 bool ignored;
7108 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7109 // FIXME need to be more flexible about rounding mode.
7111 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7112 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7113 break;
7114 return getConstant(IntVal, DL, VT);
7115 }
7116 case ISD::FP_TO_FP16:
7117 case ISD::FP_TO_BF16: {
7118 bool Ignored;
7119 // This can return overflow, underflow, or inexact; we don't care.
7120 // FIXME need to be more flexible about rounding mode.
7121 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7122 : APFloat::BFloat(),
7124 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7125 }
7126 case ISD::BITCAST:
7127 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7128 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7129 VT);
7130 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7131 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7132 VT);
7133 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7134 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7135 VT);
7136 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7137 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7138 break;
7139 }
7140 }
7141
7142 // Early-out if we failed to constant fold a bitcast.
7143 if (Opcode == ISD::BITCAST)
7144 return SDValue();
7145 }
7146
7147 // Handle binops special cases.
7148 if (NumOps == 2) {
7149 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7150 return CFP;
7151
7152 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7153 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7154 if (C1->isOpaque() || C2->isOpaque())
7155 return SDValue();
7156
7157 std::optional<APInt> FoldAttempt =
7158 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7159 if (!FoldAttempt)
7160 return SDValue();
7161
7162 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7163 assert((!Folded || !VT.isVector()) &&
7164 "Can't fold vectors ops with scalar operands");
7165 return Folded;
7166 }
7167 }
7168
7169 // fold (add Sym, c) -> Sym+c
7171 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7172 if (TLI->isCommutativeBinOp(Opcode))
7174 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7175
7176 // fold (sext_in_reg c1) -> c2
7177 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7178 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7179
7180 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7181 unsigned FromBits = EVT.getScalarSizeInBits();
7182 Val <<= Val.getBitWidth() - FromBits;
7183 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7184 return getConstant(Val, DL, ConstantVT);
7185 };
7186
7187 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7188 const APInt &Val = C1->getAPIntValue();
7189 return SignExtendInReg(Val, VT);
7190 }
7191
7193 SmallVector<SDValue, 8> ScalarOps;
7194 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7195 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7196 SDValue Op = Ops[0].getOperand(I);
7197 if (Op.isUndef()) {
7198 ScalarOps.push_back(getUNDEF(OpVT));
7199 continue;
7200 }
7201 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7202 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7203 }
7204 return getBuildVector(VT, DL, ScalarOps);
7205 }
7206
7207 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7208 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7209 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7210 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7211 Ops[0].getOperand(0).getValueType()));
7212 }
7213 }
7214
7215 // Handle fshl/fshr special cases.
7216 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7217 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7218 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7219 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7220
7221 if (C1 && C2 && C3) {
7222 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7223 return SDValue();
7224 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7225 &V3 = C3->getAPIntValue();
7226
7227 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7228 : APIntOps::fshr(V1, V2, V3);
7229 return getConstant(FoldedVal, DL, VT);
7230 }
7231 }
7232
7233 // Handle fma/fmad special cases.
7234 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7235 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7236 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7237 Ops[2].getValueType() == VT && "FMA types must match!");
7241 if (C1 && C2 && C3) {
7242 APFloat V1 = C1->getValueAPF();
7243 const APFloat &V2 = C2->getValueAPF();
7244 const APFloat &V3 = C3->getValueAPF();
7245 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7248 } else
7250 return getConstantFP(V1, DL, VT);
7251 }
7252 }
7253
7254 // This is for vector folding only from here on.
7255 if (!VT.isVector())
7256 return SDValue();
7257
7258 ElementCount NumElts = VT.getVectorElementCount();
7259
7260 // See if we can fold through any bitcasted integer ops.
7261 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7262 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7263 (Ops[0].getOpcode() == ISD::BITCAST ||
7264 Ops[1].getOpcode() == ISD::BITCAST)) {
7267 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7268 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7269 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7270 N2.getValueType().isInteger()) {
7271 bool IsLE = getDataLayout().isLittleEndian();
7272 unsigned EltBits = VT.getScalarSizeInBits();
7273 SmallVector<APInt> RawBits1, RawBits2;
7274 BitVector UndefElts1, UndefElts2;
7275 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7276 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7277 SmallVector<APInt> RawBits;
7278 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7279 std::optional<APInt> Fold = FoldValueWithUndef(
7280 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7281 if (!Fold)
7282 break;
7283 RawBits.push_back(*Fold);
7284 }
7285 if (RawBits.size() == NumElts.getFixedValue()) {
7286 // We have constant folded, but we might need to cast this again back
7287 // to the original (possibly legalized) type.
7288 EVT BVVT, BVEltVT;
7289 if (N1.getValueType() == VT) {
7290 BVVT = N1.getValueType();
7291 BVEltVT = BV1->getOperand(0).getValueType();
7292 } else {
7293 BVVT = N2.getValueType();
7294 BVEltVT = BV2->getOperand(0).getValueType();
7295 }
7296 unsigned BVEltBits = BVEltVT.getSizeInBits();
7297 SmallVector<APInt> DstBits;
7298 BitVector DstUndefs;
7300 DstBits, RawBits, DstUndefs,
7301 BitVector(RawBits.size(), false));
7302 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7303 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7304 if (DstUndefs[I])
7305 continue;
7306 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7307 }
7308 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7309 }
7310 }
7311 }
7312 }
7313
7314 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7315 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7316 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7317 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7318 APInt RHSVal;
7319 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7320 APInt NewStep = Opcode == ISD::MUL
7321 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7322 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7323 return getStepVector(DL, VT, NewStep);
7324 }
7325 }
7326
7327 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7328 return !Op.getValueType().isVector() ||
7329 Op.getValueType().getVectorElementCount() == NumElts;
7330 };
7331
7332 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7333 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7334 Op.getOpcode() == ISD::BUILD_VECTOR ||
7335 Op.getOpcode() == ISD::SPLAT_VECTOR;
7336 };
7337
7338 // All operands must be vector types with the same number of elements as
7339 // the result type and must be either UNDEF or a build/splat vector
7340 // or UNDEF scalars.
7341 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7342 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7343 return SDValue();
7344
7345 // If we are comparing vectors, then the result needs to be a i1 boolean that
7346 // is then extended back to the legal result type depending on how booleans
7347 // are represented.
7348 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7349 ISD::NodeType ExtendCode =
7350 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7351 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7353
7354 // Find legal integer scalar type for constant promotion and
7355 // ensure that its scalar size is at least as large as source.
7356 EVT LegalSVT = VT.getScalarType();
7357 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7358 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7359 if (LegalSVT.bitsLT(VT.getScalarType()))
7360 return SDValue();
7361 }
7362
7363 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7364 // only have one operand to check. For fixed-length vector types we may have
7365 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7366 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7367
7368 // Constant fold each scalar lane separately.
7369 SmallVector<SDValue, 4> ScalarResults;
7370 for (unsigned I = 0; I != NumVectorElts; I++) {
7371 SmallVector<SDValue, 4> ScalarOps;
7372 for (SDValue Op : Ops) {
7373 EVT InSVT = Op.getValueType().getScalarType();
7374 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7375 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7376 if (Op.isUndef())
7377 ScalarOps.push_back(getUNDEF(InSVT));
7378 else
7379 ScalarOps.push_back(Op);
7380 continue;
7381 }
7382
7383 SDValue ScalarOp =
7384 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7385 EVT ScalarVT = ScalarOp.getValueType();
7386
7387 // Build vector (integer) scalar operands may need implicit
7388 // truncation - do this before constant folding.
7389 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7390 // Don't create illegally-typed nodes unless they're constants or undef
7391 // - if we fail to constant fold we can't guarantee the (dead) nodes
7392 // we're creating will be cleaned up before being visited for
7393 // legalization.
7394 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7395 !isa<ConstantSDNode>(ScalarOp) &&
7396 TLI->getTypeAction(*getContext(), InSVT) !=
7398 return SDValue();
7399 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7400 }
7401
7402 ScalarOps.push_back(ScalarOp);
7403 }
7404
7405 // Constant fold the scalar operands.
7406 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7407
7408 // Scalar folding only succeeded if the result is a constant or UNDEF.
7409 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7410 ScalarResult.getOpcode() != ISD::ConstantFP)
7411 return SDValue();
7412
7413 // Legalize the (integer) scalar constant if necessary. We only do
7414 // this once we know the folding succeeded, since otherwise we would
7415 // get a node with illegal type which has a user.
7416 if (LegalSVT != SVT)
7417 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7418
7419 ScalarResults.push_back(ScalarResult);
7420 }
7421
7422 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7423 : getBuildVector(VT, DL, ScalarResults);
7424 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7425 return V;
7426}
7427
7430 // TODO: Add support for unary/ternary fp opcodes.
7431 if (Ops.size() != 2)
7432 return SDValue();
7433
7434 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7435 // should. That will require dealing with a potentially non-default
7436 // rounding mode, checking the "opStatus" return value from the APFloat
7437 // math calculations, and possibly other variations.
7438 SDValue N1 = Ops[0];
7439 SDValue N2 = Ops[1];
7440 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7441 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7442 if (N1CFP && N2CFP) {
7443 APFloat C1 = N1CFP->getValueAPF(); // make copy
7444 const APFloat &C2 = N2CFP->getValueAPF();
7445 switch (Opcode) {
7446 case ISD::FADD:
7448 return getConstantFP(C1, DL, VT);
7449 case ISD::FSUB:
7451 return getConstantFP(C1, DL, VT);
7452 case ISD::FMUL:
7454 return getConstantFP(C1, DL, VT);
7455 case ISD::FDIV:
7457 return getConstantFP(C1, DL, VT);
7458 case ISD::FREM:
7459 C1.mod(C2);
7460 return getConstantFP(C1, DL, VT);
7461 case ISD::FCOPYSIGN:
7462 C1.copySign(C2);
7463 return getConstantFP(C1, DL, VT);
7464 case ISD::FMINNUM:
7465 if (C1.isSignaling() || C2.isSignaling())
7466 return SDValue();
7467 return getConstantFP(minnum(C1, C2), DL, VT);
7468 case ISD::FMAXNUM:
7469 if (C1.isSignaling() || C2.isSignaling())
7470 return SDValue();
7471 return getConstantFP(maxnum(C1, C2), DL, VT);
7472 case ISD::FMINIMUM:
7473 return getConstantFP(minimum(C1, C2), DL, VT);
7474 case ISD::FMAXIMUM:
7475 return getConstantFP(maximum(C1, C2), DL, VT);
7476 case ISD::FMINIMUMNUM:
7477 return getConstantFP(minimumnum(C1, C2), DL, VT);
7478 case ISD::FMAXIMUMNUM:
7479 return getConstantFP(maximumnum(C1, C2), DL, VT);
7480 default: break;
7481 }
7482 }
7483 if (N1CFP && Opcode == ISD::FP_ROUND) {
7484 APFloat C1 = N1CFP->getValueAPF(); // make copy
7485 bool Unused;
7486 // This can return overflow, underflow, or inexact; we don't care.
7487 // FIXME need to be more flexible about rounding mode.
7489 &Unused);
7490 return getConstantFP(C1, DL, VT);
7491 }
7492
7493 switch (Opcode) {
7494 case ISD::FSUB:
7495 // -0.0 - undef --> undef (consistent with "fneg undef")
7496 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7497 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7498 return getUNDEF(VT);
7499 [[fallthrough]];
7500
7501 case ISD::FADD:
7502 case ISD::FMUL:
7503 case ISD::FDIV:
7504 case ISD::FREM:
7505 // If both operands are undef, the result is undef. If 1 operand is undef,
7506 // the result is NaN. This should match the behavior of the IR optimizer.
7507 if (N1.isUndef() && N2.isUndef())
7508 return getUNDEF(VT);
7509 if (N1.isUndef() || N2.isUndef())
7511 }
7512 return SDValue();
7513}
7514
7516 const SDLoc &DL, EVT DstEltVT) {
7517 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7518
7519 // If this is already the right type, we're done.
7520 if (SrcEltVT == DstEltVT)
7521 return SDValue(BV, 0);
7522
7523 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7524 unsigned DstBitSize = DstEltVT.getSizeInBits();
7525
7526 // If this is a conversion of N elements of one type to N elements of another
7527 // type, convert each element. This handles FP<->INT cases.
7528 if (SrcBitSize == DstBitSize) {
7530 for (SDValue Op : BV->op_values()) {
7531 // If the vector element type is not legal, the BUILD_VECTOR operands
7532 // are promoted and implicitly truncated. Make that explicit here.
7533 if (Op.getValueType() != SrcEltVT)
7534 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7535 Ops.push_back(getBitcast(DstEltVT, Op));
7536 }
7537 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7539 return getBuildVector(VT, DL, Ops);
7540 }
7541
7542 // Otherwise, we're growing or shrinking the elements. To avoid having to
7543 // handle annoying details of growing/shrinking FP values, we convert them to
7544 // int first.
7545 if (SrcEltVT.isFloatingPoint()) {
7546 // Convert the input float vector to a int vector where the elements are the
7547 // same sizes.
7548 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7549 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7551 DstEltVT);
7552 return SDValue();
7553 }
7554
7555 // Now we know the input is an integer vector. If the output is a FP type,
7556 // convert to integer first, then to FP of the right size.
7557 if (DstEltVT.isFloatingPoint()) {
7558 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7559 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7561 DstEltVT);
7562 return SDValue();
7563 }
7564
7565 // Okay, we know the src/dst types are both integers of differing types.
7566 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7567
7568 // Extract the constant raw bit data.
7569 BitVector UndefElements;
7570 SmallVector<APInt> RawBits;
7571 bool IsLE = getDataLayout().isLittleEndian();
7572 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7573 return SDValue();
7574
7576 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7577 if (UndefElements[I])
7578 Ops.push_back(getUNDEF(DstEltVT));
7579 else
7580 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7581 }
7582
7583 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7584 return getBuildVector(VT, DL, Ops);
7585}
7586
7588 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7589
7590 // There's no need to assert on a byte-aligned pointer. All pointers are at
7591 // least byte aligned.
7592 if (A == Align(1))
7593 return Val;
7594
7595 SDVTList VTs = getVTList(Val.getValueType());
7597 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7598 ID.AddInteger(A.value());
7599
7600 void *IP = nullptr;
7601 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7602 return SDValue(E, 0);
7603
7604 auto *N =
7605 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7606 createOperands(N, {Val});
7607
7608 CSEMap.InsertNode(N, IP);
7609 InsertNode(N);
7610
7611 SDValue V(N, 0);
7612 NewSDValueDbgMsg(V, "Creating new node: ", this);
7613 return V;
7614}
7615
7616SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7617 SDValue N1, SDValue N2) {
7618 SDNodeFlags Flags;
7619 if (Inserter)
7620 Flags = Inserter->getFlags();
7621 return getNode(Opcode, DL, VT, N1, N2, Flags);
7622}
7623
7625 SDValue &N2) const {
7626 if (!TLI->isCommutativeBinOp(Opcode))
7627 return;
7628
7629 // Canonicalize:
7630 // binop(const, nonconst) -> binop(nonconst, const)
7633 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7634 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7635 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7636 std::swap(N1, N2);
7637
7638 // Canonicalize:
7639 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7640 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7642 std::swap(N1, N2);
7643}
7644
7645SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7646 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7648 N2.getOpcode() != ISD::DELETED_NODE &&
7649 "Operand is DELETED_NODE!");
7650
7651 canonicalizeCommutativeBinop(Opcode, N1, N2);
7652
7653 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7654 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7655
7656 // Don't allow undefs in vector splats - we might be returning N2 when folding
7657 // to zero etc.
7658 ConstantSDNode *N2CV =
7659 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7660
7661 switch (Opcode) {
7662 default: break;
7663 case ISD::TokenFactor:
7664 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7665 N2.getValueType() == MVT::Other && "Invalid token factor!");
7666 // Fold trivial token factors.
7667 if (N1.getOpcode() == ISD::EntryToken) return N2;
7668 if (N2.getOpcode() == ISD::EntryToken) return N1;
7669 if (N1 == N2) return N1;
7670 break;
7671 case ISD::BUILD_VECTOR: {
7672 // Attempt to simplify BUILD_VECTOR.
7673 SDValue Ops[] = {N1, N2};
7674 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7675 return V;
7676 break;
7677 }
7678 case ISD::CONCAT_VECTORS: {
7679 SDValue Ops[] = {N1, N2};
7680 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7681 return V;
7682 break;
7683 }
7684 case ISD::AND:
7685 assert(VT.isInteger() && "This operator does not apply to FP types!");
7686 assert(N1.getValueType() == N2.getValueType() &&
7687 N1.getValueType() == VT && "Binary operator types must match!");
7688 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7689 // worth handling here.
7690 if (N2CV && N2CV->isZero())
7691 return N2;
7692 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7693 return N1;
7694 break;
7695 case ISD::OR:
7696 case ISD::XOR:
7697 case ISD::ADD:
7698 case ISD::PTRADD:
7699 case ISD::SUB:
7700 assert(VT.isInteger() && "This operator does not apply to FP types!");
7701 assert(N1.getValueType() == N2.getValueType() &&
7702 N1.getValueType() == VT && "Binary operator types must match!");
7703 // The equal operand types requirement is unnecessarily strong for PTRADD.
7704 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7705 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7706 // logic everywhere where PTRADDs may be folded or combined to properly
7707 // support them. If/when we introduce pointer types to the SDAG, we will
7708 // need to relax this constraint.
7709
7710 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7711 // it's worth handling here.
7712 if (N2CV && N2CV->isZero())
7713 return N1;
7714 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7715 VT.getScalarType() == MVT::i1)
7716 return getNode(ISD::XOR, DL, VT, N1, N2);
7717 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7718 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7719 N2.getOpcode() == ISD::VSCALE) {
7720 const APInt &C1 = N1->getConstantOperandAPInt(0);
7721 const APInt &C2 = N2->getConstantOperandAPInt(0);
7722 return getVScale(DL, VT, C1 + C2);
7723 }
7724 break;
7725 case ISD::MUL:
7726 assert(VT.isInteger() && "This operator does not apply to FP types!");
7727 assert(N1.getValueType() == N2.getValueType() &&
7728 N1.getValueType() == VT && "Binary operator types must match!");
7729 if (VT.getScalarType() == MVT::i1)
7730 return getNode(ISD::AND, DL, VT, N1, N2);
7731 if (N2CV && N2CV->isZero())
7732 return N2;
7733 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7734 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7735 const APInt &N2CImm = N2C->getAPIntValue();
7736 return getVScale(DL, VT, MulImm * N2CImm);
7737 }
7738 break;
7739 case ISD::UDIV:
7740 case ISD::UREM:
7741 case ISD::MULHU:
7742 case ISD::MULHS:
7743 case ISD::SDIV:
7744 case ISD::SREM:
7745 case ISD::SADDSAT:
7746 case ISD::SSUBSAT:
7747 case ISD::UADDSAT:
7748 case ISD::USUBSAT:
7749 assert(VT.isInteger() && "This operator does not apply to FP types!");
7750 assert(N1.getValueType() == N2.getValueType() &&
7751 N1.getValueType() == VT && "Binary operator types must match!");
7752 if (VT.getScalarType() == MVT::i1) {
7753 // fold (add_sat x, y) -> (or x, y) for bool types.
7754 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7755 return getNode(ISD::OR, DL, VT, N1, N2);
7756 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7757 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7758 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7759 }
7760 break;
7761 case ISD::SCMP:
7762 case ISD::UCMP:
7763 assert(N1.getValueType() == N2.getValueType() &&
7764 "Types of operands of UCMP/SCMP must match");
7765 assert(N1.getValueType().isVector() == VT.isVector() &&
7766 "Operands and return type of must both be scalars or vectors");
7767 if (VT.isVector())
7770 "Result and operands must have the same number of elements");
7771 break;
7772 case ISD::AVGFLOORS:
7773 case ISD::AVGFLOORU:
7774 case ISD::AVGCEILS:
7775 case ISD::AVGCEILU:
7776 assert(VT.isInteger() && "This operator does not apply to FP types!");
7777 assert(N1.getValueType() == N2.getValueType() &&
7778 N1.getValueType() == VT && "Binary operator types must match!");
7779 break;
7780 case ISD::ABDS:
7781 case ISD::ABDU:
7782 assert(VT.isInteger() && "This operator does not apply to FP types!");
7783 assert(N1.getValueType() == N2.getValueType() &&
7784 N1.getValueType() == VT && "Binary operator types must match!");
7785 if (VT.getScalarType() == MVT::i1)
7786 return getNode(ISD::XOR, DL, VT, N1, N2);
7787 break;
7788 case ISD::SMIN:
7789 case ISD::UMAX:
7790 assert(VT.isInteger() && "This operator does not apply to FP types!");
7791 assert(N1.getValueType() == N2.getValueType() &&
7792 N1.getValueType() == VT && "Binary operator types must match!");
7793 if (VT.getScalarType() == MVT::i1)
7794 return getNode(ISD::OR, DL, VT, N1, N2);
7795 break;
7796 case ISD::SMAX:
7797 case ISD::UMIN:
7798 assert(VT.isInteger() && "This operator does not apply to FP types!");
7799 assert(N1.getValueType() == N2.getValueType() &&
7800 N1.getValueType() == VT && "Binary operator types must match!");
7801 if (VT.getScalarType() == MVT::i1)
7802 return getNode(ISD::AND, DL, VT, N1, N2);
7803 break;
7804 case ISD::FADD:
7805 case ISD::FSUB:
7806 case ISD::FMUL:
7807 case ISD::FDIV:
7808 case ISD::FREM:
7809 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7810 assert(N1.getValueType() == N2.getValueType() &&
7811 N1.getValueType() == VT && "Binary operator types must match!");
7812 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7813 return V;
7814 break;
7815 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7816 assert(N1.getValueType() == VT &&
7819 "Invalid FCOPYSIGN!");
7820 break;
7821 case ISD::SHL:
7822 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7823 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7824 const APInt &ShiftImm = N2C->getAPIntValue();
7825 return getVScale(DL, VT, MulImm << ShiftImm);
7826 }
7827 [[fallthrough]];
7828 case ISD::SRA:
7829 case ISD::SRL:
7830 if (SDValue V = simplifyShift(N1, N2))
7831 return V;
7832 [[fallthrough]];
7833 case ISD::ROTL:
7834 case ISD::ROTR:
7835 case ISD::SSHLSAT:
7836 case ISD::USHLSAT:
7837 assert(VT == N1.getValueType() &&
7838 "Shift operators return type must be the same as their first arg");
7839 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7840 "Shifts only work on integers");
7841 assert((!VT.isVector() || VT == N2.getValueType()) &&
7842 "Vector shift amounts must be in the same as their first arg");
7843 // Verify that the shift amount VT is big enough to hold valid shift
7844 // amounts. This catches things like trying to shift an i1024 value by an
7845 // i8, which is easy to fall into in generic code that uses
7846 // TLI.getShiftAmount().
7849 "Invalid use of small shift amount with oversized value!");
7850
7851 // Always fold shifts of i1 values so the code generator doesn't need to
7852 // handle them. Since we know the size of the shift has to be less than the
7853 // size of the value, the shift/rotate count is guaranteed to be zero.
7854 if (VT == MVT::i1)
7855 return N1;
7856 if (N2CV && N2CV->isZero())
7857 return N1;
7858 break;
7859 case ISD::FP_ROUND:
7861 VT.bitsLE(N1.getValueType()) && N2C &&
7862 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7863 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7864 if (N1.getValueType() == VT) return N1; // noop conversion.
7865 break;
7866 case ISD::AssertNoFPClass: {
7868 "AssertNoFPClass is used for a non-floating type");
7869 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7870 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7871 assert(llvm::to_underlying(NoFPClass) <=
7873 "FPClassTest value too large");
7874 (void)NoFPClass;
7875 break;
7876 }
7877 case ISD::AssertSext:
7878 case ISD::AssertZext: {
7879 EVT EVT = cast<VTSDNode>(N2)->getVT();
7880 assert(VT == N1.getValueType() && "Not an inreg extend!");
7881 assert(VT.isInteger() && EVT.isInteger() &&
7882 "Cannot *_EXTEND_INREG FP types");
7883 assert(!EVT.isVector() &&
7884 "AssertSExt/AssertZExt type should be the vector element type "
7885 "rather than the vector type!");
7886 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7887 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7888 break;
7889 }
7891 EVT EVT = cast<VTSDNode>(N2)->getVT();
7892 assert(VT == N1.getValueType() && "Not an inreg extend!");
7893 assert(VT.isInteger() && EVT.isInteger() &&
7894 "Cannot *_EXTEND_INREG FP types");
7895 assert(EVT.isVector() == VT.isVector() &&
7896 "SIGN_EXTEND_INREG type should be vector iff the operand "
7897 "type is vector!");
7898 assert((!EVT.isVector() ||
7900 "Vector element counts must match in SIGN_EXTEND_INREG");
7901 assert(EVT.bitsLE(VT) && "Not extending!");
7902 if (EVT == VT) return N1; // Not actually extending
7903 break;
7904 }
7906 case ISD::FP_TO_UINT_SAT: {
7907 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7908 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7909 assert(N1.getValueType().isVector() == VT.isVector() &&
7910 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7911 "vector!");
7912 assert((!VT.isVector() || VT.getVectorElementCount() ==
7914 "Vector element counts must match in FP_TO_*INT_SAT");
7915 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7916 "Type to saturate to must be a scalar.");
7917 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7918 "Not extending!");
7919 break;
7920 }
7923 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7924 element type of the vector.");
7925
7926 // Extract from an undefined value or using an undefined index is undefined.
7927 if (N1.isUndef() || N2.isUndef())
7928 return getUNDEF(VT);
7929
7930 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7931 // vectors. For scalable vectors we will provide appropriate support for
7932 // dealing with arbitrary indices.
7933 if (N2C && N1.getValueType().isFixedLengthVector() &&
7934 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7935 return getUNDEF(VT);
7936
7937 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7938 // expanding copies of large vectors from registers. This only works for
7939 // fixed length vectors, since we need to know the exact number of
7940 // elements.
7941 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7943 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7944 return getExtractVectorElt(DL, VT,
7945 N1.getOperand(N2C->getZExtValue() / Factor),
7946 N2C->getZExtValue() % Factor);
7947 }
7948
7949 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7950 // lowering is expanding large vector constants.
7951 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7952 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7955 "BUILD_VECTOR used for scalable vectors");
7956 unsigned Index =
7957 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7958 SDValue Elt = N1.getOperand(Index);
7959
7960 if (VT != Elt.getValueType())
7961 // If the vector element type is not legal, the BUILD_VECTOR operands
7962 // are promoted and implicitly truncated, and the result implicitly
7963 // extended. Make that explicit here.
7964 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7965
7966 return Elt;
7967 }
7968
7969 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7970 // operations are lowered to scalars.
7971 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7972 // If the indices are the same, return the inserted element else
7973 // if the indices are known different, extract the element from
7974 // the original vector.
7975 SDValue N1Op2 = N1.getOperand(2);
7977
7978 if (N1Op2C && N2C) {
7979 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7980 if (VT == N1.getOperand(1).getValueType())
7981 return N1.getOperand(1);
7982 if (VT.isFloatingPoint()) {
7984 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7985 }
7986 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7987 }
7988 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7989 }
7990 }
7991
7992 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7993 // when vector types are scalarized and v1iX is legal.
7994 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7995 // Here we are completely ignoring the extract element index (N2),
7996 // which is fine for fixed width vectors, since any index other than 0
7997 // is undefined anyway. However, this cannot be ignored for scalable
7998 // vectors - in theory we could support this, but we don't want to do this
7999 // without a profitability check.
8000 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8002 N1.getValueType().getVectorNumElements() == 1) {
8003 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8004 N1.getOperand(1));
8005 }
8006 break;
8008 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8009 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8010 (N1.getValueType().isInteger() == VT.isInteger()) &&
8011 N1.getValueType() != VT &&
8012 "Wrong types for EXTRACT_ELEMENT!");
8013
8014 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8015 // 64-bit integers into 32-bit parts. Instead of building the extract of
8016 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8017 if (N1.getOpcode() == ISD::BUILD_PAIR)
8018 return N1.getOperand(N2C->getZExtValue());
8019
8020 // EXTRACT_ELEMENT of a constant int is also very common.
8021 if (N1C) {
8022 unsigned ElementSize = VT.getSizeInBits();
8023 unsigned Shift = ElementSize * N2C->getZExtValue();
8024 const APInt &Val = N1C->getAPIntValue();
8025 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8026 }
8027 break;
8029 EVT N1VT = N1.getValueType();
8030 assert(VT.isVector() && N1VT.isVector() &&
8031 "Extract subvector VTs must be vectors!");
8033 "Extract subvector VTs must have the same element type!");
8034 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8035 "Cannot extract a scalable vector from a fixed length vector!");
8036 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8038 "Extract subvector must be from larger vector to smaller vector!");
8039 assert(N2C && "Extract subvector index must be a constant");
8040 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8041 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8042 N1VT.getVectorMinNumElements()) &&
8043 "Extract subvector overflow!");
8044 assert(N2C->getAPIntValue().getBitWidth() ==
8045 TLI->getVectorIdxWidth(getDataLayout()) &&
8046 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8047 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8048 "Extract index is not a multiple of the output vector length");
8049
8050 // Trivial extraction.
8051 if (VT == N1VT)
8052 return N1;
8053
8054 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8055 if (N1.isUndef())
8056 return getUNDEF(VT);
8057
8058 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8059 // the concat have the same type as the extract.
8060 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8061 VT == N1.getOperand(0).getValueType()) {
8062 unsigned Factor = VT.getVectorMinNumElements();
8063 return N1.getOperand(N2C->getZExtValue() / Factor);
8064 }
8065
8066 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8067 // during shuffle legalization.
8068 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8069 VT == N1.getOperand(1).getValueType())
8070 return N1.getOperand(1);
8071 break;
8072 }
8073 }
8074
8075 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8076 switch (Opcode) {
8077 case ISD::XOR:
8078 case ISD::ADD:
8079 case ISD::PTRADD:
8080 case ISD::SUB:
8082 case ISD::UDIV:
8083 case ISD::SDIV:
8084 case ISD::UREM:
8085 case ISD::SREM:
8086 case ISD::MUL:
8087 case ISD::AND:
8088 case ISD::SSUBSAT:
8089 case ISD::USUBSAT:
8090 case ISD::UMIN:
8091 case ISD::OR:
8092 case ISD::SADDSAT:
8093 case ISD::UADDSAT:
8094 case ISD::UMAX:
8095 case ISD::SMAX:
8096 case ISD::SMIN:
8097 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8098 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8099 }
8100 }
8101
8102 // Canonicalize an UNDEF to the RHS, even over a constant.
8103 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8104 if (TLI->isCommutativeBinOp(Opcode)) {
8105 std::swap(N1, N2);
8106 } else {
8107 switch (Opcode) {
8108 case ISD::PTRADD:
8109 case ISD::SUB:
8110 // fold op(undef, non_undef_arg2) -> undef.
8111 return N1;
8113 case ISD::UDIV:
8114 case ISD::SDIV:
8115 case ISD::UREM:
8116 case ISD::SREM:
8117 case ISD::SSUBSAT:
8118 case ISD::USUBSAT:
8119 // fold op(undef, non_undef_arg2) -> 0.
8120 return getConstant(0, DL, VT);
8121 }
8122 }
8123 }
8124
8125 // Fold a bunch of operators when the RHS is undef.
8126 if (N2.getOpcode() == ISD::UNDEF) {
8127 switch (Opcode) {
8128 case ISD::XOR:
8129 if (N1.getOpcode() == ISD::UNDEF)
8130 // Handle undef ^ undef -> 0 special case. This is a common
8131 // idiom (misuse).
8132 return getConstant(0, DL, VT);
8133 [[fallthrough]];
8134 case ISD::ADD:
8135 case ISD::PTRADD:
8136 case ISD::SUB:
8137 // fold op(arg1, undef) -> undef.
8138 return N2;
8139 case ISD::UDIV:
8140 case ISD::SDIV:
8141 case ISD::UREM:
8142 case ISD::SREM:
8143 // fold op(arg1, undef) -> poison.
8144 return getPOISON(VT);
8145 case ISD::MUL:
8146 case ISD::AND:
8147 case ISD::SSUBSAT:
8148 case ISD::USUBSAT:
8149 case ISD::UMIN:
8150 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8151 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8152 case ISD::OR:
8153 case ISD::SADDSAT:
8154 case ISD::UADDSAT:
8155 case ISD::UMAX:
8156 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8157 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8158 case ISD::SMAX:
8159 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8160 return N1.getOpcode() == ISD::UNDEF
8161 ? N2
8162 : getConstant(
8164 VT);
8165 case ISD::SMIN:
8166 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8167 return N1.getOpcode() == ISD::UNDEF
8168 ? N2
8169 : getConstant(
8171 VT);
8172 }
8173 }
8174
8175 // Perform trivial constant folding.
8176 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8177 return SV;
8178
8179 // Memoize this node if possible.
8180 SDNode *N;
8181 SDVTList VTs = getVTList(VT);
8182 SDValue Ops[] = {N1, N2};
8183 if (VT != MVT::Glue) {
8185 AddNodeIDNode(ID, Opcode, VTs, Ops);
8186 void *IP = nullptr;
8187 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8188 E->intersectFlagsWith(Flags);
8189 return SDValue(E, 0);
8190 }
8191
8192 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8193 N->setFlags(Flags);
8194 createOperands(N, Ops);
8195 CSEMap.InsertNode(N, IP);
8196 } else {
8197 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8198 createOperands(N, Ops);
8199 }
8200
8201 InsertNode(N);
8202 SDValue V = SDValue(N, 0);
8203 NewSDValueDbgMsg(V, "Creating new node: ", this);
8204 return V;
8205}
8206
8207SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8208 SDValue N1, SDValue N2, SDValue N3) {
8209 SDNodeFlags Flags;
8210 if (Inserter)
8211 Flags = Inserter->getFlags();
8212 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8213}
8214
8215SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8216 SDValue N1, SDValue N2, SDValue N3,
8217 const SDNodeFlags Flags) {
8219 N2.getOpcode() != ISD::DELETED_NODE &&
8220 N3.getOpcode() != ISD::DELETED_NODE &&
8221 "Operand is DELETED_NODE!");
8222 // Perform various simplifications.
8223 switch (Opcode) {
8224 case ISD::BUILD_VECTOR: {
8225 // Attempt to simplify BUILD_VECTOR.
8226 SDValue Ops[] = {N1, N2, N3};
8227 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8228 return V;
8229 break;
8230 }
8231 case ISD::CONCAT_VECTORS: {
8232 SDValue Ops[] = {N1, N2, N3};
8233 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8234 return V;
8235 break;
8236 }
8237 case ISD::SETCC: {
8238 assert(VT.isInteger() && "SETCC result type must be an integer!");
8239 assert(N1.getValueType() == N2.getValueType() &&
8240 "SETCC operands must have the same type!");
8241 assert(VT.isVector() == N1.getValueType().isVector() &&
8242 "SETCC type should be vector iff the operand type is vector!");
8243 assert((!VT.isVector() || VT.getVectorElementCount() ==
8245 "SETCC vector element counts must match!");
8246 // Use FoldSetCC to simplify SETCC's.
8247 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8248 return V;
8249 break;
8250 }
8251 case ISD::SELECT:
8252 case ISD::VSELECT:
8253 if (SDValue V = simplifySelect(N1, N2, N3))
8254 return V;
8255 break;
8257 llvm_unreachable("should use getVectorShuffle constructor!");
8259 if (isNullConstant(N3))
8260 return N1;
8261 break;
8263 if (isNullConstant(N3))
8264 return N2;
8265 break;
8267 assert(VT.isVector() && VT == N1.getValueType() &&
8268 "INSERT_VECTOR_ELT vector type mismatch");
8270 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8271 assert((!VT.isFloatingPoint() ||
8272 VT.getVectorElementType() == N2.getValueType()) &&
8273 "INSERT_VECTOR_ELT fp scalar type mismatch");
8274 assert((!VT.isInteger() ||
8276 "INSERT_VECTOR_ELT int scalar size mismatch");
8277
8278 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8279 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8280 // for scalable vectors where we will generate appropriate code to
8281 // deal with out-of-bounds cases correctly.
8282 if (N3C && VT.isFixedLengthVector() &&
8283 N3C->getZExtValue() >= VT.getVectorNumElements())
8284 return getUNDEF(VT);
8285
8286 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8287 if (N3.isUndef())
8288 return getUNDEF(VT);
8289
8290 // If inserting poison, just use the input vector.
8291 if (N2.getOpcode() == ISD::POISON)
8292 return N1;
8293
8294 // Inserting undef into undef/poison is still undef.
8295 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8296 return getUNDEF(VT);
8297
8298 // If the inserted element is an UNDEF, just use the input vector.
8299 // But not if skipping the insert could make the result more poisonous.
8300 if (N2.isUndef()) {
8301 if (N3C && VT.isFixedLengthVector()) {
8302 APInt EltMask =
8303 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8304 if (isGuaranteedNotToBePoison(N1, EltMask))
8305 return N1;
8306 } else if (isGuaranteedNotToBePoison(N1))
8307 return N1;
8308 }
8309 break;
8310 }
8311 case ISD::INSERT_SUBVECTOR: {
8312 // If inserting poison, just use the input vector,
8313 if (N2.getOpcode() == ISD::POISON)
8314 return N1;
8315
8316 // Inserting undef into undef/poison is still undef.
8317 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8318 return getUNDEF(VT);
8319
8320 EVT N2VT = N2.getValueType();
8321 assert(VT == N1.getValueType() &&
8322 "Dest and insert subvector source types must match!");
8323 assert(VT.isVector() && N2VT.isVector() &&
8324 "Insert subvector VTs must be vectors!");
8326 "Insert subvector VTs must have the same element type!");
8327 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8328 "Cannot insert a scalable vector into a fixed length vector!");
8329 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8331 "Insert subvector must be from smaller vector to larger vector!");
8333 "Insert subvector index must be constant");
8334 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8335 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8337 "Insert subvector overflow!");
8339 TLI->getVectorIdxWidth(getDataLayout()) &&
8340 "Constant index for INSERT_SUBVECTOR has an invalid size");
8341
8342 // Trivial insertion.
8343 if (VT == N2VT)
8344 return N2;
8345
8346 // If this is an insert of an extracted vector into an undef/poison vector,
8347 // we can just use the input to the extract. But not if skipping the
8348 // extract+insert could make the result more poisonous.
8349 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8350 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8351 if (N1.getOpcode() == ISD::POISON)
8352 return N2.getOperand(0);
8353 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8354 unsigned LoBit = N3->getAsZExtVal();
8355 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8356 APInt EltMask =
8357 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8358 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8359 return N2.getOperand(0);
8360 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8361 return N2.getOperand(0);
8362 }
8363
8364 // If the inserted subvector is UNDEF, just use the input vector.
8365 // But not if skipping the insert could make the result more poisonous.
8366 if (N2.isUndef()) {
8367 if (VT.isFixedLengthVector()) {
8368 unsigned LoBit = N3->getAsZExtVal();
8369 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8370 APInt EltMask =
8371 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8372 if (isGuaranteedNotToBePoison(N1, EltMask))
8373 return N1;
8374 } else if (isGuaranteedNotToBePoison(N1))
8375 return N1;
8376 }
8377 break;
8378 }
8379 case ISD::BITCAST:
8380 // Fold bit_convert nodes from a type to themselves.
8381 if (N1.getValueType() == VT)
8382 return N1;
8383 break;
8384 case ISD::VP_TRUNCATE:
8385 case ISD::VP_SIGN_EXTEND:
8386 case ISD::VP_ZERO_EXTEND:
8387 // Don't create noop casts.
8388 if (N1.getValueType() == VT)
8389 return N1;
8390 break;
8391 case ISD::VECTOR_COMPRESS: {
8392 [[maybe_unused]] EVT VecVT = N1.getValueType();
8393 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8394 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8395 assert(VT == VecVT && "Vector and result type don't match.");
8396 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8397 "All inputs must be vectors.");
8398 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8400 "Vector and mask must have same number of elements.");
8401
8402 if (N1.isUndef() || N2.isUndef())
8403 return N3;
8404
8405 break;
8406 }
8411 [[maybe_unused]] EVT AccVT = N1.getValueType();
8412 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8413 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8414 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8415 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8416 "node to have the same type!");
8417 assert(VT.isVector() && VT == AccVT &&
8418 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8419 "the same type as its result!");
8421 AccVT.getVectorElementCount()) &&
8422 "Expected the element count of the second and third operands of the "
8423 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8424 "element count of the first operand and the result!");
8426 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8427 "node to have an element type which is the same as or smaller than "
8428 "the element type of the first operand and result!");
8429 break;
8430 }
8431 }
8432
8433 // Perform trivial constant folding for arithmetic operators.
8434 switch (Opcode) {
8435 case ISD::FMA:
8436 case ISD::FMAD:
8437 case ISD::SETCC:
8438 case ISD::FSHL:
8439 case ISD::FSHR:
8440 if (SDValue SV =
8441 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8442 return SV;
8443 break;
8444 }
8445
8446 // Memoize node if it doesn't produce a glue result.
8447 SDNode *N;
8448 SDVTList VTs = getVTList(VT);
8449 SDValue Ops[] = {N1, N2, N3};
8450 if (VT != MVT::Glue) {
8452 AddNodeIDNode(ID, Opcode, VTs, Ops);
8453 void *IP = nullptr;
8454 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8455 E->intersectFlagsWith(Flags);
8456 return SDValue(E, 0);
8457 }
8458
8459 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8460 N->setFlags(Flags);
8461 createOperands(N, Ops);
8462 CSEMap.InsertNode(N, IP);
8463 } else {
8464 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8465 createOperands(N, Ops);
8466 }
8467
8468 InsertNode(N);
8469 SDValue V = SDValue(N, 0);
8470 NewSDValueDbgMsg(V, "Creating new node: ", this);
8471 return V;
8472}
8473
8474SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8475 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8476 const SDNodeFlags Flags) {
8477 SDValue Ops[] = { N1, N2, N3, N4 };
8478 return getNode(Opcode, DL, VT, Ops, Flags);
8479}
8480
8481SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8482 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8483 SDNodeFlags Flags;
8484 if (Inserter)
8485 Flags = Inserter->getFlags();
8486 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8487}
8488
8489SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8490 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8491 SDValue N5, const SDNodeFlags Flags) {
8492 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8493 return getNode(Opcode, DL, VT, Ops, Flags);
8494}
8495
8496SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8497 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8498 SDValue N5) {
8499 SDNodeFlags Flags;
8500 if (Inserter)
8501 Flags = Inserter->getFlags();
8502 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8503}
8504
8505/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8506/// the incoming stack arguments to be loaded from the stack.
8508 SmallVector<SDValue, 8> ArgChains;
8509
8510 // Include the original chain at the beginning of the list. When this is
8511 // used by target LowerCall hooks, this helps legalize find the
8512 // CALLSEQ_BEGIN node.
8513 ArgChains.push_back(Chain);
8514
8515 // Add a chain value for each stack argument.
8516 for (SDNode *U : getEntryNode().getNode()->users())
8517 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8518 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8519 if (FI->getIndex() < 0)
8520 ArgChains.push_back(SDValue(L, 1));
8521
8522 // Build a tokenfactor for all the chains.
8523 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8524}
8525
8526/// getMemsetValue - Vectorized representation of the memset value
8527/// operand.
8529 const SDLoc &dl) {
8530 assert(!Value.isUndef());
8531
8532 unsigned NumBits = VT.getScalarSizeInBits();
8534 assert(C->getAPIntValue().getBitWidth() == 8);
8535 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8536 if (VT.isInteger()) {
8537 bool IsOpaque = VT.getSizeInBits() > 64 ||
8538 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8539 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8540 }
8541 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8542 }
8543
8544 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8545 EVT IntVT = VT.getScalarType();
8546 if (!IntVT.isInteger())
8547 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8548
8549 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8550 if (NumBits > 8) {
8551 // Use a multiplication with 0x010101... to extend the input to the
8552 // required length.
8553 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8554 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8555 DAG.getConstant(Magic, dl, IntVT));
8556 }
8557
8558 if (VT != Value.getValueType() && !VT.isInteger())
8559 Value = DAG.getBitcast(VT.getScalarType(), Value);
8560 if (VT != Value.getValueType())
8561 Value = DAG.getSplatBuildVector(VT, dl, Value);
8562
8563 return Value;
8564}
8565
8566/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8567/// used when a memcpy is turned into a memset when the source is a constant
8568/// string ptr.
8570 const TargetLowering &TLI,
8571 const ConstantDataArraySlice &Slice) {
8572 // Handle vector with all elements zero.
8573 if (Slice.Array == nullptr) {
8574 if (VT.isInteger())
8575 return DAG.getConstant(0, dl, VT);
8576 return DAG.getNode(ISD::BITCAST, dl, VT,
8577 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8578 }
8579
8580 assert(!VT.isVector() && "Can't handle vector type here!");
8581 unsigned NumVTBits = VT.getSizeInBits();
8582 unsigned NumVTBytes = NumVTBits / 8;
8583 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8584
8585 APInt Val(NumVTBits, 0);
8586 if (DAG.getDataLayout().isLittleEndian()) {
8587 for (unsigned i = 0; i != NumBytes; ++i)
8588 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8589 } else {
8590 for (unsigned i = 0; i != NumBytes; ++i)
8591 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8592 }
8593
8594 // If the "cost" of materializing the integer immediate is less than the cost
8595 // of a load, then it is cost effective to turn the load into the immediate.
8596 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8597 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8598 return DAG.getConstant(Val, dl, VT);
8599 return SDValue();
8600}
8601
8603 const SDLoc &DL,
8604 const SDNodeFlags Flags) {
8605 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8606 return getMemBasePlusOffset(Base, Index, DL, Flags);
8607}
8608
8610 const SDLoc &DL,
8611 const SDNodeFlags Flags) {
8612 assert(Offset.getValueType().isInteger());
8613 EVT BasePtrVT = Ptr.getValueType();
8614 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8615 BasePtrVT))
8616 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8617 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8618 SDNodeFlags AddFlags = Flags;
8619 AddFlags.setInBounds(false);
8620 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8621}
8622
8623/// Returns true if memcpy source is constant data.
8625 uint64_t SrcDelta = 0;
8626 GlobalAddressSDNode *G = nullptr;
8627 if (Src.getOpcode() == ISD::GlobalAddress)
8629 else if (Src->isAnyAdd() &&
8630 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8631 Src.getOperand(1).getOpcode() == ISD::Constant) {
8632 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8633 SrcDelta = Src.getConstantOperandVal(1);
8634 }
8635 if (!G)
8636 return false;
8637
8638 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8639 SrcDelta + G->getOffset());
8640}
8641
8643 SelectionDAG &DAG) {
8644 // On Darwin, -Os means optimize for size without hurting performance, so
8645 // only really optimize for size when -Oz (MinSize) is used.
8647 return MF.getFunction().hasMinSize();
8648 return DAG.shouldOptForSize();
8649}
8650
8652 SmallVector<SDValue, 32> &OutChains, unsigned From,
8653 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8654 SmallVector<SDValue, 16> &OutStoreChains) {
8655 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8656 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8657 SmallVector<SDValue, 16> GluedLoadChains;
8658 for (unsigned i = From; i < To; ++i) {
8659 OutChains.push_back(OutLoadChains[i]);
8660 GluedLoadChains.push_back(OutLoadChains[i]);
8661 }
8662
8663 // Chain for all loads.
8664 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8665 GluedLoadChains);
8666
8667 for (unsigned i = From; i < To; ++i) {
8668 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8669 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8670 ST->getBasePtr(), ST->getMemoryVT(),
8671 ST->getMemOperand());
8672 OutChains.push_back(NewStore);
8673 }
8674}
8675
8677 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8678 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8679 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8680 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8681 // Turn a memcpy of undef to nop.
8682 // FIXME: We need to honor volatile even is Src is undef.
8683 if (Src.isUndef())
8684 return Chain;
8685
8686 // Expand memcpy to a series of load and store ops if the size operand falls
8687 // below a certain threshold.
8688 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8689 // rather than maybe a humongous number of loads and stores.
8690 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8691 const DataLayout &DL = DAG.getDataLayout();
8692 LLVMContext &C = *DAG.getContext();
8693 std::vector<EVT> MemOps;
8694 bool DstAlignCanChange = false;
8696 MachineFrameInfo &MFI = MF.getFrameInfo();
8697 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8699 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8700 DstAlignCanChange = true;
8701 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8702 if (!SrcAlign || Alignment > *SrcAlign)
8703 SrcAlign = Alignment;
8704 assert(SrcAlign && "SrcAlign must be set");
8706 // If marked as volatile, perform a copy even when marked as constant.
8707 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8708 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8709 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8710 const MemOp Op = isZeroConstant
8711 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8712 /*IsZeroMemset*/ true, isVol)
8713 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8714 *SrcAlign, isVol, CopyFromConstant);
8715 if (!TLI.findOptimalMemOpLowering(
8716 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8717 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8718 return SDValue();
8719
8720 if (DstAlignCanChange) {
8721 Type *Ty = MemOps[0].getTypeForEVT(C);
8722 Align NewAlign = DL.getABITypeAlign(Ty);
8723
8724 // Don't promote to an alignment that would require dynamic stack
8725 // realignment which may conflict with optimizations such as tail call
8726 // optimization.
8728 if (!TRI->hasStackRealignment(MF))
8729 if (MaybeAlign StackAlign = DL.getStackAlignment())
8730 NewAlign = std::min(NewAlign, *StackAlign);
8731
8732 if (NewAlign > Alignment) {
8733 // Give the stack frame object a larger alignment if needed.
8734 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8735 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8736 Alignment = NewAlign;
8737 }
8738 }
8739
8740 // Prepare AAInfo for loads/stores after lowering this memcpy.
8741 AAMDNodes NewAAInfo = AAInfo;
8742 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8743
8744 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8745 bool isConstant =
8746 BatchAA && SrcVal &&
8747 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8748
8749 MachineMemOperand::Flags MMOFlags =
8751 SmallVector<SDValue, 16> OutLoadChains;
8752 SmallVector<SDValue, 16> OutStoreChains;
8753 SmallVector<SDValue, 32> OutChains;
8754 unsigned NumMemOps = MemOps.size();
8755 uint64_t SrcOff = 0, DstOff = 0;
8756 for (unsigned i = 0; i != NumMemOps; ++i) {
8757 EVT VT = MemOps[i];
8758 unsigned VTSize = VT.getSizeInBits() / 8;
8759 SDValue Value, Store;
8760
8761 if (VTSize > Size) {
8762 // Issuing an unaligned load / store pair that overlaps with the previous
8763 // pair. Adjust the offset accordingly.
8764 assert(i == NumMemOps-1 && i != 0);
8765 SrcOff -= VTSize - Size;
8766 DstOff -= VTSize - Size;
8767 }
8768
8769 if (CopyFromConstant &&
8770 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8771 // It's unlikely a store of a vector immediate can be done in a single
8772 // instruction. It would require a load from a constantpool first.
8773 // We only handle zero vectors here.
8774 // FIXME: Handle other cases where store of vector immediate is done in
8775 // a single instruction.
8776 ConstantDataArraySlice SubSlice;
8777 if (SrcOff < Slice.Length) {
8778 SubSlice = Slice;
8779 SubSlice.move(SrcOff);
8780 } else {
8781 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8782 SubSlice.Array = nullptr;
8783 SubSlice.Offset = 0;
8784 SubSlice.Length = VTSize;
8785 }
8786 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8787 if (Value.getNode()) {
8788 Store = DAG.getStore(
8789 Chain, dl, Value,
8790 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8791 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8792 OutChains.push_back(Store);
8793 }
8794 }
8795
8796 if (!Store.getNode()) {
8797 // The type might not be legal for the target. This should only happen
8798 // if the type is smaller than a legal type, as on PPC, so the right
8799 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8800 // to Load/Store if NVT==VT.
8801 // FIXME does the case above also need this?
8802 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8803 assert(NVT.bitsGE(VT));
8804
8805 bool isDereferenceable =
8806 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8807 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8808 if (isDereferenceable)
8810 if (isConstant)
8811 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8812
8813 Value = DAG.getExtLoad(
8814 ISD::EXTLOAD, dl, NVT, Chain,
8815 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8816 SrcPtrInfo.getWithOffset(SrcOff), VT,
8817 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8818 OutLoadChains.push_back(Value.getValue(1));
8819
8820 Store = DAG.getTruncStore(
8821 Chain, dl, Value,
8822 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8823 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8824 OutStoreChains.push_back(Store);
8825 }
8826 SrcOff += VTSize;
8827 DstOff += VTSize;
8828 Size -= VTSize;
8829 }
8830
8831 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8833 unsigned NumLdStInMemcpy = OutStoreChains.size();
8834
8835 if (NumLdStInMemcpy) {
8836 // It may be that memcpy might be converted to memset if it's memcpy
8837 // of constants. In such a case, we won't have loads and stores, but
8838 // just stores. In the absence of loads, there is nothing to gang up.
8839 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8840 // If target does not care, just leave as it.
8841 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8842 OutChains.push_back(OutLoadChains[i]);
8843 OutChains.push_back(OutStoreChains[i]);
8844 }
8845 } else {
8846 // Ld/St less than/equal limit set by target.
8847 if (NumLdStInMemcpy <= GluedLdStLimit) {
8848 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8849 NumLdStInMemcpy, OutLoadChains,
8850 OutStoreChains);
8851 } else {
8852 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8853 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8854 unsigned GlueIter = 0;
8855
8856 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8857 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8858 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8859
8860 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8861 OutLoadChains, OutStoreChains);
8862 GlueIter += GluedLdStLimit;
8863 }
8864
8865 // Residual ld/st.
8866 if (RemainingLdStInMemcpy) {
8867 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8868 RemainingLdStInMemcpy, OutLoadChains,
8869 OutStoreChains);
8870 }
8871 }
8872 }
8873 }
8874 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8875}
8876
8878 SDValue Chain, SDValue Dst, SDValue Src,
8879 uint64_t Size, Align Alignment,
8880 bool isVol, bool AlwaysInline,
8881 MachinePointerInfo DstPtrInfo,
8882 MachinePointerInfo SrcPtrInfo,
8883 const AAMDNodes &AAInfo) {
8884 // Turn a memmove of undef to nop.
8885 // FIXME: We need to honor volatile even is Src is undef.
8886 if (Src.isUndef())
8887 return Chain;
8888
8889 // Expand memmove to a series of load and store ops if the size operand falls
8890 // below a certain threshold.
8891 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8892 const DataLayout &DL = DAG.getDataLayout();
8893 LLVMContext &C = *DAG.getContext();
8894 std::vector<EVT> MemOps;
8895 bool DstAlignCanChange = false;
8897 MachineFrameInfo &MFI = MF.getFrameInfo();
8898 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8900 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8901 DstAlignCanChange = true;
8902 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8903 if (!SrcAlign || Alignment > *SrcAlign)
8904 SrcAlign = Alignment;
8905 assert(SrcAlign && "SrcAlign must be set");
8906 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8907 if (!TLI.findOptimalMemOpLowering(
8908 C, MemOps, Limit,
8909 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8910 /*IsVolatile*/ true),
8911 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8912 MF.getFunction().getAttributes()))
8913 return SDValue();
8914
8915 if (DstAlignCanChange) {
8916 Type *Ty = MemOps[0].getTypeForEVT(C);
8917 Align NewAlign = DL.getABITypeAlign(Ty);
8918
8919 // Don't promote to an alignment that would require dynamic stack
8920 // realignment which may conflict with optimizations such as tail call
8921 // optimization.
8923 if (!TRI->hasStackRealignment(MF))
8924 if (MaybeAlign StackAlign = DL.getStackAlignment())
8925 NewAlign = std::min(NewAlign, *StackAlign);
8926
8927 if (NewAlign > Alignment) {
8928 // Give the stack frame object a larger alignment if needed.
8929 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8930 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8931 Alignment = NewAlign;
8932 }
8933 }
8934
8935 // Prepare AAInfo for loads/stores after lowering this memmove.
8936 AAMDNodes NewAAInfo = AAInfo;
8937 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8938
8939 MachineMemOperand::Flags MMOFlags =
8941 uint64_t SrcOff = 0, DstOff = 0;
8942 SmallVector<SDValue, 8> LoadValues;
8943 SmallVector<SDValue, 8> LoadChains;
8944 SmallVector<SDValue, 8> OutChains;
8945 unsigned NumMemOps = MemOps.size();
8946 for (unsigned i = 0; i < NumMemOps; i++) {
8947 EVT VT = MemOps[i];
8948 unsigned VTSize = VT.getSizeInBits() / 8;
8949 SDValue Value;
8950
8951 bool isDereferenceable =
8952 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8953 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8954 if (isDereferenceable)
8956
8957 Value = DAG.getLoad(
8958 VT, dl, Chain,
8959 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8960 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8961 LoadValues.push_back(Value);
8962 LoadChains.push_back(Value.getValue(1));
8963 SrcOff += VTSize;
8964 }
8965 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8966 OutChains.clear();
8967 for (unsigned i = 0; i < NumMemOps; i++) {
8968 EVT VT = MemOps[i];
8969 unsigned VTSize = VT.getSizeInBits() / 8;
8970 SDValue Store;
8971
8972 Store = DAG.getStore(
8973 Chain, dl, LoadValues[i],
8974 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8975 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8976 OutChains.push_back(Store);
8977 DstOff += VTSize;
8978 }
8979
8980 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8981}
8982
8983/// Lower the call to 'memset' intrinsic function into a series of store
8984/// operations.
8985///
8986/// \param DAG Selection DAG where lowered code is placed.
8987/// \param dl Link to corresponding IR location.
8988/// \param Chain Control flow dependency.
8989/// \param Dst Pointer to destination memory location.
8990/// \param Src Value of byte to write into the memory.
8991/// \param Size Number of bytes to write.
8992/// \param Alignment Alignment of the destination in bytes.
8993/// \param isVol True if destination is volatile.
8994/// \param AlwaysInline Makes sure no function call is generated.
8995/// \param DstPtrInfo IR information on the memory pointer.
8996/// \returns New head in the control flow, if lowering was successful, empty
8997/// SDValue otherwise.
8998///
8999/// The function tries to replace 'llvm.memset' intrinsic with several store
9000/// operations and value calculation code. This is usually profitable for small
9001/// memory size or when the semantic requires inlining.
9003 SDValue Chain, SDValue Dst, SDValue Src,
9004 uint64_t Size, Align Alignment, bool isVol,
9005 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9006 const AAMDNodes &AAInfo) {
9007 // Turn a memset of undef to nop.
9008 // FIXME: We need to honor volatile even is Src is undef.
9009 if (Src.isUndef())
9010 return Chain;
9011
9012 // Expand memset to a series of load/store ops if the size operand
9013 // falls below a certain threshold.
9014 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9015 std::vector<EVT> MemOps;
9016 bool DstAlignCanChange = false;
9017 LLVMContext &C = *DAG.getContext();
9019 MachineFrameInfo &MFI = MF.getFrameInfo();
9020 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9022 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9023 DstAlignCanChange = true;
9024 bool IsZeroVal = isNullConstant(Src);
9025 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9026
9027 if (!TLI.findOptimalMemOpLowering(
9028 C, MemOps, Limit,
9029 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9030 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9031 return SDValue();
9032
9033 if (DstAlignCanChange) {
9034 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9035 const DataLayout &DL = DAG.getDataLayout();
9036 Align NewAlign = DL.getABITypeAlign(Ty);
9037
9038 // Don't promote to an alignment that would require dynamic stack
9039 // realignment which may conflict with optimizations such as tail call
9040 // optimization.
9042 if (!TRI->hasStackRealignment(MF))
9043 if (MaybeAlign StackAlign = DL.getStackAlignment())
9044 NewAlign = std::min(NewAlign, *StackAlign);
9045
9046 if (NewAlign > Alignment) {
9047 // Give the stack frame object a larger alignment if needed.
9048 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9049 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9050 Alignment = NewAlign;
9051 }
9052 }
9053
9054 SmallVector<SDValue, 8> OutChains;
9055 uint64_t DstOff = 0;
9056 unsigned NumMemOps = MemOps.size();
9057
9058 // Find the largest store and generate the bit pattern for it.
9059 EVT LargestVT = MemOps[0];
9060 for (unsigned i = 1; i < NumMemOps; i++)
9061 if (MemOps[i].bitsGT(LargestVT))
9062 LargestVT = MemOps[i];
9063 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9064
9065 // Prepare AAInfo for loads/stores after lowering this memset.
9066 AAMDNodes NewAAInfo = AAInfo;
9067 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9068
9069 for (unsigned i = 0; i < NumMemOps; i++) {
9070 EVT VT = MemOps[i];
9071 unsigned VTSize = VT.getSizeInBits() / 8;
9072 if (VTSize > Size) {
9073 // Issuing an unaligned load / store pair that overlaps with the previous
9074 // pair. Adjust the offset accordingly.
9075 assert(i == NumMemOps-1 && i != 0);
9076 DstOff -= VTSize - Size;
9077 }
9078
9079 // If this store is smaller than the largest store see whether we can get
9080 // the smaller value for free with a truncate or extract vector element and
9081 // then store.
9082 SDValue Value = MemSetValue;
9083 if (VT.bitsLT(LargestVT)) {
9084 unsigned Index;
9085 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9086 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9087 if (!LargestVT.isVector() && !VT.isVector() &&
9088 TLI.isTruncateFree(LargestVT, VT))
9089 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9090 else if (LargestVT.isVector() && !VT.isVector() &&
9092 LargestVT.getTypeForEVT(*DAG.getContext()),
9093 VT.getSizeInBits(), Index) &&
9094 TLI.isTypeLegal(SVT) &&
9095 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9096 // Target which can combine store(extractelement VectorTy, Idx) can get
9097 // the smaller value for free.
9098 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9099 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9100 } else
9101 Value = getMemsetValue(Src, VT, DAG, dl);
9102 }
9103 assert(Value.getValueType() == VT && "Value with wrong type.");
9104 SDValue Store = DAG.getStore(
9105 Chain, dl, Value,
9106 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9107 DstPtrInfo.getWithOffset(DstOff), Alignment,
9109 NewAAInfo);
9110 OutChains.push_back(Store);
9111 DstOff += VT.getSizeInBits() / 8;
9112 Size -= VTSize;
9113 }
9114
9115 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9116}
9117
9119 unsigned AS) {
9120 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9121 // pointer operands can be losslessly bitcasted to pointers of address space 0
9122 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9123 report_fatal_error("cannot lower memory intrinsic in address space " +
9124 Twine(AS));
9125 }
9126}
9127
9129 const SelectionDAG *SelDAG,
9130 bool AllowReturnsFirstArg) {
9131 if (!CI || !CI->isTailCall())
9132 return false;
9133 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9134 // helper symbol we lower to.
9135 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9136 AllowReturnsFirstArg &&
9138}
9139
9140std::pair<SDValue, SDValue>
9142 SDValue Mem1, SDValue Size, const CallInst *CI) {
9143 RTLIB::LibcallImpl MemcmpImpl = TLI->getLibcallImpl(RTLIB::MEMCMP);
9144 if (MemcmpImpl == RTLIB::Unsupported)
9145 return {};
9146
9149 {Mem0, PT},
9150 {Mem1, PT},
9152
9154 bool IsTailCall =
9155 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9156
9157 CLI.setDebugLoc(dl)
9158 .setChain(Chain)
9159 .setLibCallee(
9160 TLI->getLibcallImplCallingConv(MemcmpImpl),
9162 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9163 std::move(Args))
9164 .setTailCall(IsTailCall);
9165
9166 return TLI->LowerCallTo(CLI);
9167}
9168
9169std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9170 const SDLoc &dl,
9171 SDValue Dst, SDValue Src,
9172 const CallInst *CI) {
9173 RTLIB::LibcallImpl LCImpl = TLI->getLibcallImpl(RTLIB::STRCPY);
9174 if (LCImpl == RTLIB::Unsupported)
9175 return {};
9176
9178 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9179
9181 bool IsTailCall =
9182 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg=*/true);
9183
9184 CLI.setDebugLoc(dl)
9185 .setChain(Chain)
9186 .setLibCallee(
9187 TLI->getLibcallImplCallingConv(LCImpl), CI->getType(),
9188 getExternalSymbol(LCImpl, TLI->getPointerTy(getDataLayout())),
9189 std::move(Args))
9190 .setTailCall(IsTailCall);
9191
9192 return TLI->LowerCallTo(CLI);
9193}
9194
9195std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9196 const SDLoc &dl,
9197 SDValue Src,
9198 const CallInst *CI) {
9199 RTLIB::LibcallImpl StrlenImpl = TLI->getLibcallImpl(RTLIB::STRLEN);
9200 if (StrlenImpl == RTLIB::Unsupported)
9201 return {};
9202
9203 // Emit a library call.
9206
9208 bool IsTailCall =
9209 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9210
9211 CLI.setDebugLoc(dl)
9212 .setChain(Chain)
9213 .setLibCallee(TLI->getLibcallImplCallingConv(StrlenImpl), CI->getType(),
9215 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9216 std::move(Args))
9217 .setTailCall(IsTailCall);
9218
9219 return TLI->LowerCallTo(CLI);
9220}
9221
9223 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9224 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9225 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9226 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9227 BatchAAResults *BatchAA) {
9228 // Check to see if we should lower the memcpy to loads and stores first.
9229 // For cases within the target-specified limits, this is the best choice.
9231 if (ConstantSize) {
9232 // Memcpy with size zero? Just return the original chain.
9233 if (ConstantSize->isZero())
9234 return Chain;
9235
9237 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9238 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9239 if (Result.getNode())
9240 return Result;
9241 }
9242
9243 // Then check to see if we should lower the memcpy with target-specific
9244 // code. If the target chooses to do this, this is the next best.
9245 if (TSI) {
9246 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9247 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9248 DstPtrInfo, SrcPtrInfo);
9249 if (Result.getNode())
9250 return Result;
9251 }
9252
9253 // If we really need inline code and the target declined to provide it,
9254 // use a (potentially long) sequence of loads and stores.
9255 if (AlwaysInline) {
9256 assert(ConstantSize && "AlwaysInline requires a constant size!");
9258 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9259 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9260 }
9261
9264
9265 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9266 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9267 // respect volatile, so they may do things like read or write memory
9268 // beyond the given memory regions. But fixing this isn't easy, and most
9269 // people don't care.
9270
9271 // Emit a library call.
9274 Args.emplace_back(Dst, PtrTy);
9275 Args.emplace_back(Src, PtrTy);
9276 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9277 // FIXME: pass in SDLoc
9279 bool IsTailCall = false;
9280 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9281
9282 if (OverrideTailCall.has_value()) {
9283 IsTailCall = *OverrideTailCall;
9284 } else {
9285 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9286 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9287 }
9288
9289 CLI.setDebugLoc(dl)
9290 .setChain(Chain)
9291 .setLibCallee(
9292 TLI->getLibcallImplCallingConv(MemCpyImpl),
9293 Dst.getValueType().getTypeForEVT(*getContext()),
9294 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9295 std::move(Args))
9297 .setTailCall(IsTailCall);
9298
9299 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9300 return CallResult.second;
9301}
9302
9304 SDValue Dst, SDValue Src, SDValue Size,
9305 Type *SizeTy, unsigned ElemSz,
9306 bool isTailCall,
9307 MachinePointerInfo DstPtrInfo,
9308 MachinePointerInfo SrcPtrInfo) {
9309 // Emit a library call.
9312 Args.emplace_back(Dst, ArgTy);
9313 Args.emplace_back(Src, ArgTy);
9314 Args.emplace_back(Size, SizeTy);
9315
9316 RTLIB::Libcall LibraryCall =
9318 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9319 if (LibcallImpl == RTLIB::Unsupported)
9320 report_fatal_error("Unsupported element size");
9321
9323 CLI.setDebugLoc(dl)
9324 .setChain(Chain)
9325 .setLibCallee(
9326 TLI->getLibcallImplCallingConv(LibcallImpl),
9328 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9329 std::move(Args))
9331 .setTailCall(isTailCall);
9332
9333 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9334 return CallResult.second;
9335}
9336
9338 SDValue Src, SDValue Size, Align Alignment,
9339 bool isVol, const CallInst *CI,
9340 std::optional<bool> OverrideTailCall,
9341 MachinePointerInfo DstPtrInfo,
9342 MachinePointerInfo SrcPtrInfo,
9343 const AAMDNodes &AAInfo,
9344 BatchAAResults *BatchAA) {
9345 // Check to see if we should lower the memmove to loads and stores first.
9346 // For cases within the target-specified limits, this is the best choice.
9348 if (ConstantSize) {
9349 // Memmove with size zero? Just return the original chain.
9350 if (ConstantSize->isZero())
9351 return Chain;
9352
9354 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9355 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9356 if (Result.getNode())
9357 return Result;
9358 }
9359
9360 // Then check to see if we should lower the memmove with target-specific
9361 // code. If the target chooses to do this, this is the next best.
9362 if (TSI) {
9363 SDValue Result =
9364 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9365 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9366 if (Result.getNode())
9367 return Result;
9368 }
9369
9372
9373 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9374 // not be safe. See memcpy above for more details.
9375
9376 // Emit a library call.
9379 Args.emplace_back(Dst, PtrTy);
9380 Args.emplace_back(Src, PtrTy);
9381 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9382 // FIXME: pass in SDLoc
9384
9385 RTLIB::LibcallImpl MemmoveImpl = TLI->getLibcallImpl(RTLIB::MEMMOVE);
9386
9387 bool IsTailCall = false;
9388 if (OverrideTailCall.has_value()) {
9389 IsTailCall = *OverrideTailCall;
9390 } else {
9391 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9392 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9393 }
9394
9395 CLI.setDebugLoc(dl)
9396 .setChain(Chain)
9397 .setLibCallee(
9398 TLI->getLibcallImplCallingConv(MemmoveImpl),
9399 Dst.getValueType().getTypeForEVT(*getContext()),
9400 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9401 std::move(Args))
9403 .setTailCall(IsTailCall);
9404
9405 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9406 return CallResult.second;
9407}
9408
9410 SDValue Dst, SDValue Src, SDValue Size,
9411 Type *SizeTy, unsigned ElemSz,
9412 bool isTailCall,
9413 MachinePointerInfo DstPtrInfo,
9414 MachinePointerInfo SrcPtrInfo) {
9415 // Emit a library call.
9417 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9418 Args.emplace_back(Dst, IntPtrTy);
9419 Args.emplace_back(Src, IntPtrTy);
9420 Args.emplace_back(Size, SizeTy);
9421
9422 RTLIB::Libcall LibraryCall =
9424 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9425 if (LibcallImpl == RTLIB::Unsupported)
9426 report_fatal_error("Unsupported element size");
9427
9429 CLI.setDebugLoc(dl)
9430 .setChain(Chain)
9431 .setLibCallee(
9432 TLI->getLibcallImplCallingConv(LibcallImpl),
9434 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9435 std::move(Args))
9437 .setTailCall(isTailCall);
9438
9439 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9440 return CallResult.second;
9441}
9442
9444 SDValue Src, SDValue Size, Align Alignment,
9445 bool isVol, bool AlwaysInline,
9446 const CallInst *CI,
9447 MachinePointerInfo DstPtrInfo,
9448 const AAMDNodes &AAInfo) {
9449 // Check to see if we should lower the memset to stores first.
9450 // For cases within the target-specified limits, this is the best choice.
9452 if (ConstantSize) {
9453 // Memset with size zero? Just return the original chain.
9454 if (ConstantSize->isZero())
9455 return Chain;
9456
9457 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9458 ConstantSize->getZExtValue(), Alignment,
9459 isVol, false, DstPtrInfo, AAInfo);
9460
9461 if (Result.getNode())
9462 return Result;
9463 }
9464
9465 // Then check to see if we should lower the memset with target-specific
9466 // code. If the target chooses to do this, this is the next best.
9467 if (TSI) {
9468 SDValue Result = TSI->EmitTargetCodeForMemset(
9469 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9470 if (Result.getNode())
9471 return Result;
9472 }
9473
9474 // If we really need inline code and the target declined to provide it,
9475 // use a (potentially long) sequence of loads and stores.
9476 if (AlwaysInline) {
9477 assert(ConstantSize && "AlwaysInline requires a constant size!");
9478 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9479 ConstantSize->getZExtValue(), Alignment,
9480 isVol, true, DstPtrInfo, AAInfo);
9481 assert(Result &&
9482 "getMemsetStores must return a valid sequence when AlwaysInline");
9483 return Result;
9484 }
9485
9487
9488 // Emit a library call.
9489 auto &Ctx = *getContext();
9490 const auto& DL = getDataLayout();
9491
9493 // FIXME: pass in SDLoc
9494 CLI.setDebugLoc(dl).setChain(Chain);
9495
9496 RTLIB::LibcallImpl BzeroImpl = TLI->getLibcallImpl(RTLIB::BZERO);
9497 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9498
9499 // If zeroing out and bzero is present, use it.
9500 if (UseBZero) {
9502 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9503 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9504 CLI.setLibCallee(
9505 TLI->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9506 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9507 } else {
9508 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9509
9511 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9512 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9513 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9514 CLI.setLibCallee(TLI->getLibcallImplCallingConv(MemsetImpl),
9515 Dst.getValueType().getTypeForEVT(Ctx),
9516 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9517 std::move(Args));
9518 }
9519
9520 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9521 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9522
9523 // If we're going to use bzero, make sure not to tail call unless the
9524 // subsequent return doesn't need a value, as bzero doesn't return the first
9525 // arg unlike memset.
9526 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9527 bool IsTailCall =
9528 CI && CI->isTailCall() &&
9529 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9530 CLI.setDiscardResult().setTailCall(IsTailCall);
9531
9532 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9533 return CallResult.second;
9534}
9535
9538 Type *SizeTy, unsigned ElemSz,
9539 bool isTailCall,
9540 MachinePointerInfo DstPtrInfo) {
9541 // Emit a library call.
9543 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9544 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9545 Args.emplace_back(Size, SizeTy);
9546
9547 RTLIB::Libcall LibraryCall =
9549 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9550 if (LibcallImpl == RTLIB::Unsupported)
9551 report_fatal_error("Unsupported element size");
9552
9554 CLI.setDebugLoc(dl)
9555 .setChain(Chain)
9556 .setLibCallee(
9557 TLI->getLibcallImplCallingConv(LibcallImpl),
9559 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9560 std::move(Args))
9562 .setTailCall(isTailCall);
9563
9564 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9565 return CallResult.second;
9566}
9567
9568SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9570 MachineMemOperand *MMO,
9571 ISD::LoadExtType ExtType) {
9573 AddNodeIDNode(ID, Opcode, VTList, Ops);
9574 ID.AddInteger(MemVT.getRawBits());
9575 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9576 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9577 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9578 ID.AddInteger(MMO->getFlags());
9579 void* IP = nullptr;
9580 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9581 E->refineAlignment(MMO);
9582 E->refineRanges(MMO);
9583 return SDValue(E, 0);
9584 }
9585
9586 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9587 VTList, MemVT, MMO, ExtType);
9588 createOperands(N, Ops);
9589
9590 CSEMap.InsertNode(N, IP);
9591 InsertNode(N);
9592 SDValue V(N, 0);
9593 NewSDValueDbgMsg(V, "Creating new node: ", this);
9594 return V;
9595}
9596
9598 EVT MemVT, SDVTList VTs, SDValue Chain,
9599 SDValue Ptr, SDValue Cmp, SDValue Swp,
9600 MachineMemOperand *MMO) {
9601 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9603 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9604
9605 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9606 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9607}
9608
9609SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9610 SDValue Chain, SDValue Ptr, SDValue Val,
9611 MachineMemOperand *MMO) {
9612 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9613 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9614 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9615 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9616 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9617 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9618 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9619 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9620 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9621 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9622 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9623 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9624 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9625 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9626 Opcode == ISD::ATOMIC_STORE) &&
9627 "Invalid Atomic Op");
9628
9629 EVT VT = Val.getValueType();
9630
9631 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9632 getVTList(VT, MVT::Other);
9633 SDValue Ops[] = {Chain, Ptr, Val};
9634 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9635}
9636
9638 EVT MemVT, EVT VT, SDValue Chain,
9639 SDValue Ptr, MachineMemOperand *MMO) {
9640 SDVTList VTs = getVTList(VT, MVT::Other);
9641 SDValue Ops[] = {Chain, Ptr};
9642 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9643}
9644
9645/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9647 if (Ops.size() == 1)
9648 return Ops[0];
9649
9651 VTs.reserve(Ops.size());
9652 for (const SDValue &Op : Ops)
9653 VTs.push_back(Op.getValueType());
9654 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9655}
9656
9658 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9659 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9661 const AAMDNodes &AAInfo) {
9662 if (Size.hasValue() && !Size.getValue())
9664
9666 MachineMemOperand *MMO =
9667 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9668
9669 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9670}
9671
9673 SDVTList VTList,
9674 ArrayRef<SDValue> Ops, EVT MemVT,
9675 MachineMemOperand *MMO) {
9676 assert(
9677 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9678 Opcode == ISD::PREFETCH ||
9679 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9680 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9681 "Opcode is not a memory-accessing opcode!");
9682
9683 // Memoize the node unless it returns a glue result.
9685 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9687 AddNodeIDNode(ID, Opcode, VTList, Ops);
9688 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9689 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9690 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9691 ID.AddInteger(MMO->getFlags());
9692 ID.AddInteger(MemVT.getRawBits());
9693 void *IP = nullptr;
9694 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9695 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9696 return SDValue(E, 0);
9697 }
9698
9699 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9700 VTList, MemVT, MMO);
9701 createOperands(N, Ops);
9702
9703 CSEMap.InsertNode(N, IP);
9704 } else {
9705 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9706 VTList, MemVT, MMO);
9707 createOperands(N, Ops);
9708 }
9709 InsertNode(N);
9710 SDValue V(N, 0);
9711 NewSDValueDbgMsg(V, "Creating new node: ", this);
9712 return V;
9713}
9714
9716 SDValue Chain, int FrameIndex) {
9717 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9718 const auto VTs = getVTList(MVT::Other);
9719 SDValue Ops[2] = {
9720 Chain,
9721 getFrameIndex(FrameIndex,
9722 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9723 true)};
9724
9726 AddNodeIDNode(ID, Opcode, VTs, Ops);
9727 ID.AddInteger(FrameIndex);
9728 void *IP = nullptr;
9729 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9730 return SDValue(E, 0);
9731
9732 LifetimeSDNode *N =
9733 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9734 createOperands(N, Ops);
9735 CSEMap.InsertNode(N, IP);
9736 InsertNode(N);
9737 SDValue V(N, 0);
9738 NewSDValueDbgMsg(V, "Creating new node: ", this);
9739 return V;
9740}
9741
9743 uint64_t Guid, uint64_t Index,
9744 uint32_t Attr) {
9745 const unsigned Opcode = ISD::PSEUDO_PROBE;
9746 const auto VTs = getVTList(MVT::Other);
9747 SDValue Ops[] = {Chain};
9749 AddNodeIDNode(ID, Opcode, VTs, Ops);
9750 ID.AddInteger(Guid);
9751 ID.AddInteger(Index);
9752 void *IP = nullptr;
9753 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9754 return SDValue(E, 0);
9755
9756 auto *N = newSDNode<PseudoProbeSDNode>(
9757 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9758 createOperands(N, Ops);
9759 CSEMap.InsertNode(N, IP);
9760 InsertNode(N);
9761 SDValue V(N, 0);
9762 NewSDValueDbgMsg(V, "Creating new node: ", this);
9763 return V;
9764}
9765
9766/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9767/// MachinePointerInfo record from it. This is particularly useful because the
9768/// code generator has many cases where it doesn't bother passing in a
9769/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9771 SelectionDAG &DAG, SDValue Ptr,
9772 int64_t Offset = 0) {
9773 // If this is FI+Offset, we can model it.
9774 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9776 FI->getIndex(), Offset);
9777
9778 // If this is (FI+Offset1)+Offset2, we can model it.
9779 if (Ptr.getOpcode() != ISD::ADD ||
9782 return Info;
9783
9784 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9786 DAG.getMachineFunction(), FI,
9787 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9788}
9789
9790/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9791/// MachinePointerInfo record from it. This is particularly useful because the
9792/// code generator has many cases where it doesn't bother passing in a
9793/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9795 SelectionDAG &DAG, SDValue Ptr,
9796 SDValue OffsetOp) {
9797 // If the 'Offset' value isn't a constant, we can't handle this.
9799 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9800 if (OffsetOp.isUndef())
9801 return InferPointerInfo(Info, DAG, Ptr);
9802 return Info;
9803}
9804
9806 EVT VT, const SDLoc &dl, SDValue Chain,
9807 SDValue Ptr, SDValue Offset,
9808 MachinePointerInfo PtrInfo, EVT MemVT,
9809 Align Alignment,
9810 MachineMemOperand::Flags MMOFlags,
9811 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9812 assert(Chain.getValueType() == MVT::Other &&
9813 "Invalid chain type");
9814
9815 MMOFlags |= MachineMemOperand::MOLoad;
9816 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9817 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9818 // clients.
9819 if (PtrInfo.V.isNull())
9820 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9821
9822 TypeSize Size = MemVT.getStoreSize();
9824 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9825 Alignment, AAInfo, Ranges);
9826 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9827}
9828
9830 EVT VT, const SDLoc &dl, SDValue Chain,
9831 SDValue Ptr, SDValue Offset, EVT MemVT,
9832 MachineMemOperand *MMO) {
9833 if (VT == MemVT) {
9834 ExtType = ISD::NON_EXTLOAD;
9835 } else if (ExtType == ISD::NON_EXTLOAD) {
9836 assert(VT == MemVT && "Non-extending load from different memory type!");
9837 } else {
9838 // Extending load.
9839 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9840 "Should only be an extending load, not truncating!");
9841 assert(VT.isInteger() == MemVT.isInteger() &&
9842 "Cannot convert from FP to Int or Int -> FP!");
9843 assert(VT.isVector() == MemVT.isVector() &&
9844 "Cannot use an ext load to convert to or from a vector!");
9845 assert((!VT.isVector() ||
9847 "Cannot use an ext load to change the number of vector elements!");
9848 }
9849
9850 assert((!MMO->getRanges() ||
9852 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9853 MemVT.isInteger())) &&
9854 "Range metadata and load type must match!");
9855
9856 bool Indexed = AM != ISD::UNINDEXED;
9857 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9858
9859 SDVTList VTs = Indexed ?
9860 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9861 SDValue Ops[] = { Chain, Ptr, Offset };
9863 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9864 ID.AddInteger(MemVT.getRawBits());
9865 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9866 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9867 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9868 ID.AddInteger(MMO->getFlags());
9869 void *IP = nullptr;
9870 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9871 E->refineAlignment(MMO);
9872 E->refineRanges(MMO);
9873 return SDValue(E, 0);
9874 }
9875 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9876 ExtType, MemVT, MMO);
9877 createOperands(N, Ops);
9878
9879 CSEMap.InsertNode(N, IP);
9880 InsertNode(N);
9881 SDValue V(N, 0);
9882 NewSDValueDbgMsg(V, "Creating new node: ", this);
9883 return V;
9884}
9885
9887 SDValue Ptr, MachinePointerInfo PtrInfo,
9888 MaybeAlign Alignment,
9889 MachineMemOperand::Flags MMOFlags,
9890 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9891 SDValue Undef = getUNDEF(Ptr.getValueType());
9892 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9893 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9894}
9895
9897 SDValue Ptr, MachineMemOperand *MMO) {
9898 SDValue Undef = getUNDEF(Ptr.getValueType());
9899 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9900 VT, MMO);
9901}
9902
9904 EVT VT, SDValue Chain, SDValue Ptr,
9905 MachinePointerInfo PtrInfo, EVT MemVT,
9906 MaybeAlign Alignment,
9907 MachineMemOperand::Flags MMOFlags,
9908 const AAMDNodes &AAInfo) {
9909 SDValue Undef = getUNDEF(Ptr.getValueType());
9910 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9911 MemVT, Alignment, MMOFlags, AAInfo);
9912}
9913
9915 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9916 MachineMemOperand *MMO) {
9917 SDValue Undef = getUNDEF(Ptr.getValueType());
9918 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9919 MemVT, MMO);
9920}
9921
9925 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9926 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9927 // Don't propagate the invariant or dereferenceable flags.
9928 auto MMOFlags =
9929 LD->getMemOperand()->getFlags() &
9931 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9932 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9933 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9934}
9935
9937 SDValue Ptr, MachinePointerInfo PtrInfo,
9938 Align Alignment,
9939 MachineMemOperand::Flags MMOFlags,
9940 const AAMDNodes &AAInfo) {
9941 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9942
9943 MMOFlags |= MachineMemOperand::MOStore;
9944 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9945
9946 if (PtrInfo.V.isNull())
9947 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9948
9951 MachineMemOperand *MMO =
9952 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9953 return getStore(Chain, dl, Val, Ptr, MMO);
9954}
9955
9957 SDValue Ptr, MachineMemOperand *MMO) {
9958 SDValue Undef = getUNDEF(Ptr.getValueType());
9959 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9961}
9962
9964 SDValue Ptr, SDValue Offset, EVT SVT,
9966 bool IsTruncating) {
9967 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9968 EVT VT = Val.getValueType();
9969 if (VT == SVT) {
9970 IsTruncating = false;
9971 } else if (!IsTruncating) {
9972 assert(VT == SVT && "No-truncating store from different memory type!");
9973 } else {
9975 "Should only be a truncating store, not extending!");
9976 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9977 assert(VT.isVector() == SVT.isVector() &&
9978 "Cannot use trunc store to convert to or from a vector!");
9979 assert((!VT.isVector() ||
9981 "Cannot use trunc store to change the number of vector elements!");
9982 }
9983
9984 bool Indexed = AM != ISD::UNINDEXED;
9985 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9986 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9987 : getVTList(MVT::Other);
9988 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9991 ID.AddInteger(SVT.getRawBits());
9992 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9993 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9994 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9995 ID.AddInteger(MMO->getFlags());
9996 void *IP = nullptr;
9997 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9998 cast<StoreSDNode>(E)->refineAlignment(MMO);
9999 return SDValue(E, 0);
10000 }
10001 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10002 IsTruncating, SVT, MMO);
10003 createOperands(N, Ops);
10004
10005 CSEMap.InsertNode(N, IP);
10006 InsertNode(N);
10007 SDValue V(N, 0);
10008 NewSDValueDbgMsg(V, "Creating new node: ", this);
10009 return V;
10010}
10011
10013 SDValue Ptr, MachinePointerInfo PtrInfo,
10014 EVT SVT, Align Alignment,
10015 MachineMemOperand::Flags MMOFlags,
10016 const AAMDNodes &AAInfo) {
10017 assert(Chain.getValueType() == MVT::Other &&
10018 "Invalid chain type");
10019
10020 MMOFlags |= MachineMemOperand::MOStore;
10021 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10022
10023 if (PtrInfo.V.isNull())
10024 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10025
10027 MachineMemOperand *MMO = MF.getMachineMemOperand(
10028 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10029 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10030}
10031
10033 SDValue Ptr, EVT SVT,
10034 MachineMemOperand *MMO) {
10035 SDValue Undef = getUNDEF(Ptr.getValueType());
10036 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10037}
10038
10042 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10043 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10044 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10045 ST->getMemoryVT(), ST->getMemOperand(), AM,
10046 ST->isTruncatingStore());
10047}
10048
10050 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10051 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10052 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10053 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10054 const MDNode *Ranges, bool IsExpanding) {
10055 MMOFlags |= MachineMemOperand::MOLoad;
10056 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10057 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10058 // clients.
10059 if (PtrInfo.V.isNull())
10060 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10061
10062 TypeSize Size = MemVT.getStoreSize();
10064 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10065 Alignment, AAInfo, Ranges);
10066 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10067 MMO, IsExpanding);
10068}
10069
10071 ISD::LoadExtType ExtType, EVT VT,
10072 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10073 SDValue Offset, SDValue Mask, SDValue EVL,
10074 EVT MemVT, MachineMemOperand *MMO,
10075 bool IsExpanding) {
10076 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10077 assert(Mask.getValueType().getVectorElementCount() ==
10078 VT.getVectorElementCount() &&
10079 "Vector width mismatch between mask and data");
10080
10081 bool Indexed = AM != ISD::UNINDEXED;
10082 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10083
10084 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10085 : getVTList(VT, MVT::Other);
10086 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10088 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10089 ID.AddInteger(MemVT.getRawBits());
10090 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10091 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10092 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10093 ID.AddInteger(MMO->getFlags());
10094 void *IP = nullptr;
10095 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10096 E->refineAlignment(MMO);
10097 E->refineRanges(MMO);
10098 return SDValue(E, 0);
10099 }
10100 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10101 ExtType, IsExpanding, MemVT, MMO);
10102 createOperands(N, Ops);
10103
10104 CSEMap.InsertNode(N, IP);
10105 InsertNode(N);
10106 SDValue V(N, 0);
10107 NewSDValueDbgMsg(V, "Creating new node: ", this);
10108 return V;
10109}
10110
10112 SDValue Ptr, SDValue Mask, SDValue EVL,
10113 MachinePointerInfo PtrInfo,
10114 MaybeAlign Alignment,
10115 MachineMemOperand::Flags MMOFlags,
10116 const AAMDNodes &AAInfo, const MDNode *Ranges,
10117 bool IsExpanding) {
10118 SDValue Undef = getUNDEF(Ptr.getValueType());
10119 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10120 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10121 IsExpanding);
10122}
10123
10125 SDValue Ptr, SDValue Mask, SDValue EVL,
10126 MachineMemOperand *MMO, bool IsExpanding) {
10127 SDValue Undef = getUNDEF(Ptr.getValueType());
10128 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10129 Mask, EVL, VT, MMO, IsExpanding);
10130}
10131
10133 EVT VT, SDValue Chain, SDValue Ptr,
10134 SDValue Mask, SDValue EVL,
10135 MachinePointerInfo PtrInfo, EVT MemVT,
10136 MaybeAlign Alignment,
10137 MachineMemOperand::Flags MMOFlags,
10138 const AAMDNodes &AAInfo, bool IsExpanding) {
10139 SDValue Undef = getUNDEF(Ptr.getValueType());
10140 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10141 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10142 IsExpanding);
10143}
10144
10146 EVT VT, SDValue Chain, SDValue Ptr,
10147 SDValue Mask, SDValue EVL, EVT MemVT,
10148 MachineMemOperand *MMO, bool IsExpanding) {
10149 SDValue Undef = getUNDEF(Ptr.getValueType());
10150 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10151 EVL, MemVT, MMO, IsExpanding);
10152}
10153
10157 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10158 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10159 // Don't propagate the invariant or dereferenceable flags.
10160 auto MMOFlags =
10161 LD->getMemOperand()->getFlags() &
10163 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10164 LD->getChain(), Base, Offset, LD->getMask(),
10165 LD->getVectorLength(), LD->getPointerInfo(),
10166 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10167 nullptr, LD->isExpandingLoad());
10168}
10169
10171 SDValue Ptr, SDValue Offset, SDValue Mask,
10172 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10173 ISD::MemIndexedMode AM, bool IsTruncating,
10174 bool IsCompressing) {
10175 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10176 assert(Mask.getValueType().getVectorElementCount() ==
10178 "Vector width mismatch between mask and data");
10179
10180 bool Indexed = AM != ISD::UNINDEXED;
10181 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10182 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10183 : getVTList(MVT::Other);
10184 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10186 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10187 ID.AddInteger(MemVT.getRawBits());
10188 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10189 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10190 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10191 ID.AddInteger(MMO->getFlags());
10192 void *IP = nullptr;
10193 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10194 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10195 return SDValue(E, 0);
10196 }
10197 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10198 IsTruncating, IsCompressing, MemVT, MMO);
10199 createOperands(N, Ops);
10200
10201 CSEMap.InsertNode(N, IP);
10202 InsertNode(N);
10203 SDValue V(N, 0);
10204 NewSDValueDbgMsg(V, "Creating new node: ", this);
10205 return V;
10206}
10207
10209 SDValue Val, SDValue Ptr, SDValue Mask,
10210 SDValue EVL, MachinePointerInfo PtrInfo,
10211 EVT SVT, Align Alignment,
10212 MachineMemOperand::Flags MMOFlags,
10213 const AAMDNodes &AAInfo,
10214 bool IsCompressing) {
10215 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10216
10217 MMOFlags |= MachineMemOperand::MOStore;
10218 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10219
10220 if (PtrInfo.V.isNull())
10221 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10222
10224 MachineMemOperand *MMO = MF.getMachineMemOperand(
10225 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10226 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10227 IsCompressing);
10228}
10229
10231 SDValue Val, SDValue Ptr, SDValue Mask,
10232 SDValue EVL, EVT SVT,
10233 MachineMemOperand *MMO,
10234 bool IsCompressing) {
10235 EVT VT = Val.getValueType();
10236
10237 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10238 if (VT == SVT)
10239 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10240 EVL, VT, MMO, ISD::UNINDEXED,
10241 /*IsTruncating*/ false, IsCompressing);
10242
10244 "Should only be a truncating store, not extending!");
10245 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10246 assert(VT.isVector() == SVT.isVector() &&
10247 "Cannot use trunc store to convert to or from a vector!");
10248 assert((!VT.isVector() ||
10250 "Cannot use trunc store to change the number of vector elements!");
10251
10252 SDVTList VTs = getVTList(MVT::Other);
10253 SDValue Undef = getUNDEF(Ptr.getValueType());
10254 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10256 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10257 ID.AddInteger(SVT.getRawBits());
10258 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10259 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10260 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10261 ID.AddInteger(MMO->getFlags());
10262 void *IP = nullptr;
10263 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10264 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10265 return SDValue(E, 0);
10266 }
10267 auto *N =
10268 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10269 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10270 createOperands(N, Ops);
10271
10272 CSEMap.InsertNode(N, IP);
10273 InsertNode(N);
10274 SDValue V(N, 0);
10275 NewSDValueDbgMsg(V, "Creating new node: ", this);
10276 return V;
10277}
10278
10282 auto *ST = cast<VPStoreSDNode>(OrigStore);
10283 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10284 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10285 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10286 Offset, ST->getMask(), ST->getVectorLength()};
10288 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10289 ID.AddInteger(ST->getMemoryVT().getRawBits());
10290 ID.AddInteger(ST->getRawSubclassData());
10291 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10292 ID.AddInteger(ST->getMemOperand()->getFlags());
10293 void *IP = nullptr;
10294 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10295 return SDValue(E, 0);
10296
10297 auto *N = newSDNode<VPStoreSDNode>(
10298 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10299 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10300 createOperands(N, Ops);
10301
10302 CSEMap.InsertNode(N, IP);
10303 InsertNode(N);
10304 SDValue V(N, 0);
10305 NewSDValueDbgMsg(V, "Creating new node: ", this);
10306 return V;
10307}
10308
10310 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10311 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10312 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10313 bool Indexed = AM != ISD::UNINDEXED;
10314 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10315
10316 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10317 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10318 : getVTList(VT, MVT::Other);
10320 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10321 ID.AddInteger(VT.getRawBits());
10322 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10323 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10324 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10325
10326 void *IP = nullptr;
10327 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10328 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10329 return SDValue(E, 0);
10330 }
10331
10332 auto *N =
10333 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10334 ExtType, IsExpanding, MemVT, MMO);
10335 createOperands(N, Ops);
10336 CSEMap.InsertNode(N, IP);
10337 InsertNode(N);
10338 SDValue V(N, 0);
10339 NewSDValueDbgMsg(V, "Creating new node: ", this);
10340 return V;
10341}
10342
10344 SDValue Ptr, SDValue Stride,
10345 SDValue Mask, SDValue EVL,
10346 MachineMemOperand *MMO,
10347 bool IsExpanding) {
10348 SDValue Undef = getUNDEF(Ptr.getValueType());
10349 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10350 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10351}
10352
10354 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10355 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10356 MachineMemOperand *MMO, bool IsExpanding) {
10357 SDValue Undef = getUNDEF(Ptr.getValueType());
10358 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10359 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10360}
10361
10363 SDValue Val, SDValue Ptr,
10364 SDValue Offset, SDValue Stride,
10365 SDValue Mask, SDValue EVL, EVT MemVT,
10366 MachineMemOperand *MMO,
10368 bool IsTruncating, bool IsCompressing) {
10369 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10370 bool Indexed = AM != ISD::UNINDEXED;
10371 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10372 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10373 : getVTList(MVT::Other);
10374 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10376 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10377 ID.AddInteger(MemVT.getRawBits());
10378 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10379 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10380 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10381 void *IP = nullptr;
10382 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10383 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10384 return SDValue(E, 0);
10385 }
10386 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10387 VTs, AM, IsTruncating,
10388 IsCompressing, MemVT, MMO);
10389 createOperands(N, Ops);
10390
10391 CSEMap.InsertNode(N, IP);
10392 InsertNode(N);
10393 SDValue V(N, 0);
10394 NewSDValueDbgMsg(V, "Creating new node: ", this);
10395 return V;
10396}
10397
10399 SDValue Val, SDValue Ptr,
10400 SDValue Stride, SDValue Mask,
10401 SDValue EVL, EVT SVT,
10402 MachineMemOperand *MMO,
10403 bool IsCompressing) {
10404 EVT VT = Val.getValueType();
10405
10406 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10407 if (VT == SVT)
10408 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10409 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10410 /*IsTruncating*/ false, IsCompressing);
10411
10413 "Should only be a truncating store, not extending!");
10414 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10415 assert(VT.isVector() == SVT.isVector() &&
10416 "Cannot use trunc store to convert to or from a vector!");
10417 assert((!VT.isVector() ||
10419 "Cannot use trunc store to change the number of vector elements!");
10420
10421 SDVTList VTs = getVTList(MVT::Other);
10422 SDValue Undef = getUNDEF(Ptr.getValueType());
10423 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10425 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10426 ID.AddInteger(SVT.getRawBits());
10427 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10428 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10429 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10430 void *IP = nullptr;
10431 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10432 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10433 return SDValue(E, 0);
10434 }
10435 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10436 VTs, ISD::UNINDEXED, true,
10437 IsCompressing, SVT, MMO);
10438 createOperands(N, Ops);
10439
10440 CSEMap.InsertNode(N, IP);
10441 InsertNode(N);
10442 SDValue V(N, 0);
10443 NewSDValueDbgMsg(V, "Creating new node: ", this);
10444 return V;
10445}
10446
10449 ISD::MemIndexType IndexType) {
10450 assert(Ops.size() == 6 && "Incompatible number of operands");
10451
10453 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10454 ID.AddInteger(VT.getRawBits());
10455 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10456 dl.getIROrder(), VTs, VT, MMO, IndexType));
10457 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10458 ID.AddInteger(MMO->getFlags());
10459 void *IP = nullptr;
10460 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10461 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10462 return SDValue(E, 0);
10463 }
10464
10465 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10466 VT, MMO, IndexType);
10467 createOperands(N, Ops);
10468
10469 assert(N->getMask().getValueType().getVectorElementCount() ==
10470 N->getValueType(0).getVectorElementCount() &&
10471 "Vector width mismatch between mask and data");
10472 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10473 N->getValueType(0).getVectorElementCount().isScalable() &&
10474 "Scalable flags of index and data do not match");
10476 N->getIndex().getValueType().getVectorElementCount(),
10477 N->getValueType(0).getVectorElementCount()) &&
10478 "Vector width mismatch between index and data");
10479 assert(isa<ConstantSDNode>(N->getScale()) &&
10480 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10481 "Scale should be a constant power of 2");
10482
10483 CSEMap.InsertNode(N, IP);
10484 InsertNode(N);
10485 SDValue V(N, 0);
10486 NewSDValueDbgMsg(V, "Creating new node: ", this);
10487 return V;
10488}
10489
10492 MachineMemOperand *MMO,
10493 ISD::MemIndexType IndexType) {
10494 assert(Ops.size() == 7 && "Incompatible number of operands");
10495
10497 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10498 ID.AddInteger(VT.getRawBits());
10499 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10500 dl.getIROrder(), VTs, VT, MMO, IndexType));
10501 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10502 ID.AddInteger(MMO->getFlags());
10503 void *IP = nullptr;
10504 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10505 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10506 return SDValue(E, 0);
10507 }
10508 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10509 VT, MMO, IndexType);
10510 createOperands(N, Ops);
10511
10512 assert(N->getMask().getValueType().getVectorElementCount() ==
10513 N->getValue().getValueType().getVectorElementCount() &&
10514 "Vector width mismatch between mask and data");
10515 assert(
10516 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10517 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10518 "Scalable flags of index and data do not match");
10520 N->getIndex().getValueType().getVectorElementCount(),
10521 N->getValue().getValueType().getVectorElementCount()) &&
10522 "Vector width mismatch between index and data");
10523 assert(isa<ConstantSDNode>(N->getScale()) &&
10524 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10525 "Scale should be a constant power of 2");
10526
10527 CSEMap.InsertNode(N, IP);
10528 InsertNode(N);
10529 SDValue V(N, 0);
10530 NewSDValueDbgMsg(V, "Creating new node: ", this);
10531 return V;
10532}
10533
10536 SDValue PassThru, EVT MemVT,
10537 MachineMemOperand *MMO,
10539 ISD::LoadExtType ExtTy, bool isExpanding) {
10540 bool Indexed = AM != ISD::UNINDEXED;
10541 assert((Indexed || Offset.isUndef()) &&
10542 "Unindexed masked load with an offset!");
10543 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10544 : getVTList(VT, MVT::Other);
10545 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10548 ID.AddInteger(MemVT.getRawBits());
10549 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10550 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10551 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10552 ID.AddInteger(MMO->getFlags());
10553 void *IP = nullptr;
10554 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10555 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10556 return SDValue(E, 0);
10557 }
10558 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10559 AM, ExtTy, isExpanding, MemVT, MMO);
10560 createOperands(N, Ops);
10561
10562 CSEMap.InsertNode(N, IP);
10563 InsertNode(N);
10564 SDValue V(N, 0);
10565 NewSDValueDbgMsg(V, "Creating new node: ", this);
10566 return V;
10567}
10568
10573 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10574 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10575 Offset, LD->getMask(), LD->getPassThru(),
10576 LD->getMemoryVT(), LD->getMemOperand(), AM,
10577 LD->getExtensionType(), LD->isExpandingLoad());
10578}
10579
10582 SDValue Mask, EVT MemVT,
10583 MachineMemOperand *MMO,
10584 ISD::MemIndexedMode AM, bool IsTruncating,
10585 bool IsCompressing) {
10586 assert(Chain.getValueType() == MVT::Other &&
10587 "Invalid chain type");
10588 bool Indexed = AM != ISD::UNINDEXED;
10589 assert((Indexed || Offset.isUndef()) &&
10590 "Unindexed masked store with an offset!");
10591 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10592 : getVTList(MVT::Other);
10593 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10596 ID.AddInteger(MemVT.getRawBits());
10597 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10598 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10599 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10600 ID.AddInteger(MMO->getFlags());
10601 void *IP = nullptr;
10602 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10603 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10604 return SDValue(E, 0);
10605 }
10606 auto *N =
10607 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10608 IsTruncating, IsCompressing, MemVT, MMO);
10609 createOperands(N, Ops);
10610
10611 CSEMap.InsertNode(N, IP);
10612 InsertNode(N);
10613 SDValue V(N, 0);
10614 NewSDValueDbgMsg(V, "Creating new node: ", this);
10615 return V;
10616}
10617
10622 assert(ST->getOffset().isUndef() &&
10623 "Masked store is already a indexed store!");
10624 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10625 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10626 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10627}
10628
10631 MachineMemOperand *MMO,
10632 ISD::MemIndexType IndexType,
10633 ISD::LoadExtType ExtTy) {
10634 assert(Ops.size() == 6 && "Incompatible number of operands");
10635
10638 ID.AddInteger(MemVT.getRawBits());
10639 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10640 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10641 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10642 ID.AddInteger(MMO->getFlags());
10643 void *IP = nullptr;
10644 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10645 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10646 return SDValue(E, 0);
10647 }
10648
10649 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10650 VTs, MemVT, MMO, IndexType, ExtTy);
10651 createOperands(N, Ops);
10652
10653 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10654 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10655 assert(N->getMask().getValueType().getVectorElementCount() ==
10656 N->getValueType(0).getVectorElementCount() &&
10657 "Vector width mismatch between mask and data");
10658 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10659 N->getValueType(0).getVectorElementCount().isScalable() &&
10660 "Scalable flags of index and data do not match");
10662 N->getIndex().getValueType().getVectorElementCount(),
10663 N->getValueType(0).getVectorElementCount()) &&
10664 "Vector width mismatch between index and data");
10665 assert(isa<ConstantSDNode>(N->getScale()) &&
10666 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10667 "Scale should be a constant power of 2");
10668
10669 CSEMap.InsertNode(N, IP);
10670 InsertNode(N);
10671 SDValue V(N, 0);
10672 NewSDValueDbgMsg(V, "Creating new node: ", this);
10673 return V;
10674}
10675
10678 MachineMemOperand *MMO,
10679 ISD::MemIndexType IndexType,
10680 bool IsTrunc) {
10681 assert(Ops.size() == 6 && "Incompatible number of operands");
10682
10685 ID.AddInteger(MemVT.getRawBits());
10686 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10687 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10688 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10689 ID.AddInteger(MMO->getFlags());
10690 void *IP = nullptr;
10691 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10692 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10693 return SDValue(E, 0);
10694 }
10695
10696 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10697 VTs, MemVT, MMO, IndexType, IsTrunc);
10698 createOperands(N, Ops);
10699
10700 assert(N->getMask().getValueType().getVectorElementCount() ==
10701 N->getValue().getValueType().getVectorElementCount() &&
10702 "Vector width mismatch between mask and data");
10703 assert(
10704 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10705 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10706 "Scalable flags of index and data do not match");
10708 N->getIndex().getValueType().getVectorElementCount(),
10709 N->getValue().getValueType().getVectorElementCount()) &&
10710 "Vector width mismatch between index and data");
10711 assert(isa<ConstantSDNode>(N->getScale()) &&
10712 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10713 "Scale should be a constant power of 2");
10714
10715 CSEMap.InsertNode(N, IP);
10716 InsertNode(N);
10717 SDValue V(N, 0);
10718 NewSDValueDbgMsg(V, "Creating new node: ", this);
10719 return V;
10720}
10721
10723 const SDLoc &dl, ArrayRef<SDValue> Ops,
10724 MachineMemOperand *MMO,
10725 ISD::MemIndexType IndexType) {
10726 assert(Ops.size() == 7 && "Incompatible number of operands");
10727
10730 ID.AddInteger(MemVT.getRawBits());
10731 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10732 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10733 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10734 ID.AddInteger(MMO->getFlags());
10735 void *IP = nullptr;
10736 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10737 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10738 return SDValue(E, 0);
10739 }
10740
10741 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10742 VTs, MemVT, MMO, IndexType);
10743 createOperands(N, Ops);
10744
10745 assert(N->getMask().getValueType().getVectorElementCount() ==
10746 N->getIndex().getValueType().getVectorElementCount() &&
10747 "Vector width mismatch between mask and data");
10748 assert(isa<ConstantSDNode>(N->getScale()) &&
10749 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10750 "Scale should be a constant power of 2");
10751 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10752
10753 CSEMap.InsertNode(N, IP);
10754 InsertNode(N);
10755 SDValue V(N, 0);
10756 NewSDValueDbgMsg(V, "Creating new node: ", this);
10757 return V;
10758}
10759
10761 SDValue Ptr, SDValue Mask, SDValue EVL,
10762 MachineMemOperand *MMO) {
10763 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10764 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10766 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10767 ID.AddInteger(VT.getRawBits());
10768 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10769 VTs, VT, MMO));
10770 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10771 ID.AddInteger(MMO->getFlags());
10772 void *IP = nullptr;
10773 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10774 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10775 return SDValue(E, 0);
10776 }
10777 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10778 VT, MMO);
10779 createOperands(N, Ops);
10780
10781 CSEMap.InsertNode(N, IP);
10782 InsertNode(N);
10783 SDValue V(N, 0);
10784 NewSDValueDbgMsg(V, "Creating new node: ", this);
10785 return V;
10786}
10787
10789 EVT MemVT, MachineMemOperand *MMO) {
10790 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10791 SDVTList VTs = getVTList(MVT::Other);
10792 SDValue Ops[] = {Chain, Ptr};
10795 ID.AddInteger(MemVT.getRawBits());
10796 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10797 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10798 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10799 ID.AddInteger(MMO->getFlags());
10800 void *IP = nullptr;
10801 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10802 return SDValue(E, 0);
10803
10804 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10805 dl.getDebugLoc(), VTs, MemVT, MMO);
10806 createOperands(N, Ops);
10807
10808 CSEMap.InsertNode(N, IP);
10809 InsertNode(N);
10810 SDValue V(N, 0);
10811 NewSDValueDbgMsg(V, "Creating new node: ", this);
10812 return V;
10813}
10814
10816 EVT MemVT, MachineMemOperand *MMO) {
10817 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10818 SDVTList VTs = getVTList(MVT::Other);
10819 SDValue Ops[] = {Chain, Ptr};
10822 ID.AddInteger(MemVT.getRawBits());
10823 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10824 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10825 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10826 ID.AddInteger(MMO->getFlags());
10827 void *IP = nullptr;
10828 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10829 return SDValue(E, 0);
10830
10831 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10832 dl.getDebugLoc(), VTs, MemVT, MMO);
10833 createOperands(N, Ops);
10834
10835 CSEMap.InsertNode(N, IP);
10836 InsertNode(N);
10837 SDValue V(N, 0);
10838 NewSDValueDbgMsg(V, "Creating new node: ", this);
10839 return V;
10840}
10841
10843 // select undef, T, F --> T (if T is a constant), otherwise F
10844 // select, ?, undef, F --> F
10845 // select, ?, T, undef --> T
10846 if (Cond.isUndef())
10847 return isConstantValueOfAnyType(T) ? T : F;
10848 if (T.isUndef())
10850 if (F.isUndef())
10852
10853 // select true, T, F --> T
10854 // select false, T, F --> F
10855 if (auto C = isBoolConstant(Cond))
10856 return *C ? T : F;
10857
10858 // select ?, T, T --> T
10859 if (T == F)
10860 return T;
10861
10862 return SDValue();
10863}
10864
10866 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10867 if (X.isUndef())
10868 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10869 // shift X, undef --> undef (because it may shift by the bitwidth)
10870 if (Y.isUndef())
10871 return getUNDEF(X.getValueType());
10872
10873 // shift 0, Y --> 0
10874 // shift X, 0 --> X
10876 return X;
10877
10878 // shift X, C >= bitwidth(X) --> undef
10879 // All vector elements must be too big (or undef) to avoid partial undefs.
10880 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10881 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10882 };
10883 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10884 return getUNDEF(X.getValueType());
10885
10886 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10887 if (X.getValueType().getScalarType() == MVT::i1)
10888 return X;
10889
10890 return SDValue();
10891}
10892
10894 SDNodeFlags Flags) {
10895 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10896 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10897 // operation is poison. That result can be relaxed to undef.
10898 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10899 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10900 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10901 (YC && YC->getValueAPF().isNaN());
10902 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10903 (YC && YC->getValueAPF().isInfinity());
10904
10905 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10906 return getUNDEF(X.getValueType());
10907
10908 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10909 return getUNDEF(X.getValueType());
10910
10911 if (!YC)
10912 return SDValue();
10913
10914 // X + -0.0 --> X
10915 if (Opcode == ISD::FADD)
10916 if (YC->getValueAPF().isNegZero())
10917 return X;
10918
10919 // X - +0.0 --> X
10920 if (Opcode == ISD::FSUB)
10921 if (YC->getValueAPF().isPosZero())
10922 return X;
10923
10924 // X * 1.0 --> X
10925 // X / 1.0 --> X
10926 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10927 if (YC->getValueAPF().isExactlyValue(1.0))
10928 return X;
10929
10930 // X * 0.0 --> 0.0
10931 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10932 if (YC->getValueAPF().isZero())
10933 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10934
10935 return SDValue();
10936}
10937
10939 SDValue Ptr, SDValue SV, unsigned Align) {
10940 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10941 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10942}
10943
10944SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10946 switch (Ops.size()) {
10947 case 0: return getNode(Opcode, DL, VT);
10948 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10949 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10950 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10951 default: break;
10952 }
10953
10954 // Copy from an SDUse array into an SDValue array for use with
10955 // the regular getNode logic.
10957 return getNode(Opcode, DL, VT, NewOps);
10958}
10959
10960SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10962 SDNodeFlags Flags;
10963 if (Inserter)
10964 Flags = Inserter->getFlags();
10965 return getNode(Opcode, DL, VT, Ops, Flags);
10966}
10967
10968SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10969 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10970 unsigned NumOps = Ops.size();
10971 switch (NumOps) {
10972 case 0: return getNode(Opcode, DL, VT);
10973 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10974 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10975 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10976 default: break;
10977 }
10978
10979#ifndef NDEBUG
10980 for (const auto &Op : Ops)
10981 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10982 "Operand is DELETED_NODE!");
10983#endif
10984
10985 switch (Opcode) {
10986 default: break;
10987 case ISD::BUILD_VECTOR:
10988 // Attempt to simplify BUILD_VECTOR.
10989 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10990 return V;
10991 break;
10993 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10994 return V;
10995 break;
10996 case ISD::SELECT_CC:
10997 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10998 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10999 "LHS and RHS of condition must have same type!");
11000 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11001 "True and False arms of SelectCC must have same type!");
11002 assert(Ops[2].getValueType() == VT &&
11003 "select_cc node must be of same type as true and false value!");
11004 assert((!Ops[0].getValueType().isVector() ||
11005 Ops[0].getValueType().getVectorElementCount() ==
11006 VT.getVectorElementCount()) &&
11007 "Expected select_cc with vector result to have the same sized "
11008 "comparison type!");
11009 break;
11010 case ISD::BR_CC:
11011 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11012 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11013 "LHS/RHS of comparison should match types!");
11014 break;
11015 case ISD::VP_ADD:
11016 case ISD::VP_SUB:
11017 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11018 if (VT.getScalarType() == MVT::i1)
11019 Opcode = ISD::VP_XOR;
11020 break;
11021 case ISD::VP_MUL:
11022 // If it is VP_MUL mask operation then turn it to VP_AND
11023 if (VT.getScalarType() == MVT::i1)
11024 Opcode = ISD::VP_AND;
11025 break;
11026 case ISD::VP_REDUCE_MUL:
11027 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11028 if (VT == MVT::i1)
11029 Opcode = ISD::VP_REDUCE_AND;
11030 break;
11031 case ISD::VP_REDUCE_ADD:
11032 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11033 if (VT == MVT::i1)
11034 Opcode = ISD::VP_REDUCE_XOR;
11035 break;
11036 case ISD::VP_REDUCE_SMAX:
11037 case ISD::VP_REDUCE_UMIN:
11038 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11039 // VP_REDUCE_AND.
11040 if (VT == MVT::i1)
11041 Opcode = ISD::VP_REDUCE_AND;
11042 break;
11043 case ISD::VP_REDUCE_SMIN:
11044 case ISD::VP_REDUCE_UMAX:
11045 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11046 // VP_REDUCE_OR.
11047 if (VT == MVT::i1)
11048 Opcode = ISD::VP_REDUCE_OR;
11049 break;
11050 }
11051
11052 // Memoize nodes.
11053 SDNode *N;
11054 SDVTList VTs = getVTList(VT);
11055
11056 if (VT != MVT::Glue) {
11058 AddNodeIDNode(ID, Opcode, VTs, Ops);
11059 void *IP = nullptr;
11060
11061 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11062 E->intersectFlagsWith(Flags);
11063 return SDValue(E, 0);
11064 }
11065
11066 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11067 createOperands(N, Ops);
11068
11069 CSEMap.InsertNode(N, IP);
11070 } else {
11071 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11072 createOperands(N, Ops);
11073 }
11074
11075 N->setFlags(Flags);
11076 InsertNode(N);
11077 SDValue V(N, 0);
11078 NewSDValueDbgMsg(V, "Creating new node: ", this);
11079 return V;
11080}
11081
11082SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11083 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11084 SDNodeFlags Flags;
11085 if (Inserter)
11086 Flags = Inserter->getFlags();
11087 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11088}
11089
11090SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11092 const SDNodeFlags Flags) {
11093 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11094}
11095
11096SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11098 SDNodeFlags Flags;
11099 if (Inserter)
11100 Flags = Inserter->getFlags();
11101 return getNode(Opcode, DL, VTList, Ops, Flags);
11102}
11103
11104SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11105 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11106 if (VTList.NumVTs == 1)
11107 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11108
11109#ifndef NDEBUG
11110 for (const auto &Op : Ops)
11111 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11112 "Operand is DELETED_NODE!");
11113#endif
11114
11115 switch (Opcode) {
11116 case ISD::SADDO:
11117 case ISD::UADDO:
11118 case ISD::SSUBO:
11119 case ISD::USUBO: {
11120 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11121 "Invalid add/sub overflow op!");
11122 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11123 Ops[0].getValueType() == Ops[1].getValueType() &&
11124 Ops[0].getValueType() == VTList.VTs[0] &&
11125 "Binary operator types must match!");
11126 SDValue N1 = Ops[0], N2 = Ops[1];
11127 canonicalizeCommutativeBinop(Opcode, N1, N2);
11128
11129 // (X +- 0) -> X with zero-overflow.
11130 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11131 /*AllowTruncation*/ true);
11132 if (N2CV && N2CV->isZero()) {
11133 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11134 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11135 }
11136
11137 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11138 VTList.VTs[1].getScalarType() == MVT::i1) {
11139 SDValue F1 = getFreeze(N1);
11140 SDValue F2 = getFreeze(N2);
11141 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11142 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11143 return getNode(ISD::MERGE_VALUES, DL, VTList,
11144 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11145 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11146 Flags);
11147 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11148 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11149 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11150 return getNode(ISD::MERGE_VALUES, DL, VTList,
11151 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11152 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11153 Flags);
11154 }
11155 }
11156 break;
11157 }
11158 case ISD::SADDO_CARRY:
11159 case ISD::UADDO_CARRY:
11160 case ISD::SSUBO_CARRY:
11161 case ISD::USUBO_CARRY:
11162 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11163 "Invalid add/sub overflow op!");
11164 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11165 Ops[0].getValueType() == Ops[1].getValueType() &&
11166 Ops[0].getValueType() == VTList.VTs[0] &&
11167 Ops[2].getValueType() == VTList.VTs[1] &&
11168 "Binary operator types must match!");
11169 break;
11170 case ISD::SMUL_LOHI:
11171 case ISD::UMUL_LOHI: {
11172 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11173 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11174 VTList.VTs[0] == Ops[0].getValueType() &&
11175 VTList.VTs[0] == Ops[1].getValueType() &&
11176 "Binary operator types must match!");
11177 // Constant fold.
11180 if (LHS && RHS) {
11181 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11182 unsigned OutWidth = Width * 2;
11183 APInt Val = LHS->getAPIntValue();
11184 APInt Mul = RHS->getAPIntValue();
11185 if (Opcode == ISD::SMUL_LOHI) {
11186 Val = Val.sext(OutWidth);
11187 Mul = Mul.sext(OutWidth);
11188 } else {
11189 Val = Val.zext(OutWidth);
11190 Mul = Mul.zext(OutWidth);
11191 }
11192 Val *= Mul;
11193
11194 SDValue Hi =
11195 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11196 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11197 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11198 }
11199 break;
11200 }
11201 case ISD::FFREXP: {
11202 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11203 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11204 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11205
11207 int FrexpExp;
11208 APFloat FrexpMant =
11209 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11210 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11211 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11212 DL, VTList.VTs[1]);
11213 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11214 }
11215
11216 break;
11217 }
11219 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11220 "Invalid STRICT_FP_EXTEND!");
11221 assert(VTList.VTs[0].isFloatingPoint() &&
11222 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11223 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11224 "STRICT_FP_EXTEND result type should be vector iff the operand "
11225 "type is vector!");
11226 assert((!VTList.VTs[0].isVector() ||
11227 VTList.VTs[0].getVectorElementCount() ==
11228 Ops[1].getValueType().getVectorElementCount()) &&
11229 "Vector element count mismatch!");
11230 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11231 "Invalid fpext node, dst <= src!");
11232 break;
11234 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11235 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11236 "STRICT_FP_ROUND result type should be vector iff the operand "
11237 "type is vector!");
11238 assert((!VTList.VTs[0].isVector() ||
11239 VTList.VTs[0].getVectorElementCount() ==
11240 Ops[1].getValueType().getVectorElementCount()) &&
11241 "Vector element count mismatch!");
11242 assert(VTList.VTs[0].isFloatingPoint() &&
11243 Ops[1].getValueType().isFloatingPoint() &&
11244 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11245 Ops[2].getOpcode() == ISD::TargetConstant &&
11246 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11247 "Invalid STRICT_FP_ROUND!");
11248 break;
11249 }
11250
11251 // Memoize the node unless it returns a glue result.
11252 SDNode *N;
11253 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11255 AddNodeIDNode(ID, Opcode, VTList, Ops);
11256 void *IP = nullptr;
11257 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11258 E->intersectFlagsWith(Flags);
11259 return SDValue(E, 0);
11260 }
11261
11262 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11263 createOperands(N, Ops);
11264 CSEMap.InsertNode(N, IP);
11265 } else {
11266 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11267 createOperands(N, Ops);
11268 }
11269
11270 N->setFlags(Flags);
11271 InsertNode(N);
11272 SDValue V(N, 0);
11273 NewSDValueDbgMsg(V, "Creating new node: ", this);
11274 return V;
11275}
11276
11277SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11278 SDVTList VTList) {
11279 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11280}
11281
11282SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11283 SDValue N1) {
11284 SDValue Ops[] = { N1 };
11285 return getNode(Opcode, DL, VTList, Ops);
11286}
11287
11288SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11289 SDValue N1, SDValue N2) {
11290 SDValue Ops[] = { N1, N2 };
11291 return getNode(Opcode, DL, VTList, Ops);
11292}
11293
11294SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11295 SDValue N1, SDValue N2, SDValue N3) {
11296 SDValue Ops[] = { N1, N2, N3 };
11297 return getNode(Opcode, DL, VTList, Ops);
11298}
11299
11300SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11301 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11302 SDValue Ops[] = { N1, N2, N3, N4 };
11303 return getNode(Opcode, DL, VTList, Ops);
11304}
11305
11306SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11307 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11308 SDValue N5) {
11309 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11310 return getNode(Opcode, DL, VTList, Ops);
11311}
11312
11314 if (!VT.isExtended())
11315 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11316
11317 return makeVTList(&(*EVTs.insert(VT).first), 1);
11318}
11319
11322 ID.AddInteger(2U);
11323 ID.AddInteger(VT1.getRawBits());
11324 ID.AddInteger(VT2.getRawBits());
11325
11326 void *IP = nullptr;
11327 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11328 if (!Result) {
11329 EVT *Array = Allocator.Allocate<EVT>(2);
11330 Array[0] = VT1;
11331 Array[1] = VT2;
11332 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11333 VTListMap.InsertNode(Result, IP);
11334 }
11335 return Result->getSDVTList();
11336}
11337
11340 ID.AddInteger(3U);
11341 ID.AddInteger(VT1.getRawBits());
11342 ID.AddInteger(VT2.getRawBits());
11343 ID.AddInteger(VT3.getRawBits());
11344
11345 void *IP = nullptr;
11346 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11347 if (!Result) {
11348 EVT *Array = Allocator.Allocate<EVT>(3);
11349 Array[0] = VT1;
11350 Array[1] = VT2;
11351 Array[2] = VT3;
11352 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11353 VTListMap.InsertNode(Result, IP);
11354 }
11355 return Result->getSDVTList();
11356}
11357
11360 ID.AddInteger(4U);
11361 ID.AddInteger(VT1.getRawBits());
11362 ID.AddInteger(VT2.getRawBits());
11363 ID.AddInteger(VT3.getRawBits());
11364 ID.AddInteger(VT4.getRawBits());
11365
11366 void *IP = nullptr;
11367 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11368 if (!Result) {
11369 EVT *Array = Allocator.Allocate<EVT>(4);
11370 Array[0] = VT1;
11371 Array[1] = VT2;
11372 Array[2] = VT3;
11373 Array[3] = VT4;
11374 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11375 VTListMap.InsertNode(Result, IP);
11376 }
11377 return Result->getSDVTList();
11378}
11379
11381 unsigned NumVTs = VTs.size();
11383 ID.AddInteger(NumVTs);
11384 for (unsigned index = 0; index < NumVTs; index++) {
11385 ID.AddInteger(VTs[index].getRawBits());
11386 }
11387
11388 void *IP = nullptr;
11389 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11390 if (!Result) {
11391 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11392 llvm::copy(VTs, Array);
11393 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11394 VTListMap.InsertNode(Result, IP);
11395 }
11396 return Result->getSDVTList();
11397}
11398
11399
11400/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11401/// specified operands. If the resultant node already exists in the DAG,
11402/// this does not modify the specified node, instead it returns the node that
11403/// already exists. If the resultant node does not exist in the DAG, the
11404/// input node is returned. As a degenerate case, if you specify the same
11405/// input operands as the node already has, the input node is returned.
11407 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11408
11409 // Check to see if there is no change.
11410 if (Op == N->getOperand(0)) return N;
11411
11412 // See if the modified node already exists.
11413 void *InsertPos = nullptr;
11414 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11415 return Existing;
11416
11417 // Nope it doesn't. Remove the node from its current place in the maps.
11418 if (InsertPos)
11419 if (!RemoveNodeFromCSEMaps(N))
11420 InsertPos = nullptr;
11421
11422 // Now we update the operands.
11423 N->OperandList[0].set(Op);
11424
11426 // If this gets put into a CSE map, add it.
11427 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11428 return N;
11429}
11430
11432 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11433
11434 // Check to see if there is no change.
11435 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11436 return N; // No operands changed, just return the input node.
11437
11438 // See if the modified node already exists.
11439 void *InsertPos = nullptr;
11440 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11441 return Existing;
11442
11443 // Nope it doesn't. Remove the node from its current place in the maps.
11444 if (InsertPos)
11445 if (!RemoveNodeFromCSEMaps(N))
11446 InsertPos = nullptr;
11447
11448 // Now we update the operands.
11449 if (N->OperandList[0] != Op1)
11450 N->OperandList[0].set(Op1);
11451 if (N->OperandList[1] != Op2)
11452 N->OperandList[1].set(Op2);
11453
11455 // If this gets put into a CSE map, add it.
11456 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11457 return N;
11458}
11459
11462 SDValue Ops[] = { Op1, Op2, Op3 };
11463 return UpdateNodeOperands(N, Ops);
11464}
11465
11468 SDValue Op3, SDValue Op4) {
11469 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11470 return UpdateNodeOperands(N, Ops);
11471}
11472
11475 SDValue Op3, SDValue Op4, SDValue Op5) {
11476 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11477 return UpdateNodeOperands(N, Ops);
11478}
11479
11482 unsigned NumOps = Ops.size();
11483 assert(N->getNumOperands() == NumOps &&
11484 "Update with wrong number of operands");
11485
11486 // If no operands changed just return the input node.
11487 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11488 return N;
11489
11490 // See if the modified node already exists.
11491 void *InsertPos = nullptr;
11492 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11493 return Existing;
11494
11495 // Nope it doesn't. Remove the node from its current place in the maps.
11496 if (InsertPos)
11497 if (!RemoveNodeFromCSEMaps(N))
11498 InsertPos = nullptr;
11499
11500 // Now we update the operands.
11501 for (unsigned i = 0; i != NumOps; ++i)
11502 if (N->OperandList[i] != Ops[i])
11503 N->OperandList[i].set(Ops[i]);
11504
11506 // If this gets put into a CSE map, add it.
11507 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11508 return N;
11509}
11510
11511/// DropOperands - Release the operands and set this node to have
11512/// zero operands.
11514 // Unlike the code in MorphNodeTo that does this, we don't need to
11515 // watch for dead nodes here.
11516 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11517 SDUse &Use = *I++;
11518 Use.set(SDValue());
11519 }
11520}
11521
11523 ArrayRef<MachineMemOperand *> NewMemRefs) {
11524 if (NewMemRefs.empty()) {
11525 N->clearMemRefs();
11526 return;
11527 }
11528
11529 // Check if we can avoid allocating by storing a single reference directly.
11530 if (NewMemRefs.size() == 1) {
11531 N->MemRefs = NewMemRefs[0];
11532 N->NumMemRefs = 1;
11533 return;
11534 }
11535
11536 MachineMemOperand **MemRefsBuffer =
11537 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11538 llvm::copy(NewMemRefs, MemRefsBuffer);
11539 N->MemRefs = MemRefsBuffer;
11540 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11541}
11542
11543/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11544/// machine opcode.
11545///
11547 EVT VT) {
11548 SDVTList VTs = getVTList(VT);
11549 return SelectNodeTo(N, MachineOpc, VTs, {});
11550}
11551
11553 EVT VT, SDValue Op1) {
11554 SDVTList VTs = getVTList(VT);
11555 SDValue Ops[] = { Op1 };
11556 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11557}
11558
11560 EVT VT, SDValue Op1,
11561 SDValue Op2) {
11562 SDVTList VTs = getVTList(VT);
11563 SDValue Ops[] = { Op1, Op2 };
11564 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11565}
11566
11568 EVT VT, SDValue Op1,
11569 SDValue Op2, SDValue Op3) {
11570 SDVTList VTs = getVTList(VT);
11571 SDValue Ops[] = { Op1, Op2, Op3 };
11572 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11573}
11574
11577 SDVTList VTs = getVTList(VT);
11578 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11579}
11580
11582 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11583 SDVTList VTs = getVTList(VT1, VT2);
11584 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11585}
11586
11588 EVT VT1, EVT VT2) {
11589 SDVTList VTs = getVTList(VT1, VT2);
11590 return SelectNodeTo(N, MachineOpc, VTs, {});
11591}
11592
11594 EVT VT1, EVT VT2, EVT VT3,
11596 SDVTList VTs = getVTList(VT1, VT2, VT3);
11597 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11598}
11599
11601 EVT VT1, EVT VT2,
11602 SDValue Op1, SDValue Op2) {
11603 SDVTList VTs = getVTList(VT1, VT2);
11604 SDValue Ops[] = { Op1, Op2 };
11605 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11606}
11607
11610 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11611 // Reset the NodeID to -1.
11612 New->setNodeId(-1);
11613 if (New != N) {
11614 ReplaceAllUsesWith(N, New);
11616 }
11617 return New;
11618}
11619
11620/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11621/// the line number information on the merged node since it is not possible to
11622/// preserve the information that operation is associated with multiple lines.
11623/// This will make the debugger working better at -O0, were there is a higher
11624/// probability having other instructions associated with that line.
11625///
11626/// For IROrder, we keep the smaller of the two
11627SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11628 DebugLoc NLoc = N->getDebugLoc();
11629 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11630 N->setDebugLoc(DebugLoc());
11631 }
11632 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11633 N->setIROrder(Order);
11634 return N;
11635}
11636
11637/// MorphNodeTo - This *mutates* the specified node to have the specified
11638/// return type, opcode, and operands.
11639///
11640/// Note that MorphNodeTo returns the resultant node. If there is already a
11641/// node of the specified opcode and operands, it returns that node instead of
11642/// the current one. Note that the SDLoc need not be the same.
11643///
11644/// Using MorphNodeTo is faster than creating a new node and swapping it in
11645/// with ReplaceAllUsesWith both because it often avoids allocating a new
11646/// node, and because it doesn't require CSE recalculation for any of
11647/// the node's users.
11648///
11649/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11650/// As a consequence it isn't appropriate to use from within the DAG combiner or
11651/// the legalizer which maintain worklists that would need to be updated when
11652/// deleting things.
11655 // If an identical node already exists, use it.
11656 void *IP = nullptr;
11657 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11659 AddNodeIDNode(ID, Opc, VTs, Ops);
11660 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11661 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11662 }
11663
11664 if (!RemoveNodeFromCSEMaps(N))
11665 IP = nullptr;
11666
11667 // Start the morphing.
11668 N->NodeType = Opc;
11669 N->ValueList = VTs.VTs;
11670 N->NumValues = VTs.NumVTs;
11671
11672 // Clear the operands list, updating used nodes to remove this from their
11673 // use list. Keep track of any operands that become dead as a result.
11674 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11675 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11676 SDUse &Use = *I++;
11677 SDNode *Used = Use.getNode();
11678 Use.set(SDValue());
11679 if (Used->use_empty())
11680 DeadNodeSet.insert(Used);
11681 }
11682
11683 // For MachineNode, initialize the memory references information.
11685 MN->clearMemRefs();
11686
11687 // Swap for an appropriately sized array from the recycler.
11688 removeOperands(N);
11689 createOperands(N, Ops);
11690
11691 // Delete any nodes that are still dead after adding the uses for the
11692 // new operands.
11693 if (!DeadNodeSet.empty()) {
11694 SmallVector<SDNode *, 16> DeadNodes;
11695 for (SDNode *N : DeadNodeSet)
11696 if (N->use_empty())
11697 DeadNodes.push_back(N);
11698 RemoveDeadNodes(DeadNodes);
11699 }
11700
11701 if (IP)
11702 CSEMap.InsertNode(N, IP); // Memoize the new node.
11703 return N;
11704}
11705
11707 unsigned OrigOpc = Node->getOpcode();
11708 unsigned NewOpc;
11709 switch (OrigOpc) {
11710 default:
11711 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11712#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11713 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11714#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11715 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11716#include "llvm/IR/ConstrainedOps.def"
11717 }
11718
11719 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11720
11721 // We're taking this node out of the chain, so we need to re-link things.
11722 SDValue InputChain = Node->getOperand(0);
11723 SDValue OutputChain = SDValue(Node, 1);
11724 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11725
11727 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11728 Ops.push_back(Node->getOperand(i));
11729
11730 SDVTList VTs = getVTList(Node->getValueType(0));
11731 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11732
11733 // MorphNodeTo can operate in two ways: if an existing node with the
11734 // specified operands exists, it can just return it. Otherwise, it
11735 // updates the node in place to have the requested operands.
11736 if (Res == Node) {
11737 // If we updated the node in place, reset the node ID. To the isel,
11738 // this should be just like a newly allocated machine node.
11739 Res->setNodeId(-1);
11740 } else {
11743 }
11744
11745 return Res;
11746}
11747
11748/// getMachineNode - These are used for target selectors to create a new node
11749/// with specified return type(s), MachineInstr opcode, and operands.
11750///
11751/// Note that getMachineNode returns the resultant node. If there is already a
11752/// node of the specified opcode and operands, it returns that node instead of
11753/// the current one.
11755 EVT VT) {
11756 SDVTList VTs = getVTList(VT);
11757 return getMachineNode(Opcode, dl, VTs, {});
11758}
11759
11761 EVT VT, SDValue Op1) {
11762 SDVTList VTs = getVTList(VT);
11763 SDValue Ops[] = { Op1 };
11764 return getMachineNode(Opcode, dl, VTs, Ops);
11765}
11766
11768 EVT VT, SDValue Op1, SDValue Op2) {
11769 SDVTList VTs = getVTList(VT);
11770 SDValue Ops[] = { Op1, Op2 };
11771 return getMachineNode(Opcode, dl, VTs, Ops);
11772}
11773
11775 EVT VT, SDValue Op1, SDValue Op2,
11776 SDValue Op3) {
11777 SDVTList VTs = getVTList(VT);
11778 SDValue Ops[] = { Op1, Op2, Op3 };
11779 return getMachineNode(Opcode, dl, VTs, Ops);
11780}
11781
11784 SDVTList VTs = getVTList(VT);
11785 return getMachineNode(Opcode, dl, VTs, Ops);
11786}
11787
11789 EVT VT1, EVT VT2, SDValue Op1,
11790 SDValue Op2) {
11791 SDVTList VTs = getVTList(VT1, VT2);
11792 SDValue Ops[] = { Op1, Op2 };
11793 return getMachineNode(Opcode, dl, VTs, Ops);
11794}
11795
11797 EVT VT1, EVT VT2, SDValue Op1,
11798 SDValue Op2, SDValue Op3) {
11799 SDVTList VTs = getVTList(VT1, VT2);
11800 SDValue Ops[] = { Op1, Op2, Op3 };
11801 return getMachineNode(Opcode, dl, VTs, Ops);
11802}
11803
11805 EVT VT1, EVT VT2,
11807 SDVTList VTs = getVTList(VT1, VT2);
11808 return getMachineNode(Opcode, dl, VTs, Ops);
11809}
11810
11812 EVT VT1, EVT VT2, EVT VT3,
11813 SDValue Op1, SDValue Op2) {
11814 SDVTList VTs = getVTList(VT1, VT2, VT3);
11815 SDValue Ops[] = { Op1, Op2 };
11816 return getMachineNode(Opcode, dl, VTs, Ops);
11817}
11818
11820 EVT VT1, EVT VT2, EVT VT3,
11821 SDValue Op1, SDValue Op2,
11822 SDValue Op3) {
11823 SDVTList VTs = getVTList(VT1, VT2, VT3);
11824 SDValue Ops[] = { Op1, Op2, Op3 };
11825 return getMachineNode(Opcode, dl, VTs, Ops);
11826}
11827
11829 EVT VT1, EVT VT2, EVT VT3,
11831 SDVTList VTs = getVTList(VT1, VT2, VT3);
11832 return getMachineNode(Opcode, dl, VTs, Ops);
11833}
11834
11836 ArrayRef<EVT> ResultTys,
11838 SDVTList VTs = getVTList(ResultTys);
11839 return getMachineNode(Opcode, dl, VTs, Ops);
11840}
11841
11843 SDVTList VTs,
11845 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11847 void *IP = nullptr;
11848
11849 if (DoCSE) {
11851 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11852 IP = nullptr;
11853 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11854 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11855 }
11856 }
11857
11858 // Allocate a new MachineSDNode.
11859 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11860 createOperands(N, Ops);
11861
11862 if (DoCSE)
11863 CSEMap.InsertNode(N, IP);
11864
11865 InsertNode(N);
11866 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11867 return N;
11868}
11869
11870/// getTargetExtractSubreg - A convenience function for creating
11871/// TargetOpcode::EXTRACT_SUBREG nodes.
11873 SDValue Operand) {
11874 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11875 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11876 VT, Operand, SRIdxVal);
11877 return SDValue(Subreg, 0);
11878}
11879
11880/// getTargetInsertSubreg - A convenience function for creating
11881/// TargetOpcode::INSERT_SUBREG nodes.
11883 SDValue Operand, SDValue Subreg) {
11884 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11885 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11886 VT, Operand, Subreg, SRIdxVal);
11887 return SDValue(Result, 0);
11888}
11889
11890/// getNodeIfExists - Get the specified node if it's already available, or
11891/// else return NULL.
11894 bool AllowCommute) {
11895 SDNodeFlags Flags;
11896 if (Inserter)
11897 Flags = Inserter->getFlags();
11898 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11899}
11900
11903 const SDNodeFlags Flags,
11904 bool AllowCommute) {
11905 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11906 return nullptr;
11907
11908 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11910 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11911 void *IP = nullptr;
11912 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11913 E->intersectFlagsWith(Flags);
11914 return E;
11915 }
11916 return nullptr;
11917 };
11918
11919 if (SDNode *Existing = Lookup(Ops))
11920 return Existing;
11921
11922 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11923 return Lookup({Ops[1], Ops[0]});
11924
11925 return nullptr;
11926}
11927
11928/// doesNodeExist - Check if a node exists without modifying its flags.
11929bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11931 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11933 AddNodeIDNode(ID, Opcode, VTList, Ops);
11934 void *IP = nullptr;
11935 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11936 return true;
11937 }
11938 return false;
11939}
11940
11941/// getDbgValue - Creates a SDDbgValue node.
11942///
11943/// SDNode
11945 SDNode *N, unsigned R, bool IsIndirect,
11946 const DebugLoc &DL, unsigned O) {
11947 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11948 "Expected inlined-at fields to agree");
11949 return new (DbgInfo->getAlloc())
11950 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11951 {}, IsIndirect, DL, O,
11952 /*IsVariadic=*/false);
11953}
11954
11955/// Constant
11957 DIExpression *Expr,
11958 const Value *C,
11959 const DebugLoc &DL, unsigned O) {
11960 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11961 "Expected inlined-at fields to agree");
11962 return new (DbgInfo->getAlloc())
11963 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11964 /*IsIndirect=*/false, DL, O,
11965 /*IsVariadic=*/false);
11966}
11967
11968/// FrameIndex
11970 DIExpression *Expr, unsigned FI,
11971 bool IsIndirect,
11972 const DebugLoc &DL,
11973 unsigned O) {
11974 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11975 "Expected inlined-at fields to agree");
11976 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11977}
11978
11979/// FrameIndex with dependencies
11981 DIExpression *Expr, unsigned FI,
11982 ArrayRef<SDNode *> Dependencies,
11983 bool IsIndirect,
11984 const DebugLoc &DL,
11985 unsigned O) {
11986 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11987 "Expected inlined-at fields to agree");
11988 return new (DbgInfo->getAlloc())
11989 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11990 Dependencies, IsIndirect, DL, O,
11991 /*IsVariadic=*/false);
11992}
11993
11994/// VReg
11996 Register VReg, bool IsIndirect,
11997 const DebugLoc &DL, unsigned O) {
11998 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11999 "Expected inlined-at fields to agree");
12000 return new (DbgInfo->getAlloc())
12001 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12002 {}, IsIndirect, DL, O,
12003 /*IsVariadic=*/false);
12004}
12005
12008 ArrayRef<SDNode *> Dependencies,
12009 bool IsIndirect, const DebugLoc &DL,
12010 unsigned O, bool IsVariadic) {
12011 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12012 "Expected inlined-at fields to agree");
12013 return new (DbgInfo->getAlloc())
12014 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12015 DL, O, IsVariadic);
12016}
12017
12019 unsigned OffsetInBits, unsigned SizeInBits,
12020 bool InvalidateDbg) {
12021 SDNode *FromNode = From.getNode();
12022 SDNode *ToNode = To.getNode();
12023 assert(FromNode && ToNode && "Can't modify dbg values");
12024
12025 // PR35338
12026 // TODO: assert(From != To && "Redundant dbg value transfer");
12027 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12028 if (From == To || FromNode == ToNode)
12029 return;
12030
12031 if (!FromNode->getHasDebugValue())
12032 return;
12033
12034 SDDbgOperand FromLocOp =
12035 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12037
12039 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12040 if (Dbg->isInvalidated())
12041 continue;
12042
12043 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12044
12045 // Create a new location ops vector that is equal to the old vector, but
12046 // with each instance of FromLocOp replaced with ToLocOp.
12047 bool Changed = false;
12048 auto NewLocOps = Dbg->copyLocationOps();
12049 std::replace_if(
12050 NewLocOps.begin(), NewLocOps.end(),
12051 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12052 bool Match = Op == FromLocOp;
12053 Changed |= Match;
12054 return Match;
12055 },
12056 ToLocOp);
12057 // Ignore this SDDbgValue if we didn't find a matching location.
12058 if (!Changed)
12059 continue;
12060
12061 DIVariable *Var = Dbg->getVariable();
12062 auto *Expr = Dbg->getExpression();
12063 // If a fragment is requested, update the expression.
12064 if (SizeInBits) {
12065 // When splitting a larger (e.g., sign-extended) value whose
12066 // lower bits are described with an SDDbgValue, do not attempt
12067 // to transfer the SDDbgValue to the upper bits.
12068 if (auto FI = Expr->getFragmentInfo())
12069 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12070 continue;
12071 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12072 SizeInBits);
12073 if (!Fragment)
12074 continue;
12075 Expr = *Fragment;
12076 }
12077
12078 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12079 // Clone the SDDbgValue and move it to To.
12080 SDDbgValue *Clone = getDbgValueList(
12081 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12082 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12083 Dbg->isVariadic());
12084 ClonedDVs.push_back(Clone);
12085
12086 if (InvalidateDbg) {
12087 // Invalidate value and indicate the SDDbgValue should not be emitted.
12088 Dbg->setIsInvalidated();
12089 Dbg->setIsEmitted();
12090 }
12091 }
12092
12093 for (SDDbgValue *Dbg : ClonedDVs) {
12094 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12095 "Transferred DbgValues should depend on the new SDNode");
12096 AddDbgValue(Dbg, false);
12097 }
12098}
12099
12101 if (!N.getHasDebugValue())
12102 return;
12103
12104 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12105 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12106 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12107 return SDDbgOperand::fromNode(Node, ResNo);
12108 };
12109
12111 for (auto *DV : GetDbgValues(&N)) {
12112 if (DV->isInvalidated())
12113 continue;
12114 switch (N.getOpcode()) {
12115 default:
12116 break;
12117 case ISD::ADD: {
12118 SDValue N0 = N.getOperand(0);
12119 SDValue N1 = N.getOperand(1);
12120 if (!isa<ConstantSDNode>(N0)) {
12121 bool RHSConstant = isa<ConstantSDNode>(N1);
12123 if (RHSConstant)
12124 Offset = N.getConstantOperandVal(1);
12125 // We are not allowed to turn indirect debug values variadic, so
12126 // don't salvage those.
12127 if (!RHSConstant && DV->isIndirect())
12128 continue;
12129
12130 // Rewrite an ADD constant node into a DIExpression. Since we are
12131 // performing arithmetic to compute the variable's *value* in the
12132 // DIExpression, we need to mark the expression with a
12133 // DW_OP_stack_value.
12134 auto *DIExpr = DV->getExpression();
12135 auto NewLocOps = DV->copyLocationOps();
12136 bool Changed = false;
12137 size_t OrigLocOpsSize = NewLocOps.size();
12138 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12139 // We're not given a ResNo to compare against because the whole
12140 // node is going away. We know that any ISD::ADD only has one
12141 // result, so we can assume any node match is using the result.
12142 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12143 NewLocOps[i].getSDNode() != &N)
12144 continue;
12145 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12146 if (RHSConstant) {
12149 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12150 } else {
12151 // Convert to a variadic expression (if not already).
12152 // convertToVariadicExpression() returns a const pointer, so we use
12153 // a temporary const variable here.
12154 const auto *TmpDIExpr =
12158 ExprOps.push_back(NewLocOps.size());
12159 ExprOps.push_back(dwarf::DW_OP_plus);
12160 SDDbgOperand RHS =
12162 NewLocOps.push_back(RHS);
12163 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12164 }
12165 Changed = true;
12166 }
12167 (void)Changed;
12168 assert(Changed && "Salvage target doesn't use N");
12169
12170 bool IsVariadic =
12171 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12172
12173 auto AdditionalDependencies = DV->getAdditionalDependencies();
12174 SDDbgValue *Clone = getDbgValueList(
12175 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12176 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12177 ClonedDVs.push_back(Clone);
12178 DV->setIsInvalidated();
12179 DV->setIsEmitted();
12180 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12181 N0.getNode()->dumprFull(this);
12182 dbgs() << " into " << *DIExpr << '\n');
12183 }
12184 break;
12185 }
12186 case ISD::TRUNCATE: {
12187 SDValue N0 = N.getOperand(0);
12188 TypeSize FromSize = N0.getValueSizeInBits();
12189 TypeSize ToSize = N.getValueSizeInBits(0);
12190
12191 DIExpression *DbgExpression = DV->getExpression();
12192 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12193 auto NewLocOps = DV->copyLocationOps();
12194 bool Changed = false;
12195 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12196 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12197 NewLocOps[i].getSDNode() != &N)
12198 continue;
12199
12200 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12201 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12202 Changed = true;
12203 }
12204 assert(Changed && "Salvage target doesn't use N");
12205 (void)Changed;
12206
12207 SDDbgValue *Clone =
12208 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12209 DV->getAdditionalDependencies(), DV->isIndirect(),
12210 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12211
12212 ClonedDVs.push_back(Clone);
12213 DV->setIsInvalidated();
12214 DV->setIsEmitted();
12215 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12216 dbgs() << " into " << *DbgExpression << '\n');
12217 break;
12218 }
12219 }
12220 }
12221
12222 for (SDDbgValue *Dbg : ClonedDVs) {
12223 assert((!Dbg->getSDNodes().empty() ||
12224 llvm::any_of(Dbg->getLocationOps(),
12225 [&](const SDDbgOperand &Op) {
12226 return Op.getKind() == SDDbgOperand::FRAMEIX;
12227 })) &&
12228 "Salvaged DbgValue should depend on a new SDNode");
12229 AddDbgValue(Dbg, false);
12230 }
12231}
12232
12233/// Creates a SDDbgLabel node.
12235 const DebugLoc &DL, unsigned O) {
12236 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12237 "Expected inlined-at fields to agree");
12238 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12239}
12240
12241namespace {
12242
12243/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12244/// pointed to by a use iterator is deleted, increment the use iterator
12245/// so that it doesn't dangle.
12246///
12247class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12250
12251 void NodeDeleted(SDNode *N, SDNode *E) override {
12252 // Increment the iterator as needed.
12253 while (UI != UE && N == UI->getUser())
12254 ++UI;
12255 }
12256
12257public:
12258 RAUWUpdateListener(SelectionDAG &d,
12261 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12262};
12263
12264} // end anonymous namespace
12265
12266/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12267/// This can cause recursive merging of nodes in the DAG.
12268///
12269/// This version assumes From has a single result value.
12270///
12272 SDNode *From = FromN.getNode();
12273 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12274 "Cannot replace with this method!");
12275 assert(From != To.getNode() && "Cannot replace uses of with self");
12276
12277 // Preserve Debug Values
12278 transferDbgValues(FromN, To);
12279 // Preserve extra info.
12280 copyExtraInfo(From, To.getNode());
12281
12282 // Iterate over all the existing uses of From. New uses will be added
12283 // to the beginning of the use list, which we avoid visiting.
12284 // This specifically avoids visiting uses of From that arise while the
12285 // replacement is happening, because any such uses would be the result
12286 // of CSE: If an existing node looks like From after one of its operands
12287 // is replaced by To, we don't want to replace of all its users with To
12288 // too. See PR3018 for more info.
12289 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12290 RAUWUpdateListener Listener(*this, UI, UE);
12291 while (UI != UE) {
12292 SDNode *User = UI->getUser();
12293
12294 // This node is about to morph, remove its old self from the CSE maps.
12295 RemoveNodeFromCSEMaps(User);
12296
12297 // A user can appear in a use list multiple times, and when this
12298 // happens the uses are usually next to each other in the list.
12299 // To help reduce the number of CSE recomputations, process all
12300 // the uses of this user that we can find this way.
12301 do {
12302 SDUse &Use = *UI;
12303 ++UI;
12304 Use.set(To);
12305 if (To->isDivergent() != From->isDivergent())
12307 } while (UI != UE && UI->getUser() == User);
12308 // Now that we have modified User, add it back to the CSE maps. If it
12309 // already exists there, recursively merge the results together.
12310 AddModifiedNodeToCSEMaps(User);
12311 }
12312
12313 // If we just RAUW'd the root, take note.
12314 if (FromN == getRoot())
12315 setRoot(To);
12316}
12317
12318/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12319/// This can cause recursive merging of nodes in the DAG.
12320///
12321/// This version assumes that for each value of From, there is a
12322/// corresponding value in To in the same position with the same type.
12323///
12325#ifndef NDEBUG
12326 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12327 assert((!From->hasAnyUseOfValue(i) ||
12328 From->getValueType(i) == To->getValueType(i)) &&
12329 "Cannot use this version of ReplaceAllUsesWith!");
12330#endif
12331
12332 // Handle the trivial case.
12333 if (From == To)
12334 return;
12335
12336 // Preserve Debug Info. Only do this if there's a use.
12337 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12338 if (From->hasAnyUseOfValue(i)) {
12339 assert((i < To->getNumValues()) && "Invalid To location");
12340 transferDbgValues(SDValue(From, i), SDValue(To, i));
12341 }
12342 // Preserve extra info.
12343 copyExtraInfo(From, To);
12344
12345 // Iterate over just the existing users of From. See the comments in
12346 // the ReplaceAllUsesWith above.
12347 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12348 RAUWUpdateListener Listener(*this, UI, UE);
12349 while (UI != UE) {
12350 SDNode *User = UI->getUser();
12351
12352 // This node is about to morph, remove its old self from the CSE maps.
12353 RemoveNodeFromCSEMaps(User);
12354
12355 // A user can appear in a use list multiple times, and when this
12356 // happens the uses are usually next to each other in the list.
12357 // To help reduce the number of CSE recomputations, process all
12358 // the uses of this user that we can find this way.
12359 do {
12360 SDUse &Use = *UI;
12361 ++UI;
12362 Use.setNode(To);
12363 if (To->isDivergent() != From->isDivergent())
12365 } while (UI != UE && UI->getUser() == User);
12366
12367 // Now that we have modified User, add it back to the CSE maps. If it
12368 // already exists there, recursively merge the results together.
12369 AddModifiedNodeToCSEMaps(User);
12370 }
12371
12372 // If we just RAUW'd the root, take note.
12373 if (From == getRoot().getNode())
12374 setRoot(SDValue(To, getRoot().getResNo()));
12375}
12376
12377/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12378/// This can cause recursive merging of nodes in the DAG.
12379///
12380/// This version can replace From with any result values. To must match the
12381/// number and types of values returned by From.
12383 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12384 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12385
12386 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12387 // Preserve Debug Info.
12388 transferDbgValues(SDValue(From, i), To[i]);
12389 // Preserve extra info.
12390 copyExtraInfo(From, To[i].getNode());
12391 }
12392
12393 // Iterate over just the existing users of From. See the comments in
12394 // the ReplaceAllUsesWith above.
12395 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12396 RAUWUpdateListener Listener(*this, UI, UE);
12397 while (UI != UE) {
12398 SDNode *User = UI->getUser();
12399
12400 // This node is about to morph, remove its old self from the CSE maps.
12401 RemoveNodeFromCSEMaps(User);
12402
12403 // A user can appear in a use list multiple times, and when this happens the
12404 // uses are usually next to each other in the list. To help reduce the
12405 // number of CSE and divergence recomputations, process all the uses of this
12406 // user that we can find this way.
12407 bool To_IsDivergent = false;
12408 do {
12409 SDUse &Use = *UI;
12410 const SDValue &ToOp = To[Use.getResNo()];
12411 ++UI;
12412 Use.set(ToOp);
12413 if (ToOp.getValueType() != MVT::Other)
12414 To_IsDivergent |= ToOp->isDivergent();
12415 } while (UI != UE && UI->getUser() == User);
12416
12417 if (To_IsDivergent != From->isDivergent())
12419
12420 // Now that we have modified User, add it back to the CSE maps. If it
12421 // already exists there, recursively merge the results together.
12422 AddModifiedNodeToCSEMaps(User);
12423 }
12424
12425 // If we just RAUW'd the root, take note.
12426 if (From == getRoot().getNode())
12427 setRoot(SDValue(To[getRoot().getResNo()]));
12428}
12429
12430/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12431/// uses of other values produced by From.getNode() alone. The Deleted
12432/// vector is handled the same way as for ReplaceAllUsesWith.
12434 // Handle the really simple, really trivial case efficiently.
12435 if (From == To) return;
12436
12437 // Handle the simple, trivial, case efficiently.
12438 if (From.getNode()->getNumValues() == 1) {
12439 ReplaceAllUsesWith(From, To);
12440 return;
12441 }
12442
12443 // Preserve Debug Info.
12444 transferDbgValues(From, To);
12445 copyExtraInfo(From.getNode(), To.getNode());
12446
12447 // Iterate over just the existing users of From. See the comments in
12448 // the ReplaceAllUsesWith above.
12449 SDNode::use_iterator UI = From.getNode()->use_begin(),
12450 UE = From.getNode()->use_end();
12451 RAUWUpdateListener Listener(*this, UI, UE);
12452 while (UI != UE) {
12453 SDNode *User = UI->getUser();
12454 bool UserRemovedFromCSEMaps = false;
12455
12456 // A user can appear in a use list multiple times, and when this
12457 // happens the uses are usually next to each other in the list.
12458 // To help reduce the number of CSE recomputations, process all
12459 // the uses of this user that we can find this way.
12460 do {
12461 SDUse &Use = *UI;
12462
12463 // Skip uses of different values from the same node.
12464 if (Use.getResNo() != From.getResNo()) {
12465 ++UI;
12466 continue;
12467 }
12468
12469 // If this node hasn't been modified yet, it's still in the CSE maps,
12470 // so remove its old self from the CSE maps.
12471 if (!UserRemovedFromCSEMaps) {
12472 RemoveNodeFromCSEMaps(User);
12473 UserRemovedFromCSEMaps = true;
12474 }
12475
12476 ++UI;
12477 Use.set(To);
12478 if (To->isDivergent() != From->isDivergent())
12480 } while (UI != UE && UI->getUser() == User);
12481 // We are iterating over all uses of the From node, so if a use
12482 // doesn't use the specific value, no changes are made.
12483 if (!UserRemovedFromCSEMaps)
12484 continue;
12485
12486 // Now that we have modified User, add it back to the CSE maps. If it
12487 // already exists there, recursively merge the results together.
12488 AddModifiedNodeToCSEMaps(User);
12489 }
12490
12491 // If we just RAUW'd the root, take note.
12492 if (From == getRoot())
12493 setRoot(To);
12494}
12495
12496namespace {
12497
12498/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12499/// to record information about a use.
12500struct UseMemo {
12501 SDNode *User;
12502 unsigned Index;
12503 SDUse *Use;
12504};
12505
12506/// operator< - Sort Memos by User.
12507bool operator<(const UseMemo &L, const UseMemo &R) {
12508 return (intptr_t)L.User < (intptr_t)R.User;
12509}
12510
12511/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12512/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12513/// the node already has been taken care of recursively.
12514class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12515 SmallVectorImpl<UseMemo> &Uses;
12516
12517 void NodeDeleted(SDNode *N, SDNode *E) override {
12518 for (UseMemo &Memo : Uses)
12519 if (Memo.User == N)
12520 Memo.User = nullptr;
12521 }
12522
12523public:
12524 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12525 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12526};
12527
12528} // end anonymous namespace
12529
12530/// Return true if a glue output should propagate divergence information.
12532 switch (Node->getOpcode()) {
12533 case ISD::CopyFromReg:
12534 case ISD::CopyToReg:
12535 return false;
12536 default:
12537 return true;
12538 }
12539
12540 llvm_unreachable("covered opcode switch");
12541}
12542
12544 if (TLI->isSDNodeAlwaysUniform(N)) {
12545 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12546 "Conflicting divergence information!");
12547 return false;
12548 }
12549 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12550 return true;
12551 for (const auto &Op : N->ops()) {
12552 EVT VT = Op.getValueType();
12553
12554 // Skip Chain. It does not carry divergence.
12555 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12556 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12557 return true;
12558 }
12559 return false;
12560}
12561
12563 SmallVector<SDNode *, 16> Worklist(1, N);
12564 do {
12565 N = Worklist.pop_back_val();
12566 bool IsDivergent = calculateDivergence(N);
12567 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12568 N->SDNodeBits.IsDivergent = IsDivergent;
12569 llvm::append_range(Worklist, N->users());
12570 }
12571 } while (!Worklist.empty());
12572}
12573
12574void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12576 Order.reserve(AllNodes.size());
12577 for (auto &N : allnodes()) {
12578 unsigned NOps = N.getNumOperands();
12579 Degree[&N] = NOps;
12580 if (0 == NOps)
12581 Order.push_back(&N);
12582 }
12583 for (size_t I = 0; I != Order.size(); ++I) {
12584 SDNode *N = Order[I];
12585 for (auto *U : N->users()) {
12586 unsigned &UnsortedOps = Degree[U];
12587 if (0 == --UnsortedOps)
12588 Order.push_back(U);
12589 }
12590 }
12591}
12592
12593#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12594void SelectionDAG::VerifyDAGDivergence() {
12595 std::vector<SDNode *> TopoOrder;
12596 CreateTopologicalOrder(TopoOrder);
12597 for (auto *N : TopoOrder) {
12598 assert(calculateDivergence(N) == N->isDivergent() &&
12599 "Divergence bit inconsistency detected");
12600 }
12601}
12602#endif
12603
12604/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12605/// uses of other values produced by From.getNode() alone. The same value
12606/// may appear in both the From and To list. The Deleted vector is
12607/// handled the same way as for ReplaceAllUsesWith.
12609 const SDValue *To,
12610 unsigned Num){
12611 // Handle the simple, trivial case efficiently.
12612 if (Num == 1)
12613 return ReplaceAllUsesOfValueWith(*From, *To);
12614
12615 transferDbgValues(*From, *To);
12616 copyExtraInfo(From->getNode(), To->getNode());
12617
12618 // Read up all the uses and make records of them. This helps
12619 // processing new uses that are introduced during the
12620 // replacement process.
12622 for (unsigned i = 0; i != Num; ++i) {
12623 unsigned FromResNo = From[i].getResNo();
12624 SDNode *FromNode = From[i].getNode();
12625 for (SDUse &Use : FromNode->uses()) {
12626 if (Use.getResNo() == FromResNo) {
12627 UseMemo Memo = {Use.getUser(), i, &Use};
12628 Uses.push_back(Memo);
12629 }
12630 }
12631 }
12632
12633 // Sort the uses, so that all the uses from a given User are together.
12635 RAUOVWUpdateListener Listener(*this, Uses);
12636
12637 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12638 UseIndex != UseIndexEnd; ) {
12639 // We know that this user uses some value of From. If it is the right
12640 // value, update it.
12641 SDNode *User = Uses[UseIndex].User;
12642 // If the node has been deleted by recursive CSE updates when updating
12643 // another node, then just skip this entry.
12644 if (User == nullptr) {
12645 ++UseIndex;
12646 continue;
12647 }
12648
12649 // This node is about to morph, remove its old self from the CSE maps.
12650 RemoveNodeFromCSEMaps(User);
12651
12652 // The Uses array is sorted, so all the uses for a given User
12653 // are next to each other in the list.
12654 // To help reduce the number of CSE recomputations, process all
12655 // the uses of this user that we can find this way.
12656 do {
12657 unsigned i = Uses[UseIndex].Index;
12658 SDUse &Use = *Uses[UseIndex].Use;
12659 ++UseIndex;
12660
12661 Use.set(To[i]);
12662 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12663
12664 // Now that we have modified User, add it back to the CSE maps. If it
12665 // already exists there, recursively merge the results together.
12666 AddModifiedNodeToCSEMaps(User);
12667 }
12668}
12669
12670/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12671/// based on their topological order. It returns the maximum id and a vector
12672/// of the SDNodes* in assigned order by reference.
12674 unsigned DAGSize = 0;
12675
12676 // SortedPos tracks the progress of the algorithm. Nodes before it are
12677 // sorted, nodes after it are unsorted. When the algorithm completes
12678 // it is at the end of the list.
12679 allnodes_iterator SortedPos = allnodes_begin();
12680
12681 // Visit all the nodes. Move nodes with no operands to the front of
12682 // the list immediately. Annotate nodes that do have operands with their
12683 // operand count. Before we do this, the Node Id fields of the nodes
12684 // may contain arbitrary values. After, the Node Id fields for nodes
12685 // before SortedPos will contain the topological sort index, and the
12686 // Node Id fields for nodes At SortedPos and after will contain the
12687 // count of outstanding operands.
12689 checkForCycles(&N, this);
12690 unsigned Degree = N.getNumOperands();
12691 if (Degree == 0) {
12692 // A node with no uses, add it to the result array immediately.
12693 N.setNodeId(DAGSize++);
12694 allnodes_iterator Q(&N);
12695 if (Q != SortedPos)
12696 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12697 assert(SortedPos != AllNodes.end() && "Overran node list");
12698 ++SortedPos;
12699 } else {
12700 // Temporarily use the Node Id as scratch space for the degree count.
12701 N.setNodeId(Degree);
12702 }
12703 }
12704
12705 // Visit all the nodes. As we iterate, move nodes into sorted order,
12706 // such that by the time the end is reached all nodes will be sorted.
12707 for (SDNode &Node : allnodes()) {
12708 SDNode *N = &Node;
12709 checkForCycles(N, this);
12710 // N is in sorted position, so all its uses have one less operand
12711 // that needs to be sorted.
12712 for (SDNode *P : N->users()) {
12713 unsigned Degree = P->getNodeId();
12714 assert(Degree != 0 && "Invalid node degree");
12715 --Degree;
12716 if (Degree == 0) {
12717 // All of P's operands are sorted, so P may sorted now.
12718 P->setNodeId(DAGSize++);
12719 if (P->getIterator() != SortedPos)
12720 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12721 assert(SortedPos != AllNodes.end() && "Overran node list");
12722 ++SortedPos;
12723 } else {
12724 // Update P's outstanding operand count.
12725 P->setNodeId(Degree);
12726 }
12727 }
12728 if (Node.getIterator() == SortedPos) {
12729#ifndef NDEBUG
12731 SDNode *S = &*++I;
12732 dbgs() << "Overran sorted position:\n";
12733 S->dumprFull(this); dbgs() << "\n";
12734 dbgs() << "Checking if this is due to cycles\n";
12735 checkForCycles(this, true);
12736#endif
12737 llvm_unreachable(nullptr);
12738 }
12739 }
12740
12741 assert(SortedPos == AllNodes.end() &&
12742 "Topological sort incomplete!");
12743 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12744 "First node in topological sort is not the entry token!");
12745 assert(AllNodes.front().getNodeId() == 0 &&
12746 "First node in topological sort has non-zero id!");
12747 assert(AllNodes.front().getNumOperands() == 0 &&
12748 "First node in topological sort has operands!");
12749 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12750 "Last node in topologic sort has unexpected id!");
12751 assert(AllNodes.back().use_empty() &&
12752 "Last node in topologic sort has users!");
12753 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12754 return DAGSize;
12755}
12756
12758 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12759 SortedNodes.clear();
12760 // Node -> remaining number of outstanding operands.
12761 DenseMap<const SDNode *, unsigned> RemainingOperands;
12762
12763 // Put nodes without any operands into SortedNodes first.
12764 for (const SDNode &N : allnodes()) {
12765 checkForCycles(&N, this);
12766 unsigned NumOperands = N.getNumOperands();
12767 if (NumOperands == 0)
12768 SortedNodes.push_back(&N);
12769 else
12770 // Record their total number of outstanding operands.
12771 RemainingOperands[&N] = NumOperands;
12772 }
12773
12774 // A node is pushed into SortedNodes when all of its operands (predecessors in
12775 // the graph) are also in SortedNodes.
12776 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12777 const SDNode *N = SortedNodes[i];
12778 for (const SDNode *U : N->users()) {
12779 // HandleSDNode is never part of a DAG and therefore has no entry in
12780 // RemainingOperands.
12781 if (U->getOpcode() == ISD::HANDLENODE)
12782 continue;
12783 unsigned &NumRemOperands = RemainingOperands[U];
12784 assert(NumRemOperands && "Invalid number of remaining operands");
12785 --NumRemOperands;
12786 if (!NumRemOperands)
12787 SortedNodes.push_back(U);
12788 }
12789 }
12790
12791 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12792 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12793 "First node in topological sort is not the entry token");
12794 assert(SortedNodes.front()->getNumOperands() == 0 &&
12795 "First node in topological sort has operands");
12796}
12797
12798/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12799/// value is produced by SD.
12800void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12801 for (SDNode *SD : DB->getSDNodes()) {
12802 if (!SD)
12803 continue;
12804 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12805 SD->setHasDebugValue(true);
12806 }
12807 DbgInfo->add(DB, isParameter);
12808}
12809
12810void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12811
12813 SDValue NewMemOpChain) {
12814 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12815 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12816 // The new memory operation must have the same position as the old load in
12817 // terms of memory dependency. Create a TokenFactor for the old load and new
12818 // memory operation and update uses of the old load's output chain to use that
12819 // TokenFactor.
12820 if (OldChain == NewMemOpChain || OldChain.use_empty())
12821 return NewMemOpChain;
12822
12823 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12824 OldChain, NewMemOpChain);
12825 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12826 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12827 return TokenFactor;
12828}
12829
12831 SDValue NewMemOp) {
12832 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12833 SDValue OldChain = SDValue(OldLoad, 1);
12834 SDValue NewMemOpChain = NewMemOp.getValue(1);
12835 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12836}
12837
12839 Function **OutFunction) {
12840 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12841
12842 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12843 auto *Module = MF->getFunction().getParent();
12844 auto *Function = Module->getFunction(Symbol);
12845
12846 if (OutFunction != nullptr)
12847 *OutFunction = Function;
12848
12849 if (Function != nullptr) {
12850 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12851 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12852 }
12853
12854 std::string ErrorStr;
12855 raw_string_ostream ErrorFormatter(ErrorStr);
12856 ErrorFormatter << "Undefined external symbol ";
12857 ErrorFormatter << '"' << Symbol << '"';
12858 report_fatal_error(Twine(ErrorStr));
12859}
12860
12861//===----------------------------------------------------------------------===//
12862// SDNode Class
12863//===----------------------------------------------------------------------===//
12864
12867 return Const != nullptr && Const->isZero();
12868}
12869
12871 return V.isUndef() || isNullConstant(V);
12872}
12873
12876 return Const != nullptr && Const->isZero() && !Const->isNegative();
12877}
12878
12881 return Const != nullptr && Const->isAllOnes();
12882}
12883
12886 return Const != nullptr && Const->isOne();
12887}
12888
12891 return Const != nullptr && Const->isMinSignedValue();
12892}
12893
12894bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12895 unsigned OperandNo) {
12896 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12897 // TODO: Target-specific opcodes could be added.
12898 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12899 /*AllowTruncation*/ true)) {
12900 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12901 switch (Opcode) {
12902 case ISD::ADD:
12903 case ISD::OR:
12904 case ISD::XOR:
12905 case ISD::UMAX:
12906 return Const.isZero();
12907 case ISD::MUL:
12908 return Const.isOne();
12909 case ISD::AND:
12910 case ISD::UMIN:
12911 return Const.isAllOnes();
12912 case ISD::SMAX:
12913 return Const.isMinSignedValue();
12914 case ISD::SMIN:
12915 return Const.isMaxSignedValue();
12916 case ISD::SUB:
12917 case ISD::SHL:
12918 case ISD::SRA:
12919 case ISD::SRL:
12920 return OperandNo == 1 && Const.isZero();
12921 case ISD::UDIV:
12922 case ISD::SDIV:
12923 return OperandNo == 1 && Const.isOne();
12924 }
12925 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12926 switch (Opcode) {
12927 case ISD::FADD:
12928 return ConstFP->isZero() &&
12929 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12930 case ISD::FSUB:
12931 return OperandNo == 1 && ConstFP->isZero() &&
12932 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12933 case ISD::FMUL:
12934 return ConstFP->isExactlyValue(1.0);
12935 case ISD::FDIV:
12936 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12937 case ISD::FMINNUM:
12938 case ISD::FMAXNUM: {
12939 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12940 EVT VT = V.getValueType();
12941 const fltSemantics &Semantics = VT.getFltSemantics();
12942 APFloat NeutralAF = !Flags.hasNoNaNs()
12943 ? APFloat::getQNaN(Semantics)
12944 : !Flags.hasNoInfs()
12945 ? APFloat::getInf(Semantics)
12946 : APFloat::getLargest(Semantics);
12947 if (Opcode == ISD::FMAXNUM)
12948 NeutralAF.changeSign();
12949
12950 return ConstFP->isExactlyValue(NeutralAF);
12951 }
12952 }
12953 }
12954 return false;
12955}
12956
12958 while (V.getOpcode() == ISD::BITCAST)
12959 V = V.getOperand(0);
12960 return V;
12961}
12962
12964 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12965 V = V.getOperand(0);
12966 return V;
12967}
12968
12970 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12971 V = V.getOperand(0);
12972 return V;
12973}
12974
12976 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12977 SDValue InVec = V.getOperand(0);
12978 SDValue EltNo = V.getOperand(2);
12979 EVT VT = InVec.getValueType();
12980 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12981 if (IndexC && VT.isFixedLengthVector() &&
12982 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12983 !DemandedElts[IndexC->getZExtValue()]) {
12984 V = InVec;
12985 continue;
12986 }
12987 break;
12988 }
12989 return V;
12990}
12991
12993 while (V.getOpcode() == ISD::TRUNCATE)
12994 V = V.getOperand(0);
12995 return V;
12996}
12997
12998bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12999 if (V.getOpcode() != ISD::XOR)
13000 return false;
13001 V = peekThroughBitcasts(V.getOperand(1));
13002 unsigned NumBits = V.getScalarValueSizeInBits();
13003 ConstantSDNode *C =
13004 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13005 return C && (C->getAPIntValue().countr_one() >= NumBits);
13006}
13007
13009 bool AllowTruncation) {
13010 EVT VT = N.getValueType();
13011 APInt DemandedElts = VT.isFixedLengthVector()
13013 : APInt(1, 1);
13014 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13015}
13016
13018 bool AllowUndefs,
13019 bool AllowTruncation) {
13021 return CN;
13022
13023 // SplatVectors can truncate their operands. Ignore that case here unless
13024 // AllowTruncation is set.
13025 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13026 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13027 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13028 EVT CVT = CN->getValueType(0);
13029 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13030 if (AllowTruncation || CVT == VecEltVT)
13031 return CN;
13032 }
13033 }
13034
13036 BitVector UndefElements;
13037 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13038
13039 // BuildVectors can truncate their operands. Ignore that case here unless
13040 // AllowTruncation is set.
13041 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13042 if (CN && (UndefElements.none() || AllowUndefs)) {
13043 EVT CVT = CN->getValueType(0);
13044 EVT NSVT = N.getValueType().getScalarType();
13045 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13046 if (AllowTruncation || (CVT == NSVT))
13047 return CN;
13048 }
13049 }
13050
13051 return nullptr;
13052}
13053
13055 EVT VT = N.getValueType();
13056 APInt DemandedElts = VT.isFixedLengthVector()
13058 : APInt(1, 1);
13059 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13060}
13061
13063 const APInt &DemandedElts,
13064 bool AllowUndefs) {
13066 return CN;
13067
13069 BitVector UndefElements;
13070 ConstantFPSDNode *CN =
13071 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13072 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13073 if (CN && (UndefElements.none() || AllowUndefs))
13074 return CN;
13075 }
13076
13077 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13078 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13079 return CN;
13080
13081 return nullptr;
13082}
13083
13084bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13085 // TODO: may want to use peekThroughBitcast() here.
13086 ConstantSDNode *C =
13087 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13088 return C && C->isZero();
13089}
13090
13091bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13092 ConstantSDNode *C =
13093 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13094 return C && C->isOne();
13095}
13096
13097bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13098 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13099 return C && C->isExactlyValue(1.0);
13100}
13101
13102bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13104 unsigned BitWidth = N.getScalarValueSizeInBits();
13105 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13106 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13107}
13108
13109bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13110 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13111 return C && APInt::isSameValue(C->getAPIntValue(),
13112 APInt(C->getAPIntValue().getBitWidth(), 1));
13113}
13114
13115bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13117 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13118 return C && C->isZero();
13119}
13120
13121bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13122 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13123 return C && C->isZero();
13124}
13125
13129
13130MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13131 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13132 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13133 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13134 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13135 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13136 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13137
13138 // We check here that the size of the memory operand fits within the size of
13139 // the MMO. This is because the MMO might indicate only a possible address
13140 // range instead of specifying the affected memory addresses precisely.
13141 assert(
13142 (!MMO->getType().isValid() ||
13143 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13144 "Size mismatch!");
13145}
13146
13147/// Profile - Gather unique data for the node.
13148///
13150 AddNodeIDNode(ID, this);
13151}
13152
13153namespace {
13154
13155 struct EVTArray {
13156 std::vector<EVT> VTs;
13157
13158 EVTArray() {
13159 VTs.reserve(MVT::VALUETYPE_SIZE);
13160 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13161 VTs.push_back(MVT((MVT::SimpleValueType)i));
13162 }
13163 };
13164
13165} // end anonymous namespace
13166
13167/// getValueTypeList - Return a pointer to the specified value type.
13168///
13169const EVT *SDNode::getValueTypeList(MVT VT) {
13170 static EVTArray SimpleVTArray;
13171
13172 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13173 return &SimpleVTArray.VTs[VT.SimpleTy];
13174}
13175
13176/// hasAnyUseOfValue - Return true if there are any use of the indicated
13177/// value. This method ignores uses of other values defined by this operation.
13178bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13179 assert(Value < getNumValues() && "Bad value!");
13180
13181 for (SDUse &U : uses())
13182 if (U.getResNo() == Value)
13183 return true;
13184
13185 return false;
13186}
13187
13188/// isOnlyUserOf - Return true if this node is the only use of N.
13189bool SDNode::isOnlyUserOf(const SDNode *N) const {
13190 bool Seen = false;
13191 for (const SDNode *User : N->users()) {
13192 if (User == this)
13193 Seen = true;
13194 else
13195 return false;
13196 }
13197
13198 return Seen;
13199}
13200
13201/// Return true if the only users of N are contained in Nodes.
13203 bool Seen = false;
13204 for (const SDNode *User : N->users()) {
13205 if (llvm::is_contained(Nodes, User))
13206 Seen = true;
13207 else
13208 return false;
13209 }
13210
13211 return Seen;
13212}
13213
13214/// Return true if the referenced return value is an operand of N.
13215bool SDValue::isOperandOf(const SDNode *N) const {
13216 return is_contained(N->op_values(), *this);
13217}
13218
13219bool SDNode::isOperandOf(const SDNode *N) const {
13220 return any_of(N->op_values(),
13221 [this](SDValue Op) { return this == Op.getNode(); });
13222}
13223
13224/// reachesChainWithoutSideEffects - Return true if this operand (which must
13225/// be a chain) reaches the specified operand without crossing any
13226/// side-effecting instructions on any chain path. In practice, this looks
13227/// through token factors and non-volatile loads. In order to remain efficient,
13228/// this only looks a couple of nodes in, it does not do an exhaustive search.
13229///
13230/// Note that we only need to examine chains when we're searching for
13231/// side-effects; SelectionDAG requires that all side-effects are represented
13232/// by chains, even if another operand would force a specific ordering. This
13233/// constraint is necessary to allow transformations like splitting loads.
13235 unsigned Depth) const {
13236 if (*this == Dest) return true;
13237
13238 // Don't search too deeply, we just want to be able to see through
13239 // TokenFactor's etc.
13240 if (Depth == 0) return false;
13241
13242 // If this is a token factor, all inputs to the TF happen in parallel.
13243 if (getOpcode() == ISD::TokenFactor) {
13244 // First, try a shallow search.
13245 if (is_contained((*this)->ops(), Dest)) {
13246 // We found the chain we want as an operand of this TokenFactor.
13247 // Essentially, we reach the chain without side-effects if we could
13248 // serialize the TokenFactor into a simple chain of operations with
13249 // Dest as the last operation. This is automatically true if the
13250 // chain has one use: there are no other ordering constraints.
13251 // If the chain has more than one use, we give up: some other
13252 // use of Dest might force a side-effect between Dest and the current
13253 // node.
13254 if (Dest.hasOneUse())
13255 return true;
13256 }
13257 // Next, try a deep search: check whether every operand of the TokenFactor
13258 // reaches Dest.
13259 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13260 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13261 });
13262 }
13263
13264 // Loads don't have side effects, look through them.
13265 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13266 if (Ld->isUnordered())
13267 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13268 }
13269 return false;
13270}
13271
13272bool SDNode::hasPredecessor(const SDNode *N) const {
13275 Worklist.push_back(this);
13276 return hasPredecessorHelper(N, Visited, Worklist);
13277}
13278
13280 this->Flags &= Flags;
13281}
13282
13283SDValue
13285 ArrayRef<ISD::NodeType> CandidateBinOps,
13286 bool AllowPartials) {
13287 // The pattern must end in an extract from index 0.
13288 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13289 !isNullConstant(Extract->getOperand(1)))
13290 return SDValue();
13291
13292 // Match against one of the candidate binary ops.
13293 SDValue Op = Extract->getOperand(0);
13294 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13295 return Op.getOpcode() == unsigned(BinOp);
13296 }))
13297 return SDValue();
13298
13299 // Floating-point reductions may require relaxed constraints on the final step
13300 // of the reduction because they may reorder intermediate operations.
13301 unsigned CandidateBinOp = Op.getOpcode();
13302 if (Op.getValueType().isFloatingPoint()) {
13303 SDNodeFlags Flags = Op->getFlags();
13304 switch (CandidateBinOp) {
13305 case ISD::FADD:
13306 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13307 return SDValue();
13308 break;
13309 default:
13310 llvm_unreachable("Unhandled FP opcode for binop reduction");
13311 }
13312 }
13313
13314 // Matching failed - attempt to see if we did enough stages that a partial
13315 // reduction from a subvector is possible.
13316 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13317 if (!AllowPartials || !Op)
13318 return SDValue();
13319 EVT OpVT = Op.getValueType();
13320 EVT OpSVT = OpVT.getScalarType();
13321 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13322 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13323 return SDValue();
13324 BinOp = (ISD::NodeType)CandidateBinOp;
13325 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13326 };
13327
13328 // At each stage, we're looking for something that looks like:
13329 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13330 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13331 // i32 undef, i32 undef, i32 undef, i32 undef>
13332 // %a = binop <8 x i32> %op, %s
13333 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13334 // we expect something like:
13335 // <4,5,6,7,u,u,u,u>
13336 // <2,3,u,u,u,u,u,u>
13337 // <1,u,u,u,u,u,u,u>
13338 // While a partial reduction match would be:
13339 // <2,3,u,u,u,u,u,u>
13340 // <1,u,u,u,u,u,u,u>
13341 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13342 SDValue PrevOp;
13343 for (unsigned i = 0; i < Stages; ++i) {
13344 unsigned MaskEnd = (1 << i);
13345
13346 if (Op.getOpcode() != CandidateBinOp)
13347 return PartialReduction(PrevOp, MaskEnd);
13348
13349 SDValue Op0 = Op.getOperand(0);
13350 SDValue Op1 = Op.getOperand(1);
13351
13353 if (Shuffle) {
13354 Op = Op1;
13355 } else {
13356 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13357 Op = Op0;
13358 }
13359
13360 // The first operand of the shuffle should be the same as the other operand
13361 // of the binop.
13362 if (!Shuffle || Shuffle->getOperand(0) != Op)
13363 return PartialReduction(PrevOp, MaskEnd);
13364
13365 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13366 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13367 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13368 return PartialReduction(PrevOp, MaskEnd);
13369
13370 PrevOp = Op;
13371 }
13372
13373 // Handle subvector reductions, which tend to appear after the shuffle
13374 // reduction stages.
13375 while (Op.getOpcode() == CandidateBinOp) {
13376 unsigned NumElts = Op.getValueType().getVectorNumElements();
13377 SDValue Op0 = Op.getOperand(0);
13378 SDValue Op1 = Op.getOperand(1);
13379 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13381 Op0.getOperand(0) != Op1.getOperand(0))
13382 break;
13383 SDValue Src = Op0.getOperand(0);
13384 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13385 if (NumSrcElts != (2 * NumElts))
13386 break;
13387 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13388 Op1.getConstantOperandAPInt(1) == NumElts) &&
13389 !(Op1.getConstantOperandAPInt(1) == 0 &&
13390 Op0.getConstantOperandAPInt(1) == NumElts))
13391 break;
13392 Op = Src;
13393 }
13394
13395 BinOp = (ISD::NodeType)CandidateBinOp;
13396 return Op;
13397}
13398
13400 EVT VT = N->getValueType(0);
13401 EVT EltVT = VT.getVectorElementType();
13402 unsigned NE = VT.getVectorNumElements();
13403
13404 SDLoc dl(N);
13405
13406 // If ResNE is 0, fully unroll the vector op.
13407 if (ResNE == 0)
13408 ResNE = NE;
13409 else if (NE > ResNE)
13410 NE = ResNE;
13411
13412 if (N->getNumValues() == 2) {
13413 SmallVector<SDValue, 8> Scalars0, Scalars1;
13414 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13415 EVT VT1 = N->getValueType(1);
13416 EVT EltVT1 = VT1.getVectorElementType();
13417
13418 unsigned i;
13419 for (i = 0; i != NE; ++i) {
13420 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13421 SDValue Operand = N->getOperand(j);
13422 EVT OperandVT = Operand.getValueType();
13423
13424 // A vector operand; extract a single element.
13425 EVT OperandEltVT = OperandVT.getVectorElementType();
13426 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13427 }
13428
13429 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13430 Scalars0.push_back(EltOp);
13431 Scalars1.push_back(EltOp.getValue(1));
13432 }
13433
13434 for (; i < ResNE; ++i) {
13435 Scalars0.push_back(getUNDEF(EltVT));
13436 Scalars1.push_back(getUNDEF(EltVT1));
13437 }
13438
13439 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13440 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13441 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13442 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13443 return getMergeValues({Vec0, Vec1}, dl);
13444 }
13445
13446 assert(N->getNumValues() == 1 &&
13447 "Can't unroll a vector with multiple results!");
13448
13450 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13451
13452 unsigned i;
13453 for (i= 0; i != NE; ++i) {
13454 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13455 SDValue Operand = N->getOperand(j);
13456 EVT OperandVT = Operand.getValueType();
13457 if (OperandVT.isVector()) {
13458 // A vector operand; extract a single element.
13459 EVT OperandEltVT = OperandVT.getVectorElementType();
13460 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13461 } else {
13462 // A scalar operand; just use it as is.
13463 Operands[j] = Operand;
13464 }
13465 }
13466
13467 switch (N->getOpcode()) {
13468 default: {
13469 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13470 N->getFlags()));
13471 break;
13472 }
13473 case ISD::VSELECT:
13474 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13475 break;
13476 case ISD::SHL:
13477 case ISD::SRA:
13478 case ISD::SRL:
13479 case ISD::ROTL:
13480 case ISD::ROTR:
13481 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13482 getShiftAmountOperand(Operands[0].getValueType(),
13483 Operands[1])));
13484 break;
13486 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13487 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13488 Operands[0],
13489 getValueType(ExtVT)));
13490 break;
13491 }
13492 case ISD::ADDRSPACECAST: {
13493 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13494 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13495 ASC->getSrcAddressSpace(),
13496 ASC->getDestAddressSpace()));
13497 break;
13498 }
13499 }
13500 }
13501
13502 for (; i < ResNE; ++i)
13503 Scalars.push_back(getUNDEF(EltVT));
13504
13505 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13506 return getBuildVector(VecVT, dl, Scalars);
13507}
13508
13509std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13510 SDNode *N, unsigned ResNE) {
13511 unsigned Opcode = N->getOpcode();
13512 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13513 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13514 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13515 "Expected an overflow opcode");
13516
13517 EVT ResVT = N->getValueType(0);
13518 EVT OvVT = N->getValueType(1);
13519 EVT ResEltVT = ResVT.getVectorElementType();
13520 EVT OvEltVT = OvVT.getVectorElementType();
13521 SDLoc dl(N);
13522
13523 // If ResNE is 0, fully unroll the vector op.
13524 unsigned NE = ResVT.getVectorNumElements();
13525 if (ResNE == 0)
13526 ResNE = NE;
13527 else if (NE > ResNE)
13528 NE = ResNE;
13529
13530 SmallVector<SDValue, 8> LHSScalars;
13531 SmallVector<SDValue, 8> RHSScalars;
13532 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13533 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13534
13535 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13536 SDVTList VTs = getVTList(ResEltVT, SVT);
13537 SmallVector<SDValue, 8> ResScalars;
13538 SmallVector<SDValue, 8> OvScalars;
13539 for (unsigned i = 0; i < NE; ++i) {
13540 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13541 SDValue Ov =
13542 getSelect(dl, OvEltVT, Res.getValue(1),
13543 getBoolConstant(true, dl, OvEltVT, ResVT),
13544 getConstant(0, dl, OvEltVT));
13545
13546 ResScalars.push_back(Res);
13547 OvScalars.push_back(Ov);
13548 }
13549
13550 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13551 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13552
13553 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13554 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13555 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13556 getBuildVector(NewOvVT, dl, OvScalars));
13557}
13558
13561 unsigned Bytes,
13562 int Dist) const {
13563 if (LD->isVolatile() || Base->isVolatile())
13564 return false;
13565 // TODO: probably too restrictive for atomics, revisit
13566 if (!LD->isSimple())
13567 return false;
13568 if (LD->isIndexed() || Base->isIndexed())
13569 return false;
13570 if (LD->getChain() != Base->getChain())
13571 return false;
13572 EVT VT = LD->getMemoryVT();
13573 if (VT.getSizeInBits() / 8 != Bytes)
13574 return false;
13575
13576 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13577 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13578
13579 int64_t Offset = 0;
13580 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13581 return (Dist * (int64_t)Bytes == Offset);
13582 return false;
13583}
13584
13585/// InferPtrAlignment - Infer alignment of a load / store address. Return
13586/// std::nullopt if it cannot be inferred.
13588 // If this is a GlobalAddress + cst, return the alignment.
13589 const GlobalValue *GV = nullptr;
13590 int64_t GVOffset = 0;
13591 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13592 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13593 KnownBits Known(PtrWidth);
13595 unsigned AlignBits = Known.countMinTrailingZeros();
13596 if (AlignBits)
13597 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13598 }
13599
13600 // If this is a direct reference to a stack slot, use information about the
13601 // stack slot's alignment.
13602 int FrameIdx = INT_MIN;
13603 int64_t FrameOffset = 0;
13605 FrameIdx = FI->getIndex();
13606 } else if (isBaseWithConstantOffset(Ptr) &&
13608 // Handle FI+Cst
13609 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13610 FrameOffset = Ptr.getConstantOperandVal(1);
13611 }
13612
13613 if (FrameIdx != INT_MIN) {
13615 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13616 }
13617
13618 return std::nullopt;
13619}
13620
13621/// Split the scalar node with EXTRACT_ELEMENT using the provided
13622/// VTs and return the low/high part.
13623std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13624 const SDLoc &DL,
13625 const EVT &LoVT,
13626 const EVT &HiVT) {
13627 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13628 "Split node must be a scalar type");
13629 SDValue Lo =
13631 SDValue Hi =
13633 return std::make_pair(Lo, Hi);
13634}
13635
13636/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13637/// which is split (or expanded) into two not necessarily identical pieces.
13638std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13639 // Currently all types are split in half.
13640 EVT LoVT, HiVT;
13641 if (!VT.isVector())
13642 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13643 else
13644 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13645
13646 return std::make_pair(LoVT, HiVT);
13647}
13648
13649/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13650/// type, dependent on an enveloping VT that has been split into two identical
13651/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13652std::pair<EVT, EVT>
13654 bool *HiIsEmpty) const {
13655 EVT EltTp = VT.getVectorElementType();
13656 // Examples:
13657 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13658 // custom VL=9 with enveloping VL=8/8 yields 8/1
13659 // custom VL=10 with enveloping VL=8/8 yields 8/2
13660 // etc.
13661 ElementCount VTNumElts = VT.getVectorElementCount();
13662 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13663 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13664 "Mixing fixed width and scalable vectors when enveloping a type");
13665 EVT LoVT, HiVT;
13666 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13667 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13668 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13669 *HiIsEmpty = false;
13670 } else {
13671 // Flag that hi type has zero storage size, but return split envelop type
13672 // (this would be easier if vector types with zero elements were allowed).
13673 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13674 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13675 *HiIsEmpty = true;
13676 }
13677 return std::make_pair(LoVT, HiVT);
13678}
13679
13680/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13681/// low/high part.
13682std::pair<SDValue, SDValue>
13683SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13684 const EVT &HiVT) {
13685 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13686 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13687 "Splitting vector with an invalid mixture of fixed and scalable "
13688 "vector types");
13690 N.getValueType().getVectorMinNumElements() &&
13691 "More vector elements requested than available!");
13692 SDValue Lo, Hi;
13693 Lo = getExtractSubvector(DL, LoVT, N, 0);
13694 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13695 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13696 // IDX with the runtime scaling factor of the result vector type. For
13697 // fixed-width result vectors, that runtime scaling factor is 1.
13700 return std::make_pair(Lo, Hi);
13701}
13702
13703std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13704 const SDLoc &DL) {
13705 // Split the vector length parameter.
13706 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13707 EVT VT = N.getValueType();
13709 "Expecting the mask to be an evenly-sized vector");
13710 SDValue HalfNumElts = getElementCount(
13712 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13713 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13714 return std::make_pair(Lo, Hi);
13715}
13716
13717/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13719 EVT VT = N.getValueType();
13722 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13723}
13724
13727 unsigned Start, unsigned Count,
13728 EVT EltVT) {
13729 EVT VT = Op.getValueType();
13730 if (Count == 0)
13732 if (EltVT == EVT())
13733 EltVT = VT.getVectorElementType();
13734 SDLoc SL(Op);
13735 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13736 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13737 }
13738}
13739
13740// getAddressSpace - Return the address space this GlobalAddress belongs to.
13742 return getGlobal()->getType()->getAddressSpace();
13743}
13744
13747 return Val.MachineCPVal->getType();
13748 return Val.ConstVal->getType();
13749}
13750
13751bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13752 unsigned &SplatBitSize,
13753 bool &HasAnyUndefs,
13754 unsigned MinSplatBits,
13755 bool IsBigEndian) const {
13756 EVT VT = getValueType(0);
13757 assert(VT.isVector() && "Expected a vector type");
13758 unsigned VecWidth = VT.getSizeInBits();
13759 if (MinSplatBits > VecWidth)
13760 return false;
13761
13762 // FIXME: The widths are based on this node's type, but build vectors can
13763 // truncate their operands.
13764 SplatValue = APInt(VecWidth, 0);
13765 SplatUndef = APInt(VecWidth, 0);
13766
13767 // Get the bits. Bits with undefined values (when the corresponding element
13768 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13769 // in SplatValue. If any of the values are not constant, give up and return
13770 // false.
13771 unsigned int NumOps = getNumOperands();
13772 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13773 unsigned EltWidth = VT.getScalarSizeInBits();
13774
13775 for (unsigned j = 0; j < NumOps; ++j) {
13776 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13777 SDValue OpVal = getOperand(i);
13778 unsigned BitPos = j * EltWidth;
13779
13780 if (OpVal.isUndef())
13781 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13782 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13783 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13784 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13785 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13786 else
13787 return false;
13788 }
13789
13790 // The build_vector is all constants or undefs. Find the smallest element
13791 // size that splats the vector.
13792 HasAnyUndefs = (SplatUndef != 0);
13793
13794 // FIXME: This does not work for vectors with elements less than 8 bits.
13795 while (VecWidth > 8) {
13796 // If we can't split in half, stop here.
13797 if (VecWidth & 1)
13798 break;
13799
13800 unsigned HalfSize = VecWidth / 2;
13801 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13802 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13803 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13804 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13805
13806 // If the two halves do not match (ignoring undef bits), stop here.
13807 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13808 MinSplatBits > HalfSize)
13809 break;
13810
13811 SplatValue = HighValue | LowValue;
13812 SplatUndef = HighUndef & LowUndef;
13813
13814 VecWidth = HalfSize;
13815 }
13816
13817 // FIXME: The loop above only tries to split in halves. But if the input
13818 // vector for example is <3 x i16> it wouldn't be able to detect a
13819 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13820 // optimizations. I guess that back in the days when this helper was created
13821 // vectors normally was power-of-2 sized.
13822
13823 SplatBitSize = VecWidth;
13824 return true;
13825}
13826
13828 BitVector *UndefElements) const {
13829 unsigned NumOps = getNumOperands();
13830 if (UndefElements) {
13831 UndefElements->clear();
13832 UndefElements->resize(NumOps);
13833 }
13834 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13835 if (!DemandedElts)
13836 return SDValue();
13837 SDValue Splatted;
13838 for (unsigned i = 0; i != NumOps; ++i) {
13839 if (!DemandedElts[i])
13840 continue;
13841 SDValue Op = getOperand(i);
13842 if (Op.isUndef()) {
13843 if (UndefElements)
13844 (*UndefElements)[i] = true;
13845 } else if (!Splatted) {
13846 Splatted = Op;
13847 } else if (Splatted != Op) {
13848 return SDValue();
13849 }
13850 }
13851
13852 if (!Splatted) {
13853 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13854 assert(getOperand(FirstDemandedIdx).isUndef() &&
13855 "Can only have a splat without a constant for all undefs.");
13856 return getOperand(FirstDemandedIdx);
13857 }
13858
13859 return Splatted;
13860}
13861
13863 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13864 return getSplatValue(DemandedElts, UndefElements);
13865}
13866
13868 SmallVectorImpl<SDValue> &Sequence,
13869 BitVector *UndefElements) const {
13870 unsigned NumOps = getNumOperands();
13871 Sequence.clear();
13872 if (UndefElements) {
13873 UndefElements->clear();
13874 UndefElements->resize(NumOps);
13875 }
13876 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13877 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13878 return false;
13879
13880 // Set the undefs even if we don't find a sequence (like getSplatValue).
13881 if (UndefElements)
13882 for (unsigned I = 0; I != NumOps; ++I)
13883 if (DemandedElts[I] && getOperand(I).isUndef())
13884 (*UndefElements)[I] = true;
13885
13886 // Iteratively widen the sequence length looking for repetitions.
13887 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13888 Sequence.append(SeqLen, SDValue());
13889 for (unsigned I = 0; I != NumOps; ++I) {
13890 if (!DemandedElts[I])
13891 continue;
13892 SDValue &SeqOp = Sequence[I % SeqLen];
13894 if (Op.isUndef()) {
13895 if (!SeqOp)
13896 SeqOp = Op;
13897 continue;
13898 }
13899 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13900 Sequence.clear();
13901 break;
13902 }
13903 SeqOp = Op;
13904 }
13905 if (!Sequence.empty())
13906 return true;
13907 }
13908
13909 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13910 return false;
13911}
13912
13914 BitVector *UndefElements) const {
13915 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13916 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13917}
13918
13921 BitVector *UndefElements) const {
13923 getSplatValue(DemandedElts, UndefElements));
13924}
13925
13928 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13929}
13930
13933 BitVector *UndefElements) const {
13935 getSplatValue(DemandedElts, UndefElements));
13936}
13937
13942
13943int32_t
13945 uint32_t BitWidth) const {
13946 if (ConstantFPSDNode *CN =
13948 bool IsExact;
13949 APSInt IntVal(BitWidth);
13950 const APFloat &APF = CN->getValueAPF();
13951 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13952 APFloat::opOK ||
13953 !IsExact)
13954 return -1;
13955
13956 return IntVal.exactLogBase2();
13957 }
13958 return -1;
13959}
13960
13962 bool IsLittleEndian, unsigned DstEltSizeInBits,
13963 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13964 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13965 if (!isConstant())
13966 return false;
13967
13968 unsigned NumSrcOps = getNumOperands();
13969 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13970 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13971 "Invalid bitcast scale");
13972
13973 // Extract raw src bits.
13974 SmallVector<APInt> SrcBitElements(NumSrcOps,
13975 APInt::getZero(SrcEltSizeInBits));
13976 BitVector SrcUndeElements(NumSrcOps, false);
13977
13978 for (unsigned I = 0; I != NumSrcOps; ++I) {
13980 if (Op.isUndef()) {
13981 SrcUndeElements.set(I);
13982 continue;
13983 }
13984 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13985 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13986 assert((CInt || CFP) && "Unknown constant");
13987 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13988 : CFP->getValueAPF().bitcastToAPInt();
13989 }
13990
13991 // Recast to dst width.
13992 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13993 SrcBitElements, UndefElements, SrcUndeElements);
13994 return true;
13995}
13996
13997void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13998 unsigned DstEltSizeInBits,
13999 SmallVectorImpl<APInt> &DstBitElements,
14000 ArrayRef<APInt> SrcBitElements,
14001 BitVector &DstUndefElements,
14002 const BitVector &SrcUndefElements) {
14003 unsigned NumSrcOps = SrcBitElements.size();
14004 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14005 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14006 "Invalid bitcast scale");
14007 assert(NumSrcOps == SrcUndefElements.size() &&
14008 "Vector size mismatch");
14009
14010 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14011 DstUndefElements.clear();
14012 DstUndefElements.resize(NumDstOps, false);
14013 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14014
14015 // Concatenate src elements constant bits together into dst element.
14016 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14017 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14018 for (unsigned I = 0; I != NumDstOps; ++I) {
14019 DstUndefElements.set(I);
14020 APInt &DstBits = DstBitElements[I];
14021 for (unsigned J = 0; J != Scale; ++J) {
14022 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14023 if (SrcUndefElements[Idx])
14024 continue;
14025 DstUndefElements.reset(I);
14026 const APInt &SrcBits = SrcBitElements[Idx];
14027 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14028 "Illegal constant bitwidths");
14029 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14030 }
14031 }
14032 return;
14033 }
14034
14035 // Split src element constant bits into dst elements.
14036 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14037 for (unsigned I = 0; I != NumSrcOps; ++I) {
14038 if (SrcUndefElements[I]) {
14039 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14040 continue;
14041 }
14042 const APInt &SrcBits = SrcBitElements[I];
14043 for (unsigned J = 0; J != Scale; ++J) {
14044 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14045 APInt &DstBits = DstBitElements[Idx];
14046 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14047 }
14048 }
14049}
14050
14052 for (const SDValue &Op : op_values()) {
14053 unsigned Opc = Op.getOpcode();
14054 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14055 return false;
14056 }
14057 return true;
14058}
14059
14060std::optional<std::pair<APInt, APInt>>
14062 unsigned NumOps = getNumOperands();
14063 if (NumOps < 2)
14064 return std::nullopt;
14065
14068 return std::nullopt;
14069
14070 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14071 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14072 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14073
14074 if (Stride.isZero())
14075 return std::nullopt;
14076
14077 for (unsigned i = 2; i < NumOps; ++i) {
14079 return std::nullopt;
14080
14081 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14082 if (Val != (Start + (Stride * i)))
14083 return std::nullopt;
14084 }
14085
14086 return std::make_pair(Start, Stride);
14087}
14088
14090 // Find the first non-undef value in the shuffle mask.
14091 unsigned i, e;
14092 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14093 /* search */;
14094
14095 // If all elements are undefined, this shuffle can be considered a splat
14096 // (although it should eventually get simplified away completely).
14097 if (i == e)
14098 return true;
14099
14100 // Make sure all remaining elements are either undef or the same as the first
14101 // non-undef value.
14102 for (int Idx = Mask[i]; i != e; ++i)
14103 if (Mask[i] >= 0 && Mask[i] != Idx)
14104 return false;
14105 return true;
14106}
14107
14108// Returns true if it is a constant integer BuildVector or constant integer,
14109// possibly hidden by a bitcast.
14111 SDValue N, bool AllowOpaques) const {
14113
14114 if (auto *C = dyn_cast<ConstantSDNode>(N))
14115 return AllowOpaques || !C->isOpaque();
14116
14118 return true;
14119
14120 // Treat a GlobalAddress supporting constant offset folding as a
14121 // constant integer.
14122 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14123 if (GA->getOpcode() == ISD::GlobalAddress &&
14124 TLI->isOffsetFoldingLegal(GA))
14125 return true;
14126
14127 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14128 isa<ConstantSDNode>(N.getOperand(0)))
14129 return true;
14130 return false;
14131}
14132
14133// Returns true if it is a constant float BuildVector or constant float.
14136 return true;
14137
14139 return true;
14140
14141 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14142 isa<ConstantFPSDNode>(N.getOperand(0)))
14143 return true;
14144
14145 return false;
14146}
14147
14148std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14149 ConstantSDNode *Const =
14150 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14151 if (!Const)
14152 return std::nullopt;
14153
14154 EVT VT = N->getValueType(0);
14155 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14156 switch (TLI->getBooleanContents(N.getValueType())) {
14158 if (CVal.isOne())
14159 return true;
14160 if (CVal.isZero())
14161 return false;
14162 return std::nullopt;
14164 if (CVal.isAllOnes())
14165 return true;
14166 if (CVal.isZero())
14167 return false;
14168 return std::nullopt;
14170 return CVal[0];
14171 }
14172 llvm_unreachable("Unknown BooleanContent enum");
14173}
14174
14175void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14176 assert(!Node->OperandList && "Node already has operands");
14178 "too many operands to fit into SDNode");
14179 SDUse *Ops = OperandRecycler.allocate(
14180 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14181
14182 bool IsDivergent = false;
14183 for (unsigned I = 0; I != Vals.size(); ++I) {
14184 Ops[I].setUser(Node);
14185 Ops[I].setInitial(Vals[I]);
14186 EVT VT = Ops[I].getValueType();
14187
14188 // Skip Chain. It does not carry divergence.
14189 if (VT != MVT::Other &&
14190 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14191 Ops[I].getNode()->isDivergent()) {
14192 IsDivergent = true;
14193 }
14194 }
14195 Node->NumOperands = Vals.size();
14196 Node->OperandList = Ops;
14197 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14198 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14199 Node->SDNodeBits.IsDivergent = IsDivergent;
14200 }
14201 checkForCycles(Node);
14202}
14203
14206 size_t Limit = SDNode::getMaxNumOperands();
14207 while (Vals.size() > Limit) {
14208 unsigned SliceIdx = Vals.size() - Limit;
14209 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14210 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14211 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14212 Vals.emplace_back(NewTF);
14213 }
14214 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14215}
14216
14218 EVT VT, SDNodeFlags Flags) {
14219 switch (Opcode) {
14220 default:
14221 return SDValue();
14222 case ISD::ADD:
14223 case ISD::OR:
14224 case ISD::XOR:
14225 case ISD::UMAX:
14226 return getConstant(0, DL, VT);
14227 case ISD::MUL:
14228 return getConstant(1, DL, VT);
14229 case ISD::AND:
14230 case ISD::UMIN:
14231 return getAllOnesConstant(DL, VT);
14232 case ISD::SMAX:
14234 case ISD::SMIN:
14236 case ISD::FADD:
14237 // If flags allow, prefer positive zero since it's generally cheaper
14238 // to materialize on most targets.
14239 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14240 case ISD::FMUL:
14241 return getConstantFP(1.0, DL, VT);
14242 case ISD::FMINNUM:
14243 case ISD::FMAXNUM: {
14244 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14245 const fltSemantics &Semantics = VT.getFltSemantics();
14246 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14247 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14248 APFloat::getLargest(Semantics);
14249 if (Opcode == ISD::FMAXNUM)
14250 NeutralAF.changeSign();
14251
14252 return getConstantFP(NeutralAF, DL, VT);
14253 }
14254 case ISD::FMINIMUM:
14255 case ISD::FMAXIMUM: {
14256 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14257 const fltSemantics &Semantics = VT.getFltSemantics();
14258 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14259 : APFloat::getLargest(Semantics);
14260 if (Opcode == ISD::FMAXIMUM)
14261 NeutralAF.changeSign();
14262
14263 return getConstantFP(NeutralAF, DL, VT);
14264 }
14265
14266 }
14267}
14268
14269/// Helper used to make a call to a library function that has one argument of
14270/// pointer type.
14271///
14272/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14273/// used to get or set floating-point state. They have one argument of pointer
14274/// type, which points to the memory region containing bits of the
14275/// floating-point state. The value returned by such function is ignored in the
14276/// created call.
14277///
14278/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14279/// \param Ptr Pointer used to save/load state.
14280/// \param InChain Ingoing token chain.
14281/// \returns Outgoing chain token.
14283 SDValue InChain,
14284 const SDLoc &DLoc) {
14285 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14287 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14288 RTLIB::LibcallImpl LibcallImpl =
14289 TLI->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14290 if (LibcallImpl == RTLIB::Unsupported)
14291 reportFatalUsageError("emitting call to unsupported libcall");
14292
14293 SDValue Callee =
14294 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14296 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14297 TLI->getLibcallImplCallingConv(LibcallImpl),
14298 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14299 return TLI->LowerCallTo(CLI).second;
14300}
14301
14303 assert(From && To && "Invalid SDNode; empty source SDValue?");
14304 auto I = SDEI.find(From);
14305 if (I == SDEI.end())
14306 return;
14307
14308 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14309 // the iterator, hence the need to make a copy to prevent a use-after-free.
14310 NodeExtraInfo NEI = I->second;
14311 if (LLVM_LIKELY(!NEI.PCSections)) {
14312 // No deep copy required for the types of extra info set.
14313 //
14314 // FIXME: Investigate if other types of extra info also need deep copy. This
14315 // depends on the types of nodes they can be attached to: if some extra info
14316 // is only ever attached to nodes where a replacement To node is always the
14317 // node where later use and propagation of the extra info has the intended
14318 // semantics, no deep copy is required.
14319 SDEI[To] = std::move(NEI);
14320 return;
14321 }
14322
14323 const SDNode *EntrySDN = getEntryNode().getNode();
14324
14325 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14326 // through the replacement of From with To. Otherwise, replacements of a node
14327 // (From) with more complex nodes (To and its operands) may result in lost
14328 // extra info where the root node (To) is insignificant in further propagating
14329 // and using extra info when further lowering to MIR.
14330 //
14331 // In the first step pre-populate the visited set with the nodes reachable
14332 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14333 // DAG that is not new and should be left untouched.
14334 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14335 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14336 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14337 if (MaxDepth == 0) {
14338 // Remember this node in case we need to increase MaxDepth and continue
14339 // populating FromReach from this node.
14340 Leafs.emplace_back(N);
14341 return;
14342 }
14343 if (!FromReach.insert(N).second)
14344 return;
14345 for (const SDValue &Op : N->op_values())
14346 Self(Self, Op.getNode(), MaxDepth - 1);
14347 };
14348
14349 // Copy extra info to To and all its transitive operands (that are new).
14351 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14352 if (FromReach.contains(N))
14353 return true;
14354 if (!Visited.insert(N).second)
14355 return true;
14356 if (EntrySDN == N)
14357 return false;
14358 for (const SDValue &Op : N->op_values()) {
14359 if (N == To && Op.getNode() == EntrySDN) {
14360 // Special case: New node's operand is the entry node; just need to
14361 // copy extra info to new node.
14362 break;
14363 }
14364 if (!Self(Self, Op.getNode()))
14365 return false;
14366 }
14367 // Copy only if entry node was not reached.
14368 SDEI[N] = NEI;
14369 return true;
14370 };
14371
14372 // We first try with a lower MaxDepth, assuming that the path to common
14373 // operands between From and To is relatively short. This significantly
14374 // improves performance in the common case. The initial MaxDepth is big
14375 // enough to avoid retry in the common case; the last MaxDepth is large
14376 // enough to avoid having to use the fallback below (and protects from
14377 // potential stack exhaustion from recursion).
14378 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14379 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14380 // StartFrom is the previous (or initial) set of leafs reachable at the
14381 // previous maximum depth.
14383 std::swap(StartFrom, Leafs);
14384 for (const SDNode *N : StartFrom)
14385 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14386 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14387 return;
14388 // This should happen very rarely (reached the entry node).
14389 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14390 assert(!Leafs.empty());
14391 }
14392
14393 // This should not happen - but if it did, that means the subgraph reachable
14394 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14395 // could not visit all reachable common operands. Consequently, we were able
14396 // to reach the entry node.
14397 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14398 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14399 // Best-effort fallback if assertions disabled.
14400 SDEI[To] = std::move(NEI);
14401}
14402
14403#ifndef NDEBUG
14404static void checkForCyclesHelper(const SDNode *N,
14407 const llvm::SelectionDAG *DAG) {
14408 // If this node has already been checked, don't check it again.
14409 if (Checked.count(N))
14410 return;
14411
14412 // If a node has already been visited on this depth-first walk, reject it as
14413 // a cycle.
14414 if (!Visited.insert(N).second) {
14415 errs() << "Detected cycle in SelectionDAG\n";
14416 dbgs() << "Offending node:\n";
14417 N->dumprFull(DAG); dbgs() << "\n";
14418 abort();
14419 }
14420
14421 for (const SDValue &Op : N->op_values())
14422 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14423
14424 Checked.insert(N);
14425 Visited.erase(N);
14426}
14427#endif
14428
14430 const llvm::SelectionDAG *DAG,
14431 bool force) {
14432#ifndef NDEBUG
14433 bool check = force;
14434#ifdef EXPENSIVE_CHECKS
14435 check = true;
14436#endif // EXPENSIVE_CHECKS
14437 if (check) {
14438 assert(N && "Checking nonexistent SDNode");
14441 checkForCyclesHelper(N, visited, checked, DAG);
14442 }
14443#endif // !NDEBUG
14444}
14445
14446void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14447 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14448}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-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:569
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
iv users
Definition IVUsers.cpp:48
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
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:539
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
Contains matchers for matching SelectionDAG nodes and values.
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
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 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, BatchAAResults *BatchAA)
static SDValue getFixedOrScalableQuantity(SelectionDAG &DAG, const SDLoc &DL, EVT VT, Ty Quantity)
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 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 bool isInTailCallPositionWrapper(const CallInst *CI, const SelectionDAG *SelDAG, bool AllowReturnsFirstArg)
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 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)
static cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This file describes how to lower LLVM code to machine code.
static void removeOperands(MachineInstr &MI, unsigned i)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static const fltSemantics & BFloat()
Definition APFloat.h:295
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:347
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:346
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1102
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1190
void copySign(const APFloat &RHS)
Definition APFloat.h:1284
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6053
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1172
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:1414
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1163
bool isFinite() const
Definition APFloat.h:1436
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1329
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1181
bool isSignaling() const
Definition APFloat.h:1433
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1217
bool isZero() const
Definition APFloat.h:1427
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1314
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1208
bool isPosZero() const
Definition APFloat.h:1442
bool isNegZero() const
Definition APFloat.h:1443
void changeSign()
Definition APFloat.h:1279
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1091
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1971
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2055
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1407
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1012
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1541
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1392
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1671
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1386
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1033
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1513
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1331
APInt abs() const
Get the absolute value.
Definition APInt.h:1796
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2026
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1489
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1397
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1154
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:835
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1640
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1629
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1599
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2086
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2100
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1141
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1436
unsigned logBase2() const
Definition APInt.h:1762
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2036
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1736
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:985
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1368
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1418
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1389
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2045
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:904
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI 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 LLVM_ABI 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.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI 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.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI 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.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI 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_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI 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:282
const APFloat & getValue() const
Definition Constants.h:326
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
MachineConstantPoolValue * getMachineCPVal() const
const Constant * getConstVal() const
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI 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 LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI 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.
LLVM_ABI 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:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI 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 LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI 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.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:214
LLVM_ABI 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.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:123
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:209
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
This SDNode is used for LIFETIME_START/LIFETIME_END values.
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI 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.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for 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.
LLVM_ABI 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:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:230
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
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(Register 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.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI 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.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI 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.
LLVM_ABI 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 LLVM_ABI 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.
LLVM_ABI 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.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI 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
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
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.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI 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 void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC)
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI 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.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI 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())
LLVM_ABI 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 getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI 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.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
Lower a strlen operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI void updateDivergence(SDNode *N)
LLVM_ABI 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...
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI 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),...
LLVM_ABI 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.
LLVM_ABI SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags, bool AllowCommute=false)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI 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,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI 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,...
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI 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())
LLVM_ABI 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...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI 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)
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI 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...
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > 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...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI 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.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI 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,...
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const
Check if a use of a float value is insensitive to signed zeros.
LLVM_ABI bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
LLVM_ABI 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=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI 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...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
LLVM_ABI 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.
LLVM_ABI 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(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI 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
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI 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)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI void dump(bool Sorted=false) const
Dump the textual format of this DAG.
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI 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...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI 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.
LLVM_ABI 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
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI 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.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI 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.
LLVM_ABI std::pair< SDValue, SDValue > getStrcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, const CallInst *CI)
Lower a strcpy operation into a target library call and return the resulting chain and call result as...
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...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > 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...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI 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.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI 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)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI 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.
LLVM_ABI 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())
LLVM_ABI SDValue getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getTypeSize(const SDLoc &DL, EVT VT, TypeSize TS)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
LLVM_ABI std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
Lower a memcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI 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,...
LLVM_ABI 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.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI 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...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > 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...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI 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 ...
LLVM_ABI 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)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI 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...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
LLVM_ABI 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...
LLVM_ABI 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,...
LLVM_ABI SDValue getMaskFromElementCount(const SDLoc &DL, EVT VT, ElementCount Len)
Return a vector with the first 'Len' lanes set to true and remaining lanes set to false.
LLVM_ABI 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()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
LLVM_ABI bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
LLVM_ABI 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)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
LLVM_ABI 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...
LLVM_ABI 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
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, 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 in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI 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)
LLVM_ABI 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.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI 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.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI 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)
LLVM_ABI 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(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI 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,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void getTopologicallyOrderedNodes(SmallVectorImpl< const SDNode * > &SortedNodes) const
Get all the nodes in their topological order without modifying any states.
LLVM_ABI 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
LLVM_ABI 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 ...
LLVM_ABI 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.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI 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...
LLVM_ABI 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.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI 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...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI 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.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI 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.
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI 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...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI 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.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI 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 ...
LLVM_ABI 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...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI 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 ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI 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
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
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.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
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.
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.
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.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
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 getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
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...
BooleanContent
Enum that describes how the target represents true/false values.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, 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.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:631
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
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:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
LLVM_ABI void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:233
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:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
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:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt clmulr(const APInt &LHS, const APInt &RHS)
Perform a reversed carry-less multiply.
Definition APInt.cpp:3201
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3131
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3118
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3108
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3182
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3123
LLVM_ABI APInt clmul(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, also known as XOR multiplication, and return low-bits.
Definition APInt.cpp:3191
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2269
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3173
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3009
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3206
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2274
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3103
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3113
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
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
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI 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...
LLVM_ABI 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:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:818
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:787
@ TargetConstantPool
Definition ISDOpcodes.h:189
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ PTRADD
PTRADD represents pointer arithmetic semantics, for targets that opt in using shouldPreservePtrArith(...
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition ISDOpcodes.h:538
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:778
@ TargetBlockAddress
Definition ISDOpcodes.h:191
@ DEACTIVATION_SYMBOL
Untyped node storing deactivation symbol reference (DeactivationSymbolSDNode).
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:294
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:522
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:852
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:879
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:746
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:909
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:528
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:992
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:773
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:843
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:714
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:664
@ TargetExternalSymbol
Definition ISDOpcodes.h:190
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:786
@ TargetJumpTable
Definition ISDOpcodes.h:188
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:198
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ 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:826
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:690
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:795
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:671
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:185
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ CTLS
Count leading redundant sign bits.
Definition ISDOpcodes.h:791
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:703
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:764
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:649
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:224
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:849
@ TargetConstantFP
Definition ISDOpcodes.h:180
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:810
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ TargetFrameIndex
Definition ISDOpcodes.h:187
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by IMM elements and retu...
Definition ISDOpcodes.h:653
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:898
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:887
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:726
@ LIFETIME_START
This corresponds to the llvm.lifetime.
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:977
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:804
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ ATOMIC_LOAD_UDEC_WRAP
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:925
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:179
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:505
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:738
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ GET_FPENV_MEM
Gets the current floating-point environment.
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:734
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:709
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) right by IMM elements and re...
Definition ISDOpcodes.h:656
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:304
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:680
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:958
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:698
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:920
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:996
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ 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:944
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:855
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ VECREDUCE_SEQ_FMUL
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:832
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ SET_FPENV_MEM
Sets the current floating point environment.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:721
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:338
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:186
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
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.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI 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...
LLVM_ABI 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...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI 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...
LLVM_ABI 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 matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI 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...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI 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,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI 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).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI 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< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
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()...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI 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)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
GenericUniformityInfo< SSAContext > UniformityInfo
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:344
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:237
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1757
LLVM_ABI 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:1737
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI 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:1613
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
LLVM_ABI 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:2530
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ABI 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.
LLVM_ABI bool isOneOrOneSplatFP(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant floating-point value, or a splatted vector of a constant float...
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1625
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2184
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI 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:632
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI 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:1595
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI 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:1537
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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:1744
LLVM_ABI 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 2008 maxNum semantics.
Definition APFloat.h:1580
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:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1611
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI 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:1751
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI 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...
LLVM_ABI 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:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI 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...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1561
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
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:539
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI 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:1883
constexpr unsigned BitWidth
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:719
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI 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:1945
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI 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:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI 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:1598
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1638
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
LLVM_ABI 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:373
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:180
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:761
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:781
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:778
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
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:395
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:512
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
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:121
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
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:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
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:181
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:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:256
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
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:328
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:142
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:308
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:453
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:304
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:258
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:245
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:277
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:164
KnownBits byteSwap() const
Definition KnownBits.h:522
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:292
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:526
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:236
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:175
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
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:324
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:228
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:314
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:183
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:199
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI 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:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:329
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static LLVM_ABI 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:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:283
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:222
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
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:170
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI 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 LLVM_ABI 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:106
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.
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.
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)