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"
51#include "llvm/IR/Constants.h"
52#include "llvm/IR/DataLayout.h"
54#include "llvm/IR/DebugLoc.h"
56#include "llvm/IR/Function.h"
57#include "llvm/IR/GlobalValue.h"
58#include "llvm/IR/Metadata.h"
59#include "llvm/IR/Type.h"
63#include "llvm/Support/Debug.h"
67#include "llvm/Support/Mutex.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <set>
79#include <string>
80#include <utility>
81#include <vector>
82
83using namespace llvm;
84using namespace llvm::SDPatternMatch;
85
86/// makeVTList - Return an instance of the SDVTList struct initialized with the
87/// specified members.
88static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
89 SDVTList Res = {VTs, NumVTs};
90 return Res;
91}
92
93// Default null implementations of the callbacks.
97
98void SelectionDAG::DAGNodeDeletedListener::anchor() {}
99void SelectionDAG::DAGNodeInsertedListener::anchor() {}
100
101#define DEBUG_TYPE "selectiondag"
102
103static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
104 cl::Hidden, cl::init(true),
105 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
106
107static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
108 cl::desc("Number limit for gluing ld/st of memcpy."),
109 cl::Hidden, cl::init(0));
110
112 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
113}
114
115//===----------------------------------------------------------------------===//
116// ConstantFPSDNode Class
117//===----------------------------------------------------------------------===//
118
119/// isExactlyValue - We don't rely on operator== working on double values, as
120/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
121/// As such, this method can be used to do an exact bit-for-bit comparison of
122/// two floating point values.
124 return getValueAPF().bitwiseIsEqual(V);
125}
126
128 const APFloat& Val) {
129 assert(VT.isFloatingPoint() && "Can only convert between FP types");
130
131 // convert modifies in place, so make a copy.
132 APFloat Val2 = APFloat(Val);
133 bool losesInfo;
136 &losesInfo);
137 return !losesInfo;
138}
139
140//===----------------------------------------------------------------------===//
141// ISD Namespace
142//===----------------------------------------------------------------------===//
143
144bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
145 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
146 unsigned EltSize =
147 N->getValueType(0).getVectorElementType().getSizeInBits();
148 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
149 SplatVal = Op0->getAPIntValue().trunc(EltSize);
150 return true;
151 }
152 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
153 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
154 return true;
155 }
156 }
157
158 auto *BV = dyn_cast<BuildVectorSDNode>(N);
159 if (!BV)
160 return false;
161
162 APInt SplatUndef;
163 unsigned SplatBitSize;
164 bool HasUndefs;
165 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
166 // Endianness does not matter here. We are checking for a splat given the
167 // element size of the vector, and if we find such a splat for little endian
168 // layout, then that should be valid also for big endian (as the full vector
169 // size is known to be a multiple of the element size).
170 const bool IsBigEndian = false;
171 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
172 EltSize, IsBigEndian) &&
173 EltSize == SplatBitSize;
174}
175
176// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
177// specializations of the more general isConstantSplatVector()?
178
179bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
180 // Look through a bit convert.
181 while (N->getOpcode() == ISD::BITCAST)
182 N = N->getOperand(0).getNode();
183
184 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
185 APInt SplatVal;
186 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
187 }
188
189 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
190
191 unsigned i = 0, e = N->getNumOperands();
192
193 // Skip over all of the undef values.
194 while (i != e && N->getOperand(i).isUndef())
195 ++i;
196
197 // Do not accept an all-undef vector.
198 if (i == e) return false;
199
200 // Do not accept build_vectors that aren't all constants or which have non-~0
201 // elements. We have to be a bit careful here, as the type of the constant
202 // may not be the same as the type of the vector elements due to type
203 // legalization (the elements are promoted to a legal type for the target and
204 // a vector of a type may be legal when the base element type is not).
205 // We only want to check enough bits to cover the vector elements, because
206 // we care if the resultant vector is all ones, not whether the individual
207 // constants are.
208 SDValue NotZero = N->getOperand(i);
209 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
210 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
211 if (CN->getAPIntValue().countr_one() < EltSize)
212 return false;
213 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
214 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
215 return false;
216 } else
217 return false;
218
219 // Okay, we have at least one ~0 value, check to see if the rest match or are
220 // undefs. Even with the above element type twiddling, this should be OK, as
221 // the same type legalization should have applied to all the elements.
222 for (++i; i != e; ++i)
223 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
224 return false;
225 return true;
226}
227
228bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
229 // Look through a bit convert.
230 while (N->getOpcode() == ISD::BITCAST)
231 N = N->getOperand(0).getNode();
232
233 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
234 APInt SplatVal;
235 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
236 }
237
238 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
239
240 bool IsAllUndef = true;
241 for (const SDValue &Op : N->op_values()) {
242 if (Op.isUndef())
243 continue;
244 IsAllUndef = false;
245 // Do not accept build_vectors that aren't all constants or which have non-0
246 // elements. We have to be a bit careful here, as the type of the constant
247 // may not be the same as the type of the vector elements due to type
248 // legalization (the elements are promoted to a legal type for the target
249 // and a vector of a type may be legal when the base element type is not).
250 // We only want to check enough bits to cover the vector elements, because
251 // we care if the resultant vector is all zeros, not whether the individual
252 // constants are.
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
255 if (CN->getAPIntValue().countr_zero() < EltSize)
256 return false;
257 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
258 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
259 return false;
260 } else
261 return false;
262 }
263
264 // Do not accept an all-undef vector.
265 if (IsAllUndef)
266 return false;
267 return true;
268}
269
271 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
272}
273
275 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
276}
277
279 if (N->getOpcode() != ISD::BUILD_VECTOR)
280 return false;
281
282 for (const SDValue &Op : N->op_values()) {
283 if (Op.isUndef())
284 continue;
285 if (!isa<ConstantSDNode>(Op))
286 return false;
287 }
288 return true;
289}
290
292 if (N->getOpcode() != ISD::BUILD_VECTOR)
293 return false;
294
295 for (const SDValue &Op : N->op_values()) {
296 if (Op.isUndef())
297 continue;
298 if (!isa<ConstantFPSDNode>(Op))
299 return false;
300 }
301 return true;
302}
303
304bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
305 bool Signed) {
306 assert(N->getValueType(0).isVector() && "Expected a vector!");
307
308 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
309 if (EltSize <= NewEltSize)
310 return false;
311
312 if (N->getOpcode() == ISD::ZERO_EXTEND) {
313 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
314 NewEltSize) &&
315 !Signed;
316 }
317 if (N->getOpcode() == ISD::SIGN_EXTEND) {
318 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
319 NewEltSize) &&
320 Signed;
321 }
322 if (N->getOpcode() != ISD::BUILD_VECTOR)
323 return false;
324
325 for (const SDValue &Op : N->op_values()) {
326 if (Op.isUndef())
327 continue;
328 if (!isa<ConstantSDNode>(Op))
329 return false;
330
331 APInt C = Op->getAsAPIntVal().trunc(EltSize);
332 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
333 return false;
334 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
335 return false;
336 }
337
338 return true;
339}
340
342 // Return false if the node has no operands.
343 // This is "logically inconsistent" with the definition of "all" but
344 // is probably the desired behavior.
345 if (N->getNumOperands() == 0)
346 return false;
347 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
348}
349
351 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
352}
353
354template <typename ConstNodeType>
356 std::function<bool(ConstNodeType *)> Match,
357 bool AllowUndefs) {
358 // FIXME: Add support for scalar UNDEF cases?
359 if (auto *C = dyn_cast<ConstNodeType>(Op))
360 return Match(C);
361
362 // FIXME: Add support for vector UNDEF cases?
363 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
364 ISD::SPLAT_VECTOR != Op.getOpcode())
365 return false;
366
367 EVT SVT = Op.getValueType().getScalarType();
368 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
369 if (AllowUndefs && Op.getOperand(i).isUndef()) {
370 if (!Match(nullptr))
371 return false;
372 continue;
373 }
374
375 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
376 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
377 return false;
378 }
379 return true;
380}
381// Build used template types.
382template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
383 SDValue, std::function<bool(ConstantSDNode *)>, bool);
384template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
385 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
386
388 SDValue LHS, SDValue RHS,
389 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
390 bool AllowUndefs, bool AllowTypeMismatch) {
391 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
392 return false;
393
394 // TODO: Add support for scalar UNDEF cases?
395 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
396 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
397 return Match(LHSCst, RHSCst);
398
399 // TODO: Add support for vector UNDEF cases?
400 if (LHS.getOpcode() != RHS.getOpcode() ||
401 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
402 LHS.getOpcode() != ISD::SPLAT_VECTOR))
403 return false;
404
405 EVT SVT = LHS.getValueType().getScalarType();
406 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
407 SDValue LHSOp = LHS.getOperand(i);
408 SDValue RHSOp = RHS.getOperand(i);
409 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
410 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
411 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
412 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
413 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
414 return false;
415 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
416 LHSOp.getValueType() != RHSOp.getValueType()))
417 return false;
418 if (!Match(LHSCst, RHSCst))
419 return false;
420 }
421 return true;
422}
423
425 switch (VecReduceOpcode) {
426 default:
427 llvm_unreachable("Expected VECREDUCE opcode");
430 case ISD::VP_REDUCE_FADD:
431 case ISD::VP_REDUCE_SEQ_FADD:
432 return ISD::FADD;
435 case ISD::VP_REDUCE_FMUL:
436 case ISD::VP_REDUCE_SEQ_FMUL:
437 return ISD::FMUL;
439 case ISD::VP_REDUCE_ADD:
440 return ISD::ADD;
442 case ISD::VP_REDUCE_MUL:
443 return ISD::MUL;
445 case ISD::VP_REDUCE_AND:
446 return ISD::AND;
448 case ISD::VP_REDUCE_OR:
449 return ISD::OR;
451 case ISD::VP_REDUCE_XOR:
452 return ISD::XOR;
454 case ISD::VP_REDUCE_SMAX:
455 return ISD::SMAX;
457 case ISD::VP_REDUCE_SMIN:
458 return ISD::SMIN;
460 case ISD::VP_REDUCE_UMAX:
461 return ISD::UMAX;
463 case ISD::VP_REDUCE_UMIN:
464 return ISD::UMIN;
466 case ISD::VP_REDUCE_FMAX:
467 return ISD::FMAXNUM;
469 case ISD::VP_REDUCE_FMIN:
470 return ISD::FMINNUM;
472 case ISD::VP_REDUCE_FMAXIMUM:
473 return ISD::FMAXIMUM;
475 case ISD::VP_REDUCE_FMINIMUM:
476 return ISD::FMINIMUM;
477 }
478}
479
480bool ISD::isVPOpcode(unsigned Opcode) {
481 switch (Opcode) {
482 default:
483 return false;
484#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
485 case ISD::VPSD: \
486 return true;
487#include "llvm/IR/VPIntrinsics.def"
488 }
489}
490
491bool ISD::isVPBinaryOp(unsigned Opcode) {
492 switch (Opcode) {
493 default:
494 break;
495#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
496#define VP_PROPERTY_BINARYOP return true;
497#define END_REGISTER_VP_SDNODE(VPSD) break;
498#include "llvm/IR/VPIntrinsics.def"
499 }
500 return false;
501}
502
503bool ISD::isVPReduction(unsigned Opcode) {
504 switch (Opcode) {
505 default:
506 break;
507#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
508#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
509#define END_REGISTER_VP_SDNODE(VPSD) break;
510#include "llvm/IR/VPIntrinsics.def"
511 }
512 return false;
513}
514
515/// The operand position of the vector mask.
516std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
517 switch (Opcode) {
518 default:
519 return std::nullopt;
520#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
521 case ISD::VPSD: \
522 return MASKPOS;
523#include "llvm/IR/VPIntrinsics.def"
524 }
525}
526
527/// The operand position of the explicit vector length parameter.
528std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
529 switch (Opcode) {
530 default:
531 return std::nullopt;
532#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
533 case ISD::VPSD: \
534 return EVLPOS;
535#include "llvm/IR/VPIntrinsics.def"
536 }
537}
538
539std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
540 bool hasFPExcept) {
541 // FIXME: Return strict opcodes in case of fp exceptions.
542 switch (VPOpcode) {
543 default:
544 return std::nullopt;
545#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
546#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
547#define END_REGISTER_VP_SDNODE(VPOPC) break;
548#include "llvm/IR/VPIntrinsics.def"
549 }
550 return std::nullopt;
551}
552
553unsigned ISD::getVPForBaseOpcode(unsigned Opcode) {
554 switch (Opcode) {
555 default:
556 llvm_unreachable("can not translate this Opcode to VP.");
557#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
558#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
559#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
560#include "llvm/IR/VPIntrinsics.def"
561 }
562}
563
565 switch (ExtType) {
566 case ISD::EXTLOAD:
567 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
568 case ISD::SEXTLOAD:
569 return ISD::SIGN_EXTEND;
570 case ISD::ZEXTLOAD:
571 return ISD::ZERO_EXTEND;
572 default:
573 break;
574 }
575
576 llvm_unreachable("Invalid LoadExtType");
577}
578
580 // To perform this operation, we just need to swap the L and G bits of the
581 // operation.
582 unsigned OldL = (Operation >> 2) & 1;
583 unsigned OldG = (Operation >> 1) & 1;
584 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
585 (OldL << 1) | // New G bit
586 (OldG << 2)); // New L bit.
587}
588
590 unsigned Operation = Op;
591 if (isIntegerLike)
592 Operation ^= 7; // Flip L, G, E bits, but not U.
593 else
594 Operation ^= 15; // Flip all of the condition bits.
595
597 Operation &= ~8; // Don't let N and U bits get set.
598
599 return ISD::CondCode(Operation);
600}
601
603 return getSetCCInverseImpl(Op, Type.isInteger());
604}
605
607 bool isIntegerLike) {
608 return getSetCCInverseImpl(Op, isIntegerLike);
609}
610
611/// For an integer comparison, return 1 if the comparison is a signed operation
612/// and 2 if the result is an unsigned comparison. Return zero if the operation
613/// does not depend on the sign of the input (setne and seteq).
614static int isSignedOp(ISD::CondCode Opcode) {
615 switch (Opcode) {
616 default: llvm_unreachable("Illegal integer setcc operation!");
617 case ISD::SETEQ:
618 case ISD::SETNE: return 0;
619 case ISD::SETLT:
620 case ISD::SETLE:
621 case ISD::SETGT:
622 case ISD::SETGE: return 1;
623 case ISD::SETULT:
624 case ISD::SETULE:
625 case ISD::SETUGT:
626 case ISD::SETUGE: return 2;
627 }
628}
629
631 EVT Type) {
632 bool IsInteger = Type.isInteger();
633 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
634 // Cannot fold a signed integer setcc with an unsigned integer setcc.
635 return ISD::SETCC_INVALID;
636
637 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
638
639 // If the N and U bits get set, then the resultant comparison DOES suddenly
640 // care about orderedness, and it is true when ordered.
641 if (Op > ISD::SETTRUE2)
642 Op &= ~16; // Clear the U bit if the N bit is set.
643
644 // Canonicalize illegal integer setcc's.
645 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
646 Op = ISD::SETNE;
647
648 return ISD::CondCode(Op);
649}
650
652 EVT Type) {
653 bool IsInteger = Type.isInteger();
654 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
655 // Cannot fold a signed setcc with an unsigned setcc.
656 return ISD::SETCC_INVALID;
657
658 // Combine all of the condition bits.
659 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
660
661 // Canonicalize illegal integer setcc's.
662 if (IsInteger) {
663 switch (Result) {
664 default: break;
665 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
666 case ISD::SETOEQ: // SETEQ & SETU[LG]E
667 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
668 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
669 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
670 }
671 }
672
673 return Result;
674}
675
676//===----------------------------------------------------------------------===//
677// SDNode Profile Support
678//===----------------------------------------------------------------------===//
679
680/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
681static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
682 ID.AddInteger(OpC);
683}
684
685/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
686/// solely with their pointer.
688 ID.AddPointer(VTList.VTs);
689}
690
691/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
693 ArrayRef<SDValue> Ops) {
694 for (const auto &Op : Ops) {
695 ID.AddPointer(Op.getNode());
696 ID.AddInteger(Op.getResNo());
697 }
698}
699
700/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
702 ArrayRef<SDUse> Ops) {
703 for (const auto &Op : Ops) {
704 ID.AddPointer(Op.getNode());
705 ID.AddInteger(Op.getResNo());
706 }
707}
708
709static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
710 SDVTList VTList, ArrayRef<SDValue> OpList) {
711 AddNodeIDOpcode(ID, OpC);
712 AddNodeIDValueTypes(ID, VTList);
713 AddNodeIDOperands(ID, OpList);
714}
715
716/// If this is an SDNode with special info, add this info to the NodeID data.
718 switch (N->getOpcode()) {
721 case ISD::MCSymbol:
722 llvm_unreachable("Should only be used on nodes with operands");
723 default: break; // Normal nodes don't need extra info.
725 case ISD::Constant: {
726 const ConstantSDNode *C = cast<ConstantSDNode>(N);
727 ID.AddPointer(C->getConstantIntValue());
728 ID.AddBoolean(C->isOpaque());
729 break;
730 }
732 case ISD::ConstantFP:
733 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
734 break;
739 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
740 ID.AddPointer(GA->getGlobal());
741 ID.AddInteger(GA->getOffset());
742 ID.AddInteger(GA->getTargetFlags());
743 break;
744 }
745 case ISD::BasicBlock:
746 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
747 break;
748 case ISD::Register:
749 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
750 break;
752 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
753 break;
754 case ISD::SRCVALUE:
755 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
756 break;
757 case ISD::FrameIndex:
759 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
760 break;
763 if (cast<LifetimeSDNode>(N)->hasOffset()) {
764 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
765 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
766 }
767 break;
769 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
770 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
771 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
772 break;
773 case ISD::JumpTable:
775 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
776 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
777 break;
780 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
781 ID.AddInteger(CP->getAlign().value());
782 ID.AddInteger(CP->getOffset());
783 if (CP->isMachineConstantPoolEntry())
784 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
785 else
786 ID.AddPointer(CP->getConstVal());
787 ID.AddInteger(CP->getTargetFlags());
788 break;
789 }
790 case ISD::TargetIndex: {
791 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
792 ID.AddInteger(TI->getIndex());
793 ID.AddInteger(TI->getOffset());
794 ID.AddInteger(TI->getTargetFlags());
795 break;
796 }
797 case ISD::LOAD: {
798 const LoadSDNode *LD = cast<LoadSDNode>(N);
799 ID.AddInteger(LD->getMemoryVT().getRawBits());
800 ID.AddInteger(LD->getRawSubclassData());
801 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
802 ID.AddInteger(LD->getMemOperand()->getFlags());
803 break;
804 }
805 case ISD::STORE: {
806 const StoreSDNode *ST = cast<StoreSDNode>(N);
807 ID.AddInteger(ST->getMemoryVT().getRawBits());
808 ID.AddInteger(ST->getRawSubclassData());
809 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
810 ID.AddInteger(ST->getMemOperand()->getFlags());
811 break;
812 }
813 case ISD::VP_LOAD: {
814 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
815 ID.AddInteger(ELD->getMemoryVT().getRawBits());
816 ID.AddInteger(ELD->getRawSubclassData());
817 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
818 ID.AddInteger(ELD->getMemOperand()->getFlags());
819 break;
820 }
821 case ISD::VP_STORE: {
822 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
823 ID.AddInteger(EST->getMemoryVT().getRawBits());
824 ID.AddInteger(EST->getRawSubclassData());
825 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
826 ID.AddInteger(EST->getMemOperand()->getFlags());
827 break;
828 }
829 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
830 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
831 ID.AddInteger(SLD->getMemoryVT().getRawBits());
832 ID.AddInteger(SLD->getRawSubclassData());
833 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
834 break;
835 }
836 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
837 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
838 ID.AddInteger(SST->getMemoryVT().getRawBits());
839 ID.AddInteger(SST->getRawSubclassData());
840 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
841 break;
842 }
843 case ISD::VP_GATHER: {
844 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
845 ID.AddInteger(EG->getMemoryVT().getRawBits());
846 ID.AddInteger(EG->getRawSubclassData());
847 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
848 ID.AddInteger(EG->getMemOperand()->getFlags());
849 break;
850 }
851 case ISD::VP_SCATTER: {
852 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
853 ID.AddInteger(ES->getMemoryVT().getRawBits());
854 ID.AddInteger(ES->getRawSubclassData());
855 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
856 ID.AddInteger(ES->getMemOperand()->getFlags());
857 break;
858 }
859 case ISD::MLOAD: {
860 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
861 ID.AddInteger(MLD->getMemoryVT().getRawBits());
862 ID.AddInteger(MLD->getRawSubclassData());
863 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
864 ID.AddInteger(MLD->getMemOperand()->getFlags());
865 break;
866 }
867 case ISD::MSTORE: {
868 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
869 ID.AddInteger(MST->getMemoryVT().getRawBits());
870 ID.AddInteger(MST->getRawSubclassData());
871 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
872 ID.AddInteger(MST->getMemOperand()->getFlags());
873 break;
874 }
875 case ISD::MGATHER: {
876 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
877 ID.AddInteger(MG->getMemoryVT().getRawBits());
878 ID.AddInteger(MG->getRawSubclassData());
879 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
880 ID.AddInteger(MG->getMemOperand()->getFlags());
881 break;
882 }
883 case ISD::MSCATTER: {
884 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
885 ID.AddInteger(MS->getMemoryVT().getRawBits());
886 ID.AddInteger(MS->getRawSubclassData());
887 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
888 ID.AddInteger(MS->getMemOperand()->getFlags());
889 break;
890 }
893 case ISD::ATOMIC_SWAP:
905 case ISD::ATOMIC_LOAD:
906 case ISD::ATOMIC_STORE: {
907 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
908 ID.AddInteger(AT->getMemoryVT().getRawBits());
909 ID.AddInteger(AT->getRawSubclassData());
910 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
911 ID.AddInteger(AT->getMemOperand()->getFlags());
912 break;
913 }
914 case ISD::VECTOR_SHUFFLE: {
915 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
916 for (int M : Mask)
917 ID.AddInteger(M);
918 break;
919 }
921 case ISD::BlockAddress: {
922 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
923 ID.AddPointer(BA->getBlockAddress());
924 ID.AddInteger(BA->getOffset());
925 ID.AddInteger(BA->getTargetFlags());
926 break;
927 }
928 case ISD::AssertAlign:
929 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
930 break;
931 case ISD::PREFETCH:
934 // Handled by MemIntrinsicSDNode check after the switch.
935 break;
936 } // end switch (N->getOpcode())
937
938 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
939 // to check.
940 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
941 ID.AddInteger(MN->getRawSubclassData());
942 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
943 ID.AddInteger(MN->getMemOperand()->getFlags());
944 ID.AddInteger(MN->getMemoryVT().getRawBits());
945 }
946}
947
948/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
949/// data.
950static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
951 AddNodeIDOpcode(ID, N->getOpcode());
952 // Add the return value info.
953 AddNodeIDValueTypes(ID, N->getVTList());
954 // Add the operand info.
955 AddNodeIDOperands(ID, N->ops());
956
957 // Handle SDNode leafs with special info.
959}
960
961//===----------------------------------------------------------------------===//
962// SelectionDAG Class
963//===----------------------------------------------------------------------===//
964
965/// doNotCSE - Return true if CSE should not be performed for this node.
966static bool doNotCSE(SDNode *N) {
967 if (N->getValueType(0) == MVT::Glue)
968 return true; // Never CSE anything that produces a glue result.
969
970 switch (N->getOpcode()) {
971 default: break;
972 case ISD::HANDLENODE:
973 case ISD::EH_LABEL:
974 return true; // Never CSE these nodes.
975 }
976
977 // Check that remaining values produced are not flags.
978 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
979 if (N->getValueType(i) == MVT::Glue)
980 return true; // Never CSE anything that produces a glue result.
981
982 return false;
983}
984
985/// RemoveDeadNodes - This method deletes all unreachable nodes in the
986/// SelectionDAG.
988 // Create a dummy node (which is not added to allnodes), that adds a reference
989 // to the root node, preventing it from being deleted.
990 HandleSDNode Dummy(getRoot());
991
993
994 // Add all obviously-dead nodes to the DeadNodes worklist.
995 for (SDNode &Node : allnodes())
996 if (Node.use_empty())
997 DeadNodes.push_back(&Node);
998
999 RemoveDeadNodes(DeadNodes);
1000
1001 // If the root changed (e.g. it was a dead load, update the root).
1002 setRoot(Dummy.getValue());
1003}
1004
1005/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1006/// given list, and any nodes that become unreachable as a result.
1008
1009 // Process the worklist, deleting the nodes and adding their uses to the
1010 // worklist.
1011 while (!DeadNodes.empty()) {
1012 SDNode *N = DeadNodes.pop_back_val();
1013 // Skip to next node if we've already managed to delete the node. This could
1014 // happen if replacing a node causes a node previously added to the node to
1015 // be deleted.
1016 if (N->getOpcode() == ISD::DELETED_NODE)
1017 continue;
1018
1019 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1020 DUL->NodeDeleted(N, nullptr);
1021
1022 // Take the node out of the appropriate CSE map.
1023 RemoveNodeFromCSEMaps(N);
1024
1025 // Next, brutally remove the operand list. This is safe to do, as there are
1026 // no cycles in the graph.
1027 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1028 SDUse &Use = *I++;
1029 SDNode *Operand = Use.getNode();
1030 Use.set(SDValue());
1031
1032 // Now that we removed this operand, see if there are no uses of it left.
1033 if (Operand->use_empty())
1034 DeadNodes.push_back(Operand);
1035 }
1036
1037 DeallocateNode(N);
1038 }
1039}
1040
1042 SmallVector<SDNode*, 16> DeadNodes(1, N);
1043
1044 // Create a dummy node that adds a reference to the root node, preventing
1045 // it from being deleted. (This matters if the root is an operand of the
1046 // dead node.)
1047 HandleSDNode Dummy(getRoot());
1048
1049 RemoveDeadNodes(DeadNodes);
1050}
1051
1053 // First take this out of the appropriate CSE map.
1054 RemoveNodeFromCSEMaps(N);
1055
1056 // Finally, remove uses due to operands of this node, remove from the
1057 // AllNodes list, and delete the node.
1058 DeleteNodeNotInCSEMaps(N);
1059}
1060
1061void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1062 assert(N->getIterator() != AllNodes.begin() &&
1063 "Cannot delete the entry node!");
1064 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1065
1066 // Drop all of the operands and decrement used node's use counts.
1067 N->DropOperands();
1068
1069 DeallocateNode(N);
1070}
1071
1072void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1073 assert(!(V->isVariadic() && isParameter));
1074 if (isParameter)
1075 ByvalParmDbgValues.push_back(V);
1076 else
1077 DbgValues.push_back(V);
1078 for (const SDNode *Node : V->getSDNodes())
1079 if (Node)
1080 DbgValMap[Node].push_back(V);
1081}
1082
1083void SDDbgInfo::erase(const SDNode *Node) {
1084 DbgValMapType::iterator I = DbgValMap.find(Node);
1085 if (I == DbgValMap.end())
1086 return;
1087 for (auto &Val: I->second)
1088 Val->setIsInvalidated();
1089 DbgValMap.erase(I);
1090}
1091
1092void SelectionDAG::DeallocateNode(SDNode *N) {
1093 // If we have operands, deallocate them.
1094 removeOperands(N);
1095
1096 NodeAllocator.Deallocate(AllNodes.remove(N));
1097
1098 // Set the opcode to DELETED_NODE to help catch bugs when node
1099 // memory is reallocated.
1100 // FIXME: There are places in SDag that have grown a dependency on the opcode
1101 // value in the released node.
1102 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1103 N->NodeType = ISD::DELETED_NODE;
1104
1105 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1106 // them and forget about that node.
1107 DbgInfo->erase(N);
1108
1109 // Invalidate extra info.
1110 SDEI.erase(N);
1111}
1112
1113#ifndef NDEBUG
1114/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1115static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1116 switch (N->getOpcode()) {
1117 default:
1118 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1119 TLI->verifyTargetSDNode(N);
1120 break;
1121 case ISD::BUILD_PAIR: {
1122 EVT VT = N->getValueType(0);
1123 assert(N->getNumValues() == 1 && "Too many results!");
1124 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1125 "Wrong return type!");
1126 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1127 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1128 "Mismatched operand types!");
1129 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1130 "Wrong operand type!");
1131 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1132 "Wrong return type size");
1133 break;
1134 }
1135 case ISD::BUILD_VECTOR: {
1136 assert(N->getNumValues() == 1 && "Too many results!");
1137 assert(N->getValueType(0).isVector() && "Wrong return type!");
1138 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1139 "Wrong number of operands!");
1140 EVT EltVT = N->getValueType(0).getVectorElementType();
1141 for (const SDUse &Op : N->ops()) {
1142 assert((Op.getValueType() == EltVT ||
1143 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1144 EltVT.bitsLE(Op.getValueType()))) &&
1145 "Wrong operand type!");
1146 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1147 "Operands must all have the same type");
1148 }
1149 break;
1150 }
1151 }
1152}
1153#endif // NDEBUG
1154
1155/// Insert a newly allocated node into the DAG.
1156///
1157/// Handles insertion into the all nodes list and CSE map, as well as
1158/// verification and other common operations when a new node is allocated.
1159void SelectionDAG::InsertNode(SDNode *N) {
1160 AllNodes.push_back(N);
1161#ifndef NDEBUG
1162 N->PersistentId = NextPersistentId++;
1163 VerifySDNode(N, TLI);
1164#endif
1165 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1166 DUL->NodeInserted(N);
1167}
1168
1169/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1170/// correspond to it. This is useful when we're about to delete or repurpose
1171/// the node. We don't want future request for structurally identical nodes
1172/// to return N anymore.
1173bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1174 bool Erased = false;
1175 switch (N->getOpcode()) {
1176 case ISD::HANDLENODE: return false; // noop.
1177 case ISD::CONDCODE:
1178 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1179 "Cond code doesn't exist!");
1180 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1181 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1182 break;
1184 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1185 break;
1187 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1188 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1189 ESN->getSymbol(), ESN->getTargetFlags()));
1190 break;
1191 }
1192 case ISD::MCSymbol: {
1193 auto *MCSN = cast<MCSymbolSDNode>(N);
1194 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1195 break;
1196 }
1197 case ISD::VALUETYPE: {
1198 EVT VT = cast<VTSDNode>(N)->getVT();
1199 if (VT.isExtended()) {
1200 Erased = ExtendedValueTypeNodes.erase(VT);
1201 } else {
1202 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1203 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1204 }
1205 break;
1206 }
1207 default:
1208 // Remove it from the CSE Map.
1209 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1210 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1211 Erased = CSEMap.RemoveNode(N);
1212 break;
1213 }
1214#ifndef NDEBUG
1215 // Verify that the node was actually in one of the CSE maps, unless it has a
1216 // glue result (which cannot be CSE'd) or is one of the special cases that are
1217 // not subject to CSE.
1218 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1219 !N->isMachineOpcode() && !doNotCSE(N)) {
1220 N->dump(this);
1221 dbgs() << "\n";
1222 llvm_unreachable("Node is not in map!");
1223 }
1224#endif
1225 return Erased;
1226}
1227
1228/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1229/// maps and modified in place. Add it back to the CSE maps, unless an identical
1230/// node already exists, in which case transfer all its users to the existing
1231/// node. This transfer can potentially trigger recursive merging.
1232void
1233SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1234 // For node types that aren't CSE'd, just act as if no identical node
1235 // already exists.
1236 if (!doNotCSE(N)) {
1237 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1238 if (Existing != N) {
1239 // If there was already an existing matching node, use ReplaceAllUsesWith
1240 // to replace the dead one with the existing one. This can cause
1241 // recursive merging of other unrelated nodes down the line.
1242 ReplaceAllUsesWith(N, Existing);
1243
1244 // N is now dead. Inform the listeners and delete it.
1245 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1246 DUL->NodeDeleted(N, Existing);
1247 DeleteNodeNotInCSEMaps(N);
1248 return;
1249 }
1250 }
1251
1252 // If the node doesn't already exist, we updated it. Inform listeners.
1253 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1254 DUL->NodeUpdated(N);
1255}
1256
1257/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1258/// were replaced with those specified. If this node is never memoized,
1259/// return null, otherwise return a pointer to the slot it would take. If a
1260/// node already exists with these operands, the slot will be non-null.
1261SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1262 void *&InsertPos) {
1263 if (doNotCSE(N))
1264 return nullptr;
1265
1266 SDValue Ops[] = { Op };
1268 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1270 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1271 if (Node)
1272 Node->intersectFlagsWith(N->getFlags());
1273 return Node;
1274}
1275
1276/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1277/// were replaced with those specified. If this node is never memoized,
1278/// return null, otherwise return a pointer to the slot it would take. If a
1279/// node already exists with these operands, the slot will be non-null.
1280SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1281 SDValue Op1, SDValue Op2,
1282 void *&InsertPos) {
1283 if (doNotCSE(N))
1284 return nullptr;
1285
1286 SDValue Ops[] = { Op1, Op2 };
1288 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1290 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1291 if (Node)
1292 Node->intersectFlagsWith(N->getFlags());
1293 return Node;
1294}
1295
1296/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1297/// were replaced with those specified. If this node is never memoized,
1298/// return null, otherwise return a pointer to the slot it would take. If a
1299/// node already exists with these operands, the slot will be non-null.
1300SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1301 void *&InsertPos) {
1302 if (doNotCSE(N))
1303 return nullptr;
1304
1306 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1308 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1309 if (Node)
1310 Node->intersectFlagsWith(N->getFlags());
1311 return Node;
1312}
1313
1315 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1316 : VT.getTypeForEVT(*getContext());
1317
1318 return getDataLayout().getABITypeAlign(Ty);
1319}
1320
1321// EntryNode could meaningfully have debug info if we can find it...
1323 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1324 getVTList(MVT::Other, MVT::Glue)),
1325 Root(getEntryNode()) {
1326 InsertNode(&EntryNode);
1327 DbgInfo = new SDDbgInfo();
1328}
1329
1331 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1332 const TargetLibraryInfo *LibraryInfo,
1333 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1334 BlockFrequencyInfo *BFIin,
1335 FunctionVarLocs const *VarLocs) {
1336 MF = &NewMF;
1337 SDAGISelPass = PassPtr;
1338 ORE = &NewORE;
1341 LibInfo = LibraryInfo;
1342 Context = &MF->getFunction().getContext();
1343 UA = NewUA;
1344 PSI = PSIin;
1345 BFI = BFIin;
1346 FnVarLocs = VarLocs;
1347}
1348
1350 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1351 allnodes_clear();
1352 OperandRecycler.clear(OperandAllocator);
1353 delete DbgInfo;
1354}
1355
1357 return MF->getFunction().hasOptSize() ||
1359}
1360
1361void SelectionDAG::allnodes_clear() {
1362 assert(&*AllNodes.begin() == &EntryNode);
1363 AllNodes.remove(AllNodes.begin());
1364 while (!AllNodes.empty())
1365 DeallocateNode(&AllNodes.front());
1366#ifndef NDEBUG
1367 NextPersistentId = 0;
1368#endif
1369}
1370
1371SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1372 void *&InsertPos) {
1373 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1374 if (N) {
1375 switch (N->getOpcode()) {
1376 default: break;
1377 case ISD::Constant:
1378 case ISD::ConstantFP:
1379 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1380 "debug location. Use another overload.");
1381 }
1382 }
1383 return N;
1384}
1385
1386SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1387 const SDLoc &DL, void *&InsertPos) {
1388 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1389 if (N) {
1390 switch (N->getOpcode()) {
1391 case ISD::Constant:
1392 case ISD::ConstantFP:
1393 // Erase debug location from the node if the node is used at several
1394 // different places. Do not propagate one location to all uses as it
1395 // will cause a worse single stepping debugging experience.
1396 if (N->getDebugLoc() != DL.getDebugLoc())
1397 N->setDebugLoc(DebugLoc());
1398 break;
1399 default:
1400 // When the node's point of use is located earlier in the instruction
1401 // sequence than its prior point of use, update its debug info to the
1402 // earlier location.
1403 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1404 N->setDebugLoc(DL.getDebugLoc());
1405 break;
1406 }
1407 }
1408 return N;
1409}
1410
1412 allnodes_clear();
1413 OperandRecycler.clear(OperandAllocator);
1414 OperandAllocator.Reset();
1415 CSEMap.clear();
1416
1417 ExtendedValueTypeNodes.clear();
1418 ExternalSymbols.clear();
1419 TargetExternalSymbols.clear();
1420 MCSymbols.clear();
1421 SDEI.clear();
1422 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1423 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1424
1425 EntryNode.UseList = nullptr;
1426 InsertNode(&EntryNode);
1427 Root = getEntryNode();
1428 DbgInfo->clear();
1429}
1430
1432 return VT.bitsGT(Op.getValueType())
1433 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1434 : getNode(ISD::FP_ROUND, DL, VT, Op,
1435 getIntPtrConstant(0, DL, /*isTarget=*/true));
1436}
1437
1438std::pair<SDValue, SDValue>
1440 const SDLoc &DL, EVT VT) {
1441 assert(!VT.bitsEq(Op.getValueType()) &&
1442 "Strict no-op FP extend/round not allowed.");
1443 SDValue Res =
1444 VT.bitsGT(Op.getValueType())
1445 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1446 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1447 {Chain, Op, getIntPtrConstant(0, DL)});
1448
1449 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1450}
1451
1453 return VT.bitsGT(Op.getValueType()) ?
1454 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1455 getNode(ISD::TRUNCATE, DL, VT, Op);
1456}
1457
1459 return VT.bitsGT(Op.getValueType()) ?
1460 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1461 getNode(ISD::TRUNCATE, DL, VT, Op);
1462}
1463
1465 return VT.bitsGT(Op.getValueType()) ?
1466 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1467 getNode(ISD::TRUNCATE, DL, VT, Op);
1468}
1469
1471 EVT VT) {
1472 assert(!VT.isVector());
1473 auto Type = Op.getValueType();
1474 SDValue DestOp;
1475 if (Type == VT)
1476 return Op;
1477 auto Size = Op.getValueSizeInBits();
1478 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1479 if (DestOp.getValueType() == VT)
1480 return DestOp;
1481
1482 return getAnyExtOrTrunc(DestOp, DL, VT);
1483}
1484
1486 EVT VT) {
1487 assert(!VT.isVector());
1488 auto Type = Op.getValueType();
1489 SDValue DestOp;
1490 if (Type == VT)
1491 return Op;
1492 auto Size = Op.getValueSizeInBits();
1493 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1494 if (DestOp.getValueType() == VT)
1495 return DestOp;
1496
1497 return getSExtOrTrunc(DestOp, DL, VT);
1498}
1499
1501 EVT VT) {
1502 assert(!VT.isVector());
1503 auto Type = Op.getValueType();
1504 SDValue DestOp;
1505 if (Type == VT)
1506 return Op;
1507 auto Size = Op.getValueSizeInBits();
1508 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1509 if (DestOp.getValueType() == VT)
1510 return DestOp;
1511
1512 return getZExtOrTrunc(DestOp, DL, VT);
1513}
1514
1516 EVT OpVT) {
1517 if (VT.bitsLE(Op.getValueType()))
1518 return getNode(ISD::TRUNCATE, SL, VT, Op);
1519
1521 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1522}
1523
1525 EVT OpVT = Op.getValueType();
1526 assert(VT.isInteger() && OpVT.isInteger() &&
1527 "Cannot getZeroExtendInReg FP types");
1528 assert(VT.isVector() == OpVT.isVector() &&
1529 "getZeroExtendInReg type should be vector iff the operand "
1530 "type is vector!");
1531 assert((!VT.isVector() ||
1533 "Vector element counts must match in getZeroExtendInReg");
1534 assert(VT.bitsLE(OpVT) && "Not extending!");
1535 if (OpVT == VT)
1536 return Op;
1538 VT.getScalarSizeInBits());
1539 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1540}
1541
1543 SDValue EVL, const SDLoc &DL,
1544 EVT VT) {
1545 EVT OpVT = Op.getValueType();
1546 assert(VT.isInteger() && OpVT.isInteger() &&
1547 "Cannot getVPZeroExtendInReg FP types");
1548 assert(VT.isVector() && OpVT.isVector() &&
1549 "getVPZeroExtendInReg type and operand type should be vector!");
1551 "Vector element counts must match in getZeroExtendInReg");
1552 assert(VT.bitsLE(OpVT) && "Not extending!");
1553 if (OpVT == VT)
1554 return Op;
1556 VT.getScalarSizeInBits());
1557 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1558 EVL);
1559}
1560
1562 // Only unsigned pointer semantics are supported right now. In the future this
1563 // might delegate to TLI to check pointer signedness.
1564 return getZExtOrTrunc(Op, DL, VT);
1565}
1566
1568 // Only unsigned pointer semantics are supported right now. In the future this
1569 // might delegate to TLI to check pointer signedness.
1570 return getZeroExtendInReg(Op, DL, VT);
1571}
1572
1574 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1575}
1576
1577/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1579 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1580}
1581
1583 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1584 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1585}
1586
1588 SDValue Mask, SDValue EVL, EVT VT) {
1589 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1590 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1591}
1592
1594 SDValue Mask, SDValue EVL) {
1595 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1596}
1597
1599 SDValue Mask, SDValue EVL) {
1600 if (VT.bitsGT(Op.getValueType()))
1601 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1602 if (VT.bitsLT(Op.getValueType()))
1603 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1604 return Op;
1605}
1606
1608 EVT OpVT) {
1609 if (!V)
1610 return getConstant(0, DL, VT);
1611
1612 switch (TLI->getBooleanContents(OpVT)) {
1615 return getConstant(1, DL, VT);
1617 return getAllOnesConstant(DL, VT);
1618 }
1619 llvm_unreachable("Unexpected boolean content enum!");
1620}
1621
1623 bool isT, bool isO) {
1624 EVT EltVT = VT.getScalarType();
1625 assert((EltVT.getSizeInBits() >= 64 ||
1626 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1627 "getConstant with a uint64_t value that doesn't fit in the type!");
1628 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1629}
1630
1632 bool isT, bool isO) {
1633 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1634}
1635
1637 EVT VT, bool isT, bool isO) {
1638 assert(VT.isInteger() && "Cannot create FP integer constant!");
1639
1640 EVT EltVT = VT.getScalarType();
1641 const ConstantInt *Elt = &Val;
1642
1643 // In some cases the vector type is legal but the element type is illegal and
1644 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1645 // inserted value (the type does not need to match the vector element type).
1646 // Any extra bits introduced will be truncated away.
1647 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1649 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1650 APInt NewVal;
1651 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1652 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1653 else
1654 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1655 Elt = ConstantInt::get(*getContext(), NewVal);
1656 }
1657 // In other cases the element type is illegal and needs to be expanded, for
1658 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1659 // the value into n parts and use a vector type with n-times the elements.
1660 // Then bitcast to the type requested.
1661 // Legalizing constants too early makes the DAGCombiner's job harder so we
1662 // only legalize if the DAG tells us we must produce legal types.
1663 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1664 TLI->getTypeAction(*getContext(), EltVT) ==
1666 const APInt &NewVal = Elt->getValue();
1667 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1668 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1669
1670 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1671 if (VT.isScalableVector() ||
1673 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1674 "Can only handle an even split!");
1675 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1676
1677 SmallVector<SDValue, 2> ScalarParts;
1678 for (unsigned i = 0; i != Parts; ++i)
1679 ScalarParts.push_back(getConstant(
1680 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1681 ViaEltVT, isT, isO));
1682
1683 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1684 }
1685
1686 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1687 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1688
1689 // Check the temporary vector is the correct size. If this fails then
1690 // getTypeToTransformTo() probably returned a type whose size (in bits)
1691 // isn't a power-of-2 factor of the requested type size.
1692 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1693
1694 SmallVector<SDValue, 2> EltParts;
1695 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1696 EltParts.push_back(getConstant(
1697 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1698 ViaEltVT, isT, isO));
1699
1700 // EltParts is currently in little endian order. If we actually want
1701 // big-endian order then reverse it now.
1702 if (getDataLayout().isBigEndian())
1703 std::reverse(EltParts.begin(), EltParts.end());
1704
1705 // The elements must be reversed when the element order is different
1706 // to the endianness of the elements (because the BITCAST is itself a
1707 // vector shuffle in this situation). However, we do not need any code to
1708 // perform this reversal because getConstant() is producing a vector
1709 // splat.
1710 // This situation occurs in MIPS MSA.
1711
1713 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1714 llvm::append_range(Ops, EltParts);
1715
1716 SDValue V =
1717 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1718 return V;
1719 }
1720
1721 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1722 "APInt size does not match type size!");
1723 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1724 SDVTList VTs = getVTList(EltVT);
1726 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1727 ID.AddPointer(Elt);
1728 ID.AddBoolean(isO);
1729 void *IP = nullptr;
1730 SDNode *N = nullptr;
1731 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1732 if (!VT.isVector())
1733 return SDValue(N, 0);
1734
1735 if (!N) {
1736 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1737 CSEMap.InsertNode(N, IP);
1738 InsertNode(N);
1739 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1740 }
1741
1742 SDValue Result(N, 0);
1743 if (VT.isVector())
1744 Result = getSplat(VT, DL, Result);
1745 return Result;
1746}
1747
1749 bool isTarget) {
1750 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1751}
1752
1754 const SDLoc &DL, bool LegalTypes) {
1755 assert(VT.isInteger() && "Shift amount is not an integer type!");
1756 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout(), LegalTypes);
1757 return getConstant(Val, DL, ShiftVT);
1758}
1759
1761 const SDLoc &DL, bool LegalTypes) {
1762 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1763 return getShiftAmountConstant(Val.getZExtValue(), VT, DL, LegalTypes);
1764}
1765
1767 bool isTarget) {
1768 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1769}
1770
1772 bool isTarget) {
1773 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1774}
1775
1777 EVT VT, bool isTarget) {
1778 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1779
1780 EVT EltVT = VT.getScalarType();
1781
1782 // Do the map lookup using the actual bit pattern for the floating point
1783 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1784 // we don't have issues with SNANs.
1785 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1786 SDVTList VTs = getVTList(EltVT);
1788 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1789 ID.AddPointer(&V);
1790 void *IP = nullptr;
1791 SDNode *N = nullptr;
1792 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1793 if (!VT.isVector())
1794 return SDValue(N, 0);
1795
1796 if (!N) {
1797 N = newSDNode<ConstantFPSDNode>(isTarget, &V, VTs);
1798 CSEMap.InsertNode(N, IP);
1799 InsertNode(N);
1800 }
1801
1802 SDValue Result(N, 0);
1803 if (VT.isVector())
1804 Result = getSplat(VT, DL, Result);
1805 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1806 return Result;
1807}
1808
1810 bool isTarget) {
1811 EVT EltVT = VT.getScalarType();
1812 if (EltVT == MVT::f32)
1813 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1814 if (EltVT == MVT::f64)
1815 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1816 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1817 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1818 bool Ignored;
1819 APFloat APF = APFloat(Val);
1821 &Ignored);
1822 return getConstantFP(APF, DL, VT, isTarget);
1823 }
1824 llvm_unreachable("Unsupported type in getConstantFP");
1825}
1826
1828 EVT VT, int64_t Offset, bool isTargetGA,
1829 unsigned TargetFlags) {
1830 assert((TargetFlags == 0 || isTargetGA) &&
1831 "Cannot set target flags on target-independent globals");
1832
1833 // Truncate (with sign-extension) the offset value to the pointer size.
1835 if (BitWidth < 64)
1837
1838 unsigned Opc;
1839 if (GV->isThreadLocal())
1841 else
1842 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1843
1844 SDVTList VTs = getVTList(VT);
1846 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1847 ID.AddPointer(GV);
1848 ID.AddInteger(Offset);
1849 ID.AddInteger(TargetFlags);
1850 void *IP = nullptr;
1851 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1852 return SDValue(E, 0);
1853
1854 auto *N = newSDNode<GlobalAddressSDNode>(
1855 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1856 CSEMap.InsertNode(N, IP);
1857 InsertNode(N);
1858 return SDValue(N, 0);
1859}
1860
1861SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1862 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1863 SDVTList VTs = getVTList(VT);
1865 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1866 ID.AddInteger(FI);
1867 void *IP = nullptr;
1868 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1869 return SDValue(E, 0);
1870
1871 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1872 CSEMap.InsertNode(N, IP);
1873 InsertNode(N);
1874 return SDValue(N, 0);
1875}
1876
1877SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1878 unsigned TargetFlags) {
1879 assert((TargetFlags == 0 || isTarget) &&
1880 "Cannot set target flags on target-independent jump tables");
1881 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1882 SDVTList VTs = getVTList(VT);
1884 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1885 ID.AddInteger(JTI);
1886 ID.AddInteger(TargetFlags);
1887 void *IP = nullptr;
1888 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1889 return SDValue(E, 0);
1890
1891 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1892 CSEMap.InsertNode(N, IP);
1893 InsertNode(N);
1894 return SDValue(N, 0);
1895}
1896
1898 const SDLoc &DL) {
1900 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1901 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1902}
1903
1905 MaybeAlign Alignment, int Offset,
1906 bool isTarget, unsigned TargetFlags) {
1907 assert((TargetFlags == 0 || isTarget) &&
1908 "Cannot set target flags on target-independent globals");
1909 if (!Alignment)
1910 Alignment = shouldOptForSize()
1911 ? getDataLayout().getABITypeAlign(C->getType())
1912 : getDataLayout().getPrefTypeAlign(C->getType());
1913 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1914 SDVTList VTs = getVTList(VT);
1916 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1917 ID.AddInteger(Alignment->value());
1918 ID.AddInteger(Offset);
1919 ID.AddPointer(C);
1920 ID.AddInteger(TargetFlags);
1921 void *IP = nullptr;
1922 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1923 return SDValue(E, 0);
1924
1925 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1926 TargetFlags);
1927 CSEMap.InsertNode(N, IP);
1928 InsertNode(N);
1929 SDValue V = SDValue(N, 0);
1930 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1931 return V;
1932}
1933
1935 MaybeAlign Alignment, int Offset,
1936 bool isTarget, unsigned TargetFlags) {
1937 assert((TargetFlags == 0 || isTarget) &&
1938 "Cannot set target flags on target-independent globals");
1939 if (!Alignment)
1940 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1941 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1942 SDVTList VTs = getVTList(VT);
1944 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1945 ID.AddInteger(Alignment->value());
1946 ID.AddInteger(Offset);
1947 C->addSelectionDAGCSEId(ID);
1948 ID.AddInteger(TargetFlags);
1949 void *IP = nullptr;
1950 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1951 return SDValue(E, 0);
1952
1953 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1954 TargetFlags);
1955 CSEMap.InsertNode(N, IP);
1956 InsertNode(N);
1957 return SDValue(N, 0);
1958}
1959
1962 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), std::nullopt);
1963 ID.AddPointer(MBB);
1964 void *IP = nullptr;
1965 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1966 return SDValue(E, 0);
1967
1968 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1969 CSEMap.InsertNode(N, IP);
1970 InsertNode(N);
1971 return SDValue(N, 0);
1972}
1973
1975 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1976 ValueTypeNodes.size())
1977 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1978
1979 SDNode *&N = VT.isExtended() ?
1980 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1981
1982 if (N) return SDValue(N, 0);
1983 N = newSDNode<VTSDNode>(VT);
1984 InsertNode(N);
1985 return SDValue(N, 0);
1986}
1987
1989 SDNode *&N = ExternalSymbols[Sym];
1990 if (N) return SDValue(N, 0);
1991 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
1992 InsertNode(N);
1993 return SDValue(N, 0);
1994}
1995
1997 SDNode *&N = MCSymbols[Sym];
1998 if (N)
1999 return SDValue(N, 0);
2000 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2001 InsertNode(N);
2002 return SDValue(N, 0);
2003}
2004
2006 unsigned TargetFlags) {
2007 SDNode *&N =
2008 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2009 if (N) return SDValue(N, 0);
2010 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2011 InsertNode(N);
2012 return SDValue(N, 0);
2013}
2014
2016 if ((unsigned)Cond >= CondCodeNodes.size())
2017 CondCodeNodes.resize(Cond+1);
2018
2019 if (!CondCodeNodes[Cond]) {
2020 auto *N = newSDNode<CondCodeSDNode>(Cond);
2021 CondCodeNodes[Cond] = N;
2022 InsertNode(N);
2023 }
2024
2025 return SDValue(CondCodeNodes[Cond], 0);
2026}
2027
2029 bool ConstantFold) {
2030 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2031 "APInt size does not match type size!");
2032
2033 if (MulImm == 0)
2034 return getConstant(0, DL, VT);
2035
2036 if (ConstantFold) {
2037 const MachineFunction &MF = getMachineFunction();
2038 const Function &F = MF.getFunction();
2039 ConstantRange CR = getVScaleRange(&F, 64);
2040 if (const APInt *C = CR.getSingleElement())
2041 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2042 }
2043
2044 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2045}
2046
2048 bool ConstantFold) {
2049 if (EC.isScalable())
2050 return getVScale(DL, VT,
2051 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2052
2053 return getConstant(EC.getKnownMinValue(), DL, VT);
2054}
2055
2057 APInt One(ResVT.getScalarSizeInBits(), 1);
2058 return getStepVector(DL, ResVT, One);
2059}
2060
2062 const APInt &StepVal) {
2063 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2064 if (ResVT.isScalableVector())
2065 return getNode(
2066 ISD::STEP_VECTOR, DL, ResVT,
2067 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2068
2069 SmallVector<SDValue, 16> OpsStepConstants;
2070 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2071 OpsStepConstants.push_back(
2072 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2073 return getBuildVector(ResVT, DL, OpsStepConstants);
2074}
2075
2076/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2077/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2079 std::swap(N1, N2);
2081}
2082
2084 SDValue N2, ArrayRef<int> Mask) {
2085 assert(VT.getVectorNumElements() == Mask.size() &&
2086 "Must have the same number of vector elements as mask elements!");
2087 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2088 "Invalid VECTOR_SHUFFLE");
2089
2090 // Canonicalize shuffle undef, undef -> undef
2091 if (N1.isUndef() && N2.isUndef())
2092 return getUNDEF(VT);
2093
2094 // Validate that all indices in Mask are within the range of the elements
2095 // input to the shuffle.
2096 int NElts = Mask.size();
2097 assert(llvm::all_of(Mask,
2098 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2099 "Index out of range");
2100
2101 // Copy the mask so we can do any needed cleanup.
2102 SmallVector<int, 8> MaskVec(Mask);
2103
2104 // Canonicalize shuffle v, v -> v, undef
2105 if (N1 == N2) {
2106 N2 = getUNDEF(VT);
2107 for (int i = 0; i != NElts; ++i)
2108 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2109 }
2110
2111 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2112 if (N1.isUndef())
2113 commuteShuffle(N1, N2, MaskVec);
2114
2115 if (TLI->hasVectorBlend()) {
2116 // If shuffling a splat, try to blend the splat instead. We do this here so
2117 // that even when this arises during lowering we don't have to re-handle it.
2118 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2119 BitVector UndefElements;
2120 SDValue Splat = BV->getSplatValue(&UndefElements);
2121 if (!Splat)
2122 return;
2123
2124 for (int i = 0; i < NElts; ++i) {
2125 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2126 continue;
2127
2128 // If this input comes from undef, mark it as such.
2129 if (UndefElements[MaskVec[i] - Offset]) {
2130 MaskVec[i] = -1;
2131 continue;
2132 }
2133
2134 // If we can blend a non-undef lane, use that instead.
2135 if (!UndefElements[i])
2136 MaskVec[i] = i + Offset;
2137 }
2138 };
2139 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2140 BlendSplat(N1BV, 0);
2141 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2142 BlendSplat(N2BV, NElts);
2143 }
2144
2145 // Canonicalize all index into lhs, -> shuffle lhs, undef
2146 // Canonicalize all index into rhs, -> shuffle rhs, undef
2147 bool AllLHS = true, AllRHS = true;
2148 bool N2Undef = N2.isUndef();
2149 for (int i = 0; i != NElts; ++i) {
2150 if (MaskVec[i] >= NElts) {
2151 if (N2Undef)
2152 MaskVec[i] = -1;
2153 else
2154 AllLHS = false;
2155 } else if (MaskVec[i] >= 0) {
2156 AllRHS = false;
2157 }
2158 }
2159 if (AllLHS && AllRHS)
2160 return getUNDEF(VT);
2161 if (AllLHS && !N2Undef)
2162 N2 = getUNDEF(VT);
2163 if (AllRHS) {
2164 N1 = getUNDEF(VT);
2165 commuteShuffle(N1, N2, MaskVec);
2166 }
2167 // Reset our undef status after accounting for the mask.
2168 N2Undef = N2.isUndef();
2169 // Re-check whether both sides ended up undef.
2170 if (N1.isUndef() && N2Undef)
2171 return getUNDEF(VT);
2172
2173 // If Identity shuffle return that node.
2174 bool Identity = true, AllSame = true;
2175 for (int i = 0; i != NElts; ++i) {
2176 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2177 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2178 }
2179 if (Identity && NElts)
2180 return N1;
2181
2182 // Shuffling a constant splat doesn't change the result.
2183 if (N2Undef) {
2184 SDValue V = N1;
2185
2186 // Look through any bitcasts. We check that these don't change the number
2187 // (and size) of elements and just changes their types.
2188 while (V.getOpcode() == ISD::BITCAST)
2189 V = V->getOperand(0);
2190
2191 // A splat should always show up as a build vector node.
2192 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2193 BitVector UndefElements;
2194 SDValue Splat = BV->getSplatValue(&UndefElements);
2195 // If this is a splat of an undef, shuffling it is also undef.
2196 if (Splat && Splat.isUndef())
2197 return getUNDEF(VT);
2198
2199 bool SameNumElts =
2200 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2201
2202 // We only have a splat which can skip shuffles if there is a splatted
2203 // value and no undef lanes rearranged by the shuffle.
2204 if (Splat && UndefElements.none()) {
2205 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2206 // number of elements match or the value splatted is a zero constant.
2207 if (SameNumElts || isNullConstant(Splat))
2208 return N1;
2209 }
2210
2211 // If the shuffle itself creates a splat, build the vector directly.
2212 if (AllSame && SameNumElts) {
2213 EVT BuildVT = BV->getValueType(0);
2214 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2215 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2216
2217 // We may have jumped through bitcasts, so the type of the
2218 // BUILD_VECTOR may not match the type of the shuffle.
2219 if (BuildVT != VT)
2220 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2221 return NewBV;
2222 }
2223 }
2224 }
2225
2226 SDVTList VTs = getVTList(VT);
2228 SDValue Ops[2] = { N1, N2 };
2230 for (int i = 0; i != NElts; ++i)
2231 ID.AddInteger(MaskVec[i]);
2232
2233 void* IP = nullptr;
2234 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2235 return SDValue(E, 0);
2236
2237 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2238 // SDNode doesn't have access to it. This memory will be "leaked" when
2239 // the node is deallocated, but recovered when the NodeAllocator is released.
2240 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2241 llvm::copy(MaskVec, MaskAlloc);
2242
2243 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2244 dl.getDebugLoc(), MaskAlloc);
2245 createOperands(N, Ops);
2246
2247 CSEMap.InsertNode(N, IP);
2248 InsertNode(N);
2249 SDValue V = SDValue(N, 0);
2250 NewSDValueDbgMsg(V, "Creating new node: ", this);
2251 return V;
2252}
2253
2255 EVT VT = SV.getValueType(0);
2256 SmallVector<int, 8> MaskVec(SV.getMask());
2258
2259 SDValue Op0 = SV.getOperand(0);
2260 SDValue Op1 = SV.getOperand(1);
2261 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2262}
2263
2265 SDVTList VTs = getVTList(VT);
2267 AddNodeIDNode(ID, ISD::Register, VTs, std::nullopt);
2268 ID.AddInteger(RegNo);
2269 void *IP = nullptr;
2270 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2271 return SDValue(E, 0);
2272
2273 auto *N = newSDNode<RegisterSDNode>(RegNo, VTs);
2274 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2275 CSEMap.InsertNode(N, IP);
2276 InsertNode(N);
2277 return SDValue(N, 0);
2278}
2279
2282 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), std::nullopt);
2283 ID.AddPointer(RegMask);
2284 void *IP = nullptr;
2285 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2286 return SDValue(E, 0);
2287
2288 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2289 CSEMap.InsertNode(N, IP);
2290 InsertNode(N);
2291 return SDValue(N, 0);
2292}
2293
2295 MCSymbol *Label) {
2296 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2297}
2298
2299SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2300 SDValue Root, MCSymbol *Label) {
2302 SDValue Ops[] = { Root };
2303 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2304 ID.AddPointer(Label);
2305 void *IP = nullptr;
2306 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2307 return SDValue(E, 0);
2308
2309 auto *N =
2310 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2311 createOperands(N, Ops);
2312
2313 CSEMap.InsertNode(N, IP);
2314 InsertNode(N);
2315 return SDValue(N, 0);
2316}
2317
2319 int64_t Offset, bool isTarget,
2320 unsigned TargetFlags) {
2321 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2322 SDVTList VTs = getVTList(VT);
2323
2325 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
2326 ID.AddPointer(BA);
2327 ID.AddInteger(Offset);
2328 ID.AddInteger(TargetFlags);
2329 void *IP = nullptr;
2330 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2331 return SDValue(E, 0);
2332
2333 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2334 CSEMap.InsertNode(N, IP);
2335 InsertNode(N);
2336 return SDValue(N, 0);
2337}
2338
2341 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), std::nullopt);
2342 ID.AddPointer(V);
2343
2344 void *IP = nullptr;
2345 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2346 return SDValue(E, 0);
2347
2348 auto *N = newSDNode<SrcValueSDNode>(V);
2349 CSEMap.InsertNode(N, IP);
2350 InsertNode(N);
2351 return SDValue(N, 0);
2352}
2353
2356 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), std::nullopt);
2357 ID.AddPointer(MD);
2358
2359 void *IP = nullptr;
2360 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2361 return SDValue(E, 0);
2362
2363 auto *N = newSDNode<MDNodeSDNode>(MD);
2364 CSEMap.InsertNode(N, IP);
2365 InsertNode(N);
2366 return SDValue(N, 0);
2367}
2368
2370 if (VT == V.getValueType())
2371 return V;
2372
2373 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2374}
2375
2377 unsigned SrcAS, unsigned DestAS) {
2378 SDVTList VTs = getVTList(VT);
2379 SDValue Ops[] = {Ptr};
2382 ID.AddInteger(SrcAS);
2383 ID.AddInteger(DestAS);
2384
2385 void *IP = nullptr;
2386 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2387 return SDValue(E, 0);
2388
2389 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2390 VTs, SrcAS, DestAS);
2391 createOperands(N, Ops);
2392
2393 CSEMap.InsertNode(N, IP);
2394 InsertNode(N);
2395 return SDValue(N, 0);
2396}
2397
2399 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2400}
2401
2402/// getShiftAmountOperand - Return the specified value casted to
2403/// the target's desired shift amount type.
2405 EVT OpTy = Op.getValueType();
2406 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2407 if (OpTy == ShTy || OpTy.isVector()) return Op;
2408
2409 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2410}
2411
2413 SDLoc dl(Node);
2415 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2416 EVT VT = Node->getValueType(0);
2417 SDValue Tmp1 = Node->getOperand(0);
2418 SDValue Tmp2 = Node->getOperand(1);
2419 const MaybeAlign MA(Node->getConstantOperandVal(3));
2420
2421 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2422 Tmp2, MachinePointerInfo(V));
2423 SDValue VAList = VAListLoad;
2424
2425 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2426 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2427 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2428
2429 VAList =
2430 getNode(ISD::AND, dl, VAList.getValueType(), VAList,
2431 getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2432 }
2433
2434 // Increment the pointer, VAList, to the next vaarg
2435 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2436 getConstant(getDataLayout().getTypeAllocSize(
2437 VT.getTypeForEVT(*getContext())),
2438 dl, VAList.getValueType()));
2439 // Store the incremented VAList to the legalized pointer
2440 Tmp1 =
2441 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2442 // Load the actual argument out of the pointer VAList
2443 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2444}
2445
2447 SDLoc dl(Node);
2449 // This defaults to loading a pointer from the input and storing it to the
2450 // output, returning the chain.
2451 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2452 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2453 SDValue Tmp1 =
2454 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2455 Node->getOperand(2), MachinePointerInfo(VS));
2456 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2457 MachinePointerInfo(VD));
2458}
2459
2461 const DataLayout &DL = getDataLayout();
2462 Type *Ty = VT.getTypeForEVT(*getContext());
2463 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2464
2465 if (TLI->isTypeLegal(VT) || !VT.isVector())
2466 return RedAlign;
2467
2469 const Align StackAlign = TFI->getStackAlign();
2470
2471 // See if we can choose a smaller ABI alignment in cases where it's an
2472 // illegal vector type that will get broken down.
2473 if (RedAlign > StackAlign) {
2474 EVT IntermediateVT;
2475 MVT RegisterVT;
2476 unsigned NumIntermediates;
2477 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2478 NumIntermediates, RegisterVT);
2479 Ty = IntermediateVT.getTypeForEVT(*getContext());
2480 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2481 if (RedAlign2 < RedAlign)
2482 RedAlign = RedAlign2;
2483 }
2484
2485 return RedAlign;
2486}
2487
2489 MachineFrameInfo &MFI = MF->getFrameInfo();
2491 int StackID = 0;
2492 if (Bytes.isScalable())
2493 StackID = TFI->getStackIDForScalableVectors();
2494 // The stack id gives an indication of whether the object is scalable or
2495 // not, so it's safe to pass in the minimum size here.
2496 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2497 false, nullptr, StackID);
2498 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2499}
2500
2502 Type *Ty = VT.getTypeForEVT(*getContext());
2503 Align StackAlign =
2504 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2505 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2506}
2507
2509 TypeSize VT1Size = VT1.getStoreSize();
2510 TypeSize VT2Size = VT2.getStoreSize();
2511 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2512 "Don't know how to choose the maximum size when creating a stack "
2513 "temporary");
2514 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2515 ? VT1Size
2516 : VT2Size;
2517
2518 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2519 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2520 const DataLayout &DL = getDataLayout();
2521 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2522 return CreateStackTemporary(Bytes, Align);
2523}
2524
2526 ISD::CondCode Cond, const SDLoc &dl) {
2527 EVT OpVT = N1.getValueType();
2528
2529 auto GetUndefBooleanConstant = [&]() {
2530 if (VT.getScalarType() == MVT::i1 ||
2531 TLI->getBooleanContents(OpVT) ==
2533 return getUNDEF(VT);
2534 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2535 // so we cannot use getUNDEF(). Return zero instead.
2536 return getConstant(0, dl, VT);
2537 };
2538
2539 // These setcc operations always fold.
2540 switch (Cond) {
2541 default: break;
2542 case ISD::SETFALSE:
2543 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2544 case ISD::SETTRUE:
2545 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2546
2547 case ISD::SETOEQ:
2548 case ISD::SETOGT:
2549 case ISD::SETOGE:
2550 case ISD::SETOLT:
2551 case ISD::SETOLE:
2552 case ISD::SETONE:
2553 case ISD::SETO:
2554 case ISD::SETUO:
2555 case ISD::SETUEQ:
2556 case ISD::SETUNE:
2557 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2558 break;
2559 }
2560
2561 if (OpVT.isInteger()) {
2562 // For EQ and NE, we can always pick a value for the undef to make the
2563 // predicate pass or fail, so we can return undef.
2564 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2565 // icmp eq/ne X, undef -> undef.
2566 if ((N1.isUndef() || N2.isUndef()) &&
2567 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2568 return GetUndefBooleanConstant();
2569
2570 // If both operands are undef, we can return undef for int comparison.
2571 // icmp undef, undef -> undef.
2572 if (N1.isUndef() && N2.isUndef())
2573 return GetUndefBooleanConstant();
2574
2575 // icmp X, X -> true/false
2576 // icmp X, undef -> true/false because undef could be X.
2577 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2578 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2579 }
2580
2581 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2582 const APInt &C2 = N2C->getAPIntValue();
2583 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2584 const APInt &C1 = N1C->getAPIntValue();
2585
2587 dl, VT, OpVT);
2588 }
2589 }
2590
2591 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2592 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2593
2594 if (N1CFP && N2CFP) {
2595 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2596 switch (Cond) {
2597 default: break;
2598 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2599 return GetUndefBooleanConstant();
2600 [[fallthrough]];
2601 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2602 OpVT);
2603 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2604 return GetUndefBooleanConstant();
2605 [[fallthrough]];
2607 R==APFloat::cmpLessThan, dl, VT,
2608 OpVT);
2609 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2610 return GetUndefBooleanConstant();
2611 [[fallthrough]];
2612 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2613 OpVT);
2614 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2615 return GetUndefBooleanConstant();
2616 [[fallthrough]];
2618 VT, OpVT);
2619 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2620 return GetUndefBooleanConstant();
2621 [[fallthrough]];
2623 R==APFloat::cmpEqual, dl, VT,
2624 OpVT);
2625 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2626 return GetUndefBooleanConstant();
2627 [[fallthrough]];
2629 R==APFloat::cmpEqual, dl, VT, OpVT);
2630 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2631 OpVT);
2632 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2633 OpVT);
2635 R==APFloat::cmpEqual, dl, VT,
2636 OpVT);
2637 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2638 OpVT);
2640 R==APFloat::cmpLessThan, dl, VT,
2641 OpVT);
2643 R==APFloat::cmpUnordered, dl, VT,
2644 OpVT);
2646 VT, OpVT);
2647 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2648 OpVT);
2649 }
2650 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2651 // Ensure that the constant occurs on the RHS.
2653 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2654 return SDValue();
2655 return getSetCC(dl, VT, N2, N1, SwappedCond);
2656 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2657 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2658 // If an operand is known to be a nan (or undef that could be a nan), we can
2659 // fold it.
2660 // Choosing NaN for the undef will always make unordered comparison succeed
2661 // and ordered comparison fails.
2662 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2663 switch (ISD::getUnorderedFlavor(Cond)) {
2664 default:
2665 llvm_unreachable("Unknown flavor!");
2666 case 0: // Known false.
2667 return getBoolConstant(false, dl, VT, OpVT);
2668 case 1: // Known true.
2669 return getBoolConstant(true, dl, VT, OpVT);
2670 case 2: // Undefined.
2671 return GetUndefBooleanConstant();
2672 }
2673 }
2674
2675 // Could not fold it.
2676 return SDValue();
2677}
2678
2679/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2680/// use this predicate to simplify operations downstream.
2682 unsigned BitWidth = Op.getScalarValueSizeInBits();
2684}
2685
2686/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2687/// this predicate to simplify operations downstream. Mask is known to be zero
2688/// for bits that V cannot have.
2690 unsigned Depth) const {
2691 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2692}
2693
2694/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2695/// DemandedElts. We use this predicate to simplify operations downstream.
2696/// Mask is known to be zero for bits that V cannot have.
2698 const APInt &DemandedElts,
2699 unsigned Depth) const {
2700 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2701}
2702
2703/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2704/// DemandedElts. We use this predicate to simplify operations downstream.
2706 unsigned Depth /* = 0 */) const {
2707 return computeKnownBits(V, DemandedElts, Depth).isZero();
2708}
2709
2710/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2712 unsigned Depth) const {
2713 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2714}
2715
2717 const APInt &DemandedElts,
2718 unsigned Depth) const {
2719 EVT VT = Op.getValueType();
2720 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2721
2722 unsigned NumElts = VT.getVectorNumElements();
2723 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2724
2725 APInt KnownZeroElements = APInt::getZero(NumElts);
2726 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2727 if (!DemandedElts[EltIdx])
2728 continue; // Don't query elements that are not demanded.
2729 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2730 if (MaskedVectorIsZero(Op, Mask, Depth))
2731 KnownZeroElements.setBit(EltIdx);
2732 }
2733 return KnownZeroElements;
2734}
2735
2736/// isSplatValue - Return true if the vector V has the same value
2737/// across all DemandedElts. For scalable vectors, we don't know the
2738/// number of lanes at compile time. Instead, we use a 1 bit APInt
2739/// to represent a conservative value for all lanes; that is, that
2740/// one bit value is implicitly splatted across all lanes.
2741bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2742 APInt &UndefElts, unsigned Depth) const {
2743 unsigned Opcode = V.getOpcode();
2744 EVT VT = V.getValueType();
2745 assert(VT.isVector() && "Vector type expected");
2746 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2747 "scalable demanded bits are ignored");
2748
2749 if (!DemandedElts)
2750 return false; // No demanded elts, better to assume we don't know anything.
2751
2752 if (Depth >= MaxRecursionDepth)
2753 return false; // Limit search depth.
2754
2755 // Deal with some common cases here that work for both fixed and scalable
2756 // vector types.
2757 switch (Opcode) {
2758 case ISD::SPLAT_VECTOR:
2759 UndefElts = V.getOperand(0).isUndef()
2760 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2761 : APInt(DemandedElts.getBitWidth(), 0);
2762 return true;
2763 case ISD::ADD:
2764 case ISD::SUB:
2765 case ISD::AND:
2766 case ISD::XOR:
2767 case ISD::OR: {
2768 APInt UndefLHS, UndefRHS;
2769 SDValue LHS = V.getOperand(0);
2770 SDValue RHS = V.getOperand(1);
2771 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2772 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2773 UndefElts = UndefLHS | UndefRHS;
2774 return true;
2775 }
2776 return false;
2777 }
2778 case ISD::ABS:
2779 case ISD::TRUNCATE:
2780 case ISD::SIGN_EXTEND:
2781 case ISD::ZERO_EXTEND:
2782 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2783 default:
2784 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2785 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2786 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2787 Depth);
2788 break;
2789}
2790
2791 // We don't support other cases than those above for scalable vectors at
2792 // the moment.
2793 if (VT.isScalableVector())
2794 return false;
2795
2796 unsigned NumElts = VT.getVectorNumElements();
2797 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2798 UndefElts = APInt::getZero(NumElts);
2799
2800 switch (Opcode) {
2801 case ISD::BUILD_VECTOR: {
2802 SDValue Scl;
2803 for (unsigned i = 0; i != NumElts; ++i) {
2804 SDValue Op = V.getOperand(i);
2805 if (Op.isUndef()) {
2806 UndefElts.setBit(i);
2807 continue;
2808 }
2809 if (!DemandedElts[i])
2810 continue;
2811 if (Scl && Scl != Op)
2812 return false;
2813 Scl = Op;
2814 }
2815 return true;
2816 }
2817 case ISD::VECTOR_SHUFFLE: {
2818 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2819 APInt DemandedLHS = APInt::getZero(NumElts);
2820 APInt DemandedRHS = APInt::getZero(NumElts);
2821 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2822 for (int i = 0; i != (int)NumElts; ++i) {
2823 int M = Mask[i];
2824 if (M < 0) {
2825 UndefElts.setBit(i);
2826 continue;
2827 }
2828 if (!DemandedElts[i])
2829 continue;
2830 if (M < (int)NumElts)
2831 DemandedLHS.setBit(M);
2832 else
2833 DemandedRHS.setBit(M - NumElts);
2834 }
2835
2836 // If we aren't demanding either op, assume there's no splat.
2837 // If we are demanding both ops, assume there's no splat.
2838 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2839 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2840 return false;
2841
2842 // See if the demanded elts of the source op is a splat or we only demand
2843 // one element, which should always be a splat.
2844 // TODO: Handle source ops splats with undefs.
2845 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2846 APInt SrcUndefs;
2847 return (SrcElts.popcount() == 1) ||
2848 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2849 (SrcElts & SrcUndefs).isZero());
2850 };
2851 if (!DemandedLHS.isZero())
2852 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2853 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2854 }
2856 // Offset the demanded elts by the subvector index.
2857 SDValue Src = V.getOperand(0);
2858 // We don't support scalable vectors at the moment.
2859 if (Src.getValueType().isScalableVector())
2860 return false;
2861 uint64_t Idx = V.getConstantOperandVal(1);
2862 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2863 APInt UndefSrcElts;
2864 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2865 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2866 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2867 return true;
2868 }
2869 break;
2870 }
2874 // Widen the demanded elts by the src element count.
2875 SDValue Src = V.getOperand(0);
2876 // We don't support scalable vectors at the moment.
2877 if (Src.getValueType().isScalableVector())
2878 return false;
2879 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2880 APInt UndefSrcElts;
2881 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2882 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2883 UndefElts = UndefSrcElts.trunc(NumElts);
2884 return true;
2885 }
2886 break;
2887 }
2888 case ISD::BITCAST: {
2889 SDValue Src = V.getOperand(0);
2890 EVT SrcVT = Src.getValueType();
2891 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2892 unsigned BitWidth = VT.getScalarSizeInBits();
2893
2894 // Ignore bitcasts from unsupported types.
2895 // TODO: Add fp support?
2896 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2897 break;
2898
2899 // Bitcast 'small element' vector to 'large element' vector.
2900 if ((BitWidth % SrcBitWidth) == 0) {
2901 // See if each sub element is a splat.
2902 unsigned Scale = BitWidth / SrcBitWidth;
2903 unsigned NumSrcElts = SrcVT.getVectorNumElements();
2904 APInt ScaledDemandedElts =
2905 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
2906 for (unsigned I = 0; I != Scale; ++I) {
2907 APInt SubUndefElts;
2908 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
2909 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
2910 SubDemandedElts &= ScaledDemandedElts;
2911 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
2912 return false;
2913 // TODO: Add support for merging sub undef elements.
2914 if (!SubUndefElts.isZero())
2915 return false;
2916 }
2917 return true;
2918 }
2919 break;
2920 }
2921 }
2922
2923 return false;
2924}
2925
2926/// Helper wrapper to main isSplatValue function.
2927bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
2928 EVT VT = V.getValueType();
2929 assert(VT.isVector() && "Vector type expected");
2930
2931 APInt UndefElts;
2932 // Since the number of lanes in a scalable vector is unknown at compile time,
2933 // we track one bit which is implicitly broadcast to all lanes. This means
2934 // that all lanes in a scalable vector are considered demanded.
2935 APInt DemandedElts
2937 return isSplatValue(V, DemandedElts, UndefElts) &&
2938 (AllowUndefs || !UndefElts);
2939}
2940
2943
2944 EVT VT = V.getValueType();
2945 unsigned Opcode = V.getOpcode();
2946 switch (Opcode) {
2947 default: {
2948 APInt UndefElts;
2949 // Since the number of lanes in a scalable vector is unknown at compile time,
2950 // we track one bit which is implicitly broadcast to all lanes. This means
2951 // that all lanes in a scalable vector are considered demanded.
2952 APInt DemandedElts
2954
2955 if (isSplatValue(V, DemandedElts, UndefElts)) {
2956 if (VT.isScalableVector()) {
2957 // DemandedElts and UndefElts are ignored for scalable vectors, since
2958 // the only supported cases are SPLAT_VECTOR nodes.
2959 SplatIdx = 0;
2960 } else {
2961 // Handle case where all demanded elements are UNDEF.
2962 if (DemandedElts.isSubsetOf(UndefElts)) {
2963 SplatIdx = 0;
2964 return getUNDEF(VT);
2965 }
2966 SplatIdx = (UndefElts & DemandedElts).countr_one();
2967 }
2968 return V;
2969 }
2970 break;
2971 }
2972 case ISD::SPLAT_VECTOR:
2973 SplatIdx = 0;
2974 return V;
2975 case ISD::VECTOR_SHUFFLE: {
2976 assert(!VT.isScalableVector());
2977 // Check if this is a shuffle node doing a splat.
2978 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
2979 // getTargetVShiftNode currently struggles without the splat source.
2980 auto *SVN = cast<ShuffleVectorSDNode>(V);
2981 if (!SVN->isSplat())
2982 break;
2983 int Idx = SVN->getSplatIndex();
2984 int NumElts = V.getValueType().getVectorNumElements();
2985 SplatIdx = Idx % NumElts;
2986 return V.getOperand(Idx / NumElts);
2987 }
2988 }
2989
2990 return SDValue();
2991}
2992
2994 int SplatIdx;
2995 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
2996 EVT SVT = SrcVector.getValueType().getScalarType();
2997 EVT LegalSVT = SVT;
2998 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
2999 if (!SVT.isInteger())
3000 return SDValue();
3001 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3002 if (LegalSVT.bitsLT(SVT))
3003 return SDValue();
3004 }
3005 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
3006 getVectorIdxConstant(SplatIdx, SDLoc(V)));
3007 }
3008 return SDValue();
3009}
3010
3011std::optional<ConstantRange>
3013 unsigned Depth) const {
3014 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3015 V.getOpcode() == ISD::SRA) &&
3016 "Unknown shift node");
3017 // Shifting more than the bitwidth is not valid.
3018 unsigned BitWidth = V.getScalarValueSizeInBits();
3019
3020 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3021 const APInt &ShAmt = Cst->getAPIntValue();
3022 if (ShAmt.uge(BitWidth))
3023 return std::nullopt;
3024 return ConstantRange(ShAmt);
3025 }
3026
3027 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3028 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3029 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3030 if (!DemandedElts[i])
3031 continue;
3032 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3033 if (!SA) {
3034 MinAmt = MaxAmt = nullptr;
3035 break;
3036 }
3037 const APInt &ShAmt = SA->getAPIntValue();
3038 if (ShAmt.uge(BitWidth))
3039 return std::nullopt;
3040 if (!MinAmt || MinAmt->ugt(ShAmt))
3041 MinAmt = &ShAmt;
3042 if (!MaxAmt || MaxAmt->ult(ShAmt))
3043 MaxAmt = &ShAmt;
3044 }
3045 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3046 "Failed to find matching min/max shift amounts");
3047 if (MinAmt && MaxAmt)
3048 return ConstantRange(*MinAmt, *MaxAmt + 1);
3049 }
3050
3051 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3052 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3053 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3054 if (KnownAmt.getMaxValue().ult(BitWidth))
3055 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3056
3057 return std::nullopt;
3058}
3059
3060std::optional<uint64_t>
3062 unsigned Depth) const {
3063 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3064 V.getOpcode() == ISD::SRA) &&
3065 "Unknown shift node");
3066 if (std::optional<ConstantRange> AmtRange =
3067 getValidShiftAmountRange(V, DemandedElts, Depth))
3068 if (const APInt *ShAmt = AmtRange->getSingleElement())
3069 return ShAmt->getZExtValue();
3070 return std::nullopt;
3071}
3072
3073std::optional<uint64_t>
3075 EVT VT = V.getValueType();
3076 APInt DemandedElts = VT.isFixedLengthVector()
3078 : APInt(1, 1);
3079 return getValidShiftAmount(V, DemandedElts, Depth);
3080}
3081
3082std::optional<uint64_t>
3084 unsigned Depth) const {
3085 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3086 V.getOpcode() == ISD::SRA) &&
3087 "Unknown shift node");
3088 if (std::optional<ConstantRange> AmtRange =
3089 getValidShiftAmountRange(V, DemandedElts, Depth))
3090 return AmtRange->getUnsignedMin().getZExtValue();
3091 return std::nullopt;
3092}
3093
3094std::optional<uint64_t>
3096 EVT VT = V.getValueType();
3097 APInt DemandedElts = VT.isFixedLengthVector()
3099 : APInt(1, 1);
3100 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3101}
3102
3103std::optional<uint64_t>
3105 unsigned Depth) const {
3106 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3107 V.getOpcode() == ISD::SRA) &&
3108 "Unknown shift node");
3109 if (std::optional<ConstantRange> AmtRange =
3110 getValidShiftAmountRange(V, DemandedElts, Depth))
3111 return AmtRange->getUnsignedMax().getZExtValue();
3112 return std::nullopt;
3113}
3114
3115std::optional<uint64_t>
3117 EVT VT = V.getValueType();
3118 APInt DemandedElts = VT.isFixedLengthVector()
3120 : APInt(1, 1);
3121 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3122}
3123
3124/// Determine which bits of Op are known to be either zero or one and return
3125/// them in Known. For vectors, the known bits are those that are shared by
3126/// every vector element.
3128 EVT VT = Op.getValueType();
3129
3130 // Since the number of lanes in a scalable vector is unknown at compile time,
3131 // we track one bit which is implicitly broadcast to all lanes. This means
3132 // that all lanes in a scalable vector are considered demanded.
3133 APInt DemandedElts = VT.isFixedLengthVector()
3135 : APInt(1, 1);
3136 return computeKnownBits(Op, DemandedElts, Depth);
3137}
3138
3139/// Determine which bits of Op are known to be either zero or one and return
3140/// them in Known. The DemandedElts argument allows us to only collect the known
3141/// bits that are shared by the requested vector elements.
3143 unsigned Depth) const {
3144 unsigned BitWidth = Op.getScalarValueSizeInBits();
3145
3146 KnownBits Known(BitWidth); // Don't know anything.
3147
3148 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3149 // We know all of the bits for a constant!
3150 return KnownBits::makeConstant(C->getAPIntValue());
3151 }
3152 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3153 // We know all of the bits for a constant fp!
3154 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3155 }
3156
3157 if (Depth >= MaxRecursionDepth)
3158 return Known; // Limit search depth.
3159
3160 KnownBits Known2;
3161 unsigned NumElts = DemandedElts.getBitWidth();
3162 assert((!Op.getValueType().isFixedLengthVector() ||
3163 NumElts == Op.getValueType().getVectorNumElements()) &&
3164 "Unexpected vector size");
3165
3166 if (!DemandedElts)
3167 return Known; // No demanded elts, better to assume we don't know anything.
3168
3169 unsigned Opcode = Op.getOpcode();
3170 switch (Opcode) {
3171 case ISD::MERGE_VALUES:
3172 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3173 Depth + 1);
3174 case ISD::SPLAT_VECTOR: {
3175 SDValue SrcOp = Op.getOperand(0);
3176 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3177 "Expected SPLAT_VECTOR implicit truncation");
3178 // Implicitly truncate the bits to match the official semantics of
3179 // SPLAT_VECTOR.
3180 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3181 break;
3182 }
3184 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3185 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3186 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3187 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3188 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3189 }
3190 break;
3191 }
3192 case ISD::STEP_VECTOR: {
3193 const APInt &Step = Op.getConstantOperandAPInt(0);
3194
3195 if (Step.isPowerOf2())
3196 Known.Zero.setLowBits(Step.logBase2());
3197
3199
3200 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3201 break;
3202 const APInt MinNumElts =
3203 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3204
3205 bool Overflow;
3206 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3208 .umul_ov(MinNumElts, Overflow);
3209 if (Overflow)
3210 break;
3211
3212 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3213 if (Overflow)
3214 break;
3215
3216 Known.Zero.setHighBits(MaxValue.countl_zero());
3217 break;
3218 }
3219 case ISD::BUILD_VECTOR:
3220 assert(!Op.getValueType().isScalableVector());
3221 // Collect the known bits that are shared by every demanded vector element.
3222 Known.Zero.setAllBits(); Known.One.setAllBits();
3223 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3224 if (!DemandedElts[i])
3225 continue;
3226
3227 SDValue SrcOp = Op.getOperand(i);
3228 Known2 = computeKnownBits(SrcOp, Depth + 1);
3229
3230 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3231 if (SrcOp.getValueSizeInBits() != BitWidth) {
3232 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3233 "Expected BUILD_VECTOR implicit truncation");
3234 Known2 = Known2.trunc(BitWidth);
3235 }
3236
3237 // Known bits are the values that are shared by every demanded element.
3238 Known = Known.intersectWith(Known2);
3239
3240 // If we don't know any bits, early out.
3241 if (Known.isUnknown())
3242 break;
3243 }
3244 break;
3245 case ISD::VECTOR_SHUFFLE: {
3246 assert(!Op.getValueType().isScalableVector());
3247 // Collect the known bits that are shared by every vector element referenced
3248 // by the shuffle.
3249 APInt DemandedLHS, DemandedRHS;
3250 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3251 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3252 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3253 DemandedLHS, DemandedRHS))
3254 break;
3255
3256 // Known bits are the values that are shared by every demanded element.
3257 Known.Zero.setAllBits(); Known.One.setAllBits();
3258 if (!!DemandedLHS) {
3259 SDValue LHS = Op.getOperand(0);
3260 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3261 Known = Known.intersectWith(Known2);
3262 }
3263 // If we don't know any bits, early out.
3264 if (Known.isUnknown())
3265 break;
3266 if (!!DemandedRHS) {
3267 SDValue RHS = Op.getOperand(1);
3268 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3269 Known = Known.intersectWith(Known2);
3270 }
3271 break;
3272 }
3273 case ISD::VSCALE: {
3275 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3276 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3277 break;
3278 }
3279 case ISD::CONCAT_VECTORS: {
3280 if (Op.getValueType().isScalableVector())
3281 break;
3282 // Split DemandedElts and test each of the demanded subvectors.
3283 Known.Zero.setAllBits(); Known.One.setAllBits();
3284 EVT SubVectorVT = Op.getOperand(0).getValueType();
3285 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3286 unsigned NumSubVectors = Op.getNumOperands();
3287 for (unsigned i = 0; i != NumSubVectors; ++i) {
3288 APInt DemandedSub =
3289 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3290 if (!!DemandedSub) {
3291 SDValue Sub = Op.getOperand(i);
3292 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3293 Known = Known.intersectWith(Known2);
3294 }
3295 // If we don't know any bits, early out.
3296 if (Known.isUnknown())
3297 break;
3298 }
3299 break;
3300 }
3301 case ISD::INSERT_SUBVECTOR: {
3302 if (Op.getValueType().isScalableVector())
3303 break;
3304 // Demand any elements from the subvector and the remainder from the src its
3305 // inserted into.
3306 SDValue Src = Op.getOperand(0);
3307 SDValue Sub = Op.getOperand(1);
3308 uint64_t Idx = Op.getConstantOperandVal(2);
3309 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3310 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3311 APInt DemandedSrcElts = DemandedElts;
3312 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3313
3314 Known.One.setAllBits();
3315 Known.Zero.setAllBits();
3316 if (!!DemandedSubElts) {
3317 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3318 if (Known.isUnknown())
3319 break; // early-out.
3320 }
3321 if (!!DemandedSrcElts) {
3322 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3323 Known = Known.intersectWith(Known2);
3324 }
3325 break;
3326 }
3328 // Offset the demanded elts by the subvector index.
3329 SDValue Src = Op.getOperand(0);
3330 // Bail until we can represent demanded elements for scalable vectors.
3331 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3332 break;
3333 uint64_t Idx = Op.getConstantOperandVal(1);
3334 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3335 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3336 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3337 break;
3338 }
3339 case ISD::SCALAR_TO_VECTOR: {
3340 if (Op.getValueType().isScalableVector())
3341 break;
3342 // We know about scalar_to_vector as much as we know about it source,
3343 // which becomes the first element of otherwise unknown vector.
3344 if (DemandedElts != 1)
3345 break;
3346
3347 SDValue N0 = Op.getOperand(0);
3348 Known = computeKnownBits(N0, Depth + 1);
3349 if (N0.getValueSizeInBits() != BitWidth)
3350 Known = Known.trunc(BitWidth);
3351
3352 break;
3353 }
3354 case ISD::BITCAST: {
3355 if (Op.getValueType().isScalableVector())
3356 break;
3357
3358 SDValue N0 = Op.getOperand(0);
3359 EVT SubVT = N0.getValueType();
3360 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3361
3362 // Ignore bitcasts from unsupported types.
3363 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3364 break;
3365
3366 // Fast handling of 'identity' bitcasts.
3367 if (BitWidth == SubBitWidth) {
3368 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3369 break;
3370 }
3371
3372 bool IsLE = getDataLayout().isLittleEndian();
3373
3374 // Bitcast 'small element' vector to 'large element' scalar/vector.
3375 if ((BitWidth % SubBitWidth) == 0) {
3376 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3377
3378 // Collect known bits for the (larger) output by collecting the known
3379 // bits from each set of sub elements and shift these into place.
3380 // We need to separately call computeKnownBits for each set of
3381 // sub elements as the knownbits for each is likely to be different.
3382 unsigned SubScale = BitWidth / SubBitWidth;
3383 APInt SubDemandedElts(NumElts * SubScale, 0);
3384 for (unsigned i = 0; i != NumElts; ++i)
3385 if (DemandedElts[i])
3386 SubDemandedElts.setBit(i * SubScale);
3387
3388 for (unsigned i = 0; i != SubScale; ++i) {
3389 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3390 Depth + 1);
3391 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3392 Known.insertBits(Known2, SubBitWidth * Shifts);
3393 }
3394 }
3395
3396 // Bitcast 'large element' scalar/vector to 'small element' vector.
3397 if ((SubBitWidth % BitWidth) == 0) {
3398 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3399
3400 // Collect known bits for the (smaller) output by collecting the known
3401 // bits from the overlapping larger input elements and extracting the
3402 // sub sections we actually care about.
3403 unsigned SubScale = SubBitWidth / BitWidth;
3404 APInt SubDemandedElts =
3405 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3406 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3407
3408 Known.Zero.setAllBits(); Known.One.setAllBits();
3409 for (unsigned i = 0; i != NumElts; ++i)
3410 if (DemandedElts[i]) {
3411 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3412 unsigned Offset = (Shifts % SubScale) * BitWidth;
3413 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3414 // If we don't know any bits, early out.
3415 if (Known.isUnknown())
3416 break;
3417 }
3418 }
3419 break;
3420 }
3421 case ISD::AND:
3422 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3423 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3424
3425 Known &= Known2;
3426 break;
3427 case ISD::OR:
3428 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3429 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3430
3431 Known |= Known2;
3432 break;
3433 case ISD::XOR:
3434 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3435 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3436
3437 Known ^= Known2;
3438 break;
3439 case ISD::MUL: {
3440 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3441 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3442 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3443 // TODO: SelfMultiply can be poison, but not undef.
3444 if (SelfMultiply)
3445 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3446 Op.getOperand(0), DemandedElts, false, Depth + 1);
3447 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3448
3449 // If the multiplication is known not to overflow, the product of a number
3450 // with itself is non-negative. Only do this if we didn't already computed
3451 // the opposite value for the sign bit.
3452 if (Op->getFlags().hasNoSignedWrap() &&
3453 Op.getOperand(0) == Op.getOperand(1) &&
3454 !Known.isNegative())
3455 Known.makeNonNegative();
3456 break;
3457 }
3458 case ISD::MULHU: {
3459 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3460 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3461 Known = KnownBits::mulhu(Known, Known2);
3462 break;
3463 }
3464 case ISD::MULHS: {
3465 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3466 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3467 Known = KnownBits::mulhs(Known, Known2);
3468 break;
3469 }
3470 case ISD::ABDU: {
3471 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3472 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3473 Known = KnownBits::abdu(Known, Known2);
3474 break;
3475 }
3476 case ISD::ABDS: {
3477 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3478 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3479 Known = KnownBits::abds(Known, Known2);
3480 unsigned SignBits1 =
3481 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3482 if (SignBits1 == 1)
3483 break;
3484 unsigned SignBits0 =
3485 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3486 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3487 break;
3488 }
3489 case ISD::UMUL_LOHI: {
3490 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3491 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3492 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3493 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3494 if (Op.getResNo() == 0)
3495 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3496 else
3497 Known = KnownBits::mulhu(Known, Known2);
3498 break;
3499 }
3500 case ISD::SMUL_LOHI: {
3501 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3502 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3503 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3504 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3505 if (Op.getResNo() == 0)
3506 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3507 else
3508 Known = KnownBits::mulhs(Known, Known2);
3509 break;
3510 }
3511 case ISD::AVGFLOORU: {
3512 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3513 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3514 Known = KnownBits::avgFloorU(Known, Known2);
3515 break;
3516 }
3517 case ISD::AVGCEILU: {
3518 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3519 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3520 Known = KnownBits::avgCeilU(Known, Known2);
3521 break;
3522 }
3523 case ISD::AVGFLOORS: {
3524 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3525 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3526 Known = KnownBits::avgFloorS(Known, Known2);
3527 break;
3528 }
3529 case ISD::AVGCEILS: {
3530 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3531 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3532 Known = KnownBits::avgCeilS(Known, Known2);
3533 break;
3534 }
3535 case ISD::SELECT:
3536 case ISD::VSELECT:
3537 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3538 // If we don't know any bits, early out.
3539 if (Known.isUnknown())
3540 break;
3541 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3542
3543 // Only known if known in both the LHS and RHS.
3544 Known = Known.intersectWith(Known2);
3545 break;
3546 case ISD::SELECT_CC:
3547 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3548 // If we don't know any bits, early out.
3549 if (Known.isUnknown())
3550 break;
3551 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3552
3553 // Only known if known in both the LHS and RHS.
3554 Known = Known.intersectWith(Known2);
3555 break;
3556 case ISD::SMULO:
3557 case ISD::UMULO:
3558 if (Op.getResNo() != 1)
3559 break;
3560 // The boolean result conforms to getBooleanContents.
3561 // If we know the result of a setcc has the top bits zero, use this info.
3562 // We know that we have an integer-based boolean since these operations
3563 // are only available for integer.
3564 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3566 BitWidth > 1)
3567 Known.Zero.setBitsFrom(1);
3568 break;
3569 case ISD::SETCC:
3570 case ISD::SETCCCARRY:
3571 case ISD::STRICT_FSETCC:
3572 case ISD::STRICT_FSETCCS: {
3573 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3574 // If we know the result of a setcc has the top bits zero, use this info.
3575 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3577 BitWidth > 1)
3578 Known.Zero.setBitsFrom(1);
3579 break;
3580 }
3581 case ISD::SHL: {
3582 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3583 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3584
3585 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3586 bool NSW = Op->getFlags().hasNoSignedWrap();
3587
3588 bool ShAmtNonZero = Known2.isNonZero();
3589
3590 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3591
3592 // Minimum shift low bits are known zero.
3593 if (std::optional<uint64_t> ShMinAmt =
3594 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3595 Known.Zero.setLowBits(*ShMinAmt);
3596 break;
3597 }
3598 case ISD::SRL:
3599 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3600 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3601 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3602 Op->getFlags().hasExact());
3603
3604 // Minimum shift high bits are known zero.
3605 if (std::optional<uint64_t> ShMinAmt =
3606 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3607 Known.Zero.setHighBits(*ShMinAmt);
3608 break;
3609 case ISD::SRA:
3610 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3611 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3612 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3613 Op->getFlags().hasExact());
3614 break;
3615 case ISD::FSHL:
3616 case ISD::FSHR:
3617 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3618 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3619
3620 // For fshl, 0-shift returns the 1st arg.
3621 // For fshr, 0-shift returns the 2nd arg.
3622 if (Amt == 0) {
3623 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3624 DemandedElts, Depth + 1);
3625 break;
3626 }
3627
3628 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3629 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3630 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3631 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3632 if (Opcode == ISD::FSHL) {
3633 Known.One <<= Amt;
3634 Known.Zero <<= Amt;
3635 Known2.One.lshrInPlace(BitWidth - Amt);
3636 Known2.Zero.lshrInPlace(BitWidth - Amt);
3637 } else {
3638 Known.One <<= BitWidth - Amt;
3639 Known.Zero <<= BitWidth - Amt;
3640 Known2.One.lshrInPlace(Amt);
3641 Known2.Zero.lshrInPlace(Amt);
3642 }
3643 Known = Known.unionWith(Known2);
3644 }
3645 break;
3646 case ISD::SHL_PARTS:
3647 case ISD::SRA_PARTS:
3648 case ISD::SRL_PARTS: {
3649 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3650
3651 // Collect lo/hi source values and concatenate.
3652 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3653 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3654 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3655 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3656 Known = Known2.concat(Known);
3657
3658 // Collect shift amount.
3659 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3660
3661 if (Opcode == ISD::SHL_PARTS)
3662 Known = KnownBits::shl(Known, Known2);
3663 else if (Opcode == ISD::SRA_PARTS)
3664 Known = KnownBits::ashr(Known, Known2);
3665 else // if (Opcode == ISD::SRL_PARTS)
3666 Known = KnownBits::lshr(Known, Known2);
3667
3668 // TODO: Minimum shift low/high bits are known zero.
3669
3670 if (Op.getResNo() == 0)
3671 Known = Known.extractBits(LoBits, 0);
3672 else
3673 Known = Known.extractBits(HiBits, LoBits);
3674 break;
3675 }
3677 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3678 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3679 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3680 break;
3681 }
3682 case ISD::CTTZ:
3683 case ISD::CTTZ_ZERO_UNDEF: {
3684 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3685 // If we have a known 1, its position is our upper bound.
3686 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3687 unsigned LowBits = llvm::bit_width(PossibleTZ);
3688 Known.Zero.setBitsFrom(LowBits);
3689 break;
3690 }
3691 case ISD::CTLZ:
3692 case ISD::CTLZ_ZERO_UNDEF: {
3693 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3694 // If we have a known 1, its position is our upper bound.
3695 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3696 unsigned LowBits = llvm::bit_width(PossibleLZ);
3697 Known.Zero.setBitsFrom(LowBits);
3698 break;
3699 }
3700 case ISD::CTPOP: {
3701 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3702 // If we know some of the bits are zero, they can't be one.
3703 unsigned PossibleOnes = Known2.countMaxPopulation();
3704 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3705 break;
3706 }
3707 case ISD::PARITY: {
3708 // Parity returns 0 everywhere but the LSB.
3709 Known.Zero.setBitsFrom(1);
3710 break;
3711 }
3712 case ISD::LOAD: {
3713 LoadSDNode *LD = cast<LoadSDNode>(Op);
3714 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3715 if (ISD::isNON_EXTLoad(LD) && Cst) {
3716 // Determine any common known bits from the loaded constant pool value.
3717 Type *CstTy = Cst->getType();
3718 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3719 !Op.getValueType().isScalableVector()) {
3720 // If its a vector splat, then we can (quickly) reuse the scalar path.
3721 // NOTE: We assume all elements match and none are UNDEF.
3722 if (CstTy->isVectorTy()) {
3723 if (const Constant *Splat = Cst->getSplatValue()) {
3724 Cst = Splat;
3725 CstTy = Cst->getType();
3726 }
3727 }
3728 // TODO - do we need to handle different bitwidths?
3729 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3730 // Iterate across all vector elements finding common known bits.
3731 Known.One.setAllBits();
3732 Known.Zero.setAllBits();
3733 for (unsigned i = 0; i != NumElts; ++i) {
3734 if (!DemandedElts[i])
3735 continue;
3736 if (Constant *Elt = Cst->getAggregateElement(i)) {
3737 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3738 const APInt &Value = CInt->getValue();
3739 Known.One &= Value;
3740 Known.Zero &= ~Value;
3741 continue;
3742 }
3743 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3744 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3745 Known.One &= Value;
3746 Known.Zero &= ~Value;
3747 continue;
3748 }
3749 }
3750 Known.One.clearAllBits();
3751 Known.Zero.clearAllBits();
3752 break;
3753 }
3754 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3755 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3756 Known = KnownBits::makeConstant(CInt->getValue());
3757 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3758 Known =
3759 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3760 }
3761 }
3762 }
3763 } else if (Op.getResNo() == 0) {
3764 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3765 ? LD->getMemoryVT().getFixedSizeInBits()
3766 : BitWidth);
3767 EVT VT = Op.getValueType();
3768 // Fill in any known bits from range information. There are 3 types being
3769 // used. The results VT (same vector elt size as BitWidth), the loaded
3770 // MemoryVT (which may or may not be vector) and the range VTs original
3771 // type. The range matadata needs the full range (i.e
3772 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3773 // if it is know. These are then extended to the original VT sizes below.
3774 if (const MDNode *MD = LD->getRanges()) {
3776 if (VT.isVector()) {
3777 // Handle truncation to the first demanded element.
3778 // TODO: Figure out which demanded elements are covered
3779 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
3780 break;
3781 Known0 = Known0.trunc(BitWidth);
3782 }
3783 }
3784
3785 if (LD->getMemoryVT().isVector())
3786 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
3787
3788 // Extend the Known bits from memory to the size of the result.
3789 if (ISD::isZEXTLoad(Op.getNode()))
3790 Known = Known0.zext(BitWidth);
3791 else if (ISD::isSEXTLoad(Op.getNode()))
3792 Known = Known0.sext(BitWidth);
3793 else if (ISD::isEXTLoad(Op.getNode()))
3794 Known = Known0.anyext(BitWidth);
3795 else
3796 Known = Known0;
3797 assert(Known.getBitWidth() == BitWidth);
3798 return Known;
3799 }
3800 break;
3801 }
3803 if (Op.getValueType().isScalableVector())
3804 break;
3805 EVT InVT = Op.getOperand(0).getValueType();
3806 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3807 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3808 Known = Known.zext(BitWidth);
3809 break;
3810 }
3811 case ISD::ZERO_EXTEND: {
3812 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3813 Known = Known.zext(BitWidth);
3814 break;
3815 }
3817 if (Op.getValueType().isScalableVector())
3818 break;
3819 EVT InVT = Op.getOperand(0).getValueType();
3820 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3821 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3822 // If the sign bit is known to be zero or one, then sext will extend
3823 // it to the top bits, else it will just zext.
3824 Known = Known.sext(BitWidth);
3825 break;
3826 }
3827 case ISD::SIGN_EXTEND: {
3828 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3829 // If the sign bit is known to be zero or one, then sext will extend
3830 // it to the top bits, else it will just zext.
3831 Known = Known.sext(BitWidth);
3832 break;
3833 }
3835 if (Op.getValueType().isScalableVector())
3836 break;
3837 EVT InVT = Op.getOperand(0).getValueType();
3838 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3839 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3840 Known = Known.anyext(BitWidth);
3841 break;
3842 }
3843 case ISD::ANY_EXTEND: {
3844 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3845 Known = Known.anyext(BitWidth);
3846 break;
3847 }
3848 case ISD::TRUNCATE: {
3849 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3850 Known = Known.trunc(BitWidth);
3851 break;
3852 }
3853 case ISD::AssertZext: {
3854 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3856 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3857 Known.Zero |= (~InMask);
3858 Known.One &= (~Known.Zero);
3859 break;
3860 }
3861 case ISD::AssertAlign: {
3862 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3863 assert(LogOfAlign != 0);
3864
3865 // TODO: Should use maximum with source
3866 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3867 // well as clearing one bits.
3868 Known.Zero.setLowBits(LogOfAlign);
3869 Known.One.clearLowBits(LogOfAlign);
3870 break;
3871 }
3872 case ISD::FGETSIGN:
3873 // All bits are zero except the low bit.
3874 Known.Zero.setBitsFrom(1);
3875 break;
3876 case ISD::ADD:
3877 case ISD::SUB: {
3878 SDNodeFlags Flags = Op.getNode()->getFlags();
3879 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3880 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3882 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3883 Flags.hasNoUnsignedWrap(), Known, Known2);
3884 break;
3885 }
3886 case ISD::USUBO:
3887 case ISD::SSUBO:
3888 case ISD::USUBO_CARRY:
3889 case ISD::SSUBO_CARRY:
3890 if (Op.getResNo() == 1) {
3891 // If we know the result of a setcc has the top bits zero, use this info.
3892 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3894 BitWidth > 1)
3895 Known.Zero.setBitsFrom(1);
3896 break;
3897 }
3898 [[fallthrough]];
3899 case ISD::SUBC: {
3900 assert(Op.getResNo() == 0 &&
3901 "We only compute knownbits for the difference here.");
3902
3903 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
3904 KnownBits Borrow(1);
3905 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3906 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3907 // Borrow has bit width 1
3908 Borrow = Borrow.trunc(1);
3909 } else {
3910 Borrow.setAllZero();
3911 }
3912
3913 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3914 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3915 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
3916 break;
3917 }
3918 case ISD::UADDO:
3919 case ISD::SADDO:
3920 case ISD::UADDO_CARRY:
3921 case ISD::SADDO_CARRY:
3922 if (Op.getResNo() == 1) {
3923 // If we know the result of a setcc has the top bits zero, use this info.
3924 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3926 BitWidth > 1)
3927 Known.Zero.setBitsFrom(1);
3928 break;
3929 }
3930 [[fallthrough]];
3931 case ISD::ADDC:
3932 case ISD::ADDE: {
3933 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3934
3935 // With ADDE and UADDO_CARRY, a carry bit may be added in.
3936 KnownBits Carry(1);
3937 if (Opcode == ISD::ADDE)
3938 // Can't track carry from glue, set carry to unknown.
3939 Carry.resetAll();
3940 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3941 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3942 // Carry has bit width 1
3943 Carry = Carry.trunc(1);
3944 } else {
3945 Carry.setAllZero();
3946 }
3947
3948 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3949 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3950 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3951 break;
3952 }
3953 case ISD::UDIV: {
3954 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3955 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3956 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
3957 break;
3958 }
3959 case ISD::SDIV: {
3960 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3961 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3962 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
3963 break;
3964 }
3965 case ISD::SREM: {
3966 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3967 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3968 Known = KnownBits::srem(Known, Known2);
3969 break;
3970 }
3971 case ISD::UREM: {
3972 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3973 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3974 Known = KnownBits::urem(Known, Known2);
3975 break;
3976 }
3977 case ISD::EXTRACT_ELEMENT: {
3978 Known = computeKnownBits(Op.getOperand(0), Depth+1);
3979 const unsigned Index = Op.getConstantOperandVal(1);
3980 const unsigned EltBitWidth = Op.getValueSizeInBits();
3981
3982 // Remove low part of known bits mask
3983 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3984 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3985
3986 // Remove high part of known bit mask
3987 Known = Known.trunc(EltBitWidth);
3988 break;
3989 }
3991 SDValue InVec = Op.getOperand(0);
3992 SDValue EltNo = Op.getOperand(1);
3993 EVT VecVT = InVec.getValueType();
3994 // computeKnownBits not yet implemented for scalable vectors.
3995 if (VecVT.isScalableVector())
3996 break;
3997 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
3998 const unsigned NumSrcElts = VecVT.getVectorNumElements();
3999
4000 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4001 // anything about the extended bits.
4002 if (BitWidth > EltBitWidth)
4003 Known = Known.trunc(EltBitWidth);
4004
4005 // If we know the element index, just demand that vector element, else for
4006 // an unknown element index, ignore DemandedElts and demand them all.
4007 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4008 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4009 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4010 DemandedSrcElts =
4011 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4012
4013 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4014 if (BitWidth > EltBitWidth)
4015 Known = Known.anyext(BitWidth);
4016 break;
4017 }
4019 if (Op.getValueType().isScalableVector())
4020 break;
4021
4022 // If we know the element index, split the demand between the
4023 // source vector and the inserted element, otherwise assume we need
4024 // the original demanded vector elements and the value.
4025 SDValue InVec = Op.getOperand(0);
4026 SDValue InVal = Op.getOperand(1);
4027 SDValue EltNo = Op.getOperand(2);
4028 bool DemandedVal = true;
4029 APInt DemandedVecElts = DemandedElts;
4030 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4031 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4032 unsigned EltIdx = CEltNo->getZExtValue();
4033 DemandedVal = !!DemandedElts[EltIdx];
4034 DemandedVecElts.clearBit(EltIdx);
4035 }
4036 Known.One.setAllBits();
4037 Known.Zero.setAllBits();
4038 if (DemandedVal) {
4039 Known2 = computeKnownBits(InVal, Depth + 1);
4040 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4041 }
4042 if (!!DemandedVecElts) {
4043 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4044 Known = Known.intersectWith(Known2);
4045 }
4046 break;
4047 }
4048 case ISD::BITREVERSE: {
4049 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4050 Known = Known2.reverseBits();
4051 break;
4052 }
4053 case ISD::BSWAP: {
4054 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4055 Known = Known2.byteSwap();
4056 break;
4057 }
4058 case ISD::ABS: {
4059 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4060 Known = Known2.abs();
4061 Known.Zero.setHighBits(
4062 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4063 break;
4064 }
4065 case ISD::USUBSAT: {
4066 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4067 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4068 Known = KnownBits::usub_sat(Known, Known2);
4069 break;
4070 }
4071 case ISD::UMIN: {
4072 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4073 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4074 Known = KnownBits::umin(Known, Known2);
4075 break;
4076 }
4077 case ISD::UMAX: {
4078 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4079 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4080 Known = KnownBits::umax(Known, Known2);
4081 break;
4082 }
4083 case ISD::SMIN:
4084 case ISD::SMAX: {
4085 // If we have a clamp pattern, we know that the number of sign bits will be
4086 // the minimum of the clamp min/max range.
4087 bool IsMax = (Opcode == ISD::SMAX);
4088 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4089 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4090 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4091 CstHigh =
4092 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4093 if (CstLow && CstHigh) {
4094 if (!IsMax)
4095 std::swap(CstLow, CstHigh);
4096
4097 const APInt &ValueLow = CstLow->getAPIntValue();
4098 const APInt &ValueHigh = CstHigh->getAPIntValue();
4099 if (ValueLow.sle(ValueHigh)) {
4100 unsigned LowSignBits = ValueLow.getNumSignBits();
4101 unsigned HighSignBits = ValueHigh.getNumSignBits();
4102 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4103 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4104 Known.One.setHighBits(MinSignBits);
4105 break;
4106 }
4107 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4108 Known.Zero.setHighBits(MinSignBits);
4109 break;
4110 }
4111 }
4112 }
4113
4114 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4115 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4116 if (IsMax)
4117 Known = KnownBits::smax(Known, Known2);
4118 else
4119 Known = KnownBits::smin(Known, Known2);
4120
4121 // For SMAX, if CstLow is non-negative we know the result will be
4122 // non-negative and thus all sign bits are 0.
4123 // TODO: There's an equivalent of this for smin with negative constant for
4124 // known ones.
4125 if (IsMax && CstLow) {
4126 const APInt &ValueLow = CstLow->getAPIntValue();
4127 if (ValueLow.isNonNegative()) {
4128 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4129 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4130 }
4131 }
4132
4133 break;
4134 }
4135 case ISD::UINT_TO_FP: {
4136 Known.makeNonNegative();
4137 break;
4138 }
4139 case ISD::SINT_TO_FP: {
4140 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4141 if (Known2.isNonNegative())
4142 Known.makeNonNegative();
4143 else if (Known2.isNegative())
4144 Known.makeNegative();
4145 break;
4146 }
4147 case ISD::FP_TO_UINT_SAT: {
4148 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4149 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4151 break;
4152 }
4154 if (Op.getResNo() == 1) {
4155 // The boolean result conforms to getBooleanContents.
4156 // If we know the result of a setcc has the top bits zero, use this info.
4157 // We know that we have an integer-based boolean since these operations
4158 // are only available for integer.
4159 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4161 BitWidth > 1)
4162 Known.Zero.setBitsFrom(1);
4163 break;
4164 }
4165 [[fallthrough]];
4167 case ISD::ATOMIC_SWAP:
4179 case ISD::ATOMIC_LOAD: {
4180 unsigned MemBits =
4181 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4182 // If we are looking at the loaded value.
4183 if (Op.getResNo() == 0) {
4185 Known.Zero.setBitsFrom(MemBits);
4186 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4187 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4188 Known.Zero.setBitsFrom(MemBits);
4189 }
4190 break;
4191 }
4192 case ISD::FrameIndex:
4194 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4195 Known, getMachineFunction());
4196 break;
4197
4198 default:
4199 if (Opcode < ISD::BUILTIN_OP_END)
4200 break;
4201 [[fallthrough]];
4205 // TODO: Probably okay to remove after audit; here to reduce change size
4206 // in initial enablement patch for scalable vectors
4207 if (Op.getValueType().isScalableVector())
4208 break;
4209
4210 // Allow the target to implement this method for its nodes.
4211 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4212 break;
4213 }
4214
4215 return Known;
4216}
4217
4218/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4220 switch (OR) {
4228 }
4229 llvm_unreachable("Unknown OverflowResult");
4230}
4231
4234 // X + 0 never overflow
4235 if (isNullConstant(N1))
4236 return OFK_Never;
4237
4238 // If both operands each have at least two sign bits, the addition
4239 // cannot overflow.
4240 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4241 return OFK_Never;
4242
4243 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4244 return OFK_Sometime;
4245}
4246
4249 // X + 0 never overflow
4250 if (isNullConstant(N1))
4251 return OFK_Never;
4252
4253 // mulhi + 1 never overflow
4254 KnownBits N1Known = computeKnownBits(N1);
4255 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4256 N1Known.getMaxValue().ult(2))
4257 return OFK_Never;
4258
4259 KnownBits N0Known = computeKnownBits(N0);
4260 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4261 N0Known.getMaxValue().ult(2))
4262 return OFK_Never;
4263
4264 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4265 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4266 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4267 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4268}
4269
4272 // X - 0 never overflow
4273 if (isNullConstant(N1))
4274 return OFK_Never;
4275
4276 // If both operands each have at least two sign bits, the subtraction
4277 // cannot overflow.
4278 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4279 return OFK_Never;
4280
4281 KnownBits N0Known = computeKnownBits(N0);
4282 KnownBits N1Known = computeKnownBits(N1);
4283 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4284 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4285 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4286}
4287
4290 // X - 0 never overflow
4291 if (isNullConstant(N1))
4292 return OFK_Never;
4293
4294 KnownBits N0Known = computeKnownBits(N0);
4295 KnownBits N1Known = computeKnownBits(N1);
4296 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4297 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4298 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4299}
4300
4303 // X * 0 and X * 1 never overflow.
4304 if (isNullConstant(N1) || isOneConstant(N1))
4305 return OFK_Never;
4306
4307 KnownBits N0Known = computeKnownBits(N0);
4308 KnownBits N1Known = computeKnownBits(N1);
4309 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4310 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4311 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4312}
4313
4316 // X * 0 and X * 1 never overflow.
4317 if (isNullConstant(N1) || isOneConstant(N1))
4318 return OFK_Never;
4319
4320 // Get the size of the result.
4321 unsigned BitWidth = N0.getScalarValueSizeInBits();
4322
4323 // Sum of the sign bits.
4324 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4325
4326 // If we have enough sign bits, then there's no overflow.
4327 if (SignBits > BitWidth + 1)
4328 return OFK_Never;
4329
4330 if (SignBits == BitWidth + 1) {
4331 // The overflow occurs when the true multiplication of the
4332 // the operands is the minimum negative number.
4333 KnownBits N0Known = computeKnownBits(N0);
4334 KnownBits N1Known = computeKnownBits(N1);
4335 // If one of the operands is non-negative, then there's no
4336 // overflow.
4337 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4338 return OFK_Never;
4339 }
4340
4341 return OFK_Sometime;
4342}
4343
4345 if (Depth >= MaxRecursionDepth)
4346 return false; // Limit search depth.
4347
4348 EVT OpVT = Val.getValueType();
4349 unsigned BitWidth = OpVT.getScalarSizeInBits();
4350
4351 // Is the constant a known power of 2?
4353 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4354 }))
4355 return true;
4356
4357 // A left-shift of a constant one will have exactly one bit set because
4358 // shifting the bit off the end is undefined.
4359 if (Val.getOpcode() == ISD::SHL) {
4360 auto *C = isConstOrConstSplat(Val.getOperand(0));
4361 if (C && C->getAPIntValue() == 1)
4362 return true;
4363 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4364 isKnownNeverZero(Val, Depth);
4365 }
4366
4367 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4368 // one bit set.
4369 if (Val.getOpcode() == ISD::SRL) {
4370 auto *C = isConstOrConstSplat(Val.getOperand(0));
4371 if (C && C->getAPIntValue().isSignMask())
4372 return true;
4373 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4374 isKnownNeverZero(Val, Depth);
4375 }
4376
4377 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4378 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4379
4380 // Are all operands of a build vector constant powers of two?
4381 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4382 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4383 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4384 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4385 return false;
4386 }))
4387 return true;
4388
4389 // Is the operand of a splat vector a constant power of two?
4390 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4391 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4392 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4393 return true;
4394
4395 // vscale(power-of-two) is a power-of-two for some targets
4396 if (Val.getOpcode() == ISD::VSCALE &&
4397 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4399 return true;
4400
4401 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4402 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4403 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4405
4406 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4407 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4409
4410 // Looking for `x & -x` pattern:
4411 // If x == 0:
4412 // x & -x -> 0
4413 // If x != 0:
4414 // x & -x -> non-zero pow2
4415 // so if we find the pattern return whether we know `x` is non-zero.
4416 SDValue X;
4417 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4418 return isKnownNeverZero(X, Depth);
4419
4420 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4421 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4422
4423 // More could be done here, though the above checks are enough
4424 // to handle some common cases.
4425 return false;
4426}
4427
4429 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4430 return C1->getValueAPF().getExactLog2Abs() >= 0;
4431
4432 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4433 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4434
4435 return false;
4436}
4437
4439 EVT VT = Op.getValueType();
4440
4441 // Since the number of lanes in a scalable vector is unknown at compile time,
4442 // we track one bit which is implicitly broadcast to all lanes. This means
4443 // that all lanes in a scalable vector are considered demanded.
4444 APInt DemandedElts = VT.isFixedLengthVector()
4446 : APInt(1, 1);
4447 return ComputeNumSignBits(Op, DemandedElts, Depth);
4448}
4449
4450unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4451 unsigned Depth) const {
4452 EVT VT = Op.getValueType();
4453 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4454 unsigned VTBits = VT.getScalarSizeInBits();
4455 unsigned NumElts = DemandedElts.getBitWidth();
4456 unsigned Tmp, Tmp2;
4457 unsigned FirstAnswer = 1;
4458
4459 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4460 const APInt &Val = C->getAPIntValue();
4461 return Val.getNumSignBits();
4462 }
4463
4464 if (Depth >= MaxRecursionDepth)
4465 return 1; // Limit search depth.
4466
4467 if (!DemandedElts)
4468 return 1; // No demanded elts, better to assume we don't know anything.
4469
4470 unsigned Opcode = Op.getOpcode();
4471 switch (Opcode) {
4472 default: break;
4473 case ISD::AssertSext:
4474 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4475 return VTBits-Tmp+1;
4476 case ISD::AssertZext:
4477 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4478 return VTBits-Tmp;
4479 case ISD::MERGE_VALUES:
4480 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4481 Depth + 1);
4482 case ISD::SPLAT_VECTOR: {
4483 // Check if the sign bits of source go down as far as the truncated value.
4484 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4485 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4486 if (NumSrcSignBits > (NumSrcBits - VTBits))
4487 return NumSrcSignBits - (NumSrcBits - VTBits);
4488 break;
4489 }
4490 case ISD::BUILD_VECTOR:
4491 assert(!VT.isScalableVector());
4492 Tmp = VTBits;
4493 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4494 if (!DemandedElts[i])
4495 continue;
4496
4497 SDValue SrcOp = Op.getOperand(i);
4498 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4499 // for constant nodes to ensure we only look at the sign bits.
4500 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4501 APInt T = C->getAPIntValue().trunc(VTBits);
4502 Tmp2 = T.getNumSignBits();
4503 } else {
4504 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4505
4506 if (SrcOp.getValueSizeInBits() != VTBits) {
4507 assert(SrcOp.getValueSizeInBits() > VTBits &&
4508 "Expected BUILD_VECTOR implicit truncation");
4509 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4510 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4511 }
4512 }
4513 Tmp = std::min(Tmp, Tmp2);
4514 }
4515 return Tmp;
4516
4517 case ISD::VECTOR_SHUFFLE: {
4518 // Collect the minimum number of sign bits that are shared by every vector
4519 // element referenced by the shuffle.
4520 APInt DemandedLHS, DemandedRHS;
4521 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4522 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4523 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4524 DemandedLHS, DemandedRHS))
4525 return 1;
4526
4527 Tmp = std::numeric_limits<unsigned>::max();
4528 if (!!DemandedLHS)
4529 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4530 if (!!DemandedRHS) {
4531 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4532 Tmp = std::min(Tmp, Tmp2);
4533 }
4534 // If we don't know anything, early out and try computeKnownBits fall-back.
4535 if (Tmp == 1)
4536 break;
4537 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4538 return Tmp;
4539 }
4540
4541 case ISD::BITCAST: {
4542 if (VT.isScalableVector())
4543 break;
4544 SDValue N0 = Op.getOperand(0);
4545 EVT SrcVT = N0.getValueType();
4546 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4547
4548 // Ignore bitcasts from unsupported types..
4549 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4550 break;
4551
4552 // Fast handling of 'identity' bitcasts.
4553 if (VTBits == SrcBits)
4554 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4555
4556 bool IsLE = getDataLayout().isLittleEndian();
4557
4558 // Bitcast 'large element' scalar/vector to 'small element' vector.
4559 if ((SrcBits % VTBits) == 0) {
4560 assert(VT.isVector() && "Expected bitcast to vector");
4561
4562 unsigned Scale = SrcBits / VTBits;
4563 APInt SrcDemandedElts =
4564 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4565
4566 // Fast case - sign splat can be simply split across the small elements.
4567 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4568 if (Tmp == SrcBits)
4569 return VTBits;
4570
4571 // Slow case - determine how far the sign extends into each sub-element.
4572 Tmp2 = VTBits;
4573 for (unsigned i = 0; i != NumElts; ++i)
4574 if (DemandedElts[i]) {
4575 unsigned SubOffset = i % Scale;
4576 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4577 SubOffset = SubOffset * VTBits;
4578 if (Tmp <= SubOffset)
4579 return 1;
4580 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4581 }
4582 return Tmp2;
4583 }
4584 break;
4585 }
4586
4588 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4589 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4590 return VTBits - Tmp + 1;
4591 case ISD::SIGN_EXTEND:
4592 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4593 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4595 // Max of the input and what this extends.
4596 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4597 Tmp = VTBits-Tmp+1;
4598 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4599 return std::max(Tmp, Tmp2);
4601 if (VT.isScalableVector())
4602 break;
4603 SDValue Src = Op.getOperand(0);
4604 EVT SrcVT = Src.getValueType();
4605 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4606 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4607 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4608 }
4609 case ISD::SRA:
4610 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4611 // SRA X, C -> adds C sign bits.
4612 if (std::optional<uint64_t> ShAmt =
4613 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4614 Tmp = std::min<uint64_t>(Tmp + *ShAmt, VTBits);
4615 return Tmp;
4616 case ISD::SHL:
4617 if (std::optional<uint64_t> ShAmt =
4618 getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1)) {
4619 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4620 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4621 if (*ShAmt < Tmp)
4622 return Tmp - *ShAmt;
4623 }
4624 break;
4625 case ISD::AND:
4626 case ISD::OR:
4627 case ISD::XOR: // NOT is handled here.
4628 // Logical binary ops preserve the number of sign bits at the worst.
4629 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4630 if (Tmp != 1) {
4631 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4632 FirstAnswer = std::min(Tmp, Tmp2);
4633 // We computed what we know about the sign bits as our first
4634 // answer. Now proceed to the generic code that uses
4635 // computeKnownBits, and pick whichever answer is better.
4636 }
4637 break;
4638
4639 case ISD::SELECT:
4640 case ISD::VSELECT:
4641 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4642 if (Tmp == 1) return 1; // Early out.
4643 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4644 return std::min(Tmp, Tmp2);
4645 case ISD::SELECT_CC:
4646 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4647 if (Tmp == 1) return 1; // Early out.
4648 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4649 return std::min(Tmp, Tmp2);
4650
4651 case ISD::SMIN:
4652 case ISD::SMAX: {
4653 // If we have a clamp pattern, we know that the number of sign bits will be
4654 // the minimum of the clamp min/max range.
4655 bool IsMax = (Opcode == ISD::SMAX);
4656 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4657 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4658 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4659 CstHigh =
4660 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4661 if (CstLow && CstHigh) {
4662 if (!IsMax)
4663 std::swap(CstLow, CstHigh);
4664 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4665 Tmp = CstLow->getAPIntValue().getNumSignBits();
4666 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4667 return std::min(Tmp, Tmp2);
4668 }
4669 }
4670
4671 // Fallback - just get the minimum number of sign bits of the operands.
4672 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4673 if (Tmp == 1)
4674 return 1; // Early out.
4675 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4676 return std::min(Tmp, Tmp2);
4677 }
4678 case ISD::UMIN:
4679 case ISD::UMAX:
4680 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4681 if (Tmp == 1)
4682 return 1; // Early out.
4683 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4684 return std::min(Tmp, Tmp2);
4685 case ISD::SADDO:
4686 case ISD::UADDO:
4687 case ISD::SADDO_CARRY:
4688 case ISD::UADDO_CARRY:
4689 case ISD::SSUBO:
4690 case ISD::USUBO:
4691 case ISD::SSUBO_CARRY:
4692 case ISD::USUBO_CARRY:
4693 case ISD::SMULO:
4694 case ISD::UMULO:
4695 if (Op.getResNo() != 1)
4696 break;
4697 // The boolean result conforms to getBooleanContents. Fall through.
4698 // If setcc returns 0/-1, all bits are sign bits.
4699 // We know that we have an integer-based boolean since these operations
4700 // are only available for integer.
4701 if (TLI->getBooleanContents(VT.isVector(), false) ==
4703 return VTBits;
4704 break;
4705 case ISD::SETCC:
4706 case ISD::SETCCCARRY:
4707 case ISD::STRICT_FSETCC:
4708 case ISD::STRICT_FSETCCS: {
4709 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4710 // If setcc returns 0/-1, all bits are sign bits.
4711 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4713 return VTBits;
4714 break;
4715 }
4716 case ISD::ROTL:
4717 case ISD::ROTR:
4718 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4719
4720 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4721 if (Tmp == VTBits)
4722 return VTBits;
4723
4724 if (ConstantSDNode *C =
4725 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4726 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4727
4728 // Handle rotate right by N like a rotate left by 32-N.
4729 if (Opcode == ISD::ROTR)
4730 RotAmt = (VTBits - RotAmt) % VTBits;
4731
4732 // If we aren't rotating out all of the known-in sign bits, return the
4733 // number that are left. This handles rotl(sext(x), 1) for example.
4734 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4735 }
4736 break;
4737 case ISD::ADD:
4738 case ISD::ADDC:
4739 // Add can have at most one carry bit. Thus we know that the output
4740 // is, at worst, one more bit than the inputs.
4741 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4742 if (Tmp == 1) return 1; // Early out.
4743
4744 // Special case decrementing a value (ADD X, -1):
4745 if (ConstantSDNode *CRHS =
4746 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4747 if (CRHS->isAllOnes()) {
4748 KnownBits Known =
4749 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4750
4751 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4752 // sign bits set.
4753 if ((Known.Zero | 1).isAllOnes())
4754 return VTBits;
4755
4756 // If we are subtracting one from a positive number, there is no carry
4757 // out of the result.
4758 if (Known.isNonNegative())
4759 return Tmp;
4760 }
4761
4762 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4763 if (Tmp2 == 1) return 1; // Early out.
4764 return std::min(Tmp, Tmp2) - 1;
4765 case ISD::SUB:
4766 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4767 if (Tmp2 == 1) return 1; // Early out.
4768
4769 // Handle NEG.
4770 if (ConstantSDNode *CLHS =
4771 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4772 if (CLHS->isZero()) {
4773 KnownBits Known =
4774 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4775 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4776 // sign bits set.
4777 if ((Known.Zero | 1).isAllOnes())
4778 return VTBits;
4779
4780 // If the input is known to be positive (the sign bit is known clear),
4781 // the output of the NEG has the same number of sign bits as the input.
4782 if (Known.isNonNegative())
4783 return Tmp2;
4784
4785 // Otherwise, we treat this like a SUB.
4786 }
4787
4788 // Sub can have at most one carry bit. Thus we know that the output
4789 // is, at worst, one more bit than the inputs.
4790 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4791 if (Tmp == 1) return 1; // Early out.
4792 return std::min(Tmp, Tmp2) - 1;
4793 case ISD::MUL: {
4794 // The output of the Mul can be at most twice the valid bits in the inputs.
4795 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4796 if (SignBitsOp0 == 1)
4797 break;
4798 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
4799 if (SignBitsOp1 == 1)
4800 break;
4801 unsigned OutValidBits =
4802 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
4803 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
4804 }
4805 case ISD::AVGCEILS:
4806 case ISD::AVGFLOORS:
4807 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4808 if (Tmp == 1)
4809 return 1; // Early out.
4810 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4811 return std::min(Tmp, Tmp2);
4812 case ISD::SREM:
4813 // The sign bit is the LHS's sign bit, except when the result of the
4814 // remainder is zero. The magnitude of the result should be less than or
4815 // equal to the magnitude of the LHS. Therefore, the result should have
4816 // at least as many sign bits as the left hand side.
4817 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4818 case ISD::TRUNCATE: {
4819 // Check if the sign bits of source go down as far as the truncated value.
4820 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
4821 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4822 if (NumSrcSignBits > (NumSrcBits - VTBits))
4823 return NumSrcSignBits - (NumSrcBits - VTBits);
4824 break;
4825 }
4826 case ISD::EXTRACT_ELEMENT: {
4827 if (VT.isScalableVector())
4828 break;
4829 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
4830 const int BitWidth = Op.getValueSizeInBits();
4831 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
4832
4833 // Get reverse index (starting from 1), Op1 value indexes elements from
4834 // little end. Sign starts at big end.
4835 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
4836
4837 // If the sign portion ends in our element the subtraction gives correct
4838 // result. Otherwise it gives either negative or > bitwidth result
4839 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
4840 }
4842 if (VT.isScalableVector())
4843 break;
4844 // If we know the element index, split the demand between the
4845 // source vector and the inserted element, otherwise assume we need
4846 // the original demanded vector elements and the value.
4847 SDValue InVec = Op.getOperand(0);
4848 SDValue InVal = Op.getOperand(1);
4849 SDValue EltNo = Op.getOperand(2);
4850 bool DemandedVal = true;
4851 APInt DemandedVecElts = DemandedElts;
4852 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4853 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4854 unsigned EltIdx = CEltNo->getZExtValue();
4855 DemandedVal = !!DemandedElts[EltIdx];
4856 DemandedVecElts.clearBit(EltIdx);
4857 }
4858 Tmp = std::numeric_limits<unsigned>::max();
4859 if (DemandedVal) {
4860 // TODO - handle implicit truncation of inserted elements.
4861 if (InVal.getScalarValueSizeInBits() != VTBits)
4862 break;
4863 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
4864 Tmp = std::min(Tmp, Tmp2);
4865 }
4866 if (!!DemandedVecElts) {
4867 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
4868 Tmp = std::min(Tmp, Tmp2);
4869 }
4870 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4871 return Tmp;
4872 }
4874 assert(!VT.isScalableVector());
4875 SDValue InVec = Op.getOperand(0);
4876 SDValue EltNo = Op.getOperand(1);
4877 EVT VecVT = InVec.getValueType();
4878 // ComputeNumSignBits not yet implemented for scalable vectors.
4879 if (VecVT.isScalableVector())
4880 break;
4881 const unsigned BitWidth = Op.getValueSizeInBits();
4882 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
4883 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4884
4885 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
4886 // anything about sign bits. But if the sizes match we can derive knowledge
4887 // about sign bits from the vector operand.
4888 if (BitWidth != EltBitWidth)
4889 break;
4890
4891 // If we know the element index, just demand that vector element, else for
4892 // an unknown element index, ignore DemandedElts and demand them all.
4893 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4894 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4895 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4896 DemandedSrcElts =
4897 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4898
4899 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
4900 }
4902 // Offset the demanded elts by the subvector index.
4903 SDValue Src = Op.getOperand(0);
4904 // Bail until we can represent demanded elements for scalable vectors.
4905 if (Src.getValueType().isScalableVector())
4906 break;
4907 uint64_t Idx = Op.getConstantOperandVal(1);
4908 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
4909 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
4910 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4911 }
4912 case ISD::CONCAT_VECTORS: {
4913 if (VT.isScalableVector())
4914 break;
4915 // Determine the minimum number of sign bits across all demanded
4916 // elts of the input vectors. Early out if the result is already 1.
4917 Tmp = std::numeric_limits<unsigned>::max();
4918 EVT SubVectorVT = Op.getOperand(0).getValueType();
4919 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
4920 unsigned NumSubVectors = Op.getNumOperands();
4921 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
4922 APInt DemandedSub =
4923 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
4924 if (!DemandedSub)
4925 continue;
4926 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
4927 Tmp = std::min(Tmp, Tmp2);
4928 }
4929 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4930 return Tmp;
4931 }
4932 case ISD::INSERT_SUBVECTOR: {
4933 if (VT.isScalableVector())
4934 break;
4935 // Demand any elements from the subvector and the remainder from the src its
4936 // inserted into.
4937 SDValue Src = Op.getOperand(0);
4938 SDValue Sub = Op.getOperand(1);
4939 uint64_t Idx = Op.getConstantOperandVal(2);
4940 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
4941 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
4942 APInt DemandedSrcElts = DemandedElts;
4943 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
4944
4945 Tmp = std::numeric_limits<unsigned>::max();
4946 if (!!DemandedSubElts) {
4947 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
4948 if (Tmp == 1)
4949 return 1; // early-out
4950 }
4951 if (!!DemandedSrcElts) {
4952 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4953 Tmp = std::min(Tmp, Tmp2);
4954 }
4955 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4956 return Tmp;
4957 }
4958 case ISD::LOAD: {
4959 LoadSDNode *LD = cast<LoadSDNode>(Op);
4960 if (const MDNode *Ranges = LD->getRanges()) {
4961 if (DemandedElts != 1)
4962 break;
4963
4965 if (VTBits > CR.getBitWidth()) {
4966 switch (LD->getExtensionType()) {
4967 case ISD::SEXTLOAD:
4968 CR = CR.signExtend(VTBits);
4969 break;
4970 case ISD::ZEXTLOAD:
4971 CR = CR.zeroExtend(VTBits);
4972 break;
4973 default:
4974 break;
4975 }
4976 }
4977
4978 if (VTBits != CR.getBitWidth())
4979 break;
4980 return std::min(CR.getSignedMin().getNumSignBits(),
4982 }
4983
4984 break;
4985 }
4988 case ISD::ATOMIC_SWAP:
5000 case ISD::ATOMIC_LOAD: {
5001 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
5002 // If we are looking at the loaded value.
5003 if (Op.getResNo() == 0) {
5004 if (Tmp == VTBits)
5005 return 1; // early-out
5007 return VTBits - Tmp + 1;
5009 return VTBits - Tmp;
5010 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5011 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
5012 if (ETy == ISD::SEXTLOAD)
5013 return VTBits - Tmp + 1;
5014 if (ETy == ISD::ZEXTLOAD)
5015 return VTBits - Tmp;
5016 }
5017 }
5018 break;
5019 }
5020 }
5021
5022 // If we are looking at the loaded value of the SDNode.
5023 if (Op.getResNo() == 0) {
5024 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5025 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5026 unsigned ExtType = LD->getExtensionType();
5027 switch (ExtType) {
5028 default: break;
5029 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5030 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5031 return VTBits - Tmp + 1;
5032 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5033 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5034 return VTBits - Tmp;
5035 case ISD::NON_EXTLOAD:
5036 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5037 // We only need to handle vectors - computeKnownBits should handle
5038 // scalar cases.
5039 Type *CstTy = Cst->getType();
5040 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5041 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5042 VTBits == CstTy->getScalarSizeInBits()) {
5043 Tmp = VTBits;
5044 for (unsigned i = 0; i != NumElts; ++i) {
5045 if (!DemandedElts[i])
5046 continue;
5047 if (Constant *Elt = Cst->getAggregateElement(i)) {
5048 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5049 const APInt &Value = CInt->getValue();
5050 Tmp = std::min(Tmp, Value.getNumSignBits());
5051 continue;
5052 }
5053 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5054 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5055 Tmp = std::min(Tmp, Value.getNumSignBits());
5056 continue;
5057 }
5058 }
5059 // Unknown type. Conservatively assume no bits match sign bit.
5060 return 1;
5061 }
5062 return Tmp;
5063 }
5064 }
5065 break;
5066 }
5067 }
5068 }
5069
5070 // Allow the target to implement this method for its nodes.
5071 if (Opcode >= ISD::BUILTIN_OP_END ||
5072 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5073 Opcode == ISD::INTRINSIC_W_CHAIN ||
5074 Opcode == ISD::INTRINSIC_VOID) {
5075 // TODO: This can probably be removed once target code is audited. This
5076 // is here purely to reduce patch size and review complexity.
5077 if (!VT.isScalableVector()) {
5078 unsigned NumBits =
5079 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5080 if (NumBits > 1)
5081 FirstAnswer = std::max(FirstAnswer, NumBits);
5082 }
5083 }
5084
5085 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5086 // use this information.
5087 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5088 return std::max(FirstAnswer, Known.countMinSignBits());
5089}
5090
5092 unsigned Depth) const {
5093 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5094 return Op.getScalarValueSizeInBits() - SignBits + 1;
5095}
5096
5098 const APInt &DemandedElts,
5099 unsigned Depth) const {
5100 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5101 return Op.getScalarValueSizeInBits() - SignBits + 1;
5102}
5103
5105 unsigned Depth) const {
5106 // Early out for FREEZE.
5107 if (Op.getOpcode() == ISD::FREEZE)
5108 return true;
5109
5110 // TODO: Assume we don't know anything for now.
5111 EVT VT = Op.getValueType();
5112 if (VT.isScalableVector())
5113 return false;
5114
5115 APInt DemandedElts = VT.isVector()
5117 : APInt(1, 1);
5118 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5119}
5120
5122 const APInt &DemandedElts,
5123 bool PoisonOnly,
5124 unsigned Depth) const {
5125 unsigned Opcode = Op.getOpcode();
5126
5127 // Early out for FREEZE.
5128 if (Opcode == ISD::FREEZE)
5129 return true;
5130
5131 if (Depth >= MaxRecursionDepth)
5132 return false; // Limit search depth.
5133
5134 if (isIntOrFPConstant(Op))
5135 return true;
5136
5137 switch (Opcode) {
5138 case ISD::CONDCODE:
5139 case ISD::VALUETYPE:
5140 case ISD::FrameIndex:
5142 case ISD::CopyFromReg:
5143 return true;
5144
5145 case ISD::UNDEF:
5146 return PoisonOnly;
5147
5148 case ISD::BUILD_VECTOR:
5149 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5150 // this shouldn't affect the result.
5151 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5152 if (!DemandedElts[i])
5153 continue;
5155 Depth + 1))
5156 return false;
5157 }
5158 return true;
5159
5160 case ISD::VECTOR_SHUFFLE: {
5161 APInt DemandedLHS, DemandedRHS;
5162 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5163 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5164 DemandedElts, DemandedLHS, DemandedRHS,
5165 /*AllowUndefElts=*/false))
5166 return false;
5167 if (!DemandedLHS.isZero() &&
5168 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5169 PoisonOnly, Depth + 1))
5170 return false;
5171 if (!DemandedRHS.isZero() &&
5172 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5173 PoisonOnly, Depth + 1))
5174 return false;
5175 return true;
5176 }
5177
5178 // TODO: Search for noundef attributes from library functions.
5179
5180 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5181
5182 default:
5183 // Allow the target to implement this method for its nodes.
5184 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5185 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5187 Op, DemandedElts, *this, PoisonOnly, Depth);
5188 break;
5189 }
5190
5191 // If Op can't create undef/poison and none of its operands are undef/poison
5192 // then Op is never undef/poison.
5193 // NOTE: TargetNodes can handle this in themselves in
5194 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5195 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5196 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5197 Depth) &&
5198 all_of(Op->ops(), [&](SDValue V) {
5199 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5200 });
5201}
5202
5204 bool ConsiderFlags,
5205 unsigned Depth) const {
5206 // TODO: Assume we don't know anything for now.
5207 EVT VT = Op.getValueType();
5208 if (VT.isScalableVector())
5209 return true;
5210
5211 APInt DemandedElts = VT.isVector()
5213 : APInt(1, 1);
5214 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5215 Depth);
5216}
5217
5219 bool PoisonOnly, bool ConsiderFlags,
5220 unsigned Depth) const {
5221 // TODO: Assume we don't know anything for now.
5222 EVT VT = Op.getValueType();
5223 if (VT.isScalableVector())
5224 return true;
5225
5226 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5227 return true;
5228
5229 unsigned Opcode = Op.getOpcode();
5230 switch (Opcode) {
5231 case ISD::FREEZE:
5234 case ISD::SADDSAT:
5235 case ISD::UADDSAT:
5236 case ISD::SSUBSAT:
5237 case ISD::USUBSAT:
5238 case ISD::MULHU:
5239 case ISD::MULHS:
5240 case ISD::SMIN:
5241 case ISD::SMAX:
5242 case ISD::UMIN:
5243 case ISD::UMAX:
5244 case ISD::AND:
5245 case ISD::XOR:
5246 case ISD::ROTL:
5247 case ISD::ROTR:
5248 case ISD::FSHL:
5249 case ISD::FSHR:
5250 case ISD::BSWAP:
5251 case ISD::CTPOP:
5252 case ISD::BITREVERSE:
5253 case ISD::PARITY:
5254 case ISD::SIGN_EXTEND:
5255 case ISD::TRUNCATE:
5259 case ISD::BITCAST:
5260 case ISD::BUILD_VECTOR:
5261 case ISD::BUILD_PAIR:
5262 return false;
5263
5264 case ISD::SELECT_CC:
5265 case ISD::SETCC: {
5266 // Integer setcc cannot create undef or poison.
5267 if (Op.getOperand(0).getValueType().isInteger())
5268 return false;
5269
5270 // FP compares are more complicated. They can create poison for nan/infinity
5271 // based on options and flags. The options and flags also cause special
5272 // nonan condition codes to be used. Those condition codes may be preserved
5273 // even if the nonan flag is dropped somewhere.
5274 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5275 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5276 if (((unsigned)CCCode & 0x10U))
5277 return true;
5278
5280 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5281 }
5282
5283 case ISD::OR:
5284 case ISD::ZERO_EXTEND:
5285 case ISD::ADD:
5286 case ISD::SUB:
5287 case ISD::MUL:
5288 // No poison except from flags (which is handled above)
5289 return false;
5290
5291 case ISD::SHL:
5292 case ISD::SRL:
5293 case ISD::SRA:
5294 // If the max shift amount isn't in range, then the shift can
5295 // create poison.
5296 return !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedElts,
5297 PoisonOnly, Depth + 1) ||
5298 !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5299
5301 // Check if we demand any upper (undef) elements.
5302 return !PoisonOnly && DemandedElts.ugt(1);
5303
5306 // Ensure that the element index is in bounds.
5307 EVT VecVT = Op.getOperand(0).getValueType();
5308 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5310 Depth + 1)) {
5311 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5312 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5313 }
5314 return true;
5315 }
5316
5317 case ISD::VECTOR_SHUFFLE: {
5318 // Check for any demanded shuffle element that is undef.
5319 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5320 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5321 if (Elt < 0 && DemandedElts[Idx])
5322 return true;
5323 return false;
5324 }
5325
5326 default:
5327 // Allow the target to implement this method for its nodes.
5328 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5329 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5331 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5332 break;
5333 }
5334
5335 // Be conservative and return true.
5336 return true;
5337}
5338
5339bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5340 unsigned Opcode = Op.getOpcode();
5341 if (Opcode == ISD::OR)
5342 return Op->getFlags().hasDisjoint() ||
5343 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5344 if (Opcode == ISD::XOR)
5345 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5346 return false;
5347}
5348
5350 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5351 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5352}
5353
5354bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5355 // If we're told that NaNs won't happen, assume they won't.
5356 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5357 return true;
5358
5359 if (Depth >= MaxRecursionDepth)
5360 return false; // Limit search depth.
5361
5362 // If the value is a constant, we can obviously see if it is a NaN or not.
5363 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5364 return !C->getValueAPF().isNaN() ||
5365 (SNaN && !C->getValueAPF().isSignaling());
5366 }
5367
5368 unsigned Opcode = Op.getOpcode();
5369 switch (Opcode) {
5370 case ISD::FADD:
5371 case ISD::FSUB:
5372 case ISD::FMUL:
5373 case ISD::FDIV:
5374 case ISD::FREM:
5375 case ISD::FSIN:
5376 case ISD::FCOS:
5377 case ISD::FTAN:
5378 case ISD::FMA:
5379 case ISD::FMAD: {
5380 if (SNaN)
5381 return true;
5382 // TODO: Need isKnownNeverInfinity
5383 return false;
5384 }
5385 case ISD::FCANONICALIZE:
5386 case ISD::FEXP:
5387 case ISD::FEXP2:
5388 case ISD::FEXP10:
5389 case ISD::FTRUNC:
5390 case ISD::FFLOOR:
5391 case ISD::FCEIL:
5392 case ISD::FROUND:
5393 case ISD::FROUNDEVEN:
5394 case ISD::FRINT:
5395 case ISD::LRINT:
5396 case ISD::LLRINT:
5397 case ISD::FNEARBYINT:
5398 case ISD::FLDEXP: {
5399 if (SNaN)
5400 return true;
5401 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5402 }
5403 case ISD::FABS:
5404 case ISD::FNEG:
5405 case ISD::FCOPYSIGN: {
5406 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5407 }
5408 case ISD::SELECT:
5409 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5410 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5411 case ISD::FP_EXTEND:
5412 case ISD::FP_ROUND: {
5413 if (SNaN)
5414 return true;
5415 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5416 }
5417 case ISD::SINT_TO_FP:
5418 case ISD::UINT_TO_FP:
5419 return true;
5420 case ISD::FSQRT: // Need is known positive
5421 case ISD::FLOG:
5422 case ISD::FLOG2:
5423 case ISD::FLOG10:
5424 case ISD::FPOWI:
5425 case ISD::FPOW: {
5426 if (SNaN)
5427 return true;
5428 // TODO: Refine on operand
5429 return false;
5430 }
5431 case ISD::FMINNUM:
5432 case ISD::FMAXNUM: {
5433 // Only one needs to be known not-nan, since it will be returned if the
5434 // other ends up being one.
5435 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5436 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5437 }
5438 case ISD::FMINNUM_IEEE:
5439 case ISD::FMAXNUM_IEEE: {
5440 if (SNaN)
5441 return true;
5442 // This can return a NaN if either operand is an sNaN, or if both operands
5443 // are NaN.
5444 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5445 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5446 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5447 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5448 }
5449 case ISD::FMINIMUM:
5450 case ISD::FMAXIMUM: {
5451 // TODO: Does this quiet or return the origina NaN as-is?
5452 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5453 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5454 }
5456 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5457 }
5458 case ISD::BUILD_VECTOR: {
5459 for (const SDValue &Opnd : Op->ops())
5460 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5461 return false;
5462 return true;
5463 }
5464 default:
5465 if (Opcode >= ISD::BUILTIN_OP_END ||
5466 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5467 Opcode == ISD::INTRINSIC_W_CHAIN ||
5468 Opcode == ISD::INTRINSIC_VOID) {
5469 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5470 }
5471
5472 return false;
5473 }
5474}
5475
5477 assert(Op.getValueType().isFloatingPoint() &&
5478 "Floating point type expected");
5479
5480 // If the value is a constant, we can obviously see if it is a zero or not.
5482 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5483}
5484
5486 if (Depth >= MaxRecursionDepth)
5487 return false; // Limit search depth.
5488
5489 assert(!Op.getValueType().isFloatingPoint() &&
5490 "Floating point types unsupported - use isKnownNeverZeroFloat");
5491
5492 // If the value is a constant, we can obviously see if it is a zero or not.
5494 [](ConstantSDNode *C) { return !C->isZero(); }))
5495 return true;
5496
5497 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5498 // some degree.
5499 switch (Op.getOpcode()) {
5500 default:
5501 break;
5502
5503 case ISD::OR:
5504 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5505 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5506
5507 case ISD::VSELECT:
5508 case ISD::SELECT:
5509 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5510 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5511
5512 case ISD::SHL: {
5513 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5514 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5515 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5516 // 1 << X is never zero.
5517 if (ValKnown.One[0])
5518 return true;
5519 // If max shift cnt of known ones is non-zero, result is non-zero.
5520 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5521 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5522 !ValKnown.One.shl(MaxCnt).isZero())
5523 return true;
5524 break;
5525 }
5526 case ISD::UADDSAT:
5527 case ISD::UMAX:
5528 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5529 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5530
5531 // For smin/smax: If either operand is known negative/positive
5532 // respectively we don't need the other to be known at all.
5533 case ISD::SMAX: {
5534 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5535 if (Op1.isStrictlyPositive())
5536 return true;
5537
5538 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5539 if (Op0.isStrictlyPositive())
5540 return true;
5541
5542 if (Op1.isNonZero() && Op0.isNonZero())
5543 return true;
5544
5545 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5546 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5547 }
5548 case ISD::SMIN: {
5549 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5550 if (Op1.isNegative())
5551 return true;
5552
5553 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5554 if (Op0.isNegative())
5555 return true;
5556
5557 if (Op1.isNonZero() && Op0.isNonZero())
5558 return true;
5559
5560 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5561 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5562 }
5563 case ISD::UMIN:
5564 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5565 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5566
5567 case ISD::ROTL:
5568 case ISD::ROTR:
5569 case ISD::BITREVERSE:
5570 case ISD::BSWAP:
5571 case ISD::CTPOP:
5572 case ISD::ABS:
5573 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5574
5575 case ISD::SRA:
5576 case ISD::SRL: {
5577 if (Op->getFlags().hasExact())
5578 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5579 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5580 if (ValKnown.isNegative())
5581 return true;
5582 // If max shift cnt of known ones is non-zero, result is non-zero.
5583 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5584 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5585 !ValKnown.One.lshr(MaxCnt).isZero())
5586 return true;
5587 break;
5588 }
5589 case ISD::UDIV:
5590 case ISD::SDIV:
5591 // div exact can only produce a zero if the dividend is zero.
5592 // TODO: For udiv this is also true if Op1 u<= Op0
5593 if (Op->getFlags().hasExact())
5594 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5595 break;
5596
5597 case ISD::ADD:
5598 if (Op->getFlags().hasNoUnsignedWrap())
5599 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5600 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5601 return true;
5602 // TODO: There are a lot more cases we can prove for add.
5603 break;
5604
5605 case ISD::SUB: {
5606 if (isNullConstant(Op.getOperand(0)))
5607 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5608
5609 std::optional<bool> ne =
5610 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5611 computeKnownBits(Op.getOperand(1), Depth + 1));
5612 return ne && *ne;
5613 }
5614
5615 case ISD::MUL:
5616 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5617 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5618 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5619 return true;
5620 break;
5621
5622 case ISD::ZERO_EXTEND:
5623 case ISD::SIGN_EXTEND:
5624 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5625 }
5626
5628}
5629
5631 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
5632 return !C1->isNegative();
5633
5634 return Op.getOpcode() == ISD::FABS;
5635}
5636
5638 // Check the obvious case.
5639 if (A == B) return true;
5640
5641 // For negative and positive zero.
5642 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5643 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5644 if (CA->isZero() && CB->isZero()) return true;
5645
5646 // Otherwise they may not be equal.
5647 return false;
5648}
5649
5650// Only bits set in Mask must be negated, other bits may be arbitrary.
5652 if (isBitwiseNot(V, AllowUndefs))
5653 return V.getOperand(0);
5654
5655 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5656 // bits in the non-extended part.
5657 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5658 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5659 return SDValue();
5660 SDValue ExtArg = V.getOperand(0);
5661 if (ExtArg.getScalarValueSizeInBits() >=
5662 MaskC->getAPIntValue().getActiveBits() &&
5663 isBitwiseNot(ExtArg, AllowUndefs) &&
5664 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
5665 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
5666 return ExtArg.getOperand(0).getOperand(0);
5667 return SDValue();
5668}
5669
5671 // Match masked merge pattern (X & ~M) op (Y & M)
5672 // Including degenerate case (X & ~M) op M
5673 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
5674 SDValue Other) {
5675 if (SDValue NotOperand =
5676 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
5677 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
5678 NotOperand->getOpcode() == ISD::TRUNCATE)
5679 NotOperand = NotOperand->getOperand(0);
5680
5681 if (Other == NotOperand)
5682 return true;
5683 if (Other->getOpcode() == ISD::AND)
5684 return NotOperand == Other->getOperand(0) ||
5685 NotOperand == Other->getOperand(1);
5686 }
5687 return false;
5688 };
5689
5690 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
5691 A = A->getOperand(0);
5692
5693 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
5694 B = B->getOperand(0);
5695
5696 if (A->getOpcode() == ISD::AND)
5697 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
5698 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
5699 return false;
5700}
5701
5702// FIXME: unify with llvm::haveNoCommonBitsSet.
5704 assert(A.getValueType() == B.getValueType() &&
5705 "Values must have the same type");
5708 return true;
5711}
5712
5713static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
5714 SelectionDAG &DAG) {
5715 if (cast<ConstantSDNode>(Step)->isZero())
5716 return DAG.getConstant(0, DL, VT);
5717
5718 return SDValue();
5719}
5720
5723 SelectionDAG &DAG) {
5724 int NumOps = Ops.size();
5725 assert(NumOps != 0 && "Can't build an empty vector!");
5726 assert(!VT.isScalableVector() &&
5727 "BUILD_VECTOR cannot be used with scalable types");
5728 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
5729 "Incorrect element count in BUILD_VECTOR!");
5730
5731 // BUILD_VECTOR of UNDEFs is UNDEF.
5732 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5733 return DAG.getUNDEF(VT);
5734
5735 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
5736 SDValue IdentitySrc;
5737 bool IsIdentity = true;
5738 for (int i = 0; i != NumOps; ++i) {
5739 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5740 Ops[i].getOperand(0).getValueType() != VT ||
5741 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
5742 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
5743 Ops[i].getConstantOperandAPInt(1) != i) {
5744 IsIdentity = false;
5745 break;
5746 }
5747 IdentitySrc = Ops[i].getOperand(0);
5748 }
5749 if (IsIdentity)
5750 return IdentitySrc;
5751
5752 return SDValue();
5753}
5754
5755/// Try to simplify vector concatenation to an input value, undef, or build
5756/// vector.
5759 SelectionDAG &DAG) {
5760 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
5761 assert(llvm::all_of(Ops,
5762 [Ops](SDValue Op) {
5763 return Ops[0].getValueType() == Op.getValueType();
5764 }) &&
5765 "Concatenation of vectors with inconsistent value types!");
5766 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
5767 VT.getVectorElementCount() &&
5768 "Incorrect element count in vector concatenation!");
5769
5770 if (Ops.size() == 1)
5771 return Ops[0];
5772
5773 // Concat of UNDEFs is UNDEF.
5774 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5775 return DAG.getUNDEF(VT);
5776
5777 // Scan the operands and look for extract operations from a single source
5778 // that correspond to insertion at the same location via this concatenation:
5779 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
5780 SDValue IdentitySrc;
5781 bool IsIdentity = true;
5782 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
5783 SDValue Op = Ops[i];
5784 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
5785 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
5786 Op.getOperand(0).getValueType() != VT ||
5787 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
5788 Op.getConstantOperandVal(1) != IdentityIndex) {
5789 IsIdentity = false;
5790 break;
5791 }
5792 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
5793 "Unexpected identity source vector for concat of extracts");
5794 IdentitySrc = Op.getOperand(0);
5795 }
5796 if (IsIdentity) {
5797 assert(IdentitySrc && "Failed to set source vector of extracts");
5798 return IdentitySrc;
5799 }
5800
5801 // The code below this point is only designed to work for fixed width
5802 // vectors, so we bail out for now.
5803 if (VT.isScalableVector())
5804 return SDValue();
5805
5806 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
5807 // simplified to one big BUILD_VECTOR.
5808 // FIXME: Add support for SCALAR_TO_VECTOR as well.
5809 EVT SVT = VT.getScalarType();
5811 for (SDValue Op : Ops) {
5812 EVT OpVT = Op.getValueType();
5813 if (Op.isUndef())
5814 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
5815 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
5816 Elts.append(Op->op_begin(), Op->op_end());
5817 else
5818 return SDValue();
5819 }
5820
5821 // BUILD_VECTOR requires all inputs to be of the same type, find the
5822 // maximum type and extend them all.
5823 for (SDValue Op : Elts)
5824 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
5825
5826 if (SVT.bitsGT(VT.getScalarType())) {
5827 for (SDValue &Op : Elts) {
5828 if (Op.isUndef())
5829 Op = DAG.getUNDEF(SVT);
5830 else
5831 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
5832 ? DAG.getZExtOrTrunc(Op, DL, SVT)
5833 : DAG.getSExtOrTrunc(Op, DL, SVT);
5834 }
5835 }
5836
5837 SDValue V = DAG.getBuildVector(VT, DL, Elts);
5838 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
5839 return V;
5840}
5841
5842/// Gets or creates the specified node.
5843SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
5844 SDVTList VTs = getVTList(VT);
5846 AddNodeIDNode(ID, Opcode, VTs, std::nullopt);
5847 void *IP = nullptr;
5848 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5849 return SDValue(E, 0);
5850
5851 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5852 CSEMap.InsertNode(N, IP);
5853
5854 InsertNode(N);
5855 SDValue V = SDValue(N, 0);
5856 NewSDValueDbgMsg(V, "Creating new node: ", this);
5857 return V;
5858}
5859
5860SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5861 SDValue N1) {
5862 SDNodeFlags Flags;
5863 if (Inserter)
5864 Flags = Inserter->getFlags();
5865 return getNode(Opcode, DL, VT, N1, Flags);
5866}
5867
5868SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5869 SDValue N1, const SDNodeFlags Flags) {
5870 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
5871
5872 // Constant fold unary operations with a vector integer or float operand.
5873 switch (Opcode) {
5874 default:
5875 // FIXME: Entirely reasonable to perform folding of other unary
5876 // operations here as the need arises.
5877 break;
5878 case ISD::FNEG:
5879 case ISD::FABS:
5880 case ISD::FCEIL:
5881 case ISD::FTRUNC:
5882 case ISD::FFLOOR:
5883 case ISD::FP_EXTEND:
5884 case ISD::FP_TO_SINT:
5885 case ISD::FP_TO_UINT:
5886 case ISD::FP_TO_FP16:
5887 case ISD::FP_TO_BF16:
5888 case ISD::TRUNCATE:
5889 case ISD::ANY_EXTEND:
5890 case ISD::ZERO_EXTEND:
5891 case ISD::SIGN_EXTEND:
5892 case ISD::UINT_TO_FP:
5893 case ISD::SINT_TO_FP:
5894 case ISD::FP16_TO_FP:
5895 case ISD::BF16_TO_FP:
5896 case ISD::BITCAST:
5897 case ISD::ABS:
5898 case ISD::BITREVERSE:
5899 case ISD::BSWAP:
5900 case ISD::CTLZ:
5902 case ISD::CTTZ:
5904 case ISD::CTPOP:
5905 case ISD::STEP_VECTOR: {
5906 SDValue Ops = {N1};
5907 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
5908 return Fold;
5909 }
5910 }
5911
5912 unsigned OpOpcode = N1.getNode()->getOpcode();
5913 switch (Opcode) {
5914 case ISD::STEP_VECTOR:
5915 assert(VT.isScalableVector() &&
5916 "STEP_VECTOR can only be used with scalable types");
5917 assert(OpOpcode == ISD::TargetConstant &&
5918 VT.getVectorElementType() == N1.getValueType() &&
5919 "Unexpected step operand");
5920 break;
5921 case ISD::FREEZE:
5922 assert(VT == N1.getValueType() && "Unexpected VT!");
5923 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
5924 /*Depth*/ 1))
5925 return N1;
5926 break;
5927 case ISD::TokenFactor:
5928 case ISD::MERGE_VALUES:
5930 return N1; // Factor, merge or concat of one node? No need.
5931 case ISD::BUILD_VECTOR: {
5932 // Attempt to simplify BUILD_VECTOR.
5933 SDValue Ops[] = {N1};
5934 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5935 return V;
5936 break;
5937 }
5938 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
5939 case ISD::FP_EXTEND:
5941 "Invalid FP cast!");
5942 if (N1.getValueType() == VT) return N1; // noop conversion.
5943 assert((!VT.isVector() || VT.getVectorElementCount() ==
5945 "Vector element count mismatch!");
5946 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
5947 if (N1.isUndef())
5948 return getUNDEF(VT);
5949 break;
5950 case ISD::FP_TO_SINT:
5951 case ISD::FP_TO_UINT:
5952 if (N1.isUndef())
5953 return getUNDEF(VT);
5954 break;
5955 case ISD::SINT_TO_FP:
5956 case ISD::UINT_TO_FP:
5957 // [us]itofp(undef) = 0, because the result value is bounded.
5958 if (N1.isUndef())
5959 return getConstantFP(0.0, DL, VT);
5960 break;
5961 case ISD::SIGN_EXTEND:
5962 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5963 "Invalid SIGN_EXTEND!");
5964 assert(VT.isVector() == N1.getValueType().isVector() &&
5965 "SIGN_EXTEND result type type should be vector iff the operand "
5966 "type is vector!");
5967 if (N1.getValueType() == VT) return N1; // noop extension
5968 assert((!VT.isVector() || VT.getVectorElementCount() ==
5970 "Vector element count mismatch!");
5971 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
5972 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
5973 SDNodeFlags Flags;
5974 if (OpOpcode == ISD::ZERO_EXTEND)
5975 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5976 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
5977 }
5978 if (OpOpcode == ISD::UNDEF)
5979 // sext(undef) = 0, because the top bits will all be the same.
5980 return getConstant(0, DL, VT);
5981 break;
5982 case ISD::ZERO_EXTEND:
5983 assert(VT.isInteger() && N1.getValueType().isInteger() &&
5984 "Invalid ZERO_EXTEND!");
5985 assert(VT.isVector() == N1.getValueType().isVector() &&
5986 "ZERO_EXTEND result type type should be vector iff the operand "
5987 "type is vector!");
5988 if (N1.getValueType() == VT) return N1; // noop extension
5989 assert((!VT.isVector() || VT.getVectorElementCount() ==
5991 "Vector element count mismatch!");
5992 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
5993 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
5994 SDNodeFlags Flags;
5995 Flags.setNonNeg(N1->getFlags().hasNonNeg());
5996 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
5997 }
5998 if (OpOpcode == ISD::UNDEF)
5999 // zext(undef) = 0, because the top bits will be zero.
6000 return getConstant(0, DL, VT);
6001
6002 // Skip unnecessary zext_inreg pattern:
6003 // (zext (trunc x)) -> x iff the upper bits are known zero.
6004 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6005 // use to recognise zext_inreg patterns.
6006 if (OpOpcode == ISD::TRUNCATE) {
6007 SDValue OpOp = N1.getOperand(0);
6008 if (OpOp.getValueType() == VT) {
6009 if (OpOp.getOpcode() != ISD::AND) {
6012 if (MaskedValueIsZero(OpOp, HiBits)) {
6013 transferDbgValues(N1, OpOp);
6014 return OpOp;
6015 }
6016 }
6017 }
6018 }
6019 break;
6020 case ISD::ANY_EXTEND:
6021 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6022 "Invalid ANY_EXTEND!");
6023 assert(VT.isVector() == N1.getValueType().isVector() &&
6024 "ANY_EXTEND result type type should be vector iff the operand "
6025 "type is vector!");
6026 if (N1.getValueType() == VT) return N1; // noop extension
6027 assert((!VT.isVector() || VT.getVectorElementCount() ==
6029 "Vector element count mismatch!");
6030 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6031
6032 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6033 OpOpcode == ISD::ANY_EXTEND) {
6034 SDNodeFlags Flags;
6035 if (OpOpcode == ISD::ZERO_EXTEND)
6036 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6037 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6038 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6039 }
6040 if (OpOpcode == ISD::UNDEF)
6041 return getUNDEF(VT);
6042
6043 // (ext (trunc x)) -> x
6044 if (OpOpcode == ISD::TRUNCATE) {
6045 SDValue OpOp = N1.getOperand(0);
6046 if (OpOp.getValueType() == VT) {
6047 transferDbgValues(N1, OpOp);
6048 return OpOp;
6049 }
6050 }
6051 break;
6052 case ISD::TRUNCATE:
6053 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6054 "Invalid TRUNCATE!");
6055 assert(VT.isVector() == N1.getValueType().isVector() &&
6056 "TRUNCATE result type type should be vector iff the operand "
6057 "type is vector!");
6058 if (N1.getValueType() == VT) return N1; // noop truncate
6059 assert((!VT.isVector() || VT.getVectorElementCount() ==
6061 "Vector element count mismatch!");
6062 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6063 if (OpOpcode == ISD::TRUNCATE)
6064 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6065 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6066 OpOpcode == ISD::ANY_EXTEND) {
6067 // If the source is smaller than the dest, we still need an extend.
6069 VT.getScalarType()))
6070 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
6071 if (N1.getOperand(0).getValueType().bitsGT(VT))
6072 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6073 return N1.getOperand(0);
6074 }
6075 if (OpOpcode == ISD::UNDEF)
6076 return getUNDEF(VT);
6077 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6078 return getVScale(DL, VT,
6080 break;
6084 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6085 assert(N1.getValueType().bitsLE(VT) &&
6086 "The input must be the same size or smaller than the result.");
6089 "The destination vector type must have fewer lanes than the input.");
6090 break;
6091 case ISD::ABS:
6092 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6093 if (OpOpcode == ISD::UNDEF)
6094 return getConstant(0, DL, VT);
6095 break;
6096 case ISD::BSWAP:
6097 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6098 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6099 "BSWAP types must be a multiple of 16 bits!");
6100 if (OpOpcode == ISD::UNDEF)
6101 return getUNDEF(VT);
6102 // bswap(bswap(X)) -> X.
6103 if (OpOpcode == ISD::BSWAP)
6104 return N1.getOperand(0);
6105 break;
6106 case ISD::BITREVERSE:
6107 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6108 if (OpOpcode == ISD::UNDEF)
6109 return getUNDEF(VT);
6110 break;
6111 case ISD::BITCAST:
6113 "Cannot BITCAST between types of different sizes!");
6114 if (VT == N1.getValueType()) return N1; // noop conversion.
6115 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6116 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6117 if (OpOpcode == ISD::UNDEF)
6118 return getUNDEF(VT);
6119 break;
6121 assert(VT.isVector() && !N1.getValueType().isVector() &&
6122 (VT.getVectorElementType() == N1.getValueType() ||
6124 N1.getValueType().isInteger() &&
6126 "Illegal SCALAR_TO_VECTOR node!");
6127 if (OpOpcode == ISD::UNDEF)
6128 return getUNDEF(VT);
6129 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6130 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6131 isa<ConstantSDNode>(N1.getOperand(1)) &&
6132 N1.getConstantOperandVal(1) == 0 &&
6133 N1.getOperand(0).getValueType() == VT)
6134 return N1.getOperand(0);
6135 break;
6136 case ISD::FNEG:
6137 // Negation of an unknown bag of bits is still completely undefined.
6138 if (OpOpcode == ISD::UNDEF)
6139 return getUNDEF(VT);
6140
6141 if (OpOpcode == ISD::FNEG) // --X -> X
6142 return N1.getOperand(0);
6143 break;
6144 case ISD::FABS:
6145 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6146 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6147 break;
6148 case ISD::VSCALE:
6149 assert(VT == N1.getValueType() && "Unexpected VT!");
6150 break;
6151 case ISD::CTPOP:
6152 if (N1.getValueType().getScalarType() == MVT::i1)
6153 return N1;
6154 break;
6155 case ISD::CTLZ:
6156 case ISD::CTTZ:
6157 if (N1.getValueType().getScalarType() == MVT::i1)
6158 return getNOT(DL, N1, N1.getValueType());
6159 break;
6160 case ISD::VECREDUCE_ADD:
6161 if (N1.getValueType().getScalarType() == MVT::i1)
6162 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6163 break;
6166 if (N1.getValueType().getScalarType() == MVT::i1)
6167 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6168 break;
6171 if (N1.getValueType().getScalarType() == MVT::i1)
6172 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6173 break;
6174 case ISD::SPLAT_VECTOR:
6175 assert(VT.isVector() && "Wrong return type!");
6176 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6177 // that for now.
6179 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6181 N1.getValueType().isInteger() &&
6183 "Wrong operand type!");
6184 break;
6185 }
6186
6187 SDNode *N;
6188 SDVTList VTs = getVTList(VT);
6189 SDValue Ops[] = {N1};
6190 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6192 AddNodeIDNode(ID, Opcode, VTs, Ops);
6193 void *IP = nullptr;
6194 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6195 E->intersectFlagsWith(Flags);
6196 return SDValue(E, 0);
6197 }
6198
6199 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6200 N->setFlags(Flags);
6201 createOperands(N, Ops);
6202 CSEMap.InsertNode(N, IP);
6203 } else {
6204 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6205 createOperands(N, Ops);
6206 }
6207
6208 InsertNode(N);
6209 SDValue V = SDValue(N, 0);
6210 NewSDValueDbgMsg(V, "Creating new node: ", this);
6211 return V;
6212}
6213
6214static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6215 const APInt &C2) {
6216 switch (Opcode) {
6217 case ISD::ADD: return C1 + C2;
6218 case ISD::SUB: return C1 - C2;
6219 case ISD::MUL: return C1 * C2;
6220 case ISD::AND: return C1 & C2;
6221 case ISD::OR: return C1 | C2;
6222 case ISD::XOR: return C1 ^ C2;
6223 case ISD::SHL: return C1 << C2;
6224 case ISD::SRL: return C1.lshr(C2);
6225 case ISD::SRA: return C1.ashr(C2);
6226 case ISD::ROTL: return C1.rotl(C2);
6227 case ISD::ROTR: return C1.rotr(C2);
6228 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6229 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6230 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6231 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6232 case ISD::SADDSAT: return C1.sadd_sat(C2);
6233 case ISD::UADDSAT: return C1.uadd_sat(C2);
6234 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6235 case ISD::USUBSAT: return C1.usub_sat(C2);
6236 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6237 case ISD::USHLSAT: return C1.ushl_sat(C2);
6238 case ISD::UDIV:
6239 if (!C2.getBoolValue())
6240 break;
6241 return C1.udiv(C2);
6242 case ISD::UREM:
6243 if (!C2.getBoolValue())
6244 break;
6245 return C1.urem(C2);
6246 case ISD::SDIV:
6247 if (!C2.getBoolValue())
6248 break;
6249 return C1.sdiv(C2);
6250 case ISD::SREM:
6251 if (!C2.getBoolValue())
6252 break;
6253 return C1.srem(C2);
6254 case ISD::AVGFLOORS:
6255 return APIntOps::avgFloorS(C1, C2);
6256 case ISD::AVGFLOORU:
6257 return APIntOps::avgFloorU(C1, C2);
6258 case ISD::AVGCEILS:
6259 return APIntOps::avgCeilS(C1, C2);
6260 case ISD::AVGCEILU:
6261 return APIntOps::avgCeilU(C1, C2);
6262 case ISD::ABDS:
6263 return APIntOps::abds(C1, C2);
6264 case ISD::ABDU:
6265 return APIntOps::abdu(C1, C2);
6266 case ISD::MULHS:
6267 return APIntOps::mulhs(C1, C2);
6268 case ISD::MULHU:
6269 return APIntOps::mulhu(C1, C2);
6270 }
6271 return std::nullopt;
6272}
6273// Handle constant folding with UNDEF.
6274// TODO: Handle more cases.
6275static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6276 bool IsUndef1, const APInt &C2,
6277 bool IsUndef2) {
6278 if (!(IsUndef1 || IsUndef2))
6279 return FoldValue(Opcode, C1, C2);
6280
6281 // Fold and(x, undef) -> 0
6282 // Fold mul(x, undef) -> 0
6283 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6284 return APInt::getZero(C1.getBitWidth());
6285
6286 return std::nullopt;
6287}
6288
6290 const GlobalAddressSDNode *GA,
6291 const SDNode *N2) {
6292 if (GA->getOpcode() != ISD::GlobalAddress)
6293 return SDValue();
6294 if (!TLI->isOffsetFoldingLegal(GA))
6295 return SDValue();
6296 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6297 if (!C2)
6298 return SDValue();
6299 int64_t Offset = C2->getSExtValue();
6300 switch (Opcode) {
6301 case ISD::ADD: break;
6302 case ISD::SUB: Offset = -uint64_t(Offset); break;
6303 default: return SDValue();
6304 }
6305 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6306 GA->getOffset() + uint64_t(Offset));
6307}
6308
6309bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6310 switch (Opcode) {
6311 case ISD::SDIV:
6312 case ISD::UDIV:
6313 case ISD::SREM:
6314 case ISD::UREM: {
6315 // If a divisor is zero/undef or any element of a divisor vector is
6316 // zero/undef, the whole op is undef.
6317 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6318 SDValue Divisor = Ops[1];
6319 if (Divisor.isUndef() || isNullConstant(Divisor))
6320 return true;
6321
6322 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6323 llvm::any_of(Divisor->op_values(),
6324 [](SDValue V) { return V.isUndef() ||
6325 isNullConstant(V); });
6326 // TODO: Handle signed overflow.
6327 }
6328 // TODO: Handle oversized shifts.
6329 default:
6330 return false;
6331 }
6332}
6333
6335 EVT VT, ArrayRef<SDValue> Ops,
6336 SDNodeFlags Flags) {
6337 // If the opcode is a target-specific ISD node, there's nothing we can
6338 // do here and the operand rules may not line up with the below, so
6339 // bail early.
6340 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6341 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6342 // foldCONCAT_VECTORS in getNode before this is called.
6343 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6344 return SDValue();
6345
6346 unsigned NumOps = Ops.size();
6347 if (NumOps == 0)
6348 return SDValue();
6349
6350 if (isUndef(Opcode, Ops))
6351 return getUNDEF(VT);
6352
6353 // Handle unary special cases.
6354 if (NumOps == 1) {
6355 SDValue N1 = Ops[0];
6356
6357 // Constant fold unary operations with an integer constant operand. Even
6358 // opaque constant will be folded, because the folding of unary operations
6359 // doesn't create new constants with different values. Nevertheless, the
6360 // opaque flag is preserved during folding to prevent future folding with
6361 // other constants.
6362 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6363 const APInt &Val = C->getAPIntValue();
6364 switch (Opcode) {
6365 case ISD::SIGN_EXTEND:
6366 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6367 C->isTargetOpcode(), C->isOpaque());
6368 case ISD::TRUNCATE:
6369 if (C->isOpaque())
6370 break;
6371 [[fallthrough]];
6372 case ISD::ZERO_EXTEND:
6373 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6374 C->isTargetOpcode(), C->isOpaque());
6375 case ISD::ANY_EXTEND:
6376 // Some targets like RISCV prefer to sign extend some types.
6377 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6378 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6379 C->isTargetOpcode(), C->isOpaque());
6380 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6381 C->isTargetOpcode(), C->isOpaque());
6382 case ISD::ABS:
6383 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6384 C->isOpaque());
6385 case ISD::BITREVERSE:
6386 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6387 C->isOpaque());
6388 case ISD::BSWAP:
6389 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6390 C->isOpaque());
6391 case ISD::CTPOP:
6392 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6393 C->isOpaque());
6394 case ISD::CTLZ:
6396 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6397 C->isOpaque());
6398 case ISD::CTTZ:
6400 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6401 C->isOpaque());
6402 case ISD::UINT_TO_FP:
6403 case ISD::SINT_TO_FP: {
6406 (void)apf.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6408 return getConstantFP(apf, DL, VT);
6409 }
6410 case ISD::FP16_TO_FP:
6411 case ISD::BF16_TO_FP: {
6412 bool Ignored;
6413 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6414 : APFloat::BFloat(),
6415 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6416
6417 // This can return overflow, underflow, or inexact; we don't care.
6418 // FIXME need to be more flexible about rounding mode.
6419 (void)FPV.convert(EVTToAPFloatSemantics(VT),
6421 return getConstantFP(FPV, DL, VT);
6422 }
6423 case ISD::STEP_VECTOR:
6424 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6425 return V;
6426 break;
6427 case ISD::BITCAST:
6428 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6429 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6430 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6431 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6432 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6433 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6434 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6435 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6436 break;
6437 }
6438 }
6439
6440 // Constant fold unary operations with a floating point constant operand.
6441 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6442 APFloat V = C->getValueAPF(); // make copy
6443 switch (Opcode) {
6444 case ISD::FNEG:
6445 V.changeSign();
6446 return getConstantFP(V, DL, VT);
6447 case ISD::FABS:
6448 V.clearSign();
6449 return getConstantFP(V, DL, VT);
6450 case ISD::FCEIL: {
6451 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6452 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6453 return getConstantFP(V, DL, VT);
6454 return SDValue();
6455 }
6456 case ISD::FTRUNC: {
6457 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6458 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6459 return getConstantFP(V, DL, VT);
6460 return SDValue();
6461 }
6462 case ISD::FFLOOR: {
6463 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6464 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6465 return getConstantFP(V, DL, VT);
6466 return SDValue();
6467 }
6468 case ISD::FP_EXTEND: {
6469 bool ignored;
6470 // This can return overflow, underflow, or inexact; we don't care.
6471 // FIXME need to be more flexible about rounding mode.
6473 &ignored);
6474 return getConstantFP(V, DL, VT);
6475 }
6476 case ISD::FP_TO_SINT:
6477 case ISD::FP_TO_UINT: {
6478 bool ignored;
6479 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6480 // FIXME need to be more flexible about rounding mode.
6482 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6483 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6484 break;
6485 return getConstant(IntVal, DL, VT);
6486 }
6487 case ISD::FP_TO_FP16:
6488 case ISD::FP_TO_BF16: {
6489 bool Ignored;
6490 // This can return overflow, underflow, or inexact; we don't care.
6491 // FIXME need to be more flexible about rounding mode.
6492 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6493 : APFloat::BFloat(),
6495 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6496 }
6497 case ISD::BITCAST:
6498 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6499 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6500 VT);
6501 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6502 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6503 VT);
6504 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6505 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6506 VT);
6507 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6508 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6509 break;
6510 }
6511 }
6512
6513 // Early-out if we failed to constant fold a bitcast.
6514 if (Opcode == ISD::BITCAST)
6515 return SDValue();
6516 }
6517
6518 // Handle binops special cases.
6519 if (NumOps == 2) {
6520 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6521 return CFP;
6522
6523 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6524 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6525 if (C1->isOpaque() || C2->isOpaque())
6526 return SDValue();
6527
6528 std::optional<APInt> FoldAttempt =
6529 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6530 if (!FoldAttempt)
6531 return SDValue();
6532
6533 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6534 assert((!Folded || !VT.isVector()) &&
6535 "Can't fold vectors ops with scalar operands");
6536 return Folded;
6537 }
6538 }
6539
6540 // fold (add Sym, c) -> Sym+c
6541 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6542 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6543 if (TLI->isCommutativeBinOp(Opcode))
6544 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6545 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6546 }
6547
6548 // This is for vector folding only from here on.
6549 if (!VT.isVector())
6550 return SDValue();
6551
6552 ElementCount NumElts = VT.getVectorElementCount();
6553
6554 // See if we can fold through any bitcasted integer ops.
6555 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6556 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6557 (Ops[0].getOpcode() == ISD::BITCAST ||
6558 Ops[1].getOpcode() == ISD::BITCAST)) {
6559 SDValue N1 = peekThroughBitcasts(Ops[0]);
6560 SDValue N2 = peekThroughBitcasts(Ops[1]);
6561 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6562 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6563 if (BV1 && BV2 && N1.getValueType().isInteger() &&
6564 N2.getValueType().isInteger()) {
6565 bool IsLE = getDataLayout().isLittleEndian();
6566 unsigned EltBits = VT.getScalarSizeInBits();
6567 SmallVector<APInt> RawBits1, RawBits2;
6568 BitVector UndefElts1, UndefElts2;
6569 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6570 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6571 SmallVector<APInt> RawBits;
6572 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6573 std::optional<APInt> Fold = FoldValueWithUndef(
6574 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6575 if (!Fold)
6576 break;
6577 RawBits.push_back(*Fold);
6578 }
6579 if (RawBits.size() == NumElts.getFixedValue()) {
6580 // We have constant folded, but we might need to cast this again back
6581 // to the original (possibly legalized) type.
6582 EVT BVVT, BVEltVT;
6583 if (N1.getValueType() == VT) {
6584 BVVT = N1.getValueType();
6585 BVEltVT = BV1->getOperand(0).getValueType();
6586 } else {
6587 BVVT = N2.getValueType();
6588 BVEltVT = BV2->getOperand(0).getValueType();
6589 }
6590 unsigned BVEltBits = BVEltVT.getSizeInBits();
6591 SmallVector<APInt> DstBits;
6592 BitVector DstUndefs;
6594 DstBits, RawBits, DstUndefs,
6595 BitVector(RawBits.size(), false));
6596 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6597 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6598 if (DstUndefs[I])
6599 continue;
6600 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6601 }
6602 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6603 }
6604 }
6605 }
6606 }
6607
6608 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6609 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6610 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6611 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6612 APInt RHSVal;
6613 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6614 APInt NewStep = Opcode == ISD::MUL
6615 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6616 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6617 return getStepVector(DL, VT, NewStep);
6618 }
6619 }
6620
6621 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6622 return !Op.getValueType().isVector() ||
6623 Op.getValueType().getVectorElementCount() == NumElts;
6624 };
6625
6626 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
6627 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
6628 Op.getOpcode() == ISD::BUILD_VECTOR ||
6629 Op.getOpcode() == ISD::SPLAT_VECTOR;
6630 };
6631
6632 // All operands must be vector types with the same number of elements as
6633 // the result type and must be either UNDEF or a build/splat vector
6634 // or UNDEF scalars.
6635 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
6636 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
6637 return SDValue();
6638
6639 // If we are comparing vectors, then the result needs to be a i1 boolean that
6640 // is then extended back to the legal result type depending on how booleans
6641 // are represented.
6642 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
6643 ISD::NodeType ExtendCode =
6644 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
6647
6648 // Find legal integer scalar type for constant promotion and
6649 // ensure that its scalar size is at least as large as source.
6650 EVT LegalSVT = VT.getScalarType();
6651 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
6652 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
6653 if (LegalSVT.bitsLT(VT.getScalarType()))
6654 return SDValue();
6655 }
6656
6657 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
6658 // only have one operand to check. For fixed-length vector types we may have
6659 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
6660 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
6661
6662 // Constant fold each scalar lane separately.
6663 SmallVector<SDValue, 4> ScalarResults;
6664 for (unsigned I = 0; I != NumVectorElts; I++) {
6665 SmallVector<SDValue, 4> ScalarOps;
6666 for (SDValue Op : Ops) {
6667 EVT InSVT = Op.getValueType().getScalarType();
6668 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
6669 Op.getOpcode() != ISD::SPLAT_VECTOR) {
6670 if (Op.isUndef())
6671 ScalarOps.push_back(getUNDEF(InSVT));
6672 else
6673 ScalarOps.push_back(Op);
6674 continue;
6675 }
6676
6677 SDValue ScalarOp =
6678 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
6679 EVT ScalarVT = ScalarOp.getValueType();
6680
6681 // Build vector (integer) scalar operands may need implicit
6682 // truncation - do this before constant folding.
6683 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
6684 // Don't create illegally-typed nodes unless they're constants or undef
6685 // - if we fail to constant fold we can't guarantee the (dead) nodes
6686 // we're creating will be cleaned up before being visited for
6687 // legalization.
6688 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
6689 !isa<ConstantSDNode>(ScalarOp) &&
6690 TLI->getTypeAction(*getContext(), InSVT) !=
6692 return SDValue();
6693 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
6694 }
6695
6696 ScalarOps.push_back(ScalarOp);
6697 }
6698
6699 // Constant fold the scalar operands.
6700 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
6701
6702 // Legalize the (integer) scalar constant if necessary.
6703 if (LegalSVT != SVT)
6704 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
6705
6706 // Scalar folding only succeeded if the result is a constant or UNDEF.
6707 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
6708 ScalarResult.getOpcode() != ISD::ConstantFP)
6709 return SDValue();
6710 ScalarResults.push_back(ScalarResult);
6711 }
6712
6713 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
6714 : getBuildVector(VT, DL, ScalarResults);
6715 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
6716 return V;
6717}
6718
6720 EVT VT, ArrayRef<SDValue> Ops) {
6721 // TODO: Add support for unary/ternary fp opcodes.
6722 if (Ops.size() != 2)
6723 return SDValue();
6724
6725 // TODO: We don't do any constant folding for strict FP opcodes here, but we
6726 // should. That will require dealing with a potentially non-default
6727 // rounding mode, checking the "opStatus" return value from the APFloat
6728 // math calculations, and possibly other variations.
6729 SDValue N1 = Ops[0];
6730 SDValue N2 = Ops[1];
6731 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
6732 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
6733 if (N1CFP && N2CFP) {
6734 APFloat C1 = N1CFP->getValueAPF(); // make copy
6735 const APFloat &C2 = N2CFP->getValueAPF();
6736 switch (Opcode) {
6737 case ISD::FADD:
6739 return getConstantFP(C1, DL, VT);
6740 case ISD::FSUB:
6742 return getConstantFP(C1, DL, VT);
6743 case ISD::FMUL:
6745 return getConstantFP(C1, DL, VT);
6746 case ISD::FDIV:
6748 return getConstantFP(C1, DL, VT);
6749 case ISD::FREM:
6750 C1.mod(C2);
6751 return getConstantFP(C1, DL, VT);
6752 case ISD::FCOPYSIGN:
6753 C1.copySign(C2);
6754 return getConstantFP(C1, DL, VT);
6755 case ISD::FMINNUM:
6756 return getConstantFP(minnum(C1, C2), DL, VT);
6757 case ISD::FMAXNUM:
6758 return getConstantFP(maxnum(C1, C2), DL, VT);
6759 case ISD::FMINIMUM:
6760 return getConstantFP(minimum(C1, C2), DL, VT);
6761 case ISD::FMAXIMUM:
6762 return getConstantFP(maximum(C1, C2), DL, VT);
6763 default: break;
6764 }
6765 }
6766 if (N1CFP && Opcode == ISD::FP_ROUND) {
6767 APFloat C1 = N1CFP->getValueAPF(); // make copy
6768 bool Unused;
6769 // This can return overflow, underflow, or inexact; we don't care.
6770 // FIXME need to be more flexible about rounding mode.
6772 &Unused);
6773 return getConstantFP(C1, DL, VT);
6774 }
6775
6776 switch (Opcode) {
6777 case ISD::FSUB:
6778 // -0.0 - undef --> undef (consistent with "fneg undef")
6779 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
6780 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
6781 return getUNDEF(VT);
6782 [[fallthrough]];
6783
6784 case ISD::FADD:
6785 case ISD::FMUL:
6786 case ISD::FDIV:
6787 case ISD::FREM:
6788 // If both operands are undef, the result is undef. If 1 operand is undef,
6789 // the result is NaN. This should match the behavior of the IR optimizer.
6790 if (N1.isUndef() && N2.isUndef())
6791 return getUNDEF(VT);
6792 if (N1.isUndef() || N2.isUndef())
6794 }
6795 return SDValue();
6796}
6797
6799 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
6800
6801 // There's no need to assert on a byte-aligned pointer. All pointers are at
6802 // least byte aligned.
6803 if (A == Align(1))
6804 return Val;
6805
6806 SDVTList VTs = getVTList(Val.getValueType());
6808 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
6809 ID.AddInteger(A.value());
6810
6811 void *IP = nullptr;
6812 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6813 return SDValue(E, 0);
6814
6815 auto *N =
6816 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
6817 createOperands(N, {Val});
6818
6819 CSEMap.InsertNode(N, IP);
6820 InsertNode(N);
6821
6822 SDValue V(N, 0);
6823 NewSDValueDbgMsg(V, "Creating new node: ", this);
6824 return V;
6825}
6826
6827SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6828 SDValue N1, SDValue N2) {
6829 SDNodeFlags Flags;
6830 if (Inserter)
6831 Flags = Inserter->getFlags();
6832 return getNode(Opcode, DL, VT, N1, N2, Flags);
6833}
6834
6836 SDValue &N2) const {
6837 if (!TLI->isCommutativeBinOp(Opcode))
6838 return;
6839
6840 // Canonicalize:
6841 // binop(const, nonconst) -> binop(nonconst, const)
6846 if ((N1C && !N2C) || (N1CFP && !N2CFP))
6847 std::swap(N1, N2);
6848
6849 // Canonicalize:
6850 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
6851 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
6853 std::swap(N1, N2);
6854}
6855
6856SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6857 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
6859 N2.getOpcode() != ISD::DELETED_NODE &&
6860 "Operand is DELETED_NODE!");
6861
6862 canonicalizeCommutativeBinop(Opcode, N1, N2);
6863
6864 auto *N1C = dyn_cast<ConstantSDNode>(N1);
6865 auto *N2C = dyn_cast<ConstantSDNode>(N2);
6866
6867 // Don't allow undefs in vector splats - we might be returning N2 when folding
6868 // to zero etc.
6869 ConstantSDNode *N2CV =
6870 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
6871
6872 switch (Opcode) {
6873 default: break;
6874 case ISD::TokenFactor:
6875 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
6876 N2.getValueType() == MVT::Other && "Invalid token factor!");
6877 // Fold trivial token factors.
6878 if (N1.getOpcode() == ISD::EntryToken) return N2;
6879 if (N2.getOpcode() == ISD::EntryToken) return N1;
6880 if (N1 == N2) return N1;
6881 break;
6882 case ISD::BUILD_VECTOR: {
6883 // Attempt to simplify BUILD_VECTOR.
6884 SDValue Ops[] = {N1, N2};
6885 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6886 return V;
6887 break;
6888 }
6889 case ISD::CONCAT_VECTORS: {
6890 SDValue Ops[] = {N1, N2};
6891 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
6892 return V;
6893 break;
6894 }
6895 case ISD::AND:
6896 assert(VT.isInteger() && "This operator does not apply to FP types!");
6897 assert(N1.getValueType() == N2.getValueType() &&
6898 N1.getValueType() == VT && "Binary operator types must match!");
6899 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
6900 // worth handling here.
6901 if (N2CV && N2CV->isZero())
6902 return N2;
6903 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
6904 return N1;
6905 break;
6906 case ISD::OR:
6907 case ISD::XOR:
6908 case ISD::ADD:
6909 case ISD::SUB:
6910 assert(VT.isInteger() && "This operator does not apply to FP types!");
6911 assert(N1.getValueType() == N2.getValueType() &&
6912 N1.getValueType() == VT && "Binary operator types must match!");
6913 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
6914 // it's worth handling here.
6915 if (N2CV && N2CV->isZero())
6916 return N1;
6917 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
6918 VT.getVectorElementType() == MVT::i1)
6919 return getNode(ISD::XOR, DL, VT, N1, N2);
6920 break;
6921 case ISD::MUL:
6922 assert(VT.isInteger() && "This operator does not apply to FP types!");
6923 assert(N1.getValueType() == N2.getValueType() &&
6924 N1.getValueType() == VT && "Binary operator types must match!");
6925 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6926 return getNode(ISD::AND, DL, VT, N1, N2);
6927 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6928 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6929 const APInt &N2CImm = N2C->getAPIntValue();
6930 return getVScale(DL, VT, MulImm * N2CImm);
6931 }
6932 break;
6933 case ISD::UDIV:
6934 case ISD::UREM:
6935 case ISD::MULHU:
6936 case ISD::MULHS:
6937 case ISD::SDIV:
6938 case ISD::SREM:
6939 case ISD::SADDSAT:
6940 case ISD::SSUBSAT:
6941 case ISD::UADDSAT:
6942 case ISD::USUBSAT:
6943 assert(VT.isInteger() && "This operator does not apply to FP types!");
6944 assert(N1.getValueType() == N2.getValueType() &&
6945 N1.getValueType() == VT && "Binary operator types must match!");
6946 if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
6947 // fold (add_sat x, y) -> (or x, y) for bool types.
6948 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
6949 return getNode(ISD::OR, DL, VT, N1, N2);
6950 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
6951 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
6952 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
6953 }
6954 break;
6955 case ISD::AVGFLOORS:
6956 case ISD::AVGFLOORU:
6957 case ISD::AVGCEILS:
6958 case ISD::AVGCEILU:
6959 assert(VT.isInteger() && "This operator does not apply to FP types!");
6960 assert(N1.getValueType() == N2.getValueType() &&
6961 N1.getValueType() == VT && "Binary operator types must match!");
6962 break;
6963 case ISD::ABDS:
6964 case ISD::ABDU:
6965 assert(VT.isInteger() && "This operator does not apply to FP types!");
6966 assert(N1.getValueType() == N2.getValueType() &&
6967 N1.getValueType() == VT && "Binary operator types must match!");
6968 break;
6969 case ISD::SMIN:
6970 case ISD::UMAX:
6971 assert(VT.isInteger() && "This operator does not apply to FP types!");
6972 assert(N1.getValueType() == N2.getValueType() &&
6973 N1.getValueType() == VT && "Binary operator types must match!");
6974 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6975 return getNode(ISD::OR, DL, VT, N1, N2);
6976 break;
6977 case ISD::SMAX:
6978 case ISD::UMIN:
6979 assert(VT.isInteger() && "This operator does not apply to FP types!");
6980 assert(N1.getValueType() == N2.getValueType() &&
6981 N1.getValueType() == VT && "Binary operator types must match!");
6982 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6983 return getNode(ISD::AND, DL, VT, N1, N2);
6984 break;
6985 case ISD::FADD:
6986 case ISD::FSUB:
6987 case ISD::FMUL:
6988 case ISD::FDIV:
6989 case ISD::FREM:
6990 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
6991 assert(N1.getValueType() == N2.getValueType() &&
6992 N1.getValueType() == VT && "Binary operator types must match!");
6993 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
6994 return V;
6995 break;
6996 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
6997 assert(N1.getValueType() == VT &&
7000 "Invalid FCOPYSIGN!");
7001 break;
7002 case ISD::SHL:
7003 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7004 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7005 const APInt &ShiftImm = N2C->getAPIntValue();
7006 return getVScale(DL, VT, MulImm << ShiftImm);
7007 }
7008 [[fallthrough]];
7009 case ISD::SRA:
7010 case ISD::SRL:
7011 if (SDValue V = simplifyShift(N1, N2))
7012 return V;
7013 [[fallthrough]];
7014 case ISD::ROTL:
7015 case ISD::ROTR:
7016 assert(VT == N1.getValueType() &&
7017 "Shift operators return type must be the same as their first arg");
7018 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7019 "Shifts only work on integers");
7020 assert((!VT.isVector() || VT == N2.getValueType()) &&
7021 "Vector shift amounts must be in the same as their first arg");
7022 // Verify that the shift amount VT is big enough to hold valid shift
7023 // amounts. This catches things like trying to shift an i1024 value by an
7024 // i8, which is easy to fall into in generic code that uses
7025 // TLI.getShiftAmount().
7028 "Invalid use of small shift amount with oversized value!");
7029
7030 // Always fold shifts of i1 values so the code generator doesn't need to
7031 // handle them. Since we know the size of the shift has to be less than the
7032 // size of the value, the shift/rotate count is guaranteed to be zero.
7033 if (VT == MVT::i1)
7034 return N1;
7035 if (N2CV && N2CV->isZero())
7036 return N1;
7037 break;
7038 case ISD::FP_ROUND:
7039 assert(VT.isFloatingPoint() &&
7041 VT.bitsLE(N1.getValueType()) &&
7042 N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7043 "Invalid FP_ROUND!");
7044 if (N1.getValueType() == VT) return N1; // noop conversion.
7045 break;
7046 case ISD::AssertSext:
7047 case ISD::AssertZext: {
7048 EVT EVT = cast<VTSDNode>(N2)->getVT();
7049 assert(VT == N1.getValueType() && "Not an inreg extend!");
7050 assert(VT.isInteger() && EVT.isInteger() &&
7051 "Cannot *_EXTEND_INREG FP types");
7052 assert(!EVT.isVector() &&
7053 "AssertSExt/AssertZExt type should be the vector element type "
7054 "rather than the vector type!");
7055 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7056 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7057 break;
7058 }
7060 EVT EVT = cast<VTSDNode>(N2)->getVT();
7061 assert(VT == N1.getValueType() && "Not an inreg extend!");
7062 assert(VT.isInteger() && EVT.isInteger() &&
7063 "Cannot *_EXTEND_INREG FP types");
7064 assert(EVT.isVector() == VT.isVector() &&
7065 "SIGN_EXTEND_INREG type should be vector iff the operand "
7066 "type is vector!");
7067 assert((!EVT.isVector() ||
7069 "Vector element counts must match in SIGN_EXTEND_INREG");
7070 assert(EVT.bitsLE(VT) && "Not extending!");
7071 if (EVT == VT) return N1; // Not actually extending
7072
7073 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7074 unsigned FromBits = EVT.getScalarSizeInBits();
7075 Val <<= Val.getBitWidth() - FromBits;
7076 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7077 return getConstant(Val, DL, ConstantVT);
7078 };
7079
7080 if (N1C) {
7081 const APInt &Val = N1C->getAPIntValue();
7082 return SignExtendInReg(Val, VT);
7083 }
7084
7087 llvm::EVT OpVT = N1.getOperand(0).getValueType();
7088 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7089 SDValue Op = N1.getOperand(i);
7090 if (Op.isUndef()) {
7091 Ops.push_back(getUNDEF(OpVT));
7092 continue;
7093 }
7094 ConstantSDNode *C = cast<ConstantSDNode>(Op);
7095 APInt Val = C->getAPIntValue();
7096 Ops.push_back(SignExtendInReg(Val, OpVT));
7097 }
7098 return getBuildVector(VT, DL, Ops);
7099 }
7100
7101 if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7102 isa<ConstantSDNode>(N1.getOperand(0)))
7103 return getNode(
7104 ISD::SPLAT_VECTOR, DL, VT,
7105 SignExtendInReg(N1.getConstantOperandAPInt(0),
7106 N1.getOperand(0).getValueType()));
7107 break;
7108 }
7110 case ISD::FP_TO_UINT_SAT: {
7111 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7112 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7113 assert(N1.getValueType().isVector() == VT.isVector() &&
7114 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7115 "vector!");
7116 assert((!VT.isVector() || VT.getVectorElementCount() ==
7118 "Vector element counts must match in FP_TO_*INT_SAT");
7119 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7120 "Type to saturate to must be a scalar.");
7121 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7122 "Not extending!");
7123 break;
7124 }
7127 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7128 element type of the vector.");
7129
7130 // Extract from an undefined value or using an undefined index is undefined.
7131 if (N1.isUndef() || N2.isUndef())
7132 return getUNDEF(VT);
7133
7134 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7135 // vectors. For scalable vectors we will provide appropriate support for
7136 // dealing with arbitrary indices.
7137 if (N2C && N1.getValueType().isFixedLengthVector() &&
7138 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7139 return getUNDEF(VT);
7140
7141 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7142 // expanding copies of large vectors from registers. This only works for
7143 // fixed length vectors, since we need to know the exact number of
7144 // elements.
7145 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7147 unsigned Factor =
7150 N1.getOperand(N2C->getZExtValue() / Factor),
7151 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
7152 }
7153
7154 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7155 // lowering is expanding large vector constants.
7156 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7157 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7160 "BUILD_VECTOR used for scalable vectors");
7161 unsigned Index =
7162 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7163 SDValue Elt = N1.getOperand(Index);
7164
7165 if (VT != Elt.getValueType())
7166 // If the vector element type is not legal, the BUILD_VECTOR operands
7167 // are promoted and implicitly truncated, and the result implicitly
7168 // extended. Make that explicit here.
7169 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7170
7171 return Elt;
7172 }
7173
7174 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7175 // operations are lowered to scalars.
7176 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7177 // If the indices are the same, return the inserted element else
7178 // if the indices are known different, extract the element from
7179 // the original vector.
7180 SDValue N1Op2 = N1.getOperand(2);
7181 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7182
7183 if (N1Op2C && N2C) {
7184 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7185 if (VT == N1.getOperand(1).getValueType())
7186 return N1.getOperand(1);
7187 if (VT.isFloatingPoint()) {
7189 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7190 }
7191 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7192 }
7193 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7194 }
7195 }
7196
7197 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7198 // when vector types are scalarized and v1iX is legal.
7199 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7200 // Here we are completely ignoring the extract element index (N2),
7201 // which is fine for fixed width vectors, since any index other than 0
7202 // is undefined anyway. However, this cannot be ignored for scalable
7203 // vectors - in theory we could support this, but we don't want to do this
7204 // without a profitability check.
7205 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7207 N1.getValueType().getVectorNumElements() == 1) {
7208 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7209 N1.getOperand(1));
7210 }
7211 break;
7213 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7214 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7215 (N1.getValueType().isInteger() == VT.isInteger()) &&
7216 N1.getValueType() != VT &&
7217 "Wrong types for EXTRACT_ELEMENT!");
7218
7219 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7220 // 64-bit integers into 32-bit parts. Instead of building the extract of
7221 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7222 if (N1.getOpcode() == ISD::BUILD_PAIR)
7223 return N1.getOperand(N2C->getZExtValue());
7224
7225 // EXTRACT_ELEMENT of a constant int is also very common.
7226 if (N1C) {
7227 unsigned ElementSize = VT.getSizeInBits();
7228 unsigned Shift = ElementSize * N2C->getZExtValue();
7229 const APInt &Val = N1C->getAPIntValue();
7230 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7231 }
7232 break;
7234 EVT N1VT = N1.getValueType();
7235 assert(VT.isVector() && N1VT.isVector() &&
7236 "Extract subvector VTs must be vectors!");
7238 "Extract subvector VTs must have the same element type!");
7239 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7240 "Cannot extract a scalable vector from a fixed length vector!");
7241 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7243 "Extract subvector must be from larger vector to smaller vector!");
7244 assert(N2C && "Extract subvector index must be a constant");
7245 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7246 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7247 N1VT.getVectorMinNumElements()) &&
7248 "Extract subvector overflow!");
7249 assert(N2C->getAPIntValue().getBitWidth() ==
7250 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7251 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7252
7253 // Trivial extraction.
7254 if (VT == N1VT)
7255 return N1;
7256
7257 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7258 if (N1.isUndef())
7259 return getUNDEF(VT);
7260
7261 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7262 // the concat have the same type as the extract.
7263 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7264 VT == N1.getOperand(0).getValueType()) {
7265 unsigned Factor = VT.getVectorMinNumElements();
7266 return N1.getOperand(N2C->getZExtValue() / Factor);
7267 }
7268
7269 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7270 // during shuffle legalization.
7271 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7272 VT == N1.getOperand(1).getValueType())
7273 return N1.getOperand(1);
7274 break;
7275 }
7276 }
7277
7278 // Perform trivial constant folding.
7279 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
7280 return SV;
7281
7282 // Canonicalize an UNDEF to the RHS, even over a constant.
7283 if (N1.isUndef()) {
7284 if (TLI->isCommutativeBinOp(Opcode)) {
7285 std::swap(N1, N2);
7286 } else {
7287 switch (Opcode) {
7288 case ISD::SUB:
7289 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7291 case ISD::UDIV:
7292 case ISD::SDIV:
7293 case ISD::UREM:
7294 case ISD::SREM:
7295 case ISD::SSUBSAT:
7296 case ISD::USUBSAT:
7297 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7298 }
7299 }
7300 }
7301
7302 // Fold a bunch of operators when the RHS is undef.
7303 if (N2.isUndef()) {
7304 switch (Opcode) {
7305 case ISD::XOR:
7306 if (N1.isUndef())
7307 // Handle undef ^ undef -> 0 special case. This is a common
7308 // idiom (misuse).
7309 return getConstant(0, DL, VT);
7310 [[fallthrough]];
7311 case ISD::ADD:
7312 case ISD::SUB:
7313 case ISD::UDIV:
7314 case ISD::SDIV:
7315 case ISD::UREM:
7316 case ISD::SREM:
7317 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7318 case ISD::MUL:
7319 case ISD::AND:
7320 case ISD::SSUBSAT:
7321 case ISD::USUBSAT:
7322 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7323 case ISD::OR:
7324 case ISD::SADDSAT:
7325 case ISD::UADDSAT:
7326 return getAllOnesConstant(DL, VT);
7327 }
7328 }
7329
7330 // Memoize this node if possible.
7331 SDNode *N;
7332 SDVTList VTs = getVTList(VT);
7333 SDValue Ops[] = {N1, N2};
7334 if (VT != MVT::Glue) {
7336 AddNodeIDNode(ID, Opcode, VTs, Ops);
7337 void *IP = nullptr;
7338 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7339 E->intersectFlagsWith(Flags);
7340 return SDValue(E, 0);
7341 }
7342
7343 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7344 N->setFlags(Flags);
7345 createOperands(N, Ops);
7346 CSEMap.InsertNode(N, IP);
7347 } else {
7348 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7349 createOperands(N, Ops);
7350 }
7351
7352 InsertNode(N);
7353 SDValue V = SDValue(N, 0);
7354 NewSDValueDbgMsg(V, "Creating new node: ", this);
7355 return V;
7356}
7357
7358SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7359 SDValue N1, SDValue N2, SDValue N3) {
7360 SDNodeFlags Flags;
7361 if (Inserter)
7362 Flags = Inserter->getFlags();
7363 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7364}
7365
7366SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7367 SDValue N1, SDValue N2, SDValue N3,
7368 const SDNodeFlags Flags) {
7370 N2.getOpcode() != ISD::DELETED_NODE &&
7371 N3.getOpcode() != ISD::DELETED_NODE &&
7372 "Operand is DELETED_NODE!");
7373 // Perform various simplifications.
7374 switch (Opcode) {
7375 case ISD::FMA:
7376 case ISD::FMAD: {
7377 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7378 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7379 N3.getValueType() == VT && "FMA types must match!");
7380 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7381 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7382 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7383 if (N1CFP && N2CFP && N3CFP) {
7384 APFloat V1 = N1CFP->getValueAPF();
7385 const APFloat &V2 = N2CFP->getValueAPF();
7386 const APFloat &V3 = N3CFP->getValueAPF();
7387 if (Opcode == ISD::FMAD) {
7390 } else
7392 return getConstantFP(V1, DL, VT);
7393 }
7394 break;
7395 }
7396 case ISD::BUILD_VECTOR: {
7397 // Attempt to simplify BUILD_VECTOR.
7398 SDValue Ops[] = {N1, N2, N3};
7399 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7400 return V;
7401 break;
7402 }
7403 case ISD::CONCAT_VECTORS: {
7404 SDValue Ops[] = {N1, N2, N3};
7405 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7406 return V;
7407 break;
7408 }
7409 case ISD::SETCC: {
7410 assert(VT.isInteger() && "SETCC result type must be an integer!");
7411 assert(N1.getValueType() == N2.getValueType() &&
7412 "SETCC operands must have the same type!");
7413 assert(VT.isVector() == N1.getValueType().isVector() &&
7414 "SETCC type should be vector iff the operand type is vector!");
7415 assert((!VT.isVector() || VT.getVectorElementCount() ==
7417 "SETCC vector element counts must match!");
7418 // Use FoldSetCC to simplify SETCC's.
7419 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7420 return V;
7421 // Vector constant folding.
7422 SDValue Ops[] = {N1, N2, N3};
7423 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7424 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7425 return V;
7426 }
7427 break;
7428 }
7429 case ISD::SELECT:
7430 case ISD::VSELECT:
7431 if (SDValue V = simplifySelect(N1, N2, N3))
7432 return V;
7433 break;
7435 llvm_unreachable("should use getVectorShuffle constructor!");
7436 case ISD::VECTOR_SPLICE: {
7437 if (cast<ConstantSDNode>(N3)->isZero())
7438 return N1;
7439 break;
7440 }
7442 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7443 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7444 // for scalable vectors where we will generate appropriate code to
7445 // deal with out-of-bounds cases correctly.
7446 if (N3C && N1.getValueType().isFixedLengthVector() &&
7448 return getUNDEF(VT);
7449
7450 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7451 if (N3.isUndef())
7452 return getUNDEF(VT);
7453
7454 // If the inserted element is an UNDEF, just use the input vector.
7455 if (N2.isUndef())
7456 return N1;
7457
7458 break;
7459 }
7460 case ISD::INSERT_SUBVECTOR: {
7461 // Inserting undef into undef is still undef.
7462 if (N1.isUndef() && N2.isUndef())
7463 return getUNDEF(VT);
7464
7465 EVT N2VT = N2.getValueType();
7466 assert(VT == N1.getValueType() &&
7467 "Dest and insert subvector source types must match!");
7468 assert(VT.isVector() && N2VT.isVector() &&
7469 "Insert subvector VTs must be vectors!");
7471 "Insert subvector VTs must have the same element type!");
7472 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7473 "Cannot insert a scalable vector into a fixed length vector!");
7474 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7476 "Insert subvector must be from smaller vector to larger vector!");
7477 assert(isa<ConstantSDNode>(N3) &&
7478 "Insert subvector index must be constant");
7479 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7480 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7482 "Insert subvector overflow!");
7484 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7485 "Constant index for INSERT_SUBVECTOR has an invalid size");
7486
7487 // Trivial insertion.
7488 if (VT == N2VT)
7489 return N2;
7490
7491 // If this is an insert of an extracted vector into an undef vector, we
7492 // can just use the input to the extract.
7493 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7494 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7495 return N2.getOperand(0);
7496 break;
7497 }
7498 case ISD::BITCAST:
7499 // Fold bit_convert nodes from a type to themselves.
7500 if (N1.getValueType() == VT)
7501 return N1;
7502 break;
7503 case ISD::VP_TRUNCATE:
7504 case ISD::VP_SIGN_EXTEND:
7505 case ISD::VP_ZERO_EXTEND:
7506 // Don't create noop casts.
7507 if (N1.getValueType() == VT)
7508 return N1;
7509 break;
7510 }
7511
7512 // Memoize node if it doesn't produce a glue result.
7513 SDNode *N;
7514 SDVTList VTs = getVTList(VT);
7515 SDValue Ops[] = {N1, N2, N3};
7516 if (VT != MVT::Glue) {
7518 AddNodeIDNode(ID, Opcode, VTs, Ops);
7519 void *IP = nullptr;
7520 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7521 E->intersectFlagsWith(Flags);
7522 return SDValue(E, 0);
7523 }
7524
7525 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7526 N->setFlags(Flags);
7527 createOperands(N, Ops);
7528 CSEMap.InsertNode(N, IP);
7529 } else {
7530 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7531 createOperands(N, Ops);
7532 }
7533
7534 InsertNode(N);
7535 SDValue V = SDValue(N, 0);
7536 NewSDValueDbgMsg(V, "Creating new node: ", this);
7537 return V;
7538}
7539
7540SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7541 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7542 SDValue Ops[] = { N1, N2, N3, N4 };
7543 return getNode(Opcode, DL, VT, Ops);
7544}
7545
7546SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7547 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7548 SDValue N5) {
7549 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7550 return getNode(Opcode, DL, VT, Ops);
7551}
7552
7553/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7554/// the incoming stack arguments to be loaded from the stack.
7556 SmallVector<SDValue, 8> ArgChains;
7557
7558 // Include the original chain at the beginning of the list. When this is
7559 // used by target LowerCall hooks, this helps legalize find the
7560 // CALLSEQ_BEGIN node.
7561 ArgChains.push_back(Chain);
7562
7563 // Add a chain value for each stack argument.
7564 for (SDNode *U : getEntryNode().getNode()->uses())
7565 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7566 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7567 if (FI->getIndex() < 0)
7568 ArgChains.push_back(SDValue(L, 1));
7569
7570 // Build a tokenfactor for all the chains.
7571 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7572}
7573
7574/// getMemsetValue - Vectorized representation of the memset value
7575/// operand.
7577 const SDLoc &dl) {
7578 assert(!Value.isUndef());
7579
7580 unsigned NumBits = VT.getScalarSizeInBits();
7581 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7582 assert(C->getAPIntValue().getBitWidth() == 8);
7583 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7584 if (VT.isInteger()) {
7585 bool IsOpaque = VT.getSizeInBits() > 64 ||
7586 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7587 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7588 }
7589 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
7590 VT);
7591 }
7592
7593 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7594 EVT IntVT = VT.getScalarType();
7595 if (!IntVT.isInteger())
7596 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7597
7598 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7599 if (NumBits > 8) {
7600 // Use a multiplication with 0x010101... to extend the input to the
7601 // required length.
7602 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7603 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7604 DAG.getConstant(Magic, dl, IntVT));
7605 }
7606
7607 if (VT != Value.getValueType() && !VT.isInteger())
7608 Value = DAG.getBitcast(VT.getScalarType(), Value);
7609 if (VT != Value.getValueType())
7610 Value = DAG.getSplatBuildVector(VT, dl, Value);
7611
7612 return Value;
7613}
7614
7615/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
7616/// used when a memcpy is turned into a memset when the source is a constant
7617/// string ptr.
7619 const TargetLowering &TLI,
7620 const ConstantDataArraySlice &Slice) {
7621 // Handle vector with all elements zero.
7622 if (Slice.Array == nullptr) {
7623 if (VT.isInteger())
7624 return DAG.getConstant(0, dl, VT);
7625 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
7626 return DAG.getConstantFP(0.0, dl, VT);
7627 if (VT.isVector()) {
7628 unsigned NumElts = VT.getVectorNumElements();
7629 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
7630 return DAG.getNode(ISD::BITCAST, dl, VT,
7631 DAG.getConstant(0, dl,
7633 EltVT, NumElts)));
7634 }
7635 llvm_unreachable("Expected type!");
7636 }
7637
7638 assert(!VT.isVector() && "Can't handle vector type here!");
7639 unsigned NumVTBits = VT.getSizeInBits();
7640 unsigned NumVTBytes = NumVTBits / 8;
7641 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
7642
7643 APInt Val(NumVTBits, 0);
7644 if (DAG.getDataLayout().isLittleEndian()) {
7645 for (unsigned i = 0; i != NumBytes; ++i)
7646 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
7647 } else {
7648 for (unsigned i = 0; i != NumBytes; ++i)
7649 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
7650 }
7651
7652 // If the "cost" of materializing the integer immediate is less than the cost
7653 // of a load, then it is cost effective to turn the load into the immediate.
7654 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
7655 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
7656 return DAG.getConstant(Val, dl, VT);
7657 return SDValue();
7658}
7659
7661 const SDLoc &DL,
7662 const SDNodeFlags Flags) {
7663 EVT VT = Base.getValueType();
7664 SDValue Index;
7665
7666 if (Offset.isScalable())
7667 Index = getVScale(DL, Base.getValueType(),
7668 APInt(Base.getValueSizeInBits().getFixedValue(),
7669 Offset.getKnownMinValue()));
7670 else
7671 Index = getConstant(Offset.getFixedValue(), DL, VT);
7672
7673 return getMemBasePlusOffset(Base, Index, DL, Flags);
7674}
7675
7677 const SDLoc &DL,
7678 const SDNodeFlags Flags) {
7679 assert(Offset.getValueType().isInteger());
7680 EVT BasePtrVT = Ptr.getValueType();
7681 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
7682}
7683
7684/// Returns true if memcpy source is constant data.
7686 uint64_t SrcDelta = 0;
7687 GlobalAddressSDNode *G = nullptr;
7688 if (Src.getOpcode() == ISD::GlobalAddress)
7689 G = cast<GlobalAddressSDNode>(Src);
7690 else if (Src.getOpcode() == ISD::ADD &&
7691 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
7692 Src.getOperand(1).getOpcode() == ISD::Constant) {
7693 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
7694 SrcDelta = Src.getConstantOperandVal(1);
7695 }
7696 if (!G)
7697 return false;
7698
7699 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
7700 SrcDelta + G->getOffset());
7701}
7702
7704 SelectionDAG &DAG) {
7705 // On Darwin, -Os means optimize for size without hurting performance, so
7706 // only really optimize for size when -Oz (MinSize) is used.
7708 return MF.getFunction().hasMinSize();
7709 return DAG.shouldOptForSize();
7710}
7711
7713 SmallVector<SDValue, 32> &OutChains, unsigned From,
7714 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
7715 SmallVector<SDValue, 16> &OutStoreChains) {
7716 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
7717 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
7718 SmallVector<SDValue, 16> GluedLoadChains;
7719 for (unsigned i = From; i < To; ++i) {
7720 OutChains.push_back(OutLoadChains[i]);
7721 GluedLoadChains.push_back(OutLoadChains[i]);
7722 }
7723
7724 // Chain for all loads.
7725 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7726 GluedLoadChains);
7727
7728 for (unsigned i = From; i < To; ++i) {
7729 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
7730 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
7731 ST->getBasePtr(), ST->getMemoryVT(),
7732 ST->getMemOperand());
7733 OutChains.push_back(NewStore);
7734 }
7735}
7736
7738 SDValue Chain, SDValue Dst, SDValue Src,
7739 uint64_t Size, Align Alignment,
7740 bool isVol, bool AlwaysInline,
7741 MachinePointerInfo DstPtrInfo,
7742 MachinePointerInfo SrcPtrInfo,
7743 const AAMDNodes &AAInfo, AAResults *AA) {
7744 // Turn a memcpy of undef to nop.
7745 // FIXME: We need to honor volatile even is Src is undef.
7746 if (Src.isUndef())
7747 return Chain;
7748
7749 // Expand memcpy to a series of load and store ops if the size operand falls
7750 // below a certain threshold.
7751 // TODO: In the AlwaysInline case, if the size is big then generate a loop
7752 // rather than maybe a humongous number of loads and stores.
7753 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7754 const DataLayout &DL = DAG.getDataLayout();
7755 LLVMContext &C = *DAG.getContext();
7756 std::vector<EVT> MemOps;
7757 bool DstAlignCanChange = false;
7759 MachineFrameInfo &MFI = MF.getFrameInfo();
7760 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7761 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7762 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7763 DstAlignCanChange = true;
7764 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7765 if (!SrcAlign || Alignment > *SrcAlign)
7766 SrcAlign = Alignment;
7767 assert(SrcAlign && "SrcAlign must be set");
7769 // If marked as volatile, perform a copy even when marked as constant.
7770 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
7771 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
7772 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
7773 const MemOp Op = isZeroConstant
7774 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
7775 /*IsZeroMemset*/ true, isVol)
7776 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
7777 *SrcAlign, isVol, CopyFromConstant);
7778 if (!TLI.findOptimalMemOpLowering(
7779 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
7780 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
7781 return SDValue();
7782
7783 if (DstAlignCanChange) {
7784 Type *Ty = MemOps[0].getTypeForEVT(C);
7785 Align NewAlign = DL.getABITypeAlign(Ty);
7786
7787 // Don't promote to an alignment that would require dynamic stack
7788 // realignment which may conflict with optimizations such as tail call
7789 // optimization.
7791 if (!TRI->hasStackRealignment(MF))
7792 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7793 NewAlign = NewAlign.previous();
7794
7795 if (NewAlign > Alignment) {
7796 // Give the stack frame object a larger alignment if needed.
7797 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7798 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7799 Alignment = NewAlign;
7800 }
7801 }
7802
7803 // Prepare AAInfo for loads/stores after lowering this memcpy.
7804 AAMDNodes NewAAInfo = AAInfo;
7805 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7806
7807 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
7808 bool isConstant =
7809 AA && SrcVal &&
7810 AA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
7811
7812 MachineMemOperand::Flags MMOFlags =
7814 SmallVector<SDValue, 16> OutLoadChains;
7815 SmallVector<SDValue, 16> OutStoreChains;
7816 SmallVector<SDValue, 32> OutChains;
7817 unsigned NumMemOps = MemOps.size();
7818 uint64_t SrcOff = 0, DstOff = 0;
7819 for (unsigned i = 0; i != NumMemOps; ++i) {
7820 EVT VT = MemOps[i];
7821 unsigned VTSize = VT.getSizeInBits() / 8;
7822 SDValue Value, Store;
7823
7824 if (VTSize > Size) {
7825 // Issuing an unaligned load / store pair that overlaps with the previous
7826 // pair. Adjust the offset accordingly.
7827 assert(i == NumMemOps-1 && i != 0);
7828 SrcOff -= VTSize - Size;
7829 DstOff -= VTSize - Size;
7830 }
7831
7832 if (CopyFromConstant &&
7833 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
7834 // It's unlikely a store of a vector immediate can be done in a single
7835 // instruction. It would require a load from a constantpool first.
7836 // We only handle zero vectors here.
7837 // FIXME: Handle other cases where store of vector immediate is done in
7838 // a single instruction.
7839 ConstantDataArraySlice SubSlice;
7840 if (SrcOff < Slice.Length) {
7841 SubSlice = Slice;
7842 SubSlice.move(SrcOff);
7843 } else {
7844 // This is an out-of-bounds access and hence UB. Pretend we read zero.
7845 SubSlice.Array = nullptr;
7846 SubSlice.Offset = 0;
7847 SubSlice.Length = VTSize;
7848 }
7849 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
7850 if (Value.getNode()) {
7851 Store = DAG.getStore(
7852 Chain, dl, Value,
7853 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7854 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7855 OutChains.push_back(Store);
7856 }
7857 }
7858
7859 if (!Store.getNode()) {
7860 // The type might not be legal for the target. This should only happen
7861 // if the type is smaller than a legal type, as on PPC, so the right
7862 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
7863 // to Load/Store if NVT==VT.
7864 // FIXME does the case above also need this?
7865 EVT NVT = TLI.getTypeToTransformTo(C, VT);
7866 assert(NVT.bitsGE(VT));
7867
7868 bool isDereferenceable =
7869 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7870 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7871 if (isDereferenceable)
7873 if (isConstant)
7874 SrcMMOFlags |= MachineMemOperand::MOInvariant;
7875
7876 Value = DAG.getExtLoad(
7877 ISD::EXTLOAD, dl, NVT, Chain,
7878 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7879 SrcPtrInfo.getWithOffset(SrcOff), VT,
7880 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
7881 OutLoadChains.push_back(Value.getValue(1));
7882
7883 Store = DAG.getTruncStore(
7884 Chain, dl, Value,
7885 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7886 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
7887 OutStoreChains.push_back(Store);
7888 }
7889 SrcOff += VTSize;
7890 DstOff += VTSize;
7891 Size -= VTSize;
7892 }
7893
7894 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
7896 unsigned NumLdStInMemcpy = OutStoreChains.size();
7897
7898 if (NumLdStInMemcpy) {
7899 // It may be that memcpy might be converted to memset if it's memcpy
7900 // of constants. In such a case, we won't have loads and stores, but
7901 // just stores. In the absence of loads, there is nothing to gang up.
7902 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
7903 // If target does not care, just leave as it.
7904 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
7905 OutChains.push_back(OutLoadChains[i]);
7906 OutChains.push_back(OutStoreChains[i]);
7907 }
7908 } else {
7909 // Ld/St less than/equal limit set by target.
7910 if (NumLdStInMemcpy <= GluedLdStLimit) {
7911 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7912 NumLdStInMemcpy, OutLoadChains,
7913 OutStoreChains);
7914 } else {
7915 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
7916 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
7917 unsigned GlueIter = 0;
7918
7919 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
7920 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
7921 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
7922
7923 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
7924 OutLoadChains, OutStoreChains);
7925 GlueIter += GluedLdStLimit;
7926 }
7927
7928 // Residual ld/st.
7929 if (RemainingLdStInMemcpy) {
7930 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7931 RemainingLdStInMemcpy, OutLoadChains,
7932 OutStoreChains);
7933 }
7934 }
7935 }
7936 }
7937 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
7938}
7939
7941 SDValue Chain, SDValue Dst, SDValue Src,
7942 uint64_t Size, Align Alignment,
7943 bool isVol, bool AlwaysInline,
7944 MachinePointerInfo DstPtrInfo,
7945 MachinePointerInfo SrcPtrInfo,
7946 const AAMDNodes &AAInfo) {
7947 // Turn a memmove of undef to nop.
7948 // FIXME: We need to honor volatile even is Src is undef.
7949 if (Src.isUndef())
7950 return Chain;
7951
7952 // Expand memmove to a series of load and store ops if the size operand falls
7953 // below a certain threshold.
7954 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7955 const DataLayout &DL = DAG.getDataLayout();
7956 LLVMContext &C = *DAG.getContext();
7957 std::vector<EVT> MemOps;
7958 bool DstAlignCanChange = false;
7960 MachineFrameInfo &MFI = MF.getFrameInfo();
7961 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7962 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7963 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7964 DstAlignCanChange = true;
7965 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7966 if (!SrcAlign || Alignment > *SrcAlign)
7967 SrcAlign = Alignment;
7968 assert(SrcAlign && "SrcAlign must be set");
7969 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
7970 if (!TLI.findOptimalMemOpLowering(
7971 MemOps, Limit,
7972 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
7973 /*IsVolatile*/ true),
7974 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
7975 MF.getFunction().getAttributes()))
7976 return SDValue();
7977
7978 if (DstAlignCanChange) {
7979 Type *Ty = MemOps[0].getTypeForEVT(C);
7980 Align NewAlign = DL.getABITypeAlign(Ty);
7981
7982 // Don't promote to an alignment that would require dynamic stack
7983 // realignment which may conflict with optimizations such as tail call
7984 // optimization.
7986 if (!TRI->hasStackRealignment(MF))
7987 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7988 NewAlign = NewAlign.previous();
7989
7990 if (NewAlign > Alignment) {
7991 // Give the stack frame object a larger alignment if needed.
7992 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7993 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7994 Alignment = NewAlign;
7995 }
7996 }
7997
7998 // Prepare AAInfo for loads/stores after lowering this memmove.
7999 AAMDNodes NewAAInfo = AAInfo;
8000 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8001
8002 MachineMemOperand::Flags MMOFlags =
8004 uint64_t SrcOff = 0, DstOff = 0;
8005 SmallVector<SDValue, 8> LoadValues;
8006 SmallVector<SDValue, 8> LoadChains;
8007 SmallVector<SDValue, 8> OutChains;
8008 unsigned NumMemOps = MemOps.size();
8009 for (unsigned i = 0; i < NumMemOps; i++) {
8010 EVT VT = MemOps[i];
8011 unsigned VTSize = VT.getSizeInBits() / 8;
8012 SDValue Value;
8013
8014 bool isDereferenceable =
8015 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8016 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8017 if (isDereferenceable)
8019
8020 Value = DAG.getLoad(
8021 VT, dl, Chain,
8022 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8023 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8024 LoadValues.push_back(Value);
8025 LoadChains.push_back(Value.getValue(1));
8026 SrcOff += VTSize;
8027 }
8028 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8029 OutChains.clear();
8030 for (unsigned i = 0; i < NumMemOps; i++) {
8031 EVT VT = MemOps[i];
8032 unsigned VTSize = VT.getSizeInBits() / 8;
8033 SDValue Store;
8034
8035 Store = DAG.getStore(
8036 Chain, dl, LoadValues[i],
8037 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8038 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8039 OutChains.push_back(Store);
8040 DstOff += VTSize;
8041 }
8042
8043 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8044}
8045
8046/// Lower the call to 'memset' intrinsic function into a series of store
8047/// operations.
8048///
8049/// \param DAG Selection DAG where lowered code is placed.
8050/// \param dl Link to corresponding IR location.
8051/// \param Chain Control flow dependency.
8052/// \param Dst Pointer to destination memory location.
8053/// \param Src Value of byte to write into the memory.
8054/// \param Size Number of bytes to write.
8055/// \param Alignment Alignment of the destination in bytes.
8056/// \param isVol True if destination is volatile.
8057/// \param AlwaysInline Makes sure no function call is generated.
8058/// \param DstPtrInfo IR information on the memory pointer.
8059/// \returns New head in the control flow, if lowering was successful, empty
8060/// SDValue otherwise.
8061///
8062/// The function tries to replace 'llvm.memset' intrinsic with several store
8063/// operations and value calculation code. This is usually profitable for small
8064/// memory size or when the semantic requires inlining.
8066 SDValue Chain, SDValue Dst, SDValue Src,
8067 uint64_t Size, Align Alignment, bool isVol,
8068 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8069 const AAMDNodes &AAInfo) {
8070 // Turn a memset of undef to nop.
8071 // FIXME: We need to honor volatile even is Src is undef.
8072 if (Src.isUndef())
8073 return Chain;
8074
8075 // Expand memset to a series of load/store ops if the size operand
8076 // falls below a certain threshold.
8077 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8078 std::vector<EVT> MemOps;
8079 bool DstAlignCanChange = false;
8081 MachineFrameInfo &MFI = MF.getFrameInfo();
8082 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8083 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8084 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8085 DstAlignCanChange = true;
8086 bool IsZeroVal = isNullConstant(Src);
8087 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8088
8089 if (!TLI.findOptimalMemOpLowering(
8090 MemOps, Limit,
8091 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8092 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8093 return SDValue();
8094
8095 if (DstAlignCanChange) {
8096 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8097 const DataLayout &DL = DAG.getDataLayout();
8098 Align NewAlign = DL.getABITypeAlign(Ty);
8099
8100 // Don't promote to an alignment that would require dynamic stack
8101 // realignment which may conflict with optimizations such as tail call
8102 // optimization.
8104 if (!TRI->hasStackRealignment(MF))
8105 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8106 NewAlign = NewAlign.previous();
8107
8108 if (NewAlign > Alignment) {
8109 // Give the stack frame object a larger alignment if needed.
8110 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8111 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8112 Alignment = NewAlign;
8113 }
8114 }
8115
8116 SmallVector<SDValue, 8> OutChains;
8117 uint64_t DstOff = 0;
8118 unsigned NumMemOps = MemOps.size();
8119
8120 // Find the largest store and generate the bit pattern for it.
8121 EVT LargestVT = MemOps[0];
8122 for (unsigned i = 1; i < NumMemOps; i++)
8123 if (MemOps[i].bitsGT(LargestVT))
8124 LargestVT = MemOps[i];
8125 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8126
8127 // Prepare AAInfo for loads/stores after lowering this memset.
8128 AAMDNodes NewAAInfo = AAInfo;
8129 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8130
8131 for (unsigned i = 0; i < NumMemOps; i++) {
8132 EVT VT = MemOps[i];
8133 unsigned VTSize = VT.getSizeInBits() / 8;
8134 if (VTSize > Size) {
8135 // Issuing an unaligned load / store pair that overlaps with the previous
8136 // pair. Adjust the offset accordingly.
8137 assert(i == NumMemOps-1 && i != 0);
8138 DstOff -= VTSize - Size;
8139 }
8140
8141 // If this store is smaller than the largest store see whether we can get
8142 // the smaller value for free with a truncate or extract vector element and
8143 // then store.
8144 SDValue Value = MemSetValue;
8145 if (VT.bitsLT(LargestVT)) {
8146 unsigned Index;
8147 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8148 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8149 if (!LargestVT.isVector() && !VT.isVector() &&
8150 TLI.isTruncateFree(LargestVT, VT))
8151 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8152 else if (LargestVT.isVector() && !VT.isVector() &&
8154 LargestVT.getTypeForEVT(*DAG.getContext()),
8155 VT.getSizeInBits(), Index) &&
8156 TLI.isTypeLegal(SVT) &&
8157 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8158 // Target which can combine store(extractelement VectorTy, Idx) can get
8159 // the smaller value for free.
8160 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8161 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
8162 DAG.getVectorIdxConstant(Index, dl));
8163 } else
8164 Value = getMemsetValue(Src, VT, DAG, dl);
8165 }
8166 assert(Value.getValueType() == VT && "Value with wrong type.");
8167 SDValue Store = DAG.getStore(
8168 Chain, dl, Value,
8169 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8170 DstPtrInfo.getWithOffset(DstOff), Alignment,
8172 NewAAInfo);
8173 OutChains.push_back(Store);
8174 DstOff += VT.getSizeInBits() / 8;
8175 Size -= VTSize;
8176 }
8177
8178 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8179}
8180
8182 unsigned AS) {
8183 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8184 // pointer operands can be losslessly bitcasted to pointers of address space 0
8185 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8186 report_fatal_error("cannot lower memory intrinsic in address space " +
8187 Twine(AS));
8188 }
8189}
8190
8192 SDValue Src, SDValue Size, Align Alignment,
8193 bool isVol, bool AlwaysInline, bool isTailCall,
8194 MachinePointerInfo DstPtrInfo,
8195 MachinePointerInfo SrcPtrInfo,
8196 const AAMDNodes &AAInfo, AAResults *AA) {
8197 // Check to see if we should lower the memcpy to loads and stores first.
8198 // For cases within the target-specified limits, this is the best choice.
8199 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8200 if (ConstantSize) {
8201 // Memcpy with size zero? Just return the original chain.
8202 if (ConstantSize->isZero())
8203 return Chain;
8204
8206 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8207 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8208 if (Result.getNode())
8209 return Result;
8210 }
8211
8212 // Then check to see if we should lower the memcpy with target-specific
8213 // code. If the target chooses to do this, this is the next best.
8214 if (TSI) {
8215 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8216 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8217 DstPtrInfo, SrcPtrInfo);
8218 if (Result.getNode())
8219 return Result;
8220 }
8221
8222 // If we really need inline code and the target declined to provide it,
8223 // use a (potentially long) sequence of loads and stores.
8224 if (AlwaysInline) {
8225 assert(ConstantSize && "AlwaysInline requires a constant size!");
8227 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8228 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8229 }
8230
8233
8234 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8235 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8236 // respect volatile, so they may do things like read or write memory
8237 // beyond the given memory regions. But fixing this isn't easy, and most
8238 // people don't care.
8239
8240 // Emit a library call.
8243 Entry.Ty = PointerType::getUnqual(*getContext());
8244 Entry.Node = Dst; Args.push_back(Entry);
8245 Entry.Node = Src; Args.push_back(Entry);
8246
8247 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8248 Entry.Node = Size; Args.push_back(Entry);
8249 // FIXME: pass in SDLoc
8251 CLI.setDebugLoc(dl)
8252 .setChain(Chain)
8253 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8254 Dst.getValueType().getTypeForEVT(*getContext()),
8255 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8256 TLI->getPointerTy(getDataLayout())),
8257 std::move(Args))
8259 .setTailCall(isTailCall);
8260
8261 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8262 return CallResult.second;
8263}
8264
8266 SDValue Dst, SDValue Src, SDValue Size,
8267 Type *SizeTy, unsigned ElemSz,
8268 bool isTailCall,
8269 MachinePointerInfo DstPtrInfo,
8270 MachinePointerInfo SrcPtrInfo) {
8271 // Emit a library call.
8274 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8275 Entry.Node = Dst;
8276 Args.push_back(Entry);
8277
8278 Entry.Node = Src;
8279 Args.push_back(Entry);
8280
8281 Entry.Ty = SizeTy;
8282 Entry.Node = Size;
8283 Args.push_back(Entry);
8284
8285 RTLIB::Libcall LibraryCall =
8287 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8288 report_fatal_error("Unsupported element size");
8289
8291 CLI.setDebugLoc(dl)
8292 .setChain(Chain)
8293 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8295 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8296 TLI->getPointerTy(getDataLayout())),
8297 std::move(Args))
8299 .setTailCall(isTailCall);
8300
8301 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8302 return CallResult.second;
8303}
8304
8306 SDValue Src, SDValue Size, Align Alignment,
8307 bool isVol, bool isTailCall,
8308 MachinePointerInfo DstPtrInfo,
8309 MachinePointerInfo SrcPtrInfo,
8310 const AAMDNodes &AAInfo, AAResults *AA) {
8311 // Check to see if we should lower the memmove to loads and stores first.
8312 // For cases within the target-specified limits, this is the best choice.
8313 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8314 if (ConstantSize) {
8315 // Memmove with size zero? Just return the original chain.
8316 if (ConstantSize->isZero())
8317 return Chain;
8318
8320 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8321 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8322 if (Result.getNode())
8323 return Result;
8324 }
8325
8326 // Then check to see if we should lower the memmove with target-specific
8327 // code. If the target chooses to do this, this is the next best.
8328 if (TSI) {
8329 SDValue Result =
8330 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8331 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8332 if (Result.getNode())
8333 return Result;
8334 }
8335
8338
8339 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8340 // not be safe. See memcpy above for more details.
8341
8342 // Emit a library call.
8345 Entry.Ty = PointerType::getUnqual(*getContext());
8346 Entry.Node = Dst; Args.push_back(Entry);
8347 Entry.Node = Src; Args.push_back(Entry);
8348
8349 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8350 Entry.Node = Size; Args.push_back(Entry);
8351 // FIXME: pass in SDLoc
8353 CLI.setDebugLoc(dl)
8354 .setChain(Chain)
8355 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8356 Dst.getValueType().getTypeForEVT(*getContext()),
8357 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8358 TLI->getPointerTy(getDataLayout())),
8359 std::move(Args))
8361 .setTailCall(isTailCall);
8362
8363 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8364 return CallResult.second;
8365}
8366
8368 SDValue Dst, SDValue Src, SDValue Size,
8369 Type *SizeTy, unsigned ElemSz,
8370 bool isTailCall,
8371 MachinePointerInfo DstPtrInfo,
8372 MachinePointerInfo SrcPtrInfo) {
8373 // Emit a library call.
8376 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8377 Entry.Node = Dst;
8378 Args.push_back(Entry);
8379
8380 Entry.Node = Src;
8381 Args.push_back(Entry);
8382
8383 Entry.Ty = SizeTy;
8384 Entry.Node = Size;
8385 Args.push_back(Entry);
8386
8387 RTLIB::Libcall LibraryCall =
8389 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8390 report_fatal_error("Unsupported element size");
8391
8393 CLI.setDebugLoc(dl)
8394 .setChain(Chain)
8395 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8397 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8398 TLI->getPointerTy(getDataLayout())),
8399 std::move(Args))
8401 .setTailCall(isTailCall);
8402
8403 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8404 return CallResult.second;
8405}
8406
8408 SDValue Src, SDValue Size, Align Alignment,
8409 bool isVol, bool AlwaysInline, bool isTailCall,
8410 MachinePointerInfo DstPtrInfo,
8411 const AAMDNodes &AAInfo) {
8412 // Check to see if we should lower the memset to stores first.
8413 // For cases within the target-specified limits, this is the best choice.
8414 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8415 if (ConstantSize) {
8416 // Memset with size zero? Just return the original chain.
8417 if (ConstantSize->isZero())
8418 return Chain;
8419
8420 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8421 ConstantSize->getZExtValue(), Alignment,
8422 isVol, false, DstPtrInfo, AAInfo);
8423
8424 if (Result.getNode())
8425 return Result;
8426 }
8427
8428 // Then check to see if we should lower the memset with target-specific
8429 // code. If the target chooses to do this, this is the next best.
8430 if (TSI) {
8431 SDValue Result = TSI->EmitTargetCodeForMemset(
8432 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8433 if (Result.getNode())
8434 return Result;
8435 }
8436
8437 // If we really need inline code and the target declined to provide it,
8438 // use a (potentially long) sequence of loads and stores.
8439 if (AlwaysInline) {
8440 assert(ConstantSize && "AlwaysInline requires a constant size!");
8441 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8442 ConstantSize->getZExtValue(), Alignment,
8443 isVol, true, DstPtrInfo, AAInfo);
8444 assert(Result &&
8445 "getMemsetStores must return a valid sequence when AlwaysInline");
8446 return Result;
8447 }
8448
8450
8451 // Emit a library call.
8452 auto &Ctx = *getContext();
8453 const auto& DL = getDataLayout();
8454
8456 // FIXME: pass in SDLoc
8457 CLI.setDebugLoc(dl).setChain(Chain);
8458
8459 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8460
8461 // Helper function to create an Entry from Node and Type.
8462 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8464 Entry.Node = Node;
8465 Entry.Ty = Ty;
8466 return Entry;
8467 };
8468
8469 // If zeroing out and bzero is present, use it.
8470 if (isNullConstant(Src) && BzeroName) {
8472 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8473 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8474 CLI.setLibCallee(
8475 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8476 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8477 } else {
8479 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8480 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8481 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8482 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8483 Dst.getValueType().getTypeForEVT(Ctx),
8484 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8485 TLI->getPointerTy(DL)),
8486 std::move(Args));
8487 }
8488
8489 CLI.setDiscardResult().setTailCall(isTailCall);
8490
8491 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8492 return CallResult.second;
8493}
8494
8497 Type *SizeTy, unsigned ElemSz,
8498 bool isTailCall,
8499 MachinePointerInfo DstPtrInfo) {
8500 // Emit a library call.
8503 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8504 Entry.Node = Dst;
8505 Args.push_back(Entry);
8506
8507 Entry.Ty = Type::getInt8Ty(*getContext());
8508 Entry.Node = Value;
8509 Args.push_back(Entry);
8510
8511 Entry.Ty = SizeTy;
8512 Entry.Node = Size;
8513 Args.push_back(Entry);
8514
8515 RTLIB::Libcall LibraryCall =
8517 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8518 report_fatal_error("Unsupported element size");
8519
8521 CLI.setDebugLoc(dl)
8522 .setChain(Chain)
8523 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8525 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8526 TLI->getPointerTy(getDataLayout())),
8527 std::move(Args))
8529 .setTailCall(isTailCall);
8530
8531 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8532 return CallResult.second;
8533}
8534
8535SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8536 SDVTList VTList, ArrayRef<SDValue> Ops,
8537 MachineMemOperand *MMO) {
8539 ID.AddInteger(MemVT.getRawBits());
8540 AddNodeIDNode(ID, Opcode, VTList, Ops);
8541 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8542 ID.AddInteger(MMO->getFlags());
8543 void* IP = nullptr;
8544 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8545 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8546 return SDValue(E, 0);
8547 }
8548
8549 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8550 VTList, MemVT, MMO);
8551 createOperands(N, Ops);
8552
8553 CSEMap.InsertNode(N, IP);
8554 InsertNode(N);
8555 return SDValue(N, 0);
8556}
8557
8559 EVT MemVT, SDVTList VTs, SDValue Chain,
8560 SDValue Ptr, SDValue Cmp, SDValue Swp,
8561 MachineMemOperand *MMO) {
8562 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8564 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8565
8566 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8567 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8568}
8569
8570SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8571 SDValue Chain, SDValue Ptr, SDValue Val,
8572 MachineMemOperand *MMO) {
8573 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
8574 Opcode == ISD::ATOMIC_LOAD_SUB ||
8575 Opcode == ISD::ATOMIC_LOAD_AND ||
8576 Opcode == ISD::ATOMIC_LOAD_CLR ||
8577 Opcode == ISD::ATOMIC_LOAD_OR ||
8578 Opcode == ISD::ATOMIC_LOAD_XOR ||
8579 Opcode == ISD::ATOMIC_LOAD_NAND ||
8580 Opcode == ISD::ATOMIC_LOAD_MIN ||
8581 Opcode == ISD::ATOMIC_LOAD_MAX ||
8582 Opcode == ISD::ATOMIC_LOAD_UMIN ||
8583 Opcode == ISD::ATOMIC_LOAD_UMAX ||
8584 Opcode == ISD::ATOMIC_LOAD_FADD ||
8585 Opcode == ISD::ATOMIC_LOAD_FSUB ||
8586 Opcode == ISD::ATOMIC_LOAD_FMAX ||
8587 Opcode == ISD::ATOMIC_LOAD_FMIN ||
8588 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
8589 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
8590 Opcode == ISD::ATOMIC_SWAP ||
8591 Opcode == ISD::ATOMIC_STORE) &&
8592 "Invalid Atomic Op");
8593
8594 EVT VT = Val.getValueType();
8595
8596 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
8597 getVTList(VT, MVT::Other);
8598 SDValue Ops[] = {Chain, Ptr, Val};
8599 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8600}
8601
8602SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8603 EVT VT, SDValue Chain, SDValue Ptr,
8604 MachineMemOperand *MMO) {
8605 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
8606
8607 SDVTList VTs = getVTList(VT, MVT::Other);
8608 SDValue Ops[] = {Chain, Ptr};
8609 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8610}
8611
8612/// getMergeValues - Create a MERGE_VALUES node from the given operands.
8614 if (Ops.size() == 1)
8615 return Ops[0];
8616
8618 VTs.reserve(Ops.size());
8619 for (const SDValue &Op : Ops)
8620 VTs.push_back(Op.getValueType());
8621 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
8622}
8623
8625 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
8626 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
8628 const AAMDNodes &AAInfo) {
8629 if (Size.hasValue() && !Size.getValue())
8631
8633 MachineMemOperand *MMO =
8634 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
8635
8636 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
8637}
8638
8640 SDVTList VTList,
8641 ArrayRef<SDValue> Ops, EVT MemVT,
8642 MachineMemOperand *MMO) {
8643 assert((Opcode == ISD::INTRINSIC_VOID ||
8644 Opcode == ISD::INTRINSIC_W_CHAIN ||
8645 Opcode == ISD::PREFETCH ||
8646 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
8647 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
8648 "Opcode is not a memory-accessing opcode!");
8649
8650 // Memoize the node unless it returns a glue result.
8652 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
8654 AddNodeIDNode(ID, Opcode, VTList, Ops);
8655 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
8656 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
8657 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8658 ID.AddInteger(MMO->getFlags());
8659 ID.AddInteger(MemVT.getRawBits());
8660 void *IP = nullptr;
8661 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8662 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
8663 return SDValue(E, 0);
8664 }
8665
8666 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8667 VTList, MemVT, MMO);
8668 createOperands(N, Ops);
8669
8670 CSEMap.InsertNode(N, IP);
8671 } else {
8672 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8673 VTList, MemVT, MMO);
8674 createOperands(N, Ops);
8675 }
8676 InsertNode(N);
8677 SDValue V(N, 0);
8678 NewSDValueDbgMsg(V, "Creating new node: ", this);
8679 return V;
8680}
8681
8683 SDValue Chain, int FrameIndex,
8684 int64_t Size, int64_t Offset) {
8685 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
8686 const auto VTs = getVTList(MVT::Other);
8687 SDValue Ops[2] = {
8688 Chain,
8689 getFrameIndex(FrameIndex,
8690 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
8691 true)};
8692
8694 AddNodeIDNode(ID, Opcode, VTs, Ops);
8695 ID.AddInteger(FrameIndex);
8696 ID.AddInteger(Size);
8697 ID.AddInteger(Offset);
8698 void *IP = nullptr;
8699 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8700 return SDValue(E, 0);
8701
8702 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
8703 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
8704 createOperands(N, Ops);
8705 CSEMap.InsertNode(N, IP);
8706 InsertNode(N);
8707 SDValue V(N, 0);
8708 NewSDValueDbgMsg(V, "Creating new node: ", this);
8709 return V;
8710}
8711
8714 uint32_t Attr) {
8715 const unsigned Opcode = ISD::PSEUDO_PROBE;
8716 const auto VTs = getVTList(MVT::Other);
8717 SDValue Ops[] = {Chain};
8719 AddNodeIDNode(ID, Opcode, VTs, Ops);
8720 ID.AddInteger(Guid);
8721 ID.AddInteger(Index);
8722 void *IP = nullptr;
8723 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
8724 return SDValue(E, 0);
8725
8726 auto *N = newSDNode<PseudoProbeSDNode>(
8727 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
8728 createOperands(N, Ops);
8729 CSEMap.InsertNode(N, IP);
8730 InsertNode(N);
8731 SDValue V(N, 0);
8732 NewSDValueDbgMsg(V, "Creating new node: ", this);
8733 return V;
8734}
8735
8736/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8737/// MachinePointerInfo record from it. This is particularly useful because the
8738/// code generator has many cases where it doesn't bother passing in a
8739/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8741 SelectionDAG &DAG, SDValue Ptr,
8742 int64_t Offset = 0) {
8743 // If this is FI+Offset, we can model it.
8744 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
8746 FI->getIndex(), Offset);
8747
8748 // If this is (FI+Offset1)+Offset2, we can model it.
8749 if (Ptr.getOpcode() != ISD::ADD ||
8750 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
8751 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
8752 return Info;
8753
8754 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
8756 DAG.getMachineFunction(), FI,
8757 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
8758}
8759
8760/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8761/// MachinePointerInfo record from it. This is particularly useful because the
8762/// code generator has many cases where it doesn't bother passing in a
8763/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8765 SelectionDAG &DAG, SDValue Ptr,
8766 SDValue OffsetOp) {
8767 // If the 'Offset' value isn't a constant, we can't handle this.
8768 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
8769 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
8770 if (OffsetOp.isUndef())
8771 return InferPointerInfo(Info, DAG, Ptr);
8772 return Info;
8773}
8774
8776 EVT VT, const SDLoc &dl, SDValue Chain,
8778 MachinePointerInfo PtrInfo, EVT MemVT,
8779 Align Alignment,
8780 MachineMemOperand::Flags MMOFlags,
8781 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8782 assert(Chain.getValueType() == MVT::Other &&
8783 "Invalid chain type");
8784
8785 MMOFlags |= MachineMemOperand::MOLoad;
8786 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8787 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8788 // clients.
8789 if (PtrInfo.V.isNull())
8790 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8791
8794 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8795 Alignment, AAInfo, Ranges);
8796 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
8797}
8798
8800 EVT VT, const SDLoc &dl, SDValue Chain,
8801 SDValue Ptr, SDValue Offset, EVT MemVT,
8802 MachineMemOperand *MMO) {
8803 if (VT == MemVT) {
8804 ExtType = ISD::NON_EXTLOAD;
8805 } else if (ExtType == ISD::NON_EXTLOAD) {
8806 assert(VT == MemVT && "Non-extending load from different memory type!");
8807 } else {
8808 // Extending load.
8809 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
8810 "Should only be an extending load, not truncating!");
8811 assert(VT.isInteger() == MemVT.isInteger() &&
8812 "Cannot convert from FP to Int or Int -> FP!");
8813 assert(VT.isVector() == MemVT.isVector() &&
8814 "Cannot use an ext load to convert to or from a vector!");
8815 assert((!VT.isVector() ||
8817 "Cannot use an ext load to change the number of vector elements!");
8818 }
8819
8820 bool Indexed = AM != ISD::UNINDEXED;
8821 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8822
8823 SDVTList VTs = Indexed ?
8824 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
8825 SDValue Ops[] = { Chain, Ptr, Offset };
8827 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
8828 ID.AddInteger(MemVT.getRawBits());
8829 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
8830 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
8831 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8832 ID.AddInteger(MMO->getFlags());
8833 void *IP = nullptr;
8834 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8835 cast<LoadSDNode>(E)->refineAlignment(MMO);
8836 return SDValue(E, 0);
8837 }
8838 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8839 ExtType, MemVT, MMO);
8840 createOperands(N, Ops);
8841
8842 CSEMap.InsertNode(N, IP);
8843 InsertNode(N);
8844 SDValue V(N, 0);
8845 NewSDValueDbgMsg(V, "Creating new node: ", this);
8846 return V;
8847}
8848
8851 MaybeAlign Alignment,
8852 MachineMemOperand::Flags MMOFlags,
8853 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8854 SDValue Undef = getUNDEF(Ptr.getValueType());
8855 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8856 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
8857}
8858
8861 SDValue Undef = getUNDEF(Ptr.getValueType());
8862 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8863 VT, MMO);
8864}
8865
8867 EVT VT, SDValue Chain, SDValue Ptr,
8868 MachinePointerInfo PtrInfo, EVT MemVT,
8869 MaybeAlign Alignment,
8870 MachineMemOperand::Flags MMOFlags,
8871 const AAMDNodes &AAInfo) {
8872 SDValue Undef = getUNDEF(Ptr.getValueType());
8873 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
8874 MemVT, Alignment, MMOFlags, AAInfo);
8875}
8876
8878 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
8879 MachineMemOperand *MMO) {
8880 SDValue Undef = getUNDEF(Ptr.getValueType());
8881 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
8882 MemVT, MMO);
8883}
8884
8888 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
8889 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
8890 // Don't propagate the invariant or dereferenceable flags.
8891 auto MMOFlags =
8892 LD->getMemOperand()->getFlags() &
8894 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
8895 LD->getChain(), Base, Offset, LD->getPointerInfo(),
8896 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
8897}
8898
8901 Align Alignment,
8902 MachineMemOperand::Flags MMOFlags,
8903 const AAMDNodes &AAInfo) {
8904 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
8905
8906 MMOFlags |= MachineMemOperand::MOStore;
8907 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
8908
8909 if (PtrInfo.V.isNull())
8910 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
8911
8914 MachineMemOperand *MMO =
8915 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
8916 return getStore(Chain, dl, Val, Ptr, MMO);
8917}
8918
8921 assert(Chain.getValueType() == MVT::Other &&
8922 "Invalid chain type");
8923 EVT VT = Val.getValueType();
8924 SDVTList VTs = getVTList(MVT::Other);
8925 SDValue Undef = getUNDEF(Ptr.getValueType());
8926 SDValue Ops[] = { Chain, Val, Ptr, Undef };
8928 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8929 ID.AddInteger(VT.getRawBits());
8930 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
8931 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
8932 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8933 ID.AddInteger(MMO->getFlags());
8934 void *IP = nullptr;
8935 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8936 cast<StoreSDNode>(E)->refineAlignment(MMO);
8937 return SDValue(E, 0);
8938 }
8939 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
8940 ISD::UNINDEXED, false, VT, MMO);
8941 createOperands(N, Ops);
8942
8943 CSEMap.InsertNode(N, IP);
8944 InsertNode(N);
8945 SDValue V(N, 0);
8946 NewSDValueDbgMsg(V, "Creating new node: ", this);
8947 return V;
8948}
8949
8952 EVT SVT, Align Alignment,
8953 MachineMemOperand::Flags MMOFlags,
8954 const AAMDNodes &AAInfo) {
8955 assert(Chain.getValueType() == MVT::Other &&
8956 "Invalid chain type");
8957
8958 MMOFlags |= MachineMemOperand::MOStore;
8959 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
8960
8961 if (PtrInfo.V.isNull())
8962 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
8963
8966 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
8967 AAInfo);
8968 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
8969}
8970
8972 SDValue Ptr, EVT SVT,
8973 MachineMemOperand *MMO) {
8974 EVT VT = Val.getValueType();
8975
8976 assert(Chain.getValueType() == MVT::Other &&
8977 "Invalid chain type");
8978 if (VT == SVT)
8979 return getStore(Chain, dl, Val, Ptr, MMO);
8980
8982 "Should only be a truncating store, not extending!");
8983 assert(VT.isInteger() == SVT.isInteger() &&
8984 "Can't do FP-INT conversion!");
8985 assert(VT.isVector() == SVT.isVector() &&
8986 "Cannot use trunc store to convert to or from a vector!");
8987 assert((!VT.isVector() ||
8989 "Cannot use trunc store to change the number of vector elements!");
8990
8991 SDVTList VTs = getVTList(MVT::Other);
8992 SDValue Undef = getUNDEF(Ptr.getValueType());
8993 SDValue Ops[] = { Chain, Val, Ptr, Undef };
8995 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
8996 ID.AddInteger(SVT.getRawBits());
8997 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
8998 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
8999 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9000 ID.AddInteger(MMO->getFlags());
9001 void *IP = nullptr;
9002 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9003 cast<StoreSDNode>(E)->refineAlignment(MMO);
9004 return SDValue(E, 0);
9005 }
9006 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9007 ISD::UNINDEXED, true, SVT, MMO);
9008 createOperands(N, Ops);
9009
9010 CSEMap.InsertNode(N, IP);
9011 InsertNode(N);
9012 SDValue V(N, 0);
9013 NewSDValueDbgMsg(V, "Creating new node: ", this);
9014 return V;
9015}
9016
9020 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9021 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9022 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9023 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
9025 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9026 ID.AddInteger(ST->getMemoryVT().getRawBits());
9027 ID.AddInteger(ST->getRawSubclassData());
9028 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9029 ID.AddInteger(ST->getMemOperand()->getFlags());
9030 void *IP = nullptr;
9031 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9032 return SDValue(E, 0);
9033
9034 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9035 ST->isTruncatingStore(), ST->getMemoryVT(),
9036 ST->getMemOperand());
9037 createOperands(N, Ops);
9038
9039 CSEMap.InsertNode(N, IP);
9040 InsertNode(N);
9041 SDValue V(N, 0);
9042 NewSDValueDbgMsg(V, "Creating new node: ", this);
9043 return V;
9044}
9045
9047 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9048 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9049 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9050 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9051 const MDNode *Ranges, bool IsExpanding) {
9052 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9053
9054 MMOFlags |= MachineMemOperand::MOLoad;
9055 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9056 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9057 // clients.
9058 if (PtrInfo.V.isNull())
9059 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9060
9063 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9064 Alignment, AAInfo, Ranges);
9065 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9066 MMO, IsExpanding);
9067}
9068
9070 ISD::LoadExtType ExtType, EVT VT,
9071 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9072 SDValue Offset, SDValue Mask, SDValue EVL,
9073 EVT MemVT, MachineMemOperand *MMO,
9074 bool IsExpanding) {
9075 bool Indexed = AM != ISD::UNINDEXED;
9076 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9077
9078 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9079 : getVTList(VT, MVT::Other);
9080 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9082 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9083 ID.AddInteger(MemVT.getRawBits());
9084 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9085 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9086 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9087 ID.AddInteger(MMO->getFlags());
9088 void *IP = nullptr;
9089 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9090 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
9091 return SDValue(E, 0);
9092 }
9093 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9094 ExtType, IsExpanding, MemVT, MMO);
9095 createOperands(N, Ops);
9096
9097 CSEMap.InsertNode(N, IP);
9098 InsertNode(N);
9099 SDValue V(N, 0);
9100 NewSDValueDbgMsg(V, "Creating new node: ", this);
9101 return V;
9102}
9103
9105 SDValue Ptr, SDValue Mask, SDValue EVL,
9106 MachinePointerInfo PtrInfo,
9107 MaybeAlign Alignment,
9108 MachineMemOperand::Flags MMOFlags,
9109 const AAMDNodes &AAInfo, const MDNode *Ranges,
9110 bool IsExpanding) {
9111 SDValue Undef = getUNDEF(Ptr.getValueType());
9112 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9113 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9114 IsExpanding);
9115}
9116
9118 SDValue Ptr, SDValue Mask, SDValue EVL,
9119 MachineMemOperand *MMO, bool IsExpanding) {
9120 SDValue Undef = getUNDEF(Ptr.getValueType());
9121 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9122 Mask, EVL, VT, MMO, IsExpanding);
9123}
9124
9126 EVT VT, SDValue Chain, SDValue Ptr,
9127 SDValue Mask, SDValue EVL,
9128 MachinePointerInfo PtrInfo, EVT MemVT,
9129 MaybeAlign Alignment,
9130 MachineMemOperand::Flags MMOFlags,
9131 const AAMDNodes &AAInfo, bool IsExpanding) {
9132 SDValue Undef = getUNDEF(Ptr.getValueType());
9133 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9134 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9135 IsExpanding);
9136}
9137
9139 EVT VT, SDValue Chain, SDValue Ptr,
9140 SDValue Mask, SDValue EVL, EVT MemVT,
9141 MachineMemOperand *MMO, bool IsExpanding) {
9142 SDValue Undef = getUNDEF(Ptr.getValueType());
9143 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9144 EVL, MemVT, MMO, IsExpanding);
9145}
9146
9150 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9151 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9152 // Don't propagate the invariant or dereferenceable flags.
9153 auto MMOFlags =
9154 LD->getMemOperand()->getFlags() &
9156 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9157 LD->getChain(), Base, Offset, LD->getMask(),
9158 LD->getVectorLength(), LD->getPointerInfo(),
9159 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
9160 nullptr, LD->isExpandingLoad());
9161}
9162
9165 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
9166 ISD::MemIndexedMode AM, bool IsTruncating,
9167 bool IsCompressing) {
9168 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9169 bool Indexed = AM != ISD::UNINDEXED;
9170 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9171 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9172 : getVTList(MVT::Other);
9173 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9175 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9176 ID.AddInteger(MemVT.getRawBits());
9177 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9178 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9179 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9180 ID.AddInteger(MMO->getFlags());
9181 void *IP = nullptr;
9182 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9183 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9184 return SDValue(E, 0);
9185 }
9186 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9187 IsTruncating, IsCompressing, MemVT, MMO);
9188 createOperands(N, Ops);
9189
9190 CSEMap.InsertNode(N, IP);
9191 InsertNode(N);
9192 SDValue V(N, 0);
9193 NewSDValueDbgMsg(V, "Creating new node: ", this);
9194 return V;
9195}
9196
9198 SDValue Val, SDValue Ptr, SDValue Mask,
9199 SDValue EVL, MachinePointerInfo PtrInfo,
9200 EVT SVT, Align Alignment,
9201 MachineMemOperand::Flags MMOFlags,
9202 const AAMDNodes &AAInfo,
9203 bool IsCompressing) {
9204 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9205
9206 MMOFlags |= MachineMemOperand::MOStore;
9207 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9208
9209 if (PtrInfo.V.isNull())
9210 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9211
9214 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9215 AAInfo);
9216 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9217 IsCompressing);
9218}
9219
9221 SDValue Val, SDValue Ptr, SDValue Mask,
9222 SDValue EVL, EVT SVT,
9223 MachineMemOperand *MMO,
9224 bool IsCompressing) {
9225 EVT VT = Val.getValueType();
9226
9227 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9228 if (VT == SVT)
9229 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9230 EVL, VT, MMO, ISD::UNINDEXED,
9231 /*IsTruncating*/ false, IsCompressing);
9232
9234 "Should only be a truncating store, not extending!");
9235 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9236 assert(VT.isVector() == SVT.isVector() &&
9237 "Cannot use trunc store to convert to or from a vector!");
9238 assert((!VT.isVector() ||
9240 "Cannot use trunc store to change the number of vector elements!");
9241
9242 SDVTList VTs = getVTList(MVT::Other);
9243 SDValue Undef = getUNDEF(Ptr.getValueType());
9244 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9246 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9247 ID.AddInteger(SVT.getRawBits());
9248 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9249 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9250 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9251 ID.AddInteger(MMO->getFlags());
9252 void *IP = nullptr;
9253 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9254 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9255 return SDValue(E, 0);
9256 }
9257 auto *N =
9258 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9259 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
9260 createOperands(N, Ops);
9261
9262 CSEMap.InsertNode(N, IP);
9263 InsertNode(N);
9264 SDValue V(N, 0);
9265 NewSDValueDbgMsg(V, "Creating new node: ", this);
9266 return V;
9267}
9268
9272 auto *ST = cast<VPStoreSDNode>(OrigStore);
9273 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9274 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9275 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9276 Offset, ST->getMask(), ST->getVectorLength()};
9278 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9279 ID.AddInteger(ST->getMemoryVT().getRawBits());
9280 ID.AddInteger(ST->getRawSubclassData());
9281 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9282 ID.AddInteger(ST->getMemOperand()->getFlags());
9283 void *IP = nullptr;
9284 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9285 return SDValue(E, 0);
9286
9287 auto *N = newSDNode<VPStoreSDNode>(
9288 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9289 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9290 createOperands(N, Ops);
9291
9292 CSEMap.InsertNode(N, IP);
9293 InsertNode(N);
9294 SDValue V(N, 0);
9295 NewSDValueDbgMsg(V, "Creating new node: ", this);
9296 return V;
9297}
9298
9300 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9301 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9302 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9303 bool Indexed = AM != ISD::UNINDEXED;
9304 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9305
9306 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9307 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9308 : getVTList(VT, MVT::Other);
9310 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9311 ID.AddInteger(VT.getRawBits());
9312 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9313 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9314 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9315
9316 void *IP = nullptr;
9317 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9318 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9319 return SDValue(E, 0);
9320 }
9321
9322 auto *N =
9323 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9324 ExtType, IsExpanding, MemVT, MMO);
9325 createOperands(N, Ops);
9326 CSEMap.InsertNode(N, IP);
9327 InsertNode(N);
9328 SDValue V(N, 0);
9329 NewSDValueDbgMsg(V, "Creating new node: ", this);
9330 return V;
9331}
9332
9334 SDValue Ptr, SDValue Stride,
9335 SDValue Mask, SDValue EVL,
9336 MachineMemOperand *MMO,
9337 bool IsExpanding) {
9338 SDValue Undef = getUNDEF(Ptr.getValueType());
9340 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9341}
9342
9344 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9345 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9346 MachineMemOperand *MMO, bool IsExpanding) {
9347 SDValue Undef = getUNDEF(Ptr.getValueType());
9348 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9349 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9350}
9351
9353 SDValue Val, SDValue Ptr,
9354 SDValue Offset, SDValue Stride,
9355 SDValue Mask, SDValue EVL, EVT MemVT,
9356 MachineMemOperand *MMO,
9358 bool IsTruncating, bool IsCompressing) {
9359 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9360 bool Indexed = AM != ISD::UNINDEXED;
9361 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9362 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9363 : getVTList(MVT::Other);
9364 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9366 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9367 ID.AddInteger(MemVT.getRawBits());
9368 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9369 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9370 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9371 void *IP = nullptr;
9372 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9373 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9374 return SDValue(E, 0);
9375 }
9376 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9377 VTs, AM, IsTruncating,
9378 IsCompressing, MemVT, MMO);
9379 createOperands(N, Ops);
9380
9381 CSEMap.InsertNode(N, IP);
9382 InsertNode(N);
9383 SDValue V(N, 0);
9384 NewSDValueDbgMsg(V, "Creating new node: ", this);
9385 return V;
9386}
9387
9389 SDValue Val, SDValue Ptr,
9390 SDValue Stride, SDValue Mask,
9391 SDValue EVL, EVT SVT,
9392 MachineMemOperand *MMO,
9393 bool IsCompressing) {
9394 EVT VT = Val.getValueType();
9395
9396 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9397 if (VT == SVT)
9398 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9399 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9400 /*IsTruncating*/ false, IsCompressing);
9401
9403 "Should only be a truncating store, not extending!");
9404 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9405 assert(VT.isVector() == SVT.isVector() &&
9406 "Cannot use trunc store to convert to or from a vector!");
9407 assert((!VT.isVector() ||
9409 "Cannot use trunc store to change the number of vector elements!");
9410
9411 SDVTList VTs = getVTList(MVT::Other);
9412 SDValue Undef = getUNDEF(Ptr.getValueType());
9413 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9415 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9416 ID.AddInteger(SVT.getRawBits());
9417 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9418 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9419 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9420 void *IP = nullptr;
9421 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9422 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9423 return SDValue(E, 0);
9424 }
9425 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9426 VTs, ISD::UNINDEXED, true,
9427 IsCompressing, SVT, MMO);
9428 createOperands(N, Ops);
9429
9430 CSEMap.InsertNode(N, IP);
9431 InsertNode(N);
9432 SDValue V(N, 0);
9433 NewSDValueDbgMsg(V, "Creating new node: ", this);
9434 return V;
9435}
9436
9439 ISD::MemIndexType IndexType) {
9440 assert(Ops.size() == 6 && "Incompatible number of operands");
9441
9443 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9444 ID.AddInteger(VT.getRawBits());
9445 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9446 dl.getIROrder(), VTs, VT, MMO, IndexType));
9447 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9448 ID.AddInteger(MMO->getFlags());
9449 void *IP = nullptr;
9450 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9451 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9452 return SDValue(E, 0);
9453 }
9454
9455 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9456 VT, MMO, IndexType);
9457 createOperands(N, Ops);
9458
9459 assert(N->getMask().getValueType().getVectorElementCount() ==
9460 N->getValueType(0).getVectorElementCount() &&
9461 "Vector width mismatch between mask and data");
9462 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9463 N->getValueType(0).getVectorElementCount().isScalable() &&
9464 "Scalable flags of index and data do not match");
9466 N->getIndex().getValueType().getVectorElementCount(),
9467 N->getValueType(0).getVectorElementCount()) &&
9468 "Vector width mismatch between index and data");
9469 assert(isa<ConstantSDNode>(N->getScale()) &&
9470 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9471 "Scale should be a constant power of 2");
9472
9473 CSEMap.InsertNode(N, IP);
9474 InsertNode(N);
9475 SDValue V(N, 0);
9476 NewSDValueDbgMsg(V, "Creating new node: ", this);
9477 return V;
9478}
9479
9482 MachineMemOperand *MMO,
9483 ISD::MemIndexType IndexType) {
9484 assert(Ops.size() == 7 && "Incompatible number of operands");
9485
9487 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9488 ID.AddInteger(VT.getRawBits());
9489 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9490 dl.getIROrder(), VTs, VT, MMO, IndexType));
9491 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9492 ID.AddInteger(MMO->getFlags());
9493 void *IP = nullptr;
9494 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9495 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9496 return SDValue(E, 0);
9497 }
9498 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9499 VT, MMO, IndexType);
9500 createOperands(N, Ops);
9501
9502 assert(N->getMask().getValueType().getVectorElementCount() ==
9503 N->getValue().getValueType().getVectorElementCount() &&
9504 "Vector width mismatch between mask and data");
9505 assert(
9506 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9507 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9508 "Scalable flags of index and data do not match");
9510 N->getIndex().getValueType().getVectorElementCount(),
9511 N->getValue().getValueType().getVectorElementCount()) &&
9512 "Vector width mismatch between index and data");
9513 assert(isa<ConstantSDNode>(N->getScale()) &&
9514 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9515 "Scale should be a constant power of 2");
9516
9517 CSEMap.InsertNode(N, IP);
9518 InsertNode(N);
9519 SDValue V(N, 0);
9520 NewSDValueDbgMsg(V, "Creating new node: ", this);
9521 return V;
9522}
9523
9526 SDValue PassThru, EVT MemVT,
9527 MachineMemOperand *MMO,
9529 ISD::LoadExtType ExtTy, bool isExpanding) {
9530 bool Indexed = AM != ISD::UNINDEXED;
9531 assert((Indexed || Offset.isUndef()) &&
9532 "Unindexed masked load with an offset!");
9533 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9534 : getVTList(VT, MVT::Other);
9535 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9537 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9538 ID.AddInteger(MemVT.getRawBits());
9539 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9540 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9541 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9542 ID.AddInteger(MMO->getFlags());
9543 void *IP = nullptr;
9544 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9545 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9546 return SDValue(E, 0);
9547 }
9548 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9549 AM, ExtTy, isExpanding, MemVT, MMO);
9550 createOperands(N, Ops);
9551
9552 CSEMap.InsertNode(N, IP);
9553 InsertNode(N);
9554 SDValue V(N, 0);
9555 NewSDValueDbgMsg(V, "Creating new node: ", this);
9556 return V;
9557}
9558
9562 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9563 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9564 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9565 Offset, LD->getMask(), LD->getPassThru(),
9566 LD->getMemoryVT(), LD->getMemOperand(), AM,
9567 LD->getExtensionType(), LD->isExpandingLoad());
9568}
9569
9572 SDValue Mask, EVT MemVT,
9573 MachineMemOperand *MMO,
9574 ISD::MemIndexedMode AM, bool IsTruncating,
9575 bool IsCompressing) {
9576 assert(Chain.getValueType() == MVT::Other &&
9577 "Invalid chain type");
9578 bool Indexed = AM != ISD::UNINDEXED;
9579 assert((Indexed || Offset.isUndef()) &&
9580 "Unindexed masked store with an offset!");
9581 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
9582 : getVTList(MVT::Other);
9583 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
9585 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
9586 ID.AddInteger(MemVT.getRawBits());
9587 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
9588 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9589 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9590 ID.AddInteger(MMO->getFlags());
9591 void *IP = nullptr;
9592 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9593 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
9594 return SDValue(E, 0);
9595 }
9596 auto *N =
9597 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9598 IsTruncating, IsCompressing, MemVT, MMO);
9599 createOperands(N, Ops);
9600
9601 CSEMap.InsertNode(N, IP);
9602 InsertNode(N);
9603 SDValue V(N, 0);
9604 NewSDValueDbgMsg(V, "Creating new node: ", this);
9605 return V;
9606}
9607
9611 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
9612 assert(ST->getOffset().isUndef() &&
9613 "Masked store is already a indexed store!");
9614 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9615 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
9616 AM, ST->isTruncatingStore(), ST->isCompressingStore());
9617}
9618
9621 MachineMemOperand *MMO,
9622 ISD::MemIndexType IndexType,
9623 ISD::LoadExtType ExtTy) {
9624 assert(Ops.size() == 6 && "Incompatible number of operands");
9625
9627 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
9628 ID.AddInteger(MemVT.getRawBits());
9629 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
9630 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
9631 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9632 ID.AddInteger(MMO->getFlags());
9633 void *IP = nullptr;
9634 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9635 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9636 return SDValue(E, 0);
9637 }
9638
9639 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9640 VTs, MemVT, MMO, IndexType, ExtTy);
9641 createOperands(N, Ops);
9642
9643 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
9644 "Incompatible type of the PassThru value in MaskedGatherSDNode");
9645 assert(N->getMask().getValueType().getVectorElementCount() ==
9646 N->getValueType(0).getVectorElementCount() &&
9647 "Vector width mismatch between mask and data");
9648 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9649 N->getValueType(0).getVectorElementCount().isScalable() &&
9650 "Scalable flags of index and data do not match");
9652 N->getIndex().getValueType().getVectorElementCount(),
9653 N->getValueType(0).getVectorElementCount()) &&
9654 "Vector width mismatch between index and data");
9655 assert(isa<ConstantSDNode>(N->getScale()) &&
9656 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9657 "Scale should be a constant power of 2");
9658
9659 CSEMap.InsertNode(N, IP);
9660 InsertNode(N);
9661 SDValue V(N, 0);
9662 NewSDValueDbgMsg(V, "Creating new node: ", this);
9663 return V;
9664}
9665
9668 MachineMemOperand *MMO,
9669 ISD::MemIndexType IndexType,
9670 bool IsTrunc) {
9671 assert(Ops.size() == 6 && "Incompatible number of operands");
9672
9674 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
9675 ID.AddInteger(MemVT.getRawBits());
9676 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
9677 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
9678 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9679 ID.AddInteger(MMO->getFlags());
9680 void *IP = nullptr;
9681 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9682 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
9683 return SDValue(E, 0);
9684 }
9685
9686 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9687 VTs, MemVT, MMO, IndexType, IsTrunc);
9688 createOperands(N, Ops);
9689
9690 assert(N->getMask().getValueType().getVectorElementCount() ==
9691 N->getValue().getValueType().getVectorElementCount() &&
9692 "Vector width mismatch between mask and data");
9693 assert(
9694 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9695 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9696 "Scalable flags of index and data do not match");
9698 N->getIndex().getValueType().getVectorElementCount(),
9699 N->getValue().getValueType().getVectorElementCount()) &&
9700 "Vector width mismatch between index and data");
9701 assert(isa<ConstantSDNode>(N->getScale()) &&
9702 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9703 "Scale should be a constant power of 2");
9704
9705 CSEMap.InsertNode(N, IP);
9706 InsertNode(N);
9707 SDValue V(N, 0);
9708 NewSDValueDbgMsg(V, "Creating new node: ", this);
9709 return V;
9710}
9711
9713 const SDLoc &dl, ArrayRef<SDValue> Ops,
9714 MachineMemOperand *MMO,
9715 ISD::MemIndexType IndexType) {
9716 assert(Ops.size() == 7 && "Incompatible number of operands");
9717
9720 ID.AddInteger(MemVT.getRawBits());
9721 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
9722 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
9723 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9724 ID.AddInteger(MMO->getFlags());
9725 void *IP = nullptr;
9726 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9727 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9728 return SDValue(E, 0);
9729 }
9730
9731 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9732 VTs, MemVT, MMO, IndexType);
9733 createOperands(N, Ops);
9734
9735 assert(N->getMask().getValueType().getVectorElementCount() ==
9736 N->getIndex().getValueType().getVectorElementCount() &&
9737 "Vector width mismatch between mask and data");
9738 assert(isa<ConstantSDNode>(N->getScale()) &&
9739 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9740 "Scale should be a constant power of 2");
9741 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
9742
9743 CSEMap.InsertNode(N, IP);
9744 InsertNode(N);
9745 SDValue V(N, 0);
9746 NewSDValueDbgMsg(V, "Creating new node: ", this);
9747 return V;
9748}
9749
9751 EVT MemVT, MachineMemOperand *MMO) {
9752 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9753 SDVTList VTs = getVTList(MVT::Other);
9754 SDValue Ops[] = {Chain, Ptr};
9757 ID.AddInteger(MemVT.getRawBits());
9758 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9759 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9760 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9761 ID.AddInteger(MMO->getFlags());
9762 void *IP = nullptr;
9763 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9764 return SDValue(E, 0);
9765
9766 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
9767 dl.getDebugLoc(), VTs, MemVT, MMO);
9768 createOperands(N, Ops);
9769
9770 CSEMap.InsertNode(N, IP);
9771 InsertNode(N);
9772 SDValue V(N, 0);
9773 NewSDValueDbgMsg(V, "Creating new node: ", this);
9774 return V;
9775}
9776
9778 EVT MemVT, MachineMemOperand *MMO) {
9779 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9780 SDVTList VTs = getVTList(MVT::Other);
9781 SDValue Ops[] = {Chain, Ptr};
9784 ID.AddInteger(MemVT.getRawBits());
9785 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9786 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9787 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9788 ID.AddInteger(MMO->getFlags());
9789 void *IP = nullptr;
9790 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9791 return SDValue(E, 0);
9792
9793 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
9794 dl.getDebugLoc(), VTs, MemVT, MMO);
9795 createOperands(N, Ops);
9796
9797 CSEMap.InsertNode(N, IP);
9798 InsertNode(N);
9799 SDValue V(N, 0);
9800 NewSDValueDbgMsg(V, "Creating new node: ", this);
9801 return V;
9802}
9803
9805 // select undef, T, F --> T (if T is a constant), otherwise F
9806 // select, ?, undef, F --> F
9807 // select, ?, T, undef --> T
9808 if (Cond.isUndef())
9809 return isConstantValueOfAnyType(T) ? T : F;
9810 if (T.isUndef())
9811 return F;
9812 if (F.isUndef())
9813 return T;
9814
9815 // select true, T, F --> T
9816 // select false, T, F --> F
9817 if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
9818 return CondC->isZero() ? F : T;
9819
9820 // TODO: This should simplify VSELECT with non-zero constant condition using
9821 // something like this (but check boolean contents to be complete?):
9822 if (ConstantSDNode *CondC = isConstOrConstSplat(Cond, /*AllowUndefs*/ false,
9823 /*AllowTruncation*/ true))
9824 if (CondC->isZero())
9825 return F;
9826
9827 // select ?, T, T --> T
9828 if (T == F)
9829 return T;
9830
9831 return SDValue();
9832}
9833
9835 // shift undef, Y --> 0 (can always assume that the undef value is 0)
9836 if (X.isUndef())
9837 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
9838 // shift X, undef --> undef (because it may shift by the bitwidth)
9839 if (Y.isUndef())
9840 return getUNDEF(X.getValueType());
9841
9842 // shift 0, Y --> 0
9843 // shift X, 0 --> X
9845 return X;
9846
9847 // shift X, C >= bitwidth(X) --> undef
9848 // All vector elements must be too big (or undef) to avoid partial undefs.
9849 auto isShiftTooBig = [X](ConstantSDNode *Val) {
9850 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
9851 };
9852 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
9853 return getUNDEF(X.getValueType());
9854
9855 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
9856 if (X.getValueType().getScalarType() == MVT::i1)
9857 return X;
9858
9859 return SDValue();
9860}
9861
9863 SDNodeFlags Flags) {
9864 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
9865 // (an undef operand can be chosen to be Nan/Inf), then the result of this
9866 // operation is poison. That result can be relaxed to undef.
9867 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
9868 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
9869 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
9870 (YC && YC->getValueAPF().isNaN());
9871 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
9872 (YC && YC->getValueAPF().isInfinity());
9873
9874 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
9875 return getUNDEF(X.getValueType());
9876
9877 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
9878 return getUNDEF(X.getValueType());
9879
9880 if (!YC)
9881 return SDValue();
9882
9883 // X + -0.0 --> X
9884 if (Opcode == ISD::FADD)
9885 if (YC->getValueAPF().isNegZero())
9886 return X;
9887
9888 // X - +0.0 --> X
9889 if (Opcode == ISD::FSUB)
9890 if (YC->getValueAPF().isPosZero())
9891 return X;
9892
9893 // X * 1.0 --> X
9894 // X / 1.0 --> X
9895 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
9896 if (YC->getValueAPF().isExactlyValue(1.0))
9897 return X;
9898
9899 // X * 0.0 --> 0.0
9900 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
9901 if (YC->getValueAPF().isZero())
9902 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
9903
9904 return SDValue();
9905}
9906
9908 SDValue Ptr, SDValue SV, unsigned Align) {
9909 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
9910 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
9911}
9912
9913SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9914 ArrayRef<SDUse> Ops) {
9915 switch (Ops.size()) {
9916 case 0: return getNode(Opcode, DL, VT);
9917 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
9918 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
9919 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
9920 default: break;
9921 }
9922
9923 // Copy from an SDUse array into an SDValue array for use with
9924 // the regular getNode logic.
9925 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
9926 return getNode(Opcode, DL, VT, NewOps);
9927}
9928
9929SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9930 ArrayRef<SDValue> Ops) {
9931 SDNodeFlags Flags;
9932 if (Inserter)
9933 Flags = Inserter->getFlags();
9934 return getNode(Opcode, DL, VT, Ops, Flags);
9935}
9936
9937SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
9938 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
9939 unsigned NumOps = Ops.size();
9940 switch (NumOps) {
9941 case 0: return getNode(Opcode, DL, VT);
9942 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
9943 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
9944 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
9945 default: break;
9946 }
9947
9948#ifndef NDEBUG
9949 for (const auto &Op : Ops)
9950 assert(Op.getOpcode() != ISD::DELETED_NODE &&
9951 "Operand is DELETED_NODE!");
9952#endif
9953
9954 switch (Opcode) {
9955 default: break;
9956 case ISD::BUILD_VECTOR:
9957 // Attempt to simplify BUILD_VECTOR.
9958 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
9959 return V;
9960 break;
9962 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
9963 return V;
9964 break;
9965 case ISD::SELECT_CC:
9966 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
9967 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
9968 "LHS and RHS of condition must have same type!");
9969 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
9970 "True and False arms of SelectCC must have same type!");
9971 assert(Ops[2].getValueType() == VT &&
9972 "select_cc node must be of same type as true and false value!");
9973 assert((!Ops[0].getValueType().isVector() ||
9974 Ops[0].getValueType().getVectorElementCount() ==
9975 VT.getVectorElementCount()) &&
9976 "Expected select_cc with vector result to have the same sized "
9977 "comparison type!");
9978 break;
9979 case ISD::BR_CC:
9980 assert(NumOps == 5 && "BR_CC takes 5 operands!");
9981 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
9982 "LHS/RHS of comparison should match types!");
9983 break;
9984 case ISD::VP_ADD:
9985 case ISD::VP_SUB:
9986 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
9987 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
9988 Opcode = ISD::VP_XOR;
9989 break;
9990 case ISD::VP_MUL:
9991 // If it is VP_MUL mask operation then turn it to VP_AND
9992 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
9993 Opcode = ISD::VP_AND;
9994 break;
9995 case ISD::VP_REDUCE_MUL:
9996 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
9997 if (VT == MVT::i1)
9998 Opcode = ISD::VP_REDUCE_AND;
9999 break;
10000 case ISD::VP_REDUCE_ADD:
10001 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10002 if (VT == MVT::i1)
10003 Opcode = ISD::VP_REDUCE_XOR;
10004 break;
10005 case ISD::VP_REDUCE_SMAX:
10006 case ISD::VP_REDUCE_UMIN:
10007 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10008 // VP_REDUCE_AND.
10009 if (VT == MVT::i1)
10010 Opcode = ISD::VP_REDUCE_AND;
10011 break;
10012 case ISD::VP_REDUCE_SMIN:
10013 case ISD::VP_REDUCE_UMAX:
10014 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10015 // VP_REDUCE_OR.
10016 if (VT == MVT::i1)
10017 Opcode = ISD::VP_REDUCE_OR;
10018 break;
10019 }
10020
10021 // Memoize nodes.
10022 SDNode *N;
10023 SDVTList VTs = getVTList(VT);
10024
10025 if (VT != MVT::Glue) {
10027 AddNodeIDNode(ID, Opcode, VTs, Ops);
10028 void *IP = nullptr;
10029
10030 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10031 return SDValue(E, 0);
10032
10033 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10034 createOperands(N, Ops);
10035
10036 CSEMap.InsertNode(N, IP);
10037 } else {
10038 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10039 createOperands(N, Ops);
10040 }
10041
10042 N->setFlags(Flags);
10043 InsertNode(N);
10044 SDValue V(N, 0);
10045 NewSDValueDbgMsg(V, "Creating new node: ", this);
10046 return V;
10047}
10048
10049SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10050 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10051 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
10052}
10053
10054SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10055 ArrayRef<SDValue> Ops) {
10056 SDNodeFlags Flags;
10057 if (Inserter)
10058 Flags = Inserter->getFlags();
10059 return getNode(Opcode, DL, VTList, Ops, Flags);
10060}
10061
10062SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10063 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10064 if (VTList.NumVTs == 1)
10065 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10066
10067#ifndef NDEBUG
10068 for (const auto &Op : Ops)
10069 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10070 "Operand is DELETED_NODE!");
10071#endif
10072
10073 switch (Opcode) {
10074 case ISD::SADDO:
10075 case ISD::UADDO:
10076 case ISD::SSUBO:
10077 case ISD::USUBO: {
10078 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10079 "Invalid add/sub overflow op!");
10080 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10081 Ops[0].getValueType() == Ops[1].getValueType() &&
10082 Ops[0].getValueType() == VTList.VTs[0] &&
10083 "Binary operator types must match!");
10084 SDValue N1 = Ops[0], N2 = Ops[1];
10085 canonicalizeCommutativeBinop(Opcode, N1, N2);
10086
10087 // (X +- 0) -> X with zero-overflow.
10088 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10089 /*AllowTruncation*/ true);
10090 if (N2CV && N2CV->isZero()) {
10091 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10092 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10093 }
10094
10095 if (VTList.VTs[0].isVector() &&
10096 VTList.VTs[0].getVectorElementType() == MVT::i1 &&
10097 VTList.VTs[1].getVectorElementType() == MVT::i1) {
10098 SDValue F1 = getFreeze(N1);
10099 SDValue F2 = getFreeze(N2);
10100 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10101 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10102 return getNode(ISD::MERGE_VALUES, DL, VTList,
10103 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10104 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
10105 Flags);
10106 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
10107 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
10108 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
10109 return getNode(ISD::MERGE_VALUES, DL, VTList,
10110 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10111 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
10112 Flags);
10113 }
10114 }
10115 break;
10116 }
10117 case ISD::SADDO_CARRY:
10118 case ISD::UADDO_CARRY:
10119 case ISD::SSUBO_CARRY:
10120 case ISD::USUBO_CARRY:
10121 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
10122 "Invalid add/sub overflow op!");
10123 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10124 Ops[0].getValueType() == Ops[1].getValueType() &&
10125 Ops[0].getValueType() == VTList.VTs[0] &&
10126 Ops[2].getValueType() == VTList.VTs[1] &&
10127 "Binary operator types must match!");
10128 break;
10129 case ISD::SMUL_LOHI:
10130 case ISD::UMUL_LOHI: {
10131 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
10132 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
10133 VTList.VTs[0] == Ops[0].getValueType() &&
10134 VTList.VTs[0] == Ops[1].getValueType() &&
10135 "Binary operator types must match!");
10136 // Constant fold.
10137 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
10138 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
10139 if (LHS && RHS) {
10140 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
10141 unsigned OutWidth = Width * 2;
10142 APInt Val = LHS->getAPIntValue();
10143 APInt Mul = RHS->getAPIntValue();
10144 if (Opcode == ISD::SMUL_LOHI) {
10145 Val = Val.sext(OutWidth);
10146 Mul = Mul.sext(OutWidth);
10147 } else {
10148 Val = Val.zext(OutWidth);
10149 Mul = Mul.zext(OutWidth);
10150 }
10151 Val *= Mul;
10152
10153 SDValue Hi =
10154 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
10155 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
10156 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
10157 }
10158 break;
10159 }
10160 case ISD::FFREXP: {
10161 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
10162 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
10163 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
10164
10165 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
10166 int FrexpExp;
10167 APFloat FrexpMant =
10168 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
10169 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
10170 SDValue Result1 =
10171 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
10172 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
10173 }
10174
10175 break;
10176 }
10178 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10179 "Invalid STRICT_FP_EXTEND!");
10180 assert(VTList.VTs[0].isFloatingPoint() &&
10181 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
10182 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10183 "STRICT_FP_EXTEND result type should be vector iff the operand "
10184 "type is vector!");
10185 assert((!VTList.VTs[0].isVector() ||
10186 VTList.VTs[0].getVectorElementCount() ==
10187 Ops[1].getValueType().getVectorElementCount()) &&
10188 "Vector element count mismatch!");
10189 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
10190 "Invalid fpext node, dst <= src!");
10191 break;
10193 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
10194 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10195 "STRICT_FP_ROUND result type should be vector iff the operand "
10196 "type is vector!");
10197 assert((!VTList.VTs[0].isVector() ||
10198 VTList.VTs[0].getVectorElementCount() ==
10199 Ops[1].getValueType().getVectorElementCount()) &&
10200 "Vector element count mismatch!");
10201 assert(VTList.VTs[0].isFloatingPoint() &&
10202 Ops[1].getValueType().isFloatingPoint() &&
10203 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
10204 isa<ConstantSDNode>(Ops[2]) &&
10205 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10206 "Invalid STRICT_FP_ROUND!");
10207 break;
10208#if 0
10209 // FIXME: figure out how to safely handle things like
10210 // int foo(int x) { return 1 << (x & 255); }
10211 // int bar() { return foo(256); }
10212 case ISD::SRA_PARTS:
10213 case ISD::SRL_PARTS:
10214 case ISD::SHL_PARTS:
10215 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10216 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10217 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10218 else if (N3.getOpcode() == ISD::AND)
10219 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10220 // If the and is only masking out bits that cannot effect the shift,
10221 // eliminate the and.
10222 unsigned NumBits = VT.getScalarSizeInBits()*2;
10223 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10224 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10225 }
10226 break;
10227#endif
10228 }
10229
10230 // Memoize the node unless it returns a glue result.
10231 SDNode *N;
10232 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10234 AddNodeIDNode(ID, Opcode, VTList, Ops);
10235 void *IP = nullptr;
10236 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10237 return SDValue(E, 0);
10238
10239 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10240 createOperands(N, Ops);
10241 CSEMap.InsertNode(N, IP);
10242 } else {
10243 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10244 createOperands(N, Ops);
10245 }
10246
10247 N->setFlags(Flags);
10248 InsertNode(N);
10249 SDValue V(N, 0);
10250 NewSDValueDbgMsg(V, "Creating new node: ", this);
10251 return V;
10252}
10253
10254SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10255 SDVTList VTList) {
10256 return getNode(Opcode, DL, VTList, std::nullopt);
10257}
10258
10259SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10260 SDValue N1) {
10261 SDValue Ops[] = { N1 };
10262 return getNode(Opcode, DL, VTList, Ops);
10263}
10264
10265SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10266 SDValue N1, SDValue N2) {
10267 SDValue Ops[] = { N1, N2 };
10268 return getNode(Opcode, DL, VTList, Ops);
10269}
10270
10271SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10272 SDValue N1, SDValue N2, SDValue N3) {
10273 SDValue Ops[] = { N1, N2, N3 };
10274 return getNode(Opcode, DL, VTList, Ops);
10275}
10276
10277SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10278 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10279 SDValue Ops[] = { N1, N2, N3, N4 };
10280 return getNode(Opcode, DL, VTList, Ops);
10281}
10282
10283SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10284 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10285 SDValue N5) {
10286 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10287 return getNode(Opcode, DL, VTList, Ops);
10288}
10289
10291 return makeVTList(SDNode::getValueTypeList(VT), 1);
10292}
10293
10296 ID.AddInteger(2U);
10297 ID.AddInteger(VT1.getRawBits());
10298 ID.AddInteger(VT2.getRawBits());
10299
10300 void *IP = nullptr;
10301 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10302 if (!Result) {
10303 EVT *Array = Allocator.Allocate<EVT>(2);
10304 Array[0] = VT1;
10305 Array[1] = VT2;
10306 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10307 VTListMap.InsertNode(Result, IP);
10308 }
10309 return Result->getSDVTList();
10310}
10311
10314 ID.AddInteger(3U);
10315 ID.AddInteger(VT1.getRawBits());
10316 ID.AddInteger(VT2.getRawBits());
10317 ID.AddInteger(VT3.getRawBits());
10318
10319 void *IP = nullptr;
10320 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10321 if (!Result) {
10322 EVT *Array = Allocator.Allocate<EVT>(3);
10323 Array[0] = VT1;
10324 Array[1] = VT2;
10325 Array[2] = VT3;
10326 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10327 VTListMap.InsertNode(Result, IP);
10328 }
10329 return Result->getSDVTList();
10330}
10331
10334 ID.AddInteger(4U);
10335 ID.AddInteger(VT1.getRawBits());
10336 ID.AddInteger(VT2.getRawBits());
10337 ID.AddInteger(VT3.getRawBits());
10338 ID.AddInteger(VT4.getRawBits());
10339
10340 void *IP = nullptr;
10341 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10342 if (!Result) {
10343 EVT *Array = Allocator.Allocate<EVT>(4);
10344 Array[0] = VT1;
10345 Array[1] = VT2;
10346 Array[2] = VT3;
10347 Array[3] = VT4;
10348 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10349 VTListMap.InsertNode(Result, IP);
10350 }
10351 return Result->getSDVTList();
10352}
10353
10355 unsigned NumVTs = VTs.size();
10357 ID.AddInteger(NumVTs);
10358 for (unsigned index = 0; index < NumVTs; index++) {
10359 ID.AddInteger(VTs[index].getRawBits());
10360 }
10361
10362 void *IP = nullptr;
10363 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10364 if (!Result) {
10365 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10366 llvm::copy(VTs, Array);
10367 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10368 VTListMap.InsertNode(Result, IP);
10369 }
10370 return Result->getSDVTList();
10371}
10372
10373
10374/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10375/// specified operands. If the resultant node already exists in the DAG,
10376/// this does not modify the specified node, instead it returns the node that
10377/// already exists. If the resultant node does not exist in the DAG, the
10378/// input node is returned. As a degenerate case, if you specify the same
10379/// input operands as the node already has, the input node is returned.
10381 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10382
10383 // Check to see if there is no change.
10384 if (Op == N->getOperand(0)) return N;
10385
10386 // See if the modified node already exists.
10387 void *InsertPos = nullptr;
10388 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10389 return Existing;
10390
10391 // Nope it doesn't. Remove the node from its current place in the maps.
10392 if (InsertPos)
10393 if (!RemoveNodeFromCSEMaps(N))
10394 InsertPos = nullptr;
10395
10396 // Now we update the operands.
10397 N->OperandList[0].set(Op);
10398
10400 // If this gets put into a CSE map, add it.
10401 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10402 return N;
10403}
10404
10406 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10407
10408 // Check to see if there is no change.
10409 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10410 return N; // No operands changed, just return the input node.
10411
10412 // See if the modified node already exists.
10413 void *InsertPos = nullptr;
10414 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10415 return Existing;
10416
10417 // Nope it doesn't. Remove the node from its current place in the maps.
10418 if (InsertPos)
10419 if (!RemoveNodeFromCSEMaps(N))
10420 InsertPos = nullptr;
10421
10422 // Now we update the operands.
10423 if (N->OperandList[0] != Op1)
10424 N->OperandList[0].set(Op1);
10425 if (N->OperandList[1] != Op2)
10426 N->OperandList[1].set(Op2);
10427
10429 // If this gets put into a CSE map, add it.
10430 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10431 return N;
10432}
10433
10436 SDValue Ops[] = { Op1, Op2, Op3 };
10437 return UpdateNodeOperands(N, Ops);
10438}
10439
10442 SDValue Op3, SDValue Op4) {
10443 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10444 return UpdateNodeOperands(N, Ops);
10445}
10446
10449 SDValue Op3, SDValue Op4, SDValue Op5) {
10450 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10451 return UpdateNodeOperands(N, Ops);
10452}
10453
10456 unsigned NumOps = Ops.size();
10457 assert(N->getNumOperands() == NumOps &&
10458 "Update with wrong number of operands");
10459
10460 // If no operands changed just return the input node.
10461 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10462 return N;
10463
10464 // See if the modified node already exists.
10465 void *InsertPos = nullptr;
10466 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10467 return Existing;
10468
10469 // Nope it doesn't. Remove the node from its current place in the maps.
10470 if (InsertPos)
10471 if (!RemoveNodeFromCSEMaps(N))
10472 InsertPos = nullptr;
10473
10474 // Now we update the operands.
10475 for (unsigned i = 0; i != NumOps; ++i)
10476 if (N->OperandList[i] != Ops[i])
10477 N->OperandList[i].set(Ops[i]);
10478
10480 // If this gets put into a CSE map, add it.
10481 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10482 return N;
10483}
10484
10485/// DropOperands - Release the operands and set this node to have
10486/// zero operands.
10488 // Unlike the code in MorphNodeTo that does this, we don't need to
10489 // watch for dead nodes here.
10490 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10491 SDUse &Use = *I++;
10492 Use.set(SDValue());
10493 }
10494}
10495
10497 ArrayRef<MachineMemOperand *> NewMemRefs) {
10498 if (NewMemRefs.empty()) {
10499 N->clearMemRefs();
10500 return;
10501 }
10502
10503 // Check if we can avoid allocating by storing a single reference directly.
10504 if (NewMemRefs.size() == 1) {
10505 N->MemRefs = NewMemRefs[0];
10506 N->NumMemRefs = 1;
10507 return;
10508 }
10509
10510 MachineMemOperand **MemRefsBuffer =
10511 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10512 llvm::copy(NewMemRefs, MemRefsBuffer);
10513 N->MemRefs = MemRefsBuffer;
10514 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10515}
10516
10517/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10518/// machine opcode.
10519///
10521 EVT VT) {
10522 SDVTList VTs = getVTList(VT);
10523 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10524}
10525
10527 EVT VT, SDValue Op1) {
10528 SDVTList VTs = getVTList(VT);
10529 SDValue Ops[] = { Op1 };
10530 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10531}
10532
10534 EVT VT, SDValue Op1,
10535 SDValue Op2) {
10536 SDVTList VTs = getVTList(VT);
10537 SDValue Ops[] = { Op1, Op2 };
10538 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10539}
10540
10542 EVT VT, SDValue Op1,
10543 SDValue Op2, SDValue Op3) {
10544 SDVTList VTs = getVTList(VT);
10545 SDValue Ops[] = { Op1, Op2, Op3 };
10546 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10547}
10548
10550 EVT VT, ArrayRef<SDValue> Ops) {
10551 SDVTList VTs = getVTList(VT);
10552 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10553}
10554
10556 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10557 SDVTList VTs = getVTList(VT1, VT2);
10558 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10559}
10560
10562 EVT VT1, EVT VT2) {
10563 SDVTList VTs = getVTList(VT1, VT2);
10564 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10565}
10566
10568 EVT VT1, EVT VT2, EVT VT3,
10569 ArrayRef<SDValue> Ops) {
10570 SDVTList VTs = getVTList(VT1, VT2, VT3);
10571 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10572}
10573
10575 EVT VT1, EVT VT2,
10576 SDValue Op1, SDValue Op2) {
10577 SDVTList VTs = getVTList(VT1, VT2);
10578 SDValue Ops[] = { Op1, Op2 };
10579 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10580}
10581
10583 SDVTList VTs,ArrayRef<SDValue> Ops) {
10584 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
10585 // Reset the NodeID to -1.
10586 New->setNodeId(-1);
10587 if (New != N) {
10588 ReplaceAllUsesWith(N, New);
10590 }
10591 return New;
10592}
10593
10594/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
10595/// the line number information on the merged node since it is not possible to
10596/// preserve the information that operation is associated with multiple lines.
10597/// This will make the debugger working better at -O0, were there is a higher
10598/// probability having other instructions associated with that line.
10599///
10600/// For IROrder, we keep the smaller of the two
10601SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
10602 DebugLoc NLoc = N->getDebugLoc();
10603 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
10604 N->setDebugLoc(DebugLoc());
10605 }
10606 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
10607 N->setIROrder(Order);
10608 return N;
10609}
10610
10611/// MorphNodeTo - This *mutates* the specified node to have the specified
10612/// return type, opcode, and operands.
10613///
10614/// Note that MorphNodeTo returns the resultant node. If there is already a
10615/// node of the specified opcode and operands, it returns that node instead of
10616/// the current one. Note that the SDLoc need not be the same.
10617///
10618/// Using MorphNodeTo is faster than creating a new node and swapping it in
10619/// with ReplaceAllUsesWith both because it often avoids allocating a new
10620/// node, and because it doesn't require CSE recalculation for any of
10621/// the node's users.
10622///
10623/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
10624/// As a consequence it isn't appropriate to use from within the DAG combiner or
10625/// the legalizer which maintain worklists that would need to be updated when
10626/// deleting things.
10628 SDVTList VTs, ArrayRef<SDValue> Ops) {
10629 // If an identical node already exists, use it.
10630 void *IP = nullptr;
10631 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
10633 AddNodeIDNode(ID, Opc, VTs, Ops);
10634 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
10635 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
10636 }
10637
10638 if (!RemoveNodeFromCSEMaps(N))
10639 IP = nullptr;
10640
10641 // Start the morphing.
10642 N->NodeType = Opc;
10643 N->ValueList = VTs.VTs;
10644 N->NumValues = VTs.NumVTs;
10645
10646 // Clear the operands list, updating used nodes to remove this from their
10647 // use list. Keep track of any operands that become dead as a result.
10648 SmallPtrSet<SDNode*, 16> DeadNodeSet;
10649 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
10650 SDUse &Use = *I++;
10651 SDNode *Used = Use.getNode();
10652 Use.set(SDValue());
10653 if (Used->use_empty())
10654 DeadNodeSet.insert(Used);
10655 }
10656
10657 // For MachineNode, initialize the memory references information.
10658 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
10659 MN->clearMemRefs();
10660
10661 // Swap for an appropriately sized array from the recycler.
10662 removeOperands(N);
10663 createOperands(N, Ops);
10664
10665 // Delete any nodes that are still dead after adding the uses for the
10666 // new operands.
10667 if (!DeadNodeSet.empty()) {
10668 SmallVector<SDNode *, 16> DeadNodes;
10669 for (SDNode *N : DeadNodeSet)
10670 if (N->use_empty())
10671 DeadNodes.push_back(N);
10672 RemoveDeadNodes(DeadNodes);
10673 }
10674
10675 if (IP)
10676 CSEMap.InsertNode(N, IP); // Memoize the new node.
10677 return N;
10678}
10679
10681 unsigned OrigOpc = Node->getOpcode();
10682 unsigned NewOpc;
10683 switch (OrigOpc) {
10684 default:
10685 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
10686#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10687 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
10688#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10689 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
10690#include "llvm/IR/ConstrainedOps.def"
10691 }
10692
10693 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
10694
10695 // We're taking this node out of the chain, so we need to re-link things.
10696 SDValue InputChain = Node->getOperand(0);
10697 SDValue OutputChain = SDValue(Node, 1);
10698 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
10699
10701 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
10702 Ops.push_back(Node->getOperand(i));
10703
10704 SDVTList VTs = getVTList(Node->getValueType(0));
10705 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
10706
10707 // MorphNodeTo can operate in two ways: if an existing node with the
10708 // specified operands exists, it can just return it. Otherwise, it
10709 // updates the node in place to have the requested operands.
10710 if (Res == Node) {
10711 // If we updated the node in place, reset the node ID. To the isel,
10712 // this should be just like a newly allocated machine node.
10713 Res->setNodeId(-1);
10714 } else {
10715 ReplaceAllUsesWith(Node, Res);
10716 RemoveDeadNode(Node);
10717 }
10718
10719 return Res;
10720}
10721
10722/// getMachineNode - These are used for target selectors to create a new node
10723/// with specified return type(s), MachineInstr opcode, and operands.
10724///
10725/// Note that getMachineNode returns the resultant node. If there is already a
10726/// node of the specified opcode and operands, it returns that node instead of
10727/// the current one.
10729 EVT VT) {
10730 SDVTList VTs = getVTList(VT);
10731 return getMachineNode(Opcode, dl, VTs, std::nullopt);
10732}
10733
10735 EVT VT, SDValue Op1) {
10736 SDVTList VTs = getVTList(VT);
10737 SDValue Ops[] = { Op1 };
10738 return getMachineNode(Opcode, dl, VTs, Ops);
10739}
10740
10742 EVT VT, SDValue Op1, SDValue Op2) {
10743 SDVTList VTs = getVTList(VT);
10744 SDValue Ops[] = { Op1, Op2 };
10745 return getMachineNode(Opcode, dl, VTs, Ops);
10746}
10747
10749 EVT VT, SDValue Op1, SDValue Op2,
10750 SDValue Op3) {
10751 SDVTList VTs = getVTList(VT);
10752 SDValue Ops[] = { Op1, Op2, Op3 };
10753 return getMachineNode(Opcode, dl, VTs, Ops);
10754}
10755
10757 EVT VT, ArrayRef<SDValue> Ops) {
10758 SDVTList VTs = getVTList(VT);
10759 return getMachineNode(Opcode, dl, VTs, Ops);
10760}
10761
10763 EVT VT1, EVT VT2, SDValue Op1,
10764 SDValue Op2) {
10765 SDVTList VTs = getVTList(VT1, VT2);
10766 SDValue Ops[] = { Op1, Op2 };
10767 return getMachineNode(Opcode, dl, VTs, Ops);
10768}
10769
10771 EVT VT1, EVT VT2, SDValue Op1,
10772 SDValue Op2, SDValue Op3) {
10773 SDVTList VTs = getVTList(VT1, VT2);
10774 SDValue Ops[] = { Op1, Op2, Op3 };
10775 return getMachineNode(Opcode, dl, VTs, Ops);
10776}
10777
10779 EVT VT1, EVT VT2,
10780 ArrayRef<SDValue> Ops) {
10781 SDVTList VTs = getVTList(VT1, VT2);
10782 return getMachineNode(Opcode, dl, VTs, Ops);
10783}
10784
10786 EVT VT1, EVT VT2, EVT VT3,
10787 SDValue Op1, SDValue Op2) {
10788 SDVTList VTs = getVTList(VT1, VT2, VT3);
10789 SDValue Ops[] = { Op1, Op2 };
10790 return getMachineNode(Opcode, dl, VTs, Ops);
10791}
10792
10794 EVT VT1, EVT VT2, EVT VT3,
10795 SDValue Op1, SDValue Op2,
10796 SDValue Op3) {
10797 SDVTList VTs = getVTList(VT1, VT2, VT3);
10798 SDValue Ops[] = { Op1, Op2, Op3 };
10799 return getMachineNode(Opcode, dl, VTs, Ops);
10800}
10801
10803 EVT VT1, EVT VT2, EVT VT3,
10804 ArrayRef<SDValue> Ops) {
10805 SDVTList VTs = getVTList(VT1, VT2, VT3);
10806 return getMachineNode(Opcode, dl, VTs, Ops);
10807}
10808
10810 ArrayRef<EVT> ResultTys,
10811 ArrayRef<SDValue> Ops) {
10812 SDVTList VTs = getVTList(ResultTys);
10813 return getMachineNode(Opcode, dl, VTs, Ops);
10814}
10815
10817 SDVTList VTs,
10818 ArrayRef<SDValue> Ops) {
10819 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
10821 void *IP = nullptr;
10822
10823 if (DoCSE) {
10825 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
10826 IP = nullptr;
10827 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10828 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
10829 }
10830 }
10831
10832 // Allocate a new MachineSDNode.
10833 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10834 createOperands(N, Ops);
10835
10836 if (DoCSE)
10837 CSEMap.InsertNode(N, IP);
10838
10839 InsertNode(N);
10840 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
10841 return N;
10842}
10843
10844/// getTargetExtractSubreg - A convenience function for creating
10845/// TargetOpcode::EXTRACT_SUBREG nodes.
10847 SDValue Operand) {
10848 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10849 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
10850 VT, Operand, SRIdxVal);
10851 return SDValue(Subreg, 0);
10852}
10853
10854/// getTargetInsertSubreg - A convenience function for creating
10855/// TargetOpcode::INSERT_SUBREG nodes.
10857 SDValue Operand, SDValue Subreg) {
10858 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10859 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
10860 VT, Operand, Subreg, SRIdxVal);
10861 return SDValue(Result, 0);
10862}
10863
10864/// getNodeIfExists - Get the specified node if it's already available, or
10865/// else return NULL.
10867 ArrayRef<SDValue> Ops) {
10868 SDNodeFlags Flags;
10869 if (Inserter)
10870 Flags = Inserter->getFlags();
10871 return getNodeIfExists(Opcode, VTList, Ops, Flags);
10872}
10873
10876 const SDNodeFlags Flags) {
10877 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10879 AddNodeIDNode(ID, Opcode, VTList, Ops);
10880 void *IP = nullptr;
10881 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
10882 E->intersectFlagsWith(Flags);
10883 return E;
10884 }
10885 }
10886 return nullptr;
10887}
10888
10889/// doesNodeExist - Check if a node exists without modifying its flags.
10890bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
10891 ArrayRef<SDValue> Ops) {
10892 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10894 AddNodeIDNode(ID, Opcode, VTList, Ops);
10895 void *IP = nullptr;
10896 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
10897 return true;
10898 }
10899 return false;
10900}
10901
10902/// getDbgValue - Creates a SDDbgValue node.
10903///
10904/// SDNode
10906 SDNode *N, unsigned R, bool IsIndirect,
10907 const DebugLoc &DL, unsigned O) {
10908 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10909 "Expected inlined-at fields to agree");
10910 return new (DbgInfo->getAlloc())
10911 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
10912 {}, IsIndirect, DL, O,
10913 /*IsVariadic=*/false);
10914}
10915
10916/// Constant
10918 DIExpression *Expr,
10919 const Value *C,
10920 const DebugLoc &DL, unsigned O) {
10921 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10922 "Expected inlined-at fields to agree");
10923 return new (DbgInfo->getAlloc())
10924 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
10925 /*IsIndirect=*/false, DL, O,
10926 /*IsVariadic=*/false);
10927}
10928
10929/// FrameIndex
10931 DIExpression *Expr, unsigned FI,
10932 bool IsIndirect,
10933 const DebugLoc &DL,
10934 unsigned O) {
10935 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10936 "Expected inlined-at fields to agree");
10937 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
10938}
10939
10940/// FrameIndex with dependencies
10942 DIExpression *Expr, unsigned FI,
10943 ArrayRef<SDNode *> Dependencies,
10944 bool IsIndirect,
10945 const DebugLoc &DL,
10946 unsigned O) {
10947 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10948 "Expected inlined-at fields to agree");
10949 return new (DbgInfo->getAlloc())
10950 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
10951 Dependencies, IsIndirect, DL, O,
10952 /*IsVariadic=*/false);
10953}
10954
10955/// VReg
10957 unsigned VReg, bool IsIndirect,
10958 const DebugLoc &DL, unsigned O) {
10959 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10960 "Expected inlined-at fields to agree");
10961 return new (DbgInfo->getAlloc())
10962 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
10963 {}, IsIndirect, DL, O,
10964 /*IsVariadic=*/false);
10965}
10966
10969 ArrayRef<SDNode *> Dependencies,
10970 bool IsIndirect, const DebugLoc &DL,
10971 unsigned O, bool IsVariadic) {
10972 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
10973 "Expected inlined-at fields to agree");
10974 return new (DbgInfo->getAlloc())
10975 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
10976 DL, O, IsVariadic);
10977}
10978
10980 unsigned OffsetInBits, unsigned SizeInBits,
10981 bool InvalidateDbg) {
10982 SDNode *FromNode = From.getNode();
10983 SDNode *ToNode = To.getNode();
10984 assert(FromNode && ToNode && "Can't modify dbg values");
10985
10986 // PR35338
10987 // TODO: assert(From != To && "Redundant dbg value transfer");
10988 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
10989 if (From == To || FromNode == ToNode)
10990 return;
10991
10992 if (!FromNode->getHasDebugValue())
10993 return;
10994
10995 SDDbgOperand FromLocOp =
10996 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
10998
11000 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11001 if (Dbg->isInvalidated())
11002 continue;
11003
11004 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11005
11006 // Create a new location ops vector that is equal to the old vector, but
11007 // with each instance of FromLocOp replaced with ToLocOp.
11008 bool Changed = false;
11009 auto NewLocOps = Dbg->copyLocationOps();
11010 std::replace_if(
11011 NewLocOps.begin(), NewLocOps.end(),
11012 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11013 bool Match = Op == FromLocOp;
11014 Changed |= Match;
11015 return Match;
11016 },
11017 ToLocOp);
11018 // Ignore this SDDbgValue if we didn't find a matching location.
11019 if (!Changed)
11020 continue;
11021
11022 DIVariable *Var = Dbg->getVariable();
11023 auto *Expr = Dbg->getExpression();
11024 // If a fragment is requested, update the expression.
11025 if (SizeInBits) {
11026 // When splitting a larger (e.g., sign-extended) value whose
11027 // lower bits are described with an SDDbgValue, do not attempt
11028 // to transfer the SDDbgValue to the upper bits.
11029 if (auto FI = Expr->getFragmentInfo())
11030 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11031 continue;
11032 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11033 SizeInBits);
11034 if (!Fragment)
11035 continue;
11036 Expr = *Fragment;
11037 }
11038
11039 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11040 // Clone the SDDbgValue and move it to To.
11041 SDDbgValue *Clone = getDbgValueList(
11042 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11043 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11044 Dbg->isVariadic());
11045 ClonedDVs.push_back(Clone);
11046
11047 if (InvalidateDbg) {
11048 // Invalidate value and indicate the SDDbgValue should not be emitted.
11049 Dbg->setIsInvalidated();
11050 Dbg->setIsEmitted();
11051 }
11052 }
11053
11054 for (SDDbgValue *Dbg : ClonedDVs) {
11055 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11056 "Transferred DbgValues should depend on the new SDNode");
11057 AddDbgValue(Dbg, false);
11058 }
11059}
11060
11062 if (!N.getHasDebugValue())
11063 return;
11064
11066 for (auto *DV : GetDbgValues(&N)) {
11067 if (DV->isInvalidated())
11068 continue;
11069 switch (N.getOpcode()) {
11070 default:
11071 break;
11072 case ISD::ADD: {
11073 SDValue N0 = N.getOperand(0);
11074 SDValue N1 = N.getOperand(1);
11075 if (!isa<ConstantSDNode>(N0)) {
11076 bool RHSConstant = isa<ConstantSDNode>(N1);
11078 if (RHSConstant)
11079 Offset = N.getConstantOperandVal(1);
11080 // We are not allowed to turn indirect debug values variadic, so
11081 // don't salvage those.
11082 if (!RHSConstant && DV->isIndirect())
11083 continue;
11084
11085 // Rewrite an ADD constant node into a DIExpression. Since we are
11086 // performing arithmetic to compute the variable's *value* in the
11087 // DIExpression, we need to mark the expression with a
11088 // DW_OP_stack_value.
11089 auto *DIExpr = DV->getExpression();
11090 auto NewLocOps = DV->copyLocationOps();
11091 bool Changed = false;
11092 size_t OrigLocOpsSize = NewLocOps.size();
11093 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11094 // We're not given a ResNo to compare against because the whole
11095 // node is going away. We know that any ISD::ADD only has one
11096 // result, so we can assume any node match is using the result.
11097 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11098 NewLocOps[i].getSDNode() != &N)
11099 continue;
11100 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11101 if (RHSConstant) {
11104 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
11105 } else {
11106 // Convert to a variadic expression (if not already).
11107 // convertToVariadicExpression() returns a const pointer, so we use
11108 // a temporary const variable here.
11109 const auto *TmpDIExpr =
11113 ExprOps.push_back(NewLocOps.size());
11114 ExprOps.push_back(dwarf::DW_OP_plus);
11117 NewLocOps.push_back(RHS);
11118 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
11119 }
11120 Changed = true;
11121 }
11122 (void)Changed;
11123 assert(Changed && "Salvage target doesn't use N");
11124
11125 bool IsVariadic =
11126 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
11127
11128 auto AdditionalDependencies = DV->getAdditionalDependencies();
11129 SDDbgValue *Clone = getDbgValueList(
11130 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
11131 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
11132 ClonedDVs.push_back(Clone);
11133 DV->setIsInvalidated();
11134 DV->setIsEmitted();
11135 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
11136 N0.getNode()->dumprFull(this);
11137 dbgs() << " into " << *DIExpr << '\n');
11138 }
11139 break;
11140 }
11141 case ISD::TRUNCATE: {
11142 SDValue N0 = N.getOperand(0);
11143 TypeSize FromSize = N0.getValueSizeInBits();
11144 TypeSize ToSize = N.getValueSizeInBits(0);
11145
11146 DIExpression *DbgExpression = DV->getExpression();
11147 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
11148 auto NewLocOps = DV->copyLocationOps();
11149 bool Changed = false;
11150 for (size_t i = 0; i < NewLocOps.size(); ++i) {
11151 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11152 NewLocOps[i].getSDNode() != &N)
11153 continue;
11154
11155 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11156 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
11157 Changed = true;
11158 }
11159 assert(Changed && "Salvage target doesn't use N");
11160 (void)Changed;
11161
11162 SDDbgValue *Clone =
11163 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
11164 DV->getAdditionalDependencies(), DV->isIndirect(),
11165 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
11166
11167 ClonedDVs.push_back(Clone);
11168 DV->setIsInvalidated();
11169 DV->setIsEmitted();
11170 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
11171 dbgs() << " into " << *DbgExpression << '\n');
11172 break;
11173 }
11174 }
11175 }
11176
11177 for (SDDbgValue *Dbg : ClonedDVs) {
11178 assert(!Dbg->getSDNodes().empty() &&
11179 "Salvaged DbgValue should depend on a new SDNode");
11180 AddDbgValue(Dbg, false);
11181 }
11182}
11183
11184/// Creates a SDDbgLabel node.
11186 const DebugLoc &DL, unsigned O) {
11187 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
11188 "Expected inlined-at fields to agree");
11189 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
11190}
11191
11192namespace {
11193
11194/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
11195/// pointed to by a use iterator is deleted, increment the use iterator
11196/// so that it doesn't dangle.
11197///
11198class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
11201
11202 void NodeDeleted(SDNode *N, SDNode *E) override {
11203 // Increment the iterator as needed.
11204 while (UI != UE && N == *UI)
11205 ++UI;
11206 }
11207
11208public:
11209 RAUWUpdateListener(SelectionDAG &d,
11212 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11213};
11214
11215} // end anonymous namespace
11216
11217/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11218/// This can cause recursive merging of nodes in the DAG.
11219///
11220/// This version assumes From has a single result value.
11221///
11223 SDNode *From = FromN.getNode();
11224 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11225 "Cannot replace with this method!");
11226 assert(From != To.getNode() && "Cannot replace uses of with self");
11227
11228 // Preserve Debug Values
11229 transferDbgValues(FromN, To);
11230 // Preserve extra info.
11231 copyExtraInfo(From, To.getNode());
11232
11233 // Iterate over all the existing uses of From. New uses will be added
11234 // to the beginning of the use list, which we avoid visiting.
11235 // This specifically avoids visiting uses of From that arise while the
11236 // replacement is happening, because any such uses would be the result
11237 // of CSE: If an existing node looks like From after one of its operands
11238 // is replaced by To, we don't want to replace of all its users with To
11239 // too. See PR3018 for more info.
11240 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11241 RAUWUpdateListener Listener(*this, UI, UE);
11242 while (UI != UE) {
11243 SDNode *User = *UI;
11244
11245 // This node is about to morph, remove its old self from the CSE maps.
11246 RemoveNodeFromCSEMaps(User);
11247
11248 // A user can appear in a use list multiple times, and when this
11249 // happens the uses are usually next to each other in the list.
11250 // To help reduce the number of CSE recomputations, process all
11251 // the uses of this user that we can find this way.
11252 do {
11253 SDUse &Use = UI.getUse();
11254 ++UI;
11255 Use.set(To);
11256 if (To->isDivergent() != From->isDivergent())
11258 } while (UI != UE && *UI == User);
11259 // Now that we have modified User, add it back to the CSE maps. If it
11260 // already exists there, recursively merge the results together.
11261 AddModifiedNodeToCSEMaps(User);
11262 }
11263
11264 // If we just RAUW'd the root, take note.
11265 if (FromN == getRoot())
11266 setRoot(To);
11267}
11268
11269/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11270/// This can cause recursive merging of nodes in the DAG.
11271///
11272/// This version assumes that for each value of From, there is a
11273/// corresponding value in To in the same position with the same type.
11274///
11276#ifndef NDEBUG
11277 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11278 assert((!From->hasAnyUseOfValue(i) ||
11279 From->getValueType(i) == To->getValueType(i)) &&
11280 "Cannot use this version of ReplaceAllUsesWith!");
11281#endif
11282
11283 // Handle the trivial case.
11284 if (From == To)
11285 return;
11286
11287 // Preserve Debug Info. Only do this if there's a use.
11288 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11289 if (From->hasAnyUseOfValue(i)) {
11290 assert((i < To->getNumValues()) && "Invalid To location");
11292 }
11293 // Preserve extra info.
11294 copyExtraInfo(From, To);
11295
11296 // Iterate over just the existing users of From. See the comments in
11297 // the ReplaceAllUsesWith above.
11298 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11299 RAUWUpdateListener Listener(*this, UI, UE);
11300 while (UI != UE) {
11301 SDNode *User = *UI;
11302
11303 // This node is about to morph, remove its old self from the CSE maps.
11304 RemoveNodeFromCSEMaps(User);
11305
11306 // A user can appear in a use list multiple times, and when this
11307 // happens the uses are usually next to each other in the list.
11308 // To help reduce the number of CSE recomputations, process all
11309 // the uses of this user that we can find this way.
11310 do {
11311 SDUse &Use = UI.getUse();
11312 ++UI;
11313 Use.setNode(To);
11314 if (To->isDivergent() != From->isDivergent())
11316 } while (UI != UE && *UI == User);
11317
11318 // Now that we have modified User, add it back to the CSE maps. If it
11319 // already exists there, recursively merge the results together.
11320 AddModifiedNodeToCSEMaps(User);
11321 }
11322
11323 // If we just RAUW'd the root, take note.
11324 if (From == getRoot().getNode())
11325 setRoot(SDValue(To, getRoot().getResNo()));
11326}
11327
11328/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11329/// This can cause recursive merging of nodes in the DAG.
11330///
11331/// This version can replace From with any result values. To must match the
11332/// number and types of values returned by From.
11334 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11335 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11336
11337 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11338 // Preserve Debug Info.
11339 transferDbgValues(SDValue(From, i), To[i]);
11340 // Preserve extra info.
11341 copyExtraInfo(From, To[i].getNode());
11342 }
11343
11344 // Iterate over just the existing users of From. See the comments in
11345 // the ReplaceAllUsesWith above.
11346 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11347 RAUWUpdateListener Listener(*this, UI, UE);
11348 while (UI != UE) {
11349 SDNode *User = *UI;
11350
11351 // This node is about to morph, remove its old self from the CSE maps.
11352 RemoveNodeFromCSEMaps(User);
11353
11354 // A user can appear in a use list multiple times, and when this happens the
11355 // uses are usually next to each other in the list. To help reduce the
11356 // number of CSE and divergence recomputations, process all the uses of this
11357 // user that we can find this way.
11358 bool To_IsDivergent = false;
11359 do {
11360 SDUse &Use = UI.getUse();
11361 const SDValue &ToOp = To[Use.getResNo()];
11362 ++UI;
11363 Use.set(ToOp);
11364 To_IsDivergent |= ToOp->isDivergent();
11365 } while (UI != UE && *UI == User);
11366
11367 if (To_IsDivergent != From->isDivergent())
11369
11370 // Now that we have modified User, add it back to the CSE maps. If it
11371 // already exists there, recursively merge the results together.
11372 AddModifiedNodeToCSEMaps(User);
11373 }
11374
11375 // If we just RAUW'd the root, take note.
11376 if (From == getRoot().getNode())
11377 setRoot(SDValue(To[getRoot().getResNo()]));
11378}
11379
11380/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11381/// uses of other values produced by From.getNode() alone. The Deleted
11382/// vector is handled the same way as for ReplaceAllUsesWith.
11384 // Handle the really simple, really trivial case efficiently.
11385 if (From == To) return;
11386
11387 // Handle the simple, trivial, case efficiently.
11388 if (From.getNode()->getNumValues() == 1) {
11390 return;
11391 }
11392
11393 // Preserve Debug Info.
11395 copyExtraInfo(From.getNode(), To.getNode());
11396
11397 // Iterate over just the existing users of From. See the comments in
11398 // the ReplaceAllUsesWith above.
11399 SDNode::use_iterator UI = From.getNode()->use_begin(),
11400 UE = From.getNode()->use_end();
11401 RAUWUpdateListener Listener(*this, UI, UE);
11402 while (UI != UE) {
11403 SDNode *User = *UI;
11404 bool UserRemovedFromCSEMaps = false;
11405
11406 // A user can appear in a use list multiple times, and when this
11407 // happens the uses are usually next to each other in the list.
11408 // To help reduce the number of CSE recomputations, process all
11409 // the uses of this user that we can find this way.
11410 do {
11411 SDUse &Use = UI.getUse();
11412
11413 // Skip uses of different values from the same node.
11414 if (Use.getResNo() != From.getResNo()) {
11415 ++UI;
11416 continue;
11417 }
11418
11419 // If this node hasn't been modified yet, it's still in the CSE maps,
11420 // so remove its old self from the CSE maps.
11421 if (!UserRemovedFromCSEMaps) {
11422 RemoveNodeFromCSEMaps(User);
11423 UserRemovedFromCSEMaps = true;
11424 }
11425
11426 ++UI;
11427 Use.set(To);
11428 if (To->isDivergent() != From->isDivergent())
11430 } while (UI != UE && *UI == User);
11431 // We are iterating over all uses of the From node, so if a use
11432 // doesn't use the specific value, no changes are made.
11433 if (!UserRemovedFromCSEMaps)
11434 continue;
11435
11436 // Now that we have modified User, add it back to the CSE maps. If it
11437 // already exists there, recursively merge the results together.
11438 AddModifiedNodeToCSEMaps(User);
11439 }
11440
11441 // If we just RAUW'd the root, take note.
11442 if (From == getRoot())
11443 setRoot(To);
11444}
11445
11446namespace {
11447
11448/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11449/// to record information about a use.
11450struct UseMemo {
11451 SDNode *User;
11452 unsigned Index;
11453 SDUse *Use;
11454};
11455
11456/// operator< - Sort Memos by User.
11457bool operator<(const UseMemo &L, const UseMemo &R) {
11458 return (intptr_t)L.User < (intptr_t)R.User;
11459}
11460
11461/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11462/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11463/// the node already has been taken care of recursively.
11464class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11466
11467 void NodeDeleted(SDNode *N, SDNode *E) override {
11468 for (UseMemo &Memo : Uses)
11469 if (Memo.User == N)
11470 Memo.User = nullptr;
11471 }
11472
11473public:
11474 RAUOVWUpdateListener(SelectionDAG &d, SmallVector<UseMemo, 4> &uses)
11475 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11476};
11477
11478} // end anonymous namespace
11479
11481 if (TLI->isSDNodeAlwaysUniform(N)) {
11482 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11483 "Conflicting divergence information!");
11484 return false;
11485 }
11486 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11487 return true;
11488 for (const auto &Op : N->ops()) {
11489 if (Op.Val.getValueType() != MVT::Other && Op.getNode()->isDivergent())
11490 return true;
11491 }
11492 return false;
11493}
11494
11496 SmallVector<SDNode *, 16> Worklist(1, N);
11497 do {
11498 N = Worklist.pop_back_val();
11499 bool IsDivergent = calculateDivergence(N);
11500 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11501 N->SDNodeBits.IsDivergent = IsDivergent;
11502 llvm::append_range(Worklist, N->uses());
11503 }
11504 } while (!Worklist.empty());
11505}
11506
11507void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11509 Order.reserve(AllNodes.size());
11510 for (auto &N : allnodes()) {
11511 unsigned NOps = N.getNumOperands();
11512 Degree[&N] = NOps;
11513 if (0 == NOps)
11514 Order.push_back(&N);
11515 }
11516 for (size_t I = 0; I != Order.size(); ++I) {
11517 SDNode *N = Order[I];
11518 for (auto *U : N->uses()) {
11519 unsigned &UnsortedOps = Degree[U];
11520 if (0 == --UnsortedOps)
11521 Order.push_back(U);
11522 }
11523 }
11524}
11525
11526#ifndef NDEBUG
11528 std::vector<SDNode *> TopoOrder;
11529 CreateTopologicalOrder(TopoOrder);
11530 for (auto *N : TopoOrder) {
11531 assert(calculateDivergence(N) == N->isDivergent() &&
11532 "Divergence bit inconsistency detected");
11533 }
11534}
11535#endif
11536
11537/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11538/// uses of other values produced by From.getNode() alone. The same value
11539/// may appear in both the From and To list. The Deleted vector is
11540/// handled the same way as for ReplaceAllUsesWith.
11542 const SDValue *To,
11543 unsigned Num){
11544 // Handle the simple, trivial case efficiently.
11545 if (Num == 1)
11546 return ReplaceAllUsesOfValueWith(*From, *To);
11547
11548 transferDbgValues(*From, *To);
11549 copyExtraInfo(From->getNode(), To->getNode());
11550
11551 // Read up all the uses and make records of them. This helps
11552 // processing new uses that are introduced during the
11553 // replacement process.
11555 for (unsigned i = 0; i != Num; ++i) {
11556 unsigned FromResNo = From[i].getResNo();
11557 SDNode *FromNode = From[i].getNode();
11558 for (SDNode::use_iterator UI = FromNode->use_begin(),
11559 E = FromNode->use_end(); UI != E; ++UI) {
11560 SDUse &Use = UI.getUse();
11561 if (Use.getResNo() == FromResNo) {
11562 UseMemo Memo = { *UI, i, &Use };
11563 Uses.push_back(Memo);
11564 }
11565 }
11566 }
11567
11568 // Sort the uses, so that all the uses from a given User are together.
11570 RAUOVWUpdateListener Listener(*this, Uses);
11571
11572 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
11573 UseIndex != UseIndexEnd; ) {
11574 // We know that this user uses some value of From. If it is the right
11575 // value, update it.
11576 SDNode *User = Uses[UseIndex].User;
11577 // If the node has been deleted by recursive CSE updates when updating
11578 // another node, then just skip this entry.
11579 if (User == nullptr) {
11580 ++UseIndex;
11581 continue;
11582 }
11583
11584 // This node is about to morph, remove its old self from the CSE maps.
11585 RemoveNodeFromCSEMaps(User);
11586
11587 // The Uses array is sorted, so all the uses for a given User
11588 // are next to each other in the list.
11589 // To help reduce the number of CSE recomputations, process all
11590 // the uses of this user that we can find this way.
11591 do {
11592 unsigned i = Uses[UseIndex].Index;
11593 SDUse &Use = *Uses[UseIndex].Use;
11594 ++UseIndex;
11595
11596 Use.set(To[i]);
11597 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
11598
11599 // Now that we have modified User, add it back to the CSE maps. If it
11600 // already exists there, recursively merge the results together.
11601 AddModifiedNodeToCSEMaps(User);
11602 }
11603}
11604
11605/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
11606/// based on their topological order. It returns the maximum id and a vector
11607/// of the SDNodes* in assigned order by reference.
11609 unsigned DAGSize = 0;
11610
11611 // SortedPos tracks the progress of the algorithm. Nodes before it are
11612 // sorted, nodes after it are unsorted. When the algorithm completes
11613 // it is at the end of the list.
11614 allnodes_iterator SortedPos = allnodes_begin();
11615
11616 // Visit all the nodes. Move nodes with no operands to the front of
11617 // the list immediately. Annotate nodes that do have operands with their
11618 // operand count. Before we do this, the Node Id fields of the nodes
11619 // may contain arbitrary values. After, the Node Id fields for nodes
11620 // before SortedPos will contain the topological sort index, and the
11621 // Node Id fields for nodes At SortedPos and after will contain the
11622 // count of outstanding operands.
11624 checkForCycles(&N, this);
11625 unsigned Degree = N.getNumOperands();
11626 if (Degree == 0) {
11627 // A node with no uses, add it to the result array immediately.
11628 N.setNodeId(DAGSize++);
11629 allnodes_iterator Q(&N);
11630 if (Q != SortedPos)
11631 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
11632 assert(SortedPos != AllNodes.end() && "Overran node list");
11633 ++SortedPos;
11634 } else {
11635 // Temporarily use the Node Id as scratch space for the degree count.
11636 N.setNodeId(Degree);
11637 }
11638 }
11639
11640 // Visit all the nodes. As we iterate, move nodes into sorted order,
11641 // such that by the time the end is reached all nodes will be sorted.
11642 for (SDNode &Node : allnodes()) {
11643 SDNode *N = &Node;
11644 checkForCycles(N, this);
11645 // N is in sorted position, so all its uses have one less operand
11646 // that needs to be sorted.
11647 for (SDNode *P : N->uses()) {
11648 unsigned Degree = P->getNodeId();
11649 assert(Degree != 0 && "Invalid node degree");
11650 --Degree;
11651 if (Degree == 0) {
11652 // All of P's operands are sorted, so P may sorted now.
11653 P->setNodeId(DAGSize++);
11654 if (P->getIterator() != SortedPos)
11655 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
11656 assert(SortedPos != AllNodes.end() && "Overran node list");
11657 ++SortedPos;
11658 } else {
11659 // Update P's outstanding operand count.
11660 P->setNodeId(Degree);
11661 }
11662 }
11663 if (Node.getIterator() == SortedPos) {
11664#ifndef NDEBUG
11666 SDNode *S = &*++I;
11667 dbgs() << "Overran sorted position:\n";
11668 S->dumprFull(this); dbgs() << "\n";
11669 dbgs() << "Checking if this is due to cycles\n";
11670 checkForCycles(this, true);
11671#endif
11672 llvm_unreachable(nullptr);
11673 }
11674 }
11675
11676 assert(SortedPos == AllNodes.end() &&
11677 "Topological sort incomplete!");
11678 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
11679 "First node in topological sort is not the entry token!");
11680 assert(AllNodes.front().getNodeId() == 0 &&
11681 "First node in topological sort has non-zero id!");
11682 assert(AllNodes.front().getNumOperands() == 0 &&
11683 "First node in topological sort has operands!");
11684 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
11685 "Last node in topologic sort has unexpected id!");
11686 assert(AllNodes.back().use_empty() &&
11687 "Last node in topologic sort has users!");
11688 assert(DAGSize == allnodes_size() && "Node count mismatch!");
11689 return DAGSize;
11690}
11691
11692/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
11693/// value is produced by SD.
11694void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
11695 for (SDNode *SD : DB->getSDNodes()) {
11696 if (!SD)
11697 continue;
11698 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
11699 SD->setHasDebugValue(true);
11700 }
11701 DbgInfo->add(DB, isParameter);
11702}
11703
11704void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
11705
11707 SDValue NewMemOpChain) {
11708 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
11709 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
11710 // The new memory operation must have the same position as the old load in
11711 // terms of memory dependency. Create a TokenFactor for the old load and new
11712 // memory operation and update uses of the old load's output chain to use that
11713 // TokenFactor.
11714 if (OldChain == NewMemOpChain || OldChain.use_empty())
11715 return NewMemOpChain;
11716
11717 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
11718 OldChain, NewMemOpChain);
11719 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
11720 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
11721 return TokenFactor;
11722}
11723
11725 SDValue NewMemOp) {
11726 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
11727 SDValue OldChain = SDValue(OldLoad, 1);
11728 SDValue NewMemOpChain = NewMemOp.getValue(1);
11729 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
11730}
11731
11733 Function **OutFunction) {
11734 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
11735
11736 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
11737 auto *Module = MF->getFunction().getParent();
11738 auto *Function = Module->getFunction(Symbol);
11739
11740 if (OutFunction != nullptr)
11741 *OutFunction = Function;
11742
11743 if (Function != nullptr) {
11744 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
11745 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
11746 }
11747
11748 std::string ErrorStr;
11749 raw_string_ostream ErrorFormatter(ErrorStr);
11750 ErrorFormatter << "Undefined external symbol ";
11751 ErrorFormatter << '"' << Symbol << '"';
11752 report_fatal_error(Twine(ErrorFormatter.str()));
11753}
11754
11755//===----------------------------------------------------------------------===//
11756// SDNode Class
11757//===----------------------------------------------------------------------===//
11758
11760 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11761 return Const != nullptr && Const->isZero();
11762}
11763
11765 return V.isUndef() || isNullConstant(V);
11766}
11767
11769 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
11770 return Const != nullptr && Const->isZero() && !Const->isNegative();
11771}
11772
11774 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11775 return Const != nullptr && Const->isAllOnes();
11776}
11777
11779 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11780 return Const != nullptr && Const->isOne();
11781}
11782
11784 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11785 return Const != nullptr && Const->isMinSignedValue();
11786}
11787
11788bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
11789 unsigned OperandNo) {
11790 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
11791 // TODO: Target-specific opcodes could be added.
11792 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
11793 /*AllowTruncation*/ true)) {
11794 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
11795 switch (Opcode) {
11796 case ISD::ADD:
11797 case ISD::OR:
11798 case ISD::XOR:
11799 case ISD::UMAX:
11800 return Const.isZero();
11801 case ISD::MUL:
11802 return Const.isOne();
11803 case ISD::AND:
11804 case ISD::UMIN:
11805 return Const.isAllOnes();
11806 case ISD::SMAX:
11807 return Const.isMinSignedValue();
11808 case ISD::SMIN:
11809 return Const.isMaxSignedValue();
11810 case ISD::SUB:
11811 case ISD::SHL:
11812 case ISD::SRA:
11813 case ISD::SRL:
11814 return OperandNo == 1 && Const.isZero();
11815 case ISD::UDIV:
11816 case ISD::SDIV:
11817 return OperandNo == 1 && Const.isOne();
11818 }
11819 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
11820 switch (Opcode) {
11821 case ISD::FADD:
11822 return ConstFP->isZero() &&
11823 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
11824 case ISD::FSUB:
11825 return OperandNo == 1 && ConstFP->isZero() &&
11826 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
11827 case ISD::FMUL:
11828 return ConstFP->isExactlyValue(1.0);
11829 case ISD::FDIV:
11830 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
11831 case ISD::FMINNUM:
11832 case ISD::FMAXNUM: {
11833 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
11834 EVT VT = V.getValueType();
11836 APFloat NeutralAF = !Flags.hasNoNaNs()
11837 ? APFloat::getQNaN(Semantics)
11838 : !Flags.hasNoInfs()
11839 ? APFloat::getInf(Semantics)
11840 : APFloat::getLargest(Semantics);
11841 if (Opcode == ISD::FMAXNUM)
11842 NeutralAF.changeSign();
11843
11844 return ConstFP->isExactlyValue(NeutralAF);
11845 }
11846 }
11847 }
11848 return false;
11849}
11850
11852 while (V.getOpcode() == ISD::BITCAST)
11853 V = V.getOperand(0);
11854 return V;
11855}
11856
11858 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
11859 V = V.getOperand(0);
11860 return V;
11861}
11862
11864 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11865 V = V.getOperand(0);
11866 return V;
11867}
11868
11870 while (V.getOpcode() == ISD::TRUNCATE)
11871 V = V.getOperand(0);
11872 return V;
11873}
11874
11875bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
11876 if (V.getOpcode() != ISD::XOR)
11877 return false;
11878 V = peekThroughBitcasts(V.getOperand(1));
11879 unsigned NumBits = V.getScalarValueSizeInBits();
11880 ConstantSDNode *C =
11881 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
11882 return C && (C->getAPIntValue().countr_one() >= NumBits);
11883}
11884
11886 bool AllowTruncation) {
11887 EVT VT = N.getValueType();
11888 APInt DemandedElts = VT.isFixedLengthVector()
11890 : APInt(1, 1);
11891 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
11892}
11893
11895 bool AllowUndefs,
11896 bool AllowTruncation) {
11897 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
11898 return CN;
11899
11900 // SplatVectors can truncate their operands. Ignore that case here unless
11901 // AllowTruncation is set.
11902 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
11903 EVT VecEltVT = N->getValueType(0).getVectorElementType();
11904 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
11905 EVT CVT = CN->getValueType(0);
11906 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
11907 if (AllowTruncation || CVT == VecEltVT)
11908 return CN;
11909 }
11910 }
11911
11912 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
11913 BitVector UndefElements;
11914 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
11915
11916 // BuildVectors can truncate their operands. Ignore that case here unless
11917 // AllowTruncation is set.
11918 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
11919 if (CN && (UndefElements.none() || AllowUndefs)) {
11920 EVT CVT = CN->getValueType(0);
11921 EVT NSVT = N.getValueType().getScalarType();
11922 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
11923 if (AllowTruncation || (CVT == NSVT))
11924 return CN;
11925 }
11926 }
11927
11928 return nullptr;
11929}
11930
11932 EVT VT = N.getValueType();
11933 APInt DemandedElts = VT.isFixedLengthVector()
11935 : APInt(1, 1);
11936 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
11937}
11938
11940 const APInt &DemandedElts,
11941 bool AllowUndefs) {
11942 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
11943 return CN;
11944
11945 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
11946 BitVector UndefElements;
11947 ConstantFPSDNode *CN =
11948 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
11949 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
11950 if (CN && (UndefElements.none() || AllowUndefs))
11951 return CN;
11952 }
11953
11954 if (N.getOpcode() == ISD::SPLAT_VECTOR)
11955 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
11956 return CN;
11957
11958 return nullptr;
11959}
11960
11961bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
11962 // TODO: may want to use peekThroughBitcast() here.
11963 ConstantSDNode *C =
11964 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
11965 return C && C->isZero();
11966}
11967
11968bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
11969 ConstantSDNode *C =
11970 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
11971 return C && C->isOne();
11972}
11973
11974bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
11976 unsigned BitWidth = N.getScalarValueSizeInBits();
11977 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
11978 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
11979}
11980
11982 DropOperands();
11983}
11984
11985MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
11986 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
11987 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
11988 MemSDNodeBits.IsVolatile = MMO->isVolatile();
11989 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
11990 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
11991 MemSDNodeBits.IsInvariant = MMO->isInvariant();
11992
11993 // We check here that the size of the memory operand fits within the size of
11994 // the MMO. This is because the MMO might indicate only a possible address
11995 // range instead of specifying the affected memory addresses precisely.
11996 assert(
11997 (!MMO->getType().isValid() ||
11999 "Size mismatch!");
12000}
12001
12002/// Profile - Gather unique data for the node.
12003///
12005 AddNodeIDNode(ID, this);
12006}
12007
12008namespace {
12009
12010 struct EVTArray {
12011 std::vector<EVT> VTs;
12012
12013 EVTArray() {
12014 VTs.reserve(MVT::VALUETYPE_SIZE);
12015 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
12016 VTs.push_back(MVT((MVT::SimpleValueType)i));
12017 }
12018 };
12019
12020} // end anonymous namespace
12021
12022/// getValueTypeList - Return a pointer to the specified value type.
12023///
12024const EVT *SDNode::getValueTypeList(EVT VT) {
12025 static std::set<EVT, EVT::compareRawBits> EVTs;
12026 static EVTArray SimpleVTArray;
12027 static sys::SmartMutex<true> VTMutex;
12028
12029 if (VT.isExtended()) {
12030 sys::SmartScopedLock<true> Lock(VTMutex);
12031 return &(*EVTs.insert(VT).first);
12032 }
12033 assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
12034 return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
12035}
12036
12037/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
12038/// indicated value. This method ignores uses of other values defined by this
12039/// operation.
12040bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
12041 assert(Value < getNumValues() && "Bad value!");
12042
12043 // TODO: Only iterate over uses of a given value of the node
12044 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
12045 if (UI.getUse().getResNo() == Value) {
12046 if (NUses == 0)
12047 return false;
12048 --NUses;
12049 }
12050 }
12051
12052 // Found exactly the right number of uses?
12053 return NUses == 0;
12054}
12055
12056/// hasAnyUseOfValue - Return true if there are any use of the indicated
12057/// value. This method ignores uses of other values defined by this operation.
12058bool SDNode::hasAnyUseOfValue(unsigned Value) const {
12059 assert(Value < getNumValues() && "Bad value!");
12060
12061 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
12062 if (UI.getUse().getResNo() == Value)
12063 return true;
12064
12065 return false;
12066}
12067
12068/// isOnlyUserOf - Return true if this node is the only use of N.
12069bool SDNode::isOnlyUserOf(const SDNode *N) const {
12070 bool Seen = false;
12071 for (const SDNode *User : N->uses()) {
12072 if (User == this)
12073 Seen = true;
12074 else
12075 return false;
12076 }
12077
12078 return Seen;
12079}
12080
12081/// Return true if the only users of N are contained in Nodes.
12083 bool Seen = false;
12084 for (const SDNode *User : N->uses()) {
12085 if (llvm::is_contained(Nodes, User))
12086 Seen = true;
12087 else
12088 return false;
12089 }
12090
12091 return Seen;
12092}
12093
12094/// isOperand - Return true if this node is an operand of N.
12095bool SDValue::isOperandOf(const SDNode *N) const {
12096 return is_contained(N->op_values(), *this);
12097}
12098
12099bool SDNode::isOperandOf(const SDNode *N) const {
12100 return any_of(N->op_values(),
12101 [this](SDValue Op) { return this == Op.getNode(); });
12102}
12103
12104/// reachesChainWithoutSideEffects - Return true if this operand (which must
12105/// be a chain) reaches the specified operand without crossing any
12106/// side-effecting instructions on any chain path. In practice, this looks
12107/// through token factors and non-volatile loads. In order to remain efficient,
12108/// this only looks a couple of nodes in, it does not do an exhaustive search.
12109///
12110/// Note that we only need to examine chains when we're searching for
12111/// side-effects; SelectionDAG requires that all side-effects are represented
12112/// by chains, even if another operand would force a specific ordering. This
12113/// constraint is necessary to allow transformations like splitting loads.
12115 unsigned Depth) const {
12116 if (*this == Dest) return true;
12117
12118 // Don't search too deeply, we just want to be able to see through
12119 // TokenFactor's etc.
12120 if (Depth == 0) return false;
12121
12122 // If this is a token factor, all inputs to the TF happen in parallel.
12123 if (getOpcode() == ISD::TokenFactor) {
12124 // First, try a shallow search.
12125 if (is_contained((*this)->ops(), Dest)) {
12126 // We found the chain we want as an operand of this TokenFactor.
12127 // Essentially, we reach the chain without side-effects if we could
12128 // serialize the TokenFactor into a simple chain of operations with
12129 // Dest as the last operation. This is automatically true if the
12130 // chain has one use: there are no other ordering constraints.
12131 // If the chain has more than one use, we give up: some other
12132 // use of Dest might force a side-effect between Dest and the current
12133 // node.
12134 if (Dest.hasOneUse())
12135 return true;
12136 }
12137 // Next, try a deep search: check whether every operand of the TokenFactor
12138 // reaches Dest.
12139 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
12140 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
12141 });
12142 }
12143
12144 // Loads don't have side effects, look through them.
12145 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
12146 if (Ld->isUnordered())
12147 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
12148 }
12149 return false;
12150}
12151
12152bool SDNode::hasPredecessor(const SDNode *N) const {
12155 Worklist.push_back(this);
12156 return hasPredecessorHelper(N, Visited, Worklist);
12157}
12158
12160 this->Flags.intersectWith(Flags);
12161}
12162
12163SDValue
12165 ArrayRef<ISD::NodeType> CandidateBinOps,
12166 bool AllowPartials) {
12167 // The pattern must end in an extract from index 0.
12168 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
12169 !isNullConstant(Extract->getOperand(1)))
12170 return SDValue();
12171
12172 // Match against one of the candidate binary ops.
12173 SDValue Op = Extract->getOperand(0);
12174 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
12175 return Op.getOpcode() == unsigned(BinOp);
12176 }))
12177 return SDValue();
12178
12179 // Floating-point reductions may require relaxed constraints on the final step
12180 // of the reduction because they may reorder intermediate operations.
12181 unsigned CandidateBinOp = Op.getOpcode();
12182 if (Op.getValueType().isFloatingPoint()) {
12183 SDNodeFlags Flags = Op->getFlags();
12184 switch (CandidateBinOp) {
12185 case ISD::FADD:
12186 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
12187 return SDValue();
12188 break;
12189 default:
12190 llvm_unreachable("Unhandled FP opcode for binop reduction");
12191 }
12192 }
12193
12194 // Matching failed - attempt to see if we did enough stages that a partial
12195 // reduction from a subvector is possible.
12196 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12197 if (!AllowPartials || !Op)
12198 return SDValue();
12199 EVT OpVT = Op.getValueType();
12200 EVT OpSVT = OpVT.getScalarType();
12201 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12202 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12203 return SDValue();
12204 BinOp = (ISD::NodeType)CandidateBinOp;
12205 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12207 };
12208
12209 // At each stage, we're looking for something that looks like:
12210 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12211 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12212 // i32 undef, i32 undef, i32 undef, i32 undef>
12213 // %a = binop <8 x i32> %op, %s
12214 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12215 // we expect something like:
12216 // <4,5,6,7,u,u,u,u>
12217 // <2,3,u,u,u,u,u,u>
12218 // <1,u,u,u,u,u,u,u>
12219 // While a partial reduction match would be:
12220 // <2,3,u,u,u,u,u,u>
12221 // <1,u,u,u,u,u,u,u>
12222 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12223 SDValue PrevOp;
12224 for (unsigned i = 0; i < Stages; ++i) {
12225 unsigned MaskEnd = (1 << i);
12226
12227 if (Op.getOpcode() != CandidateBinOp)
12228 return PartialReduction(PrevOp, MaskEnd);
12229
12230 SDValue Op0 = Op.getOperand(0);
12231 SDValue Op1 = Op.getOperand(1);
12232
12233 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12234 if (Shuffle) {
12235 Op = Op1;
12236 } else {
12237 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12238 Op = Op0;
12239 }
12240
12241 // The first operand of the shuffle should be the same as the other operand
12242 // of the binop.
12243 if (!Shuffle || Shuffle->getOperand(0) != Op)
12244 return PartialReduction(PrevOp, MaskEnd);
12245
12246 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12247 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12248 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12249 return PartialReduction(PrevOp, MaskEnd);
12250
12251 PrevOp = Op;
12252 }
12253
12254 // Handle subvector reductions, which tend to appear after the shuffle
12255 // reduction stages.
12256 while (Op.getOpcode() == CandidateBinOp) {
12257 unsigned NumElts = Op.getValueType().getVectorNumElements();
12258 SDValue Op0 = Op.getOperand(0);
12259 SDValue Op1 = Op.getOperand(1);
12260 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12262 Op0.getOperand(0) != Op1.getOperand(0))
12263 break;
12264 SDValue Src = Op0.getOperand(0);
12265 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12266 if (NumSrcElts != (2 * NumElts))
12267 break;
12268 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12269 Op1.getConstantOperandAPInt(1) == NumElts) &&
12270 !(Op1.getConstantOperandAPInt(1) == 0 &&
12271 Op0.getConstantOperandAPInt(1) == NumElts))
12272 break;
12273 Op = Src;
12274 }
12275
12276 BinOp = (ISD::NodeType)CandidateBinOp;
12277 return Op;
12278}
12279
12281 EVT VT = N->getValueType(0);
12282 EVT EltVT = VT.getVectorElementType();
12283 unsigned NE = VT.getVectorNumElements();
12284
12285 SDLoc dl(N);
12286
12287 // If ResNE is 0, fully unroll the vector op.
12288 if (ResNE == 0)
12289 ResNE = NE;
12290 else if (NE > ResNE)
12291 NE = ResNE;
12292
12293 if (N->getNumValues() == 2) {
12294 SmallVector<SDValue, 8> Scalars0, Scalars1;
12295 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12296 EVT VT1 = N->getValueType(1);
12297 EVT EltVT1 = VT1.getVectorElementType();
12298
12299 unsigned i;
12300 for (i = 0; i != NE; ++i) {
12301 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12302 SDValue Operand = N->getOperand(j);
12303 EVT OperandVT = Operand.getValueType();
12304
12305 // A vector operand; extract a single element.
12306 EVT OperandEltVT = OperandVT.getVectorElementType();
12307 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12308 Operand, getVectorIdxConstant(i, dl));
12309 }
12310
12311 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12312 Scalars0.push_back(EltOp);
12313 Scalars1.push_back(EltOp.getValue(1));
12314 }
12315
12316 SDValue Vec0 = getBuildVector(VT, dl, Scalars0);
12317 SDValue Vec1 = getBuildVector(VT1, dl, Scalars1);
12318 return getMergeValues({Vec0, Vec1}, dl);
12319 }
12320
12321 assert(N->getNumValues() == 1 &&
12322 "Can't unroll a vector with multiple results!");
12323
12325 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12326
12327 unsigned i;
12328 for (i= 0; i != NE; ++i) {
12329 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12330 SDValue Operand = N->getOperand(j);
12331 EVT OperandVT = Operand.getValueType();
12332 if (OperandVT.isVector()) {
12333 // A vector operand; extract a single element.
12334 EVT OperandEltVT = OperandVT.getVectorElementType();
12335 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12336 Operand, getVectorIdxConstant(i, dl));
12337 } else {
12338 // A scalar operand; just use it as is.
12339 Operands[j] = Operand;
12340 }
12341 }
12342
12343 switch (N->getOpcode()) {
12344 default: {
12345 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12346 N->getFlags()));
12347 break;
12348 }
12349 case ISD::VSELECT:
12350 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12351 break;
12352 case ISD::SHL:
12353 case ISD::SRA:
12354 case ISD::SRL:
12355 case ISD::ROTL:
12356 case ISD::ROTR:
12357 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12359 Operands[1])));
12360 break;
12362 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12363 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12364 Operands[0],
12365 getValueType(ExtVT)));
12366 }
12367 }
12368 }
12369
12370 for (; i < ResNE; ++i)
12371 Scalars.push_back(getUNDEF(EltVT));
12372
12373 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12374 return getBuildVector(VecVT, dl, Scalars);
12375}
12376
12377std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12378 SDNode *N, unsigned ResNE) {
12379 unsigned Opcode = N->getOpcode();
12380 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12381 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12382 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12383 "Expected an overflow opcode");
12384
12385 EVT ResVT = N->getValueType(0);
12386 EVT OvVT = N->getValueType(1);
12387 EVT ResEltVT = ResVT.getVectorElementType();
12388 EVT OvEltVT = OvVT.getVectorElementType();
12389 SDLoc dl(N);
12390
12391 // If ResNE is 0, fully unroll the vector op.
12392 unsigned NE = ResVT.getVectorNumElements();
12393 if (ResNE == 0)
12394 ResNE = NE;
12395 else if (NE > ResNE)
12396 NE = ResNE;
12397
12398 SmallVector<SDValue, 8> LHSScalars;
12399 SmallVector<SDValue, 8> RHSScalars;
12400 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12401 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12402
12403 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12404 SDVTList VTs = getVTList(ResEltVT, SVT);
12405 SmallVector<SDValue, 8> ResScalars;
12406 SmallVector<SDValue, 8> OvScalars;
12407 for (unsigned i = 0; i < NE; ++i) {
12408 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12409 SDValue Ov =
12410 getSelect(dl, OvEltVT, Res.getValue(1),
12411 getBoolConstant(true, dl, OvEltVT, ResVT),
12412 getConstant(0, dl, OvEltVT));
12413
12414 ResScalars.push_back(Res);
12415 OvScalars.push_back(Ov);
12416 }
12417
12418 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12419 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12420
12421 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12422 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12423 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12424 getBuildVector(NewOvVT, dl, OvScalars));
12425}
12426
12429 unsigned Bytes,
12430 int Dist) const {
12431 if (LD->isVolatile() || Base->isVolatile())
12432 return false;
12433 // TODO: probably too restrictive for atomics, revisit
12434 if (!LD->isSimple())
12435 return false;
12436 if (LD->isIndexed() || Base->isIndexed())
12437 return false;
12438 if (LD->getChain() != Base->getChain())
12439 return false;
12440 EVT VT = LD->getMemoryVT();
12441 if (VT.getSizeInBits() / 8 != Bytes)
12442 return false;
12443
12444 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12445 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12446
12447 int64_t Offset = 0;
12448 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12449 return (Dist * (int64_t)Bytes == Offset);
12450 return false;
12451}
12452
12453/// InferPtrAlignment - Infer alignment of a load / store address. Return
12454/// std::nullopt if it cannot be inferred.
12456 // If this is a GlobalAddress + cst, return the alignment.
12457 const GlobalValue *GV = nullptr;
12458 int64_t GVOffset = 0;
12459 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12460 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12461 KnownBits Known(PtrWidth);
12463 unsigned AlignBits = Known.countMinTrailingZeros();
12464 if (AlignBits)
12465 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12466 }
12467
12468 // If this is a direct reference to a stack slot, use information about the
12469 // stack slot's alignment.
12470 int FrameIdx = INT_MIN;
12471 int64_t FrameOffset = 0;
12472 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12473 FrameIdx = FI->getIndex();
12474 } else if (isBaseWithConstantOffset(Ptr) &&
12475 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12476 // Handle FI+Cst
12477 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12478 FrameOffset = Ptr.getConstantOperandVal(1);
12479 }
12480
12481 if (FrameIdx != INT_MIN) {
12483 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12484 }
12485
12486 return std::nullopt;
12487}
12488
12489/// Split the scalar node with EXTRACT_ELEMENT using the provided
12490/// VTs and return the low/high part.
12491std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12492 const SDLoc &DL,
12493 const EVT &LoVT,
12494 const EVT &HiVT) {
12495 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12496 "Split node must be a scalar type");
12497 SDValue Lo =
12499 SDValue Hi =
12501 return std::make_pair(Lo, Hi);
12502}
12503
12504/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12505/// which is split (or expanded) into two not necessarily identical pieces.
12506std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12507 // Currently all types are split in half.
12508 EVT LoVT, HiVT;
12509 if (!VT.isVector())
12510 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12511 else
12512 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12513
12514 return std::make_pair(LoVT, HiVT);
12515}
12516
12517/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12518/// type, dependent on an enveloping VT that has been split into two identical
12519/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12520std::pair<EVT, EVT>
12522 bool *HiIsEmpty) const {
12523 EVT EltTp = VT.getVectorElementType();
12524 // Examples:
12525 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12526 // custom VL=9 with enveloping VL=8/8 yields 8/1
12527 // custom VL=10 with enveloping VL=8/8 yields 8/2
12528 // etc.
12529 ElementCount VTNumElts = VT.getVectorElementCount();
12530 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12531 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12532 "Mixing fixed width and scalable vectors when enveloping a type");
12533 EVT LoVT, HiVT;
12534 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12535 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12536 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12537 *HiIsEmpty = false;
12538 } else {
12539 // Flag that hi type has zero storage size, but return split envelop type
12540 // (this would be easier if vector types with zero elements were allowed).
12541 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12542 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12543 *HiIsEmpty = true;
12544 }
12545 return std::make_pair(LoVT, HiVT);
12546}
12547
12548/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
12549/// low/high part.
12550std::pair<SDValue, SDValue>
12551SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
12552 const EVT &HiVT) {
12553 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
12554 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
12555 "Splitting vector with an invalid mixture of fixed and scalable "
12556 "vector types");
12558 N.getValueType().getVectorMinNumElements() &&
12559 "More vector elements requested than available!");
12560 SDValue Lo, Hi;
12561 Lo =
12563 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
12564 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
12565 // IDX with the runtime scaling factor of the result vector type. For
12566 // fixed-width result vectors, that runtime scaling factor is 1.
12569 return std::make_pair(Lo, Hi);
12570}
12571
12572std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
12573 const SDLoc &DL) {
12574 // Split the vector length parameter.
12575 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
12576 EVT VT = N.getValueType();
12578 "Expecting the mask to be an evenly-sized vector");
12579 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
12580 SDValue HalfNumElts =
12581 VecVT.isFixedLengthVector()
12582 ? getConstant(HalfMinNumElts, DL, VT)
12583 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
12584 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
12585 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
12586 return std::make_pair(Lo, Hi);
12587}
12588
12589/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
12591 EVT VT = N.getValueType();
12594 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
12596}
12597
12600 unsigned Start, unsigned Count,
12601 EVT EltVT) {
12602 EVT VT = Op.getValueType();
12603 if (Count == 0)
12604 Count = VT.getVectorNumElements();
12605 if (EltVT == EVT())
12606 EltVT = VT.getVectorElementType();
12607 SDLoc SL(Op);
12608 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
12609 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
12610 getVectorIdxConstant(i, SL)));
12611 }
12612}
12613
12614// getAddressSpace - Return the address space this GlobalAddress belongs to.
12616 return getGlobal()->getType()->getAddressSpace();
12617}
12618
12621 return Val.MachineCPVal->getType();
12622 return Val.ConstVal->getType();
12623}
12624
12625bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
12626 unsigned &SplatBitSize,
12627 bool &HasAnyUndefs,
12628 unsigned MinSplatBits,
12629 bool IsBigEndian) const {
12630 EVT VT = getValueType(0);
12631 assert(VT.isVector() && "Expected a vector type");
12632 unsigned VecWidth = VT.getSizeInBits();
12633 if (MinSplatBits > VecWidth)
12634 return false;
12635
12636 // FIXME: The widths are based on this node's type, but build vectors can
12637 // truncate their operands.
12638 SplatValue = APInt(VecWidth, 0);
12639 SplatUndef = APInt(VecWidth, 0);
12640
12641 // Get the bits. Bits with undefined values (when the corresponding element
12642 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
12643 // in SplatValue. If any of the values are not constant, give up and return
12644 // false.
12645 unsigned int NumOps = getNumOperands();
12646 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
12647 unsigned EltWidth = VT.getScalarSizeInBits();
12648
12649 for (unsigned j = 0; j < NumOps; ++j) {
12650 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
12651 SDValue OpVal = getOperand(i);
12652 unsigned BitPos = j * EltWidth;
12653
12654 if (OpVal.isUndef())
12655 SplatUndef.setBits(BitPos, BitPos + EltWidth);
12656 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
12657 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
12658 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
12659 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
12660 else
12661 return false;
12662 }
12663
12664 // The build_vector is all constants or undefs. Find the smallest element
12665 // size that splats the vector.
12666 HasAnyUndefs = (SplatUndef != 0);
12667
12668 // FIXME: This does not work for vectors with elements less than 8 bits.
12669 while (VecWidth > 8) {
12670 // If we can't split in half, stop here.
12671 if (VecWidth & 1)
12672 break;
12673
12674 unsigned HalfSize = VecWidth / 2;
12675 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
12676 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
12677 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
12678 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
12679
12680 // If the two halves do not match (ignoring undef bits), stop here.
12681 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
12682 MinSplatBits > HalfSize)
12683 break;
12684
12685 SplatValue = HighValue | LowValue;
12686 SplatUndef = HighUndef & LowUndef;
12687
12688 VecWidth = HalfSize;
12689 }
12690
12691 // FIXME: The loop above only tries to split in halves. But if the input
12692 // vector for example is <3 x i16> it wouldn't be able to detect a
12693 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
12694 // optimizations. I guess that back in the days when this helper was created
12695 // vectors normally was power-of-2 sized.
12696
12697 SplatBitSize = VecWidth;
12698 return true;
12699}
12700
12702 BitVector *UndefElements) const {
12703 unsigned NumOps = getNumOperands();
12704 if (UndefElements) {
12705 UndefElements->clear();
12706 UndefElements->resize(NumOps);
12707 }
12708 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12709 if (!DemandedElts)
12710 return SDValue();
12711 SDValue Splatted;
12712 for (unsigned i = 0; i != NumOps; ++i) {
12713 if (!DemandedElts[i])
12714 continue;
12715 SDValue Op = getOperand(i);
12716 if (Op.isUndef()) {
12717 if (UndefElements)
12718 (*UndefElements)[i] = true;
12719 } else if (!Splatted) {
12720 Splatted = Op;
12721 } else if (Splatted != Op) {
12722 return SDValue();
12723 }
12724 }
12725
12726 if (!Splatted) {
12727 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
12728 assert(getOperand(FirstDemandedIdx).isUndef() &&
12729 "Can only have a splat without a constant for all undefs.");
12730 return getOperand(FirstDemandedIdx);
12731 }
12732
12733 return Splatted;
12734}
12735
12737 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12738 return getSplatValue(DemandedElts, UndefElements);
12739}
12740
12742 SmallVectorImpl<SDValue> &Sequence,
12743 BitVector *UndefElements) const {
12744 unsigned NumOps = getNumOperands();
12745 Sequence.clear();
12746 if (UndefElements) {
12747 UndefElements->clear();
12748 UndefElements->resize(NumOps);
12749 }
12750 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12751 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
12752 return false;
12753
12754 // Set the undefs even if we don't find a sequence (like getSplatValue).
12755 if (UndefElements)
12756 for (unsigned I = 0; I != NumOps; ++I)
12757 if (DemandedElts[I] && getOperand(I).isUndef())
12758 (*UndefElements)[I] = true;
12759
12760 // Iteratively widen the sequence length looking for repetitions.
12761 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
12762 Sequence.append(SeqLen, SDValue());
12763 for (unsigned I = 0; I != NumOps; ++I) {
12764 if (!DemandedElts[I])
12765 continue;
12766 SDValue &SeqOp = Sequence[I % SeqLen];
12768 if (Op.isUndef()) {
12769 if (!SeqOp)
12770 SeqOp = Op;
12771 continue;
12772 }
12773 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
12774 Sequence.clear();
12775 break;
12776 }
12777 SeqOp = Op;
12778 }
12779 if (!Sequence.empty())
12780 return true;
12781 }
12782
12783 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
12784 return false;
12785}
12786
12788 BitVector *UndefElements) const {
12789 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12790 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
12791}
12792
12795 BitVector *UndefElements) const {
12796 return dyn_cast_or_null<ConstantSDNode>(
12797 getSplatValue(DemandedElts, UndefElements));
12798}
12799
12802 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
12803}
12804
12807 BitVector *UndefElements) const {
12808 return dyn_cast_or_null<ConstantFPSDNode>(
12809 getSplatValue(DemandedElts, UndefElements));
12810}
12811
12814 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
12815}
12816
12817int32_t
12819 uint32_t BitWidth) const {
12820 if (ConstantFPSDNode *CN =
12821 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
12822 bool IsExact;
12823 APSInt IntVal(BitWidth);
12824 const APFloat &APF = CN->getValueAPF();
12825 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
12826 APFloat::opOK ||
12827 !IsExact)
12828 return -1;
12829
12830 return IntVal.exactLogBase2();
12831 }
12832 return -1;
12833}
12834
12836 bool IsLittleEndian, unsigned DstEltSizeInBits,
12837 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
12838 // Early-out if this contains anything but Undef/Constant/ConstantFP.
12839 if (!isConstant())
12840 return false;
12841
12842 unsigned NumSrcOps = getNumOperands();
12843 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
12844 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12845 "Invalid bitcast scale");
12846
12847 // Extract raw src bits.
12848 SmallVector<APInt> SrcBitElements(NumSrcOps,
12849 APInt::getZero(SrcEltSizeInBits));
12850 BitVector SrcUndeElements(NumSrcOps, false);
12851
12852 for (unsigned I = 0; I != NumSrcOps; ++I) {
12854 if (Op.isUndef()) {
12855 SrcUndeElements.set(I);
12856 continue;
12857 }
12858 auto *CInt = dyn_cast<ConstantSDNode>(Op);
12859 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
12860 assert((CInt || CFP) && "Unknown constant");
12861 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
12862 : CFP->getValueAPF().bitcastToAPInt();
12863 }
12864
12865 // Recast to dst width.
12866 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
12867 SrcBitElements, UndefElements, SrcUndeElements);
12868 return true;
12869}
12870
12871void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
12872 unsigned DstEltSizeInBits,
12873 SmallVectorImpl<APInt> &DstBitElements,
12874 ArrayRef<APInt> SrcBitElements,
12875 BitVector &DstUndefElements,
12876 const BitVector &SrcUndefElements) {
12877 unsigned NumSrcOps = SrcBitElements.size();
12878 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
12879 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12880 "Invalid bitcast scale");
12881 assert(NumSrcOps == SrcUndefElements.size() &&
12882 "Vector size mismatch");
12883
12884 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
12885 DstUndefElements.clear();
12886 DstUndefElements.resize(NumDstOps, false);
12887 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
12888
12889 // Concatenate src elements constant bits together into dst element.
12890 if (SrcEltSizeInBits <= DstEltSizeInBits) {
12891 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
12892 for (unsigned I = 0; I != NumDstOps; ++I) {
12893 DstUndefElements.set(I);
12894 APInt &DstBits = DstBitElements[I];
12895 for (unsigned J = 0; J != Scale; ++J) {
12896 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
12897 if (SrcUndefElements[Idx])
12898 continue;
12899 DstUndefElements.reset(I);
12900 const APInt &SrcBits = SrcBitElements[Idx];
12901 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
12902 "Illegal constant bitwidths");
12903 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
12904 }
12905 }
12906 return;
12907 }
12908
12909 // Split src element constant bits into dst elements.
12910 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
12911 for (unsigned I = 0; I != NumSrcOps; ++I) {
12912 if (SrcUndefElements[I]) {
12913 DstUndefElements.set(I * Scale, (I + 1) * Scale);
12914 continue;
12915 }
12916 const APInt &SrcBits = SrcBitElements[I];
12917 for (unsigned J = 0; J != Scale; ++J) {
12918 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
12919 APInt &DstBits = DstBitElements[Idx];
12920 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
12921 }
12922 }
12923}
12924
12926 for (const SDValue &Op : op_values()) {
12927 unsigned Opc = Op.getOpcode();
12928 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
12929 return false;
12930 }
12931 return true;
12932}
12933
12934std::optional<std::pair<APInt, APInt>>
12936 unsigned NumOps = getNumOperands();
12937 if (NumOps < 2)
12938 return std::nullopt;
12939
12940 if (!isa<ConstantSDNode>(getOperand(0)) ||
12941 !isa<ConstantSDNode>(getOperand(1)))
12942 return std::nullopt;
12943
12944 unsigned EltSize = getValueType(0).getScalarSizeInBits();
12945 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
12946 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
12947
12948 if (Stride.isZero())
12949 return std::nullopt;
12950
12951 for (unsigned i = 2; i < NumOps; ++i) {
12952 if (!isa<ConstantSDNode>(getOperand(i)))
12953 return std::nullopt;
12954
12955 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
12956 if (Val != (Start + (Stride * i)))
12957 return std::nullopt;
12958 }
12959
12960 return std::make_pair(Start, Stride);
12961}
12962
12963bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
12964 // Find the first non-undef value in the shuffle mask.
12965 unsigned i, e;
12966 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
12967 /* search */;
12968
12969 // If all elements are undefined, this shuffle can be considered a splat
12970 // (although it should eventually get simplified away completely).
12971 if (i == e)
12972 return true;
12973
12974 // Make sure all remaining elements are either undef or the same as the first
12975 // non-undef value.
12976 for (int Idx = Mask[i]; i != e; ++i)
12977 if (Mask[i] >= 0 && Mask[i] != Idx)
12978 return false;
12979 return true;
12980}
12981
12982// Returns the SDNode if it is a constant integer BuildVector
12983// or constant integer.
12985 if (isa<ConstantSDNode>(N))
12986 return N.getNode();
12988 return N.getNode();
12989 // Treat a GlobalAddress supporting constant offset folding as a
12990 // constant integer.
12991 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
12992 if (GA->getOpcode() == ISD::GlobalAddress &&
12993 TLI->isOffsetFoldingLegal(GA))
12994 return GA;
12995 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
12996 isa<ConstantSDNode>(N.getOperand(0)))
12997 return N.getNode();
12998 return nullptr;
12999}
13000
13001// Returns the SDNode if it is a constant float BuildVector
13002// or constant float.
13004 if (isa<ConstantFPSDNode>(N))
13005 return N.getNode();
13006
13008 return N.getNode();
13009
13010 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13011 isa<ConstantFPSDNode>(N.getOperand(0)))
13012 return N.getNode();
13013
13014 return nullptr;
13015}
13016
13017void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
13018 assert(!Node->OperandList && "Node already has operands");
13020 "too many operands to fit into SDNode");
13021 SDUse *Ops = OperandRecycler.allocate(
13022 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
13023
13024 bool IsDivergent = false;
13025 for (unsigned I = 0; I != Vals.size(); ++I) {
13026 Ops[I].setUser(Node);
13027 Ops[I].setInitial(Vals[I]);
13028 if (Ops[I].Val.getValueType() != MVT::Other) // Skip Chain. It does not carry divergence.
13029 IsDivergent |= Ops[I].getNode()->isDivergent();
13030 }
13031 Node->NumOperands = Vals.size();
13032 Node->OperandList = Ops;
13033 if (!TLI->isSDNodeAlwaysUniform(Node)) {
13034 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
13035 Node->SDNodeBits.IsDivergent = IsDivergent;
13036 }
13038}
13039
13042 size_t Limit = SDNode::getMaxNumOperands();
13043 while (Vals.size() > Limit) {
13044 unsigned SliceIdx = Vals.size() - Limit;
13045 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
13046 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
13047 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
13048 Vals.emplace_back(NewTF);
13049 }
13050 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
13051}
13052
13054 EVT VT, SDNodeFlags Flags) {
13055 switch (Opcode) {
13056 default:
13057 return SDValue();
13058 case ISD::ADD:
13059 case ISD::OR:
13060 case ISD::XOR:
13061 case ISD::UMAX:
13062 return getConstant(0, DL, VT);
13063 case ISD::MUL:
13064 return getConstant(1, DL, VT);
13065 case ISD::AND:
13066 case ISD::UMIN:
13067 return getAllOnesConstant(DL, VT);
13068 case ISD::SMAX:
13070 case ISD::SMIN:
13072 case ISD::FADD:
13073 return getConstantFP(-0.0, DL, VT);
13074 case ISD::FMUL:
13075 return getConstantFP(1.0, DL, VT);
13076 case ISD::FMINNUM:
13077 case ISD::FMAXNUM: {
13078 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13079 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
13080 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
13081 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
13082 APFloat::getLargest(Semantics);
13083 if (Opcode == ISD::FMAXNUM)
13084 NeutralAF.changeSign();
13085
13086 return getConstantFP(NeutralAF, DL, VT);
13087 }
13088 case ISD::FMINIMUM:
13089 case ISD::FMAXIMUM: {
13090 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
13091 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
13092 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
13093 : APFloat::getLargest(Semantics);
13094 if (Opcode == ISD::FMAXIMUM)
13095 NeutralAF.changeSign();
13096
13097 return getConstantFP(NeutralAF, DL, VT);
13098 }
13099
13100 }
13101}
13102
13103/// Helper used to make a call to a library function that has one argument of
13104/// pointer type.
13105///
13106/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
13107/// used to get or set floating-point state. They have one argument of pointer
13108/// type, which points to the memory region containing bits of the
13109/// floating-point state. The value returned by such function is ignored in the
13110/// created call.
13111///
13112/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
13113/// \param Ptr Pointer used to save/load state.
13114/// \param InChain Ingoing token chain.
13115/// \returns Outgoing chain token.
13117 SDValue InChain,
13118 const SDLoc &DLoc) {
13119 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
13122 Entry.Node = Ptr;
13123 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
13124 Args.push_back(Entry);
13125 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
13126 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
13127 TLI->getPointerTy(getDataLayout()));
13129 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
13130 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
13131 std::move(Args));
13132 return TLI->LowerCallTo(CLI).second;
13133}
13134
13136 assert(From && To && "Invalid SDNode; empty source SDValue?");
13137 auto I = SDEI.find(From);
13138 if (I == SDEI.end())
13139 return;
13140
13141 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
13142 // the iterator, hence the need to make a copy to prevent a use-after-free.
13143 NodeExtraInfo NEI = I->second;
13144 if (LLVM_LIKELY(!NEI.PCSections) && LLVM_LIKELY(!NEI.MMRA)) {
13145 // No deep copy required for the types of extra info set.
13146 //
13147 // FIXME: Investigate if other types of extra info also need deep copy. This
13148 // depends on the types of nodes they can be attached to: if some extra info
13149 // is only ever attached to nodes where a replacement To node is always the
13150 // node where later use and propagation of the extra info has the intended
13151 // semantics, no deep copy is required.
13152 SDEI[To] = std::move(NEI);
13153 return;
13154 }
13155
13156 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
13157 // through the replacement of From with To. Otherwise, replacements of a node
13158 // (From) with more complex nodes (To and its operands) may result in lost
13159 // extra info where the root node (To) is insignificant in further propagating
13160 // and using extra info when further lowering to MIR.
13161 //
13162 // In the first step pre-populate the visited set with the nodes reachable
13163 // from the old From node. This avoids copying NodeExtraInfo to parts of the
13164 // DAG that is not new and should be left untouched.
13165 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
13166 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
13167 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
13168 if (MaxDepth == 0) {
13169 // Remember this node in case we need to increase MaxDepth and continue
13170 // populating FromReach from this node.
13171 Leafs.emplace_back(N);
13172 return;
13173 }
13174 if (!FromReach.insert(N).second)
13175 return;
13176 for (const SDValue &Op : N->op_values())
13177 Self(Self, Op.getNode(), MaxDepth - 1);
13178 };
13179
13180 // Copy extra info to To and all its transitive operands (that are new).
13182 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
13183 if (FromReach.contains(N))
13184 return true;
13185 if (!Visited.insert(N).second)
13186 return true;
13187 if (getEntryNode().getNode() == N)
13188 return false;
13189 for (const SDValue &Op : N->op_values()) {
13190 if (!Self(Self, Op.getNode()))
13191 return false;
13192 }
13193 // Copy only if entry node was not reached.
13194 SDEI[N] = NEI;
13195 return true;
13196 };
13197
13198 // We first try with a lower MaxDepth, assuming that the path to common
13199 // operands between From and To is relatively short. This significantly
13200 // improves performance in the common case. The initial MaxDepth is big
13201 // enough to avoid retry in the common case; the last MaxDepth is large
13202 // enough to avoid having to use the fallback below (and protects from
13203 // potential stack exhaustion from recursion).
13204 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13205 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13206 // StartFrom is the previous (or initial) set of leafs reachable at the
13207 // previous maximum depth.
13209 std::swap(StartFrom, Leafs);
13210 for (const SDNode *N : StartFrom)
13211 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13212 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13213 return;
13214 // This should happen very rarely (reached the entry node).
13215 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13216 assert(!Leafs.empty());
13217 }
13218
13219 // This should not happen - but if it did, that means the subgraph reachable
13220 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13221 // could not visit all reachable common operands. Consequently, we were able
13222 // to reach the entry node.
13223 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13224 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13225 // Best-effort fallback if assertions disabled.
13226 SDEI[To] = std::move(NEI);
13227}
13228
13229#ifndef NDEBUG
13230static void checkForCyclesHelper(const SDNode *N,
13233 const llvm::SelectionDAG *DAG) {
13234 // If this node has already been checked, don't check it again.
13235 if (Checked.count(N))
13236 return;
13237
13238 // If a node has already been visited on this depth-first walk, reject it as
13239 // a cycle.
13240 if (!Visited.insert(N).second) {
13241 errs() << "Detected cycle in SelectionDAG\n";
13242 dbgs() << "Offending node:\n";
13243 N->dumprFull(DAG); dbgs() << "\n";
13244 abort();
13245 }
13246
13247 for (const SDValue &Op : N->op_values())
13248 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13249
13250 Checked.insert(N);
13251 Visited.erase(N);
13252}
13253#endif
13254
13256 const llvm::SelectionDAG *DAG,
13257 bool force) {
13258#ifndef NDEBUG
13259 bool check = force;
13260#ifdef EXPENSIVE_CHECKS
13261 check = true;
13262#endif // EXPENSIVE_CHECKS
13263 if (check) {
13264 assert(N && "Checking nonexistent SDNode");
13267 checkForCyclesHelper(N, visited, checked, DAG);
13268 }
13269#endif // !NDEBUG
13270}
13271
13272void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13273 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13274}
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:1020
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1113
void copySign(const APFloat &RHS)
Definition: APFloat.h:1207
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5282
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1095
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:1331
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1086
bool isFinite() const
Definition: APFloat.h:1353
bool isNaN() const
Definition: APFloat.h:1346
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1237
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1104
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1140
bool isZero() const
Definition: APFloat.h:1344
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1038
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1229
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:998
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1131
bool isPosZero() const
Definition: APFloat.h:1359
bool isNegZero() const
Definition: APFloat.h:1360
void changeSign()
Definition: APFloat.h:1202
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:1009
bool isInfinity() const
Definition: APFloat.h:1345
Class for arbitrary precision integers.
Definition: APInt.h:77
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:213
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1386
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:208
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1499
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1371
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1628
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1365
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:1471
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:1309
APInt abs() const
Get the absolute value.
Definition: APInt.h:1752
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:350
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1161
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:359
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:1447
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1090
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:188
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:308
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:1376
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:813
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1145
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1597
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1586
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1556
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:198
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:1396
unsigned logBase2() const
Definition: APInt.h:1718
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:806
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1298
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:313
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1129
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:1346
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:852
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:1236
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:419
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:285
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:179
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1368
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:1216
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:265
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:218
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:837
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:830
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1200
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:890
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:269
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:149
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:146
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:1686
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:327
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:698
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:695
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:350
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:41
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:161
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:190
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:196
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:382
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:227
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 getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
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:565
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 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:488
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 cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
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.
std::optional< uint64_t > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
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:492
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:451
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:390
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:545
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:842
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 getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
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:486
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.
SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
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:673
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.
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:876
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 FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
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:487
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:557
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:553
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:690
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:481
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.
std::optional< uint64_t > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
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:859
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.
bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
std::optional< uint64_t > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVMContext * getContext() const
Definition: SelectionDAG.h:499
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:574
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.
std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
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:568
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:892
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:548
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:323
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:361
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:412
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:344
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:479
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:77
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:558
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:661
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:679
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:2218
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:2213
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:764
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:243
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:737
@ TargetConstantPool
Definition: ISDOpcodes.h:174
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
Definition: ISDOpcodes.h:1192
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:484
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1297
@ 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:1360
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1391
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:505
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:257
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1290
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:567
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1081
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:728
@ TargetBlockAddress
Definition: ISDOpcodes.h:176
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1292
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1262
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1293
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:276
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition: ISDOpcodes.h:495
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:246
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1052
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:797
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:491
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:205
@ 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:1275
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:804
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:551
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1376
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:397
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1380
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:702
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:834
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1390
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:485
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:927
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1288
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:917
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:236
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1289
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:954
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1431
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1295
@ 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:1188
@ 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:1123
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:788
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:670
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:628
@ TargetExternalSymbol
Definition: ISDOpcodes.h:175
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1373
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:736
@ TargetJumpTable
Definition: ISDOpcodes.h:173
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition: ISDOpcodes.h:183
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1242
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1377
@ 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:772
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:944
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1296
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1098
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:334
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1291
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:654
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:508
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:356
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:741
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1258
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:218
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1392
@ 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:229
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition: ISDOpcodes.h:635
@ 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:1298
@ 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:215
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:330
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition: ISDOpcodes.h:170
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1385
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:659
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:719
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1287
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:608
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1286
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:581
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:999
@ 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:543
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:794
@ TargetConstantFP
Definition: ISDOpcodes.h:165
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:870
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:756
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1350
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1269
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1294
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:986
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:366
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:338
@ TargetFrameIndex
Definition: ISDOpcodes.h:172
@ 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:823
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:812
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:682
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1325
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:902
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:750
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:310
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1212
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1393
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:936
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1300
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1284
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:473
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1005
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1285
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:850
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition: ISDOpcodes.h:164
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:478
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:694
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:190
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1028
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1345
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:665
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1374
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:286
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition: ISDOpcodes.h:644
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition: ISDOpcodes.h:532
@ 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:620
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1283
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:959
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:883
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:845
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:921
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Definition: ISDOpcodes.h:1422
@ 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:869
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1381
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:800
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1167
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1361
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:777
@ 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:1299
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:501
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:347
@ AssertZext
Definition: ISDOpcodes.h:62
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1033
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:320
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:198
@ TargetGlobalTLSAddress
Definition: ISDOpcodes.h:171
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition: ISDOpcodes.h:523
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:1443
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:1612
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:1617
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:1516
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:1503
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:1554
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:1534
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:893
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:443
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:147
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:480
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:1540
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:2400
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 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:1502
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2067
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
MaybeAlign getAlign(const Function &F, unsigned Index)
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:1522
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:1426
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:1463
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:167
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:1449
@ Mul
Product of integers.
bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
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:509
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:1476
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:271
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:236
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:249
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:246
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:250
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:273
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:272
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:269
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:248
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:270
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:262
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:497
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:203
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:290
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:149
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:902
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:244
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:202
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:97
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:76
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:113
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:762
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:231
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:428
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1042
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:62
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:263
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:496
void makeNegative()
Make this value negative.
Definition: KnownBits.h:108
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:150
KnownBits byteSwap() const
Definition: KnownBits.h:456
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:278
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:82
KnownBits reverseBits() const
Definition: KnownBits.h:460
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:222
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:178
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:161
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:70
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:310
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:370
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:100
static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
Definition: KnownBits.cpp:228
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:214
static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
Definition: KnownBits.cpp:782
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:300
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:169
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:137
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:185
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:134
static KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:247
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:215
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:894
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1059
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1002
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:51
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:103
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:946
static KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
Definition: KnownBits.cpp:777
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:315
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:94
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:44
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:269
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:208
static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
Definition: KnownBits.cpp:792
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:797
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:156
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:550
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:285
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:196
static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
Definition: KnownBits.cpp:787
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:310
DAGUpdateListener *const Next
Definition: SelectionDAG.h:311
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)