LLVM 19.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
50#include "llvm/IR/Constant.h"
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"
68#include "llvm/Support/Mutex.h"
74#include <algorithm>
75#include <cassert>
76#include <cstdint>
77#include <cstdlib>
78#include <limits>
79#include <set>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85using namespace llvm::SDPatternMatch;
86
87/// makeVTList - Return an instance of the SDVTList struct initialized with the
88/// specified members.
89static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
90 SDVTList Res = {VTs, NumVTs};
91 return Res;
92}
93
94// Default null implementations of the callbacks.
98
99void SelectionDAG::DAGNodeDeletedListener::anchor() {}
100void SelectionDAG::DAGNodeInsertedListener::anchor() {}
101
102#define DEBUG_TYPE "selectiondag"
103
104static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
105 cl::Hidden, cl::init(true),
106 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
107
108static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
109 cl::desc("Number limit for gluing ld/st of memcpy."),
110 cl::Hidden, cl::init(0));
111
113 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
114}
115
116//===----------------------------------------------------------------------===//
117// ConstantFPSDNode Class
118//===----------------------------------------------------------------------===//
119
120/// isExactlyValue - We don't rely on operator== working on double values, as
121/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
122/// As such, this method can be used to do an exact bit-for-bit comparison of
123/// two floating point values.
125 return getValueAPF().bitwiseIsEqual(V);
126}
127
129 const APFloat& Val) {
130 assert(VT.isFloatingPoint() && "Can only convert between FP types");
131
132 // convert modifies in place, so make a copy.
133 APFloat Val2 = APFloat(Val);
134 bool losesInfo;
137 &losesInfo);
138 return !losesInfo;
139}
140
141//===----------------------------------------------------------------------===//
142// ISD Namespace
143//===----------------------------------------------------------------------===//
144
145bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
146 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
147 unsigned EltSize =
148 N->getValueType(0).getVectorElementType().getSizeInBits();
149 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
150 SplatVal = Op0->getAPIntValue().trunc(EltSize);
151 return true;
152 }
153 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
154 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
155 return true;
156 }
157 }
158
159 auto *BV = dyn_cast<BuildVectorSDNode>(N);
160 if (!BV)
161 return false;
162
163 APInt SplatUndef;
164 unsigned SplatBitSize;
165 bool HasUndefs;
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 // Endianness does not matter here. We are checking for a splat given the
168 // element size of the vector, and if we find such a splat for little endian
169 // layout, then that should be valid also for big endian (as the full vector
170 // size is known to be a multiple of the element size).
171 const bool IsBigEndian = false;
172 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
173 EltSize, IsBigEndian) &&
174 EltSize == SplatBitSize;
175}
176
177// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
178// specializations of the more general isConstantSplatVector()?
179
180bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
181 // Look through a bit convert.
182 while (N->getOpcode() == ISD::BITCAST)
183 N = N->getOperand(0).getNode();
184
185 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
186 APInt SplatVal;
187 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
188 }
189
190 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
191
192 unsigned i = 0, e = N->getNumOperands();
193
194 // Skip over all of the undef values.
195 while (i != e && N->getOperand(i).isUndef())
196 ++i;
197
198 // Do not accept an all-undef vector.
199 if (i == e) return false;
200
201 // Do not accept build_vectors that aren't all constants or which have non-~0
202 // elements. We have to be a bit careful here, as the type of the constant
203 // may not be the same as the type of the vector elements due to type
204 // legalization (the elements are promoted to a legal type for the target and
205 // a vector of a type may be legal when the base element type is not).
206 // We only want to check enough bits to cover the vector elements, because
207 // we care if the resultant vector is all ones, not whether the individual
208 // constants are.
209 SDValue NotZero = N->getOperand(i);
210 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
211 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
212 if (CN->getAPIntValue().countr_one() < EltSize)
213 return false;
214 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
215 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
216 return false;
217 } else
218 return false;
219
220 // Okay, we have at least one ~0 value, check to see if the rest match or are
221 // undefs. Even with the above element type twiddling, this should be OK, as
222 // the same type legalization should have applied to all the elements.
223 for (++i; i != e; ++i)
224 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
225 return false;
226 return true;
227}
228
229bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
230 // Look through a bit convert.
231 while (N->getOpcode() == ISD::BITCAST)
232 N = N->getOperand(0).getNode();
233
234 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
235 APInt SplatVal;
236 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
237 }
238
239 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
240
241 bool IsAllUndef = true;
242 for (const SDValue &Op : N->op_values()) {
243 if (Op.isUndef())
244 continue;
245 IsAllUndef = false;
246 // Do not accept build_vectors that aren't all constants or which have non-0
247 // elements. We have to be a bit careful here, as the type of the constant
248 // may not be the same as the type of the vector elements due to type
249 // legalization (the elements are promoted to a legal type for the target
250 // and a vector of a type may be legal when the base element type is not).
251 // We only want to check enough bits to cover the vector elements, because
252 // we care if the resultant vector is all zeros, not whether the individual
253 // constants are.
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
256 if (CN->getAPIntValue().countr_zero() < EltSize)
257 return false;
258 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
259 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
260 return false;
261 } else
262 return false;
263 }
264
265 // Do not accept an all-undef vector.
266 if (IsAllUndef)
267 return false;
268 return true;
269}
270
272 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
273}
274
276 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
277}
278
280 if (N->getOpcode() != ISD::BUILD_VECTOR)
281 return false;
282
283 for (const SDValue &Op : N->op_values()) {
284 if (Op.isUndef())
285 continue;
286 if (!isa<ConstantSDNode>(Op))
287 return false;
288 }
289 return true;
290}
291
293 if (N->getOpcode() != ISD::BUILD_VECTOR)
294 return false;
295
296 for (const SDValue &Op : N->op_values()) {
297 if (Op.isUndef())
298 continue;
299 if (!isa<ConstantFPSDNode>(Op))
300 return false;
301 }
302 return true;
303}
304
305bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
306 bool Signed) {
307 assert(N->getValueType(0).isVector() && "Expected a vector!");
308
309 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
310 if (EltSize <= NewEltSize)
311 return false;
312
313 if (N->getOpcode() == ISD::ZERO_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 !Signed;
317 }
318 if (N->getOpcode() == ISD::SIGN_EXTEND) {
319 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
320 NewEltSize) &&
321 Signed;
322 }
323 if (N->getOpcode() != ISD::BUILD_VECTOR)
324 return false;
325
326 for (const SDValue &Op : N->op_values()) {
327 if (Op.isUndef())
328 continue;
329 if (!isa<ConstantSDNode>(Op))
330 return false;
331
332 APInt C = Op->getAsAPIntVal().trunc(EltSize);
333 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
334 return false;
335 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
336 return false;
337 }
338
339 return true;
340}
341
343 // Return false if the node has no operands.
344 // This is "logically inconsistent" with the definition of "all" but
345 // is probably the desired behavior.
346 if (N->getNumOperands() == 0)
347 return false;
348 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
349}
350
352 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
353}
354
355template <typename ConstNodeType>
357 std::function<bool(ConstNodeType *)> Match,
358 bool AllowUndefs) {
359 // FIXME: Add support for scalar UNDEF cases?
360 if (auto *C = dyn_cast<ConstNodeType>(Op))
361 return Match(C);
362
363 // FIXME: Add support for vector UNDEF cases?
364 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
365 ISD::SPLAT_VECTOR != Op.getOpcode())
366 return false;
367
368 EVT SVT = Op.getValueType().getScalarType();
369 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
370 if (AllowUndefs && Op.getOperand(i).isUndef()) {
371 if (!Match(nullptr))
372 return false;
373 continue;
374 }
375
376 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
377 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
378 return false;
379 }
380 return true;
381}
382// Build used template types.
383template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
384 SDValue, std::function<bool(ConstantSDNode *)>, bool);
385template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
386 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
387
389 SDValue LHS, SDValue RHS,
390 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
391 bool AllowUndefs, bool AllowTypeMismatch) {
392 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
393 return false;
394
395 // TODO: Add support for scalar UNDEF cases?
396 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
397 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
398 return Match(LHSCst, RHSCst);
399
400 // TODO: Add support for vector UNDEF cases?
401 if (LHS.getOpcode() != RHS.getOpcode() ||
402 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
403 LHS.getOpcode() != ISD::SPLAT_VECTOR))
404 return false;
405
406 EVT SVT = LHS.getValueType().getScalarType();
407 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
408 SDValue LHSOp = LHS.getOperand(i);
409 SDValue RHSOp = RHS.getOperand(i);
410 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
411 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
412 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
413 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
414 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
415 return false;
416 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
417 LHSOp.getValueType() != RHSOp.getValueType()))
418 return false;
419 if (!Match(LHSCst, RHSCst))
420 return false;
421 }
422 return true;
423}
424
426 switch (VecReduceOpcode) {
427 default:
428 llvm_unreachable("Expected VECREDUCE opcode");
431 case ISD::VP_REDUCE_FADD:
432 case ISD::VP_REDUCE_SEQ_FADD:
433 return ISD::FADD;
436 case ISD::VP_REDUCE_FMUL:
437 case ISD::VP_REDUCE_SEQ_FMUL:
438 return ISD::FMUL;
440 case ISD::VP_REDUCE_ADD:
441 return ISD::ADD;
443 case ISD::VP_REDUCE_MUL:
444 return ISD::MUL;
446 case ISD::VP_REDUCE_AND:
447 return ISD::AND;
449 case ISD::VP_REDUCE_OR:
450 return ISD::OR;
452 case ISD::VP_REDUCE_XOR:
453 return ISD::XOR;
455 case ISD::VP_REDUCE_SMAX:
456 return ISD::SMAX;
458 case ISD::VP_REDUCE_SMIN:
459 return ISD::SMIN;
461 case ISD::VP_REDUCE_UMAX:
462 return ISD::UMAX;
464 case ISD::VP_REDUCE_UMIN:
465 return ISD::UMIN;
467 case ISD::VP_REDUCE_FMAX:
468 return ISD::FMAXNUM;
470 case ISD::VP_REDUCE_FMIN:
471 return ISD::FMINNUM;
473 return ISD::FMAXIMUM;
475 return ISD::FMINIMUM;
476 }
477}
478
479bool ISD::isVPOpcode(unsigned Opcode) {
480 switch (Opcode) {
481 default:
482 return false;
483#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
484 case ISD::VPSD: \
485 return true;
486#include "llvm/IR/VPIntrinsics.def"
487 }
488}
489
490bool ISD::isVPBinaryOp(unsigned Opcode) {
491 switch (Opcode) {
492 default:
493 break;
494#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
495#define VP_PROPERTY_BINARYOP return true;
496#define END_REGISTER_VP_SDNODE(VPSD) break;
497#include "llvm/IR/VPIntrinsics.def"
498 }
499 return false;
500}
501
502bool ISD::isVPReduction(unsigned Opcode) {
503 switch (Opcode) {
504 default:
505 break;
506#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
507#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
508#define END_REGISTER_VP_SDNODE(VPSD) break;
509#include "llvm/IR/VPIntrinsics.def"
510 }
511 return false;
512}
513
514/// The operand position of the vector mask.
515std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
516 switch (Opcode) {
517 default:
518 return std::nullopt;
519#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
520 case ISD::VPSD: \
521 return MASKPOS;
522#include "llvm/IR/VPIntrinsics.def"
523 }
524}
525
526/// The operand position of the explicit vector length parameter.
527std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
528 switch (Opcode) {
529 default:
530 return std::nullopt;
531#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
532 case ISD::VPSD: \
533 return EVLPOS;
534#include "llvm/IR/VPIntrinsics.def"
535 }
536}
537
538std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
539 bool hasFPExcept) {
540 // FIXME: Return strict opcodes in case of fp exceptions.
541 switch (VPOpcode) {
542 default:
543 return std::nullopt;
544#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
545#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
546#define END_REGISTER_VP_SDNODE(VPOPC) break;
547#include "llvm/IR/VPIntrinsics.def"
548 }
549 return std::nullopt;
550}
551
552unsigned ISD::getVPForBaseOpcode(unsigned Opcode) {
553 switch (Opcode) {
554 default:
555 llvm_unreachable("can not translate this Opcode to VP.");
556#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
557#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
558#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
559#include "llvm/IR/VPIntrinsics.def"
560 }
561}
562
564 switch (ExtType) {
565 case ISD::EXTLOAD:
566 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
567 case ISD::SEXTLOAD:
568 return ISD::SIGN_EXTEND;
569 case ISD::ZEXTLOAD:
570 return ISD::ZERO_EXTEND;
571 default:
572 break;
573 }
574
575 llvm_unreachable("Invalid LoadExtType");
576}
577
579 // To perform this operation, we just need to swap the L and G bits of the
580 // operation.
581 unsigned OldL = (Operation >> 2) & 1;
582 unsigned OldG = (Operation >> 1) & 1;
583 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
584 (OldL << 1) | // New G bit
585 (OldG << 2)); // New L bit.
586}
587
589 unsigned Operation = Op;
590 if (isIntegerLike)
591 Operation ^= 7; // Flip L, G, E bits, but not U.
592 else
593 Operation ^= 15; // Flip all of the condition bits.
594
596 Operation &= ~8; // Don't let N and U bits get set.
597
598 return ISD::CondCode(Operation);
599}
600
602 return getSetCCInverseImpl(Op, Type.isInteger());
603}
604
606 bool isIntegerLike) {
607 return getSetCCInverseImpl(Op, isIntegerLike);
608}
609
610/// For an integer comparison, return 1 if the comparison is a signed operation
611/// and 2 if the result is an unsigned comparison. Return zero if the operation
612/// does not depend on the sign of the input (setne and seteq).
613static int isSignedOp(ISD::CondCode Opcode) {
614 switch (Opcode) {
615 default: llvm_unreachable("Illegal integer setcc operation!");
616 case ISD::SETEQ:
617 case ISD::SETNE: return 0;
618 case ISD::SETLT:
619 case ISD::SETLE:
620 case ISD::SETGT:
621 case ISD::SETGE: return 1;
622 case ISD::SETULT:
623 case ISD::SETULE:
624 case ISD::SETUGT:
625 case ISD::SETUGE: return 2;
626 }
627}
628
630 EVT Type) {
631 bool IsInteger = Type.isInteger();
632 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
633 // Cannot fold a signed integer setcc with an unsigned integer setcc.
634 return ISD::SETCC_INVALID;
635
636 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
637
638 // If the N and U bits get set, then the resultant comparison DOES suddenly
639 // care about orderedness, and it is true when ordered.
640 if (Op > ISD::SETTRUE2)
641 Op &= ~16; // Clear the U bit if the N bit is set.
642
643 // Canonicalize illegal integer setcc's.
644 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
645 Op = ISD::SETNE;
646
647 return ISD::CondCode(Op);
648}
649
651 EVT Type) {
652 bool IsInteger = Type.isInteger();
653 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
654 // Cannot fold a signed setcc with an unsigned setcc.
655 return ISD::SETCC_INVALID;
656
657 // Combine all of the condition bits.
658 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
659
660 // Canonicalize illegal integer setcc's.
661 if (IsInteger) {
662 switch (Result) {
663 default: break;
664 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
665 case ISD::SETOEQ: // SETEQ & SETU[LG]E
666 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
667 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
668 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
669 }
670 }
671
672 return Result;
673}
674
675//===----------------------------------------------------------------------===//
676// SDNode Profile Support
677//===----------------------------------------------------------------------===//
678
679/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
680static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
681 ID.AddInteger(OpC);
682}
683
684/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
685/// solely with their pointer.
687 ID.AddPointer(VTList.VTs);
688}
689
690/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
692 ArrayRef<SDValue> Ops) {
693 for (const auto &Op : Ops) {
694 ID.AddPointer(Op.getNode());
695 ID.AddInteger(Op.getResNo());
696 }
697}
698
699/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
701 ArrayRef<SDUse> Ops) {
702 for (const auto &Op : Ops) {
703 ID.AddPointer(Op.getNode());
704 ID.AddInteger(Op.getResNo());
705 }
706}
707
708static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
709 SDVTList VTList, ArrayRef<SDValue> OpList) {
710 AddNodeIDOpcode(ID, OpC);
711 AddNodeIDValueTypes(ID, VTList);
712 AddNodeIDOperands(ID, OpList);
713}
714
715/// If this is an SDNode with special info, add this info to the NodeID data.
717 switch (N->getOpcode()) {
720 case ISD::MCSymbol:
721 llvm_unreachable("Should only be used on nodes with operands");
722 default: break; // Normal nodes don't need extra info.
724 case ISD::Constant: {
725 const ConstantSDNode *C = cast<ConstantSDNode>(N);
726 ID.AddPointer(C->getConstantIntValue());
727 ID.AddBoolean(C->isOpaque());
728 break;
729 }
731 case ISD::ConstantFP:
732 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
733 break;
738 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
739 ID.AddPointer(GA->getGlobal());
740 ID.AddInteger(GA->getOffset());
741 ID.AddInteger(GA->getTargetFlags());
742 break;
743 }
744 case ISD::BasicBlock:
745 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
746 break;
747 case ISD::Register:
748 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
749 break;
751 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
752 break;
753 case ISD::SRCVALUE:
754 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
755 break;
756 case ISD::FrameIndex:
758 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
759 break;
762 if (cast<LifetimeSDNode>(N)->hasOffset()) {
763 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
764 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
765 }
766 break;
768 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
769 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
770 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
771 break;
772 case ISD::JumpTable:
774 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
775 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
776 break;
779 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
780 ID.AddInteger(CP->getAlign().value());
781 ID.AddInteger(CP->getOffset());
782 if (CP->isMachineConstantPoolEntry())
783 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
784 else
785 ID.AddPointer(CP->getConstVal());
786 ID.AddInteger(CP->getTargetFlags());
787 break;
788 }
789 case ISD::TargetIndex: {
790 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
791 ID.AddInteger(TI->getIndex());
792 ID.AddInteger(TI->getOffset());
793 ID.AddInteger(TI->getTargetFlags());
794 break;
795 }
796 case ISD::LOAD: {
797 const LoadSDNode *LD = cast<LoadSDNode>(N);
798 ID.AddInteger(LD->getMemoryVT().getRawBits());
799 ID.AddInteger(LD->getRawSubclassData());
800 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
801 ID.AddInteger(LD->getMemOperand()->getFlags());
802 break;
803 }
804 case ISD::STORE: {
805 const StoreSDNode *ST = cast<StoreSDNode>(N);
806 ID.AddInteger(ST->getMemoryVT().getRawBits());
807 ID.AddInteger(ST->getRawSubclassData());
808 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
809 ID.AddInteger(ST->getMemOperand()->getFlags());
810 break;
811 }
812 case ISD::VP_LOAD: {
813 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
814 ID.AddInteger(ELD->getMemoryVT().getRawBits());
815 ID.AddInteger(ELD->getRawSubclassData());
816 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
817 ID.AddInteger(ELD->getMemOperand()->getFlags());
818 break;
819 }
820 case ISD::VP_STORE: {
821 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
822 ID.AddInteger(EST->getMemoryVT().getRawBits());
823 ID.AddInteger(EST->getRawSubclassData());
824 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
825 ID.AddInteger(EST->getMemOperand()->getFlags());
826 break;
827 }
828 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
829 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
830 ID.AddInteger(SLD->getMemoryVT().getRawBits());
831 ID.AddInteger(SLD->getRawSubclassData());
832 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
833 break;
834 }
835 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
836 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
837 ID.AddInteger(SST->getMemoryVT().getRawBits());
838 ID.AddInteger(SST->getRawSubclassData());
839 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
840 break;
841 }
842 case ISD::VP_GATHER: {
843 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
844 ID.AddInteger(EG->getMemoryVT().getRawBits());
845 ID.AddInteger(EG->getRawSubclassData());
846 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
847 ID.AddInteger(EG->getMemOperand()->getFlags());
848 break;
849 }
850 case ISD::VP_SCATTER: {
851 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
852 ID.AddInteger(ES->getMemoryVT().getRawBits());
853 ID.AddInteger(ES->getRawSubclassData());
854 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
855 ID.AddInteger(ES->getMemOperand()->getFlags());
856 break;
857 }
858 case ISD::MLOAD: {
859 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
860 ID.AddInteger(MLD->getMemoryVT().getRawBits());
861 ID.AddInteger(MLD->getRawSubclassData());
862 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
863 ID.AddInteger(MLD->getMemOperand()->getFlags());
864 break;
865 }
866 case ISD::MSTORE: {
867 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
868 ID.AddInteger(MST->getMemoryVT().getRawBits());
869 ID.AddInteger(MST->getRawSubclassData());
870 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
871 ID.AddInteger(MST->getMemOperand()->getFlags());
872 break;
873 }
874 case ISD::MGATHER: {
875 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
876 ID.AddInteger(MG->getMemoryVT().getRawBits());
877 ID.AddInteger(MG->getRawSubclassData());
878 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
879 ID.AddInteger(MG->getMemOperand()->getFlags());
880 break;
881 }
882 case ISD::MSCATTER: {
883 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
884 ID.AddInteger(MS->getMemoryVT().getRawBits());
885 ID.AddInteger(MS->getRawSubclassData());
886 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
887 ID.AddInteger(MS->getMemOperand()->getFlags());
888 break;
889 }
892 case ISD::ATOMIC_SWAP:
904 case ISD::ATOMIC_LOAD:
905 case ISD::ATOMIC_STORE: {
906 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
907 ID.AddInteger(AT->getMemoryVT().getRawBits());
908 ID.AddInteger(AT->getRawSubclassData());
909 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
910 ID.AddInteger(AT->getMemOperand()->getFlags());
911 break;
912 }
913 case ISD::VECTOR_SHUFFLE: {
914 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
915 for (int M : Mask)
916 ID.AddInteger(M);
917 break;
918 }
920 case ISD::BlockAddress: {
921 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
922 ID.AddPointer(BA->getBlockAddress());
923 ID.AddInteger(BA->getOffset());
924 ID.AddInteger(BA->getTargetFlags());
925 break;
926 }
927 case ISD::AssertAlign:
928 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
929 break;
930 case ISD::PREFETCH:
933 // Handled by MemIntrinsicSDNode check after the switch.
934 break;
935 } // end switch (N->getOpcode())
936
937 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
938 // to check.
939 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
940 ID.AddInteger(MN->getRawSubclassData());
941 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
942 ID.AddInteger(MN->getMemOperand()->getFlags());
943 ID.AddInteger(MN->getMemoryVT().getRawBits());
944 }
945}
946
947/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
948/// data.
949static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
950 AddNodeIDOpcode(ID, N->getOpcode());
951 // Add the return value info.
952 AddNodeIDValueTypes(ID, N->getVTList());
953 // Add the operand info.
954 AddNodeIDOperands(ID, N->ops());
955
956 // Handle SDNode leafs with special info.
958}
959
960//===----------------------------------------------------------------------===//
961// SelectionDAG Class
962//===----------------------------------------------------------------------===//
963
964/// doNotCSE - Return true if CSE should not be performed for this node.
965static bool doNotCSE(SDNode *N) {
966 if (N->getValueType(0) == MVT::Glue)
967 return true; // Never CSE anything that produces a glue result.
968
969 switch (N->getOpcode()) {
970 default: break;
971 case ISD::HANDLENODE:
972 case ISD::EH_LABEL:
973 return true; // Never CSE these nodes.
974 }
975
976 // Check that remaining values produced are not flags.
977 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
978 if (N->getValueType(i) == MVT::Glue)
979 return true; // Never CSE anything that produces a glue result.
980
981 return false;
982}
983
984/// RemoveDeadNodes - This method deletes all unreachable nodes in the
985/// SelectionDAG.
987 // Create a dummy node (which is not added to allnodes), that adds a reference
988 // to the root node, preventing it from being deleted.
989 HandleSDNode Dummy(getRoot());
990
992
993 // Add all obviously-dead nodes to the DeadNodes worklist.
994 for (SDNode &Node : allnodes())
995 if (Node.use_empty())
996 DeadNodes.push_back(&Node);
997
998 RemoveDeadNodes(DeadNodes);
999
1000 // If the root changed (e.g. it was a dead load, update the root).
1001 setRoot(Dummy.getValue());
1002}
1003
1004/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1005/// given list, and any nodes that become unreachable as a result.
1007
1008 // Process the worklist, deleting the nodes and adding their uses to the
1009 // worklist.
1010 while (!DeadNodes.empty()) {
1011 SDNode *N = DeadNodes.pop_back_val();
1012 // Skip to next node if we've already managed to delete the node. This could
1013 // happen if replacing a node causes a node previously added to the node to
1014 // be deleted.
1015 if (N->getOpcode() == ISD::DELETED_NODE)
1016 continue;
1017
1018 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1019 DUL->NodeDeleted(N, nullptr);
1020
1021 // Take the node out of the appropriate CSE map.
1022 RemoveNodeFromCSEMaps(N);
1023
1024 // Next, brutally remove the operand list. This is safe to do, as there are
1025 // no cycles in the graph.
1026 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1027 SDUse &Use = *I++;
1028 SDNode *Operand = Use.getNode();
1029 Use.set(SDValue());
1030
1031 // Now that we removed this operand, see if there are no uses of it left.
1032 if (Operand->use_empty())
1033 DeadNodes.push_back(Operand);
1034 }
1035
1036 DeallocateNode(N);
1037 }
1038}
1039
1041 SmallVector<SDNode*, 16> DeadNodes(1, N);
1042
1043 // Create a dummy node that adds a reference to the root node, preventing
1044 // it from being deleted. (This matters if the root is an operand of the
1045 // dead node.)
1046 HandleSDNode Dummy(getRoot());
1047
1048 RemoveDeadNodes(DeadNodes);
1049}
1050
1052 // First take this out of the appropriate CSE map.
1053 RemoveNodeFromCSEMaps(N);
1054
1055 // Finally, remove uses due to operands of this node, remove from the
1056 // AllNodes list, and delete the node.
1057 DeleteNodeNotInCSEMaps(N);
1058}
1059
1060void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1061 assert(N->getIterator() != AllNodes.begin() &&
1062 "Cannot delete the entry node!");
1063 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1064
1065 // Drop all of the operands and decrement used node's use counts.
1066 N->DropOperands();
1067
1068 DeallocateNode(N);
1069}
1070
1071void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1072 assert(!(V->isVariadic() && isParameter));
1073 if (isParameter)
1074 ByvalParmDbgValues.push_back(V);
1075 else
1076 DbgValues.push_back(V);
1077 for (const SDNode *Node : V->getSDNodes())
1078 if (Node)
1079 DbgValMap[Node].push_back(V);
1080}
1081
1082void SDDbgInfo::erase(const SDNode *Node) {
1083 DbgValMapType::iterator I = DbgValMap.find(Node);
1084 if (I == DbgValMap.end())
1085 return;
1086 for (auto &Val: I->second)
1087 Val->setIsInvalidated();
1088 DbgValMap.erase(I);
1089}
1090
1091void SelectionDAG::DeallocateNode(SDNode *N) {
1092 // If we have operands, deallocate them.
1093 removeOperands(N);
1094
1095 NodeAllocator.Deallocate(AllNodes.remove(N));
1096
1097 // Set the opcode to DELETED_NODE to help catch bugs when node
1098 // memory is reallocated.
1099 // FIXME: There are places in SDag that have grown a dependency on the opcode
1100 // value in the released node.
1101 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1102 N->NodeType = ISD::DELETED_NODE;
1103
1104 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1105 // them and forget about that node.
1106 DbgInfo->erase(N);
1107
1108 // Invalidate extra info.
1109 SDEI.erase(N);
1110}
1111
1112#ifndef NDEBUG
1113/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1114static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1115 switch (N->getOpcode()) {
1116 default:
1117 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1118 TLI->verifyTargetSDNode(N);
1119 break;
1120 case ISD::BUILD_PAIR: {
1121 EVT VT = N->getValueType(0);
1122 assert(N->getNumValues() == 1 && "Too many results!");
1123 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1124 "Wrong return type!");
1125 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1126 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1127 "Mismatched operand types!");
1128 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1129 "Wrong operand type!");
1130 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1131 "Wrong return type size");
1132 break;
1133 }
1134 case ISD::BUILD_VECTOR: {
1135 assert(N->getNumValues() == 1 && "Too many results!");
1136 assert(N->getValueType(0).isVector() && "Wrong return type!");
1137 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1138 "Wrong number of operands!");
1139 EVT EltVT = N->getValueType(0).getVectorElementType();
1140 for (const SDUse &Op : N->ops()) {
1141 assert((Op.getValueType() == EltVT ||
1142 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1143 EltVT.bitsLE(Op.getValueType()))) &&
1144 "Wrong operand type!");
1145 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1146 "Operands must all have the same type");
1147 }
1148 break;
1149 }
1150 }
1151}
1152#endif // NDEBUG
1153
1154/// Insert a newly allocated node into the DAG.
1155///
1156/// Handles insertion into the all nodes list and CSE map, as well as
1157/// verification and other common operations when a new node is allocated.
1158void SelectionDAG::InsertNode(SDNode *N) {
1159 AllNodes.push_back(N);
1160#ifndef NDEBUG
1161 N->PersistentId = NextPersistentId++;
1162 VerifySDNode(N, TLI);
1163#endif
1164 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1165 DUL->NodeInserted(N);
1166}
1167
1168/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1169/// correspond to it. This is useful when we're about to delete or repurpose
1170/// the node. We don't want future request for structurally identical nodes
1171/// to return N anymore.
1172bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1173 bool Erased = false;
1174 switch (N->getOpcode()) {
1175 case ISD::HANDLENODE: return false; // noop.
1176 case ISD::CONDCODE:
1177 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1178 "Cond code doesn't exist!");
1179 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1180 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1181 break;
1183 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1184 break;
1186 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1187 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1188 ESN->getSymbol(), ESN->getTargetFlags()));
1189 break;
1190 }
1191 case ISD::MCSymbol: {
1192 auto *MCSN = cast<MCSymbolSDNode>(N);
1193 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1194 break;
1195 }
1196 case ISD::VALUETYPE: {
1197 EVT VT = cast<VTSDNode>(N)->getVT();
1198 if (VT.isExtended()) {
1199 Erased = ExtendedValueTypeNodes.erase(VT);
1200 } else {
1201 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1202 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1203 }
1204 break;
1205 }
1206 default:
1207 // Remove it from the CSE Map.
1208 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1209 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1210 Erased = CSEMap.RemoveNode(N);
1211 break;
1212 }
1213#ifndef NDEBUG
1214 // Verify that the node was actually in one of the CSE maps, unless it has a
1215 // glue result (which cannot be CSE'd) or is one of the special cases that are
1216 // not subject to CSE.
1217 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1218 !N->isMachineOpcode() && !doNotCSE(N)) {
1219 N->dump(this);
1220 dbgs() << "\n";
1221 llvm_unreachable("Node is not in map!");
1222 }
1223#endif
1224 return Erased;
1225}
1226
1227/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1228/// maps and modified in place. Add it back to the CSE maps, unless an identical
1229/// node already exists, in which case transfer all its users to the existing
1230/// node. This transfer can potentially trigger recursive merging.
1231void
1232SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1233 // For node types that aren't CSE'd, just act as if no identical node
1234 // already exists.
1235 if (!doNotCSE(N)) {
1236 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1237 if (Existing != N) {
1238 // If there was already an existing matching node, use ReplaceAllUsesWith
1239 // to replace the dead one with the existing one. This can cause
1240 // recursive merging of other unrelated nodes down the line.
1241 ReplaceAllUsesWith(N, Existing);
1242
1243 // N is now dead. Inform the listeners and delete it.
1244 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1245 DUL->NodeDeleted(N, Existing);
1246 DeleteNodeNotInCSEMaps(N);
1247 return;
1248 }
1249 }
1250
1251 // If the node doesn't already exist, we updated it. Inform listeners.
1252 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1253 DUL->NodeUpdated(N);
1254}
1255
1256/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1257/// were replaced with those specified. If this node is never memoized,
1258/// return null, otherwise return a pointer to the slot it would take. If a
1259/// node already exists with these operands, the slot will be non-null.
1260SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1261 void *&InsertPos) {
1262 if (doNotCSE(N))
1263 return nullptr;
1264
1265 SDValue Ops[] = { Op };
1267 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1269 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1270 if (Node)
1271 Node->intersectFlagsWith(N->getFlags());
1272 return Node;
1273}
1274
1275/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1276/// were replaced with those specified. If this node is never memoized,
1277/// return null, otherwise return a pointer to the slot it would take. If a
1278/// node already exists with these operands, the slot will be non-null.
1279SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1280 SDValue Op1, SDValue Op2,
1281 void *&InsertPos) {
1282 if (doNotCSE(N))
1283 return nullptr;
1284
1285 SDValue Ops[] = { Op1, Op2 };
1287 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1289 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1290 if (Node)
1291 Node->intersectFlagsWith(N->getFlags());
1292 return Node;
1293}
1294
1295/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1296/// were replaced with those specified. If this node is never memoized,
1297/// return null, otherwise return a pointer to the slot it would take. If a
1298/// node already exists with these operands, the slot will be non-null.
1299SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1300 void *&InsertPos) {
1301 if (doNotCSE(N))
1302 return nullptr;
1303
1305 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1307 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1308 if (Node)
1309 Node->intersectFlagsWith(N->getFlags());
1310 return Node;
1311}
1312
1314 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1315 : VT.getTypeForEVT(*getContext());
1316
1317 return getDataLayout().getABITypeAlign(Ty);
1318}
1319
1320// EntryNode could meaningfully have debug info if we can find it...
1322 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1323 getVTList(MVT::Other, MVT::Glue)),
1324 Root(getEntryNode()) {
1325 InsertNode(&EntryNode);
1326 DbgInfo = new SDDbgInfo();
1327}
1328
1330 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1331 const TargetLibraryInfo *LibraryInfo,
1332 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1333 BlockFrequencyInfo *BFIin,
1334 FunctionVarLocs const *VarLocs) {
1335 MF = &NewMF;
1336 SDAGISelPass = PassPtr;
1337 ORE = &NewORE;
1340 LibInfo = LibraryInfo;
1341 Context = &MF->getFunction().getContext();
1342 UA = NewUA;
1343 PSI = PSIin;
1344 BFI = BFIin;
1345 FnVarLocs = VarLocs;
1346}
1347
1349 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1350 allnodes_clear();
1351 OperandRecycler.clear(OperandAllocator);
1352 delete DbgInfo;
1353}
1354
1356 return MF->getFunction().hasOptSize() ||
1358}
1359
1360void SelectionDAG::allnodes_clear() {
1361 assert(&*AllNodes.begin() == &EntryNode);
1362 AllNodes.remove(AllNodes.begin());
1363 while (!AllNodes.empty())
1364 DeallocateNode(&AllNodes.front());
1365#ifndef NDEBUG
1366 NextPersistentId = 0;
1367#endif
1368}
1369
1370SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1371 void *&InsertPos) {
1372 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1373 if (N) {
1374 switch (N->getOpcode()) {
1375 default: break;
1376 case ISD::Constant:
1377 case ISD::ConstantFP:
1378 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1379 "debug location. Use another overload.");
1380 }
1381 }
1382 return N;
1383}
1384
1385SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1386 const SDLoc &DL, void *&InsertPos) {
1387 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1388 if (N) {
1389 switch (N->getOpcode()) {
1390 case ISD::Constant:
1391 case ISD::ConstantFP:
1392 // Erase debug location from the node if the node is used at several
1393 // different places. Do not propagate one location to all uses as it
1394 // will cause a worse single stepping debugging experience.
1395 if (N->getDebugLoc() != DL.getDebugLoc())
1396 N->setDebugLoc(DebugLoc());
1397 break;
1398 default:
1399 // When the node's point of use is located earlier in the instruction
1400 // sequence than its prior point of use, update its debug info to the
1401 // earlier location.
1402 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1403 N->setDebugLoc(DL.getDebugLoc());
1404 break;
1405 }
1406 }
1407 return N;
1408}
1409
1411 allnodes_clear();
1412 OperandRecycler.clear(OperandAllocator);
1413 OperandAllocator.Reset();
1414 CSEMap.clear();
1415
1416 ExtendedValueTypeNodes.clear();
1417 ExternalSymbols.clear();
1418 TargetExternalSymbols.clear();
1419 MCSymbols.clear();
1420 SDEI.clear();
1421 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1422 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1423
1424 EntryNode.UseList = nullptr;
1425 InsertNode(&EntryNode);
1426 Root = getEntryNode();
1427 DbgInfo->clear();
1428}
1429
1431 return VT.bitsGT(Op.getValueType())
1432 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1433 : getNode(ISD::FP_ROUND, DL, VT, Op,
1434 getIntPtrConstant(0, DL, /*isTarget=*/true));
1435}
1436
1437std::pair<SDValue, SDValue>
1439 const SDLoc &DL, EVT VT) {
1440 assert(!VT.bitsEq(Op.getValueType()) &&
1441 "Strict no-op FP extend/round not allowed.");
1442 SDValue Res =
1443 VT.bitsGT(Op.getValueType())
1444 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1445 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1446 {Chain, Op, getIntPtrConstant(0, DL)});
1447
1448 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1449}
1450
1452 return VT.bitsGT(Op.getValueType()) ?
1453 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1454 getNode(ISD::TRUNCATE, DL, VT, Op);
1455}
1456
1458 return VT.bitsGT(Op.getValueType()) ?
1459 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1460 getNode(ISD::TRUNCATE, DL, VT, Op);
1461}
1462
1464 return VT.bitsGT(Op.getValueType()) ?
1465 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1466 getNode(ISD::TRUNCATE, DL, VT, Op);
1467}
1468
1470 EVT VT) {
1471 assert(!VT.isVector());
1472 auto Type = Op.getValueType();
1473 SDValue DestOp;
1474 if (Type == VT)
1475 return Op;
1476 auto Size = Op.getValueSizeInBits();
1477 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1478 if (DestOp.getValueType() == VT)
1479 return DestOp;
1480
1481 return getAnyExtOrTrunc(DestOp, DL, VT);
1482}
1483
1485 EVT VT) {
1486 assert(!VT.isVector());
1487 auto Type = Op.getValueType();
1488 SDValue DestOp;
1489 if (Type == VT)
1490 return Op;
1491 auto Size = Op.getValueSizeInBits();
1492 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1493 if (DestOp.getValueType() == VT)
1494 return DestOp;
1495
1496 return getSExtOrTrunc(DestOp, DL, VT);
1497}
1498
1500 EVT VT) {
1501 assert(!VT.isVector());
1502 auto Type = Op.getValueType();
1503 SDValue DestOp;
1504 if (Type == VT)
1505 return Op;
1506 auto Size = Op.getValueSizeInBits();
1507 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1508 if (DestOp.getValueType() == VT)
1509 return DestOp;
1510
1511 return getZExtOrTrunc(DestOp, DL, VT);
1512}
1513
1515 EVT OpVT) {
1516 if (VT.bitsLE(Op.getValueType()))
1517 return getNode(ISD::TRUNCATE, SL, VT, Op);
1518
1520 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1521}
1522
1524 EVT OpVT = Op.getValueType();
1525 assert(VT.isInteger() && OpVT.isInteger() &&
1526 "Cannot getZeroExtendInReg FP types");
1527 assert(VT.isVector() == OpVT.isVector() &&
1528 "getZeroExtendInReg type should be vector iff the operand "
1529 "type is vector!");
1530 assert((!VT.isVector() ||
1532 "Vector element counts must match in getZeroExtendInReg");
1533 assert(VT.bitsLE(OpVT) && "Not extending!");
1534 if (OpVT == VT)
1535 return Op;
1537 VT.getScalarSizeInBits());
1538 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1539}
1540
1542 // Only unsigned pointer semantics are supported right now. In the future this
1543 // might delegate to TLI to check pointer signedness.
1544 return getZExtOrTrunc(Op, DL, VT);
1545}
1546
1548 // Only unsigned pointer semantics are supported right now. In the future this
1549 // might delegate to TLI to check pointer signedness.
1550 return getZeroExtendInReg(Op, DL, VT);
1551}
1552
1554 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1555}
1556
1557/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1559 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1560}
1561
1563 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1564 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1565}
1566
1568 SDValue Mask, SDValue EVL, EVT VT) {
1569 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1570 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1571}
1572
1574 SDValue Mask, SDValue EVL) {
1575 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1576}
1577
1579 SDValue Mask, SDValue EVL) {
1580 if (VT.bitsGT(Op.getValueType()))
1581 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1582 if (VT.bitsLT(Op.getValueType()))
1583 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1584 return Op;
1585}
1586
1588 EVT OpVT) {
1589 if (!V)
1590 return getConstant(0, DL, VT);
1591
1592 switch (TLI->getBooleanContents(OpVT)) {
1595 return getConstant(1, DL, VT);
1597 return getAllOnesConstant(DL, VT);
1598 }
1599 llvm_unreachable("Unexpected boolean content enum!");
1600}
1601
1603 bool isT, bool isO) {
1604 EVT EltVT = VT.getScalarType();
1605 assert((EltVT.getSizeInBits() >= 64 ||
1606 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1607 "getConstant with a uint64_t value that doesn't fit in the type!");
1608 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1609}
1610
1612 bool isT, bool isO) {
1613 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1614}
1615
1617 EVT VT, bool isT, bool isO) {
1618 assert(VT.isInteger() && "Cannot create FP integer constant!");
1619
1620 EVT EltVT = VT.getScalarType();
1621 const ConstantInt *Elt = &Val;
1622
1623 // In some cases the vector type is legal but the element type is illegal and
1624 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1625 // inserted value (the type does not need to match the vector element type).
1626 // Any extra bits introduced will be truncated away.
1627 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1629 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1630 APInt NewVal;
1631 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1632 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1633 else
1634 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1635 Elt = ConstantInt::get(*getContext(), NewVal);
1636 }
1637 // In other cases the element type is illegal and needs to be expanded, for
1638 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1639 // the value into n parts and use a vector type with n-times the elements.
1640 // Then bitcast to the type requested.
1641 // Legalizing constants too early makes the DAGCombiner's job harder so we
1642 // only legalize if the DAG tells us we must produce legal types.
1643 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1644 TLI->getTypeAction(*getContext(), EltVT) ==
1646 const APInt &NewVal = Elt->getValue();
1647 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1648 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1649
1650 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1651 if (VT.isScalableVector() ||
1653 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1654 "Can only handle an even split!");
1655 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1656
1657 SmallVector<SDValue, 2> ScalarParts;
1658 for (unsigned i = 0; i != Parts; ++i)
1659 ScalarParts.push_back(getConstant(
1660 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1661 ViaEltVT, isT, isO));
1662
1663 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1664 }
1665
1666 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1667 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1668
1669 // Check the temporary vector is the correct size. If this fails then
1670 // getTypeToTransformTo() probably returned a type whose size (in bits)
1671 // isn't a power-of-2 factor of the requested type size.
1672 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1673
1674 SmallVector<SDValue, 2> EltParts;
1675 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1676 EltParts.push_back(getConstant(
1677 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1678 ViaEltVT, isT, isO));
1679
1680 // EltParts is currently in little endian order. If we actually want
1681 // big-endian order then reverse it now.
1682 if (getDataLayout().isBigEndian())
1683 std::reverse(EltParts.begin(), EltParts.end());
1684
1685 // The elements must be reversed when the element order is different
1686 // to the endianness of the elements (because the BITCAST is itself a
1687 // vector shuffle in this situation). However, we do not need any code to
1688 // perform this reversal because getConstant() is producing a vector
1689 // splat.
1690 // This situation occurs in MIPS MSA.
1691
1693 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1694 llvm::append_range(Ops, EltParts);
1695
1696 SDValue V =
1697 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1698 return V;
1699 }
1700
1701 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1702 "APInt size does not match type size!");
1703 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1704 SDVTList VTs = getVTList(EltVT);
1706 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1707 ID.AddPointer(Elt);
1708 ID.AddBoolean(isO);
1709 void *IP = nullptr;
1710 SDNode *N = nullptr;
1711 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1712 if (!VT.isVector())
1713 return SDValue(N, 0);
1714
1715 if (!N) {
1716 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1717 CSEMap.InsertNode(N, IP);
1718 InsertNode(N);
1719 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1720 }
1721
1722 SDValue Result(N, 0);
1723 if (VT.isVector())
1724 Result = getSplat(VT, DL, Result);
1725 return Result;
1726}
1727
1729 bool isTarget) {
1730 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1731}
1732
1734 const SDLoc &DL, bool LegalTypes) {
1735 assert(VT.isInteger() && "Shift amount is not an integer type!");
1736 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout(), LegalTypes);
1737 return getConstant(Val, DL, ShiftVT);
1738}
1739
1741 const SDLoc &DL, bool LegalTypes) {
1742 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1743 return getShiftAmountConstant(Val.getZExtValue(), VT, DL, LegalTypes);
1744}
1745
1747 bool isTarget) {
1748 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1749}
1750
1752 bool isTarget) {
1753 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1754}
1755
1757 EVT VT, bool isTarget) {
1758 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1759
1760 EVT EltVT = VT.getScalarType();
1761
1762 // Do the map lookup using the actual bit pattern for the floating point
1763 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1764 // we don't have issues with SNANs.
1765 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1766 SDVTList VTs = getVTList(EltVT);
1768 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1769 ID.AddPointer(&V);
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<ConstantFPSDNode>(isTarget, &V, VTs);
1778 CSEMap.InsertNode(N, IP);
1779 InsertNode(N);
1780 }
1781
1782 SDValue Result(N, 0);
1783 if (VT.isVector())
1784 Result = getSplat(VT, DL, Result);
1785 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1786 return Result;
1787}
1788
1790 bool isTarget) {
1791 EVT EltVT = VT.getScalarType();
1792 if (EltVT == MVT::f32)
1793 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1794 if (EltVT == MVT::f64)
1795 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1796 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1797 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1798 bool Ignored;
1799 APFloat APF = APFloat(Val);
1801 &Ignored);
1802 return getConstantFP(APF, DL, VT, isTarget);
1803 }
1804 llvm_unreachable("Unsupported type in getConstantFP");
1805}
1806
1808 EVT VT, int64_t Offset, bool isTargetGA,
1809 unsigned TargetFlags) {
1810 assert((TargetFlags == 0 || isTargetGA) &&
1811 "Cannot set target flags on target-independent globals");
1812
1813 // Truncate (with sign-extension) the offset value to the pointer size.
1815 if (BitWidth < 64)
1817
1818 unsigned Opc;
1819 if (GV->isThreadLocal())
1821 else
1822 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1823
1824 SDVTList VTs = getVTList(VT);
1826 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1827 ID.AddPointer(GV);
1828 ID.AddInteger(Offset);
1829 ID.AddInteger(TargetFlags);
1830 void *IP = nullptr;
1831 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1832 return SDValue(E, 0);
1833
1834 auto *N = newSDNode<GlobalAddressSDNode>(
1835 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1836 CSEMap.InsertNode(N, IP);
1837 InsertNode(N);
1838 return SDValue(N, 0);
1839}
1840
1841SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1842 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1843 SDVTList VTs = getVTList(VT);
1845 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1846 ID.AddInteger(FI);
1847 void *IP = nullptr;
1848 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1849 return SDValue(E, 0);
1850
1851 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1852 CSEMap.InsertNode(N, IP);
1853 InsertNode(N);
1854 return SDValue(N, 0);
1855}
1856
1857SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1858 unsigned TargetFlags) {
1859 assert((TargetFlags == 0 || isTarget) &&
1860 "Cannot set target flags on target-independent jump tables");
1861 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1862 SDVTList VTs = getVTList(VT);
1864 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1865 ID.AddInteger(JTI);
1866 ID.AddInteger(TargetFlags);
1867 void *IP = nullptr;
1868 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1869 return SDValue(E, 0);
1870
1871 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1872 CSEMap.InsertNode(N, IP);
1873 InsertNode(N);
1874 return SDValue(N, 0);
1875}
1876
1878 const SDLoc &DL) {
1880 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1881 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1882}
1883
1885 MaybeAlign Alignment, int Offset,
1886 bool isTarget, unsigned TargetFlags) {
1887 assert((TargetFlags == 0 || isTarget) &&
1888 "Cannot set target flags on target-independent globals");
1889 if (!Alignment)
1890 Alignment = shouldOptForSize()
1891 ? getDataLayout().getABITypeAlign(C->getType())
1892 : getDataLayout().getPrefTypeAlign(C->getType());
1893 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1894 SDVTList VTs = getVTList(VT);
1896 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1897 ID.AddInteger(Alignment->value());
1898 ID.AddInteger(Offset);
1899 ID.AddPointer(C);
1900 ID.AddInteger(TargetFlags);
1901 void *IP = nullptr;
1902 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1903 return SDValue(E, 0);
1904
1905 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1906 TargetFlags);
1907 CSEMap.InsertNode(N, IP);
1908 InsertNode(N);
1909 SDValue V = SDValue(N, 0);
1910 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1911 return V;
1912}
1913
1915 MaybeAlign Alignment, int Offset,
1916 bool isTarget, unsigned TargetFlags) {
1917 assert((TargetFlags == 0 || isTarget) &&
1918 "Cannot set target flags on target-independent globals");
1919 if (!Alignment)
1920 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1921 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1922 SDVTList VTs = getVTList(VT);
1924 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1925 ID.AddInteger(Alignment->value());
1926 ID.AddInteger(Offset);
1927 C->addSelectionDAGCSEId(ID);
1928 ID.AddInteger(TargetFlags);
1929 void *IP = nullptr;
1930 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1931 return SDValue(E, 0);
1932
1933 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1934 TargetFlags);
1935 CSEMap.InsertNode(N, IP);
1936 InsertNode(N);
1937 return SDValue(N, 0);
1938}
1939
1942 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), std::nullopt);
1943 ID.AddPointer(MBB);
1944 void *IP = nullptr;
1945 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1946 return SDValue(E, 0);
1947
1948 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1949 CSEMap.InsertNode(N, IP);
1950 InsertNode(N);
1951 return SDValue(N, 0);
1952}
1953
1955 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1956 ValueTypeNodes.size())
1957 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1958
1959 SDNode *&N = VT.isExtended() ?
1960 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1961
1962 if (N) return SDValue(N, 0);
1963 N = newSDNode<VTSDNode>(VT);
1964 InsertNode(N);
1965 return SDValue(N, 0);
1966}
1967
1969 SDNode *&N = ExternalSymbols[Sym];
1970 if (N) return SDValue(N, 0);
1971 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
1972 InsertNode(N);
1973 return SDValue(N, 0);
1974}
1975
1977 SDNode *&N = MCSymbols[Sym];
1978 if (N)
1979 return SDValue(N, 0);
1980 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
1981 InsertNode(N);
1982 return SDValue(N, 0);
1983}
1984
1986 unsigned TargetFlags) {
1987 SDNode *&N =
1988 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
1989 if (N) return SDValue(N, 0);
1990 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
1991 InsertNode(N);
1992 return SDValue(N, 0);
1993}
1994
1996 if ((unsigned)Cond >= CondCodeNodes.size())
1997 CondCodeNodes.resize(Cond+1);
1998
1999 if (!CondCodeNodes[Cond]) {
2000 auto *N = newSDNode<CondCodeSDNode>(Cond);
2001 CondCodeNodes[Cond] = N;
2002 InsertNode(N);
2003 }
2004
2005 return SDValue(CondCodeNodes[Cond], 0);
2006}
2007
2009 bool ConstantFold) {
2010 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2011 "APInt size does not match type size!");
2012
2013 if (MulImm == 0)
2014 return getConstant(0, DL, VT);
2015
2016 if (ConstantFold) {
2017 const MachineFunction &MF = getMachineFunction();
2018 const Function &F = MF.getFunction();
2019 ConstantRange CR = getVScaleRange(&F, 64);
2020 if (const APInt *C = CR.getSingleElement())
2021 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2022 }
2023
2024 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2025}
2026
2028 bool ConstantFold) {
2029 if (EC.isScalable())
2030 return getVScale(DL, VT,
2031 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2032
2033 return getConstant(EC.getKnownMinValue(), DL, VT);
2034}
2035
2037 APInt One(ResVT.getScalarSizeInBits(), 1);
2038 return getStepVector(DL, ResVT, One);
2039}
2040
2042 const APInt &StepVal) {
2043 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2044 if (ResVT.isScalableVector())
2045 return getNode(
2046 ISD::STEP_VECTOR, DL, ResVT,
2047 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2048
2049 SmallVector<SDValue, 16> OpsStepConstants;
2050 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2051 OpsStepConstants.push_back(
2052 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2053 return getBuildVector(ResVT, DL, OpsStepConstants);
2054}
2055
2056/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2057/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2059 std::swap(N1, N2);
2061}
2062
2064 SDValue N2, ArrayRef<int> Mask) {
2065 assert(VT.getVectorNumElements() == Mask.size() &&
2066 "Must have the same number of vector elements as mask elements!");
2067 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2068 "Invalid VECTOR_SHUFFLE");
2069
2070 // Canonicalize shuffle undef, undef -> undef
2071 if (N1.isUndef() && N2.isUndef())
2072 return getUNDEF(VT);
2073
2074 // Validate that all indices in Mask are within the range of the elements
2075 // input to the shuffle.
2076 int NElts = Mask.size();
2077 assert(llvm::all_of(Mask,
2078 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2079 "Index out of range");
2080
2081 // Copy the mask so we can do any needed cleanup.
2082 SmallVector<int, 8> MaskVec(Mask);
2083
2084 // Canonicalize shuffle v, v -> v, undef
2085 if (N1 == N2) {
2086 N2 = getUNDEF(VT);
2087 for (int i = 0; i != NElts; ++i)
2088 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2089 }
2090
2091 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2092 if (N1.isUndef())
2093 commuteShuffle(N1, N2, MaskVec);
2094
2095 if (TLI->hasVectorBlend()) {
2096 // If shuffling a splat, try to blend the splat instead. We do this here so
2097 // that even when this arises during lowering we don't have to re-handle it.
2098 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2099 BitVector UndefElements;
2100 SDValue Splat = BV->getSplatValue(&UndefElements);
2101 if (!Splat)
2102 return;
2103
2104 for (int i = 0; i < NElts; ++i) {
2105 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2106 continue;
2107
2108 // If this input comes from undef, mark it as such.
2109 if (UndefElements[MaskVec[i] - Offset]) {
2110 MaskVec[i] = -1;
2111 continue;
2112 }
2113
2114 // If we can blend a non-undef lane, use that instead.
2115 if (!UndefElements[i])
2116 MaskVec[i] = i + Offset;
2117 }
2118 };
2119 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2120 BlendSplat(N1BV, 0);
2121 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2122 BlendSplat(N2BV, NElts);
2123 }
2124
2125 // Canonicalize all index into lhs, -> shuffle lhs, undef
2126 // Canonicalize all index into rhs, -> shuffle rhs, undef
2127 bool AllLHS = true, AllRHS = true;
2128 bool N2Undef = N2.isUndef();
2129 for (int i = 0; i != NElts; ++i) {
2130 if (MaskVec[i] >= NElts) {
2131 if (N2Undef)
2132 MaskVec[i] = -1;
2133 else
2134 AllLHS = false;
2135 } else if (MaskVec[i] >= 0) {
2136 AllRHS = false;
2137 }
2138 }
2139 if (AllLHS && AllRHS)
2140 return getUNDEF(VT);
2141 if (AllLHS && !N2Undef)
2142 N2 = getUNDEF(VT);
2143 if (AllRHS) {
2144 N1 = getUNDEF(VT);
2145 commuteShuffle(N1, N2, MaskVec);
2146 }
2147 // Reset our undef status after accounting for the mask.
2148 N2Undef = N2.isUndef();
2149 // Re-check whether both sides ended up undef.
2150 if (N1.isUndef() && N2Undef)
2151 return getUNDEF(VT);
2152
2153 // If Identity shuffle return that node.
2154 bool Identity = true, AllSame = true;
2155 for (int i = 0; i != NElts; ++i) {
2156 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2157 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2158 }
2159 if (Identity && NElts)
2160 return N1;
2161
2162 // Shuffling a constant splat doesn't change the result.
2163 if (N2Undef) {
2164 SDValue V = N1;
2165
2166 // Look through any bitcasts. We check that these don't change the number
2167 // (and size) of elements and just changes their types.
2168 while (V.getOpcode() == ISD::BITCAST)
2169 V = V->getOperand(0);
2170
2171 // A splat should always show up as a build vector node.
2172 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2173 BitVector UndefElements;
2174 SDValue Splat = BV->getSplatValue(&UndefElements);
2175 // If this is a splat of an undef, shuffling it is also undef.
2176 if (Splat && Splat.isUndef())
2177 return getUNDEF(VT);
2178
2179 bool SameNumElts =
2180 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2181
2182 // We only have a splat which can skip shuffles if there is a splatted
2183 // value and no undef lanes rearranged by the shuffle.
2184 if (Splat && UndefElements.none()) {
2185 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2186 // number of elements match or the value splatted is a zero constant.
2187 if (SameNumElts || isNullConstant(Splat))
2188 return N1;
2189 }
2190
2191 // If the shuffle itself creates a splat, build the vector directly.
2192 if (AllSame && SameNumElts) {
2193 EVT BuildVT = BV->getValueType(0);
2194 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2195 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2196
2197 // We may have jumped through bitcasts, so the type of the
2198 // BUILD_VECTOR may not match the type of the shuffle.
2199 if (BuildVT != VT)
2200 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2201 return NewBV;
2202 }
2203 }
2204 }
2205
2206 SDVTList VTs = getVTList(VT);
2208 SDValue Ops[2] = { N1, N2 };
2210 for (int i = 0; i != NElts; ++i)
2211 ID.AddInteger(MaskVec[i]);
2212
2213 void* IP = nullptr;
2214 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2215 return SDValue(E, 0);
2216
2217 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2218 // SDNode doesn't have access to it. This memory will be "leaked" when
2219 // the node is deallocated, but recovered when the NodeAllocator is released.
2220 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2221 llvm::copy(MaskVec, MaskAlloc);
2222
2223 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2224 dl.getDebugLoc(), MaskAlloc);
2225 createOperands(N, Ops);
2226
2227 CSEMap.InsertNode(N, IP);
2228 InsertNode(N);
2229 SDValue V = SDValue(N, 0);
2230 NewSDValueDbgMsg(V, "Creating new node: ", this);
2231 return V;
2232}
2233
2235 EVT VT = SV.getValueType(0);
2236 SmallVector<int, 8> MaskVec(SV.getMask());
2238
2239 SDValue Op0 = SV.getOperand(0);
2240 SDValue Op1 = SV.getOperand(1);
2241 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2242}
2243
2245 SDVTList VTs = getVTList(VT);
2247 AddNodeIDNode(ID, ISD::Register, VTs, std::nullopt);
2248 ID.AddInteger(RegNo);
2249 void *IP = nullptr;
2250 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2251 return SDValue(E, 0);
2252
2253 auto *N = newSDNode<RegisterSDNode>(RegNo, VTs);
2254 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2255 CSEMap.InsertNode(N, IP);
2256 InsertNode(N);
2257 return SDValue(N, 0);
2258}
2259
2262 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), std::nullopt);
2263 ID.AddPointer(RegMask);
2264 void *IP = nullptr;
2265 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2266 return SDValue(E, 0);
2267
2268 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2269 CSEMap.InsertNode(N, IP);
2270 InsertNode(N);
2271 return SDValue(N, 0);
2272}
2273
2275 MCSymbol *Label) {
2276 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2277}
2278
2279SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2280 SDValue Root, MCSymbol *Label) {
2282 SDValue Ops[] = { Root };
2283 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2284 ID.AddPointer(Label);
2285 void *IP = nullptr;
2286 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2287 return SDValue(E, 0);
2288
2289 auto *N =
2290 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2291 createOperands(N, Ops);
2292
2293 CSEMap.InsertNode(N, IP);
2294 InsertNode(N);
2295 return SDValue(N, 0);
2296}
2297
2299 int64_t Offset, bool isTarget,
2300 unsigned TargetFlags) {
2301 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2302 SDVTList VTs = getVTList(VT);
2303
2305 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
2306 ID.AddPointer(BA);
2307 ID.AddInteger(Offset);
2308 ID.AddInteger(TargetFlags);
2309 void *IP = nullptr;
2310 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2311 return SDValue(E, 0);
2312
2313 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2314 CSEMap.InsertNode(N, IP);
2315 InsertNode(N);
2316 return SDValue(N, 0);
2317}
2318
2321 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), std::nullopt);
2322 ID.AddPointer(V);
2323
2324 void *IP = nullptr;
2325 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2326 return SDValue(E, 0);
2327
2328 auto *N = newSDNode<SrcValueSDNode>(V);
2329 CSEMap.InsertNode(N, IP);
2330 InsertNode(N);
2331 return SDValue(N, 0);
2332}
2333
2336 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), std::nullopt);
2337 ID.AddPointer(MD);
2338
2339 void *IP = nullptr;
2340 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2341 return SDValue(E, 0);
2342
2343 auto *N = newSDNode<MDNodeSDNode>(MD);
2344 CSEMap.InsertNode(N, IP);
2345 InsertNode(N);
2346 return SDValue(N, 0);
2347}
2348
2350 if (VT == V.getValueType())
2351 return V;
2352
2353 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2354}
2355
2357 unsigned SrcAS, unsigned DestAS) {
2358 SDVTList VTs = getVTList(VT);
2359 SDValue Ops[] = {Ptr};
2362 ID.AddInteger(SrcAS);
2363 ID.AddInteger(DestAS);
2364
2365 void *IP = nullptr;
2366 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2367 return SDValue(E, 0);
2368
2369 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2370 VTs, SrcAS, DestAS);
2371 createOperands(N, Ops);
2372
2373 CSEMap.InsertNode(N, IP);
2374 InsertNode(N);
2375 return SDValue(N, 0);
2376}
2377
2379 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2380}
2381
2382/// getShiftAmountOperand - Return the specified value casted to
2383/// the target's desired shift amount type.
2385 EVT OpTy = Op.getValueType();
2386 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2387 if (OpTy == ShTy || OpTy.isVector()) return Op;
2388
2389 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2390}
2391
2393 SDLoc dl(Node);
2395 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2396 EVT VT = Node->getValueType(0);
2397 SDValue Tmp1 = Node->getOperand(0);
2398 SDValue Tmp2 = Node->getOperand(1);
2399 const MaybeAlign MA(Node->getConstantOperandVal(3));
2400
2401 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2402 Tmp2, MachinePointerInfo(V));
2403 SDValue VAList = VAListLoad;
2404
2405 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2406 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2407 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2408
2409 VAList =
2410 getNode(ISD::AND, dl, VAList.getValueType(), VAList,
2411 getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2412 }
2413
2414 // Increment the pointer, VAList, to the next vaarg
2415 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2416 getConstant(getDataLayout().getTypeAllocSize(
2417 VT.getTypeForEVT(*getContext())),
2418 dl, VAList.getValueType()));
2419 // Store the incremented VAList to the legalized pointer
2420 Tmp1 =
2421 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2422 // Load the actual argument out of the pointer VAList
2423 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2424}
2425
2427 SDLoc dl(Node);
2429 // This defaults to loading a pointer from the input and storing it to the
2430 // output, returning the chain.
2431 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2432 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2433 SDValue Tmp1 =
2434 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2435 Node->getOperand(2), MachinePointerInfo(VS));
2436 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2437 MachinePointerInfo(VD));
2438}
2439
2441 const DataLayout &DL = getDataLayout();
2442 Type *Ty = VT.getTypeForEVT(*getContext());
2443 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2444
2445 if (TLI->isTypeLegal(VT) || !VT.isVector())
2446 return RedAlign;
2447
2449 const Align StackAlign = TFI->getStackAlign();
2450
2451 // See if we can choose a smaller ABI alignment in cases where it's an
2452 // illegal vector type that will get broken down.
2453 if (RedAlign > StackAlign) {
2454 EVT IntermediateVT;
2455 MVT RegisterVT;
2456 unsigned NumIntermediates;
2457 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2458 NumIntermediates, RegisterVT);
2459 Ty = IntermediateVT.getTypeForEVT(*getContext());
2460 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2461 if (RedAlign2 < RedAlign)
2462 RedAlign = RedAlign2;
2463 }
2464
2465 return RedAlign;
2466}
2467
2469 MachineFrameInfo &MFI = MF->getFrameInfo();
2471 int StackID = 0;
2472 if (Bytes.isScalable())
2473 StackID = TFI->getStackIDForScalableVectors();
2474 // The stack id gives an indication of whether the object is scalable or
2475 // not, so it's safe to pass in the minimum size here.
2476 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2477 false, nullptr, StackID);
2478 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2479}
2480
2482 Type *Ty = VT.getTypeForEVT(*getContext());
2483 Align StackAlign =
2484 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2485 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2486}
2487
2489 TypeSize VT1Size = VT1.getStoreSize();
2490 TypeSize VT2Size = VT2.getStoreSize();
2491 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2492 "Don't know how to choose the maximum size when creating a stack "
2493 "temporary");
2494 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2495 ? VT1Size
2496 : VT2Size;
2497
2498 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2499 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2500 const DataLayout &DL = getDataLayout();
2501 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2502 return CreateStackTemporary(Bytes, Align);
2503}
2504
2506 ISD::CondCode Cond, const SDLoc &dl) {
2507 EVT OpVT = N1.getValueType();
2508
2509 auto GetUndefBooleanConstant = [&]() {
2510 if (VT.getScalarType() == MVT::i1 ||
2511 TLI->getBooleanContents(OpVT) ==
2513 return getUNDEF(VT);
2514 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2515 // so we cannot use getUNDEF(). Return zero instead.
2516 return getConstant(0, dl, VT);
2517 };
2518
2519 // These setcc operations always fold.
2520 switch (Cond) {
2521 default: break;
2522 case ISD::SETFALSE:
2523 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2524 case ISD::SETTRUE:
2525 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2526
2527 case ISD::SETOEQ:
2528 case ISD::SETOGT:
2529 case ISD::SETOGE:
2530 case ISD::SETOLT:
2531 case ISD::SETOLE:
2532 case ISD::SETONE:
2533 case ISD::SETO:
2534 case ISD::SETUO:
2535 case ISD::SETUEQ:
2536 case ISD::SETUNE:
2537 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2538 break;
2539 }
2540
2541 if (OpVT.isInteger()) {
2542 // For EQ and NE, we can always pick a value for the undef to make the
2543 // predicate pass or fail, so we can return undef.
2544 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2545 // icmp eq/ne X, undef -> undef.
2546 if ((N1.isUndef() || N2.isUndef()) &&
2547 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2548 return GetUndefBooleanConstant();
2549
2550 // If both operands are undef, we can return undef for int comparison.
2551 // icmp undef, undef -> undef.
2552 if (N1.isUndef() && N2.isUndef())
2553 return GetUndefBooleanConstant();
2554
2555 // icmp X, X -> true/false
2556 // icmp X, undef -> true/false because undef could be X.
2557 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2558 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2559 }
2560
2561 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2562 const APInt &C2 = N2C->getAPIntValue();
2563 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2564 const APInt &C1 = N1C->getAPIntValue();
2565
2567 dl, VT, OpVT);
2568 }
2569 }
2570
2571 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2572 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2573
2574 if (N1CFP && N2CFP) {
2575 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2576 switch (Cond) {
2577 default: break;
2578 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2579 return GetUndefBooleanConstant();
2580 [[fallthrough]];
2581 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2582 OpVT);
2583 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2584 return GetUndefBooleanConstant();
2585 [[fallthrough]];
2587 R==APFloat::cmpLessThan, dl, VT,
2588 OpVT);
2589 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2590 return GetUndefBooleanConstant();
2591 [[fallthrough]];
2592 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2593 OpVT);
2594 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2595 return GetUndefBooleanConstant();
2596 [[fallthrough]];
2598 VT, OpVT);
2599 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2600 return GetUndefBooleanConstant();
2601 [[fallthrough]];
2603 R==APFloat::cmpEqual, dl, VT,
2604 OpVT);
2605 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2606 return GetUndefBooleanConstant();
2607 [[fallthrough]];
2609 R==APFloat::cmpEqual, dl, VT, OpVT);
2610 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2611 OpVT);
2612 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2613 OpVT);
2615 R==APFloat::cmpEqual, dl, VT,
2616 OpVT);
2617 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2618 OpVT);
2620 R==APFloat::cmpLessThan, dl, VT,
2621 OpVT);
2623 R==APFloat::cmpUnordered, dl, VT,
2624 OpVT);
2626 VT, OpVT);
2627 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2628 OpVT);
2629 }
2630 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2631 // Ensure that the constant occurs on the RHS.
2633 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2634 return SDValue();
2635 return getSetCC(dl, VT, N2, N1, SwappedCond);
2636 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2637 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2638 // If an operand is known to be a nan (or undef that could be a nan), we can
2639 // fold it.
2640 // Choosing NaN for the undef will always make unordered comparison succeed
2641 // and ordered comparison fails.
2642 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2643 switch (ISD::getUnorderedFlavor(Cond)) {
2644 default:
2645 llvm_unreachable("Unknown flavor!");
2646 case 0: // Known false.
2647 return getBoolConstant(false, dl, VT, OpVT);
2648 case 1: // Known true.
2649 return getBoolConstant(true, dl, VT, OpVT);
2650 case 2: // Undefined.
2651 return GetUndefBooleanConstant();
2652 }
2653 }
2654
2655 // Could not fold it.
2656 return SDValue();
2657}
2658
2659/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2660/// use this predicate to simplify operations downstream.
2662 unsigned BitWidth = Op.getScalarValueSizeInBits();
2664}
2665
2666/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2667/// this predicate to simplify operations downstream. Mask is known to be zero
2668/// for bits that V cannot have.
2670 unsigned Depth) const {
2671 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2672}
2673
2674/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2675/// DemandedElts. We use this predicate to simplify operations downstream.
2676/// Mask is known to be zero for bits that V cannot have.
2678 const APInt &DemandedElts,
2679 unsigned Depth) const {
2680 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2681}
2682
2683/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2684/// DemandedElts. We use this predicate to simplify operations downstream.
2686 unsigned Depth /* = 0 */) const {
2687 return computeKnownBits(V, DemandedElts, Depth).isZero();
2688}
2689
2690/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2692 unsigned Depth) const {
2693 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2694}
2695
2697 const APInt &DemandedElts,
2698 unsigned Depth) const {
2699 EVT VT = Op.getValueType();
2700 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2701
2702 unsigned NumElts = VT.getVectorNumElements();
2703 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2704
2705 APInt KnownZeroElements = APInt::getZero(NumElts);
2706 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2707 if (!DemandedElts[EltIdx])
2708 continue; // Don't query elements that are not demanded.
2709 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2710 if (MaskedVectorIsZero(Op, Mask, Depth))
2711 KnownZeroElements.setBit(EltIdx);
2712 }
2713 return KnownZeroElements;
2714}
2715
2716/// isSplatValue - Return true if the vector V has the same value
2717/// across all DemandedElts. For scalable vectors, we don't know the
2718/// number of lanes at compile time. Instead, we use a 1 bit APInt
2719/// to represent a conservative value for all lanes; that is, that
2720/// one bit value is implicitly splatted across all lanes.
2721bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2722 APInt &UndefElts, unsigned Depth) const {
2723 unsigned Opcode = V.getOpcode();
2724 EVT VT = V.getValueType();
2725 assert(VT.isVector() && "Vector type expected");
2726 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2727 "scalable demanded bits are ignored");
2728
2729 if (!DemandedElts)
2730 return false; // No demanded elts, better to assume we don't know anything.
2731
2732 if (Depth >= MaxRecursionDepth)
2733 return false; // Limit search depth.
2734
2735 // Deal with some common cases here that work for both fixed and scalable
2736 // vector types.
2737 switch (Opcode) {
2738 case ISD::SPLAT_VECTOR:
2739 UndefElts = V.getOperand(0).isUndef()
2740 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2741 : APInt(DemandedElts.getBitWidth(), 0);
2742 return true;
2743 case ISD::ADD:
2744 case ISD::SUB:
2745 case ISD::AND:
2746 case ISD::XOR:
2747 case ISD::OR: {
2748 APInt UndefLHS, UndefRHS;
2749 SDValue LHS = V.getOperand(0);
2750 SDValue RHS = V.getOperand(1);
2751 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2752 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2753 UndefElts = UndefLHS | UndefRHS;
2754 return true;
2755 }
2756 return false;
2757 }
2758 case ISD::ABS:
2759 case ISD::TRUNCATE:
2760 case ISD::SIGN_EXTEND:
2761 case ISD::ZERO_EXTEND:
2762 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2763 default:
2764 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2765 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2766 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2767 Depth);
2768 break;
2769}
2770
2771 // We don't support other cases than those above for scalable vectors at
2772 // the moment.
2773 if (VT.isScalableVector())
2774 return false;
2775
2776 unsigned NumElts = VT.getVectorNumElements();
2777 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2778 UndefElts = APInt::getZero(NumElts);
2779
2780 switch (Opcode) {
2781 case ISD::BUILD_VECTOR: {
2782 SDValue Scl;
2783 for (unsigned i = 0; i != NumElts; ++i) {
2784 SDValue Op = V.getOperand(i);
2785 if (Op.isUndef()) {
2786 UndefElts.setBit(i);
2787 continue;
2788 }
2789 if (!DemandedElts[i])
2790 continue;
2791 if (Scl && Scl != Op)
2792 return false;
2793 Scl = Op;
2794 }
2795 return true;
2796 }
2797 case ISD::VECTOR_SHUFFLE: {
2798 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2799 APInt DemandedLHS = APInt::getZero(NumElts);
2800 APInt DemandedRHS = APInt::getZero(NumElts);
2801 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2802 for (int i = 0; i != (int)NumElts; ++i) {
2803 int M = Mask[i];
2804 if (M < 0) {
2805 UndefElts.setBit(i);
2806 continue;
2807 }
2808 if (!DemandedElts[i])
2809 continue;
2810 if (M < (int)NumElts)
2811 DemandedLHS.setBit(M);
2812 else
2813 DemandedRHS.setBit(M - NumElts);
2814 }
2815
2816 // If we aren't demanding either op, assume there's no splat.
2817 // If we are demanding both ops, assume there's no splat.
2818 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2819 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2820 return false;
2821
2822 // See if the demanded elts of the source op is a splat or we only demand
2823 // one element, which should always be a splat.
2824 // TODO: Handle source ops splats with undefs.
2825 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2826 APInt SrcUndefs;
2827 return (SrcElts.popcount() == 1) ||
2828 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2829 (SrcElts & SrcUndefs).isZero());
2830 };
2831 if (!DemandedLHS.isZero())
2832 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2833 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2834 }
2836 // Offset the demanded elts by the subvector index.
2837 SDValue Src = V.getOperand(0);
2838 // We don't support scalable vectors at the moment.
2839 if (Src.getValueType().isScalableVector())
2840 return false;
2841 uint64_t Idx = V.getConstantOperandVal(1);
2842 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2843 APInt UndefSrcElts;
2844 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2845 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2846 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2847 return true;
2848 }
2849 break;
2850 }
2854 // Widen the demanded elts by the src element count.
2855 SDValue Src = V.getOperand(0);
2856 // We don't support scalable vectors at the moment.
2857 if (Src.getValueType().isScalableVector())
2858 return false;
2859 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2860 APInt UndefSrcElts;
2861 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2862 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2863 UndefElts = UndefSrcElts.trunc(NumElts);
2864 return true;
2865 }
2866 break;
2867 }
2868 case ISD::BITCAST: {
2869 SDValue Src = V.getOperand(0);
2870 EVT SrcVT = Src.getValueType();
2871 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2872 unsigned BitWidth = VT.getScalarSizeInBits();
2873
2874 // Ignore bitcasts from unsupported types.
2875 // TODO: Add fp support?
2876 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2877 break;
2878
2879 // Bitcast 'small element' vector to 'large element' vector.
2880 if ((BitWidth % SrcBitWidth) == 0) {
2881 // See if each sub element is a splat.
2882 unsigned Scale = BitWidth / SrcBitWidth;
2883 unsigned NumSrcElts = SrcVT.getVectorNumElements();
2884 APInt ScaledDemandedElts =
2885 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
2886 for (unsigned I = 0; I != Scale; ++I) {
2887 APInt SubUndefElts;
2888 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
2889 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
2890 SubDemandedElts &= ScaledDemandedElts;
2891 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
2892 return false;
2893 // TODO: Add support for merging sub undef elements.
2894 if (!SubUndefElts.isZero())
2895 return false;
2896 }
2897 return true;
2898 }
2899 break;
2900 }
2901 }
2902
2903 return false;
2904}
2905
2906/// Helper wrapper to main isSplatValue function.
2907bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
2908 EVT VT = V.getValueType();
2909 assert(VT.isVector() && "Vector type expected");
2910
2911 APInt UndefElts;
2912 // Since the number of lanes in a scalable vector is unknown at compile time,
2913 // we track one bit which is implicitly broadcast to all lanes. This means
2914 // that all lanes in a scalable vector are considered demanded.
2915 APInt DemandedElts
2917 return isSplatValue(V, DemandedElts, UndefElts) &&
2918 (AllowUndefs || !UndefElts);
2919}
2920
2923
2924 EVT VT = V.getValueType();
2925 unsigned Opcode = V.getOpcode();
2926 switch (Opcode) {
2927 default: {
2928 APInt UndefElts;
2929 // Since the number of lanes in a scalable vector is unknown at compile time,
2930 // we track one bit which is implicitly broadcast to all lanes. This means
2931 // that all lanes in a scalable vector are considered demanded.
2932 APInt DemandedElts
2934
2935 if (isSplatValue(V, DemandedElts, UndefElts)) {
2936 if (VT.isScalableVector()) {
2937 // DemandedElts and UndefElts are ignored for scalable vectors, since
2938 // the only supported cases are SPLAT_VECTOR nodes.
2939 SplatIdx = 0;
2940 } else {
2941 // Handle case where all demanded elements are UNDEF.
2942 if (DemandedElts.isSubsetOf(UndefElts)) {
2943 SplatIdx = 0;
2944 return getUNDEF(VT);
2945 }
2946 SplatIdx = (UndefElts & DemandedElts).countr_one();
2947 }
2948 return V;
2949 }
2950 break;
2951 }
2952 case ISD::SPLAT_VECTOR:
2953 SplatIdx = 0;
2954 return V;
2955 case ISD::VECTOR_SHUFFLE: {
2956 assert(!VT.isScalableVector());
2957 // Check if this is a shuffle node doing a splat.
2958 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
2959 // getTargetVShiftNode currently struggles without the splat source.
2960 auto *SVN = cast<ShuffleVectorSDNode>(V);
2961 if (!SVN->isSplat())
2962 break;
2963 int Idx = SVN->getSplatIndex();
2964 int NumElts = V.getValueType().getVectorNumElements();
2965 SplatIdx = Idx % NumElts;
2966 return V.getOperand(Idx / NumElts);
2967 }
2968 }
2969
2970 return SDValue();
2971}
2972
2974 int SplatIdx;
2975 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
2976 EVT SVT = SrcVector.getValueType().getScalarType();
2977 EVT LegalSVT = SVT;
2978 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
2979 if (!SVT.isInteger())
2980 return SDValue();
2981 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
2982 if (LegalSVT.bitsLT(SVT))
2983 return SDValue();
2984 }
2985 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
2986 getVectorIdxConstant(SplatIdx, SDLoc(V)));
2987 }
2988 return SDValue();
2989}
2990
2991const APInt *
2993 const APInt &DemandedElts) const {
2994 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
2995 V.getOpcode() == ISD::SRA) &&
2996 "Unknown shift node");
2997 unsigned BitWidth = V.getScalarValueSizeInBits();
2998 if (ConstantSDNode *SA = isConstOrConstSplat(V.getOperand(1), DemandedElts)) {
2999 // Shifting more than the bitwidth is not valid.
3000 const APInt &ShAmt = SA->getAPIntValue();
3001 if (ShAmt.ult(BitWidth))
3002 return &ShAmt;
3003 }
3004 return nullptr;
3005}
3006
3008 EVT VT = V.getValueType();
3009 APInt DemandedElts = VT.isFixedLengthVector()
3011 : APInt(1, 1);
3012 return getValidShiftAmountConstant(V, DemandedElts);
3013}
3014
3016 SDValue V, const APInt &DemandedElts) const {
3017 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3018 V.getOpcode() == ISD::SRA) &&
3019 "Unknown shift node");
3020 if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
3021 return ValidAmt;
3022 unsigned BitWidth = V.getScalarValueSizeInBits();
3023 auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
3024 if (!BV)
3025 return nullptr;
3026 const APInt *MinShAmt = nullptr;
3027 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3028 if (!DemandedElts[i])
3029 continue;
3030 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3031 if (!SA)
3032 return nullptr;
3033 // Shifting more than the bitwidth is not valid.
3034 const APInt &ShAmt = SA->getAPIntValue();
3035 if (ShAmt.uge(BitWidth))
3036 return nullptr;
3037 if (MinShAmt && MinShAmt->ule(ShAmt))
3038 continue;
3039 MinShAmt = &ShAmt;
3040 }
3041 return MinShAmt;
3042}
3043
3045 EVT VT = V.getValueType();
3046 APInt DemandedElts = VT.isFixedLengthVector()
3048 : APInt(1, 1);
3049 return getValidMinimumShiftAmountConstant(V, DemandedElts);
3050}
3051
3053 SDValue V, const APInt &DemandedElts) const {
3054 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3055 V.getOpcode() == ISD::SRA) &&
3056 "Unknown shift node");
3057 if (const APInt *ValidAmt = getValidShiftAmountConstant(V, DemandedElts))
3058 return ValidAmt;
3059 unsigned BitWidth = V.getScalarValueSizeInBits();
3060 auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
3061 if (!BV)
3062 return nullptr;
3063 const APInt *MaxShAmt = nullptr;
3064 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3065 if (!DemandedElts[i])
3066 continue;
3067 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3068 if (!SA)
3069 return nullptr;
3070 // Shifting more than the bitwidth is not valid.
3071 const APInt &ShAmt = SA->getAPIntValue();
3072 if (ShAmt.uge(BitWidth))
3073 return nullptr;
3074 if (MaxShAmt && MaxShAmt->uge(ShAmt))
3075 continue;
3076 MaxShAmt = &ShAmt;
3077 }
3078 return MaxShAmt;
3079}
3080
3082 EVT VT = V.getValueType();
3083 APInt DemandedElts = VT.isFixedLengthVector()
3085 : APInt(1, 1);
3086 return getValidMaximumShiftAmountConstant(V, DemandedElts);
3087}
3088
3089/// Determine which bits of Op are known to be either zero or one and return
3090/// them in Known. For vectors, the known bits are those that are shared by
3091/// every vector element.
3093 EVT VT = Op.getValueType();
3094
3095 // Since the number of lanes in a scalable vector is unknown at compile time,
3096 // we track one bit which is implicitly broadcast to all lanes. This means
3097 // that all lanes in a scalable vector are considered demanded.
3098 APInt DemandedElts = VT.isFixedLengthVector()
3100 : APInt(1, 1);
3101 return computeKnownBits(Op, DemandedElts, Depth);
3102}
3103
3104/// Determine which bits of Op are known to be either zero or one and return
3105/// them in Known. The DemandedElts argument allows us to only collect the known
3106/// bits that are shared by the requested vector elements.
3108 unsigned Depth) const {
3109 unsigned BitWidth = Op.getScalarValueSizeInBits();
3110
3111 KnownBits Known(BitWidth); // Don't know anything.
3112
3113 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3114 // We know all of the bits for a constant!
3115 return KnownBits::makeConstant(C->getAPIntValue());
3116 }
3117 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3118 // We know all of the bits for a constant fp!
3119 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3120 }
3121
3122 if (Depth >= MaxRecursionDepth)
3123 return Known; // Limit search depth.
3124
3125 KnownBits Known2;
3126 unsigned NumElts = DemandedElts.getBitWidth();
3127 assert((!Op.getValueType().isFixedLengthVector() ||
3128 NumElts == Op.getValueType().getVectorNumElements()) &&
3129 "Unexpected vector size");
3130
3131 if (!DemandedElts)
3132 return Known; // No demanded elts, better to assume we don't know anything.
3133
3134 unsigned Opcode = Op.getOpcode();
3135 switch (Opcode) {
3136 case ISD::MERGE_VALUES:
3137 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3138 Depth + 1);
3139 case ISD::SPLAT_VECTOR: {
3140 SDValue SrcOp = Op.getOperand(0);
3141 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3142 "Expected SPLAT_VECTOR implicit truncation");
3143 // Implicitly truncate the bits to match the official semantics of
3144 // SPLAT_VECTOR.
3145 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3146 break;
3147 }
3149 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3150 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3151 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3152 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3153 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3154 }
3155 break;
3156 }
3157 case ISD::STEP_VECTOR: {
3158 const APInt &Step = Op.getConstantOperandAPInt(0);
3159
3160 if (Step.isPowerOf2())
3161 Known.Zero.setLowBits(Step.logBase2());
3162
3164
3165 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3166 break;
3167 const APInt MinNumElts =
3168 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3169
3170 bool Overflow;
3171 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3173 .umul_ov(MinNumElts, Overflow);
3174 if (Overflow)
3175 break;
3176
3177 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3178 if (Overflow)
3179 break;
3180
3181 Known.Zero.setHighBits(MaxValue.countl_zero());
3182 break;
3183 }
3184 case ISD::BUILD_VECTOR:
3185 assert(!Op.getValueType().isScalableVector());
3186 // Collect the known bits that are shared by every demanded vector element.
3187 Known.Zero.setAllBits(); Known.One.setAllBits();
3188 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3189 if (!DemandedElts[i])
3190 continue;
3191
3192 SDValue SrcOp = Op.getOperand(i);
3193 Known2 = computeKnownBits(SrcOp, Depth + 1);
3194
3195 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3196 if (SrcOp.getValueSizeInBits() != BitWidth) {
3197 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3198 "Expected BUILD_VECTOR implicit truncation");
3199 Known2 = Known2.trunc(BitWidth);
3200 }
3201
3202 // Known bits are the values that are shared by every demanded element.
3203 Known = Known.intersectWith(Known2);
3204
3205 // If we don't know any bits, early out.
3206 if (Known.isUnknown())
3207 break;
3208 }
3209 break;
3210 case ISD::VECTOR_SHUFFLE: {
3211 assert(!Op.getValueType().isScalableVector());
3212 // Collect the known bits that are shared by every vector element referenced
3213 // by the shuffle.
3214 APInt DemandedLHS, DemandedRHS;
3215 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3216 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3217 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3218 DemandedLHS, DemandedRHS))
3219 break;
3220
3221 // Known bits are the values that are shared by every demanded element.
3222 Known.Zero.setAllBits(); Known.One.setAllBits();
3223 if (!!DemandedLHS) {
3224 SDValue LHS = Op.getOperand(0);
3225 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3226 Known = Known.intersectWith(Known2);
3227 }
3228 // If we don't know any bits, early out.
3229 if (Known.isUnknown())
3230 break;
3231 if (!!DemandedRHS) {
3232 SDValue RHS = Op.getOperand(1);
3233 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3234 Known = Known.intersectWith(Known2);
3235 }
3236 break;
3237 }
3238 case ISD::VSCALE: {
3240 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3241 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3242 break;
3243 }
3244 case ISD::CONCAT_VECTORS: {
3245 if (Op.getValueType().isScalableVector())
3246 break;
3247 // Split DemandedElts and test each of the demanded subvectors.
3248 Known.Zero.setAllBits(); Known.One.setAllBits();
3249 EVT SubVectorVT = Op.getOperand(0).getValueType();
3250 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3251 unsigned NumSubVectors = Op.getNumOperands();
3252 for (unsigned i = 0; i != NumSubVectors; ++i) {
3253 APInt DemandedSub =
3254 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3255 if (!!DemandedSub) {
3256 SDValue Sub = Op.getOperand(i);
3257 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3258 Known = Known.intersectWith(Known2);
3259 }
3260 // If we don't know any bits, early out.
3261 if (Known.isUnknown())
3262 break;
3263 }
3264 break;
3265 }
3266 case ISD::INSERT_SUBVECTOR: {
3267 if (Op.getValueType().isScalableVector())
3268 break;
3269 // Demand any elements from the subvector and the remainder from the src its
3270 // inserted into.
3271 SDValue Src = Op.getOperand(0);
3272 SDValue Sub = Op.getOperand(1);
3273 uint64_t Idx = Op.getConstantOperandVal(2);
3274 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3275 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3276 APInt DemandedSrcElts = DemandedElts;
3277 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3278
3279 Known.One.setAllBits();
3280 Known.Zero.setAllBits();
3281 if (!!DemandedSubElts) {
3282 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3283 if (Known.isUnknown())
3284 break; // early-out.
3285 }
3286 if (!!DemandedSrcElts) {
3287 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3288 Known = Known.intersectWith(Known2);
3289 }
3290 break;
3291 }
3293 // Offset the demanded elts by the subvector index.
3294 SDValue Src = Op.getOperand(0);
3295 // Bail until we can represent demanded elements for scalable vectors.
3296 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3297 break;
3298 uint64_t Idx = Op.getConstantOperandVal(1);
3299 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3300 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3301 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3302 break;
3303 }
3304 case ISD::SCALAR_TO_VECTOR: {
3305 if (Op.getValueType().isScalableVector())
3306 break;
3307 // We know about scalar_to_vector as much as we know about it source,
3308 // which becomes the first element of otherwise unknown vector.
3309 if (DemandedElts != 1)
3310 break;
3311
3312 SDValue N0 = Op.getOperand(0);
3313 Known = computeKnownBits(N0, Depth + 1);
3314 if (N0.getValueSizeInBits() != BitWidth)
3315 Known = Known.trunc(BitWidth);
3316
3317 break;
3318 }
3319 case ISD::BITCAST: {
3320 if (Op.getValueType().isScalableVector())
3321 break;
3322
3323 SDValue N0 = Op.getOperand(0);
3324 EVT SubVT = N0.getValueType();
3325 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3326
3327 // Ignore bitcasts from unsupported types.
3328 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3329 break;
3330
3331 // Fast handling of 'identity' bitcasts.
3332 if (BitWidth == SubBitWidth) {
3333 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3334 break;
3335 }
3336
3337 bool IsLE = getDataLayout().isLittleEndian();
3338
3339 // Bitcast 'small element' vector to 'large element' scalar/vector.
3340 if ((BitWidth % SubBitWidth) == 0) {
3341 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3342
3343 // Collect known bits for the (larger) output by collecting the known
3344 // bits from each set of sub elements and shift these into place.
3345 // We need to separately call computeKnownBits for each set of
3346 // sub elements as the knownbits for each is likely to be different.
3347 unsigned SubScale = BitWidth / SubBitWidth;
3348 APInt SubDemandedElts(NumElts * SubScale, 0);
3349 for (unsigned i = 0; i != NumElts; ++i)
3350 if (DemandedElts[i])
3351 SubDemandedElts.setBit(i * SubScale);
3352
3353 for (unsigned i = 0; i != SubScale; ++i) {
3354 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3355 Depth + 1);
3356 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3357 Known.insertBits(Known2, SubBitWidth * Shifts);
3358 }
3359 }
3360
3361 // Bitcast 'large element' scalar/vector to 'small element' vector.
3362 if ((SubBitWidth % BitWidth) == 0) {
3363 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3364
3365 // Collect known bits for the (smaller) output by collecting the known
3366 // bits from the overlapping larger input elements and extracting the
3367 // sub sections we actually care about.
3368 unsigned SubScale = SubBitWidth / BitWidth;
3369 APInt SubDemandedElts =
3370 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3371 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3372
3373 Known.Zero.setAllBits(); Known.One.setAllBits();
3374 for (unsigned i = 0; i != NumElts; ++i)
3375 if (DemandedElts[i]) {
3376 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3377 unsigned Offset = (Shifts % SubScale) * BitWidth;
3378 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3379 // If we don't know any bits, early out.
3380 if (Known.isUnknown())
3381 break;
3382 }
3383 }
3384 break;
3385 }
3386 case ISD::AND:
3387 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3388 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3389
3390 Known &= Known2;
3391 break;
3392 case ISD::OR:
3393 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3394 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3395
3396 Known |= Known2;
3397 break;
3398 case ISD::XOR:
3399 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3400 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3401
3402 Known ^= Known2;
3403 break;
3404 case ISD::MUL: {
3405 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3406 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3407 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3408 // TODO: SelfMultiply can be poison, but not undef.
3409 if (SelfMultiply)
3410 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3411 Op.getOperand(0), DemandedElts, false, Depth + 1);
3412 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3413
3414 // If the multiplication is known not to overflow, the product of a number
3415 // with itself is non-negative. Only do this if we didn't already computed
3416 // the opposite value for the sign bit.
3417 if (Op->getFlags().hasNoSignedWrap() &&
3418 Op.getOperand(0) == Op.getOperand(1) &&
3419 !Known.isNegative())
3420 Known.makeNonNegative();
3421 break;
3422 }
3423 case ISD::MULHU: {
3424 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3425 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3426 Known = KnownBits::mulhu(Known, Known2);
3427 break;
3428 }
3429 case ISD::MULHS: {
3430 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3431 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3432 Known = KnownBits::mulhs(Known, Known2);
3433 break;
3434 }
3435 case ISD::ABDU: {
3436 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3437 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3438 Known = KnownBits::abdu(Known, Known2);
3439 break;
3440 }
3441 case ISD::ABDS: {
3442 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3443 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3444 Known = KnownBits::abds(Known, Known2);
3445 break;
3446 }
3447 case ISD::UMUL_LOHI: {
3448 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3449 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3450 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3451 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3452 if (Op.getResNo() == 0)
3453 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3454 else
3455 Known = KnownBits::mulhu(Known, Known2);
3456 break;
3457 }
3458 case ISD::SMUL_LOHI: {
3459 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3460 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3461 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3462 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3463 if (Op.getResNo() == 0)
3464 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3465 else
3466 Known = KnownBits::mulhs(Known, Known2);
3467 break;
3468 }
3469 case ISD::AVGFLOORU:
3470 case ISD::AVGCEILU:
3471 case ISD::AVGFLOORS:
3472 case ISD::AVGCEILS: {
3473 bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
3474 bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
3475 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3476 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3477 Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
3478 Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
3479 KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
3480 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3481 Known = Known.extractBits(BitWidth, 1);
3482 break;
3483 }
3484 case ISD::SELECT:
3485 case ISD::VSELECT:
3486 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3487 // If we don't know any bits, early out.
3488 if (Known.isUnknown())
3489 break;
3490 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3491
3492 // Only known if known in both the LHS and RHS.
3493 Known = Known.intersectWith(Known2);
3494 break;
3495 case ISD::SELECT_CC:
3496 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3497 // If we don't know any bits, early out.
3498 if (Known.isUnknown())
3499 break;
3500 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3501
3502 // Only known if known in both the LHS and RHS.
3503 Known = Known.intersectWith(Known2);
3504 break;
3505 case ISD::SMULO:
3506 case ISD::UMULO:
3507 if (Op.getResNo() != 1)
3508 break;
3509 // The boolean result conforms to getBooleanContents.
3510 // If we know the result of a setcc has the top bits zero, use this info.
3511 // We know that we have an integer-based boolean since these operations
3512 // are only available for integer.
3513 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3515 BitWidth > 1)
3516 Known.Zero.setBitsFrom(1);
3517 break;
3518 case ISD::SETCC:
3519 case ISD::SETCCCARRY:
3520 case ISD::STRICT_FSETCC:
3521 case ISD::STRICT_FSETCCS: {
3522 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3523 // If we know the result of a setcc has the top bits zero, use this info.
3524 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3526 BitWidth > 1)
3527 Known.Zero.setBitsFrom(1);
3528 break;
3529 }
3530 case ISD::SHL: {
3531 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3532 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3533
3534 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3535 bool NSW = Op->getFlags().hasNoSignedWrap();
3536
3537 bool ShAmtNonZero = Known2.isNonZero();
3538
3539 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3540
3541 // Minimum shift low bits are known zero.
3542 if (const APInt *ShMinAmt =
3544 Known.Zero.setLowBits(ShMinAmt->getZExtValue());
3545 break;
3546 }
3547 case ISD::SRL:
3548 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3549 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3550 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3551 Op->getFlags().hasExact());
3552
3553 // Minimum shift high bits are known zero.
3554 if (const APInt *ShMinAmt =
3556 Known.Zero.setHighBits(ShMinAmt->getZExtValue());
3557 break;
3558 case ISD::SRA:
3559 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3560 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3561 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3562 Op->getFlags().hasExact());
3563 break;
3564 case ISD::FSHL:
3565 case ISD::FSHR:
3566 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3567 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3568
3569 // For fshl, 0-shift returns the 1st arg.
3570 // For fshr, 0-shift returns the 2nd arg.
3571 if (Amt == 0) {
3572 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3573 DemandedElts, Depth + 1);
3574 break;
3575 }
3576
3577 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3578 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3579 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3580 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3581 if (Opcode == ISD::FSHL) {
3582 Known.One <<= Amt;
3583 Known.Zero <<= Amt;
3584 Known2.One.lshrInPlace(BitWidth - Amt);
3585 Known2.Zero.lshrInPlace(BitWidth - Amt);
3586 } else {
3587 Known.One <<= BitWidth - Amt;
3588 Known.Zero <<= BitWidth - Amt;
3589 Known2.One.lshrInPlace(Amt);
3590 Known2.Zero.lshrInPlace(Amt);
3591 }
3592 Known = Known.unionWith(Known2);
3593 }
3594 break;
3595 case ISD::SHL_PARTS:
3596 case ISD::SRA_PARTS:
3597 case ISD::SRL_PARTS: {
3598 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3599
3600 // Collect lo/hi source values and concatenate.
3601 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3602 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3603 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3604 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3605 Known = Known2.concat(Known);
3606
3607 // Collect shift amount.
3608 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3609
3610 if (Opcode == ISD::SHL_PARTS)
3611 Known = KnownBits::shl(Known, Known2);
3612 else if (Opcode == ISD::SRA_PARTS)
3613 Known = KnownBits::ashr(Known, Known2);
3614 else // if (Opcode == ISD::SRL_PARTS)
3615 Known = KnownBits::lshr(Known, Known2);
3616
3617 // TODO: Minimum shift low/high bits are known zero.
3618
3619 if (Op.getResNo() == 0)
3620 Known = Known.extractBits(LoBits, 0);
3621 else
3622 Known = Known.extractBits(HiBits, LoBits);
3623 break;
3624 }
3626 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3627 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3628 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3629 break;
3630 }
3631 case ISD::CTTZ:
3632 case ISD::CTTZ_ZERO_UNDEF: {
3633 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3634 // If we have a known 1, its position is our upper bound.
3635 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3636 unsigned LowBits = llvm::bit_width(PossibleTZ);
3637 Known.Zero.setBitsFrom(LowBits);
3638 break;
3639 }
3640 case ISD::CTLZ:
3641 case ISD::CTLZ_ZERO_UNDEF: {
3642 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3643 // If we have a known 1, its position is our upper bound.
3644 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3645 unsigned LowBits = llvm::bit_width(PossibleLZ);
3646 Known.Zero.setBitsFrom(LowBits);
3647 break;
3648 }
3649 case ISD::CTPOP: {
3650 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3651 // If we know some of the bits are zero, they can't be one.
3652 unsigned PossibleOnes = Known2.countMaxPopulation();
3653 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3654 break;
3655 }
3656 case ISD::PARITY: {
3657 // Parity returns 0 everywhere but the LSB.
3658 Known.Zero.setBitsFrom(1);
3659 break;
3660 }
3661 case ISD::LOAD: {
3662 LoadSDNode *LD = cast<LoadSDNode>(Op);
3663 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3664 if (ISD::isNON_EXTLoad(LD) && Cst) {
3665 // Determine any common known bits from the loaded constant pool value.
3666 Type *CstTy = Cst->getType();
3667 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3668 !Op.getValueType().isScalableVector()) {
3669 // If its a vector splat, then we can (quickly) reuse the scalar path.
3670 // NOTE: We assume all elements match and none are UNDEF.
3671 if (CstTy->isVectorTy()) {
3672 if (const Constant *Splat = Cst->getSplatValue()) {
3673 Cst = Splat;
3674 CstTy = Cst->getType();
3675 }
3676 }
3677 // TODO - do we need to handle different bitwidths?
3678 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3679 // Iterate across all vector elements finding common known bits.
3680 Known.One.setAllBits();
3681 Known.Zero.setAllBits();
3682 for (unsigned i = 0; i != NumElts; ++i) {
3683 if (!DemandedElts[i])
3684 continue;
3685 if (Constant *Elt = Cst->getAggregateElement(i)) {
3686 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3687 const APInt &Value = CInt->getValue();
3688 Known.One &= Value;
3689 Known.Zero &= ~Value;
3690 continue;
3691 }
3692 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3693 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3694 Known.One &= Value;
3695 Known.Zero &= ~Value;
3696 continue;
3697 }
3698 }
3699 Known.One.clearAllBits();
3700 Known.Zero.clearAllBits();
3701 break;
3702 }
3703 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3704 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3705 Known = KnownBits::makeConstant(CInt->getValue());
3706 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3707 Known =
3708 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3709 }
3710 }
3711 }
3712 } else if (Op.getResNo() == 0) {
3713 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3714 ? LD->getMemoryVT().getFixedSizeInBits()
3715 : BitWidth);
3716 EVT VT = Op.getValueType();
3717 // Fill in any known bits from range information. There are 3 types being
3718 // used. The results VT (same vector elt size as BitWidth), the loaded
3719 // MemoryVT (which may or may not be vector) and the range VTs original
3720 // type. The range matadata needs the full range (i.e
3721 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3722 // if it is know. These are then extended to the original VT sizes below.
3723 if (const MDNode *MD = LD->getRanges()) {
3725 if (VT.isVector()) {
3726 // Handle truncation to the first demanded element.
3727 // TODO: Figure out which demanded elements are covered
3728 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
3729 break;
3730 Known0 = Known0.trunc(BitWidth);
3731 }
3732 }
3733
3734 if (LD->getMemoryVT().isVector())
3735 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
3736
3737 // Extend the Known bits from memory to the size of the result.
3738 if (ISD::isZEXTLoad(Op.getNode()))
3739 Known = Known0.zext(BitWidth);
3740 else if (ISD::isSEXTLoad(Op.getNode()))
3741 Known = Known0.sext(BitWidth);
3742 else if (ISD::isEXTLoad(Op.getNode()))
3743 Known = Known0.anyext(BitWidth);
3744 else
3745 Known = Known0;
3746 assert(Known.getBitWidth() == BitWidth);
3747 return Known;
3748 }
3749 break;
3750 }
3752 if (Op.getValueType().isScalableVector())
3753 break;
3754 EVT InVT = Op.getOperand(0).getValueType();
3755 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3756 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3757 Known = Known.zext(BitWidth);
3758 break;
3759 }
3760 case ISD::ZERO_EXTEND: {
3761 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3762 Known = Known.zext(BitWidth);
3763 break;
3764 }
3766 if (Op.getValueType().isScalableVector())
3767 break;
3768 EVT InVT = Op.getOperand(0).getValueType();
3769 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3770 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3771 // If the sign bit is known to be zero or one, then sext will extend
3772 // it to the top bits, else it will just zext.
3773 Known = Known.sext(BitWidth);
3774 break;
3775 }
3776 case ISD::SIGN_EXTEND: {
3777 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3778 // If the sign bit is known to be zero or one, then sext will extend
3779 // it to the top bits, else it will just zext.
3780 Known = Known.sext(BitWidth);
3781 break;
3782 }
3784 if (Op.getValueType().isScalableVector())
3785 break;
3786 EVT InVT = Op.getOperand(0).getValueType();
3787 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3788 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3789 Known = Known.anyext(BitWidth);
3790 break;
3791 }
3792 case ISD::ANY_EXTEND: {
3793 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3794 Known = Known.anyext(BitWidth);
3795 break;
3796 }
3797 case ISD::TRUNCATE: {
3798 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3799 Known = Known.trunc(BitWidth);
3800 break;
3801 }
3802 case ISD::AssertZext: {
3803 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3805 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3806 Known.Zero |= (~InMask);
3807 Known.One &= (~Known.Zero);
3808 break;
3809 }
3810 case ISD::AssertAlign: {
3811 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3812 assert(LogOfAlign != 0);
3813
3814 // TODO: Should use maximum with source
3815 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3816 // well as clearing one bits.
3817 Known.Zero.setLowBits(LogOfAlign);
3818 Known.One.clearLowBits(LogOfAlign);
3819 break;
3820 }
3821 case ISD::FGETSIGN:
3822 // All bits are zero except the low bit.
3823 Known.Zero.setBitsFrom(1);
3824 break;
3825 case ISD::ADD:
3826 case ISD::SUB: {
3827 SDNodeFlags Flags = Op.getNode()->getFlags();
3828 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3829 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3831 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3832 Flags.hasNoUnsignedWrap(), Known, Known2);
3833 break;
3834 }
3835 case ISD::USUBO:
3836 case ISD::SSUBO:
3837 case ISD::USUBO_CARRY:
3838 case ISD::SSUBO_CARRY:
3839 if (Op.getResNo() == 1) {
3840 // If we know the result of a setcc has the top bits zero, use this info.
3841 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3843 BitWidth > 1)
3844 Known.Zero.setBitsFrom(1);
3845 break;
3846 }
3847 [[fallthrough]];
3848 case ISD::SUBC: {
3849 assert(Op.getResNo() == 0 &&
3850 "We only compute knownbits for the difference here.");
3851
3852 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
3853 KnownBits Borrow(1);
3854 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3855 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3856 // Borrow has bit width 1
3857 Borrow = Borrow.trunc(1);
3858 } else {
3859 Borrow.setAllZero();
3860 }
3861
3862 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3863 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3864 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
3865 break;
3866 }
3867 case ISD::UADDO:
3868 case ISD::SADDO:
3869 case ISD::UADDO_CARRY:
3870 case ISD::SADDO_CARRY:
3871 if (Op.getResNo() == 1) {
3872 // If we know the result of a setcc has the top bits zero, use this info.
3873 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3875 BitWidth > 1)
3876 Known.Zero.setBitsFrom(1);
3877 break;
3878 }
3879 [[fallthrough]];
3880 case ISD::ADDC:
3881 case ISD::ADDE: {
3882 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3883
3884 // With ADDE and UADDO_CARRY, a carry bit may be added in.
3885 KnownBits Carry(1);
3886 if (Opcode == ISD::ADDE)
3887 // Can't track carry from glue, set carry to unknown.
3888 Carry.resetAll();
3889 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3890 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3891 // Carry has bit width 1
3892 Carry = Carry.trunc(1);
3893 } else {
3894 Carry.setAllZero();
3895 }
3896
3897 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3898 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3899 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3900 break;
3901 }
3902 case ISD::UDIV: {
3903 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3904 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3905 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
3906 break;
3907 }
3908 case ISD::SDIV: {
3909 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3910 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3911 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
3912 break;
3913 }
3914 case ISD::SREM: {
3915 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3916 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3917 Known = KnownBits::srem(Known, Known2);
3918 break;
3919 }
3920 case ISD::UREM: {
3921 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3922 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3923 Known = KnownBits::urem(Known, Known2);
3924 break;
3925 }
3926 case ISD::EXTRACT_ELEMENT: {
3927 Known = computeKnownBits(Op.getOperand(0), Depth+1);
3928 const unsigned Index = Op.getConstantOperandVal(1);
3929 const unsigned EltBitWidth = Op.getValueSizeInBits();
3930
3931 // Remove low part of known bits mask
3932 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3933 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3934
3935 // Remove high part of known bit mask
3936 Known = Known.trunc(EltBitWidth);
3937 break;
3938 }
3940 SDValue InVec = Op.getOperand(0);
3941 SDValue EltNo = Op.getOperand(1);
3942 EVT VecVT = InVec.getValueType();
3943 // computeKnownBits not yet implemented for scalable vectors.
3944 if (VecVT.isScalableVector())
3945 break;
3946 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
3947 const unsigned NumSrcElts = VecVT.getVectorNumElements();
3948
3949 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
3950 // anything about the extended bits.
3951 if (BitWidth > EltBitWidth)
3952 Known = Known.trunc(EltBitWidth);
3953
3954 // If we know the element index, just demand that vector element, else for
3955 // an unknown element index, ignore DemandedElts and demand them all.
3956 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
3957 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
3958 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
3959 DemandedSrcElts =
3960 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
3961
3962 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
3963 if (BitWidth > EltBitWidth)
3964 Known = Known.anyext(BitWidth);
3965 break;
3966 }
3968 if (Op.getValueType().isScalableVector())
3969 break;
3970
3971 // If we know the element index, split the demand between the
3972 // source vector and the inserted element, otherwise assume we need
3973 // the original demanded vector elements and the value.
3974 SDValue InVec = Op.getOperand(0);
3975 SDValue InVal = Op.getOperand(1);
3976 SDValue EltNo = Op.getOperand(2);
3977 bool DemandedVal = true;
3978 APInt DemandedVecElts = DemandedElts;
3979 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
3980 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
3981 unsigned EltIdx = CEltNo->getZExtValue();
3982 DemandedVal = !!DemandedElts[EltIdx];
3983 DemandedVecElts.clearBit(EltIdx);
3984 }
3985 Known.One.setAllBits();
3986 Known.Zero.setAllBits();
3987 if (DemandedVal) {
3988 Known2 = computeKnownBits(InVal, Depth + 1);
3989 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
3990 }
3991 if (!!DemandedVecElts) {
3992 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
3993 Known = Known.intersectWith(Known2);
3994 }
3995 break;
3996 }
3997 case ISD::BITREVERSE: {
3998 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3999 Known = Known2.reverseBits();
4000 break;
4001 }
4002 case ISD::BSWAP: {
4003 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4004 Known = Known2.byteSwap();
4005 break;
4006 }
4007 case ISD::ABS: {
4008 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4009 Known = Known2.abs();
4010 break;
4011 }
4012 case ISD::USUBSAT: {
4013 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4014 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4015 Known = KnownBits::usub_sat(Known, Known2);
4016 break;
4017 }
4018 case ISD::UMIN: {
4019 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4020 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4021 Known = KnownBits::umin(Known, Known2);
4022 break;
4023 }
4024 case ISD::UMAX: {
4025 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4026 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4027 Known = KnownBits::umax(Known, Known2);
4028 break;
4029 }
4030 case ISD::SMIN:
4031 case ISD::SMAX: {
4032 // If we have a clamp pattern, we know that the number of sign bits will be
4033 // the minimum of the clamp min/max range.
4034 bool IsMax = (Opcode == ISD::SMAX);
4035 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4036 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4037 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4038 CstHigh =
4039 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4040 if (CstLow && CstHigh) {
4041 if (!IsMax)
4042 std::swap(CstLow, CstHigh);
4043
4044 const APInt &ValueLow = CstLow->getAPIntValue();
4045 const APInt &ValueHigh = CstHigh->getAPIntValue();
4046 if (ValueLow.sle(ValueHigh)) {
4047 unsigned LowSignBits = ValueLow.getNumSignBits();
4048 unsigned HighSignBits = ValueHigh.getNumSignBits();
4049 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4050 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4051 Known.One.setHighBits(MinSignBits);
4052 break;
4053 }
4054 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4055 Known.Zero.setHighBits(MinSignBits);
4056 break;
4057 }
4058 }
4059 }
4060
4061 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4062 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4063 if (IsMax)
4064 Known = KnownBits::smax(Known, Known2);
4065 else
4066 Known = KnownBits::smin(Known, Known2);
4067
4068 // For SMAX, if CstLow is non-negative we know the result will be
4069 // non-negative and thus all sign bits are 0.
4070 // TODO: There's an equivalent of this for smin with negative constant for
4071 // known ones.
4072 if (IsMax && CstLow) {
4073 const APInt &ValueLow = CstLow->getAPIntValue();
4074 if (ValueLow.isNonNegative()) {
4075 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4076 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4077 }
4078 }
4079
4080 break;
4081 }
4082 case ISD::UINT_TO_FP: {
4083 Known.makeNonNegative();
4084 break;
4085 }
4086 case ISD::SINT_TO_FP: {
4087 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4088 if (Known2.isNonNegative())
4089 Known.makeNonNegative();
4090 else if (Known2.isNegative())
4091 Known.makeNegative();
4092 break;
4093 }
4094 case ISD::FP_TO_UINT_SAT: {
4095 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4096 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4098 break;
4099 }
4101 if (Op.getResNo() == 1) {
4102 // The boolean result conforms to getBooleanContents.
4103 // If we know the result of a setcc has the top bits zero, use this info.
4104 // We know that we have an integer-based boolean since these operations
4105 // are only available for integer.
4106 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4108 BitWidth > 1)
4109 Known.Zero.setBitsFrom(1);
4110 break;
4111 }
4112 [[fallthrough]];
4114 case ISD::ATOMIC_SWAP:
4126 case ISD::ATOMIC_LOAD: {
4127 unsigned MemBits =
4128 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4129 // If we are looking at the loaded value.
4130 if (Op.getResNo() == 0) {
4132 Known.Zero.setBitsFrom(MemBits);
4133 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4134 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4135 Known.Zero.setBitsFrom(MemBits);
4136 }
4137 break;
4138 }
4139 case ISD::FrameIndex:
4141 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4142 Known, getMachineFunction());
4143 break;
4144
4145 default:
4146 if (Opcode < ISD::BUILTIN_OP_END)
4147 break;
4148 [[fallthrough]];
4152 // TODO: Probably okay to remove after audit; here to reduce change size
4153 // in initial enablement patch for scalable vectors
4154 if (Op.getValueType().isScalableVector())
4155 break;
4156
4157 // Allow the target to implement this method for its nodes.
4158 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4159 break;
4160 }
4161
4162 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
4163 return Known;
4164}
4165
4166/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4168 switch (OR) {
4176 }
4177 llvm_unreachable("Unknown OverflowResult");
4178}
4179
4182 // X + 0 never overflow
4183 if (isNullConstant(N1))
4184 return OFK_Never;
4185
4186 // If both operands each have at least two sign bits, the addition
4187 // cannot overflow.
4188 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4189 return OFK_Never;
4190
4191 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4192 return OFK_Sometime;
4193}
4194
4197 // X + 0 never overflow
4198 if (isNullConstant(N1))
4199 return OFK_Never;
4200
4201 // mulhi + 1 never overflow
4202 KnownBits N1Known = computeKnownBits(N1);
4203 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4204 N1Known.getMaxValue().ult(2))
4205 return OFK_Never;
4206
4207 KnownBits N0Known = computeKnownBits(N0);
4208 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4209 N0Known.getMaxValue().ult(2))
4210 return OFK_Never;
4211
4212 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4213 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4214 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4215 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4216}
4217
4220 // X - 0 never overflow
4221 if (isNullConstant(N1))
4222 return OFK_Never;
4223
4224 // If both operands each have at least two sign bits, the subtraction
4225 // cannot overflow.
4226 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4227 return OFK_Never;
4228
4229 KnownBits N0Known = computeKnownBits(N0);
4230 KnownBits N1Known = computeKnownBits(N1);
4231 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4232 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4233 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4234}
4235
4238 // X - 0 never overflow
4239 if (isNullConstant(N1))
4240 return OFK_Never;
4241
4242 KnownBits N0Known = computeKnownBits(N0);
4243 KnownBits N1Known = computeKnownBits(N1);
4244 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4245 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4246 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4247}
4248
4251 // X * 0 and X * 1 never overflow.
4252 if (isNullConstant(N1) || isOneConstant(N1))
4253 return OFK_Never;
4254
4255 KnownBits N0Known = computeKnownBits(N0);
4256 KnownBits N1Known = computeKnownBits(N1);
4257 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4258 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4259 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4260}
4261
4264 // X * 0 and X * 1 never overflow.
4265 if (isNullConstant(N1) || isOneConstant(N1))
4266 return OFK_Never;
4267
4268 // Get the size of the result.
4269 unsigned BitWidth = N0.getScalarValueSizeInBits();
4270
4271 // Sum of the sign bits.
4272 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4273
4274 // If we have enough sign bits, then there's no overflow.
4275 if (SignBits > BitWidth + 1)
4276 return OFK_Never;
4277
4278 if (SignBits == BitWidth + 1) {
4279 // The overflow occurs when the true multiplication of the
4280 // the operands is the minimum negative number.
4281 KnownBits N0Known = computeKnownBits(N0);
4282 KnownBits N1Known = computeKnownBits(N1);
4283 // If one of the operands is non-negative, then there's no
4284 // overflow.
4285 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4286 return OFK_Never;
4287 }
4288
4289 return OFK_Sometime;
4290}
4291
4293 if (Depth >= MaxRecursionDepth)
4294 return false; // Limit search depth.
4295
4296 EVT OpVT = Val.getValueType();
4297 unsigned BitWidth = OpVT.getScalarSizeInBits();
4298
4299 // Is the constant a known power of 2?
4301 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4302 }))
4303 return true;
4304
4305 // A left-shift of a constant one will have exactly one bit set because
4306 // shifting the bit off the end is undefined.
4307 if (Val.getOpcode() == ISD::SHL) {
4308 auto *C = isConstOrConstSplat(Val.getOperand(0));
4309 if (C && C->getAPIntValue() == 1)
4310 return true;
4311 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4312 isKnownNeverZero(Val, Depth);
4313 }
4314
4315 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4316 // one bit set.
4317 if (Val.getOpcode() == ISD::SRL) {
4318 auto *C = isConstOrConstSplat(Val.getOperand(0));
4319 if (C && C->getAPIntValue().isSignMask())
4320 return true;
4321 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4322 isKnownNeverZero(Val, Depth);
4323 }
4324
4325 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4326 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4327
4328 // Are all operands of a build vector constant powers of two?
4329 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4330 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4331 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4332 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4333 return false;
4334 }))
4335 return true;
4336
4337 // Is the operand of a splat vector a constant power of two?
4338 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4339 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4340 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4341 return true;
4342
4343 // vscale(power-of-two) is a power-of-two for some targets
4344 if (Val.getOpcode() == ISD::VSCALE &&
4345 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4347 return true;
4348
4349 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4350 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4351 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4353
4354 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4355 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4357
4358 // Looking for `x & -x` pattern:
4359 // If x == 0:
4360 // x & -x -> 0
4361 // If x != 0:
4362 // x & -x -> non-zero pow2
4363 // so if we find the pattern return whether we know `x` is non-zero.
4364 SDValue X;
4365 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4366 return isKnownNeverZero(X, Depth);
4367
4368 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4369 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4370
4371 // More could be done here, though the above checks are enough
4372 // to handle some common cases.
4373 return false;
4374}
4375
4377 EVT VT = Op.getValueType();
4378
4379 // Since the number of lanes in a scalable vector is unknown at compile time,
4380 // we track one bit which is implicitly broadcast to all lanes. This means
4381 // that all lanes in a scalable vector are considered demanded.
4382 APInt DemandedElts = VT.isFixedLengthVector()
4384 : APInt(1, 1);
4385 return ComputeNumSignBits(Op, DemandedElts, Depth);
4386}
4387
4388unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4389 unsigned Depth) const {
4390 EVT VT = Op.getValueType();
4391 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4392 unsigned VTBits = VT.getScalarSizeInBits();
4393 unsigned NumElts = DemandedElts.getBitWidth();
4394 unsigned Tmp, Tmp2;
4395 unsigned FirstAnswer = 1;
4396
4397 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4398 const APInt &Val = C->getAPIntValue();
4399 return Val.getNumSignBits();
4400 }
4401
4402 if (Depth >= MaxRecursionDepth)
4403 return 1; // Limit search depth.
4404
4405 if (!DemandedElts)
4406 return 1; // No demanded elts, better to assume we don't know anything.
4407
4408 unsigned Opcode = Op.getOpcode();
4409 switch (Opcode) {
4410 default: break;
4411 case ISD::AssertSext:
4412 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4413 return VTBits-Tmp+1;
4414 case ISD::AssertZext:
4415 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4416 return VTBits-Tmp;
4417 case ISD::MERGE_VALUES:
4418 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4419 Depth + 1);
4420 case ISD::SPLAT_VECTOR: {
4421 // Check if the sign bits of source go down as far as the truncated value.
4422 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4423 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4424 if (NumSrcSignBits > (NumSrcBits - VTBits))
4425 return NumSrcSignBits - (NumSrcBits - VTBits);
4426 break;
4427 }
4428 case ISD::BUILD_VECTOR:
4429 assert(!VT.isScalableVector());
4430 Tmp = VTBits;
4431 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4432 if (!DemandedElts[i])
4433 continue;
4434
4435 SDValue SrcOp = Op.getOperand(i);
4436 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4437 // for constant nodes to ensure we only look at the sign bits.
4438 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4439 APInt T = C->getAPIntValue().trunc(VTBits);
4440 Tmp2 = T.getNumSignBits();
4441 } else {
4442 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4443
4444 if (SrcOp.getValueSizeInBits() != VTBits) {
4445 assert(SrcOp.getValueSizeInBits() > VTBits &&
4446 "Expected BUILD_VECTOR implicit truncation");
4447 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4448 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4449 }
4450 }
4451 Tmp = std::min(Tmp, Tmp2);
4452 }
4453 return Tmp;
4454
4455 case ISD::VECTOR_SHUFFLE: {
4456 // Collect the minimum number of sign bits that are shared by every vector
4457 // element referenced by the shuffle.
4458 APInt DemandedLHS, DemandedRHS;
4459 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4460 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4461 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4462 DemandedLHS, DemandedRHS))
4463 return 1;
4464
4465 Tmp = std::numeric_limits<unsigned>::max();
4466 if (!!DemandedLHS)
4467 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4468 if (!!DemandedRHS) {
4469 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4470 Tmp = std::min(Tmp, Tmp2);
4471 }
4472 // If we don't know anything, early out and try computeKnownBits fall-back.
4473 if (Tmp == 1)
4474 break;
4475 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4476 return Tmp;
4477 }
4478
4479 case ISD::BITCAST: {
4480 if (VT.isScalableVector())
4481 break;
4482 SDValue N0 = Op.getOperand(0);
4483 EVT SrcVT = N0.getValueType();
4484 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4485
4486 // Ignore bitcasts from unsupported types..
4487 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4488 break;
4489
4490 // Fast handling of 'identity' bitcasts.
4491 if (VTBits == SrcBits)
4492 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4493
4494 bool IsLE = getDataLayout().isLittleEndian();
4495
4496 // Bitcast 'large element' scalar/vector to 'small element' vector.
4497 if ((SrcBits % VTBits) == 0) {
4498 assert(VT.isVector() && "Expected bitcast to vector");
4499
4500 unsigned Scale = SrcBits / VTBits;
4501 APInt SrcDemandedElts =
4502 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4503
4504 // Fast case - sign splat can be simply split across the small elements.
4505 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4506 if (Tmp == SrcBits)
4507 return VTBits;
4508
4509 // Slow case - determine how far the sign extends into each sub-element.
4510 Tmp2 = VTBits;
4511 for (unsigned i = 0; i != NumElts; ++i)
4512 if (DemandedElts[i]) {
4513 unsigned SubOffset = i % Scale;
4514 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4515 SubOffset = SubOffset * VTBits;
4516 if (Tmp <= SubOffset)
4517 return 1;
4518 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4519 }
4520 return Tmp2;
4521 }
4522 break;
4523 }
4524
4526 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4527 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4528 return VTBits - Tmp + 1;
4529 case ISD::SIGN_EXTEND:
4530 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4531 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4533 // Max of the input and what this extends.
4534 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4535 Tmp = VTBits-Tmp+1;
4536 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4537 return std::max(Tmp, Tmp2);
4539 if (VT.isScalableVector())
4540 break;
4541 SDValue Src = Op.getOperand(0);
4542 EVT SrcVT = Src.getValueType();
4543 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4544 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4545 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4546 }
4547 case ISD::SRA:
4548 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4549 // SRA X, C -> adds C sign bits.
4550 if (const APInt *ShAmt =
4552 Tmp = std::min<uint64_t>(Tmp + ShAmt->getZExtValue(), VTBits);
4553 return Tmp;
4554 case ISD::SHL:
4555 if (const APInt *ShAmt =
4556 getValidMaximumShiftAmountConstant(Op, DemandedElts)) {
4557 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4558 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4559 if (ShAmt->ult(Tmp))
4560 return Tmp - ShAmt->getZExtValue();
4561 }
4562 break;
4563 case ISD::AND:
4564 case ISD::OR:
4565 case ISD::XOR: // NOT is handled here.
4566 // Logical binary ops preserve the number of sign bits at the worst.
4567 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4568 if (Tmp != 1) {
4569 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4570 FirstAnswer = std::min(Tmp, Tmp2);
4571 // We computed what we know about the sign bits as our first
4572 // answer. Now proceed to the generic code that uses
4573 // computeKnownBits, and pick whichever answer is better.
4574 }
4575 break;
4576
4577 case ISD::SELECT:
4578 case ISD::VSELECT:
4579 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4580 if (Tmp == 1) return 1; // Early out.
4581 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4582 return std::min(Tmp, Tmp2);
4583 case ISD::SELECT_CC:
4584 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4585 if (Tmp == 1) return 1; // Early out.
4586 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4587 return std::min(Tmp, Tmp2);
4588
4589 case ISD::SMIN:
4590 case ISD::SMAX: {
4591 // If we have a clamp pattern, we know that the number of sign bits will be
4592 // the minimum of the clamp min/max range.
4593 bool IsMax = (Opcode == ISD::SMAX);
4594 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4595 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4596 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4597 CstHigh =
4598 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4599 if (CstLow && CstHigh) {
4600 if (!IsMax)
4601 std::swap(CstLow, CstHigh);
4602 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4603 Tmp = CstLow->getAPIntValue().getNumSignBits();
4604 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4605 return std::min(Tmp, Tmp2);
4606 }
4607 }
4608
4609 // Fallback - just get the minimum number of sign bits of the operands.
4610 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4611 if (Tmp == 1)
4612 return 1; // Early out.
4613 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4614 return std::min(Tmp, Tmp2);
4615 }
4616 case ISD::UMIN:
4617 case ISD::UMAX:
4618 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4619 if (Tmp == 1)
4620 return 1; // Early out.
4621 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4622 return std::min(Tmp, Tmp2);
4623 case ISD::SADDO:
4624 case ISD::UADDO:
4625 case ISD::SADDO_CARRY:
4626 case ISD::UADDO_CARRY:
4627 case ISD::SSUBO:
4628 case ISD::USUBO:
4629 case ISD::SSUBO_CARRY:
4630 case ISD::USUBO_CARRY:
4631 case ISD::SMULO:
4632 case ISD::UMULO:
4633 if (Op.getResNo() != 1)
4634 break;
4635 // The boolean result conforms to getBooleanContents. Fall through.
4636 // If setcc returns 0/-1, all bits are sign bits.
4637 // We know that we have an integer-based boolean since these operations
4638 // are only available for integer.
4639 if (TLI->getBooleanContents(VT.isVector(), false) ==
4641 return VTBits;
4642 break;
4643 case ISD::SETCC:
4644 case ISD::SETCCCARRY:
4645 case ISD::STRICT_FSETCC:
4646 case ISD::STRICT_FSETCCS: {
4647 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4648 // If setcc returns 0/-1, all bits are sign bits.
4649 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4651 return VTBits;
4652 break;
4653 }
4654 case ISD::ROTL:
4655 case ISD::ROTR:
4656 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4657
4658 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4659 if (Tmp == VTBits)
4660 return VTBits;
4661
4662 if (ConstantSDNode *C =
4663 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4664 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4665
4666 // Handle rotate right by N like a rotate left by 32-N.
4667 if (Opcode == ISD::ROTR)
4668 RotAmt = (VTBits - RotAmt) % VTBits;
4669
4670 // If we aren't rotating out all of the known-in sign bits, return the
4671 // number that are left. This handles rotl(sext(x), 1) for example.
4672 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4673 }
4674 break;
4675 case ISD::ADD:
4676 case ISD::ADDC:
4677 // Add can have at most one carry bit. Thus we know that the output
4678 // is, at worst, one more bit than the inputs.
4679 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4680 if (Tmp == 1) return 1; // Early out.
4681
4682 // Special case decrementing a value (ADD X, -1):
4683 if (ConstantSDNode *CRHS =
4684 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4685 if (CRHS->isAllOnes()) {
4686 KnownBits Known =
4687 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4688
4689 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4690 // sign bits set.
4691 if ((Known.Zero | 1).isAllOnes())
4692 return VTBits;
4693
4694 // If we are subtracting one from a positive number, there is no carry
4695 // out of the result.
4696 if (Known.isNonNegative())
4697 return Tmp;
4698 }
4699
4700 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4701 if (Tmp2 == 1) return 1; // Early out.
4702 return std::min(Tmp, Tmp2) - 1;
4703 case ISD::SUB:
4704 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4705 if (Tmp2 == 1) return 1; // Early out.
4706
4707 // Handle NEG.
4708 if (ConstantSDNode *CLHS =
4709 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4710 if (CLHS->isZero()) {
4711 KnownBits Known =
4712 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4713 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4714 // sign bits set.
4715 if ((Known.Zero | 1).isAllOnes())
4716 return VTBits;
4717
4718 // If the input is known to be positive (the sign bit is known clear),
4719 // the output of the NEG has the same number of sign bits as the input.
4720 if (Known.isNonNegative())
4721 return Tmp2;
4722
4723 // Otherwise, we treat this like a SUB.
4724 }
4725
4726 // Sub can have at most one carry bit. Thus we know that the output
4727 // is, at worst, one more bit than the inputs.
4728 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4729 if (Tmp == 1) return 1; // Early out.
4730 return std::min(Tmp, Tmp2) - 1;
4731 case ISD::MUL: {
4732 // The output of the Mul can be at most twice the valid bits in the inputs.
4733 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4734 if (SignBitsOp0 == 1)
4735 break;
4736 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
4737 if (SignBitsOp1 == 1)
4738 break;
4739 unsigned OutValidBits =
4740 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
4741 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
4742 }
4743 case ISD::SREM:
4744 // The sign bit is the LHS's sign bit, except when the result of the
4745 // remainder is zero. The magnitude of the result should be less than or
4746 // equal to the magnitude of the LHS. Therefore, the result should have
4747 // at least as many sign bits as the left hand side.
4748 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4749 case ISD::TRUNCATE: {
4750 // Check if the sign bits of source go down as far as the truncated value.
4751 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
4752 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4753 if (NumSrcSignBits > (NumSrcBits - VTBits))
4754 return NumSrcSignBits - (NumSrcBits - VTBits);
4755 break;
4756 }
4757 case ISD::EXTRACT_ELEMENT: {
4758 if (VT.isScalableVector())
4759 break;
4760 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
4761 const int BitWidth = Op.getValueSizeInBits();
4762 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
4763
4764 // Get reverse index (starting from 1), Op1 value indexes elements from
4765 // little end. Sign starts at big end.
4766 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
4767
4768 // If the sign portion ends in our element the subtraction gives correct
4769 // result. Otherwise it gives either negative or > bitwidth result
4770 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
4771 }
4773 if (VT.isScalableVector())
4774 break;
4775 // If we know the element index, split the demand between the
4776 // source vector and the inserted element, otherwise assume we need
4777 // the original demanded vector elements and the value.
4778 SDValue InVec = Op.getOperand(0);
4779 SDValue InVal = Op.getOperand(1);
4780 SDValue EltNo = Op.getOperand(2);
4781 bool DemandedVal = true;
4782 APInt DemandedVecElts = DemandedElts;
4783 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4784 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4785 unsigned EltIdx = CEltNo->getZExtValue();
4786 DemandedVal = !!DemandedElts[EltIdx];
4787 DemandedVecElts.clearBit(EltIdx);
4788 }
4789 Tmp = std::numeric_limits<unsigned>::max();
4790 if (DemandedVal) {
4791 // TODO - handle implicit truncation of inserted elements.
4792 if (InVal.getScalarValueSizeInBits() != VTBits)
4793 break;
4794 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
4795 Tmp = std::min(Tmp, Tmp2);
4796 }
4797 if (!!DemandedVecElts) {
4798 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
4799 Tmp = std::min(Tmp, Tmp2);
4800 }
4801 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4802 return Tmp;
4803 }
4805 assert(!VT.isScalableVector());
4806 SDValue InVec = Op.getOperand(0);
4807 SDValue EltNo = Op.getOperand(1);
4808 EVT VecVT = InVec.getValueType();
4809 // ComputeNumSignBits not yet implemented for scalable vectors.
4810 if (VecVT.isScalableVector())
4811 break;
4812 const unsigned BitWidth = Op.getValueSizeInBits();
4813 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
4814 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4815
4816 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
4817 // anything about sign bits. But if the sizes match we can derive knowledge
4818 // about sign bits from the vector operand.
4819 if (BitWidth != EltBitWidth)
4820 break;
4821
4822 // If we know the element index, just demand that vector element, else for
4823 // an unknown element index, ignore DemandedElts and demand them all.
4824 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4825 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4826 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4827 DemandedSrcElts =
4828 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4829
4830 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
4831 }
4833 // Offset the demanded elts by the subvector index.
4834 SDValue Src = Op.getOperand(0);
4835 // Bail until we can represent demanded elements for scalable vectors.
4836 if (Src.getValueType().isScalableVector())
4837 break;
4838 uint64_t Idx = Op.getConstantOperandVal(1);
4839 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
4840 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
4841 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4842 }
4843 case ISD::CONCAT_VECTORS: {
4844 if (VT.isScalableVector())
4845 break;
4846 // Determine the minimum number of sign bits across all demanded
4847 // elts of the input vectors. Early out if the result is already 1.
4848 Tmp = std::numeric_limits<unsigned>::max();
4849 EVT SubVectorVT = Op.getOperand(0).getValueType();
4850 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
4851 unsigned NumSubVectors = Op.getNumOperands();
4852 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
4853 APInt DemandedSub =
4854 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
4855 if (!DemandedSub)
4856 continue;
4857 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
4858 Tmp = std::min(Tmp, Tmp2);
4859 }
4860 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4861 return Tmp;
4862 }
4863 case ISD::INSERT_SUBVECTOR: {
4864 if (VT.isScalableVector())
4865 break;
4866 // Demand any elements from the subvector and the remainder from the src its
4867 // inserted into.
4868 SDValue Src = Op.getOperand(0);
4869 SDValue Sub = Op.getOperand(1);
4870 uint64_t Idx = Op.getConstantOperandVal(2);
4871 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
4872 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
4873 APInt DemandedSrcElts = DemandedElts;
4874 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
4875
4876 Tmp = std::numeric_limits<unsigned>::max();
4877 if (!!DemandedSubElts) {
4878 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
4879 if (Tmp == 1)
4880 return 1; // early-out
4881 }
4882 if (!!DemandedSrcElts) {
4883 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4884 Tmp = std::min(Tmp, Tmp2);
4885 }
4886 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4887 return Tmp;
4888 }
4889 case ISD::LOAD: {
4890 LoadSDNode *LD = cast<LoadSDNode>(Op);
4891 if (const MDNode *Ranges = LD->getRanges()) {
4892 if (DemandedElts != 1)
4893 break;
4894
4896 if (VTBits > CR.getBitWidth()) {
4897 switch (LD->getExtensionType()) {
4898 case ISD::SEXTLOAD:
4899 CR = CR.signExtend(VTBits);
4900 break;
4901 case ISD::ZEXTLOAD:
4902 CR = CR.zeroExtend(VTBits);
4903 break;
4904 default:
4905 break;
4906 }
4907 }
4908
4909 if (VTBits != CR.getBitWidth())
4910 break;
4911 return std::min(CR.getSignedMin().getNumSignBits(),
4913 }
4914
4915 break;
4916 }
4919 case ISD::ATOMIC_SWAP:
4931 case ISD::ATOMIC_LOAD: {
4932 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4933 // If we are looking at the loaded value.
4934 if (Op.getResNo() == 0) {
4935 if (Tmp == VTBits)
4936 return 1; // early-out
4938 return VTBits - Tmp + 1;
4940 return VTBits - Tmp;
4941 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
4942 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
4943 if (ETy == ISD::SEXTLOAD)
4944 return VTBits - Tmp + 1;
4945 if (ETy == ISD::ZEXTLOAD)
4946 return VTBits - Tmp;
4947 }
4948 }
4949 break;
4950 }
4951 }
4952
4953 // If we are looking at the loaded value of the SDNode.
4954 if (Op.getResNo() == 0) {
4955 // Handle LOADX separately here. EXTLOAD case will fallthrough.
4956 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
4957 unsigned ExtType = LD->getExtensionType();
4958 switch (ExtType) {
4959 default: break;
4960 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
4961 Tmp = LD->getMemoryVT().getScalarSizeInBits();
4962 return VTBits - Tmp + 1;
4963 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
4964 Tmp = LD->getMemoryVT().getScalarSizeInBits();
4965 return VTBits - Tmp;
4966 case ISD::NON_EXTLOAD:
4967 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
4968 // We only need to handle vectors - computeKnownBits should handle
4969 // scalar cases.
4970 Type *CstTy = Cst->getType();
4971 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
4972 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
4973 VTBits == CstTy->getScalarSizeInBits()) {
4974 Tmp = VTBits;
4975 for (unsigned i = 0; i != NumElts; ++i) {
4976 if (!DemandedElts[i])
4977 continue;
4978 if (Constant *Elt = Cst->getAggregateElement(i)) {
4979 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4980 const APInt &Value = CInt->getValue();
4981 Tmp = std::min(Tmp, Value.getNumSignBits());
4982 continue;
4983 }
4984 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4985 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4986 Tmp = std::min(Tmp, Value.getNumSignBits());
4987 continue;
4988 }
4989 }
4990 // Unknown type. Conservatively assume no bits match sign bit.
4991 return 1;
4992 }
4993 return Tmp;
4994 }
4995 }
4996 break;
4997 }
4998 }
4999 }
5000
5001 // Allow the target to implement this method for its nodes.
5002 if (Opcode >= ISD::BUILTIN_OP_END ||
5003 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5004 Opcode == ISD::INTRINSIC_W_CHAIN ||
5005 Opcode == ISD::INTRINSIC_VOID) {
5006 // TODO: This can probably be removed once target code is audited. This
5007 // is here purely to reduce patch size and review complexity.
5008 if (!VT.isScalableVector()) {
5009 unsigned NumBits =
5010 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5011 if (NumBits > 1)
5012 FirstAnswer = std::max(FirstAnswer, NumBits);
5013 }
5014 }
5015
5016 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5017 // use this information.
5018 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5019 return std::max(FirstAnswer, Known.countMinSignBits());
5020}
5021
5023 unsigned Depth) const {
5024 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5025 return Op.getScalarValueSizeInBits() - SignBits + 1;
5026}
5027
5029 const APInt &DemandedElts,
5030 unsigned Depth) const {
5031 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5032 return Op.getScalarValueSizeInBits() - SignBits + 1;
5033}
5034
5036 unsigned Depth) const {
5037 // Early out for FREEZE.
5038 if (Op.getOpcode() == ISD::FREEZE)
5039 return true;
5040
5041 // TODO: Assume we don't know anything for now.
5042 EVT VT = Op.getValueType();
5043 if (VT.isScalableVector())
5044 return false;
5045
5046 APInt DemandedElts = VT.isVector()
5048 : APInt(1, 1);
5049 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5050}
5051
5053 const APInt &DemandedElts,
5054 bool PoisonOnly,
5055 unsigned Depth) const {
5056 unsigned Opcode = Op.getOpcode();
5057
5058 // Early out for FREEZE.
5059 if (Opcode == ISD::FREEZE)
5060 return true;
5061
5062 if (Depth >= MaxRecursionDepth)
5063 return false; // Limit search depth.
5064
5065 if (isIntOrFPConstant(Op))
5066 return true;
5067
5068 switch (Opcode) {
5069 case ISD::CONDCODE:
5070 case ISD::VALUETYPE:
5071 case ISD::FrameIndex:
5073 case ISD::CopyFromReg:
5074 return true;
5075
5076 case ISD::UNDEF:
5077 return PoisonOnly;
5078
5079 case ISD::BUILD_VECTOR:
5080 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5081 // this shouldn't affect the result.
5082 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5083 if (!DemandedElts[i])
5084 continue;
5086 Depth + 1))
5087 return false;
5088 }
5089 return true;
5090
5091 case ISD::VECTOR_SHUFFLE: {
5092 APInt DemandedLHS, DemandedRHS;
5093 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5094 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5095 DemandedElts, DemandedLHS, DemandedRHS,
5096 /*AllowUndefElts=*/false))
5097 return false;
5098 if (!DemandedLHS.isZero() &&
5099 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5100 PoisonOnly, Depth + 1))
5101 return false;
5102 if (!DemandedRHS.isZero() &&
5103 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5104 PoisonOnly, Depth + 1))
5105 return false;
5106 return true;
5107 }
5108
5109 // TODO: Search for noundef attributes from library functions.
5110
5111 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5112
5113 default:
5114 // Allow the target to implement this method for its nodes.
5115 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5116 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5118 Op, DemandedElts, *this, PoisonOnly, Depth);
5119 break;
5120 }
5121
5122 // If Op can't create undef/poison and none of its operands are undef/poison
5123 // then Op is never undef/poison.
5124 // NOTE: TargetNodes can handle this in themselves in
5125 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5126 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5127 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5128 Depth) &&
5129 all_of(Op->ops(), [&](SDValue V) {
5130 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5131 });
5132}
5133
5135 bool ConsiderFlags,
5136 unsigned Depth) const {
5137 // TODO: Assume we don't know anything for now.
5138 EVT VT = Op.getValueType();
5139 if (VT.isScalableVector())
5140 return true;
5141
5142 APInt DemandedElts = VT.isVector()
5144 : APInt(1, 1);
5145 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5146 Depth);
5147}
5148
5150 bool PoisonOnly, bool ConsiderFlags,
5151 unsigned Depth) const {
5152 // TODO: Assume we don't know anything for now.
5153 EVT VT = Op.getValueType();
5154 if (VT.isScalableVector())
5155 return true;
5156
5157 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5158 return true;
5159
5160 unsigned Opcode = Op.getOpcode();
5161 switch (Opcode) {
5162 case ISD::FREEZE:
5165 case ISD::SADDSAT:
5166 case ISD::UADDSAT:
5167 case ISD::SSUBSAT:
5168 case ISD::USUBSAT:
5169 case ISD::MULHU:
5170 case ISD::MULHS:
5171 case ISD::SMIN:
5172 case ISD::SMAX:
5173 case ISD::UMIN:
5174 case ISD::UMAX:
5175 case ISD::AND:
5176 case ISD::XOR:
5177 case ISD::ROTL:
5178 case ISD::ROTR:
5179 case ISD::FSHL:
5180 case ISD::FSHR:
5181 case ISD::BSWAP:
5182 case ISD::CTPOP:
5183 case ISD::BITREVERSE:
5184 case ISD::PARITY:
5185 case ISD::SIGN_EXTEND:
5186 case ISD::TRUNCATE:
5190 case ISD::BITCAST:
5191 case ISD::BUILD_VECTOR:
5192 case ISD::BUILD_PAIR:
5193 return false;
5194
5195 case ISD::SELECT_CC:
5196 case ISD::SETCC: {
5197 // Integer setcc cannot create undef or poison.
5198 if (Op.getOperand(0).getValueType().isInteger())
5199 return false;
5200
5201 // FP compares are more complicated. They can create poison for nan/infinity
5202 // based on options and flags. The options and flags also cause special
5203 // nonan condition codes to be used. Those condition codes may be preserved
5204 // even if the nonan flag is dropped somewhere.
5205 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5206 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5207 if (((unsigned)CCCode & 0x10U))
5208 return true;
5209
5211 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5212 }
5213
5214 case ISD::OR:
5215 case ISD::ZERO_EXTEND:
5216 case ISD::ADD:
5217 case ISD::SUB:
5218 case ISD::MUL:
5219 // No poison except from flags (which is handled above)
5220 return false;
5221
5222 case ISD::SHL:
5223 case ISD::SRL:
5224 case ISD::SRA:
5225 // If the max shift amount isn't in range, then the shift can create poison.
5226 return !getValidMaximumShiftAmountConstant(Op, DemandedElts);
5227
5229 // Check if we demand any upper (undef) elements.
5230 return !PoisonOnly && DemandedElts.ugt(1);
5231
5233 // Ensure that the element index is in bounds.
5234 EVT VecVT = Op.getOperand(0).getValueType();
5235 KnownBits KnownIdx = computeKnownBits(Op.getOperand(1), Depth + 1);
5236 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5237 }
5238
5240 // Ensure that the element index is in bounds.
5241 EVT VecVT = Op.getOperand(0).getValueType();
5242 KnownBits KnownIdx = computeKnownBits(Op.getOperand(2), Depth + 1);
5243 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5244 }
5245
5246 case ISD::VECTOR_SHUFFLE: {
5247 // Check for any demanded shuffle element that is undef.
5248 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5249 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5250 if (Elt < 0 && DemandedElts[Idx])
5251 return true;
5252 return false;
5253 }
5254
5255 default:
5256 // Allow the target to implement this method for its nodes.
5257 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5258 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5260 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5261 break;
5262 }
5263
5264 // Be conservative and return true.
5265 return true;
5266}
5267
5268bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5269 unsigned Opcode = Op.getOpcode();
5270 if (Opcode == ISD::OR)
5271 return Op->getFlags().hasDisjoint() ||
5272 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5273 if (Opcode == ISD::XOR)
5274 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5275 return false;
5276}
5277
5279 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5280 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5281}
5282
5283bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5284 // If we're told that NaNs won't happen, assume they won't.
5285 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5286 return true;
5287
5288 if (Depth >= MaxRecursionDepth)
5289 return false; // Limit search depth.
5290
5291 // If the value is a constant, we can obviously see if it is a NaN or not.
5292 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5293 return !C->getValueAPF().isNaN() ||
5294 (SNaN && !C->getValueAPF().isSignaling());
5295 }
5296
5297 unsigned Opcode = Op.getOpcode();
5298 switch (Opcode) {
5299 case ISD::FADD:
5300 case ISD::FSUB:
5301 case ISD::FMUL:
5302 case ISD::FDIV:
5303 case ISD::FREM:
5304 case ISD::FSIN:
5305 case ISD::FCOS:
5306 case ISD::FMA:
5307 case ISD::FMAD: {
5308 if (SNaN)
5309 return true;
5310 // TODO: Need isKnownNeverInfinity
5311 return false;
5312 }
5313 case ISD::FCANONICALIZE:
5314 case ISD::FEXP:
5315 case ISD::FEXP2:
5316 case ISD::FEXP10:
5317 case ISD::FTRUNC:
5318 case ISD::FFLOOR:
5319 case ISD::FCEIL:
5320 case ISD::FROUND:
5321 case ISD::FROUNDEVEN:
5322 case ISD::FRINT:
5323 case ISD::LRINT:
5324 case ISD::LLRINT:
5325 case ISD::FNEARBYINT:
5326 case ISD::FLDEXP: {
5327 if (SNaN)
5328 return true;
5329 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5330 }
5331 case ISD::FABS:
5332 case ISD::FNEG:
5333 case ISD::FCOPYSIGN: {
5334 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5335 }
5336 case ISD::SELECT:
5337 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5338 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5339 case ISD::FP_EXTEND:
5340 case ISD::FP_ROUND: {
5341 if (SNaN)
5342 return true;
5343 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5344 }
5345 case ISD::SINT_TO_FP:
5346 case ISD::UINT_TO_FP:
5347 return true;
5348 case ISD::FSQRT: // Need is known positive
5349 case ISD::FLOG:
5350 case ISD::FLOG2:
5351 case ISD::FLOG10:
5352 case ISD::FPOWI:
5353 case ISD::FPOW: {
5354 if (SNaN)
5355 return true;
5356 // TODO: Refine on operand
5357 return false;
5358 }
5359 case ISD::FMINNUM:
5360 case ISD::FMAXNUM: {
5361 // Only one needs to be known not-nan, since it will be returned if the
5362 // other ends up being one.
5363 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5364 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5365 }
5366 case ISD::FMINNUM_IEEE:
5367 case ISD::FMAXNUM_IEEE: {
5368 if (SNaN)
5369 return true;
5370 // This can return a NaN if either operand is an sNaN, or if both operands
5371 // are NaN.
5372 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5373 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5374 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5375 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5376 }
5377 case ISD::FMINIMUM:
5378 case ISD::FMAXIMUM: {
5379 // TODO: Does this quiet or return the origina NaN as-is?
5380 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5381 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5382 }
5384 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5385 }
5386 case ISD::BUILD_VECTOR: {
5387 for (const SDValue &Opnd : Op->ops())
5388 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5389 return false;
5390 return true;
5391 }
5392 default:
5393 if (Opcode >= ISD::BUILTIN_OP_END ||
5394 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5395 Opcode == ISD::INTRINSIC_W_CHAIN ||
5396 Opcode == ISD::INTRINSIC_VOID) {
5397 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5398 }
5399
5400 return false;
5401 }
5402}
5403
5405 assert(Op.getValueType().isFloatingPoint() &&
5406 "Floating point type expected");
5407
5408 // If the value is a constant, we can obviously see if it is a zero or not.
5410 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5411}
5412
5414 if (Depth >= MaxRecursionDepth)
5415 return false; // Limit search depth.
5416
5417 assert(!Op.getValueType().isFloatingPoint() &&
5418 "Floating point types unsupported - use isKnownNeverZeroFloat");
5419
5420 // If the value is a constant, we can obviously see if it is a zero or not.
5422 [](ConstantSDNode *C) { return !C->isZero(); }))
5423 return true;
5424
5425 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5426 // some degree.
5427 switch (Op.getOpcode()) {
5428 default:
5429 break;
5430
5431 case ISD::OR:
5432 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5433 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5434
5435 case ISD::VSELECT:
5436 case ISD::SELECT:
5437 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5438 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5439
5440 case ISD::SHL: {
5441 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5442 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5443 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5444 // 1 << X is never zero.
5445 if (ValKnown.One[0])
5446 return true;
5447 // If max shift cnt of known ones is non-zero, result is non-zero.
5448 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5449 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5450 !ValKnown.One.shl(MaxCnt).isZero())
5451 return true;
5452 break;
5453 }
5454 case ISD::UADDSAT:
5455 case ISD::UMAX:
5456 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5457 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5458
5459 // For smin/smax: If either operand is known negative/positive
5460 // respectively we don't need the other to be known at all.
5461 case ISD::SMAX: {
5462 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5463 if (Op1.isStrictlyPositive())
5464 return true;
5465
5466 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5467 if (Op0.isStrictlyPositive())
5468 return true;
5469
5470 if (Op1.isNonZero() && Op0.isNonZero())
5471 return true;
5472
5473 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5474 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5475 }
5476 case ISD::SMIN: {
5477 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5478 if (Op1.isNegative())
5479 return true;
5480
5481 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5482 if (Op0.isNegative())
5483 return true;
5484
5485 if (Op1.isNonZero() && Op0.isNonZero())
5486 return true;
5487
5488 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5489 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5490 }
5491 case ISD::UMIN:
5492 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5493 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5494
5495 case ISD::ROTL:
5496 case ISD::ROTR:
5497 case ISD::BITREVERSE:
5498 case ISD::BSWAP:
5499 case ISD::CTPOP:
5500 case ISD::ABS:
5501 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5502
5503 case ISD::SRA:
5504 case ISD::SRL: {
5505 if (Op->getFlags().hasExact())
5506 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5507 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5508 if (ValKnown.isNegative())
5509 return true;
5510 // If max shift cnt of known ones is non-zero, result is non-zero.
5511 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5512 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5513 !ValKnown.One.lshr(MaxCnt).isZero())
5514 return true;
5515 break;
5516 }
5517 case ISD::UDIV:
5518 case ISD::SDIV:
5519 // div exact can only produce a zero if the dividend is zero.
5520 // TODO: For udiv this is also true if Op1 u<= Op0
5521 if (Op->getFlags().hasExact())
5522 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5523 break;
5524
5525 case ISD::ADD:
5526 if (Op->getFlags().hasNoUnsignedWrap())
5527 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5528 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5529 return true;
5530 // TODO: There are a lot more cases we can prove for add.
5531 break;
5532
5533 case ISD::SUB: {
5534 if (isNullConstant(Op.getOperand(0)))
5535 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5536
5537 std::optional<bool> ne =
5538 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5539 computeKnownBits(Op.getOperand(1), Depth + 1));
5540 return ne && *ne;
5541 }
5542
5543 case ISD::MUL:
5544 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5545 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5546 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5547 return true;
5548 break;
5549
5550 case ISD::ZERO_EXTEND:
5551 case ISD::SIGN_EXTEND:
5552 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5553 }
5554
5556}
5557
5559 // Check the obvious case.
5560 if (A == B) return true;
5561
5562 // For negative and positive zero.
5563 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5564 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5565 if (CA->isZero() && CB->isZero()) return true;
5566
5567 // Otherwise they may not be equal.
5568 return false;
5569}
5570
5571// Only bits set in Mask must be negated, other bits may be arbitrary.
5573 if (isBitwiseNot(V, AllowUndefs))
5574 return V.getOperand(0);
5575
5576 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5577 // bits in the non-extended part.
5578 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5579 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5580 return SDValue();
5581 SDValue ExtArg = V.getOperand(0);
5582 if (ExtArg.getScalarValueSizeInBits() >=
5583 MaskC->getAPIntValue().getActiveBits() &&
5584 isBitwiseNot(ExtArg, AllowUndefs) &&
5585 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
5586 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
5587 return ExtArg.getOperand(0).getOperand(0);
5588 return SDValue();
5589}
5590
5592 // Match masked merge pattern (X & ~M) op (Y & M)
5593 // Including degenerate case (X & ~M) op M
5594 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
5595 SDValue Other) {
5596 if (SDValue NotOperand =
5597 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
5598 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
5599 NotOperand->getOpcode() == ISD::TRUNCATE)
5600 NotOperand = NotOperand->getOperand(0);
5601
5602 if (Other == NotOperand)
5603 return true;
5604 if (Other->getOpcode() == ISD::AND)
5605 return NotOperand == Other->getOperand(0) ||
5606 NotOperand == Other->getOperand(1);
5607 }
5608 return false;
5609 };
5610
5611 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
5612 A = A->getOperand(0);
5613
5614 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
5615 B = B->getOperand(0);
5616
5617 if (A->getOpcode() == ISD::AND)
5618 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
5619 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
5620 return false;
5621}
5622
5623// FIXME: unify with llvm::haveNoCommonBitsSet.
5625 assert(A.getValueType() == B.getValueType() &&
5626 "Values must have the same type");
5629 return true;
5632}
5633
5634static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
5635 SelectionDAG &DAG) {
5636 if (cast<ConstantSDNode>(Step)->isZero())
5637 return DAG.getConstant(0, DL, VT);
5638
5639 return SDValue();
5640}
5641
5644 SelectionDAG &DAG) {
5645 int NumOps = Ops.size();
5646 assert(NumOps != 0 && "Can't build an empty vector!");
5647 assert(!VT.isScalableVector() &&
5648 "BUILD_VECTOR cannot be used with scalable types");
5649 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
5650 "Incorrect element count in BUILD_VECTOR!");
5651
5652 // BUILD_VECTOR of UNDEFs is UNDEF.
5653 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5654 return DAG.getUNDEF(VT);
5655
5656 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
5657 SDValue IdentitySrc;
5658 bool IsIdentity = true;
5659 for (int i = 0; i != NumOps; ++i) {
5660 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5661 Ops[i].getOperand(0).getValueType() != VT ||
5662 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
5663 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
5664 Ops[i].getConstantOperandAPInt(1) != i) {
5665 IsIdentity = false;
5666 break;
5667 }
5668 IdentitySrc = Ops[i].getOperand(0);
5669 }
5670 if (IsIdentity)
5671 return IdentitySrc;
5672
5673 return SDValue();
5674}
5675
5676/// Try to simplify vector concatenation to an input value, undef, or build
5677/// vector.
5680 SelectionDAG &DAG) {
5681 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
5682 assert(llvm::all_of(Ops,
5683 [Ops](SDValue Op) {
5684 return Ops[0].getValueType() == Op.getValueType();
5685 }) &&
5686 "Concatenation of vectors with inconsistent value types!");
5687 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
5688 VT.getVectorElementCount() &&
5689 "Incorrect element count in vector concatenation!");
5690
5691 if (Ops.size() == 1)
5692 return Ops[0];
5693
5694 // Concat of UNDEFs is UNDEF.
5695 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5696 return DAG.getUNDEF(VT);
5697
5698 // Scan the operands and look for extract operations from a single source
5699 // that correspond to insertion at the same location via this concatenation:
5700 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
5701 SDValue IdentitySrc;
5702 bool IsIdentity = true;
5703 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
5704 SDValue Op = Ops[i];
5705 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
5706 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
5707 Op.getOperand(0).getValueType() != VT ||
5708 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
5709 Op.getConstantOperandVal(1) != IdentityIndex) {
5710 IsIdentity = false;
5711 break;
5712 }
5713 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
5714 "Unexpected identity source vector for concat of extracts");
5715 IdentitySrc = Op.getOperand(0);
5716 }
5717 if (IsIdentity) {
5718 assert(IdentitySrc && "Failed to set source vector of extracts");
5719 return IdentitySrc;
5720 }
5721
5722 // The code below this point is only designed to work for fixed width
5723 // vectors, so we bail out for now.
5724 if (VT.isScalableVector())
5725 return SDValue();
5726
5727 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
5728 // simplified to one big BUILD_VECTOR.
5729 // FIXME: Add support for SCALAR_TO_VECTOR as well.
5730 EVT SVT = VT.getScalarType();
5732 for (SDValue Op : Ops) {
5733 EVT OpVT = Op.getValueType();
5734 if (Op.isUndef())
5735 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
5736 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
5737 Elts.append(Op->op_begin(), Op->op_end());
5738 else
5739 return SDValue();
5740 }
5741
5742 // BUILD_VECTOR requires all inputs to be of the same type, find the
5743 // maximum type and extend them all.
5744 for (SDValue Op : Elts)
5745 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
5746
5747 if (SVT.bitsGT(VT.getScalarType())) {
5748 for (SDValue &Op : Elts) {
5749 if (Op.isUndef())
5750 Op = DAG.getUNDEF(SVT);
5751 else
5752 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
5753 ? DAG.getZExtOrTrunc(Op, DL, SVT)
5754 : DAG.getSExtOrTrunc(Op, DL, SVT);
5755 }
5756 }
5757
5758 SDValue V = DAG.getBuildVector(VT, DL, Elts);
5759 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
5760 return V;
5761}
5762
5763/// Gets or creates the specified node.
5764SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
5765 SDVTList VTs = getVTList(VT);
5767 AddNodeIDNode(ID, Opcode, VTs, std::nullopt);
5768 void *IP = nullptr;
5769 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5770 return SDValue(E, 0);
5771
5772 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5773 CSEMap.InsertNode(N, IP);
5774
5775 InsertNode(N);
5776 SDValue V = SDValue(N, 0);
5777 NewSDValueDbgMsg(V, "Creating new node: ", this);
5778 return V;
5779}
5780
5781SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5782 SDValue N1) {
5783 SDNodeFlags Flags;
5784 if (Inserter)
5785 Flags = Inserter->getFlags();
5786 return getNode(Opcode, DL, VT, N1, Flags);
5787}
5788
5789SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5790 SDValue N1, const SDNodeFlags Flags) {
5791 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
5792
5793 // Constant fold unary operations with a vector integer or float operand.
5794 switch (Opcode) {
5795 default:
5796 // FIXME: Entirely reasonable to perform folding of other unary
5797 // operations here as the need arises.
5798 break;
5799 case ISD::FNEG:
5800 case ISD::FABS:
5801 case ISD::FCEIL:
5802 case ISD::FTRUNC:
5803 case ISD::FFLOOR:
5804 case ISD::FP_EXTEND:
5805 case ISD::FP_TO_SINT:
5806 case ISD::FP_TO_UINT:
5807 case ISD::FP_TO_FP16:
5808 case ISD::FP_TO_BF16:
5809 case ISD::TRUNCATE:
5810 case ISD::ANY_EXTEND:
5811 case ISD::ZERO_EXTEND:
5812 case ISD::SIGN_EXTEND:
5813 case ISD::UINT_TO_FP:
5814 case ISD::SINT_TO_FP:
5815 case ISD::FP16_TO_FP:
5816 case ISD::BF16_TO_FP:
5817 case ISD::BITCAST:
5818 case ISD::ABS:
5819 case ISD::BITREVERSE:
5820 case ISD::BSWAP:
5821 case ISD::CTLZ:
5823 case ISD::CTTZ:
5825 case ISD::CTPOP:
5826 case ISD::STEP_VECTOR: {
5827 SDValue Ops = {N1};
5828 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
5829 return Fold;
5830 }
5831 }
5832
5833 unsigned OpOpcode = N1.getNode()->getOpcode();
5834 switch (Opcode) {
5835 case ISD::STEP_VECTOR:
5836 assert(VT.isScalableVector() &&
5837 "STEP_VECTOR can only be used with scalable types");
5838 assert(OpOpcode == ISD::TargetConstant &&
5839 VT.getVectorElementType() == N1.getValueType() &&
5840 "Unexpected step operand");
5841 break;
5842 case ISD::FREEZE:
5843 assert(VT == N1.getValueType() && "Unexpected VT!");
5844 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
5845 /*Depth*/ 1))
5846 return N1;
5847 break;
5848 case ISD::TokenFactor:
5849 case ISD::MERGE_VALUES:
5851 return N1; // Factor, merge or concat of one node? No need.
5852 case ISD::BUILD_VECTOR: {
5853 // Attempt to simplify BUILD_VECTOR.
5854 SDValue Ops[] = {N1};
5855 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5856 return V;
5857 break;
5858 }
5859 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
5860 case ISD::FP_EXTEND:
5862 "Invalid FP cast!");
5863 if (N1.getValueType() == VT) return N1; // noop conversion.
5864 assert((!VT.isVector() || VT.getVectorElementCount() ==
5866 "Vector element count mismatch!");
5867 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
5868 if (N1.isUndef())
5869 return getUNDEF(VT);
5870 break;
5871 case ISD::FP_TO_SINT:
5872 case ISD::FP_TO_UINT:
5873 if (N1.isUndef())
5874 return getUNDEF(VT);
5875 break;
5876 case ISD::SINT_TO_FP:
5877 case ISD::UINT_TO_FP:
5878 // [us]itofp(undef) = 0, because the result value is bounded.
5879 if (N1.isUndef())
5880 return getConstantFP(0.0, DL, VT);
5881 break;
5882 case ISD::SIGN_EXTEND:
5883 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5884 "Invalid SIGN_EXTEND!");
5885 assert(VT.isVector() == N1.getValueType().isVector() &&
5886 "SIGN_EXTEND result type type should be vector iff the operand "
5887 "type is vector!");
5888 if (N1.getValueType() == VT) return N1; // noop extension
5889 assert((!VT.isVector() || VT.getVectorElementCount() ==
5891 "Vector element count mismatch!");
5892 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
5893 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
5894 SDNodeFlags Flags;
5895 if (OpOpcode == ISD::ZERO_EXTEND)
5896 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5897 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
5898 }
5899 if (OpOpcode == ISD::UNDEF)
5900 // sext(undef) = 0, because the top bits will all be the same.
5901 return getConstant(0, DL, VT);
5902 break;
5903 case ISD::ZERO_EXTEND:
5904 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5905 "Invalid ZERO_EXTEND!");
5906 assert(VT.isVector() == N1.getValueType().isVector() &&
5907 "ZERO_EXTEND result type type should be vector iff the operand "
5908 "type is vector!");
5909 if (N1.getValueType() == VT) return N1; // noop extension
5910 assert((!VT.isVector() || VT.getVectorElementCount() ==
5912 "Vector element count mismatch!");
5913 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
5914 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
5915 SDNodeFlags Flags;
5916 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5917 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
5918 }
5919 if (OpOpcode == ISD::UNDEF)
5920 // zext(undef) = 0, because the top bits will be zero.
5921 return getConstant(0, DL, VT);
5922
5923 // Skip unnecessary zext_inreg pattern:
5924 // (zext (trunc x)) -> x iff the upper bits are known zero.
5925 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
5926 // use to recognise zext_inreg patterns.
5927 if (OpOpcode == ISD::TRUNCATE) {
5928 SDValue OpOp = N1.getOperand(0);
5929 if (OpOp.getValueType() == VT) {
5930 if (OpOp.getOpcode() != ISD::AND) {
5933 if (MaskedValueIsZero(OpOp, HiBits)) {
5934 transferDbgValues(N1, OpOp);
5935 return OpOp;
5936 }
5937 }
5938 }
5939 }
5940 break;
5941 case ISD::ANY_EXTEND:
5942 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5943 "Invalid ANY_EXTEND!");
5944 assert(VT.isVector() == N1.getValueType().isVector() &&
5945 "ANY_EXTEND result type type should be vector iff the operand "
5946 "type is vector!");
5947 if (N1.getValueType() == VT) return N1; // noop extension
5948 assert((!VT.isVector() || VT.getVectorElementCount() ==
5950 "Vector element count mismatch!");
5951 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
5952
5953 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
5954 OpOpcode == ISD::ANY_EXTEND) {
5955 SDNodeFlags Flags;
5956 if (OpOpcode == ISD::ZERO_EXTEND)
5957 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5958 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
5959 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
5960 }
5961 if (OpOpcode == ISD::UNDEF)
5962 return getUNDEF(VT);
5963
5964 // (ext (trunc x)) -> x
5965 if (OpOpcode == ISD::TRUNCATE) {
5966 SDValue OpOp = N1.getOperand(0);
5967 if (OpOp.getValueType() == VT) {
5968 transferDbgValues(N1, OpOp);
5969 return OpOp;
5970 }
5971 }
5972 break;
5973 case ISD::TRUNCATE:
5974 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5975 "Invalid TRUNCATE!");
5976 assert(VT.isVector() == N1.getValueType().isVector() &&
5977 "TRUNCATE result type type should be vector iff the operand "
5978 "type is vector!");
5979 if (N1.getValueType() == VT) return N1; // noop truncate
5980 assert((!VT.isVector() || VT.getVectorElementCount() ==
5982 "Vector element count mismatch!");
5983 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
5984 if (OpOpcode == ISD::TRUNCATE)
5985 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
5986 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
5987 OpOpcode == ISD::ANY_EXTEND) {
5988 // If the source is smaller than the dest, we still need an extend.
5990 VT.getScalarType()))
5991 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
5992 if (N1.getOperand(0).getValueType().bitsGT(VT))
5993 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
5994 return N1.getOperand(0);
5995 }
5996 if (OpOpcode == ISD::UNDEF)
5997 return getUNDEF(VT);
5998 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
5999 return getVScale(DL, VT,
6001 break;
6005 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6006 assert(N1.getValueType().bitsLE(VT) &&
6007 "The input must be the same size or smaller than the result.");
6010 "The destination vector type must have fewer lanes than the input.");
6011 break;
6012 case ISD::ABS:
6013 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6014 if (OpOpcode == ISD::UNDEF)
6015 return getConstant(0, DL, VT);
6016 break;
6017 case ISD::BSWAP:
6018 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6019 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6020 "BSWAP types must be a multiple of 16 bits!");
6021 if (OpOpcode == ISD::UNDEF)
6022 return getUNDEF(VT);
6023 // bswap(bswap(X)) -> X.
6024 if (OpOpcode == ISD::BSWAP)
6025 return N1.getOperand(0);
6026 break;
6027 case ISD::BITREVERSE:
6028 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6029 if (OpOpcode == ISD::UNDEF)
6030 return getUNDEF(VT);
6031 break;
6032 case ISD::BITCAST:
6034 "Cannot BITCAST between types of different sizes!");
6035 if (VT == N1.getValueType()) return N1; // noop conversion.
6036 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6037 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6038 if (OpOpcode == ISD::UNDEF)
6039 return getUNDEF(VT);
6040 break;
6042 assert(VT.isVector() && !N1.getValueType().isVector() &&
6043 (VT.getVectorElementType() == N1.getValueType() ||
6045 N1.getValueType().isInteger() &&
6047 "Illegal SCALAR_TO_VECTOR node!");
6048 if (OpOpcode == ISD::UNDEF)
6049 return getUNDEF(VT);
6050 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6051 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6052 isa<ConstantSDNode>(N1.getOperand(1)) &&
6053 N1.getConstantOperandVal(1) == 0 &&
6054 N1.getOperand(0).getValueType() == VT)
6055 return N1.getOperand(0);
6056 break;
6057 case ISD::FNEG:
6058 // Negation of an unknown bag of bits is still completely undefined.
6059 if (OpOpcode == ISD::UNDEF)
6060 return getUNDEF(VT);
6061
6062 if (OpOpcode == ISD::FNEG) // --X -> X
6063 return N1.getOperand(0);
6064 break;
6065 case ISD::FABS:
6066 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6067 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6068 break;
6069 case ISD::VSCALE:
6070 assert(VT == N1.getValueType() && "Unexpected VT!");
6071 break;
6072 case ISD::CTPOP:
6073 if (N1.getValueType().getScalarType() == MVT::i1)
6074 return N1;
6075 break;
6076 case ISD::CTLZ:
6077 case ISD::CTTZ:
6078 if (N1.getValueType().getScalarType() == MVT::i1)
6079 return getNOT(DL, N1, N1.getValueType());
6080 break;
6081 case ISD::VECREDUCE_ADD:
6082 if (N1.getValueType().getScalarType() == MVT::i1)
6083 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6084 break;
6087 if (N1.getValueType().getScalarType() == MVT::i1)
6088 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6089 break;
6092 if (N1.getValueType().getScalarType() == MVT::i1)
6093 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6094 break;
6095 case ISD::SPLAT_VECTOR:
6096 assert(VT.isVector() && "Wrong return type!");
6097 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6098 // that for now.
6100 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6102 N1.getValueType().isInteger() &&
6104 "Wrong operand type!");
6105 break;
6106 }
6107
6108 SDNode *N;
6109 SDVTList VTs = getVTList(VT);
6110 SDValue Ops[] = {N1};
6111 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6113 AddNodeIDNode(ID, Opcode, VTs, Ops);
6114 void *IP = nullptr;
6115 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6116 E->intersectFlagsWith(Flags);
6117 return SDValue(E, 0);
6118 }
6119
6120 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6121 N->setFlags(Flags);
6122 createOperands(N, Ops);
6123 CSEMap.InsertNode(N, IP);
6124 } else {
6125 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6126 createOperands(N, Ops);
6127 }
6128
6129 InsertNode(N);
6130 SDValue V = SDValue(N, 0);
6131 NewSDValueDbgMsg(V, "Creating new node: ", this);
6132 return V;
6133}
6134
6135static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6136 const APInt &C2) {
6137 switch (Opcode) {
6138 case ISD::ADD: return C1 + C2;
6139 case ISD::SUB: return C1 - C2;
6140 case ISD::MUL: return C1 * C2;
6141 case ISD::AND: return C1 & C2;
6142 case ISD::OR: return C1 | C2;
6143 case ISD::XOR: return C1 ^ C2;
6144 case ISD::SHL: return C1 << C2;
6145 case ISD::SRL: return C1.lshr(C2);
6146 case ISD::SRA: return C1.ashr(C2);
6147 case ISD::ROTL: return C1.rotl(C2);
6148 case ISD::ROTR: return C1.rotr(C2);
6149 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6150 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6151 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6152 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6153 case ISD::SADDSAT: return C1.sadd_sat(C2);
6154 case ISD::UADDSAT: return C1.uadd_sat(C2);
6155 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6156 case ISD::USUBSAT: return C1.usub_sat(C2);
6157 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6158 case ISD::USHLSAT: return C1.ushl_sat(C2);
6159 case ISD::UDIV:
6160 if (!C2.getBoolValue())
6161 break;
6162 return C1.udiv(C2);
6163 case ISD::UREM:
6164 if (!C2.getBoolValue())
6165 break;
6166 return C1.urem(C2);
6167 case ISD::SDIV:
6168 if (!C2.getBoolValue())
6169 break;
6170 return C1.sdiv(C2);
6171 case ISD::SREM:
6172 if (!C2.getBoolValue())
6173 break;
6174 return C1.srem(C2);
6175 case ISD::AVGFLOORS:
6176 return APIntOps::avgFloorS(C1, C2);
6177 case ISD::AVGFLOORU:
6178 return APIntOps::avgFloorU(C1, C2);
6179 case ISD::AVGCEILS:
6180 return APIntOps::avgCeilS(C1, C2);
6181 case ISD::AVGCEILU:
6182 return APIntOps::avgCeilU(C1, C2);
6183 case ISD::ABDS:
6184 return APIntOps::abds(C1, C2);
6185 case ISD::ABDU:
6186 return APIntOps::abdu(C1, C2);
6187 case ISD::MULHS:
6188 return APIntOps::mulhs(C1, C2);
6189 case ISD::MULHU:
6190 return APIntOps::mulhu(C1, C2);
6191 }
6192 return std::nullopt;
6193}
6194// Handle constant folding with UNDEF.
6195// TODO: Handle more cases.
6196static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6197 bool IsUndef1, const APInt &C2,
6198 bool IsUndef2) {
6199 if (!(IsUndef1 || IsUndef2))
6200 return FoldValue(Opcode, C1, C2);
6201
6202 // Fold and(x, undef) -> 0
6203 // Fold mul(x, undef) -> 0
6204 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6205 return APInt::getZero(C1.getBitWidth());
6206
6207 return std::nullopt;
6208}
6209
6211 const GlobalAddressSDNode *GA,
6212 const SDNode *N2) {
6213 if (GA->getOpcode() != ISD::GlobalAddress)
6214 return SDValue();
6215 if (!TLI->isOffsetFoldingLegal(GA))
6216 return SDValue();
6217 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6218 if (!C2)
6219 return SDValue();
6220 int64_t Offset = C2->getSExtValue();
6221 switch (Opcode) {
6222 case ISD::ADD: break;
6223 case ISD::SUB: Offset = -uint64_t(Offset); break;
6224 default: return SDValue();
6225 }
6226 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6227 GA->getOffset() + uint64_t(Offset));
6228}
6229
6230bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6231 switch (Opcode) {
6232 case ISD::SDIV:
6233 case ISD::UDIV:
6234 case ISD::SREM:
6235 case ISD::UREM: {
6236 // If a divisor is zero/undef or any element of a divisor vector is
6237 // zero/undef, the whole op is undef.
6238 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6239 SDValue Divisor = Ops[1];
6240 if (Divisor.isUndef() || isNullConstant(Divisor))
6241 return true;
6242
6243 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6244 llvm::any_of(Divisor->op_values(),
6245 [](SDValue V) { return V.isUndef() ||
6246 isNullConstant(V); });
6247 // TODO: Handle signed overflow.
6248 }
6249 // TODO: Handle oversized shifts.
6250 default:
6251 return false;
6252 }
6253}
6254
6256 EVT VT, ArrayRef<SDValue> Ops) {
6257 // If the opcode is a target-specific ISD node, there's nothing we can
6258 // do here and the operand rules may not line up with the below, so
6259 // bail early.
6260 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6261 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6262 // foldCONCAT_VECTORS in getNode before this is called.
6263 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6264 return SDValue();
6265
6266 unsigned NumOps = Ops.size();
6267 if (NumOps == 0)
6268 return SDValue();
6269
6270 if (isUndef(Opcode, Ops))
6271 return getUNDEF(VT);
6272
6273 // Handle unary special cases.
6274 if (NumOps == 1) {
6275 SDValue N1 = Ops[0];
6276
6277 // Constant fold unary operations with an integer constant operand. Even
6278 // opaque constant will be folded, because the folding of unary operations
6279 // doesn't create new constants with different values. Nevertheless, the
6280 // opaque flag is preserved during folding to prevent future folding with
6281 // other constants.
6282 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6283 const APInt &Val = C->getAPIntValue();
6284 switch (Opcode) {
6285 case ISD::SIGN_EXTEND:
6286 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6287 C->isTargetOpcode(), C->isOpaque());
6288 case ISD::TRUNCATE:
6289 if (C->isOpaque())
6290 break;
6291 [[fallthrough]];
6292 case ISD::ZERO_EXTEND:
6293 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6294 C->isTargetOpcode(), C->isOpaque());
6295 case ISD::ANY_EXTEND:
6296 // Some targets like RISCV prefer to sign extend some types.
6297 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6298 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6299 C->isTargetOpcode(), C->isOpaque());
6300 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6301 C->isTargetOpcode(), C->isOpaque());
6302 case ISD::ABS:
6303 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6304 C->isOpaque());
6305 case ISD::BITREVERSE:
6306 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6307 C->isOpaque());
6308 case ISD::BSWAP:
6309 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6310 C->isOpaque());
6311 case ISD::CTPOP:
6312 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6313 C->isOpaque());
6314 case ISD::CTLZ:
6316 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6317 C->isOpaque());
6318 case ISD::CTTZ:
6320 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6321 C->isOpaque());
6322 case ISD::UINT_TO_FP:
6323 case ISD::SINT_TO_FP: {
6326 (void)apf.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6328 return getConstantFP(apf, DL, VT);
6329 }
6330 case ISD::FP16_TO_FP:
6331 case ISD::BF16_TO_FP: {
6332 bool Ignored;
6333 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6334 : APFloat::BFloat(),
6335 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6336
6337 // This can return overflow, underflow, or inexact; we don't care.
6338 // FIXME need to be more flexible about rounding mode.
6339 (void)FPV.convert(EVTToAPFloatSemantics(VT),
6341 return getConstantFP(FPV, DL, VT);
6342 }
6343 case ISD::STEP_VECTOR:
6344 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6345 return V;
6346 break;
6347 case ISD::BITCAST:
6348 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6349 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6350 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6351 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6352 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6353 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6354 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6355 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6356 break;
6357 }
6358 }
6359
6360 // Constant fold unary operations with a floating point constant operand.
6361 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6362 APFloat V = C->getValueAPF(); // make copy
6363 switch (Opcode) {
6364 case ISD::FNEG:
6365 V.changeSign();
6366 return getConstantFP(V, DL, VT);
6367 case ISD::FABS:
6368 V.clearSign();
6369 return getConstantFP(V, DL, VT);
6370 case ISD::FCEIL: {
6371 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6372 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6373 return getConstantFP(V, DL, VT);
6374 return SDValue();
6375 }
6376 case ISD::FTRUNC: {
6377 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6378 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6379 return getConstantFP(V, DL, VT);
6380 return SDValue();
6381 }
6382 case ISD::FFLOOR: {
6383 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6384 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6385 return getConstantFP(V, DL, VT);
6386 return SDValue();
6387 }
6388 case ISD::FP_EXTEND: {
6389 bool ignored;
6390 // This can return overflow, underflow, or inexact; we don't care.
6391 // FIXME need to be more flexible about rounding mode.
6393 &ignored);
6394 return getConstantFP(V, DL, VT);
6395 }
6396 case ISD::FP_TO_SINT:
6397 case ISD::FP_TO_UINT: {
6398 bool ignored;
6399 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6400 // FIXME need to be more flexible about rounding mode.
6402 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6403 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6404 break;
6405 return getConstant(IntVal, DL, VT);
6406 }
6407 case ISD::FP_TO_FP16:
6408 case ISD::FP_TO_BF16: {
6409 bool Ignored;
6410 // This can return overflow, underflow, or inexact; we don't care.
6411 // FIXME need to be more flexible about rounding mode.
6412 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6413 : APFloat::BFloat(),
6415 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6416 }
6417 case ISD::BITCAST:
6418 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6419 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6420 VT);
6421 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6422 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6423 VT);
6424 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6425 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6426 VT);
6427 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6428 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6429 break;
6430 }
6431 }
6432
6433 // Early-out if we failed to constant fold a bitcast.
6434 if (Opcode == ISD::BITCAST)
6435 return SDValue();
6436 }
6437
6438 // Handle binops special cases.
6439 if (NumOps == 2) {
6440 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6441 return CFP;
6442
6443 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6444 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6445 if (C1->isOpaque() || C2->isOpaque())
6446 return SDValue();
6447
6448 std::optional<APInt> FoldAttempt =
6449 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6450 if (!FoldAttempt)
6451 return SDValue();
6452
6453 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6454 assert((!Folded || !VT.isVector()) &&
6455 "Can't fold vectors ops with scalar operands");
6456 return Folded;
6457 }
6458 }
6459
6460 // fold (add Sym, c) -> Sym+c
6461 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6462 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6463 if (TLI->isCommutativeBinOp(Opcode))
6464 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6465 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6466 }
6467
6468 // This is for vector folding only from here on.
6469 if (!VT.isVector())
6470 return SDValue();
6471
6472 ElementCount NumElts = VT.getVectorElementCount();
6473
6474 // See if we can fold through bitcasted integer ops.
6475 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6476 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6477 Ops[0].getOpcode() == ISD::BITCAST &&
6478 Ops[1].getOpcode() == ISD::BITCAST) {
6479 SDValue N1 = peekThroughBitcasts(Ops[0]);
6480 SDValue N2 = peekThroughBitcasts(Ops[1]);
6481 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6482 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6483 EVT BVVT = N1.getValueType();
6484 if (BV1 && BV2 && BVVT.isInteger() && BVVT == N2.getValueType()) {
6485 bool IsLE = getDataLayout().isLittleEndian();
6486 unsigned EltBits = VT.getScalarSizeInBits();
6487 SmallVector<APInt> RawBits1, RawBits2;
6488 BitVector UndefElts1, UndefElts2;
6489 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6490 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6491 SmallVector<APInt> RawBits;
6492 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6493 std::optional<APInt> Fold = FoldValueWithUndef(
6494 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6495 if (!Fold)
6496 break;
6497 RawBits.push_back(*Fold);
6498 }
6499 if (RawBits.size() == NumElts.getFixedValue()) {
6500 // We have constant folded, but we need to cast this again back to
6501 // the original (possibly legalized) type.
6502 SmallVector<APInt> DstBits;
6503 BitVector DstUndefs;
6505 DstBits, RawBits, DstUndefs,
6506 BitVector(RawBits.size(), false));
6507 EVT BVEltVT = BV1->getOperand(0).getValueType();
6508 unsigned BVEltBits = BVEltVT.getSizeInBits();
6509 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6510 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6511 if (DstUndefs[I])
6512 continue;
6513 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6514 }
6515 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6516 }
6517 }
6518 }
6519 }
6520
6521 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6522 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6523 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6524 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6525 APInt RHSVal;
6526 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6527 APInt NewStep = Opcode == ISD::MUL
6528 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6529 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6530 return getStepVector(DL, VT, NewStep);
6531 }
6532 }
6533
6534 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6535 return !Op.getValueType().isVector() ||
6536 Op.getValueType().getVectorElementCount() == NumElts;
6537 };
6538
6539 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
6540 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
6541 Op.getOpcode() == ISD::BUILD_VECTOR ||
6542 Op.getOpcode() == ISD::SPLAT_VECTOR;
6543 };
6544
6545 // All operands must be vector types with the same number of elements as
6546 // the result type and must be either UNDEF or a build/splat vector
6547 // or UNDEF scalars.
6548 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
6549 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
6550 return SDValue();
6551
6552 // If we are comparing vectors, then the result needs to be a i1 boolean that
6553 // is then extended back to the legal result type depending on how booleans
6554 // are represented.
6555 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
6556 ISD::NodeType ExtendCode =
6557 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
6560
6561 // Find legal integer scalar type for constant promotion and
6562 // ensure that its scalar size is at least as large as source.
6563 EVT LegalSVT = VT.getScalarType();
6564 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
6565 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
6566 if (LegalSVT.bitsLT(VT.getScalarType()))
6567 return SDValue();
6568 }
6569
6570 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
6571 // only have one operand to check. For fixed-length vector types we may have
6572 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
6573 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
6574
6575 // Constant fold each scalar lane separately.
6576 SmallVector<SDValue, 4> ScalarResults;
6577 for (unsigned I = 0; I != NumVectorElts; I++) {
6578 SmallVector<SDValue, 4> ScalarOps;
6579 for (SDValue Op : Ops) {
6580 EVT InSVT = Op.getValueType().getScalarType();
6581 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
6582 Op.getOpcode() != ISD::SPLAT_VECTOR) {
6583 if (Op.isUndef())
6584 ScalarOps.push_back(getUNDEF(InSVT));
6585 else
6586 ScalarOps.push_back(Op);
6587 continue;
6588 }
6589
6590 SDValue ScalarOp =
6591 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
6592 EVT ScalarVT = ScalarOp.getValueType();
6593
6594 // Build vector (integer) scalar operands may need implicit
6595 // truncation - do this before constant folding.
6596 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
6597 // Don't create illegally-typed nodes unless they're constants or undef
6598 // - if we fail to constant fold we can't guarantee the (dead) nodes
6599 // we're creating will be cleaned up before being visited for
6600 // legalization.
6601 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
6602 !isa<ConstantSDNode>(ScalarOp) &&
6603 TLI->getTypeAction(*getContext(), InSVT) !=
6605 return SDValue();
6606 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
6607 }
6608
6609 ScalarOps.push_back(ScalarOp);
6610 }
6611
6612 // Constant fold the scalar operands.
6613 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps);
6614
6615 // Legalize the (integer) scalar constant if necessary.
6616 if (LegalSVT != SVT)
6617 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
6618
6619 // Scalar folding only succeeded if the result is a constant or UNDEF.
6620 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
6621 ScalarResult.getOpcode() != ISD::ConstantFP)
6622 return SDValue();
6623 ScalarResults.push_back(ScalarResult);
6624 }
6625
6626 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
6627 : getBuildVector(VT, DL, ScalarResults);
6628 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
6629 return V;
6630}
6631
6633 EVT VT, ArrayRef<SDValue> Ops) {
6634 // TODO: Add support for unary/ternary fp opcodes.
6635 if (Ops.size() != 2)
6636 return SDValue();
6637
6638 // TODO: We don't do any constant folding for strict FP opcodes here, but we
6639 // should. That will require dealing with a potentially non-default
6640 // rounding mode, checking the "opStatus" return value from the APFloat
6641 // math calculations, and possibly other variations.
6642 SDValue N1 = Ops[0];
6643 SDValue N2 = Ops[1];
6644 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
6645 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
6646 if (N1CFP && N2CFP) {
6647 APFloat C1 = N1CFP->getValueAPF(); // make copy
6648 const APFloat &C2 = N2CFP->getValueAPF();
6649 switch (Opcode) {
6650 case ISD::FADD:
6652 return getConstantFP(C1, DL, VT);
6653 case ISD::FSUB:
6655 return getConstantFP(C1, DL, VT);
6656 case ISD::FMUL:
6658 return getConstantFP(C1, DL, VT);
6659 case ISD::FDIV:
6661 return getConstantFP(C1, DL, VT);
6662 case ISD::FREM:
6663 C1.mod(C2);
6664 return getConstantFP(C1, DL, VT);
6665 case ISD::FCOPYSIGN:
6666 C1.copySign(C2);
6667 return getConstantFP(C1, DL, VT);
6668 case ISD::FMINNUM:
6669 return getConstantFP(minnum(C1, C2), DL, VT);
6670 case ISD::FMAXNUM:
6671 return getConstantFP(maxnum(C1, C2), DL, VT);
6672 case ISD::FMINIMUM:
6673 return getConstantFP(minimum(C1, C2), DL, VT);
6674 case ISD::FMAXIMUM:
6675 return getConstantFP(maximum(C1, C2), DL, VT);
6676 default: break;
6677 }
6678 }
6679 if (N1CFP && Opcode == ISD::FP_ROUND) {
6680 APFloat C1 = N1CFP->getValueAPF(); // make copy
6681 bool Unused;
6682 // This can return overflow, underflow, or inexact; we don't care.
6683 // FIXME need to be more flexible about rounding mode.
6685 &Unused);
6686 return getConstantFP(C1, DL, VT);
6687 }
6688
6689 switch (Opcode) {
6690 case ISD::FSUB:
6691 // -0.0 - undef --> undef (consistent with "fneg undef")
6692 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
6693 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
6694 return getUNDEF(VT);
6695 [[fallthrough]];
6696
6697 case ISD::FADD:
6698 case ISD::FMUL:
6699 case ISD::FDIV:
6700 case ISD::FREM:
6701 // If both operands are undef, the result is undef. If 1 operand is undef,
6702 // the result is NaN. This should match the behavior of the IR optimizer.
6703 if (N1.isUndef() && N2.isUndef())
6704 return getUNDEF(VT);
6705 if (N1.isUndef() || N2.isUndef())
6707 }
6708 return SDValue();
6709}
6710
6712 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
6713
6714 // There's no need to assert on a byte-aligned pointer. All pointers are at
6715 // least byte aligned.
6716 if (A == Align(1))
6717 return Val;
6718
6719 SDVTList VTs = getVTList(Val.getValueType());
6721 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
6722 ID.AddInteger(A.value());
6723
6724 void *IP = nullptr;
6725 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6726 return SDValue(E, 0);
6727
6728 auto *N =
6729 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
6730 createOperands(N, {Val});
6731
6732 CSEMap.InsertNode(N, IP);
6733 InsertNode(N);
6734
6735 SDValue V(N, 0);
6736 NewSDValueDbgMsg(V, "Creating new node: ", this);
6737 return V;
6738}
6739
6740SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6741 SDValue N1, SDValue N2) {
6742 SDNodeFlags Flags;
6743 if (Inserter)
6744 Flags = Inserter->getFlags();
6745 return getNode(Opcode, DL, VT, N1, N2, Flags);
6746}
6747
6749 SDValue &N2) const {
6750 if (!TLI->isCommutativeBinOp(Opcode))
6751 return;
6752
6753 // Canonicalize:
6754 // binop(const, nonconst) -> binop(nonconst, const)
6759 if ((N1C && !N2C) || (N1CFP && !N2CFP))
6760 std::swap(N1, N2);
6761
6762 // Canonicalize:
6763 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
6764 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
6766 std::swap(N1, N2);
6767}
6768
6769SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6770 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
6772 N2.getOpcode() != ISD::DELETED_NODE &&
6773 "Operand is DELETED_NODE!");
6774
6775 canonicalizeCommutativeBinop(Opcode, N1, N2);
6776
6777 auto *N1C = dyn_cast<ConstantSDNode>(N1);
6778 auto *N2C = dyn_cast<ConstantSDNode>(N2);
6779
6780 // Don't allow undefs in vector splats - we might be returning N2 when folding
6781 // to zero etc.
6782 ConstantSDNode *N2CV =
6783 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
6784
6785 switch (Opcode) {
6786 default: break;
6787 case ISD::TokenFactor:
6788 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
6789 N2.getValueType() == MVT::Other && "Invalid token factor!");
6790 // Fold trivial token factors.
6791 if (N1.getOpcode() == ISD::EntryToken) return N2;
6792 if (N2.getOpcode() == ISD::EntryToken) return N1;
6793 if (N1 == N2) return N1;
6794 break;
6795 case ISD::BUILD_VECTOR: {
6796 // Attempt to simplify BUILD_VECTOR.
6797 SDValue Ops[] = {N1, N2};
6798 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6799 return V;
6800 break;
6801 }
6802 case ISD::CONCAT_VECTORS: {
6803 SDValue Ops[] = {N1, N2};
6804 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
6805 return V;
6806 break;
6807 }
6808 case ISD::AND:
6809 assert(VT.isInteger() && "This operator does not apply to FP types!");
6810 assert(N1.getValueType() == N2.getValueType() &&
6811 N1.getValueType() == VT && "Binary operator types must match!");
6812 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
6813 // worth handling here.
6814 if (N2CV && N2CV->isZero())
6815 return N2;
6816 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
6817 return N1;
6818 break;
6819 case ISD::OR:
6820 case ISD::XOR:
6821 case ISD::ADD:
6822 case ISD::SUB:
6823 assert(VT.isInteger() && "This operator does not apply to FP types!");
6824 assert(N1.getValueType() == N2.getValueType() &&
6825 N1.getValueType() == VT && "Binary operator types must match!");
6826 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
6827 // it's worth handling here.
6828 if (N2CV && N2CV->isZero())
6829 return N1;
6830 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
6831 VT.getVectorElementType() == MVT::i1)
6832 return getNode(ISD::XOR, DL, VT, N1, N2);
6833 break;
6834 case ISD::MUL:
6835 assert(VT.isInteger() && "This operator does not apply to FP types!");
6836 assert(N1.getValueType() == N2.getValueType() &&
6837 N1.getValueType() == VT && "Binary operator types must match!");
6838 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6839 return getNode(ISD::AND, DL, VT, N1, N2);
6840 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6841 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6842 const APInt &N2CImm = N2C->getAPIntValue();
6843 return getVScale(DL, VT, MulImm * N2CImm);
6844 }
6845 break;
6846 case ISD::UDIV:
6847 case ISD::UREM:
6848 case ISD::MULHU:
6849 case ISD::MULHS:
6850 case ISD::SDIV:
6851 case ISD::SREM:
6852 case ISD::SADDSAT:
6853 case ISD::SSUBSAT:
6854 case ISD::UADDSAT:
6855 case ISD::USUBSAT:
6856 assert(VT.isInteger() && "This operator does not apply to FP types!");
6857 assert(N1.getValueType() == N2.getValueType() &&
6858 N1.getValueType() == VT && "Binary operator types must match!");
6859 if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
6860 // fold (add_sat x, y) -> (or x, y) for bool types.
6861 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
6862 return getNode(ISD::OR, DL, VT, N1, N2);
6863 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
6864 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
6865 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
6866 }
6867 break;
6868 case ISD::ABDS:
6869 case ISD::ABDU:
6870 assert(VT.isInteger() && "This operator does not apply to FP types!");
6871 assert(N1.getValueType() == N2.getValueType() &&
6872 N1.getValueType() == VT && "Binary operator types must match!");
6873 break;
6874 case ISD::SMIN:
6875 case ISD::UMAX:
6876 assert(VT.isInteger() && "This operator does not apply to FP types!");
6877 assert(N1.getValueType() == N2.getValueType() &&
6878 N1.getValueType() == VT && "Binary operator types must match!");
6879 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6880 return getNode(ISD::OR, DL, VT, N1, N2);
6881 break;
6882 case ISD::SMAX:
6883 case ISD::UMIN:
6884 assert(VT.isInteger() && "This operator does not apply to FP types!");
6885 assert(N1.getValueType() == N2.getValueType() &&
6886 N1.getValueType() == VT && "Binary operator types must match!");
6887 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6888 return getNode(ISD::AND, DL, VT, N1, N2);
6889 break;
6890 case ISD::FADD:
6891 case ISD::FSUB:
6892 case ISD::FMUL:
6893 case ISD::FDIV:
6894 case ISD::FREM:
6895 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
6896 assert(N1.getValueType() == N2.getValueType() &&
6897 N1.getValueType() == VT && "Binary operator types must match!");
6898 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
6899 return V;
6900 break;
6901 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
6902 assert(N1.getValueType() == VT &&
6905 "Invalid FCOPYSIGN!");
6906 break;
6907 case ISD::SHL:
6908 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6909 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6910 const APInt &ShiftImm = N2C->getAPIntValue();
6911 return getVScale(DL, VT, MulImm << ShiftImm);
6912 }
6913 [[fallthrough]];
6914 case ISD::SRA:
6915 case ISD::SRL:
6916 if (SDValue V = simplifyShift(N1, N2))
6917 return V;
6918 [[fallthrough]];
6919 case ISD::ROTL:
6920 case ISD::ROTR:
6921 assert(VT == N1.getValueType() &&
6922 "Shift operators return type must be the same as their first arg");
6923 assert(VT.isInteger() && N2.getValueType().isInteger() &&
6924 "Shifts only work on integers");
6925 assert((!VT.isVector() || VT == N2.getValueType()) &&
6926 "Vector shift amounts must be in the same as their first arg");
6927 // Verify that the shift amount VT is big enough to hold valid shift
6928 // amounts. This catches things like trying to shift an i1024 value by an
6929 // i8, which is easy to fall into in generic code that uses
6930 // TLI.getShiftAmount().
6933 "Invalid use of small shift amount with oversized value!");
6934
6935 // Always fold shifts of i1 values so the code generator doesn't need to
6936 // handle them. Since we know the size of the shift has to be less than the
6937 // size of the value, the shift/rotate count is guaranteed to be zero.
6938 if (VT == MVT::i1)
6939 return N1;
6940 if (N2CV && N2CV->isZero())
6941 return N1;
6942 break;
6943 case ISD::FP_ROUND:
6944 assert(VT.isFloatingPoint() &&
6946 VT.bitsLE(N1.getValueType()) &&
6947 N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
6948 "Invalid FP_ROUND!");
6949 if (N1.getValueType() == VT) return N1; // noop conversion.
6950 break;
6951 case ISD::AssertSext:
6952 case ISD::AssertZext: {
6953 EVT EVT = cast<VTSDNode>(N2)->getVT();
6954 assert(VT == N1.getValueType() && "Not an inreg extend!");
6955 assert(VT.isInteger() && EVT.isInteger() &&
6956 "Cannot *_EXTEND_INREG FP types");
6957 assert(!EVT.isVector() &&
6958 "AssertSExt/AssertZExt type should be the vector element type "
6959 "rather than the vector type!");
6960 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
6961 if (VT.getScalarType() == EVT) return N1; // noop assertion.
6962 break;
6963 }
6965 EVT EVT = cast<VTSDNode>(N2)->getVT();
6966 assert(VT == N1.getValueType() && "Not an inreg extend!");
6967 assert(VT.isInteger() && EVT.isInteger() &&
6968 "Cannot *_EXTEND_INREG FP types");
6969 assert(EVT.isVector() == VT.isVector() &&
6970 "SIGN_EXTEND_INREG type should be vector iff the operand "
6971 "type is vector!");
6972 assert((!EVT.isVector() ||
6974 "Vector element counts must match in SIGN_EXTEND_INREG");
6975 assert(EVT.bitsLE(VT) && "Not extending!");
6976 if (EVT == VT) return N1; // Not actually extending
6977
6978 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
6979 unsigned FromBits = EVT.getScalarSizeInBits();
6980 Val <<= Val.getBitWidth() - FromBits;
6981 Val.ashrInPlace(Val.getBitWidth() - FromBits);
6982 return getConstant(Val, DL, ConstantVT);
6983 };
6984
6985 if (N1C) {
6986 const APInt &Val = N1C->getAPIntValue();
6987 return SignExtendInReg(Val, VT);
6988 }
6989
6992 llvm::EVT OpVT = N1.getOperand(0).getValueType();
6993 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
6994 SDValue Op = N1.getOperand(i);
6995 if (Op.isUndef()) {
6996 Ops.push_back(getUNDEF(OpVT));
6997 continue;
6998 }
6999 ConstantSDNode *C = cast<ConstantSDNode>(Op);
7000 APInt Val = C->getAPIntValue();
7001 Ops.push_back(SignExtendInReg(Val, OpVT));
7002 }
7003 return getBuildVector(VT, DL, Ops);
7004 }
7005
7006 if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7007 isa<ConstantSDNode>(N1.getOperand(0)))
7008 return getNode(
7009 ISD::SPLAT_VECTOR, DL, VT,
7010 SignExtendInReg(N1.getConstantOperandAPInt(0),
7011 N1.getOperand(0).getValueType()));
7012 break;
7013 }
7015 case ISD::FP_TO_UINT_SAT: {
7016 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7017 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7018 assert(N1.getValueType().isVector() == VT.isVector() &&
7019 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7020 "vector!");
7021 assert((!VT.isVector() || VT.getVectorElementCount() ==
7023 "Vector element counts must match in FP_TO_*INT_SAT");
7024 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7025 "Type to saturate to must be a scalar.");
7026 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7027 "Not extending!");
7028 break;
7029 }
7032 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7033 element type of the vector.");
7034
7035 // Extract from an undefined value or using an undefined index is undefined.
7036 if (N1.isUndef() || N2.isUndef())
7037 return getUNDEF(VT);
7038
7039 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7040 // vectors. For scalable vectors we will provide appropriate support for
7041 // dealing with arbitrary indices.
7042 if (N2C && N1.getValueType().isFixedLengthVector() &&
7043 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7044 return getUNDEF(VT);
7045
7046 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7047 // expanding copies of large vectors from registers. This only works for
7048 // fixed length vectors, since we need to know the exact number of
7049 // elements.
7050 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7052 unsigned Factor =
7055 N1.getOperand(N2C->getZExtValue() / Factor),
7056 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
7057 }
7058
7059 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7060 // lowering is expanding large vector constants.
7061 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7062 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7065 "BUILD_VECTOR used for scalable vectors");
7066 unsigned Index =
7067 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7068 SDValue Elt = N1.getOperand(Index);
7069
7070 if (VT != Elt.getValueType())
7071 // If the vector element type is not legal, the BUILD_VECTOR operands
7072 // are promoted and implicitly truncated, and the result implicitly
7073 // extended. Make that explicit here.
7074 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7075
7076 return Elt;
7077 }
7078
7079 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7080 // operations are lowered to scalars.
7081 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7082 // If the indices are the same, return the inserted element else
7083 // if the indices are known different, extract the element from
7084 // the original vector.
7085 SDValue N1Op2 = N1.getOperand(2);
7086 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7087
7088 if (N1Op2C && N2C) {
7089 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7090 if (VT == N1.getOperand(1).getValueType())
7091 return N1.getOperand(1);
7092 if (VT.isFloatingPoint()) {
7094 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7095 }
7096 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7097 }
7098 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7099 }
7100 }
7101
7102 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7103 // when vector types are scalarized and v1iX is legal.
7104 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7105 // Here we are completely ignoring the extract element index (N2),
7106 // which is fine for fixed width vectors, since any index other than 0
7107 // is undefined anyway. However, this cannot be ignored for scalable
7108 // vectors - in theory we could support this, but we don't want to do this
7109 // without a profitability check.
7110 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7112 N1.getValueType().getVectorNumElements() == 1) {
7113 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7114 N1.getOperand(1));
7115 }
7116 break;
7118 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7119 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7120 (N1.getValueType().isInteger() == VT.isInteger()) &&
7121 N1.getValueType() != VT &&
7122 "Wrong types for EXTRACT_ELEMENT!");
7123
7124 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7125 // 64-bit integers into 32-bit parts. Instead of building the extract of
7126 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7127 if (N1.getOpcode() == ISD::BUILD_PAIR)
7128 return N1.getOperand(N2C->getZExtValue());
7129
7130 // EXTRACT_ELEMENT of a constant int is also very common.
7131 if (N1C) {
7132 unsigned ElementSize = VT.getSizeInBits();
7133 unsigned Shift = ElementSize * N2C->getZExtValue();
7134 const APInt &Val = N1C->getAPIntValue();
7135 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7136 }
7137 break;
7139 EVT N1VT = N1.getValueType();
7140 assert(VT.isVector() && N1VT.isVector() &&
7141 "Extract subvector VTs must be vectors!");
7143 "Extract subvector VTs must have the same element type!");
7144 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7145 "Cannot extract a scalable vector from a fixed length vector!");
7146 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7148 "Extract subvector must be from larger vector to smaller vector!");
7149 assert(N2C && "Extract subvector index must be a constant");
7150 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7151 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7152 N1VT.getVectorMinNumElements()) &&
7153 "Extract subvector overflow!");
7154 assert(N2C->getAPIntValue().getBitWidth() ==
7155 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7156 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7157
7158 // Trivial extraction.
7159 if (VT == N1VT)
7160 return N1;
7161
7162 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7163 if (N1.isUndef())
7164 return getUNDEF(VT);
7165
7166 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7167 // the concat have the same type as the extract.
7168 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7169 VT == N1.getOperand(0).getValueType()) {
7170 unsigned Factor = VT.getVectorMinNumElements();
7171 return N1.getOperand(N2C->getZExtValue() / Factor);
7172 }
7173
7174 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7175 // during shuffle legalization.
7176 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7177 VT == N1.getOperand(1).getValueType())
7178 return N1.getOperand(1);
7179 break;
7180 }
7181 }
7182
7183 // Perform trivial constant folding.
7184 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}))
7185 return SV;
7186
7187 // Canonicalize an UNDEF to the RHS, even over a constant.
7188 if (N1.isUndef()) {
7189 if (TLI->isCommutativeBinOp(Opcode)) {
7190 std::swap(N1, N2);
7191 } else {
7192 switch (Opcode) {
7193 case ISD::SUB:
7194 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7196 case ISD::UDIV:
7197 case ISD::SDIV:
7198 case ISD::UREM:
7199 case ISD::SREM:
7200 case ISD::SSUBSAT:
7201 case ISD::USUBSAT:
7202 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7203 }
7204 }
7205 }
7206
7207 // Fold a bunch of operators when the RHS is undef.
7208 if (N2.isUndef()) {
7209 switch (Opcode) {
7210 case ISD::XOR:
7211 if (N1.isUndef())
7212 // Handle undef ^ undef -> 0 special case. This is a common
7213 // idiom (misuse).
7214 return getConstant(0, DL, VT);
7215 [[fallthrough]];
7216 case ISD::ADD:
7217 case ISD::SUB:
7218 case ISD::UDIV:
7219 case ISD::SDIV:
7220 case ISD::UREM:
7221 case ISD::SREM:
7222 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7223 case ISD::MUL:
7224 case ISD::AND:
7225 case ISD::SSUBSAT:
7226 case ISD::USUBSAT:
7227 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7228 case ISD::OR:
7229 case ISD::SADDSAT:
7230 case ISD::UADDSAT:
7231 return getAllOnesConstant(DL, VT);
7232 }
7233 }
7234
7235 // Memoize this node if possible.
7236 SDNode *N;
7237 SDVTList VTs = getVTList(VT);
7238 SDValue Ops[] = {N1, N2};
7239 if (VT != MVT::Glue) {
7241 AddNodeIDNode(ID, Opcode, VTs, Ops);
7242 void *IP = nullptr;
7243 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7244 E->intersectFlagsWith(Flags);
7245 return SDValue(E, 0);
7246 }
7247
7248 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7249 N->setFlags(Flags);
7250 createOperands(N, Ops);
7251 CSEMap.InsertNode(N, IP);
7252 } else {
7253 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7254 createOperands(N, Ops);
7255 }
7256
7257 InsertNode(N);
7258 SDValue V = SDValue(N, 0);
7259 NewSDValueDbgMsg(V, "Creating new node: ", this);
7260 return V;
7261}
7262
7263SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7264 SDValue N1, SDValue N2, SDValue N3) {
7265 SDNodeFlags Flags;
7266 if (Inserter)
7267 Flags = Inserter->getFlags();
7268 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7269}
7270
7271SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7272 SDValue N1, SDValue N2, SDValue N3,
7273 const SDNodeFlags Flags) {
7275 N2.getOpcode() != ISD::DELETED_NODE &&
7276 N3.getOpcode() != ISD::DELETED_NODE &&
7277 "Operand is DELETED_NODE!");
7278 // Perform various simplifications.
7279 switch (Opcode) {
7280 case ISD::FMA:
7281 case ISD::FMAD: {
7282 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7283 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7284 N3.getValueType() == VT && "FMA types must match!");
7285 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7286 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7287 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7288 if (N1CFP && N2CFP && N3CFP) {
7289 APFloat V1 = N1CFP->getValueAPF();
7290 const APFloat &V2 = N2CFP->getValueAPF();
7291 const APFloat &V3 = N3CFP->getValueAPF();
7292 if (Opcode == ISD::FMAD) {
7295 } else
7297 return getConstantFP(V1, DL, VT);
7298 }
7299 break;
7300 }
7301 case ISD::BUILD_VECTOR: {
7302 // Attempt to simplify BUILD_VECTOR.
7303 SDValue Ops[] = {N1, N2, N3};
7304 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7305 return V;
7306 break;
7307 }
7308 case ISD::CONCAT_VECTORS: {
7309 SDValue Ops[] = {N1, N2, N3};
7310 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7311 return V;
7312 break;
7313 }
7314 case ISD::SETCC: {
7315 assert(VT.isInteger() && "SETCC result type must be an integer!");
7316 assert(N1.getValueType() == N2.getValueType() &&
7317 "SETCC operands must have the same type!");
7318 assert(VT.isVector() == N1.getValueType().isVector() &&
7319 "SETCC type should be vector iff the operand type is vector!");
7320 assert((!VT.isVector() || VT.getVectorElementCount() ==
7322 "SETCC vector element counts must match!");
7323 // Use FoldSetCC to simplify SETCC's.
7324 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7325 return V;
7326 // Vector constant folding.
7327 SDValue Ops[] = {N1, N2, N3};
7328 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7329 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7330 return V;
7331 }
7332 break;
7333 }
7334 case ISD::SELECT:
7335 case ISD::VSELECT:
7336 if (SDValue V = simplifySelect(N1, N2, N3))
7337 return V;
7338 break;
7340 llvm_unreachable("should use getVectorShuffle constructor!");
7341 case ISD::VECTOR_SPLICE: {
7342 if (cast<ConstantSDNode>(N3)->isZero())
7343 return N1;
7344 break;
7345 }
7347 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7348 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7349 // for scalable vectors where we will generate appropriate code to
7350 // deal with out-of-bounds cases correctly.
7351 if (N3C && N1.getValueType().isFixedLengthVector() &&
7353 return getUNDEF(VT);
7354
7355 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7356 if (N3.isUndef())
7357 return getUNDEF(VT);
7358
7359 // If the inserted element is an UNDEF, just use the input vector.
7360 if (N2.isUndef())
7361 return N1;
7362
7363 break;
7364 }
7365 case ISD::INSERT_SUBVECTOR: {
7366 // Inserting undef into undef is still undef.
7367 if (N1.isUndef() && N2.isUndef())
7368 return getUNDEF(VT);
7369
7370 EVT N2VT = N2.getValueType();
7371 assert(VT == N1.getValueType() &&
7372 "Dest and insert subvector source types must match!");
7373 assert(VT.isVector() && N2VT.isVector() &&
7374 "Insert subvector VTs must be vectors!");
7376 "Insert subvector VTs must have the same element type!");
7377 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7378 "Cannot insert a scalable vector into a fixed length vector!");
7379 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7381 "Insert subvector must be from smaller vector to larger vector!");
7382 assert(isa<ConstantSDNode>(N3) &&
7383 "Insert subvector index must be constant");
7384 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7385 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7387 "Insert subvector overflow!");
7389 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7390 "Constant index for INSERT_SUBVECTOR has an invalid size");
7391
7392 // Trivial insertion.
7393 if (VT == N2VT)
7394 return N2;
7395
7396 // If this is an insert of an extracted vector into an undef vector, we
7397 // can just use the input to the extract.
7398 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7399 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7400 return N2.getOperand(0);
7401 break;
7402 }
7403 case ISD::BITCAST:
7404 // Fold bit_convert nodes from a type to themselves.
7405 if (N1.getValueType() == VT)
7406 return N1;
7407 break;
7408 case ISD::VP_TRUNCATE:
7409 case ISD::VP_SIGN_EXTEND:
7410 case ISD::VP_ZERO_EXTEND:
7411 // Don't create noop casts.
7412 if (N1.getValueType() == VT)
7413 return N1;
7414 break;
7415 }
7416
7417 // Memoize node if it doesn't produce a glue result.
7418 SDNode *N;
7419 SDVTList VTs = getVTList(VT);
7420 SDValue Ops[] = {N1, N2, N3};
7421 if (VT != MVT::Glue) {
7423 AddNodeIDNode(ID, Opcode, VTs, Ops);
7424 void *IP = nullptr;
7425 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7426 E->intersectFlagsWith(Flags);
7427 return SDValue(E, 0);
7428 }
7429
7430 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7431 N->setFlags(Flags);
7432 createOperands(N, Ops);
7433 CSEMap.InsertNode(N, IP);
7434 } else {
7435 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7436 createOperands(N, Ops);
7437 }
7438
7439 InsertNode(N);
7440 SDValue V = SDValue(N, 0);
7441 NewSDValueDbgMsg(V, "Creating new node: ", this);
7442 return V;
7443}
7444
7445SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7446 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7447 SDValue Ops[] = { N1, N2, N3, N4 };
7448 return getNode(Opcode, DL, VT, Ops);
7449}
7450
7451SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7452 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7453 SDValue N5) {
7454 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7455 return getNode(Opcode, DL, VT, Ops);
7456}
7457
7458/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7459/// the incoming stack arguments to be loaded from the stack.
7461 SmallVector<SDValue, 8> ArgChains;
7462
7463 // Include the original chain at the beginning of the list. When this is
7464 // used by target LowerCall hooks, this helps legalize find the
7465 // CALLSEQ_BEGIN node.
7466 ArgChains.push_back(Chain);
7467
7468 // Add a chain value for each stack argument.
7469 for (SDNode *U : getEntryNode().getNode()->uses())
7470 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7471 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7472 if (FI->getIndex() < 0)
7473 ArgChains.push_back(SDValue(L, 1));
7474
7475 // Build a tokenfactor for all the chains.
7476 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7477}
7478
7479/// getMemsetValue - Vectorized representation of the memset value
7480/// operand.
7482 const SDLoc &dl) {
7483 assert(!Value.isUndef());
7484
7485 unsigned NumBits = VT.getScalarSizeInBits();
7486 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7487 assert(C->getAPIntValue().getBitWidth() == 8);
7488 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7489 if (VT.isInteger()) {
7490 bool IsOpaque = VT.getSizeInBits() > 64 ||
7491 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7492 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7493 }
7494 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
7495 VT);
7496 }
7497
7498 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7499 EVT IntVT = VT.getScalarType();
7500 if (!IntVT.isInteger())
7501 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7502
7503 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7504 if (NumBits > 8) {
7505 // Use a multiplication with 0x010101... to extend the input to the
7506 // required length.
7507 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7508 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7509 DAG.getConstant(Magic, dl, IntVT));
7510 }
7511
7512 if (VT != Value.getValueType() && !VT.isInteger())
7513 Value = DAG.getBitcast(VT.getScalarType(), Value);
7514 if (VT != Value.getValueType())
7515 Value = DAG.getSplatBuildVector(VT, dl, Value);
7516
7517 return Value;
7518}
7519
7520/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
7521/// used when a memcpy is turned into a memset when the source is a constant
7522/// string ptr.
7524 const TargetLowering &TLI,
7525 const ConstantDataArraySlice &Slice) {
7526 // Handle vector with all elements zero.
7527 if (Slice.Array == nullptr) {
7528 if (VT.isInteger())
7529 return DAG.getConstant(0, dl, VT);
7530 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
7531 return DAG.getConstantFP(0.0, dl, VT);
7532 if (VT.isVector()) {
7533 unsigned NumElts = VT.getVectorNumElements();
7534 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
7535 return DAG.getNode(ISD::BITCAST, dl, VT,
7536 DAG.getConstant(0, dl,
7538 EltVT, NumElts)));
7539 }
7540 llvm_unreachable("Expected type!");
7541 }
7542
7543 assert(!VT.isVector() && "Can't handle vector type here!");
7544 unsigned NumVTBits = VT.getSizeInBits();
7545 unsigned NumVTBytes = NumVTBits / 8;
7546 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
7547
7548 APInt Val(NumVTBits, 0);
7549 if (DAG.getDataLayout().isLittleEndian()) {
7550 for (unsigned i = 0; i != NumBytes; ++i)
7551 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
7552 } else {
7553 for (unsigned i = 0; i != NumBytes; ++i)
7554 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
7555 }
7556
7557 // If the "cost" of materializing the integer immediate is less than the cost
7558 // of a load, then it is cost effective to turn the load into the immediate.
7559 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
7560 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
7561 return DAG.getConstant(Val, dl, VT);
7562 return SDValue();
7563}
7564
7566 const SDLoc &DL,
7567 const SDNodeFlags Flags) {
7568 EVT VT = Base.getValueType();
7569 SDValue Index;
7570
7571 if (Offset.isScalable())
7572 Index = getVScale(DL, Base.getValueType(),
7573 APInt(Base.getValueSizeInBits().getFixedValue(),
7574 Offset.getKnownMinValue()));
7575 else
7576 Index = getConstant(Offset.getFixedValue(), DL, VT);
7577
7578 return getMemBasePlusOffset(Base, Index, DL, Flags);
7579}
7580
7582 const SDLoc &DL,
7583 const SDNodeFlags Flags) {
7584 assert(Offset.getValueType().isInteger());
7585 EVT BasePtrVT = Ptr.getValueType();
7586 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
7587}
7588
7589/// Returns true if memcpy source is constant data.
7591 uint64_t SrcDelta = 0;
7592 GlobalAddressSDNode *G = nullptr;
7593 if (Src.getOpcode() == ISD::GlobalAddress)
7594 G = cast<GlobalAddressSDNode>(Src);
7595 else if (Src.getOpcode() == ISD::ADD &&
7596 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
7597 Src.getOperand(1).getOpcode() == ISD::Constant) {
7598 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
7599 SrcDelta = Src.getConstantOperandVal(1);
7600 }
7601 if (!G)
7602 return false;
7603
7604 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
7605 SrcDelta + G->getOffset());
7606}
7607
7609 SelectionDAG &DAG) {
7610 // On Darwin, -Os means optimize for size without hurting performance, so
7611 // only really optimize for size when -Oz (MinSize) is used.
7613 return MF.getFunction().hasMinSize();
7614 return DAG.shouldOptForSize();
7615}
7616
7618 SmallVector<SDValue, 32> &OutChains, unsigned From,
7619 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
7620 SmallVector<SDValue, 16> &OutStoreChains) {
7621 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
7622 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
7623 SmallVector<SDValue, 16> GluedLoadChains;
7624 for (unsigned i = From; i < To; ++i) {
7625 OutChains.push_back(OutLoadChains[i]);
7626 GluedLoadChains.push_back(OutLoadChains[i]);
7627 }
7628
7629 // Chain for all loads.
7630 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7631 GluedLoadChains);
7632
7633 for (unsigned i = From; i < To; ++i) {
7634 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
7635 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
7636 ST->getBasePtr(), ST->getMemoryVT(),
7637 ST->getMemOperand());
7638 OutChains.push_back(NewStore);
7639 }
7640}
7641
7643 SDValue Chain, SDValue Dst, SDValue Src,
7644 uint64_t Size, Align Alignment,
7645 bool isVol, bool AlwaysInline,
7646 MachinePointerInfo DstPtrInfo,
7647 MachinePointerInfo SrcPtrInfo,
7648 const AAMDNodes &AAInfo, AAResults *AA) {
7649 // Turn a memcpy of undef to nop.
7650 // FIXME: We need to honor volatile even is Src is undef.
7651 if (Src.isUndef())
7652 return Chain;
7653
7654 // Expand memcpy to a series of load and store ops if the size operand falls
7655 // below a certain threshold.
7656 // TODO: In the AlwaysInline case, if the size is big then generate a loop
7657 // rather than maybe a humongous number of loads and stores.
7658 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7659 const DataLayout &DL = DAG.getDataLayout();
7660 LLVMContext &C = *DAG.getContext();
7661 std::vector<EVT> MemOps;
7662 bool DstAlignCanChange = false;
7664 MachineFrameInfo &MFI = MF.getFrameInfo();
7665 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7666 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7667 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7668 DstAlignCanChange = true;
7669 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7670 if (!SrcAlign || Alignment > *SrcAlign)
7671 SrcAlign = Alignment;
7672 assert(SrcAlign && "SrcAlign must be set");
7674 // If marked as volatile, perform a copy even when marked as constant.
7675 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
7676 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
7677 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
7678 const MemOp Op = isZeroConstant
7679 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
7680 /*IsZeroMemset*/ true, isVol)
7681 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
7682 *SrcAlign, isVol, CopyFromConstant);
7683 if (!TLI.findOptimalMemOpLowering(
7684 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
7685 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
7686 return SDValue();
7687
7688 if (DstAlignCanChange) {
7689 Type *Ty = MemOps[0].getTypeForEVT(C);
7690 Align NewAlign = DL.getABITypeAlign(Ty);
7691
7692 // Don't promote to an alignment that would require dynamic stack
7693 // realignment which may conflict with optimizations such as tail call
7694 // optimization.
7696 if (!TRI->hasStackRealignment(MF))
7697 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7698 NewAlign = NewAlign.previous();
7699
7700 if (NewAlign > Alignment) {
7701 // Give the stack frame object a larger alignment if needed.
7702 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7703 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7704 Alignment = NewAlign;
7705 }
7706 }
7707
7708 // Prepare AAInfo for loads/stores after lowering this memcpy.
7709 AAMDNodes NewAAInfo = AAInfo;
7710 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7711
7712 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
7713 bool isConstant =
7714 AA && SrcVal &&
7715 AA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
7716
7717 MachineMemOperand::Flags MMOFlags =
7719 SmallVector<SDValue, 16> OutLoadChains;
7720 SmallVector<SDValue, 16> OutStoreChains;
7721 SmallVector<SDValue, 32> OutChains;
7722 unsigned NumMemOps = MemOps.size();
7723 uint64_t SrcOff = 0, DstOff = 0;
7724 for (unsigned i = 0; i != NumMemOps; ++i) {
7725 EVT VT = MemOps[i];
7726 unsigned VTSize = VT.getSizeInBits() / 8;
7727 SDValue Value, Store;
7728
7729 if (VTSize > Size) {
7730 // Issuing an unaligned load / store pair that overlaps with the previous
7731 // pair. Adjust the offset accordingly.
7732 assert(i == NumMemOps-1 && i != 0);
7733 SrcOff -= VTSize - Size;
7734 DstOff -= VTSize - Size;
7735 }
7736
7737 if (CopyFromConstant &&
7738 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
7739 // It's unlikely a store of a vector immediate can be done in a single
7740 // instruction. It would require a load from a constantpool first.
7741 // We only handle zero vectors here.
7742 // FIXME: Handle other cases where store of vector immediate is done in
7743 // a single instruction.
7744 ConstantDataArraySlice SubSlice;
7745 if (SrcOff < Slice.Length) {
7746 SubSlice = Slice;
7747 SubSlice.move(SrcOff);
7748 } else {
7749 // This is an out-of-bounds access and hence UB. Pretend we read zero.
7750 SubSlice.Array = nullptr;
7751 SubSlice.Offset = 0;
7752 SubSlice.Length = VTSize;
7753 }
7754 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
7755 if (Value.getNode()) {
7756 Store = DAG.getStore(
7757 Chain, dl, Value,
7758 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7759 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7760 OutChains.push_back(Store);
7761 }
7762 }
7763
7764 if (!Store.getNode()) {
7765 // The type might not be legal for the target. This should only happen
7766 // if the type is smaller than a legal type, as on PPC, so the right
7767 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
7768 // to Load/Store if NVT==VT.
7769 // FIXME does the case above also need this?
7770 EVT NVT = TLI.getTypeToTransformTo(C, VT);
7771 assert(NVT.bitsGE(VT));
7772
7773 bool isDereferenceable =
7774 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7775 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7776 if (isDereferenceable)
7778 if (isConstant)
7779 SrcMMOFlags |= MachineMemOperand::MOInvariant;
7780
7781 Value = DAG.getExtLoad(
7782 ISD::EXTLOAD, dl, NVT, Chain,
7783 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7784 SrcPtrInfo.getWithOffset(SrcOff), VT,
7785 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
7786 OutLoadChains.push_back(Value.getValue(1));
7787
7788 Store = DAG.getTruncStore(
7789 Chain, dl, Value,
7790 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7791 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
7792 OutStoreChains.push_back(Store);
7793 }
7794 SrcOff += VTSize;
7795 DstOff += VTSize;
7796 Size -= VTSize;
7797 }
7798
7799 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
7801 unsigned NumLdStInMemcpy = OutStoreChains.size();
7802
7803 if (NumLdStInMemcpy) {
7804 // It may be that memcpy might be converted to memset if it's memcpy
7805 // of constants. In such a case, we won't have loads and stores, but
7806 // just stores. In the absence of loads, there is nothing to gang up.
7807 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
7808 // If target does not care, just leave as it.
7809 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
7810 OutChains.push_back(OutLoadChains[i]);
7811 OutChains.push_back(OutStoreChains[i]);
7812 }
7813 } else {
7814 // Ld/St less than/equal limit set by target.
7815 if (NumLdStInMemcpy <= GluedLdStLimit) {
7816 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7817 NumLdStInMemcpy, OutLoadChains,
7818 OutStoreChains);
7819 } else {
7820 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
7821 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
7822 unsigned GlueIter = 0;
7823
7824 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
7825 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
7826 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
7827
7828 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
7829 OutLoadChains, OutStoreChains);
7830 GlueIter += GluedLdStLimit;
7831 }
7832
7833 // Residual ld/st.
7834 if (RemainingLdStInMemcpy) {
7835 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7836 RemainingLdStInMemcpy, OutLoadChains,
7837 OutStoreChains);
7838 }
7839 }
7840 }
7841 }
7842 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
7843}
7844
7846 SDValue Chain, SDValue Dst, SDValue Src,
7847 uint64_t Size, Align Alignment,
7848 bool isVol, bool AlwaysInline,
7849 MachinePointerInfo DstPtrInfo,
7850 MachinePointerInfo SrcPtrInfo,
7851 const AAMDNodes &AAInfo) {
7852 // Turn a memmove of undef to nop.
7853 // FIXME: We need to honor volatile even is Src is undef.
7854 if (Src.isUndef())
7855 return Chain;
7856
7857 // Expand memmove to a series of load and store ops if the size operand falls
7858 // below a certain threshold.
7859 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7860 const DataLayout &DL = DAG.getDataLayout();
7861 LLVMContext &C = *DAG.getContext();
7862 std::vector<EVT> MemOps;
7863 bool DstAlignCanChange = false;
7865 MachineFrameInfo &MFI = MF.getFrameInfo();
7866 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7867 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7868 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7869 DstAlignCanChange = true;
7870 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7871 if (!SrcAlign || Alignment > *SrcAlign)
7872 SrcAlign = Alignment;
7873 assert(SrcAlign && "SrcAlign must be set");
7874 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
7875 if (!TLI.findOptimalMemOpLowering(
7876 MemOps, Limit,
7877 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
7878 /*IsVolatile*/ true),
7879 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
7880 MF.getFunction().getAttributes()))
7881 return SDValue();
7882
7883 if (DstAlignCanChange) {
7884 Type *Ty = MemOps[0].getTypeForEVT(C);
7885 Align NewAlign = DL.getABITypeAlign(Ty);
7886
7887 // Don't promote to an alignment that would require dynamic stack
7888 // realignment which may conflict with optimizations such as tail call
7889 // optimization.
7891 if (!TRI->hasStackRealignment(MF))
7892 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7893 NewAlign = NewAlign.previous();
7894
7895 if (NewAlign > Alignment) {
7896 // Give the stack frame object a larger alignment if needed.
7897 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7898 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7899 Alignment = NewAlign;
7900 }
7901 }
7902
7903 // Prepare AAInfo for loads/stores after lowering this memmove.
7904 AAMDNodes NewAAInfo = AAInfo;
7905 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7906
7907 MachineMemOperand::Flags MMOFlags =
7909 uint64_t SrcOff = 0, DstOff = 0;
7910 SmallVector<SDValue, 8> LoadValues;
7911 SmallVector<SDValue, 8> LoadChains;
7912 SmallVector<SDValue, 8> OutChains;
7913 unsigned NumMemOps = MemOps.size();
7914 for (unsigned i = 0; i < NumMemOps; i++) {
7915 EVT VT = MemOps[i];
7916 unsigned VTSize = VT.getSizeInBits() / 8;
7917 SDValue Value;
7918
7919 bool isDereferenceable =
7920 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7921 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7922 if (isDereferenceable)
7924
7925 Value = DAG.getLoad(
7926 VT, dl, Chain,
7927 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7928 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
7929 LoadValues.push_back(Value);
7930 LoadChains.push_back(Value.getValue(1));
7931 SrcOff += VTSize;
7932 }
7933 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
7934 OutChains.clear();
7935 for (unsigned i = 0; i < NumMemOps; i++) {
7936 EVT VT = MemOps[i];
7937 unsigned VTSize = VT.getSizeInBits() / 8;
7938 SDValue Store;
7939
7940 Store = DAG.getStore(
7941 Chain, dl, LoadValues[i],
7942 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7943 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7944 OutChains.push_back(Store);
7945 DstOff += VTSize;
7946 }
7947
7948 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
7949}
7950
7951/// Lower the call to 'memset' intrinsic function into a series of store
7952/// operations.
7953///
7954/// \param DAG Selection DAG where lowered code is placed.
7955/// \param dl Link to corresponding IR location.
7956/// \param Chain Control flow dependency.
7957/// \param Dst Pointer to destination memory location.
7958/// \param Src Value of byte to write into the memory.
7959/// \param Size Number of bytes to write.
7960/// \param Alignment Alignment of the destination in bytes.
7961/// \param isVol True if destination is volatile.
7962/// \param AlwaysInline Makes sure no function call is generated.
7963/// \param DstPtrInfo IR information on the memory pointer.
7964/// \returns New head in the control flow, if lowering was successful, empty
7965/// SDValue otherwise.
7966///
7967/// The function tries to replace 'llvm.memset' intrinsic with several store
7968/// operations and value calculation code. This is usually profitable for small
7969/// memory size or when the semantic requires inlining.
7971 SDValue Chain, SDValue Dst, SDValue Src,
7972 uint64_t Size, Align Alignment, bool isVol,
7973 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
7974 const AAMDNodes &AAInfo) {
7975 // Turn a memset of undef to nop.
7976 // FIXME: We need to honor volatile even is Src is undef.
7977 if (Src.isUndef())
7978 return Chain;
7979
7980 // Expand memset to a series of load/store ops if the size operand
7981 // falls below a certain threshold.
7982 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7983 std::vector<EVT> MemOps;
7984 bool DstAlignCanChange = false;
7986 MachineFrameInfo &MFI = MF.getFrameInfo();
7987 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7988 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7989 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7990 DstAlignCanChange = true;
7991 bool IsZeroVal = isNullConstant(Src);
7992 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
7993
7994 if (!TLI.findOptimalMemOpLowering(
7995 MemOps, Limit,
7996 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
7997 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
7998 return SDValue();
7999
8000 if (DstAlignCanChange) {
8001 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8002 const DataLayout &DL = DAG.getDataLayout();
8003 Align NewAlign = DL.getABITypeAlign(Ty);
8004
8005 // Don't promote to an alignment that would require dynamic stack
8006 // realignment which may conflict with optimizations such as tail call
8007 // optimization.
8009 if (!TRI->hasStackRealignment(MF))
8010 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8011 NewAlign = NewAlign.previous();
8012
8013 if (NewAlign > Alignment) {
8014 // Give the stack frame object a larger alignment if needed.
8015 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8016 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8017 Alignment = NewAlign;
8018 }
8019 }
8020
8021 SmallVector<SDValue, 8> OutChains;
8022 uint64_t DstOff = 0;
8023 unsigned NumMemOps = MemOps.size();
8024
8025 // Find the largest store and generate the bit pattern for it.
8026 EVT LargestVT = MemOps[0];
8027 for (unsigned i = 1; i < NumMemOps; i++)
8028 if (MemOps[i].bitsGT(LargestVT))
8029 LargestVT = MemOps[i];
8030 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8031
8032 // Prepare AAInfo for loads/stores after lowering this memset.
8033 AAMDNodes NewAAInfo = AAInfo;
8034 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8035
8036 for (unsigned i = 0; i < NumMemOps; i++) {
8037 EVT VT = MemOps[i];
8038 unsigned VTSize = VT.getSizeInBits() / 8;
8039 if (VTSize > Size) {
8040 // Issuing an unaligned load / store pair that overlaps with the previous
8041 // pair. Adjust the offset accordingly.
8042 assert(i == NumMemOps-1 && i != 0);
8043 DstOff -= VTSize - Size;
8044 }
8045
8046 // If this store is smaller than the largest store see whether we can get
8047 // the smaller value for free with a truncate or extract vector element and
8048 // then store.
8049 SDValue Value = MemSetValue;
8050 if (VT.bitsLT(LargestVT)) {
8051 unsigned Index;
8052 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8053 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8054 if (!LargestVT.isVector() && !VT.isVector() &&
8055 TLI.isTruncateFree(LargestVT, VT))
8056 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8057 else if (LargestVT.isVector() && !VT.isVector() &&
8059 LargestVT.getTypeForEVT(*DAG.getContext()),
8060 VT.getSizeInBits(), Index) &&
8061 TLI.isTypeLegal(SVT) &&
8062 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8063 // Target which can combine store(extractelement VectorTy, Idx) can get
8064 // the smaller value for free.
8065 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8066 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
8067 DAG.getVectorIdxConstant(Index, dl));
8068 } else
8069 Value = getMemsetValue(Src, VT, DAG, dl);
8070 }
8071 assert(Value.getValueType() == VT && "Value with wrong type.");
8072 SDValue Store = DAG.getStore(
8073 Chain, dl, Value,
8074 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8075 DstPtrInfo.getWithOffset(DstOff), Alignment,
8077 NewAAInfo);
8078 OutChains.push_back(Store);
8079 DstOff += VT.getSizeInBits() / 8;
8080 Size -= VTSize;
8081 }
8082
8083 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8084}
8085
8087 unsigned AS) {
8088 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8089 // pointer operands can be losslessly bitcasted to pointers of address space 0
8090 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8091 report_fatal_error("cannot lower memory intrinsic in address space " +
8092 Twine(AS));
8093 }
8094}
8095
8097 SDValue Src, SDValue Size, Align Alignment,
8098 bool isVol, bool AlwaysInline, bool isTailCall,
8099 MachinePointerInfo DstPtrInfo,
8100 MachinePointerInfo SrcPtrInfo,
8101 const AAMDNodes &AAInfo, AAResults *AA) {
8102 // Check to see if we should lower the memcpy to loads and stores first.
8103 // For cases within the target-specified limits, this is the best choice.
8104 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8105 if (ConstantSize) {
8106 // Memcpy with size zero? Just return the original chain.
8107 if (ConstantSize->isZero())
8108 return Chain;
8109
8111 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8112 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8113 if (Result.getNode())
8114 return Result;
8115 }
8116
8117 // Then check to see if we should lower the memcpy with target-specific
8118 // code. If the target chooses to do this, this is the next best.
8119 if (TSI) {
8120 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8121 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8122 DstPtrInfo, SrcPtrInfo);
8123 if (Result.getNode())
8124 return Result;
8125 }
8126
8127 // If we really need inline code and the target declined to provide it,
8128 // use a (potentially long) sequence of loads and stores.
8129 if (AlwaysInline) {
8130 assert(ConstantSize && "AlwaysInline requires a constant size!");
8132 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8133 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8134 }
8135
8138
8139 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8140 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8141 // respect volatile, so they may do things like read or write memory
8142 // beyond the given memory regions. But fixing this isn't easy, and most
8143 // people don't care.
8144
8145 // Emit a library call.
8148 Entry.Ty = PointerType::getUnqual(*getContext());
8149 Entry.Node = Dst; Args.push_back(Entry);
8150 Entry.Node = Src; Args.push_back(Entry);
8151
8152 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8153 Entry.Node = Size; Args.push_back(Entry);
8154 // FIXME: pass in SDLoc
8156 CLI.setDebugLoc(dl)
8157 .setChain(Chain)
8158 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8159 Dst.getValueType().getTypeForEVT(*getContext()),
8160 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8161 TLI->getPointerTy(getDataLayout())),
8162 std::move(Args))
8164 .setTailCall(isTailCall);
8165
8166 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8167 return CallResult.second;
8168}
8169
8171 SDValue Dst, SDValue Src, SDValue Size,
8172 Type *SizeTy, unsigned ElemSz,
8173 bool isTailCall,
8174 MachinePointerInfo DstPtrInfo,
8175 MachinePointerInfo SrcPtrInfo) {
8176 // Emit a library call.
8179 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8180 Entry.Node = Dst;
8181 Args.push_back(Entry);
8182
8183 Entry.Node = Src;
8184 Args.push_back(Entry);
8185
8186 Entry.Ty = SizeTy;
8187 Entry.Node = Size;
8188 Args.push_back(Entry);
8189
8190 RTLIB::Libcall LibraryCall =
8192 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8193 report_fatal_error("Unsupported element size");
8194
8196 CLI.setDebugLoc(dl)
8197 .setChain(Chain)
8198 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8200 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8201 TLI->getPointerTy(getDataLayout())),
8202 std::move(Args))
8204 .setTailCall(isTailCall);
8205
8206 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8207 return CallResult.second;
8208}
8209
8211 SDValue Src, SDValue Size, Align Alignment,
8212 bool isVol, bool isTailCall,
8213 MachinePointerInfo DstPtrInfo,
8214 MachinePointerInfo SrcPtrInfo,
8215 const AAMDNodes &AAInfo, AAResults *AA) {
8216 // Check to see if we should lower the memmove to loads and stores first.
8217 // For cases within the target-specified limits, this is the best choice.
8218 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8219 if (ConstantSize) {
8220 // Memmove with size zero? Just return the original chain.
8221 if (ConstantSize->isZero())
8222 return Chain;
8223
8225 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8226 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8227 if (Result.getNode())
8228 return Result;
8229 }
8230
8231 // Then check to see if we should lower the memmove with target-specific
8232 // code. If the target chooses to do this, this is the next best.
8233 if (TSI) {
8234 SDValue Result =
8235 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8236 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8237 if (Result.getNode())
8238 return Result;
8239 }
8240
8243
8244 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8245 // not be safe. See memcpy above for more details.
8246
8247 // Emit a library call.
8250 Entry.Ty = PointerType::getUnqual(*getContext());
8251 Entry.Node = Dst; Args.push_back(Entry);
8252 Entry.Node = Src; Args.push_back(Entry);
8253
8254 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8255 Entry.Node = Size; Args.push_back(Entry);
8256 // FIXME: pass in SDLoc
8258 CLI.setDebugLoc(dl)
8259 .setChain(Chain)
8260 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8261 Dst.getValueType().getTypeForEVT(*getContext()),
8262 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8263 TLI->getPointerTy(getDataLayout())),
8264 std::move(Args))
8266 .setTailCall(isTailCall);
8267
8268 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8269 return CallResult.second;
8270}
8271
8273 SDValue Dst, SDValue Src, SDValue Size,
8274 Type *SizeTy, unsigned ElemSz,
8275 bool isTailCall,
8276 MachinePointerInfo DstPtrInfo,
8277 MachinePointerInfo SrcPtrInfo) {
8278 // Emit a library call.
8281 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8282 Entry.Node = Dst;
8283 Args.push_back(Entry);
8284
8285 Entry.Node = Src;
8286 Args.push_back(Entry);
8287
8288 Entry.Ty = SizeTy;
8289 Entry.Node = Size;
8290 Args.push_back(Entry);
8291
8292 RTLIB::Libcall LibraryCall =
8294 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8295 report_fatal_error("Unsupported element size");
8296
8298 CLI.setDebugLoc(dl)
8299 .setChain(Chain)
8300 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8302 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8303 TLI->getPointerTy(getDataLayout())),
8304 std::move(Args))
8306 .setTailCall(isTailCall);
8307
8308 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8309 return CallResult.second;
8310}
8311
8313 SDValue Src, SDValue Size, Align Alignment,
8314 bool isVol, bool AlwaysInline, bool isTailCall,
8315 MachinePointerInfo DstPtrInfo,
8316 const AAMDNodes &AAInfo) {
8317 // Check to see if we should lower the memset to stores first.
8318 // For cases within the target-specified limits, this is the best choice.
8319 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8320 if (ConstantSize) {
8321 // Memset with size zero? Just return the original chain.
8322 if (ConstantSize->isZero())
8323 return Chain;
8324
8325 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8326 ConstantSize->getZExtValue(), Alignment,
8327 isVol, false, DstPtrInfo, AAInfo);
8328
8329 if (Result.getNode())
8330 return Result;
8331 }
8332
8333 // Then check to see if we should lower the memset with target-specific
8334 // code. If the target chooses to do this, this is the next best.
8335 if (TSI) {
8336 SDValue Result = TSI->EmitTargetCodeForMemset(
8337 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8338 if (Result.getNode())
8339 return Result;
8340 }
8341
8342 // If we really need inline code and the target declined to provide it,
8343 // use a (potentially long) sequence of loads and stores.
8344 if (AlwaysInline) {
8345 assert(ConstantSize && "AlwaysInline requires a constant size!");
8346 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8347 ConstantSize->getZExtValue(), Alignment,
8348 isVol, true, DstPtrInfo, AAInfo);
8349 assert(Result &&
8350 "getMemsetStores must return a valid sequence when AlwaysInline");
8351 return Result;
8352 }
8353
8355
8356 // Emit a library call.
8357 auto &Ctx = *getContext();
8358 const auto& DL = getDataLayout();
8359
8361 // FIXME: pass in SDLoc
8362 CLI.setDebugLoc(dl).setChain(Chain);
8363
8364 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8365
8366 // Helper function to create an Entry from Node and Type.
8367 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8369 Entry.Node = Node;
8370 Entry.Ty = Ty;
8371 return Entry;
8372 };
8373
8374 // If zeroing out and bzero is present, use it.
8375 if (isNullConstant(Src) && BzeroName) {
8377 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8378 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8379 CLI.setLibCallee(
8380 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8381 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8382 } else {
8384 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8385 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8386 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8387 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8388 Dst.getValueType().getTypeForEVT(Ctx),
8389 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8390 TLI->getPointerTy(DL)),
8391 std::move(Args));
8392 }
8393
8394 CLI.setDiscardResult().setTailCall(isTailCall);
8395
8396 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8397 return CallResult.second;
8398}
8399
8402 Type *SizeTy, unsigned ElemSz,
8403 bool isTailCall,
8404 MachinePointerInfo DstPtrInfo) {
8405 // Emit a library call.
8408 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8409 Entry.Node = Dst;
8410 Args.push_back(Entry);
8411
8412 Entry.Ty = Type::getInt8Ty(*getContext());
8413 Entry.Node = Value;
8414 Args.push_back(Entry);
8415
8416 Entry.Ty = SizeTy;
8417 Entry.Node = Size;
8418 Args.push_back(Entry);
8419
8420 RTLIB::Libcall LibraryCall =
8422 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8423 report_fatal_error("Unsupported element size");
8424
8426 CLI.setDebugLoc(dl)
8427 .setChain(Chain)
8428 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8430 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8431 TLI->getPointerTy(getDataLayout())),
8432 std::move(Args))
8434 .setTailCall(isTailCall);
8435
8436 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8437 return CallResult.second;
8438}
8439
8440SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8441 SDVTList VTList, ArrayRef<SDValue> Ops,
8442 MachineMemOperand *MMO) {
8444 ID.AddInteger(MemVT.getRawBits());
8445 AddNodeIDNode(ID, Opcode, VTList, Ops);
8446 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8447 ID.AddInteger(MMO->getFlags());
8448 void* IP = nullptr;
8449 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8450 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8451 return SDValue(E, 0);
8452 }
8453
8454 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8455 VTList, MemVT, MMO);
8456 createOperands(N, Ops);
8457
8458 CSEMap.InsertNode(N, IP);
8459 InsertNode(N);
8460 return SDValue(N, 0);
8461}
8462
8464 EVT MemVT, SDVTList VTs, SDValue Chain,
8465 SDValue Ptr, SDValue Cmp, SDValue Swp,
8466 MachineMemOperand *MMO) {
8467 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8469 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8470
8471 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8472 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8473}
8474
8475SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8476 SDValue Chain, SDValue Ptr, SDValue Val,
8477 MachineMemOperand *MMO) {
8478 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
8479 Opcode == ISD::ATOMIC_LOAD_SUB ||
8480 Opcode == ISD::ATOMIC_LOAD_AND ||
8481 Opcode == ISD::ATOMIC_LOAD_CLR ||
8482 Opcode == ISD::ATOMIC_LOAD_OR ||
8483 Opcode == ISD::ATOMIC_LOAD_XOR ||
8484 Opcode == ISD::ATOMIC_LOAD_NAND ||
8485 Opcode == ISD::ATOMIC_LOAD_MIN ||
8486 Opcode == ISD::ATOMIC_LOAD_MAX ||
8487 Opcode == ISD::ATOMIC_LOAD_UMIN ||
8488 Opcode == ISD::ATOMIC_LOAD_UMAX ||
8489 Opcode == ISD::ATOMIC_LOAD_FADD ||
8490 Opcode == ISD::ATOMIC_LOAD_FSUB ||
8491 Opcode == ISD::ATOMIC_LOAD_FMAX ||
8492 Opcode == ISD::ATOMIC_LOAD_FMIN ||
8493 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
8494 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
8495 Opcode == ISD::ATOMIC_SWAP ||
8496 Opcode == ISD::ATOMIC_STORE) &&
8497 "Invalid Atomic Op");
8498
8499 EVT VT = Val.getValueType();
8500
8501 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
8502 getVTList(VT, MVT::Other);
8503 SDValue Ops[] = {Chain, Ptr, Val};
8504 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8505}
8506
8507SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8508 EVT VT, SDValue Chain, SDValue Ptr,
8509 MachineMemOperand *MMO) {
8510 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
8511
8512 SDVTList VTs = getVTList(VT, MVT::Other);
8513 SDValue Ops[] = {Chain, Ptr};
8514 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8515}
8516
8517/// getMergeValues - Create a MERGE_VALUES node from the given operands.
8519 if (Ops.size() == 1)
8520 return Ops[0];
8521
8523 VTs.reserve(Ops.size());
8524 for (const SDValue &Op : Ops)
8525 VTs.push_back(Op.getValueType());
8526 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
8527}
8528
8530 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
8531 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
8533 const AAMDNodes &AAInfo) {
8534 if (Size.hasValue() && !Size.getValue())
8536
8538 MachineMemOperand *MMO =
8539 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
8540
8541 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
8542}
8543
8545 SDVTList VTList,
8546 ArrayRef<SDValue> Ops, EVT MemVT,
8547 MachineMemOperand *MMO) {
8548 assert((Opcode == ISD::INTRINSIC_VOID ||
8549 Opcode == ISD::INTRINSIC_W_CHAIN ||
8550 Opcode == ISD::PREFETCH ||
8551 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
8552 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
8553 "Opcode is not a memory-accessing opcode!");
8554
8555 // Memoize the node unless it returns a glue result.
8557 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
8559 AddNodeIDNode(ID, Opcode, VTList, Ops);
8560 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
8561 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
8562 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8563 ID.AddInteger(MMO->getFlags());
8564 ID.AddInteger(MemVT.getRawBits());
8565 void *IP = nullptr;
8566 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8567 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
8568 return SDValue(E, 0);
8569 }
8570
8571 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8572 VTList, MemVT, MMO);
8573 createOperands(N, Ops);
8574
8575 CSEMap.InsertNode(N, IP);
8576 } else {
8577 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8578 VTList, MemVT, MMO);
8579 createOperands(N, Ops);
8580 }
8581 InsertNode(N);
8582 SDValue V(N, 0);
8583 NewSDValueDbgMsg(V, "Creating new node: ", this);
8584 return V;
8585}
8586
8588 SDValue Chain, int FrameIndex,
8589 int64_t Size, int64_t Offset) {
8590 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
8591 const auto VTs = getVTList(MVT::Other);
8592 SDValue Ops[2] = {
8593 Chain,
8594 getFrameIndex(FrameIndex,
8595 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
8596 true)};
8597
8599 AddNodeIDNode(ID, Opcode, VTs, Ops);
8600 ID.AddInteger(FrameIndex);
8601 ID.AddInteger(Size);
8602 ID.AddInteger(Offset);
8603 void *IP = nullptr;
8604 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8605 return SDValue(E, 0);
8606
8607 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
8608 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
8609 createOperands(N, Ops);
8610 CSEMap.InsertNode(N, IP);
8611 InsertNode(N);
8612 SDValue V(N, 0);
8613 NewSDValueDbgMsg(V, "Creating new node: ", this);
8614 return V;
8615}
8616
8618 uint64_t Guid, uint64_t Index,
8619 uint32_t Attr) {
8620 const unsigned Opcode = ISD::PSEUDO_PROBE;
8621 const auto VTs = getVTList(MVT::Other);
8622 SDValue Ops[] = {Chain};
8624 AddNodeIDNode(ID, Opcode, VTs, Ops);
8625 ID.AddInteger(Guid);
8626 ID.AddInteger(Index);
8627 void *IP = nullptr;
8628 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
8629 return SDValue(E, 0);
8630
8631 auto *N = newSDNode<PseudoProbeSDNode>(
8632 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
8633 createOperands(N, Ops);
8634 CSEMap.InsertNode(N, IP);
8635 InsertNode(N);
8636 SDValue V(N, 0);
8637 NewSDValueDbgMsg(V, "Creating new node: ", this);
8638 return V;
8639}
8640
8641/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8642/// MachinePointerInfo record from it. This is particularly useful because the
8643/// code generator has many cases where it doesn't bother passing in a
8644/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8646 SelectionDAG &DAG, SDValue Ptr,
8647 int64_t Offset = 0) {
8648 // If this is FI+Offset, we can model it.
8649 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
8651 FI->getIndex(), Offset);
8652
8653 // If this is (FI+Offset1)+Offset2, we can model it.
8654 if (Ptr.getOpcode() != ISD::ADD ||
8655 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
8656 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
8657 return Info;
8658
8659 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
8661 DAG.getMachineFunction(), FI,
8662 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
8663}
8664
8665/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8666/// MachinePointerInfo record from it. This is particularly useful because the
8667/// code generator has many cases where it doesn't bother passing in a
8668/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8670 SelectionDAG &DAG, SDValue Ptr,
8671 SDValue OffsetOp) {
8672 // If the 'Offset' value isn't a constant, we can't handle this.
8673 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
8674 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
8675 if (OffsetOp.isUndef())
8676 return InferPointerInfo(Info, DAG, Ptr);
8677 return Info;
8678}
8679
8681 EVT VT, const SDLoc &dl, SDValue Chain,
8683 MachinePointerInfo PtrInfo, EVT MemVT,
8684 Align Alignment,
8685 MachineMemOperand::Flags MMOFlags,
8686 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8687 assert(Chain.getValueType() == MVT::Other &&
8688 "Invalid chain type");
8689
8690 MMOFlags |= MachineMemOperand::MOLoad;
8691 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8692 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8693 // clients.
8694 if (PtrInfo.V.isNull())
8695 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8696
8699 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8700 Alignment, AAInfo, Ranges);
8701 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
8702}
8703
8705 EVT VT, const SDLoc &dl, SDValue Chain,
8706 SDValue Ptr, SDValue Offset, EVT MemVT,
8707 MachineMemOperand *MMO) {
8708 if (VT == MemVT) {
8709 ExtType = ISD::NON_EXTLOAD;
8710 } else if (ExtType == ISD::NON_EXTLOAD) {
8711 assert(VT == MemVT && "Non-extending load from different memory type!");
8712 } else {
8713 // Extending load.
8714 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
8715 "Should only be an extending load, not truncating!");
8716 assert(VT.isInteger() == MemVT.isInteger() &&
8717 "Cannot convert from FP to Int or Int -> FP!");
8718 assert(VT.isVector() == MemVT.isVector() &&
8719 "Cannot use an ext load to convert to or from a vector!");
8720 assert((!VT.isVector() ||
8722 "Cannot use an ext load to change the number of vector elements!");
8723 }
8724
8725 bool Indexed = AM != ISD::UNINDEXED;
8726 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8727
8728 SDVTList VTs = Indexed ?
8729 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
8730 SDValue Ops[] = { Chain, Ptr, Offset };
8732 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
8733 ID.AddInteger(MemVT.getRawBits());
8734 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
8735 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
8736 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8737 ID.AddInteger(MMO->getFlags());
8738 void *IP = nullptr;
8739 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8740 cast<LoadSDNode>(E)->refineAlignment(MMO);
8741 return SDValue(E, 0);
8742 }
8743 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8744 ExtType, MemVT, MMO);
8745 createOperands(N, Ops);
8746
8747 CSEMap.InsertNode(N, IP);
8748 InsertNode(N);
8749 SDValue V(N, 0);
8750 NewSDValueDbgMsg(V, "Creating new node: ", this);
8751 return V;
8752}
8753
8756 MaybeAlign Alignment,
8757 MachineMemOperand::Flags MMOFlags,
8758 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8759 SDValue Undef = getUNDEF(Ptr.getValueType());
8760 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8761 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
8762}
8763
8766 SDValue Undef = getUNDEF(Ptr.getValueType());
8767 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8768 VT, MMO);
8769}
8770
8772 EVT VT, SDValue Chain, SDValue Ptr,
8773 MachinePointerInfo PtrInfo, EVT MemVT,
8774 MaybeAlign Alignment,
8775 MachineMemOperand::Flags MMOFlags,
8776 const AAMDNodes &AAInfo) {
8777 SDValue Undef = getUNDEF(Ptr.getValueType());
8778 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
8779 MemVT, Alignment, MMOFlags, AAInfo);
8780}
8781
8783 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
8784 MachineMemOperand *MMO) {
8785 SDValue Undef = getUNDEF(Ptr.getValueType());
8786 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
8787 MemVT, MMO);
8788}
8789
8793 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
8794 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
8795 // Don't propagate the invariant or dereferenceable flags.
8796 auto MMOFlags =
8797 LD->getMemOperand()->getFlags() &
8799 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
8800 LD->getChain(), Base, Offset, LD->getPointerInfo(),
8801 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
8802}
8803
8806 Align Alignment,
8807 MachineMemOperand::Flags MMOFlags,
8808 const AAMDNodes &AAInfo) {
8809 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
8810
8811 MMOFlags |= MachineMemOperand::MOStore;
8812 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
8813
8814 if (PtrInfo.V.isNull())
8815 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
8816
8819 MachineMemOperand *MMO =
8820 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
8821 return getStore(Chain, dl, Val, Ptr, MMO);
8822}
8823
8826 assert(Chain.getValueType() == MVT::Other &&
8827 "Invalid chain type");
8828 EVT VT = Val.getValueType();
8829 SDVTList VTs = getVTList(MVT::Other);
8830 SDValue Undef = getUNDEF(Ptr.getValueType());
8831 SDValue Ops[] = { Chain, Val, Ptr, Undef };
8833 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8834 ID.AddInteger(VT.getRawBits());
8835 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
8836 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
8837 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8838 ID.AddInteger(MMO->getFlags());
8839 void *IP = nullptr;
8840 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8841 cast<StoreSDNode>(E)->refineAlignment(MMO);
8842 return SDValue(E, 0);
8843 }
8844 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
8845 ISD::UNINDEXED, false, VT, MMO);
8846 createOperands(N, Ops);
8847
8848 CSEMap.InsertNode(N, IP);
8849 InsertNode(N);
8850 SDValue V(N, 0);
8851 NewSDValueDbgMsg(V, "Creating new node: ", this);
8852 return V;
8853}
8854
8857 EVT SVT, Align Alignment,
8858 MachineMemOperand::Flags MMOFlags,
8859 const AAMDNodes &AAInfo) {
8860 assert(Chain.getValueType() == MVT::Other &&
8861 "Invalid chain type");
8862
8863 MMOFlags |= MachineMemOperand::MOStore;
8864 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
8865
8866 if (PtrInfo.V.isNull())
8867 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
8868
8871 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
8872 AAInfo);
8873 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
8874}
8875
8877 SDValue Ptr, EVT SVT,
8878 MachineMemOperand *MMO) {
8879 EVT VT = Val.getValueType();
8880
8881 assert(Chain.getValueType() == MVT::Other &&
8882 "Invalid chain type");
8883 if (VT == SVT)
8884 return getStore(Chain, dl, Val, Ptr, MMO);
8885
8887 "Should only be a truncating store, not extending!");
8888 assert(VT.isInteger() == SVT.isInteger() &&
8889 "Can't do FP-INT conversion!");
8890 assert(VT.isVector() == SVT.isVector() &&
8891 "Cannot use trunc store to convert to or from a vector!");
8892 assert((!VT.isVector() ||
8894 "Cannot use trunc store to change the number of vector elements!");
8895
8896 SDVTList VTs = getVTList(MVT::Other);
8897 SDValue Undef = getUNDEF(Ptr.getValueType());
8898 SDValue Ops[] = { Chain, Val, Ptr, Undef };
8900 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8901 ID.AddInteger(SVT.getRawBits());
8902 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
8903 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
8904 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8905 ID.AddInteger(MMO->getFlags());
8906 void *IP = nullptr;
8907 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8908 cast<StoreSDNode>(E)->refineAlignment(MMO);
8909 return SDValue(E, 0);
8910 }
8911 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
8912 ISD::UNINDEXED, true, SVT, MMO);
8913 createOperands(N, Ops);
8914
8915 CSEMap.InsertNode(N, IP);
8916 InsertNode(N);
8917 SDValue V(N, 0);
8918 NewSDValueDbgMsg(V, "Creating new node: ", this);
8919 return V;
8920}
8921
8925 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
8926 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
8927 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
8928 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
8930 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8931 ID.AddInteger(ST->getMemoryVT().getRawBits());
8932 ID.AddInteger(ST->getRawSubclassData());
8933 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
8934 ID.AddInteger(ST->getMemOperand()->getFlags());
8935 void *IP = nullptr;
8936 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8937 return SDValue(E, 0);
8938
8939 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8940 ST->isTruncatingStore(), ST->getMemoryVT(),
8941 ST->getMemOperand());
8942 createOperands(N, Ops);
8943
8944 CSEMap.InsertNode(N, IP);
8945 InsertNode(N);
8946 SDValue V(N, 0);
8947 NewSDValueDbgMsg(V, "Creating new node: ", this);
8948 return V;
8949}
8950
8952 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
8953 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
8954 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
8955 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
8956 const MDNode *Ranges, bool IsExpanding) {
8957 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
8958
8959 MMOFlags |= MachineMemOperand::MOLoad;
8960 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8961 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8962 // clients.
8963 if (PtrInfo.V.isNull())
8964 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8965
8968 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8969 Alignment, AAInfo, Ranges);
8970 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
8971 MMO, IsExpanding);
8972}
8973
8975 ISD::LoadExtType ExtType, EVT VT,
8976 const SDLoc &dl, SDValue Chain, SDValue Ptr,
8977 SDValue Offset, SDValue Mask, SDValue EVL,
8978 EVT MemVT, MachineMemOperand *MMO,
8979 bool IsExpanding) {
8980 bool Indexed = AM != ISD::UNINDEXED;
8981 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8982
8983 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
8984 : getVTList(VT, MVT::Other);
8985 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
8987 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
8988 ID.AddInteger(MemVT.getRawBits());
8989 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
8990 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
8991 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8992 ID.AddInteger(MMO->getFlags());
8993 void *IP = nullptr;
8994 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8995 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
8996 return SDValue(E, 0);
8997 }
8998 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8999 ExtType, IsExpanding, MemVT, MMO);
9000 createOperands(N, Ops);
9001
9002 CSEMap.InsertNode(N, IP);
9003 InsertNode(N);
9004 SDValue V(N, 0);
9005 NewSDValueDbgMsg(V, "Creating new node: ", this);
9006 return V;
9007}
9008
9010 SDValue Ptr, SDValue Mask, SDValue EVL,
9011 MachinePointerInfo PtrInfo,
9012 MaybeAlign Alignment,
9013 MachineMemOperand::Flags MMOFlags,
9014 const AAMDNodes &AAInfo, const MDNode *Ranges,
9015 bool IsExpanding) {
9016 SDValue Undef = getUNDEF(Ptr.getValueType());
9017 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9018 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9019 IsExpanding);
9020}
9021
9023 SDValue Ptr, SDValue Mask, SDValue EVL,
9024 MachineMemOperand *MMO, bool IsExpanding) {
9025 SDValue Undef = getUNDEF(Ptr.getValueType());
9026 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9027 Mask, EVL, VT, MMO, IsExpanding);
9028}
9029
9031 EVT VT, SDValue Chain, SDValue Ptr,
9032 SDValue Mask, SDValue EVL,
9033 MachinePointerInfo PtrInfo, EVT MemVT,
9034 MaybeAlign Alignment,
9035 MachineMemOperand::Flags MMOFlags,
9036 const AAMDNodes &AAInfo, bool IsExpanding) {
9037 SDValue Undef = getUNDEF(Ptr.getValueType());
9038 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9039 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9040 IsExpanding);
9041}
9042
9044 EVT VT, SDValue Chain, SDValue Ptr,
9045 SDValue Mask, SDValue EVL, EVT MemVT,
9046 MachineMemOperand *MMO, bool IsExpanding) {
9047 SDValue Undef = getUNDEF(Ptr.getValueType());
9048 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9049 EVL, MemVT, MMO, IsExpanding);
9050}
9051
9055 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9056 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9057 // Don't propagate the invariant or dereferenceable flags.
9058 auto MMOFlags =
9059 LD->getMemOperand()->getFlags() &
9061 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9062 LD->getChain(), Base, Offset, LD->getMask(),
9063 LD->getVectorLength(), LD->getPointerInfo(),
9064 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
9065 nullptr, LD->isExpandingLoad());
9066}
9067
9070 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
9071 ISD::MemIndexedMode AM, bool IsTruncating,
9072 bool IsCompressing) {
9073 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9074 bool Indexed = AM != ISD::UNINDEXED;
9075 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9076 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9077 : getVTList(MVT::Other);
9078 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9080 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9081 ID.AddInteger(MemVT.getRawBits());
9082 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9083 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9084 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9085 ID.AddInteger(MMO->getFlags());
9086 void *IP = nullptr;
9087 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9088 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9089 return SDValue(E, 0);
9090 }
9091 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9092 IsTruncating, IsCompressing, MemVT, MMO);
9093 createOperands(N, Ops);
9094
9095 CSEMap.InsertNode(N, IP);
9096 InsertNode(N);
9097 SDValue V(N, 0);
9098 NewSDValueDbgMsg(V, "Creating new node: ", this);
9099 return V;
9100}
9101
9103 SDValue Val, SDValue Ptr, SDValue Mask,
9104 SDValue EVL, MachinePointerInfo PtrInfo,
9105 EVT SVT, Align Alignment,
9106 MachineMemOperand::Flags MMOFlags,
9107 const AAMDNodes &AAInfo,
9108 bool IsCompressing) {
9109 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9110
9111 MMOFlags |= MachineMemOperand::MOStore;
9112 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9113
9114 if (PtrInfo.V.isNull())
9115 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9116
9119 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9120 AAInfo);
9121 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9122 IsCompressing);
9123}
9124
9126 SDValue Val, SDValue Ptr, SDValue Mask,
9127 SDValue EVL, EVT SVT,
9128 MachineMemOperand *MMO,
9129 bool IsCompressing) {
9130 EVT VT = Val.getValueType();
9131
9132 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9133 if (VT == SVT)
9134 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9135 EVL, VT, MMO, ISD::UNINDEXED,
9136 /*IsTruncating*/ false, IsCompressing);
9137
9139 "Should only be a truncating store, not extending!");
9140 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9141 assert(VT.isVector() == SVT.isVector() &&
9142 "Cannot use trunc store to convert to or from a vector!");
9143 assert((!VT.isVector() ||
9145 "Cannot use trunc store to change the number of vector elements!");
9146
9147 SDVTList VTs = getVTList(MVT::Other);
9148 SDValue Undef = getUNDEF(Ptr.getValueType());
9149 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9151 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9152 ID.AddInteger(SVT.getRawBits());
9153 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9154 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9155 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9156 ID.AddInteger(MMO->getFlags());
9157 void *IP = nullptr;
9158 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9159 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9160 return SDValue(E, 0);
9161 }
9162 auto *N =
9163 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9164 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
9165 createOperands(N, Ops);
9166
9167 CSEMap.InsertNode(N, IP);
9168 InsertNode(N);
9169 SDValue V(N, 0);
9170 NewSDValueDbgMsg(V, "Creating new node: ", this);
9171 return V;
9172}
9173
9177 auto *ST = cast<VPStoreSDNode>(OrigStore);
9178 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9179 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9180 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9181 Offset, ST->getMask(), ST->getVectorLength()};
9183 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9184 ID.AddInteger(ST->getMemoryVT().getRawBits());
9185 ID.AddInteger(ST->getRawSubclassData());
9186 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9187 ID.AddInteger(ST->getMemOperand()->getFlags());
9188 void *IP = nullptr;
9189 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9190 return SDValue(E, 0);
9191
9192 auto *N = newSDNode<VPStoreSDNode>(
9193 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9194 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9195 createOperands(N, Ops);
9196
9197 CSEMap.InsertNode(N, IP);
9198 InsertNode(N);
9199 SDValue V(N, 0);
9200 NewSDValueDbgMsg(V, "Creating new node: ", this);
9201 return V;
9202}
9203
9205 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9206 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9207 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9208 bool Indexed = AM != ISD::UNINDEXED;
9209 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9210
9211 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9212 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9213 : getVTList(VT, MVT::Other);
9215 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9216 ID.AddInteger(VT.getRawBits());
9217 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9218 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9219 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9220
9221 void *IP = nullptr;
9222 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9223 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9224 return SDValue(E, 0);
9225 }
9226
9227 auto *N =
9228 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9229 ExtType, IsExpanding, MemVT, MMO);
9230 createOperands(N, Ops);
9231 CSEMap.InsertNode(N, IP);
9232 InsertNode(N);
9233 SDValue V(N, 0);
9234 NewSDValueDbgMsg(V, "Creating new node: ", this);
9235 return V;
9236}
9237
9239 SDValue Ptr, SDValue Stride,
9240 SDValue Mask, SDValue EVL,
9241 MachineMemOperand *MMO,
9242 bool IsExpanding) {
9243 SDValue Undef = getUNDEF(Ptr.getValueType());
9245 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9246}
9247
9249 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9250 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9251 MachineMemOperand *MMO, bool IsExpanding) {
9252 SDValue Undef = getUNDEF(Ptr.getValueType());
9253 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9254 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9255}
9256
9258 SDValue Val, SDValue Ptr,
9259 SDValue Offset, SDValue Stride,
9260 SDValue Mask, SDValue EVL, EVT MemVT,
9261 MachineMemOperand *MMO,
9263 bool IsTruncating, bool IsCompressing) {
9264 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9265 bool Indexed = AM != ISD::UNINDEXED;
9266 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9267 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9268 : getVTList(MVT::Other);
9269 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9271 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9272 ID.AddInteger(MemVT.getRawBits());
9273 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9274 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9275 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9276 void *IP = nullptr;
9277 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9278 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9279 return SDValue(E, 0);
9280 }
9281 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9282 VTs, AM, IsTruncating,
9283 IsCompressing, MemVT, MMO);
9284 createOperands(N, Ops);
9285
9286 CSEMap.InsertNode(N, IP);
9287 InsertNode(N);
9288 SDValue V(N, 0);
9289 NewSDValueDbgMsg(V, "Creating new node: ", this);
9290 return V;
9291}
9292
9294 SDValue Val, SDValue Ptr,
9295 SDValue Stride, SDValue Mask,
9296 SDValue EVL, EVT SVT,
9297 MachineMemOperand *MMO,
9298 bool IsCompressing) {
9299 EVT VT = Val.getValueType();
9300
9301 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9302 if (VT == SVT)
9303 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9304 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9305 /*IsTruncating*/ false, IsCompressing);
9306
9308 "Should only be a truncating store, not extending!");
9309 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9310 assert(VT.isVector() == SVT.isVector() &&
9311 "Cannot use trunc store to convert to or from a vector!");
9312 assert((!VT.isVector() ||
9314 "Cannot use trunc store to change the number of vector elements!");
9315
9316 SDVTList VTs = getVTList(MVT::Other);
9317 SDValue Undef = getUNDEF(Ptr.getValueType());
9318 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9320 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9321 ID.AddInteger(SVT.getRawBits());
9322 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9323 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9324 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9325 void *IP = nullptr;
9326 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9327 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9328 return SDValue(E, 0);
9329 }
9330 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9331 VTs, ISD::UNINDEXED, true,
9332 IsCompressing, SVT, MMO);
9333 createOperands(N, Ops);
9334
9335 CSEMap.InsertNode(N, IP);
9336 InsertNode(N);
9337 SDValue V(N, 0);
9338 NewSDValueDbgMsg(V, "Creating new node: ", this);
9339 return V;
9340}
9341
9344 ISD::MemIndexType IndexType) {
9345 assert(Ops.size() == 6 && "Incompatible number of operands");
9346
9348 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9349 ID.AddInteger(VT.getRawBits());
9350 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9351 dl.getIROrder(), VTs, VT, MMO, IndexType));
9352 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9353 ID.AddInteger(MMO->getFlags());
9354 void *IP = nullptr;
9355 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9356 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9357 return SDValue(E, 0);
9358 }
9359
9360 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9361 VT, MMO, IndexType);
9362 createOperands(N, Ops);
9363
9364 assert(N->getMask().getValueType().getVectorElementCount() ==
9365 N->getValueType(0).getVectorElementCount() &&
9366 "Vector width mismatch between mask and data");
9367 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9368 N->getValueType(0).getVectorElementCount().isScalable() &&
9369 "Scalable flags of index and data do not match");
9371 N->getIndex().getValueType().getVectorElementCount(),
9372 N->getValueType(0).getVectorElementCount()) &&
9373 "Vector width mismatch between index and data");
9374 assert(isa<ConstantSDNode>(N->getScale()) &&
9375 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9376 "Scale should be a constant power of 2");
9377
9378 CSEMap.InsertNode(N, IP);
9379 InsertNode(N);
9380 SDValue V(N, 0);
9381 NewSDValueDbgMsg(V, "Creating new node: ", this);
9382 return V;
9383}
9384
9387 MachineMemOperand *MMO,
9388 ISD::MemIndexType IndexType) {
9389 assert(Ops.size() == 7 && "Incompatible number of operands");
9390
9392 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9393 ID.AddInteger(VT.getRawBits());
9394 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9395 dl.getIROrder(), VTs, VT, MMO, IndexType));
9396 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9397 ID.AddInteger(MMO->getFlags());
9398 void *IP = nullptr;
9399 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9400 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9401 return SDValue(E, 0);
9402 }
9403 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9404 VT, MMO, IndexType);
9405 createOperands(N, Ops);
9406
9407 assert(N->getMask().getValueType().getVectorElementCount() ==
9408 N->getValue().getValueType().getVectorElementCount() &&
9409 "Vector width mismatch between mask and data");
9410 assert(
9411 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9412 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9413 "Scalable flags of index and data do not match");
9415 N->getIndex().getValueType().getVectorElementCount(),
9416 N->getValue().getValueType().getVectorElementCount()) &&
9417 "Vector width mismatch between index and data");
9418 assert(isa<ConstantSDNode>(N->getScale()) &&
9419 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9420 "Scale should be a constant power of 2");
9421
9422 CSEMap.InsertNode(N, IP);
9423 InsertNode(N);
9424 SDValue V(N, 0);
9425 NewSDValueDbgMsg(V, "Creating new node: ", this);
9426 return V;
9427}
9428
9431 SDValue PassThru, EVT MemVT,
9432 MachineMemOperand *MMO,
9434 ISD::LoadExtType ExtTy, bool isExpanding) {
9435 bool Indexed = AM != ISD::UNINDEXED;
9436 assert((Indexed || Offset.isUndef()) &&
9437 "Unindexed masked load with an offset!");
9438 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9439 : getVTList(VT, MVT::Other);
9440 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9442 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9443 ID.AddInteger(MemVT.getRawBits());
9444 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9445 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9446 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9447 ID.AddInteger(MMO->getFlags());
9448 void *IP = nullptr;
9449 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9450 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9451 return SDValue(E, 0);
9452 }
9453 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9454 AM, ExtTy, isExpanding, MemVT, MMO);
9455 createOperands(N, Ops);
9456
9457 CSEMap.InsertNode(N, IP);
9458 InsertNode(N);
9459 SDValue V(N, 0);
9460 NewSDValueDbgMsg(V, "Creating new node: ", this);
9461 return V;
9462}
9463
9467 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9468 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9469 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9470 Offset, LD->getMask(), LD->getPassThru(),
9471 LD->getMemoryVT(), LD->getMemOperand(), AM,
9472 LD->getExtensionType(), LD->isExpandingLoad());
9473}
9474
9477 SDValue Mask, EVT MemVT,
9478 MachineMemOperand *MMO,
9479 ISD::MemIndexedMode AM, bool IsTruncating,
9480 bool IsCompressing) {
9481 assert(Chain.getValueType() == MVT::Other &&
9482 "Invalid chain type");
9483 bool Indexed = AM != ISD::UNINDEXED;
9484 assert((Indexed || Offset.isUndef()) &&
9485 "Unindexed masked store with an offset!");
9486 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
9487 : getVTList(MVT::Other);
9488 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
9490 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
9491 ID.AddInteger(MemVT.getRawBits());
9492 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
9493 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9494 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9495 ID.AddInteger(MMO->getFlags());
9496 void *IP = nullptr;
9497 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9498 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
9499 return SDValue(E, 0);
9500 }
9501 auto *N =
9502 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9503 IsTruncating, IsCompressing, MemVT, MMO);
9504 createOperands(N, Ops);
9505
9506 CSEMap.InsertNode(N, IP);
9507 InsertNode(N);
9508 SDValue V(N, 0);
9509 NewSDValueDbgMsg(V, "Creating new node: ", this);
9510 return V;
9511}
9512
9516 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
9517 assert(ST->getOffset().isUndef() &&
9518 "Masked store is already a indexed store!");
9519 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9520 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
9521 AM, ST->isTruncatingStore(), ST->isCompressingStore());
9522}
9523
9526 MachineMemOperand *MMO,
9527 ISD::MemIndexType IndexType,
9528 ISD::LoadExtType ExtTy) {
9529 assert(Ops.size() == 6 && "Incompatible number of operands");
9530
9532 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
9533 ID.AddInteger(MemVT.getRawBits());
9534 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
9535 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
9536 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9537 ID.AddInteger(MMO->getFlags());
9538 void *IP = nullptr;
9539 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9540 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9541 return SDValue(E, 0);
9542 }
9543
9544 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9545 VTs, MemVT, MMO, IndexType, ExtTy);
9546 createOperands(N, Ops);
9547
9548 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
9549 "Incompatible type of the PassThru value in MaskedGatherSDNode");
9550 assert(N->getMask().getValueType().getVectorElementCount() ==
9551 N->getValueType(0).getVectorElementCount() &&
9552 "Vector width mismatch between mask and data");
9553 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9554 N->getValueType(0).getVectorElementCount().isScalable() &&
9555 "Scalable flags of index and data do not match");
9557 N->getIndex().getValueType().getVectorElementCount(),
9558 N->getValueType(0).getVectorElementCount()) &&
9559 "Vector width mismatch between index and data");
9560 assert(isa<ConstantSDNode>(N->getScale()) &&
9561 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9562 "Scale should be a constant power of 2");
9563
9564 CSEMap.InsertNode(N, IP);
9565 InsertNode(N);
9566 SDValue V(N, 0);
9567 NewSDValueDbgMsg(V, "Creating new node: ", this);
9568 return V;
9569}
9570
9573 MachineMemOperand *MMO,
9574 ISD::MemIndexType IndexType,
9575 bool IsTrunc) {
9576 assert(Ops.size() == 6 && "Incompatible number of operands");
9577
9579 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
9580 ID.AddInteger(MemVT.getRawBits());
9581 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
9582 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
9583 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9584 ID.AddInteger(MMO->getFlags());
9585 void *IP = nullptr;
9586 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9587 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
9588 return SDValue(E, 0);
9589 }
9590
9591 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9592 VTs, MemVT, MMO, IndexType, IsTrunc);
9593 createOperands(N, Ops);
9594
9595 assert(N->getMask().getValueType().getVectorElementCount() ==
9596 N->getValue().getValueType().getVectorElementCount() &&
9597 "Vector width mismatch between mask and data");
9598 assert(
9599 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9600 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9601 "Scalable flags of index and data do not match");
9603 N->getIndex().getValueType().getVectorElementCount(),
9604 N->getValue().getValueType().getVectorElementCount()) &&
9605 "Vector width mismatch between index and data");
9606 assert(isa<ConstantSDNode>(N->getScale()) &&
9607 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9608 "Scale should be a constant power of 2");
9609
9610 CSEMap.InsertNode(N, IP);
9611 InsertNode(N);
9612 SDValue V(N, 0);
9613 NewSDValueDbgMsg(V, "Creating new node: ", this);
9614 return V;
9615}
9616
9618 EVT MemVT, MachineMemOperand *MMO) {
9619 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9620 SDVTList VTs = getVTList(MVT::Other);
9621 SDValue Ops[] = {Chain, Ptr};
9624 ID.AddInteger(MemVT.getRawBits());
9625 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9626 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9627 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9628 ID.AddInteger(MMO->getFlags());
9629 void *IP = nullptr;
9630 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9631 return SDValue(E, 0);
9632
9633 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
9634 dl.getDebugLoc(), VTs, MemVT, MMO);
9635 createOperands(N, Ops);
9636
9637 CSEMap.InsertNode(N, IP);
9638 InsertNode(N);
9639 SDValue V(N, 0);
9640 NewSDValueDbgMsg(V, "Creating new node: ", this);
9641 return V;
9642}
9643
9645 EVT MemVT, MachineMemOperand *MMO) {
9646 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9647 SDVTList VTs = getVTList(MVT::Other);
9648 SDValue Ops[] = {Chain, Ptr};
9651 ID.AddInteger(MemVT.getRawBits());
9652 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9653 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9654 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9655 ID.AddInteger(MMO->getFlags());
9656 void *IP = nullptr;
9657 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9658 return SDValue(E, 0);
9659
9660 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
9661 dl.getDebugLoc(), VTs, MemVT, MMO);
9662 createOperands(N, Ops);
9663
9664 CSEMap.InsertNode(N, IP);
9665 InsertNode(N);
9666 SDValue V(N, 0);
9667 NewSDValueDbgMsg(V, "Creating new node: ", this);
9668 return V;
9669}
9670
9672 // select undef, T, F --> T (if T is a constant), otherwise F
9673 // select, ?, undef, F --> F
9674 // select, ?, T, undef --> T
9675 if (Cond.isUndef())
9676 return isConstantValueOfAnyType(T) ? T : F;
9677 if (T.isUndef())
9678 return F;
9679 if (F.isUndef())
9680 return T;
9681
9682 // select true, T, F --> T
9683 // select false, T, F --> F
9684 if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
9685 return CondC->isZero() ? F : T;
9686
9687 // TODO: This should simplify VSELECT with non-zero constant condition using
9688 // something like this (but check boolean contents to be complete?):
9689 if (ConstantSDNode *CondC = isConstOrConstSplat(Cond, /*AllowUndefs*/ false,
9690 /*AllowTruncation*/ true))
9691 if (CondC->isZero())
9692 return F;
9693
9694 // select ?, T, T --> T
9695 if (T == F)
9696 return T;
9697
9698 return SDValue();
9699}
9700
9702 // shift undef, Y --> 0 (can always assume that the undef value is 0)
9703 if (X.isUndef())
9704 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
9705 // shift X, undef --> undef (because it may shift by the bitwidth)
9706 if (Y.isUndef())
9707 return getUNDEF(X.getValueType());
9708
9709 // shift 0, Y --> 0
9710 // shift X, 0 --> X
9712 return X;
9713
9714 // shift X, C >= bitwidth(X) --> undef
9715 // All vector elements must be too big (or undef) to avoid partial undefs.
9716 auto isShiftTooBig = [X](ConstantSDNode *Val) {
9717 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
9718 };
9719 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
9720 return getUNDEF(X.getValueType());
9721
9722 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
9723 if (X.getValueType().getScalarType() == MVT::i1)
9724 return X;
9725
9726 return SDValue();
9727}
9728
9730 SDNodeFlags Flags) {
9731 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
9732 // (an undef operand can be chosen to be Nan/Inf), then the result of this
9733 // operation is poison. That result can be relaxed to undef.
9734 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
9735 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
9736 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
9737 (YC && YC->getValueAPF().isNaN());
9738 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
9739 (YC && YC->getValueAPF().isInfinity());
9740
9741 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
9742 return getUNDEF(X.getValueType());
9743
9744 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
9745 return getUNDEF(X.getValueType());
9746
9747 if (!YC)
9748 return SDValue();
9749
9750 // X + -0.0 --> X
9751 if (Opcode == ISD::FADD)
9752 if (YC->getValueAPF().isNegZero())
9753 return X;
9754
9755 // X - +0.0 --> X
9756 if (Opcode == ISD::FSUB)
9757 if (YC->getValueAPF().isPosZero())
9758 return X;
9759
9760 // X * 1.0 --> X
9761 // X / 1.0 --> X
9762 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
9763 if (YC->getValueAPF().isExactlyValue(1.0))
9764 return X;
9765
9766 // X * 0.0 --> 0.0
9767 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
9768 if (YC->getValueAPF().isZero())
9769 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
9770
9771 return SDValue();
9772}
9773
9775 SDValue Ptr, SDValue SV, unsigned Align) {
9776 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
9777 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
9778}
9779
9780SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9781 ArrayRef<SDUse> Ops) {
9782 switch (Ops.size()) {
9783 case 0: return getNode(Opcode, DL, VT);
9784 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
9785 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
9786 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
9787 default: break;
9788 }
9789
9790 // Copy from an SDUse array into an SDValue array for use with
9791 // the regular getNode logic.
9792 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
9793 return getNode(Opcode, DL, VT, NewOps);
9794}
9795
9796SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9797 ArrayRef<SDValue> Ops) {
9798 SDNodeFlags Flags;
9799 if (Inserter)
9800 Flags = Inserter->getFlags();
9801 return getNode(Opcode, DL, VT, Ops, Flags);
9802}
9803
9804SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9805 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
9806 unsigned NumOps = Ops.size();
9807 switch (NumOps) {
9808 case 0: return getNode(Opcode, DL, VT);
9809 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
9810 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
9811 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
9812 default: break;
9813 }
9814
9815#ifndef NDEBUG
9816 for (const auto &Op : Ops)
9817 assert(Op.getOpcode() != ISD::DELETED_NODE &&
9818 "Operand is DELETED_NODE!");
9819#endif
9820
9821 switch (Opcode) {
9822 default: break;
9823 case ISD::BUILD_VECTOR:
9824 // Attempt to simplify BUILD_VECTOR.
9825 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
9826 return V;
9827 break;
9829 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
9830 return V;
9831 break;
9832 case ISD::SELECT_CC:
9833 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
9834 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
9835 "LHS and RHS of condition must have same type!");
9836 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
9837 "True and False arms of SelectCC must have same type!");
9838 assert(Ops[2].getValueType() == VT &&
9839 "select_cc node must be of same type as true and false value!");
9840 assert((!Ops[0].getValueType().isVector() ||
9841 Ops[0].getValueType().getVectorElementCount() ==
9842 VT.getVectorElementCount()) &&
9843 "Expected select_cc with vector result to have the same sized "
9844 "comparison type!");
9845 break;
9846 case ISD::BR_CC:
9847 assert(NumOps == 5 && "BR_CC takes 5 operands!");
9848 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
9849 "LHS/RHS of comparison should match types!");
9850 break;
9851 case ISD::VP_ADD:
9852 case ISD::VP_SUB:
9853 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
9854 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
9855 Opcode = ISD::VP_XOR;
9856 break;
9857 case ISD::VP_MUL:
9858 // If it is VP_MUL mask operation then turn it to VP_AND
9859 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
9860 Opcode = ISD::VP_AND;
9861 break;
9862 case ISD::VP_REDUCE_MUL:
9863 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
9864 if (VT == MVT::i1)
9865 Opcode = ISD::VP_REDUCE_AND;
9866 break;
9867 case ISD::VP_REDUCE_ADD:
9868 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
9869 if (VT == MVT::i1)
9870 Opcode = ISD::VP_REDUCE_XOR;
9871 break;
9872 case ISD::VP_REDUCE_SMAX:
9873 case ISD::VP_REDUCE_UMIN:
9874 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
9875 // VP_REDUCE_AND.
9876 if (VT == MVT::i1)
9877 Opcode = ISD::VP_REDUCE_AND;
9878 break;
9879 case ISD::VP_REDUCE_SMIN:
9880 case ISD::VP_REDUCE_UMAX:
9881 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
9882 // VP_REDUCE_OR.
9883 if (VT == MVT::i1)
9884 Opcode = ISD::VP_REDUCE_OR;
9885 break;
9886 }
9887
9888 // Memoize nodes.
9889 SDNode *N;
9890 SDVTList VTs = getVTList(VT);
9891
9892 if (VT != MVT::Glue) {
9894 AddNodeIDNode(ID, Opcode, VTs, Ops);
9895 void *IP = nullptr;
9896
9897 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
9898 return SDValue(E, 0);
9899
9900 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9901 createOperands(N, Ops);
9902
9903 CSEMap.InsertNode(N, IP);
9904 } else {
9905 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
9906 createOperands(N, Ops);
9907 }
9908
9909 N->setFlags(Flags);
9910 InsertNode(N);
9911 SDValue V(N, 0);
9912 NewSDValueDbgMsg(V, "Creating new node: ", this);
9913 return V;
9914}
9915
9916SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
9917 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
9918 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
9919}
9920
9921SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
9922 ArrayRef<SDValue> Ops) {
9923 SDNodeFlags Flags;
9924 if (Inserter)
9925 Flags = Inserter->getFlags();
9926 return getNode(Opcode, DL, VTList, Ops, Flags);
9927}
9928
9929SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
9930 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
9931 if (VTList.NumVTs == 1)
9932 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
9933
9934#ifndef NDEBUG
9935 for (const auto &Op : Ops)
9936 assert(Op.getOpcode() != ISD::DELETED_NODE &&
9937 "Operand is DELETED_NODE!");
9938#endif
9939
9940 switch (Opcode) {
9941 case ISD::SADDO:
9942 case ISD::UADDO:
9943 case ISD::SSUBO:
9944 case ISD::USUBO: {
9945 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
9946 "Invalid add/sub overflow op!");
9947 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
9948 Ops[0].getValueType() == Ops[1].getValueType() &&
9949 Ops[0].getValueType() == VTList.VTs[0] &&
9950 "Binary operator types must match!");
9951 SDValue N1 = Ops[0], N2 = Ops[1];
9952 canonicalizeCommutativeBinop(Opcode, N1, N2);
9953
9954 // (X +- 0) -> X with zero-overflow.
9955 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
9956 /*AllowTruncation*/ true);
9957 if (N2CV && N2CV->isZero()) {
9958 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
9959 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
9960 }
9961
9962 if (VTList.VTs[0].isVector() &&
9963 VTList.VTs[0].getVectorElementType() == MVT::i1 &&
9964 VTList.VTs[1].getVectorElementType() == MVT::i1) {
9965 SDValue F1 = getFreeze(N1);
9966 SDValue F2 = getFreeze(N2);
9967 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
9968 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
9969 return getNode(ISD::MERGE_VALUES, DL, VTList,
9970 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
9971 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
9972 Flags);
9973 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
9974 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
9975 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
9976 return getNode(ISD::MERGE_VALUES, DL, VTList,
9977 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
9978 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
9979 Flags);
9980 }
9981 }
9982 break;
9983 }
9984 case ISD::SADDO_CARRY:
9985 case ISD::UADDO_CARRY:
9986 case ISD::SSUBO_CARRY:
9987 case ISD::USUBO_CARRY:
9988 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
9989 "Invalid add/sub overflow op!");
9990 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
9991 Ops[0].getValueType() == Ops[1].getValueType() &&
9992 Ops[0].getValueType() == VTList.VTs[0] &&
9993 Ops[2].getValueType() == VTList.VTs[1] &&
9994 "Binary operator types must match!");
9995 break;
9996 case ISD::SMUL_LOHI:
9997 case ISD::UMUL_LOHI: {
9998 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
9999 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
10000 VTList.VTs[0] == Ops[0].getValueType() &&
10001 VTList.VTs[0] == Ops[1].getValueType() &&
10002 "Binary operator types must match!");
10003 // Constant fold.
10004 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
10005 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
10006 if (LHS && RHS) {
10007 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
10008 unsigned OutWidth = Width * 2;
10009 APInt Val = LHS->getAPIntValue();
10010 APInt Mul = RHS->getAPIntValue();
10011 if (Opcode == ISD::SMUL_LOHI) {
10012 Val = Val.sext(OutWidth);
10013 Mul = Mul.sext(OutWidth);
10014 } else {
10015 Val = Val.zext(OutWidth);
10016 Mul = Mul.zext(OutWidth);
10017 }
10018 Val *= Mul;
10019
10020 SDValue Hi =
10021 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
10022 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
10023 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
10024 }
10025 break;
10026 }
10027 case ISD::FFREXP: {
10028 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
10029 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
10030 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
10031
10032 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
10033 int FrexpExp;
10034 APFloat FrexpMant =
10035 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
10036 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
10037 SDValue Result1 =
10038 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
10039 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
10040 }
10041
10042 break;
10043 }
10045 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10046 "Invalid STRICT_FP_EXTEND!");
10047 assert(VTList.VTs[0].isFloatingPoint() &&
10048 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
10049 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10050 "STRICT_FP_EXTEND result type should be vector iff the operand "
10051 "type is vector!");
10052 assert((!VTList.VTs[0].isVector() ||
10053 VTList.VTs[0].getVectorElementCount() ==
10054 Ops[1].getValueType().getVectorElementCount()) &&
10055 "Vector element count mismatch!");
10056 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
10057 "Invalid fpext node, dst <= src!");
10058 break;
10060 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
10061 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10062 "STRICT_FP_ROUND result type should be vector iff the operand "
10063 "type is vector!");
10064 assert((!VTList.VTs[0].isVector() ||
10065 VTList.VTs[0].getVectorElementCount() ==
10066 Ops[1].getValueType().getVectorElementCount()) &&
10067 "Vector element count mismatch!");
10068 assert(VTList.VTs[0].isFloatingPoint() &&
10069 Ops[1].getValueType().isFloatingPoint() &&
10070 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
10071 isa<ConstantSDNode>(Ops[2]) &&
10072 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10073 "Invalid STRICT_FP_ROUND!");
10074 break;
10075#if 0
10076 // FIXME: figure out how to safely handle things like
10077 // int foo(int x) { return 1 << (x & 255); }
10078 // int bar() { return foo(256); }
10079 case ISD::SRA_PARTS:
10080 case ISD::SRL_PARTS:
10081 case ISD::SHL_PARTS:
10082 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10083 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10084 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10085 else if (N3.getOpcode() == ISD::AND)
10086 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10087 // If the and is only masking out bits that cannot effect the shift,
10088 // eliminate the and.
10089 unsigned NumBits = VT.getScalarSizeInBits()*2;
10090 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10091 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10092 }
10093 break;
10094#endif
10095 }
10096
10097 // Memoize the node unless it returns a glue result.
10098 SDNode *N;
10099 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10101 AddNodeIDNode(ID, Opcode, VTList, Ops);
10102 void *IP = nullptr;
10103 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10104 return SDValue(E, 0);
10105
10106 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10107 createOperands(N, Ops);
10108 CSEMap.InsertNode(N, IP);
10109 } else {
10110 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10111 createOperands(N, Ops);
10112 }
10113
10114 N->setFlags(Flags);
10115 InsertNode(N);
10116 SDValue V(N, 0);
10117 NewSDValueDbgMsg(V, "Creating new node: ", this);
10118 return V;
10119}
10120
10121SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10122 SDVTList VTList) {
10123 return getNode(Opcode, DL, VTList, std::nullopt);
10124}
10125
10126SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10127 SDValue N1) {
10128 SDValue Ops[] = { N1 };
10129 return getNode(Opcode, DL, VTList, Ops);
10130}
10131
10132SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10133 SDValue N1, SDValue N2) {
10134 SDValue Ops[] = { N1, N2 };
10135 return getNode(Opcode, DL, VTList, Ops);
10136}
10137
10138SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10139 SDValue N1, SDValue N2, SDValue N3) {
10140 SDValue Ops[] = { N1, N2, N3 };
10141 return getNode(Opcode, DL, VTList, Ops);
10142}
10143
10144SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10145 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10146 SDValue Ops[] = { N1, N2, N3, N4 };
10147 return getNode(Opcode, DL, VTList, Ops);
10148}
10149
10150SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10151 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10152 SDValue N5) {
10153 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10154 return getNode(Opcode, DL, VTList, Ops);
10155}
10156
10158 return makeVTList(SDNode::getValueTypeList(VT), 1);
10159}
10160
10163 ID.AddInteger(2U);
10164 ID.AddInteger(VT1.getRawBits());
10165 ID.AddInteger(VT2.getRawBits());
10166
10167 void *IP = nullptr;
10168 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10169 if (!Result) {
10170 EVT *Array = Allocator.Allocate<EVT>(2);
10171 Array[0] = VT1;
10172 Array[1] = VT2;
10173 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10174 VTListMap.InsertNode(Result, IP);
10175 }
10176 return Result->getSDVTList();
10177}
10178
10181 ID.AddInteger(3U);
10182 ID.AddInteger(VT1.getRawBits());
10183 ID.AddInteger(VT2.getRawBits());
10184 ID.AddInteger(VT3.getRawBits());
10185
10186 void *IP = nullptr;
10187 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10188 if (!Result) {
10189 EVT *Array = Allocator.Allocate<EVT>(3);
10190 Array[0] = VT1;
10191 Array[1] = VT2;
10192 Array[2] = VT3;
10193 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10194 VTListMap.InsertNode(Result, IP);
10195 }
10196 return Result->getSDVTList();
10197}
10198
10201 ID.AddInteger(4U);
10202 ID.AddInteger(VT1.getRawBits());
10203 ID.AddInteger(VT2.getRawBits());
10204 ID.AddInteger(VT3.getRawBits());
10205 ID.AddInteger(VT4.getRawBits());
10206
10207 void *IP = nullptr;
10208 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10209 if (!Result) {
10210 EVT *Array = Allocator.Allocate<EVT>(4);
10211 Array[0] = VT1;
10212 Array[1] = VT2;
10213 Array[2] = VT3;
10214 Array[3] = VT4;
10215 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10216 VTListMap.InsertNode(Result, IP);
10217 }
10218 return Result->getSDVTList();
10219}
10220
10222 unsigned NumVTs = VTs.size();
10224 ID.AddInteger(NumVTs);
10225 for (unsigned index = 0; index < NumVTs; index++) {
10226 ID.AddInteger(VTs[index].getRawBits());
10227 }
10228
10229 void *IP = nullptr;
10230 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10231 if (!Result) {
10232 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10233 llvm::copy(VTs, Array);
10234 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10235 VTListMap.InsertNode(Result, IP);
10236 }
10237 return Result->getSDVTList();
10238}
10239
10240
10241/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10242/// specified operands. If the resultant node already exists in the DAG,
10243/// this does not modify the specified node, instead it returns the node that
10244/// already exists. If the resultant node does not exist in the DAG, the
10245/// input node is returned. As a degenerate case, if you specify the same
10246/// input operands as the node already has, the input node is returned.
10248 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10249
10250 // Check to see if there is no change.
10251 if (Op == N->getOperand(0)) return N;
10252
10253 // See if the modified node already exists.
10254 void *InsertPos = nullptr;
10255 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10256 return Existing;
10257
10258 // Nope it doesn't. Remove the node from its current place in the maps.
10259 if (InsertPos)
10260 if (!RemoveNodeFromCSEMaps(N))
10261 InsertPos = nullptr;
10262
10263 // Now we update the operands.
10264 N->OperandList[0].set(Op);
10265
10267 // If this gets put into a CSE map, add it.
10268 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10269 return N;
10270}
10271
10273 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10274
10275 // Check to see if there is no change.
10276 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10277 return N; // No operands changed, just return the input node.
10278
10279 // See if the modified node already exists.
10280 void *InsertPos = nullptr;
10281 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10282 return Existing;
10283
10284 // Nope it doesn't. Remove the node from its current place in the maps.
10285 if (InsertPos)
10286 if (!RemoveNodeFromCSEMaps(N))
10287 InsertPos = nullptr;
10288
10289 // Now we update the operands.
10290 if (N->OperandList[0] != Op1)
10291 N->OperandList[0].set(Op1);
10292 if (N->OperandList[1] != Op2)
10293 N->OperandList[1].set(Op2);
10294
10296 // If this gets put into a CSE map, add it.
10297 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10298 return N;
10299}
10300
10303 SDValue Ops[] = { Op1, Op2, Op3 };
10304 return UpdateNodeOperands(N, Ops);
10305}
10306
10309 SDValue Op3, SDValue Op4) {
10310 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10311 return UpdateNodeOperands(N, Ops);
10312}
10313
10316 SDValue Op3, SDValue Op4, SDValue Op5) {
10317 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10318 return UpdateNodeOperands(N, Ops);
10319}
10320
10323 unsigned NumOps = Ops.size();
10324 assert(N->getNumOperands() == NumOps &&
10325 "Update with wrong number of operands");
10326
10327 // If no operands changed just return the input node.
10328 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10329 return N;
10330
10331 // See if the modified node already exists.
10332 void *InsertPos = nullptr;
10333 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10334 return Existing;
10335
10336 // Nope it doesn't. Remove the node from its current place in the maps.
10337 if (InsertPos)
10338 if (!RemoveNodeFromCSEMaps(N))
10339 InsertPos = nullptr;
10340
10341 // Now we update the operands.
10342 for (unsigned i = 0; i != NumOps; ++i)
10343 if (N->OperandList[i] != Ops[i])
10344 N->OperandList[i].set(Ops[i]);
10345
10347 // If this gets put into a CSE map, add it.
10348 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10349 return N;
10350}
10351
10352/// DropOperands - Release the operands and set this node to have
10353/// zero operands.
10355 // Unlike the code in MorphNodeTo that does this, we don't need to
10356 // watch for dead nodes here.
10357 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10358 SDUse &Use = *I++;
10359 Use.set(SDValue());
10360 }
10361}
10362
10364 ArrayRef<MachineMemOperand *> NewMemRefs) {
10365 if (NewMemRefs.empty()) {
10366 N->clearMemRefs();
10367 return;
10368 }
10369
10370 // Check if we can avoid allocating by storing a single reference directly.
10371 if (NewMemRefs.size() == 1) {
10372 N->MemRefs = NewMemRefs[0];
10373 N->NumMemRefs = 1;
10374 return;
10375 }
10376
10377 MachineMemOperand **MemRefsBuffer =
10378 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10379 llvm::copy(NewMemRefs, MemRefsBuffer);
10380 N->MemRefs = MemRefsBuffer;
10381 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10382}
10383
10384/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10385/// machine opcode.
10386///
10388 EVT VT) {
10389 SDVTList VTs = getVTList(VT);
10390 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10391}
10392
10394 EVT VT, SDValue Op1) {
10395 SDVTList VTs = getVTList(VT);
10396 SDValue Ops[] = { Op1 };
10397 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10398}
10399
10401 EVT VT, SDValue Op1,
10402 SDValue Op2) {
10403 SDVTList VTs = getVTList(VT);
10404 SDValue Ops[] = { Op1, Op2 };
10405 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10406}
10407
10409 EVT VT, SDValue Op1,
10410 SDValue Op2, SDValue Op3) {
10411 SDVTList VTs = getVTList(VT);
10412 SDValue Ops[] = { Op1, Op2, Op3 };
10413 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10414}
10415
10417 EVT VT, ArrayRef<SDValue> Ops) {
10418 SDVTList VTs = getVTList(VT);
10419 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10420}
10421
10423 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10424 SDVTList VTs = getVTList(VT1, VT2);
10425 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10426}
10427
10429 EVT VT1, EVT VT2) {
10430 SDVTList VTs = getVTList(VT1, VT2);
10431 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10432}
10433
10435 EVT VT1, EVT VT2, EVT VT3,
10436 ArrayRef<SDValue> Ops) {
10437 SDVTList VTs = getVTList(VT1, VT2, VT3);
10438 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10439}
10440
10442 EVT VT1, EVT VT2,
10443 SDValue Op1, SDValue Op2) {
10444 SDVTList VTs = getVTList(VT1, VT2);
10445 SDValue Ops[] = { Op1, Op2 };
10446 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10447}
10448
10450 SDVTList VTs,ArrayRef<SDValue> Ops) {
10451 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
10452 // Reset the NodeID to -1.
10453 New->setNodeId(-1);
10454 if (New != N) {
10455 ReplaceAllUsesWith(N, New);
10457 }
10458 return New;
10459}
10460
10461/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
10462/// the line number information on the merged node since it is not possible to
10463/// preserve the information that operation is associated with multiple lines.
10464/// This will make the debugger working better at -O0, were there is a higher
10465/// probability having other instructions associated with that line.
10466///
10467/// For IROrder, we keep the smaller of the two
10468SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
10469 DebugLoc NLoc = N->getDebugLoc();
10470 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
10471 N->setDebugLoc(DebugLoc());
10472 }
10473 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
10474 N->setIROrder(Order);
10475 return N;
10476}
10477
10478/// MorphNodeTo - This *mutates* the specified node to have the specified
10479/// return type, opcode, and operands.
10480///
10481/// Note that MorphNodeTo returns the resultant node. If there is already a
10482/// node of the specified opcode and operands, it returns that node instead of
10483/// the current one. Note that the SDLoc need not be the same.
10484///
10485/// Using MorphNodeTo is faster than creating a new node and swapping it in
10486/// with ReplaceAllUsesWith both because it often avoids allocating a new
10487/// node, and because it doesn't require CSE recalculation for any of
10488/// the node's users.
10489///
10490/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
10491/// As a consequence it isn't appropriate to use from within the DAG combiner or
10492/// the legalizer which maintain worklists that would need to be updated when
10493/// deleting things.
10495 SDVTList VTs, ArrayRef<SDValue> Ops) {
10496 // If an identical node already exists, use it.
10497 void *IP = nullptr;
10498 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
10500 AddNodeIDNode(ID, Opc, VTs, Ops);
10501 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
10502 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
10503 }
10504
10505 if (!RemoveNodeFromCSEMaps(N))
10506 IP = nullptr;
10507
10508 // Start the morphing.
10509 N->NodeType = Opc;
10510 N->ValueList = VTs.VTs;
10511 N->NumValues = VTs.NumVTs;
10512
10513 // Clear the operands list, updating used nodes to remove this from their
10514 // use list. Keep track of any operands that become dead as a result.
10515 SmallPtrSet<SDNode*, 16> DeadNodeSet;
10516 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
10517 SDUse &Use = *I++;
10518 SDNode *Used = Use.getNode();
10519 Use.set(SDValue());
10520 if (Used->use_empty())
10521 DeadNodeSet.insert(Used);
10522 }
10523
10524 // For MachineNode, initialize the memory references information.
10525 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
10526 MN->clearMemRefs();
10527
10528 // Swap for an appropriately sized array from the recycler.
10529 removeOperands(N);
10530 createOperands(N, Ops);
10531
10532 // Delete any nodes that are still dead after adding the uses for the
10533 // new operands.
10534 if (!DeadNodeSet.empty()) {
10535 SmallVector<SDNode *, 16> DeadNodes;
10536 for (SDNode *N : DeadNodeSet)
10537 if (N->use_empty())
10538 DeadNodes.push_back(N);
10539 RemoveDeadNodes(DeadNodes);
10540 }
10541
10542 if (IP)
10543 CSEMap.InsertNode(N, IP); // Memoize the new node.
10544 return N;
10545}
10546
10548 unsigned OrigOpc = Node->getOpcode();
10549 unsigned NewOpc;
10550 switch (OrigOpc) {
10551 default:
10552 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
10553#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10554 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
10555#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10556 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
10557#include "llvm/IR/ConstrainedOps.def"
10558 }
10559
10560 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
10561
10562 // We're taking this node out of the chain, so we need to re-link things.
10563 SDValue InputChain = Node->getOperand(0);
10564 SDValue OutputChain = SDValue(Node, 1);
10565 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
10566
10568 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
10569 Ops.push_back(Node->getOperand(i));
10570
10571 SDVTList VTs = getVTList(Node->getValueType(0));
10572 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
10573
10574 // MorphNodeTo can operate in two ways: if an existing node with the
10575 // specified operands exists, it can just return it. Otherwise, it
10576 // updates the node in place to have the requested operands.
10577 if (Res == Node) {
10578 // If we updated the node in place, reset the node ID. To the isel,
10579 // this should be just like a newly allocated machine node.
10580 Res->setNodeId(-1);
10581 } else {
10582 ReplaceAllUsesWith(Node, Res);
10583 RemoveDeadNode(Node);
10584 }
10585
10586 return Res;
10587}
10588
10589/// getMachineNode - These are used for target selectors to create a new node
10590/// with specified return type(s), MachineInstr opcode, and operands.
10591///
10592/// Note that getMachineNode returns the resultant node. If there is already a
10593/// node of the specified opcode and operands, it returns that node instead of
10594/// the current one.
10596 EVT VT) {
10597 SDVTList VTs = getVTList(VT);
10598 return getMachineNode(Opcode, dl, VTs, std::nullopt);
10599}
10600
10602 EVT VT, SDValue Op1) {
10603 SDVTList VTs = getVTList(VT);
10604 SDValue Ops[] = { Op1 };
10605 return getMachineNode(Opcode, dl, VTs, Ops);
10606}
10607
10609 EVT VT, SDValue Op1, SDValue Op2) {
10610 SDVTList VTs = getVTList(VT);
10611 SDValue Ops[] = { Op1, Op2 };
10612 return getMachineNode(Opcode, dl, VTs, Ops);
10613}
10614
10616 EVT VT, SDValue Op1, SDValue Op2,
10617 SDValue Op3) {
10618 SDVTList VTs = getVTList(VT);
10619 SDValue Ops[] = { Op1, Op2, Op3 };
10620 return getMachineNode(Opcode, dl, VTs, Ops);
10621}
10622
10624 EVT VT, ArrayRef<SDValue> Ops) {
10625 SDVTList VTs = getVTList(VT);
10626 return getMachineNode(Opcode, dl, VTs, Ops);
10627}
10628
10630 EVT VT1, EVT VT2, SDValue Op1,
10631 SDValue Op2) {
10632 SDVTList VTs = getVTList(VT1, VT2);
10633 SDValue Ops[] = { Op1, Op2 };
10634 return getMachineNode(Opcode, dl, VTs, Ops);
10635}
10636
10638 EVT VT1, EVT VT2, SDValue Op1,
10639 SDValue Op2, SDValue Op3) {
10640 SDVTList VTs = getVTList(VT1, VT2);
10641 SDValue Ops[] = { Op1, Op2, Op3 };
10642 return getMachineNode(Opcode, dl, VTs, Ops);
10643}
10644
10646 EVT VT1, EVT VT2,
10647 ArrayRef<SDValue> Ops) {
10648 SDVTList VTs = getVTList(VT1, VT2);
10649 return getMachineNode(Opcode, dl, VTs, Ops);
10650}
10651
10653 EVT VT1, EVT VT2, EVT VT3,
10654 SDValue Op1, SDValue Op2) {
10655 SDVTList VTs = getVTList(VT1, VT2, VT3);
10656 SDValue Ops[] = { Op1, Op2 };
10657 return getMachineNode(Opcode, dl, VTs, Ops);
10658}
10659
10661 EVT VT1, EVT VT2, EVT VT3,
10662 SDValue Op1, SDValue Op2,
10663 SDValue Op3) {
10664 SDVTList VTs = getVTList(VT1, VT2, VT3);
10665 SDValue Ops[] = { Op1, Op2, Op3 };
10666 return getMachineNode(Opcode, dl, VTs, Ops);
10667}
10668
10670 EVT VT1, EVT VT2, EVT VT3,
10671 ArrayRef<SDValue> Ops) {
10672 SDVTList VTs = getVTList(VT1, VT2, VT3);
10673 return getMachineNode(Opcode, dl, VTs, Ops);
10674}
10675
10677 ArrayRef<EVT> ResultTys,
10678 ArrayRef<SDValue> Ops) {
10679 SDVTList VTs = getVTList(ResultTys);
10680 return getMachineNode(Opcode, dl, VTs, Ops);
10681}
10682
10684 SDVTList VTs,
10685 ArrayRef<SDValue> Ops) {
10686 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
10688 void *IP = nullptr;
10689
10690 if (DoCSE) {
10692 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
10693 IP = nullptr;
10694 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10695 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
10696 }
10697 }
10698
10699 // Allocate a new MachineSDNode.
10700 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10701 createOperands(N, Ops);
10702
10703 if (DoCSE)
10704 CSEMap.InsertNode(N, IP);
10705
10706 InsertNode(N);
10707 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
10708 return N;
10709}
10710
10711/// getTargetExtractSubreg - A convenience function for creating
10712/// TargetOpcode::EXTRACT_SUBREG nodes.
10714 SDValue Operand) {
10715 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10716 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
10717 VT, Operand, SRIdxVal);
10718 return SDValue(Subreg, 0);
10719}
10720
10721/// getTargetInsertSubreg - A convenience function for creating
10722/// TargetOpcode::INSERT_SUBREG nodes.
10724 SDValue Operand, SDValue Subreg) {
10725 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10726 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
10727 VT, Operand, Subreg, SRIdxVal);
10728 return SDValue(Result, 0);
10729}
10730
10731/// getNodeIfExists - Get the specified node if it's already available, or
10732/// else return NULL.
10734 ArrayRef<SDValue> Ops) {
10735 SDNodeFlags Flags;
10736 if (Inserter)
10737 Flags = Inserter->getFlags();
10738 return getNodeIfExists(Opcode, VTList, Ops, Flags);
10739}
10740
10743 const SDNodeFlags Flags) {
10744 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10746 AddNodeIDNode(ID, Opcode, VTList, Ops);
10747 void *IP = nullptr;
10748 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
10749 E->intersectFlagsWith(Flags);
10750 return E;
10751 }
10752 }
10753 return nullptr;
10754}
10755
10756/// doesNodeExist - Check if a node exists without modifying its flags.
10757bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
10758 ArrayRef<SDValue> Ops) {
10759 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10761 AddNodeIDNode(ID, Opcode, VTList, Ops);
10762 void *IP = nullptr;
10763 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
10764 return true;
10765 }
10766 return false;
10767}
10768
10769/// getDbgValue - Creates a SDDbgValue node.
10770///
10771/// SDNode
10773 SDNode *N, unsigned R, bool IsIndirect,
10774 const DebugLoc &DL, unsigned O) {
10775 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10776 "Expected inlined-at fields to agree");
10777 return new (DbgInfo->getAlloc())
10778 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
10779 {}, IsIndirect, DL, O,
10780 /*IsVariadic=*/false);
10781}
10782
10783/// Constant
10785 DIExpression *Expr,
10786 const Value *C,
10787 const DebugLoc &DL, unsigned O) {
10788 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10789 "Expected inlined-at fields to agree");
10790 return new (DbgInfo->getAlloc())
10791 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
10792 /*IsIndirect=*/false, DL, O,
10793 /*IsVariadic=*/false);
10794}
10795
10796/// FrameIndex
10798 DIExpression *Expr, unsigned FI,
10799 bool IsIndirect,
10800 const DebugLoc &DL,
10801 unsigned O) {
10802 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10803 "Expected inlined-at fields to agree");
10804 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
10805}
10806
10807/// FrameIndex with dependencies
10809 DIExpression *Expr, unsigned FI,
10810 ArrayRef<SDNode *> Dependencies,
10811 bool IsIndirect,
10812 const DebugLoc &DL,
10813 unsigned O) {
10814 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10815 "Expected inlined-at fields to agree");
10816 return new (DbgInfo->getAlloc())
10817 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
10818 Dependencies, IsIndirect, DL, O,
10819 /*IsVariadic=*/false);
10820}
10821
10822/// VReg
10824 unsigned VReg, bool IsIndirect,
10825 const DebugLoc &DL, unsigned O) {
10826 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10827 "Expected inlined-at fields to agree");
10828 return new (DbgInfo->getAlloc())
10829 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
10830 {}, IsIndirect, DL, O,
10831 /*IsVariadic=*/false);
10832}
10833
10836 ArrayRef<SDNode *> Dependencies,
10837 bool IsIndirect, const DebugLoc &DL,
10838 unsigned O, bool IsVariadic) {
10839 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10840 "Expected inlined-at fields to agree");
10841 return new (DbgInfo->getAlloc())
10842 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
10843 DL, O, IsVariadic);
10844}
10845
10847 unsigned OffsetInBits, unsigned SizeInBits,
10848 bool InvalidateDbg) {
10849 SDNode *FromNode = From.getNode();
10850 SDNode *ToNode = To.getNode();
10851 assert(FromNode && ToNode && "Can't modify dbg values");
10852
10853 // PR35338
10854 // TODO: assert(From != To && "Redundant dbg value transfer");
10855 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
10856 if (From == To || FromNode == ToNode)
10857 return;
10858
10859 if (!FromNode->getHasDebugValue())
10860 return;
10861
10862 SDDbgOperand FromLocOp =
10863 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
10865
10867 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
10868 if (Dbg->isInvalidated())
10869 continue;
10870
10871 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
10872
10873 // Create a new location ops vector that is equal to the old vector, but
10874 // with each instance of FromLocOp replaced with ToLocOp.
10875 bool Changed = false;
10876 auto NewLocOps = Dbg->copyLocationOps();
10877 std::replace_if(
10878 NewLocOps.begin(), NewLocOps.end(),
10879 [&Changed, FromLocOp](const SDDbgOperand &Op) {
10880 bool Match = Op == FromLocOp;
10881 Changed |= Match;
10882 return Match;
10883 },
10884 ToLocOp);
10885 // Ignore this SDDbgValue if we didn't find a matching location.
10886 if (!Changed)
10887 continue;
10888
10889 DIVariable *Var = Dbg->getVariable();
10890 auto *Expr = Dbg->getExpression();
10891 // If a fragment is requested, update the expression.
10892 if (SizeInBits) {
10893 // When splitting a larger (e.g., sign-extended) value whose
10894 // lower bits are described with an SDDbgValue, do not attempt
10895 // to transfer the SDDbgValue to the upper bits.
10896 if (auto FI = Expr->getFragmentInfo())
10897 if (OffsetInBits + SizeInBits > FI->SizeInBits)
10898 continue;
10899 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
10900 SizeInBits);
10901 if (!Fragment)
10902 continue;
10903 Expr = *Fragment;
10904 }
10905
10906 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
10907 // Clone the SDDbgValue and move it to To.
10908 SDDbgValue *Clone = getDbgValueList(
10909 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
10910 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
10911 Dbg->isVariadic());
10912 ClonedDVs.push_back(Clone);
10913
10914 if (InvalidateDbg) {
10915 // Invalidate value and indicate the SDDbgValue should not be emitted.
10916 Dbg->setIsInvalidated();
10917 Dbg->setIsEmitted();
10918 }
10919 }
10920
10921 for (SDDbgValue *Dbg : ClonedDVs) {
10922 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
10923 "Transferred DbgValues should depend on the new SDNode");
10924 AddDbgValue(Dbg, false);
10925 }
10926}
10927
10929 if (!N.getHasDebugValue())
10930 return;
10931
10933 for (auto *DV : GetDbgValues(&N)) {
10934 if (DV->isInvalidated())
10935 continue;
10936 switch (N.getOpcode()) {
10937 default:
10938 break;
10939 case ISD::ADD: {
10940 SDValue N0 = N.getOperand(0);
10941 SDValue N1 = N.getOperand(1);
10942 if (!isa<ConstantSDNode>(N0)) {
10943 bool RHSConstant = isa<ConstantSDNode>(N1);
10945 if (RHSConstant)
10946 Offset = N.getConstantOperandVal(1);
10947 // We are not allowed to turn indirect debug values variadic, so
10948 // don't salvage those.
10949 if (!RHSConstant && DV->isIndirect())
10950 continue;
10951
10952 // Rewrite an ADD constant node into a DIExpression. Since we are
10953 // performing arithmetic to compute the variable's *value* in the
10954 // DIExpression, we need to mark the expression with a
10955 // DW_OP_stack_value.
10956 auto *DIExpr = DV->getExpression();
10957 auto NewLocOps = DV->copyLocationOps();
10958 bool Changed = false;
10959 size_t OrigLocOpsSize = NewLocOps.size();
10960 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
10961 // We're not given a ResNo to compare against because the whole
10962 // node is going away. We know that any ISD::ADD only has one
10963 // result, so we can assume any node match is using the result.
10964 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
10965 NewLocOps[i].getSDNode() != &N)
10966 continue;
10967 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
10968 if (RHSConstant) {
10971 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
10972 } else {
10973 // Convert to a variadic expression (if not already).
10974 // convertToVariadicExpression() returns a const pointer, so we use
10975 // a temporary const variable here.
10976 const auto *TmpDIExpr =
10980 ExprOps.push_back(NewLocOps.size());
10981 ExprOps.push_back(dwarf::DW_OP_plus);
10984 NewLocOps.push_back(RHS);
10985 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
10986 }
10987 Changed = true;
10988 }
10989 (void)Changed;
10990 assert(Changed && "Salvage target doesn't use N");
10991
10992 bool IsVariadic =
10993 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
10994
10995 auto AdditionalDependencies = DV->getAdditionalDependencies();
10996 SDDbgValue *Clone = getDbgValueList(
10997 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
10998 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
10999 ClonedDVs.push_back(Clone);
11000 DV->setIsInvalidated();
11001 DV->setIsEmitted();
11002 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
11003 N0.getNode()->dumprFull(this);
11004 dbgs() << " into " << *DIExpr << '\n');
11005 }
11006 break;
11007 }
11008 case ISD::TRUNCATE: {
11009 SDValue N0 = N.getOperand(0);
11010 TypeSize FromSize = N0.getValueSizeInBits();
11011 TypeSize ToSize = N.getValueSizeInBits(0);
11012
11013 DIExpression *DbgExpression = DV->getExpression();
11014 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
11015 auto NewLocOps = DV->copyLocationOps();
11016 bool Changed = false;
11017 for (size_t i = 0; i < NewLocOps.size(); ++i) {
11018 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11019 NewLocOps[i].getSDNode() != &N)
11020 continue;
11021
11022 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11023 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
11024 Changed = true;
11025 }
11026 assert(Changed && "Salvage target doesn't use N");
11027 (void)Changed;
11028
11029 SDDbgValue *Clone =
11030 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
11031 DV->getAdditionalDependencies(), DV->isIndirect(),
11032 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
11033
11034 ClonedDVs.push_back(Clone);
11035 DV->setIsInvalidated();
11036 DV->setIsEmitted();
11037 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
11038 dbgs() << " into " << *DbgExpression << '\n');
11039 break;
11040 }
11041 }
11042 }
11043
11044 for (SDDbgValue *Dbg : ClonedDVs) {
11045 assert(!Dbg->getSDNodes().empty() &&
11046 "Salvaged DbgValue should depend on a new SDNode");
11047 AddDbgValue(Dbg, false);
11048 }
11049}
11050
11051/// Creates a SDDbgLabel node.
11053 const DebugLoc &DL, unsigned O) {
11054 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
11055 "Expected inlined-at fields to agree");
11056 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
11057}
11058
11059namespace {
11060
11061/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
11062/// pointed to by a use iterator is deleted, increment the use iterator
11063/// so that it doesn't dangle.
11064///
11065class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
11068
11069 void NodeDeleted(SDNode *N, SDNode *E) override {
11070 // Increment the iterator as needed.
11071 while (UI != UE && N == *UI)
11072 ++UI;
11073 }
11074
11075public:
11076 RAUWUpdateListener(SelectionDAG &d,
11079 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11080};
11081
11082} // end anonymous namespace
11083
11084/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11085/// This can cause recursive merging of nodes in the DAG.
11086///
11087/// This version assumes From has a single result value.
11088///
11090 SDNode *From = FromN.getNode();
11091 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11092 "Cannot replace with this method!");
11093 assert(From != To.getNode() && "Cannot replace uses of with self");
11094
11095 // Preserve Debug Values
11096 transferDbgValues(FromN, To);
11097 // Preserve extra info.
11098 copyExtraInfo(From, To.getNode());
11099
11100 // Iterate over all the existing uses of From. New uses will be added
11101 // to the beginning of the use list, which we avoid visiting.
11102 // This specifically avoids visiting uses of From that arise while the
11103 // replacement is happening, because any such uses would be the result
11104 // of CSE: If an existing node looks like From after one of its operands
11105 // is replaced by To, we don't want to replace of all its users with To
11106 // too. See PR3018 for more info.
11107 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11108 RAUWUpdateListener Listener(*this, UI, UE);
11109 while (UI != UE) {
11110 SDNode *User = *UI;
11111
11112 // This node is about to morph, remove its old self from the CSE maps.
11113 RemoveNodeFromCSEMaps(User);
11114
11115 // A user can appear in a use list multiple times, and when this
11116 // happens the uses are usually next to each other in the list.
11117 // To help reduce the number of CSE recomputations, process all
11118 // the uses of this user that we can find this way.
11119 do {
11120 SDUse &Use = UI.getUse();
11121 ++UI;
11122 Use.set(To);
11123 if (To->isDivergent() != From->isDivergent())
11125 } while (UI != UE && *UI == User);
11126 // Now that we have modified User, add it back to the CSE maps. If it
11127 // already exists there, recursively merge the results together.
11128 AddModifiedNodeToCSEMaps(User);
11129 }
11130
11131 // If we just RAUW'd the root, take note.
11132 if (FromN == getRoot())
11133 setRoot(To);
11134}
11135
11136/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11137/// This can cause recursive merging of nodes in the DAG.
11138///
11139/// This version assumes that for each value of From, there is a
11140/// corresponding value in To in the same position with the same type.
11141///
11143#ifndef NDEBUG
11144 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11145 assert((!From->hasAnyUseOfValue(i) ||
11146 From->getValueType(i) == To->getValueType(i)) &&
11147 "Cannot use this version of ReplaceAllUsesWith!");
11148#endif
11149
11150 // Handle the trivial case.
11151 if (From == To)
11152 return;
11153
11154 // Preserve Debug Info. Only do this if there's a use.
11155 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11156 if (From->hasAnyUseOfValue(i)) {
11157 assert((i < To->getNumValues()) && "Invalid To location");
11159 }
11160 // Preserve extra info.
11161 copyExtraInfo(From, To);
11162
11163 // Iterate over just the existing users of From. See the comments in
11164 // the ReplaceAllUsesWith above.
11165 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11166 RAUWUpdateListener Listener(*this, UI, UE);
11167 while (UI != UE) {
11168 SDNode *User = *UI;
11169
11170 // This node is about to morph, remove its old self from the CSE maps.
11171 RemoveNodeFromCSEMaps(User);
11172
11173 // A user can appear in a use list multiple times, and when this
11174 // happens the uses are usually next to each other in the list.
11175 // To help reduce the number of CSE recomputations, process all
11176 // the uses of this user that we can find this way.
11177 do {
11178 SDUse &Use = UI.getUse();
11179 ++UI;
11180 Use.setNode(To);
11181 if (To->isDivergent() != From->isDivergent())
11183 } while (UI != UE && *UI == User);
11184
11185 // Now that we have modified User, add it back to the CSE maps. If it
11186 // already exists there, recursively merge the results together.
11187 AddModifiedNodeToCSEMaps(User);
11188 }
11189
11190 // If we just RAUW'd the root, take note.
11191 if (From == getRoot().getNode())
11192 setRoot(SDValue(To, getRoot().getResNo()));
11193}
11194
11195/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11196/// This can cause recursive merging of nodes in the DAG.
11197///
11198/// This version can replace From with any result values. To must match the
11199/// number and types of values returned by From.
11201 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11202 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11203
11204 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11205 // Preserve Debug Info.
11206 transferDbgValues(SDValue(From, i), To[i]);
11207 // Preserve extra info.
11208 copyExtraInfo(From, To[i].getNode());
11209 }
11210
11211 // Iterate over just the existing users of From. See the comments in
11212 // the ReplaceAllUsesWith above.
11213 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11214 RAUWUpdateListener Listener(*this, UI, UE);
11215 while (UI != UE) {
11216 SDNode *User = *UI;
11217
11218 // This node is about to morph, remove its old self from the CSE maps.
11219 RemoveNodeFromCSEMaps(User);
11220
11221 // A user can appear in a use list multiple times, and when this happens the
11222 // uses are usually next to each other in the list. To help reduce the
11223 // number of CSE and divergence recomputations, process all the uses of this
11224 // user that we can find this way.
11225 bool To_IsDivergent = false;
11226 do {
11227 SDUse &Use = UI.getUse();
11228 const SDValue &ToOp = To[Use.getResNo()];
11229 ++UI;
11230 Use.set(ToOp);
11231 To_IsDivergent |= ToOp->isDivergent();
11232 } while (UI != UE && *UI == User);
11233
11234 if (To_IsDivergent != From->isDivergent())
11236
11237 // Now that we have modified User, add it back to the CSE maps. If it
11238 // already exists there, recursively merge the results together.
11239 AddModifiedNodeToCSEMaps(User);
11240 }
11241
11242 // If we just RAUW'd the root, take note.
11243 if (From == getRoot().getNode())
11244 setRoot(SDValue(To[getRoot().getResNo()]));
11245}
11246
11247/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11248/// uses of other values produced by From.getNode() alone. The Deleted
11249/// vector is handled the same way as for ReplaceAllUsesWith.
11251 // Handle the really simple, really trivial case efficiently.
11252 if (From == To) return;
11253
11254 // Handle the simple, trivial, case efficiently.
11255 if (From.getNode()->getNumValues() == 1) {
11257 return;
11258 }
11259
11260 // Preserve Debug Info.
11262 copyExtraInfo(From.getNode(), To.getNode());
11263
11264 // Iterate over just the existing users of From. See the comments in
11265 // the ReplaceAllUsesWith above.
11266 SDNode::use_iterator UI = From.getNode()->use_begin(),
11267 UE = From.getNode()->use_end();
11268 RAUWUpdateListener Listener(*this, UI, UE);
11269 while (UI != UE) {
11270 SDNode *User = *UI;
11271 bool UserRemovedFromCSEMaps = false;
11272
11273 // A user can appear in a use list multiple times, and when this
11274 // happens the uses are usually next to each other in the list.
11275 // To help reduce the number of CSE recomputations, process all
11276 // the uses of this user that we can find this way.
11277 do {
11278 SDUse &Use = UI.getUse();
11279
11280 // Skip uses of different values from the same node.
11281 if (Use.getResNo() != From.getResNo()) {
11282 ++UI;
11283 continue;
11284 }
11285
11286 // If this node hasn't been modified yet, it's still in the CSE maps,
11287 // so remove its old self from the CSE maps.
11288 if (!UserRemovedFromCSEMaps) {
11289 RemoveNodeFromCSEMaps(User);
11290 UserRemovedFromCSEMaps = true;
11291 }
11292
11293 ++UI;
11294 Use.set(To);
11295 if (To->isDivergent() != From->isDivergent())
11297 } while (UI != UE && *UI == User);
11298 // We are iterating over all uses of the From node, so if a use
11299 // doesn't use the specific value, no changes are made.
11300 if (!UserRemovedFromCSEMaps)
11301 continue;
11302
11303 // Now that we have modified User, add it back to the CSE maps. If it
11304 // already exists there, recursively merge the results together.
11305 AddModifiedNodeToCSEMaps(User);
11306 }
11307
11308 // If we just RAUW'd the root, take note.
11309 if (From == getRoot())
11310 setRoot(To);
11311}
11312
11313namespace {
11314
11315/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11316/// to record information about a use.
11317struct UseMemo {
11318 SDNode *User;
11319 unsigned Index;
11320 SDUse *Use;
11321};
11322
11323/// operator< - Sort Memos by User.
11324bool operator<(const UseMemo &L, const UseMemo &R) {
11325 return (intptr_t)L.User < (intptr_t)R.User;
11326}
11327
11328/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11329/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11330/// the node already has been taken care of recursively.
11331class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11333
11334 void NodeDeleted(SDNode *N, SDNode *E) override {
11335 for (UseMemo &Memo : Uses)
11336 if (Memo.User == N)
11337 Memo.User = nullptr;
11338 }
11339
11340public:
11341 RAUOVWUpdateListener(SelectionDAG &d, SmallVector<UseMemo, 4> &uses)
11342 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11343};
11344
11345} // end anonymous namespace
11346
11348 if (TLI->isSDNodeAlwaysUniform(N)) {
11349 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11350 "Conflicting divergence information!");
11351 return false;
11352 }
11353 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11354 return true;
11355 for (const auto &Op : N->ops()) {
11356 if (Op.Val.getValueType() != MVT::Other && Op.getNode()->isDivergent())
11357 return true;
11358 }
11359 return false;
11360}
11361
11363 SmallVector<SDNode *, 16> Worklist(1, N);
11364 do {
11365 N = Worklist.pop_back_val();
11366 bool IsDivergent = calculateDivergence(N);
11367 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11368 N->SDNodeBits.IsDivergent = IsDivergent;
11369 llvm::append_range(Worklist, N->uses());
11370 }
11371 } while (!Worklist.empty());
11372}
11373
11374void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11376 Order.reserve(AllNodes.size());
11377 for (auto &N : allnodes()) {
11378 unsigned NOps = N.getNumOperands();
11379 Degree[&N] = NOps;
11380 if (0 == NOps)
11381 Order.push_back(&N);
11382 }
11383 for (size_t I = 0; I != Order.size(); ++I) {
11384 SDNode *N = Order[I];
11385 for (auto *U : N->uses()) {
11386 unsigned &UnsortedOps = Degree[U];
11387 if (0 == --UnsortedOps)
11388 Order.push_back(U);
11389 }
11390 }
11391}
11392
11393#ifndef NDEBUG
11395 std::vector<SDNode *> TopoOrder;
11396 CreateTopologicalOrder(TopoOrder);
11397 for (auto *N : TopoOrder) {
11398 assert(calculateDivergence(N) == N->isDivergent() &&
11399 "Divergence bit inconsistency detected");
11400 }
11401}
11402#endif
11403
11404/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11405/// uses of other values produced by From.getNode() alone. The same value
11406/// may appear in both the From and To list. The Deleted vector is
11407/// handled the same way as for ReplaceAllUsesWith.
11409 const SDValue *To,
11410 unsigned Num){
11411 // Handle the simple, trivial case efficiently.
11412 if (Num == 1)
11413 return ReplaceAllUsesOfValueWith(*From, *To);
11414
11415 transferDbgValues(*From, *To);
11416 copyExtraInfo(From->getNode(), To->getNode());
11417
11418 // Read up all the uses and make records of them. This helps
11419 // processing new uses that are introduced during the
11420 // replacement process.
11422 for (unsigned i = 0; i != Num; ++i) {
11423 unsigned FromResNo = From[i].getResNo();
11424 SDNode *FromNode = From[i].getNode();
11425 for (SDNode::use_iterator UI = FromNode->use_begin(),
11426 E = FromNode->use_end(); UI != E; ++UI) {
11427 SDUse &Use = UI.getUse();
11428 if (Use.getResNo() == FromResNo) {
11429 UseMemo Memo = { *UI, i, &Use };
11430 Uses.push_back(Memo);
11431 }
11432 }
11433 }
11434
11435 // Sort the uses, so that all the uses from a given User are together.
11437 RAUOVWUpdateListener Listener(*this, Uses);
11438
11439 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
11440 UseIndex != UseIndexEnd; ) {
11441 // We know that this user uses some value of From. If it is the right
11442 // value, update it.
11443 SDNode *User = Uses[UseIndex].User;
11444 // If the node has been deleted by recursive CSE updates when updating
11445 // another node, then just skip this entry.
11446 if (User == nullptr) {
11447 ++UseIndex;
11448 continue;
11449 }
11450
11451 // This node is about to morph, remove its old self from the CSE maps.
11452 RemoveNodeFromCSEMaps(User);
11453
11454 // The Uses array is sorted, so all the uses for a given User
11455 // are next to each other in the list.
11456 // To help reduce the number of CSE recomputations, process all
11457 // the uses of this user that we can find this way.
11458 do {
11459 unsigned i = Uses[UseIndex].Index;
11460 SDUse &Use = *Uses[UseIndex].Use;
11461 ++UseIndex;
11462
11463 Use.set(To[i]);
11464 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
11465
11466 // Now that we have modified User, add it back to the CSE maps. If it
11467 // already exists there, recursively merge the results together.
11468 AddModifiedNodeToCSEMaps(User);
11469 }
11470}
11471
11472/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
11473/// based on their topological order. It returns the maximum id and a vector
11474/// of the SDNodes* in assigned order by reference.
11476 unsigned DAGSize = 0;
11477
11478 // SortedPos tracks the progress of the algorithm. Nodes before it are
11479 // sorted, nodes after it are unsorted. When the algorithm completes
11480 // it is at the end of the list.
11481 allnodes_iterator SortedPos = allnodes_begin();
11482
11483 // Visit all the nodes. Move nodes with no operands to the front of
11484 // the list immediately. Annotate nodes that do have operands with their
11485 // operand count. Before we do this, the Node Id fields of the nodes
11486 // may contain arbitrary values. After, the Node Id fields for nodes
11487 // before SortedPos will contain the topological sort index, and the
11488 // Node Id fields for nodes At SortedPos and after will contain the
11489 // count of outstanding operands.
11491 checkForCycles(&N, this);
11492 unsigned Degree = N.getNumOperands();
11493 if (Degree == 0) {
11494 // A node with no uses, add it to the result array immediately.
11495 N.setNodeId(DAGSize++);
11496 allnodes_iterator Q(&N);
11497 if (Q != SortedPos)
11498 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
11499 assert(SortedPos != AllNodes.end() && "Overran node list");
11500 ++SortedPos;
11501 } else {
11502 // Temporarily use the Node Id as scratch space for the degree count.
11503 N.setNodeId(Degree);
11504 }
11505 }
11506
11507 // Visit all the nodes. As we iterate, move nodes into sorted order,
11508 // such that by the time the end is reached all nodes will be sorted.
11509 for (SDNode &Node : allnodes()) {
11510 SDNode *N = &Node;
11511 checkForCycles(N, this);
11512 // N is in sorted position, so all its uses have one less operand
11513 // that needs to be sorted.
11514 for (SDNode *P : N->uses()) {
11515 unsigned Degree = P->getNodeId();
11516 assert(Degree != 0 && "Invalid node degree");
11517 --Degree;
11518 if (Degree == 0) {
11519 // All of P's operands are sorted, so P may sorted now.
11520 P->setNodeId(DAGSize++);
11521 if (P->getIterator() != SortedPos)
11522 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
11523 assert(SortedPos != AllNodes.end() && "Overran node list");
11524 ++SortedPos;
11525 } else {
11526 // Update P's outstanding operand count.
11527 P->setNodeId(Degree);
11528 }
11529 }
11530 if (Node.getIterator() == SortedPos) {
11531#ifndef NDEBUG
11533 SDNode *S = &*++I;
11534 dbgs() << "Overran sorted position:\n";
11535 S->dumprFull(this); dbgs() << "\n";
11536 dbgs() << "Checking if this is due to cycles\n";
11537 checkForCycles(this, true);
11538#endif
11539 llvm_unreachable(nullptr);
11540 }
11541 }
11542
11543 assert(SortedPos == AllNodes.end() &&
11544 "Topological sort incomplete!");
11545 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
11546 "First node in topological sort is not the entry token!");
11547 assert(AllNodes.front().getNodeId() == 0 &&
11548 "First node in topological sort has non-zero id!");
11549 assert(AllNodes.front().getNumOperands() == 0 &&
11550 "First node in topological sort has operands!");
11551 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
11552 "Last node in topologic sort has unexpected id!");
11553 assert(AllNodes.back().use_empty() &&
11554 "Last node in topologic sort has users!");
11555 assert(DAGSize == allnodes_size() && "Node count mismatch!");
11556 return DAGSize;
11557}
11558
11559/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
11560/// value is produced by SD.
11561void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
11562 for (SDNode *SD : DB->getSDNodes()) {
11563 if (!SD)
11564 continue;
11565 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
11566 SD->setHasDebugValue(true);
11567 }
11568 DbgInfo->add(DB, isParameter);
11569}
11570
11571void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
11572
11574 SDValue NewMemOpChain) {
11575 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
11576 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
11577 // The new memory operation must have the same position as the old load in
11578 // terms of memory dependency. Create a TokenFactor for the old load and new
11579 // memory operation and update uses of the old load's output chain to use that
11580 // TokenFactor.
11581 if (OldChain == NewMemOpChain || OldChain.use_empty())
11582 return NewMemOpChain;
11583
11584 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
11585 OldChain, NewMemOpChain);
11586 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
11587 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
11588 return TokenFactor;
11589}
11590
11592 SDValue NewMemOp) {
11593 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
11594 SDValue OldChain = SDValue(OldLoad, 1);
11595 SDValue NewMemOpChain = NewMemOp.getValue(1);
11596 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
11597}
11598
11600 Function **OutFunction) {
11601 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
11602
11603 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
11604 auto *Module = MF->getFunction().getParent();
11605 auto *Function = Module->getFunction(Symbol);
11606
11607 if (OutFunction != nullptr)
11608 *OutFunction = Function;
11609
11610 if (Function != nullptr) {
11611 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
11612 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
11613 }
11614
11615 std::string ErrorStr;
11616 raw_string_ostream ErrorFormatter(ErrorStr);
11617 ErrorFormatter << "Undefined external symbol ";
11618 ErrorFormatter << '"' << Symbol << '"';
11619 report_fatal_error(Twine(ErrorFormatter.str()));
11620}
11621
11622//===----------------------------------------------------------------------===//
11623// SDNode Class
11624//===----------------------------------------------------------------------===//
11625
11627 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11628 return Const != nullptr && Const->isZero();
11629}
11630
11632 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
11633 return Const != nullptr && Const->isZero() && !Const->isNegative();
11634}
11635
11637 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11638 return Const != nullptr && Const->isAllOnes();
11639}
11640
11642 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11643 return Const != nullptr && Const->isOne();
11644}
11645
11647 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11648 return Const != nullptr && Const->isMinSignedValue();
11649}
11650
11651bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
11652 unsigned OperandNo) {
11653 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
11654 // TODO: Target-specific opcodes could be added.
11655 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
11656 /*AllowTruncation*/ true)) {
11657 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
11658 switch (Opcode) {
11659 case ISD::ADD:
11660 case ISD::OR:
11661 case ISD::XOR:
11662 case ISD::UMAX:
11663 return Const.isZero();
11664 case ISD::MUL:
11665 return Const.isOne();
11666 case ISD::AND:
11667 case ISD::UMIN:
11668 return Const.isAllOnes();
11669 case ISD::SMAX:
11670 return Const.isMinSignedValue();
11671 case ISD::SMIN:
11672 return Const.isMaxSignedValue();
11673 case ISD::SUB:
11674 case ISD::SHL:
11675 case ISD::SRA:
11676 case ISD::SRL:
11677 return OperandNo == 1 && Const.isZero();
11678 case ISD::UDIV:
11679 case ISD::SDIV:
11680 return OperandNo == 1 && Const.isOne();
11681 }
11682 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
11683 switch (Opcode) {
11684 case ISD::FADD:
11685 return ConstFP->isZero() &&
11686 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
11687 case ISD::FSUB:
11688 return OperandNo == 1 && ConstFP->isZero() &&
11689 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
11690 case ISD::FMUL:
11691 return ConstFP->isExactlyValue(1.0);
11692 case ISD::FDIV:
11693 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
11694 case ISD::FMINNUM:
11695 case ISD::FMAXNUM: {
11696 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
11697 EVT VT = V.getValueType();
11699 APFloat NeutralAF = !Flags.hasNoNaNs()
11700 ? APFloat::getQNaN(Semantics)
11701 : !Flags.hasNoInfs()
11702 ? APFloat::getInf(Semantics)
11703 : APFloat::getLargest(Semantics);
11704 if (Opcode == ISD::FMAXNUM)
11705 NeutralAF.changeSign();
11706
11707 return ConstFP->isExactlyValue(NeutralAF);
11708 }
11709 }
11710 }
11711 return false;
11712}
11713
11715 while (V.getOpcode() == ISD::BITCAST)
11716 V = V.getOperand(0);
11717 return V;
11718}
11719
11721 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
11722 V = V.getOperand(0);
11723 return V;
11724}
11725
11727 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11728 V = V.getOperand(0);
11729 return V;
11730}
11731
11733 while (V.getOpcode() == ISD::TRUNCATE)
11734 V = V.getOperand(0);
11735 return V;
11736}
11737
11738bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
11739 if (V.getOpcode() != ISD::XOR)
11740 return false;
11741 V = peekThroughBitcasts(V.getOperand(1));
11742 unsigned NumBits = V.getScalarValueSizeInBits();
11743 ConstantSDNode *C =
11744 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
11745 return C && (C->getAPIntValue().countr_one() >= NumBits);
11746}
11747
11749 bool AllowTruncation) {
11750 EVT VT = N.getValueType();
11751 APInt DemandedElts = VT.isFixedLengthVector()
11753 : APInt(1, 1);
11754 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
11755}
11756
11758 bool AllowUndefs,
11759 bool AllowTruncation) {
11760 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
11761 return CN;
11762
11763 // SplatVectors can truncate their operands. Ignore that case here unless
11764 // AllowTruncation is set.
11765 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
11766 EVT VecEltVT = N->getValueType(0).getVectorElementType();
11767 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11768 EVT CVT = CN->getValueType(0);
11769 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
11770 if (AllowTruncation || CVT == VecEltVT)
11771 return CN;
11772 }
11773 }
11774
11775 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
11776 BitVector UndefElements;
11777 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
11778
11779 // BuildVectors can truncate their operands. Ignore that case here unless
11780 // AllowTruncation is set.
11781 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
11782 if (CN && (UndefElements.none() || AllowUndefs)) {
11783 EVT CVT = CN->getValueType(0);
11784 EVT NSVT = N.getValueType().getScalarType();
11785 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
11786 if (AllowTruncation || (CVT == NSVT))
11787 return CN;
11788 }
11789 }
11790
11791 return nullptr;
11792}
11793
11795 EVT VT = N.getValueType();
11796 APInt DemandedElts = VT.isFixedLengthVector()
11798 : APInt(1, 1);
11799 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
11800}
11801
11803 const APInt &DemandedElts,
11804 bool AllowUndefs) {
11805 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
11806 return CN;
11807
11808 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
11809 BitVector UndefElements;
11810 ConstantFPSDNode *CN =
11811 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
11812 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
11813 if (CN && (UndefElements.none() || AllowUndefs))
11814 return CN;
11815 }
11816
11817 if (N.getOpcode() == ISD::SPLAT_VECTOR)
11818 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
11819 return CN;
11820
11821 return nullptr;
11822}
11823
11824bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
11825 // TODO: may want to use peekThroughBitcast() here.
11826 ConstantSDNode *C =
11827 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
11828 return C && C->isZero();
11829}
11830
11831bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
11832 ConstantSDNode *C =
11833 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
11834 return C && C->isOne();
11835}
11836
11837bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
11839 unsigned BitWidth = N.getScalarValueSizeInBits();
11840 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
11841 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
11842}
11843
11845 DropOperands();
11846}
11847
11848MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
11849 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
11850 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
11851 MemSDNodeBits.IsVolatile = MMO->isVolatile();
11852 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
11853 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
11854 MemSDNodeBits.IsInvariant = MMO->isInvariant();
11855
11856 // We check here that the size of the memory operand fits within the size of
11857 // the MMO. This is because the MMO might indicate only a possible address
11858 // range instead of specifying the affected memory addresses precisely.
11859 assert(
11860 (!MMO->getType().isValid() ||
11862 "Size mismatch!");
11863}
11864
11865/// Profile - Gather unique data for the node.
11866///
11868 AddNodeIDNode(ID, this);
11869}
11870
11871namespace {
11872
11873 struct EVTArray {
11874 std::vector<EVT> VTs;
11875
11876 EVTArray() {
11877 VTs.reserve(MVT::VALUETYPE_SIZE);
11878 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
11879 VTs.push_back(MVT((MVT::SimpleValueType)i));
11880 }
11881 };
11882
11883} // end anonymous namespace
11884
11885/// getValueTypeList - Return a pointer to the specified value type.
11886///
11887const EVT *SDNode::getValueTypeList(EVT VT) {
11888 static std::set<EVT, EVT::compareRawBits> EVTs;
11889 static EVTArray SimpleVTArray;
11890 static sys::SmartMutex<true> VTMutex;
11891
11892 if (VT.isExtended()) {
11893 sys::SmartScopedLock<true> Lock(VTMutex);
11894 return &(*EVTs.insert(VT).first);
11895 }
11896 assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
11897 return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
11898}
11899
11900/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
11901/// indicated value. This method ignores uses of other values defined by this
11902/// operation.
11903bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
11904 assert(Value < getNumValues() && "Bad value!");
11905
11906 // TODO: Only iterate over uses of a given value of the node
11907 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
11908 if (UI.getUse().getResNo() == Value) {
11909 if (NUses == 0)
11910 return false;
11911 --NUses;
11912 }
11913 }
11914
11915 // Found exactly the right number of uses?
11916 return NUses == 0;
11917}
11918
11919/// hasAnyUseOfValue - Return true if there are any use of the indicated
11920/// value. This method ignores uses of other values defined by this operation.
11921bool SDNode::hasAnyUseOfValue(unsigned Value) const {
11922 assert(Value < getNumValues() && "Bad value!");
11923
11924 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
11925 if (UI.getUse().getResNo() == Value)
11926 return true;
11927
11928 return false;
11929}
11930
11931/// isOnlyUserOf - Return true if this node is the only use of N.
11932bool SDNode::isOnlyUserOf(const SDNode *N) const {
11933 bool Seen = false;
11934 for (const SDNode *User : N->uses()) {
11935 if (User == this)
11936 Seen = true;
11937 else
11938 return false;
11939 }
11940
11941 return Seen;
11942}
11943
11944/// Return true if the only users of N are contained in Nodes.
11946 bool Seen = false;
11947 for (const SDNode *User : N->uses()) {
11948 if (llvm::is_contained(Nodes, User))
11949 Seen = true;
11950 else
11951 return false;
11952 }
11953
11954 return Seen;
11955}
11956
11957/// isOperand - Return true if this node is an operand of N.
11958bool SDValue::isOperandOf(const SDNode *N) const {
11959 return is_contained(N->op_values(), *this);
11960}
11961
11962bool SDNode::isOperandOf(const SDNode *N) const {
11963 return any_of(N->op_values(),
11964 [this](SDValue Op) { return this == Op.getNode(); });
11965}
11966
11967/// reachesChainWithoutSideEffects - Return true if this operand (which must
11968/// be a chain) reaches the specified operand without crossing any
11969/// side-effecting instructions on any chain path. In practice, this looks
11970/// through token factors and non-volatile loads. In order to remain efficient,
11971/// this only looks a couple of nodes in, it does not do an exhaustive search.
11972///
11973/// Note that we only need to examine chains when we're searching for
11974/// side-effects; SelectionDAG requires that all side-effects are represented
11975/// by chains, even if another operand would force a specific ordering. This
11976/// constraint is necessary to allow transformations like splitting loads.
11978 unsigned Depth) const {
11979 if (*this == Dest) return true;
11980
11981 // Don't search too deeply, we just want to be able to see through
11982 // TokenFactor's etc.
11983 if (Depth == 0) return false;
11984
11985 // If this is a token factor, all inputs to the TF happen in parallel.
11986 if (getOpcode() == ISD::TokenFactor) {
11987 // First, try a shallow search.
11988 if (is_contained((*this)->ops(), Dest)) {
11989 // We found the chain we want as an operand of this TokenFactor.
11990 // Essentially, we reach the chain without side-effects if we could
11991 // serialize the TokenFactor into a simple chain of operations with
11992 // Dest as the last operation. This is automatically true if the
11993 // chain has one use: there are no other ordering constraints.
11994 // If the chain has more than one use, we give up: some other
11995 // use of Dest might force a side-effect between Dest and the current
11996 // node.
11997 if (Dest.hasOneUse())
11998 return true;
11999 }
12000 // Next, try a deep search: check whether every operand of the TokenFactor
12001 // reaches Dest.
12002 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
12003 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
12004 });
12005 }
12006
12007 // Loads don't have side effects, look through them.
12008 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
12009 if (Ld->isUnordered())
12010 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
12011 }
12012 return false;
12013}
12014
12015bool SDNode::hasPredecessor(const SDNode *N) const {
12018 Worklist.push_back(this);
12019 return hasPredecessorHelper(N, Visited, Worklist);
12020}
12021
12023 this->Flags.intersectWith(Flags);
12024}
12025
12026SDValue
12028 ArrayRef<ISD::NodeType> CandidateBinOps,
12029 bool AllowPartials) {
12030 // The pattern must end in an extract from index 0.
12031 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
12032 !isNullConstant(Extract->getOperand(1)))
12033 return SDValue();
12034
12035 // Match against one of the candidate binary ops.
12036 SDValue Op = Extract->getOperand(0);
12037 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
12038 return Op.getOpcode() == unsigned(BinOp);
12039 }))
12040 return SDValue();
12041
12042 // Floating-point reductions may require relaxed constraints on the final step
12043 // of the reduction because they may reorder intermediate operations.
12044 unsigned CandidateBinOp = Op.getOpcode();
12045 if (Op.getValueType().isFloatingPoint()) {
12046 SDNodeFlags Flags = Op->getFlags();
12047 switch (CandidateBinOp) {
12048 case ISD::FADD:
12049 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
12050 return SDValue();
12051 break;
12052 default:
12053 llvm_unreachable("Unhandled FP opcode for binop reduction");
12054 }
12055 }
12056
12057 // Matching failed - attempt to see if we did enough stages that a partial
12058 // reduction from a subvector is possible.
12059 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12060 if (!AllowPartials || !Op)
12061 return SDValue();
12062 EVT OpVT = Op.getValueType();
12063 EVT OpSVT = OpVT.getScalarType();
12064 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12065 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12066 return SDValue();
12067 BinOp = (ISD::NodeType)CandidateBinOp;
12068 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12070 };
12071
12072 // At each stage, we're looking for something that looks like:
12073 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12074 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12075 // i32 undef, i32 undef, i32 undef, i32 undef>
12076 // %a = binop <8 x i32> %op, %s
12077 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12078 // we expect something like:
12079 // <4,5,6,7,u,u,u,u>
12080 // <2,3,u,u,u,u,u,u>
12081 // <1,u,u,u,u,u,u,u>
12082 // While a partial reduction match would be:
12083 // <2,3,u,u,u,u,u,u>
12084 // <1,u,u,u,u,u,u,u>
12085 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12086 SDValue PrevOp;
12087 for (unsigned i = 0; i < Stages; ++i) {
12088 unsigned MaskEnd = (1 << i);
12089
12090 if (Op.getOpcode() != CandidateBinOp)
12091 return PartialReduction(PrevOp, MaskEnd);
12092
12093 SDValue Op0 = Op.getOperand(0);
12094 SDValue Op1 = Op.getOperand(1);
12095
12096 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12097 if (Shuffle) {
12098 Op = Op1;
12099 } else {
12100 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12101 Op = Op0;
12102 }
12103
12104 // The first operand of the shuffle should be the same as the other operand
12105 // of the binop.
12106 if (!Shuffle || Shuffle->getOperand(0) != Op)
12107 return PartialReduction(PrevOp, MaskEnd);
12108
12109 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12110 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12111 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12112 return PartialReduction(PrevOp, MaskEnd);
12113
12114 PrevOp = Op;
12115 }
12116
12117 // Handle subvector reductions, which tend to appear after the shuffle
12118 // reduction stages.
12119 while (Op.getOpcode() == CandidateBinOp) {
12120 unsigned NumElts = Op.getValueType().getVectorNumElements();
12121 SDValue Op0 = Op.getOperand(0);
12122 SDValue Op1 = Op.getOperand(1);
12123 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12125 Op0.getOperand(0) != Op1.getOperand(0))
12126 break;
12127 SDValue Src = Op0.getOperand(0);
12128 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12129 if (NumSrcElts != (2 * NumElts))
12130 break;
12131 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12132 Op1.getConstantOperandAPInt(1) == NumElts) &&
12133 !(Op1.getConstantOperandAPInt(1) == 0 &&
12134 Op0.getConstantOperandAPInt(1) == NumElts))
12135 break;
12136 Op = Src;
12137 }
12138
12139 BinOp = (ISD::NodeType)CandidateBinOp;
12140 return Op;
12141}
12142
12144 EVT VT = N->getValueType(0);
12145 EVT EltVT = VT.getVectorElementType();
12146 unsigned NE = VT.getVectorNumElements();
12147
12148 SDLoc dl(N);
12149
12150 // If ResNE is 0, fully unroll the vector op.
12151 if (ResNE == 0)
12152 ResNE = NE;
12153 else if (NE > ResNE)
12154 NE = ResNE;
12155
12156 if (N->getNumValues() == 2) {
12157 SmallVector<SDValue, 8> Scalars0, Scalars1;
12158 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12159 EVT VT1 = N->getValueType(1);
12160 EVT EltVT1 = VT1.getVectorElementType();
12161
12162 unsigned i;
12163 for (i = 0; i != NE; ++i) {
12164 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12165 SDValue Operand = N->getOperand(j);
12166 EVT OperandVT = Operand.getValueType();
12167
12168 // A vector operand; extract a single element.
12169 EVT OperandEltVT = OperandVT.getVectorElementType();
12170 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12171 Operand, getVectorIdxConstant(i, dl));
12172 }
12173
12174 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12175 Scalars0.push_back(EltOp);
12176 Scalars1.push_back(EltOp.getValue(1));
12177 }
12178
12179 SDValue Vec0 = getBuildVector(VT, dl, Scalars0);
12180 SDValue Vec1 = getBuildVector(VT1, dl, Scalars1);
12181 return getMergeValues({Vec0, Vec1}, dl);
12182 }
12183
12184 assert(N->getNumValues() == 1 &&
12185 "Can't unroll a vector with multiple results!");
12186
12188 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12189
12190 unsigned i;
12191 for (i= 0; i != NE; ++i) {
12192 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12193 SDValue Operand = N->getOperand(j);
12194 EVT OperandVT = Operand.getValueType();
12195 if (OperandVT.isVector()) {
12196 // A vector operand; extract a single element.
12197 EVT OperandEltVT = OperandVT.getVectorElementType();
12198 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12199 Operand, getVectorIdxConstant(i, dl));
12200 } else {
12201 // A scalar operand; just use it as is.
12202 Operands[j] = Operand;
12203 }
12204 }
12205
12206 switch (N->getOpcode()) {
12207 default: {
12208 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12209 N->getFlags()));
12210 break;
12211 }
12212 case ISD::VSELECT:
12213 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12214 break;
12215 case ISD::SHL:
12216 case ISD::SRA:
12217 case ISD::SRL:
12218 case ISD::ROTL:
12219 case ISD::ROTR:
12220 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12222 Operands[1])));
12223 break;
12225 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12226 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12227 Operands[0],
12228 getValueType(ExtVT)));
12229 }
12230 }
12231 }
12232
12233 for (; i < ResNE; ++i)
12234 Scalars.push_back(getUNDEF(EltVT));
12235
12236 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12237 return getBuildVector(VecVT, dl, Scalars);
12238}
12239
12240std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12241 SDNode *N, unsigned ResNE) {
12242 unsigned Opcode = N->getOpcode();
12243 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12244 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12245 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12246 "Expected an overflow opcode");
12247
12248 EVT ResVT = N->getValueType(0);
12249 EVT OvVT = N->getValueType(1);
12250 EVT ResEltVT = ResVT.getVectorElementType();
12251 EVT OvEltVT = OvVT.getVectorElementType();
12252 SDLoc dl(N);
12253
12254 // If ResNE is 0, fully unroll the vector op.
12255 unsigned NE = ResVT.getVectorNumElements();
12256 if (ResNE == 0)
12257 ResNE = NE;
12258 else if (NE > ResNE)
12259 NE = ResNE;
12260
12261 SmallVector<SDValue, 8> LHSScalars;
12262 SmallVector<SDValue, 8> RHSScalars;
12263 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12264 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12265
12266 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12267 SDVTList VTs = getVTList(ResEltVT, SVT);
12268 SmallVector<SDValue, 8> ResScalars;
12269 SmallVector<SDValue, 8> OvScalars;
12270 for (unsigned i = 0; i < NE; ++i) {
12271 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12272 SDValue Ov =
12273 getSelect(dl, OvEltVT, Res.getValue(1),
12274 getBoolConstant(true, dl, OvEltVT, ResVT),
12275 getConstant(0, dl, OvEltVT));
12276
12277 ResScalars.push_back(Res);
12278 OvScalars.push_back(Ov);
12279 }
12280
12281 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12282 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12283
12284 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12285 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12286 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12287 getBuildVector(NewOvVT, dl, OvScalars));
12288}
12289
12292 unsigned Bytes,
12293 int Dist) const {
12294 if (LD->isVolatile() || Base->isVolatile())
12295 return false;
12296 // TODO: probably too restrictive for atomics, revisit
12297 if (!LD->isSimple())
12298 return false;
12299 if (LD->isIndexed() || Base->isIndexed())
12300 return false;
12301 if (LD->getChain() != Base->getChain())
12302 return false;
12303 EVT VT = LD->getMemoryVT();
12304 if (VT.getSizeInBits() / 8 != Bytes)
12305 return false;
12306
12307 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12308 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12309
12310 int64_t Offset = 0;
12311 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12312 return (Dist * (int64_t)Bytes == Offset);
12313 return false;
12314}
12315
12316/// InferPtrAlignment - Infer alignment of a load / store address. Return
12317/// std::nullopt if it cannot be inferred.
12319 // If this is a GlobalAddress + cst, return the alignment.
12320 const GlobalValue *GV = nullptr;
12321 int64_t GVOffset = 0;
12322 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12323 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12324 KnownBits Known(PtrWidth);
12326 unsigned AlignBits = Known.countMinTrailingZeros();
12327 if (AlignBits)
12328 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12329 }
12330
12331 // If this is a direct reference to a stack slot, use information about the
12332 // stack slot's alignment.
12333 int FrameIdx = INT_MIN;
12334 int64_t FrameOffset = 0;
12335 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12336 FrameIdx = FI->getIndex();
12337 } else if (isBaseWithConstantOffset(Ptr) &&
12338 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12339 // Handle FI+Cst
12340 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12341 FrameOffset = Ptr.getConstantOperandVal(1);
12342 }
12343
12344 if (FrameIdx != INT_MIN) {
12346 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12347 }
12348
12349 return std::nullopt;
12350}
12351
12352/// Split the scalar node with EXTRACT_ELEMENT using the provided
12353/// VTs and return the low/high part.
12354std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12355 const SDLoc &DL,
12356 const EVT &LoVT,
12357 const EVT &HiVT) {
12358 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12359 "Split node must be a scalar type");
12360 SDValue Lo =
12362 SDValue Hi =
12364 return std::make_pair(Lo, Hi);
12365}
12366
12367/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12368/// which is split (or expanded) into two not necessarily identical pieces.
12369std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12370 // Currently all types are split in half.
12371 EVT LoVT, HiVT;
12372 if (!VT.isVector())
12373 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12374 else
12375 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12376
12377 return std::make_pair(LoVT, HiVT);
12378}
12379
12380/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12381/// type, dependent on an enveloping VT that has been split into two identical
12382/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12383std::pair<EVT, EVT>
12385 bool *HiIsEmpty) const {
12386 EVT EltTp = VT.getVectorElementType();
12387 // Examples:
12388 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12389 // custom VL=9 with enveloping VL=8/8 yields 8/1
12390 // custom VL=10 with enveloping VL=8/8 yields 8/2
12391 // etc.
12392 ElementCount VTNumElts = VT.getVectorElementCount();
12393 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12394 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12395 "Mixing fixed width and scalable vectors when enveloping a type");
12396 EVT LoVT, HiVT;
12397 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12398 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12399 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12400 *HiIsEmpty = false;
12401 } else {
12402 // Flag that hi type has zero storage size, but return split envelop type
12403 // (this would be easier if vector types with zero elements were allowed).
12404 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12405 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12406 *HiIsEmpty = true;
12407 }
12408 return std::make_pair(LoVT, HiVT);
12409}
12410
12411/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
12412/// low/high part.
12413std::pair<SDValue, SDValue>
12414SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
12415 const EVT &HiVT) {
12416 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
12417 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
12418 "Splitting vector with an invalid mixture of fixed and scalable "
12419 "vector types");
12421 N.getValueType().getVectorMinNumElements() &&
12422 "More vector elements requested than available!");
12423 SDValue Lo, Hi;
12424 Lo =
12426 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
12427 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
12428 // IDX with the runtime scaling factor of the result vector type. For
12429 // fixed-width result vectors, that runtime scaling factor is 1.
12432 return std::make_pair(Lo, Hi);
12433}
12434
12435std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
12436 const SDLoc &DL) {
12437 // Split the vector length parameter.
12438 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
12439 EVT VT = N.getValueType();
12441 "Expecting the mask to be an evenly-sized vector");
12442 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
12443 SDValue HalfNumElts =
12444 VecVT.isFixedLengthVector()
12445 ? getConstant(HalfMinNumElts, DL, VT)
12446 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
12447 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
12448 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
12449 return std::make_pair(Lo, Hi);
12450}
12451
12452/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
12454 EVT VT = N.getValueType();
12457 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
12459}
12460
12463 unsigned Start, unsigned Count,
12464 EVT EltVT) {
12465 EVT VT = Op.getValueType();
12466 if (Count == 0)
12467 Count = VT.getVectorNumElements();
12468 if (EltVT == EVT())
12469 EltVT = VT.getVectorElementType();
12470 SDLoc SL(Op);
12471 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
12472 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
12473 getVectorIdxConstant(i, SL)));
12474 }
12475}
12476
12477// getAddressSpace - Return the address space this GlobalAddress belongs to.
12479 return getGlobal()->getType()->getAddressSpace();
12480}
12481
12484 return Val.MachineCPVal->getType();
12485 return Val.ConstVal->getType();
12486}
12487
12488bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
12489 unsigned &SplatBitSize,
12490 bool &HasAnyUndefs,
12491 unsigned MinSplatBits,
12492 bool IsBigEndian) const {
12493 EVT VT = getValueType(0);
12494 assert(VT.isVector() && "Expected a vector type");
12495 unsigned VecWidth = VT.getSizeInBits();
12496 if (MinSplatBits > VecWidth)
12497 return false;
12498
12499 // FIXME: The widths are based on this node's type, but build vectors can
12500 // truncate their operands.
12501 SplatValue = APInt(VecWidth, 0);
12502 SplatUndef = APInt(VecWidth, 0);
12503
12504 // Get the bits. Bits with undefined values (when the corresponding element
12505 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
12506 // in SplatValue. If any of the values are not constant, give up and return
12507 // false.
12508 unsigned int NumOps = getNumOperands();
12509 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
12510 unsigned EltWidth = VT.getScalarSizeInBits();
12511
12512 for (unsigned j = 0; j < NumOps; ++j) {
12513 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
12514 SDValue OpVal = getOperand(i);
12515 unsigned BitPos = j * EltWidth;
12516
12517 if (OpVal.isUndef())
12518 SplatUndef.setBits(BitPos, BitPos + EltWidth);
12519 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
12520 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
12521 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
12522 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
12523 else
12524 return false;
12525 }
12526
12527 // The build_vector is all constants or undefs. Find the smallest element
12528 // size that splats the vector.
12529 HasAnyUndefs = (SplatUndef != 0);
12530
12531 // FIXME: This does not work for vectors with elements less than 8 bits.
12532 while (VecWidth > 8) {
12533 // If we can't split in half, stop here.
12534 if (VecWidth & 1)
12535 break;
12536
12537 unsigned HalfSize = VecWidth / 2;
12538 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
12539 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
12540 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
12541 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
12542
12543 // If the two halves do not match (ignoring undef bits), stop here.
12544 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
12545 MinSplatBits > HalfSize)
12546 break;
12547
12548 SplatValue = HighValue | LowValue;
12549 SplatUndef = HighUndef & LowUndef;
12550
12551 VecWidth = HalfSize;
12552 }
12553
12554 // FIXME: The loop above only tries to split in halves. But if the input
12555 // vector for example is <3 x i16> it wouldn't be able to detect a
12556 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
12557 // optimizations. I guess that back in the days when this helper was created
12558 // vectors normally was power-of-2 sized.
12559
12560 SplatBitSize = VecWidth;
12561 return true;
12562}
12563
12565 BitVector *UndefElements) const {
12566 unsigned NumOps = getNumOperands();
12567 if (UndefElements) {
12568 UndefElements->clear();
12569 UndefElements->resize(NumOps);
12570 }
12571 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12572 if (!DemandedElts)
12573 return SDValue();
12574 SDValue Splatted;
12575 for (unsigned i = 0; i != NumOps; ++i) {
12576 if (!DemandedElts[i])
12577 continue;
12578 SDValue Op = getOperand(i);
12579 if (Op.isUndef()) {
12580 if (UndefElements)
12581 (*UndefElements)[i] = true;
12582 } else if (!Splatted) {
12583 Splatted = Op;
12584 } else if (Splatted != Op) {
12585 return SDValue();
12586 }
12587 }
12588
12589 if (!Splatted) {
12590 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
12591 assert(getOperand(FirstDemandedIdx).isUndef() &&
12592 "Can only have a splat without a constant for all undefs.");
12593 return getOperand(FirstDemandedIdx);
12594 }
12595
12596 return Splatted;
12597}
12598
12600 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12601 return getSplatValue(DemandedElts, UndefElements);
12602}
12603
12605 SmallVectorImpl<SDValue> &Sequence,
12606 BitVector *UndefElements) const {
12607 unsigned NumOps = getNumOperands();
12608 Sequence.clear();
12609 if (UndefElements) {
12610 UndefElements->clear();
12611 UndefElements->resize(NumOps);
12612 }
12613 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12614 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
12615 return false;
12616
12617 // Set the undefs even if we don't find a sequence (like getSplatValue).
12618 if (UndefElements)
12619 for (unsigned I = 0; I != NumOps; ++I)
12620 if (DemandedElts[I] && getOperand(I).isUndef())
12621 (*UndefElements)[I] = true;
12622
12623 // Iteratively widen the sequence length looking for repetitions.
12624 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
12625 Sequence.append(SeqLen, SDValue());
12626 for (unsigned I = 0; I != NumOps; ++I) {
12627 if (!DemandedElts[I])
12628 continue;
12629 SDValue &SeqOp = Sequence[I % SeqLen];
12631 if (Op.isUndef()) {
12632 if (!SeqOp)
12633 SeqOp = Op;
12634 continue;
12635 }
12636 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
12637 Sequence.clear();
12638 break;
12639 }
12640 SeqOp = Op;
12641 }
12642 if (!Sequence.empty())
12643 return true;
12644 }
12645
12646 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
12647 return false;
12648}
12649
12651 BitVector *UndefElements) const {
12652 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12653 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
12654}
12655
12658 BitVector *UndefElements) const {
12659 return dyn_cast_or_null<ConstantSDNode>(
12660 getSplatValue(DemandedElts, UndefElements));
12661}
12662
12665 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
12666}
12667
12670 BitVector *UndefElements) const {
12671 return dyn_cast_or_null<ConstantFPSDNode>(
12672 getSplatValue(DemandedElts, UndefElements));
12673}
12674
12677 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
12678}
12679
12680int32_t
12682 uint32_t BitWidth) const {
12683 if (ConstantFPSDNode *CN =
12684 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
12685 bool IsExact;
12686 APSInt IntVal(BitWidth);
12687 const APFloat &APF = CN->getValueAPF();
12688 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
12689 APFloat::opOK ||
12690 !IsExact)
12691 return -1;
12692
12693 return IntVal.exactLogBase2();
12694 }
12695 return -1;
12696}
12697
12699 bool IsLittleEndian, unsigned DstEltSizeInBits,
12700 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
12701 // Early-out if this contains anything but Undef/Constant/ConstantFP.
12702 if (!isConstant())
12703 return false;
12704
12705 unsigned NumSrcOps = getNumOperands();
12706 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
12707 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12708 "Invalid bitcast scale");
12709
12710 // Extract raw src bits.
12711 SmallVector<APInt> SrcBitElements(NumSrcOps,
12712 APInt::getZero(SrcEltSizeInBits));
12713 BitVector SrcUndeElements(NumSrcOps, false);
12714
12715 for (unsigned I = 0; I != NumSrcOps; ++I) {
12717 if (Op.isUndef()) {
12718 SrcUndeElements.set(I);
12719 continue;
12720 }
12721 auto *CInt = dyn_cast<ConstantSDNode>(Op);
12722 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
12723 assert((CInt || CFP) && "Unknown constant");
12724 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
12725 : CFP->getValueAPF().bitcastToAPInt();
12726 }
12727
12728 // Recast to dst width.
12729 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
12730 SrcBitElements, UndefElements, SrcUndeElements);
12731 return true;
12732}
12733
12734void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
12735 unsigned DstEltSizeInBits,
12736 SmallVectorImpl<APInt> &DstBitElements,
12737 ArrayRef<APInt> SrcBitElements,
12738 BitVector &DstUndefElements,
12739 const BitVector &SrcUndefElements) {
12740 unsigned NumSrcOps = SrcBitElements.size();
12741 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
12742 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12743 "Invalid bitcast scale");
12744 assert(NumSrcOps == SrcUndefElements.size() &&
12745 "Vector size mismatch");
12746
12747 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
12748 DstUndefElements.clear();
12749 DstUndefElements.resize(NumDstOps, false);
12750 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
12751
12752 // Concatenate src elements constant bits together into dst element.
12753 if (SrcEltSizeInBits <= DstEltSizeInBits) {
12754 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
12755 for (unsigned I = 0; I != NumDstOps; ++I) {
12756 DstUndefElements.set(I);
12757 APInt &DstBits = DstBitElements[I];
12758 for (unsigned J = 0; J != Scale; ++J) {
12759 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
12760 if (SrcUndefElements[Idx])
12761 continue;
12762 DstUndefElements.reset(I);
12763 const APInt &SrcBits = SrcBitElements[Idx];
12764 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
12765 "Illegal constant bitwidths");
12766 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
12767 }
12768 }
12769 return;
12770 }
12771
12772 // Split src element constant bits into dst elements.
12773 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
12774 for (unsigned I = 0; I != NumSrcOps; ++I) {
12775 if (SrcUndefElements[I]) {
12776 DstUndefElements.set(I * Scale, (I + 1) * Scale);
12777 continue;
12778 }
12779 const APInt &SrcBits = SrcBitElements[I];
12780 for (unsigned J = 0; J != Scale; ++J) {
12781 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
12782 APInt &DstBits = DstBitElements[Idx];
12783 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
12784 }
12785 }
12786}
12787
12789 for (const SDValue &Op : op_values()) {
12790 unsigned Opc = Op.getOpcode();
12791 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
12792 return false;
12793 }
12794 return true;
12795}
12796
12797std::optional<std::pair<APInt, APInt>>
12799 unsigned NumOps = getNumOperands();
12800 if (NumOps < 2)
12801 return std::nullopt;
12802
12803 if (!isa<ConstantSDNode>(getOperand(0)) ||
12804 !isa<ConstantSDNode>(getOperand(1)))
12805 return std::nullopt;
12806
12807 unsigned EltSize = getValueType(0).getScalarSizeInBits();
12808 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
12809 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
12810
12811 if (Stride.isZero())
12812 return std::nullopt;
12813
12814 for (unsigned i = 2; i < NumOps; ++i) {
12815 if (!isa<ConstantSDNode>(getOperand(i)))
12816 return std::nullopt;
12817
12818 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
12819 if (Val != (Start + (Stride * i)))
12820 return std::nullopt;
12821 }
12822
12823 return std::make_pair(Start, Stride);
12824}
12825
12826bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
12827 // Find the first non-undef value in the shuffle mask.
12828 unsigned i, e;
12829 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
12830 /* search */;
12831
12832 // If all elements are undefined, this shuffle can be considered a splat
12833 // (although it should eventually get simplified away completely).
12834 if (i == e)
12835 return true;
12836
12837 // Make sure all remaining elements are either undef or the same as the first
12838 // non-undef value.
12839 for (int Idx = Mask[i]; i != e; ++i)
12840 if (Mask[i] >= 0 && Mask[i] != Idx)
12841 return false;
12842 return true;
12843}
12844
12845// Returns the SDNode if it is a constant integer BuildVector
12846// or constant integer.
12848 if (isa<ConstantSDNode>(N))
12849 return N.getNode();
12851 return N.getNode();
12852 // Treat a GlobalAddress supporting constant offset folding as a
12853 // constant integer.
12854 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
12855 if (GA->getOpcode() == ISD::GlobalAddress &&
12856 TLI->isOffsetFoldingLegal(GA))
12857 return GA;
12858 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
12859 isa<ConstantSDNode>(N.getOperand(0)))
12860 return N.getNode();
12861 return nullptr;
12862}
12863
12864// Returns the SDNode if it is a constant float BuildVector
12865// or constant float.
12867 if (isa<ConstantFPSDNode>(N))
12868 return N.getNode();
12869
12871 return N.getNode();
12872
12873 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
12874 isa<ConstantFPSDNode>(N.getOperand(0)))
12875 return N.getNode();
12876
12877 return nullptr;
12878}
12879
12880void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
12881 assert(!Node->OperandList && "Node already has operands");
12883 "too many operands to fit into SDNode");
12884 SDUse *Ops = OperandRecycler.allocate(
12885 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
12886
12887 bool IsDivergent = false;
12888 for (unsigned I = 0; I != Vals.size(); ++I) {
12889 Ops[I].setUser(Node);
12890 Ops[I].setInitial(Vals[I]);
12891 if (Ops[I].Val.getValueType() != MVT::Other) // Skip Chain. It does not carry divergence.
12892 IsDivergent |= Ops[I].getNode()->isDivergent();
12893 }
12894 Node->NumOperands = Vals.size();
12895 Node->OperandList = Ops;
12896 if (!TLI->isSDNodeAlwaysUniform(Node)) {
12897 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
12898 Node->SDNodeBits.IsDivergent = IsDivergent;
12899 }
12901}
12902
12905 size_t Limit = SDNode::getMaxNumOperands();
12906 while (Vals.size() > Limit) {
12907 unsigned SliceIdx = Vals.size() - Limit;
12908 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
12909 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
12910 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
12911 Vals.emplace_back(NewTF);
12912 }
12913 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
12914}
12915
12917 EVT VT, SDNodeFlags Flags) {
12918 switch (Opcode) {
12919 default:
12920 return SDValue();
12921 case ISD::ADD:
12922 case ISD::OR:
12923 case ISD::XOR:
12924 case ISD::UMAX:
12925 return getConstant(0, DL, VT);
12926 case ISD::MUL:
12927 return getConstant(1, DL, VT);
12928 case ISD::AND:
12929 case ISD::UMIN:
12930 return getAllOnesConstant(DL, VT);
12931 case ISD::SMAX:
12933 case ISD::SMIN:
12935 case ISD::FADD:
12936 return getConstantFP(-0.0, DL, VT);
12937 case ISD::FMUL:
12938 return getConstantFP(1.0, DL, VT);
12939 case ISD::FMINNUM:
12940 case ISD::FMAXNUM: {
12941 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12942 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
12943 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
12944 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
12945 APFloat::getLargest(Semantics);
12946 if (Opcode == ISD::FMAXNUM)
12947 NeutralAF.changeSign();
12948
12949 return getConstantFP(NeutralAF, DL, VT);
12950 }
12951 case ISD::FMINIMUM:
12952 case ISD::FMAXIMUM: {
12953 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
12954 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
12955 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
12956 : APFloat::getLargest(Semantics);
12957 if (Opcode == ISD::FMAXIMUM)
12958 NeutralAF.changeSign();
12959
12960 return getConstantFP(NeutralAF, DL, VT);
12961 }
12962
12963 }
12964}
12965
12966/// Helper used to make a call to a library function that has one argument of
12967/// pointer type.
12968///
12969/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
12970/// used to get or set floating-point state. They have one argument of pointer
12971/// type, which points to the memory region containing bits of the
12972/// floating-point state. The value returned by such function is ignored in the
12973/// created call.
12974///
12975/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
12976/// \param Ptr Pointer used to save/load state.
12977/// \param InChain Ingoing token chain.
12978/// \returns Outgoing chain token.
12980 SDValue InChain,
12981 const SDLoc &DLoc) {
12982 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
12985 Entry.Node = Ptr;
12986 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
12987 Args.push_back(Entry);
12988 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
12989 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
12990 TLI->getPointerTy(getDataLayout()));
12992 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
12993 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
12994 std::move(Args));
12995 return TLI->LowerCallTo(CLI).second;
12996}
12997
12999 assert(From && To && "Invalid SDNode; empty source SDValue?");
13000 auto I = SDEI.find(From);
13001 if (I == SDEI.end())
13002 return;
13003
13004 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
13005 // the iterator, hence the need to make a copy to prevent a use-after-free.
13006 NodeExtraInfo NEI = I->second;
13007 if (LLVM_LIKELY(!NEI.PCSections) && LLVM_LIKELY(!NEI.MMRA)) {
13008 // No deep copy required for the types of extra info set.
13009 //
13010 // FIXME: Investigate if other types of extra info also need deep copy. This
13011 // depends on the types of nodes they can be attached to: if some extra info
13012 // is only ever attached to nodes where a replacement To node is always the
13013 // node where later use and propagation of the extra info has the intended
13014 // semantics, no deep copy is required.
13015 SDEI[To] = std::move(NEI);
13016 return;
13017 }
13018
13019 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
13020 // through the replacement of From with To. Otherwise, replacements of a node
13021 // (From) with more complex nodes (To and its operands) may result in lost
13022 // extra info where the root node (To) is insignificant in further propagating
13023 // and using extra info when further lowering to MIR.
13024 //
13025 // In the first step pre-populate the visited set with the nodes reachable
13026 // from the old From node. This avoids copying NodeExtraInfo to parts of the
13027 // DAG that is not new and should be left untouched.
13028 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
13029 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
13030 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
13031 if (MaxDepth == 0) {
13032 // Remember this node in case we need to increase MaxDepth and continue
13033 // populating FromReach from this node.
13034 Leafs.emplace_back(N);
13035 return;
13036 }
13037 if (!FromReach.insert(N).second)
13038 return;
13039 for (const SDValue &Op : N->op_values())
13040 Self(Self, Op.getNode(), MaxDepth - 1);
13041 };
13042
13043 // Copy extra info to To and all its transitive operands (that are new).
13045 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
13046 if (FromReach.contains(N))
13047 return true;
13048 if (!Visited.insert(N).second)
13049 return true;
13050 if (getEntryNode().getNode() == N)
13051 return false;
13052 for (const SDValue &Op : N->op_values()) {
13053 if (!Self(Self, Op.getNode()))
13054 return false;
13055 }
13056 // Copy only if entry node was not reached.
13057 SDEI[N] = NEI;
13058 return true;
13059 };
13060
13061 // We first try with a lower MaxDepth, assuming that the path to common
13062 // operands between From and To is relatively short. This significantly
13063 // improves performance in the common case. The initial MaxDepth is big
13064 // enough to avoid retry in the common case; the last MaxDepth is large
13065 // enough to avoid having to use the fallback below (and protects from
13066 // potential stack exhaustion from recursion).
13067 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13068 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13069 // StartFrom is the previous (or initial) set of leafs reachable at the
13070 // previous maximum depth.
13072 std::swap(StartFrom, Leafs);
13073 for (const SDNode *N : StartFrom)
13074 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13075 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13076 return;
13077 // This should happen very rarely (reached the entry node).
13078 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13079 assert(!Leafs.empty());
13080 }
13081
13082 // This should not happen - but if it did, that means the subgraph reachable
13083 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13084 // could not visit all reachable common operands. Consequently, we were able
13085 // to reach the entry node.
13086 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13087 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13088 // Best-effort fallback if assertions disabled.
13089 SDEI[To] = std::move(NEI);
13090}
13091
13092#ifndef NDEBUG
13093static void checkForCyclesHelper(const SDNode *N,
13096 const llvm::SelectionDAG *DAG) {
13097 // If this node has already been checked, don't check it again.
13098 if (Checked.count(N))
13099 return;
13100
13101 // If a node has already been visited on this depth-first walk, reject it as
13102 // a cycle.
13103 if (!Visited.insert(N).second) {
13104 errs() << "Detected cycle in SelectionDAG\n";
13105 dbgs() << "Offending node:\n";
13106 N->dumprFull(DAG); dbgs() << "\n";
13107 abort();
13108 }
13109
13110 for (const SDValue &Op : N->op_values())
13111 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13112
13113 Checked.insert(N);
13114 Visited.erase(N);
13115}
13116#endif
13117
13119 const llvm::SelectionDAG *DAG,
13120 bool force) {
13121#ifndef NDEBUG
13122 bool check = force;
13123#ifdef EXPENSIVE_CHECKS
13124 check = true;
13125#endif // EXPENSIVE_CHECKS
13126 if (check) {
13127 assert(N && "Checking nonexistent SDNode");
13130 checkForCyclesHelper(N, visited, checked, DAG);
13131 }
13132#endif // !NDEBUG
13133}
13134
13135void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13136 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13137}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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...
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:468
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:240
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Given that RA is a live propagate it s liveness to any other values it uses(according to Uses). void DeadArgumentEliminationPass
Given that RA is a live value
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file defines a hash set that can be used to remove duplication of nodes in a graph.
Rewrite Partial Register Uses
#define check(cond)
static const unsigned MaxDepth
static LVOptions Options
Definition: LVOptions.cpp:25
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:528
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
mir Rename Register Operands
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
unsigned const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Contains matchers for matching SelectionDAG nodes and values.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, AAResults *AA)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void VerifySDNode(SDNode *N, const TargetLowering *TLI)
VerifySDNode - Check the given SDNode. Aborts if it is invalid.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static Constant * ConstantFold(Instruction *I, const DataLayout &DL, const SmallDenseMap< Value *, Constant * > &ConstantPool)
Try to fold instruction I into a constant.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file describes how to lower LLVM code to machine code.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
Value * RHS
Value * LHS
static unsigned getSize(unsigned Kind)
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:988
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1069
void copySign(const APFloat &RHS)
Definition: APFloat.h:1163
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5196
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1051
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:1278
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1042
bool isFinite() const
Definition: APFloat.h:1300
bool isNaN() const
Definition: APFloat.h:1293
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1193
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1060
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1096
bool isZero() const
Definition: APFloat.h:1291
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1006
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1185
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:966
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1087
bool isPosZero() const
Definition: APFloat.h:1306
bool isNegZero() const
Definition: APFloat.h:1307
void changeSign()
Definition: APFloat.h:1158
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:977
bool isInfinity() const
Definition: APFloat.h:1292
Class for arbitrary precision integers.
Definition: APInt.h:76
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2025
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:212
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1385
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:207
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1491
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1370
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1620
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1364
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:608
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1002
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1463
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1308
APInt abs() const
Get the absolute value.
Definition: APInt.h:1737
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:1996
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:349
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1636
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1439
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1089
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1375
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition: APInt.cpp:1124
APInt reverseBits() const
Definition: APInt.cpp:737
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:812
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1144
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1589
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1578
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1548
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:620
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:197
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2056
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2070
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1010
APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition: APInt.cpp:1111
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition: APInt.cpp:368
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1395
unsigned logBase2() const
Definition: APInt.h:1703
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:805
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1297
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1706
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:312
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1128
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1345
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
APInt byteSwap() const
Definition: APInt.cpp:715
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1235
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:418
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1367
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:453
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1215
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:264
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:836
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1199
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2015
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
Recycle small arrays allocated from a BumpPtrAllocator.
Definition: ArrayRecycler.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:195
This is an SDNode representing atomic operations.
static BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
BitVector & reset()
Definition: BitVector.h:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition: BitVector.h:335
BitVector & set()
Definition: BitVector.h:351
bool none() const
none - Returns true if none of the bits are set.
Definition: BitVector.h:188
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:159
unsigned getTargetFlags() const
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition: Constants.h:889
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:148
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition: Allocator.h:123
static bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:148
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:145
bool isMachineConstantPoolEntry() const
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition: Constant.h:41
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1699
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
DWARF expression.
static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:238
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:878
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:865
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:763
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:874
A debug info location.
Definition: DebugLoc.h:33
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:319
MachineBasicBlock * MBB
MBB - The current block.
Data structure describing the variable locations in a function.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:685
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:682
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:340
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:358
unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:263
unsigned getAddressSpace() const
Definition: GlobalValue.h:205
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
This class is used to form a handle around another node that is persistent and is updated across invo...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
constexpr bool isValid() const
Definition: LowLevelType.h:145
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate the offet and size that ar...
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
Metadata node.
Definition: Metadata.h:1067
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:193
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:679
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Definition: PointerUnion.h:142
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Keeps track of dbg_value information through SDISel.
Definition: SelectionDAG.h:159
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:188
void add(SDDbgValue *V, bool isParameter)
void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
ArrayRef< SDDbgValue * > getSDDbgValues(const SDNode *Node) const
Definition: SelectionDAG.h:194
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(unsigned VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
MemSDNodeBitfields MemSDNodeBits
void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Return true if the type of the node type undefined.
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
SDNode * getNode() const
Convenience function for get().getNode().
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual SDValue EmitTargetCodeForMemset(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo) const
Emit target-specific code that performs a memset.
virtual SDValue EmitTargetCodeForMemmove(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memmove.
virtual SDValue EmitTargetCodeForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memcpy.
SDNodeFlags getFlags() const
Definition: SelectionDAG.h:379
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:225
Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
Definition: SelectionDAG.h:551
SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), AAResults *AA=nullptr)
SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
bool isKnownNeverSNaN(SDValue Op, unsigned Depth=0) const
SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS)
Helper function to make it easier to build Select's if you just have operands and don't want to check...
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:474
const APInt * getValidMaximumShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has constant shift amounts that are all less than the element bit-width of th...
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
void updateDivergence(SDNode *N)
SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
SDNode * isConstantIntBuildVectorOrConstantInt(SDValue N) const
Test whether the given value is a constant int or similar node.
SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, unsigned VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
bool calculateDivergence(SDNode *N)
SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
bool shouldOptForSize() const
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:478
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:448
SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
Definition: SelectionDAG.h:387
std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
Definition: SelectionDAG.h:531
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Definition: SelectionDAG.h:828
SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), AAResults *AA=nullptr)
SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
void DeleteNode(SDNode *N)
Remove the specified node from the system.
SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:472
SDNode * isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, bool isTailCall, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
Definition: SelectionDAG.h:659
SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
const APInt * getValidShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has a constant or splat constant shift amount that is less than the element b...
SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
Definition: SelectionDAG.h:862
SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
SDValue getRegister(unsigned Reg, EVT VT)
void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
SDValue getBasicBlock(MachineBasicBlock *MBB)
SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
static const fltSemantics & EVTToAPFloatSemantics(EVT VT)
Returns an APFloat semantics tag appropriate for the given type.
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:473
std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
Definition: SelectionDAG.h:543
SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getValueType(EVT)
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
Definition: SelectionDAG.h:539
SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
bool isKnownNeverNaN(SDValue Op, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN.
SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:676
unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:469
SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
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:845
SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
SDValue getRegisterMask(const uint32_t *RegMask)
SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
SDValue getCondCode(ISD::CondCode Cond)
SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex, int64_t Size, int64_t Offset=-1)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the por...
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
LLVMContext * getContext() const
Definition: SelectionDAG.h:485
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:560
const APInt * getValidMinimumShiftAmountConstant(SDValue V, const APInt &DemandedElts) const
If a SHL/SRA/SRL node V has constant shift amounts that are all less than the element bit-width of th...
SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL, bool LegalTypes=true)
SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=0, const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags)
Get the specified node if it's already available, or else return NULL.
SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:554
SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
Definition: SelectionDAG.h:878
std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
Definition: SelectionDAG.h:534
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
static bool isSplatMask(const int *Mask, EVT VT)
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:321
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:356
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:360
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:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:717
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void reserve(size_type N)
Definition: SmallVector.h:676
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
virtual MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
virtual bool isCommutativeBinOp(unsigned Opcode) const
Returns true if the opcode is a commutative binary operation.
virtual ISD::NodeType getExtendForAtomicOps() const
Returns how the platform's atomic operations are extended (ZERO_EXTEND, SIGN_EXTEND,...
virtual bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const
Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type from this source type with ...
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL, bool LegalTypes=true) const
Returns the type for the shift amount of a shift opcode.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal on this target.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
virtual bool hasVectorBlend() const
Return true if the target has a vector blend instruction.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual void computeKnownBitsForFrameIndex(int FIOp, KnownBits &Known, const MachineFunction &MF) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
This method can be implemented by targets that want to expose additional information about sign bits ...
virtual void verifyTargetSDNode(const SDNode *N) const
Check the given SDNode. Aborts if it is invalid.
virtual bool findOptimalMemOpLowering(std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual bool isKnownNeverNaNForTargetNode(SDValue Op, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const
If SNaN is false,.
virtual void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
virtual bool isSDNodeSourceOfDivergence(const SDNode *N, FunctionLoweringInfo *FLI, UniformityInfo *UA) const
virtual bool isSDNodeAlwaysUniform(const SDNode *N) const
virtual bool isSplatValueForTargetNode(SDValue Op, const APInt &DemandedElts, APInt &UndefElts, const SelectionDAG &DAG, unsigned Depth=0) const
Return true if vector Op has the same value across all DemandedElts, indicating any elements which ma...
virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const
Return true if folding a constant offset with the given GlobalAddress is legal.
virtual const Constant * getTargetConstantFromLoad(LoadSDNode *LD) const
This method returns the constant pool value that will be loaded by LD.
virtual bool isGAPlusOffset(SDNode *N, const GlobalValue *&GA, int64_t &Offset) const
Returns true (and the GlobalValue and the offset) if the node is a GlobalAddress + offset.
virtual bool isGuaranteedNotToBeUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, unsigned Depth) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
virtual bool canCreateUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, bool ConsiderFlags, unsigned Depth) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:76
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetOptions Options
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, XROS, or DriverKit).
Definition: Triple.h:553
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:342
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void set(Value *Val)
Definition: Value.h:882
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:185
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:199
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:229
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition: TypeSize.h:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:236
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:660
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:678
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
Definition: Mutex.h:28
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition: APInt.cpp:3100
const APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition: APInt.h:2203
APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition: APInt.cpp:3087
APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition: APInt.cpp:3077
const APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition: APInt.h:2198
APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition: APInt.cpp:3092
APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition: APInt.cpp:2978
APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition: APInt.cpp:3072
APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition: APInt.cpp:3082
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition: ISDOpcodes.h:40
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:751
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:237
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:724
@ TargetConstantPool
Definition: ISDOpcodes.h:168
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
Definition: ISDOpcodes.h:1178
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:477
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1283
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition: ISDOpcodes.h:44
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1346
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1377
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:498
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:251
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1276
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:560
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1067
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:715
@ TargetBlockAddress
Definition: ISDOpcodes.h:170
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1278
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1248
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1279
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:270
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition: ISDOpcodes.h:488
@ FMAXNUM_IEEE
Definition: ISDOpcodes.h:986
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:240
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1038
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:784
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:484
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:199
@ GlobalAddress
Definition: ISDOpcodes.h:78
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:1261
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:791
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:544
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1362
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:391
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1366
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:689
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:821
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1376
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:478
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:914
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1274
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:904
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:230
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1275
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:940
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1407
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1281
@ GlobalTLSAddress
Definition: ISDOpcodes.h:79
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
Definition: ISDOpcodes.h:1174
@ FrameIndex
Definition: ISDOpcodes.h:80
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1109
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:775
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:663
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:621
@ TargetExternalSymbol
Definition: ISDOpcodes.h:169
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1359
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:723
@ TargetJumpTable
Definition: ISDOpcodes.h:167
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition: ISDOpcodes.h:177
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1228
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1363
@ 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:759
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:931
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1282
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1084
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:328
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1277
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:647
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:501
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:350
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:728
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1244
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:212
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1378
@ RegisterMask
Definition: ISDOpcodes.h:75
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition: ISDOpcodes.h:223
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition: ISDOpcodes.h:628
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition: ISDOpcodes.h:68
@ ATOMIC_LOAD_FMIN
Definition: ISDOpcodes.h:1284
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition: ISDOpcodes.h:209
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:324
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition: ISDOpcodes.h:164
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1371
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:652
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:706
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1273
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:601
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1272
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:574
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:985
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition: ISDOpcodes.h:47
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:536
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:781
@ TargetConstantFP
Definition: ISDOpcodes.h:159
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:857
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:743
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1336
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1255
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1280
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:972
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:360
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:332
@ TargetFrameIndex
Definition: ISDOpcodes.h:166
@ ConstantPool
Definition: ISDOpcodes.h:82
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition: ISDOpcodes.h:810
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:799
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:675
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1311
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:889
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:737
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:304
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1198
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1379
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:923
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1286
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1270
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:466
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:991
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1271
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:837
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition: ISDOpcodes.h:158
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:471
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:681
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:184
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1014
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1331
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:658
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1360
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:280
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition: ISDOpcodes.h:637
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition: ISDOpcodes.h:525
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition: ISDOpcodes.h:52
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition: ISDOpcodes.h:613
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1269
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:945
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:870
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:832
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:908
@ 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:856
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1367
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:787
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1153
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1347
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:764
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:61
@ ATOMIC_LOAD_UINC_WRAP
Definition: ISDOpcodes.h:1285
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:494
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:341
@ AssertZext
Definition: ISDOpcodes.h:62
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1019
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:314
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:192
@ TargetGlobalTLSAddress
Definition: ISDOpcodes.h:165
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition: ISDOpcodes.h:516
unsigned getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantSDNode predicate.
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
static const int FIRST_TARGET_MEMORY_OPCODE
FIRST_TARGET_MEMORY_OPCODE - Target-specific pre-isel operations which do not reference a specific me...
Definition: ISDOpcodes.h:1419
bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
Definition: ISDOpcodes.h:1588
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:1593
std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
Definition: ISDOpcodes.h:1492
bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
Definition: ISDOpcodes.h:1479
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:1530
bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
Definition: ISDOpcodes.h:1510
bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:887
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit.
Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:146
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
Definition: Mutex.h:69
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:337
@ Offset
Definition: DWP.cpp:456
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition: Analysis.cpp:233
SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1722
bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:239
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:1527
SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are are tuples (A,...
Definition: STLExtras.h:2406
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:307
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
bool getAlign(const Function &F, unsigned index, unsigned &align)
bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:317
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1436
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2073
bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
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:1509
bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1373
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729
bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 maximumNumber semantics.
Definition: APFloat.h:1410
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:324
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:275
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1736
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54
bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 minimumNumber semantics.
Definition: APFloat.h:1396
@ Mul
Product of integers.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1824
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
bool 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:1879
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:465
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition: APFloat.h:1423
bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:360
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:760
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition: Metadata.h:780
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:777
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:249
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:220
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:233
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:230
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:234
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:251
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:250
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:247
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:232
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:248
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:246
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Align previous() const
Definition: Alignment.h:88
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition: ValueTypes.h:34
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:380
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:136
intptr_t getRawBits() const
Definition: ValueTypes.h:495
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:73
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:274
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:290
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:146
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:340
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:358
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:349
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:370
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:306
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64
bool isFixedLengthVector() const
Definition: ValueTypes.h:177
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:167
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:313
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:282
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:246
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:202
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:173
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:318
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:141
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:326
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:298
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:438
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:151
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:297
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:155
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:884
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:251
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:208
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:104
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:77
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:120
static 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:238
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:434
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1030
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:63
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:270
static std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
Definition: KnownBits.cpp:502
void makeNegative()
Make this value negative.
Definition: KnownBits.h:115
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:157
KnownBits byteSwap() const
Definition: KnownBits.h:457
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:47
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:285
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:89
KnownBits reverseBits() const
Definition: KnownBits.h:461
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:229
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:184
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:168
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:71
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:317
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:376
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:107
static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
Definition: KnownBits.cpp:234
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:221
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:307
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:176
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
Definition: KnownBits.cpp:143
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:192
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:141
static KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:253
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:221
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:875
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1049
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:988
static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition: KnownBits.cpp:57
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:110
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:929
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:328
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:101
static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition: KnownBits.cpp:50
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:276
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:215
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:777
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:163
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:556
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:291
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:202
This class contains a discriminated union of information about pointers in memory operands,...
bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
These are IR-level optimization flags that may be propagated to SDNodes.
void intersectWith(const SDNodeFlags Flags)
Clear any flags in this flag set that aren't also set in Flags.
bool hasNonNeg() const
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
Definition: SelectionDAG.h:307
DAGUpdateListener *const Next
Definition: SelectionDAG.h:308
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)