LLVM 22.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <set>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85using namespace llvm::SDPatternMatch;
86
87/// makeVTList - Return an instance of the SDVTList struct initialized with the
88/// specified members.
89static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
90 SDVTList Res = {VTs, NumVTs};
91 return Res;
92}
93
94// Default null implementations of the callbacks.
98
99void SelectionDAG::DAGNodeDeletedListener::anchor() {}
100void SelectionDAG::DAGNodeInsertedListener::anchor() {}
101
102#define DEBUG_TYPE "selectiondag"
103
104static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
105 cl::Hidden, cl::init(true),
106 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
107
108static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
109 cl::desc("Number limit for gluing ld/st of memcpy."),
110 cl::Hidden, cl::init(0));
111
113 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
114 cl::desc("DAG combiner limit number of steps when searching DAG "
115 "for predecessor nodes"));
116
118 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
119}
120
122
123//===----------------------------------------------------------------------===//
124// ConstantFPSDNode Class
125//===----------------------------------------------------------------------===//
126
127/// isExactlyValue - We don't rely on operator== working on double values, as
128/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
129/// As such, this method can be used to do an exact bit-for-bit comparison of
130/// two floating point values.
132 return getValueAPF().bitwiseIsEqual(V);
133}
134
136 const APFloat& Val) {
137 assert(VT.isFloatingPoint() && "Can only convert between FP types");
138
139 // convert modifies in place, so make a copy.
140 APFloat Val2 = APFloat(Val);
141 bool losesInfo;
143 &losesInfo);
144 return !losesInfo;
145}
146
147//===----------------------------------------------------------------------===//
148// ISD Namespace
149//===----------------------------------------------------------------------===//
150
151bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
152 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
153 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
154 unsigned EltSize =
155 N->getValueType(0).getVectorElementType().getSizeInBits();
156 SplatVal = OptAPInt->trunc(EltSize);
157 return true;
158 }
159 }
160
161 auto *BV = dyn_cast<BuildVectorSDNode>(N);
162 if (!BV)
163 return false;
164
165 APInt SplatUndef;
166 unsigned SplatBitSize;
167 bool HasUndefs;
168 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
169 // Endianness does not matter here. We are checking for a splat given the
170 // element size of the vector, and if we find such a splat for little endian
171 // layout, then that should be valid also for big endian (as the full vector
172 // size is known to be a multiple of the element size).
173 const bool IsBigEndian = false;
174 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
175 EltSize, IsBigEndian) &&
176 EltSize == SplatBitSize;
177}
178
179// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
180// specializations of the more general isConstantSplatVector()?
181
182bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
183 // Look through a bit convert.
184 while (N->getOpcode() == ISD::BITCAST)
185 N = N->getOperand(0).getNode();
186
187 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
188 APInt SplatVal;
189 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
190 }
191
192 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
193
194 unsigned i = 0, e = N->getNumOperands();
195
196 // Skip over all of the undef values.
197 while (i != e && N->getOperand(i).isUndef())
198 ++i;
199
200 // Do not accept an all-undef vector.
201 if (i == e) return false;
202
203 // Do not accept build_vectors that aren't all constants or which have non-~0
204 // elements. We have to be a bit careful here, as the type of the constant
205 // may not be the same as the type of the vector elements due to type
206 // legalization (the elements are promoted to a legal type for the target and
207 // a vector of a type may be legal when the base element type is not).
208 // We only want to check enough bits to cover the vector elements, because
209 // we care if the resultant vector is all ones, not whether the individual
210 // constants are.
211 SDValue NotZero = N->getOperand(i);
212 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
213 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
214 if (OptAPInt->countr_one() < EltSize)
215 return false;
216 } else
217 return false;
218
219 // Okay, we have at least one ~0 value, check to see if the rest match or are
220 // undefs. Even with the above element type twiddling, this should be OK, as
221 // the same type legalization should have applied to all the elements.
222 for (++i; i != e; ++i)
223 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
224 return false;
225 return true;
226}
227
228bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
229 // Look through a bit convert.
230 while (N->getOpcode() == ISD::BITCAST)
231 N = N->getOperand(0).getNode();
232
233 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
234 APInt SplatVal;
235 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
236 }
237
238 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
239
240 bool IsAllUndef = true;
241 for (const SDValue &Op : N->op_values()) {
242 if (Op.isUndef())
243 continue;
244 IsAllUndef = false;
245 // Do not accept build_vectors that aren't all constants or which have non-0
246 // elements. We have to be a bit careful here, as the type of the constant
247 // may not be the same as the type of the vector elements due to type
248 // legalization (the elements are promoted to a legal type for the target
249 // and a vector of a type may be legal when the base element type is not).
250 // We only want to check enough bits to cover the vector elements, because
251 // we care if the resultant vector is all zeros, not whether the individual
252 // constants are.
253 if (auto OptAPInt = Op->bitcastToAPInt()) {
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (OptAPInt->countr_zero() < EltSize)
256 return false;
257 } else
258 return false;
259 }
260
261 // Do not accept an all-undef vector.
262 if (IsAllUndef)
263 return false;
264 return true;
265}
266
268 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
269}
270
272 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
273}
274
276 if (N->getOpcode() != ISD::BUILD_VECTOR)
277 return false;
278
279 for (const SDValue &Op : N->op_values()) {
280 if (Op.isUndef())
281 continue;
283 return false;
284 }
285 return true;
286}
287
289 if (N->getOpcode() != ISD::BUILD_VECTOR)
290 return false;
291
292 for (const SDValue &Op : N->op_values()) {
293 if (Op.isUndef())
294 continue;
296 return false;
297 }
298 return true;
299}
300
301bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
302 bool Signed) {
303 assert(N->getValueType(0).isVector() && "Expected a vector!");
304
305 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
306 if (EltSize <= NewEltSize)
307 return false;
308
309 if (N->getOpcode() == ISD::ZERO_EXTEND) {
310 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
311 NewEltSize) &&
312 !Signed;
313 }
314 if (N->getOpcode() == ISD::SIGN_EXTEND) {
315 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
316 NewEltSize) &&
317 Signed;
318 }
319 if (N->getOpcode() != ISD::BUILD_VECTOR)
320 return false;
321
322 for (const SDValue &Op : N->op_values()) {
323 if (Op.isUndef())
324 continue;
326 return false;
327
328 APInt C = Op->getAsAPIntVal().trunc(EltSize);
329 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
330 return false;
331 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
332 return false;
333 }
334
335 return true;
336}
337
339 // Return false if the node has no operands.
340 // This is "logically inconsistent" with the definition of "all" but
341 // is probably the desired behavior.
342 if (N->getNumOperands() == 0)
343 return false;
344 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
345}
346
348 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
349}
350
351template <typename ConstNodeType>
353 std::function<bool(ConstNodeType *)> Match,
354 bool AllowUndefs, bool AllowTruncation) {
355 // FIXME: Add support for scalar UNDEF cases?
356 if (auto *C = dyn_cast<ConstNodeType>(Op))
357 return Match(C);
358
359 // FIXME: Add support for vector UNDEF cases?
360 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
361 ISD::SPLAT_VECTOR != Op.getOpcode())
362 return false;
363
364 EVT SVT = Op.getValueType().getScalarType();
365 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
366 if (AllowUndefs && Op.getOperand(i).isUndef()) {
367 if (!Match(nullptr))
368 return false;
369 continue;
370 }
371
372 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
373 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
374 !Match(Cst))
375 return false;
376 }
377 return true;
378}
379// Build used template types.
381 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
383 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
384
386 SDValue LHS, SDValue RHS,
387 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
388 bool AllowUndefs, bool AllowTypeMismatch) {
389 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
390 return false;
391
392 // TODO: Add support for scalar UNDEF cases?
393 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
394 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
395 return Match(LHSCst, RHSCst);
396
397 // TODO: Add support for vector UNDEF cases?
398 if (LHS.getOpcode() != RHS.getOpcode() ||
399 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
400 LHS.getOpcode() != ISD::SPLAT_VECTOR))
401 return false;
402
403 EVT SVT = LHS.getValueType().getScalarType();
404 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
405 SDValue LHSOp = LHS.getOperand(i);
406 SDValue RHSOp = RHS.getOperand(i);
407 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
408 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
409 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
410 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
411 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
412 return false;
413 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
414 LHSOp.getValueType() != RHSOp.getValueType()))
415 return false;
416 if (!Match(LHSCst, RHSCst))
417 return false;
418 }
419 return true;
420}
421
423 switch (MinMaxOpc) {
424 default:
425 llvm_unreachable("unrecognized opcode");
426 case ISD::UMIN:
427 return ISD::UMAX;
428 case ISD::UMAX:
429 return ISD::UMIN;
430 case ISD::SMIN:
431 return ISD::SMAX;
432 case ISD::SMAX:
433 return ISD::SMIN;
434 }
435}
436
438 switch (VecReduceOpcode) {
439 default:
440 llvm_unreachable("Expected VECREDUCE opcode");
441 case ISD::VECREDUCE_FADD:
442 case ISD::VECREDUCE_SEQ_FADD:
443 case ISD::VP_REDUCE_FADD:
444 case ISD::VP_REDUCE_SEQ_FADD:
445 return ISD::FADD;
446 case ISD::VECREDUCE_FMUL:
447 case ISD::VECREDUCE_SEQ_FMUL:
448 case ISD::VP_REDUCE_FMUL:
449 case ISD::VP_REDUCE_SEQ_FMUL:
450 return ISD::FMUL;
451 case ISD::VECREDUCE_ADD:
452 case ISD::VP_REDUCE_ADD:
453 return ISD::ADD;
454 case ISD::VECREDUCE_MUL:
455 case ISD::VP_REDUCE_MUL:
456 return ISD::MUL;
457 case ISD::VECREDUCE_AND:
458 case ISD::VP_REDUCE_AND:
459 return ISD::AND;
460 case ISD::VECREDUCE_OR:
461 case ISD::VP_REDUCE_OR:
462 return ISD::OR;
463 case ISD::VECREDUCE_XOR:
464 case ISD::VP_REDUCE_XOR:
465 return ISD::XOR;
466 case ISD::VECREDUCE_SMAX:
467 case ISD::VP_REDUCE_SMAX:
468 return ISD::SMAX;
469 case ISD::VECREDUCE_SMIN:
470 case ISD::VP_REDUCE_SMIN:
471 return ISD::SMIN;
472 case ISD::VECREDUCE_UMAX:
473 case ISD::VP_REDUCE_UMAX:
474 return ISD::UMAX;
475 case ISD::VECREDUCE_UMIN:
476 case ISD::VP_REDUCE_UMIN:
477 return ISD::UMIN;
478 case ISD::VECREDUCE_FMAX:
479 case ISD::VP_REDUCE_FMAX:
480 return ISD::FMAXNUM;
481 case ISD::VECREDUCE_FMIN:
482 case ISD::VP_REDUCE_FMIN:
483 return ISD::FMINNUM;
484 case ISD::VECREDUCE_FMAXIMUM:
485 case ISD::VP_REDUCE_FMAXIMUM:
486 return ISD::FMAXIMUM;
487 case ISD::VECREDUCE_FMINIMUM:
488 case ISD::VP_REDUCE_FMINIMUM:
489 return ISD::FMINIMUM;
490 }
491}
492
493bool ISD::isVPOpcode(unsigned Opcode) {
494 switch (Opcode) {
495 default:
496 return false;
497#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
498 case ISD::VPSD: \
499 return true;
500#include "llvm/IR/VPIntrinsics.def"
501 }
502}
503
504bool ISD::isVPBinaryOp(unsigned Opcode) {
505 switch (Opcode) {
506 default:
507 break;
508#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
509#define VP_PROPERTY_BINARYOP return true;
510#define END_REGISTER_VP_SDNODE(VPSD) break;
511#include "llvm/IR/VPIntrinsics.def"
512 }
513 return false;
514}
515
516bool ISD::isVPReduction(unsigned Opcode) {
517 switch (Opcode) {
518 default:
519 return false;
520 case ISD::VP_REDUCE_ADD:
521 case ISD::VP_REDUCE_MUL:
522 case ISD::VP_REDUCE_AND:
523 case ISD::VP_REDUCE_OR:
524 case ISD::VP_REDUCE_XOR:
525 case ISD::VP_REDUCE_SMAX:
526 case ISD::VP_REDUCE_SMIN:
527 case ISD::VP_REDUCE_UMAX:
528 case ISD::VP_REDUCE_UMIN:
529 case ISD::VP_REDUCE_FMAX:
530 case ISD::VP_REDUCE_FMIN:
531 case ISD::VP_REDUCE_FMAXIMUM:
532 case ISD::VP_REDUCE_FMINIMUM:
533 case ISD::VP_REDUCE_FADD:
534 case ISD::VP_REDUCE_FMUL:
535 case ISD::VP_REDUCE_SEQ_FADD:
536 case ISD::VP_REDUCE_SEQ_FMUL:
537 return true;
538 }
539}
540
541/// The operand position of the vector mask.
542std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
543 switch (Opcode) {
544 default:
545 return std::nullopt;
546#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
547 case ISD::VPSD: \
548 return MASKPOS;
549#include "llvm/IR/VPIntrinsics.def"
550 }
551}
552
553/// The operand position of the explicit vector length parameter.
554std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
555 switch (Opcode) {
556 default:
557 return std::nullopt;
558#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
559 case ISD::VPSD: \
560 return EVLPOS;
561#include "llvm/IR/VPIntrinsics.def"
562 }
563}
564
565std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
566 bool hasFPExcept) {
567 // FIXME: Return strict opcodes in case of fp exceptions.
568 switch (VPOpcode) {
569 default:
570 return std::nullopt;
571#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
572#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
573#define END_REGISTER_VP_SDNODE(VPOPC) break;
574#include "llvm/IR/VPIntrinsics.def"
575 }
576 return std::nullopt;
577}
578
579std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
580 switch (Opcode) {
581 default:
582 return std::nullopt;
583#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
584#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
585#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
586#include "llvm/IR/VPIntrinsics.def"
587 }
588}
589
591 switch (ExtType) {
592 case ISD::EXTLOAD:
593 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
594 case ISD::SEXTLOAD:
595 return ISD::SIGN_EXTEND;
596 case ISD::ZEXTLOAD:
597 return ISD::ZERO_EXTEND;
598 default:
599 break;
600 }
601
602 llvm_unreachable("Invalid LoadExtType");
603}
604
606 // To perform this operation, we just need to swap the L and G bits of the
607 // operation.
608 unsigned OldL = (Operation >> 2) & 1;
609 unsigned OldG = (Operation >> 1) & 1;
610 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
611 (OldL << 1) | // New G bit
612 (OldG << 2)); // New L bit.
613}
614
616 unsigned Operation = Op;
617 if (isIntegerLike)
618 Operation ^= 7; // Flip L, G, E bits, but not U.
619 else
620 Operation ^= 15; // Flip all of the condition bits.
621
623 Operation &= ~8; // Don't let N and U bits get set.
624
625 return ISD::CondCode(Operation);
626}
627
631
633 bool isIntegerLike) {
634 return getSetCCInverseImpl(Op, isIntegerLike);
635}
636
637/// For an integer comparison, return 1 if the comparison is a signed operation
638/// and 2 if the result is an unsigned comparison. Return zero if the operation
639/// does not depend on the sign of the input (setne and seteq).
640static int isSignedOp(ISD::CondCode Opcode) {
641 switch (Opcode) {
642 default: llvm_unreachable("Illegal integer setcc operation!");
643 case ISD::SETEQ:
644 case ISD::SETNE: return 0;
645 case ISD::SETLT:
646 case ISD::SETLE:
647 case ISD::SETGT:
648 case ISD::SETGE: return 1;
649 case ISD::SETULT:
650 case ISD::SETULE:
651 case ISD::SETUGT:
652 case ISD::SETUGE: return 2;
653 }
654}
655
657 EVT Type) {
658 bool IsInteger = Type.isInteger();
659 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
660 // Cannot fold a signed integer setcc with an unsigned integer setcc.
661 return ISD::SETCC_INVALID;
662
663 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
664
665 // If the N and U bits get set, then the resultant comparison DOES suddenly
666 // care about orderedness, and it is true when ordered.
667 if (Op > ISD::SETTRUE2)
668 Op &= ~16; // Clear the U bit if the N bit is set.
669
670 // Canonicalize illegal integer setcc's.
671 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
672 Op = ISD::SETNE;
673
674 return ISD::CondCode(Op);
675}
676
678 EVT Type) {
679 bool IsInteger = Type.isInteger();
680 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
681 // Cannot fold a signed setcc with an unsigned setcc.
682 return ISD::SETCC_INVALID;
683
684 // Combine all of the condition bits.
685 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
686
687 // Canonicalize illegal integer setcc's.
688 if (IsInteger) {
689 switch (Result) {
690 default: break;
691 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
692 case ISD::SETOEQ: // SETEQ & SETU[LG]E
693 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
694 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
695 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
696 }
697 }
698
699 return Result;
700}
701
702//===----------------------------------------------------------------------===//
703// SDNode Profile Support
704//===----------------------------------------------------------------------===//
705
706/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
707static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
708 ID.AddInteger(OpC);
709}
710
711/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
712/// solely with their pointer.
714 ID.AddPointer(VTList.VTs);
715}
716
717/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
720 for (const auto &Op : Ops) {
721 ID.AddPointer(Op.getNode());
722 ID.AddInteger(Op.getResNo());
723 }
724}
725
726/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
729 for (const auto &Op : Ops) {
730 ID.AddPointer(Op.getNode());
731 ID.AddInteger(Op.getResNo());
732 }
733}
734
735static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
736 SDVTList VTList, ArrayRef<SDValue> OpList) {
737 AddNodeIDOpcode(ID, OpC);
738 AddNodeIDValueTypes(ID, VTList);
739 AddNodeIDOperands(ID, OpList);
740}
741
742/// If this is an SDNode with special info, add this info to the NodeID data.
744 switch (N->getOpcode()) {
747 case ISD::MCSymbol:
748 llvm_unreachable("Should only be used on nodes with operands");
749 default: break; // Normal nodes don't need extra info.
751 case ISD::Constant: {
753 ID.AddPointer(C->getConstantIntValue());
754 ID.AddBoolean(C->isOpaque());
755 break;
756 }
758 case ISD::ConstantFP:
759 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
760 break;
766 ID.AddPointer(GA->getGlobal());
767 ID.AddInteger(GA->getOffset());
768 ID.AddInteger(GA->getTargetFlags());
769 break;
770 }
771 case ISD::BasicBlock:
773 break;
774 case ISD::Register:
775 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
776 break;
778 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
779 break;
780 case ISD::SRCVALUE:
781 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
782 break;
783 case ISD::FrameIndex:
785 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
786 break;
787 case ISD::PSEUDO_PROBE:
788 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
789 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
790 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
791 break;
792 case ISD::JumpTable:
794 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
795 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
796 break;
800 ID.AddInteger(CP->getAlign().value());
801 ID.AddInteger(CP->getOffset());
802 if (CP->isMachineConstantPoolEntry())
803 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
804 else
805 ID.AddPointer(CP->getConstVal());
806 ID.AddInteger(CP->getTargetFlags());
807 break;
808 }
809 case ISD::TargetIndex: {
811 ID.AddInteger(TI->getIndex());
812 ID.AddInteger(TI->getOffset());
813 ID.AddInteger(TI->getTargetFlags());
814 break;
815 }
816 case ISD::LOAD: {
817 const LoadSDNode *LD = cast<LoadSDNode>(N);
818 ID.AddInteger(LD->getMemoryVT().getRawBits());
819 ID.AddInteger(LD->getRawSubclassData());
820 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
821 ID.AddInteger(LD->getMemOperand()->getFlags());
822 break;
823 }
824 case ISD::STORE: {
825 const StoreSDNode *ST = cast<StoreSDNode>(N);
826 ID.AddInteger(ST->getMemoryVT().getRawBits());
827 ID.AddInteger(ST->getRawSubclassData());
828 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
829 ID.AddInteger(ST->getMemOperand()->getFlags());
830 break;
831 }
832 case ISD::VP_LOAD: {
833 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
834 ID.AddInteger(ELD->getMemoryVT().getRawBits());
835 ID.AddInteger(ELD->getRawSubclassData());
836 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
837 ID.AddInteger(ELD->getMemOperand()->getFlags());
838 break;
839 }
840 case ISD::VP_LOAD_FF: {
841 const auto *LD = cast<VPLoadFFSDNode>(N);
842 ID.AddInteger(LD->getMemoryVT().getRawBits());
843 ID.AddInteger(LD->getRawSubclassData());
844 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
845 ID.AddInteger(LD->getMemOperand()->getFlags());
846 break;
847 }
848 case ISD::VP_STORE: {
849 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
850 ID.AddInteger(EST->getMemoryVT().getRawBits());
851 ID.AddInteger(EST->getRawSubclassData());
852 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
853 ID.AddInteger(EST->getMemOperand()->getFlags());
854 break;
855 }
856 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
858 ID.AddInteger(SLD->getMemoryVT().getRawBits());
859 ID.AddInteger(SLD->getRawSubclassData());
860 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
861 break;
862 }
863 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
865 ID.AddInteger(SST->getMemoryVT().getRawBits());
866 ID.AddInteger(SST->getRawSubclassData());
867 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
868 break;
869 }
870 case ISD::VP_GATHER: {
872 ID.AddInteger(EG->getMemoryVT().getRawBits());
873 ID.AddInteger(EG->getRawSubclassData());
874 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
875 ID.AddInteger(EG->getMemOperand()->getFlags());
876 break;
877 }
878 case ISD::VP_SCATTER: {
880 ID.AddInteger(ES->getMemoryVT().getRawBits());
881 ID.AddInteger(ES->getRawSubclassData());
882 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
883 ID.AddInteger(ES->getMemOperand()->getFlags());
884 break;
885 }
886 case ISD::MLOAD: {
888 ID.AddInteger(MLD->getMemoryVT().getRawBits());
889 ID.AddInteger(MLD->getRawSubclassData());
890 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
891 ID.AddInteger(MLD->getMemOperand()->getFlags());
892 break;
893 }
894 case ISD::MSTORE: {
896 ID.AddInteger(MST->getMemoryVT().getRawBits());
897 ID.AddInteger(MST->getRawSubclassData());
898 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
899 ID.AddInteger(MST->getMemOperand()->getFlags());
900 break;
901 }
902 case ISD::MGATHER: {
904 ID.AddInteger(MG->getMemoryVT().getRawBits());
905 ID.AddInteger(MG->getRawSubclassData());
906 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
907 ID.AddInteger(MG->getMemOperand()->getFlags());
908 break;
909 }
910 case ISD::MSCATTER: {
912 ID.AddInteger(MS->getMemoryVT().getRawBits());
913 ID.AddInteger(MS->getRawSubclassData());
914 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
915 ID.AddInteger(MS->getMemOperand()->getFlags());
916 break;
917 }
918 case ISD::ATOMIC_CMP_SWAP:
919 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
920 case ISD::ATOMIC_SWAP:
921 case ISD::ATOMIC_LOAD_ADD:
922 case ISD::ATOMIC_LOAD_SUB:
923 case ISD::ATOMIC_LOAD_AND:
924 case ISD::ATOMIC_LOAD_CLR:
925 case ISD::ATOMIC_LOAD_OR:
926 case ISD::ATOMIC_LOAD_XOR:
927 case ISD::ATOMIC_LOAD_NAND:
928 case ISD::ATOMIC_LOAD_MIN:
929 case ISD::ATOMIC_LOAD_MAX:
930 case ISD::ATOMIC_LOAD_UMIN:
931 case ISD::ATOMIC_LOAD_UMAX:
932 case ISD::ATOMIC_LOAD:
933 case ISD::ATOMIC_STORE: {
934 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
935 ID.AddInteger(AT->getMemoryVT().getRawBits());
936 ID.AddInteger(AT->getRawSubclassData());
937 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
938 ID.AddInteger(AT->getMemOperand()->getFlags());
939 break;
940 }
941 case ISD::VECTOR_SHUFFLE: {
942 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
943 for (int M : Mask)
944 ID.AddInteger(M);
945 break;
946 }
947 case ISD::ADDRSPACECAST: {
949 ID.AddInteger(ASC->getSrcAddressSpace());
950 ID.AddInteger(ASC->getDestAddressSpace());
951 break;
952 }
954 case ISD::BlockAddress: {
956 ID.AddPointer(BA->getBlockAddress());
957 ID.AddInteger(BA->getOffset());
958 ID.AddInteger(BA->getTargetFlags());
959 break;
960 }
961 case ISD::AssertAlign:
962 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
963 break;
964 case ISD::PREFETCH:
967 // Handled by MemIntrinsicSDNode check after the switch.
968 break;
969 case ISD::MDNODE_SDNODE:
970 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
971 break;
972 } // end switch (N->getOpcode())
973
974 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
975 // to check.
976 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
977 ID.AddInteger(MN->getRawSubclassData());
978 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
979 ID.AddInteger(MN->getMemOperand()->getFlags());
980 ID.AddInteger(MN->getMemoryVT().getRawBits());
981 }
982}
983
984/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
985/// data.
986static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
987 AddNodeIDOpcode(ID, N->getOpcode());
988 // Add the return value info.
989 AddNodeIDValueTypes(ID, N->getVTList());
990 // Add the operand info.
991 AddNodeIDOperands(ID, N->ops());
992
993 // Handle SDNode leafs with special info.
995}
996
997//===----------------------------------------------------------------------===//
998// SelectionDAG Class
999//===----------------------------------------------------------------------===//
1000
1001/// doNotCSE - Return true if CSE should not be performed for this node.
1002static bool doNotCSE(SDNode *N) {
1003 if (N->getValueType(0) == MVT::Glue)
1004 return true; // Never CSE anything that produces a glue result.
1005
1006 switch (N->getOpcode()) {
1007 default: break;
1008 case ISD::HANDLENODE:
1009 case ISD::EH_LABEL:
1010 return true; // Never CSE these nodes.
1011 }
1012
1013 // Check that remaining values produced are not flags.
1014 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1015 if (N->getValueType(i) == MVT::Glue)
1016 return true; // Never CSE anything that produces a glue result.
1017
1018 return false;
1019}
1020
1021/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1022/// SelectionDAG.
1024 // Create a dummy node (which is not added to allnodes), that adds a reference
1025 // to the root node, preventing it from being deleted.
1026 HandleSDNode Dummy(getRoot());
1027
1028 SmallVector<SDNode*, 128> DeadNodes;
1029
1030 // Add all obviously-dead nodes to the DeadNodes worklist.
1031 for (SDNode &Node : allnodes())
1032 if (Node.use_empty())
1033 DeadNodes.push_back(&Node);
1034
1035 RemoveDeadNodes(DeadNodes);
1036
1037 // If the root changed (e.g. it was a dead load, update the root).
1038 setRoot(Dummy.getValue());
1039}
1040
1041/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1042/// given list, and any nodes that become unreachable as a result.
1044
1045 // Process the worklist, deleting the nodes and adding their uses to the
1046 // worklist.
1047 while (!DeadNodes.empty()) {
1048 SDNode *N = DeadNodes.pop_back_val();
1049 // Skip to next node if we've already managed to delete the node. This could
1050 // happen if replacing a node causes a node previously added to the node to
1051 // be deleted.
1052 if (N->getOpcode() == ISD::DELETED_NODE)
1053 continue;
1054
1055 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1056 DUL->NodeDeleted(N, nullptr);
1057
1058 // Take the node out of the appropriate CSE map.
1059 RemoveNodeFromCSEMaps(N);
1060
1061 // Next, brutally remove the operand list. This is safe to do, as there are
1062 // no cycles in the graph.
1063 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1064 SDUse &Use = *I++;
1065 SDNode *Operand = Use.getNode();
1066 Use.set(SDValue());
1067
1068 // Now that we removed this operand, see if there are no uses of it left.
1069 if (Operand->use_empty())
1070 DeadNodes.push_back(Operand);
1071 }
1072
1073 DeallocateNode(N);
1074 }
1075}
1076
1078 SmallVector<SDNode*, 16> DeadNodes(1, N);
1079
1080 // Create a dummy node that adds a reference to the root node, preventing
1081 // it from being deleted. (This matters if the root is an operand of the
1082 // dead node.)
1083 HandleSDNode Dummy(getRoot());
1084
1085 RemoveDeadNodes(DeadNodes);
1086}
1087
1089 // First take this out of the appropriate CSE map.
1090 RemoveNodeFromCSEMaps(N);
1091
1092 // Finally, remove uses due to operands of this node, remove from the
1093 // AllNodes list, and delete the node.
1094 DeleteNodeNotInCSEMaps(N);
1095}
1096
1097void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1098 assert(N->getIterator() != AllNodes.begin() &&
1099 "Cannot delete the entry node!");
1100 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1101
1102 // Drop all of the operands and decrement used node's use counts.
1103 N->DropOperands();
1104
1105 DeallocateNode(N);
1106}
1107
1108void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1109 assert(!(V->isVariadic() && isParameter));
1110 if (isParameter)
1111 ByvalParmDbgValues.push_back(V);
1112 else
1113 DbgValues.push_back(V);
1114 for (const SDNode *Node : V->getSDNodes())
1115 if (Node)
1116 DbgValMap[Node].push_back(V);
1117}
1118
1120 DbgValMapType::iterator I = DbgValMap.find(Node);
1121 if (I == DbgValMap.end())
1122 return;
1123 for (auto &Val: I->second)
1124 Val->setIsInvalidated();
1125 DbgValMap.erase(I);
1126}
1127
1128void SelectionDAG::DeallocateNode(SDNode *N) {
1129 // If we have operands, deallocate them.
1131
1132 NodeAllocator.Deallocate(AllNodes.remove(N));
1133
1134 // Set the opcode to DELETED_NODE to help catch bugs when node
1135 // memory is reallocated.
1136 // FIXME: There are places in SDag that have grown a dependency on the opcode
1137 // value in the released node.
1138 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1139 N->NodeType = ISD::DELETED_NODE;
1140
1141 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1142 // them and forget about that node.
1143 DbgInfo->erase(N);
1144
1145 // Invalidate extra info.
1146 SDEI.erase(N);
1147}
1148
1149#ifndef NDEBUG
1150/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1151void SelectionDAG::verifyNode(SDNode *N) const {
1152 switch (N->getOpcode()) {
1153 default:
1154 if (N->isTargetOpcode())
1156 break;
1157 case ISD::BUILD_PAIR: {
1158 EVT VT = N->getValueType(0);
1159 assert(N->getNumValues() == 1 && "Too many results!");
1160 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1161 "Wrong return type!");
1162 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1163 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1164 "Mismatched operand types!");
1165 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1166 "Wrong operand type!");
1167 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1168 "Wrong return type size");
1169 break;
1170 }
1171 case ISD::BUILD_VECTOR: {
1172 assert(N->getNumValues() == 1 && "Too many results!");
1173 assert(N->getValueType(0).isVector() && "Wrong return type!");
1174 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1175 "Wrong number of operands!");
1176 EVT EltVT = N->getValueType(0).getVectorElementType();
1177 for (const SDUse &Op : N->ops()) {
1178 assert((Op.getValueType() == EltVT ||
1179 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1180 EltVT.bitsLE(Op.getValueType()))) &&
1181 "Wrong operand type!");
1182 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1183 "Operands must all have the same type");
1184 }
1185 break;
1186 }
1187 }
1188}
1189#endif // NDEBUG
1190
1191/// Insert a newly allocated node into the DAG.
1192///
1193/// Handles insertion into the all nodes list and CSE map, as well as
1194/// verification and other common operations when a new node is allocated.
1195void SelectionDAG::InsertNode(SDNode *N) {
1196 AllNodes.push_back(N);
1197#ifndef NDEBUG
1198 N->PersistentId = NextPersistentId++;
1199 verifyNode(N);
1200#endif
1201 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1202 DUL->NodeInserted(N);
1203}
1204
1205/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1206/// correspond to it. This is useful when we're about to delete or repurpose
1207/// the node. We don't want future request for structurally identical nodes
1208/// to return N anymore.
1209bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1210 bool Erased = false;
1211 switch (N->getOpcode()) {
1212 case ISD::HANDLENODE: return false; // noop.
1213 case ISD::CONDCODE:
1214 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1215 "Cond code doesn't exist!");
1216 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1217 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1218 break;
1220 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1221 break;
1223 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1224 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1225 ESN->getSymbol(), ESN->getTargetFlags()));
1226 break;
1227 }
1228 case ISD::MCSymbol: {
1229 auto *MCSN = cast<MCSymbolSDNode>(N);
1230 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1231 break;
1232 }
1233 case ISD::VALUETYPE: {
1234 EVT VT = cast<VTSDNode>(N)->getVT();
1235 if (VT.isExtended()) {
1236 Erased = ExtendedValueTypeNodes.erase(VT);
1237 } else {
1238 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1239 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1240 }
1241 break;
1242 }
1243 default:
1244 // Remove it from the CSE Map.
1245 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1246 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1247 Erased = CSEMap.RemoveNode(N);
1248 break;
1249 }
1250#ifndef NDEBUG
1251 // Verify that the node was actually in one of the CSE maps, unless it has a
1252 // glue result (which cannot be CSE'd) or is one of the special cases that are
1253 // not subject to CSE.
1254 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1255 !N->isMachineOpcode() && !doNotCSE(N)) {
1256 N->dump(this);
1257 dbgs() << "\n";
1258 llvm_unreachable("Node is not in map!");
1259 }
1260#endif
1261 return Erased;
1262}
1263
1264/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1265/// maps and modified in place. Add it back to the CSE maps, unless an identical
1266/// node already exists, in which case transfer all its users to the existing
1267/// node. This transfer can potentially trigger recursive merging.
1268void
1269SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1270 // For node types that aren't CSE'd, just act as if no identical node
1271 // already exists.
1272 if (!doNotCSE(N)) {
1273 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1274 if (Existing != N) {
1275 // If there was already an existing matching node, use ReplaceAllUsesWith
1276 // to replace the dead one with the existing one. This can cause
1277 // recursive merging of other unrelated nodes down the line.
1278 Existing->intersectFlagsWith(N->getFlags());
1279 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1280 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1281 ReplaceAllUsesWith(N, Existing);
1282
1283 // N is now dead. Inform the listeners and delete it.
1284 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1285 DUL->NodeDeleted(N, Existing);
1286 DeleteNodeNotInCSEMaps(N);
1287 return;
1288 }
1289 }
1290
1291 // If the node doesn't already exist, we updated it. Inform listeners.
1292 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1293 DUL->NodeUpdated(N);
1294}
1295
1296/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1297/// were replaced with those specified. If this node is never memoized,
1298/// return null, otherwise return a pointer to the slot it would take. If a
1299/// node already exists with these operands, the slot will be non-null.
1300SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1301 void *&InsertPos) {
1302 if (doNotCSE(N))
1303 return nullptr;
1304
1305 SDValue Ops[] = { Op };
1306 FoldingSetNodeID ID;
1307 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1309 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1310 if (Node)
1311 Node->intersectFlagsWith(N->getFlags());
1312 return Node;
1313}
1314
1315/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1316/// were replaced with those specified. If this node is never memoized,
1317/// return null, otherwise return a pointer to the slot it would take. If a
1318/// node already exists with these operands, the slot will be non-null.
1319SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1320 SDValue Op1, SDValue Op2,
1321 void *&InsertPos) {
1322 if (doNotCSE(N))
1323 return nullptr;
1324
1325 SDValue Ops[] = { Op1, Op2 };
1326 FoldingSetNodeID ID;
1327 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1329 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1330 if (Node)
1331 Node->intersectFlagsWith(N->getFlags());
1332 return Node;
1333}
1334
1335/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1336/// were replaced with those specified. If this node is never memoized,
1337/// return null, otherwise return a pointer to the slot it would take. If a
1338/// node already exists with these operands, the slot will be non-null.
1339SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1340 void *&InsertPos) {
1341 if (doNotCSE(N))
1342 return nullptr;
1343
1344 FoldingSetNodeID ID;
1345 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1347 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1348 if (Node)
1349 Node->intersectFlagsWith(N->getFlags());
1350 return Node;
1351}
1352
1354 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1355 : VT.getTypeForEVT(*getContext());
1356
1357 return getDataLayout().getABITypeAlign(Ty);
1358}
1359
1360// EntryNode could meaningfully have debug info if we can find it...
1362 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1363 getVTList(MVT::Other, MVT::Glue)),
1364 Root(getEntryNode()) {
1365 InsertNode(&EntryNode);
1366 DbgInfo = new SDDbgInfo();
1367}
1368
1370 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1371 const TargetLibraryInfo *LibraryInfo,
1372 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1374 FunctionVarLocs const *VarLocs) {
1375 MF = &NewMF;
1376 SDAGISelPass = PassPtr;
1377 ORE = &NewORE;
1380 LibInfo = LibraryInfo;
1381 Context = &MF->getFunction().getContext();
1382 UA = NewUA;
1383 PSI = PSIin;
1384 BFI = BFIin;
1385 MMI = &MMIin;
1386 FnVarLocs = VarLocs;
1387}
1388
1390 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1391 allnodes_clear();
1392 OperandRecycler.clear(OperandAllocator);
1393 delete DbgInfo;
1394}
1395
1397 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1398}
1399
1400void SelectionDAG::allnodes_clear() {
1401 assert(&*AllNodes.begin() == &EntryNode);
1402 AllNodes.remove(AllNodes.begin());
1403 while (!AllNodes.empty())
1404 DeallocateNode(&AllNodes.front());
1405#ifndef NDEBUG
1406 NextPersistentId = 0;
1407#endif
1408}
1409
1410SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1411 void *&InsertPos) {
1412 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1413 if (N) {
1414 switch (N->getOpcode()) {
1415 default: break;
1416 case ISD::Constant:
1417 case ISD::ConstantFP:
1418 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1419 "debug location. Use another overload.");
1420 }
1421 }
1422 return N;
1423}
1424
1425SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1426 const SDLoc &DL, void *&InsertPos) {
1427 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1428 if (N) {
1429 switch (N->getOpcode()) {
1430 case ISD::Constant:
1431 case ISD::ConstantFP:
1432 // Erase debug location from the node if the node is used at several
1433 // different places. Do not propagate one location to all uses as it
1434 // will cause a worse single stepping debugging experience.
1435 if (N->getDebugLoc() != DL.getDebugLoc())
1436 N->setDebugLoc(DebugLoc());
1437 break;
1438 default:
1439 // When the node's point of use is located earlier in the instruction
1440 // sequence than its prior point of use, update its debug info to the
1441 // earlier location.
1442 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1443 N->setDebugLoc(DL.getDebugLoc());
1444 break;
1445 }
1446 }
1447 return N;
1448}
1449
1451 allnodes_clear();
1452 OperandRecycler.clear(OperandAllocator);
1453 OperandAllocator.Reset();
1454 CSEMap.clear();
1455
1456 ExtendedValueTypeNodes.clear();
1457 ExternalSymbols.clear();
1458 TargetExternalSymbols.clear();
1459 MCSymbols.clear();
1460 SDEI.clear();
1461 llvm::fill(CondCodeNodes, nullptr);
1462 llvm::fill(ValueTypeNodes, nullptr);
1463
1464 EntryNode.UseList = nullptr;
1465 InsertNode(&EntryNode);
1466 Root = getEntryNode();
1467 DbgInfo->clear();
1468}
1469
1471 return VT.bitsGT(Op.getValueType())
1472 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1473 : getNode(ISD::FP_ROUND, DL, VT, Op,
1474 getIntPtrConstant(0, DL, /*isTarget=*/true));
1475}
1476
1477std::pair<SDValue, SDValue>
1479 const SDLoc &DL, EVT VT) {
1480 assert(!VT.bitsEq(Op.getValueType()) &&
1481 "Strict no-op FP extend/round not allowed.");
1482 SDValue Res =
1483 VT.bitsGT(Op.getValueType())
1484 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1485 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1486 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1487
1488 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1489}
1490
1492 return VT.bitsGT(Op.getValueType()) ?
1493 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1494 getNode(ISD::TRUNCATE, DL, VT, Op);
1495}
1496
1498 return VT.bitsGT(Op.getValueType()) ?
1499 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1500 getNode(ISD::TRUNCATE, DL, VT, Op);
1501}
1502
1504 return VT.bitsGT(Op.getValueType()) ?
1505 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1506 getNode(ISD::TRUNCATE, DL, VT, Op);
1507}
1508
1510 EVT VT) {
1511 assert(!VT.isVector());
1512 auto Type = Op.getValueType();
1513 SDValue DestOp;
1514 if (Type == VT)
1515 return Op;
1516 auto Size = Op.getValueSizeInBits();
1517 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1518 if (DestOp.getValueType() == VT)
1519 return DestOp;
1520
1521 return getAnyExtOrTrunc(DestOp, DL, VT);
1522}
1523
1525 EVT VT) {
1526 assert(!VT.isVector());
1527 auto Type = Op.getValueType();
1528 SDValue DestOp;
1529 if (Type == VT)
1530 return Op;
1531 auto Size = Op.getValueSizeInBits();
1532 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1533 if (DestOp.getValueType() == VT)
1534 return DestOp;
1535
1536 return getSExtOrTrunc(DestOp, DL, VT);
1537}
1538
1540 EVT VT) {
1541 assert(!VT.isVector());
1542 auto Type = Op.getValueType();
1543 SDValue DestOp;
1544 if (Type == VT)
1545 return Op;
1546 auto Size = Op.getValueSizeInBits();
1547 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1548 if (DestOp.getValueType() == VT)
1549 return DestOp;
1550
1551 return getZExtOrTrunc(DestOp, DL, VT);
1552}
1553
1555 EVT OpVT) {
1556 if (VT.bitsLE(Op.getValueType()))
1557 return getNode(ISD::TRUNCATE, SL, VT, Op);
1558
1559 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1560 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1561}
1562
1564 EVT OpVT = Op.getValueType();
1565 assert(VT.isInteger() && OpVT.isInteger() &&
1566 "Cannot getZeroExtendInReg FP types");
1567 assert(VT.isVector() == OpVT.isVector() &&
1568 "getZeroExtendInReg type should be vector iff the operand "
1569 "type is vector!");
1570 assert((!VT.isVector() ||
1572 "Vector element counts must match in getZeroExtendInReg");
1573 assert(VT.bitsLE(OpVT) && "Not extending!");
1574 if (OpVT == VT)
1575 return Op;
1577 VT.getScalarSizeInBits());
1578 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1579}
1580
1582 SDValue EVL, const SDLoc &DL,
1583 EVT VT) {
1584 EVT OpVT = Op.getValueType();
1585 assert(VT.isInteger() && OpVT.isInteger() &&
1586 "Cannot getVPZeroExtendInReg FP types");
1587 assert(VT.isVector() && OpVT.isVector() &&
1588 "getVPZeroExtendInReg type and operand type should be vector!");
1590 "Vector element counts must match in getZeroExtendInReg");
1591 assert(VT.bitsLE(OpVT) && "Not extending!");
1592 if (OpVT == VT)
1593 return Op;
1595 VT.getScalarSizeInBits());
1596 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1597 EVL);
1598}
1599
1601 // Only unsigned pointer semantics are supported right now. In the future this
1602 // might delegate to TLI to check pointer signedness.
1603 return getZExtOrTrunc(Op, DL, VT);
1604}
1605
1607 // Only unsigned pointer semantics are supported right now. In the future this
1608 // might delegate to TLI to check pointer signedness.
1609 return getZeroExtendInReg(Op, DL, VT);
1610}
1611
1613 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1614}
1615
1616/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1618 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1619}
1620
1622 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1623 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1624}
1625
1627 SDValue Mask, SDValue EVL, EVT VT) {
1628 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1629 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1630}
1631
1633 SDValue Mask, SDValue EVL) {
1634 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1635}
1636
1638 SDValue Mask, SDValue EVL) {
1639 if (VT.bitsGT(Op.getValueType()))
1640 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1641 if (VT.bitsLT(Op.getValueType()))
1642 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1643 return Op;
1644}
1645
1647 EVT OpVT) {
1648 if (!V)
1649 return getConstant(0, DL, VT);
1650
1651 switch (TLI->getBooleanContents(OpVT)) {
1654 return getConstant(1, DL, VT);
1656 return getAllOnesConstant(DL, VT);
1657 }
1658 llvm_unreachable("Unexpected boolean content enum!");
1659}
1660
1662 bool isT, bool isO) {
1663 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1664 DL, VT, isT, isO);
1665}
1666
1668 bool isT, bool isO) {
1669 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1670}
1671
1673 EVT VT, bool isT, bool isO) {
1674 assert(VT.isInteger() && "Cannot create FP integer constant!");
1675
1676 EVT EltVT = VT.getScalarType();
1677 const ConstantInt *Elt = &Val;
1678
1679 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1680 // to-be-splatted scalar ConstantInt.
1681 if (isa<VectorType>(Elt->getType()))
1682 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1683
1684 // In some cases the vector type is legal but the element type is illegal and
1685 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1686 // inserted value (the type does not need to match the vector element type).
1687 // Any extra bits introduced will be truncated away.
1688 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1690 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1691 APInt NewVal;
1692 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1693 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1694 else
1695 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1696 Elt = ConstantInt::get(*getContext(), NewVal);
1697 }
1698 // In other cases the element type is illegal and needs to be expanded, for
1699 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1700 // the value into n parts and use a vector type with n-times the elements.
1701 // Then bitcast to the type requested.
1702 // Legalizing constants too early makes the DAGCombiner's job harder so we
1703 // only legalize if the DAG tells us we must produce legal types.
1704 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1705 TLI->getTypeAction(*getContext(), EltVT) ==
1707 const APInt &NewVal = Elt->getValue();
1708 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1709 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1710
1711 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1712 if (VT.isScalableVector() ||
1713 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1714 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1715 "Can only handle an even split!");
1716 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1717
1718 SmallVector<SDValue, 2> ScalarParts;
1719 for (unsigned i = 0; i != Parts; ++i)
1720 ScalarParts.push_back(getConstant(
1721 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1722 ViaEltVT, isT, isO));
1723
1724 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1725 }
1726
1727 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1728 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1729
1730 // Check the temporary vector is the correct size. If this fails then
1731 // getTypeToTransformTo() probably returned a type whose size (in bits)
1732 // isn't a power-of-2 factor of the requested type size.
1733 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1734
1735 SmallVector<SDValue, 2> EltParts;
1736 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1737 EltParts.push_back(getConstant(
1738 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1739 ViaEltVT, isT, isO));
1740
1741 // EltParts is currently in little endian order. If we actually want
1742 // big-endian order then reverse it now.
1743 if (getDataLayout().isBigEndian())
1744 std::reverse(EltParts.begin(), EltParts.end());
1745
1746 // The elements must be reversed when the element order is different
1747 // to the endianness of the elements (because the BITCAST is itself a
1748 // vector shuffle in this situation). However, we do not need any code to
1749 // perform this reversal because getConstant() is producing a vector
1750 // splat.
1751 // This situation occurs in MIPS MSA.
1752
1754 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1755 llvm::append_range(Ops, EltParts);
1756
1757 SDValue V =
1758 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1759 return V;
1760 }
1761
1762 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1763 "APInt size does not match type size!");
1764 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1765 SDVTList VTs = getVTList(EltVT);
1767 AddNodeIDNode(ID, Opc, VTs, {});
1768 ID.AddPointer(Elt);
1769 ID.AddBoolean(isO);
1770 void *IP = nullptr;
1771 SDNode *N = nullptr;
1772 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1773 if (!VT.isVector())
1774 return SDValue(N, 0);
1775
1776 if (!N) {
1777 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1778 CSEMap.InsertNode(N, IP);
1779 InsertNode(N);
1780 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1781 }
1782
1783 SDValue Result(N, 0);
1784 if (VT.isVector())
1785 Result = getSplat(VT, DL, Result);
1786 return Result;
1787}
1788
1790 bool isT, bool isO) {
1791 unsigned Size = VT.getScalarSizeInBits();
1792 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1793}
1794
1796 bool IsOpaque) {
1798 IsTarget, IsOpaque);
1799}
1800
1802 bool isTarget) {
1803 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1804}
1805
1807 const SDLoc &DL) {
1808 assert(VT.isInteger() && "Shift amount is not an integer type!");
1809 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1810 return getConstant(Val, DL, ShiftVT);
1811}
1812
1814 const SDLoc &DL) {
1815 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1816 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1817}
1818
1820 bool isTarget) {
1821 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1822}
1823
1825 bool isTarget) {
1826 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1827}
1828
1830 EVT VT, bool isTarget) {
1831 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1832
1833 EVT EltVT = VT.getScalarType();
1834 const ConstantFP *Elt = &V;
1835
1836 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1837 // the to-be-splatted scalar ConstantFP.
1838 if (isa<VectorType>(Elt->getType()))
1839 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1840
1841 // Do the map lookup using the actual bit pattern for the floating point
1842 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1843 // we don't have issues with SNANs.
1844 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1845 SDVTList VTs = getVTList(EltVT);
1847 AddNodeIDNode(ID, Opc, VTs, {});
1848 ID.AddPointer(Elt);
1849 void *IP = nullptr;
1850 SDNode *N = nullptr;
1851 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1852 if (!VT.isVector())
1853 return SDValue(N, 0);
1854
1855 if (!N) {
1856 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1857 CSEMap.InsertNode(N, IP);
1858 InsertNode(N);
1859 }
1860
1861 SDValue Result(N, 0);
1862 if (VT.isVector())
1863 Result = getSplat(VT, DL, Result);
1864 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1865 return Result;
1866}
1867
1869 bool isTarget) {
1870 EVT EltVT = VT.getScalarType();
1871 if (EltVT == MVT::f32)
1872 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1873 if (EltVT == MVT::f64)
1874 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1875 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1876 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1877 bool Ignored;
1878 APFloat APF = APFloat(Val);
1880 &Ignored);
1881 return getConstantFP(APF, DL, VT, isTarget);
1882 }
1883 llvm_unreachable("Unsupported type in getConstantFP");
1884}
1885
1887 EVT VT, int64_t Offset, bool isTargetGA,
1888 unsigned TargetFlags) {
1889 assert((TargetFlags == 0 || isTargetGA) &&
1890 "Cannot set target flags on target-independent globals");
1891
1892 // Truncate (with sign-extension) the offset value to the pointer size.
1894 if (BitWidth < 64)
1896
1897 unsigned Opc;
1898 if (GV->isThreadLocal())
1900 else
1902
1903 SDVTList VTs = getVTList(VT);
1905 AddNodeIDNode(ID, Opc, VTs, {});
1906 ID.AddPointer(GV);
1907 ID.AddInteger(Offset);
1908 ID.AddInteger(TargetFlags);
1909 void *IP = nullptr;
1910 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1911 return SDValue(E, 0);
1912
1913 auto *N = newSDNode<GlobalAddressSDNode>(
1914 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1915 CSEMap.InsertNode(N, IP);
1916 InsertNode(N);
1917 return SDValue(N, 0);
1918}
1919
1920SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1921 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1922 SDVTList VTs = getVTList(VT);
1924 AddNodeIDNode(ID, Opc, VTs, {});
1925 ID.AddInteger(FI);
1926 void *IP = nullptr;
1927 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1928 return SDValue(E, 0);
1929
1930 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1931 CSEMap.InsertNode(N, IP);
1932 InsertNode(N);
1933 return SDValue(N, 0);
1934}
1935
1936SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1937 unsigned TargetFlags) {
1938 assert((TargetFlags == 0 || isTarget) &&
1939 "Cannot set target flags on target-independent jump tables");
1940 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1941 SDVTList VTs = getVTList(VT);
1943 AddNodeIDNode(ID, Opc, VTs, {});
1944 ID.AddInteger(JTI);
1945 ID.AddInteger(TargetFlags);
1946 void *IP = nullptr;
1947 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1948 return SDValue(E, 0);
1949
1950 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1951 CSEMap.InsertNode(N, IP);
1952 InsertNode(N);
1953 return SDValue(N, 0);
1954}
1955
1957 const SDLoc &DL) {
1959 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1960 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1961}
1962
1964 MaybeAlign Alignment, int Offset,
1965 bool isTarget, unsigned TargetFlags) {
1966 assert((TargetFlags == 0 || isTarget) &&
1967 "Cannot set target flags on target-independent globals");
1968 if (!Alignment)
1969 Alignment = shouldOptForSize()
1970 ? getDataLayout().getABITypeAlign(C->getType())
1971 : getDataLayout().getPrefTypeAlign(C->getType());
1972 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1973 SDVTList VTs = getVTList(VT);
1975 AddNodeIDNode(ID, Opc, VTs, {});
1976 ID.AddInteger(Alignment->value());
1977 ID.AddInteger(Offset);
1978 ID.AddPointer(C);
1979 ID.AddInteger(TargetFlags);
1980 void *IP = nullptr;
1981 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1982 return SDValue(E, 0);
1983
1984 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1985 TargetFlags);
1986 CSEMap.InsertNode(N, IP);
1987 InsertNode(N);
1988 SDValue V = SDValue(N, 0);
1989 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1990 return V;
1991}
1992
1994 MaybeAlign Alignment, int Offset,
1995 bool isTarget, unsigned TargetFlags) {
1996 assert((TargetFlags == 0 || isTarget) &&
1997 "Cannot set target flags on target-independent globals");
1998 if (!Alignment)
1999 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2000 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2001 SDVTList VTs = getVTList(VT);
2003 AddNodeIDNode(ID, Opc, VTs, {});
2004 ID.AddInteger(Alignment->value());
2005 ID.AddInteger(Offset);
2006 C->addSelectionDAGCSEId(ID);
2007 ID.AddInteger(TargetFlags);
2008 void *IP = nullptr;
2009 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2010 return SDValue(E, 0);
2011
2012 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2013 TargetFlags);
2014 CSEMap.InsertNode(N, IP);
2015 InsertNode(N);
2016 return SDValue(N, 0);
2017}
2018
2021 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2022 ID.AddPointer(MBB);
2023 void *IP = nullptr;
2024 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2025 return SDValue(E, 0);
2026
2027 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2028 CSEMap.InsertNode(N, IP);
2029 InsertNode(N);
2030 return SDValue(N, 0);
2031}
2032
2034 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2035 ValueTypeNodes.size())
2036 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2037
2038 SDNode *&N = VT.isExtended() ?
2039 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2040
2041 if (N) return SDValue(N, 0);
2042 N = newSDNode<VTSDNode>(VT);
2043 InsertNode(N);
2044 return SDValue(N, 0);
2045}
2046
2048 SDNode *&N = ExternalSymbols[Sym];
2049 if (N) return SDValue(N, 0);
2050 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2051 InsertNode(N);
2052 return SDValue(N, 0);
2053}
2054
2056 SDNode *&N = MCSymbols[Sym];
2057 if (N)
2058 return SDValue(N, 0);
2059 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2060 InsertNode(N);
2061 return SDValue(N, 0);
2062}
2063
2065 unsigned TargetFlags) {
2066 SDNode *&N =
2067 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2068 if (N) return SDValue(N, 0);
2069 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2070 InsertNode(N);
2071 return SDValue(N, 0);
2072}
2073
2075 if ((unsigned)Cond >= CondCodeNodes.size())
2076 CondCodeNodes.resize(Cond+1);
2077
2078 if (!CondCodeNodes[Cond]) {
2079 auto *N = newSDNode<CondCodeSDNode>(Cond);
2080 CondCodeNodes[Cond] = N;
2081 InsertNode(N);
2082 }
2083
2084 return SDValue(CondCodeNodes[Cond], 0);
2085}
2086
2088 bool ConstantFold) {
2089 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2090 "APInt size does not match type size!");
2091
2092 if (MulImm == 0)
2093 return getConstant(0, DL, VT);
2094
2095 if (ConstantFold) {
2096 const MachineFunction &MF = getMachineFunction();
2097 const Function &F = MF.getFunction();
2098 ConstantRange CR = getVScaleRange(&F, 64);
2099 if (const APInt *C = CR.getSingleElement())
2100 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2101 }
2102
2103 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2104}
2105
2107 bool ConstantFold) {
2108 if (EC.isScalable())
2109 return getVScale(DL, VT,
2110 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2111
2112 return getConstant(EC.getKnownMinValue(), DL, VT);
2113}
2114
2116 APInt One(ResVT.getScalarSizeInBits(), 1);
2117 return getStepVector(DL, ResVT, One);
2118}
2119
2121 const APInt &StepVal) {
2122 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2123 if (ResVT.isScalableVector())
2124 return getNode(
2125 ISD::STEP_VECTOR, DL, ResVT,
2126 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2127
2128 SmallVector<SDValue, 16> OpsStepConstants;
2129 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2130 OpsStepConstants.push_back(
2131 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2132 return getBuildVector(ResVT, DL, OpsStepConstants);
2133}
2134
2135/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2136/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2141
2143 SDValue N2, ArrayRef<int> Mask) {
2144 assert(VT.getVectorNumElements() == Mask.size() &&
2145 "Must have the same number of vector elements as mask elements!");
2146 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2147 "Invalid VECTOR_SHUFFLE");
2148
2149 // Canonicalize shuffle undef, undef -> undef
2150 if (N1.isUndef() && N2.isUndef())
2151 return getUNDEF(VT);
2152
2153 // Validate that all indices in Mask are within the range of the elements
2154 // input to the shuffle.
2155 int NElts = Mask.size();
2156 assert(llvm::all_of(Mask,
2157 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2158 "Index out of range");
2159
2160 // Copy the mask so we can do any needed cleanup.
2161 SmallVector<int, 8> MaskVec(Mask);
2162
2163 // Canonicalize shuffle v, v -> v, undef
2164 if (N1 == N2) {
2165 N2 = getUNDEF(VT);
2166 for (int i = 0; i != NElts; ++i)
2167 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2168 }
2169
2170 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2171 if (N1.isUndef())
2172 commuteShuffle(N1, N2, MaskVec);
2173
2174 if (TLI->hasVectorBlend()) {
2175 // If shuffling a splat, try to blend the splat instead. We do this here so
2176 // that even when this arises during lowering we don't have to re-handle it.
2177 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2178 BitVector UndefElements;
2179 SDValue Splat = BV->getSplatValue(&UndefElements);
2180 if (!Splat)
2181 return;
2182
2183 for (int i = 0; i < NElts; ++i) {
2184 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2185 continue;
2186
2187 // If this input comes from undef, mark it as such.
2188 if (UndefElements[MaskVec[i] - Offset]) {
2189 MaskVec[i] = -1;
2190 continue;
2191 }
2192
2193 // If we can blend a non-undef lane, use that instead.
2194 if (!UndefElements[i])
2195 MaskVec[i] = i + Offset;
2196 }
2197 };
2198 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2199 BlendSplat(N1BV, 0);
2200 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2201 BlendSplat(N2BV, NElts);
2202 }
2203
2204 // Canonicalize all index into lhs, -> shuffle lhs, undef
2205 // Canonicalize all index into rhs, -> shuffle rhs, undef
2206 bool AllLHS = true, AllRHS = true;
2207 bool N2Undef = N2.isUndef();
2208 for (int i = 0; i != NElts; ++i) {
2209 if (MaskVec[i] >= NElts) {
2210 if (N2Undef)
2211 MaskVec[i] = -1;
2212 else
2213 AllLHS = false;
2214 } else if (MaskVec[i] >= 0) {
2215 AllRHS = false;
2216 }
2217 }
2218 if (AllLHS && AllRHS)
2219 return getUNDEF(VT);
2220 if (AllLHS && !N2Undef)
2221 N2 = getUNDEF(VT);
2222 if (AllRHS) {
2223 N1 = getUNDEF(VT);
2224 commuteShuffle(N1, N2, MaskVec);
2225 }
2226 // Reset our undef status after accounting for the mask.
2227 N2Undef = N2.isUndef();
2228 // Re-check whether both sides ended up undef.
2229 if (N1.isUndef() && N2Undef)
2230 return getUNDEF(VT);
2231
2232 // If Identity shuffle return that node.
2233 bool Identity = true, AllSame = true;
2234 for (int i = 0; i != NElts; ++i) {
2235 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2236 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2237 }
2238 if (Identity && NElts)
2239 return N1;
2240
2241 // Shuffling a constant splat doesn't change the result.
2242 if (N2Undef) {
2243 SDValue V = N1;
2244
2245 // Look through any bitcasts. We check that these don't change the number
2246 // (and size) of elements and just changes their types.
2247 while (V.getOpcode() == ISD::BITCAST)
2248 V = V->getOperand(0);
2249
2250 // A splat should always show up as a build vector node.
2251 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2252 BitVector UndefElements;
2253 SDValue Splat = BV->getSplatValue(&UndefElements);
2254 // If this is a splat of an undef, shuffling it is also undef.
2255 if (Splat && Splat.isUndef())
2256 return getUNDEF(VT);
2257
2258 bool SameNumElts =
2259 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2260
2261 // We only have a splat which can skip shuffles if there is a splatted
2262 // value and no undef lanes rearranged by the shuffle.
2263 if (Splat && UndefElements.none()) {
2264 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2265 // number of elements match or the value splatted is a zero constant.
2266 if (SameNumElts || isNullConstant(Splat))
2267 return N1;
2268 }
2269
2270 // If the shuffle itself creates a splat, build the vector directly.
2271 if (AllSame && SameNumElts) {
2272 EVT BuildVT = BV->getValueType(0);
2273 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2274 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2275
2276 // We may have jumped through bitcasts, so the type of the
2277 // BUILD_VECTOR may not match the type of the shuffle.
2278 if (BuildVT != VT)
2279 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2280 return NewBV;
2281 }
2282 }
2283 }
2284
2285 SDVTList VTs = getVTList(VT);
2287 SDValue Ops[2] = { N1, N2 };
2289 for (int i = 0; i != NElts; ++i)
2290 ID.AddInteger(MaskVec[i]);
2291
2292 void* IP = nullptr;
2293 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2294 return SDValue(E, 0);
2295
2296 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2297 // SDNode doesn't have access to it. This memory will be "leaked" when
2298 // the node is deallocated, but recovered when the NodeAllocator is released.
2299 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2300 llvm::copy(MaskVec, MaskAlloc);
2301
2302 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2303 dl.getDebugLoc(), MaskAlloc);
2304 createOperands(N, Ops);
2305
2306 CSEMap.InsertNode(N, IP);
2307 InsertNode(N);
2308 SDValue V = SDValue(N, 0);
2309 NewSDValueDbgMsg(V, "Creating new node: ", this);
2310 return V;
2311}
2312
2314 EVT VT = SV.getValueType(0);
2315 SmallVector<int, 8> MaskVec(SV.getMask());
2317
2318 SDValue Op0 = SV.getOperand(0);
2319 SDValue Op1 = SV.getOperand(1);
2320 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2321}
2322
2324 SDVTList VTs = getVTList(VT);
2326 AddNodeIDNode(ID, ISD::Register, VTs, {});
2327 ID.AddInteger(Reg.id());
2328 void *IP = nullptr;
2329 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2330 return SDValue(E, 0);
2331
2332 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2333 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2334 CSEMap.InsertNode(N, IP);
2335 InsertNode(N);
2336 return SDValue(N, 0);
2337}
2338
2341 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2342 ID.AddPointer(RegMask);
2343 void *IP = nullptr;
2344 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2345 return SDValue(E, 0);
2346
2347 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2348 CSEMap.InsertNode(N, IP);
2349 InsertNode(N);
2350 return SDValue(N, 0);
2351}
2352
2354 MCSymbol *Label) {
2355 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2356}
2357
2358SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2359 SDValue Root, MCSymbol *Label) {
2361 SDValue Ops[] = { Root };
2362 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2363 ID.AddPointer(Label);
2364 void *IP = nullptr;
2365 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2366 return SDValue(E, 0);
2367
2368 auto *N =
2369 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2370 createOperands(N, Ops);
2371
2372 CSEMap.InsertNode(N, IP);
2373 InsertNode(N);
2374 return SDValue(N, 0);
2375}
2376
2378 int64_t Offset, bool isTarget,
2379 unsigned TargetFlags) {
2380 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2381 SDVTList VTs = getVTList(VT);
2382
2384 AddNodeIDNode(ID, Opc, VTs, {});
2385 ID.AddPointer(BA);
2386 ID.AddInteger(Offset);
2387 ID.AddInteger(TargetFlags);
2388 void *IP = nullptr;
2389 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2390 return SDValue(E, 0);
2391
2392 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2393 CSEMap.InsertNode(N, IP);
2394 InsertNode(N);
2395 return SDValue(N, 0);
2396}
2397
2400 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2401 ID.AddPointer(V);
2402
2403 void *IP = nullptr;
2404 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2405 return SDValue(E, 0);
2406
2407 auto *N = newSDNode<SrcValueSDNode>(V);
2408 CSEMap.InsertNode(N, IP);
2409 InsertNode(N);
2410 return SDValue(N, 0);
2411}
2412
2415 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2416 ID.AddPointer(MD);
2417
2418 void *IP = nullptr;
2419 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2420 return SDValue(E, 0);
2421
2422 auto *N = newSDNode<MDNodeSDNode>(MD);
2423 CSEMap.InsertNode(N, IP);
2424 InsertNode(N);
2425 return SDValue(N, 0);
2426}
2427
2429 if (VT == V.getValueType())
2430 return V;
2431
2432 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2433}
2434
2436 unsigned SrcAS, unsigned DestAS) {
2437 SDVTList VTs = getVTList(VT);
2438 SDValue Ops[] = {Ptr};
2440 AddNodeIDNode(ID, ISD::ADDRSPACECAST, VTs, Ops);
2441 ID.AddInteger(SrcAS);
2442 ID.AddInteger(DestAS);
2443
2444 void *IP = nullptr;
2445 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2446 return SDValue(E, 0);
2447
2448 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2449 VTs, SrcAS, DestAS);
2450 createOperands(N, Ops);
2451
2452 CSEMap.InsertNode(N, IP);
2453 InsertNode(N);
2454 return SDValue(N, 0);
2455}
2456
2458 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2459}
2460
2461/// getShiftAmountOperand - Return the specified value casted to
2462/// the target's desired shift amount type.
2464 EVT OpTy = Op.getValueType();
2465 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2466 if (OpTy == ShTy || OpTy.isVector()) return Op;
2467
2468 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2469}
2470
2471/// Given a store node \p StoreNode, return true if it is safe to fold that node
2472/// into \p FPNode, which expands to a library call with output pointers.
2474 SDNode *FPNode) {
2476 SmallVector<const SDNode *, 8> DeferredNodes;
2478
2479 // Skip FPNode use by StoreNode (that's the use we want to fold into FPNode).
2480 for (SDValue Op : StoreNode->ops())
2481 if (Op.getNode() != FPNode)
2482 Worklist.push_back(Op.getNode());
2483
2485 while (!Worklist.empty()) {
2486 const SDNode *Node = Worklist.pop_back_val();
2487 auto [_, Inserted] = Visited.insert(Node);
2488 if (!Inserted)
2489 continue;
2490
2491 if (MaxSteps > 0 && Visited.size() >= MaxSteps)
2492 return false;
2493
2494 // Reached the FPNode (would result in a cycle).
2495 // OR Reached CALLSEQ_START (would result in nested call sequences).
2496 if (Node == FPNode || Node->getOpcode() == ISD::CALLSEQ_START)
2497 return false;
2498
2499 if (Node->getOpcode() == ISD::CALLSEQ_END) {
2500 // Defer looking into call sequences (so we can check we're outside one).
2501 // We still need to look through these for the predecessor check.
2502 DeferredNodes.push_back(Node);
2503 continue;
2504 }
2505
2506 for (SDValue Op : Node->ops())
2507 Worklist.push_back(Op.getNode());
2508 }
2509
2510 // True if we're outside a call sequence and don't have the FPNode as a
2511 // predecessor. No cycles or nested call sequences possible.
2512 return !SDNode::hasPredecessorHelper(FPNode, Visited, DeferredNodes,
2513 MaxSteps);
2514}
2515
2517 RTLIB::Libcall LC, SDNode *Node, SmallVectorImpl<SDValue> &Results,
2518 std::optional<unsigned> CallRetResNo) {
2519 LLVMContext &Ctx = *getContext();
2520 EVT VT = Node->getValueType(0);
2521 unsigned NumResults = Node->getNumValues();
2522
2523 if (LC == RTLIB::UNKNOWN_LIBCALL)
2524 return false;
2525
2526 const char *LCName = TLI->getLibcallName(LC);
2527 if (!LCName)
2528 return false;
2529
2530 auto getVecDesc = [&]() -> VecDesc const * {
2531 for (bool Masked : {false, true}) {
2532 if (VecDesc const *VD = getLibInfo().getVectorMappingInfo(
2533 LCName, VT.getVectorElementCount(), Masked)) {
2534 return VD;
2535 }
2536 }
2537 return nullptr;
2538 };
2539
2540 // For vector types, we must find a vector mapping for the libcall.
2541 VecDesc const *VD = nullptr;
2542 if (VT.isVector() && !(VD = getVecDesc()))
2543 return false;
2544
2545 // Find users of the node that store the results (and share input chains). The
2546 // destination pointers can be used instead of creating stack allocations.
2547 SDValue StoresInChain;
2548 SmallVector<StoreSDNode *, 2> ResultStores(NumResults);
2549 for (SDNode *User : Node->users()) {
2551 continue;
2552 auto *ST = cast<StoreSDNode>(User);
2553 SDValue StoreValue = ST->getValue();
2554 unsigned ResNo = StoreValue.getResNo();
2555 // Ensure the store corresponds to an output pointer.
2556 if (CallRetResNo == ResNo)
2557 continue;
2558 // Ensure the store to the default address space and not atomic or volatile.
2559 if (!ST->isSimple() || ST->getAddressSpace() != 0)
2560 continue;
2561 // Ensure all store chains are the same (so they don't alias).
2562 if (StoresInChain && ST->getChain() != StoresInChain)
2563 continue;
2564 // Ensure the store is properly aligned.
2565 Type *StoreType = StoreValue.getValueType().getTypeForEVT(Ctx);
2566 if (ST->getAlign() <
2567 getDataLayout().getABITypeAlign(StoreType->getScalarType()))
2568 continue;
2569 // Avoid:
2570 // 1. Creating cyclic dependencies.
2571 // 2. Expanding the node to a call within a call sequence.
2573 continue;
2574 ResultStores[ResNo] = ST;
2575 StoresInChain = ST->getChain();
2576 }
2577
2579
2580 // Pass the arguments.
2581 for (const SDValue &Op : Node->op_values()) {
2582 EVT ArgVT = Op.getValueType();
2583 Type *ArgTy = ArgVT.getTypeForEVT(Ctx);
2584 Args.emplace_back(Op, ArgTy);
2585 }
2586
2587 // Pass the output pointers.
2588 SmallVector<SDValue, 2> ResultPtrs(NumResults);
2590 for (auto [ResNo, ST] : llvm::enumerate(ResultStores)) {
2591 if (ResNo == CallRetResNo)
2592 continue;
2593 EVT ResVT = Node->getValueType(ResNo);
2594 SDValue ResultPtr = ST ? ST->getBasePtr() : CreateStackTemporary(ResVT);
2595 ResultPtrs[ResNo] = ResultPtr;
2596 Args.emplace_back(ResultPtr, PointerTy);
2597 }
2598
2599 SDLoc DL(Node);
2600
2601 // Pass the vector mask (if required).
2602 if (VD && VD->isMasked()) {
2603 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), Ctx, VT);
2604 SDValue Mask = getBoolConstant(true, DL, MaskVT, VT);
2605 Args.emplace_back(Mask, MaskVT.getTypeForEVT(Ctx));
2606 }
2607
2608 Type *RetType = CallRetResNo.has_value()
2609 ? Node->getValueType(*CallRetResNo).getTypeForEVT(Ctx)
2610 : Type::getVoidTy(Ctx);
2611 SDValue InChain = StoresInChain ? StoresInChain : getEntryNode();
2612 SDValue Callee = getExternalSymbol(VD ? VD->getVectorFnName().data() : LCName,
2613 TLI->getPointerTy(getDataLayout()));
2615 CLI.setDebugLoc(DL).setChain(InChain).setLibCallee(
2616 TLI->getLibcallCallingConv(LC), RetType, Callee, std::move(Args));
2617
2618 auto [Call, CallChain] = TLI->LowerCallTo(CLI);
2619
2620 for (auto [ResNo, ResultPtr] : llvm::enumerate(ResultPtrs)) {
2621 if (ResNo == CallRetResNo) {
2622 Results.push_back(Call);
2623 continue;
2624 }
2625 MachinePointerInfo PtrInfo;
2626 SDValue LoadResult =
2627 getLoad(Node->getValueType(ResNo), DL, CallChain, ResultPtr, PtrInfo);
2628 SDValue OutChain = LoadResult.getValue(1);
2629
2630 if (StoreSDNode *ST = ResultStores[ResNo]) {
2631 // Replace store with the library call.
2632 ReplaceAllUsesOfValueWith(SDValue(ST, 0), OutChain);
2633 PtrInfo = ST->getPointerInfo();
2634 } else {
2636 getMachineFunction(), cast<FrameIndexSDNode>(ResultPtr)->getIndex());
2637 }
2638
2639 Results.push_back(LoadResult);
2640 }
2641
2642 return true;
2643}
2644
2646 SDLoc dl(Node);
2648 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2649 EVT VT = Node->getValueType(0);
2650 SDValue Tmp1 = Node->getOperand(0);
2651 SDValue Tmp2 = Node->getOperand(1);
2652 const MaybeAlign MA(Node->getConstantOperandVal(3));
2653
2654 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2655 Tmp2, MachinePointerInfo(V));
2656 SDValue VAList = VAListLoad;
2657
2658 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2659 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2660 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2661
2662 VAList = getNode(
2663 ISD::AND, dl, VAList.getValueType(), VAList,
2664 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2665 }
2666
2667 // Increment the pointer, VAList, to the next vaarg
2668 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2669 getConstant(getDataLayout().getTypeAllocSize(
2670 VT.getTypeForEVT(*getContext())),
2671 dl, VAList.getValueType()));
2672 // Store the incremented VAList to the legalized pointer
2673 Tmp1 =
2674 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2675 // Load the actual argument out of the pointer VAList
2676 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2677}
2678
2680 SDLoc dl(Node);
2682 // This defaults to loading a pointer from the input and storing it to the
2683 // output, returning the chain.
2684 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2685 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2686 SDValue Tmp1 =
2687 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2688 Node->getOperand(2), MachinePointerInfo(VS));
2689 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2690 MachinePointerInfo(VD));
2691}
2692
2694 const DataLayout &DL = getDataLayout();
2695 Type *Ty = VT.getTypeForEVT(*getContext());
2696 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2697
2698 if (TLI->isTypeLegal(VT) || !VT.isVector())
2699 return RedAlign;
2700
2701 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2702 const Align StackAlign = TFI->getStackAlign();
2703
2704 // See if we can choose a smaller ABI alignment in cases where it's an
2705 // illegal vector type that will get broken down.
2706 if (RedAlign > StackAlign) {
2707 EVT IntermediateVT;
2708 MVT RegisterVT;
2709 unsigned NumIntermediates;
2710 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2711 NumIntermediates, RegisterVT);
2712 Ty = IntermediateVT.getTypeForEVT(*getContext());
2713 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2714 if (RedAlign2 < RedAlign)
2715 RedAlign = RedAlign2;
2716
2717 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2718 // If the stack is not realignable, the alignment should be limited to the
2719 // StackAlignment
2720 RedAlign = std::min(RedAlign, StackAlign);
2721 }
2722
2723 return RedAlign;
2724}
2725
2727 MachineFrameInfo &MFI = MF->getFrameInfo();
2728 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2729 int StackID = 0;
2730 if (Bytes.isScalable())
2731 StackID = TFI->getStackIDForScalableVectors();
2732 // The stack id gives an indication of whether the object is scalable or
2733 // not, so it's safe to pass in the minimum size here.
2734 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2735 false, nullptr, StackID);
2736 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2737}
2738
2740 Type *Ty = VT.getTypeForEVT(*getContext());
2741 Align StackAlign =
2742 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2743 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2744}
2745
2747 TypeSize VT1Size = VT1.getStoreSize();
2748 TypeSize VT2Size = VT2.getStoreSize();
2749 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2750 "Don't know how to choose the maximum size when creating a stack "
2751 "temporary");
2752 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2753 ? VT1Size
2754 : VT2Size;
2755
2756 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2757 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2758 const DataLayout &DL = getDataLayout();
2759 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2760 return CreateStackTemporary(Bytes, Align);
2761}
2762
2764 ISD::CondCode Cond, const SDLoc &dl) {
2765 EVT OpVT = N1.getValueType();
2766
2767 auto GetUndefBooleanConstant = [&]() {
2768 if (VT.getScalarType() == MVT::i1 ||
2769 TLI->getBooleanContents(OpVT) ==
2771 return getUNDEF(VT);
2772 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2773 // so we cannot use getUNDEF(). Return zero instead.
2774 return getConstant(0, dl, VT);
2775 };
2776
2777 // These setcc operations always fold.
2778 switch (Cond) {
2779 default: break;
2780 case ISD::SETFALSE:
2781 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2782 case ISD::SETTRUE:
2783 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2784
2785 case ISD::SETOEQ:
2786 case ISD::SETOGT:
2787 case ISD::SETOGE:
2788 case ISD::SETOLT:
2789 case ISD::SETOLE:
2790 case ISD::SETONE:
2791 case ISD::SETO:
2792 case ISD::SETUO:
2793 case ISD::SETUEQ:
2794 case ISD::SETUNE:
2795 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2796 break;
2797 }
2798
2799 if (OpVT.isInteger()) {
2800 // For EQ and NE, we can always pick a value for the undef to make the
2801 // predicate pass or fail, so we can return undef.
2802 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2803 // icmp eq/ne X, undef -> undef.
2804 if ((N1.isUndef() || N2.isUndef()) &&
2805 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2806 return GetUndefBooleanConstant();
2807
2808 // If both operands are undef, we can return undef for int comparison.
2809 // icmp undef, undef -> undef.
2810 if (N1.isUndef() && N2.isUndef())
2811 return GetUndefBooleanConstant();
2812
2813 // icmp X, X -> true/false
2814 // icmp X, undef -> true/false because undef could be X.
2815 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2816 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2817 }
2818
2820 const APInt &C2 = N2C->getAPIntValue();
2822 const APInt &C1 = N1C->getAPIntValue();
2823
2825 dl, VT, OpVT);
2826 }
2827 }
2828
2829 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2830 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2831
2832 if (N1CFP && N2CFP) {
2833 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2834 switch (Cond) {
2835 default: break;
2836 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2837 return GetUndefBooleanConstant();
2838 [[fallthrough]];
2839 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2840 OpVT);
2841 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2842 return GetUndefBooleanConstant();
2843 [[fallthrough]];
2845 R==APFloat::cmpLessThan, dl, VT,
2846 OpVT);
2847 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2848 return GetUndefBooleanConstant();
2849 [[fallthrough]];
2850 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2851 OpVT);
2852 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2853 return GetUndefBooleanConstant();
2854 [[fallthrough]];
2856 VT, OpVT);
2857 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2858 return GetUndefBooleanConstant();
2859 [[fallthrough]];
2861 R==APFloat::cmpEqual, dl, VT,
2862 OpVT);
2863 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2864 return GetUndefBooleanConstant();
2865 [[fallthrough]];
2867 R==APFloat::cmpEqual, dl, VT, OpVT);
2868 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2869 OpVT);
2870 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2871 OpVT);
2873 R==APFloat::cmpEqual, dl, VT,
2874 OpVT);
2875 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2876 OpVT);
2878 R==APFloat::cmpLessThan, dl, VT,
2879 OpVT);
2881 R==APFloat::cmpUnordered, dl, VT,
2882 OpVT);
2884 VT, OpVT);
2885 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2886 OpVT);
2887 }
2888 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2889 // Ensure that the constant occurs on the RHS.
2891 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2892 return SDValue();
2893 return getSetCC(dl, VT, N2, N1, SwappedCond);
2894 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2895 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2896 // If an operand is known to be a nan (or undef that could be a nan), we can
2897 // fold it.
2898 // Choosing NaN for the undef will always make unordered comparison succeed
2899 // and ordered comparison fails.
2900 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2901 switch (ISD::getUnorderedFlavor(Cond)) {
2902 default:
2903 llvm_unreachable("Unknown flavor!");
2904 case 0: // Known false.
2905 return getBoolConstant(false, dl, VT, OpVT);
2906 case 1: // Known true.
2907 return getBoolConstant(true, dl, VT, OpVT);
2908 case 2: // Undefined.
2909 return GetUndefBooleanConstant();
2910 }
2911 }
2912
2913 // Could not fold it.
2914 return SDValue();
2915}
2916
2917/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2918/// use this predicate to simplify operations downstream.
2920 unsigned BitWidth = Op.getScalarValueSizeInBits();
2922}
2923
2924/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2925/// this predicate to simplify operations downstream. Mask is known to be zero
2926/// for bits that V cannot have.
2928 unsigned Depth) const {
2929 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2930}
2931
2932/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2933/// DemandedElts. We use this predicate to simplify operations downstream.
2934/// Mask is known to be zero for bits that V cannot have.
2936 const APInt &DemandedElts,
2937 unsigned Depth) const {
2938 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2939}
2940
2941/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2942/// DemandedElts. We use this predicate to simplify operations downstream.
2944 unsigned Depth /* = 0 */) const {
2945 return computeKnownBits(V, DemandedElts, Depth).isZero();
2946}
2947
2948/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2950 unsigned Depth) const {
2951 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2952}
2953
2955 const APInt &DemandedElts,
2956 unsigned Depth) const {
2957 EVT VT = Op.getValueType();
2958 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2959
2960 unsigned NumElts = VT.getVectorNumElements();
2961 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2962
2963 APInt KnownZeroElements = APInt::getZero(NumElts);
2964 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2965 if (!DemandedElts[EltIdx])
2966 continue; // Don't query elements that are not demanded.
2967 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2968 if (MaskedVectorIsZero(Op, Mask, Depth))
2969 KnownZeroElements.setBit(EltIdx);
2970 }
2971 return KnownZeroElements;
2972}
2973
2974/// isSplatValue - Return true if the vector V has the same value
2975/// across all DemandedElts. For scalable vectors, we don't know the
2976/// number of lanes at compile time. Instead, we use a 1 bit APInt
2977/// to represent a conservative value for all lanes; that is, that
2978/// one bit value is implicitly splatted across all lanes.
2979bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2980 APInt &UndefElts, unsigned Depth) const {
2981 unsigned Opcode = V.getOpcode();
2982 EVT VT = V.getValueType();
2983 assert(VT.isVector() && "Vector type expected");
2984 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2985 "scalable demanded bits are ignored");
2986
2987 if (!DemandedElts)
2988 return false; // No demanded elts, better to assume we don't know anything.
2989
2990 if (Depth >= MaxRecursionDepth)
2991 return false; // Limit search depth.
2992
2993 // Deal with some common cases here that work for both fixed and scalable
2994 // vector types.
2995 switch (Opcode) {
2996 case ISD::SPLAT_VECTOR:
2997 UndefElts = V.getOperand(0).isUndef()
2998 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2999 : APInt(DemandedElts.getBitWidth(), 0);
3000 return true;
3001 case ISD::ADD:
3002 case ISD::SUB:
3003 case ISD::AND:
3004 case ISD::XOR:
3005 case ISD::OR: {
3006 APInt UndefLHS, UndefRHS;
3007 SDValue LHS = V.getOperand(0);
3008 SDValue RHS = V.getOperand(1);
3009 // Only recognize splats with the same demanded undef elements for both
3010 // operands, otherwise we might fail to handle binop-specific undef
3011 // handling.
3012 // e.g. (and undef, 0) -> 0 etc.
3013 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
3014 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
3015 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
3016 UndefElts = UndefLHS | UndefRHS;
3017 return true;
3018 }
3019 return false;
3020 }
3021 case ISD::ABS:
3022 case ISD::TRUNCATE:
3023 case ISD::SIGN_EXTEND:
3024 case ISD::ZERO_EXTEND:
3025 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
3026 default:
3027 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
3028 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
3029 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
3030 Depth);
3031 break;
3032 }
3033
3034 // We don't support other cases than those above for scalable vectors at
3035 // the moment.
3036 if (VT.isScalableVector())
3037 return false;
3038
3039 unsigned NumElts = VT.getVectorNumElements();
3040 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3041 UndefElts = APInt::getZero(NumElts);
3042
3043 switch (Opcode) {
3044 case ISD::BUILD_VECTOR: {
3045 SDValue Scl;
3046 for (unsigned i = 0; i != NumElts; ++i) {
3047 SDValue Op = V.getOperand(i);
3048 if (Op.isUndef()) {
3049 UndefElts.setBit(i);
3050 continue;
3051 }
3052 if (!DemandedElts[i])
3053 continue;
3054 if (Scl && Scl != Op)
3055 return false;
3056 Scl = Op;
3057 }
3058 return true;
3059 }
3060 case ISD::VECTOR_SHUFFLE: {
3061 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3062 APInt DemandedLHS = APInt::getZero(NumElts);
3063 APInt DemandedRHS = APInt::getZero(NumElts);
3064 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3065 for (int i = 0; i != (int)NumElts; ++i) {
3066 int M = Mask[i];
3067 if (M < 0) {
3068 UndefElts.setBit(i);
3069 continue;
3070 }
3071 if (!DemandedElts[i])
3072 continue;
3073 if (M < (int)NumElts)
3074 DemandedLHS.setBit(M);
3075 else
3076 DemandedRHS.setBit(M - NumElts);
3077 }
3078
3079 // If we aren't demanding either op, assume there's no splat.
3080 // If we are demanding both ops, assume there's no splat.
3081 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3082 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3083 return false;
3084
3085 // See if the demanded elts of the source op is a splat or we only demand
3086 // one element, which should always be a splat.
3087 // TODO: Handle source ops splats with undefs.
3088 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3089 APInt SrcUndefs;
3090 return (SrcElts.popcount() == 1) ||
3091 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3092 (SrcElts & SrcUndefs).isZero());
3093 };
3094 if (!DemandedLHS.isZero())
3095 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3096 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3097 }
3099 // Offset the demanded elts by the subvector index.
3100 SDValue Src = V.getOperand(0);
3101 // We don't support scalable vectors at the moment.
3102 if (Src.getValueType().isScalableVector())
3103 return false;
3104 uint64_t Idx = V.getConstantOperandVal(1);
3105 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3106 APInt UndefSrcElts;
3107 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3108 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3109 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3110 return true;
3111 }
3112 break;
3113 }
3117 // Widen the demanded elts by the src element count.
3118 SDValue Src = V.getOperand(0);
3119 // We don't support scalable vectors at the moment.
3120 if (Src.getValueType().isScalableVector())
3121 return false;
3122 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3123 APInt UndefSrcElts;
3124 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3125 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3126 UndefElts = UndefSrcElts.trunc(NumElts);
3127 return true;
3128 }
3129 break;
3130 }
3131 case ISD::BITCAST: {
3132 SDValue Src = V.getOperand(0);
3133 EVT SrcVT = Src.getValueType();
3134 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3135 unsigned BitWidth = VT.getScalarSizeInBits();
3136
3137 // Ignore bitcasts from unsupported types.
3138 // TODO: Add fp support?
3139 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3140 break;
3141
3142 // Bitcast 'small element' vector to 'large element' vector.
3143 if ((BitWidth % SrcBitWidth) == 0) {
3144 // See if each sub element is a splat.
3145 unsigned Scale = BitWidth / SrcBitWidth;
3146 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3147 APInt ScaledDemandedElts =
3148 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3149 for (unsigned I = 0; I != Scale; ++I) {
3150 APInt SubUndefElts;
3151 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3152 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3153 SubDemandedElts &= ScaledDemandedElts;
3154 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3155 return false;
3156 // TODO: Add support for merging sub undef elements.
3157 if (!SubUndefElts.isZero())
3158 return false;
3159 }
3160 return true;
3161 }
3162 break;
3163 }
3164 }
3165
3166 return false;
3167}
3168
3169/// Helper wrapper to main isSplatValue function.
3170bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3171 EVT VT = V.getValueType();
3172 assert(VT.isVector() && "Vector type expected");
3173
3174 APInt UndefElts;
3175 // Since the number of lanes in a scalable vector is unknown at compile time,
3176 // we track one bit which is implicitly broadcast to all lanes. This means
3177 // that all lanes in a scalable vector are considered demanded.
3178 APInt DemandedElts
3180 return isSplatValue(V, DemandedElts, UndefElts) &&
3181 (AllowUndefs || !UndefElts);
3182}
3183
3186
3187 EVT VT = V.getValueType();
3188 unsigned Opcode = V.getOpcode();
3189 switch (Opcode) {
3190 default: {
3191 APInt UndefElts;
3192 // Since the number of lanes in a scalable vector is unknown at compile time,
3193 // we track one bit which is implicitly broadcast to all lanes. This means
3194 // that all lanes in a scalable vector are considered demanded.
3195 APInt DemandedElts
3197
3198 if (isSplatValue(V, DemandedElts, UndefElts)) {
3199 if (VT.isScalableVector()) {
3200 // DemandedElts and UndefElts are ignored for scalable vectors, since
3201 // the only supported cases are SPLAT_VECTOR nodes.
3202 SplatIdx = 0;
3203 } else {
3204 // Handle case where all demanded elements are UNDEF.
3205 if (DemandedElts.isSubsetOf(UndefElts)) {
3206 SplatIdx = 0;
3207 return getUNDEF(VT);
3208 }
3209 SplatIdx = (UndefElts & DemandedElts).countr_one();
3210 }
3211 return V;
3212 }
3213 break;
3214 }
3215 case ISD::SPLAT_VECTOR:
3216 SplatIdx = 0;
3217 return V;
3218 case ISD::VECTOR_SHUFFLE: {
3219 assert(!VT.isScalableVector());
3220 // Check if this is a shuffle node doing a splat.
3221 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3222 // getTargetVShiftNode currently struggles without the splat source.
3223 auto *SVN = cast<ShuffleVectorSDNode>(V);
3224 if (!SVN->isSplat())
3225 break;
3226 int Idx = SVN->getSplatIndex();
3227 int NumElts = V.getValueType().getVectorNumElements();
3228 SplatIdx = Idx % NumElts;
3229 return V.getOperand(Idx / NumElts);
3230 }
3231 }
3232
3233 return SDValue();
3234}
3235
3237 int SplatIdx;
3238 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3239 EVT SVT = SrcVector.getValueType().getScalarType();
3240 EVT LegalSVT = SVT;
3241 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3242 if (!SVT.isInteger())
3243 return SDValue();
3244 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3245 if (LegalSVT.bitsLT(SVT))
3246 return SDValue();
3247 }
3248 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3249 }
3250 return SDValue();
3251}
3252
3253std::optional<ConstantRange>
3255 unsigned Depth) const {
3256 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3257 V.getOpcode() == ISD::SRA) &&
3258 "Unknown shift node");
3259 // Shifting more than the bitwidth is not valid.
3260 unsigned BitWidth = V.getScalarValueSizeInBits();
3261
3262 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3263 const APInt &ShAmt = Cst->getAPIntValue();
3264 if (ShAmt.uge(BitWidth))
3265 return std::nullopt;
3266 return ConstantRange(ShAmt);
3267 }
3268
3269 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3270 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3271 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3272 if (!DemandedElts[i])
3273 continue;
3274 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3275 if (!SA) {
3276 MinAmt = MaxAmt = nullptr;
3277 break;
3278 }
3279 const APInt &ShAmt = SA->getAPIntValue();
3280 if (ShAmt.uge(BitWidth))
3281 return std::nullopt;
3282 if (!MinAmt || MinAmt->ugt(ShAmt))
3283 MinAmt = &ShAmt;
3284 if (!MaxAmt || MaxAmt->ult(ShAmt))
3285 MaxAmt = &ShAmt;
3286 }
3287 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3288 "Failed to find matching min/max shift amounts");
3289 if (MinAmt && MaxAmt)
3290 return ConstantRange(*MinAmt, *MaxAmt + 1);
3291 }
3292
3293 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3294 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3295 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3296 if (KnownAmt.getMaxValue().ult(BitWidth))
3297 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3298
3299 return std::nullopt;
3300}
3301
3302std::optional<unsigned>
3304 unsigned Depth) const {
3305 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3306 V.getOpcode() == ISD::SRA) &&
3307 "Unknown shift node");
3308 if (std::optional<ConstantRange> AmtRange =
3309 getValidShiftAmountRange(V, DemandedElts, Depth))
3310 if (const APInt *ShAmt = AmtRange->getSingleElement())
3311 return ShAmt->getZExtValue();
3312 return std::nullopt;
3313}
3314
3315std::optional<unsigned>
3317 EVT VT = V.getValueType();
3318 APInt DemandedElts = VT.isFixedLengthVector()
3320 : APInt(1, 1);
3321 return getValidShiftAmount(V, DemandedElts, Depth);
3322}
3323
3324std::optional<unsigned>
3326 unsigned Depth) const {
3327 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3328 V.getOpcode() == ISD::SRA) &&
3329 "Unknown shift node");
3330 if (std::optional<ConstantRange> AmtRange =
3331 getValidShiftAmountRange(V, DemandedElts, Depth))
3332 return AmtRange->getUnsignedMin().getZExtValue();
3333 return std::nullopt;
3334}
3335
3336std::optional<unsigned>
3338 EVT VT = V.getValueType();
3339 APInt DemandedElts = VT.isFixedLengthVector()
3341 : APInt(1, 1);
3342 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3343}
3344
3345std::optional<unsigned>
3347 unsigned Depth) const {
3348 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3349 V.getOpcode() == ISD::SRA) &&
3350 "Unknown shift node");
3351 if (std::optional<ConstantRange> AmtRange =
3352 getValidShiftAmountRange(V, DemandedElts, Depth))
3353 return AmtRange->getUnsignedMax().getZExtValue();
3354 return std::nullopt;
3355}
3356
3357std::optional<unsigned>
3359 EVT VT = V.getValueType();
3360 APInt DemandedElts = VT.isFixedLengthVector()
3362 : APInt(1, 1);
3363 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3364}
3365
3366/// Determine which bits of Op are known to be either zero or one and return
3367/// them in Known. For vectors, the known bits are those that are shared by
3368/// every vector element.
3370 EVT VT = Op.getValueType();
3371
3372 // Since the number of lanes in a scalable vector is unknown at compile time,
3373 // we track one bit which is implicitly broadcast to all lanes. This means
3374 // that all lanes in a scalable vector are considered demanded.
3375 APInt DemandedElts = VT.isFixedLengthVector()
3377 : APInt(1, 1);
3378 return computeKnownBits(Op, DemandedElts, Depth);
3379}
3380
3381/// Determine which bits of Op are known to be either zero or one and return
3382/// them in Known. The DemandedElts argument allows us to only collect the known
3383/// bits that are shared by the requested vector elements.
3385 unsigned Depth) const {
3386 unsigned BitWidth = Op.getScalarValueSizeInBits();
3387
3388 KnownBits Known(BitWidth); // Don't know anything.
3389
3390 if (auto OptAPInt = Op->bitcastToAPInt()) {
3391 // We know all of the bits for a constant!
3392 return KnownBits::makeConstant(*std::move(OptAPInt));
3393 }
3394
3395 if (Depth >= MaxRecursionDepth)
3396 return Known; // Limit search depth.
3397
3398 KnownBits Known2;
3399 unsigned NumElts = DemandedElts.getBitWidth();
3400 assert((!Op.getValueType().isFixedLengthVector() ||
3401 NumElts == Op.getValueType().getVectorNumElements()) &&
3402 "Unexpected vector size");
3403
3404 if (!DemandedElts)
3405 return Known; // No demanded elts, better to assume we don't know anything.
3406
3407 unsigned Opcode = Op.getOpcode();
3408 switch (Opcode) {
3409 case ISD::MERGE_VALUES:
3410 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3411 Depth + 1);
3412 case ISD::SPLAT_VECTOR: {
3413 SDValue SrcOp = Op.getOperand(0);
3414 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3415 "Expected SPLAT_VECTOR implicit truncation");
3416 // Implicitly truncate the bits to match the official semantics of
3417 // SPLAT_VECTOR.
3418 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3419 break;
3420 }
3422 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3423 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3424 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3425 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3426 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3427 }
3428 break;
3429 }
3430 case ISD::STEP_VECTOR: {
3431 const APInt &Step = Op.getConstantOperandAPInt(0);
3432
3433 if (Step.isPowerOf2())
3434 Known.Zero.setLowBits(Step.logBase2());
3435
3437
3438 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3439 break;
3440 const APInt MinNumElts =
3441 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3442
3443 bool Overflow;
3444 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3446 .umul_ov(MinNumElts, Overflow);
3447 if (Overflow)
3448 break;
3449
3450 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3451 if (Overflow)
3452 break;
3453
3454 Known.Zero.setHighBits(MaxValue.countl_zero());
3455 break;
3456 }
3457 case ISD::BUILD_VECTOR:
3458 assert(!Op.getValueType().isScalableVector());
3459 // Collect the known bits that are shared by every demanded vector element.
3460 Known.setAllConflict();
3461 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3462 if (!DemandedElts[i])
3463 continue;
3464
3465 SDValue SrcOp = Op.getOperand(i);
3466 Known2 = computeKnownBits(SrcOp, Depth + 1);
3467
3468 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3469 if (SrcOp.getValueSizeInBits() != BitWidth) {
3470 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3471 "Expected BUILD_VECTOR implicit truncation");
3472 Known2 = Known2.trunc(BitWidth);
3473 }
3474
3475 // Known bits are the values that are shared by every demanded element.
3476 Known = Known.intersectWith(Known2);
3477
3478 // If we don't know any bits, early out.
3479 if (Known.isUnknown())
3480 break;
3481 }
3482 break;
3483 case ISD::VECTOR_COMPRESS: {
3484 SDValue Vec = Op.getOperand(0);
3485 SDValue PassThru = Op.getOperand(2);
3486 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3487 // If we don't know any bits, early out.
3488 if (Known.isUnknown())
3489 break;
3490 Known2 = computeKnownBits(Vec, Depth + 1);
3491 Known = Known.intersectWith(Known2);
3492 break;
3493 }
3494 case ISD::VECTOR_SHUFFLE: {
3495 assert(!Op.getValueType().isScalableVector());
3496 // Collect the known bits that are shared by every vector element referenced
3497 // by the shuffle.
3498 APInt DemandedLHS, DemandedRHS;
3500 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3501 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3502 DemandedLHS, DemandedRHS))
3503 break;
3504
3505 // Known bits are the values that are shared by every demanded element.
3506 Known.setAllConflict();
3507 if (!!DemandedLHS) {
3508 SDValue LHS = Op.getOperand(0);
3509 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3510 Known = Known.intersectWith(Known2);
3511 }
3512 // If we don't know any bits, early out.
3513 if (Known.isUnknown())
3514 break;
3515 if (!!DemandedRHS) {
3516 SDValue RHS = Op.getOperand(1);
3517 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3518 Known = Known.intersectWith(Known2);
3519 }
3520 break;
3521 }
3522 case ISD::VSCALE: {
3524 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3525 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3526 break;
3527 }
3528 case ISD::CONCAT_VECTORS: {
3529 if (Op.getValueType().isScalableVector())
3530 break;
3531 // Split DemandedElts and test each of the demanded subvectors.
3532 Known.setAllConflict();
3533 EVT SubVectorVT = Op.getOperand(0).getValueType();
3534 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3535 unsigned NumSubVectors = Op.getNumOperands();
3536 for (unsigned i = 0; i != NumSubVectors; ++i) {
3537 APInt DemandedSub =
3538 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3539 if (!!DemandedSub) {
3540 SDValue Sub = Op.getOperand(i);
3541 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3542 Known = Known.intersectWith(Known2);
3543 }
3544 // If we don't know any bits, early out.
3545 if (Known.isUnknown())
3546 break;
3547 }
3548 break;
3549 }
3550 case ISD::INSERT_SUBVECTOR: {
3551 if (Op.getValueType().isScalableVector())
3552 break;
3553 // Demand any elements from the subvector and the remainder from the src its
3554 // inserted into.
3555 SDValue Src = Op.getOperand(0);
3556 SDValue Sub = Op.getOperand(1);
3557 uint64_t Idx = Op.getConstantOperandVal(2);
3558 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3559 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3560 APInt DemandedSrcElts = DemandedElts;
3561 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3562
3563 Known.setAllConflict();
3564 if (!!DemandedSubElts) {
3565 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3566 if (Known.isUnknown())
3567 break; // early-out.
3568 }
3569 if (!!DemandedSrcElts) {
3570 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3571 Known = Known.intersectWith(Known2);
3572 }
3573 break;
3574 }
3576 // Offset the demanded elts by the subvector index.
3577 SDValue Src = Op.getOperand(0);
3578 // Bail until we can represent demanded elements for scalable vectors.
3579 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3580 break;
3581 uint64_t Idx = Op.getConstantOperandVal(1);
3582 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3583 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3584 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3585 break;
3586 }
3587 case ISD::SCALAR_TO_VECTOR: {
3588 if (Op.getValueType().isScalableVector())
3589 break;
3590 // We know about scalar_to_vector as much as we know about it source,
3591 // which becomes the first element of otherwise unknown vector.
3592 if (DemandedElts != 1)
3593 break;
3594
3595 SDValue N0 = Op.getOperand(0);
3596 Known = computeKnownBits(N0, Depth + 1);
3597 if (N0.getValueSizeInBits() != BitWidth)
3598 Known = Known.trunc(BitWidth);
3599
3600 break;
3601 }
3602 case ISD::BITCAST: {
3603 if (Op.getValueType().isScalableVector())
3604 break;
3605
3606 SDValue N0 = Op.getOperand(0);
3607 EVT SubVT = N0.getValueType();
3608 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3609
3610 // Ignore bitcasts from unsupported types.
3611 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3612 break;
3613
3614 // Fast handling of 'identity' bitcasts.
3615 if (BitWidth == SubBitWidth) {
3616 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3617 break;
3618 }
3619
3620 bool IsLE = getDataLayout().isLittleEndian();
3621
3622 // Bitcast 'small element' vector to 'large element' scalar/vector.
3623 if ((BitWidth % SubBitWidth) == 0) {
3624 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3625
3626 // Collect known bits for the (larger) output by collecting the known
3627 // bits from each set of sub elements and shift these into place.
3628 // We need to separately call computeKnownBits for each set of
3629 // sub elements as the knownbits for each is likely to be different.
3630 unsigned SubScale = BitWidth / SubBitWidth;
3631 APInt SubDemandedElts(NumElts * SubScale, 0);
3632 for (unsigned i = 0; i != NumElts; ++i)
3633 if (DemandedElts[i])
3634 SubDemandedElts.setBit(i * SubScale);
3635
3636 for (unsigned i = 0; i != SubScale; ++i) {
3637 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3638 Depth + 1);
3639 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3640 Known.insertBits(Known2, SubBitWidth * Shifts);
3641 }
3642 }
3643
3644 // Bitcast 'large element' scalar/vector to 'small element' vector.
3645 if ((SubBitWidth % BitWidth) == 0) {
3646 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3647
3648 // Collect known bits for the (smaller) output by collecting the known
3649 // bits from the overlapping larger input elements and extracting the
3650 // sub sections we actually care about.
3651 unsigned SubScale = SubBitWidth / BitWidth;
3652 APInt SubDemandedElts =
3653 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3654 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3655
3656 Known.setAllConflict();
3657 for (unsigned i = 0; i != NumElts; ++i)
3658 if (DemandedElts[i]) {
3659 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3660 unsigned Offset = (Shifts % SubScale) * BitWidth;
3661 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3662 // If we don't know any bits, early out.
3663 if (Known.isUnknown())
3664 break;
3665 }
3666 }
3667 break;
3668 }
3669 case ISD::AND:
3670 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3671 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3672
3673 Known &= Known2;
3674 break;
3675 case ISD::OR:
3676 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3677 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3678
3679 Known |= Known2;
3680 break;
3681 case ISD::XOR:
3682 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3683 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3684
3685 Known ^= Known2;
3686 break;
3687 case ISD::MUL: {
3688 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3689 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3690 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3691 // TODO: SelfMultiply can be poison, but not undef.
3692 if (SelfMultiply)
3693 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3694 Op.getOperand(0), DemandedElts, false, Depth + 1);
3695 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3696
3697 // If the multiplication is known not to overflow, the product of a number
3698 // with itself is non-negative. Only do this if we didn't already computed
3699 // the opposite value for the sign bit.
3700 if (Op->getFlags().hasNoSignedWrap() &&
3701 Op.getOperand(0) == Op.getOperand(1) &&
3702 !Known.isNegative())
3703 Known.makeNonNegative();
3704 break;
3705 }
3706 case ISD::MULHU: {
3707 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3708 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3709 Known = KnownBits::mulhu(Known, Known2);
3710 break;
3711 }
3712 case ISD::MULHS: {
3713 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3714 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3715 Known = KnownBits::mulhs(Known, Known2);
3716 break;
3717 }
3718 case ISD::ABDU: {
3719 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3720 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3721 Known = KnownBits::abdu(Known, Known2);
3722 break;
3723 }
3724 case ISD::ABDS: {
3725 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3726 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3727 Known = KnownBits::abds(Known, Known2);
3728 unsigned SignBits1 =
3729 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3730 if (SignBits1 == 1)
3731 break;
3732 unsigned SignBits0 =
3733 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3734 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3735 break;
3736 }
3737 case ISD::UMUL_LOHI: {
3738 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3739 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3740 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3741 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3742 if (Op.getResNo() == 0)
3743 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3744 else
3745 Known = KnownBits::mulhu(Known, Known2);
3746 break;
3747 }
3748 case ISD::SMUL_LOHI: {
3749 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3750 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3751 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3752 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3753 if (Op.getResNo() == 0)
3754 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3755 else
3756 Known = KnownBits::mulhs(Known, Known2);
3757 break;
3758 }
3759 case ISD::AVGFLOORU: {
3760 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3761 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3762 Known = KnownBits::avgFloorU(Known, Known2);
3763 break;
3764 }
3765 case ISD::AVGCEILU: {
3766 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3767 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3768 Known = KnownBits::avgCeilU(Known, Known2);
3769 break;
3770 }
3771 case ISD::AVGFLOORS: {
3772 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3773 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3774 Known = KnownBits::avgFloorS(Known, Known2);
3775 break;
3776 }
3777 case ISD::AVGCEILS: {
3778 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3779 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3780 Known = KnownBits::avgCeilS(Known, Known2);
3781 break;
3782 }
3783 case ISD::SELECT:
3784 case ISD::VSELECT:
3785 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3786 // If we don't know any bits, early out.
3787 if (Known.isUnknown())
3788 break;
3789 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3790
3791 // Only known if known in both the LHS and RHS.
3792 Known = Known.intersectWith(Known2);
3793 break;
3794 case ISD::SELECT_CC:
3795 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3796 // If we don't know any bits, early out.
3797 if (Known.isUnknown())
3798 break;
3799 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3800
3801 // Only known if known in both the LHS and RHS.
3802 Known = Known.intersectWith(Known2);
3803 break;
3804 case ISD::SMULO:
3805 case ISD::UMULO:
3806 if (Op.getResNo() != 1)
3807 break;
3808 // The boolean result conforms to getBooleanContents.
3809 // If we know the result of a setcc has the top bits zero, use this info.
3810 // We know that we have an integer-based boolean since these operations
3811 // are only available for integer.
3812 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3814 BitWidth > 1)
3815 Known.Zero.setBitsFrom(1);
3816 break;
3817 case ISD::SETCC:
3818 case ISD::SETCCCARRY:
3819 case ISD::STRICT_FSETCC:
3820 case ISD::STRICT_FSETCCS: {
3821 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3822 // If we know the result of a setcc has the top bits zero, use this info.
3823 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3825 BitWidth > 1)
3826 Known.Zero.setBitsFrom(1);
3827 break;
3828 }
3829 case ISD::SHL: {
3830 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3831 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3832
3833 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3834 bool NSW = Op->getFlags().hasNoSignedWrap();
3835
3836 bool ShAmtNonZero = Known2.isNonZero();
3837
3838 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3839
3840 // Minimum shift low bits are known zero.
3841 if (std::optional<unsigned> ShMinAmt =
3842 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3843 Known.Zero.setLowBits(*ShMinAmt);
3844 break;
3845 }
3846 case ISD::SRL:
3847 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3848 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3849 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3850 Op->getFlags().hasExact());
3851
3852 // Minimum shift high bits are known zero.
3853 if (std::optional<unsigned> ShMinAmt =
3854 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3855 Known.Zero.setHighBits(*ShMinAmt);
3856 break;
3857 case ISD::SRA:
3858 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3859 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3860 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3861 Op->getFlags().hasExact());
3862 break;
3863 case ISD::ROTL:
3864 case ISD::ROTR:
3865 if (ConstantSDNode *C =
3866 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3867 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3868
3869 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3870
3871 // Canonicalize to ROTR.
3872 if (Opcode == ISD::ROTL && Amt != 0)
3873 Amt = BitWidth - Amt;
3874
3875 Known.Zero = Known.Zero.rotr(Amt);
3876 Known.One = Known.One.rotr(Amt);
3877 }
3878 break;
3879 case ISD::FSHL:
3880 case ISD::FSHR:
3881 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3882 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3883
3884 // For fshl, 0-shift returns the 1st arg.
3885 // For fshr, 0-shift returns the 2nd arg.
3886 if (Amt == 0) {
3887 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3888 DemandedElts, Depth + 1);
3889 break;
3890 }
3891
3892 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3893 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3894 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3895 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3896 if (Opcode == ISD::FSHL) {
3897 Known <<= Amt;
3898 Known2 >>= BitWidth - Amt;
3899 } else {
3900 Known <<= BitWidth - Amt;
3901 Known2 >>= Amt;
3902 }
3903 Known = Known.unionWith(Known2);
3904 }
3905 break;
3906 case ISD::SHL_PARTS:
3907 case ISD::SRA_PARTS:
3908 case ISD::SRL_PARTS: {
3909 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3910
3911 // Collect lo/hi source values and concatenate.
3912 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3913 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3914 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3915 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3916 Known = Known2.concat(Known);
3917
3918 // Collect shift amount.
3919 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3920
3921 if (Opcode == ISD::SHL_PARTS)
3922 Known = KnownBits::shl(Known, Known2);
3923 else if (Opcode == ISD::SRA_PARTS)
3924 Known = KnownBits::ashr(Known, Known2);
3925 else // if (Opcode == ISD::SRL_PARTS)
3926 Known = KnownBits::lshr(Known, Known2);
3927
3928 // TODO: Minimum shift low/high bits are known zero.
3929
3930 if (Op.getResNo() == 0)
3931 Known = Known.extractBits(LoBits, 0);
3932 else
3933 Known = Known.extractBits(HiBits, LoBits);
3934 break;
3935 }
3937 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3938 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3939 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3940 break;
3941 }
3942 case ISD::CTTZ:
3943 case ISD::CTTZ_ZERO_UNDEF: {
3944 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3945 // If we have a known 1, its position is our upper bound.
3946 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3947 unsigned LowBits = llvm::bit_width(PossibleTZ);
3948 Known.Zero.setBitsFrom(LowBits);
3949 break;
3950 }
3951 case ISD::CTLZ:
3952 case ISD::CTLZ_ZERO_UNDEF: {
3953 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3954 // If we have a known 1, its position is our upper bound.
3955 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3956 unsigned LowBits = llvm::bit_width(PossibleLZ);
3957 Known.Zero.setBitsFrom(LowBits);
3958 break;
3959 }
3960 case ISD::CTPOP: {
3961 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3962 // If we know some of the bits are zero, they can't be one.
3963 unsigned PossibleOnes = Known2.countMaxPopulation();
3964 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3965 break;
3966 }
3967 case ISD::PARITY: {
3968 // Parity returns 0 everywhere but the LSB.
3969 Known.Zero.setBitsFrom(1);
3970 break;
3971 }
3972 case ISD::MGATHER:
3973 case ISD::MLOAD: {
3974 ISD::LoadExtType ETy =
3975 (Opcode == ISD::MGATHER)
3976 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3977 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3978 if (ETy == ISD::ZEXTLOAD) {
3979 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3980 KnownBits Known0(MemVT.getScalarSizeInBits());
3981 return Known0.zext(BitWidth);
3982 }
3983 break;
3984 }
3985 case ISD::LOAD: {
3987 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3988 if (ISD::isNON_EXTLoad(LD) && Cst) {
3989 // Determine any common known bits from the loaded constant pool value.
3990 Type *CstTy = Cst->getType();
3991 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3992 !Op.getValueType().isScalableVector()) {
3993 // If its a vector splat, then we can (quickly) reuse the scalar path.
3994 // NOTE: We assume all elements match and none are UNDEF.
3995 if (CstTy->isVectorTy()) {
3996 if (const Constant *Splat = Cst->getSplatValue()) {
3997 Cst = Splat;
3998 CstTy = Cst->getType();
3999 }
4000 }
4001 // TODO - do we need to handle different bitwidths?
4002 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
4003 // Iterate across all vector elements finding common known bits.
4004 Known.setAllConflict();
4005 for (unsigned i = 0; i != NumElts; ++i) {
4006 if (!DemandedElts[i])
4007 continue;
4008 if (Constant *Elt = Cst->getAggregateElement(i)) {
4009 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4010 const APInt &Value = CInt->getValue();
4011 Known.One &= Value;
4012 Known.Zero &= ~Value;
4013 continue;
4014 }
4015 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4016 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4017 Known.One &= Value;
4018 Known.Zero &= ~Value;
4019 continue;
4020 }
4021 }
4022 Known.One.clearAllBits();
4023 Known.Zero.clearAllBits();
4024 break;
4025 }
4026 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4027 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4028 Known = KnownBits::makeConstant(CInt->getValue());
4029 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4030 Known =
4031 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4032 }
4033 }
4034 }
4035 } else if (Op.getResNo() == 0) {
4036 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4037 KnownBits KnownScalarMemory(ScalarMemorySize);
4038 if (const MDNode *MD = LD->getRanges())
4039 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4040
4041 // Extend the Known bits from memory to the size of the scalar result.
4042 if (ISD::isZEXTLoad(Op.getNode()))
4043 Known = KnownScalarMemory.zext(BitWidth);
4044 else if (ISD::isSEXTLoad(Op.getNode()))
4045 Known = KnownScalarMemory.sext(BitWidth);
4046 else if (ISD::isEXTLoad(Op.getNode()))
4047 Known = KnownScalarMemory.anyext(BitWidth);
4048 else
4049 Known = KnownScalarMemory;
4050 assert(Known.getBitWidth() == BitWidth);
4051 return Known;
4052 }
4053 break;
4054 }
4056 if (Op.getValueType().isScalableVector())
4057 break;
4058 EVT InVT = Op.getOperand(0).getValueType();
4059 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4060 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4061 Known = Known.zext(BitWidth);
4062 break;
4063 }
4064 case ISD::ZERO_EXTEND: {
4065 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4066 Known = Known.zext(BitWidth);
4067 break;
4068 }
4070 if (Op.getValueType().isScalableVector())
4071 break;
4072 EVT InVT = Op.getOperand(0).getValueType();
4073 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4074 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4075 // If the sign bit is known to be zero or one, then sext will extend
4076 // it to the top bits, else it will just zext.
4077 Known = Known.sext(BitWidth);
4078 break;
4079 }
4080 case ISD::SIGN_EXTEND: {
4081 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4082 // If the sign bit is known to be zero or one, then sext will extend
4083 // it to the top bits, else it will just zext.
4084 Known = Known.sext(BitWidth);
4085 break;
4086 }
4088 if (Op.getValueType().isScalableVector())
4089 break;
4090 EVT InVT = Op.getOperand(0).getValueType();
4091 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4092 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4093 Known = Known.anyext(BitWidth);
4094 break;
4095 }
4096 case ISD::ANY_EXTEND: {
4097 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4098 Known = Known.anyext(BitWidth);
4099 break;
4100 }
4101 case ISD::TRUNCATE: {
4102 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4103 Known = Known.trunc(BitWidth);
4104 break;
4105 }
4106 case ISD::AssertZext: {
4107 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4109 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4110 Known.Zero |= (~InMask);
4111 Known.One &= (~Known.Zero);
4112 break;
4113 }
4114 case ISD::AssertAlign: {
4115 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4116 assert(LogOfAlign != 0);
4117
4118 // TODO: Should use maximum with source
4119 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4120 // well as clearing one bits.
4121 Known.Zero.setLowBits(LogOfAlign);
4122 Known.One.clearLowBits(LogOfAlign);
4123 break;
4124 }
4125 case ISD::FGETSIGN:
4126 // All bits are zero except the low bit.
4127 Known.Zero.setBitsFrom(1);
4128 break;
4129 case ISD::ADD:
4130 case ISD::SUB: {
4131 SDNodeFlags Flags = Op.getNode()->getFlags();
4132 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4133 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4135 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4136 Flags.hasNoUnsignedWrap(), Known, Known2);
4137 break;
4138 }
4139 case ISD::USUBO:
4140 case ISD::SSUBO:
4141 case ISD::USUBO_CARRY:
4142 case ISD::SSUBO_CARRY:
4143 if (Op.getResNo() == 1) {
4144 // If we know the result of a setcc has the top bits zero, use this info.
4145 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4147 BitWidth > 1)
4148 Known.Zero.setBitsFrom(1);
4149 break;
4150 }
4151 [[fallthrough]];
4152 case ISD::SUBC: {
4153 assert(Op.getResNo() == 0 &&
4154 "We only compute knownbits for the difference here.");
4155
4156 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4157 KnownBits Borrow(1);
4158 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4159 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4160 // Borrow has bit width 1
4161 Borrow = Borrow.trunc(1);
4162 } else {
4163 Borrow.setAllZero();
4164 }
4165
4166 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4167 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4168 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4169 break;
4170 }
4171 case ISD::UADDO:
4172 case ISD::SADDO:
4173 case ISD::UADDO_CARRY:
4174 case ISD::SADDO_CARRY:
4175 if (Op.getResNo() == 1) {
4176 // If we know the result of a setcc has the top bits zero, use this info.
4177 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4179 BitWidth > 1)
4180 Known.Zero.setBitsFrom(1);
4181 break;
4182 }
4183 [[fallthrough]];
4184 case ISD::ADDC:
4185 case ISD::ADDE: {
4186 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4187
4188 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4189 KnownBits Carry(1);
4190 if (Opcode == ISD::ADDE)
4191 // Can't track carry from glue, set carry to unknown.
4192 Carry.resetAll();
4193 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4194 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4195 // Carry has bit width 1
4196 Carry = Carry.trunc(1);
4197 } else {
4198 Carry.setAllZero();
4199 }
4200
4201 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4202 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4203 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4204 break;
4205 }
4206 case ISD::UDIV: {
4207 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4208 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4209 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4210 break;
4211 }
4212 case ISD::SDIV: {
4213 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4214 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4215 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4216 break;
4217 }
4218 case ISD::SREM: {
4219 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4220 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4221 Known = KnownBits::srem(Known, Known2);
4222 break;
4223 }
4224 case ISD::UREM: {
4225 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4226 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4227 Known = KnownBits::urem(Known, Known2);
4228 break;
4229 }
4230 case ISD::EXTRACT_ELEMENT: {
4231 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4232 const unsigned Index = Op.getConstantOperandVal(1);
4233 const unsigned EltBitWidth = Op.getValueSizeInBits();
4234
4235 // Remove low part of known bits mask
4236 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4237 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4238
4239 // Remove high part of known bit mask
4240 Known = Known.trunc(EltBitWidth);
4241 break;
4242 }
4244 SDValue InVec = Op.getOperand(0);
4245 SDValue EltNo = Op.getOperand(1);
4246 EVT VecVT = InVec.getValueType();
4247 // computeKnownBits not yet implemented for scalable vectors.
4248 if (VecVT.isScalableVector())
4249 break;
4250 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4251 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4252
4253 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4254 // anything about the extended bits.
4255 if (BitWidth > EltBitWidth)
4256 Known = Known.trunc(EltBitWidth);
4257
4258 // If we know the element index, just demand that vector element, else for
4259 // an unknown element index, ignore DemandedElts and demand them all.
4260 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4261 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4262 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4263 DemandedSrcElts =
4264 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4265
4266 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4267 if (BitWidth > EltBitWidth)
4268 Known = Known.anyext(BitWidth);
4269 break;
4270 }
4272 if (Op.getValueType().isScalableVector())
4273 break;
4274
4275 // If we know the element index, split the demand between the
4276 // source vector and the inserted element, otherwise assume we need
4277 // the original demanded vector elements and the value.
4278 SDValue InVec = Op.getOperand(0);
4279 SDValue InVal = Op.getOperand(1);
4280 SDValue EltNo = Op.getOperand(2);
4281 bool DemandedVal = true;
4282 APInt DemandedVecElts = DemandedElts;
4283 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4284 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4285 unsigned EltIdx = CEltNo->getZExtValue();
4286 DemandedVal = !!DemandedElts[EltIdx];
4287 DemandedVecElts.clearBit(EltIdx);
4288 }
4289 Known.setAllConflict();
4290 if (DemandedVal) {
4291 Known2 = computeKnownBits(InVal, Depth + 1);
4292 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4293 }
4294 if (!!DemandedVecElts) {
4295 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4296 Known = Known.intersectWith(Known2);
4297 }
4298 break;
4299 }
4300 case ISD::BITREVERSE: {
4301 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4302 Known = Known2.reverseBits();
4303 break;
4304 }
4305 case ISD::BSWAP: {
4306 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4307 Known = Known2.byteSwap();
4308 break;
4309 }
4310 case ISD::ABS: {
4311 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4312 Known = Known2.abs();
4313 Known.Zero.setHighBits(
4314 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4315 break;
4316 }
4317 case ISD::USUBSAT: {
4318 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4319 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4320 Known = KnownBits::usub_sat(Known, Known2);
4321 break;
4322 }
4323 case ISD::UMIN: {
4324 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4325 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4326 Known = KnownBits::umin(Known, Known2);
4327 break;
4328 }
4329 case ISD::UMAX: {
4330 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4331 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4332 Known = KnownBits::umax(Known, Known2);
4333 break;
4334 }
4335 case ISD::SMIN:
4336 case ISD::SMAX: {
4337 // If we have a clamp pattern, we know that the number of sign bits will be
4338 // the minimum of the clamp min/max range.
4339 bool IsMax = (Opcode == ISD::SMAX);
4340 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4341 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4342 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4343 CstHigh =
4344 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4345 if (CstLow && CstHigh) {
4346 if (!IsMax)
4347 std::swap(CstLow, CstHigh);
4348
4349 const APInt &ValueLow = CstLow->getAPIntValue();
4350 const APInt &ValueHigh = CstHigh->getAPIntValue();
4351 if (ValueLow.sle(ValueHigh)) {
4352 unsigned LowSignBits = ValueLow.getNumSignBits();
4353 unsigned HighSignBits = ValueHigh.getNumSignBits();
4354 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4355 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4356 Known.One.setHighBits(MinSignBits);
4357 break;
4358 }
4359 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4360 Known.Zero.setHighBits(MinSignBits);
4361 break;
4362 }
4363 }
4364 }
4365
4366 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4367 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4368 if (IsMax)
4369 Known = KnownBits::smax(Known, Known2);
4370 else
4371 Known = KnownBits::smin(Known, Known2);
4372
4373 // For SMAX, if CstLow is non-negative we know the result will be
4374 // non-negative and thus all sign bits are 0.
4375 // TODO: There's an equivalent of this for smin with negative constant for
4376 // known ones.
4377 if (IsMax && CstLow) {
4378 const APInt &ValueLow = CstLow->getAPIntValue();
4379 if (ValueLow.isNonNegative()) {
4380 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4381 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4382 }
4383 }
4384
4385 break;
4386 }
4387 case ISD::UINT_TO_FP: {
4388 Known.makeNonNegative();
4389 break;
4390 }
4391 case ISD::SINT_TO_FP: {
4392 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4393 if (Known2.isNonNegative())
4394 Known.makeNonNegative();
4395 else if (Known2.isNegative())
4396 Known.makeNegative();
4397 break;
4398 }
4399 case ISD::FP_TO_UINT_SAT: {
4400 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4401 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4403 break;
4404 }
4405 case ISD::ATOMIC_LOAD: {
4406 // If we are looking at the loaded value.
4407 if (Op.getResNo() == 0) {
4408 auto *AT = cast<AtomicSDNode>(Op);
4409 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4410 KnownBits KnownScalarMemory(ScalarMemorySize);
4411 if (const MDNode *MD = AT->getRanges())
4412 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4413
4414 switch (AT->getExtensionType()) {
4415 case ISD::ZEXTLOAD:
4416 Known = KnownScalarMemory.zext(BitWidth);
4417 break;
4418 case ISD::SEXTLOAD:
4419 Known = KnownScalarMemory.sext(BitWidth);
4420 break;
4421 case ISD::EXTLOAD:
4422 switch (TLI->getExtendForAtomicOps()) {
4423 case ISD::ZERO_EXTEND:
4424 Known = KnownScalarMemory.zext(BitWidth);
4425 break;
4426 case ISD::SIGN_EXTEND:
4427 Known = KnownScalarMemory.sext(BitWidth);
4428 break;
4429 default:
4430 Known = KnownScalarMemory.anyext(BitWidth);
4431 break;
4432 }
4433 break;
4434 case ISD::NON_EXTLOAD:
4435 Known = KnownScalarMemory;
4436 break;
4437 }
4438 assert(Known.getBitWidth() == BitWidth);
4439 }
4440 break;
4441 }
4442 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
4443 if (Op.getResNo() == 1) {
4444 // The boolean result conforms to getBooleanContents.
4445 // If we know the result of a setcc has the top bits zero, use this info.
4446 // We know that we have an integer-based boolean since these operations
4447 // are only available for integer.
4448 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4450 BitWidth > 1)
4451 Known.Zero.setBitsFrom(1);
4452 break;
4453 }
4454 [[fallthrough]];
4455 case ISD::ATOMIC_CMP_SWAP:
4456 case ISD::ATOMIC_SWAP:
4457 case ISD::ATOMIC_LOAD_ADD:
4458 case ISD::ATOMIC_LOAD_SUB:
4459 case ISD::ATOMIC_LOAD_AND:
4460 case ISD::ATOMIC_LOAD_CLR:
4461 case ISD::ATOMIC_LOAD_OR:
4462 case ISD::ATOMIC_LOAD_XOR:
4463 case ISD::ATOMIC_LOAD_NAND:
4464 case ISD::ATOMIC_LOAD_MIN:
4465 case ISD::ATOMIC_LOAD_MAX:
4466 case ISD::ATOMIC_LOAD_UMIN:
4467 case ISD::ATOMIC_LOAD_UMAX: {
4468 // If we are looking at the loaded value.
4469 if (Op.getResNo() == 0) {
4470 auto *AT = cast<AtomicSDNode>(Op);
4471 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4472
4473 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4474 Known.Zero.setBitsFrom(MemBits);
4475 }
4476 break;
4477 }
4478 case ISD::FrameIndex:
4480 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4481 Known, getMachineFunction());
4482 break;
4483
4484 default:
4485 if (Opcode < ISD::BUILTIN_OP_END)
4486 break;
4487 [[fallthrough]];
4491 // TODO: Probably okay to remove after audit; here to reduce change size
4492 // in initial enablement patch for scalable vectors
4493 if (Op.getValueType().isScalableVector())
4494 break;
4495
4496 // Allow the target to implement this method for its nodes.
4497 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4498 break;
4499 }
4500
4501 return Known;
4502}
4503
4504/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4517
4520 // X + 0 never overflow
4521 if (isNullConstant(N1))
4522 return OFK_Never;
4523
4524 // If both operands each have at least two sign bits, the addition
4525 // cannot overflow.
4526 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4527 return OFK_Never;
4528
4529 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4530 return OFK_Sometime;
4531}
4532
4535 // X + 0 never overflow
4536 if (isNullConstant(N1))
4537 return OFK_Never;
4538
4539 // mulhi + 1 never overflow
4540 KnownBits N1Known = computeKnownBits(N1);
4541 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4542 N1Known.getMaxValue().ult(2))
4543 return OFK_Never;
4544
4545 KnownBits N0Known = computeKnownBits(N0);
4546 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4547 N0Known.getMaxValue().ult(2))
4548 return OFK_Never;
4549
4550 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4551 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4552 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4553 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4554}
4555
4558 // X - 0 never overflow
4559 if (isNullConstant(N1))
4560 return OFK_Never;
4561
4562 // If both operands each have at least two sign bits, the subtraction
4563 // cannot overflow.
4564 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4565 return OFK_Never;
4566
4567 KnownBits N0Known = computeKnownBits(N0);
4568 KnownBits N1Known = computeKnownBits(N1);
4569 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4570 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4571 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4572}
4573
4576 // X - 0 never overflow
4577 if (isNullConstant(N1))
4578 return OFK_Never;
4579
4580 KnownBits N0Known = computeKnownBits(N0);
4581 KnownBits N1Known = computeKnownBits(N1);
4582 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4583 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4584 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4585}
4586
4589 // X * 0 and X * 1 never overflow.
4590 if (isNullConstant(N1) || isOneConstant(N1))
4591 return OFK_Never;
4592
4593 KnownBits N0Known = computeKnownBits(N0);
4594 KnownBits N1Known = computeKnownBits(N1);
4595 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4596 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4597 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4598}
4599
4602 // X * 0 and X * 1 never overflow.
4603 if (isNullConstant(N1) || isOneConstant(N1))
4604 return OFK_Never;
4605
4606 // Get the size of the result.
4607 unsigned BitWidth = N0.getScalarValueSizeInBits();
4608
4609 // Sum of the sign bits.
4610 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4611
4612 // If we have enough sign bits, then there's no overflow.
4613 if (SignBits > BitWidth + 1)
4614 return OFK_Never;
4615
4616 if (SignBits == BitWidth + 1) {
4617 // The overflow occurs when the true multiplication of the
4618 // the operands is the minimum negative number.
4619 KnownBits N0Known = computeKnownBits(N0);
4620 KnownBits N1Known = computeKnownBits(N1);
4621 // If one of the operands is non-negative, then there's no
4622 // overflow.
4623 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4624 return OFK_Never;
4625 }
4626
4627 return OFK_Sometime;
4628}
4629
4631 if (Depth >= MaxRecursionDepth)
4632 return false; // Limit search depth.
4633
4634 EVT OpVT = Val.getValueType();
4635 unsigned BitWidth = OpVT.getScalarSizeInBits();
4636
4637 // Is the constant a known power of 2?
4639 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4640 }))
4641 return true;
4642
4643 // A left-shift of a constant one will have exactly one bit set because
4644 // shifting the bit off the end is undefined.
4645 if (Val.getOpcode() == ISD::SHL) {
4646 auto *C = isConstOrConstSplat(Val.getOperand(0));
4647 if (C && C->getAPIntValue() == 1)
4648 return true;
4649 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4650 isKnownNeverZero(Val, Depth);
4651 }
4652
4653 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4654 // one bit set.
4655 if (Val.getOpcode() == ISD::SRL) {
4656 auto *C = isConstOrConstSplat(Val.getOperand(0));
4657 if (C && C->getAPIntValue().isSignMask())
4658 return true;
4659 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4660 isKnownNeverZero(Val, Depth);
4661 }
4662
4663 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4664 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4665
4666 // Are all operands of a build vector constant powers of two?
4667 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4668 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4669 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4670 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4671 return false;
4672 }))
4673 return true;
4674
4675 // Is the operand of a splat vector a constant power of two?
4676 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4678 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4679 return true;
4680
4681 // vscale(power-of-two) is a power-of-two for some targets
4682 if (Val.getOpcode() == ISD::VSCALE &&
4683 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4685 return true;
4686
4687 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4688 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4689 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4691
4692 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4693 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4695
4696 // Looking for `x & -x` pattern:
4697 // If x == 0:
4698 // x & -x -> 0
4699 // If x != 0:
4700 // x & -x -> non-zero pow2
4701 // so if we find the pattern return whether we know `x` is non-zero.
4702 SDValue X;
4703 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4704 return isKnownNeverZero(X, Depth);
4705
4706 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4707 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4708
4709 // More could be done here, though the above checks are enough
4710 // to handle some common cases.
4711 return false;
4712}
4713
4715 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4716 return C1->getValueAPF().getExactLog2Abs() >= 0;
4717
4718 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4719 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4720
4721 return false;
4722}
4723
4725 EVT VT = Op.getValueType();
4726
4727 // Since the number of lanes in a scalable vector is unknown at compile time,
4728 // we track one bit which is implicitly broadcast to all lanes. This means
4729 // that all lanes in a scalable vector are considered demanded.
4730 APInt DemandedElts = VT.isFixedLengthVector()
4732 : APInt(1, 1);
4733 return ComputeNumSignBits(Op, DemandedElts, Depth);
4734}
4735
4736unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4737 unsigned Depth) const {
4738 EVT VT = Op.getValueType();
4739 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4740 unsigned VTBits = VT.getScalarSizeInBits();
4741 unsigned NumElts = DemandedElts.getBitWidth();
4742 unsigned Tmp, Tmp2;
4743 unsigned FirstAnswer = 1;
4744
4745 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4746 const APInt &Val = C->getAPIntValue();
4747 return Val.getNumSignBits();
4748 }
4749
4750 if (Depth >= MaxRecursionDepth)
4751 return 1; // Limit search depth.
4752
4753 if (!DemandedElts)
4754 return 1; // No demanded elts, better to assume we don't know anything.
4755
4756 unsigned Opcode = Op.getOpcode();
4757 switch (Opcode) {
4758 default: break;
4759 case ISD::AssertSext:
4760 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4761 return VTBits-Tmp+1;
4762 case ISD::AssertZext:
4763 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4764 return VTBits-Tmp;
4765 case ISD::FREEZE:
4766 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4767 /*PoisonOnly=*/false))
4768 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4769 break;
4770 case ISD::MERGE_VALUES:
4771 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4772 Depth + 1);
4773 case ISD::SPLAT_VECTOR: {
4774 // Check if the sign bits of source go down as far as the truncated value.
4775 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4776 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4777 if (NumSrcSignBits > (NumSrcBits - VTBits))
4778 return NumSrcSignBits - (NumSrcBits - VTBits);
4779 break;
4780 }
4781 case ISD::BUILD_VECTOR:
4782 assert(!VT.isScalableVector());
4783 Tmp = VTBits;
4784 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4785 if (!DemandedElts[i])
4786 continue;
4787
4788 SDValue SrcOp = Op.getOperand(i);
4789 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4790 // for constant nodes to ensure we only look at the sign bits.
4792 APInt T = C->getAPIntValue().trunc(VTBits);
4793 Tmp2 = T.getNumSignBits();
4794 } else {
4795 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4796
4797 if (SrcOp.getValueSizeInBits() != VTBits) {
4798 assert(SrcOp.getValueSizeInBits() > VTBits &&
4799 "Expected BUILD_VECTOR implicit truncation");
4800 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4801 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4802 }
4803 }
4804 Tmp = std::min(Tmp, Tmp2);
4805 }
4806 return Tmp;
4807
4808 case ISD::VECTOR_COMPRESS: {
4809 SDValue Vec = Op.getOperand(0);
4810 SDValue PassThru = Op.getOperand(2);
4811 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4812 if (Tmp == 1)
4813 return 1;
4814 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4815 Tmp = std::min(Tmp, Tmp2);
4816 return Tmp;
4817 }
4818
4819 case ISD::VECTOR_SHUFFLE: {
4820 // Collect the minimum number of sign bits that are shared by every vector
4821 // element referenced by the shuffle.
4822 APInt DemandedLHS, DemandedRHS;
4824 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4825 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4826 DemandedLHS, DemandedRHS))
4827 return 1;
4828
4829 Tmp = std::numeric_limits<unsigned>::max();
4830 if (!!DemandedLHS)
4831 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4832 if (!!DemandedRHS) {
4833 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4834 Tmp = std::min(Tmp, Tmp2);
4835 }
4836 // If we don't know anything, early out and try computeKnownBits fall-back.
4837 if (Tmp == 1)
4838 break;
4839 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4840 return Tmp;
4841 }
4842
4843 case ISD::BITCAST: {
4844 if (VT.isScalableVector())
4845 break;
4846 SDValue N0 = Op.getOperand(0);
4847 EVT SrcVT = N0.getValueType();
4848 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4849
4850 // Ignore bitcasts from unsupported types..
4851 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4852 break;
4853
4854 // Fast handling of 'identity' bitcasts.
4855 if (VTBits == SrcBits)
4856 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4857
4858 bool IsLE = getDataLayout().isLittleEndian();
4859
4860 // Bitcast 'large element' scalar/vector to 'small element' vector.
4861 if ((SrcBits % VTBits) == 0) {
4862 assert(VT.isVector() && "Expected bitcast to vector");
4863
4864 unsigned Scale = SrcBits / VTBits;
4865 APInt SrcDemandedElts =
4866 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4867
4868 // Fast case - sign splat can be simply split across the small elements.
4869 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4870 if (Tmp == SrcBits)
4871 return VTBits;
4872
4873 // Slow case - determine how far the sign extends into each sub-element.
4874 Tmp2 = VTBits;
4875 for (unsigned i = 0; i != NumElts; ++i)
4876 if (DemandedElts[i]) {
4877 unsigned SubOffset = i % Scale;
4878 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4879 SubOffset = SubOffset * VTBits;
4880 if (Tmp <= SubOffset)
4881 return 1;
4882 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4883 }
4884 return Tmp2;
4885 }
4886 break;
4887 }
4888
4890 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4891 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4892 return VTBits - Tmp + 1;
4893 case ISD::SIGN_EXTEND:
4894 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4895 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4897 // Max of the input and what this extends.
4898 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4899 Tmp = VTBits-Tmp+1;
4900 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4901 return std::max(Tmp, Tmp2);
4903 if (VT.isScalableVector())
4904 break;
4905 SDValue Src = Op.getOperand(0);
4906 EVT SrcVT = Src.getValueType();
4907 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4908 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4909 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4910 }
4911 case ISD::SRA:
4912 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4913 // SRA X, C -> adds C sign bits.
4914 if (std::optional<unsigned> ShAmt =
4915 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4916 Tmp = std::min(Tmp + *ShAmt, VTBits);
4917 return Tmp;
4918 case ISD::SHL:
4919 if (std::optional<ConstantRange> ShAmtRange =
4920 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4921 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4922 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4923 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4924 // shifted out, then we can compute the number of sign bits for the
4925 // operand being extended. A future improvement could be to pass along the
4926 // "shifted left by" information in the recursive calls to
4927 // ComputeKnownSignBits. Allowing us to handle this more generically.
4928 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4929 SDValue Ext = Op.getOperand(0);
4930 EVT ExtVT = Ext.getValueType();
4931 SDValue Extendee = Ext.getOperand(0);
4932 EVT ExtendeeVT = Extendee.getValueType();
4933 unsigned SizeDifference =
4934 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4935 if (SizeDifference <= MinShAmt) {
4936 Tmp = SizeDifference +
4937 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4938 if (MaxShAmt < Tmp)
4939 return Tmp - MaxShAmt;
4940 }
4941 }
4942 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4943 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4944 if (MaxShAmt < Tmp)
4945 return Tmp - MaxShAmt;
4946 }
4947 break;
4948 case ISD::AND:
4949 case ISD::OR:
4950 case ISD::XOR: // NOT is handled here.
4951 // Logical binary ops preserve the number of sign bits at the worst.
4952 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4953 if (Tmp != 1) {
4954 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4955 FirstAnswer = std::min(Tmp, Tmp2);
4956 // We computed what we know about the sign bits as our first
4957 // answer. Now proceed to the generic code that uses
4958 // computeKnownBits, and pick whichever answer is better.
4959 }
4960 break;
4961
4962 case ISD::SELECT:
4963 case ISD::VSELECT:
4964 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4965 if (Tmp == 1) return 1; // Early out.
4966 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4967 return std::min(Tmp, Tmp2);
4968 case ISD::SELECT_CC:
4969 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4970 if (Tmp == 1) return 1; // Early out.
4971 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4972 return std::min(Tmp, Tmp2);
4973
4974 case ISD::SMIN:
4975 case ISD::SMAX: {
4976 // If we have a clamp pattern, we know that the number of sign bits will be
4977 // the minimum of the clamp min/max range.
4978 bool IsMax = (Opcode == ISD::SMAX);
4979 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4980 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4981 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4982 CstHigh =
4983 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4984 if (CstLow && CstHigh) {
4985 if (!IsMax)
4986 std::swap(CstLow, CstHigh);
4987 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4988 Tmp = CstLow->getAPIntValue().getNumSignBits();
4989 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4990 return std::min(Tmp, Tmp2);
4991 }
4992 }
4993
4994 // Fallback - just get the minimum number of sign bits of the operands.
4995 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4996 if (Tmp == 1)
4997 return 1; // Early out.
4998 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4999 return std::min(Tmp, Tmp2);
5000 }
5001 case ISD::UMIN:
5002 case ISD::UMAX:
5003 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5004 if (Tmp == 1)
5005 return 1; // Early out.
5006 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5007 return std::min(Tmp, Tmp2);
5008 case ISD::SSUBO_CARRY:
5009 case ISD::USUBO_CARRY:
5010 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5011 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5012 return VTBits;
5013 [[fallthrough]];
5014 case ISD::SADDO:
5015 case ISD::UADDO:
5016 case ISD::SADDO_CARRY:
5017 case ISD::UADDO_CARRY:
5018 case ISD::SSUBO:
5019 case ISD::USUBO:
5020 case ISD::SMULO:
5021 case ISD::UMULO:
5022 if (Op.getResNo() != 1)
5023 break;
5024 // The boolean result conforms to getBooleanContents. Fall through.
5025 // If setcc returns 0/-1, all bits are sign bits.
5026 // We know that we have an integer-based boolean since these operations
5027 // are only available for integer.
5028 if (TLI->getBooleanContents(VT.isVector(), false) ==
5030 return VTBits;
5031 break;
5032 case ISD::SETCC:
5033 case ISD::SETCCCARRY:
5034 case ISD::STRICT_FSETCC:
5035 case ISD::STRICT_FSETCCS: {
5036 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5037 // If setcc returns 0/-1, all bits are sign bits.
5038 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5040 return VTBits;
5041 break;
5042 }
5043 case ISD::ROTL:
5044 case ISD::ROTR:
5045 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5046
5047 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5048 if (Tmp == VTBits)
5049 return VTBits;
5050
5051 if (ConstantSDNode *C =
5052 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5053 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5054
5055 // Handle rotate right by N like a rotate left by 32-N.
5056 if (Opcode == ISD::ROTR)
5057 RotAmt = (VTBits - RotAmt) % VTBits;
5058
5059 // If we aren't rotating out all of the known-in sign bits, return the
5060 // number that are left. This handles rotl(sext(x), 1) for example.
5061 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5062 }
5063 break;
5064 case ISD::ADD:
5065 case ISD::ADDC:
5066 // TODO: Move Operand 1 check before Operand 0 check
5067 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5068 if (Tmp == 1) return 1; // Early out.
5069
5070 // Special case decrementing a value (ADD X, -1):
5071 if (ConstantSDNode *CRHS =
5072 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5073 if (CRHS->isAllOnes()) {
5074 KnownBits Known =
5075 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5076
5077 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5078 // sign bits set.
5079 if ((Known.Zero | 1).isAllOnes())
5080 return VTBits;
5081
5082 // If we are subtracting one from a positive number, there is no carry
5083 // out of the result.
5084 if (Known.isNonNegative())
5085 return Tmp;
5086 }
5087
5088 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5089 if (Tmp2 == 1) return 1; // Early out.
5090
5091 // Add can have at most one carry bit. Thus we know that the output
5092 // is, at worst, one more bit than the inputs.
5093 return std::min(Tmp, Tmp2) - 1;
5094 case ISD::SUB:
5095 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5096 if (Tmp2 == 1) return 1; // Early out.
5097
5098 // Handle NEG.
5099 if (ConstantSDNode *CLHS =
5100 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5101 if (CLHS->isZero()) {
5102 KnownBits Known =
5103 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5104 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5105 // sign bits set.
5106 if ((Known.Zero | 1).isAllOnes())
5107 return VTBits;
5108
5109 // If the input is known to be positive (the sign bit is known clear),
5110 // the output of the NEG has the same number of sign bits as the input.
5111 if (Known.isNonNegative())
5112 return Tmp2;
5113
5114 // Otherwise, we treat this like a SUB.
5115 }
5116
5117 // Sub can have at most one carry bit. Thus we know that the output
5118 // is, at worst, one more bit than the inputs.
5119 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5120 if (Tmp == 1) return 1; // Early out.
5121 return std::min(Tmp, Tmp2) - 1;
5122 case ISD::MUL: {
5123 // The output of the Mul can be at most twice the valid bits in the inputs.
5124 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5125 if (SignBitsOp0 == 1)
5126 break;
5127 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5128 if (SignBitsOp1 == 1)
5129 break;
5130 unsigned OutValidBits =
5131 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5132 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5133 }
5134 case ISD::AVGCEILS:
5135 case ISD::AVGFLOORS:
5136 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5137 if (Tmp == 1)
5138 return 1; // Early out.
5139 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5140 return std::min(Tmp, Tmp2);
5141 case ISD::SREM:
5142 // The sign bit is the LHS's sign bit, except when the result of the
5143 // remainder is zero. The magnitude of the result should be less than or
5144 // equal to the magnitude of the LHS. Therefore, the result should have
5145 // at least as many sign bits as the left hand side.
5146 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5147 case ISD::TRUNCATE: {
5148 // Check if the sign bits of source go down as far as the truncated value.
5149 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5150 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5151 if (NumSrcSignBits > (NumSrcBits - VTBits))
5152 return NumSrcSignBits - (NumSrcBits - VTBits);
5153 break;
5154 }
5155 case ISD::EXTRACT_ELEMENT: {
5156 if (VT.isScalableVector())
5157 break;
5158 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5159 const int BitWidth = Op.getValueSizeInBits();
5160 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5161
5162 // Get reverse index (starting from 1), Op1 value indexes elements from
5163 // little end. Sign starts at big end.
5164 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5165
5166 // If the sign portion ends in our element the subtraction gives correct
5167 // result. Otherwise it gives either negative or > bitwidth result
5168 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5169 }
5171 if (VT.isScalableVector())
5172 break;
5173 // If we know the element index, split the demand between the
5174 // source vector and the inserted element, otherwise assume we need
5175 // the original demanded vector elements and the value.
5176 SDValue InVec = Op.getOperand(0);
5177 SDValue InVal = Op.getOperand(1);
5178 SDValue EltNo = Op.getOperand(2);
5179 bool DemandedVal = true;
5180 APInt DemandedVecElts = DemandedElts;
5181 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5182 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5183 unsigned EltIdx = CEltNo->getZExtValue();
5184 DemandedVal = !!DemandedElts[EltIdx];
5185 DemandedVecElts.clearBit(EltIdx);
5186 }
5187 Tmp = std::numeric_limits<unsigned>::max();
5188 if (DemandedVal) {
5189 // TODO - handle implicit truncation of inserted elements.
5190 if (InVal.getScalarValueSizeInBits() != VTBits)
5191 break;
5192 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5193 Tmp = std::min(Tmp, Tmp2);
5194 }
5195 if (!!DemandedVecElts) {
5196 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5197 Tmp = std::min(Tmp, Tmp2);
5198 }
5199 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5200 return Tmp;
5201 }
5203 assert(!VT.isScalableVector());
5204 SDValue InVec = Op.getOperand(0);
5205 SDValue EltNo = Op.getOperand(1);
5206 EVT VecVT = InVec.getValueType();
5207 // ComputeNumSignBits not yet implemented for scalable vectors.
5208 if (VecVT.isScalableVector())
5209 break;
5210 const unsigned BitWidth = Op.getValueSizeInBits();
5211 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5212 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5213
5214 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5215 // anything about sign bits. But if the sizes match we can derive knowledge
5216 // about sign bits from the vector operand.
5217 if (BitWidth != EltBitWidth)
5218 break;
5219
5220 // If we know the element index, just demand that vector element, else for
5221 // an unknown element index, ignore DemandedElts and demand them all.
5222 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5223 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5224 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5225 DemandedSrcElts =
5226 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5227
5228 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5229 }
5231 // Offset the demanded elts by the subvector index.
5232 SDValue Src = Op.getOperand(0);
5233 // Bail until we can represent demanded elements for scalable vectors.
5234 if (Src.getValueType().isScalableVector())
5235 break;
5236 uint64_t Idx = Op.getConstantOperandVal(1);
5237 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5238 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5239 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5240 }
5241 case ISD::CONCAT_VECTORS: {
5242 if (VT.isScalableVector())
5243 break;
5244 // Determine the minimum number of sign bits across all demanded
5245 // elts of the input vectors. Early out if the result is already 1.
5246 Tmp = std::numeric_limits<unsigned>::max();
5247 EVT SubVectorVT = Op.getOperand(0).getValueType();
5248 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5249 unsigned NumSubVectors = Op.getNumOperands();
5250 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5251 APInt DemandedSub =
5252 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5253 if (!DemandedSub)
5254 continue;
5255 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5256 Tmp = std::min(Tmp, Tmp2);
5257 }
5258 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5259 return Tmp;
5260 }
5261 case ISD::INSERT_SUBVECTOR: {
5262 if (VT.isScalableVector())
5263 break;
5264 // Demand any elements from the subvector and the remainder from the src its
5265 // inserted into.
5266 SDValue Src = Op.getOperand(0);
5267 SDValue Sub = Op.getOperand(1);
5268 uint64_t Idx = Op.getConstantOperandVal(2);
5269 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5270 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5271 APInt DemandedSrcElts = DemandedElts;
5272 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5273
5274 Tmp = std::numeric_limits<unsigned>::max();
5275 if (!!DemandedSubElts) {
5276 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5277 if (Tmp == 1)
5278 return 1; // early-out
5279 }
5280 if (!!DemandedSrcElts) {
5281 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5282 Tmp = std::min(Tmp, Tmp2);
5283 }
5284 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5285 return Tmp;
5286 }
5287 case ISD::LOAD: {
5289 if (const MDNode *Ranges = LD->getRanges()) {
5290 if (DemandedElts != 1)
5291 break;
5292
5294 if (VTBits > CR.getBitWidth()) {
5295 switch (LD->getExtensionType()) {
5296 case ISD::SEXTLOAD:
5297 CR = CR.signExtend(VTBits);
5298 break;
5299 case ISD::ZEXTLOAD:
5300 CR = CR.zeroExtend(VTBits);
5301 break;
5302 default:
5303 break;
5304 }
5305 }
5306
5307 if (VTBits != CR.getBitWidth())
5308 break;
5309 return std::min(CR.getSignedMin().getNumSignBits(),
5311 }
5312
5313 break;
5314 }
5315 case ISD::ATOMIC_CMP_SWAP:
5316 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
5317 case ISD::ATOMIC_SWAP:
5318 case ISD::ATOMIC_LOAD_ADD:
5319 case ISD::ATOMIC_LOAD_SUB:
5320 case ISD::ATOMIC_LOAD_AND:
5321 case ISD::ATOMIC_LOAD_CLR:
5322 case ISD::ATOMIC_LOAD_OR:
5323 case ISD::ATOMIC_LOAD_XOR:
5324 case ISD::ATOMIC_LOAD_NAND:
5325 case ISD::ATOMIC_LOAD_MIN:
5326 case ISD::ATOMIC_LOAD_MAX:
5327 case ISD::ATOMIC_LOAD_UMIN:
5328 case ISD::ATOMIC_LOAD_UMAX:
5329 case ISD::ATOMIC_LOAD: {
5330 auto *AT = cast<AtomicSDNode>(Op);
5331 // If we are looking at the loaded value.
5332 if (Op.getResNo() == 0) {
5333 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5334 if (Tmp == VTBits)
5335 return 1; // early-out
5336
5337 // For atomic_load, prefer to use the extension type.
5338 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5339 switch (AT->getExtensionType()) {
5340 default:
5341 break;
5342 case ISD::SEXTLOAD:
5343 return VTBits - Tmp + 1;
5344 case ISD::ZEXTLOAD:
5345 return VTBits - Tmp;
5346 }
5347 }
5348
5349 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5350 return VTBits - Tmp + 1;
5351 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5352 return VTBits - Tmp;
5353 }
5354 break;
5355 }
5356 }
5357
5358 // If we are looking at the loaded value of the SDNode.
5359 if (Op.getResNo() == 0) {
5360 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5361 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5362 unsigned ExtType = LD->getExtensionType();
5363 switch (ExtType) {
5364 default: break;
5365 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5366 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5367 return VTBits - Tmp + 1;
5368 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5369 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5370 return VTBits - Tmp;
5371 case ISD::NON_EXTLOAD:
5372 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5373 // We only need to handle vectors - computeKnownBits should handle
5374 // scalar cases.
5375 Type *CstTy = Cst->getType();
5376 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5377 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5378 VTBits == CstTy->getScalarSizeInBits()) {
5379 Tmp = VTBits;
5380 for (unsigned i = 0; i != NumElts; ++i) {
5381 if (!DemandedElts[i])
5382 continue;
5383 if (Constant *Elt = Cst->getAggregateElement(i)) {
5384 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5385 const APInt &Value = CInt->getValue();
5386 Tmp = std::min(Tmp, Value.getNumSignBits());
5387 continue;
5388 }
5389 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5390 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5391 Tmp = std::min(Tmp, Value.getNumSignBits());
5392 continue;
5393 }
5394 }
5395 // Unknown type. Conservatively assume no bits match sign bit.
5396 return 1;
5397 }
5398 return Tmp;
5399 }
5400 }
5401 break;
5402 }
5403 }
5404 }
5405
5406 // Allow the target to implement this method for its nodes.
5407 if (Opcode >= ISD::BUILTIN_OP_END ||
5408 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5409 Opcode == ISD::INTRINSIC_W_CHAIN ||
5410 Opcode == ISD::INTRINSIC_VOID) {
5411 // TODO: This can probably be removed once target code is audited. This
5412 // is here purely to reduce patch size and review complexity.
5413 if (!VT.isScalableVector()) {
5414 unsigned NumBits =
5415 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5416 if (NumBits > 1)
5417 FirstAnswer = std::max(FirstAnswer, NumBits);
5418 }
5419 }
5420
5421 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5422 // use this information.
5423 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5424 return std::max(FirstAnswer, Known.countMinSignBits());
5425}
5426
5428 unsigned Depth) const {
5429 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5430 return Op.getScalarValueSizeInBits() - SignBits + 1;
5431}
5432
5434 const APInt &DemandedElts,
5435 unsigned Depth) const {
5436 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5437 return Op.getScalarValueSizeInBits() - SignBits + 1;
5438}
5439
5441 unsigned Depth) const {
5442 // Early out for FREEZE.
5443 if (Op.getOpcode() == ISD::FREEZE)
5444 return true;
5445
5446 EVT VT = Op.getValueType();
5447 APInt DemandedElts = VT.isFixedLengthVector()
5449 : APInt(1, 1);
5450 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5451}
5452
5454 const APInt &DemandedElts,
5455 bool PoisonOnly,
5456 unsigned Depth) const {
5457 unsigned Opcode = Op.getOpcode();
5458
5459 // Early out for FREEZE.
5460 if (Opcode == ISD::FREEZE)
5461 return true;
5462
5463 if (Depth >= MaxRecursionDepth)
5464 return false; // Limit search depth.
5465
5466 if (isIntOrFPConstant(Op))
5467 return true;
5468
5469 switch (Opcode) {
5470 case ISD::CONDCODE:
5471 case ISD::VALUETYPE:
5472 case ISD::FrameIndex:
5474 case ISD::CopyFromReg:
5475 return true;
5476
5477 case ISD::POISON:
5478 return false;
5479
5480 case ISD::UNDEF:
5481 return PoisonOnly;
5482
5483 case ISD::BUILD_VECTOR:
5484 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5485 // this shouldn't affect the result.
5486 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5487 if (!DemandedElts[i])
5488 continue;
5490 Depth + 1))
5491 return false;
5492 }
5493 return true;
5494
5496 SDValue Src = Op.getOperand(0);
5497 if (Src.getValueType().isScalableVector())
5498 break;
5499 uint64_t Idx = Op.getConstantOperandVal(1);
5500 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5501 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5502 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5503 Depth + 1);
5504 }
5505
5506 case ISD::INSERT_SUBVECTOR: {
5507 if (Op.getValueType().isScalableVector())
5508 break;
5509 SDValue Src = Op.getOperand(0);
5510 SDValue Sub = Op.getOperand(1);
5511 uint64_t Idx = Op.getConstantOperandVal(2);
5512 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5513 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5514 APInt DemandedSrcElts = DemandedElts;
5515 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5516
5517 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5518 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5519 return false;
5520 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5521 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5522 return false;
5523 return true;
5524 }
5525
5527 SDValue Src = Op.getOperand(0);
5528 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5529 EVT SrcVT = Src.getValueType();
5530 if (SrcVT.isFixedLengthVector() && IndexC &&
5531 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5532 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5533 IndexC->getZExtValue());
5534 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5535 Depth + 1);
5536 }
5537 break;
5538 }
5539
5541 SDValue InVec = Op.getOperand(0);
5542 SDValue InVal = Op.getOperand(1);
5543 SDValue EltNo = Op.getOperand(2);
5544 EVT VT = InVec.getValueType();
5545 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5546 if (IndexC && VT.isFixedLengthVector() &&
5547 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5548 if (DemandedElts[IndexC->getZExtValue()] &&
5550 return false;
5551 APInt InVecDemandedElts = DemandedElts;
5552 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5553 if (!!InVecDemandedElts &&
5555 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5556 InVecDemandedElts, PoisonOnly, Depth + 1))
5557 return false;
5558 return true;
5559 }
5560 break;
5561 }
5562
5564 // Check upper (known undef) elements.
5565 if (DemandedElts.ugt(1) && !PoisonOnly)
5566 return false;
5567 // Check element zero.
5568 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5569 Op.getOperand(0), PoisonOnly, Depth + 1))
5570 return false;
5571 return true;
5572
5573 case ISD::SPLAT_VECTOR:
5574 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5575 Depth + 1);
5576
5577 case ISD::VECTOR_SHUFFLE: {
5578 APInt DemandedLHS, DemandedRHS;
5579 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5580 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5581 DemandedElts, DemandedLHS, DemandedRHS,
5582 /*AllowUndefElts=*/false))
5583 return false;
5584 if (!DemandedLHS.isZero() &&
5585 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5586 PoisonOnly, Depth + 1))
5587 return false;
5588 if (!DemandedRHS.isZero() &&
5589 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5590 PoisonOnly, Depth + 1))
5591 return false;
5592 return true;
5593 }
5594
5595 case ISD::SHL:
5596 case ISD::SRL:
5597 case ISD::SRA:
5598 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5599 // enough to check operand 0 if Op can't create undef/poison.
5600 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5601 /*ConsiderFlags*/ true, Depth) &&
5602 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5603 PoisonOnly, Depth + 1);
5604
5605 case ISD::BSWAP:
5606 case ISD::CTPOP:
5607 case ISD::BITREVERSE:
5608 case ISD::AND:
5609 case ISD::OR:
5610 case ISD::XOR:
5611 case ISD::ADD:
5612 case ISD::SUB:
5613 case ISD::MUL:
5614 case ISD::SADDSAT:
5615 case ISD::UADDSAT:
5616 case ISD::SSUBSAT:
5617 case ISD::USUBSAT:
5618 case ISD::SSHLSAT:
5619 case ISD::USHLSAT:
5620 case ISD::SMIN:
5621 case ISD::SMAX:
5622 case ISD::UMIN:
5623 case ISD::UMAX:
5624 case ISD::ZERO_EXTEND:
5625 case ISD::SIGN_EXTEND:
5626 case ISD::ANY_EXTEND:
5627 case ISD::TRUNCATE:
5628 case ISD::VSELECT: {
5629 // If Op can't create undef/poison and none of its operands are undef/poison
5630 // then Op is never undef/poison. A difference from the more common check
5631 // below, outside the switch, is that we handle elementwise operations for
5632 // which the DemandedElts mask is valid for all operands here.
5633 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5634 /*ConsiderFlags*/ true, Depth) &&
5635 all_of(Op->ops(), [&](SDValue V) {
5636 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5637 PoisonOnly, Depth + 1);
5638 });
5639 }
5640
5641 // TODO: Search for noundef attributes from library functions.
5642
5643 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5644
5645 default:
5646 // Allow the target to implement this method for its nodes.
5647 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5648 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5649 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5650 Op, DemandedElts, *this, PoisonOnly, Depth);
5651 break;
5652 }
5653
5654 // If Op can't create undef/poison and none of its operands are undef/poison
5655 // then Op is never undef/poison.
5656 // NOTE: TargetNodes can handle this in themselves in
5657 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5658 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5659 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5660 Depth) &&
5661 all_of(Op->ops(), [&](SDValue V) {
5662 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5663 });
5664}
5665
5667 bool ConsiderFlags,
5668 unsigned Depth) const {
5669 EVT VT = Op.getValueType();
5670 APInt DemandedElts = VT.isFixedLengthVector()
5672 : APInt(1, 1);
5673 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5674 Depth);
5675}
5676
5678 bool PoisonOnly, bool ConsiderFlags,
5679 unsigned Depth) const {
5680 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5681 return true;
5682
5683 unsigned Opcode = Op.getOpcode();
5684 switch (Opcode) {
5685 case ISD::AssertSext:
5686 case ISD::AssertZext:
5687 case ISD::AssertAlign:
5689 // Assertion nodes can create poison if the assertion fails.
5690 return true;
5691
5692 case ISD::FREEZE:
5696 case ISD::SADDSAT:
5697 case ISD::UADDSAT:
5698 case ISD::SSUBSAT:
5699 case ISD::USUBSAT:
5700 case ISD::MULHU:
5701 case ISD::MULHS:
5702 case ISD::AVGFLOORS:
5703 case ISD::AVGFLOORU:
5704 case ISD::AVGCEILS:
5705 case ISD::AVGCEILU:
5706 case ISD::ABDU:
5707 case ISD::ABDS:
5708 case ISD::SMIN:
5709 case ISD::SMAX:
5710 case ISD::SCMP:
5711 case ISD::UMIN:
5712 case ISD::UMAX:
5713 case ISD::UCMP:
5714 case ISD::AND:
5715 case ISD::XOR:
5716 case ISD::ROTL:
5717 case ISD::ROTR:
5718 case ISD::FSHL:
5719 case ISD::FSHR:
5720 case ISD::BSWAP:
5721 case ISD::CTTZ:
5722 case ISD::CTLZ:
5723 case ISD::CTPOP:
5724 case ISD::BITREVERSE:
5725 case ISD::PARITY:
5726 case ISD::SIGN_EXTEND:
5727 case ISD::TRUNCATE:
5731 case ISD::BITCAST:
5732 case ISD::BUILD_VECTOR:
5733 case ISD::BUILD_PAIR:
5734 case ISD::SPLAT_VECTOR:
5735 case ISD::FABS:
5736 return false;
5737
5738 case ISD::ABS:
5739 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5740 // Different to Intrinsic::abs.
5741 return false;
5742
5743 case ISD::ADDC:
5744 case ISD::SUBC:
5745 case ISD::ADDE:
5746 case ISD::SUBE:
5747 case ISD::SADDO:
5748 case ISD::SSUBO:
5749 case ISD::SMULO:
5750 case ISD::SADDO_CARRY:
5751 case ISD::SSUBO_CARRY:
5752 case ISD::UADDO:
5753 case ISD::USUBO:
5754 case ISD::UMULO:
5755 case ISD::UADDO_CARRY:
5756 case ISD::USUBO_CARRY:
5757 // No poison on result or overflow flags.
5758 return false;
5759
5760 case ISD::SELECT_CC:
5761 case ISD::SETCC: {
5762 // Integer setcc cannot create undef or poison.
5763 if (Op.getOperand(0).getValueType().isInteger())
5764 return false;
5765
5766 // FP compares are more complicated. They can create poison for nan/infinity
5767 // based on options and flags. The options and flags also cause special
5768 // nonan condition codes to be used. Those condition codes may be preserved
5769 // even if the nonan flag is dropped somewhere.
5770 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5771 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5772 return (unsigned)CCCode & 0x10U;
5773 }
5774
5775 case ISD::OR:
5776 case ISD::ZERO_EXTEND:
5777 case ISD::SELECT:
5778 case ISD::VSELECT:
5779 case ISD::ADD:
5780 case ISD::SUB:
5781 case ISD::MUL:
5782 case ISD::FNEG:
5783 case ISD::FADD:
5784 case ISD::FSUB:
5785 case ISD::FMUL:
5786 case ISD::FDIV:
5787 case ISD::FREM:
5788 case ISD::FCOPYSIGN:
5789 case ISD::FMA:
5790 case ISD::FMAD:
5791 case ISD::FMULADD:
5792 case ISD::FP_EXTEND:
5795 // No poison except from flags (which is handled above)
5796 return false;
5797
5798 case ISD::SHL:
5799 case ISD::SRL:
5800 case ISD::SRA:
5801 // If the max shift amount isn't in range, then the shift can
5802 // create poison.
5803 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5804
5807 // If the amount is zero then the result will be poison.
5808 // TODO: Add isKnownNeverZero DemandedElts handling.
5809 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5810
5812 // Check if we demand any upper (undef) elements.
5813 return !PoisonOnly && DemandedElts.ugt(1);
5814
5817 // Ensure that the element index is in bounds.
5818 EVT VecVT = Op.getOperand(0).getValueType();
5819 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5820 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5821 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5822 }
5823
5824 case ISD::VECTOR_SHUFFLE: {
5825 // Check for any demanded shuffle element that is undef.
5826 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5827 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5828 if (Elt < 0 && DemandedElts[Idx])
5829 return true;
5830 return false;
5831 }
5832
5833 default:
5834 // Allow the target to implement this method for its nodes.
5835 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5836 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5837 return TLI->canCreateUndefOrPoisonForTargetNode(
5838 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5839 break;
5840 }
5841
5842 // Be conservative and return true.
5843 return true;
5844}
5845
5846bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5847 unsigned Opcode = Op.getOpcode();
5848 if (Opcode == ISD::OR)
5849 return Op->getFlags().hasDisjoint() ||
5850 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5851 if (Opcode == ISD::XOR)
5852 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5853 return false;
5854}
5855
5857 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5858 (Op.isAnyAdd() || isADDLike(Op));
5859}
5860
5862 unsigned Depth) const {
5863 EVT VT = Op.getValueType();
5864
5865 // Since the number of lanes in a scalable vector is unknown at compile time,
5866 // we track one bit which is implicitly broadcast to all lanes. This means
5867 // that all lanes in a scalable vector are considered demanded.
5868 APInt DemandedElts = VT.isFixedLengthVector()
5870 : APInt(1, 1);
5871
5872 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5873}
5874
5876 bool SNaN, unsigned Depth) const {
5877 assert(!DemandedElts.isZero() && "No demanded elements");
5878
5879 // If we're told that NaNs won't happen, assume they won't.
5880 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5881 return true;
5882
5883 if (Depth >= MaxRecursionDepth)
5884 return false; // Limit search depth.
5885
5886 // If the value is a constant, we can obviously see if it is a NaN or not.
5888 return !C->getValueAPF().isNaN() ||
5889 (SNaN && !C->getValueAPF().isSignaling());
5890 }
5891
5892 unsigned Opcode = Op.getOpcode();
5893 switch (Opcode) {
5894 case ISD::FADD:
5895 case ISD::FSUB:
5896 case ISD::FMUL:
5897 case ISD::FDIV:
5898 case ISD::FREM:
5899 case ISD::FSIN:
5900 case ISD::FCOS:
5901 case ISD::FTAN:
5902 case ISD::FASIN:
5903 case ISD::FACOS:
5904 case ISD::FATAN:
5905 case ISD::FATAN2:
5906 case ISD::FSINH:
5907 case ISD::FCOSH:
5908 case ISD::FTANH:
5909 case ISD::FMA:
5910 case ISD::FMULADD:
5911 case ISD::FMAD: {
5912 if (SNaN)
5913 return true;
5914 // TODO: Need isKnownNeverInfinity
5915 return false;
5916 }
5917 case ISD::FCANONICALIZE:
5918 case ISD::FEXP:
5919 case ISD::FEXP2:
5920 case ISD::FEXP10:
5921 case ISD::FTRUNC:
5922 case ISD::FFLOOR:
5923 case ISD::FCEIL:
5924 case ISD::FROUND:
5925 case ISD::FROUNDEVEN:
5926 case ISD::LROUND:
5927 case ISD::LLROUND:
5928 case ISD::FRINT:
5929 case ISD::LRINT:
5930 case ISD::LLRINT:
5931 case ISD::FNEARBYINT:
5932 case ISD::FLDEXP: {
5933 if (SNaN)
5934 return true;
5935 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5936 }
5937 case ISD::FABS:
5938 case ISD::FNEG:
5939 case ISD::FCOPYSIGN: {
5940 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5941 }
5942 case ISD::SELECT:
5943 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5944 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5945 case ISD::FP_EXTEND:
5946 case ISD::FP_ROUND: {
5947 if (SNaN)
5948 return true;
5949 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5950 }
5951 case ISD::SINT_TO_FP:
5952 case ISD::UINT_TO_FP:
5953 return true;
5954 case ISD::FSQRT: // Need is known positive
5955 case ISD::FLOG:
5956 case ISD::FLOG2:
5957 case ISD::FLOG10:
5958 case ISD::FPOWI:
5959 case ISD::FPOW: {
5960 if (SNaN)
5961 return true;
5962 // TODO: Refine on operand
5963 return false;
5964 }
5965 case ISD::FMINNUM:
5966 case ISD::FMAXNUM:
5967 case ISD::FMINIMUMNUM:
5968 case ISD::FMAXIMUMNUM: {
5969 // Only one needs to be known not-nan, since it will be returned if the
5970 // other ends up being one.
5971 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5972 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5973 }
5974 case ISD::FMINNUM_IEEE:
5975 case ISD::FMAXNUM_IEEE: {
5976 if (SNaN)
5977 return true;
5978 // This can return a NaN if either operand is an sNaN, or if both operands
5979 // are NaN.
5980 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5981 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5982 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5983 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5984 }
5985 case ISD::FMINIMUM:
5986 case ISD::FMAXIMUM: {
5987 // TODO: Does this quiet or return the origina NaN as-is?
5988 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5989 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5990 }
5992 SDValue Src = Op.getOperand(0);
5993 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5994 EVT SrcVT = Src.getValueType();
5995 if (SrcVT.isFixedLengthVector() && Idx &&
5996 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5997 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5998 Idx->getZExtValue());
5999 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6000 }
6001 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6002 }
6004 SDValue Src = Op.getOperand(0);
6005 if (Src.getValueType().isFixedLengthVector()) {
6006 unsigned Idx = Op.getConstantOperandVal(1);
6007 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
6008 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
6009 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6010 }
6011 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6012 }
6013 case ISD::INSERT_SUBVECTOR: {
6014 SDValue BaseVector = Op.getOperand(0);
6015 SDValue SubVector = Op.getOperand(1);
6016 EVT BaseVectorVT = BaseVector.getValueType();
6017 if (BaseVectorVT.isFixedLengthVector()) {
6018 unsigned Idx = Op.getConstantOperandVal(2);
6019 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6020 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6021
6022 // Clear/Extract the bits at the position where the subvector will be
6023 // inserted.
6024 APInt DemandedMask =
6025 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6026 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6027 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6028
6029 bool NeverNaN = true;
6030 if (!DemandedSrcElts.isZero())
6031 NeverNaN &=
6032 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6033 if (NeverNaN && !DemandedSubElts.isZero())
6034 NeverNaN &=
6035 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6036 return NeverNaN;
6037 }
6038 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6039 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6040 }
6041 case ISD::BUILD_VECTOR: {
6042 unsigned NumElts = Op.getNumOperands();
6043 for (unsigned I = 0; I != NumElts; ++I)
6044 if (DemandedElts[I] &&
6045 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6046 return false;
6047 return true;
6048 }
6049 case ISD::AssertNoFPClass: {
6050 FPClassTest NoFPClass =
6051 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6052 if ((NoFPClass & fcNan) == fcNan)
6053 return true;
6054 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6055 return true;
6056 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6057 }
6058 default:
6059 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6060 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6061 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6062 Depth);
6063 }
6064
6065 return false;
6066 }
6067}
6068
6070 assert(Op.getValueType().isFloatingPoint() &&
6071 "Floating point type expected");
6072
6073 // If the value is a constant, we can obviously see if it is a zero or not.
6075 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6076}
6077
6079 if (Depth >= MaxRecursionDepth)
6080 return false; // Limit search depth.
6081
6082 assert(!Op.getValueType().isFloatingPoint() &&
6083 "Floating point types unsupported - use isKnownNeverZeroFloat");
6084
6085 // If the value is a constant, we can obviously see if it is a zero or not.
6087 [](ConstantSDNode *C) { return !C->isZero(); }))
6088 return true;
6089
6090 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6091 // some degree.
6092 switch (Op.getOpcode()) {
6093 default:
6094 break;
6095
6096 case ISD::OR:
6097 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6098 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6099
6100 case ISD::VSELECT:
6101 case ISD::SELECT:
6102 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6103 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6104
6105 case ISD::SHL: {
6106 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6107 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6108 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6109 // 1 << X is never zero.
6110 if (ValKnown.One[0])
6111 return true;
6112 // If max shift cnt of known ones is non-zero, result is non-zero.
6113 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6114 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6115 !ValKnown.One.shl(MaxCnt).isZero())
6116 return true;
6117 break;
6118 }
6119 case ISD::UADDSAT:
6120 case ISD::UMAX:
6121 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6122 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6123
6124 // For smin/smax: If either operand is known negative/positive
6125 // respectively we don't need the other to be known at all.
6126 case ISD::SMAX: {
6127 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6128 if (Op1.isStrictlyPositive())
6129 return true;
6130
6131 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6132 if (Op0.isStrictlyPositive())
6133 return true;
6134
6135 if (Op1.isNonZero() && Op0.isNonZero())
6136 return true;
6137
6138 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6139 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6140 }
6141 case ISD::SMIN: {
6142 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6143 if (Op1.isNegative())
6144 return true;
6145
6146 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6147 if (Op0.isNegative())
6148 return true;
6149
6150 if (Op1.isNonZero() && Op0.isNonZero())
6151 return true;
6152
6153 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6154 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6155 }
6156 case ISD::UMIN:
6157 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6158 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6159
6160 case ISD::ROTL:
6161 case ISD::ROTR:
6162 case ISD::BITREVERSE:
6163 case ISD::BSWAP:
6164 case ISD::CTPOP:
6165 case ISD::ABS:
6166 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6167
6168 case ISD::SRA:
6169 case ISD::SRL: {
6170 if (Op->getFlags().hasExact())
6171 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6172 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6173 if (ValKnown.isNegative())
6174 return true;
6175 // If max shift cnt of known ones is non-zero, result is non-zero.
6176 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6177 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6178 !ValKnown.One.lshr(MaxCnt).isZero())
6179 return true;
6180 break;
6181 }
6182 case ISD::UDIV:
6183 case ISD::SDIV:
6184 // div exact can only produce a zero if the dividend is zero.
6185 // TODO: For udiv this is also true if Op1 u<= Op0
6186 if (Op->getFlags().hasExact())
6187 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6188 break;
6189
6190 case ISD::ADD:
6191 if (Op->getFlags().hasNoUnsignedWrap())
6192 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6193 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6194 return true;
6195 // TODO: There are a lot more cases we can prove for add.
6196 break;
6197
6198 case ISD::SUB: {
6199 if (isNullConstant(Op.getOperand(0)))
6200 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6201
6202 std::optional<bool> ne =
6203 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6204 computeKnownBits(Op.getOperand(1), Depth + 1));
6205 return ne && *ne;
6206 }
6207
6208 case ISD::MUL:
6209 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6210 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6211 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6212 return true;
6213 break;
6214
6215 case ISD::ZERO_EXTEND:
6216 case ISD::SIGN_EXTEND:
6217 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6218 case ISD::VSCALE: {
6220 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6221 ConstantRange CR =
6222 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6223 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6224 return true;
6225 break;
6226 }
6227 }
6228
6230}
6231
6233 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6234 return !C1->isNegative();
6235
6236 return Op.getOpcode() == ISD::FABS;
6237}
6238
6240 // Check the obvious case.
6241 if (A == B) return true;
6242
6243 // For negative and positive zero.
6246 if (CA->isZero() && CB->isZero()) return true;
6247
6248 // Otherwise they may not be equal.
6249 return false;
6250}
6251
6252// Only bits set in Mask must be negated, other bits may be arbitrary.
6254 if (isBitwiseNot(V, AllowUndefs))
6255 return V.getOperand(0);
6256
6257 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6258 // bits in the non-extended part.
6259 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6260 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6261 return SDValue();
6262 SDValue ExtArg = V.getOperand(0);
6263 if (ExtArg.getScalarValueSizeInBits() >=
6264 MaskC->getAPIntValue().getActiveBits() &&
6265 isBitwiseNot(ExtArg, AllowUndefs) &&
6266 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6267 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6268 return ExtArg.getOperand(0).getOperand(0);
6269 return SDValue();
6270}
6271
6273 // Match masked merge pattern (X & ~M) op (Y & M)
6274 // Including degenerate case (X & ~M) op M
6275 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6276 SDValue Other) {
6277 if (SDValue NotOperand =
6278 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6279 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6280 NotOperand->getOpcode() == ISD::TRUNCATE)
6281 NotOperand = NotOperand->getOperand(0);
6282
6283 if (Other == NotOperand)
6284 return true;
6285 if (Other->getOpcode() == ISD::AND)
6286 return NotOperand == Other->getOperand(0) ||
6287 NotOperand == Other->getOperand(1);
6288 }
6289 return false;
6290 };
6291
6292 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6293 A = A->getOperand(0);
6294
6295 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6296 B = B->getOperand(0);
6297
6298 if (A->getOpcode() == ISD::AND)
6299 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6300 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6301 return false;
6302}
6303
6304// FIXME: unify with llvm::haveNoCommonBitsSet.
6306 assert(A.getValueType() == B.getValueType() &&
6307 "Values must have the same type");
6310 return true;
6313}
6314
6315static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6316 SelectionDAG &DAG) {
6317 if (cast<ConstantSDNode>(Step)->isZero())
6318 return DAG.getConstant(0, DL, VT);
6319
6320 return SDValue();
6321}
6322
6325 SelectionDAG &DAG) {
6326 int NumOps = Ops.size();
6327 assert(NumOps != 0 && "Can't build an empty vector!");
6328 assert(!VT.isScalableVector() &&
6329 "BUILD_VECTOR cannot be used with scalable types");
6330 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6331 "Incorrect element count in BUILD_VECTOR!");
6332
6333 // BUILD_VECTOR of UNDEFs is UNDEF.
6334 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6335 return DAG.getUNDEF(VT);
6336
6337 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6338 SDValue IdentitySrc;
6339 bool IsIdentity = true;
6340 for (int i = 0; i != NumOps; ++i) {
6342 Ops[i].getOperand(0).getValueType() != VT ||
6343 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6344 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6345 Ops[i].getConstantOperandAPInt(1) != i) {
6346 IsIdentity = false;
6347 break;
6348 }
6349 IdentitySrc = Ops[i].getOperand(0);
6350 }
6351 if (IsIdentity)
6352 return IdentitySrc;
6353
6354 return SDValue();
6355}
6356
6357/// Try to simplify vector concatenation to an input value, undef, or build
6358/// vector.
6361 SelectionDAG &DAG) {
6362 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6364 [Ops](SDValue Op) {
6365 return Ops[0].getValueType() == Op.getValueType();
6366 }) &&
6367 "Concatenation of vectors with inconsistent value types!");
6368 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6369 VT.getVectorElementCount() &&
6370 "Incorrect element count in vector concatenation!");
6371
6372 if (Ops.size() == 1)
6373 return Ops[0];
6374
6375 // Concat of UNDEFs is UNDEF.
6376 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6377 return DAG.getUNDEF(VT);
6378
6379 // Scan the operands and look for extract operations from a single source
6380 // that correspond to insertion at the same location via this concatenation:
6381 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6382 SDValue IdentitySrc;
6383 bool IsIdentity = true;
6384 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6385 SDValue Op = Ops[i];
6386 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6387 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6388 Op.getOperand(0).getValueType() != VT ||
6389 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6390 Op.getConstantOperandVal(1) != IdentityIndex) {
6391 IsIdentity = false;
6392 break;
6393 }
6394 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6395 "Unexpected identity source vector for concat of extracts");
6396 IdentitySrc = Op.getOperand(0);
6397 }
6398 if (IsIdentity) {
6399 assert(IdentitySrc && "Failed to set source vector of extracts");
6400 return IdentitySrc;
6401 }
6402
6403 // The code below this point is only designed to work for fixed width
6404 // vectors, so we bail out for now.
6405 if (VT.isScalableVector())
6406 return SDValue();
6407
6408 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6409 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6410 // BUILD_VECTOR.
6411 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6412 EVT SVT = VT.getScalarType();
6414 for (SDValue Op : Ops) {
6415 EVT OpVT = Op.getValueType();
6416 if (Op.isUndef())
6417 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6418 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6419 Elts.append(Op->op_begin(), Op->op_end());
6420 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6421 OpVT.getVectorNumElements() == 1 &&
6422 isNullConstant(Op.getOperand(2)))
6423 Elts.push_back(Op.getOperand(1));
6424 else
6425 return SDValue();
6426 }
6427
6428 // BUILD_VECTOR requires all inputs to be of the same type, find the
6429 // maximum type and extend them all.
6430 for (SDValue Op : Elts)
6431 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6432
6433 if (SVT.bitsGT(VT.getScalarType())) {
6434 for (SDValue &Op : Elts) {
6435 if (Op.isUndef())
6436 Op = DAG.getUNDEF(SVT);
6437 else
6438 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6439 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6440 : DAG.getSExtOrTrunc(Op, DL, SVT);
6441 }
6442 }
6443
6444 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6445 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6446 return V;
6447}
6448
6449/// Gets or creates the specified node.
6450SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6451 SDVTList VTs = getVTList(VT);
6453 AddNodeIDNode(ID, Opcode, VTs, {});
6454 void *IP = nullptr;
6455 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6456 return SDValue(E, 0);
6457
6458 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6459 CSEMap.InsertNode(N, IP);
6460
6461 InsertNode(N);
6462 SDValue V = SDValue(N, 0);
6463 NewSDValueDbgMsg(V, "Creating new node: ", this);
6464 return V;
6465}
6466
6467SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6468 SDValue N1) {
6469 SDNodeFlags Flags;
6470 if (Inserter)
6471 Flags = Inserter->getFlags();
6472 return getNode(Opcode, DL, VT, N1, Flags);
6473}
6474
6475SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6476 SDValue N1, const SDNodeFlags Flags) {
6477 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6478
6479 // Constant fold unary operations with a vector integer or float operand.
6480 switch (Opcode) {
6481 default:
6482 // FIXME: Entirely reasonable to perform folding of other unary
6483 // operations here as the need arises.
6484 break;
6485 case ISD::FNEG:
6486 case ISD::FABS:
6487 case ISD::FCEIL:
6488 case ISD::FTRUNC:
6489 case ISD::FFLOOR:
6490 case ISD::FP_EXTEND:
6491 case ISD::FP_TO_SINT:
6492 case ISD::FP_TO_UINT:
6493 case ISD::FP_TO_FP16:
6494 case ISD::FP_TO_BF16:
6495 case ISD::TRUNCATE:
6496 case ISD::ANY_EXTEND:
6497 case ISD::ZERO_EXTEND:
6498 case ISD::SIGN_EXTEND:
6499 case ISD::UINT_TO_FP:
6500 case ISD::SINT_TO_FP:
6501 case ISD::FP16_TO_FP:
6502 case ISD::BF16_TO_FP:
6503 case ISD::BITCAST:
6504 case ISD::ABS:
6505 case ISD::BITREVERSE:
6506 case ISD::BSWAP:
6507 case ISD::CTLZ:
6509 case ISD::CTTZ:
6511 case ISD::CTPOP:
6512 case ISD::STEP_VECTOR: {
6513 SDValue Ops = {N1};
6514 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6515 return Fold;
6516 }
6517 }
6518
6519 unsigned OpOpcode = N1.getNode()->getOpcode();
6520 switch (Opcode) {
6521 case ISD::STEP_VECTOR:
6522 assert(VT.isScalableVector() &&
6523 "STEP_VECTOR can only be used with scalable types");
6524 assert(OpOpcode == ISD::TargetConstant &&
6525 VT.getVectorElementType() == N1.getValueType() &&
6526 "Unexpected step operand");
6527 break;
6528 case ISD::FREEZE:
6529 assert(VT == N1.getValueType() && "Unexpected VT!");
6530 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6531 return N1;
6532 break;
6533 case ISD::TokenFactor:
6534 case ISD::MERGE_VALUES:
6536 return N1; // Factor, merge or concat of one node? No need.
6537 case ISD::BUILD_VECTOR: {
6538 // Attempt to simplify BUILD_VECTOR.
6539 SDValue Ops[] = {N1};
6540 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6541 return V;
6542 break;
6543 }
6544 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6545 case ISD::FP_EXTEND:
6547 "Invalid FP cast!");
6548 if (N1.getValueType() == VT) return N1; // noop conversion.
6549 assert((!VT.isVector() || VT.getVectorElementCount() ==
6551 "Vector element count mismatch!");
6552 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6553 if (N1.isUndef())
6554 return getUNDEF(VT);
6555 break;
6556 case ISD::FP_TO_SINT:
6557 case ISD::FP_TO_UINT:
6558 if (N1.isUndef())
6559 return getUNDEF(VT);
6560 break;
6561 case ISD::SINT_TO_FP:
6562 case ISD::UINT_TO_FP:
6563 // [us]itofp(undef) = 0, because the result value is bounded.
6564 if (N1.isUndef())
6565 return getConstantFP(0.0, DL, VT);
6566 break;
6567 case ISD::SIGN_EXTEND:
6568 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6569 "Invalid SIGN_EXTEND!");
6570 assert(VT.isVector() == N1.getValueType().isVector() &&
6571 "SIGN_EXTEND result type type should be vector iff the operand "
6572 "type is vector!");
6573 if (N1.getValueType() == VT) return N1; // noop extension
6574 assert((!VT.isVector() || VT.getVectorElementCount() ==
6576 "Vector element count mismatch!");
6577 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6578 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6579 SDNodeFlags Flags;
6580 if (OpOpcode == ISD::ZERO_EXTEND)
6581 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6582 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6583 transferDbgValues(N1, NewVal);
6584 return NewVal;
6585 }
6586
6587 if (OpOpcode == ISD::POISON)
6588 return getPOISON(VT);
6589
6590 if (N1.isUndef())
6591 // sext(undef) = 0, because the top bits will all be the same.
6592 return getConstant(0, DL, VT);
6593
6594 // Skip unnecessary sext_inreg pattern:
6595 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6596 if (OpOpcode == ISD::TRUNCATE) {
6597 SDValue OpOp = N1.getOperand(0);
6598 if (OpOp.getValueType() == VT) {
6599 unsigned NumSignExtBits =
6601 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6602 transferDbgValues(N1, OpOp);
6603 return OpOp;
6604 }
6605 }
6606 }
6607 break;
6608 case ISD::ZERO_EXTEND:
6609 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6610 "Invalid ZERO_EXTEND!");
6611 assert(VT.isVector() == N1.getValueType().isVector() &&
6612 "ZERO_EXTEND result type type should be vector iff the operand "
6613 "type is vector!");
6614 if (N1.getValueType() == VT) return N1; // noop extension
6615 assert((!VT.isVector() || VT.getVectorElementCount() ==
6617 "Vector element count mismatch!");
6618 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6619 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6620 SDNodeFlags Flags;
6621 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6622 SDValue NewVal =
6623 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6624 transferDbgValues(N1, NewVal);
6625 return NewVal;
6626 }
6627
6628 if (OpOpcode == ISD::POISON)
6629 return getPOISON(VT);
6630
6631 if (N1.isUndef())
6632 // zext(undef) = 0, because the top bits will be zero.
6633 return getConstant(0, DL, VT);
6634
6635 // Skip unnecessary zext_inreg pattern:
6636 // (zext (trunc x)) -> x iff the upper bits are known zero.
6637 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6638 // use to recognise zext_inreg patterns.
6639 if (OpOpcode == ISD::TRUNCATE) {
6640 SDValue OpOp = N1.getOperand(0);
6641 if (OpOp.getValueType() == VT) {
6642 if (OpOp.getOpcode() != ISD::AND) {
6645 if (MaskedValueIsZero(OpOp, HiBits)) {
6646 transferDbgValues(N1, OpOp);
6647 return OpOp;
6648 }
6649 }
6650 }
6651 }
6652 break;
6653 case ISD::ANY_EXTEND:
6654 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6655 "Invalid ANY_EXTEND!");
6656 assert(VT.isVector() == N1.getValueType().isVector() &&
6657 "ANY_EXTEND result type type should be vector iff the operand "
6658 "type is vector!");
6659 if (N1.getValueType() == VT) return N1; // noop extension
6660 assert((!VT.isVector() || VT.getVectorElementCount() ==
6662 "Vector element count mismatch!");
6663 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6664
6665 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6666 OpOpcode == ISD::ANY_EXTEND) {
6667 SDNodeFlags Flags;
6668 if (OpOpcode == ISD::ZERO_EXTEND)
6669 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6670 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6671 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6672 }
6673 if (N1.isUndef())
6674 return getUNDEF(VT);
6675
6676 // (ext (trunc x)) -> x
6677 if (OpOpcode == ISD::TRUNCATE) {
6678 SDValue OpOp = N1.getOperand(0);
6679 if (OpOp.getValueType() == VT) {
6680 transferDbgValues(N1, OpOp);
6681 return OpOp;
6682 }
6683 }
6684 break;
6685 case ISD::TRUNCATE:
6686 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6687 "Invalid TRUNCATE!");
6688 assert(VT.isVector() == N1.getValueType().isVector() &&
6689 "TRUNCATE result type type should be vector iff the operand "
6690 "type is vector!");
6691 if (N1.getValueType() == VT) return N1; // noop truncate
6692 assert((!VT.isVector() || VT.getVectorElementCount() ==
6694 "Vector element count mismatch!");
6695 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6696 if (OpOpcode == ISD::TRUNCATE)
6697 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6698 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6699 OpOpcode == ISD::ANY_EXTEND) {
6700 // If the source is smaller than the dest, we still need an extend.
6702 VT.getScalarType())) {
6703 SDNodeFlags Flags;
6704 if (OpOpcode == ISD::ZERO_EXTEND)
6705 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6706 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6707 }
6708 if (N1.getOperand(0).getValueType().bitsGT(VT))
6709 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6710 return N1.getOperand(0);
6711 }
6712 if (N1.isUndef())
6713 return getUNDEF(VT);
6714 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6715 return getVScale(DL, VT,
6717 break;
6721 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6722 assert(N1.getValueType().bitsLE(VT) &&
6723 "The input must be the same size or smaller than the result.");
6726 "The destination vector type must have fewer lanes than the input.");
6727 break;
6728 case ISD::ABS:
6729 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6730 if (N1.isUndef())
6731 return getConstant(0, DL, VT);
6732 break;
6733 case ISD::BSWAP:
6734 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6735 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6736 "BSWAP types must be a multiple of 16 bits!");
6737 if (N1.isUndef())
6738 return getUNDEF(VT);
6739 // bswap(bswap(X)) -> X.
6740 if (OpOpcode == ISD::BSWAP)
6741 return N1.getOperand(0);
6742 break;
6743 case ISD::BITREVERSE:
6744 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6745 if (N1.isUndef())
6746 return getUNDEF(VT);
6747 break;
6748 case ISD::BITCAST:
6750 "Cannot BITCAST between types of different sizes!");
6751 if (VT == N1.getValueType()) return N1; // noop conversion.
6752 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6753 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6754 if (N1.isUndef())
6755 return getUNDEF(VT);
6756 break;
6758 assert(VT.isVector() && !N1.getValueType().isVector() &&
6759 (VT.getVectorElementType() == N1.getValueType() ||
6761 N1.getValueType().isInteger() &&
6763 "Illegal SCALAR_TO_VECTOR node!");
6764 if (N1.isUndef())
6765 return getUNDEF(VT);
6766 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6767 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6769 N1.getConstantOperandVal(1) == 0 &&
6770 N1.getOperand(0).getValueType() == VT)
6771 return N1.getOperand(0);
6772 break;
6773 case ISD::FNEG:
6774 // Negation of an unknown bag of bits is still completely undefined.
6775 if (N1.isUndef())
6776 return getUNDEF(VT);
6777
6778 if (OpOpcode == ISD::FNEG) // --X -> X
6779 return N1.getOperand(0);
6780 break;
6781 case ISD::FABS:
6782 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6783 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6784 break;
6785 case ISD::VSCALE:
6786 assert(VT == N1.getValueType() && "Unexpected VT!");
6787 break;
6788 case ISD::CTPOP:
6789 if (N1.getValueType().getScalarType() == MVT::i1)
6790 return N1;
6791 break;
6792 case ISD::CTLZ:
6793 case ISD::CTTZ:
6794 if (N1.getValueType().getScalarType() == MVT::i1)
6795 return getNOT(DL, N1, N1.getValueType());
6796 break;
6797 case ISD::VECREDUCE_ADD:
6798 if (N1.getValueType().getScalarType() == MVT::i1)
6799 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6800 break;
6801 case ISD::VECREDUCE_SMIN:
6802 case ISD::VECREDUCE_UMAX:
6803 if (N1.getValueType().getScalarType() == MVT::i1)
6804 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6805 break;
6806 case ISD::VECREDUCE_SMAX:
6807 case ISD::VECREDUCE_UMIN:
6808 if (N1.getValueType().getScalarType() == MVT::i1)
6809 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6810 break;
6811 case ISD::SPLAT_VECTOR:
6812 assert(VT.isVector() && "Wrong return type!");
6813 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6814 // that for now.
6816 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6818 N1.getValueType().isInteger() &&
6820 "Wrong operand type!");
6821 break;
6822 }
6823
6824 SDNode *N;
6825 SDVTList VTs = getVTList(VT);
6826 SDValue Ops[] = {N1};
6827 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6829 AddNodeIDNode(ID, Opcode, VTs, Ops);
6830 void *IP = nullptr;
6831 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6832 E->intersectFlagsWith(Flags);
6833 return SDValue(E, 0);
6834 }
6835
6836 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6837 N->setFlags(Flags);
6838 createOperands(N, Ops);
6839 CSEMap.InsertNode(N, IP);
6840 } else {
6841 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6842 createOperands(N, Ops);
6843 }
6844
6845 InsertNode(N);
6846 SDValue V = SDValue(N, 0);
6847 NewSDValueDbgMsg(V, "Creating new node: ", this);
6848 return V;
6849}
6850
6851static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6852 const APInt &C2) {
6853 switch (Opcode) {
6854 case ISD::ADD: return C1 + C2;
6855 case ISD::SUB: return C1 - C2;
6856 case ISD::MUL: return C1 * C2;
6857 case ISD::AND: return C1 & C2;
6858 case ISD::OR: return C1 | C2;
6859 case ISD::XOR: return C1 ^ C2;
6860 case ISD::SHL: return C1 << C2;
6861 case ISD::SRL: return C1.lshr(C2);
6862 case ISD::SRA: return C1.ashr(C2);
6863 case ISD::ROTL: return C1.rotl(C2);
6864 case ISD::ROTR: return C1.rotr(C2);
6865 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6866 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6867 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6868 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6869 case ISD::SADDSAT: return C1.sadd_sat(C2);
6870 case ISD::UADDSAT: return C1.uadd_sat(C2);
6871 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6872 case ISD::USUBSAT: return C1.usub_sat(C2);
6873 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6874 case ISD::USHLSAT: return C1.ushl_sat(C2);
6875 case ISD::UDIV:
6876 if (!C2.getBoolValue())
6877 break;
6878 return C1.udiv(C2);
6879 case ISD::UREM:
6880 if (!C2.getBoolValue())
6881 break;
6882 return C1.urem(C2);
6883 case ISD::SDIV:
6884 if (!C2.getBoolValue())
6885 break;
6886 return C1.sdiv(C2);
6887 case ISD::SREM:
6888 if (!C2.getBoolValue())
6889 break;
6890 return C1.srem(C2);
6891 case ISD::AVGFLOORS:
6892 return APIntOps::avgFloorS(C1, C2);
6893 case ISD::AVGFLOORU:
6894 return APIntOps::avgFloorU(C1, C2);
6895 case ISD::AVGCEILS:
6896 return APIntOps::avgCeilS(C1, C2);
6897 case ISD::AVGCEILU:
6898 return APIntOps::avgCeilU(C1, C2);
6899 case ISD::ABDS:
6900 return APIntOps::abds(C1, C2);
6901 case ISD::ABDU:
6902 return APIntOps::abdu(C1, C2);
6903 case ISD::MULHS:
6904 return APIntOps::mulhs(C1, C2);
6905 case ISD::MULHU:
6906 return APIntOps::mulhu(C1, C2);
6907 }
6908 return std::nullopt;
6909}
6910// Handle constant folding with UNDEF.
6911// TODO: Handle more cases.
6912static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6913 bool IsUndef1, const APInt &C2,
6914 bool IsUndef2) {
6915 if (!(IsUndef1 || IsUndef2))
6916 return FoldValue(Opcode, C1, C2);
6917
6918 // Fold and(x, undef) -> 0
6919 // Fold mul(x, undef) -> 0
6920 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6921 return APInt::getZero(C1.getBitWidth());
6922
6923 return std::nullopt;
6924}
6925
6927 const GlobalAddressSDNode *GA,
6928 const SDNode *N2) {
6929 if (GA->getOpcode() != ISD::GlobalAddress)
6930 return SDValue();
6931 if (!TLI->isOffsetFoldingLegal(GA))
6932 return SDValue();
6933 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6934 if (!C2)
6935 return SDValue();
6936 int64_t Offset = C2->getSExtValue();
6937 switch (Opcode) {
6938 case ISD::ADD:
6939 case ISD::PTRADD:
6940 break;
6941 case ISD::SUB: Offset = -uint64_t(Offset); break;
6942 default: return SDValue();
6943 }
6944 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6945 GA->getOffset() + uint64_t(Offset));
6946}
6947
6949 switch (Opcode) {
6950 case ISD::SDIV:
6951 case ISD::UDIV:
6952 case ISD::SREM:
6953 case ISD::UREM: {
6954 // If a divisor is zero/undef or any element of a divisor vector is
6955 // zero/undef, the whole op is undef.
6956 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6957 SDValue Divisor = Ops[1];
6958 if (Divisor.isUndef() || isNullConstant(Divisor))
6959 return true;
6960
6961 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6962 llvm::any_of(Divisor->op_values(),
6963 [](SDValue V) { return V.isUndef() ||
6964 isNullConstant(V); });
6965 // TODO: Handle signed overflow.
6966 }
6967 // TODO: Handle oversized shifts.
6968 default:
6969 return false;
6970 }
6971}
6972
6975 SDNodeFlags Flags) {
6976 // If the opcode is a target-specific ISD node, there's nothing we can
6977 // do here and the operand rules may not line up with the below, so
6978 // bail early.
6979 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6980 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6981 // foldCONCAT_VECTORS in getNode before this is called.
6982 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6983 return SDValue();
6984
6985 unsigned NumOps = Ops.size();
6986 if (NumOps == 0)
6987 return SDValue();
6988
6989 if (isUndef(Opcode, Ops))
6990 return getUNDEF(VT);
6991
6992 // Handle unary special cases.
6993 if (NumOps == 1) {
6994 SDValue N1 = Ops[0];
6995
6996 // Constant fold unary operations with an integer constant operand. Even
6997 // opaque constant will be folded, because the folding of unary operations
6998 // doesn't create new constants with different values. Nevertheless, the
6999 // opaque flag is preserved during folding to prevent future folding with
7000 // other constants.
7001 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7002 const APInt &Val = C->getAPIntValue();
7003 switch (Opcode) {
7004 case ISD::SIGN_EXTEND:
7005 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7006 C->isTargetOpcode(), C->isOpaque());
7007 case ISD::TRUNCATE:
7008 if (C->isOpaque())
7009 break;
7010 [[fallthrough]];
7011 case ISD::ZERO_EXTEND:
7012 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7013 C->isTargetOpcode(), C->isOpaque());
7014 case ISD::ANY_EXTEND:
7015 // Some targets like RISCV prefer to sign extend some types.
7016 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7017 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7018 C->isTargetOpcode(), C->isOpaque());
7019 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7020 C->isTargetOpcode(), C->isOpaque());
7021 case ISD::ABS:
7022 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7023 C->isOpaque());
7024 case ISD::BITREVERSE:
7025 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7026 C->isOpaque());
7027 case ISD::BSWAP:
7028 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7029 C->isOpaque());
7030 case ISD::CTPOP:
7031 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7032 C->isOpaque());
7033 case ISD::CTLZ:
7035 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7036 C->isOpaque());
7037 case ISD::CTTZ:
7039 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7040 C->isOpaque());
7041 case ISD::UINT_TO_FP:
7042 case ISD::SINT_TO_FP: {
7044 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7046 return getConstantFP(FPV, DL, VT);
7047 }
7048 case ISD::FP16_TO_FP:
7049 case ISD::BF16_TO_FP: {
7050 bool Ignored;
7051 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7052 : APFloat::BFloat(),
7053 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7054
7055 // This can return overflow, underflow, or inexact; we don't care.
7056 // FIXME need to be more flexible about rounding mode.
7058 &Ignored);
7059 return getConstantFP(FPV, DL, VT);
7060 }
7061 case ISD::STEP_VECTOR:
7062 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7063 return V;
7064 break;
7065 case ISD::BITCAST:
7066 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7067 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7068 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7069 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7070 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7071 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7072 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7073 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7074 break;
7075 }
7076 }
7077
7078 // Constant fold unary operations with a floating point constant operand.
7079 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7080 APFloat V = C->getValueAPF(); // make copy
7081 switch (Opcode) {
7082 case ISD::FNEG:
7083 V.changeSign();
7084 return getConstantFP(V, DL, VT);
7085 case ISD::FABS:
7086 V.clearSign();
7087 return getConstantFP(V, DL, VT);
7088 case ISD::FCEIL: {
7089 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7091 return getConstantFP(V, DL, VT);
7092 return SDValue();
7093 }
7094 case ISD::FTRUNC: {
7095 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7097 return getConstantFP(V, DL, VT);
7098 return SDValue();
7099 }
7100 case ISD::FFLOOR: {
7101 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7103 return getConstantFP(V, DL, VT);
7104 return SDValue();
7105 }
7106 case ISD::FP_EXTEND: {
7107 bool ignored;
7108 // This can return overflow, underflow, or inexact; we don't care.
7109 // FIXME need to be more flexible about rounding mode.
7110 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7111 &ignored);
7112 return getConstantFP(V, DL, VT);
7113 }
7114 case ISD::FP_TO_SINT:
7115 case ISD::FP_TO_UINT: {
7116 bool ignored;
7117 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7118 // FIXME need to be more flexible about rounding mode.
7120 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7121 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7122 break;
7123 return getConstant(IntVal, DL, VT);
7124 }
7125 case ISD::FP_TO_FP16:
7126 case ISD::FP_TO_BF16: {
7127 bool Ignored;
7128 // This can return overflow, underflow, or inexact; we don't care.
7129 // FIXME need to be more flexible about rounding mode.
7130 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7131 : APFloat::BFloat(),
7133 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7134 }
7135 case ISD::BITCAST:
7136 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7137 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7138 VT);
7139 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7140 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7141 VT);
7142 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7143 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7144 VT);
7145 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7146 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7147 break;
7148 }
7149 }
7150
7151 // Early-out if we failed to constant fold a bitcast.
7152 if (Opcode == ISD::BITCAST)
7153 return SDValue();
7154 }
7155
7156 // Handle binops special cases.
7157 if (NumOps == 2) {
7158 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7159 return CFP;
7160
7161 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7162 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7163 if (C1->isOpaque() || C2->isOpaque())
7164 return SDValue();
7165
7166 std::optional<APInt> FoldAttempt =
7167 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7168 if (!FoldAttempt)
7169 return SDValue();
7170
7171 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7172 assert((!Folded || !VT.isVector()) &&
7173 "Can't fold vectors ops with scalar operands");
7174 return Folded;
7175 }
7176 }
7177
7178 // fold (add Sym, c) -> Sym+c
7180 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7181 if (TLI->isCommutativeBinOp(Opcode))
7183 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7184
7185 // fold (sext_in_reg c1) -> c2
7186 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7187 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7188
7189 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7190 unsigned FromBits = EVT.getScalarSizeInBits();
7191 Val <<= Val.getBitWidth() - FromBits;
7192 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7193 return getConstant(Val, DL, ConstantVT);
7194 };
7195
7196 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7197 const APInt &Val = C1->getAPIntValue();
7198 return SignExtendInReg(Val, VT);
7199 }
7200
7202 SmallVector<SDValue, 8> ScalarOps;
7203 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7204 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7205 SDValue Op = Ops[0].getOperand(I);
7206 if (Op.isUndef()) {
7207 ScalarOps.push_back(getUNDEF(OpVT));
7208 continue;
7209 }
7210 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7211 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7212 }
7213 return getBuildVector(VT, DL, ScalarOps);
7214 }
7215
7216 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7217 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7218 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7219 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7220 Ops[0].getOperand(0).getValueType()));
7221 }
7222 }
7223
7224 // Handle fshl/fshr special cases.
7225 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7226 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7227 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7228 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7229
7230 if (C1 && C2 && C3) {
7231 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7232 return SDValue();
7233 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7234 &V3 = C3->getAPIntValue();
7235
7236 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7237 : APIntOps::fshr(V1, V2, V3);
7238 return getConstant(FoldedVal, DL, VT);
7239 }
7240 }
7241
7242 // Handle fma/fmad special cases.
7243 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7244 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7245 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7246 Ops[2].getValueType() == VT && "FMA types must match!");
7250 if (C1 && C2 && C3) {
7251 APFloat V1 = C1->getValueAPF();
7252 const APFloat &V2 = C2->getValueAPF();
7253 const APFloat &V3 = C3->getValueAPF();
7254 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7257 } else
7259 return getConstantFP(V1, DL, VT);
7260 }
7261 }
7262
7263 // This is for vector folding only from here on.
7264 if (!VT.isVector())
7265 return SDValue();
7266
7267 ElementCount NumElts = VT.getVectorElementCount();
7268
7269 // See if we can fold through any bitcasted integer ops.
7270 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7271 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7272 (Ops[0].getOpcode() == ISD::BITCAST ||
7273 Ops[1].getOpcode() == ISD::BITCAST)) {
7276 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7277 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7278 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7279 N2.getValueType().isInteger()) {
7280 bool IsLE = getDataLayout().isLittleEndian();
7281 unsigned EltBits = VT.getScalarSizeInBits();
7282 SmallVector<APInt> RawBits1, RawBits2;
7283 BitVector UndefElts1, UndefElts2;
7284 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7285 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7286 SmallVector<APInt> RawBits;
7287 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7288 std::optional<APInt> Fold = FoldValueWithUndef(
7289 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7290 if (!Fold)
7291 break;
7292 RawBits.push_back(*Fold);
7293 }
7294 if (RawBits.size() == NumElts.getFixedValue()) {
7295 // We have constant folded, but we might need to cast this again back
7296 // to the original (possibly legalized) type.
7297 EVT BVVT, BVEltVT;
7298 if (N1.getValueType() == VT) {
7299 BVVT = N1.getValueType();
7300 BVEltVT = BV1->getOperand(0).getValueType();
7301 } else {
7302 BVVT = N2.getValueType();
7303 BVEltVT = BV2->getOperand(0).getValueType();
7304 }
7305 unsigned BVEltBits = BVEltVT.getSizeInBits();
7306 SmallVector<APInt> DstBits;
7307 BitVector DstUndefs;
7309 DstBits, RawBits, DstUndefs,
7310 BitVector(RawBits.size(), false));
7311 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7312 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7313 if (DstUndefs[I])
7314 continue;
7315 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7316 }
7317 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7318 }
7319 }
7320 }
7321 }
7322
7323 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7324 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7325 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7326 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7327 APInt RHSVal;
7328 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7329 APInt NewStep = Opcode == ISD::MUL
7330 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7331 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7332 return getStepVector(DL, VT, NewStep);
7333 }
7334 }
7335
7336 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7337 return !Op.getValueType().isVector() ||
7338 Op.getValueType().getVectorElementCount() == NumElts;
7339 };
7340
7341 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7342 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7343 Op.getOpcode() == ISD::BUILD_VECTOR ||
7344 Op.getOpcode() == ISD::SPLAT_VECTOR;
7345 };
7346
7347 // All operands must be vector types with the same number of elements as
7348 // the result type and must be either UNDEF or a build/splat vector
7349 // or UNDEF scalars.
7350 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7351 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7352 return SDValue();
7353
7354 // If we are comparing vectors, then the result needs to be a i1 boolean that
7355 // is then extended back to the legal result type depending on how booleans
7356 // are represented.
7357 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7358 ISD::NodeType ExtendCode =
7359 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7360 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7362
7363 // Find legal integer scalar type for constant promotion and
7364 // ensure that its scalar size is at least as large as source.
7365 EVT LegalSVT = VT.getScalarType();
7366 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7367 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7368 if (LegalSVT.bitsLT(VT.getScalarType()))
7369 return SDValue();
7370 }
7371
7372 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7373 // only have one operand to check. For fixed-length vector types we may have
7374 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7375 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7376
7377 // Constant fold each scalar lane separately.
7378 SmallVector<SDValue, 4> ScalarResults;
7379 for (unsigned I = 0; I != NumVectorElts; I++) {
7380 SmallVector<SDValue, 4> ScalarOps;
7381 for (SDValue Op : Ops) {
7382 EVT InSVT = Op.getValueType().getScalarType();
7383 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7384 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7385 if (Op.isUndef())
7386 ScalarOps.push_back(getUNDEF(InSVT));
7387 else
7388 ScalarOps.push_back(Op);
7389 continue;
7390 }
7391
7392 SDValue ScalarOp =
7393 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7394 EVT ScalarVT = ScalarOp.getValueType();
7395
7396 // Build vector (integer) scalar operands may need implicit
7397 // truncation - do this before constant folding.
7398 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7399 // Don't create illegally-typed nodes unless they're constants or undef
7400 // - if we fail to constant fold we can't guarantee the (dead) nodes
7401 // we're creating will be cleaned up before being visited for
7402 // legalization.
7403 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7404 !isa<ConstantSDNode>(ScalarOp) &&
7405 TLI->getTypeAction(*getContext(), InSVT) !=
7407 return SDValue();
7408 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7409 }
7410
7411 ScalarOps.push_back(ScalarOp);
7412 }
7413
7414 // Constant fold the scalar operands.
7415 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7416
7417 // Scalar folding only succeeded if the result is a constant or UNDEF.
7418 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7419 ScalarResult.getOpcode() != ISD::ConstantFP)
7420 return SDValue();
7421
7422 // Legalize the (integer) scalar constant if necessary. We only do
7423 // this once we know the folding succeeded, since otherwise we would
7424 // get a node with illegal type which has a user.
7425 if (LegalSVT != SVT)
7426 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7427
7428 ScalarResults.push_back(ScalarResult);
7429 }
7430
7431 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7432 : getBuildVector(VT, DL, ScalarResults);
7433 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7434 return V;
7435}
7436
7439 // TODO: Add support for unary/ternary fp opcodes.
7440 if (Ops.size() != 2)
7441 return SDValue();
7442
7443 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7444 // should. That will require dealing with a potentially non-default
7445 // rounding mode, checking the "opStatus" return value from the APFloat
7446 // math calculations, and possibly other variations.
7447 SDValue N1 = Ops[0];
7448 SDValue N2 = Ops[1];
7449 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7450 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7451 if (N1CFP && N2CFP) {
7452 APFloat C1 = N1CFP->getValueAPF(); // make copy
7453 const APFloat &C2 = N2CFP->getValueAPF();
7454 switch (Opcode) {
7455 case ISD::FADD:
7457 return getConstantFP(C1, DL, VT);
7458 case ISD::FSUB:
7460 return getConstantFP(C1, DL, VT);
7461 case ISD::FMUL:
7463 return getConstantFP(C1, DL, VT);
7464 case ISD::FDIV:
7466 return getConstantFP(C1, DL, VT);
7467 case ISD::FREM:
7468 C1.mod(C2);
7469 return getConstantFP(C1, DL, VT);
7470 case ISD::FCOPYSIGN:
7471 C1.copySign(C2);
7472 return getConstantFP(C1, DL, VT);
7473 case ISD::FMINNUM:
7474 return getConstantFP(minnum(C1, C2), DL, VT);
7475 case ISD::FMAXNUM:
7476 return getConstantFP(maxnum(C1, C2), DL, VT);
7477 case ISD::FMINIMUM:
7478 return getConstantFP(minimum(C1, C2), DL, VT);
7479 case ISD::FMAXIMUM:
7480 return getConstantFP(maximum(C1, C2), DL, VT);
7481 case ISD::FMINIMUMNUM:
7482 return getConstantFP(minimumnum(C1, C2), DL, VT);
7483 case ISD::FMAXIMUMNUM:
7484 return getConstantFP(maximumnum(C1, C2), DL, VT);
7485 default: break;
7486 }
7487 }
7488 if (N1CFP && Opcode == ISD::FP_ROUND) {
7489 APFloat C1 = N1CFP->getValueAPF(); // make copy
7490 bool Unused;
7491 // This can return overflow, underflow, or inexact; we don't care.
7492 // FIXME need to be more flexible about rounding mode.
7494 &Unused);
7495 return getConstantFP(C1, DL, VT);
7496 }
7497
7498 switch (Opcode) {
7499 case ISD::FSUB:
7500 // -0.0 - undef --> undef (consistent with "fneg undef")
7501 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7502 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7503 return getUNDEF(VT);
7504 [[fallthrough]];
7505
7506 case ISD::FADD:
7507 case ISD::FMUL:
7508 case ISD::FDIV:
7509 case ISD::FREM:
7510 // If both operands are undef, the result is undef. If 1 operand is undef,
7511 // the result is NaN. This should match the behavior of the IR optimizer.
7512 if (N1.isUndef() && N2.isUndef())
7513 return getUNDEF(VT);
7514 if (N1.isUndef() || N2.isUndef())
7516 }
7517 return SDValue();
7518}
7519
7521 const SDLoc &DL, EVT DstEltVT) {
7522 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7523
7524 // If this is already the right type, we're done.
7525 if (SrcEltVT == DstEltVT)
7526 return SDValue(BV, 0);
7527
7528 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7529 unsigned DstBitSize = DstEltVT.getSizeInBits();
7530
7531 // If this is a conversion of N elements of one type to N elements of another
7532 // type, convert each element. This handles FP<->INT cases.
7533 if (SrcBitSize == DstBitSize) {
7535 for (SDValue Op : BV->op_values()) {
7536 // If the vector element type is not legal, the BUILD_VECTOR operands
7537 // are promoted and implicitly truncated. Make that explicit here.
7538 if (Op.getValueType() != SrcEltVT)
7539 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7540 Ops.push_back(getBitcast(DstEltVT, Op));
7541 }
7542 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7544 return getBuildVector(VT, DL, Ops);
7545 }
7546
7547 // Otherwise, we're growing or shrinking the elements. To avoid having to
7548 // handle annoying details of growing/shrinking FP values, we convert them to
7549 // int first.
7550 if (SrcEltVT.isFloatingPoint()) {
7551 // Convert the input float vector to a int vector where the elements are the
7552 // same sizes.
7553 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7554 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7556 DstEltVT);
7557 return SDValue();
7558 }
7559
7560 // Now we know the input is an integer vector. If the output is a FP type,
7561 // convert to integer first, then to FP of the right size.
7562 if (DstEltVT.isFloatingPoint()) {
7563 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7564 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7566 DstEltVT);
7567 return SDValue();
7568 }
7569
7570 // Okay, we know the src/dst types are both integers of differing types.
7571 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7572
7573 // Extract the constant raw bit data.
7574 BitVector UndefElements;
7575 SmallVector<APInt> RawBits;
7576 bool IsLE = getDataLayout().isLittleEndian();
7577 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7578 return SDValue();
7579
7581 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7582 if (UndefElements[I])
7583 Ops.push_back(getUNDEF(DstEltVT));
7584 else
7585 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7586 }
7587
7588 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7589 return getBuildVector(VT, DL, Ops);
7590}
7591
7593 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7594
7595 // There's no need to assert on a byte-aligned pointer. All pointers are at
7596 // least byte aligned.
7597 if (A == Align(1))
7598 return Val;
7599
7600 SDVTList VTs = getVTList(Val.getValueType());
7602 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7603 ID.AddInteger(A.value());
7604
7605 void *IP = nullptr;
7606 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7607 return SDValue(E, 0);
7608
7609 auto *N =
7610 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7611 createOperands(N, {Val});
7612
7613 CSEMap.InsertNode(N, IP);
7614 InsertNode(N);
7615
7616 SDValue V(N, 0);
7617 NewSDValueDbgMsg(V, "Creating new node: ", this);
7618 return V;
7619}
7620
7621SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7622 SDValue N1, SDValue N2) {
7623 SDNodeFlags Flags;
7624 if (Inserter)
7625 Flags = Inserter->getFlags();
7626 return getNode(Opcode, DL, VT, N1, N2, Flags);
7627}
7628
7630 SDValue &N2) const {
7631 if (!TLI->isCommutativeBinOp(Opcode))
7632 return;
7633
7634 // Canonicalize:
7635 // binop(const, nonconst) -> binop(nonconst, const)
7638 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7639 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7640 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7641 std::swap(N1, N2);
7642
7643 // Canonicalize:
7644 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7645 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7647 std::swap(N1, N2);
7648}
7649
7650SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7651 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7653 N2.getOpcode() != ISD::DELETED_NODE &&
7654 "Operand is DELETED_NODE!");
7655
7656 canonicalizeCommutativeBinop(Opcode, N1, N2);
7657
7658 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7659 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7660
7661 // Don't allow undefs in vector splats - we might be returning N2 when folding
7662 // to zero etc.
7663 ConstantSDNode *N2CV =
7664 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7665
7666 switch (Opcode) {
7667 default: break;
7668 case ISD::TokenFactor:
7669 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7670 N2.getValueType() == MVT::Other && "Invalid token factor!");
7671 // Fold trivial token factors.
7672 if (N1.getOpcode() == ISD::EntryToken) return N2;
7673 if (N2.getOpcode() == ISD::EntryToken) return N1;
7674 if (N1 == N2) return N1;
7675 break;
7676 case ISD::BUILD_VECTOR: {
7677 // Attempt to simplify BUILD_VECTOR.
7678 SDValue Ops[] = {N1, N2};
7679 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7680 return V;
7681 break;
7682 }
7683 case ISD::CONCAT_VECTORS: {
7684 SDValue Ops[] = {N1, N2};
7685 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7686 return V;
7687 break;
7688 }
7689 case ISD::AND:
7690 assert(VT.isInteger() && "This operator does not apply to FP types!");
7691 assert(N1.getValueType() == N2.getValueType() &&
7692 N1.getValueType() == VT && "Binary operator types must match!");
7693 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7694 // worth handling here.
7695 if (N2CV && N2CV->isZero())
7696 return N2;
7697 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7698 return N1;
7699 break;
7700 case ISD::OR:
7701 case ISD::XOR:
7702 case ISD::ADD:
7703 case ISD::PTRADD:
7704 case ISD::SUB:
7705 assert(VT.isInteger() && "This operator does not apply to FP types!");
7706 assert(N1.getValueType() == N2.getValueType() &&
7707 N1.getValueType() == VT && "Binary operator types must match!");
7708 // The equal operand types requirement is unnecessarily strong for PTRADD.
7709 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7710 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7711 // logic everywhere where PTRADDs may be folded or combined to properly
7712 // support them. If/when we introduce pointer types to the SDAG, we will
7713 // need to relax this constraint.
7714
7715 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7716 // it's worth handling here.
7717 if (N2CV && N2CV->isZero())
7718 return N1;
7719 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7720 VT.getScalarType() == MVT::i1)
7721 return getNode(ISD::XOR, DL, VT, N1, N2);
7722 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7723 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7724 N2.getOpcode() == ISD::VSCALE) {
7725 const APInt &C1 = N1->getConstantOperandAPInt(0);
7726 const APInt &C2 = N2->getConstantOperandAPInt(0);
7727 return getVScale(DL, VT, C1 + C2);
7728 }
7729 break;
7730 case ISD::MUL:
7731 assert(VT.isInteger() && "This operator does not apply to FP types!");
7732 assert(N1.getValueType() == N2.getValueType() &&
7733 N1.getValueType() == VT && "Binary operator types must match!");
7734 if (VT.getScalarType() == MVT::i1)
7735 return getNode(ISD::AND, DL, VT, N1, N2);
7736 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7737 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7738 const APInt &N2CImm = N2C->getAPIntValue();
7739 return getVScale(DL, VT, MulImm * N2CImm);
7740 }
7741 break;
7742 case ISD::UDIV:
7743 case ISD::UREM:
7744 case ISD::MULHU:
7745 case ISD::MULHS:
7746 case ISD::SDIV:
7747 case ISD::SREM:
7748 case ISD::SADDSAT:
7749 case ISD::SSUBSAT:
7750 case ISD::UADDSAT:
7751 case ISD::USUBSAT:
7752 assert(VT.isInteger() && "This operator does not apply to FP types!");
7753 assert(N1.getValueType() == N2.getValueType() &&
7754 N1.getValueType() == VT && "Binary operator types must match!");
7755 if (VT.getScalarType() == MVT::i1) {
7756 // fold (add_sat x, y) -> (or x, y) for bool types.
7757 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7758 return getNode(ISD::OR, DL, VT, N1, N2);
7759 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7760 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7761 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7762 }
7763 break;
7764 case ISD::SCMP:
7765 case ISD::UCMP:
7766 assert(N1.getValueType() == N2.getValueType() &&
7767 "Types of operands of UCMP/SCMP must match");
7768 assert(N1.getValueType().isVector() == VT.isVector() &&
7769 "Operands and return type of must both be scalars or vectors");
7770 if (VT.isVector())
7773 "Result and operands must have the same number of elements");
7774 break;
7775 case ISD::AVGFLOORS:
7776 case ISD::AVGFLOORU:
7777 case ISD::AVGCEILS:
7778 case ISD::AVGCEILU:
7779 assert(VT.isInteger() && "This operator does not apply to FP types!");
7780 assert(N1.getValueType() == N2.getValueType() &&
7781 N1.getValueType() == VT && "Binary operator types must match!");
7782 break;
7783 case ISD::ABDS:
7784 case ISD::ABDU:
7785 assert(VT.isInteger() && "This operator does not apply to FP types!");
7786 assert(N1.getValueType() == N2.getValueType() &&
7787 N1.getValueType() == VT && "Binary operator types must match!");
7788 if (VT.getScalarType() == MVT::i1)
7789 return getNode(ISD::XOR, DL, VT, N1, N2);
7790 break;
7791 case ISD::SMIN:
7792 case ISD::UMAX:
7793 assert(VT.isInteger() && "This operator does not apply to FP types!");
7794 assert(N1.getValueType() == N2.getValueType() &&
7795 N1.getValueType() == VT && "Binary operator types must match!");
7796 if (VT.getScalarType() == MVT::i1)
7797 return getNode(ISD::OR, DL, VT, N1, N2);
7798 break;
7799 case ISD::SMAX:
7800 case ISD::UMIN:
7801 assert(VT.isInteger() && "This operator does not apply to FP types!");
7802 assert(N1.getValueType() == N2.getValueType() &&
7803 N1.getValueType() == VT && "Binary operator types must match!");
7804 if (VT.getScalarType() == MVT::i1)
7805 return getNode(ISD::AND, DL, VT, N1, N2);
7806 break;
7807 case ISD::FADD:
7808 case ISD::FSUB:
7809 case ISD::FMUL:
7810 case ISD::FDIV:
7811 case ISD::FREM:
7812 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7813 assert(N1.getValueType() == N2.getValueType() &&
7814 N1.getValueType() == VT && "Binary operator types must match!");
7815 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7816 return V;
7817 break;
7818 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7819 assert(N1.getValueType() == VT &&
7822 "Invalid FCOPYSIGN!");
7823 break;
7824 case ISD::SHL:
7825 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7826 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7827 const APInt &ShiftImm = N2C->getAPIntValue();
7828 return getVScale(DL, VT, MulImm << ShiftImm);
7829 }
7830 [[fallthrough]];
7831 case ISD::SRA:
7832 case ISD::SRL:
7833 if (SDValue V = simplifyShift(N1, N2))
7834 return V;
7835 [[fallthrough]];
7836 case ISD::ROTL:
7837 case ISD::ROTR:
7838 assert(VT == N1.getValueType() &&
7839 "Shift operators return type must be the same as their first arg");
7840 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7841 "Shifts only work on integers");
7842 assert((!VT.isVector() || VT == N2.getValueType()) &&
7843 "Vector shift amounts must be in the same as their first arg");
7844 // Verify that the shift amount VT is big enough to hold valid shift
7845 // amounts. This catches things like trying to shift an i1024 value by an
7846 // i8, which is easy to fall into in generic code that uses
7847 // TLI.getShiftAmount().
7850 "Invalid use of small shift amount with oversized value!");
7851
7852 // Always fold shifts of i1 values so the code generator doesn't need to
7853 // handle them. Since we know the size of the shift has to be less than the
7854 // size of the value, the shift/rotate count is guaranteed to be zero.
7855 if (VT == MVT::i1)
7856 return N1;
7857 if (N2CV && N2CV->isZero())
7858 return N1;
7859 break;
7860 case ISD::FP_ROUND:
7862 VT.bitsLE(N1.getValueType()) && N2C &&
7863 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7864 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7865 if (N1.getValueType() == VT) return N1; // noop conversion.
7866 break;
7867 case ISD::AssertNoFPClass: {
7869 "AssertNoFPClass is used for a non-floating type");
7870 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7871 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7872 assert(llvm::to_underlying(NoFPClass) <=
7874 "FPClassTest value too large");
7875 (void)NoFPClass;
7876 break;
7877 }
7878 case ISD::AssertSext:
7879 case ISD::AssertZext: {
7880 EVT EVT = cast<VTSDNode>(N2)->getVT();
7881 assert(VT == N1.getValueType() && "Not an inreg extend!");
7882 assert(VT.isInteger() && EVT.isInteger() &&
7883 "Cannot *_EXTEND_INREG FP types");
7884 assert(!EVT.isVector() &&
7885 "AssertSExt/AssertZExt type should be the vector element type "
7886 "rather than the vector type!");
7887 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7888 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7889 break;
7890 }
7892 EVT EVT = cast<VTSDNode>(N2)->getVT();
7893 assert(VT == N1.getValueType() && "Not an inreg extend!");
7894 assert(VT.isInteger() && EVT.isInteger() &&
7895 "Cannot *_EXTEND_INREG FP types");
7896 assert(EVT.isVector() == VT.isVector() &&
7897 "SIGN_EXTEND_INREG type should be vector iff the operand "
7898 "type is vector!");
7899 assert((!EVT.isVector() ||
7901 "Vector element counts must match in SIGN_EXTEND_INREG");
7902 assert(EVT.bitsLE(VT) && "Not extending!");
7903 if (EVT == VT) return N1; // Not actually extending
7904 break;
7905 }
7907 case ISD::FP_TO_UINT_SAT: {
7908 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7909 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7910 assert(N1.getValueType().isVector() == VT.isVector() &&
7911 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7912 "vector!");
7913 assert((!VT.isVector() || VT.getVectorElementCount() ==
7915 "Vector element counts must match in FP_TO_*INT_SAT");
7916 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7917 "Type to saturate to must be a scalar.");
7918 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7919 "Not extending!");
7920 break;
7921 }
7924 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7925 element type of the vector.");
7926
7927 // Extract from an undefined value or using an undefined index is undefined.
7928 if (N1.isUndef() || N2.isUndef())
7929 return getUNDEF(VT);
7930
7931 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7932 // vectors. For scalable vectors we will provide appropriate support for
7933 // dealing with arbitrary indices.
7934 if (N2C && N1.getValueType().isFixedLengthVector() &&
7935 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7936 return getUNDEF(VT);
7937
7938 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7939 // expanding copies of large vectors from registers. This only works for
7940 // fixed length vectors, since we need to know the exact number of
7941 // elements.
7942 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7944 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7945 return getExtractVectorElt(DL, VT,
7946 N1.getOperand(N2C->getZExtValue() / Factor),
7947 N2C->getZExtValue() % Factor);
7948 }
7949
7950 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7951 // lowering is expanding large vector constants.
7952 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7953 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7956 "BUILD_VECTOR used for scalable vectors");
7957 unsigned Index =
7958 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7959 SDValue Elt = N1.getOperand(Index);
7960
7961 if (VT != Elt.getValueType())
7962 // If the vector element type is not legal, the BUILD_VECTOR operands
7963 // are promoted and implicitly truncated, and the result implicitly
7964 // extended. Make that explicit here.
7965 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7966
7967 return Elt;
7968 }
7969
7970 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7971 // operations are lowered to scalars.
7972 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7973 // If the indices are the same, return the inserted element else
7974 // if the indices are known different, extract the element from
7975 // the original vector.
7976 SDValue N1Op2 = N1.getOperand(2);
7978
7979 if (N1Op2C && N2C) {
7980 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7981 if (VT == N1.getOperand(1).getValueType())
7982 return N1.getOperand(1);
7983 if (VT.isFloatingPoint()) {
7985 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7986 }
7987 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7988 }
7989 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7990 }
7991 }
7992
7993 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7994 // when vector types are scalarized and v1iX is legal.
7995 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7996 // Here we are completely ignoring the extract element index (N2),
7997 // which is fine for fixed width vectors, since any index other than 0
7998 // is undefined anyway. However, this cannot be ignored for scalable
7999 // vectors - in theory we could support this, but we don't want to do this
8000 // without a profitability check.
8001 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8003 N1.getValueType().getVectorNumElements() == 1) {
8004 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8005 N1.getOperand(1));
8006 }
8007 break;
8009 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8010 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8011 (N1.getValueType().isInteger() == VT.isInteger()) &&
8012 N1.getValueType() != VT &&
8013 "Wrong types for EXTRACT_ELEMENT!");
8014
8015 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8016 // 64-bit integers into 32-bit parts. Instead of building the extract of
8017 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8018 if (N1.getOpcode() == ISD::BUILD_PAIR)
8019 return N1.getOperand(N2C->getZExtValue());
8020
8021 // EXTRACT_ELEMENT of a constant int is also very common.
8022 if (N1C) {
8023 unsigned ElementSize = VT.getSizeInBits();
8024 unsigned Shift = ElementSize * N2C->getZExtValue();
8025 const APInt &Val = N1C->getAPIntValue();
8026 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8027 }
8028 break;
8030 EVT N1VT = N1.getValueType();
8031 assert(VT.isVector() && N1VT.isVector() &&
8032 "Extract subvector VTs must be vectors!");
8034 "Extract subvector VTs must have the same element type!");
8035 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8036 "Cannot extract a scalable vector from a fixed length vector!");
8037 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8039 "Extract subvector must be from larger vector to smaller vector!");
8040 assert(N2C && "Extract subvector index must be a constant");
8041 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8042 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8043 N1VT.getVectorMinNumElements()) &&
8044 "Extract subvector overflow!");
8045 assert(N2C->getAPIntValue().getBitWidth() ==
8046 TLI->getVectorIdxWidth(getDataLayout()) &&
8047 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8048 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8049 "Extract index is not a multiple of the output vector length");
8050
8051 // Trivial extraction.
8052 if (VT == N1VT)
8053 return N1;
8054
8055 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8056 if (N1.isUndef())
8057 return getUNDEF(VT);
8058
8059 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8060 // the concat have the same type as the extract.
8061 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8062 VT == N1.getOperand(0).getValueType()) {
8063 unsigned Factor = VT.getVectorMinNumElements();
8064 return N1.getOperand(N2C->getZExtValue() / Factor);
8065 }
8066
8067 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8068 // during shuffle legalization.
8069 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8070 VT == N1.getOperand(1).getValueType())
8071 return N1.getOperand(1);
8072 break;
8073 }
8074 }
8075
8076 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8077 switch (Opcode) {
8078 case ISD::XOR:
8079 case ISD::ADD:
8080 case ISD::PTRADD:
8081 case ISD::SUB:
8083 case ISD::UDIV:
8084 case ISD::SDIV:
8085 case ISD::UREM:
8086 case ISD::SREM:
8087 case ISD::MUL:
8088 case ISD::AND:
8089 case ISD::SSUBSAT:
8090 case ISD::USUBSAT:
8091 case ISD::UMIN:
8092 case ISD::OR:
8093 case ISD::SADDSAT:
8094 case ISD::UADDSAT:
8095 case ISD::UMAX:
8096 case ISD::SMAX:
8097 case ISD::SMIN:
8098 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8099 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8100 }
8101 }
8102
8103 // Canonicalize an UNDEF to the RHS, even over a constant.
8104 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8105 if (TLI->isCommutativeBinOp(Opcode)) {
8106 std::swap(N1, N2);
8107 } else {
8108 switch (Opcode) {
8109 case ISD::PTRADD:
8110 case ISD::SUB:
8111 // fold op(undef, non_undef_arg2) -> undef.
8112 return N1;
8114 case ISD::UDIV:
8115 case ISD::SDIV:
8116 case ISD::UREM:
8117 case ISD::SREM:
8118 case ISD::SSUBSAT:
8119 case ISD::USUBSAT:
8120 // fold op(undef, non_undef_arg2) -> 0.
8121 return getConstant(0, DL, VT);
8122 }
8123 }
8124 }
8125
8126 // Fold a bunch of operators when the RHS is undef.
8127 if (N2.getOpcode() == ISD::UNDEF) {
8128 switch (Opcode) {
8129 case ISD::XOR:
8130 if (N1.getOpcode() == ISD::UNDEF)
8131 // Handle undef ^ undef -> 0 special case. This is a common
8132 // idiom (misuse).
8133 return getConstant(0, DL, VT);
8134 [[fallthrough]];
8135 case ISD::ADD:
8136 case ISD::PTRADD:
8137 case ISD::SUB:
8138 // fold op(arg1, undef) -> undef.
8139 return N2;
8140 case ISD::UDIV:
8141 case ISD::SDIV:
8142 case ISD::UREM:
8143 case ISD::SREM:
8144 // fold op(arg1, undef) -> poison.
8145 return getPOISON(VT);
8146 case ISD::MUL:
8147 case ISD::AND:
8148 case ISD::SSUBSAT:
8149 case ISD::USUBSAT:
8150 case ISD::UMIN:
8151 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8152 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8153 case ISD::OR:
8154 case ISD::SADDSAT:
8155 case ISD::UADDSAT:
8156 case ISD::UMAX:
8157 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8158 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8159 case ISD::SMAX:
8160 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8161 return N1.getOpcode() == ISD::UNDEF
8162 ? N2
8163 : getConstant(
8165 VT);
8166 case ISD::SMIN:
8167 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8168 return N1.getOpcode() == ISD::UNDEF
8169 ? N2
8170 : getConstant(
8172 VT);
8173 }
8174 }
8175
8176 // Perform trivial constant folding.
8177 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8178 return SV;
8179
8180 // Memoize this node if possible.
8181 SDNode *N;
8182 SDVTList VTs = getVTList(VT);
8183 SDValue Ops[] = {N1, N2};
8184 if (VT != MVT::Glue) {
8186 AddNodeIDNode(ID, Opcode, VTs, Ops);
8187 void *IP = nullptr;
8188 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8189 E->intersectFlagsWith(Flags);
8190 return SDValue(E, 0);
8191 }
8192
8193 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8194 N->setFlags(Flags);
8195 createOperands(N, Ops);
8196 CSEMap.InsertNode(N, IP);
8197 } else {
8198 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8199 createOperands(N, Ops);
8200 }
8201
8202 InsertNode(N);
8203 SDValue V = SDValue(N, 0);
8204 NewSDValueDbgMsg(V, "Creating new node: ", this);
8205 return V;
8206}
8207
8208SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8209 SDValue N1, SDValue N2, SDValue N3) {
8210 SDNodeFlags Flags;
8211 if (Inserter)
8212 Flags = Inserter->getFlags();
8213 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8214}
8215
8216SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8217 SDValue N1, SDValue N2, SDValue N3,
8218 const SDNodeFlags Flags) {
8220 N2.getOpcode() != ISD::DELETED_NODE &&
8221 N3.getOpcode() != ISD::DELETED_NODE &&
8222 "Operand is DELETED_NODE!");
8223 // Perform various simplifications.
8224 switch (Opcode) {
8225 case ISD::BUILD_VECTOR: {
8226 // Attempt to simplify BUILD_VECTOR.
8227 SDValue Ops[] = {N1, N2, N3};
8228 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8229 return V;
8230 break;
8231 }
8232 case ISD::CONCAT_VECTORS: {
8233 SDValue Ops[] = {N1, N2, N3};
8234 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8235 return V;
8236 break;
8237 }
8238 case ISD::SETCC: {
8239 assert(VT.isInteger() && "SETCC result type must be an integer!");
8240 assert(N1.getValueType() == N2.getValueType() &&
8241 "SETCC operands must have the same type!");
8242 assert(VT.isVector() == N1.getValueType().isVector() &&
8243 "SETCC type should be vector iff the operand type is vector!");
8244 assert((!VT.isVector() || VT.getVectorElementCount() ==
8246 "SETCC vector element counts must match!");
8247 // Use FoldSetCC to simplify SETCC's.
8248 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8249 return V;
8250 break;
8251 }
8252 case ISD::SELECT:
8253 case ISD::VSELECT:
8254 if (SDValue V = simplifySelect(N1, N2, N3))
8255 return V;
8256 break;
8258 llvm_unreachable("should use getVectorShuffle constructor!");
8259 case ISD::VECTOR_SPLICE: {
8260 if (cast<ConstantSDNode>(N3)->isZero())
8261 return N1;
8262 break;
8263 }
8265 assert(VT.isVector() && VT == N1.getValueType() &&
8266 "INSERT_VECTOR_ELT vector type mismatch");
8268 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8269 assert((!VT.isFloatingPoint() ||
8270 VT.getVectorElementType() == N2.getValueType()) &&
8271 "INSERT_VECTOR_ELT fp scalar type mismatch");
8272 assert((!VT.isInteger() ||
8274 "INSERT_VECTOR_ELT int scalar size mismatch");
8275
8276 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8277 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8278 // for scalable vectors where we will generate appropriate code to
8279 // deal with out-of-bounds cases correctly.
8280 if (N3C && VT.isFixedLengthVector() &&
8281 N3C->getZExtValue() >= VT.getVectorNumElements())
8282 return getUNDEF(VT);
8283
8284 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8285 if (N3.isUndef())
8286 return getUNDEF(VT);
8287
8288 // If inserting poison, just use the input vector.
8289 if (N2.getOpcode() == ISD::POISON)
8290 return N1;
8291
8292 // Inserting undef into undef/poison is still undef.
8293 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8294 return getUNDEF(VT);
8295
8296 // If the inserted element is an UNDEF, just use the input vector.
8297 // But not if skipping the insert could make the result more poisonous.
8298 if (N2.isUndef()) {
8299 if (N3C && VT.isFixedLengthVector()) {
8300 APInt EltMask =
8301 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8302 if (isGuaranteedNotToBePoison(N1, EltMask))
8303 return N1;
8304 } else if (isGuaranteedNotToBePoison(N1))
8305 return N1;
8306 }
8307 break;
8308 }
8309 case ISD::INSERT_SUBVECTOR: {
8310 // If inserting poison, just use the input vector,
8311 if (N2.getOpcode() == ISD::POISON)
8312 return N1;
8313
8314 // Inserting undef into undef/poison is still undef.
8315 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8316 return getUNDEF(VT);
8317
8318 EVT N2VT = N2.getValueType();
8319 assert(VT == N1.getValueType() &&
8320 "Dest and insert subvector source types must match!");
8321 assert(VT.isVector() && N2VT.isVector() &&
8322 "Insert subvector VTs must be vectors!");
8324 "Insert subvector VTs must have the same element type!");
8325 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8326 "Cannot insert a scalable vector into a fixed length vector!");
8327 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8329 "Insert subvector must be from smaller vector to larger vector!");
8331 "Insert subvector index must be constant");
8332 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8333 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8335 "Insert subvector overflow!");
8337 TLI->getVectorIdxWidth(getDataLayout()) &&
8338 "Constant index for INSERT_SUBVECTOR has an invalid size");
8339
8340 // Trivial insertion.
8341 if (VT == N2VT)
8342 return N2;
8343
8344 // If this is an insert of an extracted vector into an undef/poison vector,
8345 // we can just use the input to the extract. But not if skipping the
8346 // extract+insert could make the result more poisonous.
8347 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8348 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8349 if (N1.getOpcode() == ISD::POISON)
8350 return N2.getOperand(0);
8351 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8352 unsigned LoBit = N3->getAsZExtVal();
8353 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8354 APInt EltMask =
8355 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8356 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8357 return N2.getOperand(0);
8358 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8359 return N2.getOperand(0);
8360 }
8361
8362 // If the inserted subvector is UNDEF, just use the input vector.
8363 // But not if skipping the insert could make the result more poisonous.
8364 if (N2.isUndef()) {
8365 if (VT.isFixedLengthVector()) {
8366 unsigned LoBit = N3->getAsZExtVal();
8367 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8368 APInt EltMask =
8369 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8370 if (isGuaranteedNotToBePoison(N1, EltMask))
8371 return N1;
8372 } else if (isGuaranteedNotToBePoison(N1))
8373 return N1;
8374 }
8375 break;
8376 }
8377 case ISD::BITCAST:
8378 // Fold bit_convert nodes from a type to themselves.
8379 if (N1.getValueType() == VT)
8380 return N1;
8381 break;
8382 case ISD::VP_TRUNCATE:
8383 case ISD::VP_SIGN_EXTEND:
8384 case ISD::VP_ZERO_EXTEND:
8385 // Don't create noop casts.
8386 if (N1.getValueType() == VT)
8387 return N1;
8388 break;
8389 case ISD::VECTOR_COMPRESS: {
8390 [[maybe_unused]] EVT VecVT = N1.getValueType();
8391 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8392 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8393 assert(VT == VecVT && "Vector and result type don't match.");
8394 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8395 "All inputs must be vectors.");
8396 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8398 "Vector and mask must have same number of elements.");
8399
8400 if (N1.isUndef() || N2.isUndef())
8401 return N3;
8402
8403 break;
8404 }
8405 case ISD::PARTIAL_REDUCE_UMLA:
8406 case ISD::PARTIAL_REDUCE_SMLA:
8407 case ISD::PARTIAL_REDUCE_SUMLA:
8408 case ISD::PARTIAL_REDUCE_FMLA: {
8409 [[maybe_unused]] EVT AccVT = N1.getValueType();
8410 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8411 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8412 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8413 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8414 "node to have the same type!");
8415 assert(VT.isVector() && VT == AccVT &&
8416 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8417 "the same type as its result!");
8419 AccVT.getVectorElementCount()) &&
8420 "Expected the element count of the second and third operands of the "
8421 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8422 "element count of the first operand and the result!");
8424 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8425 "node to have an element type which is the same as or smaller than "
8426 "the element type of the first operand and result!");
8427 break;
8428 }
8429 }
8430
8431 // Perform trivial constant folding for arithmetic operators.
8432 switch (Opcode) {
8433 case ISD::FMA:
8434 case ISD::FMAD:
8435 case ISD::SETCC:
8436 case ISD::FSHL:
8437 case ISD::FSHR:
8438 if (SDValue SV =
8439 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8440 return SV;
8441 break;
8442 }
8443
8444 // Memoize node if it doesn't produce a glue result.
8445 SDNode *N;
8446 SDVTList VTs = getVTList(VT);
8447 SDValue Ops[] = {N1, N2, N3};
8448 if (VT != MVT::Glue) {
8450 AddNodeIDNode(ID, Opcode, VTs, Ops);
8451 void *IP = nullptr;
8452 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8453 E->intersectFlagsWith(Flags);
8454 return SDValue(E, 0);
8455 }
8456
8457 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8458 N->setFlags(Flags);
8459 createOperands(N, Ops);
8460 CSEMap.InsertNode(N, IP);
8461 } else {
8462 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8463 createOperands(N, Ops);
8464 }
8465
8466 InsertNode(N);
8467 SDValue V = SDValue(N, 0);
8468 NewSDValueDbgMsg(V, "Creating new node: ", this);
8469 return V;
8470}
8471
8472SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8473 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8474 const SDNodeFlags Flags) {
8475 SDValue Ops[] = { N1, N2, N3, N4 };
8476 return getNode(Opcode, DL, VT, Ops, Flags);
8477}
8478
8479SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8480 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8481 SDNodeFlags Flags;
8482 if (Inserter)
8483 Flags = Inserter->getFlags();
8484 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8485}
8486
8487SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8488 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8489 SDValue N5, const SDNodeFlags Flags) {
8490 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8491 return getNode(Opcode, DL, VT, Ops, Flags);
8492}
8493
8494SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8495 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8496 SDValue N5) {
8497 SDNodeFlags Flags;
8498 if (Inserter)
8499 Flags = Inserter->getFlags();
8500 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8501}
8502
8503/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8504/// the incoming stack arguments to be loaded from the stack.
8506 SmallVector<SDValue, 8> ArgChains;
8507
8508 // Include the original chain at the beginning of the list. When this is
8509 // used by target LowerCall hooks, this helps legalize find the
8510 // CALLSEQ_BEGIN node.
8511 ArgChains.push_back(Chain);
8512
8513 // Add a chain value for each stack argument.
8514 for (SDNode *U : getEntryNode().getNode()->users())
8515 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8516 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8517 if (FI->getIndex() < 0)
8518 ArgChains.push_back(SDValue(L, 1));
8519
8520 // Build a tokenfactor for all the chains.
8521 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8522}
8523
8524/// getMemsetValue - Vectorized representation of the memset value
8525/// operand.
8527 const SDLoc &dl) {
8528 assert(!Value.isUndef());
8529
8530 unsigned NumBits = VT.getScalarSizeInBits();
8532 assert(C->getAPIntValue().getBitWidth() == 8);
8533 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8534 if (VT.isInteger()) {
8535 bool IsOpaque = VT.getSizeInBits() > 64 ||
8536 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8537 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8538 }
8539 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8540 }
8541
8542 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8543 EVT IntVT = VT.getScalarType();
8544 if (!IntVT.isInteger())
8545 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8546
8547 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8548 if (NumBits > 8) {
8549 // Use a multiplication with 0x010101... to extend the input to the
8550 // required length.
8551 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8552 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8553 DAG.getConstant(Magic, dl, IntVT));
8554 }
8555
8556 if (VT != Value.getValueType() && !VT.isInteger())
8557 Value = DAG.getBitcast(VT.getScalarType(), Value);
8558 if (VT != Value.getValueType())
8559 Value = DAG.getSplatBuildVector(VT, dl, Value);
8560
8561 return Value;
8562}
8563
8564/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8565/// used when a memcpy is turned into a memset when the source is a constant
8566/// string ptr.
8568 const TargetLowering &TLI,
8569 const ConstantDataArraySlice &Slice) {
8570 // Handle vector with all elements zero.
8571 if (Slice.Array == nullptr) {
8572 if (VT.isInteger())
8573 return DAG.getConstant(0, dl, VT);
8574 return DAG.getNode(ISD::BITCAST, dl, VT,
8575 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8576 }
8577
8578 assert(!VT.isVector() && "Can't handle vector type here!");
8579 unsigned NumVTBits = VT.getSizeInBits();
8580 unsigned NumVTBytes = NumVTBits / 8;
8581 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8582
8583 APInt Val(NumVTBits, 0);
8584 if (DAG.getDataLayout().isLittleEndian()) {
8585 for (unsigned i = 0; i != NumBytes; ++i)
8586 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8587 } else {
8588 for (unsigned i = 0; i != NumBytes; ++i)
8589 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8590 }
8591
8592 // If the "cost" of materializing the integer immediate is less than the cost
8593 // of a load, then it is cost effective to turn the load into the immediate.
8594 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8595 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8596 return DAG.getConstant(Val, dl, VT);
8597 return SDValue();
8598}
8599
8601 const SDLoc &DL,
8602 const SDNodeFlags Flags) {
8603 EVT VT = Base.getValueType();
8604 SDValue Index;
8605
8606 if (Offset.isScalable())
8607 Index = getVScale(DL, Base.getValueType(),
8608 APInt(Base.getValueSizeInBits().getFixedValue(),
8609 Offset.getKnownMinValue()));
8610 else
8611 Index = getConstant(Offset.getFixedValue(), DL, VT);
8612
8613 return getMemBasePlusOffset(Base, Index, DL, Flags);
8614}
8615
8617 const SDLoc &DL,
8618 const SDNodeFlags Flags) {
8619 assert(Offset.getValueType().isInteger());
8620 EVT BasePtrVT = Ptr.getValueType();
8621 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8622 BasePtrVT))
8623 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8624 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8625 SDNodeFlags AddFlags = Flags;
8626 AddFlags.setInBounds(false);
8627 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8628}
8629
8630/// Returns true if memcpy source is constant data.
8632 uint64_t SrcDelta = 0;
8633 GlobalAddressSDNode *G = nullptr;
8634 if (Src.getOpcode() == ISD::GlobalAddress)
8636 else if (Src->isAnyAdd() &&
8637 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8638 Src.getOperand(1).getOpcode() == ISD::Constant) {
8639 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8640 SrcDelta = Src.getConstantOperandVal(1);
8641 }
8642 if (!G)
8643 return false;
8644
8645 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8646 SrcDelta + G->getOffset());
8647}
8648
8650 SelectionDAG &DAG) {
8651 // On Darwin, -Os means optimize for size without hurting performance, so
8652 // only really optimize for size when -Oz (MinSize) is used.
8654 return MF.getFunction().hasMinSize();
8655 return DAG.shouldOptForSize();
8656}
8657
8659 SmallVector<SDValue, 32> &OutChains, unsigned From,
8660 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8661 SmallVector<SDValue, 16> &OutStoreChains) {
8662 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8663 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8664 SmallVector<SDValue, 16> GluedLoadChains;
8665 for (unsigned i = From; i < To; ++i) {
8666 OutChains.push_back(OutLoadChains[i]);
8667 GluedLoadChains.push_back(OutLoadChains[i]);
8668 }
8669
8670 // Chain for all loads.
8671 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8672 GluedLoadChains);
8673
8674 for (unsigned i = From; i < To; ++i) {
8675 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8676 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8677 ST->getBasePtr(), ST->getMemoryVT(),
8678 ST->getMemOperand());
8679 OutChains.push_back(NewStore);
8680 }
8681}
8682
8684 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8685 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8686 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8687 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8688 // Turn a memcpy of undef to nop.
8689 // FIXME: We need to honor volatile even is Src is undef.
8690 if (Src.isUndef())
8691 return Chain;
8692
8693 // Expand memcpy to a series of load and store ops if the size operand falls
8694 // below a certain threshold.
8695 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8696 // rather than maybe a humongous number of loads and stores.
8697 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8698 const DataLayout &DL = DAG.getDataLayout();
8699 LLVMContext &C = *DAG.getContext();
8700 std::vector<EVT> MemOps;
8701 bool DstAlignCanChange = false;
8703 MachineFrameInfo &MFI = MF.getFrameInfo();
8704 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8706 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8707 DstAlignCanChange = true;
8708 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8709 if (!SrcAlign || Alignment > *SrcAlign)
8710 SrcAlign = Alignment;
8711 assert(SrcAlign && "SrcAlign must be set");
8713 // If marked as volatile, perform a copy even when marked as constant.
8714 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8715 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8716 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8717 const MemOp Op = isZeroConstant
8718 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8719 /*IsZeroMemset*/ true, isVol)
8720 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8721 *SrcAlign, isVol, CopyFromConstant);
8722 if (!TLI.findOptimalMemOpLowering(
8723 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8724 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8725 return SDValue();
8726
8727 if (DstAlignCanChange) {
8728 Type *Ty = MemOps[0].getTypeForEVT(C);
8729 Align NewAlign = DL.getABITypeAlign(Ty);
8730
8731 // Don't promote to an alignment that would require dynamic stack
8732 // realignment which may conflict with optimizations such as tail call
8733 // optimization.
8735 if (!TRI->hasStackRealignment(MF))
8736 if (MaybeAlign StackAlign = DL.getStackAlignment())
8737 NewAlign = std::min(NewAlign, *StackAlign);
8738
8739 if (NewAlign > Alignment) {
8740 // Give the stack frame object a larger alignment if needed.
8741 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8742 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8743 Alignment = NewAlign;
8744 }
8745 }
8746
8747 // Prepare AAInfo for loads/stores after lowering this memcpy.
8748 AAMDNodes NewAAInfo = AAInfo;
8749 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8750
8751 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8752 bool isConstant =
8753 BatchAA && SrcVal &&
8754 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8755
8756 MachineMemOperand::Flags MMOFlags =
8758 SmallVector<SDValue, 16> OutLoadChains;
8759 SmallVector<SDValue, 16> OutStoreChains;
8760 SmallVector<SDValue, 32> OutChains;
8761 unsigned NumMemOps = MemOps.size();
8762 uint64_t SrcOff = 0, DstOff = 0;
8763 for (unsigned i = 0; i != NumMemOps; ++i) {
8764 EVT VT = MemOps[i];
8765 unsigned VTSize = VT.getSizeInBits() / 8;
8766 SDValue Value, Store;
8767
8768 if (VTSize > Size) {
8769 // Issuing an unaligned load / store pair that overlaps with the previous
8770 // pair. Adjust the offset accordingly.
8771 assert(i == NumMemOps-1 && i != 0);
8772 SrcOff -= VTSize - Size;
8773 DstOff -= VTSize - Size;
8774 }
8775
8776 if (CopyFromConstant &&
8777 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8778 // It's unlikely a store of a vector immediate can be done in a single
8779 // instruction. It would require a load from a constantpool first.
8780 // We only handle zero vectors here.
8781 // FIXME: Handle other cases where store of vector immediate is done in
8782 // a single instruction.
8783 ConstantDataArraySlice SubSlice;
8784 if (SrcOff < Slice.Length) {
8785 SubSlice = Slice;
8786 SubSlice.move(SrcOff);
8787 } else {
8788 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8789 SubSlice.Array = nullptr;
8790 SubSlice.Offset = 0;
8791 SubSlice.Length = VTSize;
8792 }
8793 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8794 if (Value.getNode()) {
8795 Store = DAG.getStore(
8796 Chain, dl, Value,
8797 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8798 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8799 OutChains.push_back(Store);
8800 }
8801 }
8802
8803 if (!Store.getNode()) {
8804 // The type might not be legal for the target. This should only happen
8805 // if the type is smaller than a legal type, as on PPC, so the right
8806 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8807 // to Load/Store if NVT==VT.
8808 // FIXME does the case above also need this?
8809 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8810 assert(NVT.bitsGE(VT));
8811
8812 bool isDereferenceable =
8813 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8814 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8815 if (isDereferenceable)
8817 if (isConstant)
8818 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8819
8820 Value = DAG.getExtLoad(
8821 ISD::EXTLOAD, dl, NVT, Chain,
8822 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8823 SrcPtrInfo.getWithOffset(SrcOff), VT,
8824 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8825 OutLoadChains.push_back(Value.getValue(1));
8826
8827 Store = DAG.getTruncStore(
8828 Chain, dl, Value,
8829 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8830 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8831 OutStoreChains.push_back(Store);
8832 }
8833 SrcOff += VTSize;
8834 DstOff += VTSize;
8835 Size -= VTSize;
8836 }
8837
8838 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8840 unsigned NumLdStInMemcpy = OutStoreChains.size();
8841
8842 if (NumLdStInMemcpy) {
8843 // It may be that memcpy might be converted to memset if it's memcpy
8844 // of constants. In such a case, we won't have loads and stores, but
8845 // just stores. In the absence of loads, there is nothing to gang up.
8846 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8847 // If target does not care, just leave as it.
8848 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8849 OutChains.push_back(OutLoadChains[i]);
8850 OutChains.push_back(OutStoreChains[i]);
8851 }
8852 } else {
8853 // Ld/St less than/equal limit set by target.
8854 if (NumLdStInMemcpy <= GluedLdStLimit) {
8855 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8856 NumLdStInMemcpy, OutLoadChains,
8857 OutStoreChains);
8858 } else {
8859 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8860 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8861 unsigned GlueIter = 0;
8862
8863 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8864 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8865 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8866
8867 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8868 OutLoadChains, OutStoreChains);
8869 GlueIter += GluedLdStLimit;
8870 }
8871
8872 // Residual ld/st.
8873 if (RemainingLdStInMemcpy) {
8874 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8875 RemainingLdStInMemcpy, OutLoadChains,
8876 OutStoreChains);
8877 }
8878 }
8879 }
8880 }
8881 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8882}
8883
8885 SDValue Chain, SDValue Dst, SDValue Src,
8886 uint64_t Size, Align Alignment,
8887 bool isVol, bool AlwaysInline,
8888 MachinePointerInfo DstPtrInfo,
8889 MachinePointerInfo SrcPtrInfo,
8890 const AAMDNodes &AAInfo) {
8891 // Turn a memmove of undef to nop.
8892 // FIXME: We need to honor volatile even is Src is undef.
8893 if (Src.isUndef())
8894 return Chain;
8895
8896 // Expand memmove to a series of load and store ops if the size operand falls
8897 // below a certain threshold.
8898 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8899 const DataLayout &DL = DAG.getDataLayout();
8900 LLVMContext &C = *DAG.getContext();
8901 std::vector<EVT> MemOps;
8902 bool DstAlignCanChange = false;
8904 MachineFrameInfo &MFI = MF.getFrameInfo();
8905 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8907 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8908 DstAlignCanChange = true;
8909 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8910 if (!SrcAlign || Alignment > *SrcAlign)
8911 SrcAlign = Alignment;
8912 assert(SrcAlign && "SrcAlign must be set");
8913 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8914 if (!TLI.findOptimalMemOpLowering(
8915 C, MemOps, Limit,
8916 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8917 /*IsVolatile*/ true),
8918 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8919 MF.getFunction().getAttributes()))
8920 return SDValue();
8921
8922 if (DstAlignCanChange) {
8923 Type *Ty = MemOps[0].getTypeForEVT(C);
8924 Align NewAlign = DL.getABITypeAlign(Ty);
8925
8926 // Don't promote to an alignment that would require dynamic stack
8927 // realignment which may conflict with optimizations such as tail call
8928 // optimization.
8930 if (!TRI->hasStackRealignment(MF))
8931 if (MaybeAlign StackAlign = DL.getStackAlignment())
8932 NewAlign = std::min(NewAlign, *StackAlign);
8933
8934 if (NewAlign > Alignment) {
8935 // Give the stack frame object a larger alignment if needed.
8936 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8937 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8938 Alignment = NewAlign;
8939 }
8940 }
8941
8942 // Prepare AAInfo for loads/stores after lowering this memmove.
8943 AAMDNodes NewAAInfo = AAInfo;
8944 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8945
8946 MachineMemOperand::Flags MMOFlags =
8948 uint64_t SrcOff = 0, DstOff = 0;
8949 SmallVector<SDValue, 8> LoadValues;
8950 SmallVector<SDValue, 8> LoadChains;
8951 SmallVector<SDValue, 8> OutChains;
8952 unsigned NumMemOps = MemOps.size();
8953 for (unsigned i = 0; i < NumMemOps; i++) {
8954 EVT VT = MemOps[i];
8955 unsigned VTSize = VT.getSizeInBits() / 8;
8956 SDValue Value;
8957
8958 bool isDereferenceable =
8959 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8960 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8961 if (isDereferenceable)
8963
8964 Value = DAG.getLoad(
8965 VT, dl, Chain,
8966 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8967 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8968 LoadValues.push_back(Value);
8969 LoadChains.push_back(Value.getValue(1));
8970 SrcOff += VTSize;
8971 }
8972 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8973 OutChains.clear();
8974 for (unsigned i = 0; i < NumMemOps; i++) {
8975 EVT VT = MemOps[i];
8976 unsigned VTSize = VT.getSizeInBits() / 8;
8977 SDValue Store;
8978
8979 Store = DAG.getStore(
8980 Chain, dl, LoadValues[i],
8981 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8982 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8983 OutChains.push_back(Store);
8984 DstOff += VTSize;
8985 }
8986
8987 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8988}
8989
8990/// Lower the call to 'memset' intrinsic function into a series of store
8991/// operations.
8992///
8993/// \param DAG Selection DAG where lowered code is placed.
8994/// \param dl Link to corresponding IR location.
8995/// \param Chain Control flow dependency.
8996/// \param Dst Pointer to destination memory location.
8997/// \param Src Value of byte to write into the memory.
8998/// \param Size Number of bytes to write.
8999/// \param Alignment Alignment of the destination in bytes.
9000/// \param isVol True if destination is volatile.
9001/// \param AlwaysInline Makes sure no function call is generated.
9002/// \param DstPtrInfo IR information on the memory pointer.
9003/// \returns New head in the control flow, if lowering was successful, empty
9004/// SDValue otherwise.
9005///
9006/// The function tries to replace 'llvm.memset' intrinsic with several store
9007/// operations and value calculation code. This is usually profitable for small
9008/// memory size or when the semantic requires inlining.
9010 SDValue Chain, SDValue Dst, SDValue Src,
9011 uint64_t Size, Align Alignment, bool isVol,
9012 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9013 const AAMDNodes &AAInfo) {
9014 // Turn a memset of undef to nop.
9015 // FIXME: We need to honor volatile even is Src is undef.
9016 if (Src.isUndef())
9017 return Chain;
9018
9019 // Expand memset to a series of load/store ops if the size operand
9020 // falls below a certain threshold.
9021 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9022 std::vector<EVT> MemOps;
9023 bool DstAlignCanChange = false;
9024 LLVMContext &C = *DAG.getContext();
9026 MachineFrameInfo &MFI = MF.getFrameInfo();
9027 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9029 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9030 DstAlignCanChange = true;
9031 bool IsZeroVal = isNullConstant(Src);
9032 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9033
9034 if (!TLI.findOptimalMemOpLowering(
9035 C, MemOps, Limit,
9036 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9037 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9038 return SDValue();
9039
9040 if (DstAlignCanChange) {
9041 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9042 const DataLayout &DL = DAG.getDataLayout();
9043 Align NewAlign = DL.getABITypeAlign(Ty);
9044
9045 // Don't promote to an alignment that would require dynamic stack
9046 // realignment which may conflict with optimizations such as tail call
9047 // optimization.
9049 if (!TRI->hasStackRealignment(MF))
9050 if (MaybeAlign StackAlign = DL.getStackAlignment())
9051 NewAlign = std::min(NewAlign, *StackAlign);
9052
9053 if (NewAlign > Alignment) {
9054 // Give the stack frame object a larger alignment if needed.
9055 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9056 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9057 Alignment = NewAlign;
9058 }
9059 }
9060
9061 SmallVector<SDValue, 8> OutChains;
9062 uint64_t DstOff = 0;
9063 unsigned NumMemOps = MemOps.size();
9064
9065 // Find the largest store and generate the bit pattern for it.
9066 EVT LargestVT = MemOps[0];
9067 for (unsigned i = 1; i < NumMemOps; i++)
9068 if (MemOps[i].bitsGT(LargestVT))
9069 LargestVT = MemOps[i];
9070 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9071
9072 // Prepare AAInfo for loads/stores after lowering this memset.
9073 AAMDNodes NewAAInfo = AAInfo;
9074 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9075
9076 for (unsigned i = 0; i < NumMemOps; i++) {
9077 EVT VT = MemOps[i];
9078 unsigned VTSize = VT.getSizeInBits() / 8;
9079 if (VTSize > Size) {
9080 // Issuing an unaligned load / store pair that overlaps with the previous
9081 // pair. Adjust the offset accordingly.
9082 assert(i == NumMemOps-1 && i != 0);
9083 DstOff -= VTSize - Size;
9084 }
9085
9086 // If this store is smaller than the largest store see whether we can get
9087 // the smaller value for free with a truncate or extract vector element and
9088 // then store.
9089 SDValue Value = MemSetValue;
9090 if (VT.bitsLT(LargestVT)) {
9091 unsigned Index;
9092 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9093 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9094 if (!LargestVT.isVector() && !VT.isVector() &&
9095 TLI.isTruncateFree(LargestVT, VT))
9096 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9097 else if (LargestVT.isVector() && !VT.isVector() &&
9099 LargestVT.getTypeForEVT(*DAG.getContext()),
9100 VT.getSizeInBits(), Index) &&
9101 TLI.isTypeLegal(SVT) &&
9102 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9103 // Target which can combine store(extractelement VectorTy, Idx) can get
9104 // the smaller value for free.
9105 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9106 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9107 } else
9108 Value = getMemsetValue(Src, VT, DAG, dl);
9109 }
9110 assert(Value.getValueType() == VT && "Value with wrong type.");
9111 SDValue Store = DAG.getStore(
9112 Chain, dl, Value,
9113 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9114 DstPtrInfo.getWithOffset(DstOff), Alignment,
9116 NewAAInfo);
9117 OutChains.push_back(Store);
9118 DstOff += VT.getSizeInBits() / 8;
9119 Size -= VTSize;
9120 }
9121
9122 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9123}
9124
9126 unsigned AS) {
9127 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9128 // pointer operands can be losslessly bitcasted to pointers of address space 0
9129 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9130 report_fatal_error("cannot lower memory intrinsic in address space " +
9131 Twine(AS));
9132 }
9133}
9134
9136 const SelectionDAG *SelDAG,
9137 bool AllowReturnsFirstArg) {
9138 if (!CI || !CI->isTailCall())
9139 return false;
9140 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9141 // helper symbol we lower to.
9142 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9143 AllowReturnsFirstArg &&
9145}
9146
9147std::pair<SDValue, SDValue>
9149 SDValue Mem1, SDValue Size, const CallInst *CI) {
9150 const char *LibCallName = TLI->getLibcallName(RTLIB::MEMCMP);
9151 if (!LibCallName)
9152 return {};
9153
9156 {Mem0, PT},
9157 {Mem1, PT},
9159
9161 bool IsTailCall =
9162 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9163
9164 CLI.setDebugLoc(dl)
9165 .setChain(Chain)
9166 .setLibCallee(
9167 TLI->getLibcallCallingConv(RTLIB::MEMCMP),
9169 getExternalSymbol(LibCallName, TLI->getPointerTy(getDataLayout())),
9170 std::move(Args))
9171 .setTailCall(IsTailCall);
9172
9173 return TLI->LowerCallTo(CLI);
9174}
9175
9176std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9177 const SDLoc &dl,
9178 SDValue Src,
9179 const CallInst *CI) {
9180 const char *LibCallName = TLI->getLibcallName(RTLIB::STRLEN);
9181 if (!LibCallName)
9182 return {};
9183
9184 // Emit a library call.
9187
9189 bool IsTailCall =
9190 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9191
9192 CLI.setDebugLoc(dl)
9193 .setChain(Chain)
9194 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::STRLEN), CI->getType(),
9196 LibCallName, TLI->getProgramPointerTy(getDataLayout())),
9197 std::move(Args))
9198 .setTailCall(IsTailCall);
9199
9200 return TLI->LowerCallTo(CLI);
9201}
9202
9204 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9205 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9206 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9207 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9208 BatchAAResults *BatchAA) {
9209 // Check to see if we should lower the memcpy to loads and stores first.
9210 // For cases within the target-specified limits, this is the best choice.
9212 if (ConstantSize) {
9213 // Memcpy with size zero? Just return the original chain.
9214 if (ConstantSize->isZero())
9215 return Chain;
9216
9218 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9219 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9220 if (Result.getNode())
9221 return Result;
9222 }
9223
9224 // Then check to see if we should lower the memcpy with target-specific
9225 // code. If the target chooses to do this, this is the next best.
9226 if (TSI) {
9227 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9228 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9229 DstPtrInfo, SrcPtrInfo);
9230 if (Result.getNode())
9231 return Result;
9232 }
9233
9234 // If we really need inline code and the target declined to provide it,
9235 // use a (potentially long) sequence of loads and stores.
9236 if (AlwaysInline) {
9237 assert(ConstantSize && "AlwaysInline requires a constant size!");
9239 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9240 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9241 }
9242
9245
9246 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9247 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9248 // respect volatile, so they may do things like read or write memory
9249 // beyond the given memory regions. But fixing this isn't easy, and most
9250 // people don't care.
9251
9252 // Emit a library call.
9255 Args.emplace_back(Dst, PtrTy);
9256 Args.emplace_back(Src, PtrTy);
9257 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9258 // FIXME: pass in SDLoc
9260 bool IsTailCall = false;
9261 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9262
9263 if (OverrideTailCall.has_value()) {
9264 IsTailCall = *OverrideTailCall;
9265 } else {
9266 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9267 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9268 }
9269
9270 CLI.setDebugLoc(dl)
9271 .setChain(Chain)
9272 .setLibCallee(
9273 TLI->getLibcallImplCallingConv(MemCpyImpl),
9274 Dst.getValueType().getTypeForEVT(*getContext()),
9275 getExternalSymbol(TLI->getLibcallImplName(MemCpyImpl).data(),
9276 TLI->getPointerTy(getDataLayout())),
9277 std::move(Args))
9279 .setTailCall(IsTailCall);
9280
9281 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9282 return CallResult.second;
9283}
9284
9286 SDValue Dst, SDValue Src, SDValue Size,
9287 Type *SizeTy, unsigned ElemSz,
9288 bool isTailCall,
9289 MachinePointerInfo DstPtrInfo,
9290 MachinePointerInfo SrcPtrInfo) {
9291 // Emit a library call.
9294 Args.emplace_back(Dst, ArgTy);
9295 Args.emplace_back(Src, ArgTy);
9296 Args.emplace_back(Size, SizeTy);
9297
9298 RTLIB::Libcall LibraryCall =
9300 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9301 report_fatal_error("Unsupported element size");
9302
9304 CLI.setDebugLoc(dl)
9305 .setChain(Chain)
9306 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9308 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9309 TLI->getPointerTy(getDataLayout())),
9310 std::move(Args))
9312 .setTailCall(isTailCall);
9313
9314 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9315 return CallResult.second;
9316}
9317
9319 SDValue Src, SDValue Size, Align Alignment,
9320 bool isVol, const CallInst *CI,
9321 std::optional<bool> OverrideTailCall,
9322 MachinePointerInfo DstPtrInfo,
9323 MachinePointerInfo SrcPtrInfo,
9324 const AAMDNodes &AAInfo,
9325 BatchAAResults *BatchAA) {
9326 // Check to see if we should lower the memmove to loads and stores first.
9327 // For cases within the target-specified limits, this is the best choice.
9329 if (ConstantSize) {
9330 // Memmove with size zero? Just return the original chain.
9331 if (ConstantSize->isZero())
9332 return Chain;
9333
9335 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9336 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9337 if (Result.getNode())
9338 return Result;
9339 }
9340
9341 // Then check to see if we should lower the memmove with target-specific
9342 // code. If the target chooses to do this, this is the next best.
9343 if (TSI) {
9344 SDValue Result =
9345 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9346 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9347 if (Result.getNode())
9348 return Result;
9349 }
9350
9353
9354 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9355 // not be safe. See memcpy above for more details.
9356
9357 // Emit a library call.
9360 Args.emplace_back(Dst, PtrTy);
9361 Args.emplace_back(Src, PtrTy);
9362 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9363 // FIXME: pass in SDLoc
9365
9366 RTLIB::LibcallImpl MemmoveImpl = TLI->getLibcallImpl(RTLIB::MEMMOVE);
9367
9368 bool IsTailCall = false;
9369 if (OverrideTailCall.has_value()) {
9370 IsTailCall = *OverrideTailCall;
9371 } else {
9372 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9373 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9374 }
9375
9376 CLI.setDebugLoc(dl)
9377 .setChain(Chain)
9378 .setLibCallee(
9379 TLI->getLibcallImplCallingConv(MemmoveImpl),
9380 Dst.getValueType().getTypeForEVT(*getContext()),
9381 getExternalSymbol(TLI->getLibcallImplName(MemmoveImpl).data(),
9382 TLI->getPointerTy(getDataLayout())),
9383 std::move(Args))
9385 .setTailCall(IsTailCall);
9386
9387 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9388 return CallResult.second;
9389}
9390
9392 SDValue Dst, SDValue Src, SDValue Size,
9393 Type *SizeTy, unsigned ElemSz,
9394 bool isTailCall,
9395 MachinePointerInfo DstPtrInfo,
9396 MachinePointerInfo SrcPtrInfo) {
9397 // Emit a library call.
9399 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9400 Args.emplace_back(Dst, IntPtrTy);
9401 Args.emplace_back(Src, IntPtrTy);
9402 Args.emplace_back(Size, SizeTy);
9403
9404 RTLIB::Libcall LibraryCall =
9406 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9407 report_fatal_error("Unsupported element size");
9408
9410 CLI.setDebugLoc(dl)
9411 .setChain(Chain)
9412 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9414 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9415 TLI->getPointerTy(getDataLayout())),
9416 std::move(Args))
9418 .setTailCall(isTailCall);
9419
9420 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9421 return CallResult.second;
9422}
9423
9425 SDValue Src, SDValue Size, Align Alignment,
9426 bool isVol, bool AlwaysInline,
9427 const CallInst *CI,
9428 MachinePointerInfo DstPtrInfo,
9429 const AAMDNodes &AAInfo) {
9430 // Check to see if we should lower the memset to stores first.
9431 // For cases within the target-specified limits, this is the best choice.
9433 if (ConstantSize) {
9434 // Memset with size zero? Just return the original chain.
9435 if (ConstantSize->isZero())
9436 return Chain;
9437
9438 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9439 ConstantSize->getZExtValue(), Alignment,
9440 isVol, false, DstPtrInfo, AAInfo);
9441
9442 if (Result.getNode())
9443 return Result;
9444 }
9445
9446 // Then check to see if we should lower the memset with target-specific
9447 // code. If the target chooses to do this, this is the next best.
9448 if (TSI) {
9449 SDValue Result = TSI->EmitTargetCodeForMemset(
9450 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9451 if (Result.getNode())
9452 return Result;
9453 }
9454
9455 // If we really need inline code and the target declined to provide it,
9456 // use a (potentially long) sequence of loads and stores.
9457 if (AlwaysInline) {
9458 assert(ConstantSize && "AlwaysInline requires a constant size!");
9459 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9460 ConstantSize->getZExtValue(), Alignment,
9461 isVol, true, DstPtrInfo, AAInfo);
9462 assert(Result &&
9463 "getMemsetStores must return a valid sequence when AlwaysInline");
9464 return Result;
9465 }
9466
9468
9469 // Emit a library call.
9470 auto &Ctx = *getContext();
9471 const auto& DL = getDataLayout();
9472
9474 // FIXME: pass in SDLoc
9475 CLI.setDebugLoc(dl).setChain(Chain);
9476
9477 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
9478
9479 bool UseBZero = isNullConstant(Src) && BzeroName;
9480 // If zeroing out and bzero is present, use it.
9481 if (UseBZero) {
9483 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9484 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9485 CLI.setLibCallee(
9486 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
9487 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
9488 } else {
9490 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9491 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9492 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9493 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
9494 Dst.getValueType().getTypeForEVT(Ctx),
9495 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
9496 TLI->getPointerTy(DL)),
9497 std::move(Args));
9498 }
9499
9500 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9501 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9502
9503 // If we're going to use bzero, make sure not to tail call unless the
9504 // subsequent return doesn't need a value, as bzero doesn't return the first
9505 // arg unlike memset.
9506 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9507 bool IsTailCall =
9508 CI && CI->isTailCall() &&
9509 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9510 CLI.setDiscardResult().setTailCall(IsTailCall);
9511
9512 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9513 return CallResult.second;
9514}
9515
9518 Type *SizeTy, unsigned ElemSz,
9519 bool isTailCall,
9520 MachinePointerInfo DstPtrInfo) {
9521 // Emit a library call.
9523 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9524 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9525 Args.emplace_back(Size, SizeTy);
9526
9527 RTLIB::Libcall LibraryCall =
9529 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9530 report_fatal_error("Unsupported element size");
9531
9533 CLI.setDebugLoc(dl)
9534 .setChain(Chain)
9535 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9537 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9538 TLI->getPointerTy(getDataLayout())),
9539 std::move(Args))
9541 .setTailCall(isTailCall);
9542
9543 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9544 return CallResult.second;
9545}
9546
9547SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9549 MachineMemOperand *MMO,
9550 ISD::LoadExtType ExtType) {
9552 AddNodeIDNode(ID, Opcode, VTList, Ops);
9553 ID.AddInteger(MemVT.getRawBits());
9554 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9555 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9556 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9557 ID.AddInteger(MMO->getFlags());
9558 void* IP = nullptr;
9559 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9560 E->refineAlignment(MMO);
9561 E->refineRanges(MMO);
9562 return SDValue(E, 0);
9563 }
9564
9565 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9566 VTList, MemVT, MMO, ExtType);
9567 createOperands(N, Ops);
9568
9569 CSEMap.InsertNode(N, IP);
9570 InsertNode(N);
9571 SDValue V(N, 0);
9572 NewSDValueDbgMsg(V, "Creating new node: ", this);
9573 return V;
9574}
9575
9577 EVT MemVT, SDVTList VTs, SDValue Chain,
9578 SDValue Ptr, SDValue Cmp, SDValue Swp,
9579 MachineMemOperand *MMO) {
9580 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9581 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
9582 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9583
9584 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9585 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9586}
9587
9588SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9589 SDValue Chain, SDValue Ptr, SDValue Val,
9590 MachineMemOperand *MMO) {
9591 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9592 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9593 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9594 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9595 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9596 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9597 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9598 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9599 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9600 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9601 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9602 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9603 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9604 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9605 Opcode == ISD::ATOMIC_STORE) &&
9606 "Invalid Atomic Op");
9607
9608 EVT VT = Val.getValueType();
9609
9610 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9611 getVTList(VT, MVT::Other);
9612 SDValue Ops[] = {Chain, Ptr, Val};
9613 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9614}
9615
9617 EVT MemVT, EVT VT, SDValue Chain,
9619 SDVTList VTs = getVTList(VT, MVT::Other);
9620 SDValue Ops[] = {Chain, Ptr};
9621 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9622}
9623
9624/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9626 if (Ops.size() == 1)
9627 return Ops[0];
9628
9630 VTs.reserve(Ops.size());
9631 for (const SDValue &Op : Ops)
9632 VTs.push_back(Op.getValueType());
9633 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9634}
9635
9637 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9638 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9640 const AAMDNodes &AAInfo) {
9641 if (Size.hasValue() && !Size.getValue())
9643
9645 MachineMemOperand *MMO =
9646 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9647
9648 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9649}
9650
9652 SDVTList VTList,
9653 ArrayRef<SDValue> Ops, EVT MemVT,
9654 MachineMemOperand *MMO) {
9655 assert(
9656 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9657 Opcode == ISD::PREFETCH ||
9658 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9659 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9660 "Opcode is not a memory-accessing opcode!");
9661
9662 // Memoize the node unless it returns a glue result.
9664 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9666 AddNodeIDNode(ID, Opcode, VTList, Ops);
9667 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9668 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9669 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9670 ID.AddInteger(MMO->getFlags());
9671 ID.AddInteger(MemVT.getRawBits());
9672 void *IP = nullptr;
9673 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9674 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9675 return SDValue(E, 0);
9676 }
9677
9678 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9679 VTList, MemVT, MMO);
9680 createOperands(N, Ops);
9681
9682 CSEMap.InsertNode(N, IP);
9683 } else {
9684 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9685 VTList, MemVT, MMO);
9686 createOperands(N, Ops);
9687 }
9688 InsertNode(N);
9689 SDValue V(N, 0);
9690 NewSDValueDbgMsg(V, "Creating new node: ", this);
9691 return V;
9692}
9693
9695 SDValue Chain, int FrameIndex) {
9696 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9697 const auto VTs = getVTList(MVT::Other);
9698 SDValue Ops[2] = {
9699 Chain,
9700 getFrameIndex(FrameIndex,
9701 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9702 true)};
9703
9705 AddNodeIDNode(ID, Opcode, VTs, Ops);
9706 ID.AddInteger(FrameIndex);
9707 void *IP = nullptr;
9708 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9709 return SDValue(E, 0);
9710
9711 LifetimeSDNode *N =
9712 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9713 createOperands(N, Ops);
9714 CSEMap.InsertNode(N, IP);
9715 InsertNode(N);
9716 SDValue V(N, 0);
9717 NewSDValueDbgMsg(V, "Creating new node: ", this);
9718 return V;
9719}
9720
9722 uint64_t Guid, uint64_t Index,
9723 uint32_t Attr) {
9724 const unsigned Opcode = ISD::PSEUDO_PROBE;
9725 const auto VTs = getVTList(MVT::Other);
9726 SDValue Ops[] = {Chain};
9728 AddNodeIDNode(ID, Opcode, VTs, Ops);
9729 ID.AddInteger(Guid);
9730 ID.AddInteger(Index);
9731 void *IP = nullptr;
9732 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9733 return SDValue(E, 0);
9734
9735 auto *N = newSDNode<PseudoProbeSDNode>(
9736 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9737 createOperands(N, Ops);
9738 CSEMap.InsertNode(N, IP);
9739 InsertNode(N);
9740 SDValue V(N, 0);
9741 NewSDValueDbgMsg(V, "Creating new node: ", this);
9742 return V;
9743}
9744
9745/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9746/// MachinePointerInfo record from it. This is particularly useful because the
9747/// code generator has many cases where it doesn't bother passing in a
9748/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9750 SelectionDAG &DAG, SDValue Ptr,
9751 int64_t Offset = 0) {
9752 // If this is FI+Offset, we can model it.
9755 FI->getIndex(), Offset);
9756
9757 // If this is (FI+Offset1)+Offset2, we can model it.
9758 if (Ptr.getOpcode() != ISD::ADD ||
9759 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
9760 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
9761 return Info;
9762
9763 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9765 DAG.getMachineFunction(), FI,
9766 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9767}
9768
9769/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9770/// MachinePointerInfo record from it. This is particularly useful because the
9771/// code generator has many cases where it doesn't bother passing in a
9772/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9774 SelectionDAG &DAG, SDValue Ptr,
9775 SDValue OffsetOp) {
9776 // If the 'Offset' value isn't a constant, we can't handle this.
9778 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9779 if (OffsetOp.isUndef())
9780 return InferPointerInfo(Info, DAG, Ptr);
9781 return Info;
9782}
9783
9785 EVT VT, const SDLoc &dl, SDValue Chain,
9787 MachinePointerInfo PtrInfo, EVT MemVT,
9788 Align Alignment,
9789 MachineMemOperand::Flags MMOFlags,
9790 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9791 assert(Chain.getValueType() == MVT::Other &&
9792 "Invalid chain type");
9793
9794 MMOFlags |= MachineMemOperand::MOLoad;
9795 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9796 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9797 // clients.
9798 if (PtrInfo.V.isNull())
9799 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9800
9801 TypeSize Size = MemVT.getStoreSize();
9803 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9804 Alignment, AAInfo, Ranges);
9805 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9806}
9807
9809 EVT VT, const SDLoc &dl, SDValue Chain,
9810 SDValue Ptr, SDValue Offset, EVT MemVT,
9811 MachineMemOperand *MMO) {
9812 if (VT == MemVT) {
9813 ExtType = ISD::NON_EXTLOAD;
9814 } else if (ExtType == ISD::NON_EXTLOAD) {
9815 assert(VT == MemVT && "Non-extending load from different memory type!");
9816 } else {
9817 // Extending load.
9818 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9819 "Should only be an extending load, not truncating!");
9820 assert(VT.isInteger() == MemVT.isInteger() &&
9821 "Cannot convert from FP to Int or Int -> FP!");
9822 assert(VT.isVector() == MemVT.isVector() &&
9823 "Cannot use an ext load to convert to or from a vector!");
9824 assert((!VT.isVector() ||
9826 "Cannot use an ext load to change the number of vector elements!");
9827 }
9828
9829 assert((!MMO->getRanges() ||
9831 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9832 MemVT.isInteger())) &&
9833 "Range metadata and load type must match!");
9834
9835 bool Indexed = AM != ISD::UNINDEXED;
9836 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9837
9838 SDVTList VTs = Indexed ?
9839 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9840 SDValue Ops[] = { Chain, Ptr, Offset };
9842 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9843 ID.AddInteger(MemVT.getRawBits());
9844 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9845 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9846 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9847 ID.AddInteger(MMO->getFlags());
9848 void *IP = nullptr;
9849 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9850 E->refineAlignment(MMO);
9851 E->refineRanges(MMO);
9852 return SDValue(E, 0);
9853 }
9854 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9855 ExtType, MemVT, MMO);
9856 createOperands(N, Ops);
9857
9858 CSEMap.InsertNode(N, IP);
9859 InsertNode(N);
9860 SDValue V(N, 0);
9861 NewSDValueDbgMsg(V, "Creating new node: ", this);
9862 return V;
9863}
9864
9867 MaybeAlign Alignment,
9868 MachineMemOperand::Flags MMOFlags,
9869 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9870 SDValue Undef = getUNDEF(Ptr.getValueType());
9871 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9872 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9873}
9874
9877 SDValue Undef = getUNDEF(Ptr.getValueType());
9878 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9879 VT, MMO);
9880}
9881
9883 EVT VT, SDValue Chain, SDValue Ptr,
9884 MachinePointerInfo PtrInfo, EVT MemVT,
9885 MaybeAlign Alignment,
9886 MachineMemOperand::Flags MMOFlags,
9887 const AAMDNodes &AAInfo) {
9888 SDValue Undef = getUNDEF(Ptr.getValueType());
9889 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9890 MemVT, Alignment, MMOFlags, AAInfo);
9891}
9892
9894 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9895 MachineMemOperand *MMO) {
9896 SDValue Undef = getUNDEF(Ptr.getValueType());
9897 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9898 MemVT, MMO);
9899}
9900
9904 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9905 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9906 // Don't propagate the invariant or dereferenceable flags.
9907 auto MMOFlags =
9908 LD->getMemOperand()->getFlags() &
9910 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9911 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9912 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9913}
9914
9917 Align Alignment,
9918 MachineMemOperand::Flags MMOFlags,
9919 const AAMDNodes &AAInfo) {
9920 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9921
9922 MMOFlags |= MachineMemOperand::MOStore;
9923 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9924
9925 if (PtrInfo.V.isNull())
9926 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9927
9930 MachineMemOperand *MMO =
9931 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9932 return getStore(Chain, dl, Val, Ptr, MMO);
9933}
9934
9937 SDValue Undef = getUNDEF(Ptr.getValueType());
9938 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9940}
9941
9945 bool IsTruncating) {
9946 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9947 EVT VT = Val.getValueType();
9948 if (VT == SVT) {
9949 IsTruncating = false;
9950 } else if (!IsTruncating) {
9951 assert(VT == SVT && "No-truncating store from different memory type!");
9952 } else {
9954 "Should only be a truncating store, not extending!");
9955 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9956 assert(VT.isVector() == SVT.isVector() &&
9957 "Cannot use trunc store to convert to or from a vector!");
9958 assert((!VT.isVector() ||
9960 "Cannot use trunc store to change the number of vector elements!");
9961 }
9962
9963 bool Indexed = AM != ISD::UNINDEXED;
9964 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9965 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9966 : getVTList(MVT::Other);
9967 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9969 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9970 ID.AddInteger(SVT.getRawBits());
9971 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9972 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9973 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9974 ID.AddInteger(MMO->getFlags());
9975 void *IP = nullptr;
9976 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9977 cast<StoreSDNode>(E)->refineAlignment(MMO);
9978 return SDValue(E, 0);
9979 }
9980 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9981 IsTruncating, SVT, MMO);
9982 createOperands(N, Ops);
9983
9984 CSEMap.InsertNode(N, IP);
9985 InsertNode(N);
9986 SDValue V(N, 0);
9987 NewSDValueDbgMsg(V, "Creating new node: ", this);
9988 return V;
9989}
9990
9993 EVT SVT, Align Alignment,
9994 MachineMemOperand::Flags MMOFlags,
9995 const AAMDNodes &AAInfo) {
9996 assert(Chain.getValueType() == MVT::Other &&
9997 "Invalid chain type");
9998
9999 MMOFlags |= MachineMemOperand::MOStore;
10000 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10001
10002 if (PtrInfo.V.isNull())
10003 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10004
10006 MachineMemOperand *MMO = MF.getMachineMemOperand(
10007 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10008 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10009}
10010
10012 SDValue Ptr, EVT SVT,
10013 MachineMemOperand *MMO) {
10014 SDValue Undef = getUNDEF(Ptr.getValueType());
10015 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10016}
10017
10021 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10022 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10023 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10024 ST->getMemoryVT(), ST->getMemOperand(), AM,
10025 ST->isTruncatingStore());
10026}
10027
10029 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10030 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10031 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10032 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10033 const MDNode *Ranges, bool IsExpanding) {
10034 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10035
10036 MMOFlags |= MachineMemOperand::MOLoad;
10037 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10038 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10039 // clients.
10040 if (PtrInfo.V.isNull())
10041 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10042
10043 TypeSize Size = MemVT.getStoreSize();
10045 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10046 Alignment, AAInfo, Ranges);
10047 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10048 MMO, IsExpanding);
10049}
10050
10052 ISD::LoadExtType ExtType, EVT VT,
10053 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10054 SDValue Offset, SDValue Mask, SDValue EVL,
10055 EVT MemVT, MachineMemOperand *MMO,
10056 bool IsExpanding) {
10057 bool Indexed = AM != ISD::UNINDEXED;
10058 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10059
10060 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10061 : getVTList(VT, MVT::Other);
10062 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10064 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10065 ID.AddInteger(MemVT.getRawBits());
10066 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10067 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10068 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10069 ID.AddInteger(MMO->getFlags());
10070 void *IP = nullptr;
10071 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10072 E->refineAlignment(MMO);
10073 E->refineRanges(MMO);
10074 return SDValue(E, 0);
10075 }
10076 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10077 ExtType, IsExpanding, MemVT, MMO);
10078 createOperands(N, Ops);
10079
10080 CSEMap.InsertNode(N, IP);
10081 InsertNode(N);
10082 SDValue V(N, 0);
10083 NewSDValueDbgMsg(V, "Creating new node: ", this);
10084 return V;
10085}
10086
10088 SDValue Ptr, SDValue Mask, SDValue EVL,
10089 MachinePointerInfo PtrInfo,
10090 MaybeAlign Alignment,
10091 MachineMemOperand::Flags MMOFlags,
10092 const AAMDNodes &AAInfo, const MDNode *Ranges,
10093 bool IsExpanding) {
10094 SDValue Undef = getUNDEF(Ptr.getValueType());
10095 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10096 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10097 IsExpanding);
10098}
10099
10101 SDValue Ptr, SDValue Mask, SDValue EVL,
10102 MachineMemOperand *MMO, bool IsExpanding) {
10103 SDValue Undef = getUNDEF(Ptr.getValueType());
10104 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10105 Mask, EVL, VT, MMO, IsExpanding);
10106}
10107
10109 EVT VT, SDValue Chain, SDValue Ptr,
10110 SDValue Mask, SDValue EVL,
10111 MachinePointerInfo PtrInfo, EVT MemVT,
10112 MaybeAlign Alignment,
10113 MachineMemOperand::Flags MMOFlags,
10114 const AAMDNodes &AAInfo, bool IsExpanding) {
10115 SDValue Undef = getUNDEF(Ptr.getValueType());
10116 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10117 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10118 IsExpanding);
10119}
10120
10122 EVT VT, SDValue Chain, SDValue Ptr,
10123 SDValue Mask, SDValue EVL, EVT MemVT,
10124 MachineMemOperand *MMO, bool IsExpanding) {
10125 SDValue Undef = getUNDEF(Ptr.getValueType());
10126 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10127 EVL, MemVT, MMO, IsExpanding);
10128}
10129
10133 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10134 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10135 // Don't propagate the invariant or dereferenceable flags.
10136 auto MMOFlags =
10137 LD->getMemOperand()->getFlags() &
10139 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10140 LD->getChain(), Base, Offset, LD->getMask(),
10141 LD->getVectorLength(), LD->getPointerInfo(),
10142 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10143 nullptr, LD->isExpandingLoad());
10144}
10145
10148 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10149 ISD::MemIndexedMode AM, bool IsTruncating,
10150 bool IsCompressing) {
10151 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10152 bool Indexed = AM != ISD::UNINDEXED;
10153 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10154 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10155 : getVTList(MVT::Other);
10156 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10158 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10159 ID.AddInteger(MemVT.getRawBits());
10160 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10161 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10162 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10163 ID.AddInteger(MMO->getFlags());
10164 void *IP = nullptr;
10165 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10166 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10167 return SDValue(E, 0);
10168 }
10169 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10170 IsTruncating, IsCompressing, MemVT, MMO);
10171 createOperands(N, Ops);
10172
10173 CSEMap.InsertNode(N, IP);
10174 InsertNode(N);
10175 SDValue V(N, 0);
10176 NewSDValueDbgMsg(V, "Creating new node: ", this);
10177 return V;
10178}
10179
10181 SDValue Val, SDValue Ptr, SDValue Mask,
10182 SDValue EVL, MachinePointerInfo PtrInfo,
10183 EVT SVT, Align Alignment,
10184 MachineMemOperand::Flags MMOFlags,
10185 const AAMDNodes &AAInfo,
10186 bool IsCompressing) {
10187 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10188
10189 MMOFlags |= MachineMemOperand::MOStore;
10190 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10191
10192 if (PtrInfo.V.isNull())
10193 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10194
10196 MachineMemOperand *MMO = MF.getMachineMemOperand(
10197 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10198 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10199 IsCompressing);
10200}
10201
10203 SDValue Val, SDValue Ptr, SDValue Mask,
10204 SDValue EVL, EVT SVT,
10205 MachineMemOperand *MMO,
10206 bool IsCompressing) {
10207 EVT VT = Val.getValueType();
10208
10209 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10210 if (VT == SVT)
10211 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10212 EVL, VT, MMO, ISD::UNINDEXED,
10213 /*IsTruncating*/ false, IsCompressing);
10214
10216 "Should only be a truncating store, not extending!");
10217 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10218 assert(VT.isVector() == SVT.isVector() &&
10219 "Cannot use trunc store to convert to or from a vector!");
10220 assert((!VT.isVector() ||
10222 "Cannot use trunc store to change the number of vector elements!");
10223
10224 SDVTList VTs = getVTList(MVT::Other);
10225 SDValue Undef = getUNDEF(Ptr.getValueType());
10226 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10228 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10229 ID.AddInteger(SVT.getRawBits());
10230 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10231 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10232 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10233 ID.AddInteger(MMO->getFlags());
10234 void *IP = nullptr;
10235 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10236 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10237 return SDValue(E, 0);
10238 }
10239 auto *N =
10240 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10241 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10242 createOperands(N, Ops);
10243
10244 CSEMap.InsertNode(N, IP);
10245 InsertNode(N);
10246 SDValue V(N, 0);
10247 NewSDValueDbgMsg(V, "Creating new node: ", this);
10248 return V;
10249}
10250
10254 auto *ST = cast<VPStoreSDNode>(OrigStore);
10255 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10256 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10257 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10258 Offset, ST->getMask(), ST->getVectorLength()};
10260 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10261 ID.AddInteger(ST->getMemoryVT().getRawBits());
10262 ID.AddInteger(ST->getRawSubclassData());
10263 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10264 ID.AddInteger(ST->getMemOperand()->getFlags());
10265 void *IP = nullptr;
10266 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10267 return SDValue(E, 0);
10268
10269 auto *N = newSDNode<VPStoreSDNode>(
10270 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10271 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10272 createOperands(N, Ops);
10273
10274 CSEMap.InsertNode(N, IP);
10275 InsertNode(N);
10276 SDValue V(N, 0);
10277 NewSDValueDbgMsg(V, "Creating new node: ", this);
10278 return V;
10279}
10280
10282 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10283 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10284 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10285 bool Indexed = AM != ISD::UNINDEXED;
10286 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10287
10288 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10289 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10290 : getVTList(VT, MVT::Other);
10292 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10293 ID.AddInteger(VT.getRawBits());
10294 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10295 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10296 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10297
10298 void *IP = nullptr;
10299 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10300 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10301 return SDValue(E, 0);
10302 }
10303
10304 auto *N =
10305 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10306 ExtType, IsExpanding, MemVT, MMO);
10307 createOperands(N, Ops);
10308 CSEMap.InsertNode(N, IP);
10309 InsertNode(N);
10310 SDValue V(N, 0);
10311 NewSDValueDbgMsg(V, "Creating new node: ", this);
10312 return V;
10313}
10314
10316 SDValue Ptr, SDValue Stride,
10317 SDValue Mask, SDValue EVL,
10318 MachineMemOperand *MMO,
10319 bool IsExpanding) {
10320 SDValue Undef = getUNDEF(Ptr.getValueType());
10322 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10323}
10324
10326 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10327 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10328 MachineMemOperand *MMO, bool IsExpanding) {
10329 SDValue Undef = getUNDEF(Ptr.getValueType());
10330 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10331 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10332}
10333
10335 SDValue Val, SDValue Ptr,
10336 SDValue Offset, SDValue Stride,
10337 SDValue Mask, SDValue EVL, EVT MemVT,
10338 MachineMemOperand *MMO,
10340 bool IsTruncating, bool IsCompressing) {
10341 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10342 bool Indexed = AM != ISD::UNINDEXED;
10343 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10344 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10345 : getVTList(MVT::Other);
10346 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10348 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10349 ID.AddInteger(MemVT.getRawBits());
10350 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10351 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10352 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10353 void *IP = nullptr;
10354 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10355 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10356 return SDValue(E, 0);
10357 }
10358 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10359 VTs, AM, IsTruncating,
10360 IsCompressing, MemVT, MMO);
10361 createOperands(N, Ops);
10362
10363 CSEMap.InsertNode(N, IP);
10364 InsertNode(N);
10365 SDValue V(N, 0);
10366 NewSDValueDbgMsg(V, "Creating new node: ", this);
10367 return V;
10368}
10369
10371 SDValue Val, SDValue Ptr,
10372 SDValue Stride, SDValue Mask,
10373 SDValue EVL, EVT SVT,
10374 MachineMemOperand *MMO,
10375 bool IsCompressing) {
10376 EVT VT = Val.getValueType();
10377
10378 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10379 if (VT == SVT)
10380 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10381 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10382 /*IsTruncating*/ false, IsCompressing);
10383
10385 "Should only be a truncating store, not extending!");
10386 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10387 assert(VT.isVector() == SVT.isVector() &&
10388 "Cannot use trunc store to convert to or from a vector!");
10389 assert((!VT.isVector() ||
10391 "Cannot use trunc store to change the number of vector elements!");
10392
10393 SDVTList VTs = getVTList(MVT::Other);
10394 SDValue Undef = getUNDEF(Ptr.getValueType());
10395 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10397 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10398 ID.AddInteger(SVT.getRawBits());
10399 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10400 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10401 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10402 void *IP = nullptr;
10403 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10404 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10405 return SDValue(E, 0);
10406 }
10407 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10408 VTs, ISD::UNINDEXED, true,
10409 IsCompressing, SVT, MMO);
10410 createOperands(N, Ops);
10411
10412 CSEMap.InsertNode(N, IP);
10413 InsertNode(N);
10414 SDValue V(N, 0);
10415 NewSDValueDbgMsg(V, "Creating new node: ", this);
10416 return V;
10417}
10418
10421 ISD::MemIndexType IndexType) {
10422 assert(Ops.size() == 6 && "Incompatible number of operands");
10423
10425 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10426 ID.AddInteger(VT.getRawBits());
10427 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10428 dl.getIROrder(), VTs, VT, MMO, IndexType));
10429 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10430 ID.AddInteger(MMO->getFlags());
10431 void *IP = nullptr;
10432 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10433 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10434 return SDValue(E, 0);
10435 }
10436
10437 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10438 VT, MMO, IndexType);
10439 createOperands(N, Ops);
10440
10441 assert(N->getMask().getValueType().getVectorElementCount() ==
10442 N->getValueType(0).getVectorElementCount() &&
10443 "Vector width mismatch between mask and data");
10444 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10445 N->getValueType(0).getVectorElementCount().isScalable() &&
10446 "Scalable flags of index and data do not match");
10448 N->getIndex().getValueType().getVectorElementCount(),
10449 N->getValueType(0).getVectorElementCount()) &&
10450 "Vector width mismatch between index and data");
10451 assert(isa<ConstantSDNode>(N->getScale()) &&
10452 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10453 "Scale should be a constant power of 2");
10454
10455 CSEMap.InsertNode(N, IP);
10456 InsertNode(N);
10457 SDValue V(N, 0);
10458 NewSDValueDbgMsg(V, "Creating new node: ", this);
10459 return V;
10460}
10461
10464 MachineMemOperand *MMO,
10465 ISD::MemIndexType IndexType) {
10466 assert(Ops.size() == 7 && "Incompatible number of operands");
10467
10469 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10470 ID.AddInteger(VT.getRawBits());
10471 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10472 dl.getIROrder(), VTs, VT, MMO, IndexType));
10473 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10474 ID.AddInteger(MMO->getFlags());
10475 void *IP = nullptr;
10476 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10477 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10478 return SDValue(E, 0);
10479 }
10480 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10481 VT, MMO, IndexType);
10482 createOperands(N, Ops);
10483
10484 assert(N->getMask().getValueType().getVectorElementCount() ==
10485 N->getValue().getValueType().getVectorElementCount() &&
10486 "Vector width mismatch between mask and data");
10487 assert(
10488 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10489 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10490 "Scalable flags of index and data do not match");
10492 N->getIndex().getValueType().getVectorElementCount(),
10493 N->getValue().getValueType().getVectorElementCount()) &&
10494 "Vector width mismatch between index and data");
10495 assert(isa<ConstantSDNode>(N->getScale()) &&
10496 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10497 "Scale should be a constant power of 2");
10498
10499 CSEMap.InsertNode(N, IP);
10500 InsertNode(N);
10501 SDValue V(N, 0);
10502 NewSDValueDbgMsg(V, "Creating new node: ", this);
10503 return V;
10504}
10505
10508 SDValue PassThru, EVT MemVT,
10509 MachineMemOperand *MMO,
10511 ISD::LoadExtType ExtTy, bool isExpanding) {
10512 bool Indexed = AM != ISD::UNINDEXED;
10513 assert((Indexed || Offset.isUndef()) &&
10514 "Unindexed masked load with an offset!");
10515 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10516 : getVTList(VT, MVT::Other);
10517 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10519 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
10520 ID.AddInteger(MemVT.getRawBits());
10521 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10522 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10523 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10524 ID.AddInteger(MMO->getFlags());
10525 void *IP = nullptr;
10526 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10527 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10528 return SDValue(E, 0);
10529 }
10530 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10531 AM, ExtTy, isExpanding, MemVT, MMO);
10532 createOperands(N, Ops);
10533
10534 CSEMap.InsertNode(N, IP);
10535 InsertNode(N);
10536 SDValue V(N, 0);
10537 NewSDValueDbgMsg(V, "Creating new node: ", this);
10538 return V;
10539}
10540
10545 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10546 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10547 Offset, LD->getMask(), LD->getPassThru(),
10548 LD->getMemoryVT(), LD->getMemOperand(), AM,
10549 LD->getExtensionType(), LD->isExpandingLoad());
10550}
10551
10554 SDValue Mask, EVT MemVT,
10555 MachineMemOperand *MMO,
10556 ISD::MemIndexedMode AM, bool IsTruncating,
10557 bool IsCompressing) {
10558 assert(Chain.getValueType() == MVT::Other &&
10559 "Invalid chain type");
10560 bool Indexed = AM != ISD::UNINDEXED;
10561 assert((Indexed || Offset.isUndef()) &&
10562 "Unindexed masked store with an offset!");
10563 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10564 : getVTList(MVT::Other);
10565 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10567 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10568 ID.AddInteger(MemVT.getRawBits());
10569 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10570 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10571 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10572 ID.AddInteger(MMO->getFlags());
10573 void *IP = nullptr;
10574 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10575 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10576 return SDValue(E, 0);
10577 }
10578 auto *N =
10579 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10580 IsTruncating, IsCompressing, MemVT, MMO);
10581 createOperands(N, Ops);
10582
10583 CSEMap.InsertNode(N, IP);
10584 InsertNode(N);
10585 SDValue V(N, 0);
10586 NewSDValueDbgMsg(V, "Creating new node: ", this);
10587 return V;
10588}
10589
10594 assert(ST->getOffset().isUndef() &&
10595 "Masked store is already a indexed store!");
10596 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10597 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10598 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10599}
10600
10603 MachineMemOperand *MMO,
10604 ISD::MemIndexType IndexType,
10605 ISD::LoadExtType ExtTy) {
10606 assert(Ops.size() == 6 && "Incompatible number of operands");
10607
10609 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10610 ID.AddInteger(MemVT.getRawBits());
10611 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10612 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10613 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10614 ID.AddInteger(MMO->getFlags());
10615 void *IP = nullptr;
10616 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10617 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10618 return SDValue(E, 0);
10619 }
10620
10621 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10622 VTs, MemVT, MMO, IndexType, ExtTy);
10623 createOperands(N, Ops);
10624
10625 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10626 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10627 assert(N->getMask().getValueType().getVectorElementCount() ==
10628 N->getValueType(0).getVectorElementCount() &&
10629 "Vector width mismatch between mask and data");
10630 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10631 N->getValueType(0).getVectorElementCount().isScalable() &&
10632 "Scalable flags of index and data do not match");
10634 N->getIndex().getValueType().getVectorElementCount(),
10635 N->getValueType(0).getVectorElementCount()) &&
10636 "Vector width mismatch between index and data");
10637 assert(isa<ConstantSDNode>(N->getScale()) &&
10638 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10639 "Scale should be a constant power of 2");
10640
10641 CSEMap.InsertNode(N, IP);
10642 InsertNode(N);
10643 SDValue V(N, 0);
10644 NewSDValueDbgMsg(V, "Creating new node: ", this);
10645 return V;
10646}
10647
10650 MachineMemOperand *MMO,
10651 ISD::MemIndexType IndexType,
10652 bool IsTrunc) {
10653 assert(Ops.size() == 6 && "Incompatible number of operands");
10654
10656 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10657 ID.AddInteger(MemVT.getRawBits());
10658 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10659 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10660 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10661 ID.AddInteger(MMO->getFlags());
10662 void *IP = nullptr;
10663 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10664 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10665 return SDValue(E, 0);
10666 }
10667
10668 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10669 VTs, MemVT, MMO, IndexType, IsTrunc);
10670 createOperands(N, Ops);
10671
10672 assert(N->getMask().getValueType().getVectorElementCount() ==
10673 N->getValue().getValueType().getVectorElementCount() &&
10674 "Vector width mismatch between mask and data");
10675 assert(
10676 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10677 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10678 "Scalable flags of index and data do not match");
10680 N->getIndex().getValueType().getVectorElementCount(),
10681 N->getValue().getValueType().getVectorElementCount()) &&
10682 "Vector width mismatch between index and data");
10683 assert(isa<ConstantSDNode>(N->getScale()) &&
10684 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10685 "Scale should be a constant power of 2");
10686
10687 CSEMap.InsertNode(N, IP);
10688 InsertNode(N);
10689 SDValue V(N, 0);
10690 NewSDValueDbgMsg(V, "Creating new node: ", this);
10691 return V;
10692}
10693
10695 const SDLoc &dl, ArrayRef<SDValue> Ops,
10696 MachineMemOperand *MMO,
10697 ISD::MemIndexType IndexType) {
10698 assert(Ops.size() == 7 && "Incompatible number of operands");
10699
10701 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VECTOR_HISTOGRAM, VTs, Ops);
10702 ID.AddInteger(MemVT.getRawBits());
10703 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10704 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10705 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10706 ID.AddInteger(MMO->getFlags());
10707 void *IP = nullptr;
10708 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10709 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10710 return SDValue(E, 0);
10711 }
10712
10713 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10714 VTs, MemVT, MMO, IndexType);
10715 createOperands(N, Ops);
10716
10717 assert(N->getMask().getValueType().getVectorElementCount() ==
10718 N->getIndex().getValueType().getVectorElementCount() &&
10719 "Vector width mismatch between mask and data");
10720 assert(isa<ConstantSDNode>(N->getScale()) &&
10721 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10722 "Scale should be a constant power of 2");
10723 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10724
10725 CSEMap.InsertNode(N, IP);
10726 InsertNode(N);
10727 SDValue V(N, 0);
10728 NewSDValueDbgMsg(V, "Creating new node: ", this);
10729 return V;
10730}
10731
10733 SDValue Ptr, SDValue Mask, SDValue EVL,
10734 MachineMemOperand *MMO) {
10735 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10736 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10738 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10739 ID.AddInteger(VT.getRawBits());
10740 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10741 VTs, VT, MMO));
10742 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10743 ID.AddInteger(MMO->getFlags());
10744 void *IP = nullptr;
10745 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10746 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10747 return SDValue(E, 0);
10748 }
10749 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10750 VT, MMO);
10751 createOperands(N, Ops);
10752
10753 CSEMap.InsertNode(N, IP);
10754 InsertNode(N);
10755 SDValue V(N, 0);
10756 NewSDValueDbgMsg(V, "Creating new node: ", this);
10757 return V;
10758}
10759
10761 EVT MemVT, MachineMemOperand *MMO) {
10762 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10763 SDVTList VTs = getVTList(MVT::Other);
10764 SDValue Ops[] = {Chain, Ptr};
10766 AddNodeIDNode(ID, ISD::GET_FPENV_MEM, VTs, Ops);
10767 ID.AddInteger(MemVT.getRawBits());
10768 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10769 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10770 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10771 ID.AddInteger(MMO->getFlags());
10772 void *IP = nullptr;
10773 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10774 return SDValue(E, 0);
10775
10776 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10777 dl.getDebugLoc(), VTs, MemVT, MMO);
10778 createOperands(N, Ops);
10779
10780 CSEMap.InsertNode(N, IP);
10781 InsertNode(N);
10782 SDValue V(N, 0);
10783 NewSDValueDbgMsg(V, "Creating new node: ", this);
10784 return V;
10785}
10786
10788 EVT MemVT, MachineMemOperand *MMO) {
10789 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10790 SDVTList VTs = getVTList(MVT::Other);
10791 SDValue Ops[] = {Chain, Ptr};
10793 AddNodeIDNode(ID, ISD::SET_FPENV_MEM, VTs, Ops);
10794 ID.AddInteger(MemVT.getRawBits());
10795 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10796 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10797 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10798 ID.AddInteger(MMO->getFlags());
10799 void *IP = nullptr;
10800 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10801 return SDValue(E, 0);
10802
10803 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10804 dl.getDebugLoc(), VTs, MemVT, MMO);
10805 createOperands(N, Ops);
10806
10807 CSEMap.InsertNode(N, IP);
10808 InsertNode(N);
10809 SDValue V(N, 0);
10810 NewSDValueDbgMsg(V, "Creating new node: ", this);
10811 return V;
10812}
10813
10815 // select undef, T, F --> T (if T is a constant), otherwise F
10816 // select, ?, undef, F --> F
10817 // select, ?, T, undef --> T
10818 if (Cond.isUndef())
10819 return isConstantValueOfAnyType(T) ? T : F;
10820 if (T.isUndef())
10821 return F;
10822 if (F.isUndef())
10823 return T;
10824
10825 // select true, T, F --> T
10826 // select false, T, F --> F
10827 if (auto C = isBoolConstant(Cond))
10828 return *C ? T : F;
10829
10830 // select ?, T, T --> T
10831 if (T == F)
10832 return T;
10833
10834 return SDValue();
10835}
10836
10838 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10839 if (X.isUndef())
10840 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10841 // shift X, undef --> undef (because it may shift by the bitwidth)
10842 if (Y.isUndef())
10843 return getUNDEF(X.getValueType());
10844
10845 // shift 0, Y --> 0
10846 // shift X, 0 --> X
10848 return X;
10849
10850 // shift X, C >= bitwidth(X) --> undef
10851 // All vector elements must be too big (or undef) to avoid partial undefs.
10852 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10853 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10854 };
10855 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10856 return getUNDEF(X.getValueType());
10857
10858 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10859 if (X.getValueType().getScalarType() == MVT::i1)
10860 return X;
10861
10862 return SDValue();
10863}
10864
10866 SDNodeFlags Flags) {
10867 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10868 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10869 // operation is poison. That result can be relaxed to undef.
10870 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10871 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10872 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10873 (YC && YC->getValueAPF().isNaN());
10874 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10875 (YC && YC->getValueAPF().isInfinity());
10876
10877 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10878 return getUNDEF(X.getValueType());
10879
10880 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10881 return getUNDEF(X.getValueType());
10882
10883 if (!YC)
10884 return SDValue();
10885
10886 // X + -0.0 --> X
10887 if (Opcode == ISD::FADD)
10888 if (YC->getValueAPF().isNegZero())
10889 return X;
10890
10891 // X - +0.0 --> X
10892 if (Opcode == ISD::FSUB)
10893 if (YC->getValueAPF().isPosZero())
10894 return X;
10895
10896 // X * 1.0 --> X
10897 // X / 1.0 --> X
10898 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10899 if (YC->getValueAPF().isExactlyValue(1.0))
10900 return X;
10901
10902 // X * 0.0 --> 0.0
10903 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10904 if (YC->getValueAPF().isZero())
10905 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10906
10907 return SDValue();
10908}
10909
10911 SDValue Ptr, SDValue SV, unsigned Align) {
10912 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10913 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10914}
10915
10916SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10918 switch (Ops.size()) {
10919 case 0: return getNode(Opcode, DL, VT);
10920 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10921 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10922 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10923 default: break;
10924 }
10925
10926 // Copy from an SDUse array into an SDValue array for use with
10927 // the regular getNode logic.
10929 return getNode(Opcode, DL, VT, NewOps);
10930}
10931
10932SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10934 SDNodeFlags Flags;
10935 if (Inserter)
10936 Flags = Inserter->getFlags();
10937 return getNode(Opcode, DL, VT, Ops, Flags);
10938}
10939
10940SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10941 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10942 unsigned NumOps = Ops.size();
10943 switch (NumOps) {
10944 case 0: return getNode(Opcode, DL, VT);
10945 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10946 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10947 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10948 default: break;
10949 }
10950
10951#ifndef NDEBUG
10952 for (const auto &Op : Ops)
10953 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10954 "Operand is DELETED_NODE!");
10955#endif
10956
10957 switch (Opcode) {
10958 default: break;
10959 case ISD::BUILD_VECTOR:
10960 // Attempt to simplify BUILD_VECTOR.
10961 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10962 return V;
10963 break;
10965 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10966 return V;
10967 break;
10968 case ISD::SELECT_CC:
10969 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10970 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10971 "LHS and RHS of condition must have same type!");
10972 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10973 "True and False arms of SelectCC must have same type!");
10974 assert(Ops[2].getValueType() == VT &&
10975 "select_cc node must be of same type as true and false value!");
10976 assert((!Ops[0].getValueType().isVector() ||
10977 Ops[0].getValueType().getVectorElementCount() ==
10978 VT.getVectorElementCount()) &&
10979 "Expected select_cc with vector result to have the same sized "
10980 "comparison type!");
10981 break;
10982 case ISD::BR_CC:
10983 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10984 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10985 "LHS/RHS of comparison should match types!");
10986 break;
10987 case ISD::VP_ADD:
10988 case ISD::VP_SUB:
10989 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10990 if (VT.getScalarType() == MVT::i1)
10991 Opcode = ISD::VP_XOR;
10992 break;
10993 case ISD::VP_MUL:
10994 // If it is VP_MUL mask operation then turn it to VP_AND
10995 if (VT.getScalarType() == MVT::i1)
10996 Opcode = ISD::VP_AND;
10997 break;
10998 case ISD::VP_REDUCE_MUL:
10999 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11000 if (VT == MVT::i1)
11001 Opcode = ISD::VP_REDUCE_AND;
11002 break;
11003 case ISD::VP_REDUCE_ADD:
11004 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11005 if (VT == MVT::i1)
11006 Opcode = ISD::VP_REDUCE_XOR;
11007 break;
11008 case ISD::VP_REDUCE_SMAX:
11009 case ISD::VP_REDUCE_UMIN:
11010 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11011 // VP_REDUCE_AND.
11012 if (VT == MVT::i1)
11013 Opcode = ISD::VP_REDUCE_AND;
11014 break;
11015 case ISD::VP_REDUCE_SMIN:
11016 case ISD::VP_REDUCE_UMAX:
11017 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11018 // VP_REDUCE_OR.
11019 if (VT == MVT::i1)
11020 Opcode = ISD::VP_REDUCE_OR;
11021 break;
11022 }
11023
11024 // Memoize nodes.
11025 SDNode *N;
11026 SDVTList VTs = getVTList(VT);
11027
11028 if (VT != MVT::Glue) {
11030 AddNodeIDNode(ID, Opcode, VTs, Ops);
11031 void *IP = nullptr;
11032
11033 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11034 E->intersectFlagsWith(Flags);
11035 return SDValue(E, 0);
11036 }
11037
11038 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11039 createOperands(N, Ops);
11040
11041 CSEMap.InsertNode(N, IP);
11042 } else {
11043 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11044 createOperands(N, Ops);
11045 }
11046
11047 N->setFlags(Flags);
11048 InsertNode(N);
11049 SDValue V(N, 0);
11050 NewSDValueDbgMsg(V, "Creating new node: ", this);
11051 return V;
11052}
11053
11054SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11055 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11056 SDNodeFlags Flags;
11057 if (Inserter)
11058 Flags = Inserter->getFlags();
11059 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11060}
11061
11062SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11064 const SDNodeFlags Flags) {
11065 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11066}
11067
11068SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11070 SDNodeFlags Flags;
11071 if (Inserter)
11072 Flags = Inserter->getFlags();
11073 return getNode(Opcode, DL, VTList, Ops, Flags);
11074}
11075
11076SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11077 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11078 if (VTList.NumVTs == 1)
11079 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11080
11081#ifndef NDEBUG
11082 for (const auto &Op : Ops)
11083 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11084 "Operand is DELETED_NODE!");
11085#endif
11086
11087 switch (Opcode) {
11088 case ISD::SADDO:
11089 case ISD::UADDO:
11090 case ISD::SSUBO:
11091 case ISD::USUBO: {
11092 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11093 "Invalid add/sub overflow op!");
11094 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11095 Ops[0].getValueType() == Ops[1].getValueType() &&
11096 Ops[0].getValueType() == VTList.VTs[0] &&
11097 "Binary operator types must match!");
11098 SDValue N1 = Ops[0], N2 = Ops[1];
11099 canonicalizeCommutativeBinop(Opcode, N1, N2);
11100
11101 // (X +- 0) -> X with zero-overflow.
11102 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11103 /*AllowTruncation*/ true);
11104 if (N2CV && N2CV->isZero()) {
11105 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11106 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11107 }
11108
11109 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11110 VTList.VTs[1].getScalarType() == MVT::i1) {
11111 SDValue F1 = getFreeze(N1);
11112 SDValue F2 = getFreeze(N2);
11113 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11114 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11115 return getNode(ISD::MERGE_VALUES, DL, VTList,
11116 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11117 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11118 Flags);
11119 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11120 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11121 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11122 return getNode(ISD::MERGE_VALUES, DL, VTList,
11123 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11124 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11125 Flags);
11126 }
11127 }
11128 break;
11129 }
11130 case ISD::SADDO_CARRY:
11131 case ISD::UADDO_CARRY:
11132 case ISD::SSUBO_CARRY:
11133 case ISD::USUBO_CARRY:
11134 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11135 "Invalid add/sub overflow op!");
11136 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11137 Ops[0].getValueType() == Ops[1].getValueType() &&
11138 Ops[0].getValueType() == VTList.VTs[0] &&
11139 Ops[2].getValueType() == VTList.VTs[1] &&
11140 "Binary operator types must match!");
11141 break;
11142 case ISD::SMUL_LOHI:
11143 case ISD::UMUL_LOHI: {
11144 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11145 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11146 VTList.VTs[0] == Ops[0].getValueType() &&
11147 VTList.VTs[0] == Ops[1].getValueType() &&
11148 "Binary operator types must match!");
11149 // Constant fold.
11152 if (LHS && RHS) {
11153 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11154 unsigned OutWidth = Width * 2;
11155 APInt Val = LHS->getAPIntValue();
11156 APInt Mul = RHS->getAPIntValue();
11157 if (Opcode == ISD::SMUL_LOHI) {
11158 Val = Val.sext(OutWidth);
11159 Mul = Mul.sext(OutWidth);
11160 } else {
11161 Val = Val.zext(OutWidth);
11162 Mul = Mul.zext(OutWidth);
11163 }
11164 Val *= Mul;
11165
11166 SDValue Hi =
11167 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11168 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11169 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11170 }
11171 break;
11172 }
11173 case ISD::FFREXP: {
11174 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11175 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11176 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11177
11179 int FrexpExp;
11180 APFloat FrexpMant =
11181 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11182 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11183 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11184 DL, VTList.VTs[1]);
11185 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11186 }
11187
11188 break;
11189 }
11191 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11192 "Invalid STRICT_FP_EXTEND!");
11193 assert(VTList.VTs[0].isFloatingPoint() &&
11194 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11195 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11196 "STRICT_FP_EXTEND result type should be vector iff the operand "
11197 "type is vector!");
11198 assert((!VTList.VTs[0].isVector() ||
11199 VTList.VTs[0].getVectorElementCount() ==
11200 Ops[1].getValueType().getVectorElementCount()) &&
11201 "Vector element count mismatch!");
11202 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11203 "Invalid fpext node, dst <= src!");
11204 break;
11206 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11207 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11208 "STRICT_FP_ROUND result type should be vector iff the operand "
11209 "type is vector!");
11210 assert((!VTList.VTs[0].isVector() ||
11211 VTList.VTs[0].getVectorElementCount() ==
11212 Ops[1].getValueType().getVectorElementCount()) &&
11213 "Vector element count mismatch!");
11214 assert(VTList.VTs[0].isFloatingPoint() &&
11215 Ops[1].getValueType().isFloatingPoint() &&
11216 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11217 Ops[2].getOpcode() == ISD::TargetConstant &&
11218 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11219 "Invalid STRICT_FP_ROUND!");
11220 break;
11221 }
11222
11223 // Memoize the node unless it returns a glue result.
11224 SDNode *N;
11225 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11227 AddNodeIDNode(ID, Opcode, VTList, Ops);
11228 void *IP = nullptr;
11229 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11230 E->intersectFlagsWith(Flags);
11231 return SDValue(E, 0);
11232 }
11233
11234 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11235 createOperands(N, Ops);
11236 CSEMap.InsertNode(N, IP);
11237 } else {
11238 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11239 createOperands(N, Ops);
11240 }
11241
11242 N->setFlags(Flags);
11243 InsertNode(N);
11244 SDValue V(N, 0);
11245 NewSDValueDbgMsg(V, "Creating new node: ", this);
11246 return V;
11247}
11248
11249SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11250 SDVTList VTList) {
11251 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11252}
11253
11254SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11255 SDValue N1) {
11256 SDValue Ops[] = { N1 };
11257 return getNode(Opcode, DL, VTList, Ops);
11258}
11259
11260SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11261 SDValue N1, SDValue N2) {
11262 SDValue Ops[] = { N1, N2 };
11263 return getNode(Opcode, DL, VTList, Ops);
11264}
11265
11266SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11267 SDValue N1, SDValue N2, SDValue N3) {
11268 SDValue Ops[] = { N1, N2, N3 };
11269 return getNode(Opcode, DL, VTList, Ops);
11270}
11271
11272SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11273 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11274 SDValue Ops[] = { N1, N2, N3, N4 };
11275 return getNode(Opcode, DL, VTList, Ops);
11276}
11277
11278SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11279 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11280 SDValue N5) {
11281 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11282 return getNode(Opcode, DL, VTList, Ops);
11283}
11284
11286 if (!VT.isExtended())
11287 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11288
11289 return makeVTList(&(*EVTs.insert(VT).first), 1);
11290}
11291
11294 ID.AddInteger(2U);
11295 ID.AddInteger(VT1.getRawBits());
11296 ID.AddInteger(VT2.getRawBits());
11297
11298 void *IP = nullptr;
11299 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11300 if (!Result) {
11301 EVT *Array = Allocator.Allocate<EVT>(2);
11302 Array[0] = VT1;
11303 Array[1] = VT2;
11304 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11305 VTListMap.InsertNode(Result, IP);
11306 }
11307 return Result->getSDVTList();
11308}
11309
11312 ID.AddInteger(3U);
11313 ID.AddInteger(VT1.getRawBits());
11314 ID.AddInteger(VT2.getRawBits());
11315 ID.AddInteger(VT3.getRawBits());
11316
11317 void *IP = nullptr;
11318 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11319 if (!Result) {
11320 EVT *Array = Allocator.Allocate<EVT>(3);
11321 Array[0] = VT1;
11322 Array[1] = VT2;
11323 Array[2] = VT3;
11324 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11325 VTListMap.InsertNode(Result, IP);
11326 }
11327 return Result->getSDVTList();
11328}
11329
11332 ID.AddInteger(4U);
11333 ID.AddInteger(VT1.getRawBits());
11334 ID.AddInteger(VT2.getRawBits());
11335 ID.AddInteger(VT3.getRawBits());
11336 ID.AddInteger(VT4.getRawBits());
11337
11338 void *IP = nullptr;
11339 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11340 if (!Result) {
11341 EVT *Array = Allocator.Allocate<EVT>(4);
11342 Array[0] = VT1;
11343 Array[1] = VT2;
11344 Array[2] = VT3;
11345 Array[3] = VT4;
11346 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11347 VTListMap.InsertNode(Result, IP);
11348 }
11349 return Result->getSDVTList();
11350}
11351
11353 unsigned NumVTs = VTs.size();
11355 ID.AddInteger(NumVTs);
11356 for (unsigned index = 0; index < NumVTs; index++) {
11357 ID.AddInteger(VTs[index].getRawBits());
11358 }
11359
11360 void *IP = nullptr;
11361 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11362 if (!Result) {
11363 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11364 llvm::copy(VTs, Array);
11365 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11366 VTListMap.InsertNode(Result, IP);
11367 }
11368 return Result->getSDVTList();
11369}
11370
11371
11372/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11373/// specified operands. If the resultant node already exists in the DAG,
11374/// this does not modify the specified node, instead it returns the node that
11375/// already exists. If the resultant node does not exist in the DAG, the
11376/// input node is returned. As a degenerate case, if you specify the same
11377/// input operands as the node already has, the input node is returned.
11379 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11380
11381 // Check to see if there is no change.
11382 if (Op == N->getOperand(0)) return N;
11383
11384 // See if the modified node already exists.
11385 void *InsertPos = nullptr;
11386 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11387 return Existing;
11388
11389 // Nope it doesn't. Remove the node from its current place in the maps.
11390 if (InsertPos)
11391 if (!RemoveNodeFromCSEMaps(N))
11392 InsertPos = nullptr;
11393
11394 // Now we update the operands.
11395 N->OperandList[0].set(Op);
11396
11398 // If this gets put into a CSE map, add it.
11399 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11400 return N;
11401}
11402
11404 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11405
11406 // Check to see if there is no change.
11407 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11408 return N; // No operands changed, just return the input node.
11409
11410 // See if the modified node already exists.
11411 void *InsertPos = nullptr;
11412 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11413 return Existing;
11414
11415 // Nope it doesn't. Remove the node from its current place in the maps.
11416 if (InsertPos)
11417 if (!RemoveNodeFromCSEMaps(N))
11418 InsertPos = nullptr;
11419
11420 // Now we update the operands.
11421 if (N->OperandList[0] != Op1)
11422 N->OperandList[0].set(Op1);
11423 if (N->OperandList[1] != Op2)
11424 N->OperandList[1].set(Op2);
11425
11427 // If this gets put into a CSE map, add it.
11428 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11429 return N;
11430}
11431
11434 SDValue Ops[] = { Op1, Op2, Op3 };
11435 return UpdateNodeOperands(N, Ops);
11436}
11437
11440 SDValue Op3, SDValue Op4) {
11441 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11442 return UpdateNodeOperands(N, Ops);
11443}
11444
11447 SDValue Op3, SDValue Op4, SDValue Op5) {
11448 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11449 return UpdateNodeOperands(N, Ops);
11450}
11451
11454 unsigned NumOps = Ops.size();
11455 assert(N->getNumOperands() == NumOps &&
11456 "Update with wrong number of operands");
11457
11458 // If no operands changed just return the input node.
11459 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11460 return N;
11461
11462 // See if the modified node already exists.
11463 void *InsertPos = nullptr;
11464 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11465 return Existing;
11466
11467 // Nope it doesn't. Remove the node from its current place in the maps.
11468 if (InsertPos)
11469 if (!RemoveNodeFromCSEMaps(N))
11470 InsertPos = nullptr;
11471
11472 // Now we update the operands.
11473 for (unsigned i = 0; i != NumOps; ++i)
11474 if (N->OperandList[i] != Ops[i])
11475 N->OperandList[i].set(Ops[i]);
11476
11478 // If this gets put into a CSE map, add it.
11479 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11480 return N;
11481}
11482
11483/// DropOperands - Release the operands and set this node to have
11484/// zero operands.
11486 // Unlike the code in MorphNodeTo that does this, we don't need to
11487 // watch for dead nodes here.
11488 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11489 SDUse &Use = *I++;
11490 Use.set(SDValue());
11491 }
11492}
11493
11495 ArrayRef<MachineMemOperand *> NewMemRefs) {
11496 if (NewMemRefs.empty()) {
11497 N->clearMemRefs();
11498 return;
11499 }
11500
11501 // Check if we can avoid allocating by storing a single reference directly.
11502 if (NewMemRefs.size() == 1) {
11503 N->MemRefs = NewMemRefs[0];
11504 N->NumMemRefs = 1;
11505 return;
11506 }
11507
11508 MachineMemOperand **MemRefsBuffer =
11509 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11510 llvm::copy(NewMemRefs, MemRefsBuffer);
11511 N->MemRefs = MemRefsBuffer;
11512 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11513}
11514
11515/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11516/// machine opcode.
11517///
11519 EVT VT) {
11520 SDVTList VTs = getVTList(VT);
11521 return SelectNodeTo(N, MachineOpc, VTs, {});
11522}
11523
11525 EVT VT, SDValue Op1) {
11526 SDVTList VTs = getVTList(VT);
11527 SDValue Ops[] = { Op1 };
11528 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11529}
11530
11532 EVT VT, SDValue Op1,
11533 SDValue Op2) {
11534 SDVTList VTs = getVTList(VT);
11535 SDValue Ops[] = { Op1, Op2 };
11536 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11537}
11538
11540 EVT VT, SDValue Op1,
11541 SDValue Op2, SDValue Op3) {
11542 SDVTList VTs = getVTList(VT);
11543 SDValue Ops[] = { Op1, Op2, Op3 };
11544 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11545}
11546
11549 SDVTList VTs = getVTList(VT);
11550 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11551}
11552
11554 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11555 SDVTList VTs = getVTList(VT1, VT2);
11556 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11557}
11558
11560 EVT VT1, EVT VT2) {
11561 SDVTList VTs = getVTList(VT1, VT2);
11562 return SelectNodeTo(N, MachineOpc, VTs, {});
11563}
11564
11566 EVT VT1, EVT VT2, EVT VT3,
11568 SDVTList VTs = getVTList(VT1, VT2, VT3);
11569 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11570}
11571
11573 EVT VT1, EVT VT2,
11574 SDValue Op1, SDValue Op2) {
11575 SDVTList VTs = getVTList(VT1, VT2);
11576 SDValue Ops[] = { Op1, Op2 };
11577 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11578}
11579
11582 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11583 // Reset the NodeID to -1.
11584 New->setNodeId(-1);
11585 if (New != N) {
11586 ReplaceAllUsesWith(N, New);
11588 }
11589 return New;
11590}
11591
11592/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11593/// the line number information on the merged node since it is not possible to
11594/// preserve the information that operation is associated with multiple lines.
11595/// This will make the debugger working better at -O0, were there is a higher
11596/// probability having other instructions associated with that line.
11597///
11598/// For IROrder, we keep the smaller of the two
11599SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11600 DebugLoc NLoc = N->getDebugLoc();
11601 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11602 N->setDebugLoc(DebugLoc());
11603 }
11604 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11605 N->setIROrder(Order);
11606 return N;
11607}
11608
11609/// MorphNodeTo - This *mutates* the specified node to have the specified
11610/// return type, opcode, and operands.
11611///
11612/// Note that MorphNodeTo returns the resultant node. If there is already a
11613/// node of the specified opcode and operands, it returns that node instead of
11614/// the current one. Note that the SDLoc need not be the same.
11615///
11616/// Using MorphNodeTo is faster than creating a new node and swapping it in
11617/// with ReplaceAllUsesWith both because it often avoids allocating a new
11618/// node, and because it doesn't require CSE recalculation for any of
11619/// the node's users.
11620///
11621/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11622/// As a consequence it isn't appropriate to use from within the DAG combiner or
11623/// the legalizer which maintain worklists that would need to be updated when
11624/// deleting things.
11627 // If an identical node already exists, use it.
11628 void *IP = nullptr;
11629 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11631 AddNodeIDNode(ID, Opc, VTs, Ops);
11632 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11633 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11634 }
11635
11636 if (!RemoveNodeFromCSEMaps(N))
11637 IP = nullptr;
11638
11639 // Start the morphing.
11640 N->NodeType = Opc;
11641 N->ValueList = VTs.VTs;
11642 N->NumValues = VTs.NumVTs;
11643
11644 // Clear the operands list, updating used nodes to remove this from their
11645 // use list. Keep track of any operands that become dead as a result.
11646 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11647 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11648 SDUse &Use = *I++;
11649 SDNode *Used = Use.getNode();
11650 Use.set(SDValue());
11651 if (Used->use_empty())
11652 DeadNodeSet.insert(Used);
11653 }
11654
11655 // For MachineNode, initialize the memory references information.
11657 MN->clearMemRefs();
11658
11659 // Swap for an appropriately sized array from the recycler.
11660 removeOperands(N);
11661 createOperands(N, Ops);
11662
11663 // Delete any nodes that are still dead after adding the uses for the
11664 // new operands.
11665 if (!DeadNodeSet.empty()) {
11666 SmallVector<SDNode *, 16> DeadNodes;
11667 for (SDNode *N : DeadNodeSet)
11668 if (N->use_empty())
11669 DeadNodes.push_back(N);
11670 RemoveDeadNodes(DeadNodes);
11671 }
11672
11673 if (IP)
11674 CSEMap.InsertNode(N, IP); // Memoize the new node.
11675 return N;
11676}
11677
11679 unsigned OrigOpc = Node->getOpcode();
11680 unsigned NewOpc;
11681 switch (OrigOpc) {
11682 default:
11683 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11684#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11685 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11686#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11687 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11688#include "llvm/IR/ConstrainedOps.def"
11689 }
11690
11691 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11692
11693 // We're taking this node out of the chain, so we need to re-link things.
11694 SDValue InputChain = Node->getOperand(0);
11695 SDValue OutputChain = SDValue(Node, 1);
11696 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11697
11699 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11700 Ops.push_back(Node->getOperand(i));
11701
11702 SDVTList VTs = getVTList(Node->getValueType(0));
11703 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11704
11705 // MorphNodeTo can operate in two ways: if an existing node with the
11706 // specified operands exists, it can just return it. Otherwise, it
11707 // updates the node in place to have the requested operands.
11708 if (Res == Node) {
11709 // If we updated the node in place, reset the node ID. To the isel,
11710 // this should be just like a newly allocated machine node.
11711 Res->setNodeId(-1);
11712 } else {
11715 }
11716
11717 return Res;
11718}
11719
11720/// getMachineNode - These are used for target selectors to create a new node
11721/// with specified return type(s), MachineInstr opcode, and operands.
11722///
11723/// Note that getMachineNode returns the resultant node. If there is already a
11724/// node of the specified opcode and operands, it returns that node instead of
11725/// the current one.
11727 EVT VT) {
11728 SDVTList VTs = getVTList(VT);
11729 return getMachineNode(Opcode, dl, VTs, {});
11730}
11731
11733 EVT VT, SDValue Op1) {
11734 SDVTList VTs = getVTList(VT);
11735 SDValue Ops[] = { Op1 };
11736 return getMachineNode(Opcode, dl, VTs, Ops);
11737}
11738
11740 EVT VT, SDValue Op1, SDValue Op2) {
11741 SDVTList VTs = getVTList(VT);
11742 SDValue Ops[] = { Op1, Op2 };
11743 return getMachineNode(Opcode, dl, VTs, Ops);
11744}
11745
11747 EVT VT, SDValue Op1, SDValue Op2,
11748 SDValue Op3) {
11749 SDVTList VTs = getVTList(VT);
11750 SDValue Ops[] = { Op1, Op2, Op3 };
11751 return getMachineNode(Opcode, dl, VTs, Ops);
11752}
11753
11756 SDVTList VTs = getVTList(VT);
11757 return getMachineNode(Opcode, dl, VTs, Ops);
11758}
11759
11761 EVT VT1, EVT VT2, SDValue Op1,
11762 SDValue Op2) {
11763 SDVTList VTs = getVTList(VT1, VT2);
11764 SDValue Ops[] = { Op1, Op2 };
11765 return getMachineNode(Opcode, dl, VTs, Ops);
11766}
11767
11769 EVT VT1, EVT VT2, SDValue Op1,
11770 SDValue Op2, SDValue Op3) {
11771 SDVTList VTs = getVTList(VT1, VT2);
11772 SDValue Ops[] = { Op1, Op2, Op3 };
11773 return getMachineNode(Opcode, dl, VTs, Ops);
11774}
11775
11777 EVT VT1, EVT VT2,
11779 SDVTList VTs = getVTList(VT1, VT2);
11780 return getMachineNode(Opcode, dl, VTs, Ops);
11781}
11782
11784 EVT VT1, EVT VT2, EVT VT3,
11785 SDValue Op1, SDValue Op2) {
11786 SDVTList VTs = getVTList(VT1, VT2, VT3);
11787 SDValue Ops[] = { Op1, Op2 };
11788 return getMachineNode(Opcode, dl, VTs, Ops);
11789}
11790
11792 EVT VT1, EVT VT2, EVT VT3,
11793 SDValue Op1, SDValue Op2,
11794 SDValue Op3) {
11795 SDVTList VTs = getVTList(VT1, VT2, VT3);
11796 SDValue Ops[] = { Op1, Op2, Op3 };
11797 return getMachineNode(Opcode, dl, VTs, Ops);
11798}
11799
11801 EVT VT1, EVT VT2, EVT VT3,
11803 SDVTList VTs = getVTList(VT1, VT2, VT3);
11804 return getMachineNode(Opcode, dl, VTs, Ops);
11805}
11806
11808 ArrayRef<EVT> ResultTys,
11810 SDVTList VTs = getVTList(ResultTys);
11811 return getMachineNode(Opcode, dl, VTs, Ops);
11812}
11813
11815 SDVTList VTs,
11817 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11819 void *IP = nullptr;
11820
11821 if (DoCSE) {
11823 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11824 IP = nullptr;
11825 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11826 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11827 }
11828 }
11829
11830 // Allocate a new MachineSDNode.
11831 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11832 createOperands(N, Ops);
11833
11834 if (DoCSE)
11835 CSEMap.InsertNode(N, IP);
11836
11837 InsertNode(N);
11838 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11839 return N;
11840}
11841
11842/// getTargetExtractSubreg - A convenience function for creating
11843/// TargetOpcode::EXTRACT_SUBREG nodes.
11845 SDValue Operand) {
11846 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11847 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11848 VT, Operand, SRIdxVal);
11849 return SDValue(Subreg, 0);
11850}
11851
11852/// getTargetInsertSubreg - A convenience function for creating
11853/// TargetOpcode::INSERT_SUBREG nodes.
11855 SDValue Operand, SDValue Subreg) {
11856 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11857 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11858 VT, Operand, Subreg, SRIdxVal);
11859 return SDValue(Result, 0);
11860}
11861
11862/// getNodeIfExists - Get the specified node if it's already available, or
11863/// else return NULL.
11866 bool AllowCommute) {
11867 SDNodeFlags Flags;
11868 if (Inserter)
11869 Flags = Inserter->getFlags();
11870 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11871}
11872
11875 const SDNodeFlags Flags,
11876 bool AllowCommute) {
11877 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11878 return nullptr;
11879
11880 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11882 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11883 void *IP = nullptr;
11884 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11885 E->intersectFlagsWith(Flags);
11886 return E;
11887 }
11888 return nullptr;
11889 };
11890
11891 if (SDNode *Existing = Lookup(Ops))
11892 return Existing;
11893
11894 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11895 return Lookup({Ops[1], Ops[0]});
11896
11897 return nullptr;
11898}
11899
11900/// doesNodeExist - Check if a node exists without modifying its flags.
11901bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11903 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11905 AddNodeIDNode(ID, Opcode, VTList, Ops);
11906 void *IP = nullptr;
11907 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11908 return true;
11909 }
11910 return false;
11911}
11912
11913/// getDbgValue - Creates a SDDbgValue node.
11914///
11915/// SDNode
11917 SDNode *N, unsigned R, bool IsIndirect,
11918 const DebugLoc &DL, unsigned O) {
11919 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11920 "Expected inlined-at fields to agree");
11921 return new (DbgInfo->getAlloc())
11922 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11923 {}, IsIndirect, DL, O,
11924 /*IsVariadic=*/false);
11925}
11926
11927/// Constant
11929 DIExpression *Expr,
11930 const Value *C,
11931 const DebugLoc &DL, unsigned O) {
11932 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11933 "Expected inlined-at fields to agree");
11934 return new (DbgInfo->getAlloc())
11935 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11936 /*IsIndirect=*/false, DL, O,
11937 /*IsVariadic=*/false);
11938}
11939
11940/// FrameIndex
11942 DIExpression *Expr, unsigned FI,
11943 bool IsIndirect,
11944 const DebugLoc &DL,
11945 unsigned O) {
11946 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11947 "Expected inlined-at fields to agree");
11948 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11949}
11950
11951/// FrameIndex with dependencies
11953 DIExpression *Expr, unsigned FI,
11954 ArrayRef<SDNode *> Dependencies,
11955 bool IsIndirect,
11956 const DebugLoc &DL,
11957 unsigned O) {
11958 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11959 "Expected inlined-at fields to agree");
11960 return new (DbgInfo->getAlloc())
11961 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11962 Dependencies, IsIndirect, DL, O,
11963 /*IsVariadic=*/false);
11964}
11965
11966/// VReg
11968 Register VReg, bool IsIndirect,
11969 const DebugLoc &DL, unsigned O) {
11970 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11971 "Expected inlined-at fields to agree");
11972 return new (DbgInfo->getAlloc())
11973 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11974 {}, IsIndirect, DL, O,
11975 /*IsVariadic=*/false);
11976}
11977
11980 ArrayRef<SDNode *> Dependencies,
11981 bool IsIndirect, const DebugLoc &DL,
11982 unsigned O, bool IsVariadic) {
11983 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11984 "Expected inlined-at fields to agree");
11985 return new (DbgInfo->getAlloc())
11986 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11987 DL, O, IsVariadic);
11988}
11989
11991 unsigned OffsetInBits, unsigned SizeInBits,
11992 bool InvalidateDbg) {
11993 SDNode *FromNode = From.getNode();
11994 SDNode *ToNode = To.getNode();
11995 assert(FromNode && ToNode && "Can't modify dbg values");
11996
11997 // PR35338
11998 // TODO: assert(From != To && "Redundant dbg value transfer");
11999 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12000 if (From == To || FromNode == ToNode)
12001 return;
12002
12003 if (!FromNode->getHasDebugValue())
12004 return;
12005
12006 SDDbgOperand FromLocOp =
12007 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12009
12011 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12012 if (Dbg->isInvalidated())
12013 continue;
12014
12015 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12016
12017 // Create a new location ops vector that is equal to the old vector, but
12018 // with each instance of FromLocOp replaced with ToLocOp.
12019 bool Changed = false;
12020 auto NewLocOps = Dbg->copyLocationOps();
12021 std::replace_if(
12022 NewLocOps.begin(), NewLocOps.end(),
12023 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12024 bool Match = Op == FromLocOp;
12025 Changed |= Match;
12026 return Match;
12027 },
12028 ToLocOp);
12029 // Ignore this SDDbgValue if we didn't find a matching location.
12030 if (!Changed)
12031 continue;
12032
12033 DIVariable *Var = Dbg->getVariable();
12034 auto *Expr = Dbg->getExpression();
12035 // If a fragment is requested, update the expression.
12036 if (SizeInBits) {
12037 // When splitting a larger (e.g., sign-extended) value whose
12038 // lower bits are described with an SDDbgValue, do not attempt
12039 // to transfer the SDDbgValue to the upper bits.
12040 if (auto FI = Expr->getFragmentInfo())
12041 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12042 continue;
12043 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12044 SizeInBits);
12045 if (!Fragment)
12046 continue;
12047 Expr = *Fragment;
12048 }
12049
12050 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12051 // Clone the SDDbgValue and move it to To.
12052 SDDbgValue *Clone = getDbgValueList(
12053 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12054 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12055 Dbg->isVariadic());
12056 ClonedDVs.push_back(Clone);
12057
12058 if (InvalidateDbg) {
12059 // Invalidate value and indicate the SDDbgValue should not be emitted.
12060 Dbg->setIsInvalidated();
12061 Dbg->setIsEmitted();
12062 }
12063 }
12064
12065 for (SDDbgValue *Dbg : ClonedDVs) {
12066 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12067 "Transferred DbgValues should depend on the new SDNode");
12068 AddDbgValue(Dbg, false);
12069 }
12070}
12071
12073 if (!N.getHasDebugValue())
12074 return;
12075
12076 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12077 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12078 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12079 return SDDbgOperand::fromNode(Node, ResNo);
12080 };
12081
12083 for (auto *DV : GetDbgValues(&N)) {
12084 if (DV->isInvalidated())
12085 continue;
12086 switch (N.getOpcode()) {
12087 default:
12088 break;
12089 case ISD::ADD: {
12090 SDValue N0 = N.getOperand(0);
12091 SDValue N1 = N.getOperand(1);
12092 if (!isa<ConstantSDNode>(N0)) {
12093 bool RHSConstant = isa<ConstantSDNode>(N1);
12095 if (RHSConstant)
12096 Offset = N.getConstantOperandVal(1);
12097 // We are not allowed to turn indirect debug values variadic, so
12098 // don't salvage those.
12099 if (!RHSConstant && DV->isIndirect())
12100 continue;
12101
12102 // Rewrite an ADD constant node into a DIExpression. Since we are
12103 // performing arithmetic to compute the variable's *value* in the
12104 // DIExpression, we need to mark the expression with a
12105 // DW_OP_stack_value.
12106 auto *DIExpr = DV->getExpression();
12107 auto NewLocOps = DV->copyLocationOps();
12108 bool Changed = false;
12109 size_t OrigLocOpsSize = NewLocOps.size();
12110 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12111 // We're not given a ResNo to compare against because the whole
12112 // node is going away. We know that any ISD::ADD only has one
12113 // result, so we can assume any node match is using the result.
12114 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12115 NewLocOps[i].getSDNode() != &N)
12116 continue;
12117 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12118 if (RHSConstant) {
12121 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12122 } else {
12123 // Convert to a variadic expression (if not already).
12124 // convertToVariadicExpression() returns a const pointer, so we use
12125 // a temporary const variable here.
12126 const auto *TmpDIExpr =
12130 ExprOps.push_back(NewLocOps.size());
12131 ExprOps.push_back(dwarf::DW_OP_plus);
12132 SDDbgOperand RHS =
12134 NewLocOps.push_back(RHS);
12135 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12136 }
12137 Changed = true;
12138 }
12139 (void)Changed;
12140 assert(Changed && "Salvage target doesn't use N");
12141
12142 bool IsVariadic =
12143 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12144
12145 auto AdditionalDependencies = DV->getAdditionalDependencies();
12146 SDDbgValue *Clone = getDbgValueList(
12147 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12148 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12149 ClonedDVs.push_back(Clone);
12150 DV->setIsInvalidated();
12151 DV->setIsEmitted();
12152 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12153 N0.getNode()->dumprFull(this);
12154 dbgs() << " into " << *DIExpr << '\n');
12155 }
12156 break;
12157 }
12158 case ISD::TRUNCATE: {
12159 SDValue N0 = N.getOperand(0);
12160 TypeSize FromSize = N0.getValueSizeInBits();
12161 TypeSize ToSize = N.getValueSizeInBits(0);
12162
12163 DIExpression *DbgExpression = DV->getExpression();
12164 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12165 auto NewLocOps = DV->copyLocationOps();
12166 bool Changed = false;
12167 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12168 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12169 NewLocOps[i].getSDNode() != &N)
12170 continue;
12171
12172 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12173 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12174 Changed = true;
12175 }
12176 assert(Changed && "Salvage target doesn't use N");
12177 (void)Changed;
12178
12179 SDDbgValue *Clone =
12180 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12181 DV->getAdditionalDependencies(), DV->isIndirect(),
12182 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12183
12184 ClonedDVs.push_back(Clone);
12185 DV->setIsInvalidated();
12186 DV->setIsEmitted();
12187 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12188 dbgs() << " into " << *DbgExpression << '\n');
12189 break;
12190 }
12191 }
12192 }
12193
12194 for (SDDbgValue *Dbg : ClonedDVs) {
12195 assert((!Dbg->getSDNodes().empty() ||
12196 llvm::any_of(Dbg->getLocationOps(),
12197 [&](const SDDbgOperand &Op) {
12198 return Op.getKind() == SDDbgOperand::FRAMEIX;
12199 })) &&
12200 "Salvaged DbgValue should depend on a new SDNode");
12201 AddDbgValue(Dbg, false);
12202 }
12203}
12204
12205/// Creates a SDDbgLabel node.
12207 const DebugLoc &DL, unsigned O) {
12208 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12209 "Expected inlined-at fields to agree");
12210 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12211}
12212
12213namespace {
12214
12215/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12216/// pointed to by a use iterator is deleted, increment the use iterator
12217/// so that it doesn't dangle.
12218///
12219class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12222
12223 void NodeDeleted(SDNode *N, SDNode *E) override {
12224 // Increment the iterator as needed.
12225 while (UI != UE && N == UI->getUser())
12226 ++UI;
12227 }
12228
12229public:
12230 RAUWUpdateListener(SelectionDAG &d,
12233 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12234};
12235
12236} // end anonymous namespace
12237
12238/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12239/// This can cause recursive merging of nodes in the DAG.
12240///
12241/// This version assumes From has a single result value.
12242///
12244 SDNode *From = FromN.getNode();
12245 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12246 "Cannot replace with this method!");
12247 assert(From != To.getNode() && "Cannot replace uses of with self");
12248
12249 // Preserve Debug Values
12250 transferDbgValues(FromN, To);
12251 // Preserve extra info.
12252 copyExtraInfo(From, To.getNode());
12253
12254 // Iterate over all the existing uses of From. New uses will be added
12255 // to the beginning of the use list, which we avoid visiting.
12256 // This specifically avoids visiting uses of From that arise while the
12257 // replacement is happening, because any such uses would be the result
12258 // of CSE: If an existing node looks like From after one of its operands
12259 // is replaced by To, we don't want to replace of all its users with To
12260 // too. See PR3018 for more info.
12261 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12262 RAUWUpdateListener Listener(*this, UI, UE);
12263 while (UI != UE) {
12264 SDNode *User = UI->getUser();
12265
12266 // This node is about to morph, remove its old self from the CSE maps.
12267 RemoveNodeFromCSEMaps(User);
12268
12269 // A user can appear in a use list multiple times, and when this
12270 // happens the uses are usually next to each other in the list.
12271 // To help reduce the number of CSE recomputations, process all
12272 // the uses of this user that we can find this way.
12273 do {
12274 SDUse &Use = *UI;
12275 ++UI;
12276 Use.set(To);
12277 if (To->isDivergent() != From->isDivergent())
12279 } while (UI != UE && UI->getUser() == User);
12280 // Now that we have modified User, add it back to the CSE maps. If it
12281 // already exists there, recursively merge the results together.
12282 AddModifiedNodeToCSEMaps(User);
12283 }
12284
12285 // If we just RAUW'd the root, take note.
12286 if (FromN == getRoot())
12287 setRoot(To);
12288}
12289
12290/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12291/// This can cause recursive merging of nodes in the DAG.
12292///
12293/// This version assumes that for each value of From, there is a
12294/// corresponding value in To in the same position with the same type.
12295///
12297#ifndef NDEBUG
12298 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12299 assert((!From->hasAnyUseOfValue(i) ||
12300 From->getValueType(i) == To->getValueType(i)) &&
12301 "Cannot use this version of ReplaceAllUsesWith!");
12302#endif
12303
12304 // Handle the trivial case.
12305 if (From == To)
12306 return;
12307
12308 // Preserve Debug Info. Only do this if there's a use.
12309 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12310 if (From->hasAnyUseOfValue(i)) {
12311 assert((i < To->getNumValues()) && "Invalid To location");
12312 transferDbgValues(SDValue(From, i), SDValue(To, i));
12313 }
12314 // Preserve extra info.
12315 copyExtraInfo(From, To);
12316
12317 // Iterate over just the existing users of From. See the comments in
12318 // the ReplaceAllUsesWith above.
12319 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12320 RAUWUpdateListener Listener(*this, UI, UE);
12321 while (UI != UE) {
12322 SDNode *User = UI->getUser();
12323
12324 // This node is about to morph, remove its old self from the CSE maps.
12325 RemoveNodeFromCSEMaps(User);
12326
12327 // A user can appear in a use list multiple times, and when this
12328 // happens the uses are usually next to each other in the list.
12329 // To help reduce the number of CSE recomputations, process all
12330 // the uses of this user that we can find this way.
12331 do {
12332 SDUse &Use = *UI;
12333 ++UI;
12334 Use.setNode(To);
12335 if (To->isDivergent() != From->isDivergent())
12337 } while (UI != UE && UI->getUser() == User);
12338
12339 // Now that we have modified User, add it back to the CSE maps. If it
12340 // already exists there, recursively merge the results together.
12341 AddModifiedNodeToCSEMaps(User);
12342 }
12343
12344 // If we just RAUW'd the root, take note.
12345 if (From == getRoot().getNode())
12346 setRoot(SDValue(To, getRoot().getResNo()));
12347}
12348
12349/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12350/// This can cause recursive merging of nodes in the DAG.
12351///
12352/// This version can replace From with any result values. To must match the
12353/// number and types of values returned by From.
12355 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12356 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12357
12358 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12359 // Preserve Debug Info.
12360 transferDbgValues(SDValue(From, i), To[i]);
12361 // Preserve extra info.
12362 copyExtraInfo(From, To[i].getNode());
12363 }
12364
12365 // Iterate over just the existing users of From. See the comments in
12366 // the ReplaceAllUsesWith above.
12367 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12368 RAUWUpdateListener Listener(*this, UI, UE);
12369 while (UI != UE) {
12370 SDNode *User = UI->getUser();
12371
12372 // This node is about to morph, remove its old self from the CSE maps.
12373 RemoveNodeFromCSEMaps(User);
12374
12375 // A user can appear in a use list multiple times, and when this happens the
12376 // uses are usually next to each other in the list. To help reduce the
12377 // number of CSE and divergence recomputations, process all the uses of this
12378 // user that we can find this way.
12379 bool To_IsDivergent = false;
12380 do {
12381 SDUse &Use = *UI;
12382 const SDValue &ToOp = To[Use.getResNo()];
12383 ++UI;
12384 Use.set(ToOp);
12385 To_IsDivergent |= ToOp->isDivergent();
12386 } while (UI != UE && UI->getUser() == User);
12387
12388 if (To_IsDivergent != From->isDivergent())
12390
12391 // Now that we have modified User, add it back to the CSE maps. If it
12392 // already exists there, recursively merge the results together.
12393 AddModifiedNodeToCSEMaps(User);
12394 }
12395
12396 // If we just RAUW'd the root, take note.
12397 if (From == getRoot().getNode())
12398 setRoot(SDValue(To[getRoot().getResNo()]));
12399}
12400
12401/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12402/// uses of other values produced by From.getNode() alone. The Deleted
12403/// vector is handled the same way as for ReplaceAllUsesWith.
12405 // Handle the really simple, really trivial case efficiently.
12406 if (From == To) return;
12407
12408 // Handle the simple, trivial, case efficiently.
12409 if (From.getNode()->getNumValues() == 1) {
12410 ReplaceAllUsesWith(From, To);
12411 return;
12412 }
12413
12414 // Preserve Debug Info.
12415 transferDbgValues(From, To);
12416 copyExtraInfo(From.getNode(), To.getNode());
12417
12418 // Iterate over just the existing users of From. See the comments in
12419 // the ReplaceAllUsesWith above.
12420 SDNode::use_iterator UI = From.getNode()->use_begin(),
12421 UE = From.getNode()->use_end();
12422 RAUWUpdateListener Listener(*this, UI, UE);
12423 while (UI != UE) {
12424 SDNode *User = UI->getUser();
12425 bool UserRemovedFromCSEMaps = false;
12426
12427 // A user can appear in a use list multiple times, and when this
12428 // happens the uses are usually next to each other in the list.
12429 // To help reduce the number of CSE recomputations, process all
12430 // the uses of this user that we can find this way.
12431 do {
12432 SDUse &Use = *UI;
12433
12434 // Skip uses of different values from the same node.
12435 if (Use.getResNo() != From.getResNo()) {
12436 ++UI;
12437 continue;
12438 }
12439
12440 // If this node hasn't been modified yet, it's still in the CSE maps,
12441 // so remove its old self from the CSE maps.
12442 if (!UserRemovedFromCSEMaps) {
12443 RemoveNodeFromCSEMaps(User);
12444 UserRemovedFromCSEMaps = true;
12445 }
12446
12447 ++UI;
12448 Use.set(To);
12449 if (To->isDivergent() != From->isDivergent())
12451 } while (UI != UE && UI->getUser() == User);
12452 // We are iterating over all uses of the From node, so if a use
12453 // doesn't use the specific value, no changes are made.
12454 if (!UserRemovedFromCSEMaps)
12455 continue;
12456
12457 // Now that we have modified User, add it back to the CSE maps. If it
12458 // already exists there, recursively merge the results together.
12459 AddModifiedNodeToCSEMaps(User);
12460 }
12461
12462 // If we just RAUW'd the root, take note.
12463 if (From == getRoot())
12464 setRoot(To);
12465}
12466
12467namespace {
12468
12469/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12470/// to record information about a use.
12471struct UseMemo {
12472 SDNode *User;
12473 unsigned Index;
12474 SDUse *Use;
12475};
12476
12477/// operator< - Sort Memos by User.
12478bool operator<(const UseMemo &L, const UseMemo &R) {
12479 return (intptr_t)L.User < (intptr_t)R.User;
12480}
12481
12482/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12483/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12484/// the node already has been taken care of recursively.
12485class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12486 SmallVectorImpl<UseMemo> &Uses;
12487
12488 void NodeDeleted(SDNode *N, SDNode *E) override {
12489 for (UseMemo &Memo : Uses)
12490 if (Memo.User == N)
12491 Memo.User = nullptr;
12492 }
12493
12494public:
12495 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12496 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12497};
12498
12499} // end anonymous namespace
12500
12501/// Return true if a glue output should propagate divergence information.
12503 switch (Node->getOpcode()) {
12504 case ISD::CopyFromReg:
12505 case ISD::CopyToReg:
12506 return false;
12507 default:
12508 return true;
12509 }
12510
12511 llvm_unreachable("covered opcode switch");
12512}
12513
12515 if (TLI->isSDNodeAlwaysUniform(N)) {
12516 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12517 "Conflicting divergence information!");
12518 return false;
12519 }
12520 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12521 return true;
12522 for (const auto &Op : N->ops()) {
12523 EVT VT = Op.getValueType();
12524
12525 // Skip Chain. It does not carry divergence.
12526 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12527 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12528 return true;
12529 }
12530 return false;
12531}
12532
12534 SmallVector<SDNode *, 16> Worklist(1, N);
12535 do {
12536 N = Worklist.pop_back_val();
12537 bool IsDivergent = calculateDivergence(N);
12538 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12539 N->SDNodeBits.IsDivergent = IsDivergent;
12540 llvm::append_range(Worklist, N->users());
12541 }
12542 } while (!Worklist.empty());
12543}
12544
12545void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12547 Order.reserve(AllNodes.size());
12548 for (auto &N : allnodes()) {
12549 unsigned NOps = N.getNumOperands();
12550 Degree[&N] = NOps;
12551 if (0 == NOps)
12552 Order.push_back(&N);
12553 }
12554 for (size_t I = 0; I != Order.size(); ++I) {
12555 SDNode *N = Order[I];
12556 for (auto *U : N->users()) {
12557 unsigned &UnsortedOps = Degree[U];
12558 if (0 == --UnsortedOps)
12559 Order.push_back(U);
12560 }
12561 }
12562}
12563
12564#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12565void SelectionDAG::VerifyDAGDivergence() {
12566 std::vector<SDNode *> TopoOrder;
12567 CreateTopologicalOrder(TopoOrder);
12568 for (auto *N : TopoOrder) {
12569 assert(calculateDivergence(N) == N->isDivergent() &&
12570 "Divergence bit inconsistency detected");
12571 }
12572}
12573#endif
12574
12575/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12576/// uses of other values produced by From.getNode() alone. The same value
12577/// may appear in both the From and To list. The Deleted vector is
12578/// handled the same way as for ReplaceAllUsesWith.
12580 const SDValue *To,
12581 unsigned Num){
12582 // Handle the simple, trivial case efficiently.
12583 if (Num == 1)
12584 return ReplaceAllUsesOfValueWith(*From, *To);
12585
12586 transferDbgValues(*From, *To);
12587 copyExtraInfo(From->getNode(), To->getNode());
12588
12589 // Read up all the uses and make records of them. This helps
12590 // processing new uses that are introduced during the
12591 // replacement process.
12593 for (unsigned i = 0; i != Num; ++i) {
12594 unsigned FromResNo = From[i].getResNo();
12595 SDNode *FromNode = From[i].getNode();
12596 for (SDUse &Use : FromNode->uses()) {
12597 if (Use.getResNo() == FromResNo) {
12598 UseMemo Memo = {Use.getUser(), i, &Use};
12599 Uses.push_back(Memo);
12600 }
12601 }
12602 }
12603
12604 // Sort the uses, so that all the uses from a given User are together.
12606 RAUOVWUpdateListener Listener(*this, Uses);
12607
12608 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12609 UseIndex != UseIndexEnd; ) {
12610 // We know that this user uses some value of From. If it is the right
12611 // value, update it.
12612 SDNode *User = Uses[UseIndex].User;
12613 // If the node has been deleted by recursive CSE updates when updating
12614 // another node, then just skip this entry.
12615 if (User == nullptr) {
12616 ++UseIndex;
12617 continue;
12618 }
12619
12620 // This node is about to morph, remove its old self from the CSE maps.
12621 RemoveNodeFromCSEMaps(User);
12622
12623 // The Uses array is sorted, so all the uses for a given User
12624 // are next to each other in the list.
12625 // To help reduce the number of CSE recomputations, process all
12626 // the uses of this user that we can find this way.
12627 do {
12628 unsigned i = Uses[UseIndex].Index;
12629 SDUse &Use = *Uses[UseIndex].Use;
12630 ++UseIndex;
12631
12632 Use.set(To[i]);
12633 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12634
12635 // Now that we have modified User, add it back to the CSE maps. If it
12636 // already exists there, recursively merge the results together.
12637 AddModifiedNodeToCSEMaps(User);
12638 }
12639}
12640
12641/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12642/// based on their topological order. It returns the maximum id and a vector
12643/// of the SDNodes* in assigned order by reference.
12645 unsigned DAGSize = 0;
12646
12647 // SortedPos tracks the progress of the algorithm. Nodes before it are
12648 // sorted, nodes after it are unsorted. When the algorithm completes
12649 // it is at the end of the list.
12650 allnodes_iterator SortedPos = allnodes_begin();
12651
12652 // Visit all the nodes. Move nodes with no operands to the front of
12653 // the list immediately. Annotate nodes that do have operands with their
12654 // operand count. Before we do this, the Node Id fields of the nodes
12655 // may contain arbitrary values. After, the Node Id fields for nodes
12656 // before SortedPos will contain the topological sort index, and the
12657 // Node Id fields for nodes At SortedPos and after will contain the
12658 // count of outstanding operands.
12660 checkForCycles(&N, this);
12661 unsigned Degree = N.getNumOperands();
12662 if (Degree == 0) {
12663 // A node with no uses, add it to the result array immediately.
12664 N.setNodeId(DAGSize++);
12665 allnodes_iterator Q(&N);
12666 if (Q != SortedPos)
12667 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12668 assert(SortedPos != AllNodes.end() && "Overran node list");
12669 ++SortedPos;
12670 } else {
12671 // Temporarily use the Node Id as scratch space for the degree count.
12672 N.setNodeId(Degree);
12673 }
12674 }
12675
12676 // Visit all the nodes. As we iterate, move nodes into sorted order,
12677 // such that by the time the end is reached all nodes will be sorted.
12678 for (SDNode &Node : allnodes()) {
12679 SDNode *N = &Node;
12680 checkForCycles(N, this);
12681 // N is in sorted position, so all its uses have one less operand
12682 // that needs to be sorted.
12683 for (SDNode *P : N->users()) {
12684 unsigned Degree = P->getNodeId();
12685 assert(Degree != 0 && "Invalid node degree");
12686 --Degree;
12687 if (Degree == 0) {
12688 // All of P's operands are sorted, so P may sorted now.
12689 P->setNodeId(DAGSize++);
12690 if (P->getIterator() != SortedPos)
12691 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12692 assert(SortedPos != AllNodes.end() && "Overran node list");
12693 ++SortedPos;
12694 } else {
12695 // Update P's outstanding operand count.
12696 P->setNodeId(Degree);
12697 }
12698 }
12699 if (Node.getIterator() == SortedPos) {
12700#ifndef NDEBUG
12702 SDNode *S = &*++I;
12703 dbgs() << "Overran sorted position:\n";
12704 S->dumprFull(this); dbgs() << "\n";
12705 dbgs() << "Checking if this is due to cycles\n";
12706 checkForCycles(this, true);
12707#endif
12708 llvm_unreachable(nullptr);
12709 }
12710 }
12711
12712 assert(SortedPos == AllNodes.end() &&
12713 "Topological sort incomplete!");
12714 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12715 "First node in topological sort is not the entry token!");
12716 assert(AllNodes.front().getNodeId() == 0 &&
12717 "First node in topological sort has non-zero id!");
12718 assert(AllNodes.front().getNumOperands() == 0 &&
12719 "First node in topological sort has operands!");
12720 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12721 "Last node in topologic sort has unexpected id!");
12722 assert(AllNodes.back().use_empty() &&
12723 "Last node in topologic sort has users!");
12724 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12725 return DAGSize;
12726}
12727
12729 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12730 SortedNodes.clear();
12731 // Node -> remaining number of outstanding operands.
12732 DenseMap<const SDNode *, unsigned> RemainingOperands;
12733
12734 // Put nodes without any operands into SortedNodes first.
12735 for (const SDNode &N : allnodes()) {
12736 checkForCycles(&N, this);
12737 unsigned NumOperands = N.getNumOperands();
12738 if (NumOperands == 0)
12739 SortedNodes.push_back(&N);
12740 else
12741 // Record their total number of outstanding operands.
12742 RemainingOperands[&N] = NumOperands;
12743 }
12744
12745 // A node is pushed into SortedNodes when all of its operands (predecessors in
12746 // the graph) are also in SortedNodes.
12747 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12748 const SDNode *N = SortedNodes[i];
12749 for (const SDNode *U : N->users()) {
12750 // HandleSDNode is never part of a DAG and therefore has no entry in
12751 // RemainingOperands.
12752 if (U->getOpcode() == ISD::HANDLENODE)
12753 continue;
12754 unsigned &NumRemOperands = RemainingOperands[U];
12755 assert(NumRemOperands && "Invalid number of remaining operands");
12756 --NumRemOperands;
12757 if (!NumRemOperands)
12758 SortedNodes.push_back(U);
12759 }
12760 }
12761
12762 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12763 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12764 "First node in topological sort is not the entry token");
12765 assert(SortedNodes.front()->getNumOperands() == 0 &&
12766 "First node in topological sort has operands");
12767}
12768
12769/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12770/// value is produced by SD.
12771void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12772 for (SDNode *SD : DB->getSDNodes()) {
12773 if (!SD)
12774 continue;
12775 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12776 SD->setHasDebugValue(true);
12777 }
12778 DbgInfo->add(DB, isParameter);
12779}
12780
12781void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12782
12784 SDValue NewMemOpChain) {
12785 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12786 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12787 // The new memory operation must have the same position as the old load in
12788 // terms of memory dependency. Create a TokenFactor for the old load and new
12789 // memory operation and update uses of the old load's output chain to use that
12790 // TokenFactor.
12791 if (OldChain == NewMemOpChain || OldChain.use_empty())
12792 return NewMemOpChain;
12793
12794 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12795 OldChain, NewMemOpChain);
12796 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12797 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12798 return TokenFactor;
12799}
12800
12802 SDValue NewMemOp) {
12803 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12804 SDValue OldChain = SDValue(OldLoad, 1);
12805 SDValue NewMemOpChain = NewMemOp.getValue(1);
12806 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12807}
12808
12810 Function **OutFunction) {
12811 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12812
12813 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12814 auto *Module = MF->getFunction().getParent();
12815 auto *Function = Module->getFunction(Symbol);
12816
12817 if (OutFunction != nullptr)
12818 *OutFunction = Function;
12819
12820 if (Function != nullptr) {
12821 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12822 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12823 }
12824
12825 std::string ErrorStr;
12826 raw_string_ostream ErrorFormatter(ErrorStr);
12827 ErrorFormatter << "Undefined external symbol ";
12828 ErrorFormatter << '"' << Symbol << '"';
12829 report_fatal_error(Twine(ErrorStr));
12830}
12831
12832//===----------------------------------------------------------------------===//
12833// SDNode Class
12834//===----------------------------------------------------------------------===//
12835
12838 return Const != nullptr && Const->isZero();
12839}
12840
12842 return V.isUndef() || isNullConstant(V);
12843}
12844
12847 return Const != nullptr && Const->isZero() && !Const->isNegative();
12848}
12849
12852 return Const != nullptr && Const->isAllOnes();
12853}
12854
12857 return Const != nullptr && Const->isOne();
12858}
12859
12862 return Const != nullptr && Const->isMinSignedValue();
12863}
12864
12865bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12866 unsigned OperandNo) {
12867 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12868 // TODO: Target-specific opcodes could be added.
12869 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12870 /*AllowTruncation*/ true)) {
12871 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12872 switch (Opcode) {
12873 case ISD::ADD:
12874 case ISD::OR:
12875 case ISD::XOR:
12876 case ISD::UMAX:
12877 return Const.isZero();
12878 case ISD::MUL:
12879 return Const.isOne();
12880 case ISD::AND:
12881 case ISD::UMIN:
12882 return Const.isAllOnes();
12883 case ISD::SMAX:
12884 return Const.isMinSignedValue();
12885 case ISD::SMIN:
12886 return Const.isMaxSignedValue();
12887 case ISD::SUB:
12888 case ISD::SHL:
12889 case ISD::SRA:
12890 case ISD::SRL:
12891 return OperandNo == 1 && Const.isZero();
12892 case ISD::UDIV:
12893 case ISD::SDIV:
12894 return OperandNo == 1 && Const.isOne();
12895 }
12896 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12897 switch (Opcode) {
12898 case ISD::FADD:
12899 return ConstFP->isZero() &&
12900 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12901 case ISD::FSUB:
12902 return OperandNo == 1 && ConstFP->isZero() &&
12903 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12904 case ISD::FMUL:
12905 return ConstFP->isExactlyValue(1.0);
12906 case ISD::FDIV:
12907 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12908 case ISD::FMINNUM:
12909 case ISD::FMAXNUM: {
12910 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12911 EVT VT = V.getValueType();
12912 const fltSemantics &Semantics = VT.getFltSemantics();
12913 APFloat NeutralAF = !Flags.hasNoNaNs()
12914 ? APFloat::getQNaN(Semantics)
12915 : !Flags.hasNoInfs()
12916 ? APFloat::getInf(Semantics)
12917 : APFloat::getLargest(Semantics);
12918 if (Opcode == ISD::FMAXNUM)
12919 NeutralAF.changeSign();
12920
12921 return ConstFP->isExactlyValue(NeutralAF);
12922 }
12923 }
12924 }
12925 return false;
12926}
12927
12929 while (V.getOpcode() == ISD::BITCAST)
12930 V = V.getOperand(0);
12931 return V;
12932}
12933
12935 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12936 V = V.getOperand(0);
12937 return V;
12938}
12939
12941 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12942 V = V.getOperand(0);
12943 return V;
12944}
12945
12947 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12948 SDValue InVec = V.getOperand(0);
12949 SDValue EltNo = V.getOperand(2);
12950 EVT VT = InVec.getValueType();
12951 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12952 if (IndexC && VT.isFixedLengthVector() &&
12953 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12954 !DemandedElts[IndexC->getZExtValue()]) {
12955 V = InVec;
12956 continue;
12957 }
12958 break;
12959 }
12960 return V;
12961}
12962
12964 while (V.getOpcode() == ISD::TRUNCATE)
12965 V = V.getOperand(0);
12966 return V;
12967}
12968
12969bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12970 if (V.getOpcode() != ISD::XOR)
12971 return false;
12972 V = peekThroughBitcasts(V.getOperand(1));
12973 unsigned NumBits = V.getScalarValueSizeInBits();
12974 ConstantSDNode *C =
12975 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12976 return C && (C->getAPIntValue().countr_one() >= NumBits);
12977}
12978
12980 bool AllowTruncation) {
12981 EVT VT = N.getValueType();
12982 APInt DemandedElts = VT.isFixedLengthVector()
12984 : APInt(1, 1);
12985 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12986}
12987
12989 bool AllowUndefs,
12990 bool AllowTruncation) {
12992 return CN;
12993
12994 // SplatVectors can truncate their operands. Ignore that case here unless
12995 // AllowTruncation is set.
12996 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12997 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12998 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12999 EVT CVT = CN->getValueType(0);
13000 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13001 if (AllowTruncation || CVT == VecEltVT)
13002 return CN;
13003 }
13004 }
13005
13007 BitVector UndefElements;
13008 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13009
13010 // BuildVectors can truncate their operands. Ignore that case here unless
13011 // AllowTruncation is set.
13012 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13013 if (CN && (UndefElements.none() || AllowUndefs)) {
13014 EVT CVT = CN->getValueType(0);
13015 EVT NSVT = N.getValueType().getScalarType();
13016 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13017 if (AllowTruncation || (CVT == NSVT))
13018 return CN;
13019 }
13020 }
13021
13022 return nullptr;
13023}
13024
13026 EVT VT = N.getValueType();
13027 APInt DemandedElts = VT.isFixedLengthVector()
13029 : APInt(1, 1);
13030 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13031}
13032
13034 const APInt &DemandedElts,
13035 bool AllowUndefs) {
13037 return CN;
13038
13040 BitVector UndefElements;
13041 ConstantFPSDNode *CN =
13042 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13043 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13044 if (CN && (UndefElements.none() || AllowUndefs))
13045 return CN;
13046 }
13047
13048 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13049 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13050 return CN;
13051
13052 return nullptr;
13053}
13054
13055bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13056 // TODO: may want to use peekThroughBitcast() here.
13057 ConstantSDNode *C =
13058 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13059 return C && C->isZero();
13060}
13061
13062bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13063 ConstantSDNode *C =
13064 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13065 return C && C->isOne();
13066}
13067
13068bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13069 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13070 return C && C->isExactlyValue(1.0);
13071}
13072
13073bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13075 unsigned BitWidth = N.getScalarValueSizeInBits();
13076 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13077 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13078}
13079
13080bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13081 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13082 return C && APInt::isSameValue(C->getAPIntValue(),
13083 APInt(C->getAPIntValue().getBitWidth(), 1));
13084}
13085
13086bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13088 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13089 return C && C->isZero();
13090}
13091
13095
13096MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13097 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13098 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13099 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13100 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13101 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13102 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13103
13104 // We check here that the size of the memory operand fits within the size of
13105 // the MMO. This is because the MMO might indicate only a possible address
13106 // range instead of specifying the affected memory addresses precisely.
13107 assert(
13108 (!MMO->getType().isValid() ||
13109 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13110 "Size mismatch!");
13111}
13112
13113/// Profile - Gather unique data for the node.
13114///
13116 AddNodeIDNode(ID, this);
13117}
13118
13119namespace {
13120
13121 struct EVTArray {
13122 std::vector<EVT> VTs;
13123
13124 EVTArray() {
13125 VTs.reserve(MVT::VALUETYPE_SIZE);
13126 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13127 VTs.push_back(MVT((MVT::SimpleValueType)i));
13128 }
13129 };
13130
13131} // end anonymous namespace
13132
13133/// getValueTypeList - Return a pointer to the specified value type.
13134///
13135const EVT *SDNode::getValueTypeList(MVT VT) {
13136 static EVTArray SimpleVTArray;
13137
13138 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13139 return &SimpleVTArray.VTs[VT.SimpleTy];
13140}
13141
13142/// hasAnyUseOfValue - Return true if there are any use of the indicated
13143/// value. This method ignores uses of other values defined by this operation.
13144bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13145 assert(Value < getNumValues() && "Bad value!");
13146
13147 for (SDUse &U : uses())
13148 if (U.getResNo() == Value)
13149 return true;
13150
13151 return false;
13152}
13153
13154/// isOnlyUserOf - Return true if this node is the only use of N.
13155bool SDNode::isOnlyUserOf(const SDNode *N) const {
13156 bool Seen = false;
13157 for (const SDNode *User : N->users()) {
13158 if (User == this)
13159 Seen = true;
13160 else
13161 return false;
13162 }
13163
13164 return Seen;
13165}
13166
13167/// Return true if the only users of N are contained in Nodes.
13169 bool Seen = false;
13170 for (const SDNode *User : N->users()) {
13171 if (llvm::is_contained(Nodes, User))
13172 Seen = true;
13173 else
13174 return false;
13175 }
13176
13177 return Seen;
13178}
13179
13180/// Return true if the referenced return value is an operand of N.
13181bool SDValue::isOperandOf(const SDNode *N) const {
13182 return is_contained(N->op_values(), *this);
13183}
13184
13185bool SDNode::isOperandOf(const SDNode *N) const {
13186 return any_of(N->op_values(),
13187 [this](SDValue Op) { return this == Op.getNode(); });
13188}
13189
13190/// reachesChainWithoutSideEffects - Return true if this operand (which must
13191/// be a chain) reaches the specified operand without crossing any
13192/// side-effecting instructions on any chain path. In practice, this looks
13193/// through token factors and non-volatile loads. In order to remain efficient,
13194/// this only looks a couple of nodes in, it does not do an exhaustive search.
13195///
13196/// Note that we only need to examine chains when we're searching for
13197/// side-effects; SelectionDAG requires that all side-effects are represented
13198/// by chains, even if another operand would force a specific ordering. This
13199/// constraint is necessary to allow transformations like splitting loads.
13201 unsigned Depth) const {
13202 if (*this == Dest) return true;
13203
13204 // Don't search too deeply, we just want to be able to see through
13205 // TokenFactor's etc.
13206 if (Depth == 0) return false;
13207
13208 // If this is a token factor, all inputs to the TF happen in parallel.
13209 if (getOpcode() == ISD::TokenFactor) {
13210 // First, try a shallow search.
13211 if (is_contained((*this)->ops(), Dest)) {
13212 // We found the chain we want as an operand of this TokenFactor.
13213 // Essentially, we reach the chain without side-effects if we could
13214 // serialize the TokenFactor into a simple chain of operations with
13215 // Dest as the last operation. This is automatically true if the
13216 // chain has one use: there are no other ordering constraints.
13217 // If the chain has more than one use, we give up: some other
13218 // use of Dest might force a side-effect between Dest and the current
13219 // node.
13220 if (Dest.hasOneUse())
13221 return true;
13222 }
13223 // Next, try a deep search: check whether every operand of the TokenFactor
13224 // reaches Dest.
13225 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13226 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13227 });
13228 }
13229
13230 // Loads don't have side effects, look through them.
13231 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13232 if (Ld->isUnordered())
13233 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13234 }
13235 return false;
13236}
13237
13238bool SDNode::hasPredecessor(const SDNode *N) const {
13241 Worklist.push_back(this);
13242 return hasPredecessorHelper(N, Visited, Worklist);
13243}
13244
13246 this->Flags &= Flags;
13247}
13248
13249SDValue
13251 ArrayRef<ISD::NodeType> CandidateBinOps,
13252 bool AllowPartials) {
13253 // The pattern must end in an extract from index 0.
13254 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13255 !isNullConstant(Extract->getOperand(1)))
13256 return SDValue();
13257
13258 // Match against one of the candidate binary ops.
13259 SDValue Op = Extract->getOperand(0);
13260 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13261 return Op.getOpcode() == unsigned(BinOp);
13262 }))
13263 return SDValue();
13264
13265 // Floating-point reductions may require relaxed constraints on the final step
13266 // of the reduction because they may reorder intermediate operations.
13267 unsigned CandidateBinOp = Op.getOpcode();
13268 if (Op.getValueType().isFloatingPoint()) {
13269 SDNodeFlags Flags = Op->getFlags();
13270 switch (CandidateBinOp) {
13271 case ISD::FADD:
13272 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13273 return SDValue();
13274 break;
13275 default:
13276 llvm_unreachable("Unhandled FP opcode for binop reduction");
13277 }
13278 }
13279
13280 // Matching failed - attempt to see if we did enough stages that a partial
13281 // reduction from a subvector is possible.
13282 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13283 if (!AllowPartials || !Op)
13284 return SDValue();
13285 EVT OpVT = Op.getValueType();
13286 EVT OpSVT = OpVT.getScalarType();
13287 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13288 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13289 return SDValue();
13290 BinOp = (ISD::NodeType)CandidateBinOp;
13291 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13292 };
13293
13294 // At each stage, we're looking for something that looks like:
13295 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13296 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13297 // i32 undef, i32 undef, i32 undef, i32 undef>
13298 // %a = binop <8 x i32> %op, %s
13299 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13300 // we expect something like:
13301 // <4,5,6,7,u,u,u,u>
13302 // <2,3,u,u,u,u,u,u>
13303 // <1,u,u,u,u,u,u,u>
13304 // While a partial reduction match would be:
13305 // <2,3,u,u,u,u,u,u>
13306 // <1,u,u,u,u,u,u,u>
13307 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13308 SDValue PrevOp;
13309 for (unsigned i = 0; i < Stages; ++i) {
13310 unsigned MaskEnd = (1 << i);
13311
13312 if (Op.getOpcode() != CandidateBinOp)
13313 return PartialReduction(PrevOp, MaskEnd);
13314
13315 SDValue Op0 = Op.getOperand(0);
13316 SDValue Op1 = Op.getOperand(1);
13317
13319 if (Shuffle) {
13320 Op = Op1;
13321 } else {
13322 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13323 Op = Op0;
13324 }
13325
13326 // The first operand of the shuffle should be the same as the other operand
13327 // of the binop.
13328 if (!Shuffle || Shuffle->getOperand(0) != Op)
13329 return PartialReduction(PrevOp, MaskEnd);
13330
13331 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13332 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13333 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13334 return PartialReduction(PrevOp, MaskEnd);
13335
13336 PrevOp = Op;
13337 }
13338
13339 // Handle subvector reductions, which tend to appear after the shuffle
13340 // reduction stages.
13341 while (Op.getOpcode() == CandidateBinOp) {
13342 unsigned NumElts = Op.getValueType().getVectorNumElements();
13343 SDValue Op0 = Op.getOperand(0);
13344 SDValue Op1 = Op.getOperand(1);
13345 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13347 Op0.getOperand(0) != Op1.getOperand(0))
13348 break;
13349 SDValue Src = Op0.getOperand(0);
13350 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13351 if (NumSrcElts != (2 * NumElts))
13352 break;
13353 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13354 Op1.getConstantOperandAPInt(1) == NumElts) &&
13355 !(Op1.getConstantOperandAPInt(1) == 0 &&
13356 Op0.getConstantOperandAPInt(1) == NumElts))
13357 break;
13358 Op = Src;
13359 }
13360
13361 BinOp = (ISD::NodeType)CandidateBinOp;
13362 return Op;
13363}
13364
13366 EVT VT = N->getValueType(0);
13367 EVT EltVT = VT.getVectorElementType();
13368 unsigned NE = VT.getVectorNumElements();
13369
13370 SDLoc dl(N);
13371
13372 // If ResNE is 0, fully unroll the vector op.
13373 if (ResNE == 0)
13374 ResNE = NE;
13375 else if (NE > ResNE)
13376 NE = ResNE;
13377
13378 if (N->getNumValues() == 2) {
13379 SmallVector<SDValue, 8> Scalars0, Scalars1;
13380 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13381 EVT VT1 = N->getValueType(1);
13382 EVT EltVT1 = VT1.getVectorElementType();
13383
13384 unsigned i;
13385 for (i = 0; i != NE; ++i) {
13386 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13387 SDValue Operand = N->getOperand(j);
13388 EVT OperandVT = Operand.getValueType();
13389
13390 // A vector operand; extract a single element.
13391 EVT OperandEltVT = OperandVT.getVectorElementType();
13392 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13393 }
13394
13395 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13396 Scalars0.push_back(EltOp);
13397 Scalars1.push_back(EltOp.getValue(1));
13398 }
13399
13400 for (; i < ResNE; ++i) {
13401 Scalars0.push_back(getUNDEF(EltVT));
13402 Scalars1.push_back(getUNDEF(EltVT1));
13403 }
13404
13405 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13406 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13407 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13408 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13409 return getMergeValues({Vec0, Vec1}, dl);
13410 }
13411
13412 assert(N->getNumValues() == 1 &&
13413 "Can't unroll a vector with multiple results!");
13414
13416 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13417
13418 unsigned i;
13419 for (i= 0; i != NE; ++i) {
13420 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13421 SDValue Operand = N->getOperand(j);
13422 EVT OperandVT = Operand.getValueType();
13423 if (OperandVT.isVector()) {
13424 // A vector operand; extract a single element.
13425 EVT OperandEltVT = OperandVT.getVectorElementType();
13426 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13427 } else {
13428 // A scalar operand; just use it as is.
13429 Operands[j] = Operand;
13430 }
13431 }
13432
13433 switch (N->getOpcode()) {
13434 default: {
13435 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13436 N->getFlags()));
13437 break;
13438 }
13439 case ISD::VSELECT:
13440 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13441 break;
13442 case ISD::SHL:
13443 case ISD::SRA:
13444 case ISD::SRL:
13445 case ISD::ROTL:
13446 case ISD::ROTR:
13447 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13448 getShiftAmountOperand(Operands[0].getValueType(),
13449 Operands[1])));
13450 break;
13452 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13453 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13454 Operands[0],
13455 getValueType(ExtVT)));
13456 break;
13457 }
13458 case ISD::ADDRSPACECAST: {
13459 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13460 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13461 ASC->getSrcAddressSpace(),
13462 ASC->getDestAddressSpace()));
13463 break;
13464 }
13465 }
13466 }
13467
13468 for (; i < ResNE; ++i)
13469 Scalars.push_back(getUNDEF(EltVT));
13470
13471 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13472 return getBuildVector(VecVT, dl, Scalars);
13473}
13474
13475std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13476 SDNode *N, unsigned ResNE) {
13477 unsigned Opcode = N->getOpcode();
13478 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13479 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13480 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13481 "Expected an overflow opcode");
13482
13483 EVT ResVT = N->getValueType(0);
13484 EVT OvVT = N->getValueType(1);
13485 EVT ResEltVT = ResVT.getVectorElementType();
13486 EVT OvEltVT = OvVT.getVectorElementType();
13487 SDLoc dl(N);
13488
13489 // If ResNE is 0, fully unroll the vector op.
13490 unsigned NE = ResVT.getVectorNumElements();
13491 if (ResNE == 0)
13492 ResNE = NE;
13493 else if (NE > ResNE)
13494 NE = ResNE;
13495
13496 SmallVector<SDValue, 8> LHSScalars;
13497 SmallVector<SDValue, 8> RHSScalars;
13498 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13499 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13500
13501 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13502 SDVTList VTs = getVTList(ResEltVT, SVT);
13503 SmallVector<SDValue, 8> ResScalars;
13504 SmallVector<SDValue, 8> OvScalars;
13505 for (unsigned i = 0; i < NE; ++i) {
13506 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13507 SDValue Ov =
13508 getSelect(dl, OvEltVT, Res.getValue(1),
13509 getBoolConstant(true, dl, OvEltVT, ResVT),
13510 getConstant(0, dl, OvEltVT));
13511
13512 ResScalars.push_back(Res);
13513 OvScalars.push_back(Ov);
13514 }
13515
13516 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13517 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13518
13519 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13520 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13521 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13522 getBuildVector(NewOvVT, dl, OvScalars));
13523}
13524
13527 unsigned Bytes,
13528 int Dist) const {
13529 if (LD->isVolatile() || Base->isVolatile())
13530 return false;
13531 // TODO: probably too restrictive for atomics, revisit
13532 if (!LD->isSimple())
13533 return false;
13534 if (LD->isIndexed() || Base->isIndexed())
13535 return false;
13536 if (LD->getChain() != Base->getChain())
13537 return false;
13538 EVT VT = LD->getMemoryVT();
13539 if (VT.getSizeInBits() / 8 != Bytes)
13540 return false;
13541
13542 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13543 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13544
13545 int64_t Offset = 0;
13546 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13547 return (Dist * (int64_t)Bytes == Offset);
13548 return false;
13549}
13550
13551/// InferPtrAlignment - Infer alignment of a load / store address. Return
13552/// std::nullopt if it cannot be inferred.
13554 // If this is a GlobalAddress + cst, return the alignment.
13555 const GlobalValue *GV = nullptr;
13556 int64_t GVOffset = 0;
13557 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13558 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13559 KnownBits Known(PtrWidth);
13561 unsigned AlignBits = Known.countMinTrailingZeros();
13562 if (AlignBits)
13563 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13564 }
13565
13566 // If this is a direct reference to a stack slot, use information about the
13567 // stack slot's alignment.
13568 int FrameIdx = INT_MIN;
13569 int64_t FrameOffset = 0;
13571 FrameIdx = FI->getIndex();
13572 } else if (isBaseWithConstantOffset(Ptr) &&
13573 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
13574 // Handle FI+Cst
13575 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13576 FrameOffset = Ptr.getConstantOperandVal(1);
13577 }
13578
13579 if (FrameIdx != INT_MIN) {
13581 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13582 }
13583
13584 return std::nullopt;
13585}
13586
13587/// Split the scalar node with EXTRACT_ELEMENT using the provided
13588/// VTs and return the low/high part.
13589std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13590 const SDLoc &DL,
13591 const EVT &LoVT,
13592 const EVT &HiVT) {
13593 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13594 "Split node must be a scalar type");
13595 SDValue Lo =
13597 SDValue Hi =
13599 return std::make_pair(Lo, Hi);
13600}
13601
13602/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13603/// which is split (or expanded) into two not necessarily identical pieces.
13604std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13605 // Currently all types are split in half.
13606 EVT LoVT, HiVT;
13607 if (!VT.isVector())
13608 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13609 else
13610 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13611
13612 return std::make_pair(LoVT, HiVT);
13613}
13614
13615/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13616/// type, dependent on an enveloping VT that has been split into two identical
13617/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13618std::pair<EVT, EVT>
13620 bool *HiIsEmpty) const {
13621 EVT EltTp = VT.getVectorElementType();
13622 // Examples:
13623 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13624 // custom VL=9 with enveloping VL=8/8 yields 8/1
13625 // custom VL=10 with enveloping VL=8/8 yields 8/2
13626 // etc.
13627 ElementCount VTNumElts = VT.getVectorElementCount();
13628 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13629 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13630 "Mixing fixed width and scalable vectors when enveloping a type");
13631 EVT LoVT, HiVT;
13632 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13633 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13634 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13635 *HiIsEmpty = false;
13636 } else {
13637 // Flag that hi type has zero storage size, but return split envelop type
13638 // (this would be easier if vector types with zero elements were allowed).
13639 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13640 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13641 *HiIsEmpty = true;
13642 }
13643 return std::make_pair(LoVT, HiVT);
13644}
13645
13646/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13647/// low/high part.
13648std::pair<SDValue, SDValue>
13649SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13650 const EVT &HiVT) {
13651 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13652 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13653 "Splitting vector with an invalid mixture of fixed and scalable "
13654 "vector types");
13656 N.getValueType().getVectorMinNumElements() &&
13657 "More vector elements requested than available!");
13658 SDValue Lo, Hi;
13659 Lo = getExtractSubvector(DL, LoVT, N, 0);
13660 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13661 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13662 // IDX with the runtime scaling factor of the result vector type. For
13663 // fixed-width result vectors, that runtime scaling factor is 1.
13666 return std::make_pair(Lo, Hi);
13667}
13668
13669std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13670 const SDLoc &DL) {
13671 // Split the vector length parameter.
13672 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13673 EVT VT = N.getValueType();
13675 "Expecting the mask to be an evenly-sized vector");
13676 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13677 SDValue HalfNumElts =
13678 VecVT.isFixedLengthVector()
13679 ? getConstant(HalfMinNumElts, DL, VT)
13680 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13681 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13682 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13683 return std::make_pair(Lo, Hi);
13684}
13685
13686/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13688 EVT VT = N.getValueType();
13691 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13692}
13693
13696 unsigned Start, unsigned Count,
13697 EVT EltVT) {
13698 EVT VT = Op.getValueType();
13699 if (Count == 0)
13701 if (EltVT == EVT())
13702 EltVT = VT.getVectorElementType();
13703 SDLoc SL(Op);
13704 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13705 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13706 }
13707}
13708
13709// getAddressSpace - Return the address space this GlobalAddress belongs to.
13711 return getGlobal()->getType()->getAddressSpace();
13712}
13713
13716 return Val.MachineCPVal->getType();
13717 return Val.ConstVal->getType();
13718}
13719
13720bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13721 unsigned &SplatBitSize,
13722 bool &HasAnyUndefs,
13723 unsigned MinSplatBits,
13724 bool IsBigEndian) const {
13725 EVT VT = getValueType(0);
13726 assert(VT.isVector() && "Expected a vector type");
13727 unsigned VecWidth = VT.getSizeInBits();
13728 if (MinSplatBits > VecWidth)
13729 return false;
13730
13731 // FIXME: The widths are based on this node's type, but build vectors can
13732 // truncate their operands.
13733 SplatValue = APInt(VecWidth, 0);
13734 SplatUndef = APInt(VecWidth, 0);
13735
13736 // Get the bits. Bits with undefined values (when the corresponding element
13737 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13738 // in SplatValue. If any of the values are not constant, give up and return
13739 // false.
13740 unsigned int NumOps = getNumOperands();
13741 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13742 unsigned EltWidth = VT.getScalarSizeInBits();
13743
13744 for (unsigned j = 0; j < NumOps; ++j) {
13745 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13746 SDValue OpVal = getOperand(i);
13747 unsigned BitPos = j * EltWidth;
13748
13749 if (OpVal.isUndef())
13750 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13751 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13752 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13753 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13754 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13755 else
13756 return false;
13757 }
13758
13759 // The build_vector is all constants or undefs. Find the smallest element
13760 // size that splats the vector.
13761 HasAnyUndefs = (SplatUndef != 0);
13762
13763 // FIXME: This does not work for vectors with elements less than 8 bits.
13764 while (VecWidth > 8) {
13765 // If we can't split in half, stop here.
13766 if (VecWidth & 1)
13767 break;
13768
13769 unsigned HalfSize = VecWidth / 2;
13770 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13771 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13772 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13773 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13774
13775 // If the two halves do not match (ignoring undef bits), stop here.
13776 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13777 MinSplatBits > HalfSize)
13778 break;
13779
13780 SplatValue = HighValue | LowValue;
13781 SplatUndef = HighUndef & LowUndef;
13782
13783 VecWidth = HalfSize;
13784 }
13785
13786 // FIXME: The loop above only tries to split in halves. But if the input
13787 // vector for example is <3 x i16> it wouldn't be able to detect a
13788 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13789 // optimizations. I guess that back in the days when this helper was created
13790 // vectors normally was power-of-2 sized.
13791
13792 SplatBitSize = VecWidth;
13793 return true;
13794}
13795
13797 BitVector *UndefElements) const {
13798 unsigned NumOps = getNumOperands();
13799 if (UndefElements) {
13800 UndefElements->clear();
13801 UndefElements->resize(NumOps);
13802 }
13803 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13804 if (!DemandedElts)
13805 return SDValue();
13806 SDValue Splatted;
13807 for (unsigned i = 0; i != NumOps; ++i) {
13808 if (!DemandedElts[i])
13809 continue;
13810 SDValue Op = getOperand(i);
13811 if (Op.isUndef()) {
13812 if (UndefElements)
13813 (*UndefElements)[i] = true;
13814 } else if (!Splatted) {
13815 Splatted = Op;
13816 } else if (Splatted != Op) {
13817 return SDValue();
13818 }
13819 }
13820
13821 if (!Splatted) {
13822 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13823 assert(getOperand(FirstDemandedIdx).isUndef() &&
13824 "Can only have a splat without a constant for all undefs.");
13825 return getOperand(FirstDemandedIdx);
13826 }
13827
13828 return Splatted;
13829}
13830
13832 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13833 return getSplatValue(DemandedElts, UndefElements);
13834}
13835
13837 SmallVectorImpl<SDValue> &Sequence,
13838 BitVector *UndefElements) const {
13839 unsigned NumOps = getNumOperands();
13840 Sequence.clear();
13841 if (UndefElements) {
13842 UndefElements->clear();
13843 UndefElements->resize(NumOps);
13844 }
13845 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13846 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13847 return false;
13848
13849 // Set the undefs even if we don't find a sequence (like getSplatValue).
13850 if (UndefElements)
13851 for (unsigned I = 0; I != NumOps; ++I)
13852 if (DemandedElts[I] && getOperand(I).isUndef())
13853 (*UndefElements)[I] = true;
13854
13855 // Iteratively widen the sequence length looking for repetitions.
13856 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13857 Sequence.append(SeqLen, SDValue());
13858 for (unsigned I = 0; I != NumOps; ++I) {
13859 if (!DemandedElts[I])
13860 continue;
13861 SDValue &SeqOp = Sequence[I % SeqLen];
13863 if (Op.isUndef()) {
13864 if (!SeqOp)
13865 SeqOp = Op;
13866 continue;
13867 }
13868 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13869 Sequence.clear();
13870 break;
13871 }
13872 SeqOp = Op;
13873 }
13874 if (!Sequence.empty())
13875 return true;
13876 }
13877
13878 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13879 return false;
13880}
13881
13883 BitVector *UndefElements) const {
13884 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13885 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13886}
13887
13890 BitVector *UndefElements) const {
13892 getSplatValue(DemandedElts, UndefElements));
13893}
13894
13897 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13898}
13899
13902 BitVector *UndefElements) const {
13904 getSplatValue(DemandedElts, UndefElements));
13905}
13906
13911
13912int32_t
13914 uint32_t BitWidth) const {
13915 if (ConstantFPSDNode *CN =
13917 bool IsExact;
13918 APSInt IntVal(BitWidth);
13919 const APFloat &APF = CN->getValueAPF();
13920 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13921 APFloat::opOK ||
13922 !IsExact)
13923 return -1;
13924
13925 return IntVal.exactLogBase2();
13926 }
13927 return -1;
13928}
13929
13931 bool IsLittleEndian, unsigned DstEltSizeInBits,
13932 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13933 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13934 if (!isConstant())
13935 return false;
13936
13937 unsigned NumSrcOps = getNumOperands();
13938 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13939 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13940 "Invalid bitcast scale");
13941
13942 // Extract raw src bits.
13943 SmallVector<APInt> SrcBitElements(NumSrcOps,
13944 APInt::getZero(SrcEltSizeInBits));
13945 BitVector SrcUndeElements(NumSrcOps, false);
13946
13947 for (unsigned I = 0; I != NumSrcOps; ++I) {
13949 if (Op.isUndef()) {
13950 SrcUndeElements.set(I);
13951 continue;
13952 }
13953 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13954 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13955 assert((CInt || CFP) && "Unknown constant");
13956 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13957 : CFP->getValueAPF().bitcastToAPInt();
13958 }
13959
13960 // Recast to dst width.
13961 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13962 SrcBitElements, UndefElements, SrcUndeElements);
13963 return true;
13964}
13965
13966void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13967 unsigned DstEltSizeInBits,
13968 SmallVectorImpl<APInt> &DstBitElements,
13969 ArrayRef<APInt> SrcBitElements,
13970 BitVector &DstUndefElements,
13971 const BitVector &SrcUndefElements) {
13972 unsigned NumSrcOps = SrcBitElements.size();
13973 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13974 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13975 "Invalid bitcast scale");
13976 assert(NumSrcOps == SrcUndefElements.size() &&
13977 "Vector size mismatch");
13978
13979 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13980 DstUndefElements.clear();
13981 DstUndefElements.resize(NumDstOps, false);
13982 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13983
13984 // Concatenate src elements constant bits together into dst element.
13985 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13986 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13987 for (unsigned I = 0; I != NumDstOps; ++I) {
13988 DstUndefElements.set(I);
13989 APInt &DstBits = DstBitElements[I];
13990 for (unsigned J = 0; J != Scale; ++J) {
13991 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13992 if (SrcUndefElements[Idx])
13993 continue;
13994 DstUndefElements.reset(I);
13995 const APInt &SrcBits = SrcBitElements[Idx];
13996 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13997 "Illegal constant bitwidths");
13998 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13999 }
14000 }
14001 return;
14002 }
14003
14004 // Split src element constant bits into dst elements.
14005 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14006 for (unsigned I = 0; I != NumSrcOps; ++I) {
14007 if (SrcUndefElements[I]) {
14008 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14009 continue;
14010 }
14011 const APInt &SrcBits = SrcBitElements[I];
14012 for (unsigned J = 0; J != Scale; ++J) {
14013 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14014 APInt &DstBits = DstBitElements[Idx];
14015 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14016 }
14017 }
14018}
14019
14021 for (const SDValue &Op : op_values()) {
14022 unsigned Opc = Op.getOpcode();
14023 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14024 return false;
14025 }
14026 return true;
14027}
14028
14029std::optional<std::pair<APInt, APInt>>
14031 unsigned NumOps = getNumOperands();
14032 if (NumOps < 2)
14033 return std::nullopt;
14034
14037 return std::nullopt;
14038
14039 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14040 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14041 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14042
14043 if (Stride.isZero())
14044 return std::nullopt;
14045
14046 for (unsigned i = 2; i < NumOps; ++i) {
14048 return std::nullopt;
14049
14050 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14051 if (Val != (Start + (Stride * i)))
14052 return std::nullopt;
14053 }
14054
14055 return std::make_pair(Start, Stride);
14056}
14057
14059 // Find the first non-undef value in the shuffle mask.
14060 unsigned i, e;
14061 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14062 /* search */;
14063
14064 // If all elements are undefined, this shuffle can be considered a splat
14065 // (although it should eventually get simplified away completely).
14066 if (i == e)
14067 return true;
14068
14069 // Make sure all remaining elements are either undef or the same as the first
14070 // non-undef value.
14071 for (int Idx = Mask[i]; i != e; ++i)
14072 if (Mask[i] >= 0 && Mask[i] != Idx)
14073 return false;
14074 return true;
14075}
14076
14077// Returns true if it is a constant integer BuildVector or constant integer,
14078// possibly hidden by a bitcast.
14080 SDValue N, bool AllowOpaques) const {
14082
14083 if (auto *C = dyn_cast<ConstantSDNode>(N))
14084 return AllowOpaques || !C->isOpaque();
14085
14087 return true;
14088
14089 // Treat a GlobalAddress supporting constant offset folding as a
14090 // constant integer.
14091 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14092 if (GA->getOpcode() == ISD::GlobalAddress &&
14093 TLI->isOffsetFoldingLegal(GA))
14094 return true;
14095
14096 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14097 isa<ConstantSDNode>(N.getOperand(0)))
14098 return true;
14099 return false;
14100}
14101
14102// Returns true if it is a constant float BuildVector or constant float.
14105 return true;
14106
14108 return true;
14109
14110 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14111 isa<ConstantFPSDNode>(N.getOperand(0)))
14112 return true;
14113
14114 return false;
14115}
14116
14117std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14118 ConstantSDNode *Const =
14119 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14120 if (!Const)
14121 return std::nullopt;
14122
14123 EVT VT = N->getValueType(0);
14124 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14125 switch (TLI->getBooleanContents(N.getValueType())) {
14127 if (CVal.isOne())
14128 return true;
14129 if (CVal.isZero())
14130 return false;
14131 return std::nullopt;
14133 if (CVal.isAllOnes())
14134 return true;
14135 if (CVal.isZero())
14136 return false;
14137 return std::nullopt;
14139 return CVal[0];
14140 }
14141 llvm_unreachable("Unknown BooleanContent enum");
14142}
14143
14144void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14145 assert(!Node->OperandList && "Node already has operands");
14147 "too many operands to fit into SDNode");
14148 SDUse *Ops = OperandRecycler.allocate(
14149 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14150
14151 bool IsDivergent = false;
14152 for (unsigned I = 0; I != Vals.size(); ++I) {
14153 Ops[I].setUser(Node);
14154 Ops[I].setInitial(Vals[I]);
14155 EVT VT = Ops[I].getValueType();
14156
14157 // Skip Chain. It does not carry divergence.
14158 if (VT != MVT::Other &&
14159 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14160 Ops[I].getNode()->isDivergent()) {
14161 IsDivergent = true;
14162 }
14163 }
14164 Node->NumOperands = Vals.size();
14165 Node->OperandList = Ops;
14166 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14167 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14168 Node->SDNodeBits.IsDivergent = IsDivergent;
14169 }
14170 checkForCycles(Node);
14171}
14172
14175 size_t Limit = SDNode::getMaxNumOperands();
14176 while (Vals.size() > Limit) {
14177 unsigned SliceIdx = Vals.size() - Limit;
14178 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14179 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14180 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14181 Vals.emplace_back(NewTF);
14182 }
14183 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14184}
14185
14187 EVT VT, SDNodeFlags Flags) {
14188 switch (Opcode) {
14189 default:
14190 return SDValue();
14191 case ISD::ADD:
14192 case ISD::OR:
14193 case ISD::XOR:
14194 case ISD::UMAX:
14195 return getConstant(0, DL, VT);
14196 case ISD::MUL:
14197 return getConstant(1, DL, VT);
14198 case ISD::AND:
14199 case ISD::UMIN:
14200 return getAllOnesConstant(DL, VT);
14201 case ISD::SMAX:
14203 case ISD::SMIN:
14205 case ISD::FADD:
14206 // If flags allow, prefer positive zero since it's generally cheaper
14207 // to materialize on most targets.
14208 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14209 case ISD::FMUL:
14210 return getConstantFP(1.0, DL, VT);
14211 case ISD::FMINNUM:
14212 case ISD::FMAXNUM: {
14213 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14214 const fltSemantics &Semantics = VT.getFltSemantics();
14215 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14216 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14217 APFloat::getLargest(Semantics);
14218 if (Opcode == ISD::FMAXNUM)
14219 NeutralAF.changeSign();
14220
14221 return getConstantFP(NeutralAF, DL, VT);
14222 }
14223 case ISD::FMINIMUM:
14224 case ISD::FMAXIMUM: {
14225 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14226 const fltSemantics &Semantics = VT.getFltSemantics();
14227 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14228 : APFloat::getLargest(Semantics);
14229 if (Opcode == ISD::FMAXIMUM)
14230 NeutralAF.changeSign();
14231
14232 return getConstantFP(NeutralAF, DL, VT);
14233 }
14234
14235 }
14236}
14237
14238/// Helper used to make a call to a library function that has one argument of
14239/// pointer type.
14240///
14241/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14242/// used to get or set floating-point state. They have one argument of pointer
14243/// type, which points to the memory region containing bits of the
14244/// floating-point state. The value returned by such function is ignored in the
14245/// created call.
14246///
14247/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14248/// \param Ptr Pointer used to save/load state.
14249/// \param InChain Ingoing token chain.
14250/// \returns Outgoing chain token.
14252 SDValue InChain,
14253 const SDLoc &DLoc) {
14254 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14256 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14257 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
14258 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
14259 TLI->getPointerTy(getDataLayout()));
14261 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14262 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
14263 std::move(Args));
14264 return TLI->LowerCallTo(CLI).second;
14265}
14266
14268 assert(From && To && "Invalid SDNode; empty source SDValue?");
14269 auto I = SDEI.find(From);
14270 if (I == SDEI.end())
14271 return;
14272
14273 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14274 // the iterator, hence the need to make a copy to prevent a use-after-free.
14275 NodeExtraInfo NEI = I->second;
14276 if (LLVM_LIKELY(!NEI.PCSections)) {
14277 // No deep copy required for the types of extra info set.
14278 //
14279 // FIXME: Investigate if other types of extra info also need deep copy. This
14280 // depends on the types of nodes they can be attached to: if some extra info
14281 // is only ever attached to nodes where a replacement To node is always the
14282 // node where later use and propagation of the extra info has the intended
14283 // semantics, no deep copy is required.
14284 SDEI[To] = std::move(NEI);
14285 return;
14286 }
14287
14288 const SDNode *EntrySDN = getEntryNode().getNode();
14289
14290 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14291 // through the replacement of From with To. Otherwise, replacements of a node
14292 // (From) with more complex nodes (To and its operands) may result in lost
14293 // extra info where the root node (To) is insignificant in further propagating
14294 // and using extra info when further lowering to MIR.
14295 //
14296 // In the first step pre-populate the visited set with the nodes reachable
14297 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14298 // DAG that is not new and should be left untouched.
14299 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14300 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14301 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14302 if (MaxDepth == 0) {
14303 // Remember this node in case we need to increase MaxDepth and continue
14304 // populating FromReach from this node.
14305 Leafs.emplace_back(N);
14306 return;
14307 }
14308 if (!FromReach.insert(N).second)
14309 return;
14310 for (const SDValue &Op : N->op_values())
14311 Self(Self, Op.getNode(), MaxDepth - 1);
14312 };
14313
14314 // Copy extra info to To and all its transitive operands (that are new).
14316 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14317 if (FromReach.contains(N))
14318 return true;
14319 if (!Visited.insert(N).second)
14320 return true;
14321 if (EntrySDN == N)
14322 return false;
14323 for (const SDValue &Op : N->op_values()) {
14324 if (N == To && Op.getNode() == EntrySDN) {
14325 // Special case: New node's operand is the entry node; just need to
14326 // copy extra info to new node.
14327 break;
14328 }
14329 if (!Self(Self, Op.getNode()))
14330 return false;
14331 }
14332 // Copy only if entry node was not reached.
14333 SDEI[N] = NEI;
14334 return true;
14335 };
14336
14337 // We first try with a lower MaxDepth, assuming that the path to common
14338 // operands between From and To is relatively short. This significantly
14339 // improves performance in the common case. The initial MaxDepth is big
14340 // enough to avoid retry in the common case; the last MaxDepth is large
14341 // enough to avoid having to use the fallback below (and protects from
14342 // potential stack exhaustion from recursion).
14343 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14344 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14345 // StartFrom is the previous (or initial) set of leafs reachable at the
14346 // previous maximum depth.
14348 std::swap(StartFrom, Leafs);
14349 for (const SDNode *N : StartFrom)
14350 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14351 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14352 return;
14353 // This should happen very rarely (reached the entry node).
14354 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14355 assert(!Leafs.empty());
14356 }
14357
14358 // This should not happen - but if it did, that means the subgraph reachable
14359 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14360 // could not visit all reachable common operands. Consequently, we were able
14361 // to reach the entry node.
14362 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14363 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14364 // Best-effort fallback if assertions disabled.
14365 SDEI[To] = std::move(NEI);
14366}
14367
14368#ifndef NDEBUG
14369static void checkForCyclesHelper(const SDNode *N,
14372 const llvm::SelectionDAG *DAG) {
14373 // If this node has already been checked, don't check it again.
14374 if (Checked.count(N))
14375 return;
14376
14377 // If a node has already been visited on this depth-first walk, reject it as
14378 // a cycle.
14379 if (!Visited.insert(N).second) {
14380 errs() << "Detected cycle in SelectionDAG\n";
14381 dbgs() << "Offending node:\n";
14382 N->dumprFull(DAG); dbgs() << "\n";
14383 abort();
14384 }
14385
14386 for (const SDValue &Op : N->op_values())
14387 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14388
14389 Checked.insert(N);
14390 Visited.erase(N);
14391}
14392#endif
14393
14395 const llvm::SelectionDAG *DAG,
14396 bool force) {
14397#ifndef NDEBUG
14398 bool check = force;
14399#ifdef EXPENSIVE_CHECKS
14400 check = true;
14401#endif // EXPENSIVE_CHECKS
14402 if (check) {
14403 assert(N && "Checking nonexistent SDNode");
14406 checkForCyclesHelper(N, visited, checked, DAG);
14407 }
14408#endif // !NDEBUG
14409}
14410
14411void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14412 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14413}
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
Function Alias Analysis Results
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.
#define _
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:55
#define I(x, y, z)
Definition MD5.cpp:58
#define G(x, y, z)
Definition MD5.cpp:56
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 unsigned 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 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 bool canFoldStoreIntoLibCallOutputPointers(StoreSDNode *StoreNode, SDNode *FPNode)
Given a store node StoreNode, return true if it is safe to fold that node into FPNode,...
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
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:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:143
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:138
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:899
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:277
const APFloat & getValue() const
Definition Constants.h:321
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:157
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
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:63
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:207
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:124
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:330
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.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:230
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:299
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 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 getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
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 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 getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
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 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.
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 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 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)
const TargetLibraryInfo & getLibInfo() const
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.
LLVM_ABI bool expandMultipleResultFPLibCall(RTLIB::Libcall LC, SDNode *Node, SmallVectorImpl< SDValue > &Results, std::optional< unsigned > CallRetResNo={})
Expands a node with multiple results to an FP or vector libcall.
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 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)
size_type size() const
Definition SmallPtrSet.h:99
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.
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.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
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:613
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:344
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:297
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:295
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:198
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
A Use represents the edge between a Value definition and its users.
Definition Use.h: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
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
Provides info so a possible vectorization of a function can be computed.
bool isMasked() const
StringRef getVectorFnName() const
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:270
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:231
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
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:177
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:238
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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
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
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:807
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:780
@ TargetConstantPool
Definition ISDOpcodes.h:184
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:504
@ 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:231
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition ISDOpcodes.h:531
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:270
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:593
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:771
@ TargetBlockAddress
Definition ISDOpcodes.h:186
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:289
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:515
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:868
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:577
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:744
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:898
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:521
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:832
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:712
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:662
@ TargetExternalSymbol
Definition ISDOpcodes.h:185
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:779
@ TargetJumpTable
Definition ISDOpcodes.h:183
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:193
@ 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:815
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:688
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:534
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:784
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:228
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:669
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ 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:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:180
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:701
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:762
@ 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:642
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:607
@ 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:569
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:219
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:838
@ TargetConstantFP
Definition ISDOpcodes.h:175
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:799
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ TargetFrameIndex
Definition ISDOpcodes.h:182
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:887
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:876
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:724
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:793
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:323
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:914
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:174
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:498
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:736
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:200
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:732
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:707
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:299
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:678
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:236
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:558
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition ISDOpcodes.h:654
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:947
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:696
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:909
@ 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:933
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:821
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:527
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:719
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:333
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:181
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
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 isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
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.
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:477
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:241
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:1745
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:1725
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:1606
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:2472
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:2136
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
void * PointerTy
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:1588
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:1732
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:1622
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:1739
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:543
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:1835
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:723
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:1897
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 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
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#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
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:301
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:255
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:124
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:242
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:274
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:119
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:161
KnownBits byteSwap() const
Definition KnownBits.h:514
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:518
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:233
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:172
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:321
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:225
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:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
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:196
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
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:326
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:280
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:219
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:167
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)