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;
282 if (!isa<ConstantSDNode>(Op))
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;
295 if (!isa<ConstantFPSDNode>(Op))
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;
325 if (!isa<ConstantSDNode>(Op))
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.
380template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
381 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
382template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
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");
443 case ISD::VP_REDUCE_FADD:
444 case ISD::VP_REDUCE_SEQ_FADD:
445 return ISD::FADD;
448 case ISD::VP_REDUCE_FMUL:
449 case ISD::VP_REDUCE_SEQ_FMUL:
450 return ISD::FMUL;
452 case ISD::VP_REDUCE_ADD:
453 return ISD::ADD;
455 case ISD::VP_REDUCE_MUL:
456 return ISD::MUL;
458 case ISD::VP_REDUCE_AND:
459 return ISD::AND;
461 case ISD::VP_REDUCE_OR:
462 return ISD::OR;
464 case ISD::VP_REDUCE_XOR:
465 return ISD::XOR;
467 case ISD::VP_REDUCE_SMAX:
468 return ISD::SMAX;
470 case ISD::VP_REDUCE_SMIN:
471 return ISD::SMIN;
473 case ISD::VP_REDUCE_UMAX:
474 return ISD::UMAX;
476 case ISD::VP_REDUCE_UMIN:
477 return ISD::UMIN;
479 case ISD::VP_REDUCE_FMAX:
480 return ISD::FMAXNUM;
482 case ISD::VP_REDUCE_FMIN:
483 return ISD::FMINNUM;
485 case ISD::VP_REDUCE_FMAXIMUM:
486 return ISD::FMAXIMUM;
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
629 return getSetCCInverseImpl(Op, Type.isInteger());
630}
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.
719 ArrayRef<SDValue> Ops) {
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.
728 ArrayRef<SDUse> Ops) {
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: {
752 const ConstantSDNode *C = cast<ConstantSDNode>(N);
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;
765 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
766 ID.AddPointer(GA->getGlobal());
767 ID.AddInteger(GA->getOffset());
768 ID.AddInteger(GA->getTargetFlags());
769 break;
770 }
771 case ISD::BasicBlock:
772 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
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;
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;
799 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
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: {
810 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
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: {
857 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
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: {
864 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
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: {
871 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
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: {
879 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
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: {
887 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
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: {
895 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
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: {
903 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
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: {
911 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
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 }
920 case ISD::ATOMIC_SWAP:
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: {
948 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(N);
949 ID.AddInteger(ASC->getSrcAddressSpace());
950 ID.AddInteger(ASC->getDestAddressSpace());
951 break;
952 }
954 case ISD::BlockAddress: {
955 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
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;
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
1119void SDDbgInfo::erase(const SDNode *Node) {
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.
1130 removeOperands(N);
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 };
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 };
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
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
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() ||
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.
2138 std::swap(N1, N2);
2140}
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};
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
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();
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
2819 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2820 const APInt &C2 = N2C->getAPIntValue();
2821 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
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.Zero.setAllBits(); Known.One.setAllBits();
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_SHUFFLE: {
3484 assert(!Op.getValueType().isScalableVector());
3485 // Collect the known bits that are shared by every vector element referenced
3486 // by the shuffle.
3487 APInt DemandedLHS, DemandedRHS;
3488 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3489 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3490 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3491 DemandedLHS, DemandedRHS))
3492 break;
3493
3494 // Known bits are the values that are shared by every demanded element.
3495 Known.Zero.setAllBits(); Known.One.setAllBits();
3496 if (!!DemandedLHS) {
3497 SDValue LHS = Op.getOperand(0);
3498 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3499 Known = Known.intersectWith(Known2);
3500 }
3501 // If we don't know any bits, early out.
3502 if (Known.isUnknown())
3503 break;
3504 if (!!DemandedRHS) {
3505 SDValue RHS = Op.getOperand(1);
3506 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3507 Known = Known.intersectWith(Known2);
3508 }
3509 break;
3510 }
3511 case ISD::VSCALE: {
3513 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3514 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3515 break;
3516 }
3517 case ISD::CONCAT_VECTORS: {
3518 if (Op.getValueType().isScalableVector())
3519 break;
3520 // Split DemandedElts and test each of the demanded subvectors.
3521 Known.Zero.setAllBits(); Known.One.setAllBits();
3522 EVT SubVectorVT = Op.getOperand(0).getValueType();
3523 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3524 unsigned NumSubVectors = Op.getNumOperands();
3525 for (unsigned i = 0; i != NumSubVectors; ++i) {
3526 APInt DemandedSub =
3527 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3528 if (!!DemandedSub) {
3529 SDValue Sub = Op.getOperand(i);
3530 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3531 Known = Known.intersectWith(Known2);
3532 }
3533 // If we don't know any bits, early out.
3534 if (Known.isUnknown())
3535 break;
3536 }
3537 break;
3538 }
3539 case ISD::INSERT_SUBVECTOR: {
3540 if (Op.getValueType().isScalableVector())
3541 break;
3542 // Demand any elements from the subvector and the remainder from the src its
3543 // inserted into.
3544 SDValue Src = Op.getOperand(0);
3545 SDValue Sub = Op.getOperand(1);
3546 uint64_t Idx = Op.getConstantOperandVal(2);
3547 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3548 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3549 APInt DemandedSrcElts = DemandedElts;
3550 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3551
3552 Known.One.setAllBits();
3553 Known.Zero.setAllBits();
3554 if (!!DemandedSubElts) {
3555 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3556 if (Known.isUnknown())
3557 break; // early-out.
3558 }
3559 if (!!DemandedSrcElts) {
3560 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3561 Known = Known.intersectWith(Known2);
3562 }
3563 break;
3564 }
3566 // Offset the demanded elts by the subvector index.
3567 SDValue Src = Op.getOperand(0);
3568 // Bail until we can represent demanded elements for scalable vectors.
3569 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3570 break;
3571 uint64_t Idx = Op.getConstantOperandVal(1);
3572 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3573 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3574 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3575 break;
3576 }
3577 case ISD::SCALAR_TO_VECTOR: {
3578 if (Op.getValueType().isScalableVector())
3579 break;
3580 // We know about scalar_to_vector as much as we know about it source,
3581 // which becomes the first element of otherwise unknown vector.
3582 if (DemandedElts != 1)
3583 break;
3584
3585 SDValue N0 = Op.getOperand(0);
3586 Known = computeKnownBits(N0, Depth + 1);
3587 if (N0.getValueSizeInBits() != BitWidth)
3588 Known = Known.trunc(BitWidth);
3589
3590 break;
3591 }
3592 case ISD::BITCAST: {
3593 if (Op.getValueType().isScalableVector())
3594 break;
3595
3596 SDValue N0 = Op.getOperand(0);
3597 EVT SubVT = N0.getValueType();
3598 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3599
3600 // Ignore bitcasts from unsupported types.
3601 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3602 break;
3603
3604 // Fast handling of 'identity' bitcasts.
3605 if (BitWidth == SubBitWidth) {
3606 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3607 break;
3608 }
3609
3610 bool IsLE = getDataLayout().isLittleEndian();
3611
3612 // Bitcast 'small element' vector to 'large element' scalar/vector.
3613 if ((BitWidth % SubBitWidth) == 0) {
3614 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3615
3616 // Collect known bits for the (larger) output by collecting the known
3617 // bits from each set of sub elements and shift these into place.
3618 // We need to separately call computeKnownBits for each set of
3619 // sub elements as the knownbits for each is likely to be different.
3620 unsigned SubScale = BitWidth / SubBitWidth;
3621 APInt SubDemandedElts(NumElts * SubScale, 0);
3622 for (unsigned i = 0; i != NumElts; ++i)
3623 if (DemandedElts[i])
3624 SubDemandedElts.setBit(i * SubScale);
3625
3626 for (unsigned i = 0; i != SubScale; ++i) {
3627 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3628 Depth + 1);
3629 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3630 Known.insertBits(Known2, SubBitWidth * Shifts);
3631 }
3632 }
3633
3634 // Bitcast 'large element' scalar/vector to 'small element' vector.
3635 if ((SubBitWidth % BitWidth) == 0) {
3636 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3637
3638 // Collect known bits for the (smaller) output by collecting the known
3639 // bits from the overlapping larger input elements and extracting the
3640 // sub sections we actually care about.
3641 unsigned SubScale = SubBitWidth / BitWidth;
3642 APInt SubDemandedElts =
3643 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3644 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3645
3646 Known.Zero.setAllBits(); Known.One.setAllBits();
3647 for (unsigned i = 0; i != NumElts; ++i)
3648 if (DemandedElts[i]) {
3649 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3650 unsigned Offset = (Shifts % SubScale) * BitWidth;
3651 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3652 // If we don't know any bits, early out.
3653 if (Known.isUnknown())
3654 break;
3655 }
3656 }
3657 break;
3658 }
3659 case ISD::AND:
3660 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3661 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3662
3663 Known &= Known2;
3664 break;
3665 case ISD::OR:
3666 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3667 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3668
3669 Known |= Known2;
3670 break;
3671 case ISD::XOR:
3672 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3673 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3674
3675 Known ^= Known2;
3676 break;
3677 case ISD::MUL: {
3678 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3679 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3680 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3681 // TODO: SelfMultiply can be poison, but not undef.
3682 if (SelfMultiply)
3683 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3684 Op.getOperand(0), DemandedElts, false, Depth + 1);
3685 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3686
3687 // If the multiplication is known not to overflow, the product of a number
3688 // with itself is non-negative. Only do this if we didn't already computed
3689 // the opposite value for the sign bit.
3690 if (Op->getFlags().hasNoSignedWrap() &&
3691 Op.getOperand(0) == Op.getOperand(1) &&
3692 !Known.isNegative())
3693 Known.makeNonNegative();
3694 break;
3695 }
3696 case ISD::MULHU: {
3697 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3698 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3699 Known = KnownBits::mulhu(Known, Known2);
3700 break;
3701 }
3702 case ISD::MULHS: {
3703 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3704 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3705 Known = KnownBits::mulhs(Known, Known2);
3706 break;
3707 }
3708 case ISD::ABDU: {
3709 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3710 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3711 Known = KnownBits::abdu(Known, Known2);
3712 break;
3713 }
3714 case ISD::ABDS: {
3715 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3716 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3717 Known = KnownBits::abds(Known, Known2);
3718 unsigned SignBits1 =
3719 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3720 if (SignBits1 == 1)
3721 break;
3722 unsigned SignBits0 =
3723 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3724 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3725 break;
3726 }
3727 case ISD::UMUL_LOHI: {
3728 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3729 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3730 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3731 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3732 if (Op.getResNo() == 0)
3733 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3734 else
3735 Known = KnownBits::mulhu(Known, Known2);
3736 break;
3737 }
3738 case ISD::SMUL_LOHI: {
3739 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3740 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3741 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3742 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3743 if (Op.getResNo() == 0)
3744 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3745 else
3746 Known = KnownBits::mulhs(Known, Known2);
3747 break;
3748 }
3749 case ISD::AVGFLOORU: {
3750 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3751 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3752 Known = KnownBits::avgFloorU(Known, Known2);
3753 break;
3754 }
3755 case ISD::AVGCEILU: {
3756 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3757 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3758 Known = KnownBits::avgCeilU(Known, Known2);
3759 break;
3760 }
3761 case ISD::AVGFLOORS: {
3762 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3763 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3764 Known = KnownBits::avgFloorS(Known, Known2);
3765 break;
3766 }
3767 case ISD::AVGCEILS: {
3768 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3769 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3770 Known = KnownBits::avgCeilS(Known, Known2);
3771 break;
3772 }
3773 case ISD::SELECT:
3774 case ISD::VSELECT:
3775 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3776 // If we don't know any bits, early out.
3777 if (Known.isUnknown())
3778 break;
3779 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3780
3781 // Only known if known in both the LHS and RHS.
3782 Known = Known.intersectWith(Known2);
3783 break;
3784 case ISD::SELECT_CC:
3785 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3786 // If we don't know any bits, early out.
3787 if (Known.isUnknown())
3788 break;
3789 Known2 = computeKnownBits(Op.getOperand(2), 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::SMULO:
3795 case ISD::UMULO:
3796 if (Op.getResNo() != 1)
3797 break;
3798 // The boolean result conforms to getBooleanContents.
3799 // If we know the result of a setcc has the top bits zero, use this info.
3800 // We know that we have an integer-based boolean since these operations
3801 // are only available for integer.
3802 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3804 BitWidth > 1)
3805 Known.Zero.setBitsFrom(1);
3806 break;
3807 case ISD::SETCC:
3808 case ISD::SETCCCARRY:
3809 case ISD::STRICT_FSETCC:
3810 case ISD::STRICT_FSETCCS: {
3811 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3812 // If we know the result of a setcc has the top bits zero, use this info.
3813 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3815 BitWidth > 1)
3816 Known.Zero.setBitsFrom(1);
3817 break;
3818 }
3819 case ISD::SHL: {
3820 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3821 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3822
3823 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3824 bool NSW = Op->getFlags().hasNoSignedWrap();
3825
3826 bool ShAmtNonZero = Known2.isNonZero();
3827
3828 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3829
3830 // Minimum shift low bits are known zero.
3831 if (std::optional<unsigned> ShMinAmt =
3832 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3833 Known.Zero.setLowBits(*ShMinAmt);
3834 break;
3835 }
3836 case ISD::SRL:
3837 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3838 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3839 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3840 Op->getFlags().hasExact());
3841
3842 // Minimum shift high bits are known zero.
3843 if (std::optional<unsigned> ShMinAmt =
3844 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3845 Known.Zero.setHighBits(*ShMinAmt);
3846 break;
3847 case ISD::SRA:
3848 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3849 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3850 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3851 Op->getFlags().hasExact());
3852 break;
3853 case ISD::ROTL:
3854 case ISD::ROTR:
3855 if (ConstantSDNode *C =
3856 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3857 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3858
3859 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3860
3861 // Canonicalize to ROTR.
3862 if (Opcode == ISD::ROTL && Amt != 0)
3863 Amt = BitWidth - Amt;
3864
3865 Known.Zero = Known.Zero.rotr(Amt);
3866 Known.One = Known.One.rotr(Amt);
3867 }
3868 break;
3869 case ISD::FSHL:
3870 case ISD::FSHR:
3871 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3872 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3873
3874 // For fshl, 0-shift returns the 1st arg.
3875 // For fshr, 0-shift returns the 2nd arg.
3876 if (Amt == 0) {
3877 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3878 DemandedElts, Depth + 1);
3879 break;
3880 }
3881
3882 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3883 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3884 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3885 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3886 if (Opcode == ISD::FSHL) {
3887 Known <<= Amt;
3888 Known2 >>= BitWidth - Amt;
3889 } else {
3890 Known <<= BitWidth - Amt;
3891 Known2 >>= Amt;
3892 }
3893 Known = Known.unionWith(Known2);
3894 }
3895 break;
3896 case ISD::SHL_PARTS:
3897 case ISD::SRA_PARTS:
3898 case ISD::SRL_PARTS: {
3899 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3900
3901 // Collect lo/hi source values and concatenate.
3902 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3903 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3904 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3905 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3906 Known = Known2.concat(Known);
3907
3908 // Collect shift amount.
3909 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3910
3911 if (Opcode == ISD::SHL_PARTS)
3912 Known = KnownBits::shl(Known, Known2);
3913 else if (Opcode == ISD::SRA_PARTS)
3914 Known = KnownBits::ashr(Known, Known2);
3915 else // if (Opcode == ISD::SRL_PARTS)
3916 Known = KnownBits::lshr(Known, Known2);
3917
3918 // TODO: Minimum shift low/high bits are known zero.
3919
3920 if (Op.getResNo() == 0)
3921 Known = Known.extractBits(LoBits, 0);
3922 else
3923 Known = Known.extractBits(HiBits, LoBits);
3924 break;
3925 }
3927 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3928 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3929 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3930 break;
3931 }
3932 case ISD::CTTZ:
3933 case ISD::CTTZ_ZERO_UNDEF: {
3934 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3935 // If we have a known 1, its position is our upper bound.
3936 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3937 unsigned LowBits = llvm::bit_width(PossibleTZ);
3938 Known.Zero.setBitsFrom(LowBits);
3939 break;
3940 }
3941 case ISD::CTLZ:
3942 case ISD::CTLZ_ZERO_UNDEF: {
3943 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3944 // If we have a known 1, its position is our upper bound.
3945 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3946 unsigned LowBits = llvm::bit_width(PossibleLZ);
3947 Known.Zero.setBitsFrom(LowBits);
3948 break;
3949 }
3950 case ISD::CTPOP: {
3951 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3952 // If we know some of the bits are zero, they can't be one.
3953 unsigned PossibleOnes = Known2.countMaxPopulation();
3954 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3955 break;
3956 }
3957 case ISD::PARITY: {
3958 // Parity returns 0 everywhere but the LSB.
3959 Known.Zero.setBitsFrom(1);
3960 break;
3961 }
3962 case ISD::MGATHER:
3963 case ISD::MLOAD: {
3964 ISD::LoadExtType ETy =
3965 (Opcode == ISD::MGATHER)
3966 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3967 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3968 if (ETy == ISD::ZEXTLOAD) {
3969 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3970 KnownBits Known0(MemVT.getScalarSizeInBits());
3971 return Known0.zext(BitWidth);
3972 }
3973 break;
3974 }
3975 case ISD::LOAD: {
3976 LoadSDNode *LD = cast<LoadSDNode>(Op);
3977 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3978 if (ISD::isNON_EXTLoad(LD) && Cst) {
3979 // Determine any common known bits from the loaded constant pool value.
3980 Type *CstTy = Cst->getType();
3981 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3982 !Op.getValueType().isScalableVector()) {
3983 // If its a vector splat, then we can (quickly) reuse the scalar path.
3984 // NOTE: We assume all elements match and none are UNDEF.
3985 if (CstTy->isVectorTy()) {
3986 if (const Constant *Splat = Cst->getSplatValue()) {
3987 Cst = Splat;
3988 CstTy = Cst->getType();
3989 }
3990 }
3991 // TODO - do we need to handle different bitwidths?
3992 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3993 // Iterate across all vector elements finding common known bits.
3994 Known.One.setAllBits();
3995 Known.Zero.setAllBits();
3996 for (unsigned i = 0; i != NumElts; ++i) {
3997 if (!DemandedElts[i])
3998 continue;
3999 if (Constant *Elt = Cst->getAggregateElement(i)) {
4000 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4001 const APInt &Value = CInt->getValue();
4002 Known.One &= Value;
4003 Known.Zero &= ~Value;
4004 continue;
4005 }
4006 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4007 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4008 Known.One &= Value;
4009 Known.Zero &= ~Value;
4010 continue;
4011 }
4012 }
4013 Known.One.clearAllBits();
4014 Known.Zero.clearAllBits();
4015 break;
4016 }
4017 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4018 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4019 Known = KnownBits::makeConstant(CInt->getValue());
4020 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4021 Known =
4022 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4023 }
4024 }
4025 }
4026 } else if (Op.getResNo() == 0) {
4027 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4028 KnownBits KnownScalarMemory(ScalarMemorySize);
4029 if (const MDNode *MD = LD->getRanges())
4030 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4031
4032 // Extend the Known bits from memory to the size of the scalar result.
4033 if (ISD::isZEXTLoad(Op.getNode()))
4034 Known = KnownScalarMemory.zext(BitWidth);
4035 else if (ISD::isSEXTLoad(Op.getNode()))
4036 Known = KnownScalarMemory.sext(BitWidth);
4037 else if (ISD::isEXTLoad(Op.getNode()))
4038 Known = KnownScalarMemory.anyext(BitWidth);
4039 else
4040 Known = KnownScalarMemory;
4041 assert(Known.getBitWidth() == BitWidth);
4042 return Known;
4043 }
4044 break;
4045 }
4047 if (Op.getValueType().isScalableVector())
4048 break;
4049 EVT InVT = Op.getOperand(0).getValueType();
4050 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4051 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4052 Known = Known.zext(BitWidth);
4053 break;
4054 }
4055 case ISD::ZERO_EXTEND: {
4056 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4057 Known = Known.zext(BitWidth);
4058 break;
4059 }
4061 if (Op.getValueType().isScalableVector())
4062 break;
4063 EVT InVT = Op.getOperand(0).getValueType();
4064 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4065 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4066 // If the sign bit is known to be zero or one, then sext will extend
4067 // it to the top bits, else it will just zext.
4068 Known = Known.sext(BitWidth);
4069 break;
4070 }
4071 case ISD::SIGN_EXTEND: {
4072 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4073 // If the sign bit is known to be zero or one, then sext will extend
4074 // it to the top bits, else it will just zext.
4075 Known = Known.sext(BitWidth);
4076 break;
4077 }
4079 if (Op.getValueType().isScalableVector())
4080 break;
4081 EVT InVT = Op.getOperand(0).getValueType();
4082 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4083 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4084 Known = Known.anyext(BitWidth);
4085 break;
4086 }
4087 case ISD::ANY_EXTEND: {
4088 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4089 Known = Known.anyext(BitWidth);
4090 break;
4091 }
4092 case ISD::TRUNCATE: {
4093 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4094 Known = Known.trunc(BitWidth);
4095 break;
4096 }
4097 case ISD::AssertZext: {
4098 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4100 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4101 Known.Zero |= (~InMask);
4102 Known.One &= (~Known.Zero);
4103 break;
4104 }
4105 case ISD::AssertAlign: {
4106 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4107 assert(LogOfAlign != 0);
4108
4109 // TODO: Should use maximum with source
4110 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4111 // well as clearing one bits.
4112 Known.Zero.setLowBits(LogOfAlign);
4113 Known.One.clearLowBits(LogOfAlign);
4114 break;
4115 }
4116 case ISD::FGETSIGN:
4117 // All bits are zero except the low bit.
4118 Known.Zero.setBitsFrom(1);
4119 break;
4120 case ISD::ADD:
4121 case ISD::SUB: {
4122 SDNodeFlags Flags = Op.getNode()->getFlags();
4123 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4124 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4126 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4127 Flags.hasNoUnsignedWrap(), Known, Known2);
4128 break;
4129 }
4130 case ISD::USUBO:
4131 case ISD::SSUBO:
4132 case ISD::USUBO_CARRY:
4133 case ISD::SSUBO_CARRY:
4134 if (Op.getResNo() == 1) {
4135 // If we know the result of a setcc has the top bits zero, use this info.
4136 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4138 BitWidth > 1)
4139 Known.Zero.setBitsFrom(1);
4140 break;
4141 }
4142 [[fallthrough]];
4143 case ISD::SUBC: {
4144 assert(Op.getResNo() == 0 &&
4145 "We only compute knownbits for the difference here.");
4146
4147 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4148 KnownBits Borrow(1);
4149 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4150 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4151 // Borrow has bit width 1
4152 Borrow = Borrow.trunc(1);
4153 } else {
4154 Borrow.setAllZero();
4155 }
4156
4157 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4158 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4159 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4160 break;
4161 }
4162 case ISD::UADDO:
4163 case ISD::SADDO:
4164 case ISD::UADDO_CARRY:
4165 case ISD::SADDO_CARRY:
4166 if (Op.getResNo() == 1) {
4167 // If we know the result of a setcc has the top bits zero, use this info.
4168 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4170 BitWidth > 1)
4171 Known.Zero.setBitsFrom(1);
4172 break;
4173 }
4174 [[fallthrough]];
4175 case ISD::ADDC:
4176 case ISD::ADDE: {
4177 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4178
4179 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4180 KnownBits Carry(1);
4181 if (Opcode == ISD::ADDE)
4182 // Can't track carry from glue, set carry to unknown.
4183 Carry.resetAll();
4184 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4185 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4186 // Carry has bit width 1
4187 Carry = Carry.trunc(1);
4188 } else {
4189 Carry.setAllZero();
4190 }
4191
4192 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4193 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4194 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4195 break;
4196 }
4197 case ISD::UDIV: {
4198 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4199 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4200 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4201 break;
4202 }
4203 case ISD::SDIV: {
4204 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4205 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4206 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4207 break;
4208 }
4209 case ISD::SREM: {
4210 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4211 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4212 Known = KnownBits::srem(Known, Known2);
4213 break;
4214 }
4215 case ISD::UREM: {
4216 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4217 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4218 Known = KnownBits::urem(Known, Known2);
4219 break;
4220 }
4221 case ISD::EXTRACT_ELEMENT: {
4222 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4223 const unsigned Index = Op.getConstantOperandVal(1);
4224 const unsigned EltBitWidth = Op.getValueSizeInBits();
4225
4226 // Remove low part of known bits mask
4227 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4228 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4229
4230 // Remove high part of known bit mask
4231 Known = Known.trunc(EltBitWidth);
4232 break;
4233 }
4235 SDValue InVec = Op.getOperand(0);
4236 SDValue EltNo = Op.getOperand(1);
4237 EVT VecVT = InVec.getValueType();
4238 // computeKnownBits not yet implemented for scalable vectors.
4239 if (VecVT.isScalableVector())
4240 break;
4241 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4242 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4243
4244 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4245 // anything about the extended bits.
4246 if (BitWidth > EltBitWidth)
4247 Known = Known.trunc(EltBitWidth);
4248
4249 // If we know the element index, just demand that vector element, else for
4250 // an unknown element index, ignore DemandedElts and demand them all.
4251 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4252 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4253 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4254 DemandedSrcElts =
4255 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4256
4257 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4258 if (BitWidth > EltBitWidth)
4259 Known = Known.anyext(BitWidth);
4260 break;
4261 }
4263 if (Op.getValueType().isScalableVector())
4264 break;
4265
4266 // If we know the element index, split the demand between the
4267 // source vector and the inserted element, otherwise assume we need
4268 // the original demanded vector elements and the value.
4269 SDValue InVec = Op.getOperand(0);
4270 SDValue InVal = Op.getOperand(1);
4271 SDValue EltNo = Op.getOperand(2);
4272 bool DemandedVal = true;
4273 APInt DemandedVecElts = DemandedElts;
4274 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4275 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4276 unsigned EltIdx = CEltNo->getZExtValue();
4277 DemandedVal = !!DemandedElts[EltIdx];
4278 DemandedVecElts.clearBit(EltIdx);
4279 }
4280 Known.One.setAllBits();
4281 Known.Zero.setAllBits();
4282 if (DemandedVal) {
4283 Known2 = computeKnownBits(InVal, Depth + 1);
4284 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4285 }
4286 if (!!DemandedVecElts) {
4287 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4288 Known = Known.intersectWith(Known2);
4289 }
4290 break;
4291 }
4292 case ISD::BITREVERSE: {
4293 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4294 Known = Known2.reverseBits();
4295 break;
4296 }
4297 case ISD::BSWAP: {
4298 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4299 Known = Known2.byteSwap();
4300 break;
4301 }
4302 case ISD::ABS: {
4303 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4304 Known = Known2.abs();
4305 Known.Zero.setHighBits(
4306 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4307 break;
4308 }
4309 case ISD::USUBSAT: {
4310 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4311 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4312 Known = KnownBits::usub_sat(Known, Known2);
4313 break;
4314 }
4315 case ISD::UMIN: {
4316 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4317 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4318 Known = KnownBits::umin(Known, Known2);
4319 break;
4320 }
4321 case ISD::UMAX: {
4322 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4323 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4324 Known = KnownBits::umax(Known, Known2);
4325 break;
4326 }
4327 case ISD::SMIN:
4328 case ISD::SMAX: {
4329 // If we have a clamp pattern, we know that the number of sign bits will be
4330 // the minimum of the clamp min/max range.
4331 bool IsMax = (Opcode == ISD::SMAX);
4332 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4333 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4334 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4335 CstHigh =
4336 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4337 if (CstLow && CstHigh) {
4338 if (!IsMax)
4339 std::swap(CstLow, CstHigh);
4340
4341 const APInt &ValueLow = CstLow->getAPIntValue();
4342 const APInt &ValueHigh = CstHigh->getAPIntValue();
4343 if (ValueLow.sle(ValueHigh)) {
4344 unsigned LowSignBits = ValueLow.getNumSignBits();
4345 unsigned HighSignBits = ValueHigh.getNumSignBits();
4346 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4347 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4348 Known.One.setHighBits(MinSignBits);
4349 break;
4350 }
4351 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4352 Known.Zero.setHighBits(MinSignBits);
4353 break;
4354 }
4355 }
4356 }
4357
4358 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4359 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4360 if (IsMax)
4361 Known = KnownBits::smax(Known, Known2);
4362 else
4363 Known = KnownBits::smin(Known, Known2);
4364
4365 // For SMAX, if CstLow is non-negative we know the result will be
4366 // non-negative and thus all sign bits are 0.
4367 // TODO: There's an equivalent of this for smin with negative constant for
4368 // known ones.
4369 if (IsMax && CstLow) {
4370 const APInt &ValueLow = CstLow->getAPIntValue();
4371 if (ValueLow.isNonNegative()) {
4372 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4373 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4374 }
4375 }
4376
4377 break;
4378 }
4379 case ISD::UINT_TO_FP: {
4380 Known.makeNonNegative();
4381 break;
4382 }
4383 case ISD::SINT_TO_FP: {
4384 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4385 if (Known2.isNonNegative())
4386 Known.makeNonNegative();
4387 else if (Known2.isNegative())
4388 Known.makeNegative();
4389 break;
4390 }
4391 case ISD::FP_TO_UINT_SAT: {
4392 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4393 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4395 break;
4396 }
4397 case ISD::ATOMIC_LOAD: {
4398 // If we are looking at the loaded value.
4399 if (Op.getResNo() == 0) {
4400 auto *AT = cast<AtomicSDNode>(Op);
4401 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4402 KnownBits KnownScalarMemory(ScalarMemorySize);
4403 if (const MDNode *MD = AT->getRanges())
4404 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4405
4406 switch (AT->getExtensionType()) {
4407 case ISD::ZEXTLOAD:
4408 Known = KnownScalarMemory.zext(BitWidth);
4409 break;
4410 case ISD::SEXTLOAD:
4411 Known = KnownScalarMemory.sext(BitWidth);
4412 break;
4413 case ISD::EXTLOAD:
4414 switch (TLI->getExtendForAtomicOps()) {
4415 case ISD::ZERO_EXTEND:
4416 Known = KnownScalarMemory.zext(BitWidth);
4417 break;
4418 case ISD::SIGN_EXTEND:
4419 Known = KnownScalarMemory.sext(BitWidth);
4420 break;
4421 default:
4422 Known = KnownScalarMemory.anyext(BitWidth);
4423 break;
4424 }
4425 break;
4426 case ISD::NON_EXTLOAD:
4427 Known = KnownScalarMemory;
4428 break;
4429 }
4430 assert(Known.getBitWidth() == BitWidth);
4431 }
4432 break;
4433 }
4435 if (Op.getResNo() == 1) {
4436 // The boolean result conforms to getBooleanContents.
4437 // If we know the result of a setcc has the top bits zero, use this info.
4438 // We know that we have an integer-based boolean since these operations
4439 // are only available for integer.
4440 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4442 BitWidth > 1)
4443 Known.Zero.setBitsFrom(1);
4444 break;
4445 }
4446 [[fallthrough]];
4448 case ISD::ATOMIC_SWAP:
4459 case ISD::ATOMIC_LOAD_UMAX: {
4460 // If we are looking at the loaded value.
4461 if (Op.getResNo() == 0) {
4462 auto *AT = cast<AtomicSDNode>(Op);
4463 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4464
4466 Known.Zero.setBitsFrom(MemBits);
4467 }
4468 break;
4469 }
4470 case ISD::FrameIndex:
4472 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4473 Known, getMachineFunction());
4474 break;
4475
4476 default:
4477 if (Opcode < ISD::BUILTIN_OP_END)
4478 break;
4479 [[fallthrough]];
4483 // TODO: Probably okay to remove after audit; here to reduce change size
4484 // in initial enablement patch for scalable vectors
4485 if (Op.getValueType().isScalableVector())
4486 break;
4487
4488 // Allow the target to implement this method for its nodes.
4489 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4490 break;
4491 }
4492
4493 return Known;
4494}
4495
4496/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4498 switch (OR) {
4506 }
4507 llvm_unreachable("Unknown OverflowResult");
4508}
4509
4512 // X + 0 never overflow
4513 if (isNullConstant(N1))
4514 return OFK_Never;
4515
4516 // If both operands each have at least two sign bits, the addition
4517 // cannot overflow.
4518 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4519 return OFK_Never;
4520
4521 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4522 return OFK_Sometime;
4523}
4524
4527 // X + 0 never overflow
4528 if (isNullConstant(N1))
4529 return OFK_Never;
4530
4531 // mulhi + 1 never overflow
4532 KnownBits N1Known = computeKnownBits(N1);
4533 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4534 N1Known.getMaxValue().ult(2))
4535 return OFK_Never;
4536
4537 KnownBits N0Known = computeKnownBits(N0);
4538 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4539 N0Known.getMaxValue().ult(2))
4540 return OFK_Never;
4541
4542 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4543 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4544 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4545 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4546}
4547
4550 // X - 0 never overflow
4551 if (isNullConstant(N1))
4552 return OFK_Never;
4553
4554 // If both operands each have at least two sign bits, the subtraction
4555 // cannot overflow.
4556 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4557 return OFK_Never;
4558
4559 KnownBits N0Known = computeKnownBits(N0);
4560 KnownBits N1Known = computeKnownBits(N1);
4561 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4562 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4563 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4564}
4565
4568 // X - 0 never overflow
4569 if (isNullConstant(N1))
4570 return OFK_Never;
4571
4572 KnownBits N0Known = computeKnownBits(N0);
4573 KnownBits N1Known = computeKnownBits(N1);
4574 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4575 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4576 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4577}
4578
4581 // X * 0 and X * 1 never overflow.
4582 if (isNullConstant(N1) || isOneConstant(N1))
4583 return OFK_Never;
4584
4585 KnownBits N0Known = computeKnownBits(N0);
4586 KnownBits N1Known = computeKnownBits(N1);
4587 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4588 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4589 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4590}
4591
4594 // X * 0 and X * 1 never overflow.
4595 if (isNullConstant(N1) || isOneConstant(N1))
4596 return OFK_Never;
4597
4598 // Get the size of the result.
4599 unsigned BitWidth = N0.getScalarValueSizeInBits();
4600
4601 // Sum of the sign bits.
4602 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4603
4604 // If we have enough sign bits, then there's no overflow.
4605 if (SignBits > BitWidth + 1)
4606 return OFK_Never;
4607
4608 if (SignBits == BitWidth + 1) {
4609 // The overflow occurs when the true multiplication of the
4610 // the operands is the minimum negative number.
4611 KnownBits N0Known = computeKnownBits(N0);
4612 KnownBits N1Known = computeKnownBits(N1);
4613 // If one of the operands is non-negative, then there's no
4614 // overflow.
4615 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4616 return OFK_Never;
4617 }
4618
4619 return OFK_Sometime;
4620}
4621
4623 if (Depth >= MaxRecursionDepth)
4624 return false; // Limit search depth.
4625
4626 EVT OpVT = Val.getValueType();
4627 unsigned BitWidth = OpVT.getScalarSizeInBits();
4628
4629 // Is the constant a known power of 2?
4631 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4632 }))
4633 return true;
4634
4635 // A left-shift of a constant one will have exactly one bit set because
4636 // shifting the bit off the end is undefined.
4637 if (Val.getOpcode() == ISD::SHL) {
4638 auto *C = isConstOrConstSplat(Val.getOperand(0));
4639 if (C && C->getAPIntValue() == 1)
4640 return true;
4641 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4642 isKnownNeverZero(Val, Depth);
4643 }
4644
4645 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4646 // one bit set.
4647 if (Val.getOpcode() == ISD::SRL) {
4648 auto *C = isConstOrConstSplat(Val.getOperand(0));
4649 if (C && C->getAPIntValue().isSignMask())
4650 return true;
4651 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4652 isKnownNeverZero(Val, Depth);
4653 }
4654
4655 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4656 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4657
4658 // Are all operands of a build vector constant powers of two?
4659 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4660 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4661 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4662 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4663 return false;
4664 }))
4665 return true;
4666
4667 // Is the operand of a splat vector a constant power of two?
4668 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4669 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4670 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4671 return true;
4672
4673 // vscale(power-of-two) is a power-of-two for some targets
4674 if (Val.getOpcode() == ISD::VSCALE &&
4675 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4677 return true;
4678
4679 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4680 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4681 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4683
4684 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4685 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4687
4688 // Looking for `x & -x` pattern:
4689 // If x == 0:
4690 // x & -x -> 0
4691 // If x != 0:
4692 // x & -x -> non-zero pow2
4693 // so if we find the pattern return whether we know `x` is non-zero.
4694 SDValue X;
4695 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4696 return isKnownNeverZero(X, Depth);
4697
4698 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4699 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4700
4701 // More could be done here, though the above checks are enough
4702 // to handle some common cases.
4703 return false;
4704}
4705
4707 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4708 return C1->getValueAPF().getExactLog2Abs() >= 0;
4709
4710 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4711 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4712
4713 return false;
4714}
4715
4717 EVT VT = Op.getValueType();
4718
4719 // Since the number of lanes in a scalable vector is unknown at compile time,
4720 // we track one bit which is implicitly broadcast to all lanes. This means
4721 // that all lanes in a scalable vector are considered demanded.
4722 APInt DemandedElts = VT.isFixedLengthVector()
4724 : APInt(1, 1);
4725 return ComputeNumSignBits(Op, DemandedElts, Depth);
4726}
4727
4728unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4729 unsigned Depth) const {
4730 EVT VT = Op.getValueType();
4731 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4732 unsigned VTBits = VT.getScalarSizeInBits();
4733 unsigned NumElts = DemandedElts.getBitWidth();
4734 unsigned Tmp, Tmp2;
4735 unsigned FirstAnswer = 1;
4736
4737 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4738 const APInt &Val = C->getAPIntValue();
4739 return Val.getNumSignBits();
4740 }
4741
4742 if (Depth >= MaxRecursionDepth)
4743 return 1; // Limit search depth.
4744
4745 if (!DemandedElts)
4746 return 1; // No demanded elts, better to assume we don't know anything.
4747
4748 unsigned Opcode = Op.getOpcode();
4749 switch (Opcode) {
4750 default: break;
4751 case ISD::AssertSext:
4752 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4753 return VTBits-Tmp+1;
4754 case ISD::AssertZext:
4755 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4756 return VTBits-Tmp;
4757 case ISD::MERGE_VALUES:
4758 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4759 Depth + 1);
4760 case ISD::SPLAT_VECTOR: {
4761 // Check if the sign bits of source go down as far as the truncated value.
4762 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4763 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4764 if (NumSrcSignBits > (NumSrcBits - VTBits))
4765 return NumSrcSignBits - (NumSrcBits - VTBits);
4766 break;
4767 }
4768 case ISD::BUILD_VECTOR:
4769 assert(!VT.isScalableVector());
4770 Tmp = VTBits;
4771 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4772 if (!DemandedElts[i])
4773 continue;
4774
4775 SDValue SrcOp = Op.getOperand(i);
4776 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4777 // for constant nodes to ensure we only look at the sign bits.
4778 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4779 APInt T = C->getAPIntValue().trunc(VTBits);
4780 Tmp2 = T.getNumSignBits();
4781 } else {
4782 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4783
4784 if (SrcOp.getValueSizeInBits() != VTBits) {
4785 assert(SrcOp.getValueSizeInBits() > VTBits &&
4786 "Expected BUILD_VECTOR implicit truncation");
4787 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4788 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4789 }
4790 }
4791 Tmp = std::min(Tmp, Tmp2);
4792 }
4793 return Tmp;
4794
4795 case ISD::VECTOR_SHUFFLE: {
4796 // Collect the minimum number of sign bits that are shared by every vector
4797 // element referenced by the shuffle.
4798 APInt DemandedLHS, DemandedRHS;
4799 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4800 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4801 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4802 DemandedLHS, DemandedRHS))
4803 return 1;
4804
4805 Tmp = std::numeric_limits<unsigned>::max();
4806 if (!!DemandedLHS)
4807 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4808 if (!!DemandedRHS) {
4809 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4810 Tmp = std::min(Tmp, Tmp2);
4811 }
4812 // If we don't know anything, early out and try computeKnownBits fall-back.
4813 if (Tmp == 1)
4814 break;
4815 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4816 return Tmp;
4817 }
4818
4819 case ISD::BITCAST: {
4820 if (VT.isScalableVector())
4821 break;
4822 SDValue N0 = Op.getOperand(0);
4823 EVT SrcVT = N0.getValueType();
4824 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4825
4826 // Ignore bitcasts from unsupported types..
4827 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4828 break;
4829
4830 // Fast handling of 'identity' bitcasts.
4831 if (VTBits == SrcBits)
4832 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4833
4834 bool IsLE = getDataLayout().isLittleEndian();
4835
4836 // Bitcast 'large element' scalar/vector to 'small element' vector.
4837 if ((SrcBits % VTBits) == 0) {
4838 assert(VT.isVector() && "Expected bitcast to vector");
4839
4840 unsigned Scale = SrcBits / VTBits;
4841 APInt SrcDemandedElts =
4842 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4843
4844 // Fast case - sign splat can be simply split across the small elements.
4845 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4846 if (Tmp == SrcBits)
4847 return VTBits;
4848
4849 // Slow case - determine how far the sign extends into each sub-element.
4850 Tmp2 = VTBits;
4851 for (unsigned i = 0; i != NumElts; ++i)
4852 if (DemandedElts[i]) {
4853 unsigned SubOffset = i % Scale;
4854 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4855 SubOffset = SubOffset * VTBits;
4856 if (Tmp <= SubOffset)
4857 return 1;
4858 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4859 }
4860 return Tmp2;
4861 }
4862 break;
4863 }
4864
4866 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4867 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4868 return VTBits - Tmp + 1;
4869 case ISD::SIGN_EXTEND:
4870 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4871 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4873 // Max of the input and what this extends.
4874 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4875 Tmp = VTBits-Tmp+1;
4876 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4877 return std::max(Tmp, Tmp2);
4879 if (VT.isScalableVector())
4880 break;
4881 SDValue Src = Op.getOperand(0);
4882 EVT SrcVT = Src.getValueType();
4883 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4884 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4885 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4886 }
4887 case ISD::SRA:
4888 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4889 // SRA X, C -> adds C sign bits.
4890 if (std::optional<unsigned> ShAmt =
4891 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4892 Tmp = std::min(Tmp + *ShAmt, VTBits);
4893 return Tmp;
4894 case ISD::SHL:
4895 if (std::optional<ConstantRange> ShAmtRange =
4896 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4897 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4898 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4899 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4900 // shifted out, then we can compute the number of sign bits for the
4901 // operand being extended. A future improvement could be to pass along the
4902 // "shifted left by" information in the recursive calls to
4903 // ComputeKnownSignBits. Allowing us to handle this more generically.
4904 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4905 SDValue Ext = Op.getOperand(0);
4906 EVT ExtVT = Ext.getValueType();
4907 SDValue Extendee = Ext.getOperand(0);
4908 EVT ExtendeeVT = Extendee.getValueType();
4909 unsigned SizeDifference =
4910 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4911 if (SizeDifference <= MinShAmt) {
4912 Tmp = SizeDifference +
4913 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4914 if (MaxShAmt < Tmp)
4915 return Tmp - MaxShAmt;
4916 }
4917 }
4918 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4919 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4920 if (MaxShAmt < Tmp)
4921 return Tmp - MaxShAmt;
4922 }
4923 break;
4924 case ISD::AND:
4925 case ISD::OR:
4926 case ISD::XOR: // NOT is handled here.
4927 // Logical binary ops preserve the number of sign bits at the worst.
4928 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4929 if (Tmp != 1) {
4930 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4931 FirstAnswer = std::min(Tmp, Tmp2);
4932 // We computed what we know about the sign bits as our first
4933 // answer. Now proceed to the generic code that uses
4934 // computeKnownBits, and pick whichever answer is better.
4935 }
4936 break;
4937
4938 case ISD::SELECT:
4939 case ISD::VSELECT:
4940 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4941 if (Tmp == 1) return 1; // Early out.
4942 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4943 return std::min(Tmp, Tmp2);
4944 case ISD::SELECT_CC:
4945 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4946 if (Tmp == 1) return 1; // Early out.
4947 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4948 return std::min(Tmp, Tmp2);
4949
4950 case ISD::SMIN:
4951 case ISD::SMAX: {
4952 // If we have a clamp pattern, we know that the number of sign bits will be
4953 // the minimum of the clamp min/max range.
4954 bool IsMax = (Opcode == ISD::SMAX);
4955 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4956 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4957 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4958 CstHigh =
4959 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4960 if (CstLow && CstHigh) {
4961 if (!IsMax)
4962 std::swap(CstLow, CstHigh);
4963 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4964 Tmp = CstLow->getAPIntValue().getNumSignBits();
4965 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4966 return std::min(Tmp, Tmp2);
4967 }
4968 }
4969
4970 // Fallback - just get the minimum number of sign bits of the operands.
4971 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4972 if (Tmp == 1)
4973 return 1; // Early out.
4974 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4975 return std::min(Tmp, Tmp2);
4976 }
4977 case ISD::UMIN:
4978 case ISD::UMAX:
4979 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4980 if (Tmp == 1)
4981 return 1; // Early out.
4982 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4983 return std::min(Tmp, Tmp2);
4984 case ISD::SSUBO_CARRY:
4985 case ISD::USUBO_CARRY:
4986 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4987 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4988 return VTBits;
4989 [[fallthrough]];
4990 case ISD::SADDO:
4991 case ISD::UADDO:
4992 case ISD::SADDO_CARRY:
4993 case ISD::UADDO_CARRY:
4994 case ISD::SSUBO:
4995 case ISD::USUBO:
4996 case ISD::SMULO:
4997 case ISD::UMULO:
4998 if (Op.getResNo() != 1)
4999 break;
5000 // The boolean result conforms to getBooleanContents. Fall through.
5001 // If setcc returns 0/-1, all bits are sign bits.
5002 // We know that we have an integer-based boolean since these operations
5003 // are only available for integer.
5004 if (TLI->getBooleanContents(VT.isVector(), false) ==
5006 return VTBits;
5007 break;
5008 case ISD::SETCC:
5009 case ISD::SETCCCARRY:
5010 case ISD::STRICT_FSETCC:
5011 case ISD::STRICT_FSETCCS: {
5012 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5013 // If setcc returns 0/-1, all bits are sign bits.
5014 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5016 return VTBits;
5017 break;
5018 }
5019 case ISD::ROTL:
5020 case ISD::ROTR:
5021 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5022
5023 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5024 if (Tmp == VTBits)
5025 return VTBits;
5026
5027 if (ConstantSDNode *C =
5028 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5029 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5030
5031 // Handle rotate right by N like a rotate left by 32-N.
5032 if (Opcode == ISD::ROTR)
5033 RotAmt = (VTBits - RotAmt) % VTBits;
5034
5035 // If we aren't rotating out all of the known-in sign bits, return the
5036 // number that are left. This handles rotl(sext(x), 1) for example.
5037 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5038 }
5039 break;
5040 case ISD::ADD:
5041 case ISD::ADDC:
5042 // Add can have at most one carry bit. Thus we know that the output
5043 // is, at worst, one more bit than the inputs.
5044 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5045 if (Tmp == 1) return 1; // Early out.
5046
5047 // Special case decrementing a value (ADD X, -1):
5048 if (ConstantSDNode *CRHS =
5049 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5050 if (CRHS->isAllOnes()) {
5051 KnownBits Known =
5052 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5053
5054 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5055 // sign bits set.
5056 if ((Known.Zero | 1).isAllOnes())
5057 return VTBits;
5058
5059 // If we are subtracting one from a positive number, there is no carry
5060 // out of the result.
5061 if (Known.isNonNegative())
5062 return Tmp;
5063 }
5064
5065 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5066 if (Tmp2 == 1) return 1; // Early out.
5067 return std::min(Tmp, Tmp2) - 1;
5068 case ISD::SUB:
5069 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5070 if (Tmp2 == 1) return 1; // Early out.
5071
5072 // Handle NEG.
5073 if (ConstantSDNode *CLHS =
5074 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5075 if (CLHS->isZero()) {
5076 KnownBits Known =
5077 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5078 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5079 // sign bits set.
5080 if ((Known.Zero | 1).isAllOnes())
5081 return VTBits;
5082
5083 // If the input is known to be positive (the sign bit is known clear),
5084 // the output of the NEG has the same number of sign bits as the input.
5085 if (Known.isNonNegative())
5086 return Tmp2;
5087
5088 // Otherwise, we treat this like a SUB.
5089 }
5090
5091 // Sub can have at most one carry bit. Thus we know that the output
5092 // is, at worst, one more bit than the inputs.
5093 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5094 if (Tmp == 1) return 1; // Early out.
5095 return std::min(Tmp, Tmp2) - 1;
5096 case ISD::MUL: {
5097 // The output of the Mul can be at most twice the valid bits in the inputs.
5098 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5099 if (SignBitsOp0 == 1)
5100 break;
5101 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5102 if (SignBitsOp1 == 1)
5103 break;
5104 unsigned OutValidBits =
5105 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5106 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5107 }
5108 case ISD::AVGCEILS:
5109 case ISD::AVGFLOORS:
5110 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5111 if (Tmp == 1)
5112 return 1; // Early out.
5113 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5114 return std::min(Tmp, Tmp2);
5115 case ISD::SREM:
5116 // The sign bit is the LHS's sign bit, except when the result of the
5117 // remainder is zero. The magnitude of the result should be less than or
5118 // equal to the magnitude of the LHS. Therefore, the result should have
5119 // at least as many sign bits as the left hand side.
5120 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5121 case ISD::TRUNCATE: {
5122 // Check if the sign bits of source go down as far as the truncated value.
5123 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5124 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5125 if (NumSrcSignBits > (NumSrcBits - VTBits))
5126 return NumSrcSignBits - (NumSrcBits - VTBits);
5127 break;
5128 }
5129 case ISD::EXTRACT_ELEMENT: {
5130 if (VT.isScalableVector())
5131 break;
5132 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5133 const int BitWidth = Op.getValueSizeInBits();
5134 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5135
5136 // Get reverse index (starting from 1), Op1 value indexes elements from
5137 // little end. Sign starts at big end.
5138 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5139
5140 // If the sign portion ends in our element the subtraction gives correct
5141 // result. Otherwise it gives either negative or > bitwidth result
5142 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5143 }
5145 if (VT.isScalableVector())
5146 break;
5147 // If we know the element index, split the demand between the
5148 // source vector and the inserted element, otherwise assume we need
5149 // the original demanded vector elements and the value.
5150 SDValue InVec = Op.getOperand(0);
5151 SDValue InVal = Op.getOperand(1);
5152 SDValue EltNo = Op.getOperand(2);
5153 bool DemandedVal = true;
5154 APInt DemandedVecElts = DemandedElts;
5155 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5156 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5157 unsigned EltIdx = CEltNo->getZExtValue();
5158 DemandedVal = !!DemandedElts[EltIdx];
5159 DemandedVecElts.clearBit(EltIdx);
5160 }
5161 Tmp = std::numeric_limits<unsigned>::max();
5162 if (DemandedVal) {
5163 // TODO - handle implicit truncation of inserted elements.
5164 if (InVal.getScalarValueSizeInBits() != VTBits)
5165 break;
5166 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5167 Tmp = std::min(Tmp, Tmp2);
5168 }
5169 if (!!DemandedVecElts) {
5170 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5171 Tmp = std::min(Tmp, Tmp2);
5172 }
5173 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5174 return Tmp;
5175 }
5177 assert(!VT.isScalableVector());
5178 SDValue InVec = Op.getOperand(0);
5179 SDValue EltNo = Op.getOperand(1);
5180 EVT VecVT = InVec.getValueType();
5181 // ComputeNumSignBits not yet implemented for scalable vectors.
5182 if (VecVT.isScalableVector())
5183 break;
5184 const unsigned BitWidth = Op.getValueSizeInBits();
5185 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5186 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5187
5188 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5189 // anything about sign bits. But if the sizes match we can derive knowledge
5190 // about sign bits from the vector operand.
5191 if (BitWidth != EltBitWidth)
5192 break;
5193
5194 // If we know the element index, just demand that vector element, else for
5195 // an unknown element index, ignore DemandedElts and demand them all.
5196 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5197 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5198 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5199 DemandedSrcElts =
5200 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5201
5202 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5203 }
5205 // Offset the demanded elts by the subvector index.
5206 SDValue Src = Op.getOperand(0);
5207 // Bail until we can represent demanded elements for scalable vectors.
5208 if (Src.getValueType().isScalableVector())
5209 break;
5210 uint64_t Idx = Op.getConstantOperandVal(1);
5211 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5212 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5213 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5214 }
5215 case ISD::CONCAT_VECTORS: {
5216 if (VT.isScalableVector())
5217 break;
5218 // Determine the minimum number of sign bits across all demanded
5219 // elts of the input vectors. Early out if the result is already 1.
5220 Tmp = std::numeric_limits<unsigned>::max();
5221 EVT SubVectorVT = Op.getOperand(0).getValueType();
5222 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5223 unsigned NumSubVectors = Op.getNumOperands();
5224 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5225 APInt DemandedSub =
5226 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5227 if (!DemandedSub)
5228 continue;
5229 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5230 Tmp = std::min(Tmp, Tmp2);
5231 }
5232 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5233 return Tmp;
5234 }
5235 case ISD::INSERT_SUBVECTOR: {
5236 if (VT.isScalableVector())
5237 break;
5238 // Demand any elements from the subvector and the remainder from the src its
5239 // inserted into.
5240 SDValue Src = Op.getOperand(0);
5241 SDValue Sub = Op.getOperand(1);
5242 uint64_t Idx = Op.getConstantOperandVal(2);
5243 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5244 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5245 APInt DemandedSrcElts = DemandedElts;
5246 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5247
5248 Tmp = std::numeric_limits<unsigned>::max();
5249 if (!!DemandedSubElts) {
5250 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5251 if (Tmp == 1)
5252 return 1; // early-out
5253 }
5254 if (!!DemandedSrcElts) {
5255 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, 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::LOAD: {
5262 LoadSDNode *LD = cast<LoadSDNode>(Op);
5263 if (const MDNode *Ranges = LD->getRanges()) {
5264 if (DemandedElts != 1)
5265 break;
5266
5268 if (VTBits > CR.getBitWidth()) {
5269 switch (LD->getExtensionType()) {
5270 case ISD::SEXTLOAD:
5271 CR = CR.signExtend(VTBits);
5272 break;
5273 case ISD::ZEXTLOAD:
5274 CR = CR.zeroExtend(VTBits);
5275 break;
5276 default:
5277 break;
5278 }
5279 }
5280
5281 if (VTBits != CR.getBitWidth())
5282 break;
5283 return std::min(CR.getSignedMin().getNumSignBits(),
5285 }
5286
5287 break;
5288 }
5291 case ISD::ATOMIC_SWAP:
5303 case ISD::ATOMIC_LOAD: {
5304 auto *AT = cast<AtomicSDNode>(Op);
5305 // If we are looking at the loaded value.
5306 if (Op.getResNo() == 0) {
5307 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5308 if (Tmp == VTBits)
5309 return 1; // early-out
5310
5311 // For atomic_load, prefer to use the extension type.
5312 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5313 switch (AT->getExtensionType()) {
5314 default:
5315 break;
5316 case ISD::SEXTLOAD:
5317 return VTBits - Tmp + 1;
5318 case ISD::ZEXTLOAD:
5319 return VTBits - Tmp;
5320 }
5321 }
5322
5324 return VTBits - Tmp + 1;
5326 return VTBits - Tmp;
5327 }
5328 break;
5329 }
5330 }
5331
5332 // If we are looking at the loaded value of the SDNode.
5333 if (Op.getResNo() == 0) {
5334 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5335 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5336 unsigned ExtType = LD->getExtensionType();
5337 switch (ExtType) {
5338 default: break;
5339 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5340 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5341 return VTBits - Tmp + 1;
5342 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5343 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5344 return VTBits - Tmp;
5345 case ISD::NON_EXTLOAD:
5346 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5347 // We only need to handle vectors - computeKnownBits should handle
5348 // scalar cases.
5349 Type *CstTy = Cst->getType();
5350 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5351 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5352 VTBits == CstTy->getScalarSizeInBits()) {
5353 Tmp = VTBits;
5354 for (unsigned i = 0; i != NumElts; ++i) {
5355 if (!DemandedElts[i])
5356 continue;
5357 if (Constant *Elt = Cst->getAggregateElement(i)) {
5358 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5359 const APInt &Value = CInt->getValue();
5360 Tmp = std::min(Tmp, Value.getNumSignBits());
5361 continue;
5362 }
5363 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5364 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5365 Tmp = std::min(Tmp, Value.getNumSignBits());
5366 continue;
5367 }
5368 }
5369 // Unknown type. Conservatively assume no bits match sign bit.
5370 return 1;
5371 }
5372 return Tmp;
5373 }
5374 }
5375 break;
5376 }
5377 }
5378 }
5379
5380 // Allow the target to implement this method for its nodes.
5381 if (Opcode >= ISD::BUILTIN_OP_END ||
5382 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5383 Opcode == ISD::INTRINSIC_W_CHAIN ||
5384 Opcode == ISD::INTRINSIC_VOID) {
5385 // TODO: This can probably be removed once target code is audited. This
5386 // is here purely to reduce patch size and review complexity.
5387 if (!VT.isScalableVector()) {
5388 unsigned NumBits =
5389 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5390 if (NumBits > 1)
5391 FirstAnswer = std::max(FirstAnswer, NumBits);
5392 }
5393 }
5394
5395 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5396 // use this information.
5397 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5398 return std::max(FirstAnswer, Known.countMinSignBits());
5399}
5400
5402 unsigned Depth) const {
5403 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5404 return Op.getScalarValueSizeInBits() - SignBits + 1;
5405}
5406
5408 const APInt &DemandedElts,
5409 unsigned Depth) const {
5410 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5411 return Op.getScalarValueSizeInBits() - SignBits + 1;
5412}
5413
5415 unsigned Depth) const {
5416 // Early out for FREEZE.
5417 if (Op.getOpcode() == ISD::FREEZE)
5418 return true;
5419
5420 EVT VT = Op.getValueType();
5421 APInt DemandedElts = VT.isFixedLengthVector()
5423 : APInt(1, 1);
5424 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5425}
5426
5428 const APInt &DemandedElts,
5429 bool PoisonOnly,
5430 unsigned Depth) const {
5431 unsigned Opcode = Op.getOpcode();
5432
5433 // Early out for FREEZE.
5434 if (Opcode == ISD::FREEZE)
5435 return true;
5436
5437 if (Depth >= MaxRecursionDepth)
5438 return false; // Limit search depth.
5439
5440 if (isIntOrFPConstant(Op))
5441 return true;
5442
5443 switch (Opcode) {
5444 case ISD::CONDCODE:
5445 case ISD::VALUETYPE:
5446 case ISD::FrameIndex:
5448 case ISD::CopyFromReg:
5449 return true;
5450
5451 case ISD::POISON:
5452 return false;
5453
5454 case ISD::UNDEF:
5455 return PoisonOnly;
5456
5457 case ISD::BUILD_VECTOR:
5458 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5459 // this shouldn't affect the result.
5460 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5461 if (!DemandedElts[i])
5462 continue;
5464 Depth + 1))
5465 return false;
5466 }
5467 return true;
5468
5470 SDValue Src = Op.getOperand(0);
5471 if (Src.getValueType().isScalableVector())
5472 break;
5473 uint64_t Idx = Op.getConstantOperandVal(1);
5474 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5475 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5476 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5477 Depth + 1);
5478 }
5479
5480 case ISD::INSERT_SUBVECTOR: {
5481 if (Op.getValueType().isScalableVector())
5482 break;
5483 SDValue Src = Op.getOperand(0);
5484 SDValue Sub = Op.getOperand(1);
5485 uint64_t Idx = Op.getConstantOperandVal(2);
5486 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5487 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5488 APInt DemandedSrcElts = DemandedElts;
5489 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5490
5491 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5492 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5493 return false;
5494 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5495 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5496 return false;
5497 return true;
5498 }
5499
5501 SDValue Src = Op.getOperand(0);
5502 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5503 EVT SrcVT = Src.getValueType();
5504 if (SrcVT.isFixedLengthVector() && IndexC &&
5505 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5506 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5507 IndexC->getZExtValue());
5508 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5509 Depth + 1);
5510 }
5511 break;
5512 }
5513
5515 SDValue InVec = Op.getOperand(0);
5516 SDValue InVal = Op.getOperand(1);
5517 SDValue EltNo = Op.getOperand(2);
5518 EVT VT = InVec.getValueType();
5519 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5520 if (IndexC && VT.isFixedLengthVector() &&
5521 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5522 if (DemandedElts[IndexC->getZExtValue()] &&
5524 return false;
5525 APInt InVecDemandedElts = DemandedElts;
5526 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5527 if (!!InVecDemandedElts &&
5528 !isGuaranteedNotToBeUndefOrPoison(InVec, InVecDemandedElts,
5529 PoisonOnly, Depth + 1))
5530 return false;
5531 return true;
5532 }
5533 break;
5534 }
5535
5537 // Check upper (known undef) elements.
5538 if (DemandedElts.ugt(1) && !PoisonOnly)
5539 return false;
5540 // Check element zero.
5541 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5542 Op.getOperand(0), PoisonOnly, Depth + 1))
5543 return false;
5544 return true;
5545
5546 case ISD::SPLAT_VECTOR:
5547 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5548 Depth + 1);
5549
5550 case ISD::VECTOR_SHUFFLE: {
5551 APInt DemandedLHS, DemandedRHS;
5552 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5553 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5554 DemandedElts, DemandedLHS, DemandedRHS,
5555 /*AllowUndefElts=*/false))
5556 return false;
5557 if (!DemandedLHS.isZero() &&
5558 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5559 PoisonOnly, Depth + 1))
5560 return false;
5561 if (!DemandedRHS.isZero() &&
5562 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5563 PoisonOnly, Depth + 1))
5564 return false;
5565 return true;
5566 }
5567
5568 case ISD::SHL:
5569 case ISD::SRL:
5570 case ISD::SRA:
5571 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5572 // enough to check operand 0 if Op can't create undef/poison.
5573 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5574 /*ConsiderFlags*/ true, Depth) &&
5575 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5576 PoisonOnly, Depth + 1);
5577
5578 case ISD::BSWAP:
5579 case ISD::CTPOP:
5580 case ISD::BITREVERSE:
5581 case ISD::AND:
5582 case ISD::OR:
5583 case ISD::XOR:
5584 case ISD::ADD:
5585 case ISD::SUB:
5586 case ISD::MUL:
5587 case ISD::SADDSAT:
5588 case ISD::UADDSAT:
5589 case ISD::SSUBSAT:
5590 case ISD::USUBSAT:
5591 case ISD::SSHLSAT:
5592 case ISD::USHLSAT:
5593 case ISD::SMIN:
5594 case ISD::SMAX:
5595 case ISD::UMIN:
5596 case ISD::UMAX:
5597 case ISD::ZERO_EXTEND:
5598 case ISD::SIGN_EXTEND:
5599 case ISD::ANY_EXTEND:
5600 case ISD::TRUNCATE:
5601 case ISD::VSELECT: {
5602 // If Op can't create undef/poison and none of its operands are undef/poison
5603 // then Op is never undef/poison. A difference from the more common check
5604 // below, outside the switch, is that we handle elementwise operations for
5605 // which the DemandedElts mask is valid for all operands here.
5606 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5607 /*ConsiderFlags*/ true, Depth) &&
5608 all_of(Op->ops(), [&](SDValue V) {
5609 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5610 PoisonOnly, Depth + 1);
5611 });
5612 }
5613
5614 // TODO: Search for noundef attributes from library functions.
5615
5616 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5617
5618 default:
5619 // Allow the target to implement this method for its nodes.
5620 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5621 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5623 Op, DemandedElts, *this, PoisonOnly, Depth);
5624 break;
5625 }
5626
5627 // If Op can't create undef/poison and none of its operands are undef/poison
5628 // then Op is never undef/poison.
5629 // NOTE: TargetNodes can handle this in themselves in
5630 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5631 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5632 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5633 Depth) &&
5634 all_of(Op->ops(), [&](SDValue V) {
5635 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5636 });
5637}
5638
5640 bool ConsiderFlags,
5641 unsigned Depth) const {
5642 EVT VT = Op.getValueType();
5643 APInt DemandedElts = VT.isFixedLengthVector()
5645 : APInt(1, 1);
5646 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5647 Depth);
5648}
5649
5651 bool PoisonOnly, bool ConsiderFlags,
5652 unsigned Depth) const {
5653 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5654 return true;
5655
5656 unsigned Opcode = Op.getOpcode();
5657 switch (Opcode) {
5658 case ISD::AssertSext:
5659 case ISD::AssertZext:
5660 case ISD::AssertAlign:
5662 // Assertion nodes can create poison if the assertion fails.
5663 return true;
5664
5665 case ISD::FREEZE:
5669 case ISD::SADDSAT:
5670 case ISD::UADDSAT:
5671 case ISD::SSUBSAT:
5672 case ISD::USUBSAT:
5673 case ISD::MULHU:
5674 case ISD::MULHS:
5675 case ISD::ABDU:
5676 case ISD::ABDS:
5677 case ISD::SMIN:
5678 case ISD::SMAX:
5679 case ISD::SCMP:
5680 case ISD::UMIN:
5681 case ISD::UMAX:
5682 case ISD::UCMP:
5683 case ISD::AND:
5684 case ISD::XOR:
5685 case ISD::ROTL:
5686 case ISD::ROTR:
5687 case ISD::FSHL:
5688 case ISD::FSHR:
5689 case ISD::BSWAP:
5690 case ISD::CTTZ:
5691 case ISD::CTLZ:
5692 case ISD::CTPOP:
5693 case ISD::BITREVERSE:
5694 case ISD::PARITY:
5695 case ISD::SIGN_EXTEND:
5696 case ISD::TRUNCATE:
5700 case ISD::BITCAST:
5701 case ISD::BUILD_VECTOR:
5702 case ISD::BUILD_PAIR:
5703 case ISD::SPLAT_VECTOR:
5704 case ISD::FABS:
5705 return false;
5706
5707 case ISD::ABS:
5708 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5709 // Different to Intrinsic::abs.
5710 return false;
5711
5712 case ISD::ADDC:
5713 case ISD::SUBC:
5714 case ISD::ADDE:
5715 case ISD::SUBE:
5716 case ISD::SADDO:
5717 case ISD::SSUBO:
5718 case ISD::SMULO:
5719 case ISD::SADDO_CARRY:
5720 case ISD::SSUBO_CARRY:
5721 case ISD::UADDO:
5722 case ISD::USUBO:
5723 case ISD::UMULO:
5724 case ISD::UADDO_CARRY:
5725 case ISD::USUBO_CARRY:
5726 // No poison on result or overflow flags.
5727 return false;
5728
5729 case ISD::SELECT_CC:
5730 case ISD::SETCC: {
5731 // Integer setcc cannot create undef or poison.
5732 if (Op.getOperand(0).getValueType().isInteger())
5733 return false;
5734
5735 // FP compares are more complicated. They can create poison for nan/infinity
5736 // based on options and flags. The options and flags also cause special
5737 // nonan condition codes to be used. Those condition codes may be preserved
5738 // even if the nonan flag is dropped somewhere.
5739 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5740 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5741 if (((unsigned)CCCode & 0x10U))
5742 return true;
5743
5745 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5746 }
5747
5748 case ISD::OR:
5749 case ISD::ZERO_EXTEND:
5750 case ISD::SELECT:
5751 case ISD::VSELECT:
5752 case ISD::ADD:
5753 case ISD::SUB:
5754 case ISD::MUL:
5755 case ISD::FNEG:
5756 case ISD::FADD:
5757 case ISD::FSUB:
5758 case ISD::FMUL:
5759 case ISD::FDIV:
5760 case ISD::FREM:
5761 case ISD::FCOPYSIGN:
5762 case ISD::FMA:
5763 case ISD::FMAD:
5764 case ISD::FP_EXTEND:
5767 // No poison except from flags (which is handled above)
5768 return false;
5769
5770 case ISD::SHL:
5771 case ISD::SRL:
5772 case ISD::SRA:
5773 // If the max shift amount isn't in range, then the shift can
5774 // create poison.
5775 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5776
5779 // If the amount is zero then the result will be poison.
5780 // TODO: Add isKnownNeverZero DemandedElts handling.
5781 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5782
5784 // Check if we demand any upper (undef) elements.
5785 return !PoisonOnly && DemandedElts.ugt(1);
5786
5789 // Ensure that the element index is in bounds.
5790 EVT VecVT = Op.getOperand(0).getValueType();
5791 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5792 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5793 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5794 }
5795
5796 case ISD::VECTOR_SHUFFLE: {
5797 // Check for any demanded shuffle element that is undef.
5798 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5799 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5800 if (Elt < 0 && DemandedElts[Idx])
5801 return true;
5802 return false;
5803 }
5804
5805 default:
5806 // Allow the target to implement this method for its nodes.
5807 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5808 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5810 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5811 break;
5812 }
5813
5814 // Be conservative and return true.
5815 return true;
5816}
5817
5818bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5819 unsigned Opcode = Op.getOpcode();
5820 if (Opcode == ISD::OR)
5821 return Op->getFlags().hasDisjoint() ||
5822 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5823 if (Opcode == ISD::XOR)
5824 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5825 return false;
5826}
5827
5829 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5830 (Op.isAnyAdd() || isADDLike(Op));
5831}
5832
5834 unsigned Depth) const {
5835 EVT VT = Op.getValueType();
5836
5837 // Since the number of lanes in a scalable vector is unknown at compile time,
5838 // we track one bit which is implicitly broadcast to all lanes. This means
5839 // that all lanes in a scalable vector are considered demanded.
5840 APInt DemandedElts = VT.isFixedLengthVector()
5842 : APInt(1, 1);
5843
5844 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5845}
5846
5848 bool SNaN, unsigned Depth) const {
5849 assert(!DemandedElts.isZero() && "No demanded elements");
5850
5851 // If we're told that NaNs won't happen, assume they won't.
5852 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5853 return true;
5854
5855 if (Depth >= MaxRecursionDepth)
5856 return false; // Limit search depth.
5857
5858 // If the value is a constant, we can obviously see if it is a NaN or not.
5859 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5860 return !C->getValueAPF().isNaN() ||
5861 (SNaN && !C->getValueAPF().isSignaling());
5862 }
5863
5864 unsigned Opcode = Op.getOpcode();
5865 switch (Opcode) {
5866 case ISD::FADD:
5867 case ISD::FSUB:
5868 case ISD::FMUL:
5869 case ISD::FDIV:
5870 case ISD::FREM:
5871 case ISD::FSIN:
5872 case ISD::FCOS:
5873 case ISD::FTAN:
5874 case ISD::FASIN:
5875 case ISD::FACOS:
5876 case ISD::FATAN:
5877 case ISD::FATAN2:
5878 case ISD::FSINH:
5879 case ISD::FCOSH:
5880 case ISD::FTANH:
5881 case ISD::FMA:
5882 case ISD::FMAD: {
5883 if (SNaN)
5884 return true;
5885 // TODO: Need isKnownNeverInfinity
5886 return false;
5887 }
5888 case ISD::FCANONICALIZE:
5889 case ISD::FEXP:
5890 case ISD::FEXP2:
5891 case ISD::FEXP10:
5892 case ISD::FTRUNC:
5893 case ISD::FFLOOR:
5894 case ISD::FCEIL:
5895 case ISD::FROUND:
5896 case ISD::FROUNDEVEN:
5897 case ISD::LROUND:
5898 case ISD::LLROUND:
5899 case ISD::FRINT:
5900 case ISD::LRINT:
5901 case ISD::LLRINT:
5902 case ISD::FNEARBYINT:
5903 case ISD::FLDEXP: {
5904 if (SNaN)
5905 return true;
5906 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5907 }
5908 case ISD::FABS:
5909 case ISD::FNEG:
5910 case ISD::FCOPYSIGN: {
5911 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5912 }
5913 case ISD::SELECT:
5914 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5915 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5916 case ISD::FP_EXTEND:
5917 case ISD::FP_ROUND: {
5918 if (SNaN)
5919 return true;
5920 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5921 }
5922 case ISD::SINT_TO_FP:
5923 case ISD::UINT_TO_FP:
5924 return true;
5925 case ISD::FSQRT: // Need is known positive
5926 case ISD::FLOG:
5927 case ISD::FLOG2:
5928 case ISD::FLOG10:
5929 case ISD::FPOWI:
5930 case ISD::FPOW: {
5931 if (SNaN)
5932 return true;
5933 // TODO: Refine on operand
5934 return false;
5935 }
5936 case ISD::FMINNUM:
5937 case ISD::FMAXNUM:
5938 case ISD::FMINIMUMNUM:
5939 case ISD::FMAXIMUMNUM: {
5940 // Only one needs to be known not-nan, since it will be returned if the
5941 // other ends up being one.
5942 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5943 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5944 }
5945 case ISD::FMINNUM_IEEE:
5946 case ISD::FMAXNUM_IEEE: {
5947 if (SNaN)
5948 return true;
5949 // This can return a NaN if either operand is an sNaN, or if both operands
5950 // are NaN.
5951 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5952 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5953 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5954 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5955 }
5956 case ISD::FMINIMUM:
5957 case ISD::FMAXIMUM: {
5958 // TODO: Does this quiet or return the origina NaN as-is?
5959 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5960 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5961 }
5963 SDValue Src = Op.getOperand(0);
5964 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5965 EVT SrcVT = Src.getValueType();
5966 if (SrcVT.isFixedLengthVector() && Idx &&
5967 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5968 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5969 Idx->getZExtValue());
5970 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5971 }
5972 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5973 }
5975 SDValue Src = Op.getOperand(0);
5976 if (Src.getValueType().isFixedLengthVector()) {
5977 unsigned Idx = Op.getConstantOperandVal(1);
5978 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5979 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5980 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5981 }
5982 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5983 }
5984 case ISD::INSERT_SUBVECTOR: {
5985 SDValue BaseVector = Op.getOperand(0);
5986 SDValue SubVector = Op.getOperand(1);
5987 EVT BaseVectorVT = BaseVector.getValueType();
5988 if (BaseVectorVT.isFixedLengthVector()) {
5989 unsigned Idx = Op.getConstantOperandVal(2);
5990 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5991 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5992
5993 // Clear/Extract the bits at the position where the subvector will be
5994 // inserted.
5995 APInt DemandedMask =
5996 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5997 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5998 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5999
6000 bool NeverNaN = true;
6001 if (!DemandedSrcElts.isZero())
6002 NeverNaN &=
6003 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6004 if (NeverNaN && !DemandedSubElts.isZero())
6005 NeverNaN &=
6006 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6007 return NeverNaN;
6008 }
6009 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6010 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6011 }
6012 case ISD::BUILD_VECTOR: {
6013 unsigned NumElts = Op.getNumOperands();
6014 for (unsigned I = 0; I != NumElts; ++I)
6015 if (DemandedElts[I] &&
6016 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6017 return false;
6018 return true;
6019 }
6020 case ISD::AssertNoFPClass: {
6021 FPClassTest NoFPClass =
6022 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6023 if ((NoFPClass & fcNan) == fcNan)
6024 return true;
6025 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6026 return true;
6027 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6028 }
6029 default:
6030 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6031 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6032 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6033 Depth);
6034 }
6035
6036 return false;
6037 }
6038}
6039
6041 assert(Op.getValueType().isFloatingPoint() &&
6042 "Floating point type expected");
6043
6044 // If the value is a constant, we can obviously see if it is a zero or not.
6046 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6047}
6048
6050 if (Depth >= MaxRecursionDepth)
6051 return false; // Limit search depth.
6052
6053 assert(!Op.getValueType().isFloatingPoint() &&
6054 "Floating point types unsupported - use isKnownNeverZeroFloat");
6055
6056 // If the value is a constant, we can obviously see if it is a zero or not.
6058 [](ConstantSDNode *C) { return !C->isZero(); }))
6059 return true;
6060
6061 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6062 // some degree.
6063 switch (Op.getOpcode()) {
6064 default:
6065 break;
6066
6067 case ISD::OR:
6068 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6069 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6070
6071 case ISD::VSELECT:
6072 case ISD::SELECT:
6073 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6074 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6075
6076 case ISD::SHL: {
6077 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6078 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6079 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6080 // 1 << X is never zero.
6081 if (ValKnown.One[0])
6082 return true;
6083 // If max shift cnt of known ones is non-zero, result is non-zero.
6084 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6085 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6086 !ValKnown.One.shl(MaxCnt).isZero())
6087 return true;
6088 break;
6089 }
6090 case ISD::UADDSAT:
6091 case ISD::UMAX:
6092 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6093 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6094
6095 // For smin/smax: If either operand is known negative/positive
6096 // respectively we don't need the other to be known at all.
6097 case ISD::SMAX: {
6098 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6099 if (Op1.isStrictlyPositive())
6100 return true;
6101
6102 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6103 if (Op0.isStrictlyPositive())
6104 return true;
6105
6106 if (Op1.isNonZero() && Op0.isNonZero())
6107 return true;
6108
6109 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6110 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6111 }
6112 case ISD::SMIN: {
6113 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6114 if (Op1.isNegative())
6115 return true;
6116
6117 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6118 if (Op0.isNegative())
6119 return true;
6120
6121 if (Op1.isNonZero() && Op0.isNonZero())
6122 return true;
6123
6124 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6125 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6126 }
6127 case ISD::UMIN:
6128 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6129 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6130
6131 case ISD::ROTL:
6132 case ISD::ROTR:
6133 case ISD::BITREVERSE:
6134 case ISD::BSWAP:
6135 case ISD::CTPOP:
6136 case ISD::ABS:
6137 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6138
6139 case ISD::SRA:
6140 case ISD::SRL: {
6141 if (Op->getFlags().hasExact())
6142 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6143 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6144 if (ValKnown.isNegative())
6145 return true;
6146 // If max shift cnt of known ones is non-zero, result is non-zero.
6147 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6148 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6149 !ValKnown.One.lshr(MaxCnt).isZero())
6150 return true;
6151 break;
6152 }
6153 case ISD::UDIV:
6154 case ISD::SDIV:
6155 // div exact can only produce a zero if the dividend is zero.
6156 // TODO: For udiv this is also true if Op1 u<= Op0
6157 if (Op->getFlags().hasExact())
6158 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6159 break;
6160
6161 case ISD::ADD:
6162 if (Op->getFlags().hasNoUnsignedWrap())
6163 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6164 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6165 return true;
6166 // TODO: There are a lot more cases we can prove for add.
6167 break;
6168
6169 case ISD::SUB: {
6170 if (isNullConstant(Op.getOperand(0)))
6171 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6172
6173 std::optional<bool> ne =
6174 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6175 computeKnownBits(Op.getOperand(1), Depth + 1));
6176 return ne && *ne;
6177 }
6178
6179 case ISD::MUL:
6180 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6181 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6182 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6183 return true;
6184 break;
6185
6186 case ISD::ZERO_EXTEND:
6187 case ISD::SIGN_EXTEND:
6188 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6189 case ISD::VSCALE: {
6191 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6192 ConstantRange CR =
6193 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6194 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6195 return true;
6196 break;
6197 }
6198 }
6199
6201}
6202
6204 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6205 return !C1->isNegative();
6206
6207 return Op.getOpcode() == ISD::FABS;
6208}
6209
6211 // Check the obvious case.
6212 if (A == B) return true;
6213
6214 // For negative and positive zero.
6215 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
6216 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
6217 if (CA->isZero() && CB->isZero()) return true;
6218
6219 // Otherwise they may not be equal.
6220 return false;
6221}
6222
6223// Only bits set in Mask must be negated, other bits may be arbitrary.
6225 if (isBitwiseNot(V, AllowUndefs))
6226 return V.getOperand(0);
6227
6228 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6229 // bits in the non-extended part.
6230 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6231 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6232 return SDValue();
6233 SDValue ExtArg = V.getOperand(0);
6234 if (ExtArg.getScalarValueSizeInBits() >=
6235 MaskC->getAPIntValue().getActiveBits() &&
6236 isBitwiseNot(ExtArg, AllowUndefs) &&
6237 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6238 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6239 return ExtArg.getOperand(0).getOperand(0);
6240 return SDValue();
6241}
6242
6244 // Match masked merge pattern (X & ~M) op (Y & M)
6245 // Including degenerate case (X & ~M) op M
6246 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6247 SDValue Other) {
6248 if (SDValue NotOperand =
6249 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6250 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6251 NotOperand->getOpcode() == ISD::TRUNCATE)
6252 NotOperand = NotOperand->getOperand(0);
6253
6254 if (Other == NotOperand)
6255 return true;
6256 if (Other->getOpcode() == ISD::AND)
6257 return NotOperand == Other->getOperand(0) ||
6258 NotOperand == Other->getOperand(1);
6259 }
6260 return false;
6261 };
6262
6263 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6264 A = A->getOperand(0);
6265
6266 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6267 B = B->getOperand(0);
6268
6269 if (A->getOpcode() == ISD::AND)
6270 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6271 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6272 return false;
6273}
6274
6275// FIXME: unify with llvm::haveNoCommonBitsSet.
6277 assert(A.getValueType() == B.getValueType() &&
6278 "Values must have the same type");
6281 return true;
6284}
6285
6286static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6287 SelectionDAG &DAG) {
6288 if (cast<ConstantSDNode>(Step)->isZero())
6289 return DAG.getConstant(0, DL, VT);
6290
6291 return SDValue();
6292}
6293
6296 SelectionDAG &DAG) {
6297 int NumOps = Ops.size();
6298 assert(NumOps != 0 && "Can't build an empty vector!");
6299 assert(!VT.isScalableVector() &&
6300 "BUILD_VECTOR cannot be used with scalable types");
6301 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6302 "Incorrect element count in BUILD_VECTOR!");
6303
6304 // BUILD_VECTOR of UNDEFs is UNDEF.
6305 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6306 return DAG.getUNDEF(VT);
6307
6308 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6309 SDValue IdentitySrc;
6310 bool IsIdentity = true;
6311 for (int i = 0; i != NumOps; ++i) {
6312 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
6313 Ops[i].getOperand(0).getValueType() != VT ||
6314 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6315 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6316 Ops[i].getConstantOperandAPInt(1) != i) {
6317 IsIdentity = false;
6318 break;
6319 }
6320 IdentitySrc = Ops[i].getOperand(0);
6321 }
6322 if (IsIdentity)
6323 return IdentitySrc;
6324
6325 return SDValue();
6326}
6327
6328/// Try to simplify vector concatenation to an input value, undef, or build
6329/// vector.
6332 SelectionDAG &DAG) {
6333 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6334 assert(llvm::all_of(Ops,
6335 [Ops](SDValue Op) {
6336 return Ops[0].getValueType() == Op.getValueType();
6337 }) &&
6338 "Concatenation of vectors with inconsistent value types!");
6339 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6340 VT.getVectorElementCount() &&
6341 "Incorrect element count in vector concatenation!");
6342
6343 if (Ops.size() == 1)
6344 return Ops[0];
6345
6346 // Concat of UNDEFs is UNDEF.
6347 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6348 return DAG.getUNDEF(VT);
6349
6350 // Scan the operands and look for extract operations from a single source
6351 // that correspond to insertion at the same location via this concatenation:
6352 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6353 SDValue IdentitySrc;
6354 bool IsIdentity = true;
6355 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6356 SDValue Op = Ops[i];
6357 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6358 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6359 Op.getOperand(0).getValueType() != VT ||
6360 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6361 Op.getConstantOperandVal(1) != IdentityIndex) {
6362 IsIdentity = false;
6363 break;
6364 }
6365 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6366 "Unexpected identity source vector for concat of extracts");
6367 IdentitySrc = Op.getOperand(0);
6368 }
6369 if (IsIdentity) {
6370 assert(IdentitySrc && "Failed to set source vector of extracts");
6371 return IdentitySrc;
6372 }
6373
6374 // The code below this point is only designed to work for fixed width
6375 // vectors, so we bail out for now.
6376 if (VT.isScalableVector())
6377 return SDValue();
6378
6379 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
6380 // simplified to one big BUILD_VECTOR.
6381 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6382 EVT SVT = VT.getScalarType();
6384 for (SDValue Op : Ops) {
6385 EVT OpVT = Op.getValueType();
6386 if (Op.isUndef())
6387 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6388 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6389 Elts.append(Op->op_begin(), Op->op_end());
6390 else
6391 return SDValue();
6392 }
6393
6394 // BUILD_VECTOR requires all inputs to be of the same type, find the
6395 // maximum type and extend them all.
6396 for (SDValue Op : Elts)
6397 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6398
6399 if (SVT.bitsGT(VT.getScalarType())) {
6400 for (SDValue &Op : Elts) {
6401 if (Op.isUndef())
6402 Op = DAG.getUNDEF(SVT);
6403 else
6404 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6405 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6406 : DAG.getSExtOrTrunc(Op, DL, SVT);
6407 }
6408 }
6409
6410 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6411 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6412 return V;
6413}
6414
6415/// Gets or creates the specified node.
6416SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6417 SDVTList VTs = getVTList(VT);
6419 AddNodeIDNode(ID, Opcode, VTs, {});
6420 void *IP = nullptr;
6421 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6422 return SDValue(E, 0);
6423
6424 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6425 CSEMap.InsertNode(N, IP);
6426
6427 InsertNode(N);
6428 SDValue V = SDValue(N, 0);
6429 NewSDValueDbgMsg(V, "Creating new node: ", this);
6430 return V;
6431}
6432
6433SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6434 SDValue N1) {
6435 SDNodeFlags Flags;
6436 if (Inserter)
6437 Flags = Inserter->getFlags();
6438 return getNode(Opcode, DL, VT, N1, Flags);
6439}
6440
6441SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6442 SDValue N1, const SDNodeFlags Flags) {
6443 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6444
6445 // Constant fold unary operations with a vector integer or float operand.
6446 switch (Opcode) {
6447 default:
6448 // FIXME: Entirely reasonable to perform folding of other unary
6449 // operations here as the need arises.
6450 break;
6451 case ISD::FNEG:
6452 case ISD::FABS:
6453 case ISD::FCEIL:
6454 case ISD::FTRUNC:
6455 case ISD::FFLOOR:
6456 case ISD::FP_EXTEND:
6457 case ISD::FP_TO_SINT:
6458 case ISD::FP_TO_UINT:
6459 case ISD::FP_TO_FP16:
6460 case ISD::FP_TO_BF16:
6461 case ISD::TRUNCATE:
6462 case ISD::ANY_EXTEND:
6463 case ISD::ZERO_EXTEND:
6464 case ISD::SIGN_EXTEND:
6465 case ISD::UINT_TO_FP:
6466 case ISD::SINT_TO_FP:
6467 case ISD::FP16_TO_FP:
6468 case ISD::BF16_TO_FP:
6469 case ISD::BITCAST:
6470 case ISD::ABS:
6471 case ISD::BITREVERSE:
6472 case ISD::BSWAP:
6473 case ISD::CTLZ:
6475 case ISD::CTTZ:
6477 case ISD::CTPOP:
6478 case ISD::STEP_VECTOR: {
6479 SDValue Ops = {N1};
6480 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6481 return Fold;
6482 }
6483 }
6484
6485 unsigned OpOpcode = N1.getNode()->getOpcode();
6486 switch (Opcode) {
6487 case ISD::STEP_VECTOR:
6488 assert(VT.isScalableVector() &&
6489 "STEP_VECTOR can only be used with scalable types");
6490 assert(OpOpcode == ISD::TargetConstant &&
6491 VT.getVectorElementType() == N1.getValueType() &&
6492 "Unexpected step operand");
6493 break;
6494 case ISD::FREEZE:
6495 assert(VT == N1.getValueType() && "Unexpected VT!");
6496 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6497 return N1;
6498 break;
6499 case ISD::TokenFactor:
6500 case ISD::MERGE_VALUES:
6502 return N1; // Factor, merge or concat of one node? No need.
6503 case ISD::BUILD_VECTOR: {
6504 // Attempt to simplify BUILD_VECTOR.
6505 SDValue Ops[] = {N1};
6506 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6507 return V;
6508 break;
6509 }
6510 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6511 case ISD::FP_EXTEND:
6513 "Invalid FP cast!");
6514 if (N1.getValueType() == VT) return N1; // noop conversion.
6515 assert((!VT.isVector() || VT.getVectorElementCount() ==
6517 "Vector element count mismatch!");
6518 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6519 if (N1.isUndef())
6520 return getUNDEF(VT);
6521 break;
6522 case ISD::FP_TO_SINT:
6523 case ISD::FP_TO_UINT:
6524 if (N1.isUndef())
6525 return getUNDEF(VT);
6526 break;
6527 case ISD::SINT_TO_FP:
6528 case ISD::UINT_TO_FP:
6529 // [us]itofp(undef) = 0, because the result value is bounded.
6530 if (N1.isUndef())
6531 return getConstantFP(0.0, DL, VT);
6532 break;
6533 case ISD::SIGN_EXTEND:
6534 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6535 "Invalid SIGN_EXTEND!");
6536 assert(VT.isVector() == N1.getValueType().isVector() &&
6537 "SIGN_EXTEND result type type should be vector iff the operand "
6538 "type is vector!");
6539 if (N1.getValueType() == VT) return N1; // noop extension
6540 assert((!VT.isVector() || VT.getVectorElementCount() ==
6542 "Vector element count mismatch!");
6543 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6544 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6545 SDNodeFlags Flags;
6546 if (OpOpcode == ISD::ZERO_EXTEND)
6547 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6548 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6549 transferDbgValues(N1, NewVal);
6550 return NewVal;
6551 }
6552
6553 if (OpOpcode == ISD::POISON)
6554 return getPOISON(VT);
6555
6556 if (N1.isUndef())
6557 // sext(undef) = 0, because the top bits will all be the same.
6558 return getConstant(0, DL, VT);
6559
6560 // Skip unnecessary sext_inreg pattern:
6561 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6562 if (OpOpcode == ISD::TRUNCATE) {
6563 SDValue OpOp = N1.getOperand(0);
6564 if (OpOp.getValueType() == VT) {
6565 unsigned NumSignExtBits =
6567 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6568 transferDbgValues(N1, OpOp);
6569 return OpOp;
6570 }
6571 }
6572 }
6573 break;
6574 case ISD::ZERO_EXTEND:
6575 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6576 "Invalid ZERO_EXTEND!");
6577 assert(VT.isVector() == N1.getValueType().isVector() &&
6578 "ZERO_EXTEND result type type should be vector iff the operand "
6579 "type is vector!");
6580 if (N1.getValueType() == VT) return N1; // noop extension
6581 assert((!VT.isVector() || VT.getVectorElementCount() ==
6583 "Vector element count mismatch!");
6584 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6585 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6586 SDNodeFlags Flags;
6587 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6588 SDValue NewVal =
6589 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6590 transferDbgValues(N1, NewVal);
6591 return NewVal;
6592 }
6593
6594 if (OpOpcode == ISD::POISON)
6595 return getPOISON(VT);
6596
6597 if (N1.isUndef())
6598 // zext(undef) = 0, because the top bits will be zero.
6599 return getConstant(0, DL, VT);
6600
6601 // Skip unnecessary zext_inreg pattern:
6602 // (zext (trunc x)) -> x iff the upper bits are known zero.
6603 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6604 // use to recognise zext_inreg patterns.
6605 if (OpOpcode == ISD::TRUNCATE) {
6606 SDValue OpOp = N1.getOperand(0);
6607 if (OpOp.getValueType() == VT) {
6608 if (OpOp.getOpcode() != ISD::AND) {
6611 if (MaskedValueIsZero(OpOp, HiBits)) {
6612 transferDbgValues(N1, OpOp);
6613 return OpOp;
6614 }
6615 }
6616 }
6617 }
6618 break;
6619 case ISD::ANY_EXTEND:
6620 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6621 "Invalid ANY_EXTEND!");
6622 assert(VT.isVector() == N1.getValueType().isVector() &&
6623 "ANY_EXTEND result type type should be vector iff the operand "
6624 "type is vector!");
6625 if (N1.getValueType() == VT) return N1; // noop extension
6626 assert((!VT.isVector() || VT.getVectorElementCount() ==
6628 "Vector element count mismatch!");
6629 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6630
6631 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6632 OpOpcode == ISD::ANY_EXTEND) {
6633 SDNodeFlags Flags;
6634 if (OpOpcode == ISD::ZERO_EXTEND)
6635 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6636 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6637 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6638 }
6639 if (N1.isUndef())
6640 return getUNDEF(VT);
6641
6642 // (ext (trunc x)) -> x
6643 if (OpOpcode == ISD::TRUNCATE) {
6644 SDValue OpOp = N1.getOperand(0);
6645 if (OpOp.getValueType() == VT) {
6646 transferDbgValues(N1, OpOp);
6647 return OpOp;
6648 }
6649 }
6650 break;
6651 case ISD::TRUNCATE:
6652 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6653 "Invalid TRUNCATE!");
6654 assert(VT.isVector() == N1.getValueType().isVector() &&
6655 "TRUNCATE result type type should be vector iff the operand "
6656 "type is vector!");
6657 if (N1.getValueType() == VT) return N1; // noop truncate
6658 assert((!VT.isVector() || VT.getVectorElementCount() ==
6660 "Vector element count mismatch!");
6661 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6662 if (OpOpcode == ISD::TRUNCATE)
6663 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6664 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6665 OpOpcode == ISD::ANY_EXTEND) {
6666 // If the source is smaller than the dest, we still need an extend.
6668 VT.getScalarType())) {
6669 SDNodeFlags Flags;
6670 if (OpOpcode == ISD::ZERO_EXTEND)
6671 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6672 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6673 }
6674 if (N1.getOperand(0).getValueType().bitsGT(VT))
6675 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6676 return N1.getOperand(0);
6677 }
6678 if (N1.isUndef())
6679 return getUNDEF(VT);
6680 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6681 return getVScale(DL, VT,
6683 break;
6687 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6688 assert(N1.getValueType().bitsLE(VT) &&
6689 "The input must be the same size or smaller than the result.");
6692 "The destination vector type must have fewer lanes than the input.");
6693 break;
6694 case ISD::ABS:
6695 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6696 if (N1.isUndef())
6697 return getConstant(0, DL, VT);
6698 break;
6699 case ISD::BSWAP:
6700 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6701 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6702 "BSWAP types must be a multiple of 16 bits!");
6703 if (N1.isUndef())
6704 return getUNDEF(VT);
6705 // bswap(bswap(X)) -> X.
6706 if (OpOpcode == ISD::BSWAP)
6707 return N1.getOperand(0);
6708 break;
6709 case ISD::BITREVERSE:
6710 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6711 if (N1.isUndef())
6712 return getUNDEF(VT);
6713 break;
6714 case ISD::BITCAST:
6716 "Cannot BITCAST between types of different sizes!");
6717 if (VT == N1.getValueType()) return N1; // noop conversion.
6718 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6719 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6720 if (N1.isUndef())
6721 return getUNDEF(VT);
6722 break;
6724 assert(VT.isVector() && !N1.getValueType().isVector() &&
6725 (VT.getVectorElementType() == N1.getValueType() ||
6727 N1.getValueType().isInteger() &&
6729 "Illegal SCALAR_TO_VECTOR node!");
6730 if (N1.isUndef())
6731 return getUNDEF(VT);
6732 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6733 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6734 isa<ConstantSDNode>(N1.getOperand(1)) &&
6735 N1.getConstantOperandVal(1) == 0 &&
6736 N1.getOperand(0).getValueType() == VT)
6737 return N1.getOperand(0);
6738 break;
6739 case ISD::FNEG:
6740 // Negation of an unknown bag of bits is still completely undefined.
6741 if (N1.isUndef())
6742 return getUNDEF(VT);
6743
6744 if (OpOpcode == ISD::FNEG) // --X -> X
6745 return N1.getOperand(0);
6746 break;
6747 case ISD::FABS:
6748 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6749 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6750 break;
6751 case ISD::VSCALE:
6752 assert(VT == N1.getValueType() && "Unexpected VT!");
6753 break;
6754 case ISD::CTPOP:
6755 if (N1.getValueType().getScalarType() == MVT::i1)
6756 return N1;
6757 break;
6758 case ISD::CTLZ:
6759 case ISD::CTTZ:
6760 if (N1.getValueType().getScalarType() == MVT::i1)
6761 return getNOT(DL, N1, N1.getValueType());
6762 break;
6763 case ISD::VECREDUCE_ADD:
6764 if (N1.getValueType().getScalarType() == MVT::i1)
6765 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6766 break;
6769 if (N1.getValueType().getScalarType() == MVT::i1)
6770 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6771 break;
6774 if (N1.getValueType().getScalarType() == MVT::i1)
6775 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6776 break;
6777 case ISD::SPLAT_VECTOR:
6778 assert(VT.isVector() && "Wrong return type!");
6779 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6780 // that for now.
6782 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6784 N1.getValueType().isInteger() &&
6786 "Wrong operand type!");
6787 break;
6788 }
6789
6790 SDNode *N;
6791 SDVTList VTs = getVTList(VT);
6792 SDValue Ops[] = {N1};
6793 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6795 AddNodeIDNode(ID, Opcode, VTs, Ops);
6796 void *IP = nullptr;
6797 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6798 E->intersectFlagsWith(Flags);
6799 return SDValue(E, 0);
6800 }
6801
6802 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6803 N->setFlags(Flags);
6804 createOperands(N, Ops);
6805 CSEMap.InsertNode(N, IP);
6806 } else {
6807 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6808 createOperands(N, Ops);
6809 }
6810
6811 InsertNode(N);
6812 SDValue V = SDValue(N, 0);
6813 NewSDValueDbgMsg(V, "Creating new node: ", this);
6814 return V;
6815}
6816
6817static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6818 const APInt &C2) {
6819 switch (Opcode) {
6820 case ISD::ADD: return C1 + C2;
6821 case ISD::SUB: return C1 - C2;
6822 case ISD::MUL: return C1 * C2;
6823 case ISD::AND: return C1 & C2;
6824 case ISD::OR: return C1 | C2;
6825 case ISD::XOR: return C1 ^ C2;
6826 case ISD::SHL: return C1 << C2;
6827 case ISD::SRL: return C1.lshr(C2);
6828 case ISD::SRA: return C1.ashr(C2);
6829 case ISD::ROTL: return C1.rotl(C2);
6830 case ISD::ROTR: return C1.rotr(C2);
6831 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6832 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6833 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6834 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6835 case ISD::SADDSAT: return C1.sadd_sat(C2);
6836 case ISD::UADDSAT: return C1.uadd_sat(C2);
6837 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6838 case ISD::USUBSAT: return C1.usub_sat(C2);
6839 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6840 case ISD::USHLSAT: return C1.ushl_sat(C2);
6841 case ISD::UDIV:
6842 if (!C2.getBoolValue())
6843 break;
6844 return C1.udiv(C2);
6845 case ISD::UREM:
6846 if (!C2.getBoolValue())
6847 break;
6848 return C1.urem(C2);
6849 case ISD::SDIV:
6850 if (!C2.getBoolValue())
6851 break;
6852 return C1.sdiv(C2);
6853 case ISD::SREM:
6854 if (!C2.getBoolValue())
6855 break;
6856 return C1.srem(C2);
6857 case ISD::AVGFLOORS:
6858 return APIntOps::avgFloorS(C1, C2);
6859 case ISD::AVGFLOORU:
6860 return APIntOps::avgFloorU(C1, C2);
6861 case ISD::AVGCEILS:
6862 return APIntOps::avgCeilS(C1, C2);
6863 case ISD::AVGCEILU:
6864 return APIntOps::avgCeilU(C1, C2);
6865 case ISD::ABDS:
6866 return APIntOps::abds(C1, C2);
6867 case ISD::ABDU:
6868 return APIntOps::abdu(C1, C2);
6869 case ISD::MULHS:
6870 return APIntOps::mulhs(C1, C2);
6871 case ISD::MULHU:
6872 return APIntOps::mulhu(C1, C2);
6873 }
6874 return std::nullopt;
6875}
6876// Handle constant folding with UNDEF.
6877// TODO: Handle more cases.
6878static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6879 bool IsUndef1, const APInt &C2,
6880 bool IsUndef2) {
6881 if (!(IsUndef1 || IsUndef2))
6882 return FoldValue(Opcode, C1, C2);
6883
6884 // Fold and(x, undef) -> 0
6885 // Fold mul(x, undef) -> 0
6886 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6887 return APInt::getZero(C1.getBitWidth());
6888
6889 return std::nullopt;
6890}
6891
6893 const GlobalAddressSDNode *GA,
6894 const SDNode *N2) {
6895 if (GA->getOpcode() != ISD::GlobalAddress)
6896 return SDValue();
6897 if (!TLI->isOffsetFoldingLegal(GA))
6898 return SDValue();
6899 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6900 if (!C2)
6901 return SDValue();
6902 int64_t Offset = C2->getSExtValue();
6903 switch (Opcode) {
6904 case ISD::ADD:
6905 case ISD::PTRADD:
6906 break;
6907 case ISD::SUB: Offset = -uint64_t(Offset); break;
6908 default: return SDValue();
6909 }
6910 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6911 GA->getOffset() + uint64_t(Offset));
6912}
6913
6914bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6915 switch (Opcode) {
6916 case ISD::SDIV:
6917 case ISD::UDIV:
6918 case ISD::SREM:
6919 case ISD::UREM: {
6920 // If a divisor is zero/undef or any element of a divisor vector is
6921 // zero/undef, the whole op is undef.
6922 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6923 SDValue Divisor = Ops[1];
6924 if (Divisor.isUndef() || isNullConstant(Divisor))
6925 return true;
6926
6927 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6928 llvm::any_of(Divisor->op_values(),
6929 [](SDValue V) { return V.isUndef() ||
6930 isNullConstant(V); });
6931 // TODO: Handle signed overflow.
6932 }
6933 // TODO: Handle oversized shifts.
6934 default:
6935 return false;
6936 }
6937}
6938
6940 EVT VT, ArrayRef<SDValue> Ops,
6941 SDNodeFlags Flags) {
6942 // If the opcode is a target-specific ISD node, there's nothing we can
6943 // do here and the operand rules may not line up with the below, so
6944 // bail early.
6945 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6946 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6947 // foldCONCAT_VECTORS in getNode before this is called.
6948 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6949 return SDValue();
6950
6951 unsigned NumOps = Ops.size();
6952 if (NumOps == 0)
6953 return SDValue();
6954
6955 if (isUndef(Opcode, Ops))
6956 return getUNDEF(VT);
6957
6958 // Handle unary special cases.
6959 if (NumOps == 1) {
6960 SDValue N1 = Ops[0];
6961
6962 // Constant fold unary operations with an integer constant operand. Even
6963 // opaque constant will be folded, because the folding of unary operations
6964 // doesn't create new constants with different values. Nevertheless, the
6965 // opaque flag is preserved during folding to prevent future folding with
6966 // other constants.
6967 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6968 const APInt &Val = C->getAPIntValue();
6969 switch (Opcode) {
6970 case ISD::SIGN_EXTEND:
6971 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6972 C->isTargetOpcode(), C->isOpaque());
6973 case ISD::TRUNCATE:
6974 if (C->isOpaque())
6975 break;
6976 [[fallthrough]];
6977 case ISD::ZERO_EXTEND:
6978 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6979 C->isTargetOpcode(), C->isOpaque());
6980 case ISD::ANY_EXTEND:
6981 // Some targets like RISCV prefer to sign extend some types.
6982 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6983 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6984 C->isTargetOpcode(), C->isOpaque());
6985 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6986 C->isTargetOpcode(), C->isOpaque());
6987 case ISD::ABS:
6988 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6989 C->isOpaque());
6990 case ISD::BITREVERSE:
6991 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6992 C->isOpaque());
6993 case ISD::BSWAP:
6994 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6995 C->isOpaque());
6996 case ISD::CTPOP:
6997 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6998 C->isOpaque());
6999 case ISD::CTLZ:
7001 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7002 C->isOpaque());
7003 case ISD::CTTZ:
7005 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7006 C->isOpaque());
7007 case ISD::UINT_TO_FP:
7008 case ISD::SINT_TO_FP: {
7010 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7012 return getConstantFP(FPV, DL, VT);
7013 }
7014 case ISD::FP16_TO_FP:
7015 case ISD::BF16_TO_FP: {
7016 bool Ignored;
7017 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7018 : APFloat::BFloat(),
7019 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7020
7021 // This can return overflow, underflow, or inexact; we don't care.
7022 // FIXME need to be more flexible about rounding mode.
7024 &Ignored);
7025 return getConstantFP(FPV, DL, VT);
7026 }
7027 case ISD::STEP_VECTOR:
7028 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7029 return V;
7030 break;
7031 case ISD::BITCAST:
7032 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7033 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7034 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7035 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7036 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7037 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7038 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7039 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7040 break;
7041 }
7042 }
7043
7044 // Constant fold unary operations with a floating point constant operand.
7045 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7046 APFloat V = C->getValueAPF(); // make copy
7047 switch (Opcode) {
7048 case ISD::FNEG:
7049 V.changeSign();
7050 return getConstantFP(V, DL, VT);
7051 case ISD::FABS:
7052 V.clearSign();
7053 return getConstantFP(V, DL, VT);
7054 case ISD::FCEIL: {
7055 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7056 if (fs == APFloat::opOK || fs == APFloat::opInexact)
7057 return getConstantFP(V, DL, VT);
7058 return SDValue();
7059 }
7060 case ISD::FTRUNC: {
7061 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7062 if (fs == APFloat::opOK || fs == APFloat::opInexact)
7063 return getConstantFP(V, DL, VT);
7064 return SDValue();
7065 }
7066 case ISD::FFLOOR: {
7067 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7068 if (fs == APFloat::opOK || fs == APFloat::opInexact)
7069 return getConstantFP(V, DL, VT);
7070 return SDValue();
7071 }
7072 case ISD::FP_EXTEND: {
7073 bool ignored;
7074 // This can return overflow, underflow, or inexact; we don't care.
7075 // FIXME need to be more flexible about rounding mode.
7076 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7077 &ignored);
7078 return getConstantFP(V, DL, VT);
7079 }
7080 case ISD::FP_TO_SINT:
7081 case ISD::FP_TO_UINT: {
7082 bool ignored;
7083 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7084 // FIXME need to be more flexible about rounding mode.
7086 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7087 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7088 break;
7089 return getConstant(IntVal, DL, VT);
7090 }
7091 case ISD::FP_TO_FP16:
7092 case ISD::FP_TO_BF16: {
7093 bool Ignored;
7094 // This can return overflow, underflow, or inexact; we don't care.
7095 // FIXME need to be more flexible about rounding mode.
7096 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7097 : APFloat::BFloat(),
7099 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7100 }
7101 case ISD::BITCAST:
7102 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7103 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7104 VT);
7105 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7106 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7107 VT);
7108 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7109 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7110 VT);
7111 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7112 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7113 break;
7114 }
7115 }
7116
7117 // Early-out if we failed to constant fold a bitcast.
7118 if (Opcode == ISD::BITCAST)
7119 return SDValue();
7120 }
7121
7122 // Handle binops special cases.
7123 if (NumOps == 2) {
7124 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7125 return CFP;
7126
7127 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7128 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7129 if (C1->isOpaque() || C2->isOpaque())
7130 return SDValue();
7131
7132 std::optional<APInt> FoldAttempt =
7133 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7134 if (!FoldAttempt)
7135 return SDValue();
7136
7137 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7138 assert((!Folded || !VT.isVector()) &&
7139 "Can't fold vectors ops with scalar operands");
7140 return Folded;
7141 }
7142 }
7143
7144 // fold (add Sym, c) -> Sym+c
7145 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
7146 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7147 if (TLI->isCommutativeBinOp(Opcode))
7148 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
7149 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7150
7151 // fold (sext_in_reg c1) -> c2
7152 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7153 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7154
7155 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7156 unsigned FromBits = EVT.getScalarSizeInBits();
7157 Val <<= Val.getBitWidth() - FromBits;
7158 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7159 return getConstant(Val, DL, ConstantVT);
7160 };
7161
7162 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7163 const APInt &Val = C1->getAPIntValue();
7164 return SignExtendInReg(Val, VT);
7165 }
7166
7168 SmallVector<SDValue, 8> ScalarOps;
7169 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7170 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7171 SDValue Op = Ops[0].getOperand(I);
7172 if (Op.isUndef()) {
7173 ScalarOps.push_back(getUNDEF(OpVT));
7174 continue;
7175 }
7176 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7177 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7178 }
7179 return getBuildVector(VT, DL, ScalarOps);
7180 }
7181
7182 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7183 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7184 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7185 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7186 Ops[0].getOperand(0).getValueType()));
7187 }
7188 }
7189
7190 // Handle fshl/fshr special cases.
7191 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7192 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7193 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7194 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7195
7196 if (C1 && C2 && C3) {
7197 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7198 return SDValue();
7199 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7200 &V3 = C3->getAPIntValue();
7201
7202 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7203 : APIntOps::fshr(V1, V2, V3);
7204 return getConstant(FoldedVal, DL, VT);
7205 }
7206 }
7207
7208 // Handle fma/fmad special cases.
7209 if (Opcode == ISD::FMA || Opcode == ISD::FMAD) {
7210 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7211 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7212 Ops[2].getValueType() == VT && "FMA types must match!");
7213 ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(Ops[0]);
7214 ConstantFPSDNode *C2 = dyn_cast<ConstantFPSDNode>(Ops[1]);
7215 ConstantFPSDNode *C3 = dyn_cast<ConstantFPSDNode>(Ops[2]);
7216 if (C1 && C2 && C3) {
7217 APFloat V1 = C1->getValueAPF();
7218 const APFloat &V2 = C2->getValueAPF();
7219 const APFloat &V3 = C3->getValueAPF();
7220 if (Opcode == ISD::FMAD) {
7223 } else
7225 return getConstantFP(V1, DL, VT);
7226 }
7227 }
7228
7229 // This is for vector folding only from here on.
7230 if (!VT.isVector())
7231 return SDValue();
7232
7233 ElementCount NumElts = VT.getVectorElementCount();
7234
7235 // See if we can fold through any bitcasted integer ops.
7236 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7237 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7238 (Ops[0].getOpcode() == ISD::BITCAST ||
7239 Ops[1].getOpcode() == ISD::BITCAST)) {
7240 SDValue N1 = peekThroughBitcasts(Ops[0]);
7241 SDValue N2 = peekThroughBitcasts(Ops[1]);
7242 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7243 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7244 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7245 N2.getValueType().isInteger()) {
7246 bool IsLE = getDataLayout().isLittleEndian();
7247 unsigned EltBits = VT.getScalarSizeInBits();
7248 SmallVector<APInt> RawBits1, RawBits2;
7249 BitVector UndefElts1, UndefElts2;
7250 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7251 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7252 SmallVector<APInt> RawBits;
7253 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7254 std::optional<APInt> Fold = FoldValueWithUndef(
7255 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7256 if (!Fold)
7257 break;
7258 RawBits.push_back(*Fold);
7259 }
7260 if (RawBits.size() == NumElts.getFixedValue()) {
7261 // We have constant folded, but we might need to cast this again back
7262 // to the original (possibly legalized) type.
7263 EVT BVVT, BVEltVT;
7264 if (N1.getValueType() == VT) {
7265 BVVT = N1.getValueType();
7266 BVEltVT = BV1->getOperand(0).getValueType();
7267 } else {
7268 BVVT = N2.getValueType();
7269 BVEltVT = BV2->getOperand(0).getValueType();
7270 }
7271 unsigned BVEltBits = BVEltVT.getSizeInBits();
7272 SmallVector<APInt> DstBits;
7273 BitVector DstUndefs;
7275 DstBits, RawBits, DstUndefs,
7276 BitVector(RawBits.size(), false));
7277 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7278 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7279 if (DstUndefs[I])
7280 continue;
7281 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7282 }
7283 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7284 }
7285 }
7286 }
7287 }
7288
7289 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7290 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7291 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7292 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7293 APInt RHSVal;
7294 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7295 APInt NewStep = Opcode == ISD::MUL
7296 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7297 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7298 return getStepVector(DL, VT, NewStep);
7299 }
7300 }
7301
7302 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7303 return !Op.getValueType().isVector() ||
7304 Op.getValueType().getVectorElementCount() == NumElts;
7305 };
7306
7307 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7308 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7309 Op.getOpcode() == ISD::BUILD_VECTOR ||
7310 Op.getOpcode() == ISD::SPLAT_VECTOR;
7311 };
7312
7313 // All operands must be vector types with the same number of elements as
7314 // the result type and must be either UNDEF or a build/splat vector
7315 // or UNDEF scalars.
7316 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7317 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7318 return SDValue();
7319
7320 // If we are comparing vectors, then the result needs to be a i1 boolean that
7321 // is then extended back to the legal result type depending on how booleans
7322 // are represented.
7323 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7324 ISD::NodeType ExtendCode =
7325 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7328
7329 // Find legal integer scalar type for constant promotion and
7330 // ensure that its scalar size is at least as large as source.
7331 EVT LegalSVT = VT.getScalarType();
7332 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7333 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7334 if (LegalSVT.bitsLT(VT.getScalarType()))
7335 return SDValue();
7336 }
7337
7338 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7339 // only have one operand to check. For fixed-length vector types we may have
7340 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7341 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7342
7343 // Constant fold each scalar lane separately.
7344 SmallVector<SDValue, 4> ScalarResults;
7345 for (unsigned I = 0; I != NumVectorElts; I++) {
7346 SmallVector<SDValue, 4> ScalarOps;
7347 for (SDValue Op : Ops) {
7348 EVT InSVT = Op.getValueType().getScalarType();
7349 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7350 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7351 if (Op.isUndef())
7352 ScalarOps.push_back(getUNDEF(InSVT));
7353 else
7354 ScalarOps.push_back(Op);
7355 continue;
7356 }
7357
7358 SDValue ScalarOp =
7359 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7360 EVT ScalarVT = ScalarOp.getValueType();
7361
7362 // Build vector (integer) scalar operands may need implicit
7363 // truncation - do this before constant folding.
7364 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7365 // Don't create illegally-typed nodes unless they're constants or undef
7366 // - if we fail to constant fold we can't guarantee the (dead) nodes
7367 // we're creating will be cleaned up before being visited for
7368 // legalization.
7369 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7370 !isa<ConstantSDNode>(ScalarOp) &&
7371 TLI->getTypeAction(*getContext(), InSVT) !=
7373 return SDValue();
7374 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7375 }
7376
7377 ScalarOps.push_back(ScalarOp);
7378 }
7379
7380 // Constant fold the scalar operands.
7381 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7382
7383 // Scalar folding only succeeded if the result is a constant or UNDEF.
7384 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7385 ScalarResult.getOpcode() != ISD::ConstantFP)
7386 return SDValue();
7387
7388 // Legalize the (integer) scalar constant if necessary. We only do
7389 // this once we know the folding succeeded, since otherwise we would
7390 // get a node with illegal type which has a user.
7391 if (LegalSVT != SVT)
7392 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7393
7394 ScalarResults.push_back(ScalarResult);
7395 }
7396
7397 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7398 : getBuildVector(VT, DL, ScalarResults);
7399 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7400 return V;
7401}
7402
7404 EVT VT, ArrayRef<SDValue> Ops) {
7405 // TODO: Add support for unary/ternary fp opcodes.
7406 if (Ops.size() != 2)
7407 return SDValue();
7408
7409 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7410 // should. That will require dealing with a potentially non-default
7411 // rounding mode, checking the "opStatus" return value from the APFloat
7412 // math calculations, and possibly other variations.
7413 SDValue N1 = Ops[0];
7414 SDValue N2 = Ops[1];
7415 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7416 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7417 if (N1CFP && N2CFP) {
7418 APFloat C1 = N1CFP->getValueAPF(); // make copy
7419 const APFloat &C2 = N2CFP->getValueAPF();
7420 switch (Opcode) {
7421 case ISD::FADD:
7423 return getConstantFP(C1, DL, VT);
7424 case ISD::FSUB:
7426 return getConstantFP(C1, DL, VT);
7427 case ISD::FMUL:
7429 return getConstantFP(C1, DL, VT);
7430 case ISD::FDIV:
7432 return getConstantFP(C1, DL, VT);
7433 case ISD::FREM:
7434 C1.mod(C2);
7435 return getConstantFP(C1, DL, VT);
7436 case ISD::FCOPYSIGN:
7437 C1.copySign(C2);
7438 return getConstantFP(C1, DL, VT);
7439 case ISD::FMINNUM:
7440 return getConstantFP(minnum(C1, C2), DL, VT);
7441 case ISD::FMAXNUM:
7442 return getConstantFP(maxnum(C1, C2), DL, VT);
7443 case ISD::FMINIMUM:
7444 return getConstantFP(minimum(C1, C2), DL, VT);
7445 case ISD::FMAXIMUM:
7446 return getConstantFP(maximum(C1, C2), DL, VT);
7447 case ISD::FMINIMUMNUM:
7448 return getConstantFP(minimumnum(C1, C2), DL, VT);
7449 case ISD::FMAXIMUMNUM:
7450 return getConstantFP(maximumnum(C1, C2), DL, VT);
7451 default: break;
7452 }
7453 }
7454 if (N1CFP && Opcode == ISD::FP_ROUND) {
7455 APFloat C1 = N1CFP->getValueAPF(); // make copy
7456 bool Unused;
7457 // This can return overflow, underflow, or inexact; we don't care.
7458 // FIXME need to be more flexible about rounding mode.
7460 &Unused);
7461 return getConstantFP(C1, DL, VT);
7462 }
7463
7464 switch (Opcode) {
7465 case ISD::FSUB:
7466 // -0.0 - undef --> undef (consistent with "fneg undef")
7467 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7468 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7469 return getUNDEF(VT);
7470 [[fallthrough]];
7471
7472 case ISD::FADD:
7473 case ISD::FMUL:
7474 case ISD::FDIV:
7475 case ISD::FREM:
7476 // If both operands are undef, the result is undef. If 1 operand is undef,
7477 // the result is NaN. This should match the behavior of the IR optimizer.
7478 if (N1.isUndef() && N2.isUndef())
7479 return getUNDEF(VT);
7480 if (N1.isUndef() || N2.isUndef())
7482 }
7483 return SDValue();
7484}
7485
7487 const SDLoc &DL, EVT DstEltVT) {
7488 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7489
7490 // If this is already the right type, we're done.
7491 if (SrcEltVT == DstEltVT)
7492 return SDValue(BV, 0);
7493
7494 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7495 unsigned DstBitSize = DstEltVT.getSizeInBits();
7496
7497 // If this is a conversion of N elements of one type to N elements of another
7498 // type, convert each element. This handles FP<->INT cases.
7499 if (SrcBitSize == DstBitSize) {
7501 for (SDValue Op : BV->op_values()) {
7502 // If the vector element type is not legal, the BUILD_VECTOR operands
7503 // are promoted and implicitly truncated. Make that explicit here.
7504 if (Op.getValueType() != SrcEltVT)
7505 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7506 Ops.push_back(getBitcast(DstEltVT, Op));
7507 }
7508 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7510 return getBuildVector(VT, DL, Ops);
7511 }
7512
7513 // Otherwise, we're growing or shrinking the elements. To avoid having to
7514 // handle annoying details of growing/shrinking FP values, we convert them to
7515 // int first.
7516 if (SrcEltVT.isFloatingPoint()) {
7517 // Convert the input float vector to a int vector where the elements are the
7518 // same sizes.
7519 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7520 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7521 return FoldConstantBuildVector(cast<BuildVectorSDNode>(Tmp), DL,
7522 DstEltVT);
7523 return SDValue();
7524 }
7525
7526 // Now we know the input is an integer vector. If the output is a FP type,
7527 // convert to integer first, then to FP of the right size.
7528 if (DstEltVT.isFloatingPoint()) {
7529 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7530 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7531 return FoldConstantBuildVector(cast<BuildVectorSDNode>(Tmp), DL,
7532 DstEltVT);
7533 return SDValue();
7534 }
7535
7536 // Okay, we know the src/dst types are both integers of differing types.
7537 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7538
7539 // Extract the constant raw bit data.
7540 BitVector UndefElements;
7541 SmallVector<APInt> RawBits;
7542 bool IsLE = getDataLayout().isLittleEndian();
7543 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7544 return SDValue();
7545
7547 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7548 if (UndefElements[I])
7549 Ops.push_back(getUNDEF(DstEltVT));
7550 else
7551 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7552 }
7553
7554 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7555 return getBuildVector(VT, DL, Ops);
7556}
7557
7559 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7560
7561 // There's no need to assert on a byte-aligned pointer. All pointers are at
7562 // least byte aligned.
7563 if (A == Align(1))
7564 return Val;
7565
7566 SDVTList VTs = getVTList(Val.getValueType());
7568 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7569 ID.AddInteger(A.value());
7570
7571 void *IP = nullptr;
7572 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7573 return SDValue(E, 0);
7574
7575 auto *N =
7576 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7577 createOperands(N, {Val});
7578
7579 CSEMap.InsertNode(N, IP);
7580 InsertNode(N);
7581
7582 SDValue V(N, 0);
7583 NewSDValueDbgMsg(V, "Creating new node: ", this);
7584 return V;
7585}
7586
7587SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7588 SDValue N1, SDValue N2) {
7589 SDNodeFlags Flags;
7590 if (Inserter)
7591 Flags = Inserter->getFlags();
7592 return getNode(Opcode, DL, VT, N1, N2, Flags);
7593}
7594
7596 SDValue &N2) const {
7597 if (!TLI->isCommutativeBinOp(Opcode))
7598 return;
7599
7600 // Canonicalize:
7601 // binop(const, nonconst) -> binop(nonconst, const)
7604 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7605 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7606 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7607 std::swap(N1, N2);
7608
7609 // Canonicalize:
7610 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7611 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7613 std::swap(N1, N2);
7614}
7615
7616SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7617 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7619 N2.getOpcode() != ISD::DELETED_NODE &&
7620 "Operand is DELETED_NODE!");
7621
7622 canonicalizeCommutativeBinop(Opcode, N1, N2);
7623
7624 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7625 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7626
7627 // Don't allow undefs in vector splats - we might be returning N2 when folding
7628 // to zero etc.
7629 ConstantSDNode *N2CV =
7630 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7631
7632 switch (Opcode) {
7633 default: break;
7634 case ISD::TokenFactor:
7635 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7636 N2.getValueType() == MVT::Other && "Invalid token factor!");
7637 // Fold trivial token factors.
7638 if (N1.getOpcode() == ISD::EntryToken) return N2;
7639 if (N2.getOpcode() == ISD::EntryToken) return N1;
7640 if (N1 == N2) return N1;
7641 break;
7642 case ISD::BUILD_VECTOR: {
7643 // Attempt to simplify BUILD_VECTOR.
7644 SDValue Ops[] = {N1, N2};
7645 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7646 return V;
7647 break;
7648 }
7649 case ISD::CONCAT_VECTORS: {
7650 SDValue Ops[] = {N1, N2};
7651 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7652 return V;
7653 break;
7654 }
7655 case ISD::AND:
7656 assert(VT.isInteger() && "This operator does not apply to FP types!");
7657 assert(N1.getValueType() == N2.getValueType() &&
7658 N1.getValueType() == VT && "Binary operator types must match!");
7659 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7660 // worth handling here.
7661 if (N2CV && N2CV->isZero())
7662 return N2;
7663 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7664 return N1;
7665 break;
7666 case ISD::OR:
7667 case ISD::XOR:
7668 case ISD::ADD:
7669 case ISD::PTRADD:
7670 case ISD::SUB:
7671 assert(VT.isInteger() && "This operator does not apply to FP types!");
7672 assert(N1.getValueType() == N2.getValueType() &&
7673 N1.getValueType() == VT && "Binary operator types must match!");
7674 // The equal operand types requirement is unnecessarily strong for PTRADD.
7675 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7676 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7677 // logic everywhere where PTRADDs may be folded or combined to properly
7678 // support them. If/when we introduce pointer types to the SDAG, we will
7679 // need to relax this constraint.
7680
7681 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7682 // it's worth handling here.
7683 if (N2CV && N2CV->isZero())
7684 return N1;
7685 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7686 VT.getScalarType() == MVT::i1)
7687 return getNode(ISD::XOR, DL, VT, N1, N2);
7688 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7689 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7690 N2.getOpcode() == ISD::VSCALE) {
7691 const APInt &C1 = N1->getConstantOperandAPInt(0);
7692 const APInt &C2 = N2->getConstantOperandAPInt(0);
7693 return getVScale(DL, VT, C1 + C2);
7694 }
7695 break;
7696 case ISD::MUL:
7697 assert(VT.isInteger() && "This operator does not apply to FP types!");
7698 assert(N1.getValueType() == N2.getValueType() &&
7699 N1.getValueType() == VT && "Binary operator types must match!");
7700 if (VT.getScalarType() == MVT::i1)
7701 return getNode(ISD::AND, DL, VT, N1, N2);
7702 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7703 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7704 const APInt &N2CImm = N2C->getAPIntValue();
7705 return getVScale(DL, VT, MulImm * N2CImm);
7706 }
7707 break;
7708 case ISD::UDIV:
7709 case ISD::UREM:
7710 case ISD::MULHU:
7711 case ISD::MULHS:
7712 case ISD::SDIV:
7713 case ISD::SREM:
7714 case ISD::SADDSAT:
7715 case ISD::SSUBSAT:
7716 case ISD::UADDSAT:
7717 case ISD::USUBSAT:
7718 assert(VT.isInteger() && "This operator does not apply to FP types!");
7719 assert(N1.getValueType() == N2.getValueType() &&
7720 N1.getValueType() == VT && "Binary operator types must match!");
7721 if (VT.getScalarType() == MVT::i1) {
7722 // fold (add_sat x, y) -> (or x, y) for bool types.
7723 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7724 return getNode(ISD::OR, DL, VT, N1, N2);
7725 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7726 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7727 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7728 }
7729 break;
7730 case ISD::SCMP:
7731 case ISD::UCMP:
7732 assert(N1.getValueType() == N2.getValueType() &&
7733 "Types of operands of UCMP/SCMP must match");
7734 assert(N1.getValueType().isVector() == VT.isVector() &&
7735 "Operands and return type of must both be scalars or vectors");
7736 if (VT.isVector())
7739 "Result and operands must have the same number of elements");
7740 break;
7741 case ISD::AVGFLOORS:
7742 case ISD::AVGFLOORU:
7743 case ISD::AVGCEILS:
7744 case ISD::AVGCEILU:
7745 assert(VT.isInteger() && "This operator does not apply to FP types!");
7746 assert(N1.getValueType() == N2.getValueType() &&
7747 N1.getValueType() == VT && "Binary operator types must match!");
7748 break;
7749 case ISD::ABDS:
7750 case ISD::ABDU:
7751 assert(VT.isInteger() && "This operator does not apply to FP types!");
7752 assert(N1.getValueType() == N2.getValueType() &&
7753 N1.getValueType() == VT && "Binary operator types must match!");
7754 if (VT.getScalarType() == MVT::i1)
7755 return getNode(ISD::XOR, DL, VT, N1, N2);
7756 break;
7757 case ISD::SMIN:
7758 case ISD::UMAX:
7759 assert(VT.isInteger() && "This operator does not apply to FP types!");
7760 assert(N1.getValueType() == N2.getValueType() &&
7761 N1.getValueType() == VT && "Binary operator types must match!");
7762 if (VT.getScalarType() == MVT::i1)
7763 return getNode(ISD::OR, DL, VT, N1, N2);
7764 break;
7765 case ISD::SMAX:
7766 case ISD::UMIN:
7767 assert(VT.isInteger() && "This operator does not apply to FP types!");
7768 assert(N1.getValueType() == N2.getValueType() &&
7769 N1.getValueType() == VT && "Binary operator types must match!");
7770 if (VT.getScalarType() == MVT::i1)
7771 return getNode(ISD::AND, DL, VT, N1, N2);
7772 break;
7773 case ISD::FADD:
7774 case ISD::FSUB:
7775 case ISD::FMUL:
7776 case ISD::FDIV:
7777 case ISD::FREM:
7778 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7779 assert(N1.getValueType() == N2.getValueType() &&
7780 N1.getValueType() == VT && "Binary operator types must match!");
7781 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7782 return V;
7783 break;
7784 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7785 assert(N1.getValueType() == VT &&
7788 "Invalid FCOPYSIGN!");
7789 break;
7790 case ISD::SHL:
7791 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7792 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7793 const APInt &ShiftImm = N2C->getAPIntValue();
7794 return getVScale(DL, VT, MulImm << ShiftImm);
7795 }
7796 [[fallthrough]];
7797 case ISD::SRA:
7798 case ISD::SRL:
7799 if (SDValue V = simplifyShift(N1, N2))
7800 return V;
7801 [[fallthrough]];
7802 case ISD::ROTL:
7803 case ISD::ROTR:
7804 assert(VT == N1.getValueType() &&
7805 "Shift operators return type must be the same as their first arg");
7806 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7807 "Shifts only work on integers");
7808 assert((!VT.isVector() || VT == N2.getValueType()) &&
7809 "Vector shift amounts must be in the same as their first arg");
7810 // Verify that the shift amount VT is big enough to hold valid shift
7811 // amounts. This catches things like trying to shift an i1024 value by an
7812 // i8, which is easy to fall into in generic code that uses
7813 // TLI.getShiftAmount().
7816 "Invalid use of small shift amount with oversized value!");
7817
7818 // Always fold shifts of i1 values so the code generator doesn't need to
7819 // handle them. Since we know the size of the shift has to be less than the
7820 // size of the value, the shift/rotate count is guaranteed to be zero.
7821 if (VT == MVT::i1)
7822 return N1;
7823 if (N2CV && N2CV->isZero())
7824 return N1;
7825 break;
7826 case ISD::FP_ROUND:
7828 VT.bitsLE(N1.getValueType()) && N2C &&
7829 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7830 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7831 if (N1.getValueType() == VT) return N1; // noop conversion.
7832 break;
7833 case ISD::AssertNoFPClass: {
7835 "AssertNoFPClass is used for a non-floating type");
7836 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7837 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7838 assert(llvm::to_underlying(NoFPClass) <=
7839 BitmaskEnumDetail::Mask<FPClassTest>() &&
7840 "FPClassTest value too large");
7841 (void)NoFPClass;
7842 break;
7843 }
7844 case ISD::AssertSext:
7845 case ISD::AssertZext: {
7846 EVT EVT = cast<VTSDNode>(N2)->getVT();
7847 assert(VT == N1.getValueType() && "Not an inreg extend!");
7848 assert(VT.isInteger() && EVT.isInteger() &&
7849 "Cannot *_EXTEND_INREG FP types");
7850 assert(!EVT.isVector() &&
7851 "AssertSExt/AssertZExt type should be the vector element type "
7852 "rather than the vector type!");
7853 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7854 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7855 break;
7856 }
7858 EVT EVT = cast<VTSDNode>(N2)->getVT();
7859 assert(VT == N1.getValueType() && "Not an inreg extend!");
7860 assert(VT.isInteger() && EVT.isInteger() &&
7861 "Cannot *_EXTEND_INREG FP types");
7862 assert(EVT.isVector() == VT.isVector() &&
7863 "SIGN_EXTEND_INREG type should be vector iff the operand "
7864 "type is vector!");
7865 assert((!EVT.isVector() ||
7867 "Vector element counts must match in SIGN_EXTEND_INREG");
7868 assert(EVT.bitsLE(VT) && "Not extending!");
7869 if (EVT == VT) return N1; // Not actually extending
7870 break;
7871 }
7873 case ISD::FP_TO_UINT_SAT: {
7874 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7875 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7876 assert(N1.getValueType().isVector() == VT.isVector() &&
7877 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7878 "vector!");
7879 assert((!VT.isVector() || VT.getVectorElementCount() ==
7881 "Vector element counts must match in FP_TO_*INT_SAT");
7882 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7883 "Type to saturate to must be a scalar.");
7884 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7885 "Not extending!");
7886 break;
7887 }
7890 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7891 element type of the vector.");
7892
7893 // Extract from an undefined value or using an undefined index is undefined.
7894 if (N1.isUndef() || N2.isUndef())
7895 return getUNDEF(VT);
7896
7897 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7898 // vectors. For scalable vectors we will provide appropriate support for
7899 // dealing with arbitrary indices.
7900 if (N2C && N1.getValueType().isFixedLengthVector() &&
7901 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7902 return getUNDEF(VT);
7903
7904 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7905 // expanding copies of large vectors from registers. This only works for
7906 // fixed length vectors, since we need to know the exact number of
7907 // elements.
7908 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7910 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7911 return getExtractVectorElt(DL, VT,
7912 N1.getOperand(N2C->getZExtValue() / Factor),
7913 N2C->getZExtValue() % Factor);
7914 }
7915
7916 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7917 // lowering is expanding large vector constants.
7918 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7919 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7922 "BUILD_VECTOR used for scalable vectors");
7923 unsigned Index =
7924 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7925 SDValue Elt = N1.getOperand(Index);
7926
7927 if (VT != Elt.getValueType())
7928 // If the vector element type is not legal, the BUILD_VECTOR operands
7929 // are promoted and implicitly truncated, and the result implicitly
7930 // extended. Make that explicit here.
7931 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7932
7933 return Elt;
7934 }
7935
7936 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7937 // operations are lowered to scalars.
7938 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7939 // If the indices are the same, return the inserted element else
7940 // if the indices are known different, extract the element from
7941 // the original vector.
7942 SDValue N1Op2 = N1.getOperand(2);
7943 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7944
7945 if (N1Op2C && N2C) {
7946 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7947 if (VT == N1.getOperand(1).getValueType())
7948 return N1.getOperand(1);
7949 if (VT.isFloatingPoint()) {
7951 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7952 }
7953 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7954 }
7955 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7956 }
7957 }
7958
7959 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7960 // when vector types are scalarized and v1iX is legal.
7961 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7962 // Here we are completely ignoring the extract element index (N2),
7963 // which is fine for fixed width vectors, since any index other than 0
7964 // is undefined anyway. However, this cannot be ignored for scalable
7965 // vectors - in theory we could support this, but we don't want to do this
7966 // without a profitability check.
7967 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7969 N1.getValueType().getVectorNumElements() == 1) {
7970 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7971 N1.getOperand(1));
7972 }
7973 break;
7975 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7976 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7977 (N1.getValueType().isInteger() == VT.isInteger()) &&
7978 N1.getValueType() != VT &&
7979 "Wrong types for EXTRACT_ELEMENT!");
7980
7981 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7982 // 64-bit integers into 32-bit parts. Instead of building the extract of
7983 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7984 if (N1.getOpcode() == ISD::BUILD_PAIR)
7985 return N1.getOperand(N2C->getZExtValue());
7986
7987 // EXTRACT_ELEMENT of a constant int is also very common.
7988 if (N1C) {
7989 unsigned ElementSize = VT.getSizeInBits();
7990 unsigned Shift = ElementSize * N2C->getZExtValue();
7991 const APInt &Val = N1C->getAPIntValue();
7992 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7993 }
7994 break;
7996 EVT N1VT = N1.getValueType();
7997 assert(VT.isVector() && N1VT.isVector() &&
7998 "Extract subvector VTs must be vectors!");
8000 "Extract subvector VTs must have the same element type!");
8001 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8002 "Cannot extract a scalable vector from a fixed length vector!");
8003 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8005 "Extract subvector must be from larger vector to smaller vector!");
8006 assert(N2C && "Extract subvector index must be a constant");
8007 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8008 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8009 N1VT.getVectorMinNumElements()) &&
8010 "Extract subvector overflow!");
8011 assert(N2C->getAPIntValue().getBitWidth() ==
8013 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8014 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8015 "Extract index is not a multiple of the output vector length");
8016
8017 // Trivial extraction.
8018 if (VT == N1VT)
8019 return N1;
8020
8021 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8022 if (N1.isUndef())
8023 return getUNDEF(VT);
8024
8025 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8026 // the concat have the same type as the extract.
8027 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8028 VT == N1.getOperand(0).getValueType()) {
8029 unsigned Factor = VT.getVectorMinNumElements();
8030 return N1.getOperand(N2C->getZExtValue() / Factor);
8031 }
8032
8033 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8034 // during shuffle legalization.
8035 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8036 VT == N1.getOperand(1).getValueType())
8037 return N1.getOperand(1);
8038 break;
8039 }
8040 }
8041
8042 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8043 switch (Opcode) {
8044 case ISD::XOR:
8045 case ISD::ADD:
8046 case ISD::PTRADD:
8047 case ISD::SUB:
8049 case ISD::UDIV:
8050 case ISD::SDIV:
8051 case ISD::UREM:
8052 case ISD::SREM:
8053 case ISD::MUL:
8054 case ISD::AND:
8055 case ISD::SSUBSAT:
8056 case ISD::USUBSAT:
8057 case ISD::UMIN:
8058 case ISD::OR:
8059 case ISD::SADDSAT:
8060 case ISD::UADDSAT:
8061 case ISD::UMAX:
8062 case ISD::SMAX:
8063 case ISD::SMIN:
8064 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8065 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8066 }
8067 }
8068
8069 // Canonicalize an UNDEF to the RHS, even over a constant.
8070 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8071 if (TLI->isCommutativeBinOp(Opcode)) {
8072 std::swap(N1, N2);
8073 } else {
8074 switch (Opcode) {
8075 case ISD::PTRADD:
8076 case ISD::SUB:
8077 // fold op(undef, non_undef_arg2) -> undef.
8078 return N1;
8080 case ISD::UDIV:
8081 case ISD::SDIV:
8082 case ISD::UREM:
8083 case ISD::SREM:
8084 case ISD::SSUBSAT:
8085 case ISD::USUBSAT:
8086 // fold op(undef, non_undef_arg2) -> 0.
8087 return getConstant(0, DL, VT);
8088 }
8089 }
8090 }
8091
8092 // Fold a bunch of operators when the RHS is undef.
8093 if (N2.getOpcode() == ISD::UNDEF) {
8094 switch (Opcode) {
8095 case ISD::XOR:
8096 if (N1.getOpcode() == ISD::UNDEF)
8097 // Handle undef ^ undef -> 0 special case. This is a common
8098 // idiom (misuse).
8099 return getConstant(0, DL, VT);
8100 [[fallthrough]];
8101 case ISD::ADD:
8102 case ISD::PTRADD:
8103 case ISD::SUB:
8104 // fold op(arg1, undef) -> undef.
8105 return N2;
8106 case ISD::UDIV:
8107 case ISD::SDIV:
8108 case ISD::UREM:
8109 case ISD::SREM:
8110 // fold op(arg1, undef) -> poison.
8111 return getPOISON(VT);
8112 case ISD::MUL:
8113 case ISD::AND:
8114 case ISD::SSUBSAT:
8115 case ISD::USUBSAT:
8116 case ISD::UMIN:
8117 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8118 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8119 case ISD::OR:
8120 case ISD::SADDSAT:
8121 case ISD::UADDSAT:
8122 case ISD::UMAX:
8123 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8124 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8125 case ISD::SMAX:
8126 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8127 return N1.getOpcode() == ISD::UNDEF
8128 ? N2
8129 : getConstant(
8131 VT);
8132 case ISD::SMIN:
8133 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8134 return N1.getOpcode() == ISD::UNDEF
8135 ? N2
8136 : getConstant(
8138 VT);
8139 }
8140 }
8141
8142 // Perform trivial constant folding.
8143 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8144 return SV;
8145
8146 // Memoize this node if possible.
8147 SDNode *N;
8148 SDVTList VTs = getVTList(VT);
8149 SDValue Ops[] = {N1, N2};
8150 if (VT != MVT::Glue) {
8152 AddNodeIDNode(ID, Opcode, VTs, Ops);
8153 void *IP = nullptr;
8154 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8155 E->intersectFlagsWith(Flags);
8156 return SDValue(E, 0);
8157 }
8158
8159 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8160 N->setFlags(Flags);
8161 createOperands(N, Ops);
8162 CSEMap.InsertNode(N, IP);
8163 } else {
8164 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8165 createOperands(N, Ops);
8166 }
8167
8168 InsertNode(N);
8169 SDValue V = SDValue(N, 0);
8170 NewSDValueDbgMsg(V, "Creating new node: ", this);
8171 return V;
8172}
8173
8174SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8175 SDValue N1, SDValue N2, SDValue N3) {
8176 SDNodeFlags Flags;
8177 if (Inserter)
8178 Flags = Inserter->getFlags();
8179 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8180}
8181
8182SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8183 SDValue N1, SDValue N2, SDValue N3,
8184 const SDNodeFlags Flags) {
8186 N2.getOpcode() != ISD::DELETED_NODE &&
8187 N3.getOpcode() != ISD::DELETED_NODE &&
8188 "Operand is DELETED_NODE!");
8189 // Perform various simplifications.
8190 switch (Opcode) {
8191 case ISD::BUILD_VECTOR: {
8192 // Attempt to simplify BUILD_VECTOR.
8193 SDValue Ops[] = {N1, N2, N3};
8194 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8195 return V;
8196 break;
8197 }
8198 case ISD::CONCAT_VECTORS: {
8199 SDValue Ops[] = {N1, N2, N3};
8200 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8201 return V;
8202 break;
8203 }
8204 case ISD::SETCC: {
8205 assert(VT.isInteger() && "SETCC result type must be an integer!");
8206 assert(N1.getValueType() == N2.getValueType() &&
8207 "SETCC operands must have the same type!");
8208 assert(VT.isVector() == N1.getValueType().isVector() &&
8209 "SETCC type should be vector iff the operand type is vector!");
8210 assert((!VT.isVector() || VT.getVectorElementCount() ==
8212 "SETCC vector element counts must match!");
8213 // Use FoldSetCC to simplify SETCC's.
8214 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8215 return V;
8216 break;
8217 }
8218 case ISD::SELECT:
8219 case ISD::VSELECT:
8220 if (SDValue V = simplifySelect(N1, N2, N3))
8221 return V;
8222 break;
8224 llvm_unreachable("should use getVectorShuffle constructor!");
8225 case ISD::VECTOR_SPLICE: {
8226 if (cast<ConstantSDNode>(N3)->isZero())
8227 return N1;
8228 break;
8229 }
8231 assert(VT.isVector() && VT == N1.getValueType() &&
8232 "INSERT_VECTOR_ELT vector type mismatch");
8234 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8235 assert((!VT.isFloatingPoint() ||
8236 VT.getVectorElementType() == N2.getValueType()) &&
8237 "INSERT_VECTOR_ELT fp scalar type mismatch");
8238 assert((!VT.isInteger() ||
8240 "INSERT_VECTOR_ELT int scalar size mismatch");
8241
8242 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8243 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8244 // for scalable vectors where we will generate appropriate code to
8245 // deal with out-of-bounds cases correctly.
8246 if (N3C && N1.getValueType().isFixedLengthVector() &&
8247 N3C->getZExtValue() >= N1.getValueType().getVectorNumElements())
8248 return getUNDEF(VT);
8249
8250 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8251 if (N3.isUndef())
8252 return getUNDEF(VT);
8253
8254 // If the inserted element is an UNDEF, just use the input vector.
8255 if (N2.isUndef())
8256 return N1;
8257
8258 break;
8259 }
8260 case ISD::INSERT_SUBVECTOR: {
8261 // Inserting undef into undef is still undef.
8262 if (N1.isUndef() && N2.isUndef())
8263 return getUNDEF(VT);
8264
8265 EVT N2VT = N2.getValueType();
8266 assert(VT == N1.getValueType() &&
8267 "Dest and insert subvector source types must match!");
8268 assert(VT.isVector() && N2VT.isVector() &&
8269 "Insert subvector VTs must be vectors!");
8271 "Insert subvector VTs must have the same element type!");
8272 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8273 "Cannot insert a scalable vector into a fixed length vector!");
8274 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8276 "Insert subvector must be from smaller vector to larger vector!");
8277 assert(isa<ConstantSDNode>(N3) &&
8278 "Insert subvector index must be constant");
8279 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8280 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8282 "Insert subvector overflow!");
8285 "Constant index for INSERT_SUBVECTOR has an invalid size");
8286
8287 // Trivial insertion.
8288 if (VT == N2VT)
8289 return N2;
8290
8291 // If this is an insert of an extracted vector into an undef vector, we
8292 // can just use the input to the extract.
8293 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8294 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
8295 return N2.getOperand(0);
8296 break;
8297 }
8298 case ISD::BITCAST:
8299 // Fold bit_convert nodes from a type to themselves.
8300 if (N1.getValueType() == VT)
8301 return N1;
8302 break;
8303 case ISD::VP_TRUNCATE:
8304 case ISD::VP_SIGN_EXTEND:
8305 case ISD::VP_ZERO_EXTEND:
8306 // Don't create noop casts.
8307 if (N1.getValueType() == VT)
8308 return N1;
8309 break;
8310 case ISD::VECTOR_COMPRESS: {
8311 [[maybe_unused]] EVT VecVT = N1.getValueType();
8312 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8313 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8314 assert(VT == VecVT && "Vector and result type don't match.");
8315 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8316 "All inputs must be vectors.");
8317 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8319 "Vector and mask must have same number of elements.");
8320
8321 if (N1.isUndef() || N2.isUndef())
8322 return N3;
8323
8324 break;
8325 }
8329 [[maybe_unused]] EVT AccVT = N1.getValueType();
8330 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8331 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8332 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8333 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8334 "node to have the same type!");
8335 assert(VT.isVector() && VT == AccVT &&
8336 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8337 "the same type as its result!");
8339 AccVT.getVectorElementCount()) &&
8340 "Expected the element count of the second and third operands of the "
8341 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8342 "element count of the first operand and the result!");
8344 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8345 "node to have an element type which is the same as or smaller than "
8346 "the element type of the first operand and result!");
8347 break;
8348 }
8349 }
8350
8351 // Perform trivial constant folding for arithmetic operators.
8352 switch (Opcode) {
8353 case ISD::FMA:
8354 case ISD::FMAD:
8355 case ISD::SETCC:
8356 case ISD::FSHL:
8357 case ISD::FSHR:
8358 if (SDValue SV =
8359 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8360 return SV;
8361 break;
8362 }
8363
8364 // Memoize node if it doesn't produce a glue result.
8365 SDNode *N;
8366 SDVTList VTs = getVTList(VT);
8367 SDValue Ops[] = {N1, N2, N3};
8368 if (VT != MVT::Glue) {
8370 AddNodeIDNode(ID, Opcode, VTs, Ops);
8371 void *IP = nullptr;
8372 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8373 E->intersectFlagsWith(Flags);
8374 return SDValue(E, 0);
8375 }
8376
8377 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8378 N->setFlags(Flags);
8379 createOperands(N, Ops);
8380 CSEMap.InsertNode(N, IP);
8381 } else {
8382 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8383 createOperands(N, Ops);
8384 }
8385
8386 InsertNode(N);
8387 SDValue V = SDValue(N, 0);
8388 NewSDValueDbgMsg(V, "Creating new node: ", this);
8389 return V;
8390}
8391
8392SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8393 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8394 const SDNodeFlags Flags) {
8395 SDValue Ops[] = { N1, N2, N3, N4 };
8396 return getNode(Opcode, DL, VT, Ops, Flags);
8397}
8398
8399SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8400 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8401 SDNodeFlags Flags;
8402 if (Inserter)
8403 Flags = Inserter->getFlags();
8404 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8405}
8406
8407SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8408 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8409 SDValue N5, const SDNodeFlags Flags) {
8410 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8411 return getNode(Opcode, DL, VT, Ops, Flags);
8412}
8413
8414SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8415 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8416 SDValue N5) {
8417 SDNodeFlags Flags;
8418 if (Inserter)
8419 Flags = Inserter->getFlags();
8420 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8421}
8422
8423/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8424/// the incoming stack arguments to be loaded from the stack.
8426 SmallVector<SDValue, 8> ArgChains;
8427
8428 // Include the original chain at the beginning of the list. When this is
8429 // used by target LowerCall hooks, this helps legalize find the
8430 // CALLSEQ_BEGIN node.
8431 ArgChains.push_back(Chain);
8432
8433 // Add a chain value for each stack argument.
8434 for (SDNode *U : getEntryNode().getNode()->users())
8435 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8436 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8437 if (FI->getIndex() < 0)
8438 ArgChains.push_back(SDValue(L, 1));
8439
8440 // Build a tokenfactor for all the chains.
8441 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8442}
8443
8444/// getMemsetValue - Vectorized representation of the memset value
8445/// operand.
8447 const SDLoc &dl) {
8448 assert(!Value.isUndef());
8449
8450 unsigned NumBits = VT.getScalarSizeInBits();
8451 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
8452 assert(C->getAPIntValue().getBitWidth() == 8);
8453 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8454 if (VT.isInteger()) {
8455 bool IsOpaque = VT.getSizeInBits() > 64 ||
8456 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8457 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8458 }
8459 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8460 }
8461
8462 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8463 EVT IntVT = VT.getScalarType();
8464 if (!IntVT.isInteger())
8465 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8466
8467 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8468 if (NumBits > 8) {
8469 // Use a multiplication with 0x010101... to extend the input to the
8470 // required length.
8471 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8472 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8473 DAG.getConstant(Magic, dl, IntVT));
8474 }
8475
8476 if (VT != Value.getValueType() && !VT.isInteger())
8477 Value = DAG.getBitcast(VT.getScalarType(), Value);
8478 if (VT != Value.getValueType())
8479 Value = DAG.getSplatBuildVector(VT, dl, Value);
8480
8481 return Value;
8482}
8483
8484/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8485/// used when a memcpy is turned into a memset when the source is a constant
8486/// string ptr.
8488 const TargetLowering &TLI,
8489 const ConstantDataArraySlice &Slice) {
8490 // Handle vector with all elements zero.
8491 if (Slice.Array == nullptr) {
8492 if (VT.isInteger())
8493 return DAG.getConstant(0, dl, VT);
8494 return DAG.getNode(ISD::BITCAST, dl, VT,
8495 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8496 }
8497
8498 assert(!VT.isVector() && "Can't handle vector type here!");
8499 unsigned NumVTBits = VT.getSizeInBits();
8500 unsigned NumVTBytes = NumVTBits / 8;
8501 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8502
8503 APInt Val(NumVTBits, 0);
8504 if (DAG.getDataLayout().isLittleEndian()) {
8505 for (unsigned i = 0; i != NumBytes; ++i)
8506 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8507 } else {
8508 for (unsigned i = 0; i != NumBytes; ++i)
8509 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8510 }
8511
8512 // If the "cost" of materializing the integer immediate is less than the cost
8513 // of a load, then it is cost effective to turn the load into the immediate.
8514 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8515 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8516 return DAG.getConstant(Val, dl, VT);
8517 return SDValue();
8518}
8519
8521 const SDLoc &DL,
8522 const SDNodeFlags Flags) {
8523 EVT VT = Base.getValueType();
8524 SDValue Index;
8525
8526 if (Offset.isScalable())
8527 Index = getVScale(DL, Base.getValueType(),
8528 APInt(Base.getValueSizeInBits().getFixedValue(),
8529 Offset.getKnownMinValue()));
8530 else
8531 Index = getConstant(Offset.getFixedValue(), DL, VT);
8532
8533 return getMemBasePlusOffset(Base, Index, DL, Flags);
8534}
8535
8537 const SDLoc &DL,
8538 const SDNodeFlags Flags) {
8539 assert(Offset.getValueType().isInteger());
8540 EVT BasePtrVT = Ptr.getValueType();
8541 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8542 BasePtrVT))
8543 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8544 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
8545}
8546
8547/// Returns true if memcpy source is constant data.
8549 uint64_t SrcDelta = 0;
8550 GlobalAddressSDNode *G = nullptr;
8551 if (Src.getOpcode() == ISD::GlobalAddress)
8552 G = cast<GlobalAddressSDNode>(Src);
8553 else if (Src.getOpcode() == ISD::ADD &&
8554 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8555 Src.getOperand(1).getOpcode() == ISD::Constant) {
8556 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8557 SrcDelta = Src.getConstantOperandVal(1);
8558 }
8559 if (!G)
8560 return false;
8561
8562 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8563 SrcDelta + G->getOffset());
8564}
8565
8567 SelectionDAG &DAG) {
8568 // On Darwin, -Os means optimize for size without hurting performance, so
8569 // only really optimize for size when -Oz (MinSize) is used.
8571 return MF.getFunction().hasMinSize();
8572 return DAG.shouldOptForSize();
8573}
8574
8576 SmallVector<SDValue, 32> &OutChains, unsigned From,
8577 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8578 SmallVector<SDValue, 16> &OutStoreChains) {
8579 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8580 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8581 SmallVector<SDValue, 16> GluedLoadChains;
8582 for (unsigned i = From; i < To; ++i) {
8583 OutChains.push_back(OutLoadChains[i]);
8584 GluedLoadChains.push_back(OutLoadChains[i]);
8585 }
8586
8587 // Chain for all loads.
8588 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8589 GluedLoadChains);
8590
8591 for (unsigned i = From; i < To; ++i) {
8592 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8593 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8594 ST->getBasePtr(), ST->getMemoryVT(),
8595 ST->getMemOperand());
8596 OutChains.push_back(NewStore);
8597 }
8598}
8599
8601 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8602 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8603 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8604 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8605 // Turn a memcpy of undef to nop.
8606 // FIXME: We need to honor volatile even is Src is undef.
8607 if (Src.isUndef())
8608 return Chain;
8609
8610 // Expand memcpy to a series of load and store ops if the size operand falls
8611 // below a certain threshold.
8612 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8613 // rather than maybe a humongous number of loads and stores.
8614 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8615 const DataLayout &DL = DAG.getDataLayout();
8616 LLVMContext &C = *DAG.getContext();
8617 std::vector<EVT> MemOps;
8618 bool DstAlignCanChange = false;
8620 MachineFrameInfo &MFI = MF.getFrameInfo();
8621 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8622 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8623 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8624 DstAlignCanChange = true;
8625 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8626 if (!SrcAlign || Alignment > *SrcAlign)
8627 SrcAlign = Alignment;
8628 assert(SrcAlign && "SrcAlign must be set");
8630 // If marked as volatile, perform a copy even when marked as constant.
8631 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8632 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8633 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8634 const MemOp Op = isZeroConstant
8635 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8636 /*IsZeroMemset*/ true, isVol)
8637 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8638 *SrcAlign, isVol, CopyFromConstant);
8639 if (!TLI.findOptimalMemOpLowering(
8640 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8641 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8642 return SDValue();
8643
8644 if (DstAlignCanChange) {
8645 Type *Ty = MemOps[0].getTypeForEVT(C);
8646 Align NewAlign = DL.getABITypeAlign(Ty);
8647
8648 // Don't promote to an alignment that would require dynamic stack
8649 // realignment which may conflict with optimizations such as tail call
8650 // optimization.
8652 if (!TRI->hasStackRealignment(MF))
8653 if (MaybeAlign StackAlign = DL.getStackAlignment())
8654 NewAlign = std::min(NewAlign, *StackAlign);
8655
8656 if (NewAlign > Alignment) {
8657 // Give the stack frame object a larger alignment if needed.
8658 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8659 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8660 Alignment = NewAlign;
8661 }
8662 }
8663
8664 // Prepare AAInfo for loads/stores after lowering this memcpy.
8665 AAMDNodes NewAAInfo = AAInfo;
8666 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8667
8668 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8669 bool isConstant =
8670 BatchAA && SrcVal &&
8671 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8672
8673 MachineMemOperand::Flags MMOFlags =
8675 SmallVector<SDValue, 16> OutLoadChains;
8676 SmallVector<SDValue, 16> OutStoreChains;
8677 SmallVector<SDValue, 32> OutChains;
8678 unsigned NumMemOps = MemOps.size();
8679 uint64_t SrcOff = 0, DstOff = 0;
8680 for (unsigned i = 0; i != NumMemOps; ++i) {
8681 EVT VT = MemOps[i];
8682 unsigned VTSize = VT.getSizeInBits() / 8;
8683 SDValue Value, Store;
8684
8685 if (VTSize > Size) {
8686 // Issuing an unaligned load / store pair that overlaps with the previous
8687 // pair. Adjust the offset accordingly.
8688 assert(i == NumMemOps-1 && i != 0);
8689 SrcOff -= VTSize - Size;
8690 DstOff -= VTSize - Size;
8691 }
8692
8693 if (CopyFromConstant &&
8694 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8695 // It's unlikely a store of a vector immediate can be done in a single
8696 // instruction. It would require a load from a constantpool first.
8697 // We only handle zero vectors here.
8698 // FIXME: Handle other cases where store of vector immediate is done in
8699 // a single instruction.
8700 ConstantDataArraySlice SubSlice;
8701 if (SrcOff < Slice.Length) {
8702 SubSlice = Slice;
8703 SubSlice.move(SrcOff);
8704 } else {
8705 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8706 SubSlice.Array = nullptr;
8707 SubSlice.Offset = 0;
8708 SubSlice.Length = VTSize;
8709 }
8710 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8711 if (Value.getNode()) {
8712 Store = DAG.getStore(
8713 Chain, dl, Value,
8714 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8715 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8716 OutChains.push_back(Store);
8717 }
8718 }
8719
8720 if (!Store.getNode()) {
8721 // The type might not be legal for the target. This should only happen
8722 // if the type is smaller than a legal type, as on PPC, so the right
8723 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8724 // to Load/Store if NVT==VT.
8725 // FIXME does the case above also need this?
8726 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8727 assert(NVT.bitsGE(VT));
8728
8729 bool isDereferenceable =
8730 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8731 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8732 if (isDereferenceable)
8734 if (isConstant)
8735 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8736
8737 Value = DAG.getExtLoad(
8738 ISD::EXTLOAD, dl, NVT, Chain,
8739 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8740 SrcPtrInfo.getWithOffset(SrcOff), VT,
8741 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8742 OutLoadChains.push_back(Value.getValue(1));
8743
8744 Store = DAG.getTruncStore(
8745 Chain, dl, Value,
8746 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8747 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8748 OutStoreChains.push_back(Store);
8749 }
8750 SrcOff += VTSize;
8751 DstOff += VTSize;
8752 Size -= VTSize;
8753 }
8754
8755 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8757 unsigned NumLdStInMemcpy = OutStoreChains.size();
8758
8759 if (NumLdStInMemcpy) {
8760 // It may be that memcpy might be converted to memset if it's memcpy
8761 // of constants. In such a case, we won't have loads and stores, but
8762 // just stores. In the absence of loads, there is nothing to gang up.
8763 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8764 // If target does not care, just leave as it.
8765 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8766 OutChains.push_back(OutLoadChains[i]);
8767 OutChains.push_back(OutStoreChains[i]);
8768 }
8769 } else {
8770 // Ld/St less than/equal limit set by target.
8771 if (NumLdStInMemcpy <= GluedLdStLimit) {
8772 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8773 NumLdStInMemcpy, OutLoadChains,
8774 OutStoreChains);
8775 } else {
8776 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8777 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8778 unsigned GlueIter = 0;
8779
8780 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8781 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8782 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8783
8784 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8785 OutLoadChains, OutStoreChains);
8786 GlueIter += GluedLdStLimit;
8787 }
8788
8789 // Residual ld/st.
8790 if (RemainingLdStInMemcpy) {
8791 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8792 RemainingLdStInMemcpy, OutLoadChains,
8793 OutStoreChains);
8794 }
8795 }
8796 }
8797 }
8798 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8799}
8800
8802 SDValue Chain, SDValue Dst, SDValue Src,
8803 uint64_t Size, Align Alignment,
8804 bool isVol, bool AlwaysInline,
8805 MachinePointerInfo DstPtrInfo,
8806 MachinePointerInfo SrcPtrInfo,
8807 const AAMDNodes &AAInfo) {
8808 // Turn a memmove of undef to nop.
8809 // FIXME: We need to honor volatile even is Src is undef.
8810 if (Src.isUndef())
8811 return Chain;
8812
8813 // Expand memmove to a series of load and store ops if the size operand falls
8814 // below a certain threshold.
8815 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8816 const DataLayout &DL = DAG.getDataLayout();
8817 LLVMContext &C = *DAG.getContext();
8818 std::vector<EVT> MemOps;
8819 bool DstAlignCanChange = false;
8821 MachineFrameInfo &MFI = MF.getFrameInfo();
8822 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8823 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8824 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8825 DstAlignCanChange = true;
8826 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8827 if (!SrcAlign || Alignment > *SrcAlign)
8828 SrcAlign = Alignment;
8829 assert(SrcAlign && "SrcAlign must be set");
8830 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8831 if (!TLI.findOptimalMemOpLowering(
8832 C, MemOps, Limit,
8833 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8834 /*IsVolatile*/ true),
8835 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8836 MF.getFunction().getAttributes()))
8837 return SDValue();
8838
8839 if (DstAlignCanChange) {
8840 Type *Ty = MemOps[0].getTypeForEVT(C);
8841 Align NewAlign = DL.getABITypeAlign(Ty);
8842
8843 // Don't promote to an alignment that would require dynamic stack
8844 // realignment which may conflict with optimizations such as tail call
8845 // optimization.
8847 if (!TRI->hasStackRealignment(MF))
8848 if (MaybeAlign StackAlign = DL.getStackAlignment())
8849 NewAlign = std::min(NewAlign, *StackAlign);
8850
8851 if (NewAlign > Alignment) {
8852 // Give the stack frame object a larger alignment if needed.
8853 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8854 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8855 Alignment = NewAlign;
8856 }
8857 }
8858
8859 // Prepare AAInfo for loads/stores after lowering this memmove.
8860 AAMDNodes NewAAInfo = AAInfo;
8861 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8862
8863 MachineMemOperand::Flags MMOFlags =
8865 uint64_t SrcOff = 0, DstOff = 0;
8866 SmallVector<SDValue, 8> LoadValues;
8867 SmallVector<SDValue, 8> LoadChains;
8868 SmallVector<SDValue, 8> OutChains;
8869 unsigned NumMemOps = MemOps.size();
8870 for (unsigned i = 0; i < NumMemOps; i++) {
8871 EVT VT = MemOps[i];
8872 unsigned VTSize = VT.getSizeInBits() / 8;
8873 SDValue Value;
8874
8875 bool isDereferenceable =
8876 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8877 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8878 if (isDereferenceable)
8880
8881 Value = DAG.getLoad(
8882 VT, dl, Chain,
8883 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8884 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8885 LoadValues.push_back(Value);
8886 LoadChains.push_back(Value.getValue(1));
8887 SrcOff += VTSize;
8888 }
8889 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8890 OutChains.clear();
8891 for (unsigned i = 0; i < NumMemOps; i++) {
8892 EVT VT = MemOps[i];
8893 unsigned VTSize = VT.getSizeInBits() / 8;
8894 SDValue Store;
8895
8896 Store = DAG.getStore(
8897 Chain, dl, LoadValues[i],
8898 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8899 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8900 OutChains.push_back(Store);
8901 DstOff += VTSize;
8902 }
8903
8904 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8905}
8906
8907/// Lower the call to 'memset' intrinsic function into a series of store
8908/// operations.
8909///
8910/// \param DAG Selection DAG where lowered code is placed.
8911/// \param dl Link to corresponding IR location.
8912/// \param Chain Control flow dependency.
8913/// \param Dst Pointer to destination memory location.
8914/// \param Src Value of byte to write into the memory.
8915/// \param Size Number of bytes to write.
8916/// \param Alignment Alignment of the destination in bytes.
8917/// \param isVol True if destination is volatile.
8918/// \param AlwaysInline Makes sure no function call is generated.
8919/// \param DstPtrInfo IR information on the memory pointer.
8920/// \returns New head in the control flow, if lowering was successful, empty
8921/// SDValue otherwise.
8922///
8923/// The function tries to replace 'llvm.memset' intrinsic with several store
8924/// operations and value calculation code. This is usually profitable for small
8925/// memory size or when the semantic requires inlining.
8927 SDValue Chain, SDValue Dst, SDValue Src,
8928 uint64_t Size, Align Alignment, bool isVol,
8929 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8930 const AAMDNodes &AAInfo) {
8931 // Turn a memset of undef to nop.
8932 // FIXME: We need to honor volatile even is Src is undef.
8933 if (Src.isUndef())
8934 return Chain;
8935
8936 // Expand memset to a series of load/store ops if the size operand
8937 // falls below a certain threshold.
8938 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8939 std::vector<EVT> MemOps;
8940 bool DstAlignCanChange = false;
8941 LLVMContext &C = *DAG.getContext();
8943 MachineFrameInfo &MFI = MF.getFrameInfo();
8944 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8945 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8946 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8947 DstAlignCanChange = true;
8948 bool IsZeroVal = isNullConstant(Src);
8949 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8950
8951 if (!TLI.findOptimalMemOpLowering(
8952 C, MemOps, Limit,
8953 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8954 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8955 return SDValue();
8956
8957 if (DstAlignCanChange) {
8958 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8959 const DataLayout &DL = DAG.getDataLayout();
8960 Align NewAlign = DL.getABITypeAlign(Ty);
8961
8962 // Don't promote to an alignment that would require dynamic stack
8963 // realignment which may conflict with optimizations such as tail call
8964 // optimization.
8966 if (!TRI->hasStackRealignment(MF))
8967 if (MaybeAlign StackAlign = DL.getStackAlignment())
8968 NewAlign = std::min(NewAlign, *StackAlign);
8969
8970 if (NewAlign > Alignment) {
8971 // Give the stack frame object a larger alignment if needed.
8972 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8973 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8974 Alignment = NewAlign;
8975 }
8976 }
8977
8978 SmallVector<SDValue, 8> OutChains;
8979 uint64_t DstOff = 0;
8980 unsigned NumMemOps = MemOps.size();
8981
8982 // Find the largest store and generate the bit pattern for it.
8983 EVT LargestVT = MemOps[0];
8984 for (unsigned i = 1; i < NumMemOps; i++)
8985 if (MemOps[i].bitsGT(LargestVT))
8986 LargestVT = MemOps[i];
8987 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8988
8989 // Prepare AAInfo for loads/stores after lowering this memset.
8990 AAMDNodes NewAAInfo = AAInfo;
8991 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8992
8993 for (unsigned i = 0; i < NumMemOps; i++) {
8994 EVT VT = MemOps[i];
8995 unsigned VTSize = VT.getSizeInBits() / 8;
8996 if (VTSize > Size) {
8997 // Issuing an unaligned load / store pair that overlaps with the previous
8998 // pair. Adjust the offset accordingly.
8999 assert(i == NumMemOps-1 && i != 0);
9000 DstOff -= VTSize - Size;
9001 }
9002
9003 // If this store is smaller than the largest store see whether we can get
9004 // the smaller value for free with a truncate or extract vector element and
9005 // then store.
9006 SDValue Value = MemSetValue;
9007 if (VT.bitsLT(LargestVT)) {
9008 unsigned Index;
9009 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9010 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9011 if (!LargestVT.isVector() && !VT.isVector() &&
9012 TLI.isTruncateFree(LargestVT, VT))
9013 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9014 else if (LargestVT.isVector() && !VT.isVector() &&
9016 LargestVT.getTypeForEVT(*DAG.getContext()),
9017 VT.getSizeInBits(), Index) &&
9018 TLI.isTypeLegal(SVT) &&
9019 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9020 // Target which can combine store(extractelement VectorTy, Idx) can get
9021 // the smaller value for free.
9022 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9023 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9024 } else
9025 Value = getMemsetValue(Src, VT, DAG, dl);
9026 }
9027 assert(Value.getValueType() == VT && "Value with wrong type.");
9028 SDValue Store = DAG.getStore(
9029 Chain, dl, Value,
9030 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
9031 DstPtrInfo.getWithOffset(DstOff), Alignment,
9033 NewAAInfo);
9034 OutChains.push_back(Store);
9035 DstOff += VT.getSizeInBits() / 8;
9036 Size -= VTSize;
9037 }
9038
9039 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9040}
9041
9043 unsigned AS) {
9044 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9045 // pointer operands can be losslessly bitcasted to pointers of address space 0
9046 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9047 report_fatal_error("cannot lower memory intrinsic in address space " +
9048 Twine(AS));
9049 }
9050}
9051
9052std::pair<SDValue, SDValue>
9054 SDValue Mem1, SDValue Size, const CallInst *CI) {
9055 const char *LibCallName = TLI->getLibcallName(RTLIB::MEMCMP);
9056 if (!LibCallName)
9057 return {};
9058
9061 {Mem0, PT},
9062 {Mem1, PT},
9064
9066 bool IsTailCall = false;
9067 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
9068 IsTailCall = CI && CI->isTailCall() &&
9069 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg);
9070
9071 CLI.setDebugLoc(dl)
9072 .setChain(Chain)
9073 .setLibCallee(
9074 TLI->getLibcallCallingConv(RTLIB::MEMCMP),
9076 getExternalSymbol(LibCallName, TLI->getPointerTy(getDataLayout())),
9077 std::move(Args))
9078 .setTailCall(IsTailCall);
9079
9080 return TLI->LowerCallTo(CLI);
9081}
9082
9084 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9085 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9086 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9087 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9088 BatchAAResults *BatchAA) {
9089 // Check to see if we should lower the memcpy to loads and stores first.
9090 // For cases within the target-specified limits, this is the best choice.
9091 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
9092 if (ConstantSize) {
9093 // Memcpy with size zero? Just return the original chain.
9094 if (ConstantSize->isZero())
9095 return Chain;
9096
9098 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9099 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9100 if (Result.getNode())
9101 return Result;
9102 }
9103
9104 // Then check to see if we should lower the memcpy with target-specific
9105 // code. If the target chooses to do this, this is the next best.
9106 if (TSI) {
9107 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9108 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9109 DstPtrInfo, SrcPtrInfo);
9110 if (Result.getNode())
9111 return Result;
9112 }
9113
9114 // If we really need inline code and the target declined to provide it,
9115 // use a (potentially long) sequence of loads and stores.
9116 if (AlwaysInline) {
9117 assert(ConstantSize && "AlwaysInline requires a constant size!");
9119 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9120 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9121 }
9122
9125
9126 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9127 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9128 // respect volatile, so they may do things like read or write memory
9129 // beyond the given memory regions. But fixing this isn't easy, and most
9130 // people don't care.
9131
9132 // Emit a library call.
9135 Args.emplace_back(Dst, PtrTy);
9136 Args.emplace_back(Src, PtrTy);
9137 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9138 // FIXME: pass in SDLoc
9140 bool IsTailCall = false;
9141 const char *MemCpyName = TLI->getMemcpyName();
9142
9143 if (OverrideTailCall.has_value()) {
9144 IsTailCall = *OverrideTailCall;
9145 } else {
9146 bool LowersToMemcpy = StringRef(MemCpyName) == StringRef("memcpy");
9147 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
9148 IsTailCall = CI && CI->isTailCall() &&
9150 ReturnsFirstArg && LowersToMemcpy);
9151 }
9152
9153 CLI.setDebugLoc(dl)
9154 .setChain(Chain)
9155 .setLibCallee(
9156 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
9157 Dst.getValueType().getTypeForEVT(*getContext()),
9158 getExternalSymbol(MemCpyName, TLI->getPointerTy(getDataLayout())),
9159 std::move(Args))
9161 .setTailCall(IsTailCall);
9162
9163 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9164 return CallResult.second;
9165}
9166
9168 SDValue Dst, SDValue Src, SDValue Size,
9169 Type *SizeTy, unsigned ElemSz,
9170 bool isTailCall,
9171 MachinePointerInfo DstPtrInfo,
9172 MachinePointerInfo SrcPtrInfo) {
9173 // Emit a library call.
9176 Args.emplace_back(Dst, ArgTy);
9177 Args.emplace_back(Src, ArgTy);
9178 Args.emplace_back(Size, SizeTy);
9179
9180 RTLIB::Libcall LibraryCall =
9182 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9183 report_fatal_error("Unsupported element size");
9184
9186 CLI.setDebugLoc(dl)
9187 .setChain(Chain)
9188 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9190 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9191 TLI->getPointerTy(getDataLayout())),
9192 std::move(Args))
9194 .setTailCall(isTailCall);
9195
9196 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9197 return CallResult.second;
9198}
9199
9201 SDValue Src, SDValue Size, Align Alignment,
9202 bool isVol, const CallInst *CI,
9203 std::optional<bool> OverrideTailCall,
9204 MachinePointerInfo DstPtrInfo,
9205 MachinePointerInfo SrcPtrInfo,
9206 const AAMDNodes &AAInfo,
9207 BatchAAResults *BatchAA) {
9208 // Check to see if we should lower the memmove to loads and stores first.
9209 // For cases within the target-specified limits, this is the best choice.
9210 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
9211 if (ConstantSize) {
9212 // Memmove with size zero? Just return the original chain.
9213 if (ConstantSize->isZero())
9214 return Chain;
9215
9217 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9218 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9219 if (Result.getNode())
9220 return Result;
9221 }
9222
9223 // Then check to see if we should lower the memmove with target-specific
9224 // code. If the target chooses to do this, this is the next best.
9225 if (TSI) {
9226 SDValue Result =
9227 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9228 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9229 if (Result.getNode())
9230 return Result;
9231 }
9232
9235
9236 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9237 // not be safe. See memcpy above for more details.
9238
9239 // Emit a library call.
9242 Args.emplace_back(Dst, PtrTy);
9243 Args.emplace_back(Src, PtrTy);
9244 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9245 // FIXME: pass in SDLoc
9247
9248 bool IsTailCall = false;
9249 if (OverrideTailCall.has_value()) {
9250 IsTailCall = *OverrideTailCall;
9251 } else {
9252 bool LowersToMemmove =
9253 TLI->getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove");
9254 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
9255 IsTailCall = CI && CI->isTailCall() &&
9257 ReturnsFirstArg && LowersToMemmove);
9258 }
9259
9260 CLI.setDebugLoc(dl)
9261 .setChain(Chain)
9262 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
9263 Dst.getValueType().getTypeForEVT(*getContext()),
9264 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
9265 TLI->getPointerTy(getDataLayout())),
9266 std::move(Args))
9268 .setTailCall(IsTailCall);
9269
9270 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9271 return CallResult.second;
9272}
9273
9275 SDValue Dst, SDValue Src, SDValue Size,
9276 Type *SizeTy, unsigned ElemSz,
9277 bool isTailCall,
9278 MachinePointerInfo DstPtrInfo,
9279 MachinePointerInfo SrcPtrInfo) {
9280 // Emit a library call.
9282 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9283 Args.emplace_back(Dst, IntPtrTy);
9284 Args.emplace_back(Src, IntPtrTy);
9285 Args.emplace_back(Size, SizeTy);
9286
9287 RTLIB::Libcall LibraryCall =
9289 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9290 report_fatal_error("Unsupported element size");
9291
9293 CLI.setDebugLoc(dl)
9294 .setChain(Chain)
9295 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9297 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9298 TLI->getPointerTy(getDataLayout())),
9299 std::move(Args))
9301 .setTailCall(isTailCall);
9302
9303 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9304 return CallResult.second;
9305}
9306
9308 SDValue Src, SDValue Size, Align Alignment,
9309 bool isVol, bool AlwaysInline,
9310 const CallInst *CI,
9311 MachinePointerInfo DstPtrInfo,
9312 const AAMDNodes &AAInfo) {
9313 // Check to see if we should lower the memset to stores first.
9314 // For cases within the target-specified limits, this is the best choice.
9315 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
9316 if (ConstantSize) {
9317 // Memset with size zero? Just return the original chain.
9318 if (ConstantSize->isZero())
9319 return Chain;
9320
9321 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9322 ConstantSize->getZExtValue(), Alignment,
9323 isVol, false, DstPtrInfo, AAInfo);
9324
9325 if (Result.getNode())
9326 return Result;
9327 }
9328
9329 // Then check to see if we should lower the memset with target-specific
9330 // code. If the target chooses to do this, this is the next best.
9331 if (TSI) {
9332 SDValue Result = TSI->EmitTargetCodeForMemset(
9333 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9334 if (Result.getNode())
9335 return Result;
9336 }
9337
9338 // If we really need inline code and the target declined to provide it,
9339 // use a (potentially long) sequence of loads and stores.
9340 if (AlwaysInline) {
9341 assert(ConstantSize && "AlwaysInline requires a constant size!");
9342 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9343 ConstantSize->getZExtValue(), Alignment,
9344 isVol, true, DstPtrInfo, AAInfo);
9345 assert(Result &&
9346 "getMemsetStores must return a valid sequence when AlwaysInline");
9347 return Result;
9348 }
9349
9351
9352 // Emit a library call.
9353 auto &Ctx = *getContext();
9354 const auto& DL = getDataLayout();
9355
9357 // FIXME: pass in SDLoc
9358 CLI.setDebugLoc(dl).setChain(Chain);
9359
9360 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
9361
9362 bool UseBZero = isNullConstant(Src) && BzeroName;
9363 // If zeroing out and bzero is present, use it.
9364 if (UseBZero) {
9366 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9367 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9368 CLI.setLibCallee(
9369 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
9370 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
9371 } else {
9373 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9374 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9375 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9376 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
9377 Dst.getValueType().getTypeForEVT(Ctx),
9378 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
9379 TLI->getPointerTy(DL)),
9380 std::move(Args));
9381 }
9382 bool LowersToMemset =
9383 TLI->getLibcallName(RTLIB::MEMSET) == StringRef("memset");
9384 // If we're going to use bzero, make sure not to tail call unless the
9385 // subsequent return doesn't need a value, as bzero doesn't return the first
9386 // arg unlike memset.
9387 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9388 bool IsTailCall =
9389 CI && CI->isTailCall() &&
9390 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9391 CLI.setDiscardResult().setTailCall(IsTailCall);
9392
9393 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9394 return CallResult.second;
9395}
9396
9399 Type *SizeTy, unsigned ElemSz,
9400 bool isTailCall,
9401 MachinePointerInfo DstPtrInfo) {
9402 // Emit a library call.
9404 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9405 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9406 Args.emplace_back(Size, SizeTy);
9407
9408 RTLIB::Libcall LibraryCall =
9410 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9411 report_fatal_error("Unsupported element size");
9412
9414 CLI.setDebugLoc(dl)
9415 .setChain(Chain)
9416 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9418 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9419 TLI->getPointerTy(getDataLayout())),
9420 std::move(Args))
9422 .setTailCall(isTailCall);
9423
9424 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9425 return CallResult.second;
9426}
9427
9428SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9429 SDVTList VTList, ArrayRef<SDValue> Ops,
9430 MachineMemOperand *MMO,
9431 ISD::LoadExtType ExtType) {
9433 AddNodeIDNode(ID, Opcode, VTList, Ops);
9434 ID.AddInteger(MemVT.getRawBits());
9435 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9436 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9437 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9438 ID.AddInteger(MMO->getFlags());
9439 void* IP = nullptr;
9440 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9441 E->refineAlignment(MMO);
9442 E->refineRanges(MMO);
9443 return SDValue(E, 0);
9444 }
9445
9446 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9447 VTList, MemVT, MMO, ExtType);
9448 createOperands(N, Ops);
9449
9450 CSEMap.InsertNode(N, IP);
9451 InsertNode(N);
9452 SDValue V(N, 0);
9453 NewSDValueDbgMsg(V, "Creating new node: ", this);
9454 return V;
9455}
9456
9458 EVT MemVT, SDVTList VTs, SDValue Chain,
9459 SDValue Ptr, SDValue Cmp, SDValue Swp,
9460 MachineMemOperand *MMO) {
9461 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9463 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9464
9465 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9466 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9467}
9468
9469SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9470 SDValue Chain, SDValue Ptr, SDValue Val,
9471 MachineMemOperand *MMO) {
9472 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9473 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9474 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9475 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9476 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9477 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9478 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9479 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9480 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9481 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9482 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9483 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9484 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9485 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9486 Opcode == ISD::ATOMIC_STORE) &&
9487 "Invalid Atomic Op");
9488
9489 EVT VT = Val.getValueType();
9490
9491 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9492 getVTList(VT, MVT::Other);
9493 SDValue Ops[] = {Chain, Ptr, Val};
9494 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9495}
9496
9498 EVT MemVT, EVT VT, SDValue Chain,
9500 SDVTList VTs = getVTList(VT, MVT::Other);
9501 SDValue Ops[] = {Chain, Ptr};
9502 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9503}
9504
9505/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9507 if (Ops.size() == 1)
9508 return Ops[0];
9509
9511 VTs.reserve(Ops.size());
9512 for (const SDValue &Op : Ops)
9513 VTs.push_back(Op.getValueType());
9514 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9515}
9516
9518 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9519 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9521 const AAMDNodes &AAInfo) {
9522 if (Size.hasValue() && !Size.getValue())
9524
9526 MachineMemOperand *MMO =
9527 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9528
9529 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9530}
9531
9533 SDVTList VTList,
9534 ArrayRef<SDValue> Ops, EVT MemVT,
9535 MachineMemOperand *MMO) {
9536 assert(
9537 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9538 Opcode == ISD::PREFETCH ||
9539 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9540 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9541 "Opcode is not a memory-accessing opcode!");
9542
9543 // Memoize the node unless it returns a glue result.
9545 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9547 AddNodeIDNode(ID, Opcode, VTList, Ops);
9548 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9549 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9550 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9551 ID.AddInteger(MMO->getFlags());
9552 ID.AddInteger(MemVT.getRawBits());
9553 void *IP = nullptr;
9554 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9555 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9556 return SDValue(E, 0);
9557 }
9558
9559 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9560 VTList, MemVT, MMO);
9561 createOperands(N, Ops);
9562
9563 CSEMap.InsertNode(N, IP);
9564 } else {
9565 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9566 VTList, MemVT, MMO);
9567 createOperands(N, Ops);
9568 }
9569 InsertNode(N);
9570 SDValue V(N, 0);
9571 NewSDValueDbgMsg(V, "Creating new node: ", this);
9572 return V;
9573}
9574
9576 SDValue Chain, int FrameIndex) {
9577 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9578 const auto VTs = getVTList(MVT::Other);
9579 SDValue Ops[2] = {
9580 Chain,
9581 getFrameIndex(FrameIndex,
9582 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9583 true)};
9584
9586 AddNodeIDNode(ID, Opcode, VTs, Ops);
9587 ID.AddInteger(FrameIndex);
9588 void *IP = nullptr;
9589 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9590 return SDValue(E, 0);
9591
9592 LifetimeSDNode *N =
9593 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9594 createOperands(N, Ops);
9595 CSEMap.InsertNode(N, IP);
9596 InsertNode(N);
9597 SDValue V(N, 0);
9598 NewSDValueDbgMsg(V, "Creating new node: ", this);
9599 return V;
9600}
9601
9603 uint64_t Guid, uint64_t Index,
9604 uint32_t Attr) {
9605 const unsigned Opcode = ISD::PSEUDO_PROBE;
9606 const auto VTs = getVTList(MVT::Other);
9607 SDValue Ops[] = {Chain};
9609 AddNodeIDNode(ID, Opcode, VTs, Ops);
9610 ID.AddInteger(Guid);
9611 ID.AddInteger(Index);
9612 void *IP = nullptr;
9613 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9614 return SDValue(E, 0);
9615
9616 auto *N = newSDNode<PseudoProbeSDNode>(
9617 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9618 createOperands(N, Ops);
9619 CSEMap.InsertNode(N, IP);
9620 InsertNode(N);
9621 SDValue V(N, 0);
9622 NewSDValueDbgMsg(V, "Creating new node: ", this);
9623 return V;
9624}
9625
9626/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9627/// MachinePointerInfo record from it. This is particularly useful because the
9628/// code generator has many cases where it doesn't bother passing in a
9629/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9631 SelectionDAG &DAG, SDValue Ptr,
9632 int64_t Offset = 0) {
9633 // If this is FI+Offset, we can model it.
9634 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9636 FI->getIndex(), Offset);
9637
9638 // If this is (FI+Offset1)+Offset2, we can model it.
9639 if (Ptr.getOpcode() != ISD::ADD ||
9640 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
9641 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
9642 return Info;
9643
9644 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9646 DAG.getMachineFunction(), FI,
9647 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9648}
9649
9650/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9651/// MachinePointerInfo record from it. This is particularly useful because the
9652/// code generator has many cases where it doesn't bother passing in a
9653/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9655 SelectionDAG &DAG, SDValue Ptr,
9656 SDValue OffsetOp) {
9657 // If the 'Offset' value isn't a constant, we can't handle this.
9658 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
9659 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9660 if (OffsetOp.isUndef())
9661 return InferPointerInfo(Info, DAG, Ptr);
9662 return Info;
9663}
9664
9666 EVT VT, const SDLoc &dl, SDValue Chain,
9668 MachinePointerInfo PtrInfo, EVT MemVT,
9669 Align Alignment,
9670 MachineMemOperand::Flags MMOFlags,
9671 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9672 assert(Chain.getValueType() == MVT::Other &&
9673 "Invalid chain type");
9674
9675 MMOFlags |= MachineMemOperand::MOLoad;
9676 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9677 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9678 // clients.
9679 if (PtrInfo.V.isNull())
9680 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9681
9682 TypeSize Size = MemVT.getStoreSize();
9684 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9685 Alignment, AAInfo, Ranges);
9686 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9687}
9688
9690 EVT VT, const SDLoc &dl, SDValue Chain,
9691 SDValue Ptr, SDValue Offset, EVT MemVT,
9692 MachineMemOperand *MMO) {
9693 if (VT == MemVT) {
9694 ExtType = ISD::NON_EXTLOAD;
9695 } else if (ExtType == ISD::NON_EXTLOAD) {
9696 assert(VT == MemVT && "Non-extending load from different memory type!");
9697 } else {
9698 // Extending load.
9699 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9700 "Should only be an extending load, not truncating!");
9701 assert(VT.isInteger() == MemVT.isInteger() &&
9702 "Cannot convert from FP to Int or Int -> FP!");
9703 assert(VT.isVector() == MemVT.isVector() &&
9704 "Cannot use an ext load to convert to or from a vector!");
9705 assert((!VT.isVector() ||
9707 "Cannot use an ext load to change the number of vector elements!");
9708 }
9709
9710 assert((!MMO->getRanges() ||
9711 (mdconst::extract<ConstantInt>(MMO->getRanges()->getOperand(0))
9712 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9713 MemVT.isInteger())) &&
9714 "Range metadata and load type must match!");
9715
9716 bool Indexed = AM != ISD::UNINDEXED;
9717 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9718
9719 SDVTList VTs = Indexed ?
9720 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9721 SDValue Ops[] = { Chain, Ptr, Offset };
9723 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9724 ID.AddInteger(MemVT.getRawBits());
9725 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9726 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9727 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9728 ID.AddInteger(MMO->getFlags());
9729 void *IP = nullptr;
9730 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9731 E->refineAlignment(MMO);
9732 E->refineRanges(MMO);
9733 return SDValue(E, 0);
9734 }
9735 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9736 ExtType, MemVT, MMO);
9737 createOperands(N, Ops);
9738
9739 CSEMap.InsertNode(N, IP);
9740 InsertNode(N);
9741 SDValue V(N, 0);
9742 NewSDValueDbgMsg(V, "Creating new node: ", this);
9743 return V;
9744}
9745
9748 MaybeAlign Alignment,
9749 MachineMemOperand::Flags MMOFlags,
9750 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9751 SDValue Undef = getUNDEF(Ptr.getValueType());
9752 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9753 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9754}
9755
9758 SDValue Undef = getUNDEF(Ptr.getValueType());
9759 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9760 VT, MMO);
9761}
9762
9764 EVT VT, SDValue Chain, SDValue Ptr,
9765 MachinePointerInfo PtrInfo, EVT MemVT,
9766 MaybeAlign Alignment,
9767 MachineMemOperand::Flags MMOFlags,
9768 const AAMDNodes &AAInfo) {
9769 SDValue Undef = getUNDEF(Ptr.getValueType());
9770 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9771 MemVT, Alignment, MMOFlags, AAInfo);
9772}
9773
9775 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9776 MachineMemOperand *MMO) {
9777 SDValue Undef = getUNDEF(Ptr.getValueType());
9778 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9779 MemVT, MMO);
9780}
9781
9785 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9786 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9787 // Don't propagate the invariant or dereferenceable flags.
9788 auto MMOFlags =
9789 LD->getMemOperand()->getFlags() &
9791 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9792 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9793 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9794}
9795
9798 Align Alignment,
9799 MachineMemOperand::Flags MMOFlags,
9800 const AAMDNodes &AAInfo) {
9801 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9802
9803 MMOFlags |= MachineMemOperand::MOStore;
9804 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9805
9806 if (PtrInfo.V.isNull())
9807 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9808
9811 MachineMemOperand *MMO =
9812 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9813 return getStore(Chain, dl, Val, Ptr, MMO);
9814}
9815
9818 SDValue Undef = getUNDEF(Ptr.getValueType());
9819 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9821}
9822
9826 bool IsTruncating) {
9827 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9828 EVT VT = Val.getValueType();
9829 if (VT == SVT) {
9830 IsTruncating = false;
9831 } else if (!IsTruncating) {
9832 assert(VT == SVT && "No-truncating store from different memory type!");
9833 } else {
9835 "Should only be a truncating store, not extending!");
9836 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9837 assert(VT.isVector() == SVT.isVector() &&
9838 "Cannot use trunc store to convert to or from a vector!");
9839 assert((!VT.isVector() ||
9841 "Cannot use trunc store to change the number of vector elements!");
9842 }
9843
9844 bool Indexed = AM != ISD::UNINDEXED;
9845 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9846 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9847 : getVTList(MVT::Other);
9848 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9850 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9851 ID.AddInteger(SVT.getRawBits());
9852 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9853 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9854 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9855 ID.AddInteger(MMO->getFlags());
9856 void *IP = nullptr;
9857 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9858 cast<StoreSDNode>(E)->refineAlignment(MMO);
9859 return SDValue(E, 0);
9860 }
9861 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9862 IsTruncating, SVT, MMO);
9863 createOperands(N, Ops);
9864
9865 CSEMap.InsertNode(N, IP);
9866 InsertNode(N);
9867 SDValue V(N, 0);
9868 NewSDValueDbgMsg(V, "Creating new node: ", this);
9869 return V;
9870}
9871
9874 EVT SVT, Align Alignment,
9875 MachineMemOperand::Flags MMOFlags,
9876 const AAMDNodes &AAInfo) {
9877 assert(Chain.getValueType() == MVT::Other &&
9878 "Invalid chain type");
9879
9880 MMOFlags |= MachineMemOperand::MOStore;
9881 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9882
9883 if (PtrInfo.V.isNull())
9884 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9885
9888 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
9889 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9890}
9891
9893 SDValue Ptr, EVT SVT,
9894 MachineMemOperand *MMO) {
9895 SDValue Undef = getUNDEF(Ptr.getValueType());
9896 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
9897}
9898
9902 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9903 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9904 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9905 ST->getMemoryVT(), ST->getMemOperand(), AM,
9906 ST->isTruncatingStore());
9907}
9908
9910 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9911 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9912 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9913 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9914 const MDNode *Ranges, bool IsExpanding) {
9915 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9916
9917 MMOFlags |= MachineMemOperand::MOLoad;
9918 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9919 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9920 // clients.
9921 if (PtrInfo.V.isNull())
9922 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9923
9924 TypeSize Size = MemVT.getStoreSize();
9926 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9927 Alignment, AAInfo, Ranges);
9928 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9929 MMO, IsExpanding);
9930}
9931
9933 ISD::LoadExtType ExtType, EVT VT,
9934 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9935 SDValue Offset, SDValue Mask, SDValue EVL,
9936 EVT MemVT, MachineMemOperand *MMO,
9937 bool IsExpanding) {
9938 bool Indexed = AM != ISD::UNINDEXED;
9939 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9940
9941 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9942 : getVTList(VT, MVT::Other);
9943 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9945 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9946 ID.AddInteger(MemVT.getRawBits());
9947 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9948 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9949 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9950 ID.AddInteger(MMO->getFlags());
9951 void *IP = nullptr;
9952 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9953 E->refineAlignment(MMO);
9954 E->refineRanges(MMO);
9955 return SDValue(E, 0);
9956 }
9957 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9958 ExtType, IsExpanding, MemVT, MMO);
9959 createOperands(N, Ops);
9960
9961 CSEMap.InsertNode(N, IP);
9962 InsertNode(N);
9963 SDValue V(N, 0);
9964 NewSDValueDbgMsg(V, "Creating new node: ", this);
9965 return V;
9966}
9967
9969 SDValue Ptr, SDValue Mask, SDValue EVL,
9970 MachinePointerInfo PtrInfo,
9971 MaybeAlign Alignment,
9972 MachineMemOperand::Flags MMOFlags,
9973 const AAMDNodes &AAInfo, const MDNode *Ranges,
9974 bool IsExpanding) {
9975 SDValue Undef = getUNDEF(Ptr.getValueType());
9976 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9977 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9978 IsExpanding);
9979}
9980
9982 SDValue Ptr, SDValue Mask, SDValue EVL,
9983 MachineMemOperand *MMO, bool IsExpanding) {
9984 SDValue Undef = getUNDEF(Ptr.getValueType());
9985 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9986 Mask, EVL, VT, MMO, IsExpanding);
9987}
9988
9990 EVT VT, SDValue Chain, SDValue Ptr,
9991 SDValue Mask, SDValue EVL,
9992 MachinePointerInfo PtrInfo, EVT MemVT,
9993 MaybeAlign Alignment,
9994 MachineMemOperand::Flags MMOFlags,
9995 const AAMDNodes &AAInfo, bool IsExpanding) {
9996 SDValue Undef = getUNDEF(Ptr.getValueType());
9997 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9998 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9999 IsExpanding);
10000}
10001
10003 EVT VT, SDValue Chain, SDValue Ptr,
10004 SDValue Mask, SDValue EVL, EVT MemVT,
10005 MachineMemOperand *MMO, bool IsExpanding) {
10006 SDValue Undef = getUNDEF(Ptr.getValueType());
10007 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10008 EVL, MemVT, MMO, IsExpanding);
10009}
10010
10014 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10015 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10016 // Don't propagate the invariant or dereferenceable flags.
10017 auto MMOFlags =
10018 LD->getMemOperand()->getFlags() &
10020 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10021 LD->getChain(), Base, Offset, LD->getMask(),
10022 LD->getVectorLength(), LD->getPointerInfo(),
10023 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10024 nullptr, LD->isExpandingLoad());
10025}
10026
10029 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10030 ISD::MemIndexedMode AM, bool IsTruncating,
10031 bool IsCompressing) {
10032 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10033 bool Indexed = AM != ISD::UNINDEXED;
10034 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10035 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10036 : getVTList(MVT::Other);
10037 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10039 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10040 ID.AddInteger(MemVT.getRawBits());
10041 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10042 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10043 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10044 ID.AddInteger(MMO->getFlags());
10045 void *IP = nullptr;
10046 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10047 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10048 return SDValue(E, 0);
10049 }
10050 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10051 IsTruncating, IsCompressing, MemVT, MMO);
10052 createOperands(N, Ops);
10053
10054 CSEMap.InsertNode(N, IP);
10055 InsertNode(N);
10056 SDValue V(N, 0);
10057 NewSDValueDbgMsg(V, "Creating new node: ", this);
10058 return V;
10059}
10060
10062 SDValue Val, SDValue Ptr, SDValue Mask,
10063 SDValue EVL, MachinePointerInfo PtrInfo,
10064 EVT SVT, Align Alignment,
10065 MachineMemOperand::Flags MMOFlags,
10066 const AAMDNodes &AAInfo,
10067 bool IsCompressing) {
10068 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10069
10070 MMOFlags |= MachineMemOperand::MOStore;
10071 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10072
10073 if (PtrInfo.V.isNull())
10074 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10075
10078 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10079 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10080 IsCompressing);
10081}
10082
10084 SDValue Val, SDValue Ptr, SDValue Mask,
10085 SDValue EVL, EVT SVT,
10086 MachineMemOperand *MMO,
10087 bool IsCompressing) {
10088 EVT VT = Val.getValueType();
10089
10090 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10091 if (VT == SVT)
10092 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10093 EVL, VT, MMO, ISD::UNINDEXED,
10094 /*IsTruncating*/ false, IsCompressing);
10095
10097 "Should only be a truncating store, not extending!");
10098 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10099 assert(VT.isVector() == SVT.isVector() &&
10100 "Cannot use trunc store to convert to or from a vector!");
10101 assert((!VT.isVector() ||
10103 "Cannot use trunc store to change the number of vector elements!");
10104
10105 SDVTList VTs = getVTList(MVT::Other);
10106 SDValue Undef = getUNDEF(Ptr.getValueType());
10107 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10109 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10110 ID.AddInteger(SVT.getRawBits());
10111 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10112 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10113 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10114 ID.AddInteger(MMO->getFlags());
10115 void *IP = nullptr;
10116 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10117 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10118 return SDValue(E, 0);
10119 }
10120 auto *N =
10121 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10122 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10123 createOperands(N, Ops);
10124
10125 CSEMap.InsertNode(N, IP);
10126 InsertNode(N);
10127 SDValue V(N, 0);
10128 NewSDValueDbgMsg(V, "Creating new node: ", this);
10129 return V;
10130}
10131
10135 auto *ST = cast<VPStoreSDNode>(OrigStore);
10136 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10137 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10138 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10139 Offset, ST->getMask(), ST->getVectorLength()};
10141 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10142 ID.AddInteger(ST->getMemoryVT().getRawBits());
10143 ID.AddInteger(ST->getRawSubclassData());
10144 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10145 ID.AddInteger(ST->getMemOperand()->getFlags());
10146 void *IP = nullptr;
10147 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10148 return SDValue(E, 0);
10149
10150 auto *N = newSDNode<VPStoreSDNode>(
10151 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10152 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10153 createOperands(N, Ops);
10154
10155 CSEMap.InsertNode(N, IP);
10156 InsertNode(N);
10157 SDValue V(N, 0);
10158 NewSDValueDbgMsg(V, "Creating new node: ", this);
10159 return V;
10160}
10161
10163 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10164 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10165 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10166 bool Indexed = AM != ISD::UNINDEXED;
10167 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10168
10169 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10170 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10171 : getVTList(VT, MVT::Other);
10173 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10174 ID.AddInteger(VT.getRawBits());
10175 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10176 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10177 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10178
10179 void *IP = nullptr;
10180 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10181 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10182 return SDValue(E, 0);
10183 }
10184
10185 auto *N =
10186 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10187 ExtType, IsExpanding, MemVT, MMO);
10188 createOperands(N, Ops);
10189 CSEMap.InsertNode(N, IP);
10190 InsertNode(N);
10191 SDValue V(N, 0);
10192 NewSDValueDbgMsg(V, "Creating new node: ", this);
10193 return V;
10194}
10195
10197 SDValue Ptr, SDValue Stride,
10198 SDValue Mask, SDValue EVL,
10199 MachineMemOperand *MMO,
10200 bool IsExpanding) {
10201 SDValue Undef = getUNDEF(Ptr.getValueType());
10203 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10204}
10205
10207 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10208 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10209 MachineMemOperand *MMO, bool IsExpanding) {
10210 SDValue Undef = getUNDEF(Ptr.getValueType());
10211 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10212 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10213}
10214
10216 SDValue Val, SDValue Ptr,
10217 SDValue Offset, SDValue Stride,
10218 SDValue Mask, SDValue EVL, EVT MemVT,
10219 MachineMemOperand *MMO,
10221 bool IsTruncating, bool IsCompressing) {
10222 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10223 bool Indexed = AM != ISD::UNINDEXED;
10224 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10225 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10226 : getVTList(MVT::Other);
10227 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10229 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10230 ID.AddInteger(MemVT.getRawBits());
10231 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10232 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10233 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10234 void *IP = nullptr;
10235 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10236 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10237 return SDValue(E, 0);
10238 }
10239 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10240 VTs, AM, IsTruncating,
10241 IsCompressing, MemVT, 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
10252 SDValue Val, SDValue Ptr,
10253 SDValue Stride, SDValue Mask,
10254 SDValue EVL, EVT SVT,
10255 MachineMemOperand *MMO,
10256 bool IsCompressing) {
10257 EVT VT = Val.getValueType();
10258
10259 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10260 if (VT == SVT)
10261 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10262 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10263 /*IsTruncating*/ false, IsCompressing);
10264
10266 "Should only be a truncating store, not extending!");
10267 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10268 assert(VT.isVector() == SVT.isVector() &&
10269 "Cannot use trunc store to convert to or from a vector!");
10270 assert((!VT.isVector() ||
10272 "Cannot use trunc store to change the number of vector elements!");
10273
10274 SDVTList VTs = getVTList(MVT::Other);
10275 SDValue Undef = getUNDEF(Ptr.getValueType());
10276 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10278 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10279 ID.AddInteger(SVT.getRawBits());
10280 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10281 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10282 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10283 void *IP = nullptr;
10284 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10285 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10286 return SDValue(E, 0);
10287 }
10288 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10289 VTs, ISD::UNINDEXED, true,
10290 IsCompressing, SVT, MMO);
10291 createOperands(N, Ops);
10292
10293 CSEMap.InsertNode(N, IP);
10294 InsertNode(N);
10295 SDValue V(N, 0);
10296 NewSDValueDbgMsg(V, "Creating new node: ", this);
10297 return V;
10298}
10299
10302 ISD::MemIndexType IndexType) {
10303 assert(Ops.size() == 6 && "Incompatible number of operands");
10304
10306 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10307 ID.AddInteger(VT.getRawBits());
10308 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10309 dl.getIROrder(), VTs, VT, MMO, IndexType));
10310 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10311 ID.AddInteger(MMO->getFlags());
10312 void *IP = nullptr;
10313 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10314 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10315 return SDValue(E, 0);
10316 }
10317
10318 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10319 VT, MMO, IndexType);
10320 createOperands(N, Ops);
10321
10322 assert(N->getMask().getValueType().getVectorElementCount() ==
10323 N->getValueType(0).getVectorElementCount() &&
10324 "Vector width mismatch between mask and data");
10325 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10326 N->getValueType(0).getVectorElementCount().isScalable() &&
10327 "Scalable flags of index and data do not match");
10329 N->getIndex().getValueType().getVectorElementCount(),
10330 N->getValueType(0).getVectorElementCount()) &&
10331 "Vector width mismatch between index and data");
10332 assert(isa<ConstantSDNode>(N->getScale()) &&
10333 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10334 "Scale should be a constant power of 2");
10335
10336 CSEMap.InsertNode(N, IP);
10337 InsertNode(N);
10338 SDValue V(N, 0);
10339 NewSDValueDbgMsg(V, "Creating new node: ", this);
10340 return V;
10341}
10342
10345 MachineMemOperand *MMO,
10346 ISD::MemIndexType IndexType) {
10347 assert(Ops.size() == 7 && "Incompatible number of operands");
10348
10350 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10351 ID.AddInteger(VT.getRawBits());
10352 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10353 dl.getIROrder(), VTs, VT, MMO, IndexType));
10354 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10355 ID.AddInteger(MMO->getFlags());
10356 void *IP = nullptr;
10357 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10358 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10359 return SDValue(E, 0);
10360 }
10361 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10362 VT, MMO, IndexType);
10363 createOperands(N, Ops);
10364
10365 assert(N->getMask().getValueType().getVectorElementCount() ==
10366 N->getValue().getValueType().getVectorElementCount() &&
10367 "Vector width mismatch between mask and data");
10368 assert(
10369 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10370 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10371 "Scalable flags of index and data do not match");
10373 N->getIndex().getValueType().getVectorElementCount(),
10374 N->getValue().getValueType().getVectorElementCount()) &&
10375 "Vector width mismatch between index and data");
10376 assert(isa<ConstantSDNode>(N->getScale()) &&
10377 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10378 "Scale should be a constant power of 2");
10379
10380 CSEMap.InsertNode(N, IP);
10381 InsertNode(N);
10382 SDValue V(N, 0);
10383 NewSDValueDbgMsg(V, "Creating new node: ", this);
10384 return V;
10385}
10386
10389 SDValue PassThru, EVT MemVT,
10390 MachineMemOperand *MMO,
10392 ISD::LoadExtType ExtTy, bool isExpanding) {
10393 bool Indexed = AM != ISD::UNINDEXED;
10394 assert((Indexed || Offset.isUndef()) &&
10395 "Unindexed masked load with an offset!");
10396 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10397 : getVTList(VT, MVT::Other);
10398 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10400 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
10401 ID.AddInteger(MemVT.getRawBits());
10402 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10403 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10404 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10405 ID.AddInteger(MMO->getFlags());
10406 void *IP = nullptr;
10407 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10408 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10409 return SDValue(E, 0);
10410 }
10411 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10412 AM, ExtTy, isExpanding, MemVT, MMO);
10413 createOperands(N, Ops);
10414
10415 CSEMap.InsertNode(N, IP);
10416 InsertNode(N);
10417 SDValue V(N, 0);
10418 NewSDValueDbgMsg(V, "Creating new node: ", this);
10419 return V;
10420}
10421
10425 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
10426 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10427 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10428 Offset, LD->getMask(), LD->getPassThru(),
10429 LD->getMemoryVT(), LD->getMemOperand(), AM,
10430 LD->getExtensionType(), LD->isExpandingLoad());
10431}
10432
10435 SDValue Mask, EVT MemVT,
10436 MachineMemOperand *MMO,
10437 ISD::MemIndexedMode AM, bool IsTruncating,
10438 bool IsCompressing) {
10439 assert(Chain.getValueType() == MVT::Other &&
10440 "Invalid chain type");
10441 bool Indexed = AM != ISD::UNINDEXED;
10442 assert((Indexed || Offset.isUndef()) &&
10443 "Unindexed masked store with an offset!");
10444 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10445 : getVTList(MVT::Other);
10446 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10448 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10449 ID.AddInteger(MemVT.getRawBits());
10450 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10451 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10452 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10453 ID.AddInteger(MMO->getFlags());
10454 void *IP = nullptr;
10455 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10456 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10457 return SDValue(E, 0);
10458 }
10459 auto *N =
10460 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10461 IsTruncating, IsCompressing, MemVT, MMO);
10462 createOperands(N, Ops);
10463
10464 CSEMap.InsertNode(N, IP);
10465 InsertNode(N);
10466 SDValue V(N, 0);
10467 NewSDValueDbgMsg(V, "Creating new node: ", this);
10468 return V;
10469}
10470
10474 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
10475 assert(ST->getOffset().isUndef() &&
10476 "Masked store is already a indexed store!");
10477 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10478 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10479 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10480}
10481
10484 MachineMemOperand *MMO,
10485 ISD::MemIndexType IndexType,
10486 ISD::LoadExtType ExtTy) {
10487 assert(Ops.size() == 6 && "Incompatible number of operands");
10488
10490 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10491 ID.AddInteger(MemVT.getRawBits());
10492 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10493 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10494 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10495 ID.AddInteger(MMO->getFlags());
10496 void *IP = nullptr;
10497 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10498 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10499 return SDValue(E, 0);
10500 }
10501
10502 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10503 VTs, MemVT, MMO, IndexType, ExtTy);
10504 createOperands(N, Ops);
10505
10506 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10507 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10508 assert(N->getMask().getValueType().getVectorElementCount() ==
10509 N->getValueType(0).getVectorElementCount() &&
10510 "Vector width mismatch between mask and data");
10511 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10512 N->getValueType(0).getVectorElementCount().isScalable() &&
10513 "Scalable flags of index and data do not match");
10515 N->getIndex().getValueType().getVectorElementCount(),
10516 N->getValueType(0).getVectorElementCount()) &&
10517 "Vector width mismatch between index and data");
10518 assert(isa<ConstantSDNode>(N->getScale()) &&
10519 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10520 "Scale should be a constant power of 2");
10521
10522 CSEMap.InsertNode(N, IP);
10523 InsertNode(N);
10524 SDValue V(N, 0);
10525 NewSDValueDbgMsg(V, "Creating new node: ", this);
10526 return V;
10527}
10528
10531 MachineMemOperand *MMO,
10532 ISD::MemIndexType IndexType,
10533 bool IsTrunc) {
10534 assert(Ops.size() == 6 && "Incompatible number of operands");
10535
10537 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10538 ID.AddInteger(MemVT.getRawBits());
10539 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10540 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10541 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10542 ID.AddInteger(MMO->getFlags());
10543 void *IP = nullptr;
10544 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10545 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10546 return SDValue(E, 0);
10547 }
10548
10549 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10550 VTs, MemVT, MMO, IndexType, IsTrunc);
10551 createOperands(N, Ops);
10552
10553 assert(N->getMask().getValueType().getVectorElementCount() ==
10554 N->getValue().getValueType().getVectorElementCount() &&
10555 "Vector width mismatch between mask and data");
10556 assert(
10557 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10558 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10559 "Scalable flags of index and data do not match");
10561 N->getIndex().getValueType().getVectorElementCount(),
10562 N->getValue().getValueType().getVectorElementCount()) &&
10563 "Vector width mismatch between index and data");
10564 assert(isa<ConstantSDNode>(N->getScale()) &&
10565 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10566 "Scale should be a constant power of 2");
10567
10568 CSEMap.InsertNode(N, IP);
10569 InsertNode(N);
10570 SDValue V(N, 0);
10571 NewSDValueDbgMsg(V, "Creating new node: ", this);
10572 return V;
10573}
10574
10576 const SDLoc &dl, ArrayRef<SDValue> Ops,
10577 MachineMemOperand *MMO,
10578 ISD::MemIndexType IndexType) {
10579 assert(Ops.size() == 7 && "Incompatible number of operands");
10580
10583 ID.AddInteger(MemVT.getRawBits());
10584 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10585 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10586 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10587 ID.AddInteger(MMO->getFlags());
10588 void *IP = nullptr;
10589 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10590 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10591 return SDValue(E, 0);
10592 }
10593
10594 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10595 VTs, MemVT, MMO, IndexType);
10596 createOperands(N, Ops);
10597
10598 assert(N->getMask().getValueType().getVectorElementCount() ==
10599 N->getIndex().getValueType().getVectorElementCount() &&
10600 "Vector width mismatch between mask and data");
10601 assert(isa<ConstantSDNode>(N->getScale()) &&
10602 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10603 "Scale should be a constant power of 2");
10604 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10605
10606 CSEMap.InsertNode(N, IP);
10607 InsertNode(N);
10608 SDValue V(N, 0);
10609 NewSDValueDbgMsg(V, "Creating new node: ", this);
10610 return V;
10611}
10612
10614 SDValue Ptr, SDValue Mask, SDValue EVL,
10615 MachineMemOperand *MMO) {
10616 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10617 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10619 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10620 ID.AddInteger(VT.getRawBits());
10621 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10622 VTs, VT, MMO));
10623 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10624 ID.AddInteger(MMO->getFlags());
10625 void *IP = nullptr;
10626 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10627 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10628 return SDValue(E, 0);
10629 }
10630 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10631 VT, MMO);
10632 createOperands(N, Ops);
10633
10634 CSEMap.InsertNode(N, IP);
10635 InsertNode(N);
10636 SDValue V(N, 0);
10637 NewSDValueDbgMsg(V, "Creating new node: ", this);
10638 return V;
10639}
10640
10642 EVT MemVT, MachineMemOperand *MMO) {
10643 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10644 SDVTList VTs = getVTList(MVT::Other);
10645 SDValue Ops[] = {Chain, Ptr};
10648 ID.AddInteger(MemVT.getRawBits());
10649 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10650 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10651 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10652 ID.AddInteger(MMO->getFlags());
10653 void *IP = nullptr;
10654 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10655 return SDValue(E, 0);
10656
10657 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10658 dl.getDebugLoc(), VTs, MemVT, MMO);
10659 createOperands(N, Ops);
10660
10661 CSEMap.InsertNode(N, IP);
10662 InsertNode(N);
10663 SDValue V(N, 0);
10664 NewSDValueDbgMsg(V, "Creating new node: ", this);
10665 return V;
10666}
10667
10669 EVT MemVT, MachineMemOperand *MMO) {
10670 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10671 SDVTList VTs = getVTList(MVT::Other);
10672 SDValue Ops[] = {Chain, Ptr};
10675 ID.AddInteger(MemVT.getRawBits());
10676 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10677 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10678 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10679 ID.AddInteger(MMO->getFlags());
10680 void *IP = nullptr;
10681 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10682 return SDValue(E, 0);
10683
10684 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10685 dl.getDebugLoc(), VTs, MemVT, MMO);
10686 createOperands(N, Ops);
10687
10688 CSEMap.InsertNode(N, IP);
10689 InsertNode(N);
10690 SDValue V(N, 0);
10691 NewSDValueDbgMsg(V, "Creating new node: ", this);
10692 return V;
10693}
10694
10696 // select undef, T, F --> T (if T is a constant), otherwise F
10697 // select, ?, undef, F --> F
10698 // select, ?, T, undef --> T
10699 if (Cond.isUndef())
10700 return isConstantValueOfAnyType(T) ? T : F;
10701 if (T.isUndef())
10702 return F;
10703 if (F.isUndef())
10704 return T;
10705
10706 // select true, T, F --> T
10707 // select false, T, F --> F
10708 if (auto C = isBoolConstant(Cond))
10709 return *C ? T : F;
10710
10711 // select ?, T, T --> T
10712 if (T == F)
10713 return T;
10714
10715 return SDValue();
10716}
10717
10719 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10720 if (X.isUndef())
10721 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10722 // shift X, undef --> undef (because it may shift by the bitwidth)
10723 if (Y.isUndef())
10724 return getUNDEF(X.getValueType());
10725
10726 // shift 0, Y --> 0
10727 // shift X, 0 --> X
10729 return X;
10730
10731 // shift X, C >= bitwidth(X) --> undef
10732 // All vector elements must be too big (or undef) to avoid partial undefs.
10733 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10734 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10735 };
10736 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10737 return getUNDEF(X.getValueType());
10738
10739 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10740 if (X.getValueType().getScalarType() == MVT::i1)
10741 return X;
10742
10743 return SDValue();
10744}
10745
10747 SDNodeFlags Flags) {
10748 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10749 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10750 // operation is poison. That result can be relaxed to undef.
10751 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10752 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10753 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10754 (YC && YC->getValueAPF().isNaN());
10755 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10756 (YC && YC->getValueAPF().isInfinity());
10757
10758 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10759 return getUNDEF(X.getValueType());
10760
10761 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10762 return getUNDEF(X.getValueType());
10763
10764 if (!YC)
10765 return SDValue();
10766
10767 // X + -0.0 --> X
10768 if (Opcode == ISD::FADD)
10769 if (YC->getValueAPF().isNegZero())
10770 return X;
10771
10772 // X - +0.0 --> X
10773 if (Opcode == ISD::FSUB)
10774 if (YC->getValueAPF().isPosZero())
10775 return X;
10776
10777 // X * 1.0 --> X
10778 // X / 1.0 --> X
10779 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10780 if (YC->getValueAPF().isExactlyValue(1.0))
10781 return X;
10782
10783 // X * 0.0 --> 0.0
10784 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10785 if (YC->getValueAPF().isZero())
10786 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10787
10788 return SDValue();
10789}
10790
10792 SDValue Ptr, SDValue SV, unsigned Align) {
10793 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10794 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10795}
10796
10797SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10798 ArrayRef<SDUse> Ops) {
10799 switch (Ops.size()) {
10800 case 0: return getNode(Opcode, DL, VT);
10801 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10802 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10803 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10804 default: break;
10805 }
10806
10807 // Copy from an SDUse array into an SDValue array for use with
10808 // the regular getNode logic.
10809 SmallVector<SDValue, 8> NewOps(Ops);
10810 return getNode(Opcode, DL, VT, NewOps);
10811}
10812
10813SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10814 ArrayRef<SDValue> Ops) {
10815 SDNodeFlags Flags;
10816 if (Inserter)
10817 Flags = Inserter->getFlags();
10818 return getNode(Opcode, DL, VT, Ops, Flags);
10819}
10820
10821SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10822 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10823 unsigned NumOps = Ops.size();
10824 switch (NumOps) {
10825 case 0: return getNode(Opcode, DL, VT);
10826 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10827 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10828 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10829 default: break;
10830 }
10831
10832#ifndef NDEBUG
10833 for (const auto &Op : Ops)
10834 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10835 "Operand is DELETED_NODE!");
10836#endif
10837
10838 switch (Opcode) {
10839 default: break;
10840 case ISD::BUILD_VECTOR:
10841 // Attempt to simplify BUILD_VECTOR.
10842 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10843 return V;
10844 break;
10846 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10847 return V;
10848 break;
10849 case ISD::SELECT_CC:
10850 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10851 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10852 "LHS and RHS of condition must have same type!");
10853 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10854 "True and False arms of SelectCC must have same type!");
10855 assert(Ops[2].getValueType() == VT &&
10856 "select_cc node must be of same type as true and false value!");
10857 assert((!Ops[0].getValueType().isVector() ||
10858 Ops[0].getValueType().getVectorElementCount() ==
10859 VT.getVectorElementCount()) &&
10860 "Expected select_cc with vector result to have the same sized "
10861 "comparison type!");
10862 break;
10863 case ISD::BR_CC:
10864 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10865 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10866 "LHS/RHS of comparison should match types!");
10867 break;
10868 case ISD::VP_ADD:
10869 case ISD::VP_SUB:
10870 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10871 if (VT.getScalarType() == MVT::i1)
10872 Opcode = ISD::VP_XOR;
10873 break;
10874 case ISD::VP_MUL:
10875 // If it is VP_MUL mask operation then turn it to VP_AND
10876 if (VT.getScalarType() == MVT::i1)
10877 Opcode = ISD::VP_AND;
10878 break;
10879 case ISD::VP_REDUCE_MUL:
10880 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10881 if (VT == MVT::i1)
10882 Opcode = ISD::VP_REDUCE_AND;
10883 break;
10884 case ISD::VP_REDUCE_ADD:
10885 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10886 if (VT == MVT::i1)
10887 Opcode = ISD::VP_REDUCE_XOR;
10888 break;
10889 case ISD::VP_REDUCE_SMAX:
10890 case ISD::VP_REDUCE_UMIN:
10891 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10892 // VP_REDUCE_AND.
10893 if (VT == MVT::i1)
10894 Opcode = ISD::VP_REDUCE_AND;
10895 break;
10896 case ISD::VP_REDUCE_SMIN:
10897 case ISD::VP_REDUCE_UMAX:
10898 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10899 // VP_REDUCE_OR.
10900 if (VT == MVT::i1)
10901 Opcode = ISD::VP_REDUCE_OR;
10902 break;
10903 }
10904
10905 // Memoize nodes.
10906 SDNode *N;
10907 SDVTList VTs = getVTList(VT);
10908
10909 if (VT != MVT::Glue) {
10911 AddNodeIDNode(ID, Opcode, VTs, Ops);
10912 void *IP = nullptr;
10913
10914 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10915 E->intersectFlagsWith(Flags);
10916 return SDValue(E, 0);
10917 }
10918
10919 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10920 createOperands(N, Ops);
10921
10922 CSEMap.InsertNode(N, IP);
10923 } else {
10924 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10925 createOperands(N, Ops);
10926 }
10927
10928 N->setFlags(Flags);
10929 InsertNode(N);
10930 SDValue V(N, 0);
10931 NewSDValueDbgMsg(V, "Creating new node: ", this);
10932 return V;
10933}
10934
10935SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10936 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10937 SDNodeFlags Flags;
10938 if (Inserter)
10939 Flags = Inserter->getFlags();
10940 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10941}
10942
10943SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10944 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops,
10945 const SDNodeFlags Flags) {
10946 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10947}
10948
10949SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10950 ArrayRef<SDValue> Ops) {
10951 SDNodeFlags Flags;
10952 if (Inserter)
10953 Flags = Inserter->getFlags();
10954 return getNode(Opcode, DL, VTList, Ops, Flags);
10955}
10956
10957SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10958 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10959 if (VTList.NumVTs == 1)
10960 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10961
10962#ifndef NDEBUG
10963 for (const auto &Op : Ops)
10964 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10965 "Operand is DELETED_NODE!");
10966#endif
10967
10968 switch (Opcode) {
10969 case ISD::SADDO:
10970 case ISD::UADDO:
10971 case ISD::SSUBO:
10972 case ISD::USUBO: {
10973 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10974 "Invalid add/sub overflow op!");
10975 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10976 Ops[0].getValueType() == Ops[1].getValueType() &&
10977 Ops[0].getValueType() == VTList.VTs[0] &&
10978 "Binary operator types must match!");
10979 SDValue N1 = Ops[0], N2 = Ops[1];
10980 canonicalizeCommutativeBinop(Opcode, N1, N2);
10981
10982 // (X +- 0) -> X with zero-overflow.
10983 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10984 /*AllowTruncation*/ true);
10985 if (N2CV && N2CV->isZero()) {
10986 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10987 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10988 }
10989
10990 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
10991 VTList.VTs[1].getScalarType() == MVT::i1) {
10992 SDValue F1 = getFreeze(N1);
10993 SDValue F2 = getFreeze(N2);
10994 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10995 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10996 return getNode(ISD::MERGE_VALUES, DL, VTList,
10997 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10998 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
10999 Flags);
11000 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11001 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11002 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11003 return getNode(ISD::MERGE_VALUES, DL, VTList,
11004 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11005 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11006 Flags);
11007 }
11008 }
11009 break;
11010 }
11011 case ISD::SADDO_CARRY:
11012 case ISD::UADDO_CARRY:
11013 case ISD::SSUBO_CARRY:
11014 case ISD::USUBO_CARRY:
11015 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11016 "Invalid add/sub overflow op!");
11017 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11018 Ops[0].getValueType() == Ops[1].getValueType() &&
11019 Ops[0].getValueType() == VTList.VTs[0] &&
11020 Ops[2].getValueType() == VTList.VTs[1] &&
11021 "Binary operator types must match!");
11022 break;
11023 case ISD::SMUL_LOHI:
11024 case ISD::UMUL_LOHI: {
11025 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11026 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11027 VTList.VTs[0] == Ops[0].getValueType() &&
11028 VTList.VTs[0] == Ops[1].getValueType() &&
11029 "Binary operator types must match!");
11030 // Constant fold.
11031 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
11032 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
11033 if (LHS && RHS) {
11034 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11035 unsigned OutWidth = Width * 2;
11036 APInt Val = LHS->getAPIntValue();
11037 APInt Mul = RHS->getAPIntValue();
11038 if (Opcode == ISD::SMUL_LOHI) {
11039 Val = Val.sext(OutWidth);
11040 Mul = Mul.sext(OutWidth);
11041 } else {
11042 Val = Val.zext(OutWidth);
11043 Mul = Mul.zext(OutWidth);
11044 }
11045 Val *= Mul;
11046
11047 SDValue Hi =
11048 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11049 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11050 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11051 }
11052 break;
11053 }
11054 case ISD::FFREXP: {
11055 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11056 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11057 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11058
11059 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
11060 int FrexpExp;
11061 APFloat FrexpMant =
11062 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11063 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11064 SDValue Result1 =
11065 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
11066 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11067 }
11068
11069 break;
11070 }
11072 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11073 "Invalid STRICT_FP_EXTEND!");
11074 assert(VTList.VTs[0].isFloatingPoint() &&
11075 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11076 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11077 "STRICT_FP_EXTEND result type should be vector iff the operand "
11078 "type is vector!");
11079 assert((!VTList.VTs[0].isVector() ||
11080 VTList.VTs[0].getVectorElementCount() ==
11081 Ops[1].getValueType().getVectorElementCount()) &&
11082 "Vector element count mismatch!");
11083 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11084 "Invalid fpext node, dst <= src!");
11085 break;
11087 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11088 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11089 "STRICT_FP_ROUND result type should be vector iff the operand "
11090 "type is vector!");
11091 assert((!VTList.VTs[0].isVector() ||
11092 VTList.VTs[0].getVectorElementCount() ==
11093 Ops[1].getValueType().getVectorElementCount()) &&
11094 "Vector element count mismatch!");
11095 assert(VTList.VTs[0].isFloatingPoint() &&
11096 Ops[1].getValueType().isFloatingPoint() &&
11097 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11098 Ops[2].getOpcode() == ISD::TargetConstant &&
11099 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11100 "Invalid STRICT_FP_ROUND!");
11101 break;
11102 }
11103
11104 // Memoize the node unless it returns a glue result.
11105 SDNode *N;
11106 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11108 AddNodeIDNode(ID, Opcode, VTList, Ops);
11109 void *IP = nullptr;
11110 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11111 E->intersectFlagsWith(Flags);
11112 return SDValue(E, 0);
11113 }
11114
11115 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11116 createOperands(N, Ops);
11117 CSEMap.InsertNode(N, IP);
11118 } else {
11119 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11120 createOperands(N, Ops);
11121 }
11122
11123 N->setFlags(Flags);
11124 InsertNode(N);
11125 SDValue V(N, 0);
11126 NewSDValueDbgMsg(V, "Creating new node: ", this);
11127 return V;
11128}
11129
11130SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11131 SDVTList VTList) {
11132 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11133}
11134
11135SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11136 SDValue N1) {
11137 SDValue Ops[] = { N1 };
11138 return getNode(Opcode, DL, VTList, Ops);
11139}
11140
11141SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11142 SDValue N1, SDValue N2) {
11143 SDValue Ops[] = { N1, N2 };
11144 return getNode(Opcode, DL, VTList, Ops);
11145}
11146
11147SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11148 SDValue N1, SDValue N2, SDValue N3) {
11149 SDValue Ops[] = { N1, N2, N3 };
11150 return getNode(Opcode, DL, VTList, Ops);
11151}
11152
11153SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11154 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11155 SDValue Ops[] = { N1, N2, N3, N4 };
11156 return getNode(Opcode, DL, VTList, Ops);
11157}
11158
11159SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11160 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11161 SDValue N5) {
11162 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11163 return getNode(Opcode, DL, VTList, Ops);
11164}
11165
11167 if (!VT.isExtended())
11168 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11169
11170 return makeVTList(&(*EVTs.insert(VT).first), 1);
11171}
11172
11175 ID.AddInteger(2U);
11176 ID.AddInteger(VT1.getRawBits());
11177 ID.AddInteger(VT2.getRawBits());
11178
11179 void *IP = nullptr;
11180 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11181 if (!Result) {
11182 EVT *Array = Allocator.Allocate<EVT>(2);
11183 Array[0] = VT1;
11184 Array[1] = VT2;
11185 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11186 VTListMap.InsertNode(Result, IP);
11187 }
11188 return Result->getSDVTList();
11189}
11190
11193 ID.AddInteger(3U);
11194 ID.AddInteger(VT1.getRawBits());
11195 ID.AddInteger(VT2.getRawBits());
11196 ID.AddInteger(VT3.getRawBits());
11197
11198 void *IP = nullptr;
11199 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11200 if (!Result) {
11201 EVT *Array = Allocator.Allocate<EVT>(3);
11202 Array[0] = VT1;
11203 Array[1] = VT2;
11204 Array[2] = VT3;
11205 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11206 VTListMap.InsertNode(Result, IP);
11207 }
11208 return Result->getSDVTList();
11209}
11210
11213 ID.AddInteger(4U);
11214 ID.AddInteger(VT1.getRawBits());
11215 ID.AddInteger(VT2.getRawBits());
11216 ID.AddInteger(VT3.getRawBits());
11217 ID.AddInteger(VT4.getRawBits());
11218
11219 void *IP = nullptr;
11220 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11221 if (!Result) {
11222 EVT *Array = Allocator.Allocate<EVT>(4);
11223 Array[0] = VT1;
11224 Array[1] = VT2;
11225 Array[2] = VT3;
11226 Array[3] = VT4;
11227 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11228 VTListMap.InsertNode(Result, IP);
11229 }
11230 return Result->getSDVTList();
11231}
11232
11234 unsigned NumVTs = VTs.size();
11236 ID.AddInteger(NumVTs);
11237 for (unsigned index = 0; index < NumVTs; index++) {
11238 ID.AddInteger(VTs[index].getRawBits());
11239 }
11240
11241 void *IP = nullptr;
11242 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11243 if (!Result) {
11244 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11245 llvm::copy(VTs, Array);
11246 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11247 VTListMap.InsertNode(Result, IP);
11248 }
11249 return Result->getSDVTList();
11250}
11251
11252
11253/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11254/// specified operands. If the resultant node already exists in the DAG,
11255/// this does not modify the specified node, instead it returns the node that
11256/// already exists. If the resultant node does not exist in the DAG, the
11257/// input node is returned. As a degenerate case, if you specify the same
11258/// input operands as the node already has, the input node is returned.
11260 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11261
11262 // Check to see if there is no change.
11263 if (Op == N->getOperand(0)) return N;
11264
11265 // See if the modified node already exists.
11266 void *InsertPos = nullptr;
11267 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11268 return Existing;
11269
11270 // Nope it doesn't. Remove the node from its current place in the maps.
11271 if (InsertPos)
11272 if (!RemoveNodeFromCSEMaps(N))
11273 InsertPos = nullptr;
11274
11275 // Now we update the operands.
11276 N->OperandList[0].set(Op);
11277
11279 // If this gets put into a CSE map, add it.
11280 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11281 return N;
11282}
11283
11285 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11286
11287 // Check to see if there is no change.
11288 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11289 return N; // No operands changed, just return the input node.
11290
11291 // See if the modified node already exists.
11292 void *InsertPos = nullptr;
11293 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11294 return Existing;
11295
11296 // Nope it doesn't. Remove the node from its current place in the maps.
11297 if (InsertPos)
11298 if (!RemoveNodeFromCSEMaps(N))
11299 InsertPos = nullptr;
11300
11301 // Now we update the operands.
11302 if (N->OperandList[0] != Op1)
11303 N->OperandList[0].set(Op1);
11304 if (N->OperandList[1] != Op2)
11305 N->OperandList[1].set(Op2);
11306
11308 // If this gets put into a CSE map, add it.
11309 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11310 return N;
11311}
11312
11315 SDValue Ops[] = { Op1, Op2, Op3 };
11316 return UpdateNodeOperands(N, Ops);
11317}
11318
11321 SDValue Op3, SDValue Op4) {
11322 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11323 return UpdateNodeOperands(N, Ops);
11324}
11325
11328 SDValue Op3, SDValue Op4, SDValue Op5) {
11329 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11330 return UpdateNodeOperands(N, Ops);
11331}
11332
11335 unsigned NumOps = Ops.size();
11336 assert(N->getNumOperands() == NumOps &&
11337 "Update with wrong number of operands");
11338
11339 // If no operands changed just return the input node.
11340 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11341 return N;
11342
11343 // See if the modified node already exists.
11344 void *InsertPos = nullptr;
11345 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11346 return Existing;
11347
11348 // Nope it doesn't. Remove the node from its current place in the maps.
11349 if (InsertPos)
11350 if (!RemoveNodeFromCSEMaps(N))
11351 InsertPos = nullptr;
11352
11353 // Now we update the operands.
11354 for (unsigned i = 0; i != NumOps; ++i)
11355 if (N->OperandList[i] != Ops[i])
11356 N->OperandList[i].set(Ops[i]);
11357
11359 // If this gets put into a CSE map, add it.
11360 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11361 return N;
11362}
11363
11364/// DropOperands - Release the operands and set this node to have
11365/// zero operands.
11367 // Unlike the code in MorphNodeTo that does this, we don't need to
11368 // watch for dead nodes here.
11369 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11370 SDUse &Use = *I++;
11371 Use.set(SDValue());
11372 }
11373}
11374
11376 ArrayRef<MachineMemOperand *> NewMemRefs) {
11377 if (NewMemRefs.empty()) {
11378 N->clearMemRefs();
11379 return;
11380 }
11381
11382 // Check if we can avoid allocating by storing a single reference directly.
11383 if (NewMemRefs.size() == 1) {
11384 N->MemRefs = NewMemRefs[0];
11385 N->NumMemRefs = 1;
11386 return;
11387 }
11388
11389 MachineMemOperand **MemRefsBuffer =
11390 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11391 llvm::copy(NewMemRefs, MemRefsBuffer);
11392 N->MemRefs = MemRefsBuffer;
11393 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11394}
11395
11396/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11397/// machine opcode.
11398///
11400 EVT VT) {
11401 SDVTList VTs = getVTList(VT);
11402 return SelectNodeTo(N, MachineOpc, VTs, {});
11403}
11404
11406 EVT VT, SDValue Op1) {
11407 SDVTList VTs = getVTList(VT);
11408 SDValue Ops[] = { Op1 };
11409 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11410}
11411
11413 EVT VT, SDValue Op1,
11414 SDValue Op2) {
11415 SDVTList VTs = getVTList(VT);
11416 SDValue Ops[] = { Op1, Op2 };
11417 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11418}
11419
11421 EVT VT, SDValue Op1,
11422 SDValue Op2, SDValue Op3) {
11423 SDVTList VTs = getVTList(VT);
11424 SDValue Ops[] = { Op1, Op2, Op3 };
11425 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11426}
11427
11429 EVT VT, ArrayRef<SDValue> Ops) {
11430 SDVTList VTs = getVTList(VT);
11431 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11432}
11433
11435 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11436 SDVTList VTs = getVTList(VT1, VT2);
11437 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11438}
11439
11441 EVT VT1, EVT VT2) {
11442 SDVTList VTs = getVTList(VT1, VT2);
11443 return SelectNodeTo(N, MachineOpc, VTs, {});
11444}
11445
11447 EVT VT1, EVT VT2, EVT VT3,
11448 ArrayRef<SDValue> Ops) {
11449 SDVTList VTs = getVTList(VT1, VT2, VT3);
11450 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11451}
11452
11454 EVT VT1, EVT VT2,
11455 SDValue Op1, SDValue Op2) {
11456 SDVTList VTs = getVTList(VT1, VT2);
11457 SDValue Ops[] = { Op1, Op2 };
11458 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11459}
11460
11462 SDVTList VTs,ArrayRef<SDValue> Ops) {
11463 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11464 // Reset the NodeID to -1.
11465 New->setNodeId(-1);
11466 if (New != N) {
11467 ReplaceAllUsesWith(N, New);
11469 }
11470 return New;
11471}
11472
11473/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11474/// the line number information on the merged node since it is not possible to
11475/// preserve the information that operation is associated with multiple lines.
11476/// This will make the debugger working better at -O0, were there is a higher
11477/// probability having other instructions associated with that line.
11478///
11479/// For IROrder, we keep the smaller of the two
11480SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11481 DebugLoc NLoc = N->getDebugLoc();
11482 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11483 N->setDebugLoc(DebugLoc());
11484 }
11485 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11486 N->setIROrder(Order);
11487 return N;
11488}
11489
11490/// MorphNodeTo - This *mutates* the specified node to have the specified
11491/// return type, opcode, and operands.
11492///
11493/// Note that MorphNodeTo returns the resultant node. If there is already a
11494/// node of the specified opcode and operands, it returns that node instead of
11495/// the current one. Note that the SDLoc need not be the same.
11496///
11497/// Using MorphNodeTo is faster than creating a new node and swapping it in
11498/// with ReplaceAllUsesWith both because it often avoids allocating a new
11499/// node, and because it doesn't require CSE recalculation for any of
11500/// the node's users.
11501///
11502/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11503/// As a consequence it isn't appropriate to use from within the DAG combiner or
11504/// the legalizer which maintain worklists that would need to be updated when
11505/// deleting things.
11507 SDVTList VTs, ArrayRef<SDValue> Ops) {
11508 // If an identical node already exists, use it.
11509 void *IP = nullptr;
11510 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11512 AddNodeIDNode(ID, Opc, VTs, Ops);
11513 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11514 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11515 }
11516
11517 if (!RemoveNodeFromCSEMaps(N))
11518 IP = nullptr;
11519
11520 // Start the morphing.
11521 N->NodeType = Opc;
11522 N->ValueList = VTs.VTs;
11523 N->NumValues = VTs.NumVTs;
11524
11525 // Clear the operands list, updating used nodes to remove this from their
11526 // use list. Keep track of any operands that become dead as a result.
11527 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11528 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11529 SDUse &Use = *I++;
11530 SDNode *Used = Use.getNode();
11531 Use.set(SDValue());
11532 if (Used->use_empty())
11533 DeadNodeSet.insert(Used);
11534 }
11535
11536 // For MachineNode, initialize the memory references information.
11537 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
11538 MN->clearMemRefs();
11539
11540 // Swap for an appropriately sized array from the recycler.
11541 removeOperands(N);
11542 createOperands(N, Ops);
11543
11544 // Delete any nodes that are still dead after adding the uses for the
11545 // new operands.
11546 if (!DeadNodeSet.empty()) {
11547 SmallVector<SDNode *, 16> DeadNodes;
11548 for (SDNode *N : DeadNodeSet)
11549 if (N->use_empty())
11550 DeadNodes.push_back(N);
11551 RemoveDeadNodes(DeadNodes);
11552 }
11553
11554 if (IP)
11555 CSEMap.InsertNode(N, IP); // Memoize the new node.
11556 return N;
11557}
11558
11560 unsigned OrigOpc = Node->getOpcode();
11561 unsigned NewOpc;
11562 switch (OrigOpc) {
11563 default:
11564 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11565#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11566 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11567#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11568 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11569#include "llvm/IR/ConstrainedOps.def"
11570 }
11571
11572 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11573
11574 // We're taking this node out of the chain, so we need to re-link things.
11575 SDValue InputChain = Node->getOperand(0);
11576 SDValue OutputChain = SDValue(Node, 1);
11577 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11578
11580 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11581 Ops.push_back(Node->getOperand(i));
11582
11583 SDVTList VTs = getVTList(Node->getValueType(0));
11584 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11585
11586 // MorphNodeTo can operate in two ways: if an existing node with the
11587 // specified operands exists, it can just return it. Otherwise, it
11588 // updates the node in place to have the requested operands.
11589 if (Res == Node) {
11590 // If we updated the node in place, reset the node ID. To the isel,
11591 // this should be just like a newly allocated machine node.
11592 Res->setNodeId(-1);
11593 } else {
11594 ReplaceAllUsesWith(Node, Res);
11595 RemoveDeadNode(Node);
11596 }
11597
11598 return Res;
11599}
11600
11601/// getMachineNode - These are used for target selectors to create a new node
11602/// with specified return type(s), MachineInstr opcode, and operands.
11603///
11604/// Note that getMachineNode returns the resultant node. If there is already a
11605/// node of the specified opcode and operands, it returns that node instead of
11606/// the current one.
11608 EVT VT) {
11609 SDVTList VTs = getVTList(VT);
11610 return getMachineNode(Opcode, dl, VTs, {});
11611}
11612
11614 EVT VT, SDValue Op1) {
11615 SDVTList VTs = getVTList(VT);
11616 SDValue Ops[] = { Op1 };
11617 return getMachineNode(Opcode, dl, VTs, Ops);
11618}
11619
11621 EVT VT, SDValue Op1, SDValue Op2) {
11622 SDVTList VTs = getVTList(VT);
11623 SDValue Ops[] = { Op1, Op2 };
11624 return getMachineNode(Opcode, dl, VTs, Ops);
11625}
11626
11628 EVT VT, SDValue Op1, SDValue Op2,
11629 SDValue Op3) {
11630 SDVTList VTs = getVTList(VT);
11631 SDValue Ops[] = { Op1, Op2, Op3 };
11632 return getMachineNode(Opcode, dl, VTs, Ops);
11633}
11634
11636 EVT VT, ArrayRef<SDValue> Ops) {
11637 SDVTList VTs = getVTList(VT);
11638 return getMachineNode(Opcode, dl, VTs, Ops);
11639}
11640
11642 EVT VT1, EVT VT2, SDValue Op1,
11643 SDValue Op2) {
11644 SDVTList VTs = getVTList(VT1, VT2);
11645 SDValue Ops[] = { Op1, Op2 };
11646 return getMachineNode(Opcode, dl, VTs, Ops);
11647}
11648
11650 EVT VT1, EVT VT2, SDValue Op1,
11651 SDValue Op2, SDValue Op3) {
11652 SDVTList VTs = getVTList(VT1, VT2);
11653 SDValue Ops[] = { Op1, Op2, Op3 };
11654 return getMachineNode(Opcode, dl, VTs, Ops);
11655}
11656
11658 EVT VT1, EVT VT2,
11659 ArrayRef<SDValue> Ops) {
11660 SDVTList VTs = getVTList(VT1, VT2);
11661 return getMachineNode(Opcode, dl, VTs, Ops);
11662}
11663
11665 EVT VT1, EVT VT2, EVT VT3,
11666 SDValue Op1, SDValue Op2) {
11667 SDVTList VTs = getVTList(VT1, VT2, VT3);
11668 SDValue Ops[] = { Op1, Op2 };
11669 return getMachineNode(Opcode, dl, VTs, Ops);
11670}
11671
11673 EVT VT1, EVT VT2, EVT VT3,
11674 SDValue Op1, SDValue Op2,
11675 SDValue Op3) {
11676 SDVTList VTs = getVTList(VT1, VT2, VT3);
11677 SDValue Ops[] = { Op1, Op2, Op3 };
11678 return getMachineNode(Opcode, dl, VTs, Ops);
11679}
11680
11682 EVT VT1, EVT VT2, EVT VT3,
11683 ArrayRef<SDValue> Ops) {
11684 SDVTList VTs = getVTList(VT1, VT2, VT3);
11685 return getMachineNode(Opcode, dl, VTs, Ops);
11686}
11687
11689 ArrayRef<EVT> ResultTys,
11690 ArrayRef<SDValue> Ops) {
11691 SDVTList VTs = getVTList(ResultTys);
11692 return getMachineNode(Opcode, dl, VTs, Ops);
11693}
11694
11696 SDVTList VTs,
11697 ArrayRef<SDValue> Ops) {
11698 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11700 void *IP = nullptr;
11701
11702 if (DoCSE) {
11704 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11705 IP = nullptr;
11706 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11707 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11708 }
11709 }
11710
11711 // Allocate a new MachineSDNode.
11712 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11713 createOperands(N, Ops);
11714
11715 if (DoCSE)
11716 CSEMap.InsertNode(N, IP);
11717
11718 InsertNode(N);
11719 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11720 return N;
11721}
11722
11723/// getTargetExtractSubreg - A convenience function for creating
11724/// TargetOpcode::EXTRACT_SUBREG nodes.
11726 SDValue Operand) {
11727 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11728 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11729 VT, Operand, SRIdxVal);
11730 return SDValue(Subreg, 0);
11731}
11732
11733/// getTargetInsertSubreg - A convenience function for creating
11734/// TargetOpcode::INSERT_SUBREG nodes.
11736 SDValue Operand, SDValue Subreg) {
11737 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11738 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11739 VT, Operand, Subreg, SRIdxVal);
11740 return SDValue(Result, 0);
11741}
11742
11743/// getNodeIfExists - Get the specified node if it's already available, or
11744/// else return NULL.
11746 ArrayRef<SDValue> Ops) {
11747 SDNodeFlags Flags;
11748 if (Inserter)
11749 Flags = Inserter->getFlags();
11750 return getNodeIfExists(Opcode, VTList, Ops, Flags);
11751}
11752
11755 const SDNodeFlags Flags) {
11756 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11758 AddNodeIDNode(ID, Opcode, VTList, Ops);
11759 void *IP = nullptr;
11760 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
11761 E->intersectFlagsWith(Flags);
11762 return E;
11763 }
11764 }
11765 return nullptr;
11766}
11767
11768/// doesNodeExist - Check if a node exists without modifying its flags.
11769bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11770 ArrayRef<SDValue> Ops) {
11771 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11773 AddNodeIDNode(ID, Opcode, VTList, Ops);
11774 void *IP = nullptr;
11775 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11776 return true;
11777 }
11778 return false;
11779}
11780
11781/// getDbgValue - Creates a SDDbgValue node.
11782///
11783/// SDNode
11785 SDNode *N, unsigned R, bool IsIndirect,
11786 const DebugLoc &DL, unsigned O) {
11787 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11788 "Expected inlined-at fields to agree");
11789 return new (DbgInfo->getAlloc())
11790 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11791 {}, IsIndirect, DL, O,
11792 /*IsVariadic=*/false);
11793}
11794
11795/// Constant
11797 DIExpression *Expr,
11798 const Value *C,
11799 const DebugLoc &DL, unsigned O) {
11800 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11801 "Expected inlined-at fields to agree");
11802 return new (DbgInfo->getAlloc())
11803 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11804 /*IsIndirect=*/false, DL, O,
11805 /*IsVariadic=*/false);
11806}
11807
11808/// FrameIndex
11810 DIExpression *Expr, unsigned FI,
11811 bool IsIndirect,
11812 const DebugLoc &DL,
11813 unsigned O) {
11814 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11815 "Expected inlined-at fields to agree");
11816 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11817}
11818
11819/// FrameIndex with dependencies
11821 DIExpression *Expr, unsigned FI,
11822 ArrayRef<SDNode *> Dependencies,
11823 bool IsIndirect,
11824 const DebugLoc &DL,
11825 unsigned O) {
11826 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11827 "Expected inlined-at fields to agree");
11828 return new (DbgInfo->getAlloc())
11829 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11830 Dependencies, IsIndirect, DL, O,
11831 /*IsVariadic=*/false);
11832}
11833
11834/// VReg
11836 Register VReg, bool IsIndirect,
11837 const DebugLoc &DL, unsigned O) {
11838 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11839 "Expected inlined-at fields to agree");
11840 return new (DbgInfo->getAlloc())
11841 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11842 {}, IsIndirect, DL, O,
11843 /*IsVariadic=*/false);
11844}
11845
11848 ArrayRef<SDNode *> Dependencies,
11849 bool IsIndirect, const DebugLoc &DL,
11850 unsigned O, bool IsVariadic) {
11851 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11852 "Expected inlined-at fields to agree");
11853 return new (DbgInfo->getAlloc())
11854 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11855 DL, O, IsVariadic);
11856}
11857
11859 unsigned OffsetInBits, unsigned SizeInBits,
11860 bool InvalidateDbg) {
11861 SDNode *FromNode = From.getNode();
11862 SDNode *ToNode = To.getNode();
11863 assert(FromNode && ToNode && "Can't modify dbg values");
11864
11865 // PR35338
11866 // TODO: assert(From != To && "Redundant dbg value transfer");
11867 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11868 if (From == To || FromNode == ToNode)
11869 return;
11870
11871 if (!FromNode->getHasDebugValue())
11872 return;
11873
11874 SDDbgOperand FromLocOp =
11875 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11877
11879 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11880 if (Dbg->isInvalidated())
11881 continue;
11882
11883 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11884
11885 // Create a new location ops vector that is equal to the old vector, but
11886 // with each instance of FromLocOp replaced with ToLocOp.
11887 bool Changed = false;
11888 auto NewLocOps = Dbg->copyLocationOps();
11889 std::replace_if(
11890 NewLocOps.begin(), NewLocOps.end(),
11891 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11892 bool Match = Op == FromLocOp;
11893 Changed |= Match;
11894 return Match;
11895 },
11896 ToLocOp);
11897 // Ignore this SDDbgValue if we didn't find a matching location.
11898 if (!Changed)
11899 continue;
11900
11901 DIVariable *Var = Dbg->getVariable();
11902 auto *Expr = Dbg->getExpression();
11903 // If a fragment is requested, update the expression.
11904 if (SizeInBits) {
11905 // When splitting a larger (e.g., sign-extended) value whose
11906 // lower bits are described with an SDDbgValue, do not attempt
11907 // to transfer the SDDbgValue to the upper bits.
11908 if (auto FI = Expr->getFragmentInfo())
11909 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11910 continue;
11911 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11912 SizeInBits);
11913 if (!Fragment)
11914 continue;
11915 Expr = *Fragment;
11916 }
11917
11918 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11919 // Clone the SDDbgValue and move it to To.
11920 SDDbgValue *Clone = getDbgValueList(
11921 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11922 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11923 Dbg->isVariadic());
11924 ClonedDVs.push_back(Clone);
11925
11926 if (InvalidateDbg) {
11927 // Invalidate value and indicate the SDDbgValue should not be emitted.
11928 Dbg->setIsInvalidated();
11929 Dbg->setIsEmitted();
11930 }
11931 }
11932
11933 for (SDDbgValue *Dbg : ClonedDVs) {
11934 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11935 "Transferred DbgValues should depend on the new SDNode");
11936 AddDbgValue(Dbg, false);
11937 }
11938}
11939
11941 if (!N.getHasDebugValue())
11942 return;
11943
11944 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
11945 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
11946 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
11947 return SDDbgOperand::fromNode(Node, ResNo);
11948 };
11949
11951 for (auto *DV : GetDbgValues(&N)) {
11952 if (DV->isInvalidated())
11953 continue;
11954 switch (N.getOpcode()) {
11955 default:
11956 break;
11957 case ISD::ADD: {
11958 SDValue N0 = N.getOperand(0);
11959 SDValue N1 = N.getOperand(1);
11960 if (!isa<ConstantSDNode>(N0)) {
11961 bool RHSConstant = isa<ConstantSDNode>(N1);
11963 if (RHSConstant)
11964 Offset = N.getConstantOperandVal(1);
11965 // We are not allowed to turn indirect debug values variadic, so
11966 // don't salvage those.
11967 if (!RHSConstant && DV->isIndirect())
11968 continue;
11969
11970 // Rewrite an ADD constant node into a DIExpression. Since we are
11971 // performing arithmetic to compute the variable's *value* in the
11972 // DIExpression, we need to mark the expression with a
11973 // DW_OP_stack_value.
11974 auto *DIExpr = DV->getExpression();
11975 auto NewLocOps = DV->copyLocationOps();
11976 bool Changed = false;
11977 size_t OrigLocOpsSize = NewLocOps.size();
11978 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11979 // We're not given a ResNo to compare against because the whole
11980 // node is going away. We know that any ISD::ADD only has one
11981 // result, so we can assume any node match is using the result.
11982 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11983 NewLocOps[i].getSDNode() != &N)
11984 continue;
11985 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
11986 if (RHSConstant) {
11989 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
11990 } else {
11991 // Convert to a variadic expression (if not already).
11992 // convertToVariadicExpression() returns a const pointer, so we use
11993 // a temporary const variable here.
11994 const auto *TmpDIExpr =
11998 ExprOps.push_back(NewLocOps.size());
11999 ExprOps.push_back(dwarf::DW_OP_plus);
12002 NewLocOps.push_back(RHS);
12003 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12004 }
12005 Changed = true;
12006 }
12007 (void)Changed;
12008 assert(Changed && "Salvage target doesn't use N");
12009
12010 bool IsVariadic =
12011 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12012
12013 auto AdditionalDependencies = DV->getAdditionalDependencies();
12014 SDDbgValue *Clone = getDbgValueList(
12015 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12016 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12017 ClonedDVs.push_back(Clone);
12018 DV->setIsInvalidated();
12019 DV->setIsEmitted();
12020 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12021 N0.getNode()->dumprFull(this);
12022 dbgs() << " into " << *DIExpr << '\n');
12023 }
12024 break;
12025 }
12026 case ISD::TRUNCATE: {
12027 SDValue N0 = N.getOperand(0);
12028 TypeSize FromSize = N0.getValueSizeInBits();
12029 TypeSize ToSize = N.getValueSizeInBits(0);
12030
12031 DIExpression *DbgExpression = DV->getExpression();
12032 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12033 auto NewLocOps = DV->copyLocationOps();
12034 bool Changed = false;
12035 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12036 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12037 NewLocOps[i].getSDNode() != &N)
12038 continue;
12039
12040 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12041 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12042 Changed = true;
12043 }
12044 assert(Changed && "Salvage target doesn't use N");
12045 (void)Changed;
12046
12047 SDDbgValue *Clone =
12048 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12049 DV->getAdditionalDependencies(), DV->isIndirect(),
12050 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12051
12052 ClonedDVs.push_back(Clone);
12053 DV->setIsInvalidated();
12054 DV->setIsEmitted();
12055 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12056 dbgs() << " into " << *DbgExpression << '\n');
12057 break;
12058 }
12059 }
12060 }
12061
12062 for (SDDbgValue *Dbg : ClonedDVs) {
12063 assert((!Dbg->getSDNodes().empty() ||
12064 llvm::any_of(Dbg->getLocationOps(),
12065 [&](const SDDbgOperand &Op) {
12066 return Op.getKind() == SDDbgOperand::FRAMEIX;
12067 })) &&
12068 "Salvaged DbgValue should depend on a new SDNode");
12069 AddDbgValue(Dbg, false);
12070 }
12071}
12072
12073/// Creates a SDDbgLabel node.
12075 const DebugLoc &DL, unsigned O) {
12076 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12077 "Expected inlined-at fields to agree");
12078 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12079}
12080
12081namespace {
12082
12083/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12084/// pointed to by a use iterator is deleted, increment the use iterator
12085/// so that it doesn't dangle.
12086///
12087class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12090
12091 void NodeDeleted(SDNode *N, SDNode *E) override {
12092 // Increment the iterator as needed.
12093 while (UI != UE && N == UI->getUser())
12094 ++UI;
12095 }
12096
12097public:
12098 RAUWUpdateListener(SelectionDAG &d,
12101 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12102};
12103
12104} // end anonymous namespace
12105
12106/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12107/// This can cause recursive merging of nodes in the DAG.
12108///
12109/// This version assumes From has a single result value.
12110///
12112 SDNode *From = FromN.getNode();
12113 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12114 "Cannot replace with this method!");
12115 assert(From != To.getNode() && "Cannot replace uses of with self");
12116
12117 // Preserve Debug Values
12118 transferDbgValues(FromN, To);
12119 // Preserve extra info.
12120 copyExtraInfo(From, To.getNode());
12121
12122 // Iterate over all the existing uses of From. New uses will be added
12123 // to the beginning of the use list, which we avoid visiting.
12124 // This specifically avoids visiting uses of From that arise while the
12125 // replacement is happening, because any such uses would be the result
12126 // of CSE: If an existing node looks like From after one of its operands
12127 // is replaced by To, we don't want to replace of all its users with To
12128 // too. See PR3018 for more info.
12129 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12130 RAUWUpdateListener Listener(*this, UI, UE);
12131 while (UI != UE) {
12132 SDNode *User = UI->getUser();
12133
12134 // This node is about to morph, remove its old self from the CSE maps.
12135 RemoveNodeFromCSEMaps(User);
12136
12137 // A user can appear in a use list multiple times, and when this
12138 // happens the uses are usually next to each other in the list.
12139 // To help reduce the number of CSE recomputations, process all
12140 // the uses of this user that we can find this way.
12141 do {
12142 SDUse &Use = *UI;
12143 ++UI;
12144 Use.set(To);
12145 if (To->isDivergent() != From->isDivergent())
12147 } while (UI != UE && UI->getUser() == User);
12148 // Now that we have modified User, add it back to the CSE maps. If it
12149 // already exists there, recursively merge the results together.
12150 AddModifiedNodeToCSEMaps(User);
12151 }
12152
12153 // If we just RAUW'd the root, take note.
12154 if (FromN == getRoot())
12155 setRoot(To);
12156}
12157
12158/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12159/// This can cause recursive merging of nodes in the DAG.
12160///
12161/// This version assumes that for each value of From, there is a
12162/// corresponding value in To in the same position with the same type.
12163///
12165#ifndef NDEBUG
12166 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12167 assert((!From->hasAnyUseOfValue(i) ||
12168 From->getValueType(i) == To->getValueType(i)) &&
12169 "Cannot use this version of ReplaceAllUsesWith!");
12170#endif
12171
12172 // Handle the trivial case.
12173 if (From == To)
12174 return;
12175
12176 // Preserve Debug Info. Only do this if there's a use.
12177 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12178 if (From->hasAnyUseOfValue(i)) {
12179 assert((i < To->getNumValues()) && "Invalid To location");
12181 }
12182 // Preserve extra info.
12183 copyExtraInfo(From, To);
12184
12185 // Iterate over just the existing users of From. See the comments in
12186 // the ReplaceAllUsesWith above.
12187 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12188 RAUWUpdateListener Listener(*this, UI, UE);
12189 while (UI != UE) {
12190 SDNode *User = UI->getUser();
12191
12192 // This node is about to morph, remove its old self from the CSE maps.
12193 RemoveNodeFromCSEMaps(User);
12194
12195 // A user can appear in a use list multiple times, and when this
12196 // happens the uses are usually next to each other in the list.
12197 // To help reduce the number of CSE recomputations, process all
12198 // the uses of this user that we can find this way.
12199 do {
12200 SDUse &Use = *UI;
12201 ++UI;
12202 Use.setNode(To);
12203 if (To->isDivergent() != From->isDivergent())
12205 } while (UI != UE && UI->getUser() == User);
12206
12207 // Now that we have modified User, add it back to the CSE maps. If it
12208 // already exists there, recursively merge the results together.
12209 AddModifiedNodeToCSEMaps(User);
12210 }
12211
12212 // If we just RAUW'd the root, take note.
12213 if (From == getRoot().getNode())
12214 setRoot(SDValue(To, getRoot().getResNo()));
12215}
12216
12217/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12218/// This can cause recursive merging of nodes in the DAG.
12219///
12220/// This version can replace From with any result values. To must match the
12221/// number and types of values returned by From.
12223 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12224 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12225
12226 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12227 // Preserve Debug Info.
12228 transferDbgValues(SDValue(From, i), To[i]);
12229 // Preserve extra info.
12230 copyExtraInfo(From, To[i].getNode());
12231 }
12232
12233 // Iterate over just the existing users of From. See the comments in
12234 // the ReplaceAllUsesWith above.
12235 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12236 RAUWUpdateListener Listener(*this, UI, UE);
12237 while (UI != UE) {
12238 SDNode *User = UI->getUser();
12239
12240 // This node is about to morph, remove its old self from the CSE maps.
12241 RemoveNodeFromCSEMaps(User);
12242
12243 // A user can appear in a use list multiple times, and when this happens the
12244 // uses are usually next to each other in the list. To help reduce the
12245 // number of CSE and divergence recomputations, process all the uses of this
12246 // user that we can find this way.
12247 bool To_IsDivergent = false;
12248 do {
12249 SDUse &Use = *UI;
12250 const SDValue &ToOp = To[Use.getResNo()];
12251 ++UI;
12252 Use.set(ToOp);
12253 To_IsDivergent |= ToOp->isDivergent();
12254 } while (UI != UE && UI->getUser() == User);
12255
12256 if (To_IsDivergent != From->isDivergent())
12258
12259 // Now that we have modified User, add it back to the CSE maps. If it
12260 // already exists there, recursively merge the results together.
12261 AddModifiedNodeToCSEMaps(User);
12262 }
12263
12264 // If we just RAUW'd the root, take note.
12265 if (From == getRoot().getNode())
12266 setRoot(SDValue(To[getRoot().getResNo()]));
12267}
12268
12269/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12270/// uses of other values produced by From.getNode() alone. The Deleted
12271/// vector is handled the same way as for ReplaceAllUsesWith.
12273 // Handle the really simple, really trivial case efficiently.
12274 if (From == To) return;
12275
12276 // Handle the simple, trivial, case efficiently.
12277 if (From.getNode()->getNumValues() == 1) {
12279 return;
12280 }
12281
12282 // Preserve Debug Info.
12284 copyExtraInfo(From.getNode(), To.getNode());
12285
12286 // Iterate over just the existing users of From. See the comments in
12287 // the ReplaceAllUsesWith above.
12288 SDNode::use_iterator UI = From.getNode()->use_begin(),
12289 UE = From.getNode()->use_end();
12290 RAUWUpdateListener Listener(*this, UI, UE);
12291 while (UI != UE) {
12292 SDNode *User = UI->getUser();
12293 bool UserRemovedFromCSEMaps = false;
12294
12295 // A user can appear in a use list multiple times, and when this
12296 // happens the uses are usually next to each other in the list.
12297 // To help reduce the number of CSE recomputations, process all
12298 // the uses of this user that we can find this way.
12299 do {
12300 SDUse &Use = *UI;
12301
12302 // Skip uses of different values from the same node.
12303 if (Use.getResNo() != From.getResNo()) {
12304 ++UI;
12305 continue;
12306 }
12307
12308 // If this node hasn't been modified yet, it's still in the CSE maps,
12309 // so remove its old self from the CSE maps.
12310 if (!UserRemovedFromCSEMaps) {
12311 RemoveNodeFromCSEMaps(User);
12312 UserRemovedFromCSEMaps = true;
12313 }
12314
12315 ++UI;
12316 Use.set(To);
12317 if (To->isDivergent() != From->isDivergent())
12319 } while (UI != UE && UI->getUser() == User);
12320 // We are iterating over all uses of the From node, so if a use
12321 // doesn't use the specific value, no changes are made.
12322 if (!UserRemovedFromCSEMaps)
12323 continue;
12324
12325 // Now that we have modified User, add it back to the CSE maps. If it
12326 // already exists there, recursively merge the results together.
12327 AddModifiedNodeToCSEMaps(User);
12328 }
12329
12330 // If we just RAUW'd the root, take note.
12331 if (From == getRoot())
12332 setRoot(To);
12333}
12334
12335namespace {
12336
12337/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12338/// to record information about a use.
12339struct UseMemo {
12340 SDNode *User;
12341 unsigned Index;
12342 SDUse *Use;
12343};
12344
12345/// operator< - Sort Memos by User.
12346bool operator<(const UseMemo &L, const UseMemo &R) {
12347 return (intptr_t)L.User < (intptr_t)R.User;
12348}
12349
12350/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12351/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12352/// the node already has been taken care of recursively.
12353class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12355
12356 void NodeDeleted(SDNode *N, SDNode *E) override {
12357 for (UseMemo &Memo : Uses)
12358 if (Memo.User == N)
12359 Memo.User = nullptr;
12360 }
12361
12362public:
12363 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12364 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12365};
12366
12367} // end anonymous namespace
12368
12369/// Return true if a glue output should propagate divergence information.
12371 switch (Node->getOpcode()) {
12372 case ISD::CopyFromReg:
12373 case ISD::CopyToReg:
12374 return false;
12375 default:
12376 return true;
12377 }
12378
12379 llvm_unreachable("covered opcode switch");
12380}
12381
12383 if (TLI->isSDNodeAlwaysUniform(N)) {
12384 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12385 "Conflicting divergence information!");
12386 return false;
12387 }
12388 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12389 return true;
12390 for (const auto &Op : N->ops()) {
12391 EVT VT = Op.getValueType();
12392
12393 // Skip Chain. It does not carry divergence.
12394 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12395 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12396 return true;
12397 }
12398 return false;
12399}
12400
12402 SmallVector<SDNode *, 16> Worklist(1, N);
12403 do {
12404 N = Worklist.pop_back_val();
12405 bool IsDivergent = calculateDivergence(N);
12406 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12407 N->SDNodeBits.IsDivergent = IsDivergent;
12408 llvm::append_range(Worklist, N->users());
12409 }
12410 } while (!Worklist.empty());
12411}
12412
12413void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12415 Order.reserve(AllNodes.size());
12416 for (auto &N : allnodes()) {
12417 unsigned NOps = N.getNumOperands();
12418 Degree[&N] = NOps;
12419 if (0 == NOps)
12420 Order.push_back(&N);
12421 }
12422 for (size_t I = 0; I != Order.size(); ++I) {
12423 SDNode *N = Order[I];
12424 for (auto *U : N->users()) {
12425 unsigned &UnsortedOps = Degree[U];
12426 if (0 == --UnsortedOps)
12427 Order.push_back(U);
12428 }
12429 }
12430}
12431
12432#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12433void SelectionDAG::VerifyDAGDivergence() {
12434 std::vector<SDNode *> TopoOrder;
12435 CreateTopologicalOrder(TopoOrder);
12436 for (auto *N : TopoOrder) {
12437 assert(calculateDivergence(N) == N->isDivergent() &&
12438 "Divergence bit inconsistency detected");
12439 }
12440}
12441#endif
12442
12443/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12444/// uses of other values produced by From.getNode() alone. The same value
12445/// may appear in both the From and To list. The Deleted vector is
12446/// handled the same way as for ReplaceAllUsesWith.
12448 const SDValue *To,
12449 unsigned Num){
12450 // Handle the simple, trivial case efficiently.
12451 if (Num == 1)
12452 return ReplaceAllUsesOfValueWith(*From, *To);
12453
12454 transferDbgValues(*From, *To);
12455 copyExtraInfo(From->getNode(), To->getNode());
12456
12457 // Read up all the uses and make records of them. This helps
12458 // processing new uses that are introduced during the
12459 // replacement process.
12461 for (unsigned i = 0; i != Num; ++i) {
12462 unsigned FromResNo = From[i].getResNo();
12463 SDNode *FromNode = From[i].getNode();
12464 for (SDUse &Use : FromNode->uses()) {
12465 if (Use.getResNo() == FromResNo) {
12466 UseMemo Memo = {Use.getUser(), i, &Use};
12467 Uses.push_back(Memo);
12468 }
12469 }
12470 }
12471
12472 // Sort the uses, so that all the uses from a given User are together.
12474 RAUOVWUpdateListener Listener(*this, Uses);
12475
12476 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12477 UseIndex != UseIndexEnd; ) {
12478 // We know that this user uses some value of From. If it is the right
12479 // value, update it.
12480 SDNode *User = Uses[UseIndex].User;
12481 // If the node has been deleted by recursive CSE updates when updating
12482 // another node, then just skip this entry.
12483 if (User == nullptr) {
12484 ++UseIndex;
12485 continue;
12486 }
12487
12488 // This node is about to morph, remove its old self from the CSE maps.
12489 RemoveNodeFromCSEMaps(User);
12490
12491 // The Uses array is sorted, so all the uses for a given User
12492 // are next to each other in the list.
12493 // To help reduce the number of CSE recomputations, process all
12494 // the uses of this user that we can find this way.
12495 do {
12496 unsigned i = Uses[UseIndex].Index;
12497 SDUse &Use = *Uses[UseIndex].Use;
12498 ++UseIndex;
12499
12500 Use.set(To[i]);
12501 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12502
12503 // Now that we have modified User, add it back to the CSE maps. If it
12504 // already exists there, recursively merge the results together.
12505 AddModifiedNodeToCSEMaps(User);
12506 }
12507}
12508
12509/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12510/// based on their topological order. It returns the maximum id and a vector
12511/// of the SDNodes* in assigned order by reference.
12513 unsigned DAGSize = 0;
12514
12515 // SortedPos tracks the progress of the algorithm. Nodes before it are
12516 // sorted, nodes after it are unsorted. When the algorithm completes
12517 // it is at the end of the list.
12518 allnodes_iterator SortedPos = allnodes_begin();
12519
12520 // Visit all the nodes. Move nodes with no operands to the front of
12521 // the list immediately. Annotate nodes that do have operands with their
12522 // operand count. Before we do this, the Node Id fields of the nodes
12523 // may contain arbitrary values. After, the Node Id fields for nodes
12524 // before SortedPos will contain the topological sort index, and the
12525 // Node Id fields for nodes At SortedPos and after will contain the
12526 // count of outstanding operands.
12528 checkForCycles(&N, this);
12529 unsigned Degree = N.getNumOperands();
12530 if (Degree == 0) {
12531 // A node with no uses, add it to the result array immediately.
12532 N.setNodeId(DAGSize++);
12533 allnodes_iterator Q(&N);
12534 if (Q != SortedPos)
12535 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12536 assert(SortedPos != AllNodes.end() && "Overran node list");
12537 ++SortedPos;
12538 } else {
12539 // Temporarily use the Node Id as scratch space for the degree count.
12540 N.setNodeId(Degree);
12541 }
12542 }
12543
12544 // Visit all the nodes. As we iterate, move nodes into sorted order,
12545 // such that by the time the end is reached all nodes will be sorted.
12546 for (SDNode &Node : allnodes()) {
12547 SDNode *N = &Node;
12548 checkForCycles(N, this);
12549 // N is in sorted position, so all its uses have one less operand
12550 // that needs to be sorted.
12551 for (SDNode *P : N->users()) {
12552 unsigned Degree = P->getNodeId();
12553 assert(Degree != 0 && "Invalid node degree");
12554 --Degree;
12555 if (Degree == 0) {
12556 // All of P's operands are sorted, so P may sorted now.
12557 P->setNodeId(DAGSize++);
12558 if (P->getIterator() != SortedPos)
12559 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12560 assert(SortedPos != AllNodes.end() && "Overran node list");
12561 ++SortedPos;
12562 } else {
12563 // Update P's outstanding operand count.
12564 P->setNodeId(Degree);
12565 }
12566 }
12567 if (Node.getIterator() == SortedPos) {
12568#ifndef NDEBUG
12570 SDNode *S = &*++I;
12571 dbgs() << "Overran sorted position:\n";
12572 S->dumprFull(this); dbgs() << "\n";
12573 dbgs() << "Checking if this is due to cycles\n";
12574 checkForCycles(this, true);
12575#endif
12576 llvm_unreachable(nullptr);
12577 }
12578 }
12579
12580 assert(SortedPos == AllNodes.end() &&
12581 "Topological sort incomplete!");
12582 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12583 "First node in topological sort is not the entry token!");
12584 assert(AllNodes.front().getNodeId() == 0 &&
12585 "First node in topological sort has non-zero id!");
12586 assert(AllNodes.front().getNumOperands() == 0 &&
12587 "First node in topological sort has operands!");
12588 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12589 "Last node in topologic sort has unexpected id!");
12590 assert(AllNodes.back().use_empty() &&
12591 "Last node in topologic sort has users!");
12592 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12593 return DAGSize;
12594}
12595
12596/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12597/// value is produced by SD.
12598void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12599 for (SDNode *SD : DB->getSDNodes()) {
12600 if (!SD)
12601 continue;
12602 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12603 SD->setHasDebugValue(true);
12604 }
12605 DbgInfo->add(DB, isParameter);
12606}
12607
12608void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12609
12611 SDValue NewMemOpChain) {
12612 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12613 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12614 // The new memory operation must have the same position as the old load in
12615 // terms of memory dependency. Create a TokenFactor for the old load and new
12616 // memory operation and update uses of the old load's output chain to use that
12617 // TokenFactor.
12618 if (OldChain == NewMemOpChain || OldChain.use_empty())
12619 return NewMemOpChain;
12620
12621 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12622 OldChain, NewMemOpChain);
12623 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12624 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12625 return TokenFactor;
12626}
12627
12629 SDValue NewMemOp) {
12630 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12631 SDValue OldChain = SDValue(OldLoad, 1);
12632 SDValue NewMemOpChain = NewMemOp.getValue(1);
12633 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12634}
12635
12637 Function **OutFunction) {
12638 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12639
12640 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12641 auto *Module = MF->getFunction().getParent();
12642 auto *Function = Module->getFunction(Symbol);
12643
12644 if (OutFunction != nullptr)
12645 *OutFunction = Function;
12646
12647 if (Function != nullptr) {
12648 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12649 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12650 }
12651
12652 std::string ErrorStr;
12653 raw_string_ostream ErrorFormatter(ErrorStr);
12654 ErrorFormatter << "Undefined external symbol ";
12655 ErrorFormatter << '"' << Symbol << '"';
12656 report_fatal_error(Twine(ErrorStr));
12657}
12658
12659//===----------------------------------------------------------------------===//
12660// SDNode Class
12661//===----------------------------------------------------------------------===//
12662
12664 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12665 return Const != nullptr && Const->isZero();
12666}
12667
12669 return V.isUndef() || isNullConstant(V);
12670}
12671
12673 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
12674 return Const != nullptr && Const->isZero() && !Const->isNegative();
12675}
12676
12678 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12679 return Const != nullptr && Const->isAllOnes();
12680}
12681
12683 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12684 return Const != nullptr && Const->isOne();
12685}
12686
12688 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12689 return Const != nullptr && Const->isMinSignedValue();
12690}
12691
12692bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12693 unsigned OperandNo) {
12694 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12695 // TODO: Target-specific opcodes could be added.
12696 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12697 /*AllowTruncation*/ true)) {
12698 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12699 switch (Opcode) {
12700 case ISD::ADD:
12701 case ISD::OR:
12702 case ISD::XOR:
12703 case ISD::UMAX:
12704 return Const.isZero();
12705 case ISD::MUL:
12706 return Const.isOne();
12707 case ISD::AND:
12708 case ISD::UMIN:
12709 return Const.isAllOnes();
12710 case ISD::SMAX:
12711 return Const.isMinSignedValue();
12712 case ISD::SMIN:
12713 return Const.isMaxSignedValue();
12714 case ISD::SUB:
12715 case ISD::SHL:
12716 case ISD::SRA:
12717 case ISD::SRL:
12718 return OperandNo == 1 && Const.isZero();
12719 case ISD::UDIV:
12720 case ISD::SDIV:
12721 return OperandNo == 1 && Const.isOne();
12722 }
12723 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12724 switch (Opcode) {
12725 case ISD::FADD:
12726 return ConstFP->isZero() &&
12727 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12728 case ISD::FSUB:
12729 return OperandNo == 1 && ConstFP->isZero() &&
12730 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12731 case ISD::FMUL:
12732 return ConstFP->isExactlyValue(1.0);
12733 case ISD::FDIV:
12734 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12735 case ISD::FMINNUM:
12736 case ISD::FMAXNUM: {
12737 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12738 EVT VT = V.getValueType();
12739 const fltSemantics &Semantics = VT.getFltSemantics();
12740 APFloat NeutralAF = !Flags.hasNoNaNs()
12741 ? APFloat::getQNaN(Semantics)
12742 : !Flags.hasNoInfs()
12743 ? APFloat::getInf(Semantics)
12744 : APFloat::getLargest(Semantics);
12745 if (Opcode == ISD::FMAXNUM)
12746 NeutralAF.changeSign();
12747
12748 return ConstFP->isExactlyValue(NeutralAF);
12749 }
12750 }
12751 }
12752 return false;
12753}
12754
12756 while (V.getOpcode() == ISD::BITCAST)
12757 V = V.getOperand(0);
12758 return V;
12759}
12760
12762 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12763 V = V.getOperand(0);
12764 return V;
12765}
12766
12768 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12769 V = V.getOperand(0);
12770 return V;
12771}
12772
12774 while (V.getOpcode() == ISD::TRUNCATE)
12775 V = V.getOperand(0);
12776 return V;
12777}
12778
12779bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12780 if (V.getOpcode() != ISD::XOR)
12781 return false;
12782 V = peekThroughBitcasts(V.getOperand(1));
12783 unsigned NumBits = V.getScalarValueSizeInBits();
12784 ConstantSDNode *C =
12785 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12786 return C && (C->getAPIntValue().countr_one() >= NumBits);
12787}
12788
12790 bool AllowTruncation) {
12791 EVT VT = N.getValueType();
12792 APInt DemandedElts = VT.isFixedLengthVector()
12794 : APInt(1, 1);
12795 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12796}
12797
12799 bool AllowUndefs,
12800 bool AllowTruncation) {
12801 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
12802 return CN;
12803
12804 // SplatVectors can truncate their operands. Ignore that case here unless
12805 // AllowTruncation is set.
12806 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12807 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12808 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12809 EVT CVT = CN->getValueType(0);
12810 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12811 if (AllowTruncation || CVT == VecEltVT)
12812 return CN;
12813 }
12814 }
12815
12816 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12817 BitVector UndefElements;
12818 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12819
12820 // BuildVectors can truncate their operands. Ignore that case here unless
12821 // AllowTruncation is set.
12822 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12823 if (CN && (UndefElements.none() || AllowUndefs)) {
12824 EVT CVT = CN->getValueType(0);
12825 EVT NSVT = N.getValueType().getScalarType();
12826 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12827 if (AllowTruncation || (CVT == NSVT))
12828 return CN;
12829 }
12830 }
12831
12832 return nullptr;
12833}
12834
12836 EVT VT = N.getValueType();
12837 APInt DemandedElts = VT.isFixedLengthVector()
12839 : APInt(1, 1);
12840 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12841}
12842
12844 const APInt &DemandedElts,
12845 bool AllowUndefs) {
12846 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
12847 return CN;
12848
12849 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12850 BitVector UndefElements;
12851 ConstantFPSDNode *CN =
12852 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12853 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12854 if (CN && (UndefElements.none() || AllowUndefs))
12855 return CN;
12856 }
12857
12858 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12859 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12860 return CN;
12861
12862 return nullptr;
12863}
12864
12865bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12866 // TODO: may want to use peekThroughBitcast() here.
12867 ConstantSDNode *C =
12868 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12869 return C && C->isZero();
12870}
12871
12872bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12873 ConstantSDNode *C =
12874 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12875 return C && C->isOne();
12876}
12877
12878bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12880 unsigned BitWidth = N.getScalarValueSizeInBits();
12881 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12882 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12883}
12884
12885bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
12886 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12887 return C && APInt::isSameValue(C->getAPIntValue(),
12888 APInt(C->getAPIntValue().getBitWidth(), 1));
12889}
12890
12891bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
12893 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
12894 return C && C->isZero();
12895}
12896
12898 DropOperands();
12899}
12900
12901MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12902 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12903 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12904 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12905 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12906 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12907 MemSDNodeBits.IsInvariant = MMO->isInvariant();
12908
12909 // We check here that the size of the memory operand fits within the size of
12910 // the MMO. This is because the MMO might indicate only a possible address
12911 // range instead of specifying the affected memory addresses precisely.
12912 assert(
12913 (!MMO->getType().isValid() ||
12915 "Size mismatch!");
12916}
12917
12918/// Profile - Gather unique data for the node.
12919///
12921 AddNodeIDNode(ID, this);
12922}
12923
12924namespace {
12925
12926 struct EVTArray {
12927 std::vector<EVT> VTs;
12928
12929 EVTArray() {
12930 VTs.reserve(MVT::VALUETYPE_SIZE);
12931 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
12932 VTs.push_back(MVT((MVT::SimpleValueType)i));
12933 }
12934 };
12935
12936} // end anonymous namespace
12937
12938/// getValueTypeList - Return a pointer to the specified value type.
12939///
12940const EVT *SDNode::getValueTypeList(MVT VT) {
12941 static EVTArray SimpleVTArray;
12942
12943 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
12944 return &SimpleVTArray.VTs[VT.SimpleTy];
12945}
12946
12947/// hasAnyUseOfValue - Return true if there are any use of the indicated
12948/// value. This method ignores uses of other values defined by this operation.
12949bool SDNode::hasAnyUseOfValue(unsigned Value) const {
12950 assert(Value < getNumValues() && "Bad value!");
12951
12952 for (SDUse &U : uses())
12953 if (U.getResNo() == Value)
12954 return true;
12955
12956 return false;
12957}
12958
12959/// isOnlyUserOf - Return true if this node is the only use of N.
12960bool SDNode::isOnlyUserOf(const SDNode *N) const {
12961 bool Seen = false;
12962 for (const SDNode *User : N->users()) {
12963 if (User == this)
12964 Seen = true;
12965 else
12966 return false;
12967 }
12968
12969 return Seen;
12970}
12971
12972/// Return true if the only users of N are contained in Nodes.
12974 bool Seen = false;
12975 for (const SDNode *User : N->users()) {
12976 if (llvm::is_contained(Nodes, User))
12977 Seen = true;
12978 else
12979 return false;
12980 }
12981
12982 return Seen;
12983}
12984
12985/// Return true if the referenced return value is an operand of N.
12986bool SDValue::isOperandOf(const SDNode *N) const {
12987 return is_contained(N->op_values(), *this);
12988}
12989
12990bool SDNode::isOperandOf(const SDNode *N) const {
12991 return any_of(N->op_values(),
12992 [this](SDValue Op) { return this == Op.getNode(); });
12993}
12994
12995/// reachesChainWithoutSideEffects - Return true if this operand (which must
12996/// be a chain) reaches the specified operand without crossing any
12997/// side-effecting instructions on any chain path. In practice, this looks
12998/// through token factors and non-volatile loads. In order to remain efficient,
12999/// this only looks a couple of nodes in, it does not do an exhaustive search.
13000///
13001/// Note that we only need to examine chains when we're searching for
13002/// side-effects; SelectionDAG requires that all side-effects are represented
13003/// by chains, even if another operand would force a specific ordering. This
13004/// constraint is necessary to allow transformations like splitting loads.
13006 unsigned Depth) const {
13007 if (*this == Dest) return true;
13008
13009 // Don't search too deeply, we just want to be able to see through
13010 // TokenFactor's etc.
13011 if (Depth == 0) return false;
13012
13013 // If this is a token factor, all inputs to the TF happen in parallel.
13014 if (getOpcode() == ISD::TokenFactor) {
13015 // First, try a shallow search.
13016 if (is_contained((*this)->ops(), Dest)) {
13017 // We found the chain we want as an operand of this TokenFactor.
13018 // Essentially, we reach the chain without side-effects if we could
13019 // serialize the TokenFactor into a simple chain of operations with
13020 // Dest as the last operation. This is automatically true if the
13021 // chain has one use: there are no other ordering constraints.
13022 // If the chain has more than one use, we give up: some other
13023 // use of Dest might force a side-effect between Dest and the current
13024 // node.
13025 if (Dest.hasOneUse())
13026 return true;
13027 }
13028 // Next, try a deep search: check whether every operand of the TokenFactor
13029 // reaches Dest.
13030 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13031 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13032 });
13033 }
13034
13035 // Loads don't have side effects, look through them.
13036 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13037 if (Ld->isUnordered())
13038 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13039 }
13040 return false;
13041}
13042
13043bool SDNode::hasPredecessor(const SDNode *N) const {
13046 Worklist.push_back(this);
13047 return hasPredecessorHelper(N, Visited, Worklist);
13048}
13049
13051 this->Flags &= Flags;
13052}
13053
13054SDValue
13056 ArrayRef<ISD::NodeType> CandidateBinOps,
13057 bool AllowPartials) {
13058 // The pattern must end in an extract from index 0.
13059 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13060 !isNullConstant(Extract->getOperand(1)))
13061 return SDValue();
13062
13063 // Match against one of the candidate binary ops.
13064 SDValue Op = Extract->getOperand(0);
13065 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13066 return Op.getOpcode() == unsigned(BinOp);
13067 }))
13068 return SDValue();
13069
13070 // Floating-point reductions may require relaxed constraints on the final step
13071 // of the reduction because they may reorder intermediate operations.
13072 unsigned CandidateBinOp = Op.getOpcode();
13073 if (Op.getValueType().isFloatingPoint()) {
13074 SDNodeFlags Flags = Op->getFlags();
13075 switch (CandidateBinOp) {
13076 case ISD::FADD:
13077 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13078 return SDValue();
13079 break;
13080 default:
13081 llvm_unreachable("Unhandled FP opcode for binop reduction");
13082 }
13083 }
13084
13085 // Matching failed - attempt to see if we did enough stages that a partial
13086 // reduction from a subvector is possible.
13087 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13088 if (!AllowPartials || !Op)
13089 return SDValue();
13090 EVT OpVT = Op.getValueType();
13091 EVT OpSVT = OpVT.getScalarType();
13092 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13093 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13094 return SDValue();
13095 BinOp = (ISD::NodeType)CandidateBinOp;
13096 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13097 };
13098
13099 // At each stage, we're looking for something that looks like:
13100 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13101 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13102 // i32 undef, i32 undef, i32 undef, i32 undef>
13103 // %a = binop <8 x i32> %op, %s
13104 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13105 // we expect something like:
13106 // <4,5,6,7,u,u,u,u>
13107 // <2,3,u,u,u,u,u,u>
13108 // <1,u,u,u,u,u,u,u>
13109 // While a partial reduction match would be:
13110 // <2,3,u,u,u,u,u,u>
13111 // <1,u,u,u,u,u,u,u>
13112 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13113 SDValue PrevOp;
13114 for (unsigned i = 0; i < Stages; ++i) {
13115 unsigned MaskEnd = (1 << i);
13116
13117 if (Op.getOpcode() != CandidateBinOp)
13118 return PartialReduction(PrevOp, MaskEnd);
13119
13120 SDValue Op0 = Op.getOperand(0);
13121 SDValue Op1 = Op.getOperand(1);
13122
13123 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
13124 if (Shuffle) {
13125 Op = Op1;
13126 } else {
13127 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13128 Op = Op0;
13129 }
13130
13131 // The first operand of the shuffle should be the same as the other operand
13132 // of the binop.
13133 if (!Shuffle || Shuffle->getOperand(0) != Op)
13134 return PartialReduction(PrevOp, MaskEnd);
13135
13136 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13137 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13138 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13139 return PartialReduction(PrevOp, MaskEnd);
13140
13141 PrevOp = Op;
13142 }
13143
13144 // Handle subvector reductions, which tend to appear after the shuffle
13145 // reduction stages.
13146 while (Op.getOpcode() == CandidateBinOp) {
13147 unsigned NumElts = Op.getValueType().getVectorNumElements();
13148 SDValue Op0 = Op.getOperand(0);
13149 SDValue Op1 = Op.getOperand(1);
13150 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13152 Op0.getOperand(0) != Op1.getOperand(0))
13153 break;
13154 SDValue Src = Op0.getOperand(0);
13155 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13156 if (NumSrcElts != (2 * NumElts))
13157 break;
13158 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13159 Op1.getConstantOperandAPInt(1) == NumElts) &&
13160 !(Op1.getConstantOperandAPInt(1) == 0 &&
13161 Op0.getConstantOperandAPInt(1) == NumElts))
13162 break;
13163 Op = Src;
13164 }
13165
13166 BinOp = (ISD::NodeType)CandidateBinOp;
13167 return Op;
13168}
13169
13171 EVT VT = N->getValueType(0);
13172 EVT EltVT = VT.getVectorElementType();
13173 unsigned NE = VT.getVectorNumElements();
13174
13175 SDLoc dl(N);
13176
13177 // If ResNE is 0, fully unroll the vector op.
13178 if (ResNE == 0)
13179 ResNE = NE;
13180 else if (NE > ResNE)
13181 NE = ResNE;
13182
13183 if (N->getNumValues() == 2) {
13184 SmallVector<SDValue, 8> Scalars0, Scalars1;
13185 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13186 EVT VT1 = N->getValueType(1);
13187 EVT EltVT1 = VT1.getVectorElementType();
13188
13189 unsigned i;
13190 for (i = 0; i != NE; ++i) {
13191 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13192 SDValue Operand = N->getOperand(j);
13193 EVT OperandVT = Operand.getValueType();
13194
13195 // A vector operand; extract a single element.
13196 EVT OperandEltVT = OperandVT.getVectorElementType();
13197 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13198 }
13199
13200 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13201 Scalars0.push_back(EltOp);
13202 Scalars1.push_back(EltOp.getValue(1));
13203 }
13204
13205 for (; i < ResNE; ++i) {
13206 Scalars0.push_back(getUNDEF(EltVT));
13207 Scalars1.push_back(getUNDEF(EltVT1));
13208 }
13209
13210 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13211 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13212 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13213 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13214 return getMergeValues({Vec0, Vec1}, dl);
13215 }
13216
13217 assert(N->getNumValues() == 1 &&
13218 "Can't unroll a vector with multiple results!");
13219
13221 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13222
13223 unsigned i;
13224 for (i= 0; i != NE; ++i) {
13225 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13226 SDValue Operand = N->getOperand(j);
13227 EVT OperandVT = Operand.getValueType();
13228 if (OperandVT.isVector()) {
13229 // A vector operand; extract a single element.
13230 EVT OperandEltVT = OperandVT.getVectorElementType();
13231 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13232 } else {
13233 // A scalar operand; just use it as is.
13234 Operands[j] = Operand;
13235 }
13236 }
13237
13238 switch (N->getOpcode()) {
13239 default: {
13240 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13241 N->getFlags()));
13242 break;
13243 }
13244 case ISD::VSELECT:
13245 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13246 break;
13247 case ISD::SHL:
13248 case ISD::SRA:
13249 case ISD::SRL:
13250 case ISD::ROTL:
13251 case ISD::ROTR:
13252 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13254 Operands[1])));
13255 break;
13257 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13258 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13259 Operands[0],
13260 getValueType(ExtVT)));
13261 break;
13262 }
13263 case ISD::ADDRSPACECAST: {
13264 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13265 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13266 ASC->getSrcAddressSpace(),
13267 ASC->getDestAddressSpace()));
13268 break;
13269 }
13270 }
13271 }
13272
13273 for (; i < ResNE; ++i)
13274 Scalars.push_back(getUNDEF(EltVT));
13275
13276 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13277 return getBuildVector(VecVT, dl, Scalars);
13278}
13279
13280std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13281 SDNode *N, unsigned ResNE) {
13282 unsigned Opcode = N->getOpcode();
13283 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13284 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13285 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13286 "Expected an overflow opcode");
13287
13288 EVT ResVT = N->getValueType(0);
13289 EVT OvVT = N->getValueType(1);
13290 EVT ResEltVT = ResVT.getVectorElementType();
13291 EVT OvEltVT = OvVT.getVectorElementType();
13292 SDLoc dl(N);
13293
13294 // If ResNE is 0, fully unroll the vector op.
13295 unsigned NE = ResVT.getVectorNumElements();
13296 if (ResNE == 0)
13297 ResNE = NE;
13298 else if (NE > ResNE)
13299 NE = ResNE;
13300
13301 SmallVector<SDValue, 8> LHSScalars;
13302 SmallVector<SDValue, 8> RHSScalars;
13303 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13304 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13305
13306 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13307 SDVTList VTs = getVTList(ResEltVT, SVT);
13308 SmallVector<SDValue, 8> ResScalars;
13309 SmallVector<SDValue, 8> OvScalars;
13310 for (unsigned i = 0; i < NE; ++i) {
13311 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13312 SDValue Ov =
13313 getSelect(dl, OvEltVT, Res.getValue(1),
13314 getBoolConstant(true, dl, OvEltVT, ResVT),
13315 getConstant(0, dl, OvEltVT));
13316
13317 ResScalars.push_back(Res);
13318 OvScalars.push_back(Ov);
13319 }
13320
13321 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13322 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13323
13324 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13325 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13326 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13327 getBuildVector(NewOvVT, dl, OvScalars));
13328}
13329
13332 unsigned Bytes,
13333 int Dist) const {
13334 if (LD->isVolatile() || Base->isVolatile())
13335 return false;
13336 // TODO: probably too restrictive for atomics, revisit
13337 if (!LD->isSimple())
13338 return false;
13339 if (LD->isIndexed() || Base->isIndexed())
13340 return false;
13341 if (LD->getChain() != Base->getChain())
13342 return false;
13343 EVT VT = LD->getMemoryVT();
13344 if (VT.getSizeInBits() / 8 != Bytes)
13345 return false;
13346
13347 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13348 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13349
13350 int64_t Offset = 0;
13351 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13352 return (Dist * (int64_t)Bytes == Offset);
13353 return false;
13354}
13355
13356/// InferPtrAlignment - Infer alignment of a load / store address. Return
13357/// std::nullopt if it cannot be inferred.
13359 // If this is a GlobalAddress + cst, return the alignment.
13360 const GlobalValue *GV = nullptr;
13361 int64_t GVOffset = 0;
13362 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13363 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13364 KnownBits Known(PtrWidth);
13366 unsigned AlignBits = Known.countMinTrailingZeros();
13367 if (AlignBits)
13368 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13369 }
13370
13371 // If this is a direct reference to a stack slot, use information about the
13372 // stack slot's alignment.
13373 int FrameIdx = INT_MIN;
13374 int64_t FrameOffset = 0;
13375 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
13376 FrameIdx = FI->getIndex();
13377 } else if (isBaseWithConstantOffset(Ptr) &&
13378 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
13379 // Handle FI+Cst
13380 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13381 FrameOffset = Ptr.getConstantOperandVal(1);
13382 }
13383
13384 if (FrameIdx != INT_MIN) {
13386 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13387 }
13388
13389 return std::nullopt;
13390}
13391
13392/// Split the scalar node with EXTRACT_ELEMENT using the provided
13393/// VTs and return the low/high part.
13394std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13395 const SDLoc &DL,
13396 const EVT &LoVT,
13397 const EVT &HiVT) {
13398 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13399 "Split node must be a scalar type");
13400 SDValue Lo =
13402 SDValue Hi =
13404 return std::make_pair(Lo, Hi);
13405}
13406
13407/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13408/// which is split (or expanded) into two not necessarily identical pieces.
13409std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13410 // Currently all types are split in half.
13411 EVT LoVT, HiVT;
13412 if (!VT.isVector())
13413 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13414 else
13415 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13416
13417 return std::make_pair(LoVT, HiVT);
13418}
13419
13420/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13421/// type, dependent on an enveloping VT that has been split into two identical
13422/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13423std::pair<EVT, EVT>
13425 bool *HiIsEmpty) const {
13426 EVT EltTp = VT.getVectorElementType();
13427 // Examples:
13428 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13429 // custom VL=9 with enveloping VL=8/8 yields 8/1
13430 // custom VL=10 with enveloping VL=8/8 yields 8/2
13431 // etc.
13432 ElementCount VTNumElts = VT.getVectorElementCount();
13433 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13434 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13435 "Mixing fixed width and scalable vectors when enveloping a type");
13436 EVT LoVT, HiVT;
13437 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13438 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13439 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13440 *HiIsEmpty = false;
13441 } else {
13442 // Flag that hi type has zero storage size, but return split envelop type
13443 // (this would be easier if vector types with zero elements were allowed).
13444 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13445 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13446 *HiIsEmpty = true;
13447 }
13448 return std::make_pair(LoVT, HiVT);
13449}
13450
13451/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13452/// low/high part.
13453std::pair<SDValue, SDValue>
13454SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13455 const EVT &HiVT) {
13456 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13457 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13458 "Splitting vector with an invalid mixture of fixed and scalable "
13459 "vector types");
13461 N.getValueType().getVectorMinNumElements() &&
13462 "More vector elements requested than available!");
13463 SDValue Lo, Hi;
13464 Lo = getExtractSubvector(DL, LoVT, N, 0);
13465 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13466 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13467 // IDX with the runtime scaling factor of the result vector type. For
13468 // fixed-width result vectors, that runtime scaling factor is 1.
13471 return std::make_pair(Lo, Hi);
13472}
13473
13474std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13475 const SDLoc &DL) {
13476 // Split the vector length parameter.
13477 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13478 EVT VT = N.getValueType();
13480 "Expecting the mask to be an evenly-sized vector");
13481 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13482 SDValue HalfNumElts =
13483 VecVT.isFixedLengthVector()
13484 ? getConstant(HalfMinNumElts, DL, VT)
13485 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13486 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13487 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13488 return std::make_pair(Lo, Hi);
13489}
13490
13491/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13493 EVT VT = N.getValueType();
13496 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13497}
13498
13501 unsigned Start, unsigned Count,
13502 EVT EltVT) {
13503 EVT VT = Op.getValueType();
13504 if (Count == 0)
13505 Count = VT.getVectorNumElements();
13506 if (EltVT == EVT())
13507 EltVT = VT.getVectorElementType();
13508 SDLoc SL(Op);
13509 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13510 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13511 }
13512}
13513
13514// getAddressSpace - Return the address space this GlobalAddress belongs to.
13516 return getGlobal()->getType()->getAddressSpace();
13517}
13518
13521 return Val.MachineCPVal->getType();
13522 return Val.ConstVal->getType();
13523}
13524
13525bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13526 unsigned &SplatBitSize,
13527 bool &HasAnyUndefs,
13528 unsigned MinSplatBits,
13529 bool IsBigEndian) const {
13530 EVT VT = getValueType(0);
13531 assert(VT.isVector() && "Expected a vector type");
13532 unsigned VecWidth = VT.getSizeInBits();
13533 if (MinSplatBits > VecWidth)
13534 return false;
13535
13536 // FIXME: The widths are based on this node's type, but build vectors can
13537 // truncate their operands.
13538 SplatValue = APInt(VecWidth, 0);
13539 SplatUndef = APInt(VecWidth, 0);
13540
13541 // Get the bits. Bits with undefined values (when the corresponding element
13542 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13543 // in SplatValue. If any of the values are not constant, give up and return
13544 // false.
13545 unsigned int NumOps = getNumOperands();
13546 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13547 unsigned EltWidth = VT.getScalarSizeInBits();
13548
13549 for (unsigned j = 0; j < NumOps; ++j) {
13550 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13551 SDValue OpVal = getOperand(i);
13552 unsigned BitPos = j * EltWidth;
13553
13554 if (OpVal.isUndef())
13555 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13556 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13557 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13558 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13559 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13560 else
13561 return false;
13562 }
13563
13564 // The build_vector is all constants or undefs. Find the smallest element
13565 // size that splats the vector.
13566 HasAnyUndefs = (SplatUndef != 0);
13567
13568 // FIXME: This does not work for vectors with elements less than 8 bits.
13569 while (VecWidth > 8) {
13570 // If we can't split in half, stop here.
13571 if (VecWidth & 1)
13572 break;
13573
13574 unsigned HalfSize = VecWidth / 2;
13575 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13576 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13577 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13578 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13579
13580 // If the two halves do not match (ignoring undef bits), stop here.
13581 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13582 MinSplatBits > HalfSize)
13583 break;
13584
13585 SplatValue = HighValue | LowValue;
13586 SplatUndef = HighUndef & LowUndef;
13587
13588 VecWidth = HalfSize;
13589 }
13590
13591 // FIXME: The loop above only tries to split in halves. But if the input
13592 // vector for example is <3 x i16> it wouldn't be able to detect a
13593 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13594 // optimizations. I guess that back in the days when this helper was created
13595 // vectors normally was power-of-2 sized.
13596
13597 SplatBitSize = VecWidth;
13598 return true;
13599}
13600
13602 BitVector *UndefElements) const {
13603 unsigned NumOps = getNumOperands();
13604 if (UndefElements) {
13605 UndefElements->clear();
13606 UndefElements->resize(NumOps);
13607 }
13608 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13609 if (!DemandedElts)
13610 return SDValue();
13611 SDValue Splatted;
13612 for (unsigned i = 0; i != NumOps; ++i) {
13613 if (!DemandedElts[i])
13614 continue;
13615 SDValue Op = getOperand(i);
13616 if (Op.isUndef()) {
13617 if (UndefElements)
13618 (*UndefElements)[i] = true;
13619 } else if (!Splatted) {
13620 Splatted = Op;
13621 } else if (Splatted != Op) {
13622 return SDValue();
13623 }
13624 }
13625
13626 if (!Splatted) {
13627 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13628 assert(getOperand(FirstDemandedIdx).isUndef() &&
13629 "Can only have a splat without a constant for all undefs.");
13630 return getOperand(FirstDemandedIdx);
13631 }
13632
13633 return Splatted;
13634}
13635
13637 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13638 return getSplatValue(DemandedElts, UndefElements);
13639}
13640
13642 SmallVectorImpl<SDValue> &Sequence,
13643 BitVector *UndefElements) const {
13644 unsigned NumOps = getNumOperands();
13645 Sequence.clear();
13646 if (UndefElements) {
13647 UndefElements->clear();
13648 UndefElements->resize(NumOps);
13649 }
13650 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13651 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13652 return false;
13653
13654 // Set the undefs even if we don't find a sequence (like getSplatValue).
13655 if (UndefElements)
13656 for (unsigned I = 0; I != NumOps; ++I)
13657 if (DemandedElts[I] && getOperand(I).isUndef())
13658 (*UndefElements)[I] = true;
13659
13660 // Iteratively widen the sequence length looking for repetitions.
13661 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13662 Sequence.append(SeqLen, SDValue());
13663 for (unsigned I = 0; I != NumOps; ++I) {
13664 if (!DemandedElts[I])
13665 continue;
13666 SDValue &SeqOp = Sequence[I % SeqLen];
13668 if (Op.isUndef()) {
13669 if (!SeqOp)
13670 SeqOp = Op;
13671 continue;
13672 }
13673 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13674 Sequence.clear();
13675 break;
13676 }
13677 SeqOp = Op;
13678 }
13679 if (!Sequence.empty())
13680 return true;
13681 }
13682
13683 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13684 return false;
13685}
13686
13688 BitVector *UndefElements) const {
13689 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13690 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13691}
13692
13695 BitVector *UndefElements) const {
13696 return dyn_cast_or_null<ConstantSDNode>(
13697 getSplatValue(DemandedElts, UndefElements));
13698}
13699
13702 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13703}
13704
13707 BitVector *UndefElements) const {
13708 return dyn_cast_or_null<ConstantFPSDNode>(
13709 getSplatValue(DemandedElts, UndefElements));
13710}
13711
13714 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
13715}
13716
13717int32_t
13719 uint32_t BitWidth) const {
13720 if (ConstantFPSDNode *CN =
13721 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
13722 bool IsExact;
13723 APSInt IntVal(BitWidth);
13724 const APFloat &APF = CN->getValueAPF();
13725 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13726 APFloat::opOK ||
13727 !IsExact)
13728 return -1;
13729
13730 return IntVal.exactLogBase2();
13731 }
13732 return -1;
13733}
13734
13736 bool IsLittleEndian, unsigned DstEltSizeInBits,
13737 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13738 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13739 if (!isConstant())
13740 return false;
13741
13742 unsigned NumSrcOps = getNumOperands();
13743 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13744 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13745 "Invalid bitcast scale");
13746
13747 // Extract raw src bits.
13748 SmallVector<APInt> SrcBitElements(NumSrcOps,
13749 APInt::getZero(SrcEltSizeInBits));
13750 BitVector SrcUndeElements(NumSrcOps, false);
13751
13752 for (unsigned I = 0; I != NumSrcOps; ++I) {
13754 if (Op.isUndef()) {
13755 SrcUndeElements.set(I);
13756 continue;
13757 }
13758 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13759 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13760 assert((CInt || CFP) && "Unknown constant");
13761 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13762 : CFP->getValueAPF().bitcastToAPInt();
13763 }
13764
13765 // Recast to dst width.
13766 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13767 SrcBitElements, UndefElements, SrcUndeElements);
13768 return true;
13769}
13770
13771void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13772 unsigned DstEltSizeInBits,
13773 SmallVectorImpl<APInt> &DstBitElements,
13774 ArrayRef<APInt> SrcBitElements,
13775 BitVector &DstUndefElements,
13776 const BitVector &SrcUndefElements) {
13777 unsigned NumSrcOps = SrcBitElements.size();
13778 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13779 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13780 "Invalid bitcast scale");
13781 assert(NumSrcOps == SrcUndefElements.size() &&
13782 "Vector size mismatch");
13783
13784 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13785 DstUndefElements.clear();
13786 DstUndefElements.resize(NumDstOps, false);
13787 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13788
13789 // Concatenate src elements constant bits together into dst element.
13790 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13791 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13792 for (unsigned I = 0; I != NumDstOps; ++I) {
13793 DstUndefElements.set(I);
13794 APInt &DstBits = DstBitElements[I];
13795 for (unsigned J = 0; J != Scale; ++J) {
13796 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13797 if (SrcUndefElements[Idx])
13798 continue;
13799 DstUndefElements.reset(I);
13800 const APInt &SrcBits = SrcBitElements[Idx];
13801 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13802 "Illegal constant bitwidths");
13803 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13804 }
13805 }
13806 return;
13807 }
13808
13809 // Split src element constant bits into dst elements.
13810 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13811 for (unsigned I = 0; I != NumSrcOps; ++I) {
13812 if (SrcUndefElements[I]) {
13813 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13814 continue;
13815 }
13816 const APInt &SrcBits = SrcBitElements[I];
13817 for (unsigned J = 0; J != Scale; ++J) {
13818 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13819 APInt &DstBits = DstBitElements[Idx];
13820 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13821 }
13822 }
13823}
13824
13826 for (const SDValue &Op : op_values()) {
13827 unsigned Opc = Op.getOpcode();
13828 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13829 return false;
13830 }
13831 return true;
13832}
13833
13834std::optional<std::pair<APInt, APInt>>
13836 unsigned NumOps = getNumOperands();
13837 if (NumOps < 2)
13838 return std::nullopt;
13839
13840 if (!isa<ConstantSDNode>(getOperand(0)) ||
13841 !isa<ConstantSDNode>(getOperand(1)))
13842 return std::nullopt;
13843
13844 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13845 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13846 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13847
13848 if (Stride.isZero())
13849 return std::nullopt;
13850
13851 for (unsigned i = 2; i < NumOps; ++i) {
13852 if (!isa<ConstantSDNode>(getOperand(i)))
13853 return std::nullopt;
13854
13855 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13856 if (Val != (Start + (Stride * i)))
13857 return std::nullopt;
13858 }
13859
13860 return std::make_pair(Start, Stride);
13861}
13862
13864 // Find the first non-undef value in the shuffle mask.
13865 unsigned i, e;
13866 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
13867 /* search */;
13868
13869 // If all elements are undefined, this shuffle can be considered a splat
13870 // (although it should eventually get simplified away completely).
13871 if (i == e)
13872 return true;
13873
13874 // Make sure all remaining elements are either undef or the same as the first
13875 // non-undef value.
13876 for (int Idx = Mask[i]; i != e; ++i)
13877 if (Mask[i] >= 0 && Mask[i] != Idx)
13878 return false;
13879 return true;
13880}
13881
13882// Returns true if it is a constant integer BuildVector or constant integer,
13883// possibly hidden by a bitcast.
13885 SDValue N, bool AllowOpaques) const {
13887
13888 if (auto *C = dyn_cast<ConstantSDNode>(N))
13889 return AllowOpaques || !C->isOpaque();
13890
13892 return true;
13893
13894 // Treat a GlobalAddress supporting constant offset folding as a
13895 // constant integer.
13896 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
13897 if (GA->getOpcode() == ISD::GlobalAddress &&
13898 TLI->isOffsetFoldingLegal(GA))
13899 return true;
13900
13901 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13902 isa<ConstantSDNode>(N.getOperand(0)))
13903 return true;
13904 return false;
13905}
13906
13907// Returns true if it is a constant float BuildVector or constant float.
13909 if (isa<ConstantFPSDNode>(N))
13910 return true;
13911
13913 return true;
13914
13915 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13916 isa<ConstantFPSDNode>(N.getOperand(0)))
13917 return true;
13918
13919 return false;
13920}
13921
13922std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
13923 ConstantSDNode *Const =
13924 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
13925 if (!Const)
13926 return std::nullopt;
13927
13928 EVT VT = N->getValueType(0);
13929 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
13930 switch (TLI->getBooleanContents(N.getValueType())) {
13932 if (CVal.isOne())
13933 return true;
13934 if (CVal.isZero())
13935 return false;
13936 return std::nullopt;
13938 if (CVal.isAllOnes())
13939 return true;
13940 if (CVal.isZero())
13941 return false;
13942 return std::nullopt;
13944 return CVal[0];
13945 }
13946 llvm_unreachable("Unknown BooleanContent enum");
13947}
13948
13949void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
13950 assert(!Node->OperandList && "Node already has operands");
13952 "too many operands to fit into SDNode");
13953 SDUse *Ops = OperandRecycler.allocate(
13954 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
13955
13956 bool IsDivergent = false;
13957 for (unsigned I = 0; I != Vals.size(); ++I) {
13958 Ops[I].setUser(Node);
13959 Ops[I].setInitial(Vals[I]);
13960 EVT VT = Ops[I].getValueType();
13961
13962 // Skip Chain. It does not carry divergence.
13963 if (VT != MVT::Other &&
13964 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
13965 Ops[I].getNode()->isDivergent()) {
13966 IsDivergent = true;
13967 }
13968 }
13969 Node->NumOperands = Vals.size();
13970 Node->OperandList = Ops;
13971 if (!TLI->isSDNodeAlwaysUniform(Node)) {
13972 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
13973 Node->SDNodeBits.IsDivergent = IsDivergent;
13974 }
13976}
13977
13980 size_t Limit = SDNode::getMaxNumOperands();
13981 while (Vals.size() > Limit) {
13982 unsigned SliceIdx = Vals.size() - Limit;
13983 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
13984 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
13985 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
13986 Vals.emplace_back(NewTF);
13987 }
13988 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
13989}
13990
13992 EVT VT, SDNodeFlags Flags) {
13993 switch (Opcode) {
13994 default:
13995 return SDValue();
13996 case ISD::ADD:
13997 case ISD::OR:
13998 case ISD::XOR:
13999 case ISD::UMAX:
14000 return getConstant(0, DL, VT);
14001 case ISD::MUL:
14002 return getConstant(1, DL, VT);
14003 case ISD::AND:
14004 case ISD::UMIN:
14005 return getAllOnesConstant(DL, VT);
14006 case ISD::SMAX:
14008 case ISD::SMIN:
14010 case ISD::FADD:
14011 // If flags allow, prefer positive zero since it's generally cheaper
14012 // to materialize on most targets.
14013 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14014 case ISD::FMUL:
14015 return getConstantFP(1.0, DL, VT);
14016 case ISD::FMINNUM:
14017 case ISD::FMAXNUM: {
14018 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14019 const fltSemantics &Semantics = VT.getFltSemantics();
14020 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14021 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14022 APFloat::getLargest(Semantics);
14023 if (Opcode == ISD::FMAXNUM)
14024 NeutralAF.changeSign();
14025
14026 return getConstantFP(NeutralAF, DL, VT);
14027 }
14028 case ISD::FMINIMUM:
14029 case ISD::FMAXIMUM: {
14030 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14031 const fltSemantics &Semantics = VT.getFltSemantics();
14032 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14033 : APFloat::getLargest(Semantics);
14034 if (Opcode == ISD::FMAXIMUM)
14035 NeutralAF.changeSign();
14036
14037 return getConstantFP(NeutralAF, DL, VT);
14038 }
14039
14040 }
14041}
14042
14043/// Helper used to make a call to a library function that has one argument of
14044/// pointer type.
14045///
14046/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14047/// used to get or set floating-point state. They have one argument of pointer
14048/// type, which points to the memory region containing bits of the
14049/// floating-point state. The value returned by such function is ignored in the
14050/// created call.
14051///
14052/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14053/// \param Ptr Pointer used to save/load state.
14054/// \param InChain Ingoing token chain.
14055/// \returns Outgoing chain token.
14057 SDValue InChain,
14058 const SDLoc &DLoc) {
14059 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14061 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14062 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
14063 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
14064 TLI->getPointerTy(getDataLayout()));
14066 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14067 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
14068 std::move(Args));
14069 return TLI->LowerCallTo(CLI).second;
14070}
14071
14073 assert(From && To && "Invalid SDNode; empty source SDValue?");
14074 auto I = SDEI.find(From);
14075 if (I == SDEI.end())
14076 return;
14077
14078 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14079 // the iterator, hence the need to make a copy to prevent a use-after-free.
14080 NodeExtraInfo NEI = I->second;
14081 if (LLVM_LIKELY(!NEI.PCSections)) {
14082 // No deep copy required for the types of extra info set.
14083 //
14084 // FIXME: Investigate if other types of extra info also need deep copy. This
14085 // depends on the types of nodes they can be attached to: if some extra info
14086 // is only ever attached to nodes where a replacement To node is always the
14087 // node where later use and propagation of the extra info has the intended
14088 // semantics, no deep copy is required.
14089 SDEI[To] = std::move(NEI);
14090 return;
14091 }
14092
14093 const SDNode *EntrySDN = getEntryNode().getNode();
14094
14095 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14096 // through the replacement of From with To. Otherwise, replacements of a node
14097 // (From) with more complex nodes (To and its operands) may result in lost
14098 // extra info where the root node (To) is insignificant in further propagating
14099 // and using extra info when further lowering to MIR.
14100 //
14101 // In the first step pre-populate the visited set with the nodes reachable
14102 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14103 // DAG that is not new and should be left untouched.
14104 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14105 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14106 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14107 if (MaxDepth == 0) {
14108 // Remember this node in case we need to increase MaxDepth and continue
14109 // populating FromReach from this node.
14110 Leafs.emplace_back(N);
14111 return;
14112 }
14113 if (!FromReach.insert(N).second)
14114 return;
14115 for (const SDValue &Op : N->op_values())
14116 Self(Self, Op.getNode(), MaxDepth - 1);
14117 };
14118
14119 // Copy extra info to To and all its transitive operands (that are new).
14121 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14122 if (FromReach.contains(N))
14123 return true;
14124 if (!Visited.insert(N).second)
14125 return true;
14126 if (EntrySDN == N)
14127 return false;
14128 for (const SDValue &Op : N->op_values()) {
14129 if (N == To && Op.getNode() == EntrySDN) {
14130 // Special case: New node's operand is the entry node; just need to
14131 // copy extra info to new node.
14132 break;
14133 }
14134 if (!Self(Self, Op.getNode()))
14135 return false;
14136 }
14137 // Copy only if entry node was not reached.
14138 SDEI[N] = NEI;
14139 return true;
14140 };
14141
14142 // We first try with a lower MaxDepth, assuming that the path to common
14143 // operands between From and To is relatively short. This significantly
14144 // improves performance in the common case. The initial MaxDepth is big
14145 // enough to avoid retry in the common case; the last MaxDepth is large
14146 // enough to avoid having to use the fallback below (and protects from
14147 // potential stack exhaustion from recursion).
14148 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14149 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14150 // StartFrom is the previous (or initial) set of leafs reachable at the
14151 // previous maximum depth.
14153 std::swap(StartFrom, Leafs);
14154 for (const SDNode *N : StartFrom)
14155 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14156 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14157 return;
14158 // This should happen very rarely (reached the entry node).
14159 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14160 assert(!Leafs.empty());
14161 }
14162
14163 // This should not happen - but if it did, that means the subgraph reachable
14164 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14165 // could not visit all reachable common operands. Consequently, we were able
14166 // to reach the entry node.
14167 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14168 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14169 // Best-effort fallback if assertions disabled.
14170 SDEI[To] = std::move(NEI);
14171}
14172
14173#ifndef NDEBUG
14174static void checkForCyclesHelper(const SDNode *N,
14177 const llvm::SelectionDAG *DAG) {
14178 // If this node has already been checked, don't check it again.
14179 if (Checked.count(N))
14180 return;
14181
14182 // If a node has already been visited on this depth-first walk, reject it as
14183 // a cycle.
14184 if (!Visited.insert(N).second) {
14185 errs() << "Detected cycle in SelectionDAG\n";
14186 dbgs() << "Offending node:\n";
14187 N->dumprFull(DAG); dbgs() << "\n";
14188 abort();
14189 }
14190
14191 for (const SDValue &Op : N->op_values())
14192 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14193
14194 Checked.insert(N);
14195 Visited.erase(N);
14196}
14197#endif
14198
14200 const llvm::SelectionDAG *DAG,
14201 bool force) {
14202#ifndef NDEBUG
14203 bool check = force;
14204#ifdef EXPENSIVE_CHECKS
14205 check = true;
14206#endif // EXPENSIVE_CHECKS
14207 if (check) {
14208 assert(N && "Checking nonexistent SDNode");
14211 checkForCyclesHelper(N, visited, checked, DAG);
14212 }
14213#endif // !NDEBUG
14214}
14215
14216void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14217 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14218}
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.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition: Compiler.h:569
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:335
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Given that RA is a live propagate it s liveness to any other values it uses(according to Uses). void DeadArgumentEliminationPass
Given that RA is a live value
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
uint32_t Index
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file defines a hash set that can be used to remove duplication of nodes in a graph.
#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.
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:546
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
mir Rename Register Operands
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
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 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:119
This file describes how to lower LLVM code to machine code.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:247
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
Value * RHS
Value * LHS
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:1120
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1208
void copySign(const APFloat &RHS)
Definition: APFloat.h:1302
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:6057
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1190
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:1432
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1181
bool isFinite() const
Definition: APFloat.h:1454
bool isNaN() const
Definition: APFloat.h:1447
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1347
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1199
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1235
bool isZero() const
Definition: APFloat.h:1445
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1138
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1332
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1098
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1226
bool isPosZero() const
Definition: APFloat.h:1460
bool isNegZero() const
Definition: APFloat.h:1461
void changeSign()
Definition: APFloat.h:1297
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:1109
bool isInfinity() const
Definition: APFloat.h:1446
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:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1406
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:229
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1540
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1391
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1670
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1385
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:1512
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:1330
APInt abs() const
Get the absolute value.
Definition: APInt.h:1795
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:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition: APInt.h:258
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
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:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
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:1396
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:834
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1166
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1639
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1628
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1598
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:219
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:1435
unsigned logBase2() const
Definition: APInt.h:1761
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:827
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1319
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:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
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:1367
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:873
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:1257
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:440
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:553
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:306
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition: APInt.h:1417
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1388
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:1237
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:389
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
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
Recycle small arrays allocated from a BumpPtrAllocator.
Definition: ArrayRecycler.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:136
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
iterator begin() const
Definition: ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:142
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:191
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:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition: BitVector.h:335
BitVector & set()
Definition: BitVector.h:351
bool none() const
none - Returns true if none of the bits are set.
Definition: BitVector.h:188
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:159
unsigned getTargetFlags() const
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition: Constants.h: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
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:149
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition: Allocator.h:124
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
bool isMachineConstantPoolEntry() const
LLVM_ABI Type * getType() const
This class represents a range of values.
Definition: ConstantRange.h:47
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.
Definition: Constants.cpp:1713
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
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.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:198
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.
Definition: DataLayout.cpp:850
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:842
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
Definition: DataLayout.cpp:742
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:846
A debug info location.
Definition: DebugLoc.h:124
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:112
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:330
MachineBasicBlock * MBB
MBB - The current block.
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
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:359
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.
Definition: GlobalValue.h:265
unsigned getAddressSpace() const
Definition: GlobalValue.h:207
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:663
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:296
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.
constexpr bool isValid() const
Definition: LowLevelType.h:146
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)
TypeSize getValue() const
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:1077
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1445
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
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.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
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:229
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:303
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:99
Class to represent pointers.
Definition: DerivedTypes.h:700
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:720
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:740
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.
Definition: PointerUnion.h:141
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:19
Keeps track of dbg_value information through SDISel.
Definition: SelectionDAG.h:163
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:192
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.
ArrayRef< SDDbgValue * > getSDDbgValues(const SDNode *Node) const
Definition: SelectionDAG.h:198
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.
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
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
EVT getValueType() const
Convenience function for get().getValueType().
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.
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 bool isTargetMemoryOpcode(unsigned Opcode) const
Returns true if a node with the given target-specific opcode has a memory operand.
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
virtual SDValue EmitTargetCodeForMemset(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo) const
Emit target-specific code that performs a memset.
virtual SDValue EmitTargetCodeForMemmove(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memmove.
virtual SDValue EmitTargetCodeForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memcpy.
SDNodeFlags getFlags() const
Definition: SelectionDAG.h:390
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:229
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.
Definition: SelectionDAG.h:941
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.
Definition: SelectionDAG.h:578
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
Definition: SelectionDAG.h:500
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 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.
Definition: SelectionDAG.h:963
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.
Definition: SelectionDAG.h:956
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
Definition: SelectionDAG.h:504
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:459
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)
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 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...
Definition: SelectionDAG.h:398
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
Definition: SelectionDAG.h:558
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.
Definition: SelectionDAG.h:868
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
Definition: SelectionDAG.h:498
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
Definition: SelectionDAG.h:506
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.
std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
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)
Definition: SelectionDAG.h:902
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 void dump() const
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
Definition: SelectionDAG.h:499
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()
Definition: SelectionDAG.h:570
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
Definition: SelectionDAG.h:566
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)
Definition: SelectionDAG.h:707
const TargetLibraryInfo & getLibInfo() const
Definition: SelectionDAG.h:505
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 ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:493
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.
Definition: SelectionDAG.h:885
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.
LLVM_ABI ~SelectionDAG()
LLVMContext * getContext() const
Definition: SelectionDAG.h:511
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.
Definition: SelectionDAG.h:587
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 SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags)
Get the specified node if it's already available, or else return NULL.
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.
Definition: SelectionDAG.h:581
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.
Definition: SelectionDAG.h:918
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
Definition: SelectionDAG.h:561
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...
Definition: SmallPtrSet.h:380
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:418
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:470
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:705
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void reserve(size_type N)
Definition: SmallVector.h:664
iterator erase(const_iterator CI)
Definition: SmallVector.h:738
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
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:148
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.
const VecDesc * getVectorMappingInfo(StringRef F, const ElementCount &VF, bool Masked) const
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
virtual bool isCommutativeBinOp(unsigned Opcode) const
Returns true if the opcode is a commutative binary operation.
virtual ISD::NodeType getExtendForAtomicOps() const
Returns how the platform's atomic operations are extended (ZERO_EXTEND, SIGN_EXTEND,...
const char * getMemcpyName() const
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const
Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type from this source type with ...
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual bool shouldPreservePtrArith(const Function &F, EVT PtrVT) const
True if target has some particular form of dealing with pointer arithmetic semantics for pointers wit...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal for a comparison of the specified types on this ...
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
virtual bool hasVectorBlend() const
Return true if the target has a vector blend instruction.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
virtual unsigned getVectorIdxWidth(const DataLayout &DL) const
Returns the type to be used for the index operand vector operations.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual void computeKnownBitsForFrameIndex(int FIOp, KnownBits &Known, const MachineFunction &MF) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
virtual 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.
virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
This method can be implemented by targets that want to expose additional information about sign bits ...
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
virtual bool isSDNodeSourceOfDivergence(const SDNode *N, FunctionLoweringInfo *FLI, UniformityInfo *UA) const
virtual bool isSDNodeAlwaysUniform(const SDNode *N) const
virtual bool isSplatValueForTargetNode(SDValue Op, const APInt &DemandedElts, APInt &UndefElts, const SelectionDAG &DAG, unsigned Depth=0) const
Return true if vector Op has the same value across all DemandedElts, indicating any elements which ma...
virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const
Return true if folding a constant offset with the given GlobalAddress is legal.
virtual const Constant * getTargetConstantFromLoad(LoadSDNode *LD) const
This method returns the constant pool value that will be loaded by LD.
virtual bool isGAPlusOffset(SDNode *N, const GlobalValue *&GA, int64_t &Offset) const
Returns true (and the GlobalValue and the offset) if the node is a GlobalAddress + offset.
virtual bool isGuaranteedNotToBeUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, unsigned Depth) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
virtual bool isKnownNeverNaNForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const
If SNaN is false,.
virtual bool canCreateUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, bool ConsiderFlags, unsigned Depth) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:83
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetOptions Options
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetFrameLowering * getFrameLowering() 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:609
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:346
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
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:194
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:169
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:272
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:203
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:233
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:172
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:180
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:169
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:240
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
#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:2268
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:2273
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
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.
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:801
@ 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:774
@ TargetConstantPool
Definition: ISDOpcodes.h:184
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
Definition: ISDOpcodes.h:1281
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:504
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1386
@ 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
@ PARTIAL_REDUCE_SMLA
Definition: ISDOpcodes.h:1510
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1458
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
Definition: ISDOpcodes.h:1401
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1491
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:525
@ 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
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1379
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:587
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1170
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:765
@ TargetBlockAddress
Definition: ISDOpcodes.h:186
@ ConstantFP
Definition: ISDOpcodes.h:87
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1381
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1351
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1382
@ 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
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1141
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:835
@ ATOMIC_LOAD_USUB_COND
Definition: ISDOpcodes.h:1392
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:511
@ FATAN2
FATAN2 - atan2, inspired by libm.
Definition: ISDOpcodes.h:1020
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:215
@ GlobalAddress
Definition: ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:1364
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:862
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:571
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1476
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:410
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1480
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:738
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:892
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1490
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:505
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:985
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1377
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:975
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:249
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1378
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:1018
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1574
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1384
@ GlobalTLSAddress
Definition: ISDOpcodes.h:89
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
Definition: ISDOpcodes.h:1277
@ FrameIndex
Definition: ISDOpcodes.h:90
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1212
@ ATOMIC_LOAD_USUB_SAT
Definition: ISDOpcodes.h:1393
@ PARTIAL_REDUCE_UMLA
Definition: ISDOpcodes.h:1511
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:826
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:706
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:656
@ TargetExternalSymbol
Definition: ISDOpcodes.h:185
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1473
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:773
@ TargetJumpTable
Definition: ISDOpcodes.h:183
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition: ISDOpcodes.h:193
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1331
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1477
@ 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:809
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:1002
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1385
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1187
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:347
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1380
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:682
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:528
@ 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:778
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1347
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:228
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1492
@ RegisterMask
Definition: ISDOpcodes.h:85
@ 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:663
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition: ISDOpcodes.h:69
@ ATOMIC_LOAD_FMIN
Definition: ISDOpcodes.h:1387
@ 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
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1485
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:695
@ ATOMIC_LOAD_FMAXIMUM
Definition: ISDOpcodes.h:1388
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:756
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1376
@ 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:636
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1375
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:601
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:1075
@ 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:563
@ 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:832
@ TargetConstantFP
Definition: ISDOpcodes.h:175
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:928
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:793
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1448
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1358
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1383
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
Definition: ISDOpcodes.h:1059
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:351
@ ATOMIC_LOAD_FMINIMUM
Definition: ISDOpcodes.h:1389
@ TargetFrameIndex
Definition: ISDOpcodes.h:182
@ ConstantPool
Definition: ISDOpcodes.h:92
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition: ISDOpcodes.h:881
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:870
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:718
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1418
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:960
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:787
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:323
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
Definition: ISDOpcodes.h:1413
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1301
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1493
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:994
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1391
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1373
@ 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
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1081
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1374
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:908
@ 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:730
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:200
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1117
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1443
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition: ISDOpcodes.h:726
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:701
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1474
@ 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:672
@ 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:552
@ 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:648
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1372
@ ExternalSymbol
Definition: ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:1025
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:941
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition: ISDOpcodes.h:690
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:903
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:979
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Definition: ISDOpcodes.h:1546
@ 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:927
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1481
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:838
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1256
@ BlockAddress
Definition: ISDOpcodes.h:94
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1459
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:815
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
Definition: ISDOpcodes.h:1390
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:521
@ PARTIAL_REDUCE_SUMLA
Definition: ISDOpcodes.h:1512
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:360
@ AssertZext
Definition: ISDOpcodes.h:63
@ CALLSEQ_START
CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of a call sequence,...
Definition: ISDOpcodes.h:1250
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1122
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
Definition: ISDOpcodes.h:1086
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition: ISDOpcodes.h:713
@ 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:543
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)
Definition: ISDOpcodes.h:1762
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.
Definition: ISDOpcodes.h:1749
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...
Definition: ISDOpcodes.h:1754
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...
Definition: ISDOpcodes.h:1653
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.
Definition: ISDOpcodes.h:1640
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,...
Definition: ISDOpcodes.h:1691
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).
Definition: ISDOpcodes.h:1671
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()...
Definition: PatternMatch.h:980
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
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)
Definition: CommandLine.h:444
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:148
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:349
@ 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:1764
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:1744
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:1605
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:2491
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:260
void * PointerTy
Definition: GenericValue.h:21
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
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.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:270
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1643
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2155
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:252
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:663
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:1587
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:1555
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:1751
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:1598
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:336
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:288
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:1669
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition: APFloat.h:1629
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:1758
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.
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
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.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition: APFloat.h:1579
@ 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
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:1854
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:223
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition: Analysis.cpp:723
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:1916
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
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:577
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
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:1616
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition: APFloat.h:1656
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:378
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:853
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:760
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition: Metadata.h:780
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:777
static LLVM_ABI const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:266
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:294
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:307
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:304
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:308
static LLVM_ABI const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:268
static LLVM_ABI const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:267
static LLVM_ABI const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:264
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:306
static LLVM_ABI const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:265
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:320
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:390
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:507
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:279
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:295
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:345
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:368
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:354
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:380
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:311
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:318
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:287
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:251
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:216
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:323
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.
Definition: ValueTypes.cpp:330
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:331
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:303
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:448
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:294
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:158
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:916
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:248
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:211
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:101
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:117
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:773
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:235
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:427
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1056
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:267
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.
Definition: KnownBits.cpp:495
void makeNegative()
Make this value negative.
Definition: KnownBits.h:112
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:154
KnownBits byteSwap() const
Definition: KnownBits.h:507
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:282
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:86
KnownBits reverseBits() const
Definition: KnownBits.h:511
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:226
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).
Definition: KnownBits.cpp:187
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:165
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:314
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:370
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:104
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
Definition: KnownBits.cpp:228
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:218
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
Definition: KnownBits.cpp:791
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:304
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:173
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.
Definition: KnownBits.cpp:146
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:189
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:138
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:247
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:215
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:908
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1073
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1016
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:107
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:960
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
Definition: KnownBits.cpp:787
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:319
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:98
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:273
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:212
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
Definition: KnownBits.cpp:799
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:803
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:160
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:549
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).
Definition: KnownBits.cpp:285
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:205
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
Definition: KnownBits.cpp:795
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:117
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
These are IR-level optimization flags that may be propagated to SDNodes.
bool hasNonNeg() const
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
Definition: SelectionDAG.h:318
DAGUpdateListener *const Next
Definition: SelectionDAG.h:319
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)