LLVM 23.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;
1575 // TODO: Use computeKnownBits instead of AssertZext.
1576 if (Op.getOpcode() == ISD::AssertZext &&
1577 cast<VTSDNode>(Op.getOperand(1))->getVT().bitsLE(VT))
1578 return Op;
1580 VT.getScalarSizeInBits());
1581 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1582}
1583
1585 SDValue EVL, const SDLoc &DL,
1586 EVT VT) {
1587 EVT OpVT = Op.getValueType();
1588 assert(VT.isInteger() && OpVT.isInteger() &&
1589 "Cannot getVPZeroExtendInReg FP types");
1590 assert(VT.isVector() && OpVT.isVector() &&
1591 "getVPZeroExtendInReg type and operand type should be vector!");
1593 "Vector element counts must match in getZeroExtendInReg");
1594 assert(VT.bitsLE(OpVT) && "Not extending!");
1595 if (OpVT == VT)
1596 return Op;
1598 VT.getScalarSizeInBits());
1599 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1600 EVL);
1601}
1602
1604 // Only unsigned pointer semantics are supported right now. In the future this
1605 // might delegate to TLI to check pointer signedness.
1606 return getZExtOrTrunc(Op, DL, VT);
1607}
1608
1610 // Only unsigned pointer semantics are supported right now. In the future this
1611 // might delegate to TLI to check pointer signedness.
1612 return getZeroExtendInReg(Op, DL, VT);
1613}
1614
1616 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1617}
1618
1619/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1621 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1622}
1623
1625 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1626 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1627}
1628
1630 SDValue Mask, SDValue EVL, EVT VT) {
1631 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1632 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1633}
1634
1636 SDValue Mask, SDValue EVL) {
1637 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1638}
1639
1641 SDValue Mask, SDValue EVL) {
1642 if (VT.bitsGT(Op.getValueType()))
1643 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1644 if (VT.bitsLT(Op.getValueType()))
1645 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1646 return Op;
1647}
1648
1650 EVT OpVT) {
1651 if (!V)
1652 return getConstant(0, DL, VT);
1653
1654 switch (TLI->getBooleanContents(OpVT)) {
1657 return getConstant(1, DL, VT);
1659 return getAllOnesConstant(DL, VT);
1660 }
1661 llvm_unreachable("Unexpected boolean content enum!");
1662}
1663
1665 bool isT, bool isO) {
1666 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1667 DL, VT, isT, isO);
1668}
1669
1671 bool isT, bool isO) {
1672 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1673}
1674
1676 EVT VT, bool isT, bool isO) {
1677 assert(VT.isInteger() && "Cannot create FP integer constant!");
1678
1679 EVT EltVT = VT.getScalarType();
1680 const ConstantInt *Elt = &Val;
1681
1682 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1683 // to-be-splatted scalar ConstantInt.
1684 if (isa<VectorType>(Elt->getType()))
1685 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1686
1687 // In some cases the vector type is legal but the element type is illegal and
1688 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1689 // inserted value (the type does not need to match the vector element type).
1690 // Any extra bits introduced will be truncated away.
1691 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1693 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1694 APInt NewVal;
1695 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1696 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1697 else
1698 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1699 Elt = ConstantInt::get(*getContext(), NewVal);
1700 }
1701 // In other cases the element type is illegal and needs to be expanded, for
1702 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1703 // the value into n parts and use a vector type with n-times the elements.
1704 // Then bitcast to the type requested.
1705 // Legalizing constants too early makes the DAGCombiner's job harder so we
1706 // only legalize if the DAG tells us we must produce legal types.
1707 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1708 TLI->getTypeAction(*getContext(), EltVT) ==
1710 const APInt &NewVal = Elt->getValue();
1711 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1712 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1713
1714 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1715 if (VT.isScalableVector() ||
1716 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1717 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1718 "Can only handle an even split!");
1719 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1720
1721 SmallVector<SDValue, 2> ScalarParts;
1722 for (unsigned i = 0; i != Parts; ++i)
1723 ScalarParts.push_back(getConstant(
1724 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1725 ViaEltVT, isT, isO));
1726
1727 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1728 }
1729
1730 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1731 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1732
1733 // Check the temporary vector is the correct size. If this fails then
1734 // getTypeToTransformTo() probably returned a type whose size (in bits)
1735 // isn't a power-of-2 factor of the requested type size.
1736 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1737
1738 SmallVector<SDValue, 2> EltParts;
1739 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1740 EltParts.push_back(getConstant(
1741 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1742 ViaEltVT, isT, isO));
1743
1744 // EltParts is currently in little endian order. If we actually want
1745 // big-endian order then reverse it now.
1746 if (getDataLayout().isBigEndian())
1747 std::reverse(EltParts.begin(), EltParts.end());
1748
1749 // The elements must be reversed when the element order is different
1750 // to the endianness of the elements (because the BITCAST is itself a
1751 // vector shuffle in this situation). However, we do not need any code to
1752 // perform this reversal because getConstant() is producing a vector
1753 // splat.
1754 // This situation occurs in MIPS MSA.
1755
1757 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1758 llvm::append_range(Ops, EltParts);
1759
1760 SDValue V =
1761 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1762 return V;
1763 }
1764
1765 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1766 "APInt size does not match type size!");
1767 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1768 SDVTList VTs = getVTList(EltVT);
1770 AddNodeIDNode(ID, Opc, VTs, {});
1771 ID.AddPointer(Elt);
1772 ID.AddBoolean(isO);
1773 void *IP = nullptr;
1774 SDNode *N = nullptr;
1775 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1776 if (!VT.isVector())
1777 return SDValue(N, 0);
1778
1779 if (!N) {
1780 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1781 CSEMap.InsertNode(N, IP);
1782 InsertNode(N);
1783 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1784 }
1785
1786 SDValue Result(N, 0);
1787 if (VT.isVector())
1788 Result = getSplat(VT, DL, Result);
1789 return Result;
1790}
1791
1793 bool isT, bool isO) {
1794 unsigned Size = VT.getScalarSizeInBits();
1795 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1796}
1797
1799 bool IsOpaque) {
1801 IsTarget, IsOpaque);
1802}
1803
1805 bool isTarget) {
1806 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1807}
1808
1810 const SDLoc &DL) {
1811 assert(VT.isInteger() && "Shift amount is not an integer type!");
1812 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1813 return getConstant(Val, DL, ShiftVT);
1814}
1815
1817 const SDLoc &DL) {
1818 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1819 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1820}
1821
1823 bool isTarget) {
1824 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1825}
1826
1828 bool isTarget) {
1829 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1830}
1831
1833 EVT VT, bool isTarget) {
1834 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1835
1836 EVT EltVT = VT.getScalarType();
1837 const ConstantFP *Elt = &V;
1838
1839 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1840 // the to-be-splatted scalar ConstantFP.
1841 if (isa<VectorType>(Elt->getType()))
1842 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1843
1844 // Do the map lookup using the actual bit pattern for the floating point
1845 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1846 // we don't have issues with SNANs.
1847 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1848 SDVTList VTs = getVTList(EltVT);
1850 AddNodeIDNode(ID, Opc, VTs, {});
1851 ID.AddPointer(Elt);
1852 void *IP = nullptr;
1853 SDNode *N = nullptr;
1854 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1855 if (!VT.isVector())
1856 return SDValue(N, 0);
1857
1858 if (!N) {
1859 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1860 CSEMap.InsertNode(N, IP);
1861 InsertNode(N);
1862 }
1863
1864 SDValue Result(N, 0);
1865 if (VT.isVector())
1866 Result = getSplat(VT, DL, Result);
1867 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1868 return Result;
1869}
1870
1872 bool isTarget) {
1873 EVT EltVT = VT.getScalarType();
1874 if (EltVT == MVT::f32)
1875 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1876 if (EltVT == MVT::f64)
1877 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1878 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1879 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1880 bool Ignored;
1881 APFloat APF = APFloat(Val);
1883 &Ignored);
1884 return getConstantFP(APF, DL, VT, isTarget);
1885 }
1886 llvm_unreachable("Unsupported type in getConstantFP");
1887}
1888
1890 EVT VT, int64_t Offset, bool isTargetGA,
1891 unsigned TargetFlags) {
1892 assert((TargetFlags == 0 || isTargetGA) &&
1893 "Cannot set target flags on target-independent globals");
1894
1895 // Truncate (with sign-extension) the offset value to the pointer size.
1897 if (BitWidth < 64)
1899
1900 unsigned Opc;
1901 if (GV->isThreadLocal())
1903 else
1905
1906 SDVTList VTs = getVTList(VT);
1908 AddNodeIDNode(ID, Opc, VTs, {});
1909 ID.AddPointer(GV);
1910 ID.AddInteger(Offset);
1911 ID.AddInteger(TargetFlags);
1912 void *IP = nullptr;
1913 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1914 return SDValue(E, 0);
1915
1916 auto *N = newSDNode<GlobalAddressSDNode>(
1917 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1918 CSEMap.InsertNode(N, IP);
1919 InsertNode(N);
1920 return SDValue(N, 0);
1921}
1922
1924 SDVTList VTs = getVTList(MVT::Untyped);
1927 ID.AddPointer(GV);
1928 void *IP = nullptr;
1929 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1930 return SDValue(E, 0);
1931
1932 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1933 CSEMap.InsertNode(N, IP);
1934 InsertNode(N);
1935 return SDValue(N, 0);
1936}
1937
1938SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1939 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1940 SDVTList VTs = getVTList(VT);
1942 AddNodeIDNode(ID, Opc, VTs, {});
1943 ID.AddInteger(FI);
1944 void *IP = nullptr;
1945 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1946 return SDValue(E, 0);
1947
1948 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1949 CSEMap.InsertNode(N, IP);
1950 InsertNode(N);
1951 return SDValue(N, 0);
1952}
1953
1954SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1955 unsigned TargetFlags) {
1956 assert((TargetFlags == 0 || isTarget) &&
1957 "Cannot set target flags on target-independent jump tables");
1958 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1959 SDVTList VTs = getVTList(VT);
1961 AddNodeIDNode(ID, Opc, VTs, {});
1962 ID.AddInteger(JTI);
1963 ID.AddInteger(TargetFlags);
1964 void *IP = nullptr;
1965 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1966 return SDValue(E, 0);
1967
1968 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1969 CSEMap.InsertNode(N, IP);
1970 InsertNode(N);
1971 return SDValue(N, 0);
1972}
1973
1975 const SDLoc &DL) {
1977 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
1978 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1979}
1980
1982 MaybeAlign Alignment, int Offset,
1983 bool isTarget, unsigned TargetFlags) {
1984 assert((TargetFlags == 0 || isTarget) &&
1985 "Cannot set target flags on target-independent globals");
1986 if (!Alignment)
1987 Alignment = shouldOptForSize()
1988 ? getDataLayout().getABITypeAlign(C->getType())
1989 : getDataLayout().getPrefTypeAlign(C->getType());
1990 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1991 SDVTList VTs = getVTList(VT);
1993 AddNodeIDNode(ID, Opc, VTs, {});
1994 ID.AddInteger(Alignment->value());
1995 ID.AddInteger(Offset);
1996 ID.AddPointer(C);
1997 ID.AddInteger(TargetFlags);
1998 void *IP = nullptr;
1999 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2000 return SDValue(E, 0);
2001
2002 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2003 TargetFlags);
2004 CSEMap.InsertNode(N, IP);
2005 InsertNode(N);
2006 SDValue V = SDValue(N, 0);
2007 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2008 return V;
2009}
2010
2012 MaybeAlign Alignment, int Offset,
2013 bool isTarget, unsigned TargetFlags) {
2014 assert((TargetFlags == 0 || isTarget) &&
2015 "Cannot set target flags on target-independent globals");
2016 if (!Alignment)
2017 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2018 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2019 SDVTList VTs = getVTList(VT);
2021 AddNodeIDNode(ID, Opc, VTs, {});
2022 ID.AddInteger(Alignment->value());
2023 ID.AddInteger(Offset);
2024 C->addSelectionDAGCSEId(ID);
2025 ID.AddInteger(TargetFlags);
2026 void *IP = nullptr;
2027 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2028 return SDValue(E, 0);
2029
2030 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2031 TargetFlags);
2032 CSEMap.InsertNode(N, IP);
2033 InsertNode(N);
2034 return SDValue(N, 0);
2035}
2036
2039 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2040 ID.AddPointer(MBB);
2041 void *IP = nullptr;
2042 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2043 return SDValue(E, 0);
2044
2045 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2046 CSEMap.InsertNode(N, IP);
2047 InsertNode(N);
2048 return SDValue(N, 0);
2049}
2050
2052 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2053 ValueTypeNodes.size())
2054 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2055
2056 SDNode *&N = VT.isExtended() ?
2057 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2058
2059 if (N) return SDValue(N, 0);
2060 N = newSDNode<VTSDNode>(VT);
2061 InsertNode(N);
2062 return SDValue(N, 0);
2063}
2064
2066 SDNode *&N = ExternalSymbols[Sym];
2067 if (N) return SDValue(N, 0);
2068 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2069 InsertNode(N);
2070 return SDValue(N, 0);
2071}
2072
2073SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2074 StringRef SymName = TLI->getLibcallImplName(Libcall);
2075 return getExternalSymbol(SymName.data(), VT);
2076}
2077
2079 SDNode *&N = MCSymbols[Sym];
2080 if (N)
2081 return SDValue(N, 0);
2082 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2083 InsertNode(N);
2084 return SDValue(N, 0);
2085}
2086
2088 unsigned TargetFlags) {
2089 SDNode *&N =
2090 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2091 if (N) return SDValue(N, 0);
2092 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2093 InsertNode(N);
2094 return SDValue(N, 0);
2095}
2096
2098 EVT VT, unsigned TargetFlags) {
2099 StringRef SymName = TLI->getLibcallImplName(Libcall);
2100 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2101}
2102
2104 if ((unsigned)Cond >= CondCodeNodes.size())
2105 CondCodeNodes.resize(Cond+1);
2106
2107 if (!CondCodeNodes[Cond]) {
2108 auto *N = newSDNode<CondCodeSDNode>(Cond);
2109 CondCodeNodes[Cond] = N;
2110 InsertNode(N);
2111 }
2112
2113 return SDValue(CondCodeNodes[Cond], 0);
2114}
2115
2117 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2118 "APInt size does not match type size!");
2119
2120 if (MulImm == 0)
2121 return getConstant(0, DL, VT);
2122
2123 const MachineFunction &MF = getMachineFunction();
2124 const Function &F = MF.getFunction();
2125 ConstantRange CR = getVScaleRange(&F, 64);
2126 if (const APInt *C = CR.getSingleElement())
2127 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2128
2129 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2130}
2131
2132/// \returns a value of type \p VT that represents the runtime value of \p
2133/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2134/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2135/// or TypeSize.
2136template <typename Ty>
2138 EVT VT, Ty Quantity) {
2139 if (Quantity.isScalable())
2140 return DAG.getVScale(
2141 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2142
2143 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2144}
2145
2147 ElementCount EC) {
2148 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2149}
2150
2152 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2153}
2154
2156 ElementCount EC) {
2157 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2158 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2159 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2160 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2161}
2162
2164 APInt One(ResVT.getScalarSizeInBits(), 1);
2165 return getStepVector(DL, ResVT, One);
2166}
2167
2169 const APInt &StepVal) {
2170 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2171 if (ResVT.isScalableVector())
2172 return getNode(
2173 ISD::STEP_VECTOR, DL, ResVT,
2174 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2175
2176 SmallVector<SDValue, 16> OpsStepConstants;
2177 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2178 OpsStepConstants.push_back(
2179 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2180 return getBuildVector(ResVT, DL, OpsStepConstants);
2181}
2182
2183/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2184/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2189
2191 SDValue N2, ArrayRef<int> Mask) {
2192 assert(VT.getVectorNumElements() == Mask.size() &&
2193 "Must have the same number of vector elements as mask elements!");
2194 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2195 "Invalid VECTOR_SHUFFLE");
2196
2197 // Canonicalize shuffle undef, undef -> undef
2198 if (N1.isUndef() && N2.isUndef())
2199 return getUNDEF(VT);
2200
2201 // Validate that all indices in Mask are within the range of the elements
2202 // input to the shuffle.
2203 int NElts = Mask.size();
2204 assert(llvm::all_of(Mask,
2205 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2206 "Index out of range");
2207
2208 // Copy the mask so we can do any needed cleanup.
2209 SmallVector<int, 8> MaskVec(Mask);
2210
2211 // Canonicalize shuffle v, v -> v, undef
2212 if (N1 == N2) {
2213 N2 = getUNDEF(VT);
2214 for (int i = 0; i != NElts; ++i)
2215 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2216 }
2217
2218 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2219 if (N1.isUndef())
2220 commuteShuffle(N1, N2, MaskVec);
2221
2222 if (TLI->hasVectorBlend()) {
2223 // If shuffling a splat, try to blend the splat instead. We do this here so
2224 // that even when this arises during lowering we don't have to re-handle it.
2225 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2226 BitVector UndefElements;
2227 SDValue Splat = BV->getSplatValue(&UndefElements);
2228 if (!Splat)
2229 return;
2230
2231 for (int i = 0; i < NElts; ++i) {
2232 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2233 continue;
2234
2235 // If this input comes from undef, mark it as such.
2236 if (UndefElements[MaskVec[i] - Offset]) {
2237 MaskVec[i] = -1;
2238 continue;
2239 }
2240
2241 // If we can blend a non-undef lane, use that instead.
2242 if (!UndefElements[i])
2243 MaskVec[i] = i + Offset;
2244 }
2245 };
2246 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2247 BlendSplat(N1BV, 0);
2248 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2249 BlendSplat(N2BV, NElts);
2250 }
2251
2252 // Canonicalize all index into lhs, -> shuffle lhs, undef
2253 // Canonicalize all index into rhs, -> shuffle rhs, undef
2254 bool AllLHS = true, AllRHS = true;
2255 bool N2Undef = N2.isUndef();
2256 for (int i = 0; i != NElts; ++i) {
2257 if (MaskVec[i] >= NElts) {
2258 if (N2Undef)
2259 MaskVec[i] = -1;
2260 else
2261 AllLHS = false;
2262 } else if (MaskVec[i] >= 0) {
2263 AllRHS = false;
2264 }
2265 }
2266 if (AllLHS && AllRHS)
2267 return getUNDEF(VT);
2268 if (AllLHS && !N2Undef)
2269 N2 = getUNDEF(VT);
2270 if (AllRHS) {
2271 N1 = getUNDEF(VT);
2272 commuteShuffle(N1, N2, MaskVec);
2273 }
2274 // Reset our undef status after accounting for the mask.
2275 N2Undef = N2.isUndef();
2276 // Re-check whether both sides ended up undef.
2277 if (N1.isUndef() && N2Undef)
2278 return getUNDEF(VT);
2279
2280 // If Identity shuffle return that node.
2281 bool Identity = true, AllSame = true;
2282 for (int i = 0; i != NElts; ++i) {
2283 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2284 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2285 }
2286 if (Identity && NElts)
2287 return N1;
2288
2289 // Shuffling a constant splat doesn't change the result.
2290 if (N2Undef) {
2291 SDValue V = N1;
2292
2293 // Look through any bitcasts. We check that these don't change the number
2294 // (and size) of elements and just changes their types.
2295 while (V.getOpcode() == ISD::BITCAST)
2296 V = V->getOperand(0);
2297
2298 // A splat should always show up as a build vector node.
2299 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2300 BitVector UndefElements;
2301 SDValue Splat = BV->getSplatValue(&UndefElements);
2302 // If this is a splat of an undef, shuffling it is also undef.
2303 if (Splat && Splat.isUndef())
2304 return getUNDEF(VT);
2305
2306 bool SameNumElts =
2307 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2308
2309 // We only have a splat which can skip shuffles if there is a splatted
2310 // value and no undef lanes rearranged by the shuffle.
2311 if (Splat && UndefElements.none()) {
2312 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2313 // number of elements match or the value splatted is a zero constant.
2314 if (SameNumElts || isNullConstant(Splat))
2315 return N1;
2316 }
2317
2318 // If the shuffle itself creates a splat, build the vector directly.
2319 if (AllSame && SameNumElts) {
2320 EVT BuildVT = BV->getValueType(0);
2321 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2322 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2323
2324 // We may have jumped through bitcasts, so the type of the
2325 // BUILD_VECTOR may not match the type of the shuffle.
2326 if (BuildVT != VT)
2327 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2328 return NewBV;
2329 }
2330 }
2331 }
2332
2333 SDVTList VTs = getVTList(VT);
2335 SDValue Ops[2] = { N1, N2 };
2337 for (int i = 0; i != NElts; ++i)
2338 ID.AddInteger(MaskVec[i]);
2339
2340 void* IP = nullptr;
2341 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2342 return SDValue(E, 0);
2343
2344 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2345 // SDNode doesn't have access to it. This memory will be "leaked" when
2346 // the node is deallocated, but recovered when the NodeAllocator is released.
2347 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2348 llvm::copy(MaskVec, MaskAlloc);
2349
2350 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2351 dl.getDebugLoc(), MaskAlloc);
2352 createOperands(N, Ops);
2353
2354 CSEMap.InsertNode(N, IP);
2355 InsertNode(N);
2356 SDValue V = SDValue(N, 0);
2357 NewSDValueDbgMsg(V, "Creating new node: ", this);
2358 return V;
2359}
2360
2362 EVT VT = SV.getValueType(0);
2363 SmallVector<int, 8> MaskVec(SV.getMask());
2365
2366 SDValue Op0 = SV.getOperand(0);
2367 SDValue Op1 = SV.getOperand(1);
2368 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2369}
2370
2372 SDVTList VTs = getVTList(VT);
2374 AddNodeIDNode(ID, ISD::Register, VTs, {});
2375 ID.AddInteger(Reg.id());
2376 void *IP = nullptr;
2377 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2378 return SDValue(E, 0);
2379
2380 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2381 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2382 CSEMap.InsertNode(N, IP);
2383 InsertNode(N);
2384 return SDValue(N, 0);
2385}
2386
2389 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2390 ID.AddPointer(RegMask);
2391 void *IP = nullptr;
2392 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2393 return SDValue(E, 0);
2394
2395 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2396 CSEMap.InsertNode(N, IP);
2397 InsertNode(N);
2398 return SDValue(N, 0);
2399}
2400
2402 MCSymbol *Label) {
2403 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2404}
2405
2406SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2407 SDValue Root, MCSymbol *Label) {
2409 SDValue Ops[] = { Root };
2410 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2411 ID.AddPointer(Label);
2412 void *IP = nullptr;
2413 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2414 return SDValue(E, 0);
2415
2416 auto *N =
2417 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2418 createOperands(N, Ops);
2419
2420 CSEMap.InsertNode(N, IP);
2421 InsertNode(N);
2422 return SDValue(N, 0);
2423}
2424
2426 int64_t Offset, bool isTarget,
2427 unsigned TargetFlags) {
2428 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2429 SDVTList VTs = getVTList(VT);
2430
2432 AddNodeIDNode(ID, Opc, VTs, {});
2433 ID.AddPointer(BA);
2434 ID.AddInteger(Offset);
2435 ID.AddInteger(TargetFlags);
2436 void *IP = nullptr;
2437 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2438 return SDValue(E, 0);
2439
2440 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2441 CSEMap.InsertNode(N, IP);
2442 InsertNode(N);
2443 return SDValue(N, 0);
2444}
2445
2448 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2449 ID.AddPointer(V);
2450
2451 void *IP = nullptr;
2452 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2453 return SDValue(E, 0);
2454
2455 auto *N = newSDNode<SrcValueSDNode>(V);
2456 CSEMap.InsertNode(N, IP);
2457 InsertNode(N);
2458 return SDValue(N, 0);
2459}
2460
2463 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2464 ID.AddPointer(MD);
2465
2466 void *IP = nullptr;
2467 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2468 return SDValue(E, 0);
2469
2470 auto *N = newSDNode<MDNodeSDNode>(MD);
2471 CSEMap.InsertNode(N, IP);
2472 InsertNode(N);
2473 return SDValue(N, 0);
2474}
2475
2477 if (VT == V.getValueType())
2478 return V;
2479
2480 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2481}
2482
2484 unsigned SrcAS, unsigned DestAS) {
2485 SDVTList VTs = getVTList(VT);
2486 SDValue Ops[] = {Ptr};
2489 ID.AddInteger(SrcAS);
2490 ID.AddInteger(DestAS);
2491
2492 void *IP = nullptr;
2493 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2494 return SDValue(E, 0);
2495
2496 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2497 VTs, SrcAS, DestAS);
2498 createOperands(N, Ops);
2499
2500 CSEMap.InsertNode(N, IP);
2501 InsertNode(N);
2502 return SDValue(N, 0);
2503}
2504
2506 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2507}
2508
2509/// getShiftAmountOperand - Return the specified value casted to
2510/// the target's desired shift amount type.
2512 EVT OpTy = Op.getValueType();
2513 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2514 if (OpTy == ShTy || OpTy.isVector()) return Op;
2515
2516 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2517}
2518
2520 SDLoc dl(Node);
2522 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2523 EVT VT = Node->getValueType(0);
2524 SDValue Tmp1 = Node->getOperand(0);
2525 SDValue Tmp2 = Node->getOperand(1);
2526 const MaybeAlign MA(Node->getConstantOperandVal(3));
2527
2528 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2529 Tmp2, MachinePointerInfo(V));
2530 SDValue VAList = VAListLoad;
2531
2532 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2533 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2534 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2535
2536 VAList = getNode(
2537 ISD::AND, dl, VAList.getValueType(), VAList,
2538 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2539 }
2540
2541 // Increment the pointer, VAList, to the next vaarg
2542 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2543 getConstant(getDataLayout().getTypeAllocSize(
2544 VT.getTypeForEVT(*getContext())),
2545 dl, VAList.getValueType()));
2546 // Store the incremented VAList to the legalized pointer
2547 Tmp1 =
2548 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2549 // Load the actual argument out of the pointer VAList
2550 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2551}
2552
2554 SDLoc dl(Node);
2556 // This defaults to loading a pointer from the input and storing it to the
2557 // output, returning the chain.
2558 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2559 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2560 SDValue Tmp1 =
2561 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2562 Node->getOperand(2), MachinePointerInfo(VS));
2563 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2564 MachinePointerInfo(VD));
2565}
2566
2568 const DataLayout &DL = getDataLayout();
2569 Type *Ty = VT.getTypeForEVT(*getContext());
2570 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2571
2572 if (TLI->isTypeLegal(VT) || !VT.isVector())
2573 return RedAlign;
2574
2575 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2576 const Align StackAlign = TFI->getStackAlign();
2577
2578 // See if we can choose a smaller ABI alignment in cases where it's an
2579 // illegal vector type that will get broken down.
2580 if (RedAlign > StackAlign) {
2581 EVT IntermediateVT;
2582 MVT RegisterVT;
2583 unsigned NumIntermediates;
2584 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2585 NumIntermediates, RegisterVT);
2586 Ty = IntermediateVT.getTypeForEVT(*getContext());
2587 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2588 if (RedAlign2 < RedAlign)
2589 RedAlign = RedAlign2;
2590
2591 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2592 // If the stack is not realignable, the alignment should be limited to the
2593 // StackAlignment
2594 RedAlign = std::min(RedAlign, StackAlign);
2595 }
2596
2597 return RedAlign;
2598}
2599
2601 MachineFrameInfo &MFI = MF->getFrameInfo();
2602 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2603 int StackID = 0;
2604 if (Bytes.isScalable())
2605 StackID = TFI->getStackIDForScalableVectors();
2606 // The stack id gives an indication of whether the object is scalable or
2607 // not, so it's safe to pass in the minimum size here.
2608 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2609 false, nullptr, StackID);
2610 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2611}
2612
2614 Type *Ty = VT.getTypeForEVT(*getContext());
2615 Align StackAlign =
2616 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2617 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2618}
2619
2621 TypeSize VT1Size = VT1.getStoreSize();
2622 TypeSize VT2Size = VT2.getStoreSize();
2623 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2624 "Don't know how to choose the maximum size when creating a stack "
2625 "temporary");
2626 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2627 ? VT1Size
2628 : VT2Size;
2629
2630 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2631 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2632 const DataLayout &DL = getDataLayout();
2633 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2634 return CreateStackTemporary(Bytes, Align);
2635}
2636
2638 ISD::CondCode Cond, const SDLoc &dl) {
2639 EVT OpVT = N1.getValueType();
2640
2641 auto GetUndefBooleanConstant = [&]() {
2642 if (VT.getScalarType() == MVT::i1 ||
2643 TLI->getBooleanContents(OpVT) ==
2645 return getUNDEF(VT);
2646 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2647 // so we cannot use getUNDEF(). Return zero instead.
2648 return getConstant(0, dl, VT);
2649 };
2650
2651 // These setcc operations always fold.
2652 switch (Cond) {
2653 default: break;
2654 case ISD::SETFALSE:
2655 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2656 case ISD::SETTRUE:
2657 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2658
2659 case ISD::SETOEQ:
2660 case ISD::SETOGT:
2661 case ISD::SETOGE:
2662 case ISD::SETOLT:
2663 case ISD::SETOLE:
2664 case ISD::SETONE:
2665 case ISD::SETO:
2666 case ISD::SETUO:
2667 case ISD::SETUEQ:
2668 case ISD::SETUNE:
2669 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2670 break;
2671 }
2672
2673 if (OpVT.isInteger()) {
2674 // For EQ and NE, we can always pick a value for the undef to make the
2675 // predicate pass or fail, so we can return undef.
2676 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2677 // icmp eq/ne X, undef -> undef.
2678 if ((N1.isUndef() || N2.isUndef()) &&
2679 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2680 return GetUndefBooleanConstant();
2681
2682 // If both operands are undef, we can return undef for int comparison.
2683 // icmp undef, undef -> undef.
2684 if (N1.isUndef() && N2.isUndef())
2685 return GetUndefBooleanConstant();
2686
2687 // icmp X, X -> true/false
2688 // icmp X, undef -> true/false because undef could be X.
2689 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2690 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2691 }
2692
2694 const APInt &C2 = N2C->getAPIntValue();
2696 const APInt &C1 = N1C->getAPIntValue();
2697
2699 dl, VT, OpVT);
2700 }
2701 }
2702
2703 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2704 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2705
2706 if (N1CFP && N2CFP) {
2707 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2708 switch (Cond) {
2709 default: break;
2710 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2711 return GetUndefBooleanConstant();
2712 [[fallthrough]];
2713 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2714 OpVT);
2715 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2716 return GetUndefBooleanConstant();
2717 [[fallthrough]];
2719 R==APFloat::cmpLessThan, dl, VT,
2720 OpVT);
2721 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2722 return GetUndefBooleanConstant();
2723 [[fallthrough]];
2724 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2725 OpVT);
2726 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2727 return GetUndefBooleanConstant();
2728 [[fallthrough]];
2730 VT, OpVT);
2731 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2732 return GetUndefBooleanConstant();
2733 [[fallthrough]];
2735 R==APFloat::cmpEqual, dl, VT,
2736 OpVT);
2737 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2738 return GetUndefBooleanConstant();
2739 [[fallthrough]];
2741 R==APFloat::cmpEqual, dl, VT, OpVT);
2742 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2743 OpVT);
2744 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2745 OpVT);
2747 R==APFloat::cmpEqual, dl, VT,
2748 OpVT);
2749 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2750 OpVT);
2752 R==APFloat::cmpLessThan, dl, VT,
2753 OpVT);
2755 R==APFloat::cmpUnordered, dl, VT,
2756 OpVT);
2758 VT, OpVT);
2759 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2760 OpVT);
2761 }
2762 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2763 // Ensure that the constant occurs on the RHS.
2765 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2766 return SDValue();
2767 return getSetCC(dl, VT, N2, N1, SwappedCond);
2768 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2769 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2770 // If an operand is known to be a nan (or undef that could be a nan), we can
2771 // fold it.
2772 // Choosing NaN for the undef will always make unordered comparison succeed
2773 // and ordered comparison fails.
2774 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2775 switch (ISD::getUnorderedFlavor(Cond)) {
2776 default:
2777 llvm_unreachable("Unknown flavor!");
2778 case 0: // Known false.
2779 return getBoolConstant(false, dl, VT, OpVT);
2780 case 1: // Known true.
2781 return getBoolConstant(true, dl, VT, OpVT);
2782 case 2: // Undefined.
2783 return GetUndefBooleanConstant();
2784 }
2785 }
2786
2787 // Could not fold it.
2788 return SDValue();
2789}
2790
2791/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2792/// use this predicate to simplify operations downstream.
2794 unsigned BitWidth = Op.getScalarValueSizeInBits();
2796}
2797
2798// TODO: Should have argument to specify if sign bit of nan is ignorable.
2800 if (Depth >= MaxRecursionDepth)
2801 return false; // Limit search depth.
2802
2803 unsigned Opc = Op.getOpcode();
2804 switch (Opc) {
2805 case ISD::FABS:
2806 return true;
2807 case ISD::AssertNoFPClass: {
2808 FPClassTest NoFPClass =
2809 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2810
2811 const FPClassTest TestMask = fcNan | fcNegative;
2812 return (NoFPClass & TestMask) == TestMask;
2813 }
2814 case ISD::ARITH_FENCE:
2815 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2816 case ISD::FEXP:
2817 case ISD::FEXP2:
2818 case ISD::FEXP10:
2819 return Op->getFlags().hasNoNaNs();
2820 case ISD::FMINNUM:
2821 case ISD::FMINNUM_IEEE:
2822 case ISD::FMINIMUM:
2823 case ISD::FMINIMUMNUM:
2824 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2825 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2826 case ISD::FMAXNUM:
2827 case ISD::FMAXNUM_IEEE:
2828 case ISD::FMAXIMUM:
2829 case ISD::FMAXIMUMNUM:
2830 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2831 // is sufficient.
2832 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2833 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2834 default:
2835 return false;
2836 }
2837
2838 llvm_unreachable("covered opcode switch");
2839}
2840
2841/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2842/// this predicate to simplify operations downstream. Mask is known to be zero
2843/// for bits that V cannot have.
2845 unsigned Depth) const {
2846 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2847}
2848
2849/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2850/// DemandedElts. We use this predicate to simplify operations downstream.
2851/// Mask is known to be zero for bits that V cannot have.
2853 const APInt &DemandedElts,
2854 unsigned Depth) const {
2855 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2856}
2857
2858/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2859/// DemandedElts. We use this predicate to simplify operations downstream.
2861 unsigned Depth /* = 0 */) const {
2862 return computeKnownBits(V, DemandedElts, Depth).isZero();
2863}
2864
2865/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2867 unsigned Depth) const {
2868 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2869}
2870
2872 const APInt &DemandedElts,
2873 unsigned Depth) const {
2874 EVT VT = Op.getValueType();
2875 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2876
2877 unsigned NumElts = VT.getVectorNumElements();
2878 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2879
2880 APInt KnownZeroElements = APInt::getZero(NumElts);
2881 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2882 if (!DemandedElts[EltIdx])
2883 continue; // Don't query elements that are not demanded.
2884 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2885 if (MaskedVectorIsZero(Op, Mask, Depth))
2886 KnownZeroElements.setBit(EltIdx);
2887 }
2888 return KnownZeroElements;
2889}
2890
2891/// isSplatValue - Return true if the vector V has the same value
2892/// across all DemandedElts. For scalable vectors, we don't know the
2893/// number of lanes at compile time. Instead, we use a 1 bit APInt
2894/// to represent a conservative value for all lanes; that is, that
2895/// one bit value is implicitly splatted across all lanes.
2896bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2897 APInt &UndefElts, unsigned Depth) const {
2898 unsigned Opcode = V.getOpcode();
2899 EVT VT = V.getValueType();
2900 assert(VT.isVector() && "Vector type expected");
2901 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2902 "scalable demanded bits are ignored");
2903
2904 if (!DemandedElts)
2905 return false; // No demanded elts, better to assume we don't know anything.
2906
2907 if (Depth >= MaxRecursionDepth)
2908 return false; // Limit search depth.
2909
2910 // Deal with some common cases here that work for both fixed and scalable
2911 // vector types.
2912 switch (Opcode) {
2913 case ISD::SPLAT_VECTOR:
2914 UndefElts = V.getOperand(0).isUndef()
2915 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2916 : APInt(DemandedElts.getBitWidth(), 0);
2917 return true;
2918 case ISD::ADD:
2919 case ISD::SUB:
2920 case ISD::AND:
2921 case ISD::XOR:
2922 case ISD::OR: {
2923 APInt UndefLHS, UndefRHS;
2924 SDValue LHS = V.getOperand(0);
2925 SDValue RHS = V.getOperand(1);
2926 // Only recognize splats with the same demanded undef elements for both
2927 // operands, otherwise we might fail to handle binop-specific undef
2928 // handling.
2929 // e.g. (and undef, 0) -> 0 etc.
2930 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2931 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2932 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2933 UndefElts = UndefLHS | UndefRHS;
2934 return true;
2935 }
2936 return false;
2937 }
2938 case ISD::ABS:
2939 case ISD::TRUNCATE:
2940 case ISD::SIGN_EXTEND:
2941 case ISD::ZERO_EXTEND:
2942 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2943 default:
2944 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2945 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2946 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2947 Depth);
2948 break;
2949 }
2950
2951 // We don't support other cases than those above for scalable vectors at
2952 // the moment.
2953 if (VT.isScalableVector())
2954 return false;
2955
2956 unsigned NumElts = VT.getVectorNumElements();
2957 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2958 UndefElts = APInt::getZero(NumElts);
2959
2960 switch (Opcode) {
2961 case ISD::BUILD_VECTOR: {
2962 SDValue Scl;
2963 for (unsigned i = 0; i != NumElts; ++i) {
2964 SDValue Op = V.getOperand(i);
2965 if (Op.isUndef()) {
2966 UndefElts.setBit(i);
2967 continue;
2968 }
2969 if (!DemandedElts[i])
2970 continue;
2971 if (Scl && Scl != Op)
2972 return false;
2973 Scl = Op;
2974 }
2975 return true;
2976 }
2977 case ISD::VECTOR_SHUFFLE: {
2978 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2979 APInt DemandedLHS = APInt::getZero(NumElts);
2980 APInt DemandedRHS = APInt::getZero(NumElts);
2981 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2982 for (int i = 0; i != (int)NumElts; ++i) {
2983 int M = Mask[i];
2984 if (M < 0) {
2985 UndefElts.setBit(i);
2986 continue;
2987 }
2988 if (!DemandedElts[i])
2989 continue;
2990 if (M < (int)NumElts)
2991 DemandedLHS.setBit(M);
2992 else
2993 DemandedRHS.setBit(M - NumElts);
2994 }
2995
2996 // If we aren't demanding either op, assume there's no splat.
2997 // If we are demanding both ops, assume there's no splat.
2998 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2999 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3000 return false;
3001
3002 // See if the demanded elts of the source op is a splat or we only demand
3003 // one element, which should always be a splat.
3004 // TODO: Handle source ops splats with undefs.
3005 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3006 APInt SrcUndefs;
3007 return (SrcElts.popcount() == 1) ||
3008 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3009 (SrcElts & SrcUndefs).isZero());
3010 };
3011 if (!DemandedLHS.isZero())
3012 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3013 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3014 }
3016 // Offset the demanded elts by the subvector index.
3017 SDValue Src = V.getOperand(0);
3018 // We don't support scalable vectors at the moment.
3019 if (Src.getValueType().isScalableVector())
3020 return false;
3021 uint64_t Idx = V.getConstantOperandVal(1);
3022 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3023 APInt UndefSrcElts;
3024 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3025 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3026 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3027 return true;
3028 }
3029 break;
3030 }
3034 // Widen the demanded elts by the src element count.
3035 SDValue Src = V.getOperand(0);
3036 // We don't support scalable vectors at the moment.
3037 if (Src.getValueType().isScalableVector())
3038 return false;
3039 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3040 APInt UndefSrcElts;
3041 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3042 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3043 UndefElts = UndefSrcElts.trunc(NumElts);
3044 return true;
3045 }
3046 break;
3047 }
3048 case ISD::BITCAST: {
3049 SDValue Src = V.getOperand(0);
3050 EVT SrcVT = Src.getValueType();
3051 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3052 unsigned BitWidth = VT.getScalarSizeInBits();
3053
3054 // Ignore bitcasts from unsupported types.
3055 // TODO: Add fp support?
3056 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3057 break;
3058
3059 // Bitcast 'small element' vector to 'large element' vector.
3060 if ((BitWidth % SrcBitWidth) == 0) {
3061 // See if each sub element is a splat.
3062 unsigned Scale = BitWidth / SrcBitWidth;
3063 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3064 APInt ScaledDemandedElts =
3065 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3066 for (unsigned I = 0; I != Scale; ++I) {
3067 APInt SubUndefElts;
3068 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3069 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3070 SubDemandedElts &= ScaledDemandedElts;
3071 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3072 return false;
3073 // TODO: Add support for merging sub undef elements.
3074 if (!SubUndefElts.isZero())
3075 return false;
3076 }
3077 return true;
3078 }
3079 break;
3080 }
3081 }
3082
3083 return false;
3084}
3085
3086/// Helper wrapper to main isSplatValue function.
3087bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3088 EVT VT = V.getValueType();
3089 assert(VT.isVector() && "Vector type expected");
3090
3091 APInt UndefElts;
3092 // Since the number of lanes in a scalable vector is unknown at compile time,
3093 // we track one bit which is implicitly broadcast to all lanes. This means
3094 // that all lanes in a scalable vector are considered demanded.
3095 APInt DemandedElts
3097 return isSplatValue(V, DemandedElts, UndefElts) &&
3098 (AllowUndefs || !UndefElts);
3099}
3100
3103
3104 EVT VT = V.getValueType();
3105 unsigned Opcode = V.getOpcode();
3106 switch (Opcode) {
3107 default: {
3108 APInt UndefElts;
3109 // Since the number of lanes in a scalable vector is unknown at compile time,
3110 // we track one bit which is implicitly broadcast to all lanes. This means
3111 // that all lanes in a scalable vector are considered demanded.
3112 APInt DemandedElts
3114
3115 if (isSplatValue(V, DemandedElts, UndefElts)) {
3116 if (VT.isScalableVector()) {
3117 // DemandedElts and UndefElts are ignored for scalable vectors, since
3118 // the only supported cases are SPLAT_VECTOR nodes.
3119 SplatIdx = 0;
3120 } else {
3121 // Handle case where all demanded elements are UNDEF.
3122 if (DemandedElts.isSubsetOf(UndefElts)) {
3123 SplatIdx = 0;
3124 return getUNDEF(VT);
3125 }
3126 SplatIdx = (UndefElts & DemandedElts).countr_one();
3127 }
3128 return V;
3129 }
3130 break;
3131 }
3132 case ISD::SPLAT_VECTOR:
3133 SplatIdx = 0;
3134 return V;
3135 case ISD::VECTOR_SHUFFLE: {
3136 assert(!VT.isScalableVector());
3137 // Check if this is a shuffle node doing a splat.
3138 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3139 // getTargetVShiftNode currently struggles without the splat source.
3140 auto *SVN = cast<ShuffleVectorSDNode>(V);
3141 if (!SVN->isSplat())
3142 break;
3143 int Idx = SVN->getSplatIndex();
3144 int NumElts = V.getValueType().getVectorNumElements();
3145 SplatIdx = Idx % NumElts;
3146 return V.getOperand(Idx / NumElts);
3147 }
3148 }
3149
3150 return SDValue();
3151}
3152
3154 int SplatIdx;
3155 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3156 EVT SVT = SrcVector.getValueType().getScalarType();
3157 EVT LegalSVT = SVT;
3158 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3159 if (!SVT.isInteger())
3160 return SDValue();
3161 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3162 if (LegalSVT.bitsLT(SVT))
3163 return SDValue();
3164 }
3165 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3166 }
3167 return SDValue();
3168}
3169
3170std::optional<ConstantRange>
3172 unsigned Depth) const {
3173 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3174 V.getOpcode() == ISD::SRA) &&
3175 "Unknown shift node");
3176 // Shifting more than the bitwidth is not valid.
3177 unsigned BitWidth = V.getScalarValueSizeInBits();
3178
3179 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3180 const APInt &ShAmt = Cst->getAPIntValue();
3181 if (ShAmt.uge(BitWidth))
3182 return std::nullopt;
3183 return ConstantRange(ShAmt);
3184 }
3185
3186 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3187 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3188 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3189 if (!DemandedElts[i])
3190 continue;
3191 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3192 if (!SA) {
3193 MinAmt = MaxAmt = nullptr;
3194 break;
3195 }
3196 const APInt &ShAmt = SA->getAPIntValue();
3197 if (ShAmt.uge(BitWidth))
3198 return std::nullopt;
3199 if (!MinAmt || MinAmt->ugt(ShAmt))
3200 MinAmt = &ShAmt;
3201 if (!MaxAmt || MaxAmt->ult(ShAmt))
3202 MaxAmt = &ShAmt;
3203 }
3204 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3205 "Failed to find matching min/max shift amounts");
3206 if (MinAmt && MaxAmt)
3207 return ConstantRange(*MinAmt, *MaxAmt + 1);
3208 }
3209
3210 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3211 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3212 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3213 if (KnownAmt.getMaxValue().ult(BitWidth))
3214 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3215
3216 return std::nullopt;
3217}
3218
3219std::optional<unsigned>
3221 unsigned Depth) const {
3222 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3223 V.getOpcode() == ISD::SRA) &&
3224 "Unknown shift node");
3225 if (std::optional<ConstantRange> AmtRange =
3226 getValidShiftAmountRange(V, DemandedElts, Depth))
3227 if (const APInt *ShAmt = AmtRange->getSingleElement())
3228 return ShAmt->getZExtValue();
3229 return std::nullopt;
3230}
3231
3232std::optional<unsigned>
3234 EVT VT = V.getValueType();
3235 APInt DemandedElts = VT.isFixedLengthVector()
3237 : APInt(1, 1);
3238 return getValidShiftAmount(V, DemandedElts, Depth);
3239}
3240
3241std::optional<unsigned>
3243 unsigned Depth) const {
3244 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3245 V.getOpcode() == ISD::SRA) &&
3246 "Unknown shift node");
3247 if (std::optional<ConstantRange> AmtRange =
3248 getValidShiftAmountRange(V, DemandedElts, Depth))
3249 return AmtRange->getUnsignedMin().getZExtValue();
3250 return std::nullopt;
3251}
3252
3253std::optional<unsigned>
3255 EVT VT = V.getValueType();
3256 APInt DemandedElts = VT.isFixedLengthVector()
3258 : APInt(1, 1);
3259 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3260}
3261
3262std::optional<unsigned>
3264 unsigned Depth) const {
3265 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3266 V.getOpcode() == ISD::SRA) &&
3267 "Unknown shift node");
3268 if (std::optional<ConstantRange> AmtRange =
3269 getValidShiftAmountRange(V, DemandedElts, Depth))
3270 return AmtRange->getUnsignedMax().getZExtValue();
3271 return std::nullopt;
3272}
3273
3274std::optional<unsigned>
3276 EVT VT = V.getValueType();
3277 APInt DemandedElts = VT.isFixedLengthVector()
3279 : APInt(1, 1);
3280 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3281}
3282
3283/// Determine which bits of Op are known to be either zero or one and return
3284/// them in Known. For vectors, the known bits are those that are shared by
3285/// every vector element.
3287 EVT VT = Op.getValueType();
3288
3289 // Since the number of lanes in a scalable vector is unknown at compile time,
3290 // we track one bit which is implicitly broadcast to all lanes. This means
3291 // that all lanes in a scalable vector are considered demanded.
3292 APInt DemandedElts = VT.isFixedLengthVector()
3294 : APInt(1, 1);
3295 return computeKnownBits(Op, DemandedElts, Depth);
3296}
3297
3298/// Determine which bits of Op are known to be either zero or one and return
3299/// them in Known. The DemandedElts argument allows us to only collect the known
3300/// bits that are shared by the requested vector elements.
3302 unsigned Depth) const {
3303 unsigned BitWidth = Op.getScalarValueSizeInBits();
3304
3305 KnownBits Known(BitWidth); // Don't know anything.
3306
3307 if (auto OptAPInt = Op->bitcastToAPInt()) {
3308 // We know all of the bits for a constant!
3309 return KnownBits::makeConstant(*std::move(OptAPInt));
3310 }
3311
3312 if (Depth >= MaxRecursionDepth)
3313 return Known; // Limit search depth.
3314
3315 KnownBits Known2;
3316 unsigned NumElts = DemandedElts.getBitWidth();
3317 assert((!Op.getValueType().isFixedLengthVector() ||
3318 NumElts == Op.getValueType().getVectorNumElements()) &&
3319 "Unexpected vector size");
3320
3321 if (!DemandedElts)
3322 return Known; // No demanded elts, better to assume we don't know anything.
3323
3324 unsigned Opcode = Op.getOpcode();
3325 switch (Opcode) {
3326 case ISD::MERGE_VALUES:
3327 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3328 Depth + 1);
3329 case ISD::SPLAT_VECTOR: {
3330 SDValue SrcOp = Op.getOperand(0);
3331 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3332 "Expected SPLAT_VECTOR implicit truncation");
3333 // Implicitly truncate the bits to match the official semantics of
3334 // SPLAT_VECTOR.
3335 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3336 break;
3337 }
3339 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3340 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3341 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3342 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3343 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3344 }
3345 break;
3346 }
3347 case ISD::STEP_VECTOR: {
3348 const APInt &Step = Op.getConstantOperandAPInt(0);
3349
3350 if (Step.isPowerOf2())
3351 Known.Zero.setLowBits(Step.logBase2());
3352
3354
3355 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3356 break;
3357 const APInt MinNumElts =
3358 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3359
3360 bool Overflow;
3361 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3363 .umul_ov(MinNumElts, Overflow);
3364 if (Overflow)
3365 break;
3366
3367 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3368 if (Overflow)
3369 break;
3370
3371 Known.Zero.setHighBits(MaxValue.countl_zero());
3372 break;
3373 }
3374 case ISD::BUILD_VECTOR:
3375 assert(!Op.getValueType().isScalableVector());
3376 // Collect the known bits that are shared by every demanded vector element.
3377 Known.setAllConflict();
3378 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3379 if (!DemandedElts[i])
3380 continue;
3381
3382 SDValue SrcOp = Op.getOperand(i);
3383 Known2 = computeKnownBits(SrcOp, Depth + 1);
3384
3385 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3386 if (SrcOp.getValueSizeInBits() != BitWidth) {
3387 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3388 "Expected BUILD_VECTOR implicit truncation");
3389 Known2 = Known2.trunc(BitWidth);
3390 }
3391
3392 // Known bits are the values that are shared by every demanded element.
3393 Known = Known.intersectWith(Known2);
3394
3395 // If we don't know any bits, early out.
3396 if (Known.isUnknown())
3397 break;
3398 }
3399 break;
3400 case ISD::VECTOR_COMPRESS: {
3401 SDValue Vec = Op.getOperand(0);
3402 SDValue PassThru = Op.getOperand(2);
3403 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3404 // If we don't know any bits, early out.
3405 if (Known.isUnknown())
3406 break;
3407 Known2 = computeKnownBits(Vec, Depth + 1);
3408 Known = Known.intersectWith(Known2);
3409 break;
3410 }
3411 case ISD::VECTOR_SHUFFLE: {
3412 assert(!Op.getValueType().isScalableVector());
3413 // Collect the known bits that are shared by every vector element referenced
3414 // by the shuffle.
3415 APInt DemandedLHS, DemandedRHS;
3417 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3418 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3419 DemandedLHS, DemandedRHS))
3420 break;
3421
3422 // Known bits are the values that are shared by every demanded element.
3423 Known.setAllConflict();
3424 if (!!DemandedLHS) {
3425 SDValue LHS = Op.getOperand(0);
3426 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3427 Known = Known.intersectWith(Known2);
3428 }
3429 // If we don't know any bits, early out.
3430 if (Known.isUnknown())
3431 break;
3432 if (!!DemandedRHS) {
3433 SDValue RHS = Op.getOperand(1);
3434 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3435 Known = Known.intersectWith(Known2);
3436 }
3437 break;
3438 }
3439 case ISD::VSCALE: {
3441 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3442 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3443 break;
3444 }
3445 case ISD::CONCAT_VECTORS: {
3446 if (Op.getValueType().isScalableVector())
3447 break;
3448 // Split DemandedElts and test each of the demanded subvectors.
3449 Known.setAllConflict();
3450 EVT SubVectorVT = Op.getOperand(0).getValueType();
3451 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3452 unsigned NumSubVectors = Op.getNumOperands();
3453 for (unsigned i = 0; i != NumSubVectors; ++i) {
3454 APInt DemandedSub =
3455 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3456 if (!!DemandedSub) {
3457 SDValue Sub = Op.getOperand(i);
3458 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3459 Known = Known.intersectWith(Known2);
3460 }
3461 // If we don't know any bits, early out.
3462 if (Known.isUnknown())
3463 break;
3464 }
3465 break;
3466 }
3467 case ISD::INSERT_SUBVECTOR: {
3468 if (Op.getValueType().isScalableVector())
3469 break;
3470 // Demand any elements from the subvector and the remainder from the src its
3471 // inserted into.
3472 SDValue Src = Op.getOperand(0);
3473 SDValue Sub = Op.getOperand(1);
3474 uint64_t Idx = Op.getConstantOperandVal(2);
3475 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3476 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3477 APInt DemandedSrcElts = DemandedElts;
3478 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3479
3480 Known.setAllConflict();
3481 if (!!DemandedSubElts) {
3482 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3483 if (Known.isUnknown())
3484 break; // early-out.
3485 }
3486 if (!!DemandedSrcElts) {
3487 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3488 Known = Known.intersectWith(Known2);
3489 }
3490 break;
3491 }
3493 // Offset the demanded elts by the subvector index.
3494 SDValue Src = Op.getOperand(0);
3495 // Bail until we can represent demanded elements for scalable vectors.
3496 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3497 break;
3498 uint64_t Idx = Op.getConstantOperandVal(1);
3499 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3500 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3501 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3502 break;
3503 }
3504 case ISD::SCALAR_TO_VECTOR: {
3505 if (Op.getValueType().isScalableVector())
3506 break;
3507 // We know about scalar_to_vector as much as we know about it source,
3508 // which becomes the first element of otherwise unknown vector.
3509 if (DemandedElts != 1)
3510 break;
3511
3512 SDValue N0 = Op.getOperand(0);
3513 Known = computeKnownBits(N0, Depth + 1);
3514 if (N0.getValueSizeInBits() != BitWidth)
3515 Known = Known.trunc(BitWidth);
3516
3517 break;
3518 }
3519 case ISD::BITCAST: {
3520 if (Op.getValueType().isScalableVector())
3521 break;
3522
3523 SDValue N0 = Op.getOperand(0);
3524 EVT SubVT = N0.getValueType();
3525 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3526
3527 // Ignore bitcasts from unsupported types.
3528 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3529 break;
3530
3531 // Fast handling of 'identity' bitcasts.
3532 if (BitWidth == SubBitWidth) {
3533 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3534 break;
3535 }
3536
3537 bool IsLE = getDataLayout().isLittleEndian();
3538
3539 // Bitcast 'small element' vector to 'large element' scalar/vector.
3540 if ((BitWidth % SubBitWidth) == 0) {
3541 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3542
3543 // Collect known bits for the (larger) output by collecting the known
3544 // bits from each set of sub elements and shift these into place.
3545 // We need to separately call computeKnownBits for each set of
3546 // sub elements as the knownbits for each is likely to be different.
3547 unsigned SubScale = BitWidth / SubBitWidth;
3548 APInt SubDemandedElts(NumElts * SubScale, 0);
3549 for (unsigned i = 0; i != NumElts; ++i)
3550 if (DemandedElts[i])
3551 SubDemandedElts.setBit(i * SubScale);
3552
3553 for (unsigned i = 0; i != SubScale; ++i) {
3554 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3555 Depth + 1);
3556 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3557 Known.insertBits(Known2, SubBitWidth * Shifts);
3558 }
3559 }
3560
3561 // Bitcast 'large element' scalar/vector to 'small element' vector.
3562 if ((SubBitWidth % BitWidth) == 0) {
3563 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3564
3565 // Collect known bits for the (smaller) output by collecting the known
3566 // bits from the overlapping larger input elements and extracting the
3567 // sub sections we actually care about.
3568 unsigned SubScale = SubBitWidth / BitWidth;
3569 APInt SubDemandedElts =
3570 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3571 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3572
3573 Known.setAllConflict();
3574 for (unsigned i = 0; i != NumElts; ++i)
3575 if (DemandedElts[i]) {
3576 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3577 unsigned Offset = (Shifts % SubScale) * BitWidth;
3578 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3579 // If we don't know any bits, early out.
3580 if (Known.isUnknown())
3581 break;
3582 }
3583 }
3584 break;
3585 }
3586 case ISD::AND:
3587 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3588 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3589
3590 Known &= Known2;
3591 break;
3592 case ISD::OR:
3593 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3594 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3595
3596 Known |= Known2;
3597 break;
3598 case ISD::XOR:
3599 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3600 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3601
3602 Known ^= Known2;
3603 break;
3604 case ISD::MUL: {
3605 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3606 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3607 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3608 // TODO: SelfMultiply can be poison, but not undef.
3609 if (SelfMultiply)
3610 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3611 Op.getOperand(0), DemandedElts, false, Depth + 1);
3612 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3613
3614 // If the multiplication is known not to overflow, the product of a number
3615 // with itself is non-negative. Only do this if we didn't already computed
3616 // the opposite value for the sign bit.
3617 if (Op->getFlags().hasNoSignedWrap() &&
3618 Op.getOperand(0) == Op.getOperand(1) &&
3619 !Known.isNegative())
3620 Known.makeNonNegative();
3621 break;
3622 }
3623 case ISD::MULHU: {
3624 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3625 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3626 Known = KnownBits::mulhu(Known, Known2);
3627 break;
3628 }
3629 case ISD::MULHS: {
3630 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3631 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3632 Known = KnownBits::mulhs(Known, Known2);
3633 break;
3634 }
3635 case ISD::ABDU: {
3636 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3637 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3638 Known = KnownBits::abdu(Known, Known2);
3639 break;
3640 }
3641 case ISD::ABDS: {
3642 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3643 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3644 Known = KnownBits::abds(Known, Known2);
3645 unsigned SignBits1 =
3646 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3647 if (SignBits1 == 1)
3648 break;
3649 unsigned SignBits0 =
3650 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3651 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3652 break;
3653 }
3654 case ISD::UMUL_LOHI: {
3655 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3656 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3657 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3658 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3659 if (Op.getResNo() == 0)
3660 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3661 else
3662 Known = KnownBits::mulhu(Known, Known2);
3663 break;
3664 }
3665 case ISD::SMUL_LOHI: {
3666 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3667 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3668 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3669 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3670 if (Op.getResNo() == 0)
3671 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3672 else
3673 Known = KnownBits::mulhs(Known, Known2);
3674 break;
3675 }
3676 case ISD::AVGFLOORU: {
3677 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3678 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3679 Known = KnownBits::avgFloorU(Known, Known2);
3680 break;
3681 }
3682 case ISD::AVGCEILU: {
3683 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3684 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3685 Known = KnownBits::avgCeilU(Known, Known2);
3686 break;
3687 }
3688 case ISD::AVGFLOORS: {
3689 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3690 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3691 Known = KnownBits::avgFloorS(Known, Known2);
3692 break;
3693 }
3694 case ISD::AVGCEILS: {
3695 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3696 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3697 Known = KnownBits::avgCeilS(Known, Known2);
3698 break;
3699 }
3700 case ISD::SELECT:
3701 case ISD::VSELECT:
3702 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3703 // If we don't know any bits, early out.
3704 if (Known.isUnknown())
3705 break;
3706 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3707
3708 // Only known if known in both the LHS and RHS.
3709 Known = Known.intersectWith(Known2);
3710 break;
3711 case ISD::SELECT_CC:
3712 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3713 // If we don't know any bits, early out.
3714 if (Known.isUnknown())
3715 break;
3716 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3717
3718 // Only known if known in both the LHS and RHS.
3719 Known = Known.intersectWith(Known2);
3720 break;
3721 case ISD::SMULO:
3722 case ISD::UMULO:
3723 if (Op.getResNo() != 1)
3724 break;
3725 // The boolean result conforms to getBooleanContents.
3726 // If we know the result of a setcc has the top bits zero, use this info.
3727 // We know that we have an integer-based boolean since these operations
3728 // are only available for integer.
3729 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3731 BitWidth > 1)
3732 Known.Zero.setBitsFrom(1);
3733 break;
3734 case ISD::SETCC:
3735 case ISD::SETCCCARRY:
3736 case ISD::STRICT_FSETCC:
3737 case ISD::STRICT_FSETCCS: {
3738 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3739 // If we know the result of a setcc has the top bits zero, use this info.
3740 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3742 BitWidth > 1)
3743 Known.Zero.setBitsFrom(1);
3744 break;
3745 }
3746 case ISD::SHL: {
3747 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3748 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3749
3750 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3751 bool NSW = Op->getFlags().hasNoSignedWrap();
3752
3753 bool ShAmtNonZero = Known2.isNonZero();
3754
3755 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3756
3757 // Minimum shift low bits are known zero.
3758 if (std::optional<unsigned> ShMinAmt =
3759 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3760 Known.Zero.setLowBits(*ShMinAmt);
3761 break;
3762 }
3763 case ISD::SRL:
3764 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3765 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3766 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3767 Op->getFlags().hasExact());
3768
3769 // Minimum shift high bits are known zero.
3770 if (std::optional<unsigned> ShMinAmt =
3771 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3772 Known.Zero.setHighBits(*ShMinAmt);
3773 break;
3774 case ISD::SRA:
3775 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3776 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3777 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3778 Op->getFlags().hasExact());
3779 break;
3780 case ISD::ROTL:
3781 case ISD::ROTR:
3782 if (ConstantSDNode *C =
3783 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3784 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3785
3786 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3787
3788 // Canonicalize to ROTR.
3789 if (Opcode == ISD::ROTL && Amt != 0)
3790 Amt = BitWidth - Amt;
3791
3792 Known.Zero = Known.Zero.rotr(Amt);
3793 Known.One = Known.One.rotr(Amt);
3794 }
3795 break;
3796 case ISD::FSHL:
3797 case ISD::FSHR:
3798 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3799 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3800
3801 // For fshl, 0-shift returns the 1st arg.
3802 // For fshr, 0-shift returns the 2nd arg.
3803 if (Amt == 0) {
3804 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3805 DemandedElts, Depth + 1);
3806 break;
3807 }
3808
3809 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3810 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3811 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3812 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3813 if (Opcode == ISD::FSHL) {
3814 Known <<= Amt;
3815 Known2 >>= BitWidth - Amt;
3816 } else {
3817 Known <<= BitWidth - Amt;
3818 Known2 >>= Amt;
3819 }
3820 Known = Known.unionWith(Known2);
3821 }
3822 break;
3823 case ISD::SHL_PARTS:
3824 case ISD::SRA_PARTS:
3825 case ISD::SRL_PARTS: {
3826 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3827
3828 // Collect lo/hi source values and concatenate.
3829 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3830 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3831 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3832 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3833 Known = Known2.concat(Known);
3834
3835 // Collect shift amount.
3836 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3837
3838 if (Opcode == ISD::SHL_PARTS)
3839 Known = KnownBits::shl(Known, Known2);
3840 else if (Opcode == ISD::SRA_PARTS)
3841 Known = KnownBits::ashr(Known, Known2);
3842 else // if (Opcode == ISD::SRL_PARTS)
3843 Known = KnownBits::lshr(Known, Known2);
3844
3845 // TODO: Minimum shift low/high bits are known zero.
3846
3847 if (Op.getResNo() == 0)
3848 Known = Known.extractBits(LoBits, 0);
3849 else
3850 Known = Known.extractBits(HiBits, LoBits);
3851 break;
3852 }
3854 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3855 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3856 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3857 break;
3858 }
3859 case ISD::CTTZ:
3860 case ISD::CTTZ_ZERO_UNDEF: {
3861 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3862 // If we have a known 1, its position is our upper bound.
3863 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3864 unsigned LowBits = llvm::bit_width(PossibleTZ);
3865 Known.Zero.setBitsFrom(LowBits);
3866 break;
3867 }
3868 case ISD::CTLZ:
3869 case ISD::CTLZ_ZERO_UNDEF: {
3870 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3871 // If we have a known 1, its position is our upper bound.
3872 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3873 unsigned LowBits = llvm::bit_width(PossibleLZ);
3874 Known.Zero.setBitsFrom(LowBits);
3875 break;
3876 }
3877 case ISD::CTPOP: {
3878 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3879 // If we know some of the bits are zero, they can't be one.
3880 unsigned PossibleOnes = Known2.countMaxPopulation();
3881 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3882 break;
3883 }
3884 case ISD::PARITY: {
3885 // Parity returns 0 everywhere but the LSB.
3886 Known.Zero.setBitsFrom(1);
3887 break;
3888 }
3889 case ISD::MGATHER:
3890 case ISD::MLOAD: {
3891 ISD::LoadExtType ETy =
3892 (Opcode == ISD::MGATHER)
3893 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3894 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3895 if (ETy == ISD::ZEXTLOAD) {
3896 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3897 KnownBits Known0(MemVT.getScalarSizeInBits());
3898 return Known0.zext(BitWidth);
3899 }
3900 break;
3901 }
3902 case ISD::LOAD: {
3904 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3905 if (ISD::isNON_EXTLoad(LD) && Cst) {
3906 // Determine any common known bits from the loaded constant pool value.
3907 Type *CstTy = Cst->getType();
3908 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3909 !Op.getValueType().isScalableVector()) {
3910 // If its a vector splat, then we can (quickly) reuse the scalar path.
3911 // NOTE: We assume all elements match and none are UNDEF.
3912 if (CstTy->isVectorTy()) {
3913 if (const Constant *Splat = Cst->getSplatValue()) {
3914 Cst = Splat;
3915 CstTy = Cst->getType();
3916 }
3917 }
3918 // TODO - do we need to handle different bitwidths?
3919 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3920 // Iterate across all vector elements finding common known bits.
3921 Known.setAllConflict();
3922 for (unsigned i = 0; i != NumElts; ++i) {
3923 if (!DemandedElts[i])
3924 continue;
3925 if (Constant *Elt = Cst->getAggregateElement(i)) {
3926 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3927 const APInt &Value = CInt->getValue();
3928 Known.One &= Value;
3929 Known.Zero &= ~Value;
3930 continue;
3931 }
3932 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3933 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3934 Known.One &= Value;
3935 Known.Zero &= ~Value;
3936 continue;
3937 }
3938 }
3939 Known.One.clearAllBits();
3940 Known.Zero.clearAllBits();
3941 break;
3942 }
3943 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3944 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3945 Known = KnownBits::makeConstant(CInt->getValue());
3946 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3947 Known =
3948 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3949 }
3950 }
3951 }
3952 } else if (Op.getResNo() == 0) {
3953 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3954 KnownBits KnownScalarMemory(ScalarMemorySize);
3955 if (const MDNode *MD = LD->getRanges())
3956 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3957
3958 // Extend the Known bits from memory to the size of the scalar result.
3959 if (ISD::isZEXTLoad(Op.getNode()))
3960 Known = KnownScalarMemory.zext(BitWidth);
3961 else if (ISD::isSEXTLoad(Op.getNode()))
3962 Known = KnownScalarMemory.sext(BitWidth);
3963 else if (ISD::isEXTLoad(Op.getNode()))
3964 Known = KnownScalarMemory.anyext(BitWidth);
3965 else
3966 Known = KnownScalarMemory;
3967 assert(Known.getBitWidth() == BitWidth);
3968 return Known;
3969 }
3970 break;
3971 }
3973 if (Op.getValueType().isScalableVector())
3974 break;
3975 EVT InVT = Op.getOperand(0).getValueType();
3976 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3977 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3978 Known = Known.zext(BitWidth);
3979 break;
3980 }
3981 case ISD::ZERO_EXTEND: {
3982 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3983 Known = Known.zext(BitWidth);
3984 break;
3985 }
3987 if (Op.getValueType().isScalableVector())
3988 break;
3989 EVT InVT = Op.getOperand(0).getValueType();
3990 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3991 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3992 // If the sign bit is known to be zero or one, then sext will extend
3993 // it to the top bits, else it will just zext.
3994 Known = Known.sext(BitWidth);
3995 break;
3996 }
3997 case ISD::SIGN_EXTEND: {
3998 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3999 // If the sign bit is known to be zero or one, then sext will extend
4000 // it to the top bits, else it will just zext.
4001 Known = Known.sext(BitWidth);
4002 break;
4003 }
4005 if (Op.getValueType().isScalableVector())
4006 break;
4007 EVT InVT = Op.getOperand(0).getValueType();
4008 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4009 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4010 Known = Known.anyext(BitWidth);
4011 break;
4012 }
4013 case ISD::ANY_EXTEND: {
4014 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4015 Known = Known.anyext(BitWidth);
4016 break;
4017 }
4018 case ISD::TRUNCATE: {
4019 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4020 Known = Known.trunc(BitWidth);
4021 break;
4022 }
4023 case ISD::AssertZext: {
4024 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4026 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4027 Known.Zero |= (~InMask);
4028 Known.One &= (~Known.Zero);
4029 break;
4030 }
4031 case ISD::AssertAlign: {
4032 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4033 assert(LogOfAlign != 0);
4034
4035 // TODO: Should use maximum with source
4036 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4037 // well as clearing one bits.
4038 Known.Zero.setLowBits(LogOfAlign);
4039 Known.One.clearLowBits(LogOfAlign);
4040 break;
4041 }
4042 case ISD::AssertNoFPClass: {
4043 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4044
4045 FPClassTest NoFPClass =
4046 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4047 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4048 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4049 // Cannot be negative.
4050 Known.makeNonNegative();
4051 }
4052
4053 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4054 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4055 // Cannot be positive.
4056 Known.makeNegative();
4057 }
4058
4059 break;
4060 }
4061 case ISD::FGETSIGN:
4062 // All bits are zero except the low bit.
4063 Known.Zero.setBitsFrom(1);
4064 break;
4065 case ISD::ADD:
4066 case ISD::SUB: {
4067 SDNodeFlags Flags = Op.getNode()->getFlags();
4068 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4069 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4071 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4072 Flags.hasNoUnsignedWrap(), Known, Known2);
4073 break;
4074 }
4075 case ISD::USUBO:
4076 case ISD::SSUBO:
4077 case ISD::USUBO_CARRY:
4078 case ISD::SSUBO_CARRY:
4079 if (Op.getResNo() == 1) {
4080 // If we know the result of a setcc has the top bits zero, use this info.
4081 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4083 BitWidth > 1)
4084 Known.Zero.setBitsFrom(1);
4085 break;
4086 }
4087 [[fallthrough]];
4088 case ISD::SUBC: {
4089 assert(Op.getResNo() == 0 &&
4090 "We only compute knownbits for the difference here.");
4091
4092 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4093 KnownBits Borrow(1);
4094 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4095 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4096 // Borrow has bit width 1
4097 Borrow = Borrow.trunc(1);
4098 } else {
4099 Borrow.setAllZero();
4100 }
4101
4102 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4103 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4104 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4105 break;
4106 }
4107 case ISD::UADDO:
4108 case ISD::SADDO:
4109 case ISD::UADDO_CARRY:
4110 case ISD::SADDO_CARRY:
4111 if (Op.getResNo() == 1) {
4112 // If we know the result of a setcc has the top bits zero, use this info.
4113 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4115 BitWidth > 1)
4116 Known.Zero.setBitsFrom(1);
4117 break;
4118 }
4119 [[fallthrough]];
4120 case ISD::ADDC:
4121 case ISD::ADDE: {
4122 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4123
4124 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4125 KnownBits Carry(1);
4126 if (Opcode == ISD::ADDE)
4127 // Can't track carry from glue, set carry to unknown.
4128 Carry.resetAll();
4129 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4130 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4131 // Carry has bit width 1
4132 Carry = Carry.trunc(1);
4133 } else {
4134 Carry.setAllZero();
4135 }
4136
4137 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4138 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4139 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4140 break;
4141 }
4142 case ISD::UDIV: {
4143 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4144 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4145 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4146 break;
4147 }
4148 case ISD::SDIV: {
4149 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4150 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4151 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4152 break;
4153 }
4154 case ISD::SREM: {
4155 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4156 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4157 Known = KnownBits::srem(Known, Known2);
4158 break;
4159 }
4160 case ISD::UREM: {
4161 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4162 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4163 Known = KnownBits::urem(Known, Known2);
4164 break;
4165 }
4166 case ISD::EXTRACT_ELEMENT: {
4167 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4168 const unsigned Index = Op.getConstantOperandVal(1);
4169 const unsigned EltBitWidth = Op.getValueSizeInBits();
4170
4171 // Remove low part of known bits mask
4172 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4173 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4174
4175 // Remove high part of known bit mask
4176 Known = Known.trunc(EltBitWidth);
4177 break;
4178 }
4180 SDValue InVec = Op.getOperand(0);
4181 SDValue EltNo = Op.getOperand(1);
4182 EVT VecVT = InVec.getValueType();
4183 // computeKnownBits not yet implemented for scalable vectors.
4184 if (VecVT.isScalableVector())
4185 break;
4186 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4187 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4188
4189 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4190 // anything about the extended bits.
4191 if (BitWidth > EltBitWidth)
4192 Known = Known.trunc(EltBitWidth);
4193
4194 // If we know the element index, just demand that vector element, else for
4195 // an unknown element index, ignore DemandedElts and demand them all.
4196 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4197 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4198 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4199 DemandedSrcElts =
4200 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4201
4202 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4203 if (BitWidth > EltBitWidth)
4204 Known = Known.anyext(BitWidth);
4205 break;
4206 }
4208 if (Op.getValueType().isScalableVector())
4209 break;
4210
4211 // If we know the element index, split the demand between the
4212 // source vector and the inserted element, otherwise assume we need
4213 // the original demanded vector elements and the value.
4214 SDValue InVec = Op.getOperand(0);
4215 SDValue InVal = Op.getOperand(1);
4216 SDValue EltNo = Op.getOperand(2);
4217 bool DemandedVal = true;
4218 APInt DemandedVecElts = DemandedElts;
4219 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4220 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4221 unsigned EltIdx = CEltNo->getZExtValue();
4222 DemandedVal = !!DemandedElts[EltIdx];
4223 DemandedVecElts.clearBit(EltIdx);
4224 }
4225 Known.setAllConflict();
4226 if (DemandedVal) {
4227 Known2 = computeKnownBits(InVal, Depth + 1);
4228 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4229 }
4230 if (!!DemandedVecElts) {
4231 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4232 Known = Known.intersectWith(Known2);
4233 }
4234 break;
4235 }
4236 case ISD::BITREVERSE: {
4237 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4238 Known = Known2.reverseBits();
4239 break;
4240 }
4241 case ISD::BSWAP: {
4242 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4243 Known = Known2.byteSwap();
4244 break;
4245 }
4246 case ISD::ABS: {
4247 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4248 Known = Known2.abs();
4249 Known.Zero.setHighBits(
4250 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4251 break;
4252 }
4253 case ISD::USUBSAT: {
4254 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4255 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4256 Known = KnownBits::usub_sat(Known, Known2);
4257 break;
4258 }
4259 case ISD::UMIN: {
4260 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4261 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4262 Known = KnownBits::umin(Known, Known2);
4263 break;
4264 }
4265 case ISD::UMAX: {
4266 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4267 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4268 Known = KnownBits::umax(Known, Known2);
4269 break;
4270 }
4271 case ISD::SMIN:
4272 case ISD::SMAX: {
4273 // If we have a clamp pattern, we know that the number of sign bits will be
4274 // the minimum of the clamp min/max range.
4275 bool IsMax = (Opcode == ISD::SMAX);
4276 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4277 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4278 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4279 CstHigh =
4280 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4281 if (CstLow && CstHigh) {
4282 if (!IsMax)
4283 std::swap(CstLow, CstHigh);
4284
4285 const APInt &ValueLow = CstLow->getAPIntValue();
4286 const APInt &ValueHigh = CstHigh->getAPIntValue();
4287 if (ValueLow.sle(ValueHigh)) {
4288 unsigned LowSignBits = ValueLow.getNumSignBits();
4289 unsigned HighSignBits = ValueHigh.getNumSignBits();
4290 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4291 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4292 Known.One.setHighBits(MinSignBits);
4293 break;
4294 }
4295 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4296 Known.Zero.setHighBits(MinSignBits);
4297 break;
4298 }
4299 }
4300 }
4301
4302 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4303 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4304 if (IsMax)
4305 Known = KnownBits::smax(Known, Known2);
4306 else
4307 Known = KnownBits::smin(Known, Known2);
4308
4309 // For SMAX, if CstLow is non-negative we know the result will be
4310 // non-negative and thus all sign bits are 0.
4311 // TODO: There's an equivalent of this for smin with negative constant for
4312 // known ones.
4313 if (IsMax && CstLow) {
4314 const APInt &ValueLow = CstLow->getAPIntValue();
4315 if (ValueLow.isNonNegative()) {
4316 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4317 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4318 }
4319 }
4320
4321 break;
4322 }
4323 case ISD::UINT_TO_FP: {
4324 Known.makeNonNegative();
4325 break;
4326 }
4327 case ISD::SINT_TO_FP: {
4328 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4329 if (Known2.isNonNegative())
4330 Known.makeNonNegative();
4331 else if (Known2.isNegative())
4332 Known.makeNegative();
4333 break;
4334 }
4335 case ISD::FP_TO_UINT_SAT: {
4336 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4337 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4339 break;
4340 }
4341 case ISD::ATOMIC_LOAD: {
4342 // If we are looking at the loaded value.
4343 if (Op.getResNo() == 0) {
4344 auto *AT = cast<AtomicSDNode>(Op);
4345 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4346 KnownBits KnownScalarMemory(ScalarMemorySize);
4347 if (const MDNode *MD = AT->getRanges())
4348 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4349
4350 switch (AT->getExtensionType()) {
4351 case ISD::ZEXTLOAD:
4352 Known = KnownScalarMemory.zext(BitWidth);
4353 break;
4354 case ISD::SEXTLOAD:
4355 Known = KnownScalarMemory.sext(BitWidth);
4356 break;
4357 case ISD::EXTLOAD:
4358 switch (TLI->getExtendForAtomicOps()) {
4359 case ISD::ZERO_EXTEND:
4360 Known = KnownScalarMemory.zext(BitWidth);
4361 break;
4362 case ISD::SIGN_EXTEND:
4363 Known = KnownScalarMemory.sext(BitWidth);
4364 break;
4365 default:
4366 Known = KnownScalarMemory.anyext(BitWidth);
4367 break;
4368 }
4369 break;
4370 case ISD::NON_EXTLOAD:
4371 Known = KnownScalarMemory;
4372 break;
4373 }
4374 assert(Known.getBitWidth() == BitWidth);
4375 }
4376 break;
4377 }
4379 if (Op.getResNo() == 1) {
4380 // The boolean result conforms to getBooleanContents.
4381 // If we know the result of a setcc has the top bits zero, use this info.
4382 // We know that we have an integer-based boolean since these operations
4383 // are only available for integer.
4384 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4386 BitWidth > 1)
4387 Known.Zero.setBitsFrom(1);
4388 break;
4389 }
4390 [[fallthrough]];
4392 case ISD::ATOMIC_SWAP:
4403 case ISD::ATOMIC_LOAD_UMAX: {
4404 // If we are looking at the loaded value.
4405 if (Op.getResNo() == 0) {
4406 auto *AT = cast<AtomicSDNode>(Op);
4407 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4408
4409 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4410 Known.Zero.setBitsFrom(MemBits);
4411 }
4412 break;
4413 }
4414 case ISD::FrameIndex:
4416 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4417 Known, getMachineFunction());
4418 break;
4419
4420 default:
4421 if (Opcode < ISD::BUILTIN_OP_END)
4422 break;
4423 [[fallthrough]];
4427 // TODO: Probably okay to remove after audit; here to reduce change size
4428 // in initial enablement patch for scalable vectors
4429 if (Op.getValueType().isScalableVector())
4430 break;
4431
4432 // Allow the target to implement this method for its nodes.
4433 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4434 break;
4435 }
4436
4437 return Known;
4438}
4439
4440/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4453
4456 // X + 0 never overflow
4457 if (isNullConstant(N1))
4458 return OFK_Never;
4459
4460 // If both operands each have at least two sign bits, the addition
4461 // cannot overflow.
4462 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4463 return OFK_Never;
4464
4465 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4466 return OFK_Sometime;
4467}
4468
4471 // X + 0 never overflow
4472 if (isNullConstant(N1))
4473 return OFK_Never;
4474
4475 // mulhi + 1 never overflow
4476 KnownBits N1Known = computeKnownBits(N1);
4477 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4478 N1Known.getMaxValue().ult(2))
4479 return OFK_Never;
4480
4481 KnownBits N0Known = computeKnownBits(N0);
4482 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4483 N0Known.getMaxValue().ult(2))
4484 return OFK_Never;
4485
4486 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4487 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4488 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4489 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4490}
4491
4494 // X - 0 never overflow
4495 if (isNullConstant(N1))
4496 return OFK_Never;
4497
4498 // If both operands each have at least two sign bits, the subtraction
4499 // cannot overflow.
4500 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4501 return OFK_Never;
4502
4503 KnownBits N0Known = computeKnownBits(N0);
4504 KnownBits N1Known = computeKnownBits(N1);
4505 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4506 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4507 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4508}
4509
4512 // X - 0 never overflow
4513 if (isNullConstant(N1))
4514 return OFK_Never;
4515
4516 KnownBits N0Known = computeKnownBits(N0);
4517 KnownBits N1Known = computeKnownBits(N1);
4518 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4519 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4520 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4521}
4522
4525 // X * 0 and X * 1 never overflow.
4526 if (isNullConstant(N1) || isOneConstant(N1))
4527 return OFK_Never;
4528
4529 KnownBits N0Known = computeKnownBits(N0);
4530 KnownBits N1Known = computeKnownBits(N1);
4531 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4532 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4533 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4534}
4535
4538 // X * 0 and X * 1 never overflow.
4539 if (isNullConstant(N1) || isOneConstant(N1))
4540 return OFK_Never;
4541
4542 // Get the size of the result.
4543 unsigned BitWidth = N0.getScalarValueSizeInBits();
4544
4545 // Sum of the sign bits.
4546 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4547
4548 // If we have enough sign bits, then there's no overflow.
4549 if (SignBits > BitWidth + 1)
4550 return OFK_Never;
4551
4552 if (SignBits == BitWidth + 1) {
4553 // The overflow occurs when the true multiplication of the
4554 // the operands is the minimum negative number.
4555 KnownBits N0Known = computeKnownBits(N0);
4556 KnownBits N1Known = computeKnownBits(N1);
4557 // If one of the operands is non-negative, then there's no
4558 // overflow.
4559 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4560 return OFK_Never;
4561 }
4562
4563 return OFK_Sometime;
4564}
4565
4567 if (Depth >= MaxRecursionDepth)
4568 return false; // Limit search depth.
4569
4570 EVT OpVT = Val.getValueType();
4571 unsigned BitWidth = OpVT.getScalarSizeInBits();
4572
4573 // Is the constant a known power of 2?
4575 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4576 }))
4577 return true;
4578
4579 // A left-shift of a constant one will have exactly one bit set because
4580 // shifting the bit off the end is undefined.
4581 if (Val.getOpcode() == ISD::SHL) {
4582 auto *C = isConstOrConstSplat(Val.getOperand(0));
4583 if (C && C->getAPIntValue() == 1)
4584 return true;
4585 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4586 isKnownNeverZero(Val, Depth);
4587 }
4588
4589 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4590 // one bit set.
4591 if (Val.getOpcode() == ISD::SRL) {
4592 auto *C = isConstOrConstSplat(Val.getOperand(0));
4593 if (C && C->getAPIntValue().isSignMask())
4594 return true;
4595 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4596 isKnownNeverZero(Val, Depth);
4597 }
4598
4599 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4600 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4601
4602 // Are all operands of a build vector constant powers of two?
4603 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4604 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4605 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4606 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4607 return false;
4608 }))
4609 return true;
4610
4611 // Is the operand of a splat vector a constant power of two?
4612 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4614 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4615 return true;
4616
4617 // vscale(power-of-two) is a power-of-two for some targets
4618 if (Val.getOpcode() == ISD::VSCALE &&
4619 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4621 return true;
4622
4623 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4624 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4625 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4627
4628 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4629 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4631
4632 // Looking for `x & -x` pattern:
4633 // If x == 0:
4634 // x & -x -> 0
4635 // If x != 0:
4636 // x & -x -> non-zero pow2
4637 // so if we find the pattern return whether we know `x` is non-zero.
4638 SDValue X;
4639 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4640 return isKnownNeverZero(X, Depth);
4641
4642 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4643 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4644
4645 // More could be done here, though the above checks are enough
4646 // to handle some common cases.
4647 return false;
4648}
4649
4651 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4652 return C1->getValueAPF().getExactLog2Abs() >= 0;
4653
4654 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4655 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4656
4657 return false;
4658}
4659
4661 EVT VT = Op.getValueType();
4662
4663 // Since the number of lanes in a scalable vector is unknown at compile time,
4664 // we track one bit which is implicitly broadcast to all lanes. This means
4665 // that all lanes in a scalable vector are considered demanded.
4666 APInt DemandedElts = VT.isFixedLengthVector()
4668 : APInt(1, 1);
4669 return ComputeNumSignBits(Op, DemandedElts, Depth);
4670}
4671
4672unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4673 unsigned Depth) const {
4674 EVT VT = Op.getValueType();
4675 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4676 unsigned VTBits = VT.getScalarSizeInBits();
4677 unsigned NumElts = DemandedElts.getBitWidth();
4678 unsigned Tmp, Tmp2;
4679 unsigned FirstAnswer = 1;
4680
4681 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4682 const APInt &Val = C->getAPIntValue();
4683 return Val.getNumSignBits();
4684 }
4685
4686 if (Depth >= MaxRecursionDepth)
4687 return 1; // Limit search depth.
4688
4689 if (!DemandedElts)
4690 return 1; // No demanded elts, better to assume we don't know anything.
4691
4692 unsigned Opcode = Op.getOpcode();
4693 switch (Opcode) {
4694 default: break;
4695 case ISD::AssertSext:
4696 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4697 return VTBits-Tmp+1;
4698 case ISD::AssertZext:
4699 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4700 return VTBits-Tmp;
4701 case ISD::FREEZE:
4702 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4703 /*PoisonOnly=*/false))
4704 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4705 break;
4706 case ISD::MERGE_VALUES:
4707 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4708 Depth + 1);
4709 case ISD::SPLAT_VECTOR: {
4710 // Check if the sign bits of source go down as far as the truncated value.
4711 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4712 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4713 if (NumSrcSignBits > (NumSrcBits - VTBits))
4714 return NumSrcSignBits - (NumSrcBits - VTBits);
4715 break;
4716 }
4717 case ISD::BUILD_VECTOR:
4718 assert(!VT.isScalableVector());
4719 Tmp = VTBits;
4720 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4721 if (!DemandedElts[i])
4722 continue;
4723
4724 SDValue SrcOp = Op.getOperand(i);
4725 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4726 // for constant nodes to ensure we only look at the sign bits.
4728 APInt T = C->getAPIntValue().trunc(VTBits);
4729 Tmp2 = T.getNumSignBits();
4730 } else {
4731 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4732
4733 if (SrcOp.getValueSizeInBits() != VTBits) {
4734 assert(SrcOp.getValueSizeInBits() > VTBits &&
4735 "Expected BUILD_VECTOR implicit truncation");
4736 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4737 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4738 }
4739 }
4740 Tmp = std::min(Tmp, Tmp2);
4741 }
4742 return Tmp;
4743
4744 case ISD::VECTOR_COMPRESS: {
4745 SDValue Vec = Op.getOperand(0);
4746 SDValue PassThru = Op.getOperand(2);
4747 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4748 if (Tmp == 1)
4749 return 1;
4750 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4751 Tmp = std::min(Tmp, Tmp2);
4752 return Tmp;
4753 }
4754
4755 case ISD::VECTOR_SHUFFLE: {
4756 // Collect the minimum number of sign bits that are shared by every vector
4757 // element referenced by the shuffle.
4758 APInt DemandedLHS, DemandedRHS;
4760 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4761 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4762 DemandedLHS, DemandedRHS))
4763 return 1;
4764
4765 Tmp = std::numeric_limits<unsigned>::max();
4766 if (!!DemandedLHS)
4767 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4768 if (!!DemandedRHS) {
4769 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4770 Tmp = std::min(Tmp, Tmp2);
4771 }
4772 // If we don't know anything, early out and try computeKnownBits fall-back.
4773 if (Tmp == 1)
4774 break;
4775 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4776 return Tmp;
4777 }
4778
4779 case ISD::BITCAST: {
4780 if (VT.isScalableVector())
4781 break;
4782 SDValue N0 = Op.getOperand(0);
4783 EVT SrcVT = N0.getValueType();
4784 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4785
4786 // Ignore bitcasts from unsupported types..
4787 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4788 break;
4789
4790 // Fast handling of 'identity' bitcasts.
4791 if (VTBits == SrcBits)
4792 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4793
4794 bool IsLE = getDataLayout().isLittleEndian();
4795
4796 // Bitcast 'large element' scalar/vector to 'small element' vector.
4797 if ((SrcBits % VTBits) == 0) {
4798 assert(VT.isVector() && "Expected bitcast to vector");
4799
4800 unsigned Scale = SrcBits / VTBits;
4801 APInt SrcDemandedElts =
4802 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4803
4804 // Fast case - sign splat can be simply split across the small elements.
4805 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4806 if (Tmp == SrcBits)
4807 return VTBits;
4808
4809 // Slow case - determine how far the sign extends into each sub-element.
4810 Tmp2 = VTBits;
4811 for (unsigned i = 0; i != NumElts; ++i)
4812 if (DemandedElts[i]) {
4813 unsigned SubOffset = i % Scale;
4814 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4815 SubOffset = SubOffset * VTBits;
4816 if (Tmp <= SubOffset)
4817 return 1;
4818 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4819 }
4820 return Tmp2;
4821 }
4822 break;
4823 }
4824
4826 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4827 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4828 return VTBits - Tmp + 1;
4829 case ISD::SIGN_EXTEND:
4830 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4831 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4833 // Max of the input and what this extends.
4834 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4835 Tmp = VTBits-Tmp+1;
4836 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4837 return std::max(Tmp, Tmp2);
4839 if (VT.isScalableVector())
4840 break;
4841 SDValue Src = Op.getOperand(0);
4842 EVT SrcVT = Src.getValueType();
4843 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4844 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4845 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4846 }
4847 case ISD::SRA:
4848 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4849 // SRA X, C -> adds C sign bits.
4850 if (std::optional<unsigned> ShAmt =
4851 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4852 Tmp = std::min(Tmp + *ShAmt, VTBits);
4853 return Tmp;
4854 case ISD::SHL:
4855 if (std::optional<ConstantRange> ShAmtRange =
4856 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4857 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4858 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4859 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4860 // shifted out, then we can compute the number of sign bits for the
4861 // operand being extended. A future improvement could be to pass along the
4862 // "shifted left by" information in the recursive calls to
4863 // ComputeKnownSignBits. Allowing us to handle this more generically.
4864 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4865 SDValue Ext = Op.getOperand(0);
4866 EVT ExtVT = Ext.getValueType();
4867 SDValue Extendee = Ext.getOperand(0);
4868 EVT ExtendeeVT = Extendee.getValueType();
4869 unsigned SizeDifference =
4870 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4871 if (SizeDifference <= MinShAmt) {
4872 Tmp = SizeDifference +
4873 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4874 if (MaxShAmt < Tmp)
4875 return Tmp - MaxShAmt;
4876 }
4877 }
4878 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4879 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4880 if (MaxShAmt < Tmp)
4881 return Tmp - MaxShAmt;
4882 }
4883 break;
4884 case ISD::AND:
4885 case ISD::OR:
4886 case ISD::XOR: // NOT is handled here.
4887 // Logical binary ops preserve the number of sign bits at the worst.
4888 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4889 if (Tmp != 1) {
4890 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4891 FirstAnswer = std::min(Tmp, Tmp2);
4892 // We computed what we know about the sign bits as our first
4893 // answer. Now proceed to the generic code that uses
4894 // computeKnownBits, and pick whichever answer is better.
4895 }
4896 break;
4897
4898 case ISD::SELECT:
4899 case ISD::VSELECT:
4900 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4901 if (Tmp == 1) return 1; // Early out.
4902 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4903 return std::min(Tmp, Tmp2);
4904 case ISD::SELECT_CC:
4905 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4906 if (Tmp == 1) return 1; // Early out.
4907 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4908 return std::min(Tmp, Tmp2);
4909
4910 case ISD::SMIN:
4911 case ISD::SMAX: {
4912 // If we have a clamp pattern, we know that the number of sign bits will be
4913 // the minimum of the clamp min/max range.
4914 bool IsMax = (Opcode == ISD::SMAX);
4915 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4916 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4917 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4918 CstHigh =
4919 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4920 if (CstLow && CstHigh) {
4921 if (!IsMax)
4922 std::swap(CstLow, CstHigh);
4923 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4924 Tmp = CstLow->getAPIntValue().getNumSignBits();
4925 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4926 return std::min(Tmp, Tmp2);
4927 }
4928 }
4929
4930 // Fallback - just get the minimum number of sign bits of the operands.
4931 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4932 if (Tmp == 1)
4933 return 1; // Early out.
4934 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4935 return std::min(Tmp, Tmp2);
4936 }
4937 case ISD::UMIN:
4938 case ISD::UMAX:
4939 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4940 if (Tmp == 1)
4941 return 1; // Early out.
4942 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4943 return std::min(Tmp, Tmp2);
4944 case ISD::SSUBO_CARRY:
4945 case ISD::USUBO_CARRY:
4946 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4947 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4948 return VTBits;
4949 [[fallthrough]];
4950 case ISD::SADDO:
4951 case ISD::UADDO:
4952 case ISD::SADDO_CARRY:
4953 case ISD::UADDO_CARRY:
4954 case ISD::SSUBO:
4955 case ISD::USUBO:
4956 case ISD::SMULO:
4957 case ISD::UMULO:
4958 if (Op.getResNo() != 1)
4959 break;
4960 // The boolean result conforms to getBooleanContents. Fall through.
4961 // If setcc returns 0/-1, all bits are sign bits.
4962 // We know that we have an integer-based boolean since these operations
4963 // are only available for integer.
4964 if (TLI->getBooleanContents(VT.isVector(), false) ==
4966 return VTBits;
4967 break;
4968 case ISD::SETCC:
4969 case ISD::SETCCCARRY:
4970 case ISD::STRICT_FSETCC:
4971 case ISD::STRICT_FSETCCS: {
4972 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4973 // If setcc returns 0/-1, all bits are sign bits.
4974 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4976 return VTBits;
4977 break;
4978 }
4979 case ISD::ROTL:
4980 case ISD::ROTR:
4981 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4982
4983 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4984 if (Tmp == VTBits)
4985 return VTBits;
4986
4987 if (ConstantSDNode *C =
4988 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4989 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4990
4991 // Handle rotate right by N like a rotate left by 32-N.
4992 if (Opcode == ISD::ROTR)
4993 RotAmt = (VTBits - RotAmt) % VTBits;
4994
4995 // If we aren't rotating out all of the known-in sign bits, return the
4996 // number that are left. This handles rotl(sext(x), 1) for example.
4997 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4998 }
4999 break;
5000 case ISD::ADD:
5001 case ISD::ADDC:
5002 // TODO: Move Operand 1 check before Operand 0 check
5003 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5004 if (Tmp == 1) return 1; // Early out.
5005
5006 // Special case decrementing a value (ADD X, -1):
5007 if (ConstantSDNode *CRHS =
5008 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5009 if (CRHS->isAllOnes()) {
5010 KnownBits Known =
5011 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5012
5013 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5014 // sign bits set.
5015 if ((Known.Zero | 1).isAllOnes())
5016 return VTBits;
5017
5018 // If we are subtracting one from a positive number, there is no carry
5019 // out of the result.
5020 if (Known.isNonNegative())
5021 return Tmp;
5022 }
5023
5024 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5025 if (Tmp2 == 1) return 1; // Early out.
5026
5027 // Add can have at most one carry bit. Thus we know that the output
5028 // is, at worst, one more bit than the inputs.
5029 return std::min(Tmp, Tmp2) - 1;
5030 case ISD::SUB:
5031 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5032 if (Tmp2 == 1) return 1; // Early out.
5033
5034 // Handle NEG.
5035 if (ConstantSDNode *CLHS =
5036 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5037 if (CLHS->isZero()) {
5038 KnownBits Known =
5039 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5040 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5041 // sign bits set.
5042 if ((Known.Zero | 1).isAllOnes())
5043 return VTBits;
5044
5045 // If the input is known to be positive (the sign bit is known clear),
5046 // the output of the NEG has the same number of sign bits as the input.
5047 if (Known.isNonNegative())
5048 return Tmp2;
5049
5050 // Otherwise, we treat this like a SUB.
5051 }
5052
5053 // Sub can have at most one carry bit. Thus we know that the output
5054 // is, at worst, one more bit than the inputs.
5055 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5056 if (Tmp == 1) return 1; // Early out.
5057 return std::min(Tmp, Tmp2) - 1;
5058 case ISD::MUL: {
5059 // The output of the Mul can be at most twice the valid bits in the inputs.
5060 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5061 if (SignBitsOp0 == 1)
5062 break;
5063 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5064 if (SignBitsOp1 == 1)
5065 break;
5066 unsigned OutValidBits =
5067 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5068 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5069 }
5070 case ISD::AVGCEILS:
5071 case ISD::AVGFLOORS:
5072 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5073 if (Tmp == 1)
5074 return 1; // Early out.
5075 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5076 return std::min(Tmp, Tmp2);
5077 case ISD::SREM:
5078 // The sign bit is the LHS's sign bit, except when the result of the
5079 // remainder is zero. The magnitude of the result should be less than or
5080 // equal to the magnitude of the LHS. Therefore, the result should have
5081 // at least as many sign bits as the left hand side.
5082 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5083 case ISD::TRUNCATE: {
5084 // Check if the sign bits of source go down as far as the truncated value.
5085 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5086 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5087 if (NumSrcSignBits > (NumSrcBits - VTBits))
5088 return NumSrcSignBits - (NumSrcBits - VTBits);
5089 break;
5090 }
5091 case ISD::EXTRACT_ELEMENT: {
5092 if (VT.isScalableVector())
5093 break;
5094 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5095 const int BitWidth = Op.getValueSizeInBits();
5096 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5097
5098 // Get reverse index (starting from 1), Op1 value indexes elements from
5099 // little end. Sign starts at big end.
5100 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5101
5102 // If the sign portion ends in our element the subtraction gives correct
5103 // result. Otherwise it gives either negative or > bitwidth result
5104 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5105 }
5107 if (VT.isScalableVector())
5108 break;
5109 // If we know the element index, split the demand between the
5110 // source vector and the inserted element, otherwise assume we need
5111 // the original demanded vector elements and the value.
5112 SDValue InVec = Op.getOperand(0);
5113 SDValue InVal = Op.getOperand(1);
5114 SDValue EltNo = Op.getOperand(2);
5115 bool DemandedVal = true;
5116 APInt DemandedVecElts = DemandedElts;
5117 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5118 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5119 unsigned EltIdx = CEltNo->getZExtValue();
5120 DemandedVal = !!DemandedElts[EltIdx];
5121 DemandedVecElts.clearBit(EltIdx);
5122 }
5123 Tmp = std::numeric_limits<unsigned>::max();
5124 if (DemandedVal) {
5125 // TODO - handle implicit truncation of inserted elements.
5126 if (InVal.getScalarValueSizeInBits() != VTBits)
5127 break;
5128 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5129 Tmp = std::min(Tmp, Tmp2);
5130 }
5131 if (!!DemandedVecElts) {
5132 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5133 Tmp = std::min(Tmp, Tmp2);
5134 }
5135 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5136 return Tmp;
5137 }
5139 assert(!VT.isScalableVector());
5140 SDValue InVec = Op.getOperand(0);
5141 SDValue EltNo = Op.getOperand(1);
5142 EVT VecVT = InVec.getValueType();
5143 // ComputeNumSignBits not yet implemented for scalable vectors.
5144 if (VecVT.isScalableVector())
5145 break;
5146 const unsigned BitWidth = Op.getValueSizeInBits();
5147 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5148 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5149
5150 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5151 // anything about sign bits. But if the sizes match we can derive knowledge
5152 // about sign bits from the vector operand.
5153 if (BitWidth != EltBitWidth)
5154 break;
5155
5156 // If we know the element index, just demand that vector element, else for
5157 // an unknown element index, ignore DemandedElts and demand them all.
5158 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5159 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5160 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5161 DemandedSrcElts =
5162 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5163
5164 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5165 }
5167 // Offset the demanded elts by the subvector index.
5168 SDValue Src = Op.getOperand(0);
5169 // Bail until we can represent demanded elements for scalable vectors.
5170 if (Src.getValueType().isScalableVector())
5171 break;
5172 uint64_t Idx = Op.getConstantOperandVal(1);
5173 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5174 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5175 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5176 }
5177 case ISD::CONCAT_VECTORS: {
5178 if (VT.isScalableVector())
5179 break;
5180 // Determine the minimum number of sign bits across all demanded
5181 // elts of the input vectors. Early out if the result is already 1.
5182 Tmp = std::numeric_limits<unsigned>::max();
5183 EVT SubVectorVT = Op.getOperand(0).getValueType();
5184 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5185 unsigned NumSubVectors = Op.getNumOperands();
5186 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5187 APInt DemandedSub =
5188 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5189 if (!DemandedSub)
5190 continue;
5191 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5192 Tmp = std::min(Tmp, Tmp2);
5193 }
5194 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5195 return Tmp;
5196 }
5197 case ISD::INSERT_SUBVECTOR: {
5198 if (VT.isScalableVector())
5199 break;
5200 // Demand any elements from the subvector and the remainder from the src its
5201 // inserted into.
5202 SDValue Src = Op.getOperand(0);
5203 SDValue Sub = Op.getOperand(1);
5204 uint64_t Idx = Op.getConstantOperandVal(2);
5205 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5206 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5207 APInt DemandedSrcElts = DemandedElts;
5208 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5209
5210 Tmp = std::numeric_limits<unsigned>::max();
5211 if (!!DemandedSubElts) {
5212 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5213 if (Tmp == 1)
5214 return 1; // early-out
5215 }
5216 if (!!DemandedSrcElts) {
5217 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5218 Tmp = std::min(Tmp, Tmp2);
5219 }
5220 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5221 return Tmp;
5222 }
5223 case ISD::LOAD: {
5224 // If we are looking at the loaded value of the SDNode.
5225 if (Op.getResNo() != 0)
5226 break;
5227
5229 if (const MDNode *Ranges = LD->getRanges()) {
5230 if (DemandedElts != 1)
5231 break;
5232
5234 if (VTBits > CR.getBitWidth()) {
5235 switch (LD->getExtensionType()) {
5236 case ISD::SEXTLOAD:
5237 CR = CR.signExtend(VTBits);
5238 break;
5239 case ISD::ZEXTLOAD:
5240 CR = CR.zeroExtend(VTBits);
5241 break;
5242 default:
5243 break;
5244 }
5245 }
5246
5247 if (VTBits != CR.getBitWidth())
5248 break;
5249 return std::min(CR.getSignedMin().getNumSignBits(),
5251 }
5252
5253 unsigned ExtType = LD->getExtensionType();
5254 switch (ExtType) {
5255 default:
5256 break;
5257 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5258 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5259 return VTBits - Tmp + 1;
5260 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5261 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5262 return VTBits - Tmp;
5263 case ISD::NON_EXTLOAD:
5264 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5265 // We only need to handle vectors - computeKnownBits should handle
5266 // scalar cases.
5267 Type *CstTy = Cst->getType();
5268 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5269 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5270 VTBits == CstTy->getScalarSizeInBits()) {
5271 Tmp = VTBits;
5272 for (unsigned i = 0; i != NumElts; ++i) {
5273 if (!DemandedElts[i])
5274 continue;
5275 if (Constant *Elt = Cst->getAggregateElement(i)) {
5276 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5277 const APInt &Value = CInt->getValue();
5278 Tmp = std::min(Tmp, Value.getNumSignBits());
5279 continue;
5280 }
5281 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5282 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5283 Tmp = std::min(Tmp, Value.getNumSignBits());
5284 continue;
5285 }
5286 }
5287 // Unknown type. Conservatively assume no bits match sign bit.
5288 return 1;
5289 }
5290 return Tmp;
5291 }
5292 }
5293 break;
5294 }
5295
5296 break;
5297 }
5300 case ISD::ATOMIC_SWAP:
5312 case ISD::ATOMIC_LOAD: {
5313 auto *AT = cast<AtomicSDNode>(Op);
5314 // If we are looking at the loaded value.
5315 if (Op.getResNo() == 0) {
5316 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5317 if (Tmp == VTBits)
5318 return 1; // early-out
5319
5320 // For atomic_load, prefer to use the extension type.
5321 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5322 switch (AT->getExtensionType()) {
5323 default:
5324 break;
5325 case ISD::SEXTLOAD:
5326 return VTBits - Tmp + 1;
5327 case ISD::ZEXTLOAD:
5328 return VTBits - Tmp;
5329 }
5330 }
5331
5332 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5333 return VTBits - Tmp + 1;
5334 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5335 return VTBits - Tmp;
5336 }
5337 break;
5338 }
5339 }
5340
5341 // Allow the target to implement this method for its nodes.
5342 if (Opcode >= ISD::BUILTIN_OP_END ||
5343 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5344 Opcode == ISD::INTRINSIC_W_CHAIN ||
5345 Opcode == ISD::INTRINSIC_VOID) {
5346 // TODO: This can probably be removed once target code is audited. This
5347 // is here purely to reduce patch size and review complexity.
5348 if (!VT.isScalableVector()) {
5349 unsigned NumBits =
5350 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5351 if (NumBits > 1)
5352 FirstAnswer = std::max(FirstAnswer, NumBits);
5353 }
5354 }
5355
5356 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5357 // use this information.
5358 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5359 return std::max(FirstAnswer, Known.countMinSignBits());
5360}
5361
5363 unsigned Depth) const {
5364 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5365 return Op.getScalarValueSizeInBits() - SignBits + 1;
5366}
5367
5369 const APInt &DemandedElts,
5370 unsigned Depth) const {
5371 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5372 return Op.getScalarValueSizeInBits() - SignBits + 1;
5373}
5374
5376 unsigned Depth) const {
5377 // Early out for FREEZE.
5378 if (Op.getOpcode() == ISD::FREEZE)
5379 return true;
5380
5381 EVT VT = Op.getValueType();
5382 APInt DemandedElts = VT.isFixedLengthVector()
5384 : APInt(1, 1);
5385 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5386}
5387
5389 const APInt &DemandedElts,
5390 bool PoisonOnly,
5391 unsigned Depth) const {
5392 unsigned Opcode = Op.getOpcode();
5393
5394 // Early out for FREEZE.
5395 if (Opcode == ISD::FREEZE)
5396 return true;
5397
5398 if (Depth >= MaxRecursionDepth)
5399 return false; // Limit search depth.
5400
5401 if (isIntOrFPConstant(Op))
5402 return true;
5403
5404 switch (Opcode) {
5405 case ISD::CONDCODE:
5406 case ISD::VALUETYPE:
5407 case ISD::FrameIndex:
5409 case ISD::CopyFromReg:
5410 return true;
5411
5412 case ISD::POISON:
5413 return false;
5414
5415 case ISD::UNDEF:
5416 return PoisonOnly;
5417
5418 case ISD::BUILD_VECTOR:
5419 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5420 // this shouldn't affect the result.
5421 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5422 if (!DemandedElts[i])
5423 continue;
5425 Depth + 1))
5426 return false;
5427 }
5428 return true;
5429
5431 SDValue Src = Op.getOperand(0);
5432 if (Src.getValueType().isScalableVector())
5433 break;
5434 uint64_t Idx = Op.getConstantOperandVal(1);
5435 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5436 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5437 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5438 Depth + 1);
5439 }
5440
5441 case ISD::INSERT_SUBVECTOR: {
5442 if (Op.getValueType().isScalableVector())
5443 break;
5444 SDValue Src = Op.getOperand(0);
5445 SDValue Sub = Op.getOperand(1);
5446 uint64_t Idx = Op.getConstantOperandVal(2);
5447 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5448 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5449 APInt DemandedSrcElts = DemandedElts;
5450 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5451
5452 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5453 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5454 return false;
5455 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5456 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5457 return false;
5458 return true;
5459 }
5460
5462 SDValue Src = Op.getOperand(0);
5463 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5464 EVT SrcVT = Src.getValueType();
5465 if (SrcVT.isFixedLengthVector() && IndexC &&
5466 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5467 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5468 IndexC->getZExtValue());
5469 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5470 Depth + 1);
5471 }
5472 break;
5473 }
5474
5476 SDValue InVec = Op.getOperand(0);
5477 SDValue InVal = Op.getOperand(1);
5478 SDValue EltNo = Op.getOperand(2);
5479 EVT VT = InVec.getValueType();
5480 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5481 if (IndexC && VT.isFixedLengthVector() &&
5482 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5483 if (DemandedElts[IndexC->getZExtValue()] &&
5485 return false;
5486 APInt InVecDemandedElts = DemandedElts;
5487 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5488 if (!!InVecDemandedElts &&
5490 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5491 InVecDemandedElts, PoisonOnly, Depth + 1))
5492 return false;
5493 return true;
5494 }
5495 break;
5496 }
5497
5499 // Check upper (known undef) elements.
5500 if (DemandedElts.ugt(1) && !PoisonOnly)
5501 return false;
5502 // Check element zero.
5503 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5504 Op.getOperand(0), PoisonOnly, Depth + 1))
5505 return false;
5506 return true;
5507
5508 case ISD::SPLAT_VECTOR:
5509 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5510 Depth + 1);
5511
5512 case ISD::VECTOR_SHUFFLE: {
5513 APInt DemandedLHS, DemandedRHS;
5514 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5515 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5516 DemandedElts, DemandedLHS, DemandedRHS,
5517 /*AllowUndefElts=*/false))
5518 return false;
5519 if (!DemandedLHS.isZero() &&
5520 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5521 PoisonOnly, Depth + 1))
5522 return false;
5523 if (!DemandedRHS.isZero() &&
5524 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5525 PoisonOnly, Depth + 1))
5526 return false;
5527 return true;
5528 }
5529
5530 case ISD::SHL:
5531 case ISD::SRL:
5532 case ISD::SRA:
5533 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5534 // enough to check operand 0 if Op can't create undef/poison.
5535 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5536 /*ConsiderFlags*/ true, Depth) &&
5537 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5538 PoisonOnly, Depth + 1);
5539
5540 case ISD::BSWAP:
5541 case ISD::CTPOP:
5542 case ISD::BITREVERSE:
5543 case ISD::AND:
5544 case ISD::OR:
5545 case ISD::XOR:
5546 case ISD::ADD:
5547 case ISD::SUB:
5548 case ISD::MUL:
5549 case ISD::SADDSAT:
5550 case ISD::UADDSAT:
5551 case ISD::SSUBSAT:
5552 case ISD::USUBSAT:
5553 case ISD::SSHLSAT:
5554 case ISD::USHLSAT:
5555 case ISD::SMIN:
5556 case ISD::SMAX:
5557 case ISD::UMIN:
5558 case ISD::UMAX:
5559 case ISD::ZERO_EXTEND:
5560 case ISD::SIGN_EXTEND:
5561 case ISD::ANY_EXTEND:
5562 case ISD::TRUNCATE:
5563 case ISD::VSELECT: {
5564 // If Op can't create undef/poison and none of its operands are undef/poison
5565 // then Op is never undef/poison. A difference from the more common check
5566 // below, outside the switch, is that we handle elementwise operations for
5567 // which the DemandedElts mask is valid for all operands here.
5568 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5569 /*ConsiderFlags*/ true, Depth) &&
5570 all_of(Op->ops(), [&](SDValue V) {
5571 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5572 PoisonOnly, Depth + 1);
5573 });
5574 }
5575
5576 // TODO: Search for noundef attributes from library functions.
5577
5578 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5579
5580 default:
5581 // Allow the target to implement this method for its nodes.
5582 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5583 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5584 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5585 Op, DemandedElts, *this, PoisonOnly, Depth);
5586 break;
5587 }
5588
5589 // If Op can't create undef/poison and none of its operands are undef/poison
5590 // then Op is never undef/poison.
5591 // NOTE: TargetNodes can handle this in themselves in
5592 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5593 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5594 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5595 Depth) &&
5596 all_of(Op->ops(), [&](SDValue V) {
5597 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5598 });
5599}
5600
5602 bool ConsiderFlags,
5603 unsigned Depth) const {
5604 EVT VT = Op.getValueType();
5605 APInt DemandedElts = VT.isFixedLengthVector()
5607 : APInt(1, 1);
5608 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5609 Depth);
5610}
5611
5613 bool PoisonOnly, bool ConsiderFlags,
5614 unsigned Depth) const {
5615 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5616 return true;
5617
5618 unsigned Opcode = Op.getOpcode();
5619 switch (Opcode) {
5620 case ISD::AssertSext:
5621 case ISD::AssertZext:
5622 case ISD::AssertAlign:
5624 // Assertion nodes can create poison if the assertion fails.
5625 return true;
5626
5627 case ISD::FREEZE:
5631 case ISD::SADDSAT:
5632 case ISD::UADDSAT:
5633 case ISD::SSUBSAT:
5634 case ISD::USUBSAT:
5635 case ISD::MULHU:
5636 case ISD::MULHS:
5637 case ISD::AVGFLOORS:
5638 case ISD::AVGFLOORU:
5639 case ISD::AVGCEILS:
5640 case ISD::AVGCEILU:
5641 case ISD::ABDU:
5642 case ISD::ABDS:
5643 case ISD::SMIN:
5644 case ISD::SMAX:
5645 case ISD::SCMP:
5646 case ISD::UMIN:
5647 case ISD::UMAX:
5648 case ISD::UCMP:
5649 case ISD::AND:
5650 case ISD::XOR:
5651 case ISD::ROTL:
5652 case ISD::ROTR:
5653 case ISD::FSHL:
5654 case ISD::FSHR:
5655 case ISD::BSWAP:
5656 case ISD::CTTZ:
5657 case ISD::CTLZ:
5658 case ISD::CTLS:
5659 case ISD::CTPOP:
5660 case ISD::BITREVERSE:
5661 case ISD::PARITY:
5662 case ISD::SIGN_EXTEND:
5663 case ISD::TRUNCATE:
5667 case ISD::BITCAST:
5668 case ISD::BUILD_VECTOR:
5669 case ISD::BUILD_PAIR:
5670 case ISD::SPLAT_VECTOR:
5671 case ISD::FABS:
5672 return false;
5673
5674 case ISD::ABS:
5675 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5676 // Different to Intrinsic::abs.
5677 return false;
5678
5679 case ISD::ADDC:
5680 case ISD::SUBC:
5681 case ISD::ADDE:
5682 case ISD::SUBE:
5683 case ISD::SADDO:
5684 case ISD::SSUBO:
5685 case ISD::SMULO:
5686 case ISD::SADDO_CARRY:
5687 case ISD::SSUBO_CARRY:
5688 case ISD::UADDO:
5689 case ISD::USUBO:
5690 case ISD::UMULO:
5691 case ISD::UADDO_CARRY:
5692 case ISD::USUBO_CARRY:
5693 // No poison on result or overflow flags.
5694 return false;
5695
5696 case ISD::SELECT_CC:
5697 case ISD::SETCC: {
5698 // Integer setcc cannot create undef or poison.
5699 if (Op.getOperand(0).getValueType().isInteger())
5700 return false;
5701
5702 // FP compares are more complicated. They can create poison for nan/infinity
5703 // based on options and flags. The options and flags also cause special
5704 // nonan condition codes to be used. Those condition codes may be preserved
5705 // even if the nonan flag is dropped somewhere.
5706 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5707 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5708 return (unsigned)CCCode & 0x10U;
5709 }
5710
5711 case ISD::OR:
5712 case ISD::ZERO_EXTEND:
5713 case ISD::SELECT:
5714 case ISD::VSELECT:
5715 case ISD::ADD:
5716 case ISD::SUB:
5717 case ISD::MUL:
5718 case ISD::FNEG:
5719 case ISD::FADD:
5720 case ISD::FSUB:
5721 case ISD::FMUL:
5722 case ISD::FDIV:
5723 case ISD::FREM:
5724 case ISD::FCOPYSIGN:
5725 case ISD::FMA:
5726 case ISD::FMAD:
5727 case ISD::FMULADD:
5728 case ISD::FP_EXTEND:
5731 // No poison except from flags (which is handled above)
5732 return false;
5733
5734 case ISD::SHL:
5735 case ISD::SRL:
5736 case ISD::SRA:
5737 // If the max shift amount isn't in range, then the shift can
5738 // create poison.
5739 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5740
5743 // If the amount is zero then the result will be poison.
5744 // TODO: Add isKnownNeverZero DemandedElts handling.
5745 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5746
5748 // Check if we demand any upper (undef) elements.
5749 return !PoisonOnly && DemandedElts.ugt(1);
5750
5753 // Ensure that the element index is in bounds.
5754 EVT VecVT = Op.getOperand(0).getValueType();
5755 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5756 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5757 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5758 }
5759
5760 case ISD::VECTOR_SHUFFLE: {
5761 // Check for any demanded shuffle element that is undef.
5762 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5763 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5764 if (Elt < 0 && DemandedElts[Idx])
5765 return true;
5766 return false;
5767 }
5768
5770 return false;
5771
5772 default:
5773 // Allow the target to implement this method for its nodes.
5774 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5775 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5776 return TLI->canCreateUndefOrPoisonForTargetNode(
5777 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5778 break;
5779 }
5780
5781 // Be conservative and return true.
5782 return true;
5783}
5784
5785bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5786 unsigned Opcode = Op.getOpcode();
5787 if (Opcode == ISD::OR)
5788 return Op->getFlags().hasDisjoint() ||
5789 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5790 if (Opcode == ISD::XOR)
5791 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5792 return false;
5793}
5794
5796 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5797 (Op.isAnyAdd() || isADDLike(Op));
5798}
5799
5801 unsigned Depth) const {
5802 EVT VT = Op.getValueType();
5803
5804 // Since the number of lanes in a scalable vector is unknown at compile time,
5805 // we track one bit which is implicitly broadcast to all lanes. This means
5806 // that all lanes in a scalable vector are considered demanded.
5807 APInt DemandedElts = VT.isFixedLengthVector()
5809 : APInt(1, 1);
5810
5811 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5812}
5813
5815 bool SNaN, unsigned Depth) const {
5816 assert(!DemandedElts.isZero() && "No demanded elements");
5817
5818 // If we're told that NaNs won't happen, assume they won't.
5819 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5820 return true;
5821
5822 if (Depth >= MaxRecursionDepth)
5823 return false; // Limit search depth.
5824
5825 // If the value is a constant, we can obviously see if it is a NaN or not.
5827 return !C->getValueAPF().isNaN() ||
5828 (SNaN && !C->getValueAPF().isSignaling());
5829 }
5830
5831 unsigned Opcode = Op.getOpcode();
5832 switch (Opcode) {
5833 case ISD::FADD:
5834 case ISD::FSUB:
5835 case ISD::FMUL:
5836 case ISD::FDIV:
5837 case ISD::FREM:
5838 case ISD::FSIN:
5839 case ISD::FCOS:
5840 case ISD::FTAN:
5841 case ISD::FASIN:
5842 case ISD::FACOS:
5843 case ISD::FATAN:
5844 case ISD::FATAN2:
5845 case ISD::FSINH:
5846 case ISD::FCOSH:
5847 case ISD::FTANH:
5848 case ISD::FMA:
5849 case ISD::FMULADD:
5850 case ISD::FMAD: {
5851 if (SNaN)
5852 return true;
5853 // TODO: Need isKnownNeverInfinity
5854 return false;
5855 }
5856 case ISD::FCANONICALIZE:
5857 case ISD::FEXP:
5858 case ISD::FEXP2:
5859 case ISD::FEXP10:
5860 case ISD::FTRUNC:
5861 case ISD::FFLOOR:
5862 case ISD::FCEIL:
5863 case ISD::FROUND:
5864 case ISD::FROUNDEVEN:
5865 case ISD::LROUND:
5866 case ISD::LLROUND:
5867 case ISD::FRINT:
5868 case ISD::LRINT:
5869 case ISD::LLRINT:
5870 case ISD::FNEARBYINT:
5871 case ISD::FLDEXP: {
5872 if (SNaN)
5873 return true;
5874 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5875 }
5876 case ISD::FABS:
5877 case ISD::FNEG:
5878 case ISD::FCOPYSIGN: {
5879 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5880 }
5881 case ISD::SELECT:
5882 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5883 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5884 case ISD::FP_EXTEND:
5885 case ISD::FP_ROUND: {
5886 if (SNaN)
5887 return true;
5888 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5889 }
5890 case ISD::SINT_TO_FP:
5891 case ISD::UINT_TO_FP:
5892 return true;
5893 case ISD::FSQRT: // Need is known positive
5894 case ISD::FLOG:
5895 case ISD::FLOG2:
5896 case ISD::FLOG10:
5897 case ISD::FPOWI:
5898 case ISD::FPOW: {
5899 if (SNaN)
5900 return true;
5901 // TODO: Refine on operand
5902 return false;
5903 }
5904 case ISD::FMINNUM:
5905 case ISD::FMAXNUM:
5906 case ISD::FMINIMUMNUM:
5907 case ISD::FMAXIMUMNUM: {
5908 // Only one needs to be known not-nan, since it will be returned if the
5909 // other ends up being one.
5910 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5911 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5912 }
5913 case ISD::FMINNUM_IEEE:
5914 case ISD::FMAXNUM_IEEE: {
5915 if (SNaN)
5916 return true;
5917 // This can return a NaN if either operand is an sNaN, or if both operands
5918 // are NaN.
5919 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5920 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5921 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5922 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5923 }
5924 case ISD::FMINIMUM:
5925 case ISD::FMAXIMUM: {
5926 // TODO: Does this quiet or return the origina NaN as-is?
5927 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5928 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5929 }
5931 SDValue Src = Op.getOperand(0);
5932 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5933 EVT SrcVT = Src.getValueType();
5934 if (SrcVT.isFixedLengthVector() && Idx &&
5935 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5936 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5937 Idx->getZExtValue());
5938 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5939 }
5940 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5941 }
5943 SDValue Src = Op.getOperand(0);
5944 if (Src.getValueType().isFixedLengthVector()) {
5945 unsigned Idx = Op.getConstantOperandVal(1);
5946 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5947 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5948 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5949 }
5950 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5951 }
5952 case ISD::INSERT_SUBVECTOR: {
5953 SDValue BaseVector = Op.getOperand(0);
5954 SDValue SubVector = Op.getOperand(1);
5955 EVT BaseVectorVT = BaseVector.getValueType();
5956 if (BaseVectorVT.isFixedLengthVector()) {
5957 unsigned Idx = Op.getConstantOperandVal(2);
5958 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5959 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5960
5961 // Clear/Extract the bits at the position where the subvector will be
5962 // inserted.
5963 APInt DemandedMask =
5964 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5965 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5966 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5967
5968 bool NeverNaN = true;
5969 if (!DemandedSrcElts.isZero())
5970 NeverNaN &=
5971 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5972 if (NeverNaN && !DemandedSubElts.isZero())
5973 NeverNaN &=
5974 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
5975 return NeverNaN;
5976 }
5977 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
5978 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
5979 }
5980 case ISD::BUILD_VECTOR: {
5981 unsigned NumElts = Op.getNumOperands();
5982 for (unsigned I = 0; I != NumElts; ++I)
5983 if (DemandedElts[I] &&
5984 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
5985 return false;
5986 return true;
5987 }
5988 case ISD::AssertNoFPClass: {
5989 FPClassTest NoFPClass =
5990 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
5991 if ((NoFPClass & fcNan) == fcNan)
5992 return true;
5993 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
5994 return true;
5995 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5996 }
5997 default:
5998 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5999 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6000 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6001 Depth);
6002 }
6003
6004 return false;
6005 }
6006}
6007
6009 assert(Op.getValueType().isFloatingPoint() &&
6010 "Floating point type expected");
6011
6012 // If the value is a constant, we can obviously see if it is a zero or not.
6014 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6015}
6016
6018 if (Depth >= MaxRecursionDepth)
6019 return false; // Limit search depth.
6020
6021 assert(!Op.getValueType().isFloatingPoint() &&
6022 "Floating point types unsupported - use isKnownNeverZeroFloat");
6023
6024 // If the value is a constant, we can obviously see if it is a zero or not.
6026 [](ConstantSDNode *C) { return !C->isZero(); }))
6027 return true;
6028
6029 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6030 // some degree.
6031 switch (Op.getOpcode()) {
6032 default:
6033 break;
6034
6035 case ISD::OR:
6036 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6037 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6038
6039 case ISD::VSELECT:
6040 case ISD::SELECT:
6041 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6042 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6043
6044 case ISD::SHL: {
6045 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6046 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6047 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6048 // 1 << X is never zero.
6049 if (ValKnown.One[0])
6050 return true;
6051 // If max shift cnt of known ones is non-zero, result is non-zero.
6052 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6053 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6054 !ValKnown.One.shl(MaxCnt).isZero())
6055 return true;
6056 break;
6057 }
6058 case ISD::UADDSAT:
6059 case ISD::UMAX:
6060 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6061 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6062
6063 // For smin/smax: If either operand is known negative/positive
6064 // respectively we don't need the other to be known at all.
6065 case ISD::SMAX: {
6066 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6067 if (Op1.isStrictlyPositive())
6068 return true;
6069
6070 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6071 if (Op0.isStrictlyPositive())
6072 return true;
6073
6074 if (Op1.isNonZero() && Op0.isNonZero())
6075 return true;
6076
6077 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6078 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6079 }
6080 case ISD::SMIN: {
6081 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6082 if (Op1.isNegative())
6083 return true;
6084
6085 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6086 if (Op0.isNegative())
6087 return true;
6088
6089 if (Op1.isNonZero() && Op0.isNonZero())
6090 return true;
6091
6092 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6093 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6094 }
6095 case ISD::UMIN:
6096 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6097 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6098
6099 case ISD::ROTL:
6100 case ISD::ROTR:
6101 case ISD::BITREVERSE:
6102 case ISD::BSWAP:
6103 case ISD::CTPOP:
6104 case ISD::ABS:
6105 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6106
6107 case ISD::SRA:
6108 case ISD::SRL: {
6109 if (Op->getFlags().hasExact())
6110 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6111 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6112 if (ValKnown.isNegative())
6113 return true;
6114 // If max shift cnt of known ones is non-zero, result is non-zero.
6115 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6116 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6117 !ValKnown.One.lshr(MaxCnt).isZero())
6118 return true;
6119 break;
6120 }
6121 case ISD::UDIV:
6122 case ISD::SDIV:
6123 // div exact can only produce a zero if the dividend is zero.
6124 // TODO: For udiv this is also true if Op1 u<= Op0
6125 if (Op->getFlags().hasExact())
6126 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6127 break;
6128
6129 case ISD::ADD:
6130 if (Op->getFlags().hasNoUnsignedWrap())
6131 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6132 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6133 return true;
6134 // TODO: There are a lot more cases we can prove for add.
6135 break;
6136
6137 case ISD::SUB: {
6138 if (isNullConstant(Op.getOperand(0)))
6139 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6140
6141 std::optional<bool> ne =
6142 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6143 computeKnownBits(Op.getOperand(1), Depth + 1));
6144 return ne && *ne;
6145 }
6146
6147 case ISD::MUL:
6148 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6149 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6150 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6151 return true;
6152 break;
6153
6154 case ISD::ZERO_EXTEND:
6155 case ISD::SIGN_EXTEND:
6156 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6157 case ISD::VSCALE: {
6159 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6160 ConstantRange CR =
6161 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6162 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6163 return true;
6164 break;
6165 }
6166 }
6167
6169}
6170
6172 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6173 return !C1->isNegative();
6174
6175 switch (Op.getOpcode()) {
6176 case ISD::FABS:
6177 case ISD::FEXP:
6178 case ISD::FEXP2:
6179 case ISD::FEXP10:
6180 return true;
6181 default:
6182 return false;
6183 }
6184
6185 llvm_unreachable("covered opcode switch");
6186}
6187
6189 assert(Use.getValueType().isFloatingPoint());
6190 const SDNode *User = Use.getUser();
6191 unsigned OperandNo = Use.getOperandNo();
6192 // Check if this use is insensitive to the sign of zero
6193 switch (User->getOpcode()) {
6194 case ISD::SETCC:
6195 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6196 case ISD::FABS:
6197 // fabs always produces +0.0.
6198 return true;
6199 case ISD::FCOPYSIGN:
6200 // copysign overwrites the sign bit of the first operand.
6201 return OperandNo == 0;
6202 case ISD::FADD:
6203 case ISD::FSUB: {
6204 // Arithmetic with non-zero constants fixes the uncertainty around the
6205 // sign bit.
6206 SDValue Other = User->getOperand(1 - OperandNo);
6208 }
6209 case ISD::FP_TO_SINT:
6210 case ISD::FP_TO_UINT:
6211 // fp-to-int conversions normalize signed zeros.
6212 return true;
6213 default:
6214 return false;
6215 }
6216}
6217
6219 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6220 // regression. Ideally, this should be implemented as a demanded-bits
6221 // optimization that stems from the users.
6222 if (Op->use_size() > 2)
6223 return false;
6224 return all_of(Op->uses(),
6225 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6226}
6227
6229 // Check the obvious case.
6230 if (A == B) return true;
6231
6232 // For negative and positive zero.
6235 if (CA->isZero() && CB->isZero()) return true;
6236
6237 // Otherwise they may not be equal.
6238 return false;
6239}
6240
6241// Only bits set in Mask must be negated, other bits may be arbitrary.
6243 if (isBitwiseNot(V, AllowUndefs))
6244 return V.getOperand(0);
6245
6246 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6247 // bits in the non-extended part.
6248 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6249 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6250 return SDValue();
6251 SDValue ExtArg = V.getOperand(0);
6252 if (ExtArg.getScalarValueSizeInBits() >=
6253 MaskC->getAPIntValue().getActiveBits() &&
6254 isBitwiseNot(ExtArg, AllowUndefs) &&
6255 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6256 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6257 return ExtArg.getOperand(0).getOperand(0);
6258 return SDValue();
6259}
6260
6262 // Match masked merge pattern (X & ~M) op (Y & M)
6263 // Including degenerate case (X & ~M) op M
6264 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6265 SDValue Other) {
6266 if (SDValue NotOperand =
6267 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6268 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6269 NotOperand->getOpcode() == ISD::TRUNCATE)
6270 NotOperand = NotOperand->getOperand(0);
6271
6272 if (Other == NotOperand)
6273 return true;
6274 if (Other->getOpcode() == ISD::AND)
6275 return NotOperand == Other->getOperand(0) ||
6276 NotOperand == Other->getOperand(1);
6277 }
6278 return false;
6279 };
6280
6281 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6282 A = A->getOperand(0);
6283
6284 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6285 B = B->getOperand(0);
6286
6287 if (A->getOpcode() == ISD::AND)
6288 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6289 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6290 return false;
6291}
6292
6293// FIXME: unify with llvm::haveNoCommonBitsSet.
6295 assert(A.getValueType() == B.getValueType() &&
6296 "Values must have the same type");
6299 return true;
6302}
6303
6304static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6305 SelectionDAG &DAG) {
6306 if (cast<ConstantSDNode>(Step)->isZero())
6307 return DAG.getConstant(0, DL, VT);
6308
6309 return SDValue();
6310}
6311
6314 SelectionDAG &DAG) {
6315 int NumOps = Ops.size();
6316 assert(NumOps != 0 && "Can't build an empty vector!");
6317 assert(!VT.isScalableVector() &&
6318 "BUILD_VECTOR cannot be used with scalable types");
6319 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6320 "Incorrect element count in BUILD_VECTOR!");
6321
6322 // BUILD_VECTOR of UNDEFs is UNDEF.
6323 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6324 return DAG.getUNDEF(VT);
6325
6326 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6327 SDValue IdentitySrc;
6328 bool IsIdentity = true;
6329 for (int i = 0; i != NumOps; ++i) {
6331 Ops[i].getOperand(0).getValueType() != VT ||
6332 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6333 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6334 Ops[i].getConstantOperandAPInt(1) != i) {
6335 IsIdentity = false;
6336 break;
6337 }
6338 IdentitySrc = Ops[i].getOperand(0);
6339 }
6340 if (IsIdentity)
6341 return IdentitySrc;
6342
6343 return SDValue();
6344}
6345
6346/// Try to simplify vector concatenation to an input value, undef, or build
6347/// vector.
6350 SelectionDAG &DAG) {
6351 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6353 [Ops](SDValue Op) {
6354 return Ops[0].getValueType() == Op.getValueType();
6355 }) &&
6356 "Concatenation of vectors with inconsistent value types!");
6357 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6358 VT.getVectorElementCount() &&
6359 "Incorrect element count in vector concatenation!");
6360
6361 if (Ops.size() == 1)
6362 return Ops[0];
6363
6364 // Concat of UNDEFs is UNDEF.
6365 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6366 return DAG.getUNDEF(VT);
6367
6368 // Scan the operands and look for extract operations from a single source
6369 // that correspond to insertion at the same location via this concatenation:
6370 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6371 SDValue IdentitySrc;
6372 bool IsIdentity = true;
6373 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6374 SDValue Op = Ops[i];
6375 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6376 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6377 Op.getOperand(0).getValueType() != VT ||
6378 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6379 Op.getConstantOperandVal(1) != IdentityIndex) {
6380 IsIdentity = false;
6381 break;
6382 }
6383 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6384 "Unexpected identity source vector for concat of extracts");
6385 IdentitySrc = Op.getOperand(0);
6386 }
6387 if (IsIdentity) {
6388 assert(IdentitySrc && "Failed to set source vector of extracts");
6389 return IdentitySrc;
6390 }
6391
6392 // The code below this point is only designed to work for fixed width
6393 // vectors, so we bail out for now.
6394 if (VT.isScalableVector())
6395 return SDValue();
6396
6397 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6398 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6399 // BUILD_VECTOR.
6400 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6401 EVT SVT = VT.getScalarType();
6403 for (SDValue Op : Ops) {
6404 EVT OpVT = Op.getValueType();
6405 if (Op.isUndef())
6406 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6407 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6408 Elts.append(Op->op_begin(), Op->op_end());
6409 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6410 OpVT.getVectorNumElements() == 1 &&
6411 isNullConstant(Op.getOperand(2)))
6412 Elts.push_back(Op.getOperand(1));
6413 else
6414 return SDValue();
6415 }
6416
6417 // BUILD_VECTOR requires all inputs to be of the same type, find the
6418 // maximum type and extend them all.
6419 for (SDValue Op : Elts)
6420 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6421
6422 if (SVT.bitsGT(VT.getScalarType())) {
6423 for (SDValue &Op : Elts) {
6424 if (Op.isUndef())
6425 Op = DAG.getUNDEF(SVT);
6426 else
6427 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6428 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6429 : DAG.getSExtOrTrunc(Op, DL, SVT);
6430 }
6431 }
6432
6433 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6434 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6435 return V;
6436}
6437
6438/// Gets or creates the specified node.
6439SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6440 SDVTList VTs = getVTList(VT);
6442 AddNodeIDNode(ID, Opcode, VTs, {});
6443 void *IP = nullptr;
6444 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6445 return SDValue(E, 0);
6446
6447 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6448 CSEMap.InsertNode(N, IP);
6449
6450 InsertNode(N);
6451 SDValue V = SDValue(N, 0);
6452 NewSDValueDbgMsg(V, "Creating new node: ", this);
6453 return V;
6454}
6455
6456SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6457 SDValue N1) {
6458 SDNodeFlags Flags;
6459 if (Inserter)
6460 Flags = Inserter->getFlags();
6461 return getNode(Opcode, DL, VT, N1, Flags);
6462}
6463
6464SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6465 SDValue N1, const SDNodeFlags Flags) {
6466 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6467
6468 // Constant fold unary operations with a vector integer or float operand.
6469 switch (Opcode) {
6470 default:
6471 // FIXME: Entirely reasonable to perform folding of other unary
6472 // operations here as the need arises.
6473 break;
6474 case ISD::FNEG:
6475 case ISD::FABS:
6476 case ISD::FCEIL:
6477 case ISD::FTRUNC:
6478 case ISD::FFLOOR:
6479 case ISD::FP_EXTEND:
6480 case ISD::FP_TO_SINT:
6481 case ISD::FP_TO_UINT:
6482 case ISD::FP_TO_FP16:
6483 case ISD::FP_TO_BF16:
6484 case ISD::TRUNCATE:
6485 case ISD::ANY_EXTEND:
6486 case ISD::ZERO_EXTEND:
6487 case ISD::SIGN_EXTEND:
6488 case ISD::UINT_TO_FP:
6489 case ISD::SINT_TO_FP:
6490 case ISD::FP16_TO_FP:
6491 case ISD::BF16_TO_FP:
6492 case ISD::BITCAST:
6493 case ISD::ABS:
6494 case ISD::BITREVERSE:
6495 case ISD::BSWAP:
6496 case ISD::CTLZ:
6498 case ISD::CTTZ:
6500 case ISD::CTPOP:
6501 case ISD::STEP_VECTOR: {
6502 SDValue Ops = {N1};
6503 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6504 return Fold;
6505 }
6506 }
6507
6508 unsigned OpOpcode = N1.getNode()->getOpcode();
6509 switch (Opcode) {
6510 case ISD::STEP_VECTOR:
6511 assert(VT.isScalableVector() &&
6512 "STEP_VECTOR can only be used with scalable types");
6513 assert(OpOpcode == ISD::TargetConstant &&
6514 VT.getVectorElementType() == N1.getValueType() &&
6515 "Unexpected step operand");
6516 break;
6517 case ISD::FREEZE:
6518 assert(VT == N1.getValueType() && "Unexpected VT!");
6519 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6520 return N1;
6521 break;
6522 case ISD::TokenFactor:
6523 case ISD::MERGE_VALUES:
6525 return N1; // Factor, merge or concat of one node? No need.
6526 case ISD::BUILD_VECTOR: {
6527 // Attempt to simplify BUILD_VECTOR.
6528 SDValue Ops[] = {N1};
6529 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6530 return V;
6531 break;
6532 }
6533 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6534 case ISD::FP_EXTEND:
6536 "Invalid FP cast!");
6537 if (N1.getValueType() == VT) return N1; // noop conversion.
6538 assert((!VT.isVector() || VT.getVectorElementCount() ==
6540 "Vector element count mismatch!");
6541 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6542 if (N1.isUndef())
6543 return getUNDEF(VT);
6544 break;
6545 case ISD::FP_TO_SINT:
6546 case ISD::FP_TO_UINT:
6547 if (N1.isUndef())
6548 return getUNDEF(VT);
6549 break;
6550 case ISD::SINT_TO_FP:
6551 case ISD::UINT_TO_FP:
6552 // [us]itofp(undef) = 0, because the result value is bounded.
6553 if (N1.isUndef())
6554 return getConstantFP(0.0, DL, VT);
6555 break;
6556 case ISD::SIGN_EXTEND:
6557 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6558 "Invalid SIGN_EXTEND!");
6559 assert(VT.isVector() == N1.getValueType().isVector() &&
6560 "SIGN_EXTEND result type type should be vector iff the operand "
6561 "type is vector!");
6562 if (N1.getValueType() == VT) return N1; // noop extension
6563 assert((!VT.isVector() || VT.getVectorElementCount() ==
6565 "Vector element count mismatch!");
6566 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6567 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6568 SDNodeFlags Flags;
6569 if (OpOpcode == ISD::ZERO_EXTEND)
6570 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6571 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6572 transferDbgValues(N1, NewVal);
6573 return NewVal;
6574 }
6575
6576 if (OpOpcode == ISD::POISON)
6577 return getPOISON(VT);
6578
6579 if (N1.isUndef())
6580 // sext(undef) = 0, because the top bits will all be the same.
6581 return getConstant(0, DL, VT);
6582
6583 // Skip unnecessary sext_inreg pattern:
6584 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6585 if (OpOpcode == ISD::TRUNCATE) {
6586 SDValue OpOp = N1.getOperand(0);
6587 if (OpOp.getValueType() == VT) {
6588 unsigned NumSignExtBits =
6590 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6591 transferDbgValues(N1, OpOp);
6592 return OpOp;
6593 }
6594 }
6595 }
6596 break;
6597 case ISD::ZERO_EXTEND:
6598 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6599 "Invalid ZERO_EXTEND!");
6600 assert(VT.isVector() == N1.getValueType().isVector() &&
6601 "ZERO_EXTEND result type type should be vector iff the operand "
6602 "type is vector!");
6603 if (N1.getValueType() == VT) return N1; // noop extension
6604 assert((!VT.isVector() || VT.getVectorElementCount() ==
6606 "Vector element count mismatch!");
6607 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6608 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6609 SDNodeFlags Flags;
6610 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6611 SDValue NewVal =
6612 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6613 transferDbgValues(N1, NewVal);
6614 return NewVal;
6615 }
6616
6617 if (OpOpcode == ISD::POISON)
6618 return getPOISON(VT);
6619
6620 if (N1.isUndef())
6621 // zext(undef) = 0, because the top bits will be zero.
6622 return getConstant(0, DL, VT);
6623
6624 // Skip unnecessary zext_inreg pattern:
6625 // (zext (trunc x)) -> x iff the upper bits are known zero.
6626 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6627 // use to recognise zext_inreg patterns.
6628 if (OpOpcode == ISD::TRUNCATE) {
6629 SDValue OpOp = N1.getOperand(0);
6630 if (OpOp.getValueType() == VT) {
6631 if (OpOp.getOpcode() != ISD::AND) {
6634 if (MaskedValueIsZero(OpOp, HiBits)) {
6635 transferDbgValues(N1, OpOp);
6636 return OpOp;
6637 }
6638 }
6639 }
6640 }
6641 break;
6642 case ISD::ANY_EXTEND:
6643 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6644 "Invalid ANY_EXTEND!");
6645 assert(VT.isVector() == N1.getValueType().isVector() &&
6646 "ANY_EXTEND result type type should be vector iff the operand "
6647 "type is vector!");
6648 if (N1.getValueType() == VT) return N1; // noop extension
6649 assert((!VT.isVector() || VT.getVectorElementCount() ==
6651 "Vector element count mismatch!");
6652 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6653
6654 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6655 OpOpcode == ISD::ANY_EXTEND) {
6656 SDNodeFlags Flags;
6657 if (OpOpcode == ISD::ZERO_EXTEND)
6658 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6659 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6660 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6661 }
6662 if (N1.isUndef())
6663 return getUNDEF(VT);
6664
6665 // (ext (trunc x)) -> x
6666 if (OpOpcode == ISD::TRUNCATE) {
6667 SDValue OpOp = N1.getOperand(0);
6668 if (OpOp.getValueType() == VT) {
6669 transferDbgValues(N1, OpOp);
6670 return OpOp;
6671 }
6672 }
6673 break;
6674 case ISD::TRUNCATE:
6675 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6676 "Invalid TRUNCATE!");
6677 assert(VT.isVector() == N1.getValueType().isVector() &&
6678 "TRUNCATE result type type should be vector iff the operand "
6679 "type is vector!");
6680 if (N1.getValueType() == VT) return N1; // noop truncate
6681 assert((!VT.isVector() || VT.getVectorElementCount() ==
6683 "Vector element count mismatch!");
6684 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6685 if (OpOpcode == ISD::TRUNCATE)
6686 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6687 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6688 OpOpcode == ISD::ANY_EXTEND) {
6689 // If the source is smaller than the dest, we still need an extend.
6691 VT.getScalarType())) {
6692 SDNodeFlags Flags;
6693 if (OpOpcode == ISD::ZERO_EXTEND)
6694 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6695 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6696 }
6697 if (N1.getOperand(0).getValueType().bitsGT(VT))
6698 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6699 return N1.getOperand(0);
6700 }
6701 if (N1.isUndef())
6702 return getUNDEF(VT);
6703 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6704 return getVScale(DL, VT,
6706 break;
6710 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6711 assert(N1.getValueType().bitsLE(VT) &&
6712 "The input must be the same size or smaller than the result.");
6715 "The destination vector type must have fewer lanes than the input.");
6716 break;
6717 case ISD::ABS:
6718 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6719 if (N1.isUndef())
6720 return getConstant(0, DL, VT);
6721 break;
6722 case ISD::BSWAP:
6723 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6724 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6725 "BSWAP types must be a multiple of 16 bits!");
6726 if (N1.isUndef())
6727 return getUNDEF(VT);
6728 // bswap(bswap(X)) -> X.
6729 if (OpOpcode == ISD::BSWAP)
6730 return N1.getOperand(0);
6731 break;
6732 case ISD::BITREVERSE:
6733 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6734 if (N1.isUndef())
6735 return getUNDEF(VT);
6736 break;
6737 case ISD::BITCAST:
6739 "Cannot BITCAST between types of different sizes!");
6740 if (VT == N1.getValueType()) return N1; // noop conversion.
6741 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6742 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6743 if (N1.isUndef())
6744 return getUNDEF(VT);
6745 break;
6747 assert(VT.isVector() && !N1.getValueType().isVector() &&
6748 (VT.getVectorElementType() == N1.getValueType() ||
6750 N1.getValueType().isInteger() &&
6752 "Illegal SCALAR_TO_VECTOR node!");
6753 if (N1.isUndef())
6754 return getUNDEF(VT);
6755 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6756 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6758 N1.getConstantOperandVal(1) == 0 &&
6759 N1.getOperand(0).getValueType() == VT)
6760 return N1.getOperand(0);
6761 break;
6762 case ISD::FNEG:
6763 // Negation of an unknown bag of bits is still completely undefined.
6764 if (N1.isUndef())
6765 return getUNDEF(VT);
6766
6767 if (OpOpcode == ISD::FNEG) // --X -> X
6768 return N1.getOperand(0);
6769 break;
6770 case ISD::FABS:
6771 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6772 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6773 break;
6774 case ISD::VSCALE:
6775 assert(VT == N1.getValueType() && "Unexpected VT!");
6776 break;
6777 case ISD::CTPOP:
6778 if (N1.getValueType().getScalarType() == MVT::i1)
6779 return N1;
6780 break;
6781 case ISD::CTLZ:
6782 case ISD::CTTZ:
6783 if (N1.getValueType().getScalarType() == MVT::i1)
6784 return getNOT(DL, N1, N1.getValueType());
6785 break;
6786 case ISD::VECREDUCE_ADD:
6787 if (N1.getValueType().getScalarType() == MVT::i1)
6788 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6789 break;
6792 if (N1.getValueType().getScalarType() == MVT::i1)
6793 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6794 break;
6797 if (N1.getValueType().getScalarType() == MVT::i1)
6798 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6799 break;
6800 case ISD::SPLAT_VECTOR:
6801 assert(VT.isVector() && "Wrong return type!");
6802 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6803 // that for now.
6805 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6807 N1.getValueType().isInteger() &&
6809 "Wrong operand type!");
6810 break;
6811 }
6812
6813 SDNode *N;
6814 SDVTList VTs = getVTList(VT);
6815 SDValue Ops[] = {N1};
6816 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6818 AddNodeIDNode(ID, Opcode, VTs, Ops);
6819 void *IP = nullptr;
6820 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6821 E->intersectFlagsWith(Flags);
6822 return SDValue(E, 0);
6823 }
6824
6825 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6826 N->setFlags(Flags);
6827 createOperands(N, Ops);
6828 CSEMap.InsertNode(N, IP);
6829 } else {
6830 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6831 createOperands(N, Ops);
6832 }
6833
6834 InsertNode(N);
6835 SDValue V = SDValue(N, 0);
6836 NewSDValueDbgMsg(V, "Creating new node: ", this);
6837 return V;
6838}
6839
6840static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6841 const APInt &C2) {
6842 switch (Opcode) {
6843 case ISD::ADD: return C1 + C2;
6844 case ISD::SUB: return C1 - C2;
6845 case ISD::MUL: return C1 * C2;
6846 case ISD::AND: return C1 & C2;
6847 case ISD::OR: return C1 | C2;
6848 case ISD::XOR: return C1 ^ C2;
6849 case ISD::SHL: return C1 << C2;
6850 case ISD::SRL: return C1.lshr(C2);
6851 case ISD::SRA: return C1.ashr(C2);
6852 case ISD::ROTL: return C1.rotl(C2);
6853 case ISD::ROTR: return C1.rotr(C2);
6854 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6855 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6856 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6857 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6858 case ISD::SADDSAT: return C1.sadd_sat(C2);
6859 case ISD::UADDSAT: return C1.uadd_sat(C2);
6860 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6861 case ISD::USUBSAT: return C1.usub_sat(C2);
6862 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6863 case ISD::USHLSAT: return C1.ushl_sat(C2);
6864 case ISD::UDIV:
6865 if (!C2.getBoolValue())
6866 break;
6867 return C1.udiv(C2);
6868 case ISD::UREM:
6869 if (!C2.getBoolValue())
6870 break;
6871 return C1.urem(C2);
6872 case ISD::SDIV:
6873 if (!C2.getBoolValue())
6874 break;
6875 return C1.sdiv(C2);
6876 case ISD::SREM:
6877 if (!C2.getBoolValue())
6878 break;
6879 return C1.srem(C2);
6880 case ISD::AVGFLOORS:
6881 return APIntOps::avgFloorS(C1, C2);
6882 case ISD::AVGFLOORU:
6883 return APIntOps::avgFloorU(C1, C2);
6884 case ISD::AVGCEILS:
6885 return APIntOps::avgCeilS(C1, C2);
6886 case ISD::AVGCEILU:
6887 return APIntOps::avgCeilU(C1, C2);
6888 case ISD::ABDS:
6889 return APIntOps::abds(C1, C2);
6890 case ISD::ABDU:
6891 return APIntOps::abdu(C1, C2);
6892 case ISD::MULHS:
6893 return APIntOps::mulhs(C1, C2);
6894 case ISD::MULHU:
6895 return APIntOps::mulhu(C1, C2);
6896 case ISD::CLMUL:
6897 return APIntOps::clmul(C1, C2);
6898 case ISD::CLMULR:
6899 return APIntOps::clmulr(C1, C2);
6900 case ISD::CLMULH:
6901 return APIntOps::clmulh(C1, C2);
6902 }
6903 return std::nullopt;
6904}
6905// Handle constant folding with UNDEF.
6906// TODO: Handle more cases.
6907static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6908 bool IsUndef1, const APInt &C2,
6909 bool IsUndef2) {
6910 if (!(IsUndef1 || IsUndef2))
6911 return FoldValue(Opcode, C1, C2);
6912
6913 // Fold and(x, undef) -> 0
6914 // Fold mul(x, undef) -> 0
6915 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6916 return APInt::getZero(C1.getBitWidth());
6917
6918 return std::nullopt;
6919}
6920
6922 const GlobalAddressSDNode *GA,
6923 const SDNode *N2) {
6924 if (GA->getOpcode() != ISD::GlobalAddress)
6925 return SDValue();
6926 if (!TLI->isOffsetFoldingLegal(GA))
6927 return SDValue();
6928 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6929 if (!C2)
6930 return SDValue();
6931 int64_t Offset = C2->getSExtValue();
6932 switch (Opcode) {
6933 case ISD::ADD:
6934 case ISD::PTRADD:
6935 break;
6936 case ISD::SUB: Offset = -uint64_t(Offset); break;
6937 default: return SDValue();
6938 }
6939 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6940 GA->getOffset() + uint64_t(Offset));
6941}
6942
6944 switch (Opcode) {
6945 case ISD::SDIV:
6946 case ISD::UDIV:
6947 case ISD::SREM:
6948 case ISD::UREM: {
6949 // If a divisor is zero/undef or any element of a divisor vector is
6950 // zero/undef, the whole op is undef.
6951 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6952 SDValue Divisor = Ops[1];
6953 if (Divisor.isUndef() || isNullConstant(Divisor))
6954 return true;
6955
6956 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6957 llvm::any_of(Divisor->op_values(),
6958 [](SDValue V) { return V.isUndef() ||
6959 isNullConstant(V); });
6960 // TODO: Handle signed overflow.
6961 }
6962 // TODO: Handle oversized shifts.
6963 default:
6964 return false;
6965 }
6966}
6967
6970 SDNodeFlags Flags) {
6971 // If the opcode is a target-specific ISD node, there's nothing we can
6972 // do here and the operand rules may not line up with the below, so
6973 // bail early.
6974 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6975 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6976 // foldCONCAT_VECTORS in getNode before this is called.
6977 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6978 return SDValue();
6979
6980 unsigned NumOps = Ops.size();
6981 if (NumOps == 0)
6982 return SDValue();
6983
6984 if (isUndef(Opcode, Ops))
6985 return getUNDEF(VT);
6986
6987 // Handle unary special cases.
6988 if (NumOps == 1) {
6989 SDValue N1 = Ops[0];
6990
6991 // Constant fold unary operations with an integer constant operand. Even
6992 // opaque constant will be folded, because the folding of unary operations
6993 // doesn't create new constants with different values. Nevertheless, the
6994 // opaque flag is preserved during folding to prevent future folding with
6995 // other constants.
6996 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6997 const APInt &Val = C->getAPIntValue();
6998 switch (Opcode) {
6999 case ISD::SIGN_EXTEND:
7000 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7001 C->isTargetOpcode(), C->isOpaque());
7002 case ISD::TRUNCATE:
7003 if (C->isOpaque())
7004 break;
7005 [[fallthrough]];
7006 case ISD::ZERO_EXTEND:
7007 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7008 C->isTargetOpcode(), C->isOpaque());
7009 case ISD::ANY_EXTEND:
7010 // Some targets like RISCV prefer to sign extend some types.
7011 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7012 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7013 C->isTargetOpcode(), C->isOpaque());
7014 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7015 C->isTargetOpcode(), C->isOpaque());
7016 case ISD::ABS:
7017 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7018 C->isOpaque());
7019 case ISD::BITREVERSE:
7020 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7021 C->isOpaque());
7022 case ISD::BSWAP:
7023 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7024 C->isOpaque());
7025 case ISD::CTPOP:
7026 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7027 C->isOpaque());
7028 case ISD::CTLZ:
7030 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7031 C->isOpaque());
7032 case ISD::CTTZ:
7034 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7035 C->isOpaque());
7036 case ISD::UINT_TO_FP:
7037 case ISD::SINT_TO_FP: {
7039 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7041 return getConstantFP(FPV, DL, VT);
7042 }
7043 case ISD::FP16_TO_FP:
7044 case ISD::BF16_TO_FP: {
7045 bool Ignored;
7046 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7047 : APFloat::BFloat(),
7048 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7049
7050 // This can return overflow, underflow, or inexact; we don't care.
7051 // FIXME need to be more flexible about rounding mode.
7053 &Ignored);
7054 return getConstantFP(FPV, DL, VT);
7055 }
7056 case ISD::STEP_VECTOR:
7057 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7058 return V;
7059 break;
7060 case ISD::BITCAST:
7061 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7062 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7063 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7064 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7065 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7066 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7067 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7068 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7069 break;
7070 }
7071 }
7072
7073 // Constant fold unary operations with a floating point constant operand.
7074 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7075 APFloat V = C->getValueAPF(); // make copy
7076 switch (Opcode) {
7077 case ISD::FNEG:
7078 V.changeSign();
7079 return getConstantFP(V, DL, VT);
7080 case ISD::FABS:
7081 V.clearSign();
7082 return getConstantFP(V, DL, VT);
7083 case ISD::FCEIL: {
7084 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7086 return getConstantFP(V, DL, VT);
7087 return SDValue();
7088 }
7089 case ISD::FTRUNC: {
7090 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7092 return getConstantFP(V, DL, VT);
7093 return SDValue();
7094 }
7095 case ISD::FFLOOR: {
7096 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7098 return getConstantFP(V, DL, VT);
7099 return SDValue();
7100 }
7101 case ISD::FP_EXTEND: {
7102 bool ignored;
7103 // This can return overflow, underflow, or inexact; we don't care.
7104 // FIXME need to be more flexible about rounding mode.
7105 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7106 &ignored);
7107 return getConstantFP(V, DL, VT);
7108 }
7109 case ISD::FP_TO_SINT:
7110 case ISD::FP_TO_UINT: {
7111 bool ignored;
7112 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7113 // FIXME need to be more flexible about rounding mode.
7115 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7116 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7117 break;
7118 return getConstant(IntVal, DL, VT);
7119 }
7120 case ISD::FP_TO_FP16:
7121 case ISD::FP_TO_BF16: {
7122 bool Ignored;
7123 // This can return overflow, underflow, or inexact; we don't care.
7124 // FIXME need to be more flexible about rounding mode.
7125 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7126 : APFloat::BFloat(),
7128 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7129 }
7130 case ISD::BITCAST:
7131 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7132 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7133 VT);
7134 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7135 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7136 VT);
7137 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7138 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7139 VT);
7140 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7141 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7142 break;
7143 }
7144 }
7145
7146 // Early-out if we failed to constant fold a bitcast.
7147 if (Opcode == ISD::BITCAST)
7148 return SDValue();
7149 }
7150
7151 // Handle binops special cases.
7152 if (NumOps == 2) {
7153 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7154 return CFP;
7155
7156 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7157 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7158 if (C1->isOpaque() || C2->isOpaque())
7159 return SDValue();
7160
7161 std::optional<APInt> FoldAttempt =
7162 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7163 if (!FoldAttempt)
7164 return SDValue();
7165
7166 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7167 assert((!Folded || !VT.isVector()) &&
7168 "Can't fold vectors ops with scalar operands");
7169 return Folded;
7170 }
7171 }
7172
7173 // fold (add Sym, c) -> Sym+c
7175 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7176 if (TLI->isCommutativeBinOp(Opcode))
7178 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7179
7180 // fold (sext_in_reg c1) -> c2
7181 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7182 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7183
7184 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7185 unsigned FromBits = EVT.getScalarSizeInBits();
7186 Val <<= Val.getBitWidth() - FromBits;
7187 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7188 return getConstant(Val, DL, ConstantVT);
7189 };
7190
7191 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7192 const APInt &Val = C1->getAPIntValue();
7193 return SignExtendInReg(Val, VT);
7194 }
7195
7197 SmallVector<SDValue, 8> ScalarOps;
7198 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7199 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7200 SDValue Op = Ops[0].getOperand(I);
7201 if (Op.isUndef()) {
7202 ScalarOps.push_back(getUNDEF(OpVT));
7203 continue;
7204 }
7205 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7206 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7207 }
7208 return getBuildVector(VT, DL, ScalarOps);
7209 }
7210
7211 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7212 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7213 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7214 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7215 Ops[0].getOperand(0).getValueType()));
7216 }
7217 }
7218
7219 // Handle fshl/fshr special cases.
7220 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7221 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7222 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7223 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7224
7225 if (C1 && C2 && C3) {
7226 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7227 return SDValue();
7228 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7229 &V3 = C3->getAPIntValue();
7230
7231 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7232 : APIntOps::fshr(V1, V2, V3);
7233 return getConstant(FoldedVal, DL, VT);
7234 }
7235 }
7236
7237 // Handle fma/fmad special cases.
7238 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7239 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7240 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7241 Ops[2].getValueType() == VT && "FMA types must match!");
7245 if (C1 && C2 && C3) {
7246 APFloat V1 = C1->getValueAPF();
7247 const APFloat &V2 = C2->getValueAPF();
7248 const APFloat &V3 = C3->getValueAPF();
7249 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7252 } else
7254 return getConstantFP(V1, DL, VT);
7255 }
7256 }
7257
7258 // This is for vector folding only from here on.
7259 if (!VT.isVector())
7260 return SDValue();
7261
7262 ElementCount NumElts = VT.getVectorElementCount();
7263
7264 // See if we can fold through any bitcasted integer ops.
7265 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7266 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7267 (Ops[0].getOpcode() == ISD::BITCAST ||
7268 Ops[1].getOpcode() == ISD::BITCAST)) {
7271 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7272 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7273 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7274 N2.getValueType().isInteger()) {
7275 bool IsLE = getDataLayout().isLittleEndian();
7276 unsigned EltBits = VT.getScalarSizeInBits();
7277 SmallVector<APInt> RawBits1, RawBits2;
7278 BitVector UndefElts1, UndefElts2;
7279 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7280 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7281 SmallVector<APInt> RawBits;
7282 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7283 std::optional<APInt> Fold = FoldValueWithUndef(
7284 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7285 if (!Fold)
7286 break;
7287 RawBits.push_back(*Fold);
7288 }
7289 if (RawBits.size() == NumElts.getFixedValue()) {
7290 // We have constant folded, but we might need to cast this again back
7291 // to the original (possibly legalized) type.
7292 EVT BVVT, BVEltVT;
7293 if (N1.getValueType() == VT) {
7294 BVVT = N1.getValueType();
7295 BVEltVT = BV1->getOperand(0).getValueType();
7296 } else {
7297 BVVT = N2.getValueType();
7298 BVEltVT = BV2->getOperand(0).getValueType();
7299 }
7300 unsigned BVEltBits = BVEltVT.getSizeInBits();
7301 SmallVector<APInt> DstBits;
7302 BitVector DstUndefs;
7304 DstBits, RawBits, DstUndefs,
7305 BitVector(RawBits.size(), false));
7306 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7307 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7308 if (DstUndefs[I])
7309 continue;
7310 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7311 }
7312 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7313 }
7314 }
7315 }
7316 }
7317
7318 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7319 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7320 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7321 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7322 APInt RHSVal;
7323 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7324 APInt NewStep = Opcode == ISD::MUL
7325 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7326 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7327 return getStepVector(DL, VT, NewStep);
7328 }
7329 }
7330
7331 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7332 return !Op.getValueType().isVector() ||
7333 Op.getValueType().getVectorElementCount() == NumElts;
7334 };
7335
7336 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7337 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7338 Op.getOpcode() == ISD::BUILD_VECTOR ||
7339 Op.getOpcode() == ISD::SPLAT_VECTOR;
7340 };
7341
7342 // All operands must be vector types with the same number of elements as
7343 // the result type and must be either UNDEF or a build/splat vector
7344 // or UNDEF scalars.
7345 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7346 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7347 return SDValue();
7348
7349 // If we are comparing vectors, then the result needs to be a i1 boolean that
7350 // is then extended back to the legal result type depending on how booleans
7351 // are represented.
7352 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7353 ISD::NodeType ExtendCode =
7354 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7355 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7357
7358 // Find legal integer scalar type for constant promotion and
7359 // ensure that its scalar size is at least as large as source.
7360 EVT LegalSVT = VT.getScalarType();
7361 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7362 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7363 if (LegalSVT.bitsLT(VT.getScalarType()))
7364 return SDValue();
7365 }
7366
7367 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7368 // only have one operand to check. For fixed-length vector types we may have
7369 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7370 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7371
7372 // Constant fold each scalar lane separately.
7373 SmallVector<SDValue, 4> ScalarResults;
7374 for (unsigned I = 0; I != NumVectorElts; I++) {
7375 SmallVector<SDValue, 4> ScalarOps;
7376 for (SDValue Op : Ops) {
7377 EVT InSVT = Op.getValueType().getScalarType();
7378 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7379 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7380 if (Op.isUndef())
7381 ScalarOps.push_back(getUNDEF(InSVT));
7382 else
7383 ScalarOps.push_back(Op);
7384 continue;
7385 }
7386
7387 SDValue ScalarOp =
7388 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7389 EVT ScalarVT = ScalarOp.getValueType();
7390
7391 // Build vector (integer) scalar operands may need implicit
7392 // truncation - do this before constant folding.
7393 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7394 // Don't create illegally-typed nodes unless they're constants or undef
7395 // - if we fail to constant fold we can't guarantee the (dead) nodes
7396 // we're creating will be cleaned up before being visited for
7397 // legalization.
7398 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7399 !isa<ConstantSDNode>(ScalarOp) &&
7400 TLI->getTypeAction(*getContext(), InSVT) !=
7402 return SDValue();
7403 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7404 }
7405
7406 ScalarOps.push_back(ScalarOp);
7407 }
7408
7409 // Constant fold the scalar operands.
7410 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7411
7412 // Scalar folding only succeeded if the result is a constant or UNDEF.
7413 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7414 ScalarResult.getOpcode() != ISD::ConstantFP)
7415 return SDValue();
7416
7417 // Legalize the (integer) scalar constant if necessary. We only do
7418 // this once we know the folding succeeded, since otherwise we would
7419 // get a node with illegal type which has a user.
7420 if (LegalSVT != SVT)
7421 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7422
7423 ScalarResults.push_back(ScalarResult);
7424 }
7425
7426 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7427 : getBuildVector(VT, DL, ScalarResults);
7428 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7429 return V;
7430}
7431
7434 // TODO: Add support for unary/ternary fp opcodes.
7435 if (Ops.size() != 2)
7436 return SDValue();
7437
7438 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7439 // should. That will require dealing with a potentially non-default
7440 // rounding mode, checking the "opStatus" return value from the APFloat
7441 // math calculations, and possibly other variations.
7442 SDValue N1 = Ops[0];
7443 SDValue N2 = Ops[1];
7444 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7445 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7446 if (N1CFP && N2CFP) {
7447 APFloat C1 = N1CFP->getValueAPF(); // make copy
7448 const APFloat &C2 = N2CFP->getValueAPF();
7449 switch (Opcode) {
7450 case ISD::FADD:
7452 return getConstantFP(C1, DL, VT);
7453 case ISD::FSUB:
7455 return getConstantFP(C1, DL, VT);
7456 case ISD::FMUL:
7458 return getConstantFP(C1, DL, VT);
7459 case ISD::FDIV:
7461 return getConstantFP(C1, DL, VT);
7462 case ISD::FREM:
7463 C1.mod(C2);
7464 return getConstantFP(C1, DL, VT);
7465 case ISD::FCOPYSIGN:
7466 C1.copySign(C2);
7467 return getConstantFP(C1, DL, VT);
7468 case ISD::FMINNUM:
7469 if (C1.isSignaling() || C2.isSignaling())
7470 return SDValue();
7471 return getConstantFP(minnum(C1, C2), DL, VT);
7472 case ISD::FMAXNUM:
7473 if (C1.isSignaling() || C2.isSignaling())
7474 return SDValue();
7475 return getConstantFP(maxnum(C1, C2), DL, VT);
7476 case ISD::FMINIMUM:
7477 return getConstantFP(minimum(C1, C2), DL, VT);
7478 case ISD::FMAXIMUM:
7479 return getConstantFP(maximum(C1, C2), DL, VT);
7480 case ISD::FMINIMUMNUM:
7481 return getConstantFP(minimumnum(C1, C2), DL, VT);
7482 case ISD::FMAXIMUMNUM:
7483 return getConstantFP(maximumnum(C1, C2), DL, VT);
7484 default: break;
7485 }
7486 }
7487 if (N1CFP && Opcode == ISD::FP_ROUND) {
7488 APFloat C1 = N1CFP->getValueAPF(); // make copy
7489 bool Unused;
7490 // This can return overflow, underflow, or inexact; we don't care.
7491 // FIXME need to be more flexible about rounding mode.
7493 &Unused);
7494 return getConstantFP(C1, DL, VT);
7495 }
7496
7497 switch (Opcode) {
7498 case ISD::FSUB:
7499 // -0.0 - undef --> undef (consistent with "fneg undef")
7500 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7501 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7502 return getUNDEF(VT);
7503 [[fallthrough]];
7504
7505 case ISD::FADD:
7506 case ISD::FMUL:
7507 case ISD::FDIV:
7508 case ISD::FREM:
7509 // If both operands are undef, the result is undef. If 1 operand is undef,
7510 // the result is NaN. This should match the behavior of the IR optimizer.
7511 if (N1.isUndef() && N2.isUndef())
7512 return getUNDEF(VT);
7513 if (N1.isUndef() || N2.isUndef())
7515 }
7516 return SDValue();
7517}
7518
7520 const SDLoc &DL, EVT DstEltVT) {
7521 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7522
7523 // If this is already the right type, we're done.
7524 if (SrcEltVT == DstEltVT)
7525 return SDValue(BV, 0);
7526
7527 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7528 unsigned DstBitSize = DstEltVT.getSizeInBits();
7529
7530 // If this is a conversion of N elements of one type to N elements of another
7531 // type, convert each element. This handles FP<->INT cases.
7532 if (SrcBitSize == DstBitSize) {
7534 for (SDValue Op : BV->op_values()) {
7535 // If the vector element type is not legal, the BUILD_VECTOR operands
7536 // are promoted and implicitly truncated. Make that explicit here.
7537 if (Op.getValueType() != SrcEltVT)
7538 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7539 Ops.push_back(getBitcast(DstEltVT, Op));
7540 }
7541 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7543 return getBuildVector(VT, DL, Ops);
7544 }
7545
7546 // Otherwise, we're growing or shrinking the elements. To avoid having to
7547 // handle annoying details of growing/shrinking FP values, we convert them to
7548 // int first.
7549 if (SrcEltVT.isFloatingPoint()) {
7550 // Convert the input float vector to a int vector where the elements are the
7551 // same sizes.
7552 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7553 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7555 DstEltVT);
7556 return SDValue();
7557 }
7558
7559 // Now we know the input is an integer vector. If the output is a FP type,
7560 // convert to integer first, then to FP of the right size.
7561 if (DstEltVT.isFloatingPoint()) {
7562 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7563 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7565 DstEltVT);
7566 return SDValue();
7567 }
7568
7569 // Okay, we know the src/dst types are both integers of differing types.
7570 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7571
7572 // Extract the constant raw bit data.
7573 BitVector UndefElements;
7574 SmallVector<APInt> RawBits;
7575 bool IsLE = getDataLayout().isLittleEndian();
7576 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7577 return SDValue();
7578
7580 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7581 if (UndefElements[I])
7582 Ops.push_back(getUNDEF(DstEltVT));
7583 else
7584 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7585 }
7586
7587 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7588 return getBuildVector(VT, DL, Ops);
7589}
7590
7592 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7593
7594 // There's no need to assert on a byte-aligned pointer. All pointers are at
7595 // least byte aligned.
7596 if (A == Align(1))
7597 return Val;
7598
7599 SDVTList VTs = getVTList(Val.getValueType());
7601 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7602 ID.AddInteger(A.value());
7603
7604 void *IP = nullptr;
7605 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7606 return SDValue(E, 0);
7607
7608 auto *N =
7609 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7610 createOperands(N, {Val});
7611
7612 CSEMap.InsertNode(N, IP);
7613 InsertNode(N);
7614
7615 SDValue V(N, 0);
7616 NewSDValueDbgMsg(V, "Creating new node: ", this);
7617 return V;
7618}
7619
7620SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7621 SDValue N1, SDValue N2) {
7622 SDNodeFlags Flags;
7623 if (Inserter)
7624 Flags = Inserter->getFlags();
7625 return getNode(Opcode, DL, VT, N1, N2, Flags);
7626}
7627
7629 SDValue &N2) const {
7630 if (!TLI->isCommutativeBinOp(Opcode))
7631 return;
7632
7633 // Canonicalize:
7634 // binop(const, nonconst) -> binop(nonconst, const)
7637 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7638 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7639 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7640 std::swap(N1, N2);
7641
7642 // Canonicalize:
7643 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7644 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7646 std::swap(N1, N2);
7647}
7648
7649SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7650 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7652 N2.getOpcode() != ISD::DELETED_NODE &&
7653 "Operand is DELETED_NODE!");
7654
7655 canonicalizeCommutativeBinop(Opcode, N1, N2);
7656
7657 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7658 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7659
7660 // Don't allow undefs in vector splats - we might be returning N2 when folding
7661 // to zero etc.
7662 ConstantSDNode *N2CV =
7663 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7664
7665 switch (Opcode) {
7666 default: break;
7667 case ISD::TokenFactor:
7668 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7669 N2.getValueType() == MVT::Other && "Invalid token factor!");
7670 // Fold trivial token factors.
7671 if (N1.getOpcode() == ISD::EntryToken) return N2;
7672 if (N2.getOpcode() == ISD::EntryToken) return N1;
7673 if (N1 == N2) return N1;
7674 break;
7675 case ISD::BUILD_VECTOR: {
7676 // Attempt to simplify BUILD_VECTOR.
7677 SDValue Ops[] = {N1, N2};
7678 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7679 return V;
7680 break;
7681 }
7682 case ISD::CONCAT_VECTORS: {
7683 SDValue Ops[] = {N1, N2};
7684 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7685 return V;
7686 break;
7687 }
7688 case ISD::AND:
7689 assert(VT.isInteger() && "This operator does not apply to FP types!");
7690 assert(N1.getValueType() == N2.getValueType() &&
7691 N1.getValueType() == VT && "Binary operator types must match!");
7692 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7693 // worth handling here.
7694 if (N2CV && N2CV->isZero())
7695 return N2;
7696 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7697 return N1;
7698 break;
7699 case ISD::OR:
7700 case ISD::XOR:
7701 case ISD::ADD:
7702 case ISD::PTRADD:
7703 case ISD::SUB:
7704 assert(VT.isInteger() && "This operator does not apply to FP types!");
7705 assert(N1.getValueType() == N2.getValueType() &&
7706 N1.getValueType() == VT && "Binary operator types must match!");
7707 // The equal operand types requirement is unnecessarily strong for PTRADD.
7708 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7709 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7710 // logic everywhere where PTRADDs may be folded or combined to properly
7711 // support them. If/when we introduce pointer types to the SDAG, we will
7712 // need to relax this constraint.
7713
7714 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7715 // it's worth handling here.
7716 if (N2CV && N2CV->isZero())
7717 return N1;
7718 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7719 VT.getScalarType() == MVT::i1)
7720 return getNode(ISD::XOR, DL, VT, N1, N2);
7721 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7722 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7723 N2.getOpcode() == ISD::VSCALE) {
7724 const APInt &C1 = N1->getConstantOperandAPInt(0);
7725 const APInt &C2 = N2->getConstantOperandAPInt(0);
7726 return getVScale(DL, VT, C1 + C2);
7727 }
7728 break;
7729 case ISD::MUL:
7730 assert(VT.isInteger() && "This operator does not apply to FP types!");
7731 assert(N1.getValueType() == N2.getValueType() &&
7732 N1.getValueType() == VT && "Binary operator types must match!");
7733 if (VT.getScalarType() == MVT::i1)
7734 return getNode(ISD::AND, DL, VT, N1, N2);
7735 if (N2CV && N2CV->isZero())
7736 return N2;
7737 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7738 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7739 const APInt &N2CImm = N2C->getAPIntValue();
7740 return getVScale(DL, VT, MulImm * N2CImm);
7741 }
7742 break;
7743 case ISD::UDIV:
7744 case ISD::UREM:
7745 case ISD::MULHU:
7746 case ISD::MULHS:
7747 case ISD::SDIV:
7748 case ISD::SREM:
7749 case ISD::SADDSAT:
7750 case ISD::SSUBSAT:
7751 case ISD::UADDSAT:
7752 case ISD::USUBSAT:
7753 assert(VT.isInteger() && "This operator does not apply to FP types!");
7754 assert(N1.getValueType() == N2.getValueType() &&
7755 N1.getValueType() == VT && "Binary operator types must match!");
7756 if (VT.getScalarType() == MVT::i1) {
7757 // fold (add_sat x, y) -> (or x, y) for bool types.
7758 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7759 return getNode(ISD::OR, DL, VT, N1, N2);
7760 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7761 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7762 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7763 }
7764 break;
7765 case ISD::SCMP:
7766 case ISD::UCMP:
7767 assert(N1.getValueType() == N2.getValueType() &&
7768 "Types of operands of UCMP/SCMP must match");
7769 assert(N1.getValueType().isVector() == VT.isVector() &&
7770 "Operands and return type of must both be scalars or vectors");
7771 if (VT.isVector())
7774 "Result and operands must have the same number of elements");
7775 break;
7776 case ISD::AVGFLOORS:
7777 case ISD::AVGFLOORU:
7778 case ISD::AVGCEILS:
7779 case ISD::AVGCEILU:
7780 assert(VT.isInteger() && "This operator does not apply to FP types!");
7781 assert(N1.getValueType() == N2.getValueType() &&
7782 N1.getValueType() == VT && "Binary operator types must match!");
7783 break;
7784 case ISD::ABDS:
7785 case ISD::ABDU:
7786 assert(VT.isInteger() && "This operator does not apply to FP types!");
7787 assert(N1.getValueType() == N2.getValueType() &&
7788 N1.getValueType() == VT && "Binary operator types must match!");
7789 if (VT.getScalarType() == MVT::i1)
7790 return getNode(ISD::XOR, DL, VT, N1, N2);
7791 break;
7792 case ISD::SMIN:
7793 case ISD::UMAX:
7794 assert(VT.isInteger() && "This operator does not apply to FP types!");
7795 assert(N1.getValueType() == N2.getValueType() &&
7796 N1.getValueType() == VT && "Binary operator types must match!");
7797 if (VT.getScalarType() == MVT::i1)
7798 return getNode(ISD::OR, DL, VT, N1, N2);
7799 break;
7800 case ISD::SMAX:
7801 case ISD::UMIN:
7802 assert(VT.isInteger() && "This operator does not apply to FP types!");
7803 assert(N1.getValueType() == N2.getValueType() &&
7804 N1.getValueType() == VT && "Binary operator types must match!");
7805 if (VT.getScalarType() == MVT::i1)
7806 return getNode(ISD::AND, DL, VT, N1, N2);
7807 break;
7808 case ISD::FADD:
7809 case ISD::FSUB:
7810 case ISD::FMUL:
7811 case ISD::FDIV:
7812 case ISD::FREM:
7813 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7814 assert(N1.getValueType() == N2.getValueType() &&
7815 N1.getValueType() == VT && "Binary operator types must match!");
7816 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7817 return V;
7818 break;
7819 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7820 assert(N1.getValueType() == VT &&
7823 "Invalid FCOPYSIGN!");
7824 break;
7825 case ISD::SHL:
7826 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7827 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7828 const APInt &ShiftImm = N2C->getAPIntValue();
7829 return getVScale(DL, VT, MulImm << ShiftImm);
7830 }
7831 [[fallthrough]];
7832 case ISD::SRA:
7833 case ISD::SRL:
7834 if (SDValue V = simplifyShift(N1, N2))
7835 return V;
7836 [[fallthrough]];
7837 case ISD::ROTL:
7838 case ISD::ROTR:
7839 case ISD::SSHLSAT:
7840 case ISD::USHLSAT:
7841 assert(VT == N1.getValueType() &&
7842 "Shift operators return type must be the same as their first arg");
7843 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7844 "Shifts only work on integers");
7845 assert((!VT.isVector() || VT == N2.getValueType()) &&
7846 "Vector shift amounts must be in the same as their first arg");
7847 // Verify that the shift amount VT is big enough to hold valid shift
7848 // amounts. This catches things like trying to shift an i1024 value by an
7849 // i8, which is easy to fall into in generic code that uses
7850 // TLI.getShiftAmount().
7853 "Invalid use of small shift amount with oversized value!");
7854
7855 // Always fold shifts of i1 values so the code generator doesn't need to
7856 // handle them. Since we know the size of the shift has to be less than the
7857 // size of the value, the shift/rotate count is guaranteed to be zero.
7858 if (VT == MVT::i1)
7859 return N1;
7860 if (N2CV && N2CV->isZero())
7861 return N1;
7862 break;
7863 case ISD::FP_ROUND:
7865 VT.bitsLE(N1.getValueType()) && N2C &&
7866 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7867 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7868 if (N1.getValueType() == VT) return N1; // noop conversion.
7869 break;
7870 case ISD::AssertNoFPClass: {
7872 "AssertNoFPClass is used for a non-floating type");
7873 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7874 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7875 assert(llvm::to_underlying(NoFPClass) <=
7877 "FPClassTest value too large");
7878 (void)NoFPClass;
7879 break;
7880 }
7881 case ISD::AssertSext:
7882 case ISD::AssertZext: {
7883 EVT EVT = cast<VTSDNode>(N2)->getVT();
7884 assert(VT == N1.getValueType() && "Not an inreg extend!");
7885 assert(VT.isInteger() && EVT.isInteger() &&
7886 "Cannot *_EXTEND_INREG FP types");
7887 assert(!EVT.isVector() &&
7888 "AssertSExt/AssertZExt type should be the vector element type "
7889 "rather than the vector type!");
7890 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7891 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7892 break;
7893 }
7895 EVT EVT = cast<VTSDNode>(N2)->getVT();
7896 assert(VT == N1.getValueType() && "Not an inreg extend!");
7897 assert(VT.isInteger() && EVT.isInteger() &&
7898 "Cannot *_EXTEND_INREG FP types");
7899 assert(EVT.isVector() == VT.isVector() &&
7900 "SIGN_EXTEND_INREG type should be vector iff the operand "
7901 "type is vector!");
7902 assert((!EVT.isVector() ||
7904 "Vector element counts must match in SIGN_EXTEND_INREG");
7905 assert(EVT.bitsLE(VT) && "Not extending!");
7906 if (EVT == VT) return N1; // Not actually extending
7907 break;
7908 }
7910 case ISD::FP_TO_UINT_SAT: {
7911 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7912 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7913 assert(N1.getValueType().isVector() == VT.isVector() &&
7914 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7915 "vector!");
7916 assert((!VT.isVector() || VT.getVectorElementCount() ==
7918 "Vector element counts must match in FP_TO_*INT_SAT");
7919 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7920 "Type to saturate to must be a scalar.");
7921 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7922 "Not extending!");
7923 break;
7924 }
7927 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7928 element type of the vector.");
7929
7930 // Extract from an undefined value or using an undefined index is undefined.
7931 if (N1.isUndef() || N2.isUndef())
7932 return getUNDEF(VT);
7933
7934 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7935 // vectors. For scalable vectors we will provide appropriate support for
7936 // dealing with arbitrary indices.
7937 if (N2C && N1.getValueType().isFixedLengthVector() &&
7938 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7939 return getUNDEF(VT);
7940
7941 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7942 // expanding copies of large vectors from registers. This only works for
7943 // fixed length vectors, since we need to know the exact number of
7944 // elements.
7945 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7947 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7948 return getExtractVectorElt(DL, VT,
7949 N1.getOperand(N2C->getZExtValue() / Factor),
7950 N2C->getZExtValue() % Factor);
7951 }
7952
7953 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7954 // lowering is expanding large vector constants.
7955 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7956 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7959 "BUILD_VECTOR used for scalable vectors");
7960 unsigned Index =
7961 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7962 SDValue Elt = N1.getOperand(Index);
7963
7964 if (VT != Elt.getValueType())
7965 // If the vector element type is not legal, the BUILD_VECTOR operands
7966 // are promoted and implicitly truncated, and the result implicitly
7967 // extended. Make that explicit here.
7968 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7969
7970 return Elt;
7971 }
7972
7973 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7974 // operations are lowered to scalars.
7975 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7976 // If the indices are the same, return the inserted element else
7977 // if the indices are known different, extract the element from
7978 // the original vector.
7979 SDValue N1Op2 = N1.getOperand(2);
7981
7982 if (N1Op2C && N2C) {
7983 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7984 if (VT == N1.getOperand(1).getValueType())
7985 return N1.getOperand(1);
7986 if (VT.isFloatingPoint()) {
7988 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7989 }
7990 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7991 }
7992 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7993 }
7994 }
7995
7996 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7997 // when vector types are scalarized and v1iX is legal.
7998 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7999 // Here we are completely ignoring the extract element index (N2),
8000 // which is fine for fixed width vectors, since any index other than 0
8001 // is undefined anyway. However, this cannot be ignored for scalable
8002 // vectors - in theory we could support this, but we don't want to do this
8003 // without a profitability check.
8004 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8006 N1.getValueType().getVectorNumElements() == 1) {
8007 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8008 N1.getOperand(1));
8009 }
8010 break;
8012 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8013 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8014 (N1.getValueType().isInteger() == VT.isInteger()) &&
8015 N1.getValueType() != VT &&
8016 "Wrong types for EXTRACT_ELEMENT!");
8017
8018 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8019 // 64-bit integers into 32-bit parts. Instead of building the extract of
8020 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8021 if (N1.getOpcode() == ISD::BUILD_PAIR)
8022 return N1.getOperand(N2C->getZExtValue());
8023
8024 // EXTRACT_ELEMENT of a constant int is also very common.
8025 if (N1C) {
8026 unsigned ElementSize = VT.getSizeInBits();
8027 unsigned Shift = ElementSize * N2C->getZExtValue();
8028 const APInt &Val = N1C->getAPIntValue();
8029 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8030 }
8031 break;
8033 EVT N1VT = N1.getValueType();
8034 assert(VT.isVector() && N1VT.isVector() &&
8035 "Extract subvector VTs must be vectors!");
8037 "Extract subvector VTs must have the same element type!");
8038 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8039 "Cannot extract a scalable vector from a fixed length vector!");
8040 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8042 "Extract subvector must be from larger vector to smaller vector!");
8043 assert(N2C && "Extract subvector index must be a constant");
8044 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8045 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8046 N1VT.getVectorMinNumElements()) &&
8047 "Extract subvector overflow!");
8048 assert(N2C->getAPIntValue().getBitWidth() ==
8049 TLI->getVectorIdxWidth(getDataLayout()) &&
8050 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8051 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8052 "Extract index is not a multiple of the output vector length");
8053
8054 // Trivial extraction.
8055 if (VT == N1VT)
8056 return N1;
8057
8058 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8059 if (N1.isUndef())
8060 return getUNDEF(VT);
8061
8062 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8063 // the concat have the same type as the extract.
8064 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8065 VT == N1.getOperand(0).getValueType()) {
8066 unsigned Factor = VT.getVectorMinNumElements();
8067 return N1.getOperand(N2C->getZExtValue() / Factor);
8068 }
8069
8070 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8071 // during shuffle legalization.
8072 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8073 VT == N1.getOperand(1).getValueType())
8074 return N1.getOperand(1);
8075 break;
8076 }
8077 }
8078
8079 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8080 switch (Opcode) {
8081 case ISD::XOR:
8082 case ISD::ADD:
8083 case ISD::PTRADD:
8084 case ISD::SUB:
8086 case ISD::UDIV:
8087 case ISD::SDIV:
8088 case ISD::UREM:
8089 case ISD::SREM:
8090 case ISD::MUL:
8091 case ISD::AND:
8092 case ISD::SSUBSAT:
8093 case ISD::USUBSAT:
8094 case ISD::UMIN:
8095 case ISD::OR:
8096 case ISD::SADDSAT:
8097 case ISD::UADDSAT:
8098 case ISD::UMAX:
8099 case ISD::SMAX:
8100 case ISD::SMIN:
8101 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8102 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8103 }
8104 }
8105
8106 // Canonicalize an UNDEF to the RHS, even over a constant.
8107 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8108 if (TLI->isCommutativeBinOp(Opcode)) {
8109 std::swap(N1, N2);
8110 } else {
8111 switch (Opcode) {
8112 case ISD::PTRADD:
8113 case ISD::SUB:
8114 // fold op(undef, non_undef_arg2) -> undef.
8115 return N1;
8117 case ISD::UDIV:
8118 case ISD::SDIV:
8119 case ISD::UREM:
8120 case ISD::SREM:
8121 case ISD::SSUBSAT:
8122 case ISD::USUBSAT:
8123 // fold op(undef, non_undef_arg2) -> 0.
8124 return getConstant(0, DL, VT);
8125 }
8126 }
8127 }
8128
8129 // Fold a bunch of operators when the RHS is undef.
8130 if (N2.getOpcode() == ISD::UNDEF) {
8131 switch (Opcode) {
8132 case ISD::XOR:
8133 if (N1.getOpcode() == ISD::UNDEF)
8134 // Handle undef ^ undef -> 0 special case. This is a common
8135 // idiom (misuse).
8136 return getConstant(0, DL, VT);
8137 [[fallthrough]];
8138 case ISD::ADD:
8139 case ISD::PTRADD:
8140 case ISD::SUB:
8141 // fold op(arg1, undef) -> undef.
8142 return N2;
8143 case ISD::UDIV:
8144 case ISD::SDIV:
8145 case ISD::UREM:
8146 case ISD::SREM:
8147 // fold op(arg1, undef) -> poison.
8148 return getPOISON(VT);
8149 case ISD::MUL:
8150 case ISD::AND:
8151 case ISD::SSUBSAT:
8152 case ISD::USUBSAT:
8153 case ISD::UMIN:
8154 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8155 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8156 case ISD::OR:
8157 case ISD::SADDSAT:
8158 case ISD::UADDSAT:
8159 case ISD::UMAX:
8160 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8161 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8162 case ISD::SMAX:
8163 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8164 return N1.getOpcode() == ISD::UNDEF
8165 ? N2
8166 : getConstant(
8168 VT);
8169 case ISD::SMIN:
8170 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8171 return N1.getOpcode() == ISD::UNDEF
8172 ? N2
8173 : getConstant(
8175 VT);
8176 }
8177 }
8178
8179 // Perform trivial constant folding.
8180 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8181 return SV;
8182
8183 // Memoize this node if possible.
8184 SDNode *N;
8185 SDVTList VTs = getVTList(VT);
8186 SDValue Ops[] = {N1, N2};
8187 if (VT != MVT::Glue) {
8189 AddNodeIDNode(ID, Opcode, VTs, Ops);
8190 void *IP = nullptr;
8191 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8192 E->intersectFlagsWith(Flags);
8193 return SDValue(E, 0);
8194 }
8195
8196 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8197 N->setFlags(Flags);
8198 createOperands(N, Ops);
8199 CSEMap.InsertNode(N, IP);
8200 } else {
8201 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8202 createOperands(N, Ops);
8203 }
8204
8205 InsertNode(N);
8206 SDValue V = SDValue(N, 0);
8207 NewSDValueDbgMsg(V, "Creating new node: ", this);
8208 return V;
8209}
8210
8211SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8212 SDValue N1, SDValue N2, SDValue N3) {
8213 SDNodeFlags Flags;
8214 if (Inserter)
8215 Flags = Inserter->getFlags();
8216 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8217}
8218
8219SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8220 SDValue N1, SDValue N2, SDValue N3,
8221 const SDNodeFlags Flags) {
8223 N2.getOpcode() != ISD::DELETED_NODE &&
8224 N3.getOpcode() != ISD::DELETED_NODE &&
8225 "Operand is DELETED_NODE!");
8226 // Perform various simplifications.
8227 switch (Opcode) {
8228 case ISD::BUILD_VECTOR: {
8229 // Attempt to simplify BUILD_VECTOR.
8230 SDValue Ops[] = {N1, N2, N3};
8231 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8232 return V;
8233 break;
8234 }
8235 case ISD::CONCAT_VECTORS: {
8236 SDValue Ops[] = {N1, N2, N3};
8237 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8238 return V;
8239 break;
8240 }
8241 case ISD::SETCC: {
8242 assert(VT.isInteger() && "SETCC result type must be an integer!");
8243 assert(N1.getValueType() == N2.getValueType() &&
8244 "SETCC operands must have the same type!");
8245 assert(VT.isVector() == N1.getValueType().isVector() &&
8246 "SETCC type should be vector iff the operand type is vector!");
8247 assert((!VT.isVector() || VT.getVectorElementCount() ==
8249 "SETCC vector element counts must match!");
8250 // Use FoldSetCC to simplify SETCC's.
8251 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8252 return V;
8253 break;
8254 }
8255 case ISD::SELECT:
8256 case ISD::VSELECT:
8257 if (SDValue V = simplifySelect(N1, N2, N3))
8258 return V;
8259 break;
8261 llvm_unreachable("should use getVectorShuffle constructor!");
8263 if (isNullConstant(N3))
8264 return N1;
8265 break;
8267 if (isNullConstant(N3))
8268 return N2;
8269 break;
8271 assert(VT.isVector() && VT == N1.getValueType() &&
8272 "INSERT_VECTOR_ELT vector type mismatch");
8274 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8275 assert((!VT.isFloatingPoint() ||
8276 VT.getVectorElementType() == N2.getValueType()) &&
8277 "INSERT_VECTOR_ELT fp scalar type mismatch");
8278 assert((!VT.isInteger() ||
8280 "INSERT_VECTOR_ELT int scalar size mismatch");
8281
8282 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8283 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8284 // for scalable vectors where we will generate appropriate code to
8285 // deal with out-of-bounds cases correctly.
8286 if (N3C && VT.isFixedLengthVector() &&
8287 N3C->getZExtValue() >= VT.getVectorNumElements())
8288 return getUNDEF(VT);
8289
8290 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8291 if (N3.isUndef())
8292 return getUNDEF(VT);
8293
8294 // If inserting poison, just use the input vector.
8295 if (N2.getOpcode() == ISD::POISON)
8296 return N1;
8297
8298 // Inserting undef into undef/poison is still undef.
8299 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8300 return getUNDEF(VT);
8301
8302 // If the inserted element is an UNDEF, just use the input vector.
8303 // But not if skipping the insert could make the result more poisonous.
8304 if (N2.isUndef()) {
8305 if (N3C && VT.isFixedLengthVector()) {
8306 APInt EltMask =
8307 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8308 if (isGuaranteedNotToBePoison(N1, EltMask))
8309 return N1;
8310 } else if (isGuaranteedNotToBePoison(N1))
8311 return N1;
8312 }
8313 break;
8314 }
8315 case ISD::INSERT_SUBVECTOR: {
8316 // If inserting poison, just use the input vector,
8317 if (N2.getOpcode() == ISD::POISON)
8318 return N1;
8319
8320 // Inserting undef into undef/poison is still undef.
8321 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8322 return getUNDEF(VT);
8323
8324 EVT N2VT = N2.getValueType();
8325 assert(VT == N1.getValueType() &&
8326 "Dest and insert subvector source types must match!");
8327 assert(VT.isVector() && N2VT.isVector() &&
8328 "Insert subvector VTs must be vectors!");
8330 "Insert subvector VTs must have the same element type!");
8331 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8332 "Cannot insert a scalable vector into a fixed length vector!");
8333 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8335 "Insert subvector must be from smaller vector to larger vector!");
8337 "Insert subvector index must be constant");
8338 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8339 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8341 "Insert subvector overflow!");
8343 TLI->getVectorIdxWidth(getDataLayout()) &&
8344 "Constant index for INSERT_SUBVECTOR has an invalid size");
8345
8346 // Trivial insertion.
8347 if (VT == N2VT)
8348 return N2;
8349
8350 // If this is an insert of an extracted vector into an undef/poison vector,
8351 // we can just use the input to the extract. But not if skipping the
8352 // extract+insert could make the result more poisonous.
8353 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8354 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8355 if (N1.getOpcode() == ISD::POISON)
8356 return N2.getOperand(0);
8357 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8358 unsigned LoBit = N3->getAsZExtVal();
8359 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8360 APInt EltMask =
8361 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8362 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8363 return N2.getOperand(0);
8364 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8365 return N2.getOperand(0);
8366 }
8367
8368 // If the inserted subvector is UNDEF, just use the input vector.
8369 // But not if skipping the insert could make the result more poisonous.
8370 if (N2.isUndef()) {
8371 if (VT.isFixedLengthVector()) {
8372 unsigned LoBit = N3->getAsZExtVal();
8373 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8374 APInt EltMask =
8375 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8376 if (isGuaranteedNotToBePoison(N1, EltMask))
8377 return N1;
8378 } else if (isGuaranteedNotToBePoison(N1))
8379 return N1;
8380 }
8381 break;
8382 }
8383 case ISD::BITCAST:
8384 // Fold bit_convert nodes from a type to themselves.
8385 if (N1.getValueType() == VT)
8386 return N1;
8387 break;
8388 case ISD::VP_TRUNCATE:
8389 case ISD::VP_SIGN_EXTEND:
8390 case ISD::VP_ZERO_EXTEND:
8391 // Don't create noop casts.
8392 if (N1.getValueType() == VT)
8393 return N1;
8394 break;
8395 case ISD::VECTOR_COMPRESS: {
8396 [[maybe_unused]] EVT VecVT = N1.getValueType();
8397 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8398 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8399 assert(VT == VecVT && "Vector and result type don't match.");
8400 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8401 "All inputs must be vectors.");
8402 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8404 "Vector and mask must have same number of elements.");
8405
8406 if (N1.isUndef() || N2.isUndef())
8407 return N3;
8408
8409 break;
8410 }
8415 [[maybe_unused]] EVT AccVT = N1.getValueType();
8416 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8417 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8418 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8419 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8420 "node to have the same type!");
8421 assert(VT.isVector() && VT == AccVT &&
8422 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8423 "the same type as its result!");
8425 AccVT.getVectorElementCount()) &&
8426 "Expected the element count of the second and third operands of the "
8427 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8428 "element count of the first operand and the result!");
8430 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8431 "node to have an element type which is the same as or smaller than "
8432 "the element type of the first operand and result!");
8433 break;
8434 }
8435 }
8436
8437 // Perform trivial constant folding for arithmetic operators.
8438 switch (Opcode) {
8439 case ISD::FMA:
8440 case ISD::FMAD:
8441 case ISD::SETCC:
8442 case ISD::FSHL:
8443 case ISD::FSHR:
8444 if (SDValue SV =
8445 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8446 return SV;
8447 break;
8448 }
8449
8450 // Memoize node if it doesn't produce a glue result.
8451 SDNode *N;
8452 SDVTList VTs = getVTList(VT);
8453 SDValue Ops[] = {N1, N2, N3};
8454 if (VT != MVT::Glue) {
8456 AddNodeIDNode(ID, Opcode, VTs, Ops);
8457 void *IP = nullptr;
8458 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8459 E->intersectFlagsWith(Flags);
8460 return SDValue(E, 0);
8461 }
8462
8463 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8464 N->setFlags(Flags);
8465 createOperands(N, Ops);
8466 CSEMap.InsertNode(N, IP);
8467 } else {
8468 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8469 createOperands(N, Ops);
8470 }
8471
8472 InsertNode(N);
8473 SDValue V = SDValue(N, 0);
8474 NewSDValueDbgMsg(V, "Creating new node: ", this);
8475 return V;
8476}
8477
8478SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8479 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8480 const SDNodeFlags Flags) {
8481 SDValue Ops[] = { N1, N2, N3, N4 };
8482 return getNode(Opcode, DL, VT, Ops, Flags);
8483}
8484
8485SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8486 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8487 SDNodeFlags Flags;
8488 if (Inserter)
8489 Flags = Inserter->getFlags();
8490 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8491}
8492
8493SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8494 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8495 SDValue N5, const SDNodeFlags Flags) {
8496 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8497 return getNode(Opcode, DL, VT, Ops, Flags);
8498}
8499
8500SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8501 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8502 SDValue N5) {
8503 SDNodeFlags Flags;
8504 if (Inserter)
8505 Flags = Inserter->getFlags();
8506 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8507}
8508
8509/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8510/// the incoming stack arguments to be loaded from the stack.
8512 SmallVector<SDValue, 8> ArgChains;
8513
8514 // Include the original chain at the beginning of the list. When this is
8515 // used by target LowerCall hooks, this helps legalize find the
8516 // CALLSEQ_BEGIN node.
8517 ArgChains.push_back(Chain);
8518
8519 // Add a chain value for each stack argument.
8520 for (SDNode *U : getEntryNode().getNode()->users())
8521 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8522 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8523 if (FI->getIndex() < 0)
8524 ArgChains.push_back(SDValue(L, 1));
8525
8526 // Build a tokenfactor for all the chains.
8527 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8528}
8529
8530/// getMemsetValue - Vectorized representation of the memset value
8531/// operand.
8533 const SDLoc &dl) {
8534 assert(!Value.isUndef());
8535
8536 unsigned NumBits = VT.getScalarSizeInBits();
8538 assert(C->getAPIntValue().getBitWidth() == 8);
8539 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8540 if (VT.isInteger()) {
8541 bool IsOpaque = VT.getSizeInBits() > 64 ||
8542 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8543 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8544 }
8545 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8546 }
8547
8548 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8549 EVT IntVT = VT.getScalarType();
8550 if (!IntVT.isInteger())
8551 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8552
8553 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8554 if (NumBits > 8) {
8555 // Use a multiplication with 0x010101... to extend the input to the
8556 // required length.
8557 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8558 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8559 DAG.getConstant(Magic, dl, IntVT));
8560 }
8561
8562 if (VT != Value.getValueType() && !VT.isInteger())
8563 Value = DAG.getBitcast(VT.getScalarType(), Value);
8564 if (VT != Value.getValueType())
8565 Value = DAG.getSplatBuildVector(VT, dl, Value);
8566
8567 return Value;
8568}
8569
8570/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8571/// used when a memcpy is turned into a memset when the source is a constant
8572/// string ptr.
8574 const TargetLowering &TLI,
8575 const ConstantDataArraySlice &Slice) {
8576 // Handle vector with all elements zero.
8577 if (Slice.Array == nullptr) {
8578 if (VT.isInteger())
8579 return DAG.getConstant(0, dl, VT);
8580 return DAG.getNode(ISD::BITCAST, dl, VT,
8581 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8582 }
8583
8584 assert(!VT.isVector() && "Can't handle vector type here!");
8585 unsigned NumVTBits = VT.getSizeInBits();
8586 unsigned NumVTBytes = NumVTBits / 8;
8587 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8588
8589 APInt Val(NumVTBits, 0);
8590 if (DAG.getDataLayout().isLittleEndian()) {
8591 for (unsigned i = 0; i != NumBytes; ++i)
8592 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8593 } else {
8594 for (unsigned i = 0; i != NumBytes; ++i)
8595 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8596 }
8597
8598 // If the "cost" of materializing the integer immediate is less than the cost
8599 // of a load, then it is cost effective to turn the load into the immediate.
8600 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8601 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8602 return DAG.getConstant(Val, dl, VT);
8603 return SDValue();
8604}
8605
8607 const SDLoc &DL,
8608 const SDNodeFlags Flags) {
8609 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8610 return getMemBasePlusOffset(Base, Index, DL, Flags);
8611}
8612
8614 const SDLoc &DL,
8615 const SDNodeFlags Flags) {
8616 assert(Offset.getValueType().isInteger());
8617 EVT BasePtrVT = Ptr.getValueType();
8618 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8619 BasePtrVT))
8620 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8621 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8622 SDNodeFlags AddFlags = Flags;
8623 AddFlags.setInBounds(false);
8624 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8625}
8626
8627/// Returns true if memcpy source is constant data.
8629 uint64_t SrcDelta = 0;
8630 GlobalAddressSDNode *G = nullptr;
8631 if (Src.getOpcode() == ISD::GlobalAddress)
8633 else if (Src->isAnyAdd() &&
8634 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8635 Src.getOperand(1).getOpcode() == ISD::Constant) {
8636 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8637 SrcDelta = Src.getConstantOperandVal(1);
8638 }
8639 if (!G)
8640 return false;
8641
8642 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8643 SrcDelta + G->getOffset());
8644}
8645
8647 SelectionDAG &DAG) {
8648 // On Darwin, -Os means optimize for size without hurting performance, so
8649 // only really optimize for size when -Oz (MinSize) is used.
8651 return MF.getFunction().hasMinSize();
8652 return DAG.shouldOptForSize();
8653}
8654
8656 SmallVector<SDValue, 32> &OutChains, unsigned From,
8657 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8658 SmallVector<SDValue, 16> &OutStoreChains) {
8659 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8660 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8661 SmallVector<SDValue, 16> GluedLoadChains;
8662 for (unsigned i = From; i < To; ++i) {
8663 OutChains.push_back(OutLoadChains[i]);
8664 GluedLoadChains.push_back(OutLoadChains[i]);
8665 }
8666
8667 // Chain for all loads.
8668 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8669 GluedLoadChains);
8670
8671 for (unsigned i = From; i < To; ++i) {
8672 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8673 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8674 ST->getBasePtr(), ST->getMemoryVT(),
8675 ST->getMemOperand());
8676 OutChains.push_back(NewStore);
8677 }
8678}
8679
8681 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8682 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8683 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8684 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8685 // Turn a memcpy of undef to nop.
8686 // FIXME: We need to honor volatile even is Src is undef.
8687 if (Src.isUndef())
8688 return Chain;
8689
8690 // Expand memcpy to a series of load and store ops if the size operand falls
8691 // below a certain threshold.
8692 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8693 // rather than maybe a humongous number of loads and stores.
8694 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8695 const DataLayout &DL = DAG.getDataLayout();
8696 LLVMContext &C = *DAG.getContext();
8697 std::vector<EVT> MemOps;
8698 bool DstAlignCanChange = false;
8700 MachineFrameInfo &MFI = MF.getFrameInfo();
8701 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8703 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8704 DstAlignCanChange = true;
8705 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8706 if (!SrcAlign || Alignment > *SrcAlign)
8707 SrcAlign = Alignment;
8708 assert(SrcAlign && "SrcAlign must be set");
8710 // If marked as volatile, perform a copy even when marked as constant.
8711 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8712 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8713 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8714 const MemOp Op = isZeroConstant
8715 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8716 /*IsZeroMemset*/ true, isVol)
8717 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8718 *SrcAlign, isVol, CopyFromConstant);
8719 if (!TLI.findOptimalMemOpLowering(
8720 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8721 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8722 return SDValue();
8723
8724 if (DstAlignCanChange) {
8725 Type *Ty = MemOps[0].getTypeForEVT(C);
8726 Align NewAlign = DL.getABITypeAlign(Ty);
8727
8728 // Don't promote to an alignment that would require dynamic stack
8729 // realignment which may conflict with optimizations such as tail call
8730 // optimization.
8732 if (!TRI->hasStackRealignment(MF))
8733 if (MaybeAlign StackAlign = DL.getStackAlignment())
8734 NewAlign = std::min(NewAlign, *StackAlign);
8735
8736 if (NewAlign > Alignment) {
8737 // Give the stack frame object a larger alignment if needed.
8738 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8739 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8740 Alignment = NewAlign;
8741 }
8742 }
8743
8744 // Prepare AAInfo for loads/stores after lowering this memcpy.
8745 AAMDNodes NewAAInfo = AAInfo;
8746 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8747
8748 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8749 bool isConstant =
8750 BatchAA && SrcVal &&
8751 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8752
8753 MachineMemOperand::Flags MMOFlags =
8755 SmallVector<SDValue, 16> OutLoadChains;
8756 SmallVector<SDValue, 16> OutStoreChains;
8757 SmallVector<SDValue, 32> OutChains;
8758 unsigned NumMemOps = MemOps.size();
8759 uint64_t SrcOff = 0, DstOff = 0;
8760 for (unsigned i = 0; i != NumMemOps; ++i) {
8761 EVT VT = MemOps[i];
8762 unsigned VTSize = VT.getSizeInBits() / 8;
8763 SDValue Value, Store;
8764
8765 if (VTSize > Size) {
8766 // Issuing an unaligned load / store pair that overlaps with the previous
8767 // pair. Adjust the offset accordingly.
8768 assert(i == NumMemOps-1 && i != 0);
8769 SrcOff -= VTSize - Size;
8770 DstOff -= VTSize - Size;
8771 }
8772
8773 if (CopyFromConstant &&
8774 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8775 // It's unlikely a store of a vector immediate can be done in a single
8776 // instruction. It would require a load from a constantpool first.
8777 // We only handle zero vectors here.
8778 // FIXME: Handle other cases where store of vector immediate is done in
8779 // a single instruction.
8780 ConstantDataArraySlice SubSlice;
8781 if (SrcOff < Slice.Length) {
8782 SubSlice = Slice;
8783 SubSlice.move(SrcOff);
8784 } else {
8785 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8786 SubSlice.Array = nullptr;
8787 SubSlice.Offset = 0;
8788 SubSlice.Length = VTSize;
8789 }
8790 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8791 if (Value.getNode()) {
8792 Store = DAG.getStore(
8793 Chain, dl, Value,
8794 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8795 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8796 OutChains.push_back(Store);
8797 }
8798 }
8799
8800 if (!Store.getNode()) {
8801 // The type might not be legal for the target. This should only happen
8802 // if the type is smaller than a legal type, as on PPC, so the right
8803 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8804 // to Load/Store if NVT==VT.
8805 // FIXME does the case above also need this?
8806 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8807 assert(NVT.bitsGE(VT));
8808
8809 bool isDereferenceable =
8810 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8811 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8812 if (isDereferenceable)
8814 if (isConstant)
8815 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8816
8817 Value = DAG.getExtLoad(
8818 ISD::EXTLOAD, dl, NVT, Chain,
8819 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8820 SrcPtrInfo.getWithOffset(SrcOff), VT,
8821 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8822 OutLoadChains.push_back(Value.getValue(1));
8823
8824 Store = DAG.getTruncStore(
8825 Chain, dl, Value,
8826 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8827 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8828 OutStoreChains.push_back(Store);
8829 }
8830 SrcOff += VTSize;
8831 DstOff += VTSize;
8832 Size -= VTSize;
8833 }
8834
8835 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8837 unsigned NumLdStInMemcpy = OutStoreChains.size();
8838
8839 if (NumLdStInMemcpy) {
8840 // It may be that memcpy might be converted to memset if it's memcpy
8841 // of constants. In such a case, we won't have loads and stores, but
8842 // just stores. In the absence of loads, there is nothing to gang up.
8843 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8844 // If target does not care, just leave as it.
8845 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8846 OutChains.push_back(OutLoadChains[i]);
8847 OutChains.push_back(OutStoreChains[i]);
8848 }
8849 } else {
8850 // Ld/St less than/equal limit set by target.
8851 if (NumLdStInMemcpy <= GluedLdStLimit) {
8852 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8853 NumLdStInMemcpy, OutLoadChains,
8854 OutStoreChains);
8855 } else {
8856 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8857 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8858 unsigned GlueIter = 0;
8859
8860 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8861 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8862 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8863
8864 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8865 OutLoadChains, OutStoreChains);
8866 GlueIter += GluedLdStLimit;
8867 }
8868
8869 // Residual ld/st.
8870 if (RemainingLdStInMemcpy) {
8871 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8872 RemainingLdStInMemcpy, OutLoadChains,
8873 OutStoreChains);
8874 }
8875 }
8876 }
8877 }
8878 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8879}
8880
8882 SDValue Chain, SDValue Dst, SDValue Src,
8883 uint64_t Size, Align Alignment,
8884 bool isVol, bool AlwaysInline,
8885 MachinePointerInfo DstPtrInfo,
8886 MachinePointerInfo SrcPtrInfo,
8887 const AAMDNodes &AAInfo) {
8888 // Turn a memmove of undef to nop.
8889 // FIXME: We need to honor volatile even is Src is undef.
8890 if (Src.isUndef())
8891 return Chain;
8892
8893 // Expand memmove to a series of load and store ops if the size operand falls
8894 // below a certain threshold.
8895 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8896 const DataLayout &DL = DAG.getDataLayout();
8897 LLVMContext &C = *DAG.getContext();
8898 std::vector<EVT> MemOps;
8899 bool DstAlignCanChange = false;
8901 MachineFrameInfo &MFI = MF.getFrameInfo();
8902 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8904 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8905 DstAlignCanChange = true;
8906 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8907 if (!SrcAlign || Alignment > *SrcAlign)
8908 SrcAlign = Alignment;
8909 assert(SrcAlign && "SrcAlign must be set");
8910 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8911 if (!TLI.findOptimalMemOpLowering(
8912 C, MemOps, Limit,
8913 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8914 /*IsVolatile*/ true),
8915 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8916 MF.getFunction().getAttributes()))
8917 return SDValue();
8918
8919 if (DstAlignCanChange) {
8920 Type *Ty = MemOps[0].getTypeForEVT(C);
8921 Align NewAlign = DL.getABITypeAlign(Ty);
8922
8923 // Don't promote to an alignment that would require dynamic stack
8924 // realignment which may conflict with optimizations such as tail call
8925 // optimization.
8927 if (!TRI->hasStackRealignment(MF))
8928 if (MaybeAlign StackAlign = DL.getStackAlignment())
8929 NewAlign = std::min(NewAlign, *StackAlign);
8930
8931 if (NewAlign > Alignment) {
8932 // Give the stack frame object a larger alignment if needed.
8933 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8934 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8935 Alignment = NewAlign;
8936 }
8937 }
8938
8939 // Prepare AAInfo for loads/stores after lowering this memmove.
8940 AAMDNodes NewAAInfo = AAInfo;
8941 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8942
8943 MachineMemOperand::Flags MMOFlags =
8945 uint64_t SrcOff = 0, DstOff = 0;
8946 SmallVector<SDValue, 8> LoadValues;
8947 SmallVector<SDValue, 8> LoadChains;
8948 SmallVector<SDValue, 8> OutChains;
8949 unsigned NumMemOps = MemOps.size();
8950 for (unsigned i = 0; i < NumMemOps; i++) {
8951 EVT VT = MemOps[i];
8952 unsigned VTSize = VT.getSizeInBits() / 8;
8953 SDValue Value;
8954
8955 bool isDereferenceable =
8956 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8957 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8958 if (isDereferenceable)
8960
8961 Value = DAG.getLoad(
8962 VT, dl, Chain,
8963 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8964 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8965 LoadValues.push_back(Value);
8966 LoadChains.push_back(Value.getValue(1));
8967 SrcOff += VTSize;
8968 }
8969 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8970 OutChains.clear();
8971 for (unsigned i = 0; i < NumMemOps; i++) {
8972 EVT VT = MemOps[i];
8973 unsigned VTSize = VT.getSizeInBits() / 8;
8974 SDValue Store;
8975
8976 Store = DAG.getStore(
8977 Chain, dl, LoadValues[i],
8978 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8979 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8980 OutChains.push_back(Store);
8981 DstOff += VTSize;
8982 }
8983
8984 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8985}
8986
8987/// Lower the call to 'memset' intrinsic function into a series of store
8988/// operations.
8989///
8990/// \param DAG Selection DAG where lowered code is placed.
8991/// \param dl Link to corresponding IR location.
8992/// \param Chain Control flow dependency.
8993/// \param Dst Pointer to destination memory location.
8994/// \param Src Value of byte to write into the memory.
8995/// \param Size Number of bytes to write.
8996/// \param Alignment Alignment of the destination in bytes.
8997/// \param isVol True if destination is volatile.
8998/// \param AlwaysInline Makes sure no function call is generated.
8999/// \param DstPtrInfo IR information on the memory pointer.
9000/// \returns New head in the control flow, if lowering was successful, empty
9001/// SDValue otherwise.
9002///
9003/// The function tries to replace 'llvm.memset' intrinsic with several store
9004/// operations and value calculation code. This is usually profitable for small
9005/// memory size or when the semantic requires inlining.
9007 SDValue Chain, SDValue Dst, SDValue Src,
9008 uint64_t Size, Align Alignment, bool isVol,
9009 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9010 const AAMDNodes &AAInfo) {
9011 // Turn a memset of undef to nop.
9012 // FIXME: We need to honor volatile even is Src is undef.
9013 if (Src.isUndef())
9014 return Chain;
9015
9016 // Expand memset to a series of load/store ops if the size operand
9017 // falls below a certain threshold.
9018 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9019 std::vector<EVT> MemOps;
9020 bool DstAlignCanChange = false;
9021 LLVMContext &C = *DAG.getContext();
9023 MachineFrameInfo &MFI = MF.getFrameInfo();
9024 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9026 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9027 DstAlignCanChange = true;
9028 bool IsZeroVal = isNullConstant(Src);
9029 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9030
9031 if (!TLI.findOptimalMemOpLowering(
9032 C, MemOps, Limit,
9033 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9034 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9035 return SDValue();
9036
9037 if (DstAlignCanChange) {
9038 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9039 const DataLayout &DL = DAG.getDataLayout();
9040 Align NewAlign = DL.getABITypeAlign(Ty);
9041
9042 // Don't promote to an alignment that would require dynamic stack
9043 // realignment which may conflict with optimizations such as tail call
9044 // optimization.
9046 if (!TRI->hasStackRealignment(MF))
9047 if (MaybeAlign StackAlign = DL.getStackAlignment())
9048 NewAlign = std::min(NewAlign, *StackAlign);
9049
9050 if (NewAlign > Alignment) {
9051 // Give the stack frame object a larger alignment if needed.
9052 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9053 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9054 Alignment = NewAlign;
9055 }
9056 }
9057
9058 SmallVector<SDValue, 8> OutChains;
9059 uint64_t DstOff = 0;
9060 unsigned NumMemOps = MemOps.size();
9061
9062 // Find the largest store and generate the bit pattern for it.
9063 EVT LargestVT = MemOps[0];
9064 for (unsigned i = 1; i < NumMemOps; i++)
9065 if (MemOps[i].bitsGT(LargestVT))
9066 LargestVT = MemOps[i];
9067 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9068
9069 // Prepare AAInfo for loads/stores after lowering this memset.
9070 AAMDNodes NewAAInfo = AAInfo;
9071 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9072
9073 for (unsigned i = 0; i < NumMemOps; i++) {
9074 EVT VT = MemOps[i];
9075 unsigned VTSize = VT.getSizeInBits() / 8;
9076 if (VTSize > Size) {
9077 // Issuing an unaligned load / store pair that overlaps with the previous
9078 // pair. Adjust the offset accordingly.
9079 assert(i == NumMemOps-1 && i != 0);
9080 DstOff -= VTSize - Size;
9081 }
9082
9083 // If this store is smaller than the largest store see whether we can get
9084 // the smaller value for free with a truncate or extract vector element and
9085 // then store.
9086 SDValue Value = MemSetValue;
9087 if (VT.bitsLT(LargestVT)) {
9088 unsigned Index;
9089 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9090 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9091 if (!LargestVT.isVector() && !VT.isVector() &&
9092 TLI.isTruncateFree(LargestVT, VT))
9093 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9094 else if (LargestVT.isVector() && !VT.isVector() &&
9096 LargestVT.getTypeForEVT(*DAG.getContext()),
9097 VT.getSizeInBits(), Index) &&
9098 TLI.isTypeLegal(SVT) &&
9099 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9100 // Target which can combine store(extractelement VectorTy, Idx) can get
9101 // the smaller value for free.
9102 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9103 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9104 } else
9105 Value = getMemsetValue(Src, VT, DAG, dl);
9106 }
9107 assert(Value.getValueType() == VT && "Value with wrong type.");
9108 SDValue Store = DAG.getStore(
9109 Chain, dl, Value,
9110 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9111 DstPtrInfo.getWithOffset(DstOff), Alignment,
9113 NewAAInfo);
9114 OutChains.push_back(Store);
9115 DstOff += VT.getSizeInBits() / 8;
9116 Size -= VTSize;
9117 }
9118
9119 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9120}
9121
9123 unsigned AS) {
9124 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9125 // pointer operands can be losslessly bitcasted to pointers of address space 0
9126 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9127 report_fatal_error("cannot lower memory intrinsic in address space " +
9128 Twine(AS));
9129 }
9130}
9131
9133 const SelectionDAG *SelDAG,
9134 bool AllowReturnsFirstArg) {
9135 if (!CI || !CI->isTailCall())
9136 return false;
9137 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9138 // helper symbol we lower to.
9139 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9140 AllowReturnsFirstArg &&
9142}
9143
9144std::pair<SDValue, SDValue>
9146 SDValue Mem1, SDValue Size, const CallInst *CI) {
9147 RTLIB::LibcallImpl MemcmpImpl = TLI->getLibcallImpl(RTLIB::MEMCMP);
9148 if (MemcmpImpl == RTLIB::Unsupported)
9149 return {};
9150
9153 {Mem0, PT},
9154 {Mem1, PT},
9156
9158 bool IsTailCall =
9159 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9160
9161 CLI.setDebugLoc(dl)
9162 .setChain(Chain)
9163 .setLibCallee(
9164 TLI->getLibcallImplCallingConv(MemcmpImpl),
9166 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9167 std::move(Args))
9168 .setTailCall(IsTailCall);
9169
9170 return TLI->LowerCallTo(CLI);
9171}
9172
9173std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9174 const SDLoc &dl,
9175 SDValue Dst, SDValue Src,
9176 const CallInst *CI) {
9177 RTLIB::LibcallImpl LCImpl = TLI->getLibcallImpl(RTLIB::STRCPY);
9178 if (LCImpl == RTLIB::Unsupported)
9179 return {};
9180
9182 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9183
9185 bool IsTailCall =
9186 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg=*/true);
9187
9188 CLI.setDebugLoc(dl)
9189 .setChain(Chain)
9190 .setLibCallee(
9191 TLI->getLibcallImplCallingConv(LCImpl), CI->getType(),
9192 getExternalSymbol(LCImpl, TLI->getPointerTy(getDataLayout())),
9193 std::move(Args))
9194 .setTailCall(IsTailCall);
9195
9196 return TLI->LowerCallTo(CLI);
9197}
9198
9199std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9200 const SDLoc &dl,
9201 SDValue Src,
9202 const CallInst *CI) {
9203 RTLIB::LibcallImpl StrlenImpl = TLI->getLibcallImpl(RTLIB::STRLEN);
9204 if (StrlenImpl == RTLIB::Unsupported)
9205 return {};
9206
9207 // Emit a library call.
9210
9212 bool IsTailCall =
9213 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9214
9215 CLI.setDebugLoc(dl)
9216 .setChain(Chain)
9217 .setLibCallee(TLI->getLibcallImplCallingConv(StrlenImpl), CI->getType(),
9219 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9220 std::move(Args))
9221 .setTailCall(IsTailCall);
9222
9223 return TLI->LowerCallTo(CLI);
9224}
9225
9227 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9228 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9229 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9230 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9231 BatchAAResults *BatchAA) {
9232 // Check to see if we should lower the memcpy to loads and stores first.
9233 // For cases within the target-specified limits, this is the best choice.
9235 if (ConstantSize) {
9236 // Memcpy with size zero? Just return the original chain.
9237 if (ConstantSize->isZero())
9238 return Chain;
9239
9241 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9242 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9243 if (Result.getNode())
9244 return Result;
9245 }
9246
9247 // Then check to see if we should lower the memcpy with target-specific
9248 // code. If the target chooses to do this, this is the next best.
9249 if (TSI) {
9250 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9251 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9252 DstPtrInfo, SrcPtrInfo);
9253 if (Result.getNode())
9254 return Result;
9255 }
9256
9257 // If we really need inline code and the target declined to provide it,
9258 // use a (potentially long) sequence of loads and stores.
9259 if (AlwaysInline) {
9260 assert(ConstantSize && "AlwaysInline requires a constant size!");
9262 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9263 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9264 }
9265
9268
9269 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9270 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9271 // respect volatile, so they may do things like read or write memory
9272 // beyond the given memory regions. But fixing this isn't easy, and most
9273 // people don't care.
9274
9275 // Emit a library call.
9278 Args.emplace_back(Dst, PtrTy);
9279 Args.emplace_back(Src, PtrTy);
9280 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9281 // FIXME: pass in SDLoc
9283 bool IsTailCall = false;
9284 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9285
9286 if (OverrideTailCall.has_value()) {
9287 IsTailCall = *OverrideTailCall;
9288 } else {
9289 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9290 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9291 }
9292
9293 CLI.setDebugLoc(dl)
9294 .setChain(Chain)
9295 .setLibCallee(
9296 TLI->getLibcallImplCallingConv(MemCpyImpl),
9297 Dst.getValueType().getTypeForEVT(*getContext()),
9298 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9299 std::move(Args))
9301 .setTailCall(IsTailCall);
9302
9303 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9304 return CallResult.second;
9305}
9306
9308 SDValue Dst, SDValue Src, SDValue Size,
9309 Type *SizeTy, unsigned ElemSz,
9310 bool isTailCall,
9311 MachinePointerInfo DstPtrInfo,
9312 MachinePointerInfo SrcPtrInfo) {
9313 // Emit a library call.
9316 Args.emplace_back(Dst, ArgTy);
9317 Args.emplace_back(Src, ArgTy);
9318 Args.emplace_back(Size, SizeTy);
9319
9320 RTLIB::Libcall LibraryCall =
9322 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9323 if (LibcallImpl == RTLIB::Unsupported)
9324 report_fatal_error("Unsupported element size");
9325
9327 CLI.setDebugLoc(dl)
9328 .setChain(Chain)
9329 .setLibCallee(
9330 TLI->getLibcallImplCallingConv(LibcallImpl),
9332 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9333 std::move(Args))
9335 .setTailCall(isTailCall);
9336
9337 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9338 return CallResult.second;
9339}
9340
9342 SDValue Src, SDValue Size, Align Alignment,
9343 bool isVol, const CallInst *CI,
9344 std::optional<bool> OverrideTailCall,
9345 MachinePointerInfo DstPtrInfo,
9346 MachinePointerInfo SrcPtrInfo,
9347 const AAMDNodes &AAInfo,
9348 BatchAAResults *BatchAA) {
9349 // Check to see if we should lower the memmove to loads and stores first.
9350 // For cases within the target-specified limits, this is the best choice.
9352 if (ConstantSize) {
9353 // Memmove with size zero? Just return the original chain.
9354 if (ConstantSize->isZero())
9355 return Chain;
9356
9358 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9359 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9360 if (Result.getNode())
9361 return Result;
9362 }
9363
9364 // Then check to see if we should lower the memmove with target-specific
9365 // code. If the target chooses to do this, this is the next best.
9366 if (TSI) {
9367 SDValue Result =
9368 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9369 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9370 if (Result.getNode())
9371 return Result;
9372 }
9373
9376
9377 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9378 // not be safe. See memcpy above for more details.
9379
9380 // Emit a library call.
9383 Args.emplace_back(Dst, PtrTy);
9384 Args.emplace_back(Src, PtrTy);
9385 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9386 // FIXME: pass in SDLoc
9388
9389 RTLIB::LibcallImpl MemmoveImpl = TLI->getLibcallImpl(RTLIB::MEMMOVE);
9390
9391 bool IsTailCall = false;
9392 if (OverrideTailCall.has_value()) {
9393 IsTailCall = *OverrideTailCall;
9394 } else {
9395 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9396 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9397 }
9398
9399 CLI.setDebugLoc(dl)
9400 .setChain(Chain)
9401 .setLibCallee(
9402 TLI->getLibcallImplCallingConv(MemmoveImpl),
9403 Dst.getValueType().getTypeForEVT(*getContext()),
9404 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9405 std::move(Args))
9407 .setTailCall(IsTailCall);
9408
9409 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9410 return CallResult.second;
9411}
9412
9414 SDValue Dst, SDValue Src, SDValue Size,
9415 Type *SizeTy, unsigned ElemSz,
9416 bool isTailCall,
9417 MachinePointerInfo DstPtrInfo,
9418 MachinePointerInfo SrcPtrInfo) {
9419 // Emit a library call.
9421 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9422 Args.emplace_back(Dst, IntPtrTy);
9423 Args.emplace_back(Src, IntPtrTy);
9424 Args.emplace_back(Size, SizeTy);
9425
9426 RTLIB::Libcall LibraryCall =
9428 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9429 if (LibcallImpl == RTLIB::Unsupported)
9430 report_fatal_error("Unsupported element size");
9431
9433 CLI.setDebugLoc(dl)
9434 .setChain(Chain)
9435 .setLibCallee(
9436 TLI->getLibcallImplCallingConv(LibcallImpl),
9438 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9439 std::move(Args))
9441 .setTailCall(isTailCall);
9442
9443 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9444 return CallResult.second;
9445}
9446
9448 SDValue Src, SDValue Size, Align Alignment,
9449 bool isVol, bool AlwaysInline,
9450 const CallInst *CI,
9451 MachinePointerInfo DstPtrInfo,
9452 const AAMDNodes &AAInfo) {
9453 // Check to see if we should lower the memset to stores first.
9454 // For cases within the target-specified limits, this is the best choice.
9456 if (ConstantSize) {
9457 // Memset with size zero? Just return the original chain.
9458 if (ConstantSize->isZero())
9459 return Chain;
9460
9461 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9462 ConstantSize->getZExtValue(), Alignment,
9463 isVol, false, DstPtrInfo, AAInfo);
9464
9465 if (Result.getNode())
9466 return Result;
9467 }
9468
9469 // Then check to see if we should lower the memset with target-specific
9470 // code. If the target chooses to do this, this is the next best.
9471 if (TSI) {
9472 SDValue Result = TSI->EmitTargetCodeForMemset(
9473 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9474 if (Result.getNode())
9475 return Result;
9476 }
9477
9478 // If we really need inline code and the target declined to provide it,
9479 // use a (potentially long) sequence of loads and stores.
9480 if (AlwaysInline) {
9481 assert(ConstantSize && "AlwaysInline requires a constant size!");
9482 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9483 ConstantSize->getZExtValue(), Alignment,
9484 isVol, true, DstPtrInfo, AAInfo);
9485 assert(Result &&
9486 "getMemsetStores must return a valid sequence when AlwaysInline");
9487 return Result;
9488 }
9489
9491
9492 // Emit a library call.
9493 auto &Ctx = *getContext();
9494 const auto& DL = getDataLayout();
9495
9497 // FIXME: pass in SDLoc
9498 CLI.setDebugLoc(dl).setChain(Chain);
9499
9500 RTLIB::LibcallImpl BzeroImpl = TLI->getLibcallImpl(RTLIB::BZERO);
9501 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9502
9503 // If zeroing out and bzero is present, use it.
9504 if (UseBZero) {
9506 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9507 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9508 CLI.setLibCallee(
9509 TLI->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9510 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9511 } else {
9512 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9513
9515 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9516 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9517 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9518 CLI.setLibCallee(TLI->getLibcallImplCallingConv(MemsetImpl),
9519 Dst.getValueType().getTypeForEVT(Ctx),
9520 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9521 std::move(Args));
9522 }
9523
9524 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9525 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9526
9527 // If we're going to use bzero, make sure not to tail call unless the
9528 // subsequent return doesn't need a value, as bzero doesn't return the first
9529 // arg unlike memset.
9530 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9531 bool IsTailCall =
9532 CI && CI->isTailCall() &&
9533 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9534 CLI.setDiscardResult().setTailCall(IsTailCall);
9535
9536 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9537 return CallResult.second;
9538}
9539
9542 Type *SizeTy, unsigned ElemSz,
9543 bool isTailCall,
9544 MachinePointerInfo DstPtrInfo) {
9545 // Emit a library call.
9547 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9548 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9549 Args.emplace_back(Size, SizeTy);
9550
9551 RTLIB::Libcall LibraryCall =
9553 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9554 if (LibcallImpl == RTLIB::Unsupported)
9555 report_fatal_error("Unsupported element size");
9556
9558 CLI.setDebugLoc(dl)
9559 .setChain(Chain)
9560 .setLibCallee(
9561 TLI->getLibcallImplCallingConv(LibcallImpl),
9563 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9564 std::move(Args))
9566 .setTailCall(isTailCall);
9567
9568 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9569 return CallResult.second;
9570}
9571
9572SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9574 MachineMemOperand *MMO,
9575 ISD::LoadExtType ExtType) {
9577 AddNodeIDNode(ID, Opcode, VTList, Ops);
9578 ID.AddInteger(MemVT.getRawBits());
9579 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9580 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9581 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9582 ID.AddInteger(MMO->getFlags());
9583 void* IP = nullptr;
9584 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9585 E->refineAlignment(MMO);
9586 E->refineRanges(MMO);
9587 return SDValue(E, 0);
9588 }
9589
9590 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9591 VTList, MemVT, MMO, ExtType);
9592 createOperands(N, Ops);
9593
9594 CSEMap.InsertNode(N, IP);
9595 InsertNode(N);
9596 SDValue V(N, 0);
9597 NewSDValueDbgMsg(V, "Creating new node: ", this);
9598 return V;
9599}
9600
9602 EVT MemVT, SDVTList VTs, SDValue Chain,
9603 SDValue Ptr, SDValue Cmp, SDValue Swp,
9604 MachineMemOperand *MMO) {
9605 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9607 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9608
9609 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9610 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9611}
9612
9613SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9614 SDValue Chain, SDValue Ptr, SDValue Val,
9615 MachineMemOperand *MMO) {
9616 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9617 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9618 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9619 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9620 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9621 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9622 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9623 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9624 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9625 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9626 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9627 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9628 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9629 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9630 Opcode == ISD::ATOMIC_STORE) &&
9631 "Invalid Atomic Op");
9632
9633 EVT VT = Val.getValueType();
9634
9635 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9636 getVTList(VT, MVT::Other);
9637 SDValue Ops[] = {Chain, Ptr, Val};
9638 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9639}
9640
9642 EVT MemVT, EVT VT, SDValue Chain,
9643 SDValue Ptr, MachineMemOperand *MMO) {
9644 SDVTList VTs = getVTList(VT, MVT::Other);
9645 SDValue Ops[] = {Chain, Ptr};
9646 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9647}
9648
9649/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9651 if (Ops.size() == 1)
9652 return Ops[0];
9653
9655 VTs.reserve(Ops.size());
9656 for (const SDValue &Op : Ops)
9657 VTs.push_back(Op.getValueType());
9658 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9659}
9660
9662 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9663 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9665 const AAMDNodes &AAInfo) {
9666 if (Size.hasValue() && !Size.getValue())
9668
9670 MachineMemOperand *MMO =
9671 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9672
9673 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9674}
9675
9677 SDVTList VTList,
9678 ArrayRef<SDValue> Ops, EVT MemVT,
9679 MachineMemOperand *MMO) {
9680 assert(
9681 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9682 Opcode == ISD::PREFETCH ||
9683 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9684 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9685 "Opcode is not a memory-accessing opcode!");
9686
9687 // Memoize the node unless it returns a glue result.
9689 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9691 AddNodeIDNode(ID, Opcode, VTList, Ops);
9692 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9693 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9694 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9695 ID.AddInteger(MMO->getFlags());
9696 ID.AddInteger(MemVT.getRawBits());
9697 void *IP = nullptr;
9698 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9699 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9700 return SDValue(E, 0);
9701 }
9702
9703 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9704 VTList, MemVT, MMO);
9705 createOperands(N, Ops);
9706
9707 CSEMap.InsertNode(N, IP);
9708 } else {
9709 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9710 VTList, MemVT, MMO);
9711 createOperands(N, Ops);
9712 }
9713 InsertNode(N);
9714 SDValue V(N, 0);
9715 NewSDValueDbgMsg(V, "Creating new node: ", this);
9716 return V;
9717}
9718
9720 SDValue Chain, int FrameIndex) {
9721 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9722 const auto VTs = getVTList(MVT::Other);
9723 SDValue Ops[2] = {
9724 Chain,
9725 getFrameIndex(FrameIndex,
9726 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9727 true)};
9728
9730 AddNodeIDNode(ID, Opcode, VTs, Ops);
9731 ID.AddInteger(FrameIndex);
9732 void *IP = nullptr;
9733 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9734 return SDValue(E, 0);
9735
9736 LifetimeSDNode *N =
9737 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9738 createOperands(N, Ops);
9739 CSEMap.InsertNode(N, IP);
9740 InsertNode(N);
9741 SDValue V(N, 0);
9742 NewSDValueDbgMsg(V, "Creating new node: ", this);
9743 return V;
9744}
9745
9747 uint64_t Guid, uint64_t Index,
9748 uint32_t Attr) {
9749 const unsigned Opcode = ISD::PSEUDO_PROBE;
9750 const auto VTs = getVTList(MVT::Other);
9751 SDValue Ops[] = {Chain};
9753 AddNodeIDNode(ID, Opcode, VTs, Ops);
9754 ID.AddInteger(Guid);
9755 ID.AddInteger(Index);
9756 void *IP = nullptr;
9757 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9758 return SDValue(E, 0);
9759
9760 auto *N = newSDNode<PseudoProbeSDNode>(
9761 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9762 createOperands(N, Ops);
9763 CSEMap.InsertNode(N, IP);
9764 InsertNode(N);
9765 SDValue V(N, 0);
9766 NewSDValueDbgMsg(V, "Creating new node: ", this);
9767 return V;
9768}
9769
9770/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9771/// MachinePointerInfo record from it. This is particularly useful because the
9772/// code generator has many cases where it doesn't bother passing in a
9773/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9775 SelectionDAG &DAG, SDValue Ptr,
9776 int64_t Offset = 0) {
9777 // If this is FI+Offset, we can model it.
9778 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9780 FI->getIndex(), Offset);
9781
9782 // If this is (FI+Offset1)+Offset2, we can model it.
9783 if (Ptr.getOpcode() != ISD::ADD ||
9786 return Info;
9787
9788 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9790 DAG.getMachineFunction(), FI,
9791 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9792}
9793
9794/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9795/// MachinePointerInfo record from it. This is particularly useful because the
9796/// code generator has many cases where it doesn't bother passing in a
9797/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9799 SelectionDAG &DAG, SDValue Ptr,
9800 SDValue OffsetOp) {
9801 // If the 'Offset' value isn't a constant, we can't handle this.
9803 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9804 if (OffsetOp.isUndef())
9805 return InferPointerInfo(Info, DAG, Ptr);
9806 return Info;
9807}
9808
9810 EVT VT, const SDLoc &dl, SDValue Chain,
9811 SDValue Ptr, SDValue Offset,
9812 MachinePointerInfo PtrInfo, EVT MemVT,
9813 Align Alignment,
9814 MachineMemOperand::Flags MMOFlags,
9815 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9816 assert(Chain.getValueType() == MVT::Other &&
9817 "Invalid chain type");
9818
9819 MMOFlags |= MachineMemOperand::MOLoad;
9820 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9821 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9822 // clients.
9823 if (PtrInfo.V.isNull())
9824 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9825
9826 TypeSize Size = MemVT.getStoreSize();
9828 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9829 Alignment, AAInfo, Ranges);
9830 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9831}
9832
9834 EVT VT, const SDLoc &dl, SDValue Chain,
9835 SDValue Ptr, SDValue Offset, EVT MemVT,
9836 MachineMemOperand *MMO) {
9837 if (VT == MemVT) {
9838 ExtType = ISD::NON_EXTLOAD;
9839 } else if (ExtType == ISD::NON_EXTLOAD) {
9840 assert(VT == MemVT && "Non-extending load from different memory type!");
9841 } else {
9842 // Extending load.
9843 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9844 "Should only be an extending load, not truncating!");
9845 assert(VT.isInteger() == MemVT.isInteger() &&
9846 "Cannot convert from FP to Int or Int -> FP!");
9847 assert(VT.isVector() == MemVT.isVector() &&
9848 "Cannot use an ext load to convert to or from a vector!");
9849 assert((!VT.isVector() ||
9851 "Cannot use an ext load to change the number of vector elements!");
9852 }
9853
9854 assert((!MMO->getRanges() ||
9856 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9857 MemVT.isInteger())) &&
9858 "Range metadata and load type must match!");
9859
9860 bool Indexed = AM != ISD::UNINDEXED;
9861 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9862
9863 SDVTList VTs = Indexed ?
9864 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9865 SDValue Ops[] = { Chain, Ptr, Offset };
9867 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9868 ID.AddInteger(MemVT.getRawBits());
9869 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9870 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9871 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9872 ID.AddInteger(MMO->getFlags());
9873 void *IP = nullptr;
9874 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9875 E->refineAlignment(MMO);
9876 E->refineRanges(MMO);
9877 return SDValue(E, 0);
9878 }
9879 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9880 ExtType, MemVT, MMO);
9881 createOperands(N, Ops);
9882
9883 CSEMap.InsertNode(N, IP);
9884 InsertNode(N);
9885 SDValue V(N, 0);
9886 NewSDValueDbgMsg(V, "Creating new node: ", this);
9887 return V;
9888}
9889
9891 SDValue Ptr, MachinePointerInfo PtrInfo,
9892 MaybeAlign Alignment,
9893 MachineMemOperand::Flags MMOFlags,
9894 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9895 SDValue Undef = getUNDEF(Ptr.getValueType());
9896 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9897 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9898}
9899
9901 SDValue Ptr, MachineMemOperand *MMO) {
9902 SDValue Undef = getUNDEF(Ptr.getValueType());
9903 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9904 VT, MMO);
9905}
9906
9908 EVT VT, SDValue Chain, SDValue Ptr,
9909 MachinePointerInfo PtrInfo, EVT MemVT,
9910 MaybeAlign Alignment,
9911 MachineMemOperand::Flags MMOFlags,
9912 const AAMDNodes &AAInfo) {
9913 SDValue Undef = getUNDEF(Ptr.getValueType());
9914 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9915 MemVT, Alignment, MMOFlags, AAInfo);
9916}
9917
9919 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9920 MachineMemOperand *MMO) {
9921 SDValue Undef = getUNDEF(Ptr.getValueType());
9922 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9923 MemVT, MMO);
9924}
9925
9929 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9930 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9931 // Don't propagate the invariant or dereferenceable flags.
9932 auto MMOFlags =
9933 LD->getMemOperand()->getFlags() &
9935 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9936 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9937 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9938}
9939
9941 SDValue Ptr, MachinePointerInfo PtrInfo,
9942 Align Alignment,
9943 MachineMemOperand::Flags MMOFlags,
9944 const AAMDNodes &AAInfo) {
9945 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9946
9947 MMOFlags |= MachineMemOperand::MOStore;
9948 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9949
9950 if (PtrInfo.V.isNull())
9951 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9952
9955 MachineMemOperand *MMO =
9956 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9957 return getStore(Chain, dl, Val, Ptr, MMO);
9958}
9959
9961 SDValue Ptr, MachineMemOperand *MMO) {
9962 SDValue Undef = getUNDEF(Ptr.getValueType());
9963 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9965}
9966
9968 SDValue Ptr, SDValue Offset, EVT SVT,
9970 bool IsTruncating) {
9971 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9972 EVT VT = Val.getValueType();
9973 if (VT == SVT) {
9974 IsTruncating = false;
9975 } else if (!IsTruncating) {
9976 assert(VT == SVT && "No-truncating store from different memory type!");
9977 } else {
9979 "Should only be a truncating store, not extending!");
9980 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9981 assert(VT.isVector() == SVT.isVector() &&
9982 "Cannot use trunc store to convert to or from a vector!");
9983 assert((!VT.isVector() ||
9985 "Cannot use trunc store to change the number of vector elements!");
9986 }
9987
9988 bool Indexed = AM != ISD::UNINDEXED;
9989 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9990 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9991 : getVTList(MVT::Other);
9992 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9995 ID.AddInteger(SVT.getRawBits());
9996 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9997 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9998 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9999 ID.AddInteger(MMO->getFlags());
10000 void *IP = nullptr;
10001 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10002 cast<StoreSDNode>(E)->refineAlignment(MMO);
10003 return SDValue(E, 0);
10004 }
10005 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10006 IsTruncating, SVT, MMO);
10007 createOperands(N, Ops);
10008
10009 CSEMap.InsertNode(N, IP);
10010 InsertNode(N);
10011 SDValue V(N, 0);
10012 NewSDValueDbgMsg(V, "Creating new node: ", this);
10013 return V;
10014}
10015
10017 SDValue Ptr, MachinePointerInfo PtrInfo,
10018 EVT SVT, Align Alignment,
10019 MachineMemOperand::Flags MMOFlags,
10020 const AAMDNodes &AAInfo) {
10021 assert(Chain.getValueType() == MVT::Other &&
10022 "Invalid chain type");
10023
10024 MMOFlags |= MachineMemOperand::MOStore;
10025 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10026
10027 if (PtrInfo.V.isNull())
10028 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10029
10031 MachineMemOperand *MMO = MF.getMachineMemOperand(
10032 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10033 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10034}
10035
10037 SDValue Ptr, EVT SVT,
10038 MachineMemOperand *MMO) {
10039 SDValue Undef = getUNDEF(Ptr.getValueType());
10040 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10041}
10042
10046 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10047 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10048 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10049 ST->getMemoryVT(), ST->getMemOperand(), AM,
10050 ST->isTruncatingStore());
10051}
10052
10054 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10055 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10056 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10057 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10058 const MDNode *Ranges, bool IsExpanding) {
10059 MMOFlags |= MachineMemOperand::MOLoad;
10060 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10061 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10062 // clients.
10063 if (PtrInfo.V.isNull())
10064 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10065
10066 TypeSize Size = MemVT.getStoreSize();
10068 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10069 Alignment, AAInfo, Ranges);
10070 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10071 MMO, IsExpanding);
10072}
10073
10075 ISD::LoadExtType ExtType, EVT VT,
10076 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10077 SDValue Offset, SDValue Mask, SDValue EVL,
10078 EVT MemVT, MachineMemOperand *MMO,
10079 bool IsExpanding) {
10080 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10081 assert(Mask.getValueType().getVectorElementCount() ==
10082 VT.getVectorElementCount() &&
10083 "Vector width mismatch between mask and data");
10084
10085 bool Indexed = AM != ISD::UNINDEXED;
10086 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10087
10088 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10089 : getVTList(VT, MVT::Other);
10090 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10092 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10093 ID.AddInteger(MemVT.getRawBits());
10094 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10095 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10096 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10097 ID.AddInteger(MMO->getFlags());
10098 void *IP = nullptr;
10099 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10100 E->refineAlignment(MMO);
10101 E->refineRanges(MMO);
10102 return SDValue(E, 0);
10103 }
10104 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10105 ExtType, IsExpanding, MemVT, MMO);
10106 createOperands(N, Ops);
10107
10108 CSEMap.InsertNode(N, IP);
10109 InsertNode(N);
10110 SDValue V(N, 0);
10111 NewSDValueDbgMsg(V, "Creating new node: ", this);
10112 return V;
10113}
10114
10116 SDValue Ptr, SDValue Mask, SDValue EVL,
10117 MachinePointerInfo PtrInfo,
10118 MaybeAlign Alignment,
10119 MachineMemOperand::Flags MMOFlags,
10120 const AAMDNodes &AAInfo, const MDNode *Ranges,
10121 bool IsExpanding) {
10122 SDValue Undef = getUNDEF(Ptr.getValueType());
10123 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10124 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10125 IsExpanding);
10126}
10127
10129 SDValue Ptr, SDValue Mask, SDValue EVL,
10130 MachineMemOperand *MMO, bool IsExpanding) {
10131 SDValue Undef = getUNDEF(Ptr.getValueType());
10132 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10133 Mask, EVL, VT, MMO, IsExpanding);
10134}
10135
10137 EVT VT, SDValue Chain, SDValue Ptr,
10138 SDValue Mask, SDValue EVL,
10139 MachinePointerInfo PtrInfo, EVT MemVT,
10140 MaybeAlign Alignment,
10141 MachineMemOperand::Flags MMOFlags,
10142 const AAMDNodes &AAInfo, bool IsExpanding) {
10143 SDValue Undef = getUNDEF(Ptr.getValueType());
10144 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10145 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10146 IsExpanding);
10147}
10148
10150 EVT VT, SDValue Chain, SDValue Ptr,
10151 SDValue Mask, SDValue EVL, EVT MemVT,
10152 MachineMemOperand *MMO, bool IsExpanding) {
10153 SDValue Undef = getUNDEF(Ptr.getValueType());
10154 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10155 EVL, MemVT, MMO, IsExpanding);
10156}
10157
10161 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10162 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10163 // Don't propagate the invariant or dereferenceable flags.
10164 auto MMOFlags =
10165 LD->getMemOperand()->getFlags() &
10167 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10168 LD->getChain(), Base, Offset, LD->getMask(),
10169 LD->getVectorLength(), LD->getPointerInfo(),
10170 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10171 nullptr, LD->isExpandingLoad());
10172}
10173
10175 SDValue Ptr, SDValue Offset, SDValue Mask,
10176 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10177 ISD::MemIndexedMode AM, bool IsTruncating,
10178 bool IsCompressing) {
10179 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10180 assert(Mask.getValueType().getVectorElementCount() ==
10182 "Vector width mismatch between mask and data");
10183
10184 bool Indexed = AM != ISD::UNINDEXED;
10185 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10186 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10187 : getVTList(MVT::Other);
10188 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10190 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10191 ID.AddInteger(MemVT.getRawBits());
10192 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10193 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10194 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10195 ID.AddInteger(MMO->getFlags());
10196 void *IP = nullptr;
10197 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10198 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10199 return SDValue(E, 0);
10200 }
10201 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10202 IsTruncating, IsCompressing, MemVT, MMO);
10203 createOperands(N, Ops);
10204
10205 CSEMap.InsertNode(N, IP);
10206 InsertNode(N);
10207 SDValue V(N, 0);
10208 NewSDValueDbgMsg(V, "Creating new node: ", this);
10209 return V;
10210}
10211
10213 SDValue Val, SDValue Ptr, SDValue Mask,
10214 SDValue EVL, MachinePointerInfo PtrInfo,
10215 EVT SVT, Align Alignment,
10216 MachineMemOperand::Flags MMOFlags,
10217 const AAMDNodes &AAInfo,
10218 bool IsCompressing) {
10219 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10220
10221 MMOFlags |= MachineMemOperand::MOStore;
10222 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10223
10224 if (PtrInfo.V.isNull())
10225 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10226
10228 MachineMemOperand *MMO = MF.getMachineMemOperand(
10229 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10230 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10231 IsCompressing);
10232}
10233
10235 SDValue Val, SDValue Ptr, SDValue Mask,
10236 SDValue EVL, EVT SVT,
10237 MachineMemOperand *MMO,
10238 bool IsCompressing) {
10239 EVT VT = Val.getValueType();
10240
10241 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10242 if (VT == SVT)
10243 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10244 EVL, VT, MMO, ISD::UNINDEXED,
10245 /*IsTruncating*/ false, IsCompressing);
10246
10248 "Should only be a truncating store, not extending!");
10249 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10250 assert(VT.isVector() == SVT.isVector() &&
10251 "Cannot use trunc store to convert to or from a vector!");
10252 assert((!VT.isVector() ||
10254 "Cannot use trunc store to change the number of vector elements!");
10255
10256 SDVTList VTs = getVTList(MVT::Other);
10257 SDValue Undef = getUNDEF(Ptr.getValueType());
10258 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10260 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10261 ID.AddInteger(SVT.getRawBits());
10262 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10263 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10264 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10265 ID.AddInteger(MMO->getFlags());
10266 void *IP = nullptr;
10267 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10268 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10269 return SDValue(E, 0);
10270 }
10271 auto *N =
10272 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10273 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10274 createOperands(N, Ops);
10275
10276 CSEMap.InsertNode(N, IP);
10277 InsertNode(N);
10278 SDValue V(N, 0);
10279 NewSDValueDbgMsg(V, "Creating new node: ", this);
10280 return V;
10281}
10282
10286 auto *ST = cast<VPStoreSDNode>(OrigStore);
10287 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10288 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10289 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10290 Offset, ST->getMask(), ST->getVectorLength()};
10292 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10293 ID.AddInteger(ST->getMemoryVT().getRawBits());
10294 ID.AddInteger(ST->getRawSubclassData());
10295 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10296 ID.AddInteger(ST->getMemOperand()->getFlags());
10297 void *IP = nullptr;
10298 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10299 return SDValue(E, 0);
10300
10301 auto *N = newSDNode<VPStoreSDNode>(
10302 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10303 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10304 createOperands(N, Ops);
10305
10306 CSEMap.InsertNode(N, IP);
10307 InsertNode(N);
10308 SDValue V(N, 0);
10309 NewSDValueDbgMsg(V, "Creating new node: ", this);
10310 return V;
10311}
10312
10314 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10315 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10316 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10317 bool Indexed = AM != ISD::UNINDEXED;
10318 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10319
10320 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10321 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10322 : getVTList(VT, MVT::Other);
10324 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10325 ID.AddInteger(VT.getRawBits());
10326 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10327 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10328 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10329
10330 void *IP = nullptr;
10331 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10332 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10333 return SDValue(E, 0);
10334 }
10335
10336 auto *N =
10337 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10338 ExtType, IsExpanding, MemVT, MMO);
10339 createOperands(N, Ops);
10340 CSEMap.InsertNode(N, IP);
10341 InsertNode(N);
10342 SDValue V(N, 0);
10343 NewSDValueDbgMsg(V, "Creating new node: ", this);
10344 return V;
10345}
10346
10348 SDValue Ptr, SDValue Stride,
10349 SDValue Mask, SDValue EVL,
10350 MachineMemOperand *MMO,
10351 bool IsExpanding) {
10352 SDValue Undef = getUNDEF(Ptr.getValueType());
10353 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10354 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10355}
10356
10358 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10359 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10360 MachineMemOperand *MMO, bool IsExpanding) {
10361 SDValue Undef = getUNDEF(Ptr.getValueType());
10362 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10363 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10364}
10365
10367 SDValue Val, SDValue Ptr,
10368 SDValue Offset, SDValue Stride,
10369 SDValue Mask, SDValue EVL, EVT MemVT,
10370 MachineMemOperand *MMO,
10372 bool IsTruncating, bool IsCompressing) {
10373 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10374 bool Indexed = AM != ISD::UNINDEXED;
10375 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10376 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10377 : getVTList(MVT::Other);
10378 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10380 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10381 ID.AddInteger(MemVT.getRawBits());
10382 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10383 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10384 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10385 void *IP = nullptr;
10386 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10387 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10388 return SDValue(E, 0);
10389 }
10390 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10391 VTs, AM, IsTruncating,
10392 IsCompressing, MemVT, MMO);
10393 createOperands(N, Ops);
10394
10395 CSEMap.InsertNode(N, IP);
10396 InsertNode(N);
10397 SDValue V(N, 0);
10398 NewSDValueDbgMsg(V, "Creating new node: ", this);
10399 return V;
10400}
10401
10403 SDValue Val, SDValue Ptr,
10404 SDValue Stride, SDValue Mask,
10405 SDValue EVL, EVT SVT,
10406 MachineMemOperand *MMO,
10407 bool IsCompressing) {
10408 EVT VT = Val.getValueType();
10409
10410 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10411 if (VT == SVT)
10412 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10413 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10414 /*IsTruncating*/ false, IsCompressing);
10415
10417 "Should only be a truncating store, not extending!");
10418 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10419 assert(VT.isVector() == SVT.isVector() &&
10420 "Cannot use trunc store to convert to or from a vector!");
10421 assert((!VT.isVector() ||
10423 "Cannot use trunc store to change the number of vector elements!");
10424
10425 SDVTList VTs = getVTList(MVT::Other);
10426 SDValue Undef = getUNDEF(Ptr.getValueType());
10427 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10429 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10430 ID.AddInteger(SVT.getRawBits());
10431 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10432 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10433 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10434 void *IP = nullptr;
10435 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10436 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10437 return SDValue(E, 0);
10438 }
10439 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10440 VTs, ISD::UNINDEXED, true,
10441 IsCompressing, SVT, MMO);
10442 createOperands(N, Ops);
10443
10444 CSEMap.InsertNode(N, IP);
10445 InsertNode(N);
10446 SDValue V(N, 0);
10447 NewSDValueDbgMsg(V, "Creating new node: ", this);
10448 return V;
10449}
10450
10453 ISD::MemIndexType IndexType) {
10454 assert(Ops.size() == 6 && "Incompatible number of operands");
10455
10457 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10458 ID.AddInteger(VT.getRawBits());
10459 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10460 dl.getIROrder(), VTs, VT, MMO, IndexType));
10461 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10462 ID.AddInteger(MMO->getFlags());
10463 void *IP = nullptr;
10464 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10465 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10466 return SDValue(E, 0);
10467 }
10468
10469 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10470 VT, MMO, IndexType);
10471 createOperands(N, Ops);
10472
10473 assert(N->getMask().getValueType().getVectorElementCount() ==
10474 N->getValueType(0).getVectorElementCount() &&
10475 "Vector width mismatch between mask and data");
10476 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10477 N->getValueType(0).getVectorElementCount().isScalable() &&
10478 "Scalable flags of index and data do not match");
10480 N->getIndex().getValueType().getVectorElementCount(),
10481 N->getValueType(0).getVectorElementCount()) &&
10482 "Vector width mismatch between index and data");
10483 assert(isa<ConstantSDNode>(N->getScale()) &&
10484 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10485 "Scale should be a constant power of 2");
10486
10487 CSEMap.InsertNode(N, IP);
10488 InsertNode(N);
10489 SDValue V(N, 0);
10490 NewSDValueDbgMsg(V, "Creating new node: ", this);
10491 return V;
10492}
10493
10496 MachineMemOperand *MMO,
10497 ISD::MemIndexType IndexType) {
10498 assert(Ops.size() == 7 && "Incompatible number of operands");
10499
10501 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10502 ID.AddInteger(VT.getRawBits());
10503 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10504 dl.getIROrder(), VTs, VT, MMO, IndexType));
10505 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10506 ID.AddInteger(MMO->getFlags());
10507 void *IP = nullptr;
10508 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10509 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10510 return SDValue(E, 0);
10511 }
10512 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10513 VT, MMO, IndexType);
10514 createOperands(N, Ops);
10515
10516 assert(N->getMask().getValueType().getVectorElementCount() ==
10517 N->getValue().getValueType().getVectorElementCount() &&
10518 "Vector width mismatch between mask and data");
10519 assert(
10520 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10521 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10522 "Scalable flags of index and data do not match");
10524 N->getIndex().getValueType().getVectorElementCount(),
10525 N->getValue().getValueType().getVectorElementCount()) &&
10526 "Vector width mismatch between index and data");
10527 assert(isa<ConstantSDNode>(N->getScale()) &&
10528 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10529 "Scale should be a constant power of 2");
10530
10531 CSEMap.InsertNode(N, IP);
10532 InsertNode(N);
10533 SDValue V(N, 0);
10534 NewSDValueDbgMsg(V, "Creating new node: ", this);
10535 return V;
10536}
10537
10540 SDValue PassThru, EVT MemVT,
10541 MachineMemOperand *MMO,
10543 ISD::LoadExtType ExtTy, bool isExpanding) {
10544 bool Indexed = AM != ISD::UNINDEXED;
10545 assert((Indexed || Offset.isUndef()) &&
10546 "Unindexed masked load with an offset!");
10547 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10548 : getVTList(VT, MVT::Other);
10549 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10552 ID.AddInteger(MemVT.getRawBits());
10553 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10554 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10555 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10556 ID.AddInteger(MMO->getFlags());
10557 void *IP = nullptr;
10558 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10559 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10560 return SDValue(E, 0);
10561 }
10562 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10563 AM, ExtTy, isExpanding, MemVT, MMO);
10564 createOperands(N, Ops);
10565
10566 CSEMap.InsertNode(N, IP);
10567 InsertNode(N);
10568 SDValue V(N, 0);
10569 NewSDValueDbgMsg(V, "Creating new node: ", this);
10570 return V;
10571}
10572
10577 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10578 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10579 Offset, LD->getMask(), LD->getPassThru(),
10580 LD->getMemoryVT(), LD->getMemOperand(), AM,
10581 LD->getExtensionType(), LD->isExpandingLoad());
10582}
10583
10586 SDValue Mask, EVT MemVT,
10587 MachineMemOperand *MMO,
10588 ISD::MemIndexedMode AM, bool IsTruncating,
10589 bool IsCompressing) {
10590 assert(Chain.getValueType() == MVT::Other &&
10591 "Invalid chain type");
10592 bool Indexed = AM != ISD::UNINDEXED;
10593 assert((Indexed || Offset.isUndef()) &&
10594 "Unindexed masked store with an offset!");
10595 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10596 : getVTList(MVT::Other);
10597 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10600 ID.AddInteger(MemVT.getRawBits());
10601 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10602 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10603 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10604 ID.AddInteger(MMO->getFlags());
10605 void *IP = nullptr;
10606 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10607 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10608 return SDValue(E, 0);
10609 }
10610 auto *N =
10611 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10612 IsTruncating, IsCompressing, MemVT, MMO);
10613 createOperands(N, Ops);
10614
10615 CSEMap.InsertNode(N, IP);
10616 InsertNode(N);
10617 SDValue V(N, 0);
10618 NewSDValueDbgMsg(V, "Creating new node: ", this);
10619 return V;
10620}
10621
10626 assert(ST->getOffset().isUndef() &&
10627 "Masked store is already a indexed store!");
10628 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10629 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10630 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10631}
10632
10635 MachineMemOperand *MMO,
10636 ISD::MemIndexType IndexType,
10637 ISD::LoadExtType ExtTy) {
10638 assert(Ops.size() == 6 && "Incompatible number of operands");
10639
10642 ID.AddInteger(MemVT.getRawBits());
10643 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10644 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10645 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10646 ID.AddInteger(MMO->getFlags());
10647 void *IP = nullptr;
10648 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10649 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10650 return SDValue(E, 0);
10651 }
10652
10653 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10654 VTs, MemVT, MMO, IndexType, ExtTy);
10655 createOperands(N, Ops);
10656
10657 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10658 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10659 assert(N->getMask().getValueType().getVectorElementCount() ==
10660 N->getValueType(0).getVectorElementCount() &&
10661 "Vector width mismatch between mask and data");
10662 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10663 N->getValueType(0).getVectorElementCount().isScalable() &&
10664 "Scalable flags of index and data do not match");
10666 N->getIndex().getValueType().getVectorElementCount(),
10667 N->getValueType(0).getVectorElementCount()) &&
10668 "Vector width mismatch between index and data");
10669 assert(isa<ConstantSDNode>(N->getScale()) &&
10670 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10671 "Scale should be a constant power of 2");
10672
10673 CSEMap.InsertNode(N, IP);
10674 InsertNode(N);
10675 SDValue V(N, 0);
10676 NewSDValueDbgMsg(V, "Creating new node: ", this);
10677 return V;
10678}
10679
10682 MachineMemOperand *MMO,
10683 ISD::MemIndexType IndexType,
10684 bool IsTrunc) {
10685 assert(Ops.size() == 6 && "Incompatible number of operands");
10686
10689 ID.AddInteger(MemVT.getRawBits());
10690 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10691 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10692 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10693 ID.AddInteger(MMO->getFlags());
10694 void *IP = nullptr;
10695 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10696 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10697 return SDValue(E, 0);
10698 }
10699
10700 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10701 VTs, MemVT, MMO, IndexType, IsTrunc);
10702 createOperands(N, Ops);
10703
10704 assert(N->getMask().getValueType().getVectorElementCount() ==
10705 N->getValue().getValueType().getVectorElementCount() &&
10706 "Vector width mismatch between mask and data");
10707 assert(
10708 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10709 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10710 "Scalable flags of index and data do not match");
10712 N->getIndex().getValueType().getVectorElementCount(),
10713 N->getValue().getValueType().getVectorElementCount()) &&
10714 "Vector width mismatch between index and data");
10715 assert(isa<ConstantSDNode>(N->getScale()) &&
10716 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10717 "Scale should be a constant power of 2");
10718
10719 CSEMap.InsertNode(N, IP);
10720 InsertNode(N);
10721 SDValue V(N, 0);
10722 NewSDValueDbgMsg(V, "Creating new node: ", this);
10723 return V;
10724}
10725
10727 const SDLoc &dl, ArrayRef<SDValue> Ops,
10728 MachineMemOperand *MMO,
10729 ISD::MemIndexType IndexType) {
10730 assert(Ops.size() == 7 && "Incompatible number of operands");
10731
10734 ID.AddInteger(MemVT.getRawBits());
10735 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10736 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10737 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10738 ID.AddInteger(MMO->getFlags());
10739 void *IP = nullptr;
10740 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10741 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10742 return SDValue(E, 0);
10743 }
10744
10745 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10746 VTs, MemVT, MMO, IndexType);
10747 createOperands(N, Ops);
10748
10749 assert(N->getMask().getValueType().getVectorElementCount() ==
10750 N->getIndex().getValueType().getVectorElementCount() &&
10751 "Vector width mismatch between mask and data");
10752 assert(isa<ConstantSDNode>(N->getScale()) &&
10753 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10754 "Scale should be a constant power of 2");
10755 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10756
10757 CSEMap.InsertNode(N, IP);
10758 InsertNode(N);
10759 SDValue V(N, 0);
10760 NewSDValueDbgMsg(V, "Creating new node: ", this);
10761 return V;
10762}
10763
10765 SDValue Ptr, SDValue Mask, SDValue EVL,
10766 MachineMemOperand *MMO) {
10767 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10768 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10770 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10771 ID.AddInteger(VT.getRawBits());
10772 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10773 VTs, VT, MMO));
10774 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10775 ID.AddInteger(MMO->getFlags());
10776 void *IP = nullptr;
10777 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10778 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10779 return SDValue(E, 0);
10780 }
10781 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10782 VT, MMO);
10783 createOperands(N, Ops);
10784
10785 CSEMap.InsertNode(N, IP);
10786 InsertNode(N);
10787 SDValue V(N, 0);
10788 NewSDValueDbgMsg(V, "Creating new node: ", this);
10789 return V;
10790}
10791
10793 EVT MemVT, MachineMemOperand *MMO) {
10794 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10795 SDVTList VTs = getVTList(MVT::Other);
10796 SDValue Ops[] = {Chain, Ptr};
10799 ID.AddInteger(MemVT.getRawBits());
10800 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10801 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10802 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10803 ID.AddInteger(MMO->getFlags());
10804 void *IP = nullptr;
10805 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10806 return SDValue(E, 0);
10807
10808 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10809 dl.getDebugLoc(), VTs, MemVT, MMO);
10810 createOperands(N, Ops);
10811
10812 CSEMap.InsertNode(N, IP);
10813 InsertNode(N);
10814 SDValue V(N, 0);
10815 NewSDValueDbgMsg(V, "Creating new node: ", this);
10816 return V;
10817}
10818
10820 EVT MemVT, MachineMemOperand *MMO) {
10821 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10822 SDVTList VTs = getVTList(MVT::Other);
10823 SDValue Ops[] = {Chain, Ptr};
10826 ID.AddInteger(MemVT.getRawBits());
10827 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10828 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10829 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10830 ID.AddInteger(MMO->getFlags());
10831 void *IP = nullptr;
10832 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10833 return SDValue(E, 0);
10834
10835 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10836 dl.getDebugLoc(), VTs, MemVT, MMO);
10837 createOperands(N, Ops);
10838
10839 CSEMap.InsertNode(N, IP);
10840 InsertNode(N);
10841 SDValue V(N, 0);
10842 NewSDValueDbgMsg(V, "Creating new node: ", this);
10843 return V;
10844}
10845
10847 // select undef, T, F --> T (if T is a constant), otherwise F
10848 // select, ?, undef, F --> F
10849 // select, ?, T, undef --> T
10850 if (Cond.isUndef())
10851 return isConstantValueOfAnyType(T) ? T : F;
10852 if (T.isUndef())
10854 if (F.isUndef())
10856
10857 // select true, T, F --> T
10858 // select false, T, F --> F
10859 if (auto C = isBoolConstant(Cond))
10860 return *C ? T : F;
10861
10862 // select ?, T, T --> T
10863 if (T == F)
10864 return T;
10865
10866 return SDValue();
10867}
10868
10870 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10871 if (X.isUndef())
10872 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10873 // shift X, undef --> undef (because it may shift by the bitwidth)
10874 if (Y.isUndef())
10875 return getUNDEF(X.getValueType());
10876
10877 // shift 0, Y --> 0
10878 // shift X, 0 --> X
10880 return X;
10881
10882 // shift X, C >= bitwidth(X) --> undef
10883 // All vector elements must be too big (or undef) to avoid partial undefs.
10884 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10885 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10886 };
10887 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10888 return getUNDEF(X.getValueType());
10889
10890 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10891 if (X.getValueType().getScalarType() == MVT::i1)
10892 return X;
10893
10894 return SDValue();
10895}
10896
10898 SDNodeFlags Flags) {
10899 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10900 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10901 // operation is poison. That result can be relaxed to undef.
10902 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10903 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10904 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10905 (YC && YC->getValueAPF().isNaN());
10906 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10907 (YC && YC->getValueAPF().isInfinity());
10908
10909 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10910 return getUNDEF(X.getValueType());
10911
10912 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10913 return getUNDEF(X.getValueType());
10914
10915 if (!YC)
10916 return SDValue();
10917
10918 // X + -0.0 --> X
10919 if (Opcode == ISD::FADD)
10920 if (YC->getValueAPF().isNegZero())
10921 return X;
10922
10923 // X - +0.0 --> X
10924 if (Opcode == ISD::FSUB)
10925 if (YC->getValueAPF().isPosZero())
10926 return X;
10927
10928 // X * 1.0 --> X
10929 // X / 1.0 --> X
10930 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10931 if (YC->getValueAPF().isExactlyValue(1.0))
10932 return X;
10933
10934 // X * 0.0 --> 0.0
10935 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10936 if (YC->getValueAPF().isZero())
10937 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10938
10939 return SDValue();
10940}
10941
10943 SDValue Ptr, SDValue SV, unsigned Align) {
10944 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10945 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10946}
10947
10948SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10950 switch (Ops.size()) {
10951 case 0: return getNode(Opcode, DL, VT);
10952 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10953 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10954 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10955 default: break;
10956 }
10957
10958 // Copy from an SDUse array into an SDValue array for use with
10959 // the regular getNode logic.
10961 return getNode(Opcode, DL, VT, NewOps);
10962}
10963
10964SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10966 SDNodeFlags Flags;
10967 if (Inserter)
10968 Flags = Inserter->getFlags();
10969 return getNode(Opcode, DL, VT, Ops, Flags);
10970}
10971
10972SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10973 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10974 unsigned NumOps = Ops.size();
10975 switch (NumOps) {
10976 case 0: return getNode(Opcode, DL, VT);
10977 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10978 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10979 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10980 default: break;
10981 }
10982
10983#ifndef NDEBUG
10984 for (const auto &Op : Ops)
10985 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10986 "Operand is DELETED_NODE!");
10987#endif
10988
10989 switch (Opcode) {
10990 default: break;
10991 case ISD::BUILD_VECTOR:
10992 // Attempt to simplify BUILD_VECTOR.
10993 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10994 return V;
10995 break;
10997 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10998 return V;
10999 break;
11000 case ISD::SELECT_CC:
11001 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11002 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11003 "LHS and RHS of condition must have same type!");
11004 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11005 "True and False arms of SelectCC must have same type!");
11006 assert(Ops[2].getValueType() == VT &&
11007 "select_cc node must be of same type as true and false value!");
11008 assert((!Ops[0].getValueType().isVector() ||
11009 Ops[0].getValueType().getVectorElementCount() ==
11010 VT.getVectorElementCount()) &&
11011 "Expected select_cc with vector result to have the same sized "
11012 "comparison type!");
11013 break;
11014 case ISD::BR_CC:
11015 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11016 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11017 "LHS/RHS of comparison should match types!");
11018 break;
11019 case ISD::VP_ADD:
11020 case ISD::VP_SUB:
11021 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11022 if (VT.getScalarType() == MVT::i1)
11023 Opcode = ISD::VP_XOR;
11024 break;
11025 case ISD::VP_MUL:
11026 // If it is VP_MUL mask operation then turn it to VP_AND
11027 if (VT.getScalarType() == MVT::i1)
11028 Opcode = ISD::VP_AND;
11029 break;
11030 case ISD::VP_REDUCE_MUL:
11031 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11032 if (VT == MVT::i1)
11033 Opcode = ISD::VP_REDUCE_AND;
11034 break;
11035 case ISD::VP_REDUCE_ADD:
11036 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11037 if (VT == MVT::i1)
11038 Opcode = ISD::VP_REDUCE_XOR;
11039 break;
11040 case ISD::VP_REDUCE_SMAX:
11041 case ISD::VP_REDUCE_UMIN:
11042 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11043 // VP_REDUCE_AND.
11044 if (VT == MVT::i1)
11045 Opcode = ISD::VP_REDUCE_AND;
11046 break;
11047 case ISD::VP_REDUCE_SMIN:
11048 case ISD::VP_REDUCE_UMAX:
11049 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11050 // VP_REDUCE_OR.
11051 if (VT == MVT::i1)
11052 Opcode = ISD::VP_REDUCE_OR;
11053 break;
11054 }
11055
11056 // Memoize nodes.
11057 SDNode *N;
11058 SDVTList VTs = getVTList(VT);
11059
11060 if (VT != MVT::Glue) {
11062 AddNodeIDNode(ID, Opcode, VTs, Ops);
11063 void *IP = nullptr;
11064
11065 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11066 E->intersectFlagsWith(Flags);
11067 return SDValue(E, 0);
11068 }
11069
11070 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11071 createOperands(N, Ops);
11072
11073 CSEMap.InsertNode(N, IP);
11074 } else {
11075 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11076 createOperands(N, Ops);
11077 }
11078
11079 N->setFlags(Flags);
11080 InsertNode(N);
11081 SDValue V(N, 0);
11082 NewSDValueDbgMsg(V, "Creating new node: ", this);
11083 return V;
11084}
11085
11086SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11087 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11088 SDNodeFlags Flags;
11089 if (Inserter)
11090 Flags = Inserter->getFlags();
11091 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11092}
11093
11094SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11096 const SDNodeFlags Flags) {
11097 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11098}
11099
11100SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11102 SDNodeFlags Flags;
11103 if (Inserter)
11104 Flags = Inserter->getFlags();
11105 return getNode(Opcode, DL, VTList, Ops, Flags);
11106}
11107
11108SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11109 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11110 if (VTList.NumVTs == 1)
11111 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11112
11113#ifndef NDEBUG
11114 for (const auto &Op : Ops)
11115 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11116 "Operand is DELETED_NODE!");
11117#endif
11118
11119 switch (Opcode) {
11120 case ISD::SADDO:
11121 case ISD::UADDO:
11122 case ISD::SSUBO:
11123 case ISD::USUBO: {
11124 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11125 "Invalid add/sub overflow op!");
11126 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11127 Ops[0].getValueType() == Ops[1].getValueType() &&
11128 Ops[0].getValueType() == VTList.VTs[0] &&
11129 "Binary operator types must match!");
11130 SDValue N1 = Ops[0], N2 = Ops[1];
11131 canonicalizeCommutativeBinop(Opcode, N1, N2);
11132
11133 // (X +- 0) -> X with zero-overflow.
11134 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11135 /*AllowTruncation*/ true);
11136 if (N2CV && N2CV->isZero()) {
11137 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11138 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11139 }
11140
11141 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11142 VTList.VTs[1].getScalarType() == MVT::i1) {
11143 SDValue F1 = getFreeze(N1);
11144 SDValue F2 = getFreeze(N2);
11145 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11146 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11147 return getNode(ISD::MERGE_VALUES, DL, VTList,
11148 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11149 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11150 Flags);
11151 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11152 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11153 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11154 return getNode(ISD::MERGE_VALUES, DL, VTList,
11155 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11156 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11157 Flags);
11158 }
11159 }
11160 break;
11161 }
11162 case ISD::SADDO_CARRY:
11163 case ISD::UADDO_CARRY:
11164 case ISD::SSUBO_CARRY:
11165 case ISD::USUBO_CARRY:
11166 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11167 "Invalid add/sub overflow op!");
11168 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11169 Ops[0].getValueType() == Ops[1].getValueType() &&
11170 Ops[0].getValueType() == VTList.VTs[0] &&
11171 Ops[2].getValueType() == VTList.VTs[1] &&
11172 "Binary operator types must match!");
11173 break;
11174 case ISD::SMUL_LOHI:
11175 case ISD::UMUL_LOHI: {
11176 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11177 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11178 VTList.VTs[0] == Ops[0].getValueType() &&
11179 VTList.VTs[0] == Ops[1].getValueType() &&
11180 "Binary operator types must match!");
11181 // Constant fold.
11184 if (LHS && RHS) {
11185 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11186 unsigned OutWidth = Width * 2;
11187 APInt Val = LHS->getAPIntValue();
11188 APInt Mul = RHS->getAPIntValue();
11189 if (Opcode == ISD::SMUL_LOHI) {
11190 Val = Val.sext(OutWidth);
11191 Mul = Mul.sext(OutWidth);
11192 } else {
11193 Val = Val.zext(OutWidth);
11194 Mul = Mul.zext(OutWidth);
11195 }
11196 Val *= Mul;
11197
11198 SDValue Hi =
11199 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11200 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11201 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11202 }
11203 break;
11204 }
11205 case ISD::FFREXP: {
11206 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11207 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11208 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11209
11211 int FrexpExp;
11212 APFloat FrexpMant =
11213 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11214 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11215 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11216 DL, VTList.VTs[1]);
11217 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11218 }
11219
11220 break;
11221 }
11223 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11224 "Invalid STRICT_FP_EXTEND!");
11225 assert(VTList.VTs[0].isFloatingPoint() &&
11226 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11227 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11228 "STRICT_FP_EXTEND result type should be vector iff the operand "
11229 "type is vector!");
11230 assert((!VTList.VTs[0].isVector() ||
11231 VTList.VTs[0].getVectorElementCount() ==
11232 Ops[1].getValueType().getVectorElementCount()) &&
11233 "Vector element count mismatch!");
11234 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11235 "Invalid fpext node, dst <= src!");
11236 break;
11238 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11239 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11240 "STRICT_FP_ROUND result type should be vector iff the operand "
11241 "type is vector!");
11242 assert((!VTList.VTs[0].isVector() ||
11243 VTList.VTs[0].getVectorElementCount() ==
11244 Ops[1].getValueType().getVectorElementCount()) &&
11245 "Vector element count mismatch!");
11246 assert(VTList.VTs[0].isFloatingPoint() &&
11247 Ops[1].getValueType().isFloatingPoint() &&
11248 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11249 Ops[2].getOpcode() == ISD::TargetConstant &&
11250 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11251 "Invalid STRICT_FP_ROUND!");
11252 break;
11253 }
11254
11255 // Memoize the node unless it returns a glue result.
11256 SDNode *N;
11257 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11259 AddNodeIDNode(ID, Opcode, VTList, Ops);
11260 void *IP = nullptr;
11261 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11262 E->intersectFlagsWith(Flags);
11263 return SDValue(E, 0);
11264 }
11265
11266 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11267 createOperands(N, Ops);
11268 CSEMap.InsertNode(N, IP);
11269 } else {
11270 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11271 createOperands(N, Ops);
11272 }
11273
11274 N->setFlags(Flags);
11275 InsertNode(N);
11276 SDValue V(N, 0);
11277 NewSDValueDbgMsg(V, "Creating new node: ", this);
11278 return V;
11279}
11280
11281SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11282 SDVTList VTList) {
11283 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11284}
11285
11286SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11287 SDValue N1) {
11288 SDValue Ops[] = { N1 };
11289 return getNode(Opcode, DL, VTList, Ops);
11290}
11291
11292SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11293 SDValue N1, SDValue N2) {
11294 SDValue Ops[] = { N1, N2 };
11295 return getNode(Opcode, DL, VTList, Ops);
11296}
11297
11298SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11299 SDValue N1, SDValue N2, SDValue N3) {
11300 SDValue Ops[] = { N1, N2, N3 };
11301 return getNode(Opcode, DL, VTList, Ops);
11302}
11303
11304SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11305 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11306 SDValue Ops[] = { N1, N2, N3, N4 };
11307 return getNode(Opcode, DL, VTList, Ops);
11308}
11309
11310SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11311 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11312 SDValue N5) {
11313 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11314 return getNode(Opcode, DL, VTList, Ops);
11315}
11316
11318 if (!VT.isExtended())
11319 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11320
11321 return makeVTList(&(*EVTs.insert(VT).first), 1);
11322}
11323
11326 ID.AddInteger(2U);
11327 ID.AddInteger(VT1.getRawBits());
11328 ID.AddInteger(VT2.getRawBits());
11329
11330 void *IP = nullptr;
11331 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11332 if (!Result) {
11333 EVT *Array = Allocator.Allocate<EVT>(2);
11334 Array[0] = VT1;
11335 Array[1] = VT2;
11336 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11337 VTListMap.InsertNode(Result, IP);
11338 }
11339 return Result->getSDVTList();
11340}
11341
11344 ID.AddInteger(3U);
11345 ID.AddInteger(VT1.getRawBits());
11346 ID.AddInteger(VT2.getRawBits());
11347 ID.AddInteger(VT3.getRawBits());
11348
11349 void *IP = nullptr;
11350 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11351 if (!Result) {
11352 EVT *Array = Allocator.Allocate<EVT>(3);
11353 Array[0] = VT1;
11354 Array[1] = VT2;
11355 Array[2] = VT3;
11356 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11357 VTListMap.InsertNode(Result, IP);
11358 }
11359 return Result->getSDVTList();
11360}
11361
11364 ID.AddInteger(4U);
11365 ID.AddInteger(VT1.getRawBits());
11366 ID.AddInteger(VT2.getRawBits());
11367 ID.AddInteger(VT3.getRawBits());
11368 ID.AddInteger(VT4.getRawBits());
11369
11370 void *IP = nullptr;
11371 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11372 if (!Result) {
11373 EVT *Array = Allocator.Allocate<EVT>(4);
11374 Array[0] = VT1;
11375 Array[1] = VT2;
11376 Array[2] = VT3;
11377 Array[3] = VT4;
11378 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11379 VTListMap.InsertNode(Result, IP);
11380 }
11381 return Result->getSDVTList();
11382}
11383
11385 unsigned NumVTs = VTs.size();
11387 ID.AddInteger(NumVTs);
11388 for (unsigned index = 0; index < NumVTs; index++) {
11389 ID.AddInteger(VTs[index].getRawBits());
11390 }
11391
11392 void *IP = nullptr;
11393 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11394 if (!Result) {
11395 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11396 llvm::copy(VTs, Array);
11397 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11398 VTListMap.InsertNode(Result, IP);
11399 }
11400 return Result->getSDVTList();
11401}
11402
11403
11404/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11405/// specified operands. If the resultant node already exists in the DAG,
11406/// this does not modify the specified node, instead it returns the node that
11407/// already exists. If the resultant node does not exist in the DAG, the
11408/// input node is returned. As a degenerate case, if you specify the same
11409/// input operands as the node already has, the input node is returned.
11411 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11412
11413 // Check to see if there is no change.
11414 if (Op == N->getOperand(0)) return N;
11415
11416 // See if the modified node already exists.
11417 void *InsertPos = nullptr;
11418 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11419 return Existing;
11420
11421 // Nope it doesn't. Remove the node from its current place in the maps.
11422 if (InsertPos)
11423 if (!RemoveNodeFromCSEMaps(N))
11424 InsertPos = nullptr;
11425
11426 // Now we update the operands.
11427 N->OperandList[0].set(Op);
11428
11430 // If this gets put into a CSE map, add it.
11431 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11432 return N;
11433}
11434
11436 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11437
11438 // Check to see if there is no change.
11439 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11440 return N; // No operands changed, just return the input node.
11441
11442 // See if the modified node already exists.
11443 void *InsertPos = nullptr;
11444 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11445 return Existing;
11446
11447 // Nope it doesn't. Remove the node from its current place in the maps.
11448 if (InsertPos)
11449 if (!RemoveNodeFromCSEMaps(N))
11450 InsertPos = nullptr;
11451
11452 // Now we update the operands.
11453 if (N->OperandList[0] != Op1)
11454 N->OperandList[0].set(Op1);
11455 if (N->OperandList[1] != Op2)
11456 N->OperandList[1].set(Op2);
11457
11459 // If this gets put into a CSE map, add it.
11460 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11461 return N;
11462}
11463
11466 SDValue Ops[] = { Op1, Op2, Op3 };
11467 return UpdateNodeOperands(N, Ops);
11468}
11469
11472 SDValue Op3, SDValue Op4) {
11473 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11474 return UpdateNodeOperands(N, Ops);
11475}
11476
11479 SDValue Op3, SDValue Op4, SDValue Op5) {
11480 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11481 return UpdateNodeOperands(N, Ops);
11482}
11483
11486 unsigned NumOps = Ops.size();
11487 assert(N->getNumOperands() == NumOps &&
11488 "Update with wrong number of operands");
11489
11490 // If no operands changed just return the input node.
11491 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11492 return N;
11493
11494 // See if the modified node already exists.
11495 void *InsertPos = nullptr;
11496 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11497 return Existing;
11498
11499 // Nope it doesn't. Remove the node from its current place in the maps.
11500 if (InsertPos)
11501 if (!RemoveNodeFromCSEMaps(N))
11502 InsertPos = nullptr;
11503
11504 // Now we update the operands.
11505 for (unsigned i = 0; i != NumOps; ++i)
11506 if (N->OperandList[i] != Ops[i])
11507 N->OperandList[i].set(Ops[i]);
11508
11510 // If this gets put into a CSE map, add it.
11511 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11512 return N;
11513}
11514
11515/// DropOperands - Release the operands and set this node to have
11516/// zero operands.
11518 // Unlike the code in MorphNodeTo that does this, we don't need to
11519 // watch for dead nodes here.
11520 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11521 SDUse &Use = *I++;
11522 Use.set(SDValue());
11523 }
11524}
11525
11527 ArrayRef<MachineMemOperand *> NewMemRefs) {
11528 if (NewMemRefs.empty()) {
11529 N->clearMemRefs();
11530 return;
11531 }
11532
11533 // Check if we can avoid allocating by storing a single reference directly.
11534 if (NewMemRefs.size() == 1) {
11535 N->MemRefs = NewMemRefs[0];
11536 N->NumMemRefs = 1;
11537 return;
11538 }
11539
11540 MachineMemOperand **MemRefsBuffer =
11541 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11542 llvm::copy(NewMemRefs, MemRefsBuffer);
11543 N->MemRefs = MemRefsBuffer;
11544 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11545}
11546
11547/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11548/// machine opcode.
11549///
11551 EVT VT) {
11552 SDVTList VTs = getVTList(VT);
11553 return SelectNodeTo(N, MachineOpc, VTs, {});
11554}
11555
11557 EVT VT, SDValue Op1) {
11558 SDVTList VTs = getVTList(VT);
11559 SDValue Ops[] = { Op1 };
11560 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11561}
11562
11564 EVT VT, SDValue Op1,
11565 SDValue Op2) {
11566 SDVTList VTs = getVTList(VT);
11567 SDValue Ops[] = { Op1, Op2 };
11568 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11569}
11570
11572 EVT VT, SDValue Op1,
11573 SDValue Op2, SDValue Op3) {
11574 SDVTList VTs = getVTList(VT);
11575 SDValue Ops[] = { Op1, Op2, Op3 };
11576 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11577}
11578
11581 SDVTList VTs = getVTList(VT);
11582 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11583}
11584
11586 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11587 SDVTList VTs = getVTList(VT1, VT2);
11588 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11589}
11590
11592 EVT VT1, EVT VT2) {
11593 SDVTList VTs = getVTList(VT1, VT2);
11594 return SelectNodeTo(N, MachineOpc, VTs, {});
11595}
11596
11598 EVT VT1, EVT VT2, EVT VT3,
11600 SDVTList VTs = getVTList(VT1, VT2, VT3);
11601 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11602}
11603
11605 EVT VT1, EVT VT2,
11606 SDValue Op1, SDValue Op2) {
11607 SDVTList VTs = getVTList(VT1, VT2);
11608 SDValue Ops[] = { Op1, Op2 };
11609 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11610}
11611
11614 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11615 // Reset the NodeID to -1.
11616 New->setNodeId(-1);
11617 if (New != N) {
11618 ReplaceAllUsesWith(N, New);
11620 }
11621 return New;
11622}
11623
11624/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11625/// the line number information on the merged node since it is not possible to
11626/// preserve the information that operation is associated with multiple lines.
11627/// This will make the debugger working better at -O0, were there is a higher
11628/// probability having other instructions associated with that line.
11629///
11630/// For IROrder, we keep the smaller of the two
11631SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11632 DebugLoc NLoc = N->getDebugLoc();
11633 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11634 N->setDebugLoc(DebugLoc());
11635 }
11636 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11637 N->setIROrder(Order);
11638 return N;
11639}
11640
11641/// MorphNodeTo - This *mutates* the specified node to have the specified
11642/// return type, opcode, and operands.
11643///
11644/// Note that MorphNodeTo returns the resultant node. If there is already a
11645/// node of the specified opcode and operands, it returns that node instead of
11646/// the current one. Note that the SDLoc need not be the same.
11647///
11648/// Using MorphNodeTo is faster than creating a new node and swapping it in
11649/// with ReplaceAllUsesWith both because it often avoids allocating a new
11650/// node, and because it doesn't require CSE recalculation for any of
11651/// the node's users.
11652///
11653/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11654/// As a consequence it isn't appropriate to use from within the DAG combiner or
11655/// the legalizer which maintain worklists that would need to be updated when
11656/// deleting things.
11659 // If an identical node already exists, use it.
11660 void *IP = nullptr;
11661 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11663 AddNodeIDNode(ID, Opc, VTs, Ops);
11664 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11665 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11666 }
11667
11668 if (!RemoveNodeFromCSEMaps(N))
11669 IP = nullptr;
11670
11671 // Start the morphing.
11672 N->NodeType = Opc;
11673 N->ValueList = VTs.VTs;
11674 N->NumValues = VTs.NumVTs;
11675
11676 // Clear the operands list, updating used nodes to remove this from their
11677 // use list. Keep track of any operands that become dead as a result.
11678 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11679 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11680 SDUse &Use = *I++;
11681 SDNode *Used = Use.getNode();
11682 Use.set(SDValue());
11683 if (Used->use_empty())
11684 DeadNodeSet.insert(Used);
11685 }
11686
11687 // For MachineNode, initialize the memory references information.
11689 MN->clearMemRefs();
11690
11691 // Swap for an appropriately sized array from the recycler.
11692 removeOperands(N);
11693 createOperands(N, Ops);
11694
11695 // Delete any nodes that are still dead after adding the uses for the
11696 // new operands.
11697 if (!DeadNodeSet.empty()) {
11698 SmallVector<SDNode *, 16> DeadNodes;
11699 for (SDNode *N : DeadNodeSet)
11700 if (N->use_empty())
11701 DeadNodes.push_back(N);
11702 RemoveDeadNodes(DeadNodes);
11703 }
11704
11705 if (IP)
11706 CSEMap.InsertNode(N, IP); // Memoize the new node.
11707 return N;
11708}
11709
11711 unsigned OrigOpc = Node->getOpcode();
11712 unsigned NewOpc;
11713 switch (OrigOpc) {
11714 default:
11715 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11716#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11717 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11718#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11719 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11720#include "llvm/IR/ConstrainedOps.def"
11721 }
11722
11723 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11724
11725 // We're taking this node out of the chain, so we need to re-link things.
11726 SDValue InputChain = Node->getOperand(0);
11727 SDValue OutputChain = SDValue(Node, 1);
11728 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11729
11731 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11732 Ops.push_back(Node->getOperand(i));
11733
11734 SDVTList VTs = getVTList(Node->getValueType(0));
11735 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11736
11737 // MorphNodeTo can operate in two ways: if an existing node with the
11738 // specified operands exists, it can just return it. Otherwise, it
11739 // updates the node in place to have the requested operands.
11740 if (Res == Node) {
11741 // If we updated the node in place, reset the node ID. To the isel,
11742 // this should be just like a newly allocated machine node.
11743 Res->setNodeId(-1);
11744 } else {
11747 }
11748
11749 return Res;
11750}
11751
11752/// getMachineNode - These are used for target selectors to create a new node
11753/// with specified return type(s), MachineInstr opcode, and operands.
11754///
11755/// Note that getMachineNode returns the resultant node. If there is already a
11756/// node of the specified opcode and operands, it returns that node instead of
11757/// the current one.
11759 EVT VT) {
11760 SDVTList VTs = getVTList(VT);
11761 return getMachineNode(Opcode, dl, VTs, {});
11762}
11763
11765 EVT VT, SDValue Op1) {
11766 SDVTList VTs = getVTList(VT);
11767 SDValue Ops[] = { Op1 };
11768 return getMachineNode(Opcode, dl, VTs, Ops);
11769}
11770
11772 EVT VT, SDValue Op1, SDValue Op2) {
11773 SDVTList VTs = getVTList(VT);
11774 SDValue Ops[] = { Op1, Op2 };
11775 return getMachineNode(Opcode, dl, VTs, Ops);
11776}
11777
11779 EVT VT, SDValue Op1, SDValue Op2,
11780 SDValue Op3) {
11781 SDVTList VTs = getVTList(VT);
11782 SDValue Ops[] = { Op1, Op2, Op3 };
11783 return getMachineNode(Opcode, dl, VTs, Ops);
11784}
11785
11788 SDVTList VTs = getVTList(VT);
11789 return getMachineNode(Opcode, dl, VTs, Ops);
11790}
11791
11793 EVT VT1, EVT VT2, SDValue Op1,
11794 SDValue Op2) {
11795 SDVTList VTs = getVTList(VT1, VT2);
11796 SDValue Ops[] = { Op1, Op2 };
11797 return getMachineNode(Opcode, dl, VTs, Ops);
11798}
11799
11801 EVT VT1, EVT VT2, SDValue Op1,
11802 SDValue Op2, SDValue Op3) {
11803 SDVTList VTs = getVTList(VT1, VT2);
11804 SDValue Ops[] = { Op1, Op2, Op3 };
11805 return getMachineNode(Opcode, dl, VTs, Ops);
11806}
11807
11809 EVT VT1, EVT VT2,
11811 SDVTList VTs = getVTList(VT1, VT2);
11812 return getMachineNode(Opcode, dl, VTs, Ops);
11813}
11814
11816 EVT VT1, EVT VT2, EVT VT3,
11817 SDValue Op1, SDValue Op2) {
11818 SDVTList VTs = getVTList(VT1, VT2, VT3);
11819 SDValue Ops[] = { Op1, Op2 };
11820 return getMachineNode(Opcode, dl, VTs, Ops);
11821}
11822
11824 EVT VT1, EVT VT2, EVT VT3,
11825 SDValue Op1, SDValue Op2,
11826 SDValue Op3) {
11827 SDVTList VTs = getVTList(VT1, VT2, VT3);
11828 SDValue Ops[] = { Op1, Op2, Op3 };
11829 return getMachineNode(Opcode, dl, VTs, Ops);
11830}
11831
11833 EVT VT1, EVT VT2, EVT VT3,
11835 SDVTList VTs = getVTList(VT1, VT2, VT3);
11836 return getMachineNode(Opcode, dl, VTs, Ops);
11837}
11838
11840 ArrayRef<EVT> ResultTys,
11842 SDVTList VTs = getVTList(ResultTys);
11843 return getMachineNode(Opcode, dl, VTs, Ops);
11844}
11845
11847 SDVTList VTs,
11849 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11851 void *IP = nullptr;
11852
11853 if (DoCSE) {
11855 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11856 IP = nullptr;
11857 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11858 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11859 }
11860 }
11861
11862 // Allocate a new MachineSDNode.
11863 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11864 createOperands(N, Ops);
11865
11866 if (DoCSE)
11867 CSEMap.InsertNode(N, IP);
11868
11869 InsertNode(N);
11870 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11871 return N;
11872}
11873
11874/// getTargetExtractSubreg - A convenience function for creating
11875/// TargetOpcode::EXTRACT_SUBREG nodes.
11877 SDValue Operand) {
11878 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11879 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11880 VT, Operand, SRIdxVal);
11881 return SDValue(Subreg, 0);
11882}
11883
11884/// getTargetInsertSubreg - A convenience function for creating
11885/// TargetOpcode::INSERT_SUBREG nodes.
11887 SDValue Operand, SDValue Subreg) {
11888 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11889 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11890 VT, Operand, Subreg, SRIdxVal);
11891 return SDValue(Result, 0);
11892}
11893
11894/// getNodeIfExists - Get the specified node if it's already available, or
11895/// else return NULL.
11898 bool AllowCommute) {
11899 SDNodeFlags Flags;
11900 if (Inserter)
11901 Flags = Inserter->getFlags();
11902 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11903}
11904
11907 const SDNodeFlags Flags,
11908 bool AllowCommute) {
11909 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11910 return nullptr;
11911
11912 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11914 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11915 void *IP = nullptr;
11916 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11917 E->intersectFlagsWith(Flags);
11918 return E;
11919 }
11920 return nullptr;
11921 };
11922
11923 if (SDNode *Existing = Lookup(Ops))
11924 return Existing;
11925
11926 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11927 return Lookup({Ops[1], Ops[0]});
11928
11929 return nullptr;
11930}
11931
11932/// doesNodeExist - Check if a node exists without modifying its flags.
11933bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11935 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11937 AddNodeIDNode(ID, Opcode, VTList, Ops);
11938 void *IP = nullptr;
11939 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11940 return true;
11941 }
11942 return false;
11943}
11944
11945/// getDbgValue - Creates a SDDbgValue node.
11946///
11947/// SDNode
11949 SDNode *N, unsigned R, bool IsIndirect,
11950 const DebugLoc &DL, unsigned O) {
11951 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11952 "Expected inlined-at fields to agree");
11953 return new (DbgInfo->getAlloc())
11954 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11955 {}, IsIndirect, DL, O,
11956 /*IsVariadic=*/false);
11957}
11958
11959/// Constant
11961 DIExpression *Expr,
11962 const Value *C,
11963 const DebugLoc &DL, unsigned O) {
11964 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11965 "Expected inlined-at fields to agree");
11966 return new (DbgInfo->getAlloc())
11967 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11968 /*IsIndirect=*/false, DL, O,
11969 /*IsVariadic=*/false);
11970}
11971
11972/// FrameIndex
11974 DIExpression *Expr, unsigned FI,
11975 bool IsIndirect,
11976 const DebugLoc &DL,
11977 unsigned O) {
11978 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11979 "Expected inlined-at fields to agree");
11980 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11981}
11982
11983/// FrameIndex with dependencies
11985 DIExpression *Expr, unsigned FI,
11986 ArrayRef<SDNode *> Dependencies,
11987 bool IsIndirect,
11988 const DebugLoc &DL,
11989 unsigned O) {
11990 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11991 "Expected inlined-at fields to agree");
11992 return new (DbgInfo->getAlloc())
11993 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11994 Dependencies, IsIndirect, DL, O,
11995 /*IsVariadic=*/false);
11996}
11997
11998/// VReg
12000 Register VReg, bool IsIndirect,
12001 const DebugLoc &DL, unsigned O) {
12002 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12003 "Expected inlined-at fields to agree");
12004 return new (DbgInfo->getAlloc())
12005 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12006 {}, IsIndirect, DL, O,
12007 /*IsVariadic=*/false);
12008}
12009
12012 ArrayRef<SDNode *> Dependencies,
12013 bool IsIndirect, const DebugLoc &DL,
12014 unsigned O, bool IsVariadic) {
12015 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12016 "Expected inlined-at fields to agree");
12017 return new (DbgInfo->getAlloc())
12018 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12019 DL, O, IsVariadic);
12020}
12021
12023 unsigned OffsetInBits, unsigned SizeInBits,
12024 bool InvalidateDbg) {
12025 SDNode *FromNode = From.getNode();
12026 SDNode *ToNode = To.getNode();
12027 assert(FromNode && ToNode && "Can't modify dbg values");
12028
12029 // PR35338
12030 // TODO: assert(From != To && "Redundant dbg value transfer");
12031 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12032 if (From == To || FromNode == ToNode)
12033 return;
12034
12035 if (!FromNode->getHasDebugValue())
12036 return;
12037
12038 SDDbgOperand FromLocOp =
12039 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12041
12043 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12044 if (Dbg->isInvalidated())
12045 continue;
12046
12047 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12048
12049 // Create a new location ops vector that is equal to the old vector, but
12050 // with each instance of FromLocOp replaced with ToLocOp.
12051 bool Changed = false;
12052 auto NewLocOps = Dbg->copyLocationOps();
12053 std::replace_if(
12054 NewLocOps.begin(), NewLocOps.end(),
12055 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12056 bool Match = Op == FromLocOp;
12057 Changed |= Match;
12058 return Match;
12059 },
12060 ToLocOp);
12061 // Ignore this SDDbgValue if we didn't find a matching location.
12062 if (!Changed)
12063 continue;
12064
12065 DIVariable *Var = Dbg->getVariable();
12066 auto *Expr = Dbg->getExpression();
12067 // If a fragment is requested, update the expression.
12068 if (SizeInBits) {
12069 // When splitting a larger (e.g., sign-extended) value whose
12070 // lower bits are described with an SDDbgValue, do not attempt
12071 // to transfer the SDDbgValue to the upper bits.
12072 if (auto FI = Expr->getFragmentInfo())
12073 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12074 continue;
12075 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12076 SizeInBits);
12077 if (!Fragment)
12078 continue;
12079 Expr = *Fragment;
12080 }
12081
12082 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12083 // Clone the SDDbgValue and move it to To.
12084 SDDbgValue *Clone = getDbgValueList(
12085 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12086 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12087 Dbg->isVariadic());
12088 ClonedDVs.push_back(Clone);
12089
12090 if (InvalidateDbg) {
12091 // Invalidate value and indicate the SDDbgValue should not be emitted.
12092 Dbg->setIsInvalidated();
12093 Dbg->setIsEmitted();
12094 }
12095 }
12096
12097 for (SDDbgValue *Dbg : ClonedDVs) {
12098 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12099 "Transferred DbgValues should depend on the new SDNode");
12100 AddDbgValue(Dbg, false);
12101 }
12102}
12103
12105 if (!N.getHasDebugValue())
12106 return;
12107
12108 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12109 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12110 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12111 return SDDbgOperand::fromNode(Node, ResNo);
12112 };
12113
12115 for (auto *DV : GetDbgValues(&N)) {
12116 if (DV->isInvalidated())
12117 continue;
12118 switch (N.getOpcode()) {
12119 default:
12120 break;
12121 case ISD::ADD: {
12122 SDValue N0 = N.getOperand(0);
12123 SDValue N1 = N.getOperand(1);
12124 if (!isa<ConstantSDNode>(N0)) {
12125 bool RHSConstant = isa<ConstantSDNode>(N1);
12127 if (RHSConstant)
12128 Offset = N.getConstantOperandVal(1);
12129 // We are not allowed to turn indirect debug values variadic, so
12130 // don't salvage those.
12131 if (!RHSConstant && DV->isIndirect())
12132 continue;
12133
12134 // Rewrite an ADD constant node into a DIExpression. Since we are
12135 // performing arithmetic to compute the variable's *value* in the
12136 // DIExpression, we need to mark the expression with a
12137 // DW_OP_stack_value.
12138 auto *DIExpr = DV->getExpression();
12139 auto NewLocOps = DV->copyLocationOps();
12140 bool Changed = false;
12141 size_t OrigLocOpsSize = NewLocOps.size();
12142 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12143 // We're not given a ResNo to compare against because the whole
12144 // node is going away. We know that any ISD::ADD only has one
12145 // result, so we can assume any node match is using the result.
12146 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12147 NewLocOps[i].getSDNode() != &N)
12148 continue;
12149 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12150 if (RHSConstant) {
12153 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12154 } else {
12155 // Convert to a variadic expression (if not already).
12156 // convertToVariadicExpression() returns a const pointer, so we use
12157 // a temporary const variable here.
12158 const auto *TmpDIExpr =
12162 ExprOps.push_back(NewLocOps.size());
12163 ExprOps.push_back(dwarf::DW_OP_plus);
12164 SDDbgOperand RHS =
12166 NewLocOps.push_back(RHS);
12167 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12168 }
12169 Changed = true;
12170 }
12171 (void)Changed;
12172 assert(Changed && "Salvage target doesn't use N");
12173
12174 bool IsVariadic =
12175 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12176
12177 auto AdditionalDependencies = DV->getAdditionalDependencies();
12178 SDDbgValue *Clone = getDbgValueList(
12179 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12180 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12181 ClonedDVs.push_back(Clone);
12182 DV->setIsInvalidated();
12183 DV->setIsEmitted();
12184 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12185 N0.getNode()->dumprFull(this);
12186 dbgs() << " into " << *DIExpr << '\n');
12187 }
12188 break;
12189 }
12190 case ISD::TRUNCATE: {
12191 SDValue N0 = N.getOperand(0);
12192 TypeSize FromSize = N0.getValueSizeInBits();
12193 TypeSize ToSize = N.getValueSizeInBits(0);
12194
12195 DIExpression *DbgExpression = DV->getExpression();
12196 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12197 auto NewLocOps = DV->copyLocationOps();
12198 bool Changed = false;
12199 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12200 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12201 NewLocOps[i].getSDNode() != &N)
12202 continue;
12203
12204 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12205 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12206 Changed = true;
12207 }
12208 assert(Changed && "Salvage target doesn't use N");
12209 (void)Changed;
12210
12211 SDDbgValue *Clone =
12212 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12213 DV->getAdditionalDependencies(), DV->isIndirect(),
12214 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12215
12216 ClonedDVs.push_back(Clone);
12217 DV->setIsInvalidated();
12218 DV->setIsEmitted();
12219 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12220 dbgs() << " into " << *DbgExpression << '\n');
12221 break;
12222 }
12223 }
12224 }
12225
12226 for (SDDbgValue *Dbg : ClonedDVs) {
12227 assert((!Dbg->getSDNodes().empty() ||
12228 llvm::any_of(Dbg->getLocationOps(),
12229 [&](const SDDbgOperand &Op) {
12230 return Op.getKind() == SDDbgOperand::FRAMEIX;
12231 })) &&
12232 "Salvaged DbgValue should depend on a new SDNode");
12233 AddDbgValue(Dbg, false);
12234 }
12235}
12236
12237/// Creates a SDDbgLabel node.
12239 const DebugLoc &DL, unsigned O) {
12240 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12241 "Expected inlined-at fields to agree");
12242 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12243}
12244
12245namespace {
12246
12247/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12248/// pointed to by a use iterator is deleted, increment the use iterator
12249/// so that it doesn't dangle.
12250///
12251class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12254
12255 void NodeDeleted(SDNode *N, SDNode *E) override {
12256 // Increment the iterator as needed.
12257 while (UI != UE && N == UI->getUser())
12258 ++UI;
12259 }
12260
12261public:
12262 RAUWUpdateListener(SelectionDAG &d,
12265 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12266};
12267
12268} // end anonymous namespace
12269
12270/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12271/// This can cause recursive merging of nodes in the DAG.
12272///
12273/// This version assumes From has a single result value.
12274///
12276 SDNode *From = FromN.getNode();
12277 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12278 "Cannot replace with this method!");
12279 assert(From != To.getNode() && "Cannot replace uses of with self");
12280
12281 // Preserve Debug Values
12282 transferDbgValues(FromN, To);
12283 // Preserve extra info.
12284 copyExtraInfo(From, To.getNode());
12285
12286 // Iterate over all the existing uses of From. New uses will be added
12287 // to the beginning of the use list, which we avoid visiting.
12288 // This specifically avoids visiting uses of From that arise while the
12289 // replacement is happening, because any such uses would be the result
12290 // of CSE: If an existing node looks like From after one of its operands
12291 // is replaced by To, we don't want to replace of all its users with To
12292 // too. See PR3018 for more info.
12293 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12294 RAUWUpdateListener Listener(*this, UI, UE);
12295 while (UI != UE) {
12296 SDNode *User = UI->getUser();
12297
12298 // This node is about to morph, remove its old self from the CSE maps.
12299 RemoveNodeFromCSEMaps(User);
12300
12301 // A user can appear in a use list multiple times, and when this
12302 // happens the uses are usually next to each other in the list.
12303 // To help reduce the number of CSE recomputations, process all
12304 // the uses of this user that we can find this way.
12305 do {
12306 SDUse &Use = *UI;
12307 ++UI;
12308 Use.set(To);
12309 if (To->isDivergent() != From->isDivergent())
12311 } while (UI != UE && UI->getUser() == User);
12312 // Now that we have modified User, add it back to the CSE maps. If it
12313 // already exists there, recursively merge the results together.
12314 AddModifiedNodeToCSEMaps(User);
12315 }
12316
12317 // If we just RAUW'd the root, take note.
12318 if (FromN == getRoot())
12319 setRoot(To);
12320}
12321
12322/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12323/// This can cause recursive merging of nodes in the DAG.
12324///
12325/// This version assumes that for each value of From, there is a
12326/// corresponding value in To in the same position with the same type.
12327///
12329#ifndef NDEBUG
12330 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12331 assert((!From->hasAnyUseOfValue(i) ||
12332 From->getValueType(i) == To->getValueType(i)) &&
12333 "Cannot use this version of ReplaceAllUsesWith!");
12334#endif
12335
12336 // Handle the trivial case.
12337 if (From == To)
12338 return;
12339
12340 // Preserve Debug Info. Only do this if there's a use.
12341 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12342 if (From->hasAnyUseOfValue(i)) {
12343 assert((i < To->getNumValues()) && "Invalid To location");
12344 transferDbgValues(SDValue(From, i), SDValue(To, i));
12345 }
12346 // Preserve extra info.
12347 copyExtraInfo(From, To);
12348
12349 // Iterate over just the existing users of From. See the comments in
12350 // the ReplaceAllUsesWith above.
12351 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12352 RAUWUpdateListener Listener(*this, UI, UE);
12353 while (UI != UE) {
12354 SDNode *User = UI->getUser();
12355
12356 // This node is about to morph, remove its old self from the CSE maps.
12357 RemoveNodeFromCSEMaps(User);
12358
12359 // A user can appear in a use list multiple times, and when this
12360 // happens the uses are usually next to each other in the list.
12361 // To help reduce the number of CSE recomputations, process all
12362 // the uses of this user that we can find this way.
12363 do {
12364 SDUse &Use = *UI;
12365 ++UI;
12366 Use.setNode(To);
12367 if (To->isDivergent() != From->isDivergent())
12369 } while (UI != UE && UI->getUser() == User);
12370
12371 // Now that we have modified User, add it back to the CSE maps. If it
12372 // already exists there, recursively merge the results together.
12373 AddModifiedNodeToCSEMaps(User);
12374 }
12375
12376 // If we just RAUW'd the root, take note.
12377 if (From == getRoot().getNode())
12378 setRoot(SDValue(To, getRoot().getResNo()));
12379}
12380
12381/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12382/// This can cause recursive merging of nodes in the DAG.
12383///
12384/// This version can replace From with any result values. To must match the
12385/// number and types of values returned by From.
12387 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12388 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12389
12390 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12391 // Preserve Debug Info.
12392 transferDbgValues(SDValue(From, i), To[i]);
12393 // Preserve extra info.
12394 copyExtraInfo(From, To[i].getNode());
12395 }
12396
12397 // Iterate over just the existing users of From. See the comments in
12398 // the ReplaceAllUsesWith above.
12399 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12400 RAUWUpdateListener Listener(*this, UI, UE);
12401 while (UI != UE) {
12402 SDNode *User = UI->getUser();
12403
12404 // This node is about to morph, remove its old self from the CSE maps.
12405 RemoveNodeFromCSEMaps(User);
12406
12407 // A user can appear in a use list multiple times, and when this happens the
12408 // uses are usually next to each other in the list. To help reduce the
12409 // number of CSE and divergence recomputations, process all the uses of this
12410 // user that we can find this way.
12411 bool To_IsDivergent = false;
12412 do {
12413 SDUse &Use = *UI;
12414 const SDValue &ToOp = To[Use.getResNo()];
12415 ++UI;
12416 Use.set(ToOp);
12417 if (ToOp.getValueType() != MVT::Other)
12418 To_IsDivergent |= ToOp->isDivergent();
12419 } while (UI != UE && UI->getUser() == User);
12420
12421 if (To_IsDivergent != From->isDivergent())
12423
12424 // Now that we have modified User, add it back to the CSE maps. If it
12425 // already exists there, recursively merge the results together.
12426 AddModifiedNodeToCSEMaps(User);
12427 }
12428
12429 // If we just RAUW'd the root, take note.
12430 if (From == getRoot().getNode())
12431 setRoot(SDValue(To[getRoot().getResNo()]));
12432}
12433
12434/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12435/// uses of other values produced by From.getNode() alone. The Deleted
12436/// vector is handled the same way as for ReplaceAllUsesWith.
12438 // Handle the really simple, really trivial case efficiently.
12439 if (From == To) return;
12440
12441 // Handle the simple, trivial, case efficiently.
12442 if (From.getNode()->getNumValues() == 1) {
12443 ReplaceAllUsesWith(From, To);
12444 return;
12445 }
12446
12447 // Preserve Debug Info.
12448 transferDbgValues(From, To);
12449 copyExtraInfo(From.getNode(), To.getNode());
12450
12451 // Iterate over just the existing users of From. See the comments in
12452 // the ReplaceAllUsesWith above.
12453 SDNode::use_iterator UI = From.getNode()->use_begin(),
12454 UE = From.getNode()->use_end();
12455 RAUWUpdateListener Listener(*this, UI, UE);
12456 while (UI != UE) {
12457 SDNode *User = UI->getUser();
12458 bool UserRemovedFromCSEMaps = false;
12459
12460 // A user can appear in a use list multiple times, and when this
12461 // happens the uses are usually next to each other in the list.
12462 // To help reduce the number of CSE recomputations, process all
12463 // the uses of this user that we can find this way.
12464 do {
12465 SDUse &Use = *UI;
12466
12467 // Skip uses of different values from the same node.
12468 if (Use.getResNo() != From.getResNo()) {
12469 ++UI;
12470 continue;
12471 }
12472
12473 // If this node hasn't been modified yet, it's still in the CSE maps,
12474 // so remove its old self from the CSE maps.
12475 if (!UserRemovedFromCSEMaps) {
12476 RemoveNodeFromCSEMaps(User);
12477 UserRemovedFromCSEMaps = true;
12478 }
12479
12480 ++UI;
12481 Use.set(To);
12482 if (To->isDivergent() != From->isDivergent())
12484 } while (UI != UE && UI->getUser() == User);
12485 // We are iterating over all uses of the From node, so if a use
12486 // doesn't use the specific value, no changes are made.
12487 if (!UserRemovedFromCSEMaps)
12488 continue;
12489
12490 // Now that we have modified User, add it back to the CSE maps. If it
12491 // already exists there, recursively merge the results together.
12492 AddModifiedNodeToCSEMaps(User);
12493 }
12494
12495 // If we just RAUW'd the root, take note.
12496 if (From == getRoot())
12497 setRoot(To);
12498}
12499
12500namespace {
12501
12502/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12503/// to record information about a use.
12504struct UseMemo {
12505 SDNode *User;
12506 unsigned Index;
12507 SDUse *Use;
12508};
12509
12510/// operator< - Sort Memos by User.
12511bool operator<(const UseMemo &L, const UseMemo &R) {
12512 return (intptr_t)L.User < (intptr_t)R.User;
12513}
12514
12515/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12516/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12517/// the node already has been taken care of recursively.
12518class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12519 SmallVectorImpl<UseMemo> &Uses;
12520
12521 void NodeDeleted(SDNode *N, SDNode *E) override {
12522 for (UseMemo &Memo : Uses)
12523 if (Memo.User == N)
12524 Memo.User = nullptr;
12525 }
12526
12527public:
12528 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12529 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12530};
12531
12532} // end anonymous namespace
12533
12534/// Return true if a glue output should propagate divergence information.
12536 switch (Node->getOpcode()) {
12537 case ISD::CopyFromReg:
12538 case ISD::CopyToReg:
12539 return false;
12540 default:
12541 return true;
12542 }
12543
12544 llvm_unreachable("covered opcode switch");
12545}
12546
12548 if (TLI->isSDNodeAlwaysUniform(N)) {
12549 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12550 "Conflicting divergence information!");
12551 return false;
12552 }
12553 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12554 return true;
12555 for (const auto &Op : N->ops()) {
12556 EVT VT = Op.getValueType();
12557
12558 // Skip Chain. It does not carry divergence.
12559 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12560 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12561 return true;
12562 }
12563 return false;
12564}
12565
12567 SmallVector<SDNode *, 16> Worklist(1, N);
12568 do {
12569 N = Worklist.pop_back_val();
12570 bool IsDivergent = calculateDivergence(N);
12571 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12572 N->SDNodeBits.IsDivergent = IsDivergent;
12573 llvm::append_range(Worklist, N->users());
12574 }
12575 } while (!Worklist.empty());
12576}
12577
12578void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12580 Order.reserve(AllNodes.size());
12581 for (auto &N : allnodes()) {
12582 unsigned NOps = N.getNumOperands();
12583 Degree[&N] = NOps;
12584 if (0 == NOps)
12585 Order.push_back(&N);
12586 }
12587 for (size_t I = 0; I != Order.size(); ++I) {
12588 SDNode *N = Order[I];
12589 for (auto *U : N->users()) {
12590 unsigned &UnsortedOps = Degree[U];
12591 if (0 == --UnsortedOps)
12592 Order.push_back(U);
12593 }
12594 }
12595}
12596
12597#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12598void SelectionDAG::VerifyDAGDivergence() {
12599 std::vector<SDNode *> TopoOrder;
12600 CreateTopologicalOrder(TopoOrder);
12601 for (auto *N : TopoOrder) {
12602 assert(calculateDivergence(N) == N->isDivergent() &&
12603 "Divergence bit inconsistency detected");
12604 }
12605}
12606#endif
12607
12608/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12609/// uses of other values produced by From.getNode() alone. The same value
12610/// may appear in both the From and To list. The Deleted vector is
12611/// handled the same way as for ReplaceAllUsesWith.
12613 const SDValue *To,
12614 unsigned Num){
12615 // Handle the simple, trivial case efficiently.
12616 if (Num == 1)
12617 return ReplaceAllUsesOfValueWith(*From, *To);
12618
12619 transferDbgValues(*From, *To);
12620 copyExtraInfo(From->getNode(), To->getNode());
12621
12622 // Read up all the uses and make records of them. This helps
12623 // processing new uses that are introduced during the
12624 // replacement process.
12626 for (unsigned i = 0; i != Num; ++i) {
12627 unsigned FromResNo = From[i].getResNo();
12628 SDNode *FromNode = From[i].getNode();
12629 for (SDUse &Use : FromNode->uses()) {
12630 if (Use.getResNo() == FromResNo) {
12631 UseMemo Memo = {Use.getUser(), i, &Use};
12632 Uses.push_back(Memo);
12633 }
12634 }
12635 }
12636
12637 // Sort the uses, so that all the uses from a given User are together.
12639 RAUOVWUpdateListener Listener(*this, Uses);
12640
12641 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12642 UseIndex != UseIndexEnd; ) {
12643 // We know that this user uses some value of From. If it is the right
12644 // value, update it.
12645 SDNode *User = Uses[UseIndex].User;
12646 // If the node has been deleted by recursive CSE updates when updating
12647 // another node, then just skip this entry.
12648 if (User == nullptr) {
12649 ++UseIndex;
12650 continue;
12651 }
12652
12653 // This node is about to morph, remove its old self from the CSE maps.
12654 RemoveNodeFromCSEMaps(User);
12655
12656 // The Uses array is sorted, so all the uses for a given User
12657 // are next to each other in the list.
12658 // To help reduce the number of CSE recomputations, process all
12659 // the uses of this user that we can find this way.
12660 do {
12661 unsigned i = Uses[UseIndex].Index;
12662 SDUse &Use = *Uses[UseIndex].Use;
12663 ++UseIndex;
12664
12665 Use.set(To[i]);
12666 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12667
12668 // Now that we have modified User, add it back to the CSE maps. If it
12669 // already exists there, recursively merge the results together.
12670 AddModifiedNodeToCSEMaps(User);
12671 }
12672}
12673
12674/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12675/// based on their topological order. It returns the maximum id and a vector
12676/// of the SDNodes* in assigned order by reference.
12678 unsigned DAGSize = 0;
12679
12680 // SortedPos tracks the progress of the algorithm. Nodes before it are
12681 // sorted, nodes after it are unsorted. When the algorithm completes
12682 // it is at the end of the list.
12683 allnodes_iterator SortedPos = allnodes_begin();
12684
12685 // Visit all the nodes. Move nodes with no operands to the front of
12686 // the list immediately. Annotate nodes that do have operands with their
12687 // operand count. Before we do this, the Node Id fields of the nodes
12688 // may contain arbitrary values. After, the Node Id fields for nodes
12689 // before SortedPos will contain the topological sort index, and the
12690 // Node Id fields for nodes At SortedPos and after will contain the
12691 // count of outstanding operands.
12693 checkForCycles(&N, this);
12694 unsigned Degree = N.getNumOperands();
12695 if (Degree == 0) {
12696 // A node with no uses, add it to the result array immediately.
12697 N.setNodeId(DAGSize++);
12698 allnodes_iterator Q(&N);
12699 if (Q != SortedPos)
12700 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12701 assert(SortedPos != AllNodes.end() && "Overran node list");
12702 ++SortedPos;
12703 } else {
12704 // Temporarily use the Node Id as scratch space for the degree count.
12705 N.setNodeId(Degree);
12706 }
12707 }
12708
12709 // Visit all the nodes. As we iterate, move nodes into sorted order,
12710 // such that by the time the end is reached all nodes will be sorted.
12711 for (SDNode &Node : allnodes()) {
12712 SDNode *N = &Node;
12713 checkForCycles(N, this);
12714 // N is in sorted position, so all its uses have one less operand
12715 // that needs to be sorted.
12716 for (SDNode *P : N->users()) {
12717 unsigned Degree = P->getNodeId();
12718 assert(Degree != 0 && "Invalid node degree");
12719 --Degree;
12720 if (Degree == 0) {
12721 // All of P's operands are sorted, so P may sorted now.
12722 P->setNodeId(DAGSize++);
12723 if (P->getIterator() != SortedPos)
12724 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12725 assert(SortedPos != AllNodes.end() && "Overran node list");
12726 ++SortedPos;
12727 } else {
12728 // Update P's outstanding operand count.
12729 P->setNodeId(Degree);
12730 }
12731 }
12732 if (Node.getIterator() == SortedPos) {
12733#ifndef NDEBUG
12735 SDNode *S = &*++I;
12736 dbgs() << "Overran sorted position:\n";
12737 S->dumprFull(this); dbgs() << "\n";
12738 dbgs() << "Checking if this is due to cycles\n";
12739 checkForCycles(this, true);
12740#endif
12741 llvm_unreachable(nullptr);
12742 }
12743 }
12744
12745 assert(SortedPos == AllNodes.end() &&
12746 "Topological sort incomplete!");
12747 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12748 "First node in topological sort is not the entry token!");
12749 assert(AllNodes.front().getNodeId() == 0 &&
12750 "First node in topological sort has non-zero id!");
12751 assert(AllNodes.front().getNumOperands() == 0 &&
12752 "First node in topological sort has operands!");
12753 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12754 "Last node in topologic sort has unexpected id!");
12755 assert(AllNodes.back().use_empty() &&
12756 "Last node in topologic sort has users!");
12757 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12758 return DAGSize;
12759}
12760
12762 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12763 SortedNodes.clear();
12764 // Node -> remaining number of outstanding operands.
12765 DenseMap<const SDNode *, unsigned> RemainingOperands;
12766
12767 // Put nodes without any operands into SortedNodes first.
12768 for (const SDNode &N : allnodes()) {
12769 checkForCycles(&N, this);
12770 unsigned NumOperands = N.getNumOperands();
12771 if (NumOperands == 0)
12772 SortedNodes.push_back(&N);
12773 else
12774 // Record their total number of outstanding operands.
12775 RemainingOperands[&N] = NumOperands;
12776 }
12777
12778 // A node is pushed into SortedNodes when all of its operands (predecessors in
12779 // the graph) are also in SortedNodes.
12780 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12781 const SDNode *N = SortedNodes[i];
12782 for (const SDNode *U : N->users()) {
12783 // HandleSDNode is never part of a DAG and therefore has no entry in
12784 // RemainingOperands.
12785 if (U->getOpcode() == ISD::HANDLENODE)
12786 continue;
12787 unsigned &NumRemOperands = RemainingOperands[U];
12788 assert(NumRemOperands && "Invalid number of remaining operands");
12789 --NumRemOperands;
12790 if (!NumRemOperands)
12791 SortedNodes.push_back(U);
12792 }
12793 }
12794
12795 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12796 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12797 "First node in topological sort is not the entry token");
12798 assert(SortedNodes.front()->getNumOperands() == 0 &&
12799 "First node in topological sort has operands");
12800}
12801
12802/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12803/// value is produced by SD.
12804void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12805 for (SDNode *SD : DB->getSDNodes()) {
12806 if (!SD)
12807 continue;
12808 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12809 SD->setHasDebugValue(true);
12810 }
12811 DbgInfo->add(DB, isParameter);
12812}
12813
12814void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12815
12817 SDValue NewMemOpChain) {
12818 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12819 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12820 // The new memory operation must have the same position as the old load in
12821 // terms of memory dependency. Create a TokenFactor for the old load and new
12822 // memory operation and update uses of the old load's output chain to use that
12823 // TokenFactor.
12824 if (OldChain == NewMemOpChain || OldChain.use_empty())
12825 return NewMemOpChain;
12826
12827 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12828 OldChain, NewMemOpChain);
12829 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12830 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12831 return TokenFactor;
12832}
12833
12835 SDValue NewMemOp) {
12836 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12837 SDValue OldChain = SDValue(OldLoad, 1);
12838 SDValue NewMemOpChain = NewMemOp.getValue(1);
12839 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12840}
12841
12843 Function **OutFunction) {
12844 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12845
12846 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12847 auto *Module = MF->getFunction().getParent();
12848 auto *Function = Module->getFunction(Symbol);
12849
12850 if (OutFunction != nullptr)
12851 *OutFunction = Function;
12852
12853 if (Function != nullptr) {
12854 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12855 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12856 }
12857
12858 std::string ErrorStr;
12859 raw_string_ostream ErrorFormatter(ErrorStr);
12860 ErrorFormatter << "Undefined external symbol ";
12861 ErrorFormatter << '"' << Symbol << '"';
12862 report_fatal_error(Twine(ErrorStr));
12863}
12864
12865//===----------------------------------------------------------------------===//
12866// SDNode Class
12867//===----------------------------------------------------------------------===//
12868
12871 return Const != nullptr && Const->isZero();
12872}
12873
12875 return V.isUndef() || isNullConstant(V);
12876}
12877
12880 return Const != nullptr && Const->isZero() && !Const->isNegative();
12881}
12882
12885 return Const != nullptr && Const->isAllOnes();
12886}
12887
12890 return Const != nullptr && Const->isOne();
12891}
12892
12895 return Const != nullptr && Const->isMinSignedValue();
12896}
12897
12898bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12899 unsigned OperandNo) {
12900 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12901 // TODO: Target-specific opcodes could be added.
12902 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12903 /*AllowTruncation*/ true)) {
12904 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12905 switch (Opcode) {
12906 case ISD::ADD:
12907 case ISD::OR:
12908 case ISD::XOR:
12909 case ISD::UMAX:
12910 return Const.isZero();
12911 case ISD::MUL:
12912 return Const.isOne();
12913 case ISD::AND:
12914 case ISD::UMIN:
12915 return Const.isAllOnes();
12916 case ISD::SMAX:
12917 return Const.isMinSignedValue();
12918 case ISD::SMIN:
12919 return Const.isMaxSignedValue();
12920 case ISD::SUB:
12921 case ISD::SHL:
12922 case ISD::SRA:
12923 case ISD::SRL:
12924 return OperandNo == 1 && Const.isZero();
12925 case ISD::UDIV:
12926 case ISD::SDIV:
12927 return OperandNo == 1 && Const.isOne();
12928 }
12929 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12930 switch (Opcode) {
12931 case ISD::FADD:
12932 return ConstFP->isZero() &&
12933 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12934 case ISD::FSUB:
12935 return OperandNo == 1 && ConstFP->isZero() &&
12936 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12937 case ISD::FMUL:
12938 return ConstFP->isExactlyValue(1.0);
12939 case ISD::FDIV:
12940 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12941 case ISD::FMINNUM:
12942 case ISD::FMAXNUM: {
12943 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12944 EVT VT = V.getValueType();
12945 const fltSemantics &Semantics = VT.getFltSemantics();
12946 APFloat NeutralAF = !Flags.hasNoNaNs()
12947 ? APFloat::getQNaN(Semantics)
12948 : !Flags.hasNoInfs()
12949 ? APFloat::getInf(Semantics)
12950 : APFloat::getLargest(Semantics);
12951 if (Opcode == ISD::FMAXNUM)
12952 NeutralAF.changeSign();
12953
12954 return ConstFP->isExactlyValue(NeutralAF);
12955 }
12956 }
12957 }
12958 return false;
12959}
12960
12962 while (V.getOpcode() == ISD::BITCAST)
12963 V = V.getOperand(0);
12964 return V;
12965}
12966
12968 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12969 V = V.getOperand(0);
12970 return V;
12971}
12972
12974 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12975 V = V.getOperand(0);
12976 return V;
12977}
12978
12980 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12981 SDValue InVec = V.getOperand(0);
12982 SDValue EltNo = V.getOperand(2);
12983 EVT VT = InVec.getValueType();
12984 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12985 if (IndexC && VT.isFixedLengthVector() &&
12986 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12987 !DemandedElts[IndexC->getZExtValue()]) {
12988 V = InVec;
12989 continue;
12990 }
12991 break;
12992 }
12993 return V;
12994}
12995
12997 while (V.getOpcode() == ISD::TRUNCATE)
12998 V = V.getOperand(0);
12999 return V;
13000}
13001
13002bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13003 if (V.getOpcode() != ISD::XOR)
13004 return false;
13005 V = peekThroughBitcasts(V.getOperand(1));
13006 unsigned NumBits = V.getScalarValueSizeInBits();
13007 ConstantSDNode *C =
13008 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13009 return C && (C->getAPIntValue().countr_one() >= NumBits);
13010}
13011
13013 bool AllowTruncation) {
13014 EVT VT = N.getValueType();
13015 APInt DemandedElts = VT.isFixedLengthVector()
13017 : APInt(1, 1);
13018 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13019}
13020
13022 bool AllowUndefs,
13023 bool AllowTruncation) {
13025 return CN;
13026
13027 // SplatVectors can truncate their operands. Ignore that case here unless
13028 // AllowTruncation is set.
13029 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13030 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13031 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13032 EVT CVT = CN->getValueType(0);
13033 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13034 if (AllowTruncation || CVT == VecEltVT)
13035 return CN;
13036 }
13037 }
13038
13040 BitVector UndefElements;
13041 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13042
13043 // BuildVectors can truncate their operands. Ignore that case here unless
13044 // AllowTruncation is set.
13045 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13046 if (CN && (UndefElements.none() || AllowUndefs)) {
13047 EVT CVT = CN->getValueType(0);
13048 EVT NSVT = N.getValueType().getScalarType();
13049 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13050 if (AllowTruncation || (CVT == NSVT))
13051 return CN;
13052 }
13053 }
13054
13055 return nullptr;
13056}
13057
13059 EVT VT = N.getValueType();
13060 APInt DemandedElts = VT.isFixedLengthVector()
13062 : APInt(1, 1);
13063 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13064}
13065
13067 const APInt &DemandedElts,
13068 bool AllowUndefs) {
13070 return CN;
13071
13073 BitVector UndefElements;
13074 ConstantFPSDNode *CN =
13075 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13076 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13077 if (CN && (UndefElements.none() || AllowUndefs))
13078 return CN;
13079 }
13080
13081 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13082 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13083 return CN;
13084
13085 return nullptr;
13086}
13087
13088bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13089 // TODO: may want to use peekThroughBitcast() here.
13090 ConstantSDNode *C =
13091 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13092 return C && C->isZero();
13093}
13094
13095bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13096 ConstantSDNode *C =
13097 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13098 return C && C->isOne();
13099}
13100
13101bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13102 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13103 return C && C->isExactlyValue(1.0);
13104}
13105
13106bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13108 unsigned BitWidth = N.getScalarValueSizeInBits();
13109 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13110 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13111}
13112
13113bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13114 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13115 return C && APInt::isSameValue(C->getAPIntValue(),
13116 APInt(C->getAPIntValue().getBitWidth(), 1));
13117}
13118
13119bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13121 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13122 return C && C->isZero();
13123}
13124
13125bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13126 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13127 return C && C->isZero();
13128}
13129
13133
13134MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13135 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13136 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13137 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13138 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13139 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13140 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13141
13142 // We check here that the size of the memory operand fits within the size of
13143 // the MMO. This is because the MMO might indicate only a possible address
13144 // range instead of specifying the affected memory addresses precisely.
13145 assert(
13146 (!MMO->getType().isValid() ||
13147 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13148 "Size mismatch!");
13149}
13150
13151/// Profile - Gather unique data for the node.
13152///
13154 AddNodeIDNode(ID, this);
13155}
13156
13157namespace {
13158
13159 struct EVTArray {
13160 std::vector<EVT> VTs;
13161
13162 EVTArray() {
13163 VTs.reserve(MVT::VALUETYPE_SIZE);
13164 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13165 VTs.push_back(MVT((MVT::SimpleValueType)i));
13166 }
13167 };
13168
13169} // end anonymous namespace
13170
13171/// getValueTypeList - Return a pointer to the specified value type.
13172///
13173const EVT *SDNode::getValueTypeList(MVT VT) {
13174 static EVTArray SimpleVTArray;
13175
13176 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13177 return &SimpleVTArray.VTs[VT.SimpleTy];
13178}
13179
13180/// hasAnyUseOfValue - Return true if there are any use of the indicated
13181/// value. This method ignores uses of other values defined by this operation.
13182bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13183 assert(Value < getNumValues() && "Bad value!");
13184
13185 for (SDUse &U : uses())
13186 if (U.getResNo() == Value)
13187 return true;
13188
13189 return false;
13190}
13191
13192/// isOnlyUserOf - Return true if this node is the only use of N.
13193bool SDNode::isOnlyUserOf(const SDNode *N) const {
13194 bool Seen = false;
13195 for (const SDNode *User : N->users()) {
13196 if (User == this)
13197 Seen = true;
13198 else
13199 return false;
13200 }
13201
13202 return Seen;
13203}
13204
13205/// Return true if the only users of N are contained in Nodes.
13207 bool Seen = false;
13208 for (const SDNode *User : N->users()) {
13209 if (llvm::is_contained(Nodes, User))
13210 Seen = true;
13211 else
13212 return false;
13213 }
13214
13215 return Seen;
13216}
13217
13218/// Return true if the referenced return value is an operand of N.
13219bool SDValue::isOperandOf(const SDNode *N) const {
13220 return is_contained(N->op_values(), *this);
13221}
13222
13223bool SDNode::isOperandOf(const SDNode *N) const {
13224 return any_of(N->op_values(),
13225 [this](SDValue Op) { return this == Op.getNode(); });
13226}
13227
13228/// reachesChainWithoutSideEffects - Return true if this operand (which must
13229/// be a chain) reaches the specified operand without crossing any
13230/// side-effecting instructions on any chain path. In practice, this looks
13231/// through token factors and non-volatile loads. In order to remain efficient,
13232/// this only looks a couple of nodes in, it does not do an exhaustive search.
13233///
13234/// Note that we only need to examine chains when we're searching for
13235/// side-effects; SelectionDAG requires that all side-effects are represented
13236/// by chains, even if another operand would force a specific ordering. This
13237/// constraint is necessary to allow transformations like splitting loads.
13239 unsigned Depth) const {
13240 if (*this == Dest) return true;
13241
13242 // Don't search too deeply, we just want to be able to see through
13243 // TokenFactor's etc.
13244 if (Depth == 0) return false;
13245
13246 // If this is a token factor, all inputs to the TF happen in parallel.
13247 if (getOpcode() == ISD::TokenFactor) {
13248 // First, try a shallow search.
13249 if (is_contained((*this)->ops(), Dest)) {
13250 // We found the chain we want as an operand of this TokenFactor.
13251 // Essentially, we reach the chain without side-effects if we could
13252 // serialize the TokenFactor into a simple chain of operations with
13253 // Dest as the last operation. This is automatically true if the
13254 // chain has one use: there are no other ordering constraints.
13255 // If the chain has more than one use, we give up: some other
13256 // use of Dest might force a side-effect between Dest and the current
13257 // node.
13258 if (Dest.hasOneUse())
13259 return true;
13260 }
13261 // Next, try a deep search: check whether every operand of the TokenFactor
13262 // reaches Dest.
13263 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13264 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13265 });
13266 }
13267
13268 // Loads don't have side effects, look through them.
13269 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13270 if (Ld->isUnordered())
13271 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13272 }
13273 return false;
13274}
13275
13276bool SDNode::hasPredecessor(const SDNode *N) const {
13279 Worklist.push_back(this);
13280 return hasPredecessorHelper(N, Visited, Worklist);
13281}
13282
13284 this->Flags &= Flags;
13285}
13286
13287SDValue
13289 ArrayRef<ISD::NodeType> CandidateBinOps,
13290 bool AllowPartials) {
13291 // The pattern must end in an extract from index 0.
13292 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13293 !isNullConstant(Extract->getOperand(1)))
13294 return SDValue();
13295
13296 // Match against one of the candidate binary ops.
13297 SDValue Op = Extract->getOperand(0);
13298 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13299 return Op.getOpcode() == unsigned(BinOp);
13300 }))
13301 return SDValue();
13302
13303 // Floating-point reductions may require relaxed constraints on the final step
13304 // of the reduction because they may reorder intermediate operations.
13305 unsigned CandidateBinOp = Op.getOpcode();
13306 if (Op.getValueType().isFloatingPoint()) {
13307 SDNodeFlags Flags = Op->getFlags();
13308 switch (CandidateBinOp) {
13309 case ISD::FADD:
13310 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13311 return SDValue();
13312 break;
13313 default:
13314 llvm_unreachable("Unhandled FP opcode for binop reduction");
13315 }
13316 }
13317
13318 // Matching failed - attempt to see if we did enough stages that a partial
13319 // reduction from a subvector is possible.
13320 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13321 if (!AllowPartials || !Op)
13322 return SDValue();
13323 EVT OpVT = Op.getValueType();
13324 EVT OpSVT = OpVT.getScalarType();
13325 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13326 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13327 return SDValue();
13328 BinOp = (ISD::NodeType)CandidateBinOp;
13329 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13330 };
13331
13332 // At each stage, we're looking for something that looks like:
13333 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13334 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13335 // i32 undef, i32 undef, i32 undef, i32 undef>
13336 // %a = binop <8 x i32> %op, %s
13337 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13338 // we expect something like:
13339 // <4,5,6,7,u,u,u,u>
13340 // <2,3,u,u,u,u,u,u>
13341 // <1,u,u,u,u,u,u,u>
13342 // While a partial reduction match would be:
13343 // <2,3,u,u,u,u,u,u>
13344 // <1,u,u,u,u,u,u,u>
13345 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13346 SDValue PrevOp;
13347 for (unsigned i = 0; i < Stages; ++i) {
13348 unsigned MaskEnd = (1 << i);
13349
13350 if (Op.getOpcode() != CandidateBinOp)
13351 return PartialReduction(PrevOp, MaskEnd);
13352
13353 SDValue Op0 = Op.getOperand(0);
13354 SDValue Op1 = Op.getOperand(1);
13355
13357 if (Shuffle) {
13358 Op = Op1;
13359 } else {
13360 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13361 Op = Op0;
13362 }
13363
13364 // The first operand of the shuffle should be the same as the other operand
13365 // of the binop.
13366 if (!Shuffle || Shuffle->getOperand(0) != Op)
13367 return PartialReduction(PrevOp, MaskEnd);
13368
13369 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13370 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13371 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13372 return PartialReduction(PrevOp, MaskEnd);
13373
13374 PrevOp = Op;
13375 }
13376
13377 // Handle subvector reductions, which tend to appear after the shuffle
13378 // reduction stages.
13379 while (Op.getOpcode() == CandidateBinOp) {
13380 unsigned NumElts = Op.getValueType().getVectorNumElements();
13381 SDValue Op0 = Op.getOperand(0);
13382 SDValue Op1 = Op.getOperand(1);
13383 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13385 Op0.getOperand(0) != Op1.getOperand(0))
13386 break;
13387 SDValue Src = Op0.getOperand(0);
13388 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13389 if (NumSrcElts != (2 * NumElts))
13390 break;
13391 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13392 Op1.getConstantOperandAPInt(1) == NumElts) &&
13393 !(Op1.getConstantOperandAPInt(1) == 0 &&
13394 Op0.getConstantOperandAPInt(1) == NumElts))
13395 break;
13396 Op = Src;
13397 }
13398
13399 BinOp = (ISD::NodeType)CandidateBinOp;
13400 return Op;
13401}
13402
13404 EVT VT = N->getValueType(0);
13405 EVT EltVT = VT.getVectorElementType();
13406 unsigned NE = VT.getVectorNumElements();
13407
13408 SDLoc dl(N);
13409
13410 // If ResNE is 0, fully unroll the vector op.
13411 if (ResNE == 0)
13412 ResNE = NE;
13413 else if (NE > ResNE)
13414 NE = ResNE;
13415
13416 if (N->getNumValues() == 2) {
13417 SmallVector<SDValue, 8> Scalars0, Scalars1;
13418 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13419 EVT VT1 = N->getValueType(1);
13420 EVT EltVT1 = VT1.getVectorElementType();
13421
13422 unsigned i;
13423 for (i = 0; i != NE; ++i) {
13424 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13425 SDValue Operand = N->getOperand(j);
13426 EVT OperandVT = Operand.getValueType();
13427
13428 // A vector operand; extract a single element.
13429 EVT OperandEltVT = OperandVT.getVectorElementType();
13430 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13431 }
13432
13433 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13434 Scalars0.push_back(EltOp);
13435 Scalars1.push_back(EltOp.getValue(1));
13436 }
13437
13438 for (; i < ResNE; ++i) {
13439 Scalars0.push_back(getUNDEF(EltVT));
13440 Scalars1.push_back(getUNDEF(EltVT1));
13441 }
13442
13443 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13444 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13445 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13446 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13447 return getMergeValues({Vec0, Vec1}, dl);
13448 }
13449
13450 assert(N->getNumValues() == 1 &&
13451 "Can't unroll a vector with multiple results!");
13452
13454 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13455
13456 unsigned i;
13457 for (i= 0; i != NE; ++i) {
13458 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13459 SDValue Operand = N->getOperand(j);
13460 EVT OperandVT = Operand.getValueType();
13461 if (OperandVT.isVector()) {
13462 // A vector operand; extract a single element.
13463 EVT OperandEltVT = OperandVT.getVectorElementType();
13464 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13465 } else {
13466 // A scalar operand; just use it as is.
13467 Operands[j] = Operand;
13468 }
13469 }
13470
13471 switch (N->getOpcode()) {
13472 default: {
13473 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13474 N->getFlags()));
13475 break;
13476 }
13477 case ISD::VSELECT:
13478 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13479 break;
13480 case ISD::SHL:
13481 case ISD::SRA:
13482 case ISD::SRL:
13483 case ISD::ROTL:
13484 case ISD::ROTR:
13485 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13486 getShiftAmountOperand(Operands[0].getValueType(),
13487 Operands[1])));
13488 break;
13490 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13491 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13492 Operands[0],
13493 getValueType(ExtVT)));
13494 break;
13495 }
13496 case ISD::ADDRSPACECAST: {
13497 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13498 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13499 ASC->getSrcAddressSpace(),
13500 ASC->getDestAddressSpace()));
13501 break;
13502 }
13503 }
13504 }
13505
13506 for (; i < ResNE; ++i)
13507 Scalars.push_back(getUNDEF(EltVT));
13508
13509 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13510 return getBuildVector(VecVT, dl, Scalars);
13511}
13512
13513std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13514 SDNode *N, unsigned ResNE) {
13515 unsigned Opcode = N->getOpcode();
13516 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13517 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13518 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13519 "Expected an overflow opcode");
13520
13521 EVT ResVT = N->getValueType(0);
13522 EVT OvVT = N->getValueType(1);
13523 EVT ResEltVT = ResVT.getVectorElementType();
13524 EVT OvEltVT = OvVT.getVectorElementType();
13525 SDLoc dl(N);
13526
13527 // If ResNE is 0, fully unroll the vector op.
13528 unsigned NE = ResVT.getVectorNumElements();
13529 if (ResNE == 0)
13530 ResNE = NE;
13531 else if (NE > ResNE)
13532 NE = ResNE;
13533
13534 SmallVector<SDValue, 8> LHSScalars;
13535 SmallVector<SDValue, 8> RHSScalars;
13536 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13537 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13538
13539 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13540 SDVTList VTs = getVTList(ResEltVT, SVT);
13541 SmallVector<SDValue, 8> ResScalars;
13542 SmallVector<SDValue, 8> OvScalars;
13543 for (unsigned i = 0; i < NE; ++i) {
13544 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13545 SDValue Ov =
13546 getSelect(dl, OvEltVT, Res.getValue(1),
13547 getBoolConstant(true, dl, OvEltVT, ResVT),
13548 getConstant(0, dl, OvEltVT));
13549
13550 ResScalars.push_back(Res);
13551 OvScalars.push_back(Ov);
13552 }
13553
13554 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13555 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13556
13557 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13558 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13559 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13560 getBuildVector(NewOvVT, dl, OvScalars));
13561}
13562
13565 unsigned Bytes,
13566 int Dist) const {
13567 if (LD->isVolatile() || Base->isVolatile())
13568 return false;
13569 // TODO: probably too restrictive for atomics, revisit
13570 if (!LD->isSimple())
13571 return false;
13572 if (LD->isIndexed() || Base->isIndexed())
13573 return false;
13574 if (LD->getChain() != Base->getChain())
13575 return false;
13576 EVT VT = LD->getMemoryVT();
13577 if (VT.getSizeInBits() / 8 != Bytes)
13578 return false;
13579
13580 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13581 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13582
13583 int64_t Offset = 0;
13584 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13585 return (Dist * (int64_t)Bytes == Offset);
13586 return false;
13587}
13588
13589/// InferPtrAlignment - Infer alignment of a load / store address. Return
13590/// std::nullopt if it cannot be inferred.
13592 // If this is a GlobalAddress + cst, return the alignment.
13593 const GlobalValue *GV = nullptr;
13594 int64_t GVOffset = 0;
13595 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13596 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13597 KnownBits Known(PtrWidth);
13599 unsigned AlignBits = Known.countMinTrailingZeros();
13600 if (AlignBits)
13601 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13602 }
13603
13604 // If this is a direct reference to a stack slot, use information about the
13605 // stack slot's alignment.
13606 int FrameIdx = INT_MIN;
13607 int64_t FrameOffset = 0;
13609 FrameIdx = FI->getIndex();
13610 } else if (isBaseWithConstantOffset(Ptr) &&
13612 // Handle FI+Cst
13613 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13614 FrameOffset = Ptr.getConstantOperandVal(1);
13615 }
13616
13617 if (FrameIdx != INT_MIN) {
13619 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13620 }
13621
13622 return std::nullopt;
13623}
13624
13625/// Split the scalar node with EXTRACT_ELEMENT using the provided
13626/// VTs and return the low/high part.
13627std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13628 const SDLoc &DL,
13629 const EVT &LoVT,
13630 const EVT &HiVT) {
13631 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13632 "Split node must be a scalar type");
13633 SDValue Lo =
13635 SDValue Hi =
13637 return std::make_pair(Lo, Hi);
13638}
13639
13640/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13641/// which is split (or expanded) into two not necessarily identical pieces.
13642std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13643 // Currently all types are split in half.
13644 EVT LoVT, HiVT;
13645 if (!VT.isVector())
13646 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13647 else
13648 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13649
13650 return std::make_pair(LoVT, HiVT);
13651}
13652
13653/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13654/// type, dependent on an enveloping VT that has been split into two identical
13655/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13656std::pair<EVT, EVT>
13658 bool *HiIsEmpty) const {
13659 EVT EltTp = VT.getVectorElementType();
13660 // Examples:
13661 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13662 // custom VL=9 with enveloping VL=8/8 yields 8/1
13663 // custom VL=10 with enveloping VL=8/8 yields 8/2
13664 // etc.
13665 ElementCount VTNumElts = VT.getVectorElementCount();
13666 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13667 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13668 "Mixing fixed width and scalable vectors when enveloping a type");
13669 EVT LoVT, HiVT;
13670 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13671 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13672 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13673 *HiIsEmpty = false;
13674 } else {
13675 // Flag that hi type has zero storage size, but return split envelop type
13676 // (this would be easier if vector types with zero elements were allowed).
13677 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13678 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13679 *HiIsEmpty = true;
13680 }
13681 return std::make_pair(LoVT, HiVT);
13682}
13683
13684/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13685/// low/high part.
13686std::pair<SDValue, SDValue>
13687SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13688 const EVT &HiVT) {
13689 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13690 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13691 "Splitting vector with an invalid mixture of fixed and scalable "
13692 "vector types");
13694 N.getValueType().getVectorMinNumElements() &&
13695 "More vector elements requested than available!");
13696 SDValue Lo, Hi;
13697 Lo = getExtractSubvector(DL, LoVT, N, 0);
13698 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13699 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13700 // IDX with the runtime scaling factor of the result vector type. For
13701 // fixed-width result vectors, that runtime scaling factor is 1.
13704 return std::make_pair(Lo, Hi);
13705}
13706
13707std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13708 const SDLoc &DL) {
13709 // Split the vector length parameter.
13710 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13711 EVT VT = N.getValueType();
13713 "Expecting the mask to be an evenly-sized vector");
13714 SDValue HalfNumElts = getElementCount(
13716 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13717 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13718 return std::make_pair(Lo, Hi);
13719}
13720
13721/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13723 EVT VT = N.getValueType();
13726 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13727}
13728
13731 unsigned Start, unsigned Count,
13732 EVT EltVT) {
13733 EVT VT = Op.getValueType();
13734 if (Count == 0)
13736 if (EltVT == EVT())
13737 EltVT = VT.getVectorElementType();
13738 SDLoc SL(Op);
13739 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13740 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13741 }
13742}
13743
13744// getAddressSpace - Return the address space this GlobalAddress belongs to.
13746 return getGlobal()->getType()->getAddressSpace();
13747}
13748
13751 return Val.MachineCPVal->getType();
13752 return Val.ConstVal->getType();
13753}
13754
13755bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13756 unsigned &SplatBitSize,
13757 bool &HasAnyUndefs,
13758 unsigned MinSplatBits,
13759 bool IsBigEndian) const {
13760 EVT VT = getValueType(0);
13761 assert(VT.isVector() && "Expected a vector type");
13762 unsigned VecWidth = VT.getSizeInBits();
13763 if (MinSplatBits > VecWidth)
13764 return false;
13765
13766 // FIXME: The widths are based on this node's type, but build vectors can
13767 // truncate their operands.
13768 SplatValue = APInt(VecWidth, 0);
13769 SplatUndef = APInt(VecWidth, 0);
13770
13771 // Get the bits. Bits with undefined values (when the corresponding element
13772 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13773 // in SplatValue. If any of the values are not constant, give up and return
13774 // false.
13775 unsigned int NumOps = getNumOperands();
13776 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13777 unsigned EltWidth = VT.getScalarSizeInBits();
13778
13779 for (unsigned j = 0; j < NumOps; ++j) {
13780 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13781 SDValue OpVal = getOperand(i);
13782 unsigned BitPos = j * EltWidth;
13783
13784 if (OpVal.isUndef())
13785 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13786 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13787 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13788 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13789 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13790 else
13791 return false;
13792 }
13793
13794 // The build_vector is all constants or undefs. Find the smallest element
13795 // size that splats the vector.
13796 HasAnyUndefs = (SplatUndef != 0);
13797
13798 // FIXME: This does not work for vectors with elements less than 8 bits.
13799 while (VecWidth > 8) {
13800 // If we can't split in half, stop here.
13801 if (VecWidth & 1)
13802 break;
13803
13804 unsigned HalfSize = VecWidth / 2;
13805 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13806 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13807 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13808 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13809
13810 // If the two halves do not match (ignoring undef bits), stop here.
13811 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13812 MinSplatBits > HalfSize)
13813 break;
13814
13815 SplatValue = HighValue | LowValue;
13816 SplatUndef = HighUndef & LowUndef;
13817
13818 VecWidth = HalfSize;
13819 }
13820
13821 // FIXME: The loop above only tries to split in halves. But if the input
13822 // vector for example is <3 x i16> it wouldn't be able to detect a
13823 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13824 // optimizations. I guess that back in the days when this helper was created
13825 // vectors normally was power-of-2 sized.
13826
13827 SplatBitSize = VecWidth;
13828 return true;
13829}
13830
13832 BitVector *UndefElements) const {
13833 unsigned NumOps = getNumOperands();
13834 if (UndefElements) {
13835 UndefElements->clear();
13836 UndefElements->resize(NumOps);
13837 }
13838 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13839 if (!DemandedElts)
13840 return SDValue();
13841 SDValue Splatted;
13842 for (unsigned i = 0; i != NumOps; ++i) {
13843 if (!DemandedElts[i])
13844 continue;
13845 SDValue Op = getOperand(i);
13846 if (Op.isUndef()) {
13847 if (UndefElements)
13848 (*UndefElements)[i] = true;
13849 } else if (!Splatted) {
13850 Splatted = Op;
13851 } else if (Splatted != Op) {
13852 return SDValue();
13853 }
13854 }
13855
13856 if (!Splatted) {
13857 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13858 assert(getOperand(FirstDemandedIdx).isUndef() &&
13859 "Can only have a splat without a constant for all undefs.");
13860 return getOperand(FirstDemandedIdx);
13861 }
13862
13863 return Splatted;
13864}
13865
13867 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13868 return getSplatValue(DemandedElts, UndefElements);
13869}
13870
13872 SmallVectorImpl<SDValue> &Sequence,
13873 BitVector *UndefElements) const {
13874 unsigned NumOps = getNumOperands();
13875 Sequence.clear();
13876 if (UndefElements) {
13877 UndefElements->clear();
13878 UndefElements->resize(NumOps);
13879 }
13880 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13881 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13882 return false;
13883
13884 // Set the undefs even if we don't find a sequence (like getSplatValue).
13885 if (UndefElements)
13886 for (unsigned I = 0; I != NumOps; ++I)
13887 if (DemandedElts[I] && getOperand(I).isUndef())
13888 (*UndefElements)[I] = true;
13889
13890 // Iteratively widen the sequence length looking for repetitions.
13891 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13892 Sequence.append(SeqLen, SDValue());
13893 for (unsigned I = 0; I != NumOps; ++I) {
13894 if (!DemandedElts[I])
13895 continue;
13896 SDValue &SeqOp = Sequence[I % SeqLen];
13898 if (Op.isUndef()) {
13899 if (!SeqOp)
13900 SeqOp = Op;
13901 continue;
13902 }
13903 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13904 Sequence.clear();
13905 break;
13906 }
13907 SeqOp = Op;
13908 }
13909 if (!Sequence.empty())
13910 return true;
13911 }
13912
13913 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13914 return false;
13915}
13916
13918 BitVector *UndefElements) const {
13919 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13920 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13921}
13922
13925 BitVector *UndefElements) const {
13927 getSplatValue(DemandedElts, UndefElements));
13928}
13929
13932 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13933}
13934
13937 BitVector *UndefElements) const {
13939 getSplatValue(DemandedElts, UndefElements));
13940}
13941
13946
13947int32_t
13949 uint32_t BitWidth) const {
13950 if (ConstantFPSDNode *CN =
13952 bool IsExact;
13953 APSInt IntVal(BitWidth);
13954 const APFloat &APF = CN->getValueAPF();
13955 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13956 APFloat::opOK ||
13957 !IsExact)
13958 return -1;
13959
13960 return IntVal.exactLogBase2();
13961 }
13962 return -1;
13963}
13964
13966 bool IsLittleEndian, unsigned DstEltSizeInBits,
13967 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13968 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13969 if (!isConstant())
13970 return false;
13971
13972 unsigned NumSrcOps = getNumOperands();
13973 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13974 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13975 "Invalid bitcast scale");
13976
13977 // Extract raw src bits.
13978 SmallVector<APInt> SrcBitElements(NumSrcOps,
13979 APInt::getZero(SrcEltSizeInBits));
13980 BitVector SrcUndeElements(NumSrcOps, false);
13981
13982 for (unsigned I = 0; I != NumSrcOps; ++I) {
13984 if (Op.isUndef()) {
13985 SrcUndeElements.set(I);
13986 continue;
13987 }
13988 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13989 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13990 assert((CInt || CFP) && "Unknown constant");
13991 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13992 : CFP->getValueAPF().bitcastToAPInt();
13993 }
13994
13995 // Recast to dst width.
13996 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13997 SrcBitElements, UndefElements, SrcUndeElements);
13998 return true;
13999}
14000
14001void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14002 unsigned DstEltSizeInBits,
14003 SmallVectorImpl<APInt> &DstBitElements,
14004 ArrayRef<APInt> SrcBitElements,
14005 BitVector &DstUndefElements,
14006 const BitVector &SrcUndefElements) {
14007 unsigned NumSrcOps = SrcBitElements.size();
14008 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14009 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14010 "Invalid bitcast scale");
14011 assert(NumSrcOps == SrcUndefElements.size() &&
14012 "Vector size mismatch");
14013
14014 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14015 DstUndefElements.clear();
14016 DstUndefElements.resize(NumDstOps, false);
14017 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14018
14019 // Concatenate src elements constant bits together into dst element.
14020 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14021 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14022 for (unsigned I = 0; I != NumDstOps; ++I) {
14023 DstUndefElements.set(I);
14024 APInt &DstBits = DstBitElements[I];
14025 for (unsigned J = 0; J != Scale; ++J) {
14026 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14027 if (SrcUndefElements[Idx])
14028 continue;
14029 DstUndefElements.reset(I);
14030 const APInt &SrcBits = SrcBitElements[Idx];
14031 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14032 "Illegal constant bitwidths");
14033 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14034 }
14035 }
14036 return;
14037 }
14038
14039 // Split src element constant bits into dst elements.
14040 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14041 for (unsigned I = 0; I != NumSrcOps; ++I) {
14042 if (SrcUndefElements[I]) {
14043 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14044 continue;
14045 }
14046 const APInt &SrcBits = SrcBitElements[I];
14047 for (unsigned J = 0; J != Scale; ++J) {
14048 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14049 APInt &DstBits = DstBitElements[Idx];
14050 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14051 }
14052 }
14053}
14054
14056 for (const SDValue &Op : op_values()) {
14057 unsigned Opc = Op.getOpcode();
14058 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14059 return false;
14060 }
14061 return true;
14062}
14063
14064std::optional<std::pair<APInt, APInt>>
14066 unsigned NumOps = getNumOperands();
14067 if (NumOps < 2)
14068 return std::nullopt;
14069
14072 return std::nullopt;
14073
14074 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14075 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14076 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14077
14078 if (Stride.isZero())
14079 return std::nullopt;
14080
14081 for (unsigned i = 2; i < NumOps; ++i) {
14083 return std::nullopt;
14084
14085 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14086 if (Val != (Start + (Stride * i)))
14087 return std::nullopt;
14088 }
14089
14090 return std::make_pair(Start, Stride);
14091}
14092
14094 // Find the first non-undef value in the shuffle mask.
14095 unsigned i, e;
14096 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14097 /* search */;
14098
14099 // If all elements are undefined, this shuffle can be considered a splat
14100 // (although it should eventually get simplified away completely).
14101 if (i == e)
14102 return true;
14103
14104 // Make sure all remaining elements are either undef or the same as the first
14105 // non-undef value.
14106 for (int Idx = Mask[i]; i != e; ++i)
14107 if (Mask[i] >= 0 && Mask[i] != Idx)
14108 return false;
14109 return true;
14110}
14111
14112// Returns true if it is a constant integer BuildVector or constant integer,
14113// possibly hidden by a bitcast.
14115 SDValue N, bool AllowOpaques) const {
14117
14118 if (auto *C = dyn_cast<ConstantSDNode>(N))
14119 return AllowOpaques || !C->isOpaque();
14120
14122 return true;
14123
14124 // Treat a GlobalAddress supporting constant offset folding as a
14125 // constant integer.
14126 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14127 if (GA->getOpcode() == ISD::GlobalAddress &&
14128 TLI->isOffsetFoldingLegal(GA))
14129 return true;
14130
14131 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14132 isa<ConstantSDNode>(N.getOperand(0)))
14133 return true;
14134 return false;
14135}
14136
14137// Returns true if it is a constant float BuildVector or constant float.
14140 return true;
14141
14143 return true;
14144
14145 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14146 isa<ConstantFPSDNode>(N.getOperand(0)))
14147 return true;
14148
14149 return false;
14150}
14151
14152std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14153 ConstantSDNode *Const =
14154 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14155 if (!Const)
14156 return std::nullopt;
14157
14158 EVT VT = N->getValueType(0);
14159 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14160 switch (TLI->getBooleanContents(N.getValueType())) {
14162 if (CVal.isOne())
14163 return true;
14164 if (CVal.isZero())
14165 return false;
14166 return std::nullopt;
14168 if (CVal.isAllOnes())
14169 return true;
14170 if (CVal.isZero())
14171 return false;
14172 return std::nullopt;
14174 return CVal[0];
14175 }
14176 llvm_unreachable("Unknown BooleanContent enum");
14177}
14178
14179void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14180 assert(!Node->OperandList && "Node already has operands");
14182 "too many operands to fit into SDNode");
14183 SDUse *Ops = OperandRecycler.allocate(
14184 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14185
14186 bool IsDivergent = false;
14187 for (unsigned I = 0; I != Vals.size(); ++I) {
14188 Ops[I].setUser(Node);
14189 Ops[I].setInitial(Vals[I]);
14190 EVT VT = Ops[I].getValueType();
14191
14192 // Skip Chain. It does not carry divergence.
14193 if (VT != MVT::Other &&
14194 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14195 Ops[I].getNode()->isDivergent()) {
14196 IsDivergent = true;
14197 }
14198 }
14199 Node->NumOperands = Vals.size();
14200 Node->OperandList = Ops;
14201 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14202 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14203 Node->SDNodeBits.IsDivergent = IsDivergent;
14204 }
14205 checkForCycles(Node);
14206}
14207
14210 size_t Limit = SDNode::getMaxNumOperands();
14211 while (Vals.size() > Limit) {
14212 unsigned SliceIdx = Vals.size() - Limit;
14213 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14214 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14215 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14216 Vals.emplace_back(NewTF);
14217 }
14218 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14219}
14220
14222 EVT VT, SDNodeFlags Flags) {
14223 switch (Opcode) {
14224 default:
14225 return SDValue();
14226 case ISD::ADD:
14227 case ISD::OR:
14228 case ISD::XOR:
14229 case ISD::UMAX:
14230 return getConstant(0, DL, VT);
14231 case ISD::MUL:
14232 return getConstant(1, DL, VT);
14233 case ISD::AND:
14234 case ISD::UMIN:
14235 return getAllOnesConstant(DL, VT);
14236 case ISD::SMAX:
14238 case ISD::SMIN:
14240 case ISD::FADD:
14241 // If flags allow, prefer positive zero since it's generally cheaper
14242 // to materialize on most targets.
14243 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14244 case ISD::FMUL:
14245 return getConstantFP(1.0, DL, VT);
14246 case ISD::FMINNUM:
14247 case ISD::FMAXNUM: {
14248 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14249 const fltSemantics &Semantics = VT.getFltSemantics();
14250 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14251 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14252 APFloat::getLargest(Semantics);
14253 if (Opcode == ISD::FMAXNUM)
14254 NeutralAF.changeSign();
14255
14256 return getConstantFP(NeutralAF, DL, VT);
14257 }
14258 case ISD::FMINIMUM:
14259 case ISD::FMAXIMUM: {
14260 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14261 const fltSemantics &Semantics = VT.getFltSemantics();
14262 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14263 : APFloat::getLargest(Semantics);
14264 if (Opcode == ISD::FMAXIMUM)
14265 NeutralAF.changeSign();
14266
14267 return getConstantFP(NeutralAF, DL, VT);
14268 }
14269
14270 }
14271}
14272
14273/// Helper used to make a call to a library function that has one argument of
14274/// pointer type.
14275///
14276/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14277/// used to get or set floating-point state. They have one argument of pointer
14278/// type, which points to the memory region containing bits of the
14279/// floating-point state. The value returned by such function is ignored in the
14280/// created call.
14281///
14282/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14283/// \param Ptr Pointer used to save/load state.
14284/// \param InChain Ingoing token chain.
14285/// \returns Outgoing chain token.
14287 SDValue InChain,
14288 const SDLoc &DLoc) {
14289 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14291 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14292 RTLIB::LibcallImpl LibcallImpl =
14293 TLI->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14294 if (LibcallImpl == RTLIB::Unsupported)
14295 reportFatalUsageError("emitting call to unsupported libcall");
14296
14297 SDValue Callee =
14298 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14300 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14301 TLI->getLibcallImplCallingConv(LibcallImpl),
14302 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14303 return TLI->LowerCallTo(CLI).second;
14304}
14305
14307 assert(From && To && "Invalid SDNode; empty source SDValue?");
14308 auto I = SDEI.find(From);
14309 if (I == SDEI.end())
14310 return;
14311
14312 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14313 // the iterator, hence the need to make a copy to prevent a use-after-free.
14314 NodeExtraInfo NEI = I->second;
14315 if (LLVM_LIKELY(!NEI.PCSections)) {
14316 // No deep copy required for the types of extra info set.
14317 //
14318 // FIXME: Investigate if other types of extra info also need deep copy. This
14319 // depends on the types of nodes they can be attached to: if some extra info
14320 // is only ever attached to nodes where a replacement To node is always the
14321 // node where later use and propagation of the extra info has the intended
14322 // semantics, no deep copy is required.
14323 SDEI[To] = std::move(NEI);
14324 return;
14325 }
14326
14327 const SDNode *EntrySDN = getEntryNode().getNode();
14328
14329 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14330 // through the replacement of From with To. Otherwise, replacements of a node
14331 // (From) with more complex nodes (To and its operands) may result in lost
14332 // extra info where the root node (To) is insignificant in further propagating
14333 // and using extra info when further lowering to MIR.
14334 //
14335 // In the first step pre-populate the visited set with the nodes reachable
14336 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14337 // DAG that is not new and should be left untouched.
14338 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14339 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14340 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14341 if (MaxDepth == 0) {
14342 // Remember this node in case we need to increase MaxDepth and continue
14343 // populating FromReach from this node.
14344 Leafs.emplace_back(N);
14345 return;
14346 }
14347 if (!FromReach.insert(N).second)
14348 return;
14349 for (const SDValue &Op : N->op_values())
14350 Self(Self, Op.getNode(), MaxDepth - 1);
14351 };
14352
14353 // Copy extra info to To and all its transitive operands (that are new).
14355 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14356 if (FromReach.contains(N))
14357 return true;
14358 if (!Visited.insert(N).second)
14359 return true;
14360 if (EntrySDN == N)
14361 return false;
14362 for (const SDValue &Op : N->op_values()) {
14363 if (N == To && Op.getNode() == EntrySDN) {
14364 // Special case: New node's operand is the entry node; just need to
14365 // copy extra info to new node.
14366 break;
14367 }
14368 if (!Self(Self, Op.getNode()))
14369 return false;
14370 }
14371 // Copy only if entry node was not reached.
14372 SDEI[N] = NEI;
14373 return true;
14374 };
14375
14376 // We first try with a lower MaxDepth, assuming that the path to common
14377 // operands between From and To is relatively short. This significantly
14378 // improves performance in the common case. The initial MaxDepth is big
14379 // enough to avoid retry in the common case; the last MaxDepth is large
14380 // enough to avoid having to use the fallback below (and protects from
14381 // potential stack exhaustion from recursion).
14382 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14383 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14384 // StartFrom is the previous (or initial) set of leafs reachable at the
14385 // previous maximum depth.
14387 std::swap(StartFrom, Leafs);
14388 for (const SDNode *N : StartFrom)
14389 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14390 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14391 return;
14392 // This should happen very rarely (reached the entry node).
14393 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14394 assert(!Leafs.empty());
14395 }
14396
14397 // This should not happen - but if it did, that means the subgraph reachable
14398 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14399 // could not visit all reachable common operands. Consequently, we were able
14400 // to reach the entry node.
14401 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14402 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14403 // Best-effort fallback if assertions disabled.
14404 SDEI[To] = std::move(NEI);
14405}
14406
14407#ifndef NDEBUG
14408static void checkForCyclesHelper(const SDNode *N,
14411 const llvm::SelectionDAG *DAG) {
14412 // If this node has already been checked, don't check it again.
14413 if (Checked.count(N))
14414 return;
14415
14416 // If a node has already been visited on this depth-first walk, reject it as
14417 // a cycle.
14418 if (!Visited.insert(N).second) {
14419 errs() << "Detected cycle in SelectionDAG\n";
14420 dbgs() << "Offending node:\n";
14421 N->dumprFull(DAG); dbgs() << "\n";
14422 abort();
14423 }
14424
14425 for (const SDValue &Op : N->op_values())
14426 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14427
14428 Checked.insert(N);
14429 Visited.erase(N);
14430}
14431#endif
14432
14434 const llvm::SelectionDAG *DAG,
14435 bool force) {
14436#ifndef NDEBUG
14437 bool check = force;
14438#ifdef EXPENSIVE_CHECKS
14439 check = true;
14440#endif // EXPENSIVE_CHECKS
14441 if (check) {
14442 assert(N && "Checking nonexistent SDNode");
14445 checkForCyclesHelper(N, visited, checked, DAG);
14446 }
14447#endif // !NDEBUG
14448}
14449
14450void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14451 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14452}
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:235
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)